Skip to content

Instantly share code, notes, and snippets.

@derdewey
Created February 10, 2011 19:58
Show Gist options
  • Save derdewey/821211 to your computer and use it in GitHub Desktop.
Save derdewey/821211 to your computer and use it in GitHub Desktop.
Per the lists recommendation, this workaround looks for the two WS headers and works around the lack of content-length.
DEBUG src/connection.c:656: parsing
'GET /wsstream HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:6767
Origin: http://localhost:6767
Sec-WebSocket-Key1: m 4 115S8 g8if m aLD T8 yd124
Sec-WebSocket-Key2: 9 %F8 s]6129' ^ 3 C 76
t�C"���`'
DEBUG src/connection.c:658: parsed
'GET /wsstream HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:6767
Origin: http://localhost:6767
Sec-WebSocket-Key1: m 4 115S8 g8if m aLD T8 yd124
Sec-WebSocket-Key2: 9 %F8 s]6129' ^ 3 C 76
'
DEBUG src/state.rl:54: --> register_request(REQ_RECV:110) State_exec:54
DEBUG src/connection.c:475: HTTP MESSAGE
DEBUG src/state.rl:57: --> route_request(HTTP_REQ:105) State_exec:57
DEBUG src/server.c:221: Looking for target host: localhost
DEBUG src/routing.c:200: Found simple suffix: localhost
DEBUG src/routing.c:218: Found simple prefix: /wsstream
DEBUG src/host.c:127: Found backend at /wsstream
DEBUG src/state.rl:60: --> http_to_handler(HANDLER:104) State_exec:60
DEBUG src/connection.c:231: READ ALL CALLED with content_len: 8, and MAX_CONTENT_LENGTH: 20480
DEBUG src/io.c:350: INITIAL READ: len: 8, nread: 8
DEBUG src/io.c:365: ATTEMPTS: 0, RETRIES: 5
DEBUG src/connection.c:169: HTTP TO HANDLER: 54c6755b-9628-40a4-9a2d-cc82a816345f 159 /wsstream 338:{"PATH":"/wsstream","host":"localhost:6767","sec-websocket-key1":"m 4 115S8 g8if m aLD T8 yd124","origin":"http://localhost:6767","x-forwarded-for":"127.0.0.1","upgrade":"WebSocket","connection":"Upgrade","sec-websocket-key2":"9 %F8 s]6129' ^ 3 C 76","METHOD":"GET","VERSION":"HTTP/1.1","URI":"/wsstream","PATTERN":"/wsstream"},8:t
### BEEP BEEP see that malformed netstring at the end? What's up with that?
DEBUG src/connection.c:656: parsing
'GET /wsstream HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:6767
Origin: http://localhost:6767
Sec-WebSocket-Key1: 2 7419 11 v8 0 0
Sec-WebSocket-Key2: 33 P 48Z}6Q9^42.O S}h8
�B}���'
DEBUG src/connection.c:658: parsed
'GET /wsstream HTTP/1.1
Upgrade: WebSocket
Connection: Upgrade
Host: localhost:6767
Origin: http://localhost:6767
Sec-WebSocket-Key1: 2 7419 11 v8 0 0
Sec-WebSocket-Key2: 33 P 48Z}6Q9^42.O S}h8
'
DEBUG src/state.rl:54: --> register_request(REQ_RECV:110) State_exec:54
DEBUG src/connection.c:475: HTTP MESSAGE
DEBUG src/state.rl:57: --> route_request(HTTP_REQ:105) State_exec:57
DEBUG src/server.c:221: Looking for target host: localhost
DEBUG src/routing.c:200: Found simple suffix: localhost
DEBUG src/routing.c:218: Found simple prefix: /wsstream
DEBUG src/host.c:127: Found backend at /wsstream
DEBUG src/state.rl:60: --> http_to_handler(HANDLER:104) State_exec:60
DEBUG src/connection.c:231: READ ALL CALLED with content_len: 8, and MAX_CONTENT_LENGTH: 20480
DEBUG src/io.c:350: INITIAL READ: len: 8, nread: 8
DEBUG src/io.c:365: ATTEMPTS: 0, RETRIES: 5
DEBUG src/connection.c:169: HTTP TO HANDLER: 54c6755b-9628-40a4-9a2d-cc82a816345f 79 /wsstream 316:{"PATH":"/wsstream","host":"localhost:6767","sec-websocket-key1":"2 7419 11 v8 0 0","origin":"http://localhost:6767","x-forwarded-for":"127.0.0.1","upgrade":"WebSocket","connection":"Upgrade","sec-websocket-key2":"33 P 48Z}6Q9^42.O S}h8","METHOD":"GET","VERSION":"HTTP/1.1","URI":"/wsstream","PATTERN":"/wsstream"},8:�
DEBUG src/connection.c:242: REQ_SENT to handler!
DEBUG src/state.rl:53: --> parse(REQ_SENT:111) State_exec:53
DEBUG src/connection.c:656: parsing
'GET /favicon.ico HTTP/1.1
Host: localhost:6767
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.45 Safari/534.16
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
'
DEBUG src/connection.c:658: parsed
'GET /favicon.ico HTTP/1.1
Host: localhost:6767
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.45 Safari/534.16
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
'
### Umm, so this situation was good when checked against my handler. Maybe that's the problem...
static struct tagbstring UPGRADE = bsStatic("upgrade");
static struct tagbstring CONNECTION = bsStatic("connection");
int connection_http_to_handler(Connection *conn)
{
int content_len = Request_content_length(conn->req);
int rc = 0;
char *body = NULL;
Handler *handler = Request_get_action(conn->req, handler);
error_unless(handler, conn, 404, "No action for request: %s", bdata(Request_path(conn->req)));
// we don't need the header anymore, so commit the buffer and deal with the body
IOBuf_read_commit(conn->iob, Request_header_length(conn->req));
// WebSocket work around
// Problem: Sometimes avail returns 8 but when the message goes to
// the handler it's malformed.
// Ex: the body netstring looks like: '8:t' and misses those last 7 bytes!
bstring upgrade = Request_get(conn->req, (bstring)&UPGRADE);
bstring connection = Request_get(conn->req, (bstring)&CONNECTION);
if(upgrade != NULL && connection != NULL){
int avail = IOBuf_avail(conn->iob);
if(avail != 8){
debug("Purported websocket but body does not have 8 bytes");
goto error;
}
/*
int outlen;
debug("Bytes available: %d",avail);
char* data = IOBuf_read(conn->iob,avail,&outlen);
debug("Bytes read: %d\nThe bytes:\n'%s'\n", outlen, data);
*/
content_len = avail;
} else {
debug("Not a websocket.");
}
if(content_len == 0) {
body = "";
rc = Connection_send_to_handler(conn, handler, body, content_len);
check_debug(rc == 0, "Failed to deliver to the handler.");
} else if(content_len > MAX_CONTENT_LENGTH) {
rc = Upload_file(conn, handler, content_len);
check(rc == 0, "Failed to upload file.");
} else {
if(content_len > conn->iob->len) {
// temporarily grow the buffer
IOBuf_resize(conn->iob, content_len);
}
debug("READ ALL CALLED with content_len: %d, and MAX_CONTENT_LENGTH: %d", content_len, MAX_CONTENT_LENGTH);
body = IOBuf_read_all(conn->iob, content_len, CLIENT_READ_RETRIES);
check(body != NULL, "Client closed the connection during upload.");
rc = Connection_send_to_handler(conn, handler, body, content_len);
check_debug(rc == 0, "Failed to deliver to the handler.");
}
Log_request(conn, 200, content_len);
debug("REQ_SENT to handler!");
return REQ_SENT;
error:
return CLOSE;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment