remove obsolete hixie protocol support

pull/3/head
Andreas Weigel 8 years ago committed by Christian Beier
parent bcefa591cd
commit a2322e7006
No known key found for this signature in database
GPG Key ID: 421BB3B45C6067F8

@ -70,14 +70,6 @@ static int gettid() {
*/ */
#define GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" #define GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
#define SERVER_HANDSHAKE_HIXIE "HTTP/1.1 101 Web Socket Protocol Handshake\r\n\
Upgrade: WebSocket\r\n\
Connection: Upgrade\r\n\
%sWebSocket-Origin: %s\r\n\
%sWebSocket-Location: %s://%s%s\r\n\
%sWebSocket-Protocol: %s\r\n\
\r\n%s"
#define SERVER_HANDSHAKE_HYBI "HTTP/1.1 101 Switching Protocols\r\n\ #define SERVER_HANDSHAKE_HYBI "HTTP/1.1 101 Switching Protocols\r\n\
Upgrade: websocket\r\n\ Upgrade: websocket\r\n\
Connection: Upgrade\r\n\ Connection: Upgrade\r\n\
@ -107,8 +99,6 @@ static rfbBool webSocketsHandshake(rfbClientPtr cl, char *scheme);
void webSocketsGenMd5(char * target, char *key1, char *key2, char *key3); void webSocketsGenMd5(char * target, char *key1, char *key2, char *key3);
static int webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst); static int webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst);
static int webSocketsEncodeHixie(rfbClientPtr cl, const char *src, int len, char **dst);
static int webSocketsDecodeHixie(ws_ctx_t *wsctx, char *dst, int len);
static int ws_read(void *cl, char *buf, int len); static int ws_read(void *cl, char *buf, int len);
static int ws_peek(void *cl, char *buf, int len); static int ws_peek(void *cl, char *buf, int len);
@ -294,20 +284,23 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
} }
} }
if (!(path && host && (origin || sec_ws_origin))) { /* older hixie handshake, this could be removed if
rfbErr("webSocketsHandshake: incomplete client handshake\n"); * a final standard is established -- removed now */
if (!sec_ws_version) {
rfbErr("Hixie no longer supported\n");
free(response); free(response);
free(buf); free(buf);
return FALSE; return FALSE;
} }
if ((protocol) && (strstr(protocol, "binary"))) { if (!(path && host && (origin || sec_ws_origin))) {
if (! sec_ws_version) { rfbErr("webSocketsHandshake: incomplete client handshake\n");
rfbErr("webSocketsHandshake: 'binary' protocol not supported with Hixie\n");
free(response); free(response);
free(buf); free(buf);
return FALSE; return FALSE;
} }
if ((protocol) && (strstr(protocol, "binary"))) {
rfbLog(" - webSocketsHandshake: using binary/raw encoding\n"); rfbLog(" - webSocketsHandshake: using binary/raw encoding\n");
base64 = FALSE; base64 = FALSE;
protocol = "binary"; protocol = "binary";
@ -325,32 +318,16 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
* Generate the WebSockets server response based on the the headers sent * Generate the WebSockets server response based on the the headers sent
* by the client. * by the client.
*/ */
if (sec_ws_version) {
char accept[B64LEN(SHA1_HASH_SIZE) + 1]; char accept[B64LEN(SHA1_HASH_SIZE) + 1];
rfbLog(" - WebSockets client version hybi-%02d\n", sec_ws_version); rfbLog(" - WebSockets client version hybi-%02d\n", sec_ws_version);
webSocketsGenSha1Key(accept, sizeof(accept), sec_ws_key); webSocketsGenSha1Key(accept, sizeof(accept), sec_ws_key);
if(strlen(protocol) > 0)
if(strlen(protocol) > 0) {
len = snprintf(response, WEBSOCKETS_MAX_HANDSHAKE_LEN, len = snprintf(response, WEBSOCKETS_MAX_HANDSHAKE_LEN,
SERVER_HANDSHAKE_HYBI, accept, protocol); SERVER_HANDSHAKE_HYBI, accept, protocol);
else
len = snprintf(response, WEBSOCKETS_MAX_HANDSHAKE_LEN,
SERVER_HANDSHAKE_HYBI_NO_PROTOCOL, accept);
} else {
/* older hixie handshake, this could be removed if
* a final standard is established */
if (!(key1 && key2 && key3)) {
rfbLog(" - WebSockets client version hixie-75\n");
prefix[0] = '\0';
trailer[0] = '\0';
} else { } else {
rfbLog(" - WebSockets client version hixie-76\n");
snprintf(prefix, 5, "Sec-");
webSocketsGenMd5(trailer, key1, key2, key3);
}
len = snprintf(response, WEBSOCKETS_MAX_HANDSHAKE_LEN, len = snprintf(response, WEBSOCKETS_MAX_HANDSHAKE_LEN,
SERVER_HANDSHAKE_HIXIE, prefix, origin, prefix, scheme, SERVER_HANDSHAKE_HYBI_NO_PROTOCOL, accept);
host, path, prefix, protocol, trailer);
} }
if (rfbWriteExact(cl, response, len) < 0) { if (rfbWriteExact(cl, response, len) < 0) {
@ -363,17 +340,10 @@ webSocketsHandshake(rfbClientPtr cl, char *scheme)
free(response); free(response);
free(buf); free(buf);
wsctx = calloc(1, sizeof(ws_ctx_t)); wsctx = calloc(1, sizeof(ws_ctx_t));
if (sec_ws_version) {
wsctx->version = WEBSOCKETS_VERSION_HYBI; wsctx->version = WEBSOCKETS_VERSION_HYBI;
wsctx->encode = webSocketsEncodeHybi; wsctx->encode = webSocketsEncodeHybi;
wsctx->decode = webSocketsDecodeHybi; wsctx->decode = webSocketsDecodeHybi;
} else {
wsctx->version = WEBSOCKETS_VERSION_HIXIE;
wsctx->encode = webSocketsEncodeHixie;
wsctx->decode = webSocketsDecodeHixie;
}
wsctx->ctxInfo.readFunc = ws_read; wsctx->ctxInfo.readFunc = ws_read;
wsctx->ctxInfo.peekFunc = ws_peek; wsctx->ctxInfo.peekFunc = ws_peek;
wsctx->base64 = base64; wsctx->base64 = base64;
@ -432,24 +402,6 @@ webSocketsGenMd5(char * target, char *key1, char *key2, char *key3)
return; return;
} }
static int
webSocketsEncodeHixie(rfbClientPtr cl, const char *src, int len, char **dst)
{
int sz = 0;
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
wsctx->codeBufEncode[sz++] = '\x00';
len = b64_ntop((unsigned char *)src, len, wsctx->codeBufEncode+sz, sizeof(wsctx->codeBufEncode) - (sz + 1));
if (len < 0) {
return len;
}
sz += len;
wsctx->codeBufEncode[sz++] = '\xff';
*dst = wsctx->codeBufEncode;
return sz;
}
static int static int
ws_read(void *ctxPtr, char *buf, int len) ws_read(void *ctxPtr, char *buf, int len)
{ {
@ -479,104 +431,6 @@ ws_peek(void *ctxPtr, char *buf, int len)
return n; return n;
} }
static int
webSocketsDecodeHixie(ws_ctx_t *wsctx, char *dst, int len)
{
int retlen = 0, n, i, avail, modlen, needlen;
char *buf, *end = NULL;
buf = wsctx->codeBufDecode;
//n = ws_peek(cl, buf, len*2+2);
n = wsctx->ctxInfo.peekFunc(wsctx->ctxInfo.ctxPtr, buf, len*2+2);
if (n <= 0) {
/* save errno because rfbErr() will tamper it */
int olderrno = errno;
rfbErr("%s: peek (%d) %m\n", __func__, errno);
errno = olderrno;
return n;
}
/* Base64 encoded WebSockets stream */
if (buf[0] == '\xff') {
//i = ws_read(cl, buf, 1); /* Consume marker */
i = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, buf, 1);
buf++;
n--;
}
if (n == 0) {
errno = EAGAIN;
return -1;
}
if (buf[0] == '\x00') {
//i = ws_read(cl, buf, 1); /* Consume marker */
i = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, buf, 1);
buf++;
n--;
}
if (n == 0) {
errno = EAGAIN;
return -1;
}
/* end = memchr(buf, '\xff', len*2+2); */
end = memchr(buf, '\xff', n);
if (!end) {
end = buf + n;
}
avail = end - buf;
len -= wsctx->carrylen;
/* Determine how much base64 data we need */
modlen = len + (len+2)/3;
needlen = modlen;
if (needlen % 4) {
needlen += 4 - (needlen % 4);
}
if (needlen > avail) {
/* rfbLog("Waiting for more base64 data\n"); */
errno = EAGAIN;
return -1;
}
/* Any carryover from previous decode */
for (i=0; i < wsctx->carrylen; i++) {
/* rfbLog("Adding carryover %d\n", wsctx->carryBuf[i]); */
dst[i] = wsctx->carryBuf[i];
retlen += 1;
}
/* Decode the rest of what we need */
buf[needlen] = '\x00'; /* Replace end marker with end of string */
/* rfbLog("buf: %s\n", buf); */
n = b64_pton(buf, (unsigned char *)dst+retlen, 2+len);
if (n < len) {
rfbErr("Base64 decode error\n");
errno = EIO;
return -1;
}
retlen += n;
/* Consume the data from socket */
//i = ws_read(cl, buf, needlen);
i = wsctx->ctxInfo.readFunc(wsctx->ctxInfo.ctxPtr, buf, needlen);
wsctx->carrylen = n - len;
retlen -= wsctx->carrylen;
for (i=0; i < wsctx->carrylen; i++) {
/* rfbLog("Saving carryover %d\n", dst[retlen + i]); */
wsctx->carryBuf[i] = dst[retlen + i];
}
/* rfbLog("<< webSocketsDecode, retlen: %d\n", retlen); */
return retlen;
}
static int static int
webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst) webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst)
{ {
@ -645,7 +499,7 @@ webSocketsEncodeHybi(rfbClientPtr cl, const char *src, int len, char **dst)
int int
webSocketsEncode(rfbClientPtr cl, const char *src, int len, char **dst) webSocketsEncode(rfbClientPtr cl, const char *src, int len, char **dst)
{ {
return ((ws_ctx_t *)cl->wsctx)->encode(cl, src, len, dst); return webSocketsEncodeHybi(cl, src, len, dst);
} }
int int
@ -653,67 +507,18 @@ webSocketsDecode(rfbClientPtr cl, char *dst, int len)
{ {
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx; ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
wsctx->ctxInfo.ctxPtr = cl; wsctx->ctxInfo.ctxPtr = cl;
return wsctx->decode(wsctx, dst, len); return webSocketsDecodeHybi(wsctx, dst, len);
} }
/* returns TRUE if client sent a close frame or a single 'end of frame' /* returns TRUE if client sent a close frame or a single 'end of frame'
* marker was received, FALSE otherwise * marker was received, FALSE otherwise
* *
* Note: This is a Hixie-only hack! * Note: This was a Hixie-only hack!
**/ **/
rfbBool rfbBool
webSocketCheckDisconnect(rfbClientPtr cl) webSocketCheckDisconnect(rfbClientPtr cl)
{ {
ws_ctx_t *wsctx = (ws_ctx_t *)cl->wsctx;
/* With Base64 encoding we need at least 4 bytes */
char peekbuf[4];
int n;
if (wsctx->version == WEBSOCKETS_VERSION_HYBI)
return FALSE;
if (cl->sslctx)
n = rfbssl_peek(cl, peekbuf, 4);
else
n = recv(cl->sock, peekbuf, 4, MSG_PEEK);
if (n <= 0) {
if (n != 0)
rfbErr("%s: peek; %m", __func__);
rfbCloseClient(cl);
return TRUE;
}
if (peekbuf[0] == '\xff') {
int doclose = 0;
/* Make sure we don't miss a client disconnect on an end frame
* marker. Because we use a peek buffer in some cases it is not
* applicable to wait for more data per select(). */
switch (n) {
case 3:
if (peekbuf[1] == '\xff' && peekbuf[2] == '\x00')
doclose = 1;
break;
case 2:
if (peekbuf[1] == '\x00')
doclose = 1;
break;
default:
return FALSE;
}
if (cl->sslctx)
n = rfbssl_read(cl, peekbuf, n);
else
n = read(cl->sock, peekbuf, n);
if (doclose) {
rfbErr("%s: websocket close frame received\n", __func__);
rfbCloseClient(cl);
}
return TRUE;
}
return FALSE; return FALSE;
} }

Loading…
Cancel
Save