Header deserialization should be case insensitive
See original GitHub issue- Package Name: core-http
- Package Version: 2.2.5 and 2.2.6
- Operating system: all
- nodejs
- version: all
- browser
- name/version:
- typescript
- version: all
- Is the bug related to documentation in
- README.md
- source code documentation
- SDK API docs on https://docs.microsoft.com
Describe the bug
We are using a custom HTTP client to preserve header casings as they come from the server. When we do this we do something like:
// the object our sendRequest method returns
const httpOperationResponse: HttpOperationResponse = {
request: httpRequest,
status: response.statusCode,
// response.rawHeaders is a collection of raw HTTP response headers exactly as they were received.
// Header keys and values come in a single list and not as a list of tuples.
// Header keys are immediately followed by their respective values.
// Header names are not lowercased, and duplicates are not merged.
headers: this.#parseNetRawHeaders(response.rawHeaders),
readableStreamBody: undefined,
bodyAsText: undefined
};
and
#parseNetRawHeaders(rawHeaders): HttpHeadersLike {
const httpHeaders = new CoreHttp.HttpHeaders();
for (let i = 0; i < rawHeaders.length; i += 2) {
httpHeaders.set(rawHeaders[i], rawHeaders[i + 1]);
}
return httpHeaders;
}
We recently started noticing that this introduces problems during deserialization of headers in core-http’s deserializeCompositeType
. Specifically in this code block:
else {
const property = responseBody[propertyName];
instance[key] = serializer.deserialize(propertyMapper, property, propertyObjectName, options);
}
The propertyName
is an all-lowercase version of the header, while the responseBody
contains the headers with original-cased names.
This should be handled better, as HTTP headers should be treated case insensitively by code. It should not matter what case the server used for a header. Since the code assumes that responseBody
contains headers with lowercased names, then deserialization of headers can fail (such as the last-modified example in my screenshot).
To Reproduce
- Do as described above
Expected behavior Deserialization should work.
Screenshots See above.
Additional context Possible solutions:
- In the short-term we will consider being more stringent about what headers we respect casing for.
- In the long-term we would expect this to be fixed in core-http. Perhaps the best spot to do this would be in
deserializeResponseBody
when the headers are deserialized. Instead of passing inparsedResponse.headers.rawHeaders()
tooperationSpec.serializer.deserialize
, y’all could useparsedResponse.headers.toJson({ preserveCase: false })
? IfoperationSpec.serializer.deserialize
is going to assume header keys have been lowercased, usingrawHeaders
/headers which are potentially uppercase probably isn’t a good idea.
Issue Analytics
- State:
- Created a year ago
- Comments:11 (5 by maintainers)
@xirzec I tested it locally and it seems to work well/as expected.
@MRayermannMSFT looks like this actually made the nightly (which is really a daily) build yesterday. Give
@dev
or 2.2.7-alpha.20220815.2 a try!