Skip to content

Instantly share code, notes, and snippets.

@schmurfy
Created September 25, 2018 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schmurfy/5115723157f1b7390b51e62e2fe83da8 to your computer and use it in GitHub Desktop.
Save schmurfy/5115723157f1b7390b51e62e2fe83da8 to your computer and use it in GitHub Desktop.
mongoose_chunked
#include "mongoose.h"
static int s_exit_flag = 0;
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
struct http_message *hm = (struct http_message *) ev_data;
switch (ev) {
case MG_EV_CONNECT:
if (*(int *) ev_data != 0) {
fprintf(stderr, "connect() failed: %s\n", strerror(*(int *) ev_data));
s_exit_flag = 1;
}
printf("connected.\n");
break;
case MG_EV_HTTP_CHUNK:
// nc->flags |= MG_F_DELETE_CHUNK;
// printf("chunk\n");
// if( hm->body.len > 0 ){
// // printf(" [[%zu]]> %.*s\n", hm->body.len, (int)hm->body.len, hm->body.p);
// printf(" %.*s\n", (int)hm->body.len, hm->body.p);
// } else {
// printf("**********\n");
// }
// fwrite(hm->body.p, 1, hm->body.len, stdout);
// putchar('\n');
break;
case MG_EV_HTTP_REPLY:
printf("reply\n");
fwrite(hm->message.p, 1, hm->message.len, stdout);
putchar('\n');
mg_printf(nc, "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
break;
case MG_EV_CLOSE:
if (s_exit_flag == 0) {
printf("Server closed connection\n");
s_exit_flag = 1;
}
printf("connection closed.\n");
break;
default:
break;
}
}
int main(int argc, char *argv[]) {
struct mg_mgr mgr;
mg_mgr_init(&mgr, NULL);
usleep(20000);
struct mg_connection *nc = mg_connect(&mgr, "tcp://127.0.0.1:10123", ev_handler);
mg_set_protocol_http_websocket(nc);
mg_printf(nc, "GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
while( s_exit_flag == 0 ){
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
#include "mongoose.h"
static struct mg_serve_http_opts s_http_server_opts;
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
struct http_message *hm = (struct http_message *) ev_data;
switch (ev) {
case MG_EV_TIMER:
mg_send_http_chunk(nc, "cccccccccccccccccccccccccc", 26);
mg_send_http_chunk(nc, "", 0);
// mg_set_timer(nc, mg_time() + 0.5 );
break;
case MG_EV_HTTP_REQUEST:
mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
// mg_printf_http_chunk(nc, "azertyuiopqsdfghjklmwxcvbn");
// mg_send_http_chunk(nc, "", 0);
// mg_send_http_chunk(nc, "aaaaaaaaaaaaaaaaaaaaaaaaaa", 26);
// mg_send_http_chunk(nc, "bbbbbbbbbbbbbbbbbbbbbbbbbb", 26);
// mg_send_http_chunk(nc, "", 0);
// mg_printf_http_chunk(nc, "azertyuiopqsdfghjklmwxcvbn");
// mg_printf_http_chunk(nc, "azertyuiopqsdfghjklmwxcvbn");
// mg_send_http_chunk(nc, "", 0);
// mg_printf_http_chunk(nc, "azertyuiopqsdfghjklmwxcvbn");
// mg_send_http_chunk(nc, "", 0);
mg_set_timer(nc, mg_time() + 0.5 );
break;
}
}
int main(int argc, char const *argv[])
{
struct mg_mgr mgr;
struct mg_connection *nc;
mg_mgr_init(&mgr, NULL);
nc = mg_bind(&mgr, "tcp://127.0.0.1:10123", ev_handler);
mg_set_protocol_http_websocket(nc);
while(1){
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment