Skip to content

Commit 6212ae9

Browse files
committed
Raise an error if multipart/@media-type conflicts with Content-Type header
Closes #39
1 parent 09db3be commit 6212ae9

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

http-client-java/src/main/java/org/expath/httpclient/HttpClientError.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public enum HttpClientError implements ExpathError {
2323
HC003("HC003", "With a multipart response, the override-media-type must be either a multipart media type or application/octet-stream."),
2424
HC004("HC004", "The src attribute on the body element is mutually exclusive with all other attribute (except the media-type)."),
2525
HC005("HC005", "The request element is not valid."),
26-
HC006("HC006", "A timeout occurred waiting for the response.");
26+
HC006("HC006", "A timeout occurred waiting for the response."),
27+
HC007("HC007", "Multipart @media-type conflicts with outer Content-Type header");
2728

2829
private final QName name;
2930
private final String description;

http-client-java/src/main/java/org/expath/httpclient/impl/MultipartRequestBody.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,19 @@
1414
import java.util.ArrayList;
1515
import java.util.List;
1616
import org.apache.http.Header;
17-
import org.expath.httpclient.*;
17+
import org.apache.http.message.BasicHeader;
18+
import org.expath.httpclient.ContentType;
19+
import org.expath.httpclient.HeaderSet;
20+
import org.expath.httpclient.HttpClientError;
21+
import org.expath.httpclient.HttpClientException;
22+
import org.expath.httpclient.HttpConstants;
23+
import org.expath.httpclient.HttpRequestBody;
1824
import org.expath.tools.ToolsException;
1925
import org.expath.tools.model.Element;
2026
import org.expath.tools.model.Sequence;
2127

28+
import javax.annotation.Nullable;
29+
2230
/**
2331
* A multipart body in the request.
2432
*
@@ -59,7 +67,9 @@ public void setHeaders(HeaderSet headers)
5967
throws HttpClientException
6068
{
6169
// set the Content-Type header (if not set by the user)
62-
if ( headers.getFirstHeader("Content-Type") == null ) {
70+
@Nullable final Header explicitContentTypeHeader = headers.getFirstHeader("Content-Type");
71+
72+
if ( explicitContentTypeHeader == null ) {
6373
StringBuilder type = new StringBuilder(getContentType());
6474
type.append("; boundary=");
6575
type.append("\"");
@@ -71,6 +81,14 @@ public void setHeaders(HeaderSet headers)
7181
}
7282
type.append("\"");
7383
headers.add("Content-Type", type.toString());
84+
} else {
85+
// if the outer explicit http:header/@name="Content-Type" is not same as the http:multipart/@media-type then we have an invalid conflict
86+
final ContentType explicitContentType = ContentType.parse(explicitContentTypeHeader, null, null);
87+
final ContentType multipartMediaType = ContentType.parse(new BasicHeader("Content-Type", getContentType()), null, null);
88+
89+
if (!explicitContentType.getType().equals(multipartMediaType.getType())) {
90+
throw new HttpClientException(HttpClientError.HC007, "http:header/@name=\"Content-Type\" is " + explicitContentType.getType() + ", but http:multipart/@media-type is " + multipartMediaType.getType());
91+
}
7492
}
7593
}
7694

http-client-java/src/main/java/org/expath/httpclient/impl/RequestParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ else if ( "chunked".equals(local) ) {
148148
// walk the elements
149149
// TODO: Check element structure validity (header*, (multipart|body)?)
150150
HeaderSet headers = new HeaderSet();
151-
req.setHeaders(headers);
152151
for ( Element child : myRequest.children() ) {
153152
String local = child.getLocalName();
154153
String ns = child.getNamespaceUri();
@@ -174,6 +173,7 @@ else if ( "body".equals(local) || "multipart".equals(local) ) {
174173
throw new HttpClientException(HttpClientError.HC005, "Unknown element: " + local);
175174
}
176175
}
176+
req.setHeaders(headers);
177177

178178
return req;
179179
}

0 commit comments

Comments
 (0)