MCPcopy Create free account
hub / github.com/apache/httpd / lua_websocket_greet

Function lua_websocket_greet

modules/lua/lua_request.c:2191–2233  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2189}
2190
2191static int lua_websocket_greet(lua_State *L)
2192{
2193 const char *key = NULL;
2194 unsigned char digest[APR_SHA1_DIGESTSIZE];
2195 apr_sha1_ctx_t sha1;
2196 char *encoded;
2197 int encoded_len;
2198 request_rec *r = ap_lua_check_request_rec(L, 1);
2199 key = apr_table_get(r->headers_in, "Sec-WebSocket-Key");
2200 if (key != NULL) {
2201 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03011)
2202 "Websocket: Got websocket key: %s", key);
2203 key = apr_pstrcat(r->pool, key, "258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
2204 NULL);
2205 apr_sha1_init(&sha1);
2206 apr_sha1_update(&sha1, key, strlen(key));
2207 apr_sha1_final(digest, &sha1);
2208 encoded_len = apr_base64_encode_len(APR_SHA1_DIGESTSIZE);
2209 if (encoded_len) {
2210 encoded = apr_palloc(r->pool, encoded_len);
2211 encoded_len = apr_base64_encode(encoded, (char*) digest, APR_SHA1_DIGESTSIZE);
2212 r->status = 101;
2213 apr_table_setn(r->headers_out, "Upgrade", "websocket");
2214 apr_table_setn(r->headers_out, "Connection", "Upgrade");
2215 apr_table_setn(r->headers_out, "Sec-WebSocket-Accept", encoded);
2216
2217 /* Trick httpd into NOT using the chunked filter, IMPORTANT!!!111*/
2218 apr_table_setn(r->headers_out, "Transfer-Encoding", "chunked");
2219
2220 r->clength = 0;
2221 r->bytes_sent = 0;
2222 r->read_chunked = 0;
2223 ap_rflush(r);
2224 ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03012)
2225 "Websocket: Upgraded from HTTP to Websocket");
2226 lua_pushboolean(L, 1);
2227 return 1;
2228 }
2229 }
2230 ap_log_rerror(APLOG_MARK, APLOG_NOTICE, 0, r, APLOGNO(02666)
2231 "Websocket: Upgrade from HTTP to Websocket failed");
2232 return 0;
2233}
2234
2235static apr_status_t lua_websocket_readbytes(conn_rec* c,
2236 apr_bucket_brigade *brigade,

Callers

nothing calls this directly

Calls 2

ap_rflushFunction · 0.85
ap_lua_check_request_recFunction · 0.70

Tested by

no test coverage detected