Skip to content

Commit 396ba5c

Browse files
committed
Changed the order of handlers: protocol first, user second
1 parent 8dc62c2 commit 396ba5c

File tree

4 files changed

+22
-35
lines changed

4 files changed

+22
-35
lines changed

examples/http-streaming-client/main.c

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,53 +27,44 @@ static void fn(struct mg_connection *c, int ev, void *ev_data) {
2727
// Send request
2828
mg_printf(c,
2929
"GET %s HTTP/1.1\r\n"
30-
"Connection: keep-alive\r\n"
31-
"Keep-Alive: timeout=60\r\n"
30+
"Connection: close\r\n"
3231
"Host: %.*s\r\n"
3332
"\r\n",
3433
mg_url_uri(s_url), (int) host.len, host.ptr);
3534
} else if (ev == MG_EV_READ) {
36-
// Parse the incoming data ourselves. If we can parse the request,
37-
// store two size_t variables in the c->data: expected len and recv len.
38-
size_t *data = (size_t *) c->data;
39-
if (data[0]) { // Already parsed, simply print received data
40-
data[1] += c->recv.len;
41-
fwrite(c->recv.buf, 1, c->recv.len, stdout);
42-
c->recv.len = 0; // And cleanup the receive buffer. Streming!
43-
if (data[1] >= data[0]) * (bool *) c->fn_data = true;
44-
} else {
35+
// c->data[0] holds a flag, whether we have parsed the request already
36+
if (c->data[0] == 0) {
4537
struct mg_http_message hm;
4638
int n = mg_http_parse((char *) c->recv.buf, c->recv.len, &hm);
4739
if (n < 0) mg_error(c, "Bad response");
4840
if (n > 0) {
4941
fwrite(c->recv.buf + n, 1, c->recv.len - n, stdout); // Print body
50-
data[0] = n + hm.body.len;
51-
data[1] = c->recv.len;
5242
c->recv.len = 0; // Cleanup receive buffer
53-
if (data[1] >= data[0]) * (bool *) c->fn_data = true;
43+
c->data[0] = 1; // Request parsed, set the flag
5444
}
45+
} else {
46+
fwrite(c->recv.buf, 1, c->recv.len, stdout);
47+
c->recv.len = 0; // Cleanup the receive buffer
5548
}
5649
} else if (ev == MG_EV_CLOSE) {
57-
*(bool *) c->fn_data = true; // tell event loop to stop
50+
*(bool *) c->fn_data = true; // Done, tell event loop to stop
5851
} else if (ev == MG_EV_ERROR) {
59-
*(bool *) c->fn_data = true; // Error, tell event loop to stop
52+
*(bool *) c->fn_data = true; // Error, Tell event loop to stop
6053
}
6154
(void) ev_data;
6255
}
6356

6457
int main(int argc, char *argv[]) {
65-
struct mg_mgr mgr; // Event manager
66-
bool done = false; // Event handler flips it to true
67-
const char *log_level = getenv("V"); // Log level
58+
struct mg_mgr mgr; // Event manager
59+
bool done = false; // Event handler flips it to true
6860

69-
mg_mgr_init(&mgr); // Initialise event manager
70-
if (log_level == NULL) log_level = "2"; // If not set, set to DEBUG
71-
mg_log_set(atoi(log_level)); // Set to 0 to disable debug log
72-
if (argc > 1) s_url = argv[1]; // Use URL from command line
61+
mg_mgr_init(&mgr); // Initialise event manager
62+
mg_log_set(MG_LL_INFO); // Set to 0 to disable debug log
7363

74-
mg_http_connect(&mgr, s_url, fn, &done); // Create client connection
75-
while (!done) mg_mgr_poll(&mgr, 1000); // Infinite event loop
76-
mg_mgr_free(&mgr); // Free resources
64+
if (argc > 1) s_url = argv[1]; // Use URL from command line
65+
mg_connect(&mgr, s_url, fn, &done); // Create client connection
66+
while (!done) mg_mgr_poll(&mgr, 1000); // Infinite event loop
67+
mg_mgr_free(&mgr); // Free resources
7768

7869
return 0;
7970
}

mongoose.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,11 +1345,9 @@ void mg_call(struct mg_connection *c, int ev, void *ev_data) {
13451345
MG_PROF_ADD(c, names[ev]);
13461346
}
13471347
#endif
1348-
// Run user-defined handler first, in order to give it an ability
1349-
// to intercept processing (e.g. clean input buffer) before the
1350-
// protocol handler kicks in
1351-
if (c->fn != NULL) c->fn(c, ev, ev_data);
1348+
// Fire protocol handler first, user handler second. See #2559
13521349
if (c->pfn != NULL) c->pfn(c, ev, ev_data);
1350+
if (c->fn != NULL) c->fn(c, ev, ev_data);
13531351
}
13541352

13551353
void mg_error(struct mg_connection *c, const char *fmt, ...) {

src/event.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ void mg_call(struct mg_connection *c, int ev, void *ev_data) {
1616
MG_PROF_ADD(c, names[ev]);
1717
}
1818
#endif
19-
// Run user-defined handler first, in order to give it an ability
20-
// to intercept processing (e.g. clean input buffer) before the
21-
// protocol handler kicks in
22-
if (c->fn != NULL) c->fn(c, ev, ev_data);
19+
// Fire protocol handler first, user handler second. See #2559
2320
if (c->pfn != NULL) c->pfn(c, ev, ev_data);
21+
if (c->fn != NULL) c->fn(c, ev, ev_data);
2422
}
2523

2624
void mg_error(struct mg_connection *c, const char *fmt, ...) {

test/unit_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1407,7 +1407,7 @@ static void test_http_no_content_length(void) {
14071407
for (i = 0; i < 1000 && strchr(buf2, 'c') == NULL; i++) mg_mgr_poll(&mgr, 10);
14081408
MG_INFO(("[%s] [%s]", buf1, buf2));
14091409
ASSERT(strcmp(buf1, "mc") == 0);
1410-
ASSERT(strcmp(buf2, "cm") == 0); // See #1475
1410+
ASSERT(strcmp(buf2, "mc") == 0);
14111411
mg_mgr_free(&mgr);
14121412
ASSERT(mgr.conns == NULL);
14131413
}

0 commit comments

Comments
 (0)