mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-16 10:39:49 +00:00
Remove mg_http_run_simple_server - move to docs
This commit is contained in:
1 parent
24a330c750
commit
9ac09da1d2
4 files changed
-122
No files matched your search
-60
@@ -2560,66 +2560,6 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
|
||||
(void) ev_data;
|
||||
}
|
||||
|
||||
// Helper function for mg_hfn, see below
|
||||
static void mg_hfn_push_data(struct mg_connection *c) {
|
||||
size_t left = *(size_t *) c->data; // Bytes left to send
|
||||
if (left > 0 && c->send.len < MG_IO_SIZE) {
|
||||
const char chunk[] = "abcdefghijklmnopqrstubwxyz0123456789\n";
|
||||
size_t cs = sizeof(chunk) - 1;
|
||||
while (left >= cs && c->send.len < MG_IO_SIZE * 2) {
|
||||
mg_send(c, chunk, cs);
|
||||
left -= cs;
|
||||
}
|
||||
if (left < cs) {
|
||||
if (left > 0) mg_send(c, chunk, left - 1);
|
||||
if (left > 0) mg_send(c, "\n", 1);
|
||||
left = 0;
|
||||
}
|
||||
*(size_t *) c->data = left;
|
||||
if (left == 0) c->is_resp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Implement a simple server with the following endpoints:
|
||||
// URI POST data Description
|
||||
// /debug {"level": 0} Set server debug level, from 0 to 4
|
||||
// /quit n/a Return from mg_http_run_simple_server()
|
||||
// /?SIZE n/a Send a text response, default size is 256
|
||||
static void mg_hfn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_match(hm->uri, mg_str("/quit"), NULL)) {
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
c->is_draining = 1;
|
||||
c->data[sizeof(size_t)] = 'X';
|
||||
} else if (mg_match(hm->uri, mg_str("/debug"), NULL)) {
|
||||
int level = (int) mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG);
|
||||
mg_log_set(level);
|
||||
mg_http_reply(c, 200, "", "Debug level set to %d\n", level);
|
||||
} else {
|
||||
size_t size = (size_t) mg_json_get_long(hm->query, "$", 256);
|
||||
if (size == 0) size = 256;
|
||||
*(size_t *) c->data = size;
|
||||
c->is_resp = 1;
|
||||
mg_printf(c, "HTTP/1.1 200 OK\nContent-Length: %lu\n\n", size);
|
||||
mg_hfn_push_data(c);
|
||||
}
|
||||
} else if (ev == MG_EV_WRITE) {
|
||||
mg_hfn_push_data(c);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
if (c->data[sizeof(size_t)] == 'X') *(bool *) c->fn_data = true;
|
||||
}
|
||||
}
|
||||
|
||||
void mg_http_run_simple_server(const char *url) {
|
||||
struct mg_mgr mgr;
|
||||
bool done = false;
|
||||
mg_mgr_init(&mgr);
|
||||
if (mg_http_listen(&mgr, url, mg_hfn, &done) == NULL) done = true;
|
||||
while (done == false) mg_mgr_poll(&mgr, 100);
|
||||
mg_mgr_free(&mgr);
|
||||
}
|
||||
|
||||
struct mg_connection *mg_http_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, http_cb, NULL);
|
||||
|
||||
@@ -1750,7 +1750,6 @@ void mg_http_bauth(struct mg_connection *, const char *user, const char *pass);
|
||||
struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v);
|
||||
size_t mg_http_next_multipart(struct mg_str, size_t, struct mg_http_part *);
|
||||
int mg_http_status(const struct mg_http_message *hm);
|
||||
void mg_http_run_simple_server(const char *url);
|
||||
|
||||
|
||||
void mg_http_serve_ssi(struct mg_connection *c, const char *root,
|
||||
|
||||
-60
@@ -1119,66 +1119,6 @@ static void http_cb(struct mg_connection *c, int ev, void *ev_data) {
|
||||
(void) ev_data;
|
||||
}
|
||||
|
||||
// Helper function for mg_hfn, see below
|
||||
static void mg_hfn_push_data(struct mg_connection *c) {
|
||||
size_t left = *(size_t *) c->data; // Bytes left to send
|
||||
if (left > 0 && c->send.len < MG_IO_SIZE) {
|
||||
const char chunk[] = "abcdefghijklmnopqrstubwxyz0123456789\n";
|
||||
size_t cs = sizeof(chunk) - 1;
|
||||
while (left >= cs && c->send.len < MG_IO_SIZE * 2) {
|
||||
mg_send(c, chunk, cs);
|
||||
left -= cs;
|
||||
}
|
||||
if (left < cs) {
|
||||
if (left > 0) mg_send(c, chunk, left - 1);
|
||||
if (left > 0) mg_send(c, "\n", 1);
|
||||
left = 0;
|
||||
}
|
||||
*(size_t *) c->data = left;
|
||||
if (left == 0) c->is_resp = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Implement a simple server with the following endpoints:
|
||||
// URI POST data Description
|
||||
// /debug {"level": 0} Set server debug level, from 0 to 4
|
||||
// /quit n/a Return from mg_http_run_simple_server()
|
||||
// /?SIZE n/a Send a text response, default size is 256
|
||||
static void mg_hfn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_match(hm->uri, mg_str("/quit"), NULL)) {
|
||||
mg_http_reply(c, 200, "", "ok\n");
|
||||
c->is_draining = 1;
|
||||
c->data[sizeof(size_t)] = 'X';
|
||||
} else if (mg_match(hm->uri, mg_str("/debug"), NULL)) {
|
||||
int level = (int) mg_json_get_long(hm->body, "$.level", MG_LL_DEBUG);
|
||||
mg_log_set(level);
|
||||
mg_http_reply(c, 200, "", "Debug level set to %d\n", level);
|
||||
} else {
|
||||
size_t size = (size_t) mg_json_get_long(hm->query, "$", 256);
|
||||
if (size == 0) size = 256;
|
||||
*(size_t *) c->data = size;
|
||||
c->is_resp = 1;
|
||||
mg_printf(c, "HTTP/1.1 200 OK\nContent-Length: %lu\n\n", size);
|
||||
mg_hfn_push_data(c);
|
||||
}
|
||||
} else if (ev == MG_EV_WRITE) {
|
||||
mg_hfn_push_data(c);
|
||||
} else if (ev == MG_EV_CLOSE) {
|
||||
if (c->data[sizeof(size_t)] == 'X') *(bool *) c->fn_data = true;
|
||||
}
|
||||
}
|
||||
|
||||
void mg_http_run_simple_server(const char *url) {
|
||||
struct mg_mgr mgr;
|
||||
bool done = false;
|
||||
mg_mgr_init(&mgr);
|
||||
if (mg_http_listen(&mgr, url, mg_hfn, &done) == NULL) done = true;
|
||||
while (done == false) mg_mgr_poll(&mgr, 100);
|
||||
mg_mgr_free(&mgr);
|
||||
}
|
||||
|
||||
struct mg_connection *mg_http_connect(struct mg_mgr *mgr, const char *url,
|
||||
mg_event_handler_t fn, void *fn_data) {
|
||||
return mg_connect_svc(mgr, url, fn, fn_data, http_cb, NULL);
|
||||
|
||||
@@ -63,4 +63,3 @@ void mg_http_bauth(struct mg_connection *, const char *user, const char *pass);
|
||||
struct mg_str mg_http_get_header_var(struct mg_str s, struct mg_str v);
|
||||
size_t mg_http_next_multipart(struct mg_str, size_t, struct mg_http_part *);
|
||||
int mg_http_status(const struct mg_http_message *hm);
|
||||
void mg_http_run_simple_server(const char *url);
|
||||
Reference in new issue
Block a user