Skip to content

Instantly share code, notes, and snippets.

@schmurfy
Created September 14, 2018 09:54
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/1ee7c001553e4d5f970ec06bcb72fb6c to your computer and use it in GitHub Desktop.
Save schmurfy/1ee7c001553e4d5f970ec06bcb72fb6c to your computer and use it in GitHub Desktop.
Mongoose issue
#include "mongoose.h"
// gcc -o test -I mongoose/ mongoose/mongoose.c test.c
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;
}
break;
case MG_EV_HTTP_CHUNK:
nc->flags |= MG_F_DELETE_CHUNK;
fwrite(hm->body.p, 1, hm->body.len, stdout);
putchar('\n');
break;
case MG_EV_HTTP_REPLY:
nc->flags |= MG_F_CLOSE_IMMEDIATELY;
fwrite(hm->message.p, 1, hm->message.len, stdout);
putchar('\n');
s_exit_flag = 1;
break;
case MG_EV_CLOSE:
if (s_exit_flag == 0) {
printf("Server closed connection\n");
s_exit_flag = 1;
}
break;
default:
break;
}
}
int main(int argc, char *argv[]) {
struct mg_mgr mgr;
mg_mgr_init(&mgr, NULL);
mg_connect_http(&mgr, ev_handler, argv[0], NULL, NULL);
while( s_exit_flag == 0 ){
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