Fix #1329 - send MG_EV_WS_OPEN for server connections

This commit is contained in:
Sergey Lyubka committed 2021-08-04 11:38:32 +01:00
1 parent 9b18a6350a
commit c2c6ff11ed
5 files changed
+46 -23

No files matched your search

+1 -1
View File
@@ -740,7 +740,7 @@ void mg_http_serve_dir(struct mg_connection *c, struct mg_http_message *hm,
int flags = uri_to_path(c, hm, opts, root, sizeof(root), path, sizeof(path));
if (flags == 0) return;
LOG(LL_DEBUG, ("root [%s], path [%s] %d", root, path, flags));
// LOG(LL_DEBUG, ("root [%s], path [%s] %d", root, path, flags));
if (flags & MG_FS_DIR) {
listdir(c, hm, opts, path);
} else if (opts->ssi_pattern != NULL &&
+13 -5
View File
@@ -14,15 +14,16 @@ struct ws_msg {
size_t data_len;
};
static void ws_handshake(struct mg_connection *c, const char *key,
size_t key_len, const char *fmt, va_list ap) {
static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey,
const struct mg_str *wsproto, const char *fmt,
va_list ap) {
const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
unsigned char sha[20], b64_sha[30];
char mem[128], *buf = mem;
mg_sha1_ctx sha_ctx;
mg_sha1_init(&sha_ctx);
mg_sha1_update(&sha_ctx, (unsigned char *) key, key_len);
mg_sha1_update(&sha_ctx, (unsigned char *) wskey->ptr, wskey->len);
mg_sha1_update(&sha_ctx, (unsigned char *) magic, 36);
mg_sha1_final(sha, &sha_ctx);
mg_base64_encode(sha, sizeof(sha), (char *) b64_sha);
@@ -33,9 +34,14 @@ static void ws_handshake(struct mg_connection *c, const char *key,
"Upgrade: websocket\r\n"
"Connection: Upgrade\r\n"
"Sec-WebSocket-Accept: %s\r\n"
"%s\r\n",
"%s",
b64_sha, buf);
if (buf != mem) free(buf);
if (wsproto != NULL) {
mg_printf(c, "Sec-WebSocket-Protocol: %.*s\r\n", (int) wsproto->len,
wsproto->ptr);
}
mg_send(c, "\r\n", 2);
}
static size_t ws_process(uint8_t *buf, size_t len, struct ws_msg *msg) {
@@ -221,11 +227,13 @@ void mg_ws_upgrade(struct mg_connection *c, struct mg_http_message *hm,
mg_http_reply(c, 426, "", "WS upgrade expected\n");
c->is_draining = 1;
} else {
struct mg_str *wsproto = mg_http_get_header(hm, "Sec-WebSocket-Protocol");
va_list ap;
va_start(ap, fmt);
ws_handshake(c, wskey->ptr, wskey->len, fmt, ap);
ws_handshake(c, wskey, wsproto, fmt, ap);
va_end(ap);
c->is_websocket = 1;
mg_call(c, MG_EV_WS_OPEN, &hm);
}
}