MCPcopy Create free account
hub / github.com/cesanta/mongoose / http_ev_handler

Function http_ev_handler

tutorials/http/singleton-client/main.c:44–87  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

42 "-----END CERTIFICATE-----"
43
44static void http_ev_handler(struct mg_connection *c, int ev, void *ev_data) {
45 uint64_t *start_time = (uint64_t *) c->data; // Stored start time
46 bool *success = (bool *) (start_time + 1); // Stored success flag
47 if (ev == MG_EV_OPEN) {
48 *start_time = mg_millis();
49 MG_DEBUG(("%lu Connection started, time: %llu", c->id, *start_time));
50 } else if (ev == MG_EV_POLL) {
51 if (*start_time + s_connect_timeout_ms < mg_millis()) {
52 mg_error(c, "Connect timeout");
53 }
54 } else if (ev == MG_EV_CONNECT) {
55 // Connected to server. Extract host name from URL
56 struct mg_str host = mg_url_host(s_url);
57 if (c->is_tls) {
58 struct mg_tls_opts opts = {};
59 opts.ca = mg_str(TLS_CA);
60 opts.name = host;
61 mg_tls_init(c, &opts);
62 }
63 // Send request
64 int content_length = s_post_data ? strlen(s_post_data) : 0;
65 mg_printf(c,
66 "%s %s HTTP/1.0\r\n"
67 "Host: %.*s\r\n"
68 "Content-Type: octet-stream\r\n"
69 "Content-Length: %d\r\n"
70 "\r\n",
71 s_post_data ? "POST" : "GET", mg_url_uri(s_url), host.len,
72 host.buf, content_length);
73 mg_send(c, s_post_data, content_length);
74 } else if (ev == MG_EV_HTTP_MSG) {
75 // Response is received. Print it
76 struct mg_http_message *hm = (struct mg_http_message *) ev_data;
77 MG_INFO(("%lu Got response %lu bytes %lu", c->id, hm->body.len, c->rtls.len));
78 *success = true;
79 c->is_closing = 1;
80 } else if (ev == MG_EV_ERROR) {
81 MG_ERROR(("%lu Connection error: %s", c->id, ev_data));
82 } else if (ev == MG_EV_CLOSE) {
83 MG_DEBUG(("%lu Connection closed %lu", c->id, mg_millis() - *start_time));
84 if (*success == false) MG_ERROR(("%lu NO RESPONSE %lu", c->id, c->rtls.len));
85 if (c->is_draining == 0) s_conn = NULL;
86 }
87}
88
89static void timer_fn(void *arg) {
90 struct mg_mgr *mgr = (struct mg_mgr *) arg;

Callers

nothing calls this directly

Calls 8

mg_strClass · 0.70
mg_millisFunction · 0.50
mg_errorFunction · 0.50
mg_url_hostFunction · 0.50
mg_tls_initFunction · 0.50
mg_printfFunction · 0.50
mg_url_uriFunction · 0.50
mg_sendFunction · 0.50

Tested by

no test coverage detected