@Test public void testInjectAtTheBeginning() throws Exception { assertEquals( "<!DOCTYPE html><html><head><injected/><title>Title</title></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<!DOCTYPE html><html><head><title>Title</title></head><body>Hello, World!</body></html>" )); assertEquals( "<!DOCTYPE html><html><injected/><title>Title</title></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<!DOCTYPE html><html><title>Title</title></head><body>Hello, World!</body></html>" )); assertEquals( "<!DOCTYPE html><injected/><title>Title</title></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<!DOCTYPE html><title>Title</title></head><body>Hello, World!</body></html>" )); assertEquals( "<title>Title</title><injected/><script>alert();</script></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<title>Title</title><script>alert();</script></head><body>Hello, World!</body></html>" )); assertEquals( "<!DOCTYPE html><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" /><injected/><title>Title</title></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<!DOCTYPE html><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" /><title>Title</title></head><body>Hello, World!</body></html>" )); assertEquals( "<!DOCTYPE html><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" ><injected/><title>Title</title></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<!DOCTYPE html><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" ><title>Title</title></head><body>Hello, World!</body></html>" )); assertEquals( "<!DOCTYPE html><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" ></meta><injected/><title>Title</title></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<!DOCTYPE html><html><head><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" ></meta><title>Title</title></head><body>Hello, World!</body></html>" )); assertEquals( "<!DOCTYPE html><html><head><injected/><script> </script><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" /><title>Title</title></head><body>Hello, World!</body></html>", injectAtTheBeginning( "<!DOCTYPE html><html><head><script> </script><meta http-equiv=\"X-UA-Compatible\" content=\"IE=9; IE=8; IE=7; IE=EDGE\" /><title>Title</title></head><body>Hello, World!</body></html>" )); }
public void injectAtTheBeginning(String content) throws IOException { String str = new String(buffer.leadingBytes(16 * 1024), characterEncoding).toLowerCase(); StringBuilder sb = new StringBuilder(str); int afterHtml = sb.indexOf("<html"); if (afterHtml != -1) { afterHtml = sb.indexOf(">", afterHtml) + 1; if (0 == afterHtml) afterHtml = -1; } int afterHead = sb.indexOf("<head"); if (afterHead != -1) { afterHead = sb.indexOf(">", afterHead) + 1; if (0 == afterHead) afterHead = -1; } int afterDocType = sb.indexOf("<!doctype"); if (afterDocType != -1) { afterDocType = sb.indexOf(">", afterDocType) + 1; if (0 == afterDocType) afterDocType = -1; } int beforeScript = sb.indexOf("<script"); int beforeBase = sb.indexOf("<base"); int beforeScriptOrBase = beforeScript >= 0 && beforeBase >= 0 ? Math.min(beforeScript, beforeBase) : beforeScript >= 0 && beforeBase < 0 ? beforeScript : beforeBase >= 0 && beforeScript < 0 ? beforeBase : -1; String contentBeforeScript = -1 == beforeScriptOrBase ? sb.toString() : sb.substring(0, beforeScriptOrBase); int lastMetaOpeningTag = contentBeforeScript.lastIndexOf("<meta"); int lastMetaClosingTag = contentBeforeScript.lastIndexOf("</meta"); int afterLastMeta = -1; if (lastMetaOpeningTag != -1 || lastMetaClosingTag != -1) { afterLastMeta = contentBeforeScript.indexOf(">", Math.max(lastMetaOpeningTag, lastMetaClosingTag)) + 1; if (0 == afterLastMeta) afterLastMeta = -1; } int i; if (-1 != afterLastMeta) { i = afterLastMeta; } else if (-1 != afterHead && (-1 == beforeScriptOrBase || afterHead <= beforeScriptOrBase)) { i = afterHead; } else if (-1 != afterHtml && (-1 == beforeScriptOrBase || afterHtml <= beforeScriptOrBase)) { i = afterHtml; } else if (-1 != afterDocType && (-1 == beforeScriptOrBase || afterDocType < beforeScriptOrBase)) { i = afterDocType; } else if (-1 != beforeScriptOrBase) { i = beforeScriptOrBase; } else { i = 0; } int offset = str.substring(0, i).getBytes(characterEncoding).length; buffer.insertAt(offset, content.getBytes(characterEncoding)); }
HtmlInjector { public void injectAtTheBeginning(String content) throws IOException { String str = new String(buffer.leadingBytes(16 * 1024), characterEncoding).toLowerCase(); StringBuilder sb = new StringBuilder(str); int afterHtml = sb.indexOf("<html"); if (afterHtml != -1) { afterHtml = sb.indexOf(">", afterHtml) + 1; if (0 == afterHtml) afterHtml = -1; } int afterHead = sb.indexOf("<head"); if (afterHead != -1) { afterHead = sb.indexOf(">", afterHead) + 1; if (0 == afterHead) afterHead = -1; } int afterDocType = sb.indexOf("<!doctype"); if (afterDocType != -1) { afterDocType = sb.indexOf(">", afterDocType) + 1; if (0 == afterDocType) afterDocType = -1; } int beforeScript = sb.indexOf("<script"); int beforeBase = sb.indexOf("<base"); int beforeScriptOrBase = beforeScript >= 0 && beforeBase >= 0 ? Math.min(beforeScript, beforeBase) : beforeScript >= 0 && beforeBase < 0 ? beforeScript : beforeBase >= 0 && beforeScript < 0 ? beforeBase : -1; String contentBeforeScript = -1 == beforeScriptOrBase ? sb.toString() : sb.substring(0, beforeScriptOrBase); int lastMetaOpeningTag = contentBeforeScript.lastIndexOf("<meta"); int lastMetaClosingTag = contentBeforeScript.lastIndexOf("</meta"); int afterLastMeta = -1; if (lastMetaOpeningTag != -1 || lastMetaClosingTag != -1) { afterLastMeta = contentBeforeScript.indexOf(">", Math.max(lastMetaOpeningTag, lastMetaClosingTag)) + 1; if (0 == afterLastMeta) afterLastMeta = -1; } int i; if (-1 != afterLastMeta) { i = afterLastMeta; } else if (-1 != afterHead && (-1 == beforeScriptOrBase || afterHead <= beforeScriptOrBase)) { i = afterHead; } else if (-1 != afterHtml && (-1 == beforeScriptOrBase || afterHtml <= beforeScriptOrBase)) { i = afterHtml; } else if (-1 != afterDocType && (-1 == beforeScriptOrBase || afterDocType < beforeScriptOrBase)) { i = afterDocType; } else if (-1 != beforeScriptOrBase) { i = beforeScriptOrBase; } else { i = 0; } int offset = str.substring(0, i).getBytes(characterEncoding).length; buffer.insertAt(offset, content.getBytes(characterEncoding)); } }
HtmlInjector { public void injectAtTheBeginning(String content) throws IOException { String str = new String(buffer.leadingBytes(16 * 1024), characterEncoding).toLowerCase(); StringBuilder sb = new StringBuilder(str); int afterHtml = sb.indexOf("<html"); if (afterHtml != -1) { afterHtml = sb.indexOf(">", afterHtml) + 1; if (0 == afterHtml) afterHtml = -1; } int afterHead = sb.indexOf("<head"); if (afterHead != -1) { afterHead = sb.indexOf(">", afterHead) + 1; if (0 == afterHead) afterHead = -1; } int afterDocType = sb.indexOf("<!doctype"); if (afterDocType != -1) { afterDocType = sb.indexOf(">", afterDocType) + 1; if (0 == afterDocType) afterDocType = -1; } int beforeScript = sb.indexOf("<script"); int beforeBase = sb.indexOf("<base"); int beforeScriptOrBase = beforeScript >= 0 && beforeBase >= 0 ? Math.min(beforeScript, beforeBase) : beforeScript >= 0 && beforeBase < 0 ? beforeScript : beforeBase >= 0 && beforeScript < 0 ? beforeBase : -1; String contentBeforeScript = -1 == beforeScriptOrBase ? sb.toString() : sb.substring(0, beforeScriptOrBase); int lastMetaOpeningTag = contentBeforeScript.lastIndexOf("<meta"); int lastMetaClosingTag = contentBeforeScript.lastIndexOf("</meta"); int afterLastMeta = -1; if (lastMetaOpeningTag != -1 || lastMetaClosingTag != -1) { afterLastMeta = contentBeforeScript.indexOf(">", Math.max(lastMetaOpeningTag, lastMetaClosingTag)) + 1; if (0 == afterLastMeta) afterLastMeta = -1; } int i; if (-1 != afterLastMeta) { i = afterLastMeta; } else if (-1 != afterHead && (-1 == beforeScriptOrBase || afterHead <= beforeScriptOrBase)) { i = afterHead; } else if (-1 != afterHtml && (-1 == beforeScriptOrBase || afterHtml <= beforeScriptOrBase)) { i = afterHtml; } else if (-1 != afterDocType && (-1 == beforeScriptOrBase || afterDocType < beforeScriptOrBase)) { i = afterDocType; } else if (-1 != beforeScriptOrBase) { i = beforeScriptOrBase; } else { i = 0; } int offset = str.substring(0, i).getBytes(characterEncoding).length; buffer.insertAt(offset, content.getBytes(characterEncoding)); } HtmlInjector(Buffer buffer);  HtmlInjector(Buffer buffer, String characterEncoding); }
HtmlInjector { public void injectAtTheBeginning(String content) throws IOException { String str = new String(buffer.leadingBytes(16 * 1024), characterEncoding).toLowerCase(); StringBuilder sb = new StringBuilder(str); int afterHtml = sb.indexOf("<html"); if (afterHtml != -1) { afterHtml = sb.indexOf(">", afterHtml) + 1; if (0 == afterHtml) afterHtml = -1; } int afterHead = sb.indexOf("<head"); if (afterHead != -1) { afterHead = sb.indexOf(">", afterHead) + 1; if (0 == afterHead) afterHead = -1; } int afterDocType = sb.indexOf("<!doctype"); if (afterDocType != -1) { afterDocType = sb.indexOf(">", afterDocType) + 1; if (0 == afterDocType) afterDocType = -1; } int beforeScript = sb.indexOf("<script"); int beforeBase = sb.indexOf("<base"); int beforeScriptOrBase = beforeScript >= 0 && beforeBase >= 0 ? Math.min(beforeScript, beforeBase) : beforeScript >= 0 && beforeBase < 0 ? beforeScript : beforeBase >= 0 && beforeScript < 0 ? beforeBase : -1; String contentBeforeScript = -1 == beforeScriptOrBase ? sb.toString() : sb.substring(0, beforeScriptOrBase); int lastMetaOpeningTag = contentBeforeScript.lastIndexOf("<meta"); int lastMetaClosingTag = contentBeforeScript.lastIndexOf("</meta"); int afterLastMeta = -1; if (lastMetaOpeningTag != -1 || lastMetaClosingTag != -1) { afterLastMeta = contentBeforeScript.indexOf(">", Math.max(lastMetaOpeningTag, lastMetaClosingTag)) + 1; if (0 == afterLastMeta) afterLastMeta = -1; } int i; if (-1 != afterLastMeta) { i = afterLastMeta; } else if (-1 != afterHead && (-1 == beforeScriptOrBase || afterHead <= beforeScriptOrBase)) { i = afterHead; } else if (-1 != afterHtml && (-1 == beforeScriptOrBase || afterHtml <= beforeScriptOrBase)) { i = afterHtml; } else if (-1 != afterDocType && (-1 == beforeScriptOrBase || afterDocType < beforeScriptOrBase)) { i = afterDocType; } else if (-1 != beforeScriptOrBase) { i = beforeScriptOrBase; } else { i = 0; } int offset = str.substring(0, i).getBytes(characterEncoding).length; buffer.insertAt(offset, content.getBytes(characterEncoding)); } HtmlInjector(Buffer buffer);  HtmlInjector(Buffer buffer, String characterEncoding); void injectAtTheEnd(String content); void injectAtTheBeginning(String content); }
HtmlInjector { public void injectAtTheBeginning(String content) throws IOException { String str = new String(buffer.leadingBytes(16 * 1024), characterEncoding).toLowerCase(); StringBuilder sb = new StringBuilder(str); int afterHtml = sb.indexOf("<html"); if (afterHtml != -1) { afterHtml = sb.indexOf(">", afterHtml) + 1; if (0 == afterHtml) afterHtml = -1; } int afterHead = sb.indexOf("<head"); if (afterHead != -1) { afterHead = sb.indexOf(">", afterHead) + 1; if (0 == afterHead) afterHead = -1; } int afterDocType = sb.indexOf("<!doctype"); if (afterDocType != -1) { afterDocType = sb.indexOf(">", afterDocType) + 1; if (0 == afterDocType) afterDocType = -1; } int beforeScript = sb.indexOf("<script"); int beforeBase = sb.indexOf("<base"); int beforeScriptOrBase = beforeScript >= 0 && beforeBase >= 0 ? Math.min(beforeScript, beforeBase) : beforeScript >= 0 && beforeBase < 0 ? beforeScript : beforeBase >= 0 && beforeScript < 0 ? beforeBase : -1; String contentBeforeScript = -1 == beforeScriptOrBase ? sb.toString() : sb.substring(0, beforeScriptOrBase); int lastMetaOpeningTag = contentBeforeScript.lastIndexOf("<meta"); int lastMetaClosingTag = contentBeforeScript.lastIndexOf("</meta"); int afterLastMeta = -1; if (lastMetaOpeningTag != -1 || lastMetaClosingTag != -1) { afterLastMeta = contentBeforeScript.indexOf(">", Math.max(lastMetaOpeningTag, lastMetaClosingTag)) + 1; if (0 == afterLastMeta) afterLastMeta = -1; } int i; if (-1 != afterLastMeta) { i = afterLastMeta; } else if (-1 != afterHead && (-1 == beforeScriptOrBase || afterHead <= beforeScriptOrBase)) { i = afterHead; } else if (-1 != afterHtml && (-1 == beforeScriptOrBase || afterHtml <= beforeScriptOrBase)) { i = afterHtml; } else if (-1 != afterDocType && (-1 == beforeScriptOrBase || afterDocType < beforeScriptOrBase)) { i = afterDocType; } else if (-1 != beforeScriptOrBase) { i = beforeScriptOrBase; } else { i = 0; } int offset = str.substring(0, i).getBytes(characterEncoding).length; buffer.insertAt(offset, content.getBytes(characterEncoding)); } HtmlInjector(Buffer buffer);  HtmlInjector(Buffer buffer, String characterEncoding); void injectAtTheEnd(String content); void injectAtTheBeginning(String content);  }