static void ev_handler(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case NS_HTTP_REQUEST: if (ns_vcmp(&hm->uri, "/api/v1/sum") == 0) { handle_sum_call(nc, hm); /* Handle RESTful call */ } else if (ns_vcmp(&hm->uri, "/printcontent") == 0) { char buf[100] = {0}; memcpy(buf, hm->body.p, sizeof(buf) - 1 < hm->body.len? sizeof(buf) - 1 : hm->body.len); printf("%s\n", buf); } else { ns_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ } break; default: break; } }
static void cb1(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; if (ev == NS_HTTP_REQUEST) { if (ns_vcmp(&hm->uri, "/foo") == 0) { ns_printf(nc, "HTTP/1.0 200 OK\n\n[%.*s %d]", (int) hm->uri.len, hm->uri.p, (int) hm->body.len); nc->flags |= NSF_FINISHED_SENDING_DATA; } else { static struct ns_serve_http_opts opts; opts.document_root = "."; ns_serve_http(nc, hm, opts); } } }
static void ev_handler(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; switch (ev) { case NS_HTTP_REQUEST: if (ns_vcmp(&hm->uri, "/api/v1/sum") == 0) { handle_sum_call(nc, hm); /* Handle RESTful call */ } else { ns_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ } nc->flags |= NSF_FINISHED_SENDING_DATA; break; default: break; } }
static void cb7(struct ns_connection *nc, int ev, void *ev_data) { struct http_message *hm = (struct http_message *) ev_data; struct ns_str *s; size_t size; char *data; if (ev == NS_HTTP_REPLY) { /* Make sure that we've downloaded this executable, byte-to-byte */ data = read_file(s_argv_0, &size); strcpy((char *) nc->user_data, data == NULL || size != hm->body.len || (s = ns_get_http_header(hm, "Content-Type")) == NULL || (ns_vcmp(s, "text/plain")) != 0 || memcmp(hm->body.p, data, size) != 0 ? "fail" : "success"); free(data); nc->flags |= NSF_CLOSE_IMMEDIATELY; } }