mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-15 10:29:47 +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
|
||||
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
|
||||
MG_ERROR(("%p %s", c->fd, (char *) ev_data));
|
||||
} else if (ev == MG_EV_WS_OPEN) {
|
||||
@@ -33,6 +35,7 @@ int main(void) {
|
||||
bool done = false; // Event handler flips it to true
|
||||
struct mg_connection *c; // Client connection
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_log_set("4");
|
||||
c = mg_ws_connect(&mgr, s_url, fn, &done, NULL); // Create client
|
||||
while (c && done == false) mg_mgr_poll(&mgr, 1000); // Wait for echo
|
||||
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),
|
||||
'\0'};
|
||||
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)));
|
||||
if (n == 0) {
|
||||
// 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;
|
||||
struct mg_str host = mg_url_host(url);
|
||||
size_t n1 = 0, n2 = 0;
|
||||
nonce[0] = key[0] = mem1[0] = mem2[0] = '\0';
|
||||
if (fmt != NULL) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
@@ -5077,7 +5078,6 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
||||
va_end(ap);
|
||||
}
|
||||
// Send handshake request
|
||||
// mg_url_host(url, host, sizeof(host));
|
||||
mg_random(nonce, sizeof(nonce));
|
||||
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
||||
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-Key: %s\r\n"
|
||||
"\r\n",
|
||||
mg_url_uri(url), (int) host.len, host.ptr, n1, buf1, key);
|
||||
mg_send(c, buf2, n2 > 0 ? n2 : 0);
|
||||
mg_url_uri(url), (int) host.len, host.ptr, (int) n1, buf1,
|
||||
key);
|
||||
mg_send(c, buf2, n2);
|
||||
if (buf1 != mem1) free(buf1);
|
||||
if (buf2 != mem2) free(buf2);
|
||||
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),
|
||||
'\0'};
|
||||
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)));
|
||||
if (n == 0) {
|
||||
// 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;
|
||||
struct mg_str host = mg_url_host(url);
|
||||
size_t n1 = 0, n2 = 0;
|
||||
nonce[0] = key[0] = mem1[0] = mem2[0] = '\0';
|
||||
if (fmt != NULL) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
@@ -223,7 +224,6 @@ struct mg_connection *mg_ws_connect(struct mg_mgr *mgr, const char *url,
|
||||
va_end(ap);
|
||||
}
|
||||
// Send handshake request
|
||||
// mg_url_host(url, host, sizeof(host));
|
||||
mg_random(nonce, sizeof(nonce));
|
||||
mg_base64_encode((unsigned char *) nonce, sizeof(nonce), key);
|
||||
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-Key: %s\r\n"
|
||||
"\r\n",
|
||||
mg_url_uri(url), (int) host.len, host.ptr, n1, buf1, key);
|
||||
mg_send(c, buf2, n2 > 0 ? n2 : 0);
|
||||
mg_url_uri(url), (int) host.len, host.ptr, (int) n1, buf1,
|
||||
key);
|
||||
mg_send(c, buf2, n2);
|
||||
if (buf1 != mem1) free(buf1);
|
||||
if (buf2 != mem2) free(buf2);
|
||||
c->pfn = mg_ws_cb;
|
||||
|
||||
+1
-2
@@ -1345,8 +1345,7 @@ static void test_dns(void) {
|
||||
MG_INFO(("--------------"));
|
||||
ASSERT(mg_dns_parse(d, sizeof(d), &dm) == 1);
|
||||
MG_INFO(("[%s]", dm.name));
|
||||
ASSERT(strcmp(dm.name, "com") == 0);
|
||||
exit(0);
|
||||
ASSERT(strcmp(dm.name, "com") == 0); // TODO(lsm): fix this
|
||||
}
|
||||
|
||||
test_dns_timeout("udp://127.0.0.1:12345", "DNS timeout");
|
||||
|
||||
Reference in new issue
Block a user