Skip to content

Commit

Permalink
Raise an error if multipart/@media-type conflicts with Content-Type h…
Browse files Browse the repository at this point in the history
…eader

Closes #39
  • Loading branch information
adamretter committed Jun 18, 2024
1 parent 09db3be commit 6212ae9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum HttpClientError implements ExpathError {
HC003("HC003", "With a multipart response, the override-media-type must be either a multipart media type or application/octet-stream."),
HC004("HC004", "The src attribute on the body element is mutually exclusive with all other attribute (except the media-type)."),
HC005("HC005", "The request element is not valid."),
HC006("HC006", "A timeout occurred waiting for the response.");
HC006("HC006", "A timeout occurred waiting for the response."),
HC007("HC007", "Multipart @media-type conflicts with outer Content-Type header");

private final QName name;
private final String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import org.expath.httpclient.*;
import org.apache.http.message.BasicHeader;
import org.expath.httpclient.ContentType;
import org.expath.httpclient.HeaderSet;
import org.expath.httpclient.HttpClientError;
import org.expath.httpclient.HttpClientException;
import org.expath.httpclient.HttpConstants;
import org.expath.httpclient.HttpRequestBody;
import org.expath.tools.ToolsException;
import org.expath.tools.model.Element;
import org.expath.tools.model.Sequence;

import javax.annotation.Nullable;

/**
* A multipart body in the request.
*
Expand Down Expand Up @@ -59,7 +67,9 @@ public void setHeaders(HeaderSet headers)
throws HttpClientException
{
// set the Content-Type header (if not set by the user)
if ( headers.getFirstHeader("Content-Type") == null ) {
@Nullable final Header explicitContentTypeHeader = headers.getFirstHeader("Content-Type");

if ( explicitContentTypeHeader == null ) {
StringBuilder type = new StringBuilder(getContentType());
type.append("; boundary=");
type.append("\"");
Expand All @@ -71,6 +81,14 @@ public void setHeaders(HeaderSet headers)
}
type.append("\"");
headers.add("Content-Type", type.toString());
} else {
// if the outer explicit http:header/@name="Content-Type" is not same as the http:multipart/@media-type then we have an invalid conflict
final ContentType explicitContentType = ContentType.parse(explicitContentTypeHeader, null, null);
final ContentType multipartMediaType = ContentType.parse(new BasicHeader("Content-Type", getContentType()), null, null);

if (!explicitContentType.getType().equals(multipartMediaType.getType())) {
throw new HttpClientException(HttpClientError.HC007, "http:header/@name=\"Content-Type\" is " + explicitContentType.getType() + ", but http:multipart/@media-type is " + multipartMediaType.getType());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ else if ( "chunked".equals(local) ) {
// walk the elements
// TODO: Check element structure validity (header*, (multipart|body)?)
HeaderSet headers = new HeaderSet();
req.setHeaders(headers);
for ( Element child : myRequest.children() ) {
String local = child.getLocalName();
String ns = child.getNamespaceUri();
Expand All @@ -174,6 +173,7 @@ else if ( "body".equals(local) || "multipart".equals(local) ) {
throw new HttpClientException(HttpClientError.HC005, "Unknown element: " + local);
}
}
req.setHeaders(headers);

return req;
}
Expand Down

0 comments on commit 6212ae9

Please sign in to comment.