Comments (6)
Thanks for the write-up, @jpillora. Sorry, @peterjosling that you ran into an issue due to header casing. Since HTTP headers should be treated case insensitive per spec, I believe we’ll have to satisfy two constraints:
- Preserve case of all headers set in XHook.
- Allow retrieval of headers (
getResponseHeader
) using any case, e.g.getResponseHeader('foo'), getResponseHeader('FOO'), getResponseHeader('fOo')
should all return the same.
One idea is to save a map of the original headers for sending it to the server and one for keeping a normalized map (e.g. lowercase) for case insensitive retrieval. One edge case is allowing multi-value headers where the header name is set using different cases. Maybe the last one wins then, e.g. setRequestHeader('foo', 2)
, setRequestHeader('FOO', 3)
will be FOO: 2, 3
. What do you think?
from xhook.
Yep, looks good.
The hard part is making this work with the XHook headers
object API (https://github.com/jpillora/xhook#request-object). Maybe multiple setRequestHeader
calls could:
- convert
name
:string
intoname
:[string,string]
, or it could arr = string.split(','); arr.push(newstring); arr.join(',')
And also if we go with the last key, the prior key would need to be deleted.
I think I'll go with the latter, since I'm hesitant about changing the existing name
:string
API.
An alternative to a second map, would to simply scan the existing map on all setRequestHeader
calls and on match, perform the split
/join
. This shouldn't be much of a performance hit since people generally only set 1-5 headers per request.
from xhook.
Unless there any objections, will try to get this in tonight
from xhook.
@jpillora Just to clarify, are you talking about how to support multi-value headers with setRequestHeader
, e.g. setRequestHeader('foo', 1); setRequestHeader('foo', 2)
should equal foo: 1, 2
? I’d vote for keeping an array in case of multiple values to make the API cleaner. What does the code currently do?
Documentation: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#setRequestHeader()
Re: preserving case for setRequestHeader
while allowing case insensitive lookup using getResponseHeader
. You’re right that if we assume a small number of headers, an object traversal with normalization might be sufficient, e.g. (untested)
getResponseHeader = (name) ->
values = (value for header, value of headers when name.toLowerCase() is header.toLowerCase())
values.join ','
from xhook.
@gasi Yep, that will be the new getResponseHeader
. The code currently:
- Initialises underlying XHR, the facade
xhr
, and blankheaders = {}
- Calls to
xhr.setRequestHeader
just modifyheaders
(Here is where we need the newsetRequestHeader
, which would find the value with a case-insensitive scan and perform thesplit
join
- instead of using an array) before
hooks run, passing the user theheaders
object for optional modification- Call to
xhr.send
causes each ofheaders
to be set on the underlying XHR, then it is sent
The before
hooks receive headers
as name
:value
(String
), since what is actually sent is just a comma-delimited string, I think we should just pass that string to the user and they can modify it as they wish. If we used an array, then all users must do if headers instanceof Array else ...
first, whereas it's always a string, they can optionally split it again if they want an array.
from xhook.
Thanks for the outline—LGTM 👍 Not 100% sure about string vs array though. I can see that if headers instanceof Array else ...
(BTW, I believe Array.isArray headers
is safer: http://stackoverflow.com/a/4029057/125305) is cumbersome but at least the user will quickly notice if it’s a multi-valued header. Just doing a comma delimited string hides the fact that the header returned multiple values.
I know this isn’t Java, but I found Netty’s HTTP message implementation quite elegant:
http://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/codec/http/HttpMessage.html#getHeader(java.lang.String)
setHeader
to overwrite existing header (not sure this jives with W3C spec in XHR)addHeader
to support multi-value headers.getHeader
return first value of header.getHeaders
return all header names and values.
Just 🍔 for 💭 😄
from xhook.
Related Issues (20)
- can't post formData HOT 2
- How to get the request in the iframe HOT 1
- Warning 'Event.path' is deprecated and will be removed in M109, around January 2023. Please use 'Event.composedPath()' instead HOT 1
- Slowing down the network calls HOT 2
- Errors are passed to Promise.resolve instead of Promise.reject with xhook 1.4.9 HOT 6
- Automate release process HOT 10
- Speed up tests
- Some files are unnecessary. HOT 1
- Automatic Github releases using conventional commits
- xhook breaks `navigator.sendBeacon(url, formData)` HOT 6
- Change the output of xhook in src/main.js HOT 1
- FormData fails all calls - testing solution but can't make it work at all HOT 17
- Before handlers for fetch(new Request('/foo')) requests : 1) sync handlers get no information; 2) sync and async can't change request parameters or replace the Request instance.
- When other programs modify XMLHttpRequest.prototype., xhook will not work HOT 1
- [Question] How to skip certain URL (some URL use mock data and some still go to real API )? HOT 1
- When you use xhr.response to read response data, the hook becomes invalid
- [Bug] Unable to retrieve the requested URL in the response
- The HTTP field name should be lowercase
- [Bug] The default value of XHR's response is incorrect
- [Bug] isFetch parameter is undefined
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xhook.