Skip to content

Connection upgrade support only websocket #56

@matus-sabo

Description

@matus-sabo

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]
   } 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions