Skip to content
This repository has been archived by the owner on Nov 16, 2018. It is now read-only.

Commit

Permalink
Issue #32 : Fixes getImageURIString to handle images with query param…
Browse files Browse the repository at this point in the history
…eters
  • Loading branch information
sripathikrishnan committed Aug 21, 2011
1 parent 6727493 commit 493501d
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
30 changes: 23 additions & 7 deletions src/net/nczonline/web/cssembed/CSSURLEmbedder.java
Expand Up @@ -320,13 +320,10 @@ public void embedImages(Writer out, String root) throws IOException {
* @return The appropriate data URI to use.
* @throws java.io.IOException
*/
private String getImageURIString(String url, String originalUrl) throws IOException {

//check the extension - only encode for images
String fileType = url.substring(url.lastIndexOf(".") + 1);
String getImageURIString(String url, String originalUrl) throws IOException {

//it's an image, so encode it
if (imageTypes.contains(fileType)){
if (isImage(url)){

DataURIGenerator.setVerbose(verbose);

Expand Down Expand Up @@ -392,8 +389,27 @@ private String getImageURIString(String url, String originalUrl) throws IOExcept
}

}

private String getFilename(String path){

/*
* Detects if the given url represents an image
* This method simply checks the file extension.
* A better way to detect an image is via content type response headers or by content sniffing,
* but both are expensive approaches. We can do without them for now.
*/
static boolean isImage(String url) {
int startPos = url.lastIndexOf(".") + 1;
/*
* Some images are of the form some-image.png?parameter=value
*/
int endPos = url.lastIndexOf("?");
if(endPos == -1 || endPos <= startPos) {
endPos = url.length();
}
String fileType = url.substring(startPos, endPos);
return imageTypes.contains(fileType);
}

private String getFilename(String path){
if (path.indexOf("/") > -1){
return path.substring(path.lastIndexOf("/")+1);
} else if (path.indexOf("\\") > -1){
Expand Down
49 changes: 48 additions & 1 deletion tests/net/nczonline/web/cssembed/CSSURLEmbedderTest.java
Expand Up @@ -196,7 +196,7 @@ public void testAbsoluteLocalFileOverMaxImageSize() throws IOException {

@Test
public void testReadFromAndWriteToSameFile() throws IOException {
String filename = CSSURLEmbedderTest.class.getResource("samefiletest.css").getPath().replace("%20", " ");
String filename = this.getClass().getClassLoader().getResource("samefiletest.css").getPath().replace("%20", " ");
File file = new File(filename);
Reader in = new InputStreamReader(new FileInputStream(file));

Expand Down Expand Up @@ -232,4 +232,51 @@ public void testRegularUrlWithMhtml() throws IOException {
String result = writer.toString();
assertEquals("background: url(folder.txt);", result);
}

@Test
public void testRemoteUrlWithQueryString() throws IOException {
final String expectedUrl = "http://some-http-server.com/image/with/query/parameters/image.png?a=b&c=d";
String code = "background : url(/image/with/query/parameters/image.png?a=b&c=d)";

StringWriter writer = new StringWriter();
embedder = new CSSURLEmbedder(new StringReader(code), CSSURLEmbedder.DATAURI_OPTION, true, 0, 200) {
/*
* Override method to prevent a network call during unit tests
*/
@Override
String getImageURIString(String url, String originalUrl) throws IOException {
if(url.equals("")) {
throw new IllegalArgumentException("Expected URL " + expectedUrl + ", but found " + url);
}
return "data:image/gif;base64,AAAABBBBCCCCDDDD";
}
};
embedder.embedImages(writer, "http://some-http-server.com/");

String result = writer.toString();
assertEquals("background : url(data:image/gif;base64,AAAABBBBCCCCDDDD)", result);
}

@Test
public void testImageDetection() {
String tests[] = {
"file://path/to/image.png",
"http://some.server.com/image.png",
"http://some.server.com/image.png?param=legalvalue&anotherparam=anothervalue",
"http://some.server.com/image.png?param=illegal.value.with.period"
};
boolean expectedImage[] = {
true, true, true, false
};

for(int i=0; i<tests.length; i++) {
if(expectedImage[i]) {
assertTrue("Expected " + tests[i] + " to resolve to an image", CSSURLEmbedder.isImage(tests[i]));
}
else {
assertFalse("Did NOT expect " + tests[i] + " to resolve as an image", CSSURLEmbedder.isImage(tests[i]));
}
}

}
}

0 comments on commit 493501d

Please sign in to comment.