static int websocket_client_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len, void *user_data) { struct mg_context *ctx = mg_get_context(conn); struct tclient_data *pclient_data = (struct tclient_data *)mg_get_user_data(ctx); (void)user_data; /* TODO: check this */ ck_assert(pclient_data != NULL); ck_assert_int_eq(flags, (int)(128 | 1)); printf("Client received data from server: "); fwrite(data, 1, data_len, stdout); printf("\n"); pclient_data->data = malloc(data_len); ck_assert(pclient_data->data != NULL); memcpy(pclient_data->data, data, data_len); pclient_data->len = data_len; return 1; }
static void websocket_client_close_handler(const struct mg_connection *conn, void * user_data) { struct mg_context *ctx = mg_get_context(conn); struct tclient_data *pclient_data = (struct tclient_data *) mg_get_user_data(ctx); printf("Client: Close handler\n"); pclient_data->closed++; }
static void websocket_client_close_handler(const struct mg_connection *conn, void *user_data) { struct mg_context *ctx = mg_get_context(conn); struct tclient_data *pclient_data = (struct tclient_data *)mg_get_user_data(ctx); (void)user_data; /* TODO: check this */ ck_assert(pclient_data != NULL); printf("Client: Close handler\n"); pclient_data->closed++; }
/* Web server event handler - return the current .png image */ static void *ccwin_ehandler(enum mg_event event, struct mg_connection *conn) { const struct mg_request_info *request_info = mg_get_request_info(conn); chws *p = (chws *)mg_get_user_data(conn); char *cp; char sbuf[200]; debugr2((errout,"ccwin_ehandler()\n")); if (event != MG_NEW_REQUEST) { return NULL; } debugr2((errout,"Event: uri = '%s'\n",request_info->uri)); #ifdef SEND_TEST_FILE #pragma message("############################# ccwin.c SEND_TEST_FILE defined ! ##") return NULL; #endif if (p->ibuf != NULL && p->ilen > 0 && (cp = strrchr(request_info->uri, '.')) != NULL && strcmp(cp, ".png") == 0) { debugr2((errout,"Event: Loading %s\n",request_info->uri)); debugr2((errout,"Returning current png size %d bytes\n",(int)p->ilen)); sprintf(sbuf, "HTTP/1.1 200 OK\r\n" "Content-Type: image/png\r\n" "Content-Length: %d\r\n" "\r\n" ,(int)p->ilen); mg_write(conn, sbuf, strlen(sbuf)); mg_write(conn, p->ibuf, p->ilen); } else { debugr2((errout,"Bad request or png - returning 404\n")); sprintf(sbuf, "HTTP/1.0 404 Not Found\r\n" "\r\n" "<html><body><h1>Error 404 - Not Found</h1></body></html>"); mg_write(conn, sbuf, strlen(sbuf)); } return "yes"; }
END_TEST static int log_msg_func(const struct mg_connection *conn, const char *message) { struct mg_context *ctx; char *ud; ck_assert(conn != NULL); ctx = mg_get_context(conn); ck_assert(ctx != NULL); ud = (char *)mg_get_user_data(ctx); strncpy(ud, message, 255); ud[255] = 0; return 1; }
static int request_test_handler(struct mg_connection *conn, void *cbdata) { int i; char chunk_data[32]; const struct mg_request_info *ri; struct mg_context *ctx; void *ud, *cud; ctx = mg_get_context(conn); ud = mg_get_user_data(ctx); ri = mg_get_request_info(conn); ck_assert(ri != NULL); ck_assert(ctx == g_ctx); ck_assert(ud == &g_ctx); mg_set_user_connection_data(conn, (void *)6543); cud = mg_get_user_connection_data(conn); ck_assert_ptr_eq((void *)cud, (void *)6543); ck_assert_ptr_eq((void *)cbdata, (void *)7); strcpy(chunk_data, "123456789A123456789B123456789C"); mg_printf(conn, "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "Content-Type: text/plain\r\n\r\n"); for (i = 1; i <= 10; i++) { mg_printf(conn, "%x\r\n", i); mg_write(conn, chunk_data, (unsigned)i); mg_printf(conn, "\r\n"); } mg_printf(conn, "0\r\n\r\n"); return 1; }
static void *twiliomax_mongoose_callback(enum mg_event event, struct mg_connection *conn) { t_twiliomax *x = (t_twiliomax*)mg_get_user_data(conn); if (event == MG_NEW_REQUEST) { char post_data[1024], sms_from[sizeof(post_data)], sms_body[sizeof(post_data)]; int post_data_len; // Read POST data post_data_len = mg_read(conn, post_data, sizeof(post_data)); mg_get_var(post_data, post_data_len, "From", sms_from, sizeof(sms_from)); mg_get_var(post_data, post_data_len, "Body", sms_body, sizeof(sms_body)); if (strlen(sms_body) > 0 && strlen(sms_from) > 0) { t_atom sms_atoms[2]; atom_setsym(&sms_atoms[0], gensym(sms_from)); atom_setsym(&sms_atoms[1], gensym(sms_body)); outlet_anything(x->m_outlet1, gensym("sms"), 2, sms_atoms); } mg_printf(conn, "HTTP/1.0 200 OK\r\n" "Content-Type: application/xml\r\n\r\n" "%s", twilio_response); return ""; } else { return NULL; } }