diff --git a/build/Makefile b/build/Makefile index b0a7be3d4a308b946fd4ab917df535cd124f99d9..92c7e82a53bd55ede4fc8f557e7651171ac583d9 100644 --- a/build/Makefile +++ b/build/Makefile @@ -57,7 +57,7 @@ endif all: @echo "make (unix|windows|macos)" -../mongoose.c: mod_lua.c ../mongoose.h Makefile $(SOURCES) +../mongoose.c: mod_lua.c ../mongoose.h Makefile src/internal.h $(SOURCES) cat src/internal.h src/mongoose.c | sed '/#include "internal.h"/d' > $@ unix_unit_test: $(LUA_SOURCES) Makefile ../test/unit_test.c diff --git a/build/src/internal.h b/build/src/internal.h index e2f05e1c9ae3c1c72b70d84bd78121fc3612159e..2426b889747635dc85b2c4e33dabc8466e98e16e 100644 --- a/build/src/internal.h +++ b/build/src/internal.h @@ -323,6 +323,8 @@ struct ssl_func { void (*ptr)(void); // Function pointer }; +static struct ssl_func ssl_sw[]; + #define SSL_free (* (void (*)(SSL *)) ssl_sw[0].ptr) #define SSL_accept (* (int (*)(SSL *)) ssl_sw[1].ptr) #define SSL_connect (* (int (*)(SSL *)) ssl_sw[2].ptr) @@ -356,4 +358,103 @@ struct ssl_func { (* (void (*)(unsigned long (*)(void))) crypto_sw[2].ptr) #define ERR_get_error (* (unsigned long (*)(void)) crypto_sw[3].ptr) #define ERR_error_string (* (char * (*)(unsigned long,char *)) crypto_sw[4].ptr) +#endif // NO_SSL_DL + +// Unified socket address. For IPv6 support, add IPv6 address structure +// in the union u. +union usa { + struct sockaddr sa; + struct sockaddr_in sin; +#if defined(USE_IPV6) + struct sockaddr_in6 sin6; +#endif +}; + +// Describes a string (chunk of memory). +struct vec { + const char *ptr; + size_t len; +}; + +struct file { + int is_directory; + time_t modification_time; + int64_t size; + // set to 1 if the content is gzipped + // in which case we need a content-encoding: gzip header + int gzipped; +}; +#define STRUCT_FILE_INITIALIZER { 0, 0, 0, 0 } + +// Describes listening socket, or socket which was accept()-ed by the master +// thread and queued for future handling by the worker thread. +struct socket { + SOCKET sock; // Listening socket + union usa lsa; // Local socket address + union usa rsa; // Remote socket address + unsigned is_ssl:1; // Is port SSL-ed + unsigned ssl_redir:1; // Is port supposed to redirect everything to SSL port +}; + +// NOTE(lsm): this enum shoulds be in sync with the config_options. +enum { + CGI_EXTENSIONS, CGI_ENVIRONMENT, PUT_DELETE_PASSWORDS_FILE, CGI_INTERPRETER, + PROTECT_URI, AUTHENTICATION_DOMAIN, SSI_EXTENSIONS, THROTTLE, + ACCESS_LOG_FILE, ENABLE_DIRECTORY_LISTING, ERROR_LOG_FILE, + GLOBAL_PASSWORDS_FILE, INDEX_FILES, ENABLE_KEEP_ALIVE, ACCESS_CONTROL_LIST, + EXTRA_MIME_TYPES, LISTENING_PORTS, DOCUMENT_ROOT, SSL_CERTIFICATE, + NUM_THREADS, RUN_AS_USER, REWRITE, HIDE_FILES, REQUEST_TIMEOUT, + NUM_OPTIONS +}; + +struct mg_context { + volatile int stop_flag; // Should we stop event loop + SSL_CTX *ssl_ctx; // SSL context + char *config[NUM_OPTIONS]; // Mongoose configuration parameters + mg_event_handler_t event_handler; // User-defined callback function + void *user_data; // User-defined data + + struct socket *listening_sockets; + int num_listening_sockets; + + volatile int num_threads; // Number of threads + pthread_mutex_t mutex; // Protects (max|num)_threads + pthread_cond_t cond; // Condvar for tracking workers terminations + + struct socket queue[MGSQLEN]; // Accepted sockets + volatile int sq_head; // Head of the socket queue + volatile int sq_tail; // Tail of the socket queue + pthread_cond_t sq_full; // Signaled when socket is produced + pthread_cond_t sq_empty; // Signaled when socket is consumed +}; + +struct mg_connection { + struct mg_request_info request_info; + struct mg_event event; + struct mg_context *ctx; + SSL *ssl; // SSL descriptor + SSL_CTX *client_ssl_ctx; // SSL context for client connections + struct socket client; // Connected client + time_t birth_time; // Time when request was received + int64_t num_bytes_sent; // Total bytes sent to client + int64_t content_len; // Content-Length header value + int64_t num_bytes_read; // Bytes read from a remote socket + char *buf; // Buffer for received data + char *path_info; // PATH_INFO part of the URL + int must_close; // 1 if connection must be closed + int buf_size; // Buffer size + int request_len; // Size of the request + headers in a buffer + int data_len; // Total size of data in a buffer + int status_code; // HTTP reply status code, e.g. 200 + int throttle; // Throttling, bytes/sec. <= 0 means no throttle + time_t last_throttle_time; // Last time throttled data was sent + int64_t last_throttle_bytes;// Bytes sent this second +}; + +// Directory entry +struct de { + struct mg_connection *conn; + char *file_name; + struct file file; +}; diff --git a/build/src/mongoose.c b/build/src/mongoose.c index 65504e296bd5f00fb4b118f4d3a81eff71d89928..f646d0a0aa154f0b0696cda7f2e51e2df514b834 100644 --- a/build/src/mongoose.c +++ b/build/src/mongoose.c @@ -1,99 +1,10 @@ #include "internal.h" -// set_ssl_option() function updates this array. -// It loads SSL library dynamically and changes NULLs to the actual addresses -// of respective functions. The macros above (like SSL_connect()) are really -// just calling these functions indirectly via the pointer. -static struct ssl_func ssl_sw[] = { - {"SSL_free", NULL}, - {"SSL_accept", NULL}, - {"SSL_connect", NULL}, - {"SSL_read", NULL}, - {"SSL_write", NULL}, - {"SSL_get_error", NULL}, - {"SSL_set_fd", NULL}, - {"SSL_new", NULL}, - {"SSL_CTX_new", NULL}, - {"SSLv23_server_method", NULL}, - {"SSL_library_init", NULL}, - {"SSL_CTX_use_PrivateKey_file", NULL}, - {"SSL_CTX_use_certificate_file",NULL}, - {"SSL_CTX_set_default_passwd_cb",NULL}, - {"SSL_CTX_free", NULL}, - {"SSL_load_error_strings", NULL}, - {"SSL_CTX_use_certificate_chain_file", NULL}, - {"SSLv23_client_method", NULL}, - {"SSL_pending", NULL}, - {"SSL_CTX_set_verify", NULL}, - {"SSL_shutdown", NULL}, - {NULL, NULL} -}; - -// Similar array as ssl_sw. These functions could be located in different lib. -#if !defined(NO_SSL) -static struct ssl_func crypto_sw[] = { - {"CRYPTO_num_locks", NULL}, - {"CRYPTO_set_locking_callback", NULL}, - {"CRYPTO_set_id_callback", NULL}, - {"ERR_get_error", NULL}, - {"ERR_error_string", NULL}, - {NULL, NULL} -}; -#endif // NO_SSL -#endif // NO_SSL_DL - static const char *month_names[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; -// Unified socket address. For IPv6 support, add IPv6 address structure -// in the union u. -union usa { - struct sockaddr sa; - struct sockaddr_in sin; -#if defined(USE_IPV6) - struct sockaddr_in6 sin6; -#endif -}; - -// Describes a string (chunk of memory). -struct vec { - const char *ptr; - size_t len; -}; - -struct file { - int is_directory; - time_t modification_time; - int64_t size; - // set to 1 if the content is gzipped - // in which case we need a content-encoding: gzip header - int gzipped; -}; -#define STRUCT_FILE_INITIALIZER { 0, 0, 0, 0 } - -// Describes listening socket, or socket which was accept()-ed by the master -// thread and queued for future handling by the worker thread. -struct socket { - SOCKET sock; // Listening socket - union usa lsa; // Local socket address - union usa rsa; // Remote socket address - unsigned is_ssl:1; // Is port SSL-ed - unsigned ssl_redir:1; // Is port supposed to redirect everything to SSL port -}; - -// NOTE(lsm): this enum shoulds be in sync with the config_options below. -enum { - CGI_EXTENSIONS, CGI_ENVIRONMENT, PUT_DELETE_PASSWORDS_FILE, CGI_INTERPRETER, - PROTECT_URI, AUTHENTICATION_DOMAIN, SSI_EXTENSIONS, THROTTLE, - ACCESS_LOG_FILE, ENABLE_DIRECTORY_LISTING, ERROR_LOG_FILE, - GLOBAL_PASSWORDS_FILE, INDEX_FILES, ENABLE_KEEP_ALIVE, ACCESS_CONTROL_LIST, - EXTRA_MIME_TYPES, LISTENING_PORTS, DOCUMENT_ROOT, SSL_CERTIFICATE, - NUM_THREADS, RUN_AS_USER, REWRITE, HIDE_FILES, REQUEST_TIMEOUT, - NUM_OPTIONS -}; - static const char *config_options[] = { "cgi_pattern", "**.cgi$|**.pl$|**.php$", "cgi_environment", NULL, @@ -123,57 +34,6 @@ static const char *config_options[] = { NULL }; -struct mg_context { - volatile int stop_flag; // Should we stop event loop - SSL_CTX *ssl_ctx; // SSL context - char *config[NUM_OPTIONS]; // Mongoose configuration parameters - mg_event_handler_t event_handler; // User-defined callback function - void *user_data; // User-defined data - - struct socket *listening_sockets; - int num_listening_sockets; - - volatile int num_threads; // Number of threads - pthread_mutex_t mutex; // Protects (max|num)_threads - pthread_cond_t cond; // Condvar for tracking workers terminations - - struct socket queue[MGSQLEN]; // Accepted sockets - volatile int sq_head; // Head of the socket queue - volatile int sq_tail; // Tail of the socket queue - pthread_cond_t sq_full; // Signaled when socket is produced - pthread_cond_t sq_empty; // Signaled when socket is consumed -}; - -struct mg_connection { - struct mg_request_info request_info; - struct mg_event event; - struct mg_context *ctx; - SSL *ssl; // SSL descriptor - SSL_CTX *client_ssl_ctx; // SSL context for client connections - struct socket client; // Connected client - time_t birth_time; // Time when request was received - int64_t num_bytes_sent; // Total bytes sent to client - int64_t content_len; // Content-Length header value - int64_t num_bytes_read; // Bytes read from a remote socket - char *buf; // Buffer for received data - char *path_info; // PATH_INFO part of the URL - int must_close; // 1 if connection must be closed - int buf_size; // Buffer size - int request_len; // Size of the request + headers in a buffer - int data_len; // Total size of data in a buffer - int status_code; // HTTP reply status code, e.g. 200 - int throttle; // Throttling, bytes/sec. <= 0 means no throttle - time_t last_throttle_time; // Last time throttled data was sent - int64_t last_throttle_bytes;// Bytes sent this second -}; - -// Directory entry -struct de { - struct mg_connection *conn; - char *file_name; - struct file file; -}; - // Return number of bytes left to read for this connection static int64_t left_to_read(const struct mg_connection *conn) { return conn->content_len + conn->request_len - conn->num_bytes_read; @@ -1119,12 +979,9 @@ static int64_t push(FILE *fp, SOCKET sock, SSL *ssl, const char *buf, // How many bytes we send in this iteration k = len - sent > INT_MAX ? INT_MAX : (int) (len - sent); -#ifndef NO_SSL if (ssl != NULL) { n = SSL_write(ssl, buf + sent, k); - } else -#endif - if (fp != NULL) { + } else if (fp != NULL) { n = (int) fwrite(buf + sent, 1, (size_t) k, fp); if (ferror(fp)) n = -1; @@ -4325,6 +4182,45 @@ static int set_uid_option(struct mg_context *ctx) { #endif // !_WIN32 #if !defined(NO_SSL) +// set_ssl_option() function updates this array. +// It loads SSL library dynamically and changes NULLs to the actual addresses +// of respective functions. The macros above (like SSL_connect()) are really +// just calling these functions indirectly via the pointer. +static struct ssl_func ssl_sw[] = { + {"SSL_free", NULL}, + {"SSL_accept", NULL}, + {"SSL_connect", NULL}, + {"SSL_read", NULL}, + {"SSL_write", NULL}, + {"SSL_get_error", NULL}, + {"SSL_set_fd", NULL}, + {"SSL_new", NULL}, + {"SSL_CTX_new", NULL}, + {"SSLv23_server_method", NULL}, + {"SSL_library_init", NULL}, + {"SSL_CTX_use_PrivateKey_file", NULL}, + {"SSL_CTX_use_certificate_file",NULL}, + {"SSL_CTX_set_default_passwd_cb",NULL}, + {"SSL_CTX_free", NULL}, + {"SSL_load_error_strings", NULL}, + {"SSL_CTX_use_certificate_chain_file", NULL}, + {"SSLv23_client_method", NULL}, + {"SSL_pending", NULL}, + {"SSL_CTX_set_verify", NULL}, + {"SSL_shutdown", NULL}, + {NULL, NULL} +}; + +// Similar array as ssl_sw. These functions could be located in different lib. +static struct ssl_func crypto_sw[] = { + {"CRYPTO_num_locks", NULL}, + {"CRYPTO_set_locking_callback", NULL}, + {"CRYPTO_set_id_callback", NULL}, + {"ERR_get_error", NULL}, + {"ERR_error_string", NULL}, + {NULL, NULL} +}; + static pthread_mutex_t *ssl_mutexes; static int sslize(struct mg_connection *conn, SSL_CTX *s, int (*func)(SSL *)) { diff --git a/mongoose.c b/mongoose.c index 40c4d4b916782cef8147e4d293c6bf3e9d9fde00..c2e7c737a861e6e9f5571358dbe11c48bf5eae51 100644 --- a/mongoose.c +++ b/mongoose.c @@ -323,6 +323,8 @@ struct ssl_func { void (*ptr)(void); // Function pointer }; +static struct ssl_func ssl_sw[]; + #define SSL_free (* (void (*)(SSL *)) ssl_sw[0].ptr) #define SSL_accept (* (int (*)(SSL *)) ssl_sw[1].ptr) #define SSL_connect (* (int (*)(SSL *)) ssl_sw[2].ptr) @@ -356,55 +358,8 @@ struct ssl_func { (* (void (*)(unsigned long (*)(void))) crypto_sw[2].ptr) #define ERR_get_error (* (unsigned long (*)(void)) crypto_sw[3].ptr) #define ERR_error_string (* (char * (*)(unsigned long,char *)) crypto_sw[4].ptr) - - -// set_ssl_option() function updates this array. -// It loads SSL library dynamically and changes NULLs to the actual addresses -// of respective functions. The macros above (like SSL_connect()) are really -// just calling these functions indirectly via the pointer. -static struct ssl_func ssl_sw[] = { - {"SSL_free", NULL}, - {"SSL_accept", NULL}, - {"SSL_connect", NULL}, - {"SSL_read", NULL}, - {"SSL_write", NULL}, - {"SSL_get_error", NULL}, - {"SSL_set_fd", NULL}, - {"SSL_new", NULL}, - {"SSL_CTX_new", NULL}, - {"SSLv23_server_method", NULL}, - {"SSL_library_init", NULL}, - {"SSL_CTX_use_PrivateKey_file", NULL}, - {"SSL_CTX_use_certificate_file",NULL}, - {"SSL_CTX_set_default_passwd_cb",NULL}, - {"SSL_CTX_free", NULL}, - {"SSL_load_error_strings", NULL}, - {"SSL_CTX_use_certificate_chain_file", NULL}, - {"SSLv23_client_method", NULL}, - {"SSL_pending", NULL}, - {"SSL_CTX_set_verify", NULL}, - {"SSL_shutdown", NULL}, - {NULL, NULL} -}; - -// Similar array as ssl_sw. These functions could be located in different lib. -#if !defined(NO_SSL) -static struct ssl_func crypto_sw[] = { - {"CRYPTO_num_locks", NULL}, - {"CRYPTO_set_locking_callback", NULL}, - {"CRYPTO_set_id_callback", NULL}, - {"ERR_get_error", NULL}, - {"ERR_error_string", NULL}, - {NULL, NULL} -}; -#endif // NO_SSL #endif // NO_SSL_DL -static const char *month_names[] = { - "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" -}; - // Unified socket address. For IPv6 support, add IPv6 address structure // in the union u. union usa { @@ -441,7 +396,7 @@ struct socket { unsigned ssl_redir:1; // Is port supposed to redirect everything to SSL port }; -// NOTE(lsm): this enum shoulds be in sync with the config_options below. +// NOTE(lsm): this enum shoulds be in sync with the config_options. enum { CGI_EXTENSIONS, CGI_ENVIRONMENT, PUT_DELETE_PASSWORDS_FILE, CGI_INTERPRETER, PROTECT_URI, AUTHENTICATION_DOMAIN, SSI_EXTENSIONS, THROTTLE, @@ -452,35 +407,6 @@ enum { NUM_OPTIONS }; -static const char *config_options[] = { - "cgi_pattern", "**.cgi$|**.pl$|**.php$", - "cgi_environment", NULL, - "put_delete_auth_file", NULL, - "cgi_interpreter", NULL, - "protect_uri", NULL, - "authentication_domain", "mydomain.com", - "ssi_pattern", "**.shtml$|**.shtm$", - "throttle", NULL, - "access_log_file", NULL, - "enable_directory_listing", "yes", - "error_log_file", NULL, - "global_auth_file", NULL, - "index_files", - "index.html,index.htm,index.cgi,index.shtml,index.php,index.lp", - "enable_keep_alive", "no", - "access_control_list", NULL, - "extra_mime_types", NULL, - "listening_ports", "8080", - "document_root", NULL, - "ssl_certificate", NULL, - "num_threads", "50", - "run_as_user", NULL, - "url_rewrite_patterns", NULL, - "hide_files_patterns", NULL, - "request_timeout_ms", "30000", - NULL -}; - struct mg_context { volatile int stop_flag; // Should we stop event loop SSL_CTX *ssl_ctx; // SSL context @@ -532,6 +458,41 @@ struct de { struct file file; }; + +static const char *month_names[] = { + "Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" +}; + +static const char *config_options[] = { + "cgi_pattern", "**.cgi$|**.pl$|**.php$", + "cgi_environment", NULL, + "put_delete_auth_file", NULL, + "cgi_interpreter", NULL, + "protect_uri", NULL, + "authentication_domain", "mydomain.com", + "ssi_pattern", "**.shtml$|**.shtm$", + "throttle", NULL, + "access_log_file", NULL, + "enable_directory_listing", "yes", + "error_log_file", NULL, + "global_auth_file", NULL, + "index_files", + "index.html,index.htm,index.cgi,index.shtml,index.php,index.lp", + "enable_keep_alive", "no", + "access_control_list", NULL, + "extra_mime_types", NULL, + "listening_ports", "8080", + "document_root", NULL, + "ssl_certificate", NULL, + "num_threads", "50", + "run_as_user", NULL, + "url_rewrite_patterns", NULL, + "hide_files_patterns", NULL, + "request_timeout_ms", "30000", + NULL +}; + // Return number of bytes left to read for this connection static int64_t left_to_read(const struct mg_connection *conn) { return conn->content_len + conn->request_len - conn->num_bytes_read; @@ -1477,12 +1438,9 @@ static int64_t push(FILE *fp, SOCKET sock, SSL *ssl, const char *buf, // How many bytes we send in this iteration k = len - sent > INT_MAX ? INT_MAX : (int) (len - sent); -#ifndef NO_SSL if (ssl != NULL) { n = SSL_write(ssl, buf + sent, k); - } else -#endif - if (fp != NULL) { + } else if (fp != NULL) { n = (int) fwrite(buf + sent, 1, (size_t) k, fp); if (ferror(fp)) n = -1; @@ -4683,6 +4641,45 @@ static int set_uid_option(struct mg_context *ctx) { #endif // !_WIN32 #if !defined(NO_SSL) +// set_ssl_option() function updates this array. +// It loads SSL library dynamically and changes NULLs to the actual addresses +// of respective functions. The macros above (like SSL_connect()) are really +// just calling these functions indirectly via the pointer. +static struct ssl_func ssl_sw[] = { + {"SSL_free", NULL}, + {"SSL_accept", NULL}, + {"SSL_connect", NULL}, + {"SSL_read", NULL}, + {"SSL_write", NULL}, + {"SSL_get_error", NULL}, + {"SSL_set_fd", NULL}, + {"SSL_new", NULL}, + {"SSL_CTX_new", NULL}, + {"SSLv23_server_method", NULL}, + {"SSL_library_init", NULL}, + {"SSL_CTX_use_PrivateKey_file", NULL}, + {"SSL_CTX_use_certificate_file",NULL}, + {"SSL_CTX_set_default_passwd_cb",NULL}, + {"SSL_CTX_free", NULL}, + {"SSL_load_error_strings", NULL}, + {"SSL_CTX_use_certificate_chain_file", NULL}, + {"SSLv23_client_method", NULL}, + {"SSL_pending", NULL}, + {"SSL_CTX_set_verify", NULL}, + {"SSL_shutdown", NULL}, + {NULL, NULL} +}; + +// Similar array as ssl_sw. These functions could be located in different lib. +static struct ssl_func crypto_sw[] = { + {"CRYPTO_num_locks", NULL}, + {"CRYPTO_set_locking_callback", NULL}, + {"CRYPTO_set_id_callback", NULL}, + {"ERR_get_error", NULL}, + {"ERR_error_string", NULL}, + {NULL, NULL} +}; + static pthread_mutex_t *ssl_mutexes; static int sslize(struct mg_connection *conn, SSL_CTX *s, int (*func)(SSL *)) {