mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-19 19:40:56 +00:00
Handle #1497 - handshake format
This commit is contained in:
1 parent
0fbc4b8ff4
commit
ad4812218d
5 files changed
+15
-11
No files matched your search
@@ -11,7 +11,9 @@ static const char *s_url = "ws://localhost:8000/websocket";
|
|||||||
|
|
||||||
// Print websocket response and signal that we're done
|
// Print websocket response and signal that we're done
|
||||||
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) {
|
||||||
if (ev == MG_EV_ERROR) {
|
if (ev == MG_EV_OPEN) {
|
||||||
|
c->is_hexdumping = 1;
|
||||||
|
} else if (ev == MG_EV_ERROR) {
|
||||||
// On error, log error message
|
// On error, log error message
|
||||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||||
} else if (ev == MG_EV_WS_OPEN) {
|
} else if (ev == MG_EV_WS_OPEN) {
|
||||||
@@ -33,6 +35,7 @@ int main(void) {
|
|||||||
bool done = false; // Event handler flips it to true
|
bool done = false; // Event handler flips it to true
|
||||||
struct mg_connection *c; // Client connection
|
struct mg_connection *c; // Client connection
|
||||||
mg_mgr_init(&mgr); // Initialise event manager
|
mg_mgr_init(&mgr); // Initialise event manager
|
||||||
|
mg_log_set("4");
|
||||||
c = mg_ws_connect(&mgr, s_url, fn, &done, NULL); // Create client
|
c = mg_ws_connect(&mgr, s_url, fn, &done, NULL); // Create client
|
||||||
while (c && done == false) mg_mgr_poll(&mgr, 1000); // Wait for echo
|
while (c && done == false) mg_mgr_poll(&mgr, 1000); // Wait for echo
|
||||||
mg_mgr_free(&mgr); // Deallocate resources
|
mg_mgr_free(&mgr); // Deallocate resources
|
||||||
|
|||||||
+5
-4
@@ -3269,7 +3269,7 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
|||||||
(char) ('0' + c->is_writable),
|
(char) ('0' + c->is_writable),
|
||||||
'\0'};
|
'\0'};
|
||||||
MG_LOG(log_level,
|
MG_LOG(log_level,
|
||||||
("%3lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
("%lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
||||||
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
@@ -5070,6 +5070,7 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
|||||||
char nonce[16], key[30], mem1[128], mem2[256], *buf1 = mem1, *buf2 = mem2;
|
char nonce[16], key[30], mem1[128], mem2[256], *buf1 = mem1, *buf2 = mem2;
|
||||||
struct mg_str host = mg_url_host(url);
|
struct mg_str host = mg_url_host(url);
|
||||||
size_t n1 = 0, n2 = 0;
|
size_t n1 = 0, n2 = 0;
|
||||||
|
nonce[0] = key[0] = mem1[0] = mem2[0] = '\0';
|
||||||
if (fmt != NULL) {
|
if (fmt != NULL) {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
@@ -5077,7 +5078,6 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
|||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
// Send handshake request
|
// Send handshake request
|
||||||
// mg_url_host(url, host, sizeof(host));
|
|
||||||
mg_random(nonce, sizeof(nonce));
|
mg_random(nonce, sizeof(nonce));
|
||||||
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
||||||
n2 = mg_asprintf(&buf2, sizeof(mem2),
|
n2 = mg_asprintf(&buf2, sizeof(mem2),
|
||||||
@@ -5089,8 +5089,9 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
|||||||
"Sec-WebSocket-Version: 13\r\n"
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
"Sec-WebSocket-Key: %s\r\n"
|
"Sec-WebSocket-Key: %s\r\n"
|
||||||
"\r\n",
|
"\r\n",
|
||||||
mg_url_uri(url), (int) host.len, host.ptr, n1, buf1, key);
|
mg_url_uri(url), (int) host.len, host.ptr, (int) n1, buf1,
|
||||||
mg_send(c, buf2, n2 > 0 ? n2 : 0);
|
key);
|
||||||
|
mg_send(c, buf2, n2);
|
||||||
if (buf1 != mem1) free(buf1);
|
if (buf1 != mem1) free(buf1);
|
||||||
if (buf2 != mem2) free(buf2);
|
if (buf2 != mem2) free(buf2);
|
||||||
c->pfn = mg_ws_cb;
|
c->pfn = mg_ws_cb;
|
||||||
|
|||||||
+1
-1
@@ -106,7 +106,7 @@ static void iolog(struct mg_connection *c, char *buf, long n, bool r) {
|
|||||||
(char) ('0' + c->is_writable),
|
(char) ('0' + c->is_writable),
|
||||||
'\0'};
|
'\0'};
|
||||||
MG_LOG(log_level,
|
MG_LOG(log_level,
|
||||||
("%3lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
("%lu %s %d:%d %ld err %d (%s)", c->id, flags, (int) c->send.len,
|
||||||
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
(int) c->recv.len, n, MG_SOCK_ERRNO, strerror(errno)));
|
||||||
if (n == 0) {
|
if (n == 0) {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
|
|||||||
@@ -216,6 +216,7 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
|||||||
char nonce[16], key[30], mem1[128], mem2[256], *buf1 = mem1, *buf2 = mem2;
|
char nonce[16], key[30], mem1[128], mem2[256], *buf1 = mem1, *buf2 = mem2;
|
||||||
struct mg_str host = mg_url_host(url);
|
struct mg_str host = mg_url_host(url);
|
||||||
size_t n1 = 0, n2 = 0;
|
size_t n1 = 0, n2 = 0;
|
||||||
|
nonce[0] = key[0] = mem1[0] = mem2[0] = '\0';
|
||||||
if (fmt != NULL) {
|
if (fmt != NULL) {
|
||||||
va_list ap;
|
va_list ap;
|
||||||
va_start(ap, fmt);
|
va_start(ap, fmt);
|
||||||
@@ -223,7 +224,6 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
|||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
// Send handshake request
|
// Send handshake request
|
||||||
// mg_url_host(url, host, sizeof(host));
|
|
||||||
mg_random(nonce, sizeof(nonce));
|
mg_random(nonce, sizeof(nonce));
|
||||||
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
||||||
n2 = mg_asprintf(&buf2, sizeof(mem2),
|
n2 = mg_asprintf(&buf2, sizeof(mem2),
|
||||||
@@ -235,8 +235,9 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
|||||||
"Sec-WebSocket-Version: 13\r\n"
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
"Sec-WebSocket-Key: %s\r\n"
|
"Sec-WebSocket-Key: %s\r\n"
|
||||||
"\r\n",
|
"\r\n",
|
||||||
mg_url_uri(url), (int) host.len, host.ptr, n1, buf1, key);
|
mg_url_uri(url), (int) host.len, host.ptr, (int) n1, buf1,
|
||||||
mg_send(c, buf2, n2 > 0 ? n2 : 0);
|
key);
|
||||||
|
mg_send(c, buf2, n2);
|
||||||
if (buf1 != mem1) free(buf1);
|
if (buf1 != mem1) free(buf1);
|
||||||
if (buf2 != mem2) free(buf2);
|
if (buf2 != mem2) free(buf2);
|
||||||
c->pfn = mg_ws_cb;
|
c->pfn = mg_ws_cb;
|
||||||
|
|||||||
+1
-2
@@ -1345,8 +1345,7 @@ static void test_dns(void) {
|
|||||||
MG_INFO(("--------------"));
|
MG_INFO(("--------------"));
|
||||||
ASSERT(mg_dns_parse(d, sizeof(d), &dm) == 1);
|
ASSERT(mg_dns_parse(d, sizeof(d), &dm) == 1);
|
||||||
MG_INFO(("[%s]", dm.name));
|
MG_INFO(("[%s]", dm.name));
|
||||||
ASSERT(strcmp(dm.name, "com") == 0);
|
ASSERT(strcmp(dm.name, "com") == 0); // TODO(lsm): fix this
|
||||||
exit(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test_dns_timeout("udp://127.0.0.1:12345", "DNS timeout");
|
test_dns_timeout("udp://127.0.0.1:12345", "DNS timeout");
|
||||||
|
|||||||
Reference in new issue
Block a user