Skip to content

Commit

Permalink
Changelog and visibility for auto multipater
Browse files Browse the repository at this point in the history
  • Loading branch information
jhy committed Dec 27, 2018
1 parent 56ea479 commit 68ff8cb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ jsoup changelog
* Improvement: treat center tags as block tags.
<https://github.com/jhy/jsoup/pull/1113>

* Improvement: allow forms to be submitted with Content-Type=multipart/form-data without requiring a file upload;
automatically set the mime boundary.
<https://github.com/jhy/jsoup/pull/1058>

* Bugfix: when using the tag case preserving parsing settings, certain HTML tree building rules where not followed
for upper case tags.
<https://github.com/jhy/jsoup/issues/1149>
Expand Down
13 changes: 5 additions & 8 deletions src/main/java/org/jsoup/helper/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public class HttpConnection implements Connection {
public static final String DEFAULT_UA =
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36";
private static final String USER_AGENT = "User-Agent";
private static final String CONTENT_TYPE = "Content-Type";
private static final String MULTIPART_FORM_DATA = "multipart/form-data";
private static final String FORM_URL_ENCODED = "application/x-www-form-urlencoded";
public static final String CONTENT_TYPE = "Content-Type";
public static final String MULTIPART_FORM_DATA = "multipart/form-data";
public static final String FORM_URL_ENCODED = "application/x-www-form-urlencoded";
private static final int HTTP_TEMP_REDIR = 307; // http/1.1 temporary redirect, not in Java's set.
private static final String DefaultUploadType = "application/octet-stream";

Expand Down Expand Up @@ -1004,12 +1004,9 @@ private static String setOutputContentType(final Connection.Request req) {
String bound = null;
if (req.hasHeader(CONTENT_TYPE)) {
// no-op; don't add content type as already set (e.g. for requestBody())
// todo - if content type already set, we could add charset or boundary if those aren't included
// todo - if content type already set, we could add charset

// vcoder4c's patch: 17th May 2018
// Description: support for the issue: https://github.com/jhy/jsoup/issues/788
// check whether the boundary has been added when the content type is multipart/form-data
// For the charset, I think it should be set explicit by the user.
// if user has set content type to multipart/form-data, auto add boundary.
if(req.header(CONTENT_TYPE).contains(MULTIPART_FORM_DATA) &&
!req.header(CONTENT_TYPE).contains("boundary")) {
bound = DataUtil.mimeBoundary();
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/org/jsoup/integration/ConnectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import java.net.URL;
import java.util.Map;

import static org.jsoup.helper.HttpConnection.CONTENT_TYPE;
import static org.jsoup.helper.HttpConnection.MULTIPART_FORM_DATA;
import static org.jsoup.integration.UrlConnectTest.browserUa;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
Expand Down Expand Up @@ -104,12 +106,14 @@ public void doesPost() throws IOException {
@Test
public void doesPostMultipartWithoutInputstream() throws IOException {
Document doc = Jsoup.connect(echoUrl)
.header("Content-Type", "multipart/form-data")
.header(CONTENT_TYPE, MULTIPART_FORM_DATA)
.userAgent(browserUa)
.data("uname", "Jsoup", "uname", "Jonathan", "百", "度一下")
.post();

assertTrue(ihVal("Content-Type", doc).contains("boundary"));
assertTrue(ihVal("Content-Type", doc).contains(MULTIPART_FORM_DATA));

assertTrue(ihVal("Content-Type", doc).contains("boundary")); // should be automatically set
assertEquals("Jsoup, Jonathan", ihVal("uname", doc));
assertEquals("度一下", ihVal("百", doc));
}
Expand Down

0 comments on commit 68ff8cb

Please sign in to comment.