-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Manually constructed content-type headers with quotes are not processed correctly #1181
Comments
@wrosenuance thanks for the details, since you have all the details, do you think you can submit a PR, also see if |
@ptrthomas I will check with my company - I need to get approval for contributions, which can take a while. Assuming that comes through I can definitely work on a PR! |
moving to project board: https://github.com/intuit/karate/projects/3#card-41317156 |
@ptrthomas can you assign this issue to me if it is not yet picked? |
@ptrthomas Have done initial analysis. Double Quotes if present in front or in end are not removed in above class and it end up escaping the quotes while creating ContentType object. Will be adding a new method in StringUtils.trimDoubleQuotes to trim the double quotes from starting and end so if any double quotes are present in front and last it will be removed. Is this approach looks fine? Should we also remove all the double quotes from front or end or only the first quote. If media type has below values what should be the expected out come? "abc" -> abc
"\"abc\"" -> should it be "abc" or should it also be abc
"\"\"\"abc\"\"\"" -> should it be abc? Based on this will add a regex. |
this may need research but if we are only trying to get the Charset - we should use the |
Till here everything works fine. |
@Nishant-sehgal let me try again, I'm saying we need to rewrite the karate httputils to use this method: https://netty.io/4.1/api/io/netty/handler/codec/http/HttpUtil.html#getCharset-java.lang.CharSequence-java.nio.charset.Charset- |
@ptrthomas I am trying to understand how will using HttpUtil.getCharset of netty help. If we use netty Implementation or existing karate httputils both are behaving the same way and returning the same Charset object with same data. Issue is when we are setting parameter in content type. ct = ct.withParameters(new BasicNameValuePair(entry.getKey(), entry.getValue())); Since we are creating HttpEntity object that requires ContentType. I also found ContentType.parse(mediaType) correctly populate mimeType,charset and parameters and internally handles quotes as well. But we are calling ContentType ct = ContentType.parse(mediaType).withCharset(charset); where withCharset reset the parameters object as it creates a new object and now if we call withParameters it adds the parameters but that does not handles the quotes. I think there are 2 options:
Please suggest how shall i take it forward. |
@Nishant-sehgal thanks for the analysis, this helps ! option 2 sounds like less of a hack to me |
@ptrthomas Have raised an initial PR Please share your thoughts for the same. PR |
Meanwhile i also though of an other implementation private static ContentType getContentType(String mediaType, Charset charset) {
if (!HttpUtils.isPrintable(mediaType)) {
try {
return ContentType.create(mediaType);
} catch (Exception e) {
return null;
}
}
final CharArrayBuffer buf = new CharArrayBuffer(mediaType.length());
buf.append(mediaType);
final HeaderElement[] elements =
BasicHeaderValueParser.INSTANCE.parseElements(buf, new ParserCursor(0, mediaType.length()));
HeaderElement headerElement = null;
if(null != elements && elements.length == 1) {
headerElement = elements[0];
}
ContentType ct = ContentType.create(headerElement.getName(), HttpUtil.getCharset(mediaType, charset));
if(headerElement.getParameters() != null) {
for (NameValuePair nameValuePair : headerElement.getParameters()) {
ct = ct.withParameters(nameValuePair);
}
}
return ct;
} |
1.0 released |
I was attempting to create a MTOM SOAP payload using SAAJ to send through Karate, and encountered an issue because the Content-Type header that SAAJ generated was being adjusted by Karate. While debugging, I was able to narrow down the location where the issue was introduced to the ApacheHttpUtils class, and I can replicate the issue with the following code:
The output from running this is:
The problem is that the Content-Type header is split at semi-colons and then broken into name-value pairs, but quotes around values are not removed. Then the names and values are passed to code that escapes the quotes that were not removed, leading to the appearance above.
To workaround this I had to remove quotes manually from the Content-Type value coming from SAAJ, which seems less than ideal since these values could in theory contain spaces or other characters that would cause problems for the string splitting algorithm.
The text was updated successfully, but these errors were encountered: