Skip to content

Commit

Permalink
Handling of Attachments is improved. Now we treat responses only have…
Browse files Browse the repository at this point in the history
…n and Content-Type header of application/octetstream in the same way as if the Content-Disposition header was set to attachment. This is was real browsers also do (#789)
  • Loading branch information
rbri committed May 18, 2024
1 parent 5f85dcd commit 31e8574
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
for the XMLHttpRequest ActiveX object that is no longer there.
</action>

<action type="update" dev="rbri" issue="#789">
Handling of Attachments is improved. Now we treat responses only haven and Content-Type header of application/octetstream
in the same way as if the Content-Disposition header was set to attachment. This is was real browsers also do.
</action>
<action type="update" dev="rbri">
Upgrade commons-logging to 1.3.2
</action>
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/org/htmlunit/attachment/AttachmentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.htmlunit.HttpHeader;
import org.htmlunit.Page;
import org.htmlunit.WebResponse;
import org.htmlunit.util.MimeType;

/**
* <p>A handler for attachments, which represent pages received from the server which contain
Expand Down Expand Up @@ -82,7 +83,12 @@ default boolean handleAttachment(final WebResponse response, final String attach
default boolean isAttachment(final WebResponse response) {
final String disp = response.getResponseHeaderValue(HttpHeader.CONTENT_DISPOSITION);
if (disp == null) {
return false;
// if there is no content disposition header and content type application/octet-stream
// is handled like an attachment by the browsers
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types#applicationoctet-stream
// They treat it as if the Content-Disposition header was set to attachment, and propose a "Save As" dialog.
final String contentType = response.getResponseHeaderValue(HttpHeader.CONTENT_TYPE);
return MimeType.APPLICATION_OCTET_STREAM.equals(contentType.toLowerCase(Locale.ROOT));
}
return disp.toLowerCase(Locale.ROOT).startsWith("attachment");
}
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/org/htmlunit/attachment/AttachmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,64 @@ public void handleAttachment(final Page page, final String attachmentFilename) {

assertEquals("htmlunit.zip", attachmentFilenames.get(0));
}

/**
* Tests attachment callbacks and the contents of attachments.
* @throws Exception if an error occurs
*/
@Test
public void handleResponseOnlyApplicationOctetstream() throws Exception {
final String content1 = "<html><body>\n"
+ "<form method='POST' name='form' action='" + URL_SECOND + "'>\n"
+ "<input type='submit' value='ok'>\n"
+ "</form>\n"
+ "<a href='#' onclick='document.form.submit()'>click me</a>\n"
+ "</body></html>";
final String content2 = "download file contents";

final WebClient client = getWebClient();
final List<WebResponse> attachments = new ArrayList<>();
final List<String> attachmentFilenames = new ArrayList<>();

client.setAttachmentHandler(new AttachmentHandler() {
@Override
public boolean handleAttachment(final WebResponse response, final String attachmentFilename) {
attachments.add(response);
attachmentFilenames.add(attachmentFilename);
return true;
}

@Override
public void handleAttachment(final Page page, final String attachmentFilename) {
throw new IllegalAccessError("handleAttachment(Page, String) called");
}
});

final List<NameValuePair> headers = new ArrayList<>();
headers.add(new NameValuePair("Content-Type", MimeType.APPLICATION_OCTET_STREAM));

final MockWebConnection conn = new MockWebConnection();
conn.setResponse(URL_FIRST, content1);
conn.setResponse(URL_SECOND, content2, 200, "OK", MimeType.TEXT_HTML, headers);
client.setWebConnection(conn);
assertTrue(attachments.isEmpty());
assertTrue(attachmentFilenames.isEmpty());

final HtmlPage result = client.getPage(URL_FIRST);
final HtmlAnchor anchor = result.getAnchors().get(0);
final Page clickResult = anchor.click();
assertEquals(result, clickResult);
assertEquals(1, attachments.size());
assertEquals(1, attachmentFilenames.size());
assertEquals(1, client.getWebWindows().size());

final WebResponse attachmentResponse = attachments.get(0);
final InputStream attachmentStream = attachmentResponse.getContentAsStream();
HttpWebConnectionTest.assertEquals(new ByteArrayInputStream(content2.getBytes()), attachmentStream);
assertEquals(MimeType.APPLICATION_OCTET_STREAM, attachmentResponse.getContentType());
assertEquals(200, attachmentResponse.getStatusCode());
assertEquals(URL_SECOND, attachmentResponse.getWebRequest().getUrl());

assertNull(attachmentFilenames.get(0));
}
}

0 comments on commit 31e8574

Please sign in to comment.