Skip to content
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

Connection upgrade support only websocket #56

Open
matus-sabo opened this issue Apr 29, 2021 · 1 comment
Open

Connection upgrade support only websocket #56

matus-sabo opened this issue Apr 29, 2021 · 1 comment

Comments

@matus-sabo
Copy link

matus-sabo commented Apr 29, 2021

Following code accept only upgrade header equal to websocket, thats not correct as stated in link

async function proxy (
{ req, socket, res = socket, head, proxyName },
onReq,
onRes
) {
if (req.aborted) {
return
}
const headers = getRequestHeaders(req, proxyName)
if (head !== undefined) {
if (req.method !== 'GET') {
throw new HttpError('only GET request allowed', null, 405)
}
if (req.headers[UPGRADE] !== 'websocket') {
throw new HttpError('missing upgrade header', null, 400)
}
if (head && head.length) {
res.unshift(head)
}
setupSocket(res)
headers[CONNECTION] = 'upgrade'
headers[UPGRADE] = 'websocket'
}

For example, the client might send a GET request as shown, listing the preferred protocols to switch to (in this case "example/1" and "foo/2"):

GET /index.html HTTP/1.1
Host: www.example.com
Connection: upgrade
Upgrade: example/1, foo/2

Send back a 101 Switching Protocols response status with an Upgrade header that specifies the protocol(s) being switched to. For example:

HTTP/1.1 101 Switching Protocols
Upgrade: foo/2
Connection: Upgrade

Simple fix example

async function proxy ( 
   { req, socket, res = socket, head, proxyName }, 
   onReq, 
   onRes 
 ) { 
   if (req.aborted) { 
     return 
   } 
  
   const headers = getRequestHeaders(req, proxyName) 
  
   if (head !== undefined) { 
     if (req.method !== 'GET') { 
       throw new HttpError('only GET request allowed', null, 405) 
     } 
  
     if (req.headers[UPGRADE] === undefined) { 
       throw new HttpError('missing upgrade header', null, 400) 
     } 
  
     if (head && head.length) { 
       res.unshift(head) 
     } 
  
     setupSocket(res) 

     headers[CONNECTION] = 'upgrade' 
     headers[UPGRADE] = req.headers[UPGRADE]
   } 
@tjhiggins
Copy link

A tmp workaround is to do the following:

const upgrade = req.headers.upgrade;
// https://github.com/nxtedition/node-http2-proxy/issues/56
req.method = 'GET';
req.headers.upgrade = 'websocket';

proxy.ws(req, socket, head, {
  hostname,
  port,
  onReq: (req, { headers }) => {
    // https://github.com/nxtedition/node-http2-proxy/issues/56
    headers!.upgrade = upgrade;
  },
}, defaultWsHandler);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants