mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-09 16:21:45 +00:00
Event log
This commit is contained in:
1 parent
90051642ef
commit
8534053e57
9 files changed
+182
-96
No files matched your search
+26
-1
@@ -32,10 +32,26 @@ struct mg_field_set {
|
||||
int write_level;
|
||||
};
|
||||
|
||||
struct mg_dash_custom_handler {
|
||||
struct mg_dash_custom_handler *next;
|
||||
struct mg_str uri_pattern;
|
||||
mg_event_handler_t handler;
|
||||
};
|
||||
|
||||
struct mg_dash {
|
||||
struct mg_field_set *sets;
|
||||
struct mg_dash_custom_handler *custom_handlers;
|
||||
};
|
||||
|
||||
#define MG_DASH_REGISTER_CUSTOM_HANDLER(dash_, uri_, fn_) \
|
||||
do { \
|
||||
static struct mg_dash_custom_handler ch_; \
|
||||
ch_.next = (dash_)->custom_handlers; \
|
||||
ch_.uri_pattern = mg_str(uri_); \
|
||||
ch_.handler = (fn_); \
|
||||
(dash_)->custom_handlers = &ch_; \
|
||||
} while (0)
|
||||
|
||||
static inline struct mg_str trimq(struct mg_str s) { // Trim double quotes
|
||||
if (s.len > 1 && s.buf[0] == '"') s.len -= 2, s.buf++;
|
||||
return s;
|
||||
@@ -249,12 +265,21 @@ static inline void mg_dash_ev_handler(struct mg_connection *c, int ev,
|
||||
} else if (mg_match(hm->uri, mg_str("/api/set"), NULL)) {
|
||||
int count = mg_dash_apply(c, dash, hm->body);
|
||||
mg_http_reply(c, 200, MG_JSON_HEADERS, "%d\n", count);
|
||||
} else {
|
||||
} else if (mg_match(hm->uri, mg_str("/"), NULL)) {
|
||||
struct mg_http_serve_opts opts;
|
||||
memset(&opts, 0, sizeof(opts));
|
||||
opts.fs = &mg_fs_packed;
|
||||
mg_mem_files = mg_packed_files;
|
||||
mg_http_serve_file(c, hm, "/dashboard.html", &opts);
|
||||
} else {
|
||||
struct mg_dash_custom_handler *ch = dash->custom_handlers;
|
||||
for (ch = dash->custom_handlers; ch != NULL; ch = ch->next) {
|
||||
if (mg_match(hm->uri, ch->uri_pattern, NULL)) {
|
||||
ch->handler(c, ev, ev_data);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ch == NULL) mg_http_reply(c, 404, MG_JSON_HEADERS, "Not Found");
|
||||
}
|
||||
} else if (ev == MG_EV_WS_MSG) {
|
||||
// Add this to automatically handle "get" and "set" JSON-RPC calls
|
||||
|
||||
+19
-17
@@ -1,15 +1,12 @@
|
||||
#include "http.h"
|
||||
#include "arch.h"
|
||||
#include "base64.h"
|
||||
#include "fmt.h"
|
||||
#include "json.h"
|
||||
#include "log.h"
|
||||
#include "net.h"
|
||||
#include "printf.h"
|
||||
#include "ssi.h"
|
||||
#include "util.h"
|
||||
#include "version.h"
|
||||
#include "ws.h"
|
||||
|
||||
static int mg_ncasecmp(const char *s1, const char *s2, size_t len) {
|
||||
int diff = 0;
|
||||
@@ -255,11 +252,14 @@ static bool mg_http_parse_headers(const char *s, const char *end,
|
||||
v.len--; // Trim spaces
|
||||
}
|
||||
// detect duplicated headers -> discard
|
||||
if (((mg_strcasecmp(k, mg_str("Content-Length")) == 0) && (++cl_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Transfer-Encoding")) == 0) && (++te_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Authorization")) == 0) && (++auth_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Cookie")) == 0) && (++cookie_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Connection")) == 0) && (++conn_count > 1)))
|
||||
if (((mg_strcasecmp(k, mg_str("Content-Length")) == 0) &&
|
||||
(++cl_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Transfer-Encoding")) == 0) &&
|
||||
(++te_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Authorization")) == 0) &&
|
||||
(++auth_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Cookie")) == 0) && (++cookie_count > 1)) ||
|
||||
((mg_strcasecmp(k, mg_str("Connection")) == 0) && (++conn_count > 1)))
|
||||
return false;
|
||||
// MG_INFO(("--HH [%.*s] [%.*s]", (int) k.len, k.buf, (int) v.len, v.buf));
|
||||
h[i].name = k, h[i].value = v; // Success. Assign values
|
||||
@@ -296,7 +296,7 @@ int mg_http_parse(const char *s, size_t len, struct mg_http_message *hm) {
|
||||
version_prefix_valid =
|
||||
hm->proto.len > 5 && (mg_ncasecmp(hm->proto.buf, "HTTP/", 5) == 0);
|
||||
if (!is_response && !version_prefix_valid)
|
||||
return -1; // no version detected in request
|
||||
return -1; // no version detected in request
|
||||
if (!is_response && hm->proto.len > 0 &&
|
||||
(!version_prefix_valid || hm->proto.len != 8 ||
|
||||
(hm->proto.buf[5] < '0' || hm->proto.buf[5] > '9') ||
|
||||
@@ -579,12 +579,14 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
||||
const char *path,
|
||||
const struct mg_http_serve_opts *opts) {
|
||||
char etag[64], tmp[MG_PATH_MAX];
|
||||
struct mg_fs *fs = opts->fs == NULL ? &mg_fs_posix : opts->fs;
|
||||
struct mg_fs *fs = opts && opts->fs ? opts->fs : &mg_fs_posix;
|
||||
struct mg_fd *fd = NULL;
|
||||
size_t size = 0;
|
||||
time_t mtime = 0;
|
||||
const char *mime_types = opts && opts->mime_types ? opts->mime_types : NULL;
|
||||
const char *hdrs = opts && opts->extra_headers ? opts->extra_headers : "";
|
||||
struct mg_str *inm = NULL;
|
||||
struct mg_str mime = guess_content_type(mg_str(path), opts->mime_types);
|
||||
struct mg_str mime = guess_content_type(mg_str(path), mime_types);
|
||||
bool gzip = false;
|
||||
|
||||
if (path != NULL) {
|
||||
@@ -602,21 +604,21 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
||||
}
|
||||
|
||||
// Failed to open, and page404 is configured? Open it, then
|
||||
if (fd == NULL && opts->page404 != NULL) {
|
||||
if (fd == NULL && opts && opts->page404) {
|
||||
fd = mg_fs_open(fs, opts->page404, MG_FS_READ);
|
||||
path = opts->page404;
|
||||
mime = guess_content_type(mg_str(path), opts->mime_types);
|
||||
mime = guess_content_type(mg_str(path), mime_types);
|
||||
}
|
||||
|
||||
if (fd == NULL || fs->st(path, &size, &mtime) == 0) {
|
||||
mg_http_reply(c, 404, opts->extra_headers, "Not found\n");
|
||||
mg_http_reply(c, 404, hdrs, "Not found\n");
|
||||
mg_fs_close(fd);
|
||||
// NOTE: mg_http_etag() call should go first!
|
||||
} else if (mg_http_etag(etag, sizeof(etag), size, mtime) != NULL &&
|
||||
(inm = mg_http_get_header(hm, "If-None-Match")) != NULL &&
|
||||
mg_strcasecmp(*inm, mg_str(etag)) == 0) {
|
||||
mg_fs_close(fd);
|
||||
mg_http_reply(c, 304, opts->extra_headers, "");
|
||||
mg_http_reply(c, 304, hdrs, "");
|
||||
} else {
|
||||
int n, status = 200;
|
||||
char range[100];
|
||||
@@ -650,11 +652,11 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
|
||||
"%s%s%s\r\n",
|
||||
status, mg_http_status_code_str(status), (int) mime.len, mime.buf,
|
||||
etag, (uint64_t) cl, gzip ? "Content-Encoding: gzip\r\n" : "",
|
||||
range, opts->extra_headers ? opts->extra_headers : "");
|
||||
range, hdrs);
|
||||
if (mg_strcasecmp(hm->method, mg_str("HEAD")) == 0 || c->is_closing) {
|
||||
c->is_resp = 0;
|
||||
mg_fs_close(fd);
|
||||
} else { // start serving static content only if not closing, see #3354
|
||||
} else { // start serving static content only if not closing, see #3354
|
||||
// Track to-be-sent content length at the end of c->data, aligned
|
||||
size_t *clp = (size_t *) &c->data[(sizeof(c->data) - sizeof(size_t)) /
|
||||
sizeof(size_t) * sizeof(size_t)];
|
||||
|
||||
Reference in new issue
Block a user