Commit 95d88814 authored by Sergey Lyubka's avatar Sergey Lyubka

Fixed spool() to properly resize, commented out non-implemented API

parent a50a52ca
...@@ -112,6 +112,7 @@ struct linked_list_link { struct linked_list_link *prev, *next; }; ...@@ -112,6 +112,7 @@ struct linked_list_link { struct linked_list_link *prev, *next; };
#define MAX_REQUEST_SIZE 16384 #define MAX_REQUEST_SIZE 16384
#define IOBUF_SIZE 8192 #define IOBUF_SIZE 8192
#define MAX_PATH_SIZE 8192 #define MAX_PATH_SIZE 8192
#define LUA_SCRIPT_PATTERN "mg_*.lua$"
// Extra HTTP headers to send in every static file reply // Extra HTTP headers to send in every static file reply
#if !defined(EXTRA_HTTP_HEADERS) #if !defined(EXTRA_HTTP_HEADERS)
...@@ -153,6 +154,30 @@ enum { ...@@ -153,6 +154,30 @@ enum {
SSL_CERTIFICATE, SSI_PATTERN, URL_REWRITES, NUM_OPTIONS 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 { struct mg_server {
sock_t listening_sock; sock_t listening_sock;
union socket_address lsa; // Listening socket address union socket_address lsa; // Listening socket address
...@@ -174,15 +199,15 @@ union endpoint { ...@@ -174,15 +199,15 @@ union endpoint {
void *ssl; // SSL descriptor 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 }; enum connection_flags { CONN_CLOSE = 1, CONN_SPOOL_DONE = 2 };
struct connection { struct connection {
struct mg_connection mg_conn; // XXX: Must be first struct mg_connection mg_conn; // XXX: Must be first
struct linked_list_link link; // Linkage to server->active_connections struct linked_list_link link; // Linkage to server->active_connections
struct mg_server *server; struct mg_server *server;
sock_t client_sock; // Connected client sock_t client_sock; // Connected client
union socket_address csa; // Client's socket address union socket_address csa; // Client's socket address
struct iobuf local_iobuf; struct iobuf local_iobuf;
struct iobuf remote_iobuf; struct iobuf remote_iobuf;
union endpoint endpoint; union endpoint endpoint;
...@@ -192,32 +217,10 @@ struct connection { ...@@ -192,32 +217,10 @@ struct connection {
int request_len; int request_len;
int flags; int flags;
int status_code; 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[] = { static void close_local_endpoint(struct connection *conn);
"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 const struct { static const struct {
const char *extension; const char *extension;
...@@ -864,14 +867,19 @@ static int convert_uri_to_file_name(struct connection *conn, char *buf, ...@@ -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) { 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) { if (len <= 0) {
} else if (len < io->size - io->len || } else if (new_len < io->size) {
(p = (char *) realloc(io->buf, io->len + len - io->size)) != 0) {
io->buf = p;
memcpy(io->buf + io->len, buf, len); memcpy(io->buf + io->len, buf, len);
io->len += 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 { } else {
len = 0; len = 0;
} }
...@@ -1178,6 +1186,10 @@ static void send_http_error(struct connection *conn, const char *fmt, ...) { ...@@ -1178,6 +1186,10 @@ static void send_http_error(struct connection *conn, const char *fmt, ...) {
conn->flags |= CONN_SPOOL_DONE; 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) { static void open_local_endpoint(struct connection *conn) {
char path[MAX_PATH_SIZE] = {'\0'}; char path[MAX_PATH_SIZE] = {'\0'};
file_stat_t st; file_stat_t st;
...@@ -1207,10 +1219,9 @@ static void open_local_endpoint(struct connection *conn) { ...@@ -1207,10 +1219,9 @@ static void open_local_endpoint(struct connection *conn) {
} else if (is_directory && } else if (is_directory &&
!substitute_index_file(conn, path, sizeof(path), &st)) { !substitute_index_file(conn, path, sizeof(path), &st)) {
send_http_error(conn, "%s", "HTTP/1.1 403 Listing Denied\r\n\r\n"); send_http_error(conn, "%s", "HTTP/1.1 403 Listing Denied\r\n\r\n");
#ifdef USE_LUA } else if (match_prefix(LUA_SCRIPT_PATTERN, 6, path) > 0) {
} else if (match_prefix("**.lua$", 6, path) > 0) { exec_lua_script(conn, path);
send_http_error(conn, "%s", "HTTP/1.1 200 :-)\r\n\r\n"); conn->flags |= CONN_SPOOL_DONE;
#endif
} else if (is_not_modified(conn, &st)) { } else if (is_not_modified(conn, &st)) {
send_http_error(conn, "%s", "HTTP/1.1 304 Not Modified\r\n\r\n"); 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) { } else if ((conn->endpoint.fd = open(path, O_RDONLY)) != -1) {
...@@ -1274,7 +1285,8 @@ static void close_local_endpoint(struct connection *conn) { ...@@ -1274,7 +1285,8 @@ static void close_local_endpoint(struct connection *conn) {
// Close file descriptor // Close file descriptor
switch (conn->endpoint_type) { switch (conn->endpoint_type) {
case EP_FILE: close(conn->endpoint.fd); break; 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 // Get rid of that request from the buffer. NOTE: order is important here
......
...@@ -56,8 +56,10 @@ void mg_destroy_server(struct mg_server **); ...@@ -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); 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_poll_server(struct mg_server *, int milliseconds);
void mg_add_uri_handler(struct mg_server *, const char *uri, mg_uri_handler_t); 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_error_handler(struct mg_server *, mg_error_handler_t);
void mg_set_log_handler(struct mg_server*, int (*)(struct mg_connection*, int)); 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_valid_option_names(void);
const char *mg_get_option(const struct mg_server *server, const char *name); 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, ...@@ -70,6 +72,8 @@ int mg_websocket_write(struct mg_connection* conn, int opcode,
// Connection management functions // Connection management functions
int mg_write(struct mg_connection *, const void *buf, int len); int mg_write(struct mg_connection *, const void *buf, int len);
int mg_printf(struct mg_connection *, const char *fmt, ...); int mg_printf(struct mg_connection *, const char *fmt, ...);
#if 0
void mg_send_file(struct mg_connection *, const char *path); void mg_send_file(struct mg_connection *, const char *path);
int mg_read(struct mg_connection *, void *buf, int len); int mg_read(struct mg_connection *, void *buf, int len);
const char *mg_get_header(const struct mg_connection *, const char *name); 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); ...@@ -81,6 +85,7 @@ const char *mg_get_mime_type(const char *file_name);
// Utility functions // Utility functions
int mg_start_thread(void *(*func)(void *), void *param); int mg_start_thread(void *(*func)(void *), void *param);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment