From f41c9a2a1f59d90913757b080bf32fac27b2c53d Mon Sep 17 00:00:00 2001 From: Pavel Pimenov Date: Wed, 23 Jul 2014 11:26:20 +0400 Subject: [PATCH 1/5] stat - function result cache --- mongoose.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/mongoose.c b/mongoose.c index e8bfb4cd..270b7db2 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2332,6 +2332,7 @@ static int convert_uri_to_file_name(struct connection *conn, char *buf, const char *rewrites = conn->server->config_options[URL_REWRITES]; const char *root = conn->server->config_options[DOCUMENT_ROOT]; #ifndef MONGOOSE_NO_CGI + file_stat_t st_cgi; const char *cgi_pat = conn->server->config_options[CGI_PATTERN]; char *p; #endif @@ -2375,7 +2376,7 @@ static int convert_uri_to_file_name(struct connection *conn, char *buf, if (*p == '/') { *p = '\0'; if (mg_match_prefix(cgi_pat, strlen(cgi_pat), buf) > 0 && - !stat(buf, st)) { + !stat(buf, &st_cgi)) { DBG(("!!!! [%s]", buf)); *p = '/'; conn->path_info = mg_strdup(p); @@ -3501,16 +3502,15 @@ void mg_send_digest_auth_request(struct mg_connection *c) { // Use the global passwords file, if specified by auth_gpass option, // or search for .htpasswd in the requested directory. -static FILE *open_auth_file(struct connection *conn, const char *path) { +static FILE *open_auth_file(struct connection *conn, const char *path, int is_directory) { char name[MAX_PATH_SIZE]; const char *p, *gpass = conn->server->config_options[GLOBAL_AUTH_FILE]; - file_stat_t st; FILE *fp = NULL; if (gpass != NULL) { // Use global passwords file fp = fopen(gpass, "r"); - } else if (!stat(path, &st) && S_ISDIR(st.st_mode)) { + } else if (is_directory) { mg_snprintf(name, sizeof(name), "%s%c%s", path, '/', PASSWORDS_FILE_NAME); fp = fopen(name, "r"); } else { @@ -3800,11 +3800,11 @@ int mg_authorize_digest(struct mg_connection *c, FILE *fp) { // Return 1 if request is authorised, 0 otherwise. -static int is_authorized(struct connection *conn, const char *path) { +static int is_authorized(struct connection *conn, const char *path, int is_directory) { FILE *fp; int authorized = MG_TRUE; - if ((fp = open_auth_file(conn, path)) != NULL) { + if ((fp = open_auth_file(conn, path, is_directory)) != NULL) { authorized = mg_authorize_digest(&conn->mg_conn, fp); fclose(fp); } @@ -4143,11 +4143,10 @@ static void proxify_connection(struct connection *conn) { } #ifndef MONGOOSE_NO_FILESYSTEM -void mg_send_file(struct mg_connection *c, const char *file_name) { +void mg_send_file_internal(struct mg_connection *c, const char *file_name, file_stat_t* st, int exists) { struct connection *conn = MG_CONN_2_CONN(c); - file_stat_t st; char path[MAX_PATH_SIZE]; - int exists = 0, is_directory = 0; + const int is_directory = S_ISDIR(st->st_mode); #ifndef MONGOOSE_NO_CGI const char *cgi_pat = conn->server->config_options[CGI_PATTERN]; #else @@ -4160,8 +4159,6 @@ void mg_send_file(struct mg_connection *c, const char *file_name) { #endif mg_snprintf(path, sizeof(path), "%s", file_name); - exists = stat(path, &st) == 0; - is_directory = S_ISDIR(st.st_mode); if (!exists || must_hide_file(conn, path)) { send_http_error(conn, 404, NULL); @@ -4171,7 +4168,7 @@ void mg_send_file(struct mg_connection *c, const char *file_name) { mg_printf(&conn->mg_conn, "HTTP/1.1 301 Moved Permanently\r\n" "Location: %s/\r\n\r\n", conn->mg_conn.uri); close_local_endpoint(conn); - } else if (is_directory && !find_index_file(conn, path, sizeof(path), &st)) { + } else if (is_directory && !find_index_file(conn, path, sizeof(path), st)) { if (!mg_strcasecmp(dir_lst, "yes")) { #ifndef MONGOOSE_NO_DIRECTORY_LISTING send_directory_listing(conn, path); @@ -4193,16 +4190,21 @@ void mg_send_file(struct mg_connection *c, const char *file_name) { path) > 0) { handle_ssi_request(conn, path); #endif - } else if (is_not_modified(conn, &st)) { + } else if (is_not_modified(conn, st)) { send_http_error(conn, 304, NULL); } else if ((conn->endpoint.fd = open(path, O_RDONLY | O_BINARY)) != -1) { // O_BINARY is required for Windows, otherwise in default text mode // two bytes \r\n will be read as one. - open_file_endpoint(conn, path, &st); + open_file_endpoint(conn, path, st); } else { send_http_error(conn, 404, NULL); } } +void mg_send_file(struct mg_connection *c, const char *file_name) { + file_stat_t st; + const int exists = stat(file_name, &st) == 0; + mg_send_file_internal(c, file_name, &st, exists); +} #endif // !MONGOOSE_NO_FILESYSTEM static void open_local_endpoint(struct connection *conn, int skip_user) { @@ -4265,7 +4267,7 @@ static void open_local_endpoint(struct connection *conn, int skip_user) { } else if (conn->server->config_options[DOCUMENT_ROOT] == NULL) { send_http_error(conn, 404, NULL); #ifndef MONGOOSE_NO_AUTH - } else if ((!is_dav_request(conn) && !is_authorized(conn, path)) || + } else if ((!is_dav_request(conn) && !is_authorized(conn, path, exists && S_ISDIR(st.st_mode))) || (is_dav_request(conn) && !is_authorized_for_dav(conn))) { mg_send_digest_auth_request(&conn->mg_conn); close_local_endpoint(conn); @@ -4283,7 +4285,7 @@ static void open_local_endpoint(struct connection *conn, int skip_user) { handle_put(conn, path); #endif } else { - mg_send_file(&conn->mg_conn, path); + mg_send_file_internal(&conn->mg_conn, path, &st, exists); } #endif // MONGOOSE_NO_FILESYSTEM } From bc4e47630a1d93a83ad1777b1d3f656140f26431 Mon Sep 17 00:00:00 2001 From: "David M." Date: Sat, 26 Jul 2014 20:55:59 -0400 Subject: [PATCH 2/5] Set the correct json content-type Chance text/json to application/json to comply with the RFC. See http://www.ietf.org/rfc/rfc4627.txt for more information. --- mongoose.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mongoose.c b/mongoose.c index e8bfb4cd..7a0bf849 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1342,7 +1342,7 @@ static const struct { {".ogg", 4, "application/ogg"}, {".ram", 4, "audio/x-pn-realaudio"}, {".xml", 4, "text/xml"}, - {".json", 5, "text/json"}, + {".json", 5, "application/json"}, {".xslt", 5, "application/xml"}, {".xsl", 4, "application/xml"}, {".ra", 3, "audio/x-pn-realaudio"}, From 2108ec5a0cfc8fc15a1b8db4799df81370f99198 Mon Sep 17 00:00:00 2001 From: Pavel Pimenov Date: Sun, 27 Jul 2014 12:44:08 +0400 Subject: [PATCH 3/5] Fix style for pointer --- mongoose.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/mongoose.c b/mongoose.c index 270b7db2..a0f4090e 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2545,7 +2545,7 @@ static void SHA1Transform(uint32_t state[5], const unsigned char buffer[64]) { (void) a; (void) b; (void) c; (void) d; (void) e; } -static void SHA1Init(SHA1_CTX* context) { +static void SHA1Init(SHA1_CTX *context) { context->state[0] = 0x67452301; context->state[1] = 0xEFCDAB89; context->state[2] = 0x98BADCFE; @@ -2554,7 +2554,7 @@ static void SHA1Init(SHA1_CTX* context) { context->count[0] = context->count[1] = 0; } -static void SHA1Update(SHA1_CTX* context, const unsigned char* data, +static void SHA1Update(SHA1_CTX *context, const unsigned char *data, uint32_t len) { uint32_t i, j; @@ -2575,7 +2575,7 @@ static void SHA1Update(SHA1_CTX* context, const unsigned char* data, memcpy(&context->buffer[j], &data[i], len - i); } -static void SHA1Final(unsigned char digest[20], SHA1_CTX* context) { +static void SHA1Final(unsigned char digest[20], SHA1_CTX *context) { unsigned i; unsigned char finalcount[8], c; @@ -2691,7 +2691,7 @@ static int deliver_websocket_frame(struct connection *conn) { return buffered; } -size_t mg_websocket_write(struct mg_connection* conn, int opcode, +size_t mg_websocket_write(struct mg_connection *conn, int opcode, const char *data, size_t data_len) { unsigned char mem[4192], *copy = mem; size_t copy_len = 0; @@ -2741,7 +2741,7 @@ size_t mg_websocket_write(struct mg_connection* conn, int opcode, return MG_CONN_2_CONN(conn)->ns_conn->send_iobuf.len; } -size_t mg_websocket_printf(struct mg_connection* conn, int opcode, +size_t mg_websocket_printf(struct mg_connection *conn, int opcode, const char *fmt, ...) { char mem[4192], *buf = mem; va_list ap; @@ -4143,7 +4143,7 @@ static void proxify_connection(struct connection *conn) { } #ifndef MONGOOSE_NO_FILESYSTEM -void mg_send_file_internal(struct mg_connection *c, const char *file_name, file_stat_t* st, int exists) { +void mg_send_file_internal(struct mg_connection *c, const char *file_name, file_stat_t *st, int exists) { struct connection *conn = MG_CONN_2_CONN(c); char path[MAX_PATH_SIZE]; const int is_directory = S_ISDIR(st->st_mode); From e6eca1738fe3f66b27ecfcf7b57f78c190f9d8e2 Mon Sep 17 00:00:00 2001 From: Pavel Pimenov Date: Sun, 27 Jul 2014 12:53:37 +0400 Subject: [PATCH 4/5] 80 characters limit + remove tabs --- mongoose.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/mongoose.c b/mongoose.c index a0f4090e..ec551527 100644 --- a/mongoose.c +++ b/mongoose.c @@ -3502,7 +3502,8 @@ void mg_send_digest_auth_request(struct mg_connection *c) { // Use the global passwords file, if specified by auth_gpass option, // or search for .htpasswd in the requested directory. -static FILE *open_auth_file(struct connection *conn, const char *path, int is_directory) { +static FILE *open_auth_file(struct connection *conn, const char *path, + int is_directory) { char name[MAX_PATH_SIZE]; const char *p, *gpass = conn->server->config_options[GLOBAL_AUTH_FILE]; FILE *fp = NULL; @@ -3800,7 +3801,8 @@ int mg_authorize_digest(struct mg_connection *c, FILE *fp) { // Return 1 if request is authorised, 0 otherwise. -static int is_authorized(struct connection *conn, const char *path, int is_directory) { +static int is_authorized(struct connection *conn, const char *path, + int is_directory) { FILE *fp; int authorized = MG_TRUE; @@ -4143,7 +4145,8 @@ static void proxify_connection(struct connection *conn) { } #ifndef MONGOOSE_NO_FILESYSTEM -void mg_send_file_internal(struct mg_connection *c, const char *file_name, file_stat_t *st, int exists) { +void mg_send_file_internal(struct mg_connection *c, const char *file_name, + file_stat_t *st, int exists) { struct connection *conn = MG_CONN_2_CONN(c); char path[MAX_PATH_SIZE]; const int is_directory = S_ISDIR(st->st_mode); @@ -4202,8 +4205,8 @@ void mg_send_file_internal(struct mg_connection *c, const char *file_name, file_ } void mg_send_file(struct mg_connection *c, const char *file_name) { file_stat_t st; - const int exists = stat(file_name, &st) == 0; - mg_send_file_internal(c, file_name, &st, exists); + const int exists = stat(file_name, &st) == 0; + mg_send_file_internal(c, file_name, &st, exists); } #endif // !MONGOOSE_NO_FILESYSTEM @@ -4267,7 +4270,8 @@ static void open_local_endpoint(struct connection *conn, int skip_user) { } else if (conn->server->config_options[DOCUMENT_ROOT] == NULL) { send_http_error(conn, 404, NULL); #ifndef MONGOOSE_NO_AUTH - } else if ((!is_dav_request(conn) && !is_authorized(conn, path, exists && S_ISDIR(st.st_mode))) || + } else if ((!is_dav_request(conn) && !is_authorized(conn, path, + exists && S_ISDIR(st.st_mode))) || (is_dav_request(conn) && !is_authorized_for_dav(conn))) { mg_send_digest_auth_request(&conn->mg_conn); close_local_endpoint(conn); @@ -4354,7 +4358,8 @@ static void on_recv_data(struct connection *conn) { } try_parse(conn); - DBG(("%p %d %lu %d", conn, conn->request_len, (unsigned long)io->len, conn->ns_conn->flags)); + DBG(("%p %d %lu %d", conn, conn->request_len, (unsigned long)io->len, + conn->ns_conn->flags)); if (conn->request_len < 0 || (conn->request_len > 0 && !is_valid_uri(conn->mg_conn.uri))) { send_http_error(conn, 400, NULL); From 01a058d29b42c11d5a9534debe108ef1d63a5d0d Mon Sep 17 00:00:00 2001 From: Pavel Pimenov Date: Sun, 27 Jul 2014 13:10:49 +0400 Subject: [PATCH 5/5] Remove file_stat_t st_cgi; --- mongoose.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/mongoose.c b/mongoose.c index ec551527..0edd69bd 100644 --- a/mongoose.c +++ b/mongoose.c @@ -2332,7 +2332,6 @@ static int convert_uri_to_file_name(struct connection *conn, char *buf, const char *rewrites = conn->server->config_options[URL_REWRITES]; const char *root = conn->server->config_options[DOCUMENT_ROOT]; #ifndef MONGOOSE_NO_CGI - file_stat_t st_cgi; const char *cgi_pat = conn->server->config_options[CGI_PATTERN]; char *p; #endif @@ -2376,7 +2375,7 @@ static int convert_uri_to_file_name(struct connection *conn, char *buf, if (*p == '/') { *p = '\0'; if (mg_match_prefix(cgi_pat, strlen(cgi_pat), buf) > 0 && - !stat(buf, &st_cgi)) { + !stat(buf, st)) { DBG(("!!!! [%s]", buf)); *p = '/'; conn->path_info = mg_strdup(p);