From 95d8881454900921d80344ccef43663b2a5166c0 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka <valenok@gmail.com> Date: Wed, 4 Dec 2013 10:19:55 +0000 Subject: [PATCH] Fixed spool() to properly resize, commented out non-implemented API --- build/src/core.c | 84 +++++++++++++++++++++++++++--------------------- build/src/core.h | 5 +++ 2 files changed, 53 insertions(+), 36 deletions(-) diff --git a/build/src/core.c b/build/src/core.c index c1158e930..ebb45c649 100644 --- a/build/src/core.c +++ b/build/src/core.c @@ -112,6 +112,7 @@ struct linked_list_link { struct linked_list_link *prev, *next; }; #define MAX_REQUEST_SIZE 16384 #define IOBUF_SIZE 8192 #define MAX_PATH_SIZE 8192 +#define LUA_SCRIPT_PATTERN "mg_*.lua$" // Extra HTTP headers to send in every static file reply #if !defined(EXTRA_HTTP_HEADERS) @@ -153,6 +154,30 @@ enum { SSL_CERTIFICATE, SSI_PATTERN, URL_REWRITES, NUM_OPTIONS }; +static const char *static_config_options[] = { + "access_control_list", NULL, + "access_log_file", NULL, + "auth_domain", "mydomain.com", + "cgi_interpreter", NULL, + "cgi_pattern", "**.cgi$|**.pl$|**.php$", + "document_root", NULL, + "enable_directory_listing", "yes", + "error_log_file", NULL, + "extra_mime_types", NULL, + "global_auth_file", NULL, + "hide_files_patterns", NULL, + "idle_timeout_ms", "30000", + "index_files","index.html,index.htm,index.cgi,index.shtml,index.php,index.lp", + "listening_port", NULL, + "num_threads", "50", + "put_delete_auth_file", NULL, + "run_as_user", NULL, + "ssl_certificate", NULL, + "ssi_pattern", "**.shtml$|**.shtm$", + "url_rewrites", NULL, + NULL +}; + struct mg_server { sock_t listening_sock; union socket_address lsa; // Listening socket address @@ -174,15 +199,15 @@ union endpoint { void *ssl; // SSL descriptor }; -enum endpoint_type { EP_NONE, EP_FILE, EP_DIR, EP_CGI, EP_SSL, EP_USER }; +enum endpoint_type { EP_NONE, EP_FILE, EP_USER }; enum connection_flags { CONN_CLOSE = 1, CONN_SPOOL_DONE = 2 }; struct connection { struct mg_connection mg_conn; // XXX: Must be first struct linked_list_link link; // Linkage to server->active_connections struct mg_server *server; - sock_t client_sock; // Connected client - union socket_address csa; // Client's socket address + sock_t client_sock; // Connected client + union socket_address csa; // Client's socket address struct iobuf local_iobuf; struct iobuf remote_iobuf; union endpoint endpoint; @@ -192,32 +217,10 @@ struct connection { int request_len; int flags; int status_code; - mutex_t mutex; // Guards concurrent mg_write() and mg_printf() calls + mutex_t mutex; // Guards concurrent mg_write() calls }; -static const char *static_config_options[] = { - "access_control_list", NULL, - "access_log_file", NULL, - "auth_domain", "mydomain.com", - "cgi_interpreter", NULL, - "cgi_pattern", "**.cgi$|**.pl$|**.php$", - "document_root", NULL, - "enable_directory_listing", "yes", - "error_log_file", NULL, - "extra_mime_types", NULL, - "global_auth_file", NULL, - "hide_files_patterns", NULL, - "idle_timeout_ms", "30000", - "index_files","index.html,index.htm,index.cgi,index.shtml,index.php,index.lp", - "listening_port", NULL, - "num_threads", "50", - "put_delete_auth_file", NULL, - "run_as_user", NULL, - "ssl_certificate", NULL, - "ssi_pattern", "**.shtml$|**.shtm$", - "url_rewrites", NULL, - NULL -}; +static void close_local_endpoint(struct connection *conn); static const struct { const char *extension; @@ -864,14 +867,19 @@ static int convert_uri_to_file_name(struct connection *conn, char *buf, } static int spool(struct iobuf *io, const void *buf, int len) { - char *p = io->buf; + char *p = NULL; + int new_len = io->len + len; + DBG(("%d %d %d", len, io->len, io->size)); if (len <= 0) { - } else if (len < io->size - io->len || - (p = (char *) realloc(io->buf, io->len + len - io->size)) != 0) { - io->buf = p; + } else if (new_len < io->size) { memcpy(io->buf + io->len, buf, len); io->len += len; + } else if ((p = (char *) realloc(io->buf, new_len * 2)) != NULL) { + io->buf = p; + memcpy(io->buf + io->len, buf, len); + io->len = new_len; + io->size = new_len * 2; } else { len = 0; } @@ -1178,6 +1186,10 @@ static void send_http_error(struct connection *conn, const char *fmt, ...) { conn->flags |= CONN_SPOOL_DONE; } +static void exec_lua_script(struct connection *conn, const char *path) { + send_http_error(conn, "%s", "HTTP/1.1 501 Not Implemented\r\n\r\n"); +} + static void open_local_endpoint(struct connection *conn) { char path[MAX_PATH_SIZE] = {'\0'}; file_stat_t st; @@ -1207,10 +1219,9 @@ static void open_local_endpoint(struct connection *conn) { } else if (is_directory && !substitute_index_file(conn, path, sizeof(path), &st)) { send_http_error(conn, "%s", "HTTP/1.1 403 Listing Denied\r\n\r\n"); -#ifdef USE_LUA - } else if (match_prefix("**.lua$", 6, path) > 0) { - send_http_error(conn, "%s", "HTTP/1.1 200 :-)\r\n\r\n"); -#endif + } else if (match_prefix(LUA_SCRIPT_PATTERN, 6, path) > 0) { + exec_lua_script(conn, path); + conn->flags |= CONN_SPOOL_DONE; } else if (is_not_modified(conn, &st)) { send_http_error(conn, "%s", "HTTP/1.1 304 Not Modified\r\n\r\n"); } else if ((conn->endpoint.fd = open(path, O_RDONLY)) != -1) { @@ -1274,7 +1285,8 @@ static void close_local_endpoint(struct connection *conn) { // Close file descriptor switch (conn->endpoint_type) { case EP_FILE: close(conn->endpoint.fd); break; - default: assert(1); break; + case EP_NONE: break; + default: assert(0); break; } // Get rid of that request from the buffer. NOTE: order is important here diff --git a/build/src/core.h b/build/src/core.h index 621bd30c0..f3fc54255 100644 --- a/build/src/core.h +++ b/build/src/core.h @@ -56,8 +56,10 @@ void mg_destroy_server(struct mg_server **); const char *mg_set_option(struct mg_server *, const char *opt, const char *val); void mg_poll_server(struct mg_server *, int milliseconds); void mg_add_uri_handler(struct mg_server *, const char *uri, mg_uri_handler_t); +#if 0 void mg_set_error_handler(struct mg_server *, mg_error_handler_t); void mg_set_log_handler(struct mg_server*, int (*)(struct mg_connection*, int)); +#endif const char **mg_get_valid_option_names(void); const char *mg_get_option(const struct mg_server *server, const char *name); @@ -70,6 +72,8 @@ int mg_websocket_write(struct mg_connection* conn, int opcode, // Connection management functions int mg_write(struct mg_connection *, const void *buf, int len); int mg_printf(struct mg_connection *, const char *fmt, ...); + +#if 0 void mg_send_file(struct mg_connection *, const char *path); int mg_read(struct mg_connection *, void *buf, int len); const char *mg_get_header(const struct mg_connection *, const char *name); @@ -81,6 +85,7 @@ const char *mg_get_mime_type(const char *file_name); // Utility functions int mg_start_thread(void *(*func)(void *), void *param); +#endif #ifdef __cplusplus } -- GitLab