I want to change Origin Header of the websocket handshake in react native.
like this
var ws = new WebSocket(
'http://localhost/auth',
[],
{
'headers': {
'Origin': "https://secret-host.com"
}
}
);
I also tried react-native ios websocket library directly.
from
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), (__bridge CFStringRef)_url.RCTSR_origin);
to
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Origin"), CFSTR("https://secret-host.com"));
But it can't work.
Does anyone know how to change it? I need this for using skyway(WebRTC) in react-native.
Thanks.
Your proposed approach should actually work in React Native 0.68.* and higher:
The caveat is that it always adds an origin header, so you cannot remove it without patching RN itself (which is doable using patch-package). This is the code you were referring to - it always generates a default
Origin:
header. You can set it toOrigin: ""
but it will still be sent which may not be good enough for your server.On later lines in the linked RN code, it's being overwritten by your custom JS value in an "enumeration"/loop which is why your code should work.
To check if the header is being set, you can add a breakpoint in that file you linked on line 483 (
[self _readHTTPHeader];
) in Xcode and when the socket connects, add a watch for the following code:Then right click the watched value and click "Print description":
You should see the actual output of the HTTP request, which will be something like this: