From d317ce3e826f54e129612f8be4f29a7e81b78624 Mon Sep 17 00:00:00 2001 From: Lammert Bies Date: Tue, 13 Dec 2016 17:32:31 +0100 Subject: [PATCH] Renamed mg_ to httplib_ --- doc/Embedding.md | 16 +- doc/UserManual.md | 2 +- examples/chat/chat.c | 79 +- examples/embedded_c/embedded_c.c | 380 ++++---- examples/embedded_cpp/embedded_cpp.cpp | 116 ++- examples/hello/hello.c | 14 +- examples/post/post.c | 22 +- examples/upload/upload.c | 22 +- examples/websocket/WebSockCallbacks.c | 90 +- examples/websocket/WebSockCallbacks.h | 20 +- examples/websocket/websocket.c | 12 +- examples/websocket_client/websocket_client.c | 92 +- examples/ws_server/ws_server.c | 47 +- src/LibHTTPServer.cpp | 135 ++- src/httplib_accept_new_connection.c | 12 +- src/httplib_addenv.c | 4 +- src/httplib_authorize.c | 2 +- src/httplib_check_acl.c | 6 +- src/httplib_check_authorization.c | 4 +- src/httplib_check_feature.c | 8 +- src/httplib_check_password.c | 6 +- src/httplib_close_all_listening_sockets.c | 4 +- src/httplib_close_connection.c | 18 +- src/httplib_close_socket_gracefully.c | 6 +- src/httplib_config_options.c | 4 +- src/httplib_connect_client.c | 41 +- src/httplib_connect_socket.c | 2 +- src/httplib_connect_websocket_client.c | 26 +- src/httplib_consume_socket.c | 6 +- src/httplib_cry.c | 4 +- src/httplib_delete_file.c | 4 +- src/httplib_discard_unread_request_data.c | 10 +- src/httplib_download.c | 14 +- src/httplib_fc.c | 4 +- src/httplib_fclose_on_exec.c | 4 +- src/httplib_fopen.c | 2 +- src/httplib_forward_body_data.c | 12 +- src/httplib_free_context.c | 6 +- src/httplib_get_builtin_mime_type.c | 4 +- src/httplib_get_context.c | 6 +- src/httplib_get_cookie.c | 4 +- src/httplib_get_first_ssl_listener_index.c | 4 +- src/httplib_get_header.c | 8 +- src/httplib_get_mime_type.c | 4 +- src/httplib_get_option.c | 4 +- src/httplib_get_rel_url_at_current_server.c | 6 +- src/httplib_get_remote_ip.c | 4 +- src/httplib_get_request_handler.c | 16 +- src/httplib_get_request_info.c | 4 +- src/httplib_get_response.c | 12 +- src/httplib_get_response_code_text.c | 10 +- src/httplib_get_server_ports.c | 4 +- src/httplib_get_uri_type.c | 2 +- src/httplib_get_user_connection_data.c | 4 +- src/httplib_get_user_data.c | 4 +- src/httplib_get_valid_options.c | 4 +- src/httplib_get_var.c | 14 +- src/httplib_getreq.c | 15 +- src/httplib_handle_cgi_request.c | 35 +- src/httplib_handle_directory_request.c | 12 +- src/httplib_handle_file_based_request.c | 4 +- src/httplib_handle_form_request.c | 81 +- ..._handle_not_modified_static_file_request.c | 8 +- src/httplib_handle_propfind.c | 26 +- src/httplib_handle_request.c | 26 +- src/httplib_handle_static_file_request.c | 14 +- src/httplib_handle_websocket_request.c | 12 +- src/httplib_header_has_option.c | 2 +- src/httplib_initialize_ssl.c | 6 +- src/httplib_interpret_uri.c | 4 +- src/httplib_is_authorized_for_put.c | 2 +- src/httplib_is_file_in_memory.c | 2 +- src/httplib_is_not_modified.c | 10 +- src/httplib_is_put_or_delete_method.c | 2 +- src/httplib_is_websocket_protocol.c | 8 +- src/httplib_load_dll.c | 8 +- src/httplib_lock_unlock_connection.c | 16 +- src/httplib_lock_unlock_context.c | 16 +- src/httplib_log_access.c | 14 +- src/httplib_main.h | 264 +++--- src/httplib_malloc.c | 16 +- src/httplib_master_thread.c | 8 +- src/httplib_md5.c | 4 +- src/httplib_mkcol.c | 10 +- src/httplib_mkdir.c | 2 +- src/httplib_modify_passwords_file.c | 8 +- src/httplib_must_hide_file.c | 2 +- src/httplib_open_auth_file.c | 8 +- src/httplib_opendir.c | 2 +- src/httplib_parse_auth_header.c | 4 +- src/httplib_parse_http_headers.c | 4 +- src/httplib_parse_http_message.c | 8 +- src/httplib_path_to_unicode.c | 8 +- src/httplib_prepare_cgi_environment.c | 12 +- src/httplib_print_dir_entry.c | 4 +- src/httplib_printf.c | 4 +- src/httplib_process_new_connection.c | 6 +- src/httplib_produce_socket.c | 8 +- src/httplib_pull.c | 2 +- src/httplib_pull_all.c | 2 +- src/httplib_push_all.c | 4 +- src/httplib_put_dir.c | 4 +- src/httplib_put_file.c | 14 +- src/httplib_read.c | 23 +- src/httplib_read_auth_file.c | 8 +- src/httplib_read_request.c | 6 +- src/httplib_read_websocket.c | 8 +- src/httplib_redirect_to_https_port.c | 8 +- src/httplib_refresh_trust.c | 8 +- src/httplib_remove.c | 2 +- src/httplib_remove_bad_file.c | 6 +- src/httplib_remove_directory.c | 4 +- src/httplib_reset_per_request_attributes.c | 4 +- src/httplib_scan_directory.c | 4 +- src/httplib_send_authorization_request.c | 6 +- src/httplib_send_file.c | 18 +- src/httplib_send_file_data.c | 11 +- src/httplib_send_http_error.c | 14 +- src/httplib_send_no_cache_header.c | 4 +- src/httplib_send_options.c | 6 +- src/httplib_send_static_cache_header.c | 4 +- src/httplib_send_websocket_handshake.c | 14 +- src/httplib_set_acl_option.c | 4 +- src/httplib_set_auth_handler.c | 8 +- src/httplib_set_close_on_exec.c | 6 +- src/httplib_set_gpass_option.c | 6 +- src/httplib_set_handler_type.c | 23 +- src/httplib_set_ports_option.c | 36 +- src/httplib_set_request_handler.c | 8 +- src/httplib_set_ssl_option.c | 18 +- src/httplib_set_uid_option.c | 12 +- src/httplib_set_user_connection_data.c | 2 +- src/httplib_set_websocket_handler.c | 8 +- src/httplib_should_decode_url.c | 4 +- src/httplib_should_keep_alive.c | 6 +- src/httplib_snprintf.c | 2 +- src/httplib_spawn_process.c | 23 +- src/httplib_ssi.c | 46 +- src/httplib_ssl.h | 16 +- src/httplib_ssl_get_client_cert_info.c | 4 +- src/httplib_ssl_id_callback.c | 4 +- src/httplib_ssl_use_pem_file.c | 12 +- src/httplib_sslize.c | 8 +- src/httplib_start.c | 51 +- src/httplib_start_thread.c | 8 +- src/httplib_start_thread_with_id.c | 2 +- src/httplib_stat.c | 4 +- src/httplib_stop.c | 12 +- src/httplib_store_body.c | 14 +- src/httplib_strcasecmp.c | 4 +- src/httplib_strcasestr.c | 2 +- src/httplib_string.h | 6 +- src/httplib_strncasecmp.c | 4 +- src/httplib_substitute_index_file.c | 4 +- src/httplib_suggest_connection_header.c | 2 +- src/httplib_timer.c | 18 +- src/httplib_tls_dtor.c | 2 +- src/httplib_uninitialize_ssl.c | 4 +- src/httplib_url_decode.c | 4 +- src/httplib_url_encode.c | 4 +- src/httplib_version.c | 8 +- src/httplib_vprintf.c | 4 +- src/httplib_vsnprintf.c | 4 +- src/httplib_websocket_client_write.c | 10 +- src/httplib_websocket_write.c | 8 +- src/httplib_websocket_write_exec.c | 14 +- src/httplib_worker_thread.c | 10 +- src/httplib_write.c | 4 +- src/main.c | 121 ++- src/win32_pthread_cond_timedwait.c | 4 +- test/embed.c | 134 ++- test/form.html | 2 +- test/private.c | 56 +- test/public_func.c | 219 +++-- test/public_server.c | 839 ++++++++---------- test/test.pl | 4 +- test/testmime.c | 2 +- test/unit_test.c | 579 ++++++------ 178 files changed, 2173 insertions(+), 2513 deletions(-) diff --git a/doc/Embedding.md b/doc/Embedding.md index 70947831..be61e19e 100644 --- a/doc/Embedding.md +++ b/doc/Embedding.md @@ -55,11 +55,11 @@ By default, the server will automatically serve up files like a normal HTTP serv ### C - Include the C interface ```libhttp.h```. - - Use `mg_start()` to start the server. + - Use `httplib_start()` to start the server. - Use *options* to select the port and document root among other things. - Use *callbacks* to add your own hooks. - - Use `mg_set_request_handler()` to easily add your own request handlers. - - Use `mg_stop()` to stop the server. + - Use `httplib_set_request_handler()` to easily add your own request handlers. + - Use `httplib_stop()` to stop the server. ### C++ - Note that LibHTTP is Clean C, and C++ interface ```LibHTTPServer.h``` is only a wrapper layer around the C interface. @@ -136,8 +136,8 @@ LibHTTP can be built with server side JavaScript support by including the Duktap LibHTTP internals ------ -LibHTTP is multithreaded web server. `mg_start()` function allocates -web server context (`struct mg_context`), which holds all information +LibHTTP is multithreaded web server. `httplib_start()` function allocates +web server context (`struct httplib_context`), which holds all information about web server instance: - configuration options. Note that LibHTTP makes internal copies of @@ -148,14 +148,14 @@ about web server instance: - a queue for accepted sockets - mutexes and condition variables for inter-thread synchronization -When `mg_start()` returns, all initialization is guaranteed to be complete -(e.g. listening ports are opened, SSL is initialized, etc). `mg_start()` starts +When `httplib_start()` returns, all initialization is guaranteed to be complete +(e.g. listening ports are opened, SSL is initialized, etc). `httplib_start()` starts some threads: a master thread, that accepts new connections, and several worker threads, that process accepted connections. The number of worker threads is configurable via `num_threads` configuration option. That number puts a limit on number of simultaneous requests that can be handled by LibHTTP. If you embed LibHTTP into a program that uses SSL outside LibHTTP as well, -you may need to initialize SSL before calling `mg_start()`, and set the pre- +you may need to initialize SSL before calling `httplib_start()`, and set the pre- processor define SSL_ALREADY_INITIALIZED. This is not required if SSL is used only within LibHTTP. diff --git a/doc/UserManual.md b/doc/UserManual.md index 9f594ac2..aa9547f2 100644 --- a/doc/UserManual.md +++ b/doc/UserManual.md @@ -531,7 +531,7 @@ is an example for a plain Lua script. is an example for a Lua Server Page. Both examples show the content of the `mg.request_info` object as the page -content. Please refer to `struct mg_request_info` definition in +content. Please refer to `struct httplib_request_info` definition in [libhttp.h](https://github.com/lammertb/libhttp/blob/master/include/libhttp.h) to see additional information on the elements of the `mg.request_info` object. diff --git a/examples/chat/chat.c b/examples/chat/chat.c index ad146183..23ba77d4 100644 --- a/examples/chat/chat.c +++ b/examples/chat/chat.c @@ -54,13 +54,13 @@ static long last_message_id; static pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER; // Get session object for the connection. Caller must hold the lock. -static struct session *get_session(const struct mg_connection *conn) +static struct session *get_session(const struct httplib_connection *conn) { int i; - const char *cookie = mg_get_header(conn, "Cookie"); + const char *cookie = httplib_get_header(conn, "Cookie"); char session_id[33]; time_t now = time(NULL); - mg_get_cookie(cookie, "session", session_id, sizeof(session_id)); + httplib_get_cookie(cookie, "session", session_id, sizeof(session_id)); for (i = 0; i < MAX_SESSIONS; i++) { if (sessions[i].expire != 0 && sessions[i].expire > now && @@ -71,11 +71,10 @@ static struct session *get_session(const struct mg_connection *conn) return i == MAX_SESSIONS ? NULL : &sessions[i]; } -static void get_qsvar(const struct mg_request_info *request_info, - const char *name, char *dst, size_t dst_len) +static void get_qsvar(const struct httplib_request_info *request_info, const char *name, char *dst, size_t dst_len) { const char *qs = request_info->query_string; - mg_get_var(qs, strlen(qs == NULL ? "" : qs), name, dst, dst_len); + httplib_get_var(qs, strlen(qs == NULL ? "" : qs), name, dst, dst_len); } // Get a get of messages with IDs greater than last_id and transform them @@ -118,14 +117,14 @@ static char *messages_to_json(long last_id) // If "callback" param is present in query string, this is JSONP call. // Return 1 in this case, or 0 if "callback" is not specified. // Wrap an output in Javascript function call. -static int handle_jsonp(struct mg_connection *conn, - const struct mg_request_info *request_info) +static int handle_jsonp(struct httplib_connection *conn, + const struct httplib_request_info *request_info) { char cb[64]; get_qsvar(request_info, "callback", cb, sizeof(cb)); if (cb[0] != '\0') { - mg_printf(conn, "%s(", cb); + httplib_printf(conn, "%s(", cb); } return cb[0] == '\0' ? 0 : 1; @@ -133,23 +132,23 @@ static int handle_jsonp(struct mg_connection *conn, // A handler for the /ajax/get_messages endpoint. // Return a list of messages with ID greater than requested. -static void ajax_get_messages(struct mg_connection *conn, - const struct mg_request_info *request_info) +static void ajax_get_messages(struct httplib_connection *conn, + const struct httplib_request_info *request_info) { char last_id[32], *json; int is_jsonp; - mg_printf(conn, "%s", ajax_reply_start); + httplib_printf(conn, "%s", ajax_reply_start); is_jsonp = handle_jsonp(conn, request_info); get_qsvar(request_info, "last_id", last_id, sizeof(last_id)); if ((json = messages_to_json(strtoul(last_id, NULL, 10))) != NULL) { - mg_printf(conn, "[%s]", json); + httplib_printf(conn, "[%s]", json); free(json); } if (is_jsonp) { - mg_printf(conn, "%s", ")"); + httplib_printf(conn, "%s", ")"); } } @@ -170,15 +169,15 @@ static void my_strlcpy(char *dst, const char *src, size_t len) } // A handler for the /ajax/send_message endpoint. -static void ajax_send_message(struct mg_connection *conn, - const struct mg_request_info *request_info) +static void ajax_send_message(struct httplib_connection *conn, + const struct httplib_request_info *request_info) { struct message *message; struct session *session; char text[sizeof(message->text) - 1]; int is_jsonp; - mg_printf(conn, "%s", ajax_reply_start); + httplib_printf(conn, "%s", ajax_reply_start); is_jsonp = handle_jsonp(conn, request_info); get_qsvar(request_info, "text", text, sizeof(text)); @@ -195,19 +194,19 @@ static void ajax_send_message(struct mg_connection *conn, pthread_rwlock_unlock(&rwlock); } - mg_printf(conn, "%s", text[0] == '\0' ? "false" : "true"); + httplib_printf(conn, "%s", text[0] == '\0' ? "false" : "true"); if (is_jsonp) { - mg_printf(conn, "%s", ")"); + httplib_printf(conn, "%s", ")"); } } // Redirect user to the login form. In the cookie, store the original URL // we came from, so that after the authorization we could redirect back. -static void redirect_to_login(struct mg_connection *conn, - const struct mg_request_info *request_info) +static void redirect_to_login(struct httplib_connection *conn, + const struct httplib_request_info *request_info) { - mg_printf(conn, "HTTP/1.1 302 Found\r\n" + httplib_printf(conn, "HTTP/1.1 302 Found\r\n" "Set-Cookie: original_url=%s\r\n" "Location: %s\r\n\r\n", request_info->uri, login_url); @@ -244,7 +243,7 @@ static struct session *new_session(void) static void generate_session_id(char *buf, const char *random, const char *user) { - mg_md5(buf, random, user, NULL); + httplib_md5(buf, random, user, NULL); } static void send_server_message(const char *fmt, ...) @@ -264,8 +263,8 @@ static void send_server_message(const char *fmt, ...) // A handler for the /authorize endpoint. // Login page form sends user name and password to this endpoint. -static void authorize(struct mg_connection *conn, - const struct mg_request_info *request_info) +static void authorize(struct httplib_connection *conn, + const struct httplib_request_info *request_info) { char user[MAX_USER_LEN], password[MAX_USER_LEN]; struct session *session; @@ -290,7 +289,7 @@ static void authorize(struct mg_connection *conn, snprintf(session->random, sizeof(session->random), "%d", rand()); generate_session_id(session->session_id, session->random, session->user); send_server_message("<%s> joined", session->user); - mg_printf(conn, "HTTP/1.1 302 Found\r\n" + httplib_printf(conn, "HTTP/1.1 302 Found\r\n" "Set-Cookie: session=%s; max-age=3600; http-only\r\n" // Session ID "Set-Cookie: user=%s\r\n" // Set user, needed by Javascript code "Set-Cookie: original_url=/; max-age=0\r\n" // Delete original_url @@ -303,8 +302,8 @@ static void authorize(struct mg_connection *conn, } // Return 1 if request is authorized, 0 otherwise. -static int is_authorized(const struct mg_connection *conn, - const struct mg_request_info *request_info) +static int is_authorized(const struct httplib_connection *conn, + const struct httplib_request_info *request_info) { struct session *session; char valid_id[33]; @@ -329,22 +328,22 @@ static int is_authorized(const struct mg_connection *conn, return authorized; } -static void redirect_to_ssl(struct mg_connection *conn, - const struct mg_request_info *request_info) +static void redirect_to_ssl(struct httplib_connection *conn, + const struct httplib_request_info *request_info) { - const char *p, *host = mg_get_header(conn, "Host"); + const char *p, *host = httplib_get_header(conn, "Host"); if (host != NULL && (p = strchr(host, ':')) != NULL) { - mg_printf(conn, "HTTP/1.1 302 Found\r\n" + httplib_printf(conn, "HTTP/1.1 302 Found\r\n" "Location: https://%.*s:8082/%s:8082\r\n\r\n", (int) (p - host), host, request_info->uri); } else { - mg_printf(conn, "%s", "HTTP/1.1 500 Error\r\n\r\nHost: header is not set"); + httplib_printf(conn, "%s", "HTTP/1.1 500 Error\r\n\r\nHost: header is not set"); } } -static int begin_request_handler(struct mg_connection *conn) +static int begin_request_handler(struct httplib_connection *conn) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); int processed = 1; if (!request_info->is_ssl) { @@ -375,8 +374,8 @@ static const char *options[] = { int main(void) { - struct mg_callbacks callbacks; - struct mg_context *ctx; + struct httplib_callbacks callbacks; + struct httplib_context *ctx; // Initialize random number generator. It will be used later on for // the session identifier creation. @@ -385,16 +384,16 @@ int main(void) // Setup and start Civetweb memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = begin_request_handler; - if ((ctx = mg_start(&callbacks, NULL, options)) == NULL) { + if ((ctx = httplib_start(&callbacks, NULL, options)) == NULL) { printf("%s\n", "Cannot start chat server, fatal exit"); exit(EXIT_FAILURE); } // Wait until enter is pressed, then exit printf("Chat server started on ports %s, press enter to quit.\n", - mg_get_option(ctx, "listening_ports")); + httplib_get_option(ctx, "listening_ports")); getchar(); - mg_stop(ctx); + httplib_stop(ctx); printf("%s\n", "Chat server stopped."); return EXIT_SUCCESS; diff --git a/examples/embedded_c/embedded_c.c b/examples/embedded_c/embedded_c.c index ca542c61..8bb5d3bc 100644 --- a/examples/embedded_c/embedded_c.c +++ b/examples/embedded_c/embedded_c.c @@ -38,157 +38,157 @@ int exitNow = 0; int -ExampleHandler(struct mg_connection *conn, void *cbdata) +ExampleHandler(struct httplib_connection *conn, void *cbdata) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is an example text from a C handler

"); - mg_printf( + httplib_printf(conn, ""); + httplib_printf(conn, "

This is an example text from a C handler

"); + httplib_printf( conn, "

To see a page from the A handler click A

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the A handler click " "A/A

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the A/B handler click A/B

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the B handler (0) click B

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the B handler (1) click B/A

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the B handler (2) click B/B

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the *.foo handler click xy.foo

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the close handler click close

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the FileHandler handler click form (the starting point of the " "form test)

"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the CookieHandler handler click cookie

"); - mg_printf(conn, + httplib_printf(conn, "

To see an example for parsing files on the fly click form (form for " "uploading files)

"); #ifdef USE_WEBSOCKET - mg_printf(conn, + httplib_printf(conn, "

To test websocket handler click " "websocket

"); #endif - mg_printf(conn, "

To exit click exit

", EXIT_URI); - mg_printf(conn, "\n"); + httplib_printf(conn, "

To exit click exit

", EXIT_URI); + httplib_printf(conn, "\n"); return 1; } int -ExitHandler(struct mg_connection *conn, void *cbdata) +ExitHandler(struct httplib_connection *conn, void *cbdata) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/plain\r\nConnection: close\r\n\r\n"); - mg_printf(conn, "Server will shut down.\n"); - mg_printf(conn, "Bye!\n"); + httplib_printf(conn, "Server will shut down.\n"); + httplib_printf(conn, "Bye!\n"); exitNow = 1; return 1; } int -AHandler(struct mg_connection *conn, void *cbdata) +AHandler(struct httplib_connection *conn, void *cbdata) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is the A handler!!!

"); - mg_printf(conn, "\n"); + httplib_printf(conn, ""); + httplib_printf(conn, "

This is the A handler!!!

"); + httplib_printf(conn, "\n"); return 1; } int -ABHandler(struct mg_connection *conn, void *cbdata) +ABHandler(struct httplib_connection *conn, void *cbdata) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is the AB handler!!!

"); - mg_printf(conn, "\n"); + httplib_printf(conn, ""); + httplib_printf(conn, "

This is the AB handler!!!

"); + httplib_printf(conn, "\n"); return 1; } int -BXHandler(struct mg_connection *conn, void *cbdata) +BXHandler(struct httplib_connection *conn, void *cbdata) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is the BX handler %p!!!

", cbdata); - mg_printf(conn, "

The actual uri is %s

", req_info->uri); - mg_printf(conn, "\n"); + httplib_printf(conn, ""); + httplib_printf(conn, "

This is the BX handler %p!!!

", cbdata); + httplib_printf(conn, "

The actual uri is %s

", req_info->uri); + httplib_printf(conn, "\n"); return 1; } int -FooHandler(struct mg_connection *conn, void *cbdata) +FooHandler(struct httplib_connection *conn, void *cbdata) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is the Foo handler!!!

"); - mg_printf(conn, + httplib_printf(conn, ""); + httplib_printf(conn, "

This is the Foo handler!!!

"); + httplib_printf(conn, "

The request was:

%s %s HTTP/%s

", req_info->request_method, req_info->uri, req_info->http_version); - mg_printf(conn, "\n"); + httplib_printf(conn, "\n"); return 1; } int -CloseHandler(struct mg_connection *conn, void *cbdata) +CloseHandler(struct httplib_connection *conn, void *cbdata) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, + httplib_printf(conn, ""); + httplib_printf(conn, "

This handler will close the connection in a second

"); #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif - mg_printf(conn, "bye"); + httplib_printf(conn, "bye"); printf("CloseHandler: close connection\n"); - mg_close_connection(conn); + httplib_close_connection(conn); printf("CloseHandler: wait 10 sec\n"); #ifdef _WIN32 Sleep(10000); @@ -201,12 +201,12 @@ CloseHandler(struct mg_connection *conn, void *cbdata) int -FileHandler(struct mg_connection *conn, void *cbdata) +FileHandler(struct httplib_connection *conn, void *cbdata) { /* In this handler, we ignore the req_info and send the file "fileName". */ const char *fileName = (const char *)cbdata; - mg_send_file(conn, fileName); + httplib_send_file(conn, fileName); return 1; } @@ -218,9 +218,9 @@ field_found(const char *key, size_t pathlen, void *user_data) { - struct mg_connection *conn = (struct mg_connection *)user_data; + struct httplib_connection *conn = (struct httplib_connection *)user_data; - mg_printf(conn, "\r\n\r\n%s:\r\n", key); + httplib_printf(conn, "\r\n\r\n%s:\r\n", key); if (filename && *filename) { #ifdef _WIN32 @@ -237,12 +237,12 @@ field_found(const char *key, int field_get(const char *key, const char *value, size_t valuelen, void *user_data) { - struct mg_connection *conn = (struct mg_connection *)user_data; + struct httplib_connection *conn = (struct httplib_connection *)user_data; if (key[0]) { - mg_printf(conn, "%s = ", key); + httplib_printf(conn, "%s = ", key); } - mg_write(conn, value, valuelen); + httplib_write(conn, value, valuelen); return 0; } @@ -251,62 +251,59 @@ field_get(const char *key, const char *value, size_t valuelen, void *user_data) int field_stored(const char *path, long long file_size, void *user_data) { - struct mg_connection *conn = (struct mg_connection *)user_data; + struct httplib_connection *conn = (struct httplib_connection *)user_data; - mg_printf(conn, - "stored as %s (%lu bytes)\r\n\r\n", - path, - (unsigned long)file_size); + httplib_printf(conn, "stored as %s (%lu bytes)\r\n\r\n", path, (unsigned long)file_size); return 0; } int -FormHandler(struct mg_connection *conn, void *cbdata) +FormHandler(struct httplib_connection *conn, void *cbdata) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); int ret; - struct mg_form_data_handler fdh = {field_found, field_get, field_stored, 0}; + struct httplib_form_data_handler fdh = {field_found, field_get, field_stored, 0}; /* It would be possible to check the request info here before calling - * mg_handle_form_request. */ + * httplib_handle_form_request. */ (void)req_info; - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/plain\r\nConnection: close\r\n\r\n"); fdh.user_data = (void *)conn; /* Call the form handler */ - mg_printf(conn, "Form data:"); - ret = mg_handle_form_request(conn, &fdh); - mg_printf(conn, "\r\n%i fields found", ret); + httplib_printf(conn, "Form data:"); + ret = httplib_handle_form_request(conn, &fdh); + httplib_printf(conn, "\r\n%i fields found", ret); return 1; } int -FileUploadForm(struct mg_connection *conn, void *cbdata) +FileUploadForm(struct httplib_connection *conn, void *cbdata) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, "\n"); - mg_printf(conn, "\n\n"); - mg_printf(conn, "\n"); - mg_printf(conn, "File upload\n"); - mg_printf(conn, "\n\n"); - mg_printf(conn, + httplib_printf(conn, "\n"); + httplib_printf(conn, "\n\n"); + httplib_printf(conn, "\n"); + httplib_printf(conn, "File upload\n"); + httplib_printf(conn, "\n\n"); + httplib_printf(conn, "
\n", (const char *)cbdata); - mg_printf(conn, "\n"); - mg_printf(conn, "\n"); - mg_printf(conn, "
\n\n\n"); + httplib_printf(conn, "\n"); + httplib_printf(conn, "\n"); + httplib_printf(conn, "\n\n\n"); return 1; } @@ -371,66 +368,63 @@ field_get_checksum(const char *key, int -CheckSumHandler(struct mg_connection *conn, void *cbdata) +CheckSumHandler(struct httplib_connection *conn, void *cbdata) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); int i, j, ret; struct tfiles_checksums chksums; md5_byte_t digest[16]; - struct mg_form_data_handler fdh = {field_disp_read_on_the_fly, - field_get_checksum, - 0, - (void *)&chksums}; + struct httplib_form_data_handler fdh = {field_disp_read_on_the_fly, field_get_checksum, 0,(void *)&chksums}; /* It would be possible to check the request info here before calling - * mg_handle_form_request. */ + * httplib_handle_form_request. */ (void)req_info; memset(&chksums, 0, sizeof(chksums)); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Connection: close\r\n\r\n"); /* Call the form handler */ - mg_printf(conn, "File checksums:"); - ret = mg_handle_form_request(conn, &fdh); + httplib_printf(conn, "File checksums:"); + ret = httplib_handle_form_request(conn, &fdh); for (i = 0; i < chksums.index; i++) { md5_finish(&(chksums.file[i].chksum), digest); /* Visual Studio 2010+ support llu */ - mg_printf(conn, + httplib_printf(conn, "\r\n%s %llu ", chksums.file[i].name, chksums.file[i].length); for (j = 0; j < 16; j++) { - mg_printf(conn, "%02x", (unsigned int)digest[j]); + httplib_printf(conn, "%02x", (unsigned int)digest[j]); } } - mg_printf(conn, "\r\n%i files\r\n", ret); + httplib_printf(conn, "\r\n%i files\r\n", ret); return 1; } int -CookieHandler(struct mg_connection *conn, void *cbdata) +CookieHandler(struct httplib_connection *conn, void *cbdata) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); - const char *cookie = mg_get_header(conn, "Cookie"); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); + const char *cookie = httplib_get_header(conn, "Cookie"); char first_str[64], count_str[64]; int count; - (void)mg_get_cookie(cookie, "first", first_str, sizeof(first_str)); - (void)mg_get_cookie(cookie, "count", count_str, sizeof(count_str)); + httplib_get_cookie(cookie, "first", first_str, sizeof(first_str)); + httplib_get_cookie(cookie, "count", count_str, sizeof(count_str)); - mg_printf(conn, "HTTP/1.1 200 OK\r\nConnection: close\r\n"); + httplib_printf(conn, "HTTP/1.1 200 OK\r\nConnection: close\r\n"); if (first_str[0] == 0) { time_t t = time(0); struct tm *ptm = localtime(&t); - mg_printf(conn, + httplib_printf(conn, "Set-Cookie: first=%04i-%02i-%02iT%02i:%02i:%02i\r\n", ptm->tm_year + 1900, ptm->tm_mon + 1, @@ -440,42 +434,42 @@ CookieHandler(struct mg_connection *conn, void *cbdata) ptm->tm_sec); } count = (count_str[0] == 0) ? 0 : atoi(count_str); - mg_printf(conn, "Set-Cookie: count=%i\r\n", count + 1); - mg_printf(conn, "Content-Type: text/html\r\n\r\n"); + httplib_printf(conn, "Set-Cookie: count=%i\r\n", count + 1); + httplib_printf(conn, "Content-Type: text/html\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is the CookieHandler.

"); - mg_printf(conn, "

The actual uri is %s

", req_info->uri); + httplib_printf(conn, ""); + httplib_printf(conn, "

This is the CookieHandler.

"); + httplib_printf(conn, "

The actual uri is %s

", req_info->uri); if (first_str[0] == 0) { - mg_printf(conn, "

This is the first time, you opened this page

"); + httplib_printf(conn, "

This is the first time, you opened this page

"); } else { - mg_printf(conn, "

You opened this page %i times before.

", count); - mg_printf(conn, "

You first opened this page on %s.

", first_str); + httplib_printf(conn, "

You opened this page %i times before.

", count); + httplib_printf(conn, "

You first opened this page on %s.

", first_str); } - mg_printf(conn, "\n"); + httplib_printf(conn, "\n"); return 1; } int -WebSocketStartHandler(struct mg_connection *conn, void *cbdata) +WebSocketStartHandler(struct httplib_connection *conn, void *cbdata) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: " "close\r\n\r\n"); - mg_printf(conn, "\n"); - mg_printf(conn, "\n\n"); - mg_printf(conn, "\n"); - mg_printf(conn, "Embedded websocket example\n"); + httplib_printf(conn, "\n"); + httplib_printf(conn, "\n\n"); + httplib_printf(conn, "\n"); + httplib_printf(conn, "Embedded websocket example\n"); #ifdef USE_WEBSOCKET - /* mg_printf(conn, "\n"); ... xhtml style */ - mg_printf(conn, "\n"); - mg_printf(conn, "\n\n"); - mg_printf( + /* httplib_printf(conn, "]]>\n"); ... xhtml style */ + httplib_printf(conn, "\n"); + httplib_printf(conn, "\n\n"); + httplib_printf( conn, "
No websocket connection yet
\n"); #else - mg_printf(conn, "\n\n"); - mg_printf(conn, "Example not compiled with USE_WEBSOCKET\n"); + httplib_printf(conn, "\n\n"); + httplib_printf(conn, "Example not compiled with USE_WEBSOCKET\n"); #endif - mg_printf(conn, "\n\n"); + httplib_printf(conn, "\n\n"); return 1; } @@ -517,7 +511,7 @@ WebSocketStartHandler(struct mg_connection *conn, void *cbdata) #define MAX_WS_CLIENTS (5) struct t_ws_client { - struct mg_connection *conn; + struct httplib_connection *conn; int state; } static ws_clients[MAX_WS_CLIENTS]; @@ -533,39 +527,36 @@ struct t_ws_client { int -WebSocketConnectHandler(const struct mg_connection *conn, void *cbdata) +WebSocketConnectHandler(const struct httplib_connection *conn, void *cbdata) { - struct mg_context *ctx = mg_get_context(conn); + struct httplib_context *ctx = httplib_get_context(conn); int reject = 1; int i; - mg_lock_context(ctx); + httplib_lock_context(ctx); for (i = 0; i < MAX_WS_CLIENTS; i++) { if (ws_clients[i].conn == NULL) { - ws_clients[i].conn = (struct mg_connection *)conn; + ws_clients[i].conn = (struct httplib_connection *)conn; ws_clients[i].state = 1; - mg_set_user_connection_data(ws_clients[i].conn, - (void *)(ws_clients + i)); + httplib_set_user_connection_data(ws_clients[i].conn, (void *)(ws_clients + i)); reject = 0; break; } } - mg_unlock_context(ctx); + httplib_unlock_context(ctx); - fprintf(stdout, - "Websocket client %s\r\n\r\n", - (reject ? "rejected" : "accepted")); + fprintf(stdout, "Websocket client %s\r\n\r\n", (reject ? "rejected" : "accepted")); return reject; } void -WebSocketReadyHandler(struct mg_connection *conn, void *cbdata) +WebSocketReadyHandler(struct httplib_connection *conn, void *cbdata) { const char *text = "Hello from the websocket ready handler"; - struct t_ws_client *client = mg_get_user_connection_data(conn); + struct t_ws_client *client = httplib_get_user_connection_data(conn); - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text)); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text)); fprintf(stdout, "Greeting message sent to websocket client\r\n\r\n"); ASSERT(client->conn == conn); ASSERT(client->state == 1); @@ -575,13 +566,9 @@ WebSocketReadyHandler(struct mg_connection *conn, void *cbdata) int -WebsocketDataHandler(struct mg_connection *conn, - int bits, - char *data, - size_t len, - void *cbdata) +WebsocketDataHandler(struct httplib_connection *conn, int bits, char *data, size_t len, void *cbdata) { - struct t_ws_client *client = mg_get_user_connection_data(conn); + struct t_ws_client *client = httplib_get_user_connection_data(conn); ASSERT(client->conn == conn); ASSERT(client->state >= 1); @@ -594,25 +581,24 @@ WebsocketDataHandler(struct mg_connection *conn, void -WebSocketCloseHandler(const struct mg_connection *conn, void *cbdata) +WebSocketCloseHandler(const struct httplib_connection *conn, void *cbdata) { - struct mg_context *ctx = mg_get_context(conn); - struct t_ws_client *client = mg_get_user_connection_data(conn); + struct httplib_context *ctx = httplib_get_context(conn); + struct t_ws_client *client = httplib_get_user_connection_data(conn); ASSERT(client->conn == conn); ASSERT(client->state >= 1); - mg_lock_context(ctx); + httplib_lock_context(ctx); client->state = 0; client->conn = NULL; - mg_unlock_context(ctx); + httplib_unlock_context(ctx); - fprintf(stdout, - "Client droped from the set of webserver connections\r\n\r\n"); + fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n"); } void -InformWebsockets(struct mg_context *ctx) +InformWebsockets(struct httplib_context *ctx) { static unsigned long cnt = 0; char text[32]; @@ -620,16 +606,13 @@ InformWebsockets(struct mg_context *ctx) sprintf(text, "%lu", ++cnt); - mg_lock_context(ctx); + httplib_lock_context(ctx); for (i = 0; i < MAX_WS_CLIENTS; i++) { if (ws_clients[i].state == 2) { - mg_websocket_write(ws_clients[i].conn, - WEBSOCKET_OPCODE_TEXT, - text, - strlen(text)); + httplib_websocket_write(ws_clients[i].conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text)); } } - mg_unlock_context(ctx); + httplib_unlock_context(ctx); } #endif @@ -747,15 +730,15 @@ main(int argc, char *argv[]) #endif #endif 0}; - struct mg_callbacks callbacks; - struct mg_context *ctx; - struct mg_server_ports ports[32]; + struct httplib_callbacks callbacks; + struct httplib_context *ctx; + struct httplib_server_ports ports[32]; int port_cnt, n; int err = 0; /* Check if libcivetweb has been built with all required features. */ #ifdef USE_IPV6 - if (!mg_check_feature(8)) { + if (!httplib_check_feature(8)) { fprintf(stderr, "Error: Embedded example built with IPv6 support, " "but civetweb library build without.\n"); @@ -763,7 +746,7 @@ main(int argc, char *argv[]) } #endif #ifdef USE_WEBSOCKET - if (!mg_check_feature(16)) { + if (!httplib_check_feature(16)) { fprintf(stderr, "Error: Embedded example built with websocket support, " "but civetweb library build without.\n"); @@ -771,7 +754,7 @@ main(int argc, char *argv[]) } #endif #ifndef NO_SSL - if (!mg_check_feature(2)) { + if (!httplib_check_feature(2)) { fprintf(stderr, "Error: Embedded example built with SSL support, " "but civetweb library build without.\n"); @@ -788,58 +771,49 @@ main(int argc, char *argv[]) #ifndef NO_SSL callbacks.init_ssl = init_ssl; #endif - ctx = mg_start(&callbacks, 0, options); + ctx = httplib_start(&callbacks, 0, options); /* Add handler EXAMPLE_URI, to explain the example */ - mg_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0); - mg_set_request_handler(ctx, EXIT_URI, ExitHandler, 0); + httplib_set_request_handler(ctx, EXAMPLE_URI, ExampleHandler, 0); + httplib_set_request_handler(ctx, EXIT_URI, ExitHandler, 0); /* Add handler for /A* and special handler for /A/B */ - mg_set_request_handler(ctx, "/A", AHandler, 0); - mg_set_request_handler(ctx, "/A/B", ABHandler, 0); + httplib_set_request_handler(ctx, "/A", AHandler, 0); + httplib_set_request_handler(ctx, "/A/B", ABHandler, 0); /* Add handler for /B, /B/A, /B/B but not for /B* */ - mg_set_request_handler(ctx, "/B$", BXHandler, (void *)0); - mg_set_request_handler(ctx, "/B/A$", BXHandler, (void *)1); - mg_set_request_handler(ctx, "/B/B$", BXHandler, (void *)2); + httplib_set_request_handler(ctx, "/B$", BXHandler, (void *)0); + httplib_set_request_handler(ctx, "/B/A$", BXHandler, (void *)1); + httplib_set_request_handler(ctx, "/B/B$", BXHandler, (void *)2); /* Add handler for all files with .foo extention */ - mg_set_request_handler(ctx, "**.foo$", FooHandler, 0); + httplib_set_request_handler(ctx, "**.foo$", FooHandler, 0); /* Add handler for /close extention */ - mg_set_request_handler(ctx, "/close", CloseHandler, 0); + httplib_set_request_handler(ctx, "/close", CloseHandler, 0); /* Add handler for /form (serve a file outside the document root) */ - mg_set_request_handler(ctx, - "/form", - FileHandler, - (void *)"../../test/form.html"); + httplib_set_request_handler(ctx, "/form", FileHandler, (void *)"../../test/form.html"); /* Add handler for form data */ - mg_set_request_handler(ctx, + httplib_set_request_handler(ctx, "/handle_form.embedded_c.example.callback", FormHandler, (void *)0); /* Add a file upload handler for parsing files on the fly */ - mg_set_request_handler(ctx, - "/on_the_fly_form", - FileUploadForm, - (void *)"/on_the_fly_form.md5.callback"); - mg_set_request_handler(ctx, - "/on_the_fly_form.md5.callback", - CheckSumHandler, - (void *)0); + httplib_set_request_handler(ctx, "/on_the_fly_form", FileUploadForm, (void *)"/on_the_fly_form.md5.callback"); + httplib_set_request_handler(ctx, "/on_the_fly_form.md5.callback", CheckSumHandler, (void *)0); /* Add handler for /cookie example */ - mg_set_request_handler(ctx, "/cookie", CookieHandler, 0); + httplib_set_request_handler(ctx, "/cookie", CookieHandler, 0); /* Add HTTP site to open a websocket connection */ - mg_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0); + httplib_set_request_handler(ctx, "/websocket", WebSocketStartHandler, 0); #ifdef USE_WEBSOCKET /* WS site for the websocket connection */ - mg_set_websocket_handler(ctx, + httplib_set_websocket_handler(ctx, "/websocket", WebSocketConnectHandler, WebSocketReadyHandler, @@ -850,7 +824,7 @@ main(int argc, char *argv[]) /* List all listening ports */ memset(ports, 0, sizeof(ports)); - port_cnt = mg_get_server_ports(ctx, 32, ports); + port_cnt = httplib_get_server_ports(ctx, 32, ports); printf("\n%i listening ports:\n\n", port_cnt); for (n = 0; n < port_cnt && n < 32; n++) { @@ -899,7 +873,7 @@ main(int argc, char *argv[]) } /* Stop the server */ - mg_stop(ctx); + httplib_stop(ctx); printf("Server stopped.\n"); printf("Bye!\n"); diff --git a/examples/embedded_cpp/embedded_cpp.cpp b/examples/embedded_cpp/embedded_cpp.cpp index 67d1e43d..f7640d12 100644 --- a/examples/embedded_cpp/embedded_cpp.cpp +++ b/examples/embedded_cpp/embedded_cpp.cpp @@ -23,30 +23,30 @@ class ExampleHandler : public CivetHandler { public: bool - handleGet(CivetServer *server, struct mg_connection *conn) + handleGet(CivetServer *server, struct httplib_connection *conn) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/html\r\nConnection: close\r\n\r\n"); - mg_printf(conn, "\r\n"); - mg_printf(conn, + httplib_printf(conn, "\r\n"); + httplib_printf(conn, "

This is an example text from a C++ handler

\r\n"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the A handler click here

\r\n"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the A handler with a parameter " "click here

\r\n"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the A/B handler click here

\r\n"); - mg_printf(conn, + httplib_printf(conn, "

To see a page from the *.foo handler click here

\r\n"); - mg_printf(conn, + httplib_printf(conn, "

To exit click here

\r\n", EXIT_URI); - mg_printf(conn, "\r\n"); + httplib_printf(conn, "\r\n"); return true; } }; @@ -55,12 +55,12 @@ class ExitHandler : public CivetHandler { public: bool - handleGet(CivetServer *server, struct mg_connection *conn) + handleGet(CivetServer *server, struct httplib_connection *conn) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/plain\r\nConnection: close\r\n\r\n"); - mg_printf(conn, "Bye!\n"); + httplib_printf(conn, "Bye!\n"); exitNow = true; return true; } @@ -72,31 +72,31 @@ class AHandler : public CivetHandler bool handleAll(const char *method, CivetServer *server, - struct mg_connection *conn) + struct httplib_connection *conn) { std::string s = ""; - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/html\r\nConnection: close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is the A handler for \"%s\" !

", method); + httplib_printf(conn, ""); + httplib_printf(conn, "

This is the A handler for \"%s\" !

", method); if (CivetServer::getParam(conn, "param", s)) { - mg_printf(conn, "

param set to %s

", s.c_str()); + httplib_printf(conn, "

param set to %s

", s.c_str()); } else { - mg_printf(conn, "

param not set

"); + httplib_printf(conn, "

param not set

"); } - mg_printf(conn, "\n"); + httplib_printf(conn, "\n"); return true; } public: bool - handleGet(CivetServer *server, struct mg_connection *conn) + handleGet(CivetServer *server, struct httplib_connection *conn) { return handleAll("GET", server, conn); } bool - handlePost(CivetServer *server, struct mg_connection *conn) + handlePost(CivetServer *server, struct httplib_connection *conn) { return handleAll("POST", server, conn); } @@ -106,14 +106,14 @@ class ABHandler : public CivetHandler { public: bool - handleGet(CivetServer *server, struct mg_connection *conn) + handleGet(CivetServer *server, struct httplib_connection *conn) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/html\r\nConnection: close\r\n\r\n"); - mg_printf(conn, ""); - mg_printf(conn, "

This is the AB handler!!!

"); - mg_printf(conn, "\n"); + httplib_printf(conn, ""); + httplib_printf(conn, "

This is the AB handler!!!

"); + httplib_printf(conn, "\n"); return true; } }; @@ -122,68 +122,64 @@ class FooHandler : public CivetHandler { public: bool - handleGet(CivetServer *server, struct mg_connection *conn) + handleGet(CivetServer *server, struct httplib_connection *conn) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/html\r\nConnection: close\r\n\r\n"); - mg_printf(conn, "\n"); - mg_printf(conn, "

This is the Foo GET handler!!!

\n"); - mg_printf(conn, + httplib_printf(conn, "\n"); + httplib_printf(conn, "

This is the Foo GET handler!!!

\n"); + httplib_printf(conn, "

The request was:

%s %s HTTP/%s

\n", req_info->request_method, req_info->uri, req_info->http_version); - mg_printf(conn, "\n"); + httplib_printf(conn, "\n"); return true; } bool - handlePost(CivetServer *server, struct mg_connection *conn) + handlePost(CivetServer *server, struct httplib_connection *conn) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); long long rlen, wlen; long long nlen = 0; long long tlen = req_info->content_length; char buf[1024]; - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\nContent-Type: " "text/html\r\nConnection: close\r\n\r\n"); - mg_printf(conn, "\n"); - mg_printf(conn, "

This is the Foo POST handler!!!

\n"); - mg_printf(conn, + httplib_printf(conn, "\n"); + httplib_printf(conn, "

This is the Foo POST handler!!!

\n"); + httplib_printf(conn, "

The request was:

%s %s HTTP/%s

\n", req_info->request_method, req_info->uri, req_info->http_version); - mg_printf(conn, "

Content Length: %li

\n", (long)tlen); - mg_printf(conn, "
\n");
+		httplib_printf(conn, "

Content Length: %li

\n", (long)tlen); + httplib_printf(conn, "
\n");
 
 		while (nlen < tlen) {
 			rlen = tlen - nlen;
 			if (rlen > sizeof(buf)) {
 				rlen = sizeof(buf);
 			}
-			rlen = mg_read(conn, buf, (size_t)rlen);
-			if (rlen <= 0) {
-				break;
-			}
-			wlen = mg_write(conn, buf, (size_t)rlen);
-			if (rlen != rlen) {
-				break;
-			}
+			rlen = httplib_read(conn, buf, (size_t)rlen);
+			if (rlen <= 0) break;
+			wlen = httplib_write(conn, buf, (size_t)rlen);
+			if (rlen != rlen) break;
 			nlen += wlen;
 		}
 
-		mg_printf(conn, "\n
\n"); - mg_printf(conn, "\n"); + httplib_printf(conn, "\n
\n"); + httplib_printf(conn, "\n"); return true; } @@ -191,10 +187,10 @@ class FooHandler : public CivetHandler #define fopen_recursive fopen bool - handlePut(CivetServer *server, struct mg_connection *conn) + handlePut(CivetServer *server, struct httplib_connection *conn) { - /* Handler may access the request info using mg_get_request_info */ - const struct mg_request_info *req_info = mg_get_request_info(conn); + /* Handler may access the request info using httplib_get_request_info */ + const struct httplib_request_info *req_info = httplib_get_request_info(conn); long long rlen, wlen; long long nlen = 0; long long tlen = req_info->content_length; @@ -220,7 +216,7 @@ class FooHandler : public CivetHandler if (rlen > sizeof(buf)) { rlen = sizeof(buf); } - rlen = mg_read(conn, buf, (size_t)rlen); + rlen = httplib_read(conn, buf, (size_t)rlen); if (rlen <= 0) { fail = 1; break; @@ -236,12 +232,12 @@ class FooHandler : public CivetHandler } if (fail) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 409 Conflict\r\n" "Content-Type: text/plain\r\n" "Connection: close\r\n\r\n"); } else { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 201 Created\r\n" "Content-Type: text/plain\r\n" "Connection: close\r\n\r\n"); diff --git a/examples/hello/hello.c b/examples/hello/hello.c index 39253c1a..a77af2ec 100644 --- a/examples/hello/hello.c +++ b/examples/hello/hello.c @@ -3,9 +3,9 @@ #include "civetweb.h" // This function will be called by civetweb on every new request. -static int begin_request_handler(struct mg_connection *conn) +static int begin_request_handler(struct httplib_connection *conn) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); char content[100]; // Prepare the message we're going to send @@ -14,7 +14,7 @@ static int begin_request_handler(struct mg_connection *conn) request_info->remote_port); // Send HTTP reply to the client - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" "Content-Length: %d\r\n" // Always set Content-Length @@ -29,8 +29,8 @@ static int begin_request_handler(struct mg_connection *conn) int main(void) { - struct mg_context *ctx; - struct mg_callbacks callbacks; + struct httplib_context *ctx; + struct httplib_callbacks callbacks; // List of options. Last element must be NULL. const char *options[] = {"listening_ports", "8080", NULL}; @@ -40,14 +40,14 @@ int main(void) callbacks.begin_request = begin_request_handler; // Start the web server. - ctx = mg_start(&callbacks, NULL, options); + ctx = httplib_start(&callbacks, NULL, options); // Wait until user hits "enter". Server is running in separate thread. // Navigating to http://localhost:8080 will invoke begin_request_handler(). getchar(); // Stop the server. - mg_stop(ctx); + httplib_stop(ctx); return 0; } diff --git a/examples/post/post.c b/examples/post/post.c index 3c5c68c8..e9faa2a2 100644 --- a/examples/post/post.c +++ b/examples/post/post.c @@ -10,22 +10,22 @@ static const char *html_form = "" ""; -static int begin_request_handler(struct mg_connection *conn) +static int begin_request_handler(struct httplib_connection *conn) { - const struct mg_request_info *ri = mg_get_request_info(conn); + const struct httplib_request_info *ri = httplib_get_request_info(conn); char post_data[1024], input1[sizeof(post_data)], input2[sizeof(post_data)]; int post_data_len; if (!strcmp(ri->uri, "/handle_post_request")) { // User has submitted a form, show submitted data and a variable value - post_data_len = mg_read(conn, post_data, sizeof(post_data)); + post_data_len = httplib_read(conn, post_data, sizeof(post_data)); // Parse form data. input1 and input2 are guaranteed to be NUL-terminated - mg_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1)); - mg_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2)); + httplib_get_var(post_data, post_data_len, "input_1", input1, sizeof(input1)); + httplib_get_var(post_data, post_data_len, "input_2", input2, sizeof(input2)); // Send reply to the client, showing submitted form values. - mg_printf(conn, "HTTP/1.0 200 OK\r\n" + httplib_printf(conn, "HTTP/1.0 200 OK\r\n" "Content-Type: text/plain\r\n\r\n" "Submitted data: [%.*s]\n" "Submitted data length: %d bytes\n" @@ -34,7 +34,7 @@ static int begin_request_handler(struct mg_connection *conn) post_data_len, post_data, post_data_len, input1, input2); } else { // Show HTML form. - mg_printf(conn, "HTTP/1.0 200 OK\r\n" + httplib_printf(conn, "HTTP/1.0 200 OK\r\n" "Content-Length: %d\r\n" "Content-Type: text/html\r\n\r\n%s", (int) strlen(html_form), html_form); @@ -44,15 +44,15 @@ static int begin_request_handler(struct mg_connection *conn) int main(void) { - struct mg_context *ctx; + struct httplib_context *ctx; const char *options[] = {"listening_ports", "8080", NULL}; - struct mg_callbacks callbacks; + struct httplib_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = begin_request_handler; - ctx = mg_start(&callbacks, NULL, options); + ctx = httplib_start(&callbacks, NULL, options); getchar(); // Wait until user hits "enter" - mg_stop(ctx); + httplib_stop(ctx); return 0; } diff --git a/examples/upload/upload.c b/examples/upload/upload.c index 56c4be4f..2108cccb 100644 --- a/examples/upload/upload.c +++ b/examples/upload/upload.c @@ -29,7 +29,7 @@ typedef __int64 int64_t; /* callback: used to generate all content */ -static int begin_request_handler(struct mg_connection *conn) +static int begin_request_handler(struct httplib_connection *conn) { const char * tempPath = "."; #ifdef _WIN32 @@ -40,10 +40,10 @@ static int begin_request_handler(struct mg_connection *conn) tempPath = "/tmp"; #endif - if (!strcmp(mg_get_request_info(conn)->uri, "/handle_post_request")) { + if (!strcmp(httplib_get_request_info(conn)->uri, "/handle_post_request")) { - mg_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n"); - mg_upload(conn, tempPath); + httplib_printf(conn, "%s", "HTTP/1.0 200 OK\r\n\r\n"); + httplib_upload(conn, tempPath); } else { /* Show HTML form. */ /* See http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1 */ @@ -60,7 +60,7 @@ static int begin_request_handler(struct mg_connection *conn) "" ""; - mg_printf(conn, "HTTP/1.0 200 OK\r\n" + httplib_printf(conn, "HTTP/1.0 200 OK\r\n" "Content-Length: %d\r\n" "Content-Type: text/html\r\n\r\n%s", (int) strlen(html_form), html_form); @@ -72,9 +72,9 @@ static int begin_request_handler(struct mg_connection *conn) /* callback: called after uploading a file is completed */ -static void upload_handler(struct mg_connection *conn, const char *path) +static void upload_handler(struct httplib_connection *conn, const char *path) { - mg_printf(conn, "Saved [%s]", path); + httplib_printf(conn, "Saved [%s]", path); } @@ -85,11 +85,11 @@ int main(void) const char * PORT = "8080"; /* Startup options for the server */ - struct mg_context *ctx; + struct httplib_context *ctx; const char *options[] = { "listening_ports", PORT, NULL}; - struct mg_callbacks callbacks; + struct httplib_callbacks callbacks; memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = begin_request_handler; @@ -100,11 +100,11 @@ int main(void) printf("Open http://localhost:%s/ in your browser.\n\n", PORT); /* Start the server */ - ctx = mg_start(&callbacks, NULL, options); + ctx = httplib_start(&callbacks, NULL, options); /* Wait until thr user hits "enter", then stop the server */ getchar(); - mg_stop(ctx); + httplib_stop(ctx); return 0; } diff --git a/examples/websocket/WebSockCallbacks.c b/examples/websocket/WebSockCallbacks.c index b31bc956..d7e5a16b 100644 --- a/examples/websocket/WebSockCallbacks.c +++ b/examples/websocket/WebSockCallbacks.c @@ -11,49 +11,49 @@ #ifdef _WIN32 #include -#define mg_sleep(x) Sleep(x) +#define httplib_sleep(x) Sleep(x) #else #include #include -#define mg_sleep(x) usleep((x)*1000) +#define httplib_sleep(x) usleep((x)*1000) #endif static void -send_to_all_websockets(struct mg_context *ctx, const char *data, int data_len) +send_to_all_websockets(struct httplib_context *ctx, const char *data, int data_len) { int i; - tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx); - mg_lock_context(ctx); + httplib_lock_context(ctx); for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { if (ws_ctx->socketList[i] && (ws_ctx->socketList[i]->webSockState == 2)) { - mg_websocket_write(ws_ctx->socketList[i]->conn, + httplib_websocket_write(ws_ctx->socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len); } } - mg_unlock_context(ctx); + httplib_unlock_context(ctx); } void -websocket_ready_handler(struct mg_connection *conn, void *_ignored) +websocket_ready_handler(struct httplib_connection *conn, void *_ignored) { int i; - const struct mg_request_info *rq = mg_get_request_info(conn); - struct mg_context *ctx = mg_get_context(conn); - tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + const struct httplib_request_info *rq = httplib_get_request_info(conn); + struct httplib_context *ctx = httplib_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx); tWebSockInfo *wsock = malloc(sizeof(tWebSockInfo)); assert(wsock); wsock->webSockState = 0; - mg_set_user_connection_data(conn, wsock); + httplib_set_user_connection_data(conn, wsock); - mg_lock_context(ctx); + httplib_lock_context(ctx); for (i = 0; i < MAX_NUM_OF_WEBSOCKS; i++) { if (0 == ws_ctx->socketList[i]) { ws_ctx->socketList[i] = wsock; @@ -65,7 +65,7 @@ websocket_ready_handler(struct mg_connection *conn, void *_ignored) printf("\nNew websocket attached: %s:%u\n", rq->remote_addr, rq->remote_port); - mg_unlock_context(ctx); + httplib_unlock_context(ctx); } @@ -84,33 +84,33 @@ websocket_done(tWebSockContext *ws_ctx, tWebSockInfo *wsock) } } printf("\nClose websocket attached: %s:%u\n", - mg_get_request_info(wsock->conn)->remote_addr, - mg_get_request_info(wsock->conn)->remote_port); + httplib_get_request_info(wsock->conn)->remote_addr, + httplib_get_request_info(wsock->conn)->remote_port); free(wsock); } } int -websocket_data_handler(struct mg_connection *conn, +websocket_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len, void *_ignored) { - const struct mg_request_info *rq = mg_get_request_info(conn); + const struct httplib_request_info *rq = httplib_get_request_info(conn); tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data; - struct mg_context *ctx = mg_get_context(conn); - tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + struct httplib_context *ctx = httplib_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx); char msg[128]; - mg_lock_context(ctx); + httplib_lock_context(ctx); if (flags == 136) { // close websock websocket_done(ws_ctx, wsock); - mg_set_user_connection_data(conn, NULL); - mg_unlock_context(ctx); + httplib_set_user_connection_data(conn, NULL); + httplib_unlock_context(ctx); return 1; } if (((data_len >= 5) && (data_len < 100) && (flags == 129)) @@ -127,42 +127,42 @@ websocket_data_handler(struct mg_connection *conn, if (gid > 0 && chk != NULL && *chk == 0) { wsock->webSockState = 2; } - mg_unlock_context(ctx); + httplib_unlock_context(ctx); return 1; } // chat message if ((wsock->webSockState == 2) && (!memcmp(data, "msg ", 4))) { send_to_all_websockets(ctx, data, data_len); - mg_unlock_context(ctx); + httplib_unlock_context(ctx); return 1; } } // keep alive if ((data_len == 4) && !memcmp(data, "ping", 4)) { - mg_unlock_context(ctx); + httplib_unlock_context(ctx); return 1; } - mg_unlock_context(ctx); + httplib_unlock_context(ctx); return 0; } void -connection_close_handler(const struct mg_connection *conn, void *_ignored) +connection_close_handler(const struct httplib_connection *conn, void *_ignored) { - const struct mg_request_info *rq = mg_get_request_info(conn); + const struct httplib_request_info *rq = httplib_get_request_info(conn); tWebSockInfo *wsock = (tWebSockInfo *)rq->conn_data; - struct mg_context *ctx = mg_get_context(conn); - tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + struct httplib_context *ctx = httplib_get_context(conn); + tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx); - mg_lock_context(ctx); + httplib_lock_context(ctx); websocket_done(ws_ctx, wsock); - mg_set_user_connection_data(conn, NULL); - mg_unlock_context(ctx); + httplib_set_user_connection_data(conn, NULL); + httplib_unlock_context(ctx); } @@ -171,8 +171,8 @@ eventMain(void *arg) { char msg[256]; - struct mg_context *ctx = (struct mg_context *)arg; - tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + struct httplib_context *ctx = (struct httplib_context *)arg; + tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx); ws_ctx->runLoop = 1; while (ws_ctx->runLoop) { @@ -181,7 +181,7 @@ eventMain(void *arg) strftime(msg, sizeof(msg), "title %c", timestr); send_to_all_websockets(ctx, msg, strlen(msg)); - mg_sleep(1000); + httplib_sleep(1000); } return NULL; @@ -189,7 +189,7 @@ eventMain(void *arg) void -websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len) +websock_send_broadcast(struct httplib_context *ctx, const char *data, int data_len) { char buffer[260]; @@ -204,22 +204,22 @@ websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len) void -websock_init_lib(const struct mg_context *ctx) +websock_init_lib(const struct httplib_context *ctx) { - tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx); memset(ws_ctx, 0, sizeof(*ws_ctx)); - /* todo: use mg_start_thread_id instead of mg_start_thread */ - mg_start_thread(eventMain, (void *)ctx); + /* todo: use httplib_start_thread_id instead of httplib_start_thread */ + httplib_start_thread(eventMain, (void *)ctx); } void -websock_exit_lib(const struct mg_context *ctx) +websock_exit_lib(const struct httplib_context *ctx) { - tWebSockContext *ws_ctx = (tWebSockContext *)mg_get_user_data(ctx); + tWebSockContext *ws_ctx = (tWebSockContext *)httplib_get_user_data(ctx); ws_ctx->runLoop = 0; /* todo: wait for the thread instead of a timeout */ - mg_sleep(2000); + httplib_sleep(2000); } diff --git a/examples/websocket/WebSockCallbacks.h b/examples/websocket/WebSockCallbacks.h index f44821da..bfa08bd7 100644 --- a/examples/websocket/WebSockCallbacks.h +++ b/examples/websocket/WebSockCallbacks.h @@ -11,7 +11,7 @@ extern "C" { typedef struct tWebSockInfo { int webSockState; unsigned long initId; - struct mg_connection *conn; + struct httplib_connection *conn; } tWebSockInfo; #define MAX_NUM_OF_WEBSOCKS (256) @@ -22,23 +22,19 @@ typedef struct tWebSockContext { } tWebSockContext; -void websock_init_lib(const struct mg_context *ctx); -void websock_exit_lib(const struct mg_context *ctx); +void websock_init_lib(const struct httplib_context *ctx); +void websock_exit_lib(const struct httplib_context *ctx); void -websock_send_broadcast(struct mg_context *ctx, const char *data, int data_len); +websock_send_broadcast(struct httplib_context *ctx, const char *data, int data_len); -void websocket_ready_handler(struct mg_connection *conn, void *_ignored); -int websocket_data_handler(struct mg_connection *conn, - int flags, - char *data, - size_t data_len, - void *_ignored); -void connection_close_handler(const struct mg_connection *conn, void *_ignored); +void websocket_ready_handler(struct httplib_connection *conn, void *_ignored); +int websocket_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len, void *_ignored); +void connection_close_handler(const struct httplib_connection *conn, void *_ignored); #ifdef __cplusplus } #endif -#endif \ No newline at end of file +#endif diff --git a/examples/websocket/websocket.c b/examples/websocket/websocket.c index 3aadf98b..babc52a8 100644 --- a/examples/websocket/websocket.c +++ b/examples/websocket/websocket.c @@ -13,8 +13,8 @@ int main(void) { - struct mg_context *ctx = 0; - struct mg_callbacks callback_funcs = {0}; + struct httplib_context *ctx = 0; + struct httplib_callbacks callback_funcs = {0}; tWebSockContext ws_ctx; char inbuf[4]; @@ -35,9 +35,9 @@ main(void) callback_funcs.init_context = websock_init_lib; callback_funcs.exit_context = websock_exit_lib; - ctx = mg_start(&callback_funcs, &ws_ctx, server_options); + ctx = httplib_start(&callback_funcs, &ws_ctx, server_options); - mg_set_websocket_handler(ctx, + httplib_set_websocket_handler(ctx, "/MyWebSock", NULL, websocket_ready_handler, @@ -46,7 +46,7 @@ main(void) NULL); printf("Connect to localhost:%s/websock.htm\n", - mg_get_option(ctx, "listening_ports")); + httplib_get_option(ctx, "listening_ports")); puts("Enter an (ASCII) character or * to exit:"); for (;;) { @@ -59,7 +59,7 @@ main(void) websock_send_broadcast(ctx, inbuf, 1); } - mg_stop(ctx); + httplib_stop(ctx); return 0; } diff --git a/examples/websocket_client/websocket_client.c b/examples/websocket_client/websocket_client.c index c7bef625..bb2554bf 100644 --- a/examples/websocket_client/websocket_client.c +++ b/examples/websocket_client/websocket_client.c @@ -40,10 +40,10 @@ const size_t websocket_goodbye_msg_len = 14 /* strlen(websocket_goodbye_msg) */; /*************************************************************************************/ #if defined(MG_LEGACY_INTERFACE) int -websock_server_connect(const struct mg_connection *conn) +websock_server_connect(const struct httplib_connection *conn) #else int -websocket_server_connect(const struct mg_connection *conn, void *_ignored) +websocket_server_connect(const struct httplib_connection *conn, void *_ignored) #endif { printf("Server: Websocket connected\n"); @@ -53,33 +53,33 @@ websocket_server_connect(const struct mg_connection *conn, void *_ignored) #if defined(MG_LEGACY_INTERFACE) void -websocket_server_ready(struct mg_connection *conn) +websocket_server_ready(struct httplib_connection *conn) #else void -websocket_server_ready(struct mg_connection *conn, void *_ignored) +websocket_server_ready(struct httplib_connection *conn, void *_ignored) #endif { printf("Server: Websocket ready\n"); /* Send websocket welcome message */ - mg_lock_connection(conn); - mg_websocket_write(conn, + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, websocket_welcome_msg, websocket_welcome_msg_len); - mg_unlock_connection(conn); + httplib_unlock_connection(conn); } #if defined(MG_LEGACY_INTERFACE) int -websocket_server_data(struct mg_connection *conn, +websocket_server_data(struct httplib_connection *conn, int bits, char *data, size_t data_len) #else int -websocket_server_data(struct mg_connection *conn, +websocket_server_data(struct httplib_connection *conn, int bits, char *data, size_t data_len, @@ -93,20 +93,20 @@ websocket_server_data(struct mg_connection *conn, if (data_len < 3 || 0 != memcmp(data, "bye", 3)) { /* Send websocket acknowledge message */ - mg_lock_connection(conn); - mg_websocket_write(conn, + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, websocket_acknowledge_msg, websocket_acknowledge_msg_len); - mg_unlock_connection(conn); + httplib_unlock_connection(conn); } else { /* Send websocket acknowledge message */ - mg_lock_connection(conn); - mg_websocket_write(conn, + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, websocket_goodbye_msg, websocket_goodbye_msg_len); - mg_unlock_connection(conn); + httplib_unlock_connection(conn); } return 1; /* return 1 to keep the connetion open */ @@ -115,10 +115,10 @@ websocket_server_data(struct mg_connection *conn, #if defined(MG_LEGACY_INTERFACE) void -websocket_server_connection_close(const struct mg_connection *conn) +websocket_server_connection_close(const struct httplib_connection *conn) #else void -websocket_server_connection_close(const struct mg_connection *conn, +websocket_server_connection_close(const struct httplib_connection *conn, void *_ignored) #endif { @@ -129,7 +129,7 @@ websocket_server_connection_close(const struct mg_connection *conn, } -struct mg_context * +struct httplib_context * start_websocket_server() { const char *options[] = {"document_root", @@ -141,8 +141,8 @@ start_websocket_server() "request_timeout_ms", "5000", 0}; - struct mg_callbacks callbacks; - struct mg_context *ctx; + struct httplib_callbacks callbacks; + struct httplib_context *ctx; memset(&callbacks, 0, sizeof(callbacks)); @@ -153,12 +153,12 @@ start_websocket_server() callbacks.websocket_data = websocket_server_data; callbacks.connection_close = websocket_server_connection_close; - ctx = mg_start(&callbacks, 0, options); + ctx = httplib_start(&callbacks, 0, options); #else /* New interface: */ - ctx = mg_start(&callbacks, 0, options); + ctx = httplib_start(&callbacks, 0, options); - mg_set_websocket_handler(ctx, + httplib_set_websocket_handler(ctx, "/websocket", websocket_server_connect, websocket_server_ready, @@ -181,15 +181,11 @@ struct tclient_data { }; static int -websocket_client_data_handler(struct mg_connection *conn, - int flags, - char *data, - size_t data_len, - void *user_data) -{ - struct mg_context *ctx = mg_get_context(conn); +websocket_client_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len, void *user_data) { + + struct httplib_context *ctx = httplib_get_context(conn); struct tclient_data *pclient_data = - (struct tclient_data *)mg_get_user_data(ctx); + (struct tclient_data *)httplib_get_user_data(ctx); printf("Client received data from server: "); fwrite(data, 1, data_len, stdout); @@ -204,12 +200,12 @@ websocket_client_data_handler(struct mg_connection *conn, } static void -websocket_client_close_handler(const struct mg_connection *conn, +websocket_client_close_handler(const struct httplib_connection *conn, void *user_data) { - struct mg_context *ctx = mg_get_context(conn); + struct httplib_context *ctx = httplib_get_context(conn); struct tclient_data *pclient_data = - (struct tclient_data *)mg_get_user_data(ctx); + (struct tclient_data *)httplib_get_user_data(ctx); printf("Client: Close handler\n"); pclient_data->closed++; @@ -219,13 +215,13 @@ websocket_client_close_handler(const struct mg_connection *conn, int main(int argc, char *argv[]) { - struct mg_context *ctx = NULL; + struct httplib_context *ctx = NULL; struct tclient_data client1_data = {NULL, 0, 0}; struct tclient_data client2_data = {NULL, 0, 0}; struct tclient_data client3_data = {NULL, 0, 0}; - struct mg_connection *newconn1 = NULL; - struct mg_connection *newconn2 = NULL; - struct mg_connection *newconn3 = NULL; + struct httplib_connection *newconn1 = NULL; + struct httplib_connection *newconn2 = NULL; + struct httplib_connection *newconn3 = NULL; char ebuf[100] = {0}; assert(websocket_welcome_msg_len == strlen(websocket_welcome_msg)); @@ -236,7 +232,7 @@ main(int argc, char *argv[]) printf("Server init\n\n"); /* Then connect a first client */ - newconn1 = mg_connect_websocket_client("localhost", + newconn1 = httplib_connect_websocket_client("localhost", atoi(PORT), 0, ebuf, @@ -266,7 +262,7 @@ main(int argc, char *argv[]) client1_data.data = NULL; client1_data.len = 0; - mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data1", 5); + httplib_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data1", 5); sleep(1); /* Should get the acknowledge message */ assert(client1_data.closed == 0); @@ -283,7 +279,7 @@ main(int argc, char *argv[]) client1_data.len = 0; /* Now connect a second client */ - newconn2 = mg_connect_websocket_client("localhost", + newconn2 = httplib_connect_websocket_client("localhost", atoi(PORT), 0, ebuf, @@ -313,7 +309,7 @@ main(int argc, char *argv[]) client2_data.data = NULL; client2_data.len = 0; - mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data2", 5); + httplib_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "data2", 5); sleep(1); /* Should get the acknowledge message */ assert(client1_data.closed == 0); @@ -329,7 +325,7 @@ main(int argc, char *argv[]) client1_data.data = NULL; client1_data.len = 0; - mg_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "bye", 3); + httplib_websocket_client_write(newconn1, WEBSOCKET_OPCODE_TEXT, "bye", 3); sleep(1); /* Should get the goodbye message */ assert(client1_data.closed == 0); @@ -345,7 +341,7 @@ main(int argc, char *argv[]) client1_data.data = NULL; client1_data.len = 0; - mg_close_connection(newconn1); + httplib_close_connection(newconn1); sleep(1); /* Won't get any message */ assert(client1_data.closed == 1); @@ -355,7 +351,7 @@ main(int argc, char *argv[]) assert(client2_data.data == NULL); assert(client2_data.len == 0); - mg_websocket_client_write(newconn2, WEBSOCKET_OPCODE_TEXT, "bye", 3); + httplib_websocket_client_write(newconn2, WEBSOCKET_OPCODE_TEXT, "bye", 3); sleep(1); /* Should get the goodbye message */ assert(client1_data.closed == 1); @@ -371,7 +367,7 @@ main(int argc, char *argv[]) client2_data.data = NULL; client2_data.len = 0; - mg_close_connection(newconn2); + httplib_close_connection(newconn2); sleep(1); /* Won't get any message */ assert(client1_data.closed == 1); @@ -382,7 +378,7 @@ main(int argc, char *argv[]) assert(client2_data.len == 0); /* Connect client 3 */ - newconn3 = mg_connect_websocket_client("localhost", + newconn3 = httplib_connect_websocket_client("localhost", atoi(PORT), 0, ebuf, @@ -410,7 +406,7 @@ main(int argc, char *argv[]) client3_data.data = NULL; client3_data.len = 0; - mg_stop(ctx); + httplib_stop(ctx); printf("Server shutdown\n"); sleep(10); diff --git a/examples/ws_server/ws_server.c b/examples/ws_server/ws_server.c index 575a26a6..fb5ffad9 100644 --- a/examples/ws_server/ws_server.c +++ b/examples/ws_server/ws_server.c @@ -12,7 +12,7 @@ // simple structure for keeping track of websocket connection struct ws_connection { - struct mg_connection *conn; + struct httplib_connection *conn; int update; int closing; }; @@ -36,7 +36,7 @@ static struct ws_connection ws_conn[CONNECTIONS]; static void *ws_server_thread(void *parm) { int wsd = (long)parm; - struct mg_connection *conn = ws_conn[wsd].conn; + struct httplib_connection *conn = ws_conn[wsd].conn; int timer = 0; char tstr[32]; int i; @@ -61,7 +61,7 @@ static void *ws_server_thread(void *parm) meter[i].value = meter[i].limit; sprintf(tstr, "meter%d:%d,%d", i+1, meter[i].value, meter[i].limit); - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr)); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr)); } /* While the connection is open, send periodic updates */ @@ -83,7 +83,7 @@ static void *ws_server_thread(void *parm) if (!ws_conn[wsd].closing) { sprintf(tstr, "meter%d:%d,%d", i+1, meter[i].value, meter[i].limit); - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr)); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, tstr, strlen(tstr)); } } } @@ -91,7 +91,7 @@ static void *ws_server_thread(void *parm) /* Send periodic PING to assure websocket remains connected, except if we are closing */ if (timer%100 == 0 && !ws_conn[wsd].closing) - mg_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_PING, NULL, 0); } fprintf(stderr, "ws_server_thread %d exiting\n", wsd); @@ -108,7 +108,7 @@ static void *ws_server_thread(void *parm) // On new client connection, find next available server connection and store // new connection information. If no more server connections are available // tell civetweb to not accept the client request. -static int websocket_connect_handler(const struct mg_connection *conn) +static int websocket_connect_handler(const struct httplib_connection *conn) { int i; @@ -117,7 +117,7 @@ static int websocket_connect_handler(const struct mg_connection *conn) for(i=0; i < CONNECTIONS; ++i) { if (ws_conn[i].conn == NULL) { fprintf(stderr, "...prep for server %d\n", i); - ws_conn[i].conn = (struct mg_connection *)conn; + ws_conn[i].conn = (struct httplib_connection *)conn; ws_conn[i].closing = 0; ws_conn[i].update = 0; break; @@ -133,7 +133,7 @@ static int websocket_connect_handler(const struct mg_connection *conn) // websocket_ready_handler() // Once websocket negotiation is complete, start a server for the connection -static void websocket_ready_handler(struct mg_connection *conn) +static void websocket_ready_handler(struct httplib_connection *conn) { int i; @@ -142,7 +142,7 @@ static void websocket_ready_handler(struct mg_connection *conn) for(i=0; i < CONNECTIONS; ++i) { if (ws_conn[i].conn == conn) { fprintf(stderr, "...start server %d\n", i); - mg_start_thread(ws_server_thread, (void *)(long)i); + httplib_start_thread(ws_server_thread, (void *)(long)i); break; } } @@ -150,7 +150,7 @@ static void websocket_ready_handler(struct mg_connection *conn) // websocket_close_handler() // When websocket is closed, tell the associated server to shut down -static void websocket_close_handler(struct mg_connection *conn) +static void websocket_close_handler(struct httplib_connection *conn) { int i; @@ -168,8 +168,7 @@ static void websocket_close_handler(struct mg_connection *conn) // flags: first byte of websocket frame, see websocket RFC, // http://tools.ietf.org/html/rfc6455, section 5.2 // data, data_len: payload data. Mask, if any, is already applied. -static int websocket_data_handler(struct mg_connection *conn, int flags, - char *data, size_t data_len) +static int websocket_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len) { int i; int wsd; @@ -198,12 +197,12 @@ static int websocket_data_handler(struct mg_connection *conn, int flags, /* turn on updates */ ws_conn[wsd].update = 1; /* echo back */ - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); } else if (strncmp("update off", data, data_len)== 0) { /* turn off updates */ ws_conn[wsd].update = 0; /* echo back */ - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, data, data_len); } break; case WEBSOCKET_OPCODE_BINARY: @@ -213,14 +212,14 @@ static int websocket_data_handler(struct mg_connection *conn, int flags, fprintf(stderr, "CLOSE...\n"); /* If client initiated close, respond with close message in acknowlegement */ if (!ws_conn[wsd].closing) { - mg_websocket_write(conn, WEBSOCKET_OPCODE_CONNECTION_CLOSE, data, data_len); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_CONNECTION_CLOSE, data, data_len); ws_conn[wsd].closing = 1; /* we should not send addional messages when close requested/acknowledged */ } return 0; /* time to close the connection */ break; case WEBSOCKET_OPCODE_PING: /* client sent PING, respond with PONG */ - mg_websocket_write(conn, WEBSOCKET_OPCODE_PONG, data, data_len); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_PONG, data, data_len); break; case WEBSOCKET_OPCODE_PONG: /* received PONG to our PING, no action */ @@ -238,8 +237,8 @@ static int websocket_data_handler(struct mg_connection *conn, int flags, int main(void) { char server_name[40]; - struct mg_context *ctx; - struct mg_callbacks callbacks; + struct httplib_context *ctx; + struct httplib_callbacks callbacks; const char *options[] = { "listening_ports", "8080", "document_root", "docroot", @@ -247,9 +246,7 @@ int main(void) }; /* get simple greeting for the web server */ - snprintf(server_name, sizeof(server_name), - "Civetweb websocket server v. %s", - mg_version()); + snprintf(server_name, sizeof(server_name), "Civetweb websocket server v. %s", httplib_version()); memset(&callbacks, 0, sizeof(callbacks)); callbacks.websocket_connect = websocket_connect_handler; @@ -257,15 +254,15 @@ int main(void) callbacks.websocket_data = websocket_data_handler; callbacks.connection_close = websocket_close_handler; - ctx = mg_start(&callbacks, NULL, options); + ctx = httplib_start(&callbacks, NULL, options); /* show the greeting and some basic information */ printf("%s started on port(s) %s with web root [%s]\n", - server_name, mg_get_option(ctx, "listening_ports"), - mg_get_option(ctx, "document_root")); + server_name, httplib_get_option(ctx, "listening_ports"), + httplib_get_option(ctx, "document_root")); getchar(); // Wait until user hits "enter" - mg_stop(ctx); + httplib_stop(ctx); return 0; } diff --git a/src/LibHTTPServer.cpp b/src/LibHTTPServer.cpp index d7d38a5e..283a4995 100644 --- a/src/LibHTTPServer.cpp +++ b/src/LibHTTPServer.cpp @@ -16,7 +16,7 @@ #endif bool -CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn) +CivetHandler::handleGet(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -24,7 +24,7 @@ CivetHandler::handleGet(CivetServer *server, struct mg_connection *conn) } bool -CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn) +CivetHandler::handlePost(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -32,7 +32,7 @@ CivetHandler::handlePost(CivetServer *server, struct mg_connection *conn) } bool -CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn) +CivetHandler::handleHead(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -40,7 +40,7 @@ CivetHandler::handleHead(CivetServer *server, struct mg_connection *conn) } bool -CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn) +CivetHandler::handlePut(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -48,7 +48,7 @@ CivetHandler::handlePut(CivetServer *server, struct mg_connection *conn) } bool -CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn) +CivetHandler::handlePatch(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -56,7 +56,7 @@ CivetHandler::handlePatch(CivetServer *server, struct mg_connection *conn) } bool -CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn) +CivetHandler::handleDelete(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -64,7 +64,7 @@ CivetHandler::handleDelete(CivetServer *server, struct mg_connection *conn) } bool -CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn) +CivetHandler::handleOptions(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -72,8 +72,7 @@ CivetHandler::handleOptions(CivetServer *server, struct mg_connection *conn) } bool -CivetWebSocketHandler::handleConnection(CivetServer *server, - const struct mg_connection *conn) +CivetWebSocketHandler::handleConnection(CivetServer *server, const struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -81,8 +80,7 @@ CivetWebSocketHandler::handleConnection(CivetServer *server, } void -CivetWebSocketHandler::handleReadyState(CivetServer *server, - struct mg_connection *conn) +CivetWebSocketHandler::handleReadyState(CivetServer *server, struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -90,11 +88,7 @@ CivetWebSocketHandler::handleReadyState(CivetServer *server, } bool -CivetWebSocketHandler::handleData(CivetServer *server, - struct mg_connection *conn, - int bits, - char *data, - size_t data_len) +CivetWebSocketHandler::handleData(CivetServer *server, struct httplib_connection *conn, int bits, char *data, size_t data_len) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -105,8 +99,7 @@ CivetWebSocketHandler::handleData(CivetServer *server, } void -CivetWebSocketHandler::handleClose(CivetServer *server, - const struct mg_connection *conn) +CivetWebSocketHandler::handleClose(CivetServer *server, const struct httplib_connection *conn) { UNUSED_PARAMETER(server); UNUSED_PARAMETER(conn); @@ -114,9 +107,9 @@ CivetWebSocketHandler::handleClose(CivetServer *server, } int -CivetServer::requestHandler(struct mg_connection *conn, void *cbdata) +CivetServer::requestHandler(struct httplib_connection *conn, void *cbdata) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); assert(request_info != NULL); CivetServer *me = (CivetServer *)(request_info->user_data); assert(me != NULL); @@ -125,9 +118,9 @@ CivetServer::requestHandler(struct mg_connection *conn, void *cbdata) if (me->context == NULL) return 0; - mg_lock_context(me->context); + httplib_lock_context(me->context); me->connections[conn] = CivetConnection(); - mg_unlock_context(me->context); + httplib_unlock_context(me->context); CivetHandler *handler = (CivetHandler *)cbdata; @@ -153,9 +146,9 @@ CivetServer::requestHandler(struct mg_connection *conn, void *cbdata) } int -CivetServer::authHandler(struct mg_connection *conn, void *cbdata) +CivetServer::authHandler(struct httplib_connection *conn, void *cbdata) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); assert(request_info != NULL); CivetServer *me = (CivetServer *)(request_info->user_data); assert(me != NULL); @@ -164,9 +157,9 @@ CivetServer::authHandler(struct mg_connection *conn, void *cbdata) if (me->context == NULL) return 0; - mg_lock_context(me->context); + httplib_lock_context(me->context); me->connections[conn] = CivetConnection(); - mg_unlock_context(me->context); + httplib_unlock_context(me->context); CivetAuthHandler *handler = (CivetAuthHandler *)cbdata; @@ -178,10 +171,9 @@ CivetServer::authHandler(struct mg_connection *conn, void *cbdata) } int -CivetServer::webSocketConnectionHandler(const struct mg_connection *conn, - void *cbdata) +CivetServer::webSocketConnectionHandler(const struct httplib_connection *conn, void *cbdata) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); assert(request_info != NULL); CivetServer *me = (CivetServer *)(request_info->user_data); assert(me != NULL); @@ -200,9 +192,9 @@ CivetServer::webSocketConnectionHandler(const struct mg_connection *conn, } void -CivetServer::webSocketReadyHandler(struct mg_connection *conn, void *cbdata) +CivetServer::webSocketReadyHandler(struct httplib_connection *conn, void *cbdata) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); assert(request_info != NULL); CivetServer *me = (CivetServer *)(request_info->user_data); assert(me != NULL); @@ -219,13 +211,9 @@ CivetServer::webSocketReadyHandler(struct mg_connection *conn, void *cbdata) } int -CivetServer::webSocketDataHandler(struct mg_connection *conn, - int bits, - char *data, - size_t data_len, - void *cbdata) +CivetServer::webSocketDataHandler(struct httplib_connection *conn, int bits, char *data, size_t data_len, void *cbdata) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); assert(request_info != NULL); CivetServer *me = (CivetServer *)(request_info->user_data); assert(me != NULL); @@ -244,10 +232,9 @@ CivetServer::webSocketDataHandler(struct mg_connection *conn, } void -CivetServer::webSocketCloseHandler(const struct mg_connection *conn, - void *cbdata) +CivetServer::webSocketCloseHandler(const struct httplib_connection *conn, void *cbdata) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); assert(request_info != NULL); CivetServer *me = (CivetServer *)(request_info->user_data); assert(me != NULL); @@ -281,7 +268,7 @@ CivetServer::CivetServer(const char **options, userCloseHandler = NULL; } callbacks.connection_close = closeHandler; - context = mg_start(&callbacks, this, options); + context = httplib_start(&callbacks, this, options); if (context == NULL) throw CivetException("null context when constructing CivetServer. " "Possible problem binding to port."); @@ -307,7 +294,7 @@ CivetServer::CivetServer(std::vector options, } pointers.push_back(0); - context = mg_start(&callbacks, this, &pointers[0]); + context = httplib_start(&callbacks, this, &pointers[0]); if (context == NULL) throw CivetException("null context when constructing CivetServer. " "Possible problem binding to port."); @@ -319,9 +306,9 @@ CivetServer::~CivetServer() } void -CivetServer::closeHandler(const struct mg_connection *conn) +CivetServer::closeHandler(const struct httplib_connection *conn) { - const struct mg_request_info *request_info = mg_get_request_info(conn); + const struct httplib_request_info *request_info = httplib_get_request_info(conn); assert(request_info != NULL); CivetServer *me = (CivetServer *)(request_info->user_data); assert(me != NULL); @@ -332,22 +319,22 @@ CivetServer::closeHandler(const struct mg_connection *conn) if (me->userCloseHandler) me->userCloseHandler(conn); - mg_lock_context(me->context); - me->connections.erase(const_cast(conn)); - mg_unlock_context(me->context); + httplib_lock_context(me->context); + me->connections.erase(const_cast(conn)); + httplib_unlock_context(me->context); } void CivetServer::addHandler(const std::string &uri, CivetHandler *handler) { - mg_set_request_handler(context, uri.c_str(), requestHandler, handler); + httplib_set_request_handler(context, uri.c_str(), requestHandler, handler); } void CivetServer::addWebSocketHandler(const std::string &uri, CivetWebSocketHandler *handler) { - mg_set_websocket_handler(context, + httplib_set_websocket_handler(context, uri.c_str(), webSocketConnectionHandler, webSocketReadyHandler, @@ -359,60 +346,55 @@ CivetServer::addWebSocketHandler(const std::string &uri, void CivetServer::addAuthHandler(const std::string &uri, CivetAuthHandler *handler) { - mg_set_auth_handler(context, uri.c_str(), authHandler, handler); + httplib_set_auth_handler(context, uri.c_str(), authHandler, handler); } void CivetServer::removeHandler(const std::string &uri) { - mg_set_request_handler(context, uri.c_str(), NULL, NULL); + httplib_set_request_handler(context, uri.c_str(), NULL, NULL); } void CivetServer::removeWebSocketHandler(const std::string &uri) { - mg_set_websocket_handler( - context, uri.c_str(), NULL, NULL, NULL, NULL, NULL); + httplib_set_websocket_handler( context, uri.c_str(), NULL, NULL, NULL, NULL, NULL); } void CivetServer::removeAuthHandler(const std::string &uri) { - mg_set_auth_handler(context, uri.c_str(), NULL, NULL); + httplib_set_auth_handler(context, uri.c_str(), NULL, NULL); } void CivetServer::close() { if (context) { - mg_stop(context); + httplib_stop(context); context = 0; } } int -CivetServer::getCookie(struct mg_connection *conn, +CivetServer::getCookie(struct httplib_connection *conn, const std::string &cookieName, std::string &cookieValue) { // Maximum cookie length as per microsoft is 4096. // http://msdn.microsoft.com/en-us/library/ms178194.aspx char _cookieValue[4096]; - const char *cookie = mg_get_header(conn, "Cookie"); - int lRead = mg_get_cookie(cookie, - cookieName.c_str(), - _cookieValue, - sizeof(_cookieValue)); + const char *cookie = httplib_get_header(conn, "Cookie"); + int lRead = httplib_get_cookie(cookie, cookieName.c_str(), _cookieValue, sizeof(_cookieValue)); cookieValue.clear(); cookieValue.append(_cookieValue); return lRead; } const char * -CivetServer::getHeader(struct mg_connection *conn, - const std::string &headerName) -{ - return mg_get_header(conn, headerName.c_str()); +CivetServer::getHeader(struct httplib_connection *conn, const std::string &headerName) { + + return httplib_get_header(conn, headerName.c_str()); } void @@ -450,25 +432,22 @@ CivetServer::urlDecode(const char *src, } bool -CivetServer::getParam(struct mg_connection *conn, - const char *name, - std::string &dst, - size_t occurrence) +CivetServer::getParam(struct httplib_connection *conn, const char *name, std::string &dst, size_t occurrence) { const char *formParams = NULL; - const struct mg_request_info *ri = mg_get_request_info(conn); + const struct httplib_request_info *ri = httplib_get_request_info(conn); assert(ri != NULL); CivetServer *me = (CivetServer *)(ri->user_data); assert(me != NULL); - mg_lock_context(me->context); + httplib_lock_context(me->context); CivetConnection &conobj = me->connections[conn]; - mg_lock_connection(conn); - mg_unlock_context(me->context); + httplib_lock_connection(conn); + httplib_unlock_context(me->context); if (conobj.postData != NULL) { formParams = conobj.postData; } else { - const char *con_len_str = mg_get_header(conn, "Content-Length"); + const char *con_len_str = httplib_get_header(conn, "Content-Length"); if (con_len_str) { unsigned long con_len = atoi(con_len_str); if (con_len > 0) { @@ -479,7 +458,7 @@ CivetServer::getParam(struct mg_connection *conn, conobj.postData = (char *)malloc(con_len + 1); if (conobj.postData != NULL) { // malloc may fail for huge requests - mg_read(conn, conobj.postData, con_len); + httplib_read(conn, conobj.postData, con_len); conobj.postData[con_len] = 0; formParams = conobj.postData; conobj.postDataLen = con_len; @@ -492,7 +471,7 @@ CivetServer::getParam(struct mg_connection *conn, // query_string formParams = ri->query_string; } - mg_unlock_connection(conn); + httplib_unlock_connection(conn); if (formParams != NULL) { return getParam(formParams, strlen(formParams), name, dst, occurrence); @@ -521,7 +500,7 @@ CivetServer::getParam(const char *data, // data is "var1=val1&var2=val2...". Find variable first for (p = data; p + name_len < e; p++) { if ((p == data || p[-1] == '&') && p[name_len] == '=' - && !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) { + && !httplib_strncasecmp(name, p, name_len) && 0 == occurrence--) { // Point p to variable value p += name_len + 1; @@ -576,7 +555,7 @@ CivetServer::getListeningPorts() { std::vector ports(10); std::vector ssl(10); - size_t size = mg_get_ports(context, ports.size(), &ports[0], &ssl[0]); + size_t size = httplib_get_ports(context, ports.size(), &ports[0], &ssl[0]); ports.resize(size); ssl.resize(size); return ports; diff --git a/src/httplib_accept_new_connection.c b/src/httplib_accept_new_connection.c index 3ea0a38e..471d78ab 100644 --- a/src/httplib_accept_new_connection.c +++ b/src/httplib_accept_new_connection.c @@ -26,13 +26,13 @@ #include "httplib_ssl.h" /* - * void XX_httplib_accept_new_connection( const struct socket *lostener, struct mg_context *ctx ); + * void XX_httplib_accept_new_connection( const struct socket *lostener, struct httplib_context *ctx ); * * The function XX_httplib_accept_new_connection() is used to process new * incoming connections to the server. */ -void XX_httplib_accept_new_connection( const struct socket *listener, struct mg_context *ctx ) { +void XX_httplib_accept_new_connection( const struct socket *listener, struct httplib_context *ctx ) { struct socket so; char src_addr[IP_ADDR_STR_LEN]; @@ -46,7 +46,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct mg_ == INVALID_SOCKET) { } else if (!XX_httplib_check_acl(ctx, ntohl(*(uint32_t *)&so.rsa.sin.sin_addr))) { XX_httplib_sockaddr_to_string(src_addr, sizeof(src_addr), &so.rsa); - mg_cry( XX_httplib_fc(ctx), "%s: %s is not allowed to connect", __func__, src_addr); + httplib_cry( XX_httplib_fc(ctx), "%s: %s is not allowed to connect", __func__, src_addr); closesocket(so.sock); so.sock = INVALID_SOCKET; } else { @@ -55,7 +55,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct mg_ so.is_ssl = listener->is_ssl; so.ssl_redir = listener->ssl_redir; if (getsockname(so.sock, &so.lsa.sa, &len) != 0) { - mg_cry( XX_httplib_fc(ctx), "%s: getsockname() failed: %s", __func__, strerror(ERRNO)); + httplib_cry( XX_httplib_fc(ctx), "%s: getsockname() failed: %s", __func__, strerror(ERRNO)); } /* Set TCP keep-alive. This is needed because if HTTP-level @@ -67,7 +67,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct mg_ * Thanks to Igor Klopov who suggested the patch. */ if (setsockopt(so.sock, SOL_SOCKET, SO_KEEPALIVE, (SOCK_OPT_TYPE)&on, sizeof(on)) != 0) { - mg_cry( XX_httplib_fc(ctx), "%s: setsockopt(SOL_SOCKET SO_KEEPALIVE) failed: %s", __func__, strerror(ERRNO)); + httplib_cry( XX_httplib_fc(ctx), "%s: setsockopt(SOL_SOCKET SO_KEEPALIVE) failed: %s", __func__, strerror(ERRNO)); } /* Disable TCP Nagle's algorithm. Normally TCP packets are coalesced @@ -80,7 +80,7 @@ void XX_httplib_accept_new_connection( const struct socket *listener, struct mg_ if ((ctx != NULL) && (ctx->config[CONFIG_TCP_NODELAY] != NULL) && (!strcmp(ctx->config[CONFIG_TCP_NODELAY], "1"))) { if (XX_httplib_set_tcp_nodelay(so.sock, 1) != 0) { - mg_cry( XX_httplib_fc(ctx), "%s: setsockopt(IPPROTO_TCP TCP_NODELAY) failed: %s", __func__, strerror(ERRNO)); + httplib_cry( XX_httplib_fc(ctx), "%s: setsockopt(IPPROTO_TCP TCP_NODELAY) failed: %s", __func__, strerror(ERRNO)); } } diff --git a/src/httplib_addenv.c b/src/httplib_addenv.c index ce4ce4ef..c8e6029e 100644 --- a/src/httplib_addenv.c +++ b/src/httplib_addenv.c @@ -59,7 +59,7 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) { added = (char *)XX_httplib_realloc(env->buf, n); if (!added) { /* Out of memory */ - mg_cry(env->conn, "%s: Cannot allocate memory for CGI variable [%s]", __func__, fmt); + httplib_cry(env->conn, "%s: Cannot allocate memory for CGI variable [%s]", __func__, fmt); return; } env->buf = added; @@ -90,7 +90,7 @@ void XX_httplib_addenv( struct cgi_environment *env, const char *fmt, ... ) { /* Now update the variable index */ space = (env->varlen - env->varused); if (space < 2) { - mg_cry(env->conn, "%s: Cannot register CGI variable [%s]", __func__, fmt); + httplib_cry(env->conn, "%s: Cannot register CGI variable [%s]", __func__, fmt); return; } diff --git a/src/httplib_authorize.c b/src/httplib_authorize.c index f8070483..49986f48 100644 --- a/src/httplib_authorize.c +++ b/src/httplib_authorize.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* Authorize against the opened passwords file. Return 1 if authorized. */ -int XX_httplib_authorize( struct mg_connection *conn, struct file *filep ) { +int XX_httplib_authorize( struct httplib_connection *conn, struct file *filep ) { struct read_auth_file_struct workdata; char buf[MG_BUF_LEN]; diff --git a/src/httplib_check_acl.c b/src/httplib_check_acl.c index eaab6487..2bb553b8 100644 --- a/src/httplib_check_acl.c +++ b/src/httplib_check_acl.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* - * int XX_httplib_check_acl( struct mg_context *ctx, uint32_t remote_ip ); + * int XX_httplib_check_acl( struct httplib_context *ctx, uint32_t remote_ip ); * * The function XX_httplib_check_acl() is used to check of the socket address * of a connection is allowed according to the access control list. The @@ -33,7 +33,7 @@ * allowed and 1 if the address is allowed. */ -int XX_httplib_check_acl( struct mg_context *ctx, uint32_t remote_ip ) { +int XX_httplib_check_acl( struct httplib_context *ctx, uint32_t remote_ip ) { int allowed; int flag; @@ -50,7 +50,7 @@ int XX_httplib_check_acl( struct mg_context *ctx, uint32_t remote_ip ) { while ((list = XX_httplib_next_option(list, &vec, NULL)) != NULL) { flag = vec.ptr[0]; if ((flag != '+' && flag != '-') || XX_httplib_parse_net(&vec.ptr[1], &net, &mask) == 0) { - mg_cry( XX_httplib_fc(ctx), "%s: subnet must be [+|-]x.x.x.x[/x]", __func__); + httplib_cry( XX_httplib_fc(ctx), "%s: subnet must be [+|-]x.x.x.x[/x]", __func__); return -1; } diff --git a/src/httplib_check_authorization.c b/src/httplib_check_authorization.c index 0bf624ab..0450f8bc 100644 --- a/src/httplib_check_authorization.c +++ b/src/httplib_check_authorization.c @@ -26,7 +26,7 @@ #include "httplib_string.h" /* Return 1 if request is authorised, 0 otherwise. */ -int XX_httplib_check_authorization( struct mg_connection *conn, const char *path ) { +int XX_httplib_check_authorization( struct httplib_connection *conn, const char *path ) { char fname[PATH_MAX]; struct vec uri_vec; @@ -44,7 +44,7 @@ int XX_httplib_check_authorization( struct mg_connection *conn, const char *path XX_httplib_snprintf(conn, &truncated, fname, sizeof(fname), "%.*s", (int)filename_vec.len, filename_vec.ptr); if (truncated || !XX_httplib_fopen(conn, fname, "r", &file)) { - mg_cry(conn, "%s: cannot open %s: %s", __func__, fname, strerror(errno)); + httplib_cry(conn, "%s: cannot open %s: %s", __func__, fname, strerror(errno)); } break; } diff --git a/src/httplib_check_feature.c b/src/httplib_check_feature.c index f5c6bf4e..e0ddc083 100644 --- a/src/httplib_check_feature.c +++ b/src/httplib_check_feature.c @@ -25,13 +25,13 @@ #include "httplib_main.h" /* - * unsigned mg_check_feature( unsigned feature ); + * unsigned httplib_check_feature( unsigned feature ); * - * The function mg_check_feature returns an integer indicating if a specific + * The function httplib_check_feature returns an integer indicating if a specific * functionality has been compiled in at compile time. */ -unsigned mg_check_feature( unsigned feature ) { +unsigned httplib_check_feature( unsigned feature ) { static const unsigned feature_set = 0 /* Set bits for available features according to API documentation. @@ -71,4 +71,4 @@ unsigned mg_check_feature( unsigned feature ) { ; return (feature & feature_set); -} /* mg_check_feature */ +} /* httplib_check_feature */ diff --git a/src/httplib_check_password.c b/src/httplib_check_password.c index 7b4737c6..cddd9974 100644 --- a/src/httplib_check_password.c +++ b/src/httplib_check_password.c @@ -36,9 +36,9 @@ int XX_httplib_check_password( const char *method, const char *ha1, const char * /* NOTE(lsm): due to a bug in MSIE, we do not compare the URI */ if (strlen(response) != 32) return 0; - mg_md5(ha2, method, ":", uri, NULL); - mg_md5(expected_response, ha1, ":", nonce, ":", nc, ":", cnonce, ":", qop, ":", ha2, NULL); + httplib_md5(ha2, method, ":", uri, NULL); + httplib_md5(expected_response, ha1, ":", nonce, ":", nc, ":", cnonce, ":", qop, ":", ha2, NULL); - return mg_strcasecmp(response, expected_response) == 0; + return httplib_strcasecmp(response, expected_response) == 0; } /* XX_httplib_check_password */ diff --git a/src/httplib_close_all_listening_sockets.c b/src/httplib_close_all_listening_sockets.c index c99b3cf8..27820c4d 100644 --- a/src/httplib_close_all_listening_sockets.c +++ b/src/httplib_close_all_listening_sockets.c @@ -26,13 +26,13 @@ #include "httplib_memory.h" /* - * void XX_httplib_close_all_listening_sockets( struct mg_context *ctx ); + * void XX_httplib_close_all_listening_sockets( struct httplib_context *ctx ); * * The function XX_httplib_close_all_listening_sockets() closes all listening * sockets of a given context. */ -void XX_httplib_close_all_listening_sockets( struct mg_context *ctx ) { +void XX_httplib_close_all_listening_sockets( struct httplib_context *ctx ) { unsigned int i; diff --git a/src/httplib_close_connection.c b/src/httplib_close_connection.c index 6724c638..d5269a77 100644 --- a/src/httplib_close_connection.c +++ b/src/httplib_close_connection.c @@ -28,13 +28,13 @@ #include "httplib_ssl.h" /* - * void XX_httplib_close_connection( struct mg_connection *conn ); + * void XX_httplib_close_connection( struct httplib_connection *conn ); * * The function XX_httplib_close_connection() is the internal function which * does the heavy lifting to close a connection. */ -void XX_httplib_close_connection( struct mg_connection *conn ) { +void XX_httplib_close_connection( struct httplib_connection *conn ) { if ( conn == NULL || conn->ctx == NULL ) return; @@ -43,7 +43,7 @@ void XX_httplib_close_connection( struct mg_connection *conn ) { conn->ctx->callbacks.connection_close(conn); } - mg_lock_connection( conn ); + httplib_lock_connection( conn ); conn->must_close = 1; @@ -65,23 +65,23 @@ void XX_httplib_close_connection( struct mg_connection *conn ) { conn->client.sock = INVALID_SOCKET; } - mg_unlock_connection( conn ); + httplib_unlock_connection( conn ); } /* XX_httplib_close_connection */ /* - * void mg_close_connection( struct mg_connection *conn ); + * void httplib_close_connection( struct httplib_connection *conn ); * - * The function mg_close_connection() closes the connection passed as a + * The function httplib_close_connection() closes the connection passed as a * parameter to this function. The function does not return a success or * failure value. */ -void mg_close_connection( struct mg_connection *conn ) { +void httplib_close_connection( struct httplib_connection *conn ) { - struct mg_context *client_ctx = NULL; + struct httplib_context *client_ctx = NULL; unsigned int i; if ( conn == NULL ) return; @@ -109,4 +109,4 @@ void mg_close_connection( struct mg_connection *conn ) { XX_httplib_free(conn); } -} /* mg_close_connection */ +} /* httplib_close_connection */ diff --git a/src/httplib_close_socket_gracefully.c b/src/httplib_close_socket_gracefully.c index 89958fc4..8dfd4670 100644 --- a/src/httplib_close_socket_gracefully.c +++ b/src/httplib_close_socket_gracefully.c @@ -25,13 +25,13 @@ #include "httplib_main.h" /* - * void XX_httplib_close_socket_gracefully( struct mg_connection *conn ); + * void XX_httplib_close_socket_gracefully( struct httplib_connection *conn ); * * The function XX_httplib_close_socket_gracefully() closes a socket in a * graceful way. */ -void XX_httplib_close_socket_gracefully( struct mg_connection *conn ) { +void XX_httplib_close_socket_gracefully( struct httplib_connection *conn ) { #if defined(_WIN32) char buf[MG_BUF_LEN]; @@ -54,7 +54,7 @@ void XX_httplib_close_socket_gracefully( struct mg_connection *conn ) { /* Socket already closed by client/peer, close socket without linger */ } else { if (setsockopt(conn->client.sock, SOL_SOCKET, SO_LINGER, (char *)&linger, sizeof(linger)) != 0) { - mg_cry(conn, "%s: setsockopt(SOL_SOCKET SO_LINGER) failed: %s", __func__, strerror(ERRNO)); + httplib_cry(conn, "%s: setsockopt(SOL_SOCKET SO_LINGER) failed: %s", __func__, strerror(ERRNO)); } } diff --git a/src/httplib_config_options.c b/src/httplib_config_options.c index 777019c0..04091416 100644 --- a/src/httplib_config_options.c +++ b/src/httplib_config_options.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* Config option name, config types, default value */ -struct mg_option XX_httplib_config_options[] = { +struct httplib_option XX_httplib_config_options[] = { { "cgi_pattern", CONFIG_TYPE_EXT_PATTERN, "**.cgi$|**.pl$|**.php$" }, { "cgi_environment", CONFIG_TYPE_STRING, NULL }, { "put_delete_auth_file", CONFIG_TYPE_FILE, NULL }, @@ -81,4 +81,4 @@ struct mg_option XX_httplib_config_options[] = { * Check if the XX_httplib_config_options and the corresponding enum have * compatible sizes */ -mg_static_assert((sizeof(XX_httplib_config_options) / sizeof(XX_httplib_config_options[0])) == (NUM_OPTIONS + 1), "XX_httplib_config_options and enum not sync"); +httplib_static_assert((sizeof(XX_httplib_config_options) / sizeof(XX_httplib_config_options[0])) == (NUM_OPTIONS + 1), "XX_httplib_config_options and enum not sync"); diff --git a/src/httplib_connect_client.c b/src/httplib_connect_client.c index f0b23e91..85a9a57c 100644 --- a/src/httplib_connect_client.c +++ b/src/httplib_connect_client.c @@ -28,65 +28,64 @@ #include "httplib_ssl.h" #include "httplib_string.h" -static struct mg_connection * mg_connect_client_impl( const struct mg_client_options *client_options, int use_ssl, char *ebuf, size_t ebuf_len ); +static struct httplib_connection * httplib_connect_client_impl( const struct httplib_client_options *client_options, int use_ssl, char *ebuf, size_t ebuf_len ); /* - * struct mg_connection *mg_connect_client_secure( const struct mg_client_options *client options, char *error buffer, size_t error_buffer_size ); + * struct httplib_connection *httplib_connect_client_secure( const struct httplib_client_options *client options, char *error buffer, size_t error_buffer_size ); * - * The function mg_connect_client_secure() creates a secure connection as a + * The function httplib_connect_client_secure() creates a secure connection as a * client to a remote server and returns a pointer to the connection * information, or NULL if an error occured. */ -CIVETWEB_API struct mg_connection *mg_connect_client_secure( const struct mg_client_options *client_options, char *error_buffer, size_t error_buffer_size ) { +CIVETWEB_API struct httplib_connection *httplib_connect_client_secure( const struct httplib_client_options *client_options, char *error_buffer, size_t error_buffer_size ) { - return mg_connect_client_impl( client_options, 1, error_buffer, error_buffer_size ); + return httplib_connect_client_impl( client_options, 1, error_buffer, error_buffer_size ); -} /* mg_connect_client_secure */ +} /* httplib_connect_client_secure */ /* - * struct mg_connection *mg_connect_client( const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size ); + * struct httplib_connection *httplib_connect_client( const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size ); * - * The function mg_connect_client() connects to a remote server as a client + * The function httplib_connect_client() connects to a remote server as a client * with the options of the connection provided as parameters. */ -struct mg_connection * mg_connect_client( const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size ) { +struct httplib_connection * httplib_connect_client( const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size ) { - struct mg_client_options opts; + struct httplib_client_options opts; memset( &opts, 0, sizeof(opts) ); opts.host = host; opts.port = port; - return mg_connect_client_impl( &opts, use_ssl, error_buffer, error_buffer_size ); + return httplib_connect_client_impl( &opts, use_ssl, error_buffer, error_buffer_size ); -} /* mg_connect_client */ +} /* httplib_connect_client */ /* - * static struct mg_connection *mg_connect_client_impl( const struct mg_client_options *client_options, int use_ssl, char *ebuf, size_t ebuf_len ); + * static struct httplib_connection *httplib_connect_client_impl( const struct httplib_client_options *client_options, int use_ssl, char *ebuf, size_t ebuf_len ); * - * The function mg_connect_client_impl() is the background function doing the + * The function httplib_connect_client_impl() is the background function doing the * heavy lifting to make connections as a client to remote servers. */ -static struct mg_connection *mg_connect_client_impl( const struct mg_client_options *client_options, int use_ssl, char *ebuf, size_t ebuf_len ) { +static struct httplib_connection *httplib_connect_client_impl( const struct httplib_client_options *client_options, int use_ssl, char *ebuf, size_t ebuf_len ) { - static struct mg_context fake_ctx; - struct mg_connection *conn = NULL; + static struct httplib_context fake_ctx; + struct httplib_connection *conn = NULL; SOCKET sock; union usa sa; if (!XX_httplib_connect_socket(&fake_ctx, client_options->host, client_options->port, use_ssl, ebuf, ebuf_len, &sock, &sa)) { ; - } else if ((conn = (struct mg_connection *) - XX_httplib_calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE)) == NULL) { + } else if ((conn = (struct httplib_connection *) XX_httplib_calloc(1, sizeof(*conn) + MAX_REQUEST_SIZE)) == NULL) { XX_httplib_snprintf(NULL, NULL, ebuf, ebuf_len, "calloc(): %s", strerror(ERRNO)); closesocket(sock); #ifndef NO_SSL @@ -114,7 +113,7 @@ static struct mg_connection *mg_connect_client_impl( const struct mg_client_opti conn->client.sock = sock; conn->client.lsa = sa; - if (getsockname(sock, psa, &len) != 0) mg_cry(conn, "%s: getsockname() failed: %s", __func__, strerror(ERRNO)); + if (getsockname(sock, psa, &len) != 0) httplib_cry(conn, "%s: getsockname() failed: %s", __func__, strerror(ERRNO)); conn->client.is_ssl = use_ssl ? 1 : 0; pthread_mutex_init(&conn->mutex, &XX_httplib_pthread_mutex_attr); @@ -161,4 +160,4 @@ static struct mg_connection *mg_connect_client_impl( const struct mg_client_opti return conn; -} /* mg_connect_client_impl */ +} /* httplib_connect_client_impl */ diff --git a/src/httplib_connect_socket.c b/src/httplib_connect_socket.c index 9e3329d0..c2b03e15 100644 --- a/src/httplib_connect_socket.c +++ b/src/httplib_connect_socket.c @@ -36,7 +36,7 @@ * may not be null for this function to succeed. */ -int XX_httplib_connect_socket( struct mg_context *ctx, const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, SOCKET *sock, union usa *sa ) { +int XX_httplib_connect_socket( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, SOCKET *sock, union usa *sa ) { int ip_ver = 0; diff --git a/src/httplib_connect_websocket_client.c b/src/httplib_connect_websocket_client.c index 7e45ab21..5cfc288c 100644 --- a/src/httplib_connect_websocket_client.c +++ b/src/httplib_connect_websocket_client.c @@ -27,28 +27,28 @@ #include "httplib_string.h" /* - * struct mg_connection *mg_connect_websocket_client(); + * struct httplib_connection *httplib_connect_websocket_client(); * - * The function mg_connect_websocket_client() connects as a client to a + * The function httplib_connect_websocket_client() connects as a client to a * websocket on another server. If this succeeds a connection pointer is * returned, otherwise NULL. */ -struct mg_connection *mg_connect_websocket_client(const char *host, +struct httplib_connection *httplib_connect_websocket_client(const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size, const char *path, const char *origin, - mg_websocket_data_handler data_func, - mg_websocket_close_handler close_func, + httplib_websocket_data_handler data_func, + httplib_websocket_close_handler close_func, void *user_data) { - struct mg_connection *conn = NULL; + struct httplib_connection *conn = NULL; #if defined(USE_WEBSOCKET) - struct mg_context *newctx = NULL; + struct httplib_context *newctx = NULL; struct websocket_client_thread_data *thread_data; static const char *magic = "x3JJHMbDL1EzLkh9GBhXDw=="; static const char *handshake_req; @@ -73,7 +73,7 @@ struct mg_connection *mg_connect_websocket_client(const char *host, } /* Establish the client connection and request upgrade */ - conn = mg_download(host, port, use_ssl, error_buffer, error_buffer_size, handshake_req, path, host, magic, origin); + conn = httplib_download(host, port, use_ssl, error_buffer, error_buffer_size, handshake_req, path, host, magic, origin); /* Connection object will be null if something goes wrong */ if (conn == NULL || (strcmp(conn->request_info.request_uri, "101") != 0)) { @@ -89,10 +89,10 @@ struct mg_connection *mg_connect_websocket_client(const char *host, return conn; } - /* For client connections, mg_context is fake. Since we need to set a + /* For client connections, httplib_context is fake. Since we need to set a * callback function, we need to create a copy and modify it. */ - newctx = (struct mg_context *)XX_httplib_malloc(sizeof(struct mg_context)); - memcpy(newctx, conn->ctx, sizeof(struct mg_context)); + newctx = (struct httplib_context *)XX_httplib_malloc(sizeof(struct httplib_context)); + memcpy(newctx, conn->ctx, sizeof(struct httplib_context)); newctx->user_data = user_data; newctx->context_type = 2; /* client context type */ newctx->cfg_worker_threads = 1; /* one worker thread will be created */ @@ -105,7 +105,7 @@ struct mg_connection *mg_connect_websocket_client(const char *host, thread_data->callback_data = NULL; /* Start a thread to read the websocket client connection - * This thread will automatically stop when mg_disconnect is + * This thread will automatically stop when httplib_disconnect is * called on the client connection */ if (XX_httplib_start_thread_with_id( XX_httplib_websocket_client_thread, (void *)thread_data, newctx->workerthreadids) != 0) { @@ -131,4 +131,4 @@ struct mg_connection *mg_connect_websocket_client(const char *host, return conn; -} /* mg_connect_websocket_client */ +} /* httplib_connect_websocket_client */ diff --git a/src/httplib_consume_socket.c b/src/httplib_consume_socket.c index d58131bd..5b8a5c3c 100644 --- a/src/httplib_consume_socket.c +++ b/src/httplib_consume_socket.c @@ -26,7 +26,7 @@ #include "httplib_pthread.h" /* - * int XX_httplib_consume_socket( struct mg_context *ctx, struct socket *sp, int thread_index ); + * int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, int thread_index ); * * The function XX_httplib_consume_socket() takes an accepted socket from the * queue for further processing. @@ -34,7 +34,7 @@ #if defined(ALTERNATIVE_QUEUE) -int XX_httplib_consume_socket( struct mg_context *ctx, struct socket *sp, int thread_index ) { +int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, int thread_index ) { ctx->client_socks[thread_index].in_use = 0; event_wait(ctx->client_wait_events[thread_index]); @@ -47,7 +47,7 @@ int XX_httplib_consume_socket( struct mg_context *ctx, struct socket *sp, int th #else /* ALTERNATIVE_QUEUE */ /* Worker threads take accepted socket from the queue */ -int XX_httplib_consume_socket( struct mg_context *ctx, struct socket *sp, int thread_index ) { +int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, int thread_index ) { #define QUEUE_SIZE(ctx) ((int)(ARRAY_SIZE(ctx->queue))) diff --git a/src/httplib_cry.c b/src/httplib_cry.c index a7795879..4b10d995 100644 --- a/src/httplib_cry.c +++ b/src/httplib_cry.c @@ -26,7 +26,7 @@ #include "httplib_ssl.h" /* Print error message to the opened error log stream. */ -void mg_cry(const struct mg_connection *conn, const char *fmt, ...) { +void httplib_cry(const struct httplib_connection *conn, const char *fmt, ...) { char buf[MG_BUF_LEN]; char src_addr[IP_ADDR_STR_LEN]; @@ -82,4 +82,4 @@ void mg_cry(const struct mg_connection *conn, const char *fmt, ...) { } } -} /* mg_cry */ +} /* httplib_cry */ diff --git a/src/httplib_delete_file.c b/src/httplib_delete_file.c index 1a07c684..311b6895 100644 --- a/src/httplib_delete_file.c +++ b/src/httplib_delete_file.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* - * void XX_httplib_delete_file( struct mg_connection *conn, const char *path ); + * void XX_httplib_delete_file( struct httplib_connection *conn, const char *path ); * * The function XX_httplib_delete_file() deletes a file after a request over a * connection. @@ -33,7 +33,7 @@ #if !defined(NO_FILES) -void XX_httplib_delete_file( struct mg_connection *conn, const char *path ) { +void XX_httplib_delete_file( struct httplib_connection *conn, const char *path ) { struct de de; diff --git a/src/httplib_discard_unread_request_data.c b/src/httplib_discard_unread_request_data.c index e777b8ed..e6627f15 100644 --- a/src/httplib_discard_unread_request_data.c +++ b/src/httplib_discard_unread_request_data.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -void XX_httplib_discard_unread_request_data( struct mg_connection *conn ) { +void XX_httplib_discard_unread_request_data( struct httplib_connection *conn ) { char buf[MG_BUF_LEN]; size_t to_read; @@ -40,10 +40,8 @@ void XX_httplib_discard_unread_request_data( struct mg_connection *conn ) { /* Chunked encoding: 1=chunk not read completely, 2=chunk read * completely */ while (conn->is_chunked == 1) { - nread = mg_read(conn, buf, to_read); - if (nread <= 0) { - break; - } + nread = httplib_read(conn, buf, to_read); + if (nread <= 0) break; } } else { @@ -54,7 +52,7 @@ void XX_httplib_discard_unread_request_data( struct mg_connection *conn ) { to_read = (size_t)(conn->content_len - conn->consumed_content); } - nread = mg_read(conn, buf, to_read); + nread = httplib_read(conn, buf, to_read); if (nread <= 0) break; } } diff --git a/src/httplib_download.c b/src/httplib_download.c index ddfb70c4..f0f0666c 100644 --- a/src/httplib_download.c +++ b/src/httplib_download.c @@ -26,15 +26,15 @@ #include "httplib_string.h" /* - * struct mg_connection *mg_download(); + * struct httplib_connection *httplib_download(); * - * The function mg_download() is used to download a file from a remote location + * The function httplib_download() is used to download a file from a remote location * and returns a pointer to the connection on success, or NULL on error. */ -struct mg_connection * mg_download( const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, const char *fmt, ... ) { +struct httplib_connection * httplib_download( const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, const char *fmt, ... ) { - struct mg_connection *conn; + struct httplib_connection *conn; va_list ap; int i; int reqerr; @@ -43,7 +43,7 @@ struct mg_connection * mg_download( const char *host, int port, int use_ssl, cha ebuf[0] = '\0'; /* open a connection */ - conn = mg_connect_client(host, port, use_ssl, ebuf, ebuf_len); + conn = httplib_connect_client(host, port, use_ssl, ebuf, ebuf_len); if (conn != NULL) { i = XX_httplib_vprintf(conn, fmt, ap); @@ -60,11 +60,11 @@ struct mg_connection * mg_download( const char *host, int port, int use_ssl, cha /* if an error occured, close the connection */ if (ebuf[0] != '\0' && conn != NULL) { - mg_close_connection(conn); + httplib_close_connection(conn); conn = NULL; } va_end(ap); return conn; -} /* mg_download */ +} /* httplib_download */ diff --git a/src/httplib_fc.c b/src/httplib_fc.c index f9d37408..c7bc62c7 100644 --- a/src/httplib_fc.c +++ b/src/httplib_fc.c @@ -26,9 +26,9 @@ /* Return fake connection structure. Used for logging, if connection * is not applicable at the moment of logging. */ -struct mg_connection *XX_httplib_fc( struct mg_context *ctx ) { +struct httplib_connection *XX_httplib_fc( struct httplib_context *ctx ) { - static struct mg_connection fake_connection; + static struct httplib_connection fake_connection; fake_connection.ctx = ctx; return &fake_connection; diff --git a/src/httplib_fclose_on_exec.c b/src/httplib_fclose_on_exec.c index 1be69608..2180dae6 100644 --- a/src/httplib_fclose_on_exec.c +++ b/src/httplib_fclose_on_exec.c @@ -24,14 +24,14 @@ #include "httplib_main.h" -void XX_httplib_fclose_on_exec( struct file *filep, struct mg_connection *conn ) { +void XX_httplib_fclose_on_exec( struct file *filep, struct httplib_connection *conn ) { if (filep != NULL && filep->fp != NULL) { #ifdef _WIN32 (void)conn; /* Unused. */ #else if (fcntl(fileno(filep->fp), F_SETFD, FD_CLOEXEC) != 0) { - mg_cry(conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO)); + httplib_cry(conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO)); } #endif } diff --git a/src/httplib_fopen.c b/src/httplib_fopen.c index 8a3fffda..07ddb662 100644 --- a/src/httplib_fopen.c +++ b/src/httplib_fopen.c @@ -29,7 +29,7 @@ * The input parameter mode is the same as for fopen. * Either fp or membuf will be set in the output struct filep. * The function returns 1 on success, 0 on error. */ -int XX_httplib_fopen( const struct mg_connection *conn, const char *path, const char *mode, struct file *filep ) { +int XX_httplib_fopen( const struct httplib_connection *conn, const char *path, const char *mode, struct file *filep ) { struct stat st; diff --git a/src/httplib_forward_body_data.c b/src/httplib_forward_body_data.c index 6073e054..55078e4b 100644 --- a/src/httplib_forward_body_data.c +++ b/src/httplib_forward_body_data.c @@ -25,14 +25,14 @@ #include "httplib_main.h" /* - * int XX_httplib_forward_body_data( struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl ); + * int XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp, SOCKET sock, SSL *ssl ); * * The function XX_httplib_forward_body_data() forwards body data to the * client. */ #if !defined(NO_CGI) || !defined(NO_FILES) -int XX_httplib_forward_body_data( struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl ) { +int XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp, SOCKET sock, SSL *ssl ) { const char *expect; const char *body; @@ -43,12 +43,12 @@ int XX_httplib_forward_body_data( struct mg_connection *conn, FILE *fp, SOCKET s int64_t buffered_len; double timeout = -1.0; - if (!conn) { return 0; } + if ( conn == NULL ) return 0; if (conn->ctx->config[REQUEST_TIMEOUT]) { timeout = atoi(conn->ctx->config[REQUEST_TIMEOUT]) / 1000.0; } - expect = mg_get_header(conn, "Expect"); + expect = httplib_get_header(conn, "Expect"); /* assert(fp != NULL); */ if (!fp) { XX_httplib_send_http_error(conn, 500, "%s", "Error: NULL File"); @@ -59,12 +59,12 @@ int XX_httplib_forward_body_data( struct mg_connection *conn, FILE *fp, SOCKET s /* Content length is not specified by the client. */ XX_httplib_send_http_error(conn, 411, "%s", "Error: Client did not specify content length"); } else if ((expect != NULL) - && (mg_strcasecmp(expect, "100-continue") != 0)) { + && (httplib_strcasecmp(expect, "100-continue") != 0)) { /* Client sent an "Expect: xyz" header and xyz is not 100-continue. */ XX_httplib_send_http_error(conn, 417, "Error: Can not fulfill expectation %s", expect); } else { if (expect != NULL) { - (void)mg_printf(conn, "%s", "HTTP/1.1 100 Continue\r\n\r\n"); + (void)httplib_printf(conn, "%s", "HTTP/1.1 100 Continue\r\n\r\n"); conn->status_code = 100; } else conn->status_code = 200; diff --git a/src/httplib_free_context.c b/src/httplib_free_context.c index b0341d76..e8cd3a50 100644 --- a/src/httplib_free_context.c +++ b/src/httplib_free_context.c @@ -29,16 +29,16 @@ #include "httplib_utils.h" /* - * void XX_httplib_free_context( struct mg_context *ctx ); + * void XX_httplib_free_context( struct httplib_context *ctx ); * * The function XX_httplib_free_context() is used to free the resources * associated with a context. */ -void XX_httplib_free_context( struct mg_context *ctx ) { +void XX_httplib_free_context( struct httplib_context *ctx ) { int i; - struct mg_handler_info *tmp_rh; + struct httplib_handler_info *tmp_rh; if ( ctx == NULL ) return; diff --git a/src/httplib_get_builtin_mime_type.c b/src/httplib_get_builtin_mime_type.c index 2e87e6c6..9f2e5380 100644 --- a/src/httplib_get_builtin_mime_type.c +++ b/src/httplib_get_builtin_mime_type.c @@ -570,14 +570,14 @@ const char *httplib_get_builtin_mime_type( const char *path ) { while ( eind-start > 1 ) { midden = (start+eind) >> 1; - retval = mg_strcasecmp( ext, builtin_mime_types[midden].extension ); + retval = httplib_strcasecmp( ext, builtin_mime_types[midden].extension ); if ( retval == 0 ) return builtin_mime_types[midden].mime_type; else if ( retval < 0 ) eind = midden; else start = midden; } - if ( ! mg_strcasecmp( ext, builtin_mime_types[start].extension ) ) return builtin_mime_types[start].mime_type; + if ( ! httplib_strcasecmp( ext, builtin_mime_types[start].extension ) ) return builtin_mime_types[start].mime_type; return "text/plain"; diff --git a/src/httplib_get_context.c b/src/httplib_get_context.c index d0b97f12..96b1cf3f 100644 --- a/src/httplib_get_context.c +++ b/src/httplib_get_context.c @@ -24,8 +24,8 @@ #include "httplib_main.h" -struct mg_context * mg_get_context(const struct mg_connection *conn) { +struct httplib_context * httplib_get_context(const struct httplib_connection *conn) { - return (conn == NULL) ? (struct mg_context *)NULL : (conn->ctx); + return (conn == NULL) ? (struct httplib_context *)NULL : (conn->ctx); -} /* mg_get_context */ +} /* httplib_get_context */ diff --git a/src/httplib_get_cookie.c b/src/httplib_get_cookie.c index 4db4b89a..7465cc63 100644 --- a/src/httplib_get_cookie.c +++ b/src/httplib_get_cookie.c @@ -26,7 +26,7 @@ #include "httplib_string.h" /* HCP24: some changes to compare hole var_name */ -int mg_get_cookie(const char *cookie_header, const char *var_name, char *dst, size_t dst_size) { +int httplib_get_cookie(const char *cookie_header, const char *var_name, char *dst, size_t dst_size) { const char *s; const char *p; @@ -64,4 +64,4 @@ int mg_get_cookie(const char *cookie_header, const char *var_name, char *dst, si } return len; -} /* mg_get_cookie */ +} /* httplib_get_cookie */ diff --git a/src/httplib_get_first_ssl_listener_index.c b/src/httplib_get_first_ssl_listener_index.c index 3465af09..a9efe16c 100644 --- a/src/httplib_get_first_ssl_listener_index.c +++ b/src/httplib_get_first_ssl_listener_index.c @@ -26,13 +26,13 @@ #include "httplib_ssl.h" /* - * int XX_httplib_get_first_ssl_listener_index( const struct mg_context *ctx ); + * int XX_httplib_get_first_ssl_listener_index( const struct httplib_context *ctx ); * * The function XX_httplib_get_first_ssl_listener_index() returns the first * index of a listening socket where SSL encryption is active. */ -int XX_httplib_get_first_ssl_listener_index( const struct mg_context *ctx ) { +int XX_httplib_get_first_ssl_listener_index( const struct httplib_context *ctx ) { unsigned int i; int idx; diff --git a/src/httplib_get_header.c b/src/httplib_get_header.c index 805a130a..9a449023 100644 --- a/src/httplib_get_header.c +++ b/src/httplib_get_header.c @@ -25,12 +25,12 @@ #include "httplib_main.h" /* Return HTTP header value, or NULL if not found. */ -const char *XX_httplib_get_header( const struct mg_request_info *ri, const char *name ) { +const char *XX_httplib_get_header( const struct httplib_request_info *ri, const char *name ) { int i; if (ri) { for (i = 0; i < ri->num_headers; i++) { - if (!mg_strcasecmp(name, ri->http_headers[i].name)) return ri->http_headers[i].value; + if (!httplib_strcasecmp(name, ri->http_headers[i].name)) return ri->http_headers[i].value; } } @@ -39,10 +39,10 @@ const char *XX_httplib_get_header( const struct mg_request_info *ri, const char } /* XX_httplib_get_header */ -const char *mg_get_header( const struct mg_connection *conn, const char *name ) { +const char *httplib_get_header( const struct httplib_connection *conn, const char *name ) { if ( conn == NULL ) return NULL; return XX_httplib_get_header( & conn->request_info, name ); -} /* mg_get_header */ +} /* httplib_get_header */ diff --git a/src/httplib_get_mime_type.c b/src/httplib_get_mime_type.c index 019c6b2e..54b7af87 100644 --- a/src/httplib_get_mime_type.c +++ b/src/httplib_get_mime_type.c @@ -26,7 +26,7 @@ /* Look at the "path" extension and figure what mime type it has. * Store mime type in the vector. */ -void XX_httplib_get_mime_type( struct mg_context *ctx, const char *path, struct vec *vec ) { +void XX_httplib_get_mime_type( struct httplib_context *ctx, const char *path, struct vec *vec ) { struct vec ext_vec; struct vec mime_vec; @@ -44,7 +44,7 @@ void XX_httplib_get_mime_type( struct mg_context *ctx, const char *path, struct while ((list = XX_httplib_next_option(list, &ext_vec, &mime_vec)) != NULL) { /* ext now points to the path suffix */ ext = path + path_len - ext_vec.len; - if (mg_strncasecmp(ext, ext_vec.ptr, ext_vec.len) == 0) { + if (httplib_strncasecmp(ext, ext_vec.ptr, ext_vec.len) == 0) { *vec = mime_vec; return; } diff --git a/src/httplib_get_option.c b/src/httplib_get_option.c index be10201e..55d727d0 100644 --- a/src/httplib_get_option.c +++ b/src/httplib_get_option.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -const char *mg_get_option(const struct mg_context *ctx, const char *name) { +const char *httplib_get_option(const struct httplib_context *ctx, const char *name) { int i; @@ -32,4 +32,4 @@ const char *mg_get_option(const struct mg_context *ctx, const char *name) { else if ( ctx == NULL || ctx->config[i] == NULL ) return ""; else return ctx->config[i]; -} /* mg_get_option */ +} /* httplib_get_option */ diff --git a/src/httplib_get_rel_url_at_current_server.c b/src/httplib_get_rel_url_at_current_server.c index 47ff5512..b6da00fb 100644 --- a/src/httplib_get_rel_url_at_current_server.c +++ b/src/httplib_get_rel_url_at_current_server.c @@ -25,13 +25,13 @@ #include "httplib_main.h" /* - * const char *XX_httplib_get_rel_url_at_current_server( const char *uri, const struct mg_connection *conn ); + * const char *XX_httplib_get_rel_url_at_current_server( const char *uri, const struct httplib_connection *conn ); * * The function XX_httplib_get_rel_url_at_current_server() returns the relative * uri at the current server. */ -const char * XX_httplib_get_rel_url_at_current_server( const char *uri, const struct mg_connection *conn ) { +const char * XX_httplib_get_rel_url_at_current_server( const char *uri, const struct httplib_connection *conn ) { const char *server_domain; size_t server_domain_len; @@ -55,7 +55,7 @@ const char * XX_httplib_get_rel_url_at_current_server( const char *uri, const st } for (i = 0; XX_httplib_abs_uri_protocols[i].proto != NULL; i++) { - if (mg_strncasecmp(uri, + if (httplib_strncasecmp(uri, XX_httplib_abs_uri_protocols[i].proto, XX_httplib_abs_uri_protocols[i].proto_len) == 0) { diff --git a/src/httplib_get_remote_ip.c b/src/httplib_get_remote_ip.c index 0f15b3ac..8deb37a6 100644 --- a/src/httplib_get_remote_ip.c +++ b/src/httplib_get_remote_ip.c @@ -25,13 +25,13 @@ #include "httplib_main.h" /* - * uint32_t XX_httplib_get_remote_ip( const struct mg_connection *conn ); + * uint32_t XX_httplib_get_remote_ip( const struct httplib_connection *conn ); * * The function XX_httplib_get_remote_ip() returns the IPv4 address of the * remote peer. */ -uint32_t XX_httplib_get_remote_ip( const struct mg_connection *conn ) { +uint32_t XX_httplib_get_remote_ip( const struct httplib_connection *conn ) { if ( conn == NULL ) return 0; diff --git a/src/httplib_get_request_handler.c b/src/httplib_get_request_handler.c index e3a57001..6ef045b7 100644 --- a/src/httplib_get_request_handler.c +++ b/src/httplib_get_request_handler.c @@ -31,18 +31,18 @@ * for a connection. */ -int XX_httplib_get_request_handler( struct mg_connection *conn, int handler_type, mg_request_handler *handler, mg_websocket_connect_handler *connect_handler, mg_websocket_ready_handler *ready_handler, mg_websocket_data_handler *data_handler, mg_websocket_close_handler *close_handler, mg_authorization_handler *auth_handler, void **cbdata ) { - const struct mg_request_info *request_info = mg_get_request_info(conn); +int XX_httplib_get_request_handler( struct httplib_connection *conn, int handler_type, httplib_request_handler *handler, httplib_websocket_connect_handler *connect_handler, httplib_websocket_ready_handler *ready_handler, httplib_websocket_data_handler *data_handler, httplib_websocket_close_handler *close_handler, httplib_authorization_handler *auth_handler, void **cbdata ) { + const struct httplib_request_info *request_info = httplib_get_request_info(conn); if ( request_info == NULL ) return 0; const char *uri = request_info->local_uri; size_t urilen = strlen(uri); - struct mg_handler_info *tmp_rh; + struct httplib_handler_info *tmp_rh; if ( conn == NULL || conn->ctx == NULL ) return 0; - mg_lock_context(conn->ctx); + httplib_lock_context(conn->ctx); /* first try for an exact match */ for (tmp_rh = conn->ctx->handlers; tmp_rh != NULL; @@ -60,7 +60,7 @@ int XX_httplib_get_request_handler( struct mg_connection *conn, int handler_type *auth_handler = tmp_rh->auth_handler; } *cbdata = tmp_rh->cbdata; - mg_unlock_context(conn->ctx); + httplib_unlock_context(conn->ctx); return 1; } } @@ -83,7 +83,7 @@ int XX_httplib_get_request_handler( struct mg_connection *conn, int handler_type *auth_handler = tmp_rh->auth_handler; } *cbdata = tmp_rh->cbdata; - mg_unlock_context(conn->ctx); + httplib_unlock_context(conn->ctx); return 1; } } @@ -105,13 +105,13 @@ int XX_httplib_get_request_handler( struct mg_connection *conn, int handler_type *auth_handler = tmp_rh->auth_handler; } *cbdata = tmp_rh->cbdata; - mg_unlock_context(conn->ctx); + httplib_unlock_context(conn->ctx); return 1; } } } - mg_unlock_context(conn->ctx); + httplib_unlock_context(conn->ctx); return 0; /* none found */ diff --git a/src/httplib_get_request_info.c b/src/httplib_get_request_info.c index a4b1be5d..3bf0572d 100644 --- a/src/httplib_get_request_info.c +++ b/src/httplib_get_request_info.c @@ -24,10 +24,10 @@ #include "httplib_main.h" -const struct mg_request_info *mg_get_request_info( const struct mg_connection *conn ) { +const struct httplib_request_info *httplib_get_request_info( const struct httplib_connection *conn ) { if ( conn == NULL ) return NULL; return & conn->request_info; -} /* mg_get_request_info */ +} /* httplib_get_request_info */ diff --git a/src/httplib_get_response.c b/src/httplib_get_response.c index 64b879e8..5c1862d1 100644 --- a/src/httplib_get_response.c +++ b/src/httplib_get_response.c @@ -26,19 +26,19 @@ #include "httplib_string.h" /* - * int mg_get_response( struct mg_connection *conn, char *ebuf, size_t ebuf_len, int timeout ); + * int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int timeout ); * - * The function mg_get_response tries to get a response from a remote peer. + * The function httplib_get_response tries to get a response from a remote peer. */ -int mg_get_response( struct mg_connection *conn, char *ebuf, size_t ebuf_len, int timeout ) { +int httplib_get_response( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int timeout ) { if ( conn == NULL ) return -1; /* Implementation of API function for HTTP clients */ int err, ret; - struct mg_context *octx = conn->ctx; - struct mg_context rctx = *(conn->ctx); + struct httplib_context *octx = conn->ctx; + struct httplib_context rctx = *(conn->ctx); char txt[32]; /* will not overflow */ if (timeout >= 0) { @@ -61,4 +61,4 @@ int mg_get_response( struct mg_connection *conn, char *ebuf, size_t ebuf_len, in * For the first test use <0 for error and >0 for OK */ return (ret == 0) ? -1 : +1; -} /* mg_get_response */ +} /* httplib_get_response */ diff --git a/src/httplib_get_response_code_text.c b/src/httplib_get_response_code_text.c index 0cdac51a..f229bd86 100644 --- a/src/httplib_get_response_code_text.c +++ b/src/httplib_get_response_code_text.c @@ -25,13 +25,13 @@ #include "httplib_main.h" /* - * const char *mg_get_response_code_text( struct mg_connection *conn, int response_code ); + * const char *httplib_get_response_code_text( struct httplib_connection *conn, int response_code ); * - * The function mg_get_response_code_text() returns a text associated with an + * The function httplib_get_response_code_text() returns a text associated with an * HTTP response code. */ -const char *mg_get_response_code_text( struct mg_connection *conn, int response_code ) { +const char *httplib_get_response_code_text( struct httplib_connection *conn, int response_code ) { /* See IANA HTTP status code assignment: * http://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml @@ -111,7 +111,7 @@ const char *mg_get_response_code_text( struct mg_connection *conn, int response_ default: /* This error code is unknown. This should not happen. */ - if ( conn != NULL) mg_cry( conn, "Unknown HTTP response code: %u", response_code ); + if ( conn != NULL) httplib_cry( conn, "Unknown HTTP response code: %u", response_code ); /* Return at least a category according to RFC 2616 Section 10. */ if (response_code >= 100 && response_code < 200) return "Information"; @@ -123,4 +123,4 @@ const char *mg_get_response_code_text( struct mg_connection *conn, int response_ return ""; } -} /* mg_get_response_code_text */ +} /* httplib_get_response_code_text */ diff --git a/src/httplib_get_server_ports.c b/src/httplib_get_server_ports.c index 1a3f86ef..e69269e0 100644 --- a/src/httplib_get_server_ports.c +++ b/src/httplib_get_server_ports.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int mg_get_server_ports(const struct mg_context *ctx, int size, struct mg_server_ports *ports) { +int httplib_get_server_ports(const struct httplib_context *ctx, int size, struct httplib_server_ports *ports) { int i; int cnt = 0; @@ -59,4 +59,4 @@ int mg_get_server_ports(const struct mg_context *ctx, int size, struct mg_server return cnt; -} /* mg_get_server_ports */ +} /* httplib_get_server_ports */ diff --git a/src/httplib_get_uri_type.c b/src/httplib_get_uri_type.c index 4f7b0a2c..627839fe 100644 --- a/src/httplib_get_uri_type.c +++ b/src/httplib_get_uri_type.c @@ -95,7 +95,7 @@ int XX_httplib_get_uri_type( const char *uri ) { * addressing the current server. So LibHTTP can also be used * as a proxy server. */ for (i = 0; XX_httplib_abs_uri_protocols[i].proto != NULL; i++) { - if (mg_strncasecmp(uri, XX_httplib_abs_uri_protocols[i].proto, XX_httplib_abs_uri_protocols[i].proto_len) == 0) { + if (httplib_strncasecmp(uri, XX_httplib_abs_uri_protocols[i].proto, XX_httplib_abs_uri_protocols[i].proto_len) == 0) { hostend = strchr(uri + XX_httplib_abs_uri_protocols[i].proto_len, '/'); if (!hostend) return 0; diff --git a/src/httplib_get_user_connection_data.c b/src/httplib_get_user_connection_data.c index 94f1d8c5..6cf1d001 100644 --- a/src/httplib_get_user_connection_data.c +++ b/src/httplib_get_user_connection_data.c @@ -24,10 +24,10 @@ #include "httplib_main.h" -void * mg_get_user_connection_data(const struct mg_connection *conn) { +void * httplib_get_user_connection_data(const struct httplib_connection *conn) { if (conn != NULL) return conn->request_info.conn_data; return NULL; -} /* mg_get_user_connection_data */ +} /* httplib_get_user_connection_data */ diff --git a/src/httplib_get_user_data.c b/src/httplib_get_user_data.c index 8c9b470c..03cb2745 100644 --- a/src/httplib_get_user_data.c +++ b/src/httplib_get_user_data.c @@ -24,8 +24,8 @@ #include "httplib_main.h" -void * mg_get_user_data(const struct mg_context *ctx) { +void * httplib_get_user_data(const struct httplib_context *ctx) { return (ctx == NULL) ? NULL : ctx->user_data; -} /* mg_get_user_data */ +} /* httplib_get_user_data */ diff --git a/src/httplib_get_valid_options.c b/src/httplib_get_valid_options.c index 51d0c0d5..6b5431a2 100644 --- a/src/httplib_get_valid_options.c +++ b/src/httplib_get_valid_options.c @@ -24,8 +24,8 @@ #include "httplib_main.h" -const struct mg_option *mg_get_valid_options(void) { +const struct httplib_option *httplib_get_valid_options(void) { return XX_httplib_config_options; -} /* mg_get_valid_options */ +} /* httplib_get_valid_options */ diff --git a/src/httplib_get_var.c b/src/httplib_get_var.c index 6ef77306..552d419d 100644 --- a/src/httplib_get_var.c +++ b/src/httplib_get_var.c @@ -24,14 +24,14 @@ #include "httplib_main.h" -int mg_get_var( const char *data, size_t data_len, const char *name, char *dst, size_t dst_len ) { +int httplib_get_var( const char *data, size_t data_len, const char *name, char *dst, size_t dst_len ) { - return mg_get_var2( data, data_len, name, dst, dst_len, 0 ); + return httplib_get_var2( data, data_len, name, dst, dst_len, 0 ); -} /* mg_get_var */ +} /* httplib_get_var */ -int mg_get_var2( const char *data, size_t data_len, const char *name, char *dst, size_t dst_len, size_t occurrence ) { +int httplib_get_var2( const char *data, size_t data_len, const char *name, char *dst, size_t dst_len, size_t occurrence ) { const char *p; const char *e; @@ -53,7 +53,7 @@ int mg_get_var2( const char *data, size_t data_len, const char *name, char *dst, /* data is "var1=val1&var2=val2...". Find variable first */ for (p = data; p + name_len < e; p++) { if ((p == data || p[-1] == '&') && p[name_len] == '=' - && !mg_strncasecmp(name, p, name_len) && 0 == occurrence--) { + && !httplib_strncasecmp(name, p, name_len) && 0 == occurrence--) { /* Point p to variable value */ p += name_len + 1; @@ -64,7 +64,7 @@ int mg_get_var2( const char *data, size_t data_len, const char *name, char *dst, if (s < p) return -3; /* Decode variable into destination buffer */ - len = mg_url_decode(p, (int)(s - p), dst, (int)dst_len, 1); + len = httplib_url_decode(p, (int)(s - p), dst, (int)dst_len, 1); /* Redirect error code from -1 to -2 (destination buffer too * small). */ @@ -76,4 +76,4 @@ int mg_get_var2( const char *data, size_t data_len, const char *name, char *dst, return len; -} /* mg_get_var2 */ +} /* httplib_get_var2 */ diff --git a/src/httplib_getreq.c b/src/httplib_getreq.c index 8263cf8b..e4ad49e9 100644 --- a/src/httplib_getreq.c +++ b/src/httplib_getreq.c @@ -26,12 +26,12 @@ #include "httplib_string.h" /* - * int XX_httplib_getreq( struct mg_connection *conn, char *ebuf, size_t ebuf_len, int *err ); + * int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int *err ); * * The function XX_httplib_getreq() processes a request from a remote client. */ -int XX_httplib_getreq( struct mg_connection *conn, char *ebuf, size_t ebuf_len, int *err ) { +int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int *err ) { const char *cl; @@ -93,16 +93,13 @@ int XX_httplib_getreq( struct mg_connection *conn, char *ebuf, size_t ebuf_len, conn->request_info.content_length = conn->content_len; } else if ((cl = XX_httplib_get_header(&conn->request_info, "Transfer-Encoding")) != NULL - && !mg_strcasecmp(cl, "chunked")) { + && !httplib_strcasecmp(cl, "chunked")) { conn->is_chunked = 1; - } else if (!mg_strcasecmp(conn->request_info.request_method, "POST") - || !mg_strcasecmp(conn->request_info.request_method, - "PUT")) { + } else if (!httplib_strcasecmp(conn->request_info.request_method, "POST") + || !httplib_strcasecmp(conn->request_info.request_method, "PUT")) { /* POST or PUT request without content length set */ conn->content_len = -1; - } else if (!mg_strncasecmp(conn->request_info.request_method, - "HTTP/", - 5)) { + } else if (!httplib_strncasecmp(conn->request_info.request_method, "HTTP/", 5)) { /* Response without content length set */ conn->content_len = -1; } else { diff --git a/src/httplib_handle_cgi_request.c b/src/httplib_handle_cgi_request.c index d7cf7002..94101707 100644 --- a/src/httplib_handle_cgi_request.c +++ b/src/httplib_handle_cgi_request.c @@ -27,7 +27,7 @@ #include "httplib_string.h" /* - * void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog ); + * void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char *prog ); * * The function XX_httplib_handle_cgi_request() handles a request for a CGI * resource. @@ -35,7 +35,7 @@ #if !defined(NO_CGI) -void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog ) { +void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char *prog ) { char *buf; size_t buflen; @@ -52,7 +52,7 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog char *pbuf; char dir[PATH_MAX]; char *p; - struct mg_request_info ri; + struct httplib_request_info ri; struct cgi_environment blk; FILE *in; FILE *out; @@ -76,7 +76,7 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog XX_httplib_snprintf(conn, &truncated, dir, sizeof(dir), "%s", prog); if (truncated) { - mg_cry(conn, "Error: CGI program \"%s\": Path too long", prog); + httplib_cry(conn, "Error: CGI program \"%s\": Path too long", prog); XX_httplib_send_http_error(conn, 500, "Error: %s", "CGI path too long"); goto done; } @@ -90,7 +90,7 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog if (pipe(fdin) != 0 || pipe(fdout) != 0 || pipe(fderr) != 0) { status = strerror(ERRNO); - mg_cry(conn, "Error: CGI program \"%s\": Can not create CGI pipes: %s", prog, status); + httplib_cry(conn, "Error: CGI program \"%s\": Can not create CGI pipes: %s", prog, status); XX_httplib_send_http_error(conn, 500, "Error: Cannot create CGI pipe: %s", status); goto done; } @@ -99,7 +99,7 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog if (pid == (pid_t)-1) { status = strerror(ERRNO); - mg_cry(conn, "Error: CGI program \"%s\": Can not spawn CGI process: %s", prog, status); + httplib_cry(conn, "Error: CGI program \"%s\": Can not spawn CGI process: %s", prog, status); XX_httplib_send_http_error(conn, 500, "Error: Cannot spawn CGI process [%s]: %s", prog, status); goto done; } @@ -126,21 +126,21 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog if ((in = fdopen(fdin[1], "wb")) == NULL) { status = strerror(ERRNO); - mg_cry(conn, "Error: CGI program \"%s\": Can not open stdin: %s", prog, status); + httplib_cry(conn, "Error: CGI program \"%s\": Can not open stdin: %s", prog, status); XX_httplib_send_http_error(conn, 500, "Error: CGI can not open fdin\nfopen: %s", status); goto done; } if ((out = fdopen(fdout[0], "rb")) == NULL) { status = strerror(ERRNO); - mg_cry(conn, "Error: CGI program \"%s\": Can not open stdout: %s", prog, status); + httplib_cry(conn, "Error: CGI program \"%s\": Can not open stdout: %s", prog, status); XX_httplib_send_http_error(conn, 500, "Error: CGI can not open fdout\nfopen: %s", status); goto done; } if ((err = fdopen(fderr[0], "rb")) == NULL) { status = strerror(ERRNO); - mg_cry(conn, "Error: CGI program \"%s\": Can not open stderr: %s", prog, status); + httplib_cry(conn, "Error: CGI program \"%s\": Can not open stderr: %s", prog, status); XX_httplib_send_http_error(conn, 500, "Error: CGI can not open fdout\nfopen: %s", status); goto done; } @@ -154,7 +154,7 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog /* This is a POST/PUT request, or another request with body data. */ if (!XX_httplib_forward_body_data(conn, in, INVALID_SOCKET, NULL)) { /* Error sending the body data */ - mg_cry(conn, "Error: CGI program \"%s\": Forward body data failed", prog); + httplib_cry(conn, "Error: CGI program \"%s\": Forward body data failed", prog); goto done; } } @@ -172,7 +172,7 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog buf = (char *)XX_httplib_malloc(buflen); if (buf == NULL) { XX_httplib_send_http_error(conn, 500, "Error: Not enough memory for CGI buffer (%u bytes)", (unsigned int)buflen); - mg_cry(conn, "Error: CGI program \"%s\": Not enough memory for buffer (%u " "bytes)", prog, (unsigned int)buflen); + httplib_cry(conn, "Error: CGI program \"%s\": Not enough memory for buffer (%u " "bytes)", prog, (unsigned int)buflen); goto done; } headers_len = XX_httplib_read_request(out, conn, buf, (int)buflen, &data_len); @@ -182,10 +182,10 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog * stderr. */ i = XX_httplib_pull_all(err, conn, buf, (int)buflen); if (i > 0) { - mg_cry(conn, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf); + httplib_cry(conn, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf); XX_httplib_send_http_error(conn, 500, "Error: CGI program \"%s\" sent error " "message: [%.*s]", prog, i, buf); } else { - mg_cry(conn, "Error: CGI program sent malformed or too big " "(>%u bytes) HTTP headers: [%.*s]", (unsigned)buflen, data_len, buf); + httplib_cry(conn, "Error: CGI program sent malformed or too big " "(>%u bytes) HTTP headers: [%.*s]", (unsigned)buflen, data_len, buf); XX_httplib_send_http_error(conn, 500, @@ -220,17 +220,16 @@ void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog if (!XX_httplib_header_has_option(connection_state, "keep-alive")) { conn->must_close = 1; } - mg_printf(conn, "HTTP/1.1 %d %s\r\n", conn->status_code, status_text); + httplib_printf(conn, "HTTP/1.1 %d %s\r\n", conn->status_code, status_text); /* Send headers */ for (i = 0; i < ri.num_headers; i++) { - mg_printf(conn, "%s: %s\r\n", ri.http_headers[i].name, ri.http_headers[i].value); + httplib_printf(conn, "%s: %s\r\n", ri.http_headers[i].name, ri.http_headers[i].value); } - mg_write(conn, "\r\n", 2); + httplib_write(conn, "\r\n", 2); /* Send chunk of data that may have been read after the headers */ - conn->num_bytes_sent += - mg_write(conn, buf + headers_len, (size_t)(data_len - headers_len)); + conn->num_bytes_sent += httplib_write(conn, buf + headers_len, (size_t)(data_len - headers_len)); /* Read the rest of CGI output and send to the client */ XX_httplib_send_file_data(conn, &fout, 0, INT64_MAX); diff --git a/src/httplib_handle_directory_request.c b/src/httplib_handle_directory_request.c index 48a47f46..9b700858 100644 --- a/src/httplib_handle_directory_request.c +++ b/src/httplib_handle_directory_request.c @@ -26,7 +26,7 @@ #include "httplib_memory.h" #include "httplib_utils.h" -void XX_httplib_handle_directory_request( struct mg_connection *conn, const char *dir ) { +void XX_httplib_handle_directory_request( struct httplib_connection *conn, const char *dir ) { unsigned int i; int sort_direction; @@ -46,12 +46,12 @@ void XX_httplib_handle_directory_request( struct mg_connection *conn, const char sort_direction = ((conn->request_info.query_string != NULL) && (conn->request_info.query_string[1] == 'd')) ? 'a' : 'd'; conn->must_close = 1; - mg_printf(conn, "HTTP/1.1 200 OK\r\n"); + httplib_printf(conn, "HTTP/1.1 200 OK\r\n"); XX_httplib_send_static_cache_header(conn); - mg_printf(conn, "Date: %s\r\n" "Connection: close\r\n" "Content-Type: text/html; charset=utf-8\r\n\r\n", date); + httplib_printf(conn, "Date: %s\r\n" "Connection: close\r\n" "Content-Type: text/html; charset=utf-8\r\n\r\n", date); conn->num_bytes_sent += - mg_printf(conn, + httplib_printf(conn, "Index of %s" "" "

Index of %s

"
@@ -67,7 +67,7 @@ void XX_httplib_handle_directory_request( struct mg_connection *conn, const char
 
 	/* Print first entry - link to a parent directory */
 	conn->num_bytes_sent +=
-	    mg_printf(conn,
+	    httplib_printf(conn,
 	              ""
 	              "\n",
 	              conn->request_info.local_uri,
@@ -89,7 +89,7 @@ void XX_httplib_handle_directory_request( struct mg_connection *conn, const char
 		XX_httplib_free(data.entries);
 	}
 
-	conn->num_bytes_sent += mg_printf(conn, "%s", "
%s %s  %s
"); + conn->num_bytes_sent += httplib_printf(conn, "%s", ""); conn->status_code = 200; } /* XX_httplib_handle_directory_request */ diff --git a/src/httplib_handle_file_based_request.c b/src/httplib_handle_file_based_request.c index 06480f4e..0cb2dd31 100644 --- a/src/httplib_handle_file_based_request.c +++ b/src/httplib_handle_file_based_request.c @@ -25,14 +25,14 @@ #include "httplib_main.h" /* - * void XX_httplib_handle_file_based_request( struct mg_connection *conn, const char *path, struct file *file ); + * void XX_httplib_handle_file_based_request( struct httplib_connection *conn, const char *path, struct file *file ); * * The function XX_httplib_handle_file_based_request() handles a request which * involves a file. This can either be a CGI request, an SSI request of a * request for a static file. */ -void XX_httplib_handle_file_based_request( struct mg_connection *conn, const char *path, struct file *file ) { +void XX_httplib_handle_file_based_request( struct httplib_connection *conn, const char *path, struct file *file ) { if ( conn == NULL || conn->ctx == NULL ) return; diff --git a/src/httplib_handle_form_request.c b/src/httplib_handle_form_request.c index ce847c47..e99bfeb2 100644 --- a/src/httplib_handle_form_request.c +++ b/src/httplib_handle_form_request.c @@ -24,14 +24,14 @@ #include "httplib_main.h" #include "httplib_memory.h" -static int url_encoded_field_found(const struct mg_connection *conn, +static int url_encoded_field_found(const struct httplib_connection *conn, const char *key, size_t key_len, const char *filename, size_t filename_len, char *path, size_t path_len, - struct mg_form_data_handler *fdh) { + struct httplib_form_data_handler *fdh) { char key_dec[1024]; char filename_dec[1024]; @@ -39,17 +39,17 @@ static int url_encoded_field_found(const struct mg_connection *conn, int filename_dec_len; int ret; - key_dec_len = mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1); + key_dec_len = httplib_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1); if (((size_t)key_dec_len >= (size_t)sizeof(key_dec)) || (key_dec_len < 0)) return FORM_FIELD_STORAGE_SKIP; if (filename) { - filename_dec_len = mg_url_decode(filename, (int)filename_len, filename_dec, (int)sizeof(filename_dec), 1); + filename_dec_len = httplib_url_decode(filename, (int)filename_len, filename_dec, (int)sizeof(filename_dec), 1); if (((size_t)filename_dec_len >= (size_t)sizeof(filename_dec)) || (filename_dec_len < 0)) { /* Log error message and skip this field. */ - mg_cry(conn, "%s: Cannot decode filename", __func__); + httplib_cry(conn, "%s: Cannot decode filename", __func__); return FORM_FIELD_STORAGE_SKIP; } @@ -60,13 +60,13 @@ static int url_encoded_field_found(const struct mg_connection *conn, if ((ret & 0xF) == FORM_FIELD_STORAGE_GET) { if (fdh->field_get == NULL) { - mg_cry(conn, "%s: Function \"Get\" not available", __func__); + httplib_cry(conn, "%s: Function \"Get\" not available", __func__); return FORM_FIELD_STORAGE_SKIP; } } if ((ret & 0xF) == FORM_FIELD_STORAGE_STORE) { if (fdh->field_store == NULL) { - mg_cry(conn, "%s: Function \"Store\" not available", __func__); + httplib_cry(conn, "%s: Function \"Store\" not available", __func__); return FORM_FIELD_STORAGE_SKIP; } } @@ -75,12 +75,12 @@ static int url_encoded_field_found(const struct mg_connection *conn, } -static int url_encoded_field_get(const struct mg_connection *conn, +static int url_encoded_field_get(const struct httplib_connection *conn, const char *key, size_t key_len, const char *value, size_t value_len, - struct mg_form_data_handler *fdh) { + struct httplib_form_data_handler *fdh) { char key_dec[1024]; @@ -89,13 +89,13 @@ static int url_encoded_field_get(const struct mg_connection *conn, if (!value_dec) { /* Log error message and stop parsing the form data. */ - mg_cry(conn, "%s: Not enough memory (required: %lu)", __func__, (unsigned long)(value_len + 1)); + httplib_cry(conn, "%s: Not enough memory (required: %lu)", __func__, (unsigned long)(value_len + 1)); return FORM_FIELD_STORAGE_ABORT; } - mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1); + httplib_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1); - value_dec_len = mg_url_decode(value, (int)value_len, value_dec, (int)value_len + 1, 1); + value_dec_len = httplib_url_decode(value, (int)value_len, value_dec, (int)value_len + 1, 1); ret = fdh->field_get(key_dec, value_dec, (size_t)value_dec_len, fdh->user_data); XX_httplib_free(value_dec); @@ -104,28 +104,27 @@ static int url_encoded_field_get(const struct mg_connection *conn, } -static int -unencoded_field_get(const struct mg_connection *conn, +static int unencoded_field_get(const struct httplib_connection *conn, const char *key, size_t key_len, const char *value, size_t value_len, - struct mg_form_data_handler *fdh) { + struct httplib_form_data_handler *fdh) { char key_dec[1024]; (void)conn; - mg_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1); + httplib_url_decode(key, (int)key_len, key_dec, (int)sizeof(key_dec), 1); return fdh->field_get(key_dec, value, value_len, fdh->user_data); } -static int field_stored(const struct mg_connection *conn, const char *path, long long file_size, struct mg_form_data_handler *fdh) { +static int field_stored(const struct httplib_connection *conn, const char *path, long long file_size, struct httplib_form_data_handler *fdh) { - /* Equivalent to "upload" callback of "mg_upload". */ + /* Equivalent to "upload" callback of "httplib_upload". */ - (void)conn; /* we do not need mg_cry here, so conn is currently unused */ + (void)conn; /* we do not need httplib_cry here, so conn is currently unused */ return fdh->field_store(path, file_size, fdh->user_data); } @@ -149,7 +148,7 @@ static const char * search_boundary(const char *buf, size_t buf_len, const char } -int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handler *fdh) { +int httplib_handle_form_request(struct httplib_connection *conn, struct httplib_form_data_handler *fdh) { const char *content_type; char path[512]; @@ -186,7 +185,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl /* GET request: form data is in the query string. */ /* The entire data has already been loaded, so there is no nead to - * call mg_read. We just need to split the query string into key-value + * call httplib_read. We just need to split the query string into key-value * pairs. */ data = conn->request_info.query_string; if (!data) { @@ -243,7 +242,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp); if ((n != (size_t)vallen) || (ferror(fstore.fp))) { - mg_cry(conn, "%s: Cannot write file %s", __func__, path); + httplib_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; XX_httplib_remove_bad_file(conn, path); @@ -256,13 +255,13 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl /* stored successfully */ field_stored(conn, path, file_size, fdh); } else { - mg_cry(conn, "%s: Error saving file %s", __func__, path); + httplib_cry(conn, "%s: Error saving file %s", __func__, path); XX_httplib_remove_bad_file(conn, path); } fstore.fp = NULL; } - } else mg_cry(conn, "%s: Cannot create file %s", __func__, path); + } else httplib_cry(conn, "%s: Cannot create file %s", __func__, path); } /* if (field_storage == FORM_FIELD_STORAGE_READ) { */ @@ -290,11 +289,11 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl return field_count; } - content_type = mg_get_header(conn, "Content-Type"); + content_type = httplib_get_header(conn, "Content-Type"); if (!content_type - || !mg_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED") - || !mg_strcasecmp(content_type, "APPLICATION/WWW-FORM-URLENCODED")) { + || !httplib_strcasecmp(content_type, "APPLICATION/X-WWW-FORM-URLENCODED") + || !httplib_strcasecmp(content_type, "APPLICATION/WWW-FORM-URLENCODED")) { /* The form data is in the request body data, encoded in key/value * pairs. */ int all_data_read = 0; @@ -315,7 +314,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl if ((size_t)buf_fill < (sizeof(buf) - 1)) { size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill; - r = mg_read(conn, buf + (size_t)buf_fill, to_read); + r = httplib_read(conn, buf + (size_t)buf_fill, to_read); if (r < 0) { /* read error */ return -1; @@ -355,7 +354,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl if (field_storage == FORM_FIELD_STORAGE_STORE) { if (XX_httplib_fopen(conn, path, "wb", &fstore) == 0) fstore.fp = NULL; file_size = 0; - if (!fstore.fp) mg_cry(conn, "%s: Cannot create file %s", __func__, path); + if (!fstore.fp) httplib_cry(conn, "%s: Cannot create file %s", __func__, path); } get_block = 0; @@ -393,7 +392,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl if (fstore.fp) { size_t n = (size_t)fwrite(val, 1, (size_t)vallen, fstore.fp); if ((n != (size_t)vallen) || (ferror(fstore.fp))) { - mg_cry(conn, "%s: Cannot write file %s", __func__, path); + httplib_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; XX_httplib_remove_bad_file(conn, path); @@ -408,7 +407,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl if ((size_t)buf_fill < (sizeof(buf) - 1)) { size_t to_read = sizeof(buf) - 1 - (size_t)buf_fill; - r = mg_read(conn, buf + (size_t)buf_fill, to_read); + r = httplib_read(conn, buf + (size_t)buf_fill, to_read); if (r < 0) { /* read error */ return -1; @@ -436,7 +435,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl /* stored successfully */ field_stored(conn, path, file_size, fdh); } else { - mg_cry(conn, "%s: Error saving file %s", __func__, path); + httplib_cry(conn, "%s: Error saving file %s", __func__, path); XX_httplib_remove_bad_file(conn, path); } fstore.fp = NULL; @@ -451,14 +450,14 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl return field_count; } - if (!mg_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) { + if (!httplib_strncasecmp(content_type, "MULTIPART/FORM-DATA;", 20)) { /* The form data is in the request body data, encoded as multipart * content (see https://www.ietf.org/rfc/rfc1867.txt, * https://www.ietf.org/rfc/rfc2388.txt). */ const char *boundary; size_t bl; ptrdiff_t used; - struct mg_request_info part_header; + struct httplib_request_info part_header; char *hbuf; char *hend; char *fbeg; @@ -475,7 +474,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl while (content_type[bl] == ' ') bl++; /* There has to be a BOUNDARY definition in the Content-Type header */ - if (mg_strncasecmp(content_type + bl, "BOUNDARY=", 9)) { + if (httplib_strncasecmp(content_type + bl, "BOUNDARY=", 9)) { /* Malformed request */ return -1; } @@ -499,7 +498,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl size_t towrite, n; int get_block; - r = mg_read(conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill); + r = httplib_read(conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill); if (r < 0) { /* read error */ return -1; @@ -600,7 +599,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl if (XX_httplib_fopen(conn, path, "wb", &fstore) == 0) fstore.fp = NULL; file_size = 0; - if (!fstore.fp) mg_cry(conn, "%s: Cannot create file %s", __func__, path); + if (!fstore.fp) httplib_cry(conn, "%s: Cannot create file %s", __func__, path); } get_block = 0; @@ -631,7 +630,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl /* Store the content of the buffer. */ n = (size_t)fwrite(hend, 1, towrite, fstore.fp); if ((n != towrite) || (ferror(fstore.fp))) { - mg_cry(conn, "%s: Cannot write file %s", __func__, path); + httplib_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; XX_httplib_remove_bad_file(conn, path); @@ -645,7 +644,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl hend = buf; /* Read new data */ - r = mg_read(conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill); + r = httplib_read(conn, buf + (size_t)buf_fill, sizeof(buf) - 1 - (size_t)buf_fill); if (r < 0) { /* read error */ return -1; @@ -679,7 +678,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl if (fstore.fp) { n = (size_t)fwrite(hend, 1, towrite, fstore.fp); if ((n != towrite) || (ferror(fstore.fp))) { - mg_cry(conn, "%s: Cannot write file %s", __func__, path); + httplib_cry(conn, "%s: Cannot write file %s", __func__, path); fclose(fstore.fp); fstore.fp = NULL; XX_httplib_remove_bad_file(conn, path); @@ -696,7 +695,7 @@ int mg_handle_form_request(struct mg_connection *conn, struct mg_form_data_handl /* stored successfully */ field_stored(conn, path, file_size, fdh); } else { - mg_cry(conn, "%s: Error saving file %s", __func__, path); + httplib_cry(conn, "%s: Error saving file %s", __func__, path); XX_httplib_remove_bad_file(conn, path); } fstore.fp = NULL; diff --git a/src/httplib_handle_not_modified_static_file_request.c b/src/httplib_handle_not_modified_static_file_request.c index bbabe584..63a99314 100644 --- a/src/httplib_handle_not_modified_static_file_request.c +++ b/src/httplib_handle_not_modified_static_file_request.c @@ -26,7 +26,7 @@ #include "httplib_utils.h" /* - * void XX_httplib_handle_not_modified_static_file_request( struct mg_connection *conn, struct file *filep ); + * void XX_httplib_handle_not_modified_static_file_request( struct httplib_connection *conn, struct file *filep ); * * The function XX_httplib_handle_not_modified_static_file_request() is used to * send a 304 response to a client to indicate that the requested resource has @@ -34,7 +34,7 @@ */ #if !defined(NO_CACHING) -void XX_httplib_handle_not_modified_static_file_request( struct mg_connection *conn, struct file *filep ) { +void XX_httplib_handle_not_modified_static_file_request( struct httplib_connection *conn, struct file *filep ) { char date[64]; char lm[64]; @@ -50,9 +50,9 @@ void XX_httplib_handle_not_modified_static_file_request( struct mg_connection *c XX_httplib_gmt_time_string(lm, sizeof(lm), &filep->last_modified); XX_httplib_construct_etag(etag, sizeof(etag), filep); - mg_printf(conn, "HTTP/1.1 %d %s\r\n" "Date: %s\r\n", conn->status_code, mg_get_response_code_text(conn, conn->status_code), date); + httplib_printf(conn, "HTTP/1.1 %d %s\r\n" "Date: %s\r\n", conn->status_code, httplib_get_response_code_text(conn, conn->status_code), date); XX_httplib_send_static_cache_header(conn); - mg_printf(conn, "Last-Modified: %s\r\n" "Etag: %s\r\n" "Connection: %s\r\n" "\r\n", lm, etag, XX_httplib_suggest_connection_header(conn)); + httplib_printf(conn, "Last-Modified: %s\r\n" "Etag: %s\r\n" "Connection: %s\r\n" "\r\n", lm, etag, XX_httplib_suggest_connection_header(conn)); } /* XX_httplib_handle_not_modified_static_file_request */ diff --git a/src/httplib_handle_propfind.c b/src/httplib_handle_propfind.c index c4343b58..143ed993 100644 --- a/src/httplib_handle_propfind.c +++ b/src/httplib_handle_propfind.c @@ -29,13 +29,13 @@ #if !defined(NO_FILES) /* - * static void print_props( struct mg_connection *conn, const char *uri, struct file *filep ); + * static void print_props( struct httplib_connection *conn, const char *uri, struct file *filep ); * * The function print_props() writes the PROPFIND properties for a collection * event. */ -static void print_props( struct mg_connection *conn, const char *uri, struct file *filep ) { +static void print_props( struct httplib_connection *conn, const char *uri, struct file *filep ) { char mtime[64]; @@ -43,7 +43,7 @@ static void print_props( struct mg_connection *conn, const char *uri, struct fil XX_httplib_gmt_time_string(mtime, sizeof(mtime), &filep->last_modified); conn->num_bytes_sent += - mg_printf(conn, + httplib_printf(conn, "" "%s" "" @@ -77,13 +77,13 @@ static void print_dav_dir_entry( struct de *de, void *data ) { char href_encoded[PATH_MAX * 3 /* worst case */]; int truncated; - struct mg_connection *conn = (struct mg_connection *)data; + struct httplib_connection *conn = (struct httplib_connection *)data; if (!de || !conn) return; XX_httplib_snprintf(conn, &truncated, href, sizeof(href), "%s%s", conn->request_info.local_uri, de->file_name); if (!truncated) { - mg_url_encode(href, href_encoded, PATH_MAX * 3); + httplib_url_encode(href, href_encoded, PATH_MAX * 3); print_props(conn, href_encoded, &de->file); } @@ -92,14 +92,14 @@ static void print_dav_dir_entry( struct de *de, void *data ) { /* - * void XX_httplib_handle_propfind( struct mg_connection *conn, const char *path, struct file *filep ); + * void XX_httplib_handle_propfind( struct httplib_connection *conn, const char *path, struct file *filep ); * * The function XX_httlib_handle_propfind() handles a propfind request. */ -void XX_httplib_handle_propfind( struct mg_connection *conn, const char *path, struct file *filep ) { +void XX_httplib_handle_propfind( struct httplib_connection *conn, const char *path, struct file *filep ) { - const char *depth = mg_get_header(conn, "Depth"); + const char *depth = httplib_get_header(conn, "Depth"); char date[64]; time_t curtime = time(NULL); @@ -109,23 +109,23 @@ void XX_httplib_handle_propfind( struct mg_connection *conn, const char *path, s conn->must_close = 1; conn->status_code = 207; - mg_printf(conn, "HTTP/1.1 207 Multi-Status\r\n" "Date: %s\r\n", date); + httplib_printf(conn, "HTTP/1.1 207 Multi-Status\r\n" "Date: %s\r\n", date); XX_httplib_send_static_cache_header(conn); - mg_printf(conn, "Connection: %s\r\n" "Content-Type: text/xml; charset=utf-8\r\n\r\n", XX_httplib_suggest_connection_header(conn)); + httplib_printf(conn, "Connection: %s\r\n" "Content-Type: text/xml; charset=utf-8\r\n\r\n", XX_httplib_suggest_connection_header(conn)); - conn->num_bytes_sent += mg_printf(conn, "" "\n"); + conn->num_bytes_sent += httplib_printf(conn, "" "\n"); /* Print properties for the requested resource itself */ print_props(conn, conn->request_info.local_uri, filep); /* If it is a directory, print directory entries too if Depth is not 0 */ if (filep && filep->is_directory - && !mg_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes") + && !httplib_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes") && (depth == NULL || strcmp(depth, "0") != 0)) { XX_httplib_scan_directory(conn, path, conn, &print_dav_dir_entry); } - conn->num_bytes_sent += mg_printf(conn, "%s\n", ""); + conn->num_bytes_sent += httplib_printf(conn, "%s\n", ""); } /* XX_httplib_handle_propfind */ diff --git a/src/httplib_handle_request.c b/src/httplib_handle_request.c index dfa2d527..71d0a355 100644 --- a/src/httplib_handle_request.c +++ b/src/httplib_handle_request.c @@ -27,7 +27,7 @@ #include "httplib_utils.h" /* - * void XX_httplib_handle_request( struct mg_connection *conn ); + * void XX_httplib_handle_request( struct httplib_connection *conn ); * * The function XX_httplib_handle_request() handles an incoming request. This * is the heart of the LibHTTP's logic. This function is called when the @@ -35,11 +35,11 @@ * to take: serve a file, or a directory, or call embedded function, etcetera. */ -void XX_httplib_handle_request( struct mg_connection *conn ) { +void XX_httplib_handle_request( struct httplib_connection *conn ) { if ( conn == NULL ) return; - struct mg_request_info *ri = &conn->request_info; + struct httplib_request_info *ri = &conn->request_info; char path[PATH_MAX]; int uri_len; int ssl_index; @@ -50,13 +50,13 @@ void XX_httplib_handle_request( struct mg_connection *conn ) { int is_callback_resource = 0; int i; struct file file = STRUCT_FILE_INITIALIZER; - mg_request_handler callback_handler = NULL; - mg_websocket_connect_handler ws_connect_handler = NULL; - mg_websocket_ready_handler ws_ready_handler = NULL; - mg_websocket_data_handler ws_data_handler = NULL; - mg_websocket_close_handler ws_close_handler = NULL; + httplib_request_handler callback_handler = NULL; + httplib_websocket_connect_handler ws_connect_handler = NULL; + httplib_websocket_ready_handler ws_ready_handler = NULL; + httplib_websocket_data_handler ws_data_handler = NULL; + httplib_websocket_close_handler ws_close_handler = NULL; void *callback_data = NULL; - mg_authorization_handler auth_handler = NULL; + httplib_authorization_handler auth_handler = NULL; void *auth_callback_data = NULL; #if !defined(NO_FILES) time_t curtime = time(NULL); @@ -83,14 +83,14 @@ void XX_httplib_handle_request( struct mg_connection *conn ) { /* A http to https forward port has been specified, * but no https port to forward to. */ XX_httplib_send_http_error(conn, 503, "%s", "Error: SSL forward not configured properly"); - mg_cry(conn, "Can not redirect to SSL, no SSL port available"); + httplib_cry(conn, "Can not redirect to SSL, no SSL port available"); } return; } uri_len = (int)strlen(ri->local_uri); /* 1.3. decode url (if config says so) */ - if (XX_httplib_should_decode_url(conn)) mg_url_decode( ri->local_uri, uri_len, (char *)ri->local_uri, uri_len + 1, 0); + if (XX_httplib_should_decode_url(conn)) httplib_url_decode( ri->local_uri, uri_len, (char *)ri->local_uri, uri_len + 1, 0); /* 1.4. clean URIs, so a path like allowed_dir/../forbidden_file is * not possible */ @@ -324,7 +324,7 @@ void XX_httplib_handle_request( struct mg_connection *conn ) { if (file.is_directory && (uri_len > 0) && (ri->local_uri[uri_len - 1] != '/')) { XX_httplib_gmt_time_string(date, sizeof(date), &curtime); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 301 Moved Permanently\r\n" "Location: %s/\r\n" "Date: %s\r\n" @@ -368,7 +368,7 @@ void XX_httplib_handle_request( struct mg_connection *conn ) { * define what should be possible in this case. */ } else { /* 14.2. no substitute file */ - if (!mg_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes")) XX_httplib_handle_directory_request(conn, path); + if (!httplib_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes")) XX_httplib_handle_directory_request(conn, path); else XX_httplib_send_http_error(conn, 403, "%s", "Error: Directory listing denied"); return; } diff --git a/src/httplib_handle_static_file_request.c b/src/httplib_handle_static_file_request.c index 70d4e1a2..ec959683 100644 --- a/src/httplib_handle_static_file_request.c +++ b/src/httplib_handle_static_file_request.c @@ -33,7 +33,7 @@ * request for a static file. */ -void XX_httplib_handle_static_file_request( struct mg_connection *conn, const char *path, struct file *filep, const char *mime_type, const char *additional_headers ) { +void XX_httplib_handle_static_file_request( struct httplib_connection *conn, const char *path, struct file *filep, const char *mime_type, const char *additional_headers ) { char date[64]; char lm[64]; @@ -92,7 +92,7 @@ void XX_httplib_handle_static_file_request( struct mg_connection *conn, const ch /* If Range: header specified, act accordingly */ r1 = r2 = 0; - hdr = mg_get_header(conn, "Range"); + hdr = httplib_get_header(conn, "Range"); if (hdr != NULL && (n = XX_httplib_parse_range_header(hdr, &r1, &r2)) > 0 && r1 >= 0 && r2 >= 0) { /* actually, range requests don't play well with a pre-gzipped @@ -115,7 +115,7 @@ void XX_httplib_handle_static_file_request( struct mg_connection *conn, const ch msg = "Partial Content"; } - hdr = mg_get_header(conn, "Origin"); + hdr = httplib_get_header(conn, "Origin"); if (hdr) { /* Cross-origin resource sharing (CORS), see * http://www.html5rocks.com/en/tutorials/cors/, @@ -134,9 +134,9 @@ void XX_httplib_handle_static_file_request( struct mg_connection *conn, const ch XX_httplib_gmt_time_string(lm, sizeof(lm), &filep->last_modified); XX_httplib_construct_etag(etag, sizeof(etag), filep); - mg_printf(conn, "HTTP/1.1 %d %s\r\n" "%s%s%s" "Date: %s\r\n", conn->status_code, msg, cors1, cors2, cors3, date); + httplib_printf(conn, "HTTP/1.1 %d %s\r\n" "%s%s%s" "Date: %s\r\n", conn->status_code, msg, cors1, cors2, cors3, date); XX_httplib_send_static_cache_header(conn); - mg_printf(conn, + httplib_printf(conn, "Last-Modified: %s\r\n" "Etag: %s\r\n" "Content-Type: %.*s\r\n" @@ -157,8 +157,8 @@ void XX_httplib_handle_static_file_request( struct mg_connection *conn, const ch * sure no one of the additional_headers is included twice */ if (additional_headers != NULL) { - mg_printf(conn, "%.*s\r\n\r\n", (int)strlen(additional_headers), additional_headers); - } else mg_printf(conn, "\r\n"); + httplib_printf(conn, "%.*s\r\n\r\n", (int)strlen(additional_headers), additional_headers); + } else httplib_printf(conn, "\r\n"); if (strcmp(conn->request_info.request_method, "HEAD") != 0) XX_httplib_send_file_data(conn, filep, r1, cl); XX_httplib_fclose(filep); diff --git a/src/httplib_handle_websocket_request.c b/src/httplib_handle_websocket_request.c index edf5e9ee..1c8afe4a 100644 --- a/src/httplib_handle_websocket_request.c +++ b/src/httplib_handle_websocket_request.c @@ -33,10 +33,10 @@ #if defined(USE_WEBSOCKET) -void XX_httplib_handle_websocket_request( struct mg_connection *conn, const char *path, int is_callback_resource, mg_websocket_connect_handler ws_connect_handler, mg_websocket_ready_handler ws_ready_handler, mg_websocket_data_handler ws_data_handler, mg_websocket_close_handler ws_close_handler, void *cbData ) { +void XX_httplib_handle_websocket_request( struct httplib_connection *conn, const char *path, int is_callback_resource, httplib_websocket_connect_handler ws_connect_handler, httplib_websocket_ready_handler ws_ready_handler, httplib_websocket_data_handler ws_data_handler, httplib_websocket_close_handler ws_close_handler, void *cbData ) { - const char *websock_key = mg_get_header(conn, "Sec-WebSocket-Key"); - const char *version = mg_get_header(conn, "Sec-WebSocket-Version"); + const char *websock_key = httplib_get_header(conn, "Sec-WebSocket-Key"); + const char *version = httplib_get_header(conn, "Sec-WebSocket-Version"); int lua_websock = 0; (void)path; @@ -50,14 +50,14 @@ void XX_httplib_handle_websocket_request( struct mg_connection *conn, const char /* It could be the hixie draft version * (http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76). */ - const char *key1 = mg_get_header(conn, "Sec-WebSocket-Key1"); - const char *key2 = mg_get_header(conn, "Sec-WebSocket-Key2"); + const char *key1 = httplib_get_header(conn, "Sec-WebSocket-Key1"); + const char *key2 = httplib_get_header(conn, "Sec-WebSocket-Key2"); char key3[8]; if ((key1 != NULL) && (key2 != NULL)) { /* This version uses 8 byte body data in a GET request */ conn->content_len = 8; - if (8 == mg_read(conn, key3, 8)) { + if (8 == httplib_read(conn, key3, 8)) { /* This is the hixie version */ XX_httplib_send_http_error(conn, 426, "%s", "Protocol upgrade to RFC 6455 required"); return; diff --git a/src/httplib_header_has_option.c b/src/httplib_header_has_option.c index 40e6a122..740a2d85 100644 --- a/src/httplib_header_has_option.c +++ b/src/httplib_header_has_option.c @@ -36,7 +36,7 @@ int XX_httplib_header_has_option( const char *header, const char *option ) { assert(option[0] != '\0'); while ((header = XX_httplib_next_option(header, &opt_vec, &eq_vec)) != NULL) { - if (mg_strncasecmp(option, opt_vec.ptr, opt_vec.len) == 0) return 1; + if (httplib_strncasecmp(option, opt_vec.ptr, opt_vec.len) == 0) return 1; } return 0; diff --git a/src/httplib_initialize_ssl.c b/src/httplib_initialize_ssl.c index 817384df..a8f1bb22 100644 --- a/src/httplib_initialize_ssl.c +++ b/src/httplib_initialize_ssl.c @@ -43,13 +43,13 @@ static void *cryptolib_dll_handle; /* Store the crypto library handle. */ /* - * int XX_httplib_initialize_ssl( struct mg_context *ctx ); + * int XX_httplib_initialize_ssl( struct httplib_context *ctx ); * * The function XX_httplib_initialize_ssl() initializes the use of SSL * encrypted communication on the given context. */ -int XX_httplib_initialize_ssl( struct mg_context *ctx ) { +int XX_httplib_initialize_ssl( struct httplib_context *ctx ) { int i; size_t size; @@ -70,7 +70,7 @@ int XX_httplib_initialize_ssl( struct mg_context *ctx ) { if (i < 0) i = 0; size = sizeof(pthread_mutex_t) * ((size_t)(i)); if ((XX_httplib_ssl_mutexes = (pthread_mutex_t *)XX_httplib_malloc(size)) == NULL) { - mg_cry( XX_httplib_fc(ctx), "%s: cannot allocate mutexes: %s", __func__, XX_httplib_ssl_error()); + httplib_cry( XX_httplib_fc(ctx), "%s: cannot allocate mutexes: %s", __func__, XX_httplib_ssl_error()); return 0; } diff --git a/src/httplib_interpret_uri.c b/src/httplib_interpret_uri.c index c134915c..a72a9b94 100644 --- a/src/httplib_interpret_uri.c +++ b/src/httplib_interpret_uri.c @@ -41,7 +41,7 @@ * is_put_or_delete_request: out: put/delete file? */ -void XX_httplib_interpret_uri( struct mg_connection *conn, char *filename, size_t filename_buf_len, struct file *filep, int *is_found, int *is_script_resource, int *is_websocket_request, int *is_put_or_delete_request ) { +void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, size_t filename_buf_len, struct file *filep, int *is_found, int *is_script_resource, int *is_websocket_request, int *is_put_or_delete_request ) { /* TODO (high): Restructure this function */ @@ -141,7 +141,7 @@ void XX_httplib_interpret_uri( struct mg_connection *conn, char *filename, size_ * to indicate that the response need to have the content- * encoding: gzip header. * We can only do this if the browser declares support. */ - if ((accept_encoding = mg_get_header(conn, "Accept-Encoding")) != NULL) { + if ((accept_encoding = httplib_get_header(conn, "Accept-Encoding")) != NULL) { if (strstr(accept_encoding, "gzip") != NULL) { XX_httplib_snprintf( conn, &truncated, gz_path, sizeof(gz_path), "%s.gz", filename); diff --git a/src/httplib_is_authorized_for_put.c b/src/httplib_is_authorized_for_put.c index a3bc4aef..37ce1234 100644 --- a/src/httplib_is_authorized_for_put.c +++ b/src/httplib_is_authorized_for_put.c @@ -25,7 +25,7 @@ #include "httplib_main.h" #if !defined(NO_FILES) -int XX_httplib_is_authorized_for_put( struct mg_connection *conn ) { +int XX_httplib_is_authorized_for_put( struct httplib_connection *conn ) { if ( conn == NULL ) return 0; diff --git a/src/httplib_is_file_in_memory.c b/src/httplib_is_file_in_memory.c index d861c2b6..7737d923 100644 --- a/src/httplib_is_file_in_memory.c +++ b/src/httplib_is_file_in_memory.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int XX_httplib_is_file_in_memory( const struct mg_connection *conn, const char *path, struct file *filep ) { +int XX_httplib_is_file_in_memory( const struct httplib_connection *conn, const char *path, struct file *filep ) { size_t size = 0; diff --git a/src/httplib_is_not_modified.c b/src/httplib_is_not_modified.c index c3281d04..308117ec 100644 --- a/src/httplib_is_not_modified.c +++ b/src/httplib_is_not_modified.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* - * int XX_httplib_is_not_modified( const struct mg_connection *conn, const struct file *filep ); + * int XX_httplib_is_not_modified( const struct httplib_connection *conn, const struct file *filep ); * * The function XX_httplib_is_not_modified() returns true, if a resource has * not been modified sinze a given datetime and a 304 response should therefore @@ -34,15 +34,15 @@ #if !defined(NO_CACHING) -int XX_httplib_is_not_modified( const struct mg_connection *conn, const struct file *filep ) { +int XX_httplib_is_not_modified( const struct httplib_connection *conn, const struct file *filep ) { char etag[64]; - const char *ims = mg_get_header( conn, "If-Modified-Since" ); - const char *inm = mg_get_header( conn, "If-None-Match" ); + const char *ims = httplib_get_header( conn, "If-Modified-Since" ); + const char *inm = httplib_get_header( conn, "If-None-Match" ); XX_httplib_construct_etag( etag, sizeof(etag), filep ); if ( filep == NULL ) return 0; - return (inm != NULL && !mg_strcasecmp(etag, inm)) || (ims != NULL && (filep->last_modified <= XX_httplib_parse_date_string(ims))); + return (inm != NULL && !httplib_strcasecmp(etag, inm)) || (ims != NULL && (filep->last_modified <= XX_httplib_parse_date_string(ims))); } /* XX_httplib_is_not_modified */ diff --git a/src/httplib_is_put_or_delete_method.c b/src/httplib_is_put_or_delete_method.c index bcc60912..377279fd 100644 --- a/src/httplib_is_put_or_delete_method.c +++ b/src/httplib_is_put_or_delete_method.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int XX_httplib_is_put_or_delete_method( const struct mg_connection *conn ) { +int XX_httplib_is_put_or_delete_method( const struct httplib_connection *conn ) { if ( conn == NULL ) return 0; diff --git a/src/httplib_is_websocket_protocol.c b/src/httplib_is_websocket_protocol.c index d02168e7..34d91a6b 100644 --- a/src/httplib_is_websocket_protocol.c +++ b/src/httplib_is_websocket_protocol.c @@ -26,13 +26,13 @@ #include "httplib_string.h" /* - * int XX_httplib_is_websocket_protocol( const struct mg_connection *conn ); + * int XX_httplib_is_websocket_protocol( const struct httplib_connection *conn ); * * The function XX_httplib_is_websocket_protocol() checks the request headers * to see if the connection is a valid websocket protocol. */ -int XX_httplib_is_websocket_protocol( const struct mg_connection *conn ) { +int XX_httplib_is_websocket_protocol( const struct httplib_connection *conn ) { #if defined(USE_WEBSOCKET) const char *upgrade; @@ -44,12 +44,12 @@ int XX_httplib_is_websocket_protocol( const struct mg_connection *conn ) { * Upgrade: Websocket */ - upgrade = mg_get_header(conn, "Upgrade"); + upgrade = httplib_get_header(conn, "Upgrade"); if (upgrade == NULL) return 0; /* fail early, don't waste time checking other header * fields */ if (!XX_httplib_strcasestr(upgrade, "websocket")) return 0; - connection = mg_get_header(conn, "Connection"); + connection = httplib_get_header(conn, "Connection"); if (connection == NULL) return 0; if (!XX_httplib_strcasestr(connection, "upgrade")) return 0; diff --git a/src/httplib_load_dll.c b/src/httplib_load_dll.c index 53a51366..a916d334 100644 --- a/src/httplib_load_dll.c +++ b/src/httplib_load_dll.c @@ -61,7 +61,7 @@ static int dlclose( void *handle ) { /* - * XX_httplib_load_dll( struct mg_context *ctx, const char *dll_name, struct ssl_func *sw ); + * XX_httplib_load_dll( struct httplib_context *ctx, const char *dll_name, struct ssl_func *sw ); * * The function XX_httplib_load_dll() is used to load the SSL library in a * dynamic way. The function returns either a handle to the dll, or NULL if an @@ -71,7 +71,7 @@ static int dlclose( void *handle ) { #if !defined(NO_SSL) #if !defined(NO_SSL_DL) -void *XX_httplib_load_dll( struct mg_context *ctx, const char *dll_name, struct ssl_func *sw ) { +void *XX_httplib_load_dll( struct httplib_context *ctx, const char *dll_name, struct ssl_func *sw ) { union { void *p; @@ -81,7 +81,7 @@ void *XX_httplib_load_dll( struct mg_context *ctx, const char *dll_name, struct struct ssl_func *fp; if ((dll_handle = dlopen(dll_name, RTLD_LAZY)) == NULL) { - mg_cry( XX_httplib_fc(ctx), "%s: cannot load %s", __func__, dll_name); + httplib_cry( XX_httplib_fc(ctx), "%s: cannot load %s", __func__, dll_name); return NULL; } @@ -96,7 +96,7 @@ void *XX_httplib_load_dll( struct mg_context *ctx, const char *dll_name, struct u.p = dlsym(dll_handle, fp->name); #endif /* _WIN32 */ if (u.fp == NULL) { - mg_cry( XX_httplib_fc(ctx), "%s: %s: cannot find %s", __func__, dll_name, fp->name); + httplib_cry( XX_httplib_fc(ctx), "%s: %s: cannot find %s", __func__, dll_name, fp->name); dlclose(dll_handle); return NULL; } else fp->ptr = u.fp; diff --git a/src/httplib_lock_unlock_connection.c b/src/httplib_lock_unlock_connection.c index 02474376..91974f84 100644 --- a/src/httplib_lock_unlock_connection.c +++ b/src/httplib_lock_unlock_connection.c @@ -26,28 +26,28 @@ #include "httplib_pthread.h" /* - * void mg_lock_connection( struct mg_connection *conn ); + * void httplib_lock_connection( struct httplib_connection *conn ); * - * The function mg_lock_connection() puts a lock on a connection. + * The function httplib_lock_connection() puts a lock on a connection. */ -void mg_lock_connection( struct mg_connection *conn ) { +void httplib_lock_connection( struct httplib_connection *conn ) { if ( conn != NULL ) pthread_mutex_lock( & conn->mutex ); -} /* mg_lock_connection */ +} /* httplib_lock_connection */ /* - * void mg_unlock_connection( struct mg_connection *conn ); + * void httplib_unlock_connection( struct httplib_connection *conn ); * - * The function mg_unlock_connection() removes the current lock from a + * The function httplib_unlock_connection() removes the current lock from a * connection. */ -void mg_unlock_connection( struct mg_connection *conn ) { +void httplib_unlock_connection( struct httplib_connection *conn ) { if ( conn != NULL ) pthread_mutex_unlock( & conn->mutex ); -} /* mg_unlock_connection */ +} /* httplib_unlock_connection */ diff --git a/src/httplib_lock_unlock_context.c b/src/httplib_lock_unlock_context.c index 82a7452c..ba659133 100644 --- a/src/httplib_lock_unlock_context.c +++ b/src/httplib_lock_unlock_context.c @@ -26,27 +26,27 @@ #include "httplib_pthread.h" /* - * void mg_lock_context( struct mg_context *ctx ); + * void httplib_lock_context( struct httplib_context *ctx ); * - * The function mg_lock_context() puts a lock on the context. + * The function httplib_lock_context() puts a lock on the context. */ -void mg_lock_context( struct mg_context *ctx ) { +void httplib_lock_context( struct httplib_context *ctx ) { if ( ctx != NULL ) pthread_mutex_lock( & ctx->nonce_mutex ); -} /* mg_lock_context */ +} /* httplib_lock_context */ /* - * void mg_unlock_context( struct mg_context *ctx ); + * void httplib_unlock_context( struct httplib_context *ctx ); * - * The function mg_unlock_context() removes the current lock from the context. + * The function httplib_unlock_context() removes the current lock from the context. */ -void mg_unlock_context( struct mg_context *ctx ) { +void httplib_unlock_context( struct httplib_context *ctx ) { if ( ctx != NULL ) pthread_mutex_unlock( & ctx->nonce_mutex ); -} /* mg_unlock_context */ +} /* httplib_unlock_context */ diff --git a/src/httplib_log_access.c b/src/httplib_log_access.c index c12e660a..c6d10e26 100644 --- a/src/httplib_log_access.c +++ b/src/httplib_log_access.c @@ -26,17 +26,17 @@ #include "httplib_ssl.h" #include "httplib_string.h" -static const char *header_val( const struct mg_connection *conn, const char *header ); +static const char *header_val( const struct httplib_connection *conn, const char *header ); /* - * void XX_httplib_log_access( const struct mg_connection *conn ); + * void XX_httplib_log_access( const struct httplib_connection *conn ); * * The function XX_httplib_log_access() logs an access of a client. */ -void XX_httplib_log_access( const struct mg_connection *conn ) { +void XX_httplib_log_access( const struct httplib_connection *conn ) { - const struct mg_request_info *ri; + const struct httplib_request_info *ri; struct file fi; char date[64]; char src_addr[IP_ADDR_STR_LEN]; @@ -104,17 +104,17 @@ void XX_httplib_log_access( const struct mg_connection *conn ) { /* - * static const char *header_val( const struct mg_connection *conn, const char *header ); + * static const char *header_val( const struct httplib_connection *conn, const char *header ); * * The function header_val() returns the value of a specific header of a * connection. */ -static const char *header_val( const struct mg_connection *conn, const char *header ) { +static const char *header_val( const struct httplib_connection *conn, const char *header ) { const char *header_value; - if ((header_value = mg_get_header(conn, header)) == NULL) return "-"; + if ((header_value = httplib_get_header(conn, header)) == NULL) return "-"; else return header_value; } /* header_val */ diff --git a/src/httplib_main.h b/src/httplib_main.h index 041dc936..29bfb92d 100644 --- a/src/httplib_main.h +++ b/src/httplib_main.h @@ -73,20 +73,20 @@ * Unfortunately some compilers still do not support it, so we have a * replacement function here. */ #if defined(_MSC_VER) && (_MSC_VER >= 1600) -#define mg_static_assert static_assert +#define httplib_static_assert static_assert #elif defined(__cplusplus) && (__cplusplus >= 201103L) -#define mg_static_assert static_assert +#define httplib_static_assert static_assert #elif defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201112L) -#define mg_static_assert _Static_assert +#define httplib_static_assert _Static_assert #else char static_assert_replacement[1]; -#define mg_static_assert(cond, txt) \ +#define httplib_static_assert(cond, txt) \ extern char static_assert_replacement[(cond) ? 1 : -1] #endif -mg_static_assert(sizeof(int) == 4 || sizeof(int) == 8, "int data type size check"); -mg_static_assert(sizeof(void *) == 4 || sizeof(void *) == 8, "pointer data type size check"); -mg_static_assert(sizeof(void *) >= sizeof(int), "data type size check"); +httplib_static_assert(sizeof(int) == 4 || sizeof(int) == 8, "int data type size check"); +httplib_static_assert(sizeof(void *) == 4 || sizeof(void *) == 8, "pointer data type size check"); +httplib_static_assert(sizeof(void *) >= sizeof(int), "data type size check"); /* DTL -- including winsock2.h works better if lean and mean */ @@ -163,7 +163,7 @@ typedef const char *SOCK_OPT_TYPE; #define PATH_MAX (4096) #endif /* PATH_MAX */ -mg_static_assert(PATH_MAX >= 1, "path length must be a positive number"); +httplib_static_assert(PATH_MAX >= 1, "path length must be a positive number"); #ifndef _IN_PORT_T #ifndef in_port_t @@ -234,7 +234,7 @@ typedef long off_t; #define WINCDECL __cdecl #define vsnprintf_impl _vsnprintf #define access _access -#define mg_sleep(x) (Sleep(x)) +#define httplib_sleep(x) (Sleep(x)) #define pipe(x) _pipe(x, MG_BUF_LEN, _O_BINARY) #ifndef popen @@ -265,7 +265,7 @@ typedef DWORD pthread_key_t; typedef HANDLE pthread_t; typedef struct { CRITICAL_SECTION threadIdSec; - struct mg_workerTLS * waiting_thread; /* The chain of threads */ + struct httplib_workerTLS * waiting_thread; /* The chain of threads */ } pthread_cond_t; #ifndef __clockid_t_defined @@ -361,11 +361,11 @@ typedef unsigned short int in_port_t; #define O_BINARY (0) #endif /* O_BINARY */ #define closesocket(a) (close(a)) -#define mg_mkdir(conn, path, mode) (mkdir(path, mode)) -#define mg_sleep(x) (usleep((x)*1000)) -#define mg_opendir(conn, x) (opendir(x)) -#define mg_closedir(x) (closedir(x)) -#define mg_readdir(x) (readdir(x)) +#define httplib_mkdir(conn, path, mode) (mkdir(path, mode)) +#define httplib_sleep(x) (usleep((x)*1000)) +#define httplib_opendir(conn, x) (opendir(x)) +#define httplib_closedir(x) (closedir(x)) +#define httplib_readdir(x) (readdir(x)) #define ERRNO (errno) #define INVALID_SOCKET (-1) #define INT64_FMT PRId64 @@ -516,12 +516,12 @@ typedef struct x509 X509; #endif /* NO_SSL_DL */ #endif /* NO_SSL */ -struct mg_workerTLS { +struct httplib_workerTLS { int is_master; unsigned long thread_idx; #if defined(_WIN32) HANDLE pthread_cond_helper_mutex; - struct mg_workerTLS *next_waiting_thread; + struct httplib_workerTLS *next_waiting_thread; #endif }; @@ -534,7 +534,7 @@ extern CRITICAL_SECTION global_log_file_lock; #define MAX_CGI_ENVIR_VARS (256) #define MG_BUF_LEN (8192) -mg_static_assert(MAX_REQUEST_SIZE >= 256, "request size length must be a positive number"); +httplib_static_assert(MAX_REQUEST_SIZE >= 256, "request size length must be a positive number"); /* Describes listening socket, or socket which was accept()-ed by the master @@ -551,10 +551,10 @@ struct socket { /* - * struct mg_handler_info; + * struct httplib_handler_info; */ -struct mg_handler_info { +struct httplib_handler_info { /* Name/Pattern of the URI. */ char *uri; size_t uri_len; @@ -563,34 +563,34 @@ struct mg_handler_info { int handler_type; /* Handler for http/https or authorization requests. */ - mg_request_handler handler; + httplib_request_handler handler; /* Handler for ws/wss (websocket) requests. */ - mg_websocket_connect_handler connect_handler; - mg_websocket_ready_handler ready_handler; - mg_websocket_data_handler data_handler; - mg_websocket_close_handler close_handler; + httplib_websocket_connect_handler connect_handler; + httplib_websocket_ready_handler ready_handler; + httplib_websocket_data_handler data_handler; + httplib_websocket_close_handler close_handler; /* Handler for authorization requests */ - mg_authorization_handler auth_handler; + httplib_authorization_handler auth_handler; /* User supplied argument for the handler function. */ void *cbdata; /* next handler in a linked list */ - struct mg_handler_info *next; + struct httplib_handler_info *next; }; /* - * struct mg_context; + * struct httplib_context; */ -struct mg_context { +struct httplib_context { volatile int stop_flag; /* Should we stop event loop */ SSL_CTX *ssl_ctx; /* SSL context */ char *config[NUM_OPTIONS]; /* LibHTTP configuration parameters */ - struct mg_callbacks callbacks; /* User-defined callback function */ + struct httplib_callbacks callbacks; /* User-defined callback function */ void *user_data; /* User-defined data */ int context_type; /* 1 = server context, 2 = client context */ @@ -624,7 +624,7 @@ struct mg_context { char *systemName; /* What operating system is running */ /* linked list of uri handlers */ - struct mg_handler_info *handlers; + struct httplib_handler_info *handlers; #ifdef USE_TIMERS struct ttimers *timers; @@ -632,12 +632,12 @@ struct mg_context { }; /* - * struct mg_connection; + * struct httplib_connection; */ -struct mg_connection { - struct mg_request_info request_info; - struct mg_context *ctx; +struct httplib_connection { + struct httplib_request_info request_info; + struct httplib_context *ctx; SSL *ssl; /* SSL descriptor */ SSL_CTX *client_ssl_ctx; /* SSL context for client connections */ struct socket client; /* Connected client */ @@ -662,21 +662,21 @@ struct mg_connection { 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 */ - pthread_mutex_t mutex; /* Used by mg_(un)lock_connection to ensure atomic transmissions for websockets */ + pthread_mutex_t mutex; /* Used by httplib_(un)lock_connection to ensure atomic transmissions for websockets */ int thread_index; /* Thread index within ctx */ }; struct worker_thread_args { - struct mg_context *ctx; + struct httplib_context *ctx; int index; }; struct websocket_client_thread_data { - struct mg_connection *conn; - mg_websocket_data_handler data_handler; - mg_websocket_close_handler close_handler; + struct httplib_connection *conn; + httplib_websocket_data_handler data_handler; + httplib_websocket_close_handler close_handler; void *callback_data; }; @@ -707,7 +707,7 @@ enum { REQUEST_HANDLER, WEBSOCKET_HANDLER, AUTH_HANDLER }; /* Directory entry */ struct de { - struct mg_connection *conn; + struct httplib_connection *conn; char *file_name; struct file file; }; @@ -728,7 +728,7 @@ struct dir_scan_data { * We satisfy both worlds: we create an envp array (which is vars), all * entries are actually pointers inside buf. */ struct cgi_environment { - struct mg_connection *conn; + struct httplib_connection *conn; /* Data block */ char *buf; /* Environment buffer */ size_t buflen; /* Space available in buf */ @@ -751,7 +751,7 @@ struct ah { }; struct read_auth_file_struct { - struct mg_connection *conn; + struct httplib_connection *conn; struct ah ah; char *domain; char buf[256 + 256 + 40]; @@ -773,8 +773,8 @@ typedef struct { -mg_static_assert(MAX_WORKER_THREADS >= 1, "worker threads must be a positive number"); -mg_static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, "size_t data type size check"); +httplib_static_assert(MAX_WORKER_THREADS >= 1, "worker threads must be a positive number"); +httplib_static_assert(sizeof(size_t) == 4 || sizeof(size_t) == 8, "size_t data type size check"); /* va_copy should always be a macro, C99 and C++11 - DTL */ #ifndef va_copy @@ -809,127 +809,127 @@ void SHA1Final( unsigned char digest[20], SHA1_CTX *context ); void SHA1Init( SHA1_CTX *context ); void SHA1Update( SHA1_CTX *context, const unsigned char *data, uint32_t len ); -void XX_httplib_accept_new_connection( const struct socket *listener, struct mg_context *ctx ); -int XX_httplib_authorize( struct mg_connection *conn, struct file *filep ); +void XX_httplib_accept_new_connection( const struct socket *listener, struct httplib_context *ctx ); +int XX_httplib_authorize( struct httplib_connection *conn, struct file *filep ); const char * XX_httplib_builtin_mime_ext( int index ); const char * XX_httplib_builtin_mime_type( int index ); -int XX_httplib_check_acl( struct mg_context *ctx, uint32_t remote_ip ); -int XX_httplib_check_authorization( struct mg_connection *conn, const char *path ); +int XX_httplib_check_acl( struct httplib_context *ctx, uint32_t remote_ip ); +int XX_httplib_check_authorization( struct httplib_connection *conn, const char *path ); int XX_httplib_check_password( const char *method, const char *ha1, const char *uri, const char *nonce, const char *nc, const char *cnonce, const char *qop, const char *response ); -void XX_httplib_close_all_listening_sockets( struct mg_context *ctx ); -void XX_httplib_close_connection( struct mg_connection *conn ); +void XX_httplib_close_all_listening_sockets( struct httplib_context *ctx ); +void XX_httplib_close_connection( struct httplib_connection *conn ); int XX_httplib_closedir( DIR *dir ); -void XX_httplib_close_socket_gracefully( struct mg_connection *conn ); +void XX_httplib_close_socket_gracefully( struct httplib_connection *conn ); int WINCDECL XX_httplib_compare_dir_entries( const void *p1, const void *p2 ); -int XX_httplib_connect_socket( struct mg_context *ctx, const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, SOCKET *sock, union usa *sa ); +int XX_httplib_connect_socket( struct httplib_context *ctx, const char *host, int port, int use_ssl, char *ebuf, size_t ebuf_len, SOCKET *sock, union usa *sa ); void XX_httplib_construct_etag( char *buf, size_t buf_len, const struct file *filep ); -int XX_httplib_consume_socket( struct mg_context *ctx, struct socket *sp, int thread_index ); -void XX_httplib_delete_file( struct mg_connection *conn, const char *path ); +int XX_httplib_consume_socket( struct httplib_context *ctx, struct socket *sp, int thread_index ); +void XX_httplib_delete_file( struct httplib_connection *conn, const char *path ); void XX_httplib_dir_scan_callback( struct de *de, void *data ); -void XX_httplib_discard_unread_request_data( struct mg_connection *conn ); -struct mg_connection * XX_httplib_fc( struct mg_context *ctx ); +void XX_httplib_discard_unread_request_data( struct httplib_connection *conn ); +struct httplib_connection * XX_httplib_fc( struct httplib_context *ctx ); void XX_httplib_fclose( struct file *filep ); -void XX_httplib_fclose_on_exec( struct file *filep, struct mg_connection *conn ); +void XX_httplib_fclose_on_exec( struct file *filep, struct httplib_connection *conn ); const char * XX_httplib_fgets( char *buf, size_t size, struct file *filep, char **p ); -int XX_httplib_fopen( const struct mg_connection *conn, const char *path, const char *mode, struct file *filep ); -int XX_httplib_forward_body_data( struct mg_connection *conn, FILE *fp, SOCKET sock, SSL *ssl ); -void XX_httplib_free_context( struct mg_context *ctx ); -const char * XX_httplib_get_header( const struct mg_request_info *ri, const char *name ); -void XX_httplib_get_mime_type( struct mg_context *ctx, const char *path, struct vec *vec ); +int XX_httplib_fopen( const struct httplib_connection *conn, const char *path, const char *mode, struct file *filep ); +int XX_httplib_forward_body_data( struct httplib_connection *conn, FILE *fp, SOCKET sock, SSL *ssl ); +void XX_httplib_free_context( struct httplib_context *ctx ); +const char * XX_httplib_get_header( const struct httplib_request_info *ri, const char *name ); +void XX_httplib_get_mime_type( struct httplib_context *ctx, const char *path, struct vec *vec ); int XX_httplib_get_option_index( const char *name ); -const char * XX_httplib_get_rel_url_at_current_server( const char *uri, const struct mg_connection *conn ); -uint32_t XX_httplib_get_remote_ip( const struct mg_connection *conn ); -int XX_httplib_get_request_handler( struct mg_connection *conn, int handler_type, mg_request_handler *handler, mg_websocket_connect_handler *connect_handler, mg_websocket_ready_handler *ready_handler, mg_websocket_data_handler *data_handler, mg_websocket_close_handler *close_handler, mg_authorization_handler *auth_handler, void **cbdata ); +const char * XX_httplib_get_rel_url_at_current_server( const char *uri, const struct httplib_connection *conn ); +uint32_t XX_httplib_get_remote_ip( const struct httplib_connection *conn ); +int XX_httplib_get_request_handler( struct httplib_connection *conn, int handler_type, httplib_request_handler *handler, httplib_websocket_connect_handler *connect_handler, httplib_websocket_ready_handler *ready_handler, httplib_websocket_data_handler *data_handler, httplib_websocket_close_handler *close_handler, httplib_authorization_handler *auth_handler, void **cbdata ); int XX_httplib_get_request_len( const char *buf, int buflen ); void XX_httplib_get_system_name( char **sysName ); int XX_httplib_get_uri_type( const char *uri ); -int XX_httplib_getreq( struct mg_connection *conn, char *ebuf, size_t ebuf_len, int *err ); -void XX_httplib_handle_cgi_request( struct mg_connection *conn, const char *prog ); -void XX_httplib_handle_directory_request( struct mg_connection *conn, const char *dir ); -void XX_httplib_handle_file_based_request( struct mg_connection *conn, const char *path, struct file *filep ); -void XX_httplib_handle_not_modified_static_file_request( struct mg_connection *conn, struct file *filep ); -void XX_httplib_handle_propfind( struct mg_connection *conn, const char *path, struct file *filep ); -void XX_httplib_handle_request( struct mg_connection *conn ); -void XX_httplib_handle_ssi_file_request( struct mg_connection *conn, const char *path, struct file *filep ); -void XX_httplib_handle_static_file_request( struct mg_connection *conn, const char *path, struct file *filep, const char *mime_type, const char *additional_headers ); -void XX_httplib_handle_websocket_request( struct mg_connection *conn, const char *path, int is_callback_resource, mg_websocket_connect_handler ws_connect_handler, mg_websocket_ready_handler ws_ready_handler, mg_websocket_data_handler ws_data_handler, mg_websocket_close_handler ws_close_handler, void *cbData ); +int XX_httplib_getreq( struct httplib_connection *conn, char *ebuf, size_t ebuf_len, int *err ); +void XX_httplib_handle_cgi_request( struct httplib_connection *conn, const char *prog ); +void XX_httplib_handle_directory_request( struct httplib_connection *conn, const char *dir ); +void XX_httplib_handle_file_based_request( struct httplib_connection *conn, const char *path, struct file *filep ); +void XX_httplib_handle_not_modified_static_file_request( struct httplib_connection *conn, struct file *filep ); +void XX_httplib_handle_propfind( struct httplib_connection *conn, const char *path, struct file *filep ); +void XX_httplib_handle_request( struct httplib_connection *conn ); +void XX_httplib_handle_ssi_file_request( struct httplib_connection *conn, const char *path, struct file *filep ); +void XX_httplib_handle_static_file_request( struct httplib_connection *conn, const char *path, struct file *filep, const char *mime_type, const char *additional_headers ); +void XX_httplib_handle_websocket_request( struct httplib_connection *conn, const char *path, int is_callback_resource, httplib_websocket_connect_handler ws_connect_handler, httplib_websocket_ready_handler ws_ready_handler, httplib_websocket_data_handler ws_data_handler, httplib_websocket_close_handler ws_close_handler, void *cbData ); int XX_httplib_header_has_option( const char *header, const char *option ); -void XX_httplib_interpret_uri( struct mg_connection *conn, char *filename, size_t filename_buf_len, struct file *filep, int *is_found, int *is_script_resource, int *is_websocket_request, int *is_put_or_delete_request ); -int XX_httplib_is_authorized_for_put( struct mg_connection *conn ); -int XX_httplib_is_file_in_memory( const struct mg_connection *conn, const char *path, struct file *filep ); +void XX_httplib_interpret_uri( struct httplib_connection *conn, char *filename, size_t filename_buf_len, struct file *filep, int *is_found, int *is_script_resource, int *is_websocket_request, int *is_put_or_delete_request ); +int XX_httplib_is_authorized_for_put( struct httplib_connection *conn ); +int XX_httplib_is_file_in_memory( const struct httplib_connection *conn, const char *path, struct file *filep ); bool XX_httplib_is_file_opened( const struct file *filep ); -int XX_httplib_is_not_modified( const struct mg_connection *conn, const struct file *filep ); -int XX_httplib_is_put_or_delete_method( const struct mg_connection *conn ); +int XX_httplib_is_not_modified( const struct httplib_connection *conn, const struct file *filep ); +int XX_httplib_is_put_or_delete_method( const struct httplib_connection *conn ); bool XX_httplib_is_valid_http_method( const char *method ); int XX_httplib_is_valid_port( unsigned long port ); -int XX_httplib_is_websocket_protocol( const struct mg_connection *conn ); +int XX_httplib_is_websocket_protocol( const struct httplib_connection *conn ); int XX_httplib_join_thread( pthread_t threadid ); int XX_httplib_kill( pid_t pid, int sig_num ); -void * XX_httplib_load_dll( struct mg_context *ctx, const char *dll_name, struct ssl_func *sw ); -void XX_httplib_log_access( const struct mg_connection *conn ); +void * XX_httplib_load_dll( struct httplib_context *ctx, const char *dll_name, struct ssl_func *sw ); +void XX_httplib_log_access( const struct httplib_connection *conn ); int XX_httplib_match_prefix(const char *pattern, size_t pattern_len, const char *str); -void XX_httplib_mkcol( struct mg_connection *conn, const char *path ); -int XX_httplib_mkdir( const struct mg_connection *conn, const char *path, int mode ); -int XX_httplib_must_hide_file( struct mg_connection *conn, const char *path ); +void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ); +int XX_httplib_mkdir( const struct httplib_connection *conn, const char *path, int mode ); +int XX_httplib_must_hide_file( struct httplib_connection *conn, const char *path ); const char * XX_httplib_next_option( const char *list, struct vec *val, struct vec *eq_val ); -void XX_httplib_open_auth_file( struct mg_connection *conn, const char *path, struct file *filep ); -DIR * XX_httplib_opendir( const struct mg_connection *conn, const char *name ); -int XX_httplib_parse_auth_header( struct mg_connection *conn, char *buf, size_t buf_size, struct ah *ah ); +void XX_httplib_open_auth_file( struct httplib_connection *conn, const char *path, struct file *filep ); +DIR * XX_httplib_opendir( const struct httplib_connection *conn, const char *name ); +int XX_httplib_parse_auth_header( struct httplib_connection *conn, char *buf, size_t buf_size, struct ah *ah ); time_t XX_httplib_parse_date_string( const char *datetime ); -int XX_httplib_parse_http_headers( char **buf, struct mg_request_info *ri ); -int XX_httplib_parse_http_message( char *buf, int len, struct mg_request_info *ri ); +int XX_httplib_parse_http_headers( char **buf, struct httplib_request_info *ri ); +int XX_httplib_parse_http_message( char *buf, int len, struct httplib_request_info *ri ); int XX_httplib_parse_net( const char *spec, uint32_t *net, uint32_t *mask ); int XX_httplib_parse_range_header( const char *header, int64_t *a, int64_t *b ); -void XX_httplib_path_to_unicode( const struct mg_connection *conn, const char *path, wchar_t *wbuf, size_t wbuf_len ); -void XX_httplib_prepare_cgi_environment( struct mg_connection *conn, const char *prog, struct cgi_environment *env ); +void XX_httplib_path_to_unicode( const struct httplib_connection *conn, const char *path, wchar_t *wbuf, size_t wbuf_len ); +void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const char *prog, struct cgi_environment *env ); void XX_httplib_print_dir_entry( struct de *de ); -void XX_httplib_process_new_connection( struct mg_connection *conn ); -void XX_httplib_produce_socket( struct mg_context *ctx, const struct socket *sp ); -int XX_httplib_pull( FILE *fp, struct mg_connection *conn, char *buf, int len, double timeout ); -int XX_httplib_pull_all( FILE *fp, struct mg_connection *conn, char *buf, int len ); -int64_t XX_httplib_push_all( struct mg_context *ctx, FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int64_t len ); -int XX_httplib_put_dir( struct mg_connection *conn, const char *path ); -void XX_httplib_put_file( struct mg_connection *conn, const char *path ); +void XX_httplib_process_new_connection( struct httplib_connection *conn ); +void XX_httplib_produce_socket( struct httplib_context *ctx, const struct socket *sp ); +int XX_httplib_pull( FILE *fp, struct httplib_connection *conn, char *buf, int len, double timeout ); +int XX_httplib_pull_all( FILE *fp, struct httplib_connection *conn, char *buf, int len ); +int64_t XX_httplib_push_all( struct httplib_context *ctx, FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int64_t len ); +int XX_httplib_put_dir( struct httplib_connection *conn, const char *path ); +void XX_httplib_put_file( struct httplib_connection *conn, const char *path ); int XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct *workdata ); -int XX_httplib_read_request( FILE *fp, struct mg_connection *conn, char *buf, int bufsiz, int *nread ); -void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_handler ws_data_handler, void *callback_data ); +int XX_httplib_read_request( FILE *fp, struct httplib_connection *conn, char *buf, int bufsiz, int *nread ); +void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websocket_data_handler ws_data_handler, void *callback_data ); struct dirent * XX_httplib_readdir( DIR *dir ); -void XX_httplib_redirect_to_https_port( struct mg_connection *conn, int ssl_index ); -int XX_httplib_refresh_trust( struct mg_connection *conn ); -int XX_httplib_remove( const struct mg_connection *conn, const char *path ); -void XX_httplib_remove_bad_file( const struct mg_connection *conn, const char *path ); -int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ); +void XX_httplib_redirect_to_https_port( struct httplib_connection *conn, int ssl_index ); +int XX_httplib_refresh_trust( struct httplib_connection *conn ); +int XX_httplib_remove( const struct httplib_connection *conn, const char *path ); +void XX_httplib_remove_bad_file( const struct httplib_connection *conn, const char *path ); +int XX_httplib_remove_directory( struct httplib_connection *conn, const char *dir ); void XX_httplib_remove_double_dots_and_double_slashes( char *s ); -void XX_httplib_reset_per_request_attributes( struct mg_connection *conn ); -int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void *data, void (*cb)(struct de *, void *) ); -void XX_httplib_send_authorization_request( struct mg_connection *conn ); -void XX_httplib_send_file_data( struct mg_connection *conn, struct file *filep, int64_t offset, int64_t len ); -void XX_httplib_send_http_error( struct mg_connection *, int, PRINTF_FORMAT_STRING(const char *fmt), ... ) PRINTF_ARGS(3, 4); -int XX_httplib_send_no_cache_header( struct mg_connection *conn ); -void XX_httplib_send_options( struct mg_connection *conn ); -int XX_httplib_send_static_cache_header( struct mg_connection *conn ); -int XX_httplib_send_websocket_handshake( struct mg_connection *conn, const char *websock_key ); -int XX_httplib_set_acl_option( struct mg_context *ctx ); -void XX_httplib_set_close_on_exec( SOCKET sock, struct mg_connection *conn ); -int XX_httplib_set_gpass_option( struct mg_context *ctx ); -void XX_httplib_set_handler_type( struct mg_context *ctx, const char *uri, int handler_type, int is_delete_request, mg_request_handler handler, mg_websocket_connect_handler connect_handler, mg_websocket_ready_handler ready_handler, mg_websocket_data_handler data_handler, mg_websocket_close_handler close_handler, mg_authorization_handler auth_handler, void *cbdata ); +void XX_httplib_reset_per_request_attributes( struct httplib_connection *conn ); +int XX_httplib_scan_directory( struct httplib_connection *conn, const char *dir, void *data, void (*cb)(struct de *, void *) ); +void XX_httplib_send_authorization_request( struct httplib_connection *conn ); +void XX_httplib_send_file_data( struct httplib_connection *conn, struct file *filep, int64_t offset, int64_t len ); +void XX_httplib_send_http_error( struct httplib_connection *, int, PRINTF_FORMAT_STRING(const char *fmt), ... ) PRINTF_ARGS(3, 4); +int XX_httplib_send_no_cache_header( struct httplib_connection *conn ); +void XX_httplib_send_options( struct httplib_connection *conn ); +int XX_httplib_send_static_cache_header( struct httplib_connection *conn ); +int XX_httplib_send_websocket_handshake( struct httplib_connection *conn, const char *websock_key ); +int XX_httplib_set_acl_option( struct httplib_context *ctx ); +void XX_httplib_set_close_on_exec( SOCKET sock, struct httplib_connection *conn ); +int XX_httplib_set_gpass_option( struct httplib_context *ctx ); +void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri, int handler_type, int is_delete_request, httplib_request_handler handler, httplib_websocket_connect_handler connect_handler, httplib_websocket_ready_handler ready_handler, httplib_websocket_data_handler data_handler, httplib_websocket_close_handler close_handler, httplib_authorization_handler auth_handler, void *cbdata ); int XX_httplib_set_non_blocking_mode( SOCKET sock ); -int XX_httplib_set_ports_option( struct mg_context *ctx ); +int XX_httplib_set_ports_option( struct httplib_context *ctx ); int XX_httplib_set_sock_timeout( SOCKET sock, int milliseconds ); int XX_httplib_set_tcp_nodelay( SOCKET sock, int nodelay_on ); void XX_httplib_set_thread_name( const char *name ); int XX_httplib_set_throttle( const char *spec, uint32_t remote_ip, const char *uri ); -int XX_httplib_set_uid_option( struct mg_context *ctx ); -int XX_httplib_should_decode_url( const struct mg_connection *conn ); -int XX_httplib_should_keep_alive( const struct mg_connection *conn ); +int XX_httplib_set_uid_option( struct httplib_context *ctx ); +int XX_httplib_should_decode_url( const struct httplib_connection *conn ); +int XX_httplib_should_keep_alive( const struct httplib_connection *conn ); char * XX_httplib_skip( char **buf, const char *delimiters ); char * XX_httplib_skip_quoted( char **buf, const char *delimiters, const char *whitespace, char quotechar ); void XX_httplib_sockaddr_to_string(char *buf, size_t len, const union usa *usa ); -pid_t XX_httplib_spawn_process( struct mg_connection *conn, const char *prog, char *envblk, char *envp[], int fdin[2], int fdout[2], int fderr[2], const char *dir ); -int XX_httplib_stat( struct mg_connection *conn, const char *path, struct file *filep ); -int XX_httplib_substitute_index_file( struct mg_connection *conn, char *path, size_t path_len, struct file *filep ); -const char * XX_httplib_suggest_connection_header( const struct mg_connection *conn ); -int XX_httplib_websocket_write_exec( struct mg_connection *conn, int opcode, const char *data, size_t dataLen, uint32_t masking_key ); +pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *prog, char *envblk, char *envp[], int fdin[2], int fdout[2], int fderr[2], const char *dir ); +int XX_httplib_stat( struct httplib_connection *conn, const char *path, struct file *filep ); +int XX_httplib_substitute_index_file( struct httplib_connection *conn, char *path, size_t path_len, struct file *filep ); +const char * XX_httplib_suggest_connection_header( const struct httplib_connection *conn ); +int XX_httplib_websocket_write_exec( struct httplib_connection *conn, int opcode, const char *data, size_t dataLen, uint32_t masking_key ); @@ -957,7 +957,7 @@ unsigned __stdcall XX_httplib_worker_thread( void *thread_func_param ); extern struct pthread_mutex_undefined_struct * XX_httplib_pthread_mutex_attr; #else /* _WIN32 */ void * XX_httplib_master_thread( void *thread_func_param ); -int XX_httplib_start_thread_with_id( mg_thread_func_t func, void *param, pthread_t *threadidptr ); +int XX_httplib_start_thread_with_id( httplib_thread_func_t func, void *param, pthread_t *threadidptr ); void * XX_httplib_websocket_client_thread( void *data ); void * XX_httplib_worker_thread( void *thread_func_param ); @@ -965,4 +965,4 @@ extern pthread_mutexattr_t XX_httplib_pthread_mutex_attr; #endif /* _WIN32 */ extern const struct uriprot_tp XX_httplib_abs_uri_protocols[]; -extern struct mg_option XX_httplib_config_options[]; +extern struct httplib_option XX_httplib_config_options[]; diff --git a/src/httplib_malloc.c b/src/httplib_malloc.c index e3d40d3a..f3c84e9f 100644 --- a/src/httplib_malloc.c +++ b/src/httplib_malloc.c @@ -27,8 +27,8 @@ #if defined(MEMORY_DEBUGGING) -unsigned long mg_memory_debug_blockCount = 0; -unsigned long mg_memory_debug_totalMemUsed = 0; +unsigned long httplib_memory_debug_blockCount = 0; +unsigned long httplib_memory_debug_totalMemUsed = 0; void *XX_httplib_malloc_ex( size_t size, const char *file, unsigned line ) { @@ -39,8 +39,8 @@ void *XX_httplib_malloc_ex( size_t size, const char *file, unsigned line ) { if (data) { *(size_t *)data = size; - mg_memory_debug_totalMemUsed += size; - mg_memory_debug_blockCount++; + httplib_memory_debug_totalMemUsed += size; + httplib_memory_debug_blockCount++; memory = (void *)(((char *)data) + sizeof(size_t)); } @@ -67,8 +67,8 @@ void XX_httplib_free_ex( void *memory, const char *file, unsigned line ) { if (memory) { size = *(size_t *)data; - mg_memory_debug_totalMemUsed -= size; - mg_memory_debug_blockCount--; + httplib_memory_debug_totalMemUsed -= size; + httplib_memory_debug_blockCount--; free(data); } @@ -90,8 +90,8 @@ void *XX_httplib_realloc_ex( void *memory, size_t newsize, const char *file, uns _realloc = realloc(data, newsize + sizeof(size_t)); if (_realloc) { data = _realloc; - mg_memory_debug_totalMemUsed -= oldsize; - mg_memory_debug_totalMemUsed += newsize; + httplib_memory_debug_totalMemUsed -= oldsize; + httplib_memory_debug_totalMemUsed += newsize; *(size_t *)data = newsize; data = (void *)(((char *)data) + sizeof(size_t)); } else { diff --git a/src/httplib_master_thread.c b/src/httplib_master_thread.c index bd0e4833..e054c529 100644 --- a/src/httplib_master_thread.c +++ b/src/httplib_master_thread.c @@ -58,8 +58,8 @@ void *XX_httplib_master_thread( void *thread_func_param ) { static void master_thread_run(void *thread_func_param) { - struct mg_context *ctx = (struct mg_context *)thread_func_param; - struct mg_workerTLS tls; + struct httplib_context *ctx = (struct httplib_context *)thread_func_param; + struct httplib_workerTLS tls; struct pollfd *pfd; unsigned int i; unsigned int workerthreadcount; @@ -122,7 +122,7 @@ static void master_thread_run(void *thread_func_param) { /* Here stop_flag is 1 - Initiate shutdown. */ - /* Stop signal received: somebody called mg_stop. Quit. */ + /* Stop signal received: somebody called httplib_stop. Quit. */ XX_httplib_close_all_listening_sockets(ctx); /* Wakeup workers that are waiting for connections to handle. */ @@ -158,7 +158,7 @@ static void master_thread_run(void *thread_func_param) { #endif pthread_setspecific(XX_httplib_sTlsKey, NULL); - /* Signal mg_stop() that we're done. + /* Signal httplib_stop() that we're done. * WARNING: This must be the very last thing this * thread does, as ctx becomes invalid after this line. */ ctx->stop_flag = 2; diff --git a/src/httplib_md5.c b/src/httplib_md5.c index e387adc0..32e9ad49 100644 --- a/src/httplib_md5.c +++ b/src/httplib_md5.c @@ -40,7 +40,7 @@ static void bin2str(char *to, const unsigned char *p, size_t len) { /* Return stringified MD5 hash for list of strings. Buffer must be 33 bytes. */ -char * mg_md5(char buf[33], ...) { +char * httplib_md5(char buf[33], ...) { md5_byte_t hash[16]; const char *p; @@ -57,4 +57,4 @@ char * mg_md5(char buf[33], ...) { bin2str(buf, hash, sizeof(hash)); return buf; -} /* mg_md5 */ +} /* httplib_md5 */ diff --git a/src/httplib_mkcol.c b/src/httplib_mkcol.c index 87edc690..5b7faf6e 100644 --- a/src/httplib_mkcol.c +++ b/src/httplib_mkcol.c @@ -26,14 +26,14 @@ #include "httplib_utils.h" /* - * void XX_httplib_mkcol( struct mg_connection *conn, const char *path ); + * void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ); * * The function XX_httplib_mkcol() handles a MKCOL command from a remote * client. */ #if !defined(NO_FILES) -void XX_httplib_mkcol( struct mg_connection *conn, const char *path ) { +void XX_httplib_mkcol( struct httplib_connection *conn, const char *path ) { int rc; int body_len; @@ -49,7 +49,7 @@ void XX_httplib_mkcol( struct mg_connection *conn, const char *path ) { memset(&de.file, 0, sizeof(de.file)); if (!XX_httplib_stat(conn, path, &de.file)) { - mg_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); + httplib_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); } if (de.file.last_modified) { @@ -70,9 +70,9 @@ void XX_httplib_mkcol( struct mg_connection *conn, const char *path ) { conn->status_code = 201; XX_httplib_gmt_time_string(date, sizeof(date), &curtime); - mg_printf(conn, "HTTP/1.1 %d Created\r\n" "Date: %s\r\n", conn->status_code, date); + httplib_printf(conn, "HTTP/1.1 %d Created\r\n" "Date: %s\r\n", conn->status_code, date); XX_httplib_send_static_cache_header(conn); - mg_printf(conn, "Content-Length: 0\r\n" "Connection: %s\r\n\r\n", XX_httplib_suggest_connection_header(conn)); + httplib_printf(conn, "Content-Length: 0\r\n" "Connection: %s\r\n\r\n", XX_httplib_suggest_connection_header(conn)); } else if (rc == -1) { diff --git a/src/httplib_mkdir.c b/src/httplib_mkdir.c index e2acbd1a..66e73f4a 100644 --- a/src/httplib_mkdir.c +++ b/src/httplib_mkdir.c @@ -26,7 +26,7 @@ #if defined(_WIN32) -int XX_httplib_mkdir( const struct mg_connection *conn, const char *path, int mode ) { +int XX_httplib_mkdir( const struct httplib_connection *conn, const char *path, int mode ) { wchar_t wbuf[PATH_MAX]; diff --git a/src/httplib_modify_passwords_file.c b/src/httplib_modify_passwords_file.c index 22b316f1..6a380c32 100644 --- a/src/httplib_modify_passwords_file.c +++ b/src/httplib_modify_passwords_file.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int mg_modify_passwords_file( const char *fname, const char *domain, const char *user, const char *pass ) { +int httplib_modify_passwords_file( const char *fname, const char *domain, const char *user, const char *pass ) { int found, i; char line[512]; @@ -90,7 +90,7 @@ int mg_modify_passwords_file( const char *fname, const char *domain, const char if (!strcmp(u, user) && !strcmp(d, domain)) { found++; if (pass != NULL) { - mg_md5(ha1, user, ":", domain, ":", pass, NULL); + httplib_md5(ha1, user, ":", domain, ":", pass, NULL); fprintf(fp2, "%s:%s:%s\n", user, domain, ha1); } } else { @@ -100,7 +100,7 @@ int mg_modify_passwords_file( const char *fname, const char *domain, const char /* If new user, just add it */ if (!found && pass != NULL) { - mg_md5(ha1, user, ":", domain, ":", pass, NULL); + httplib_md5(ha1, user, ":", domain, ":", pass, NULL); fprintf(fp2, "%s:%s:%s\n", user, domain, ha1); } @@ -114,4 +114,4 @@ int mg_modify_passwords_file( const char *fname, const char *domain, const char return 1; -} /* mg_modify_passwords_file */ +} /* httplib_modify_passwords_file */ diff --git a/src/httplib_must_hide_file.c b/src/httplib_must_hide_file.c index 9550ede4..584ee276 100644 --- a/src/httplib_must_hide_file.c +++ b/src/httplib_must_hide_file.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int XX_httplib_must_hide_file( struct mg_connection *conn, const char *path ) { +int XX_httplib_must_hide_file( struct httplib_connection *conn, const char *path ) { if (conn && conn->ctx) { const char *pw_pattern = "**" PASSWORDS_FILE_NAME "$"; diff --git a/src/httplib_open_auth_file.c b/src/httplib_open_auth_file.c index 42a175ec..c7a3b950 100644 --- a/src/httplib_open_auth_file.c +++ b/src/httplib_open_auth_file.c @@ -27,7 +27,7 @@ /* Use the global passwords file, if specified by auth_gpass option, * or search for .htpasswd in the requested directory. */ -void XX_httplib_open_auth_file( struct mg_connection *conn, const char *path, struct file *filep ) { +void XX_httplib_open_auth_file( struct httplib_connection *conn, const char *path, struct file *filep ) { if ( conn == NULL || conn->ctx == NULL ) return; @@ -42,7 +42,7 @@ void XX_httplib_open_auth_file( struct mg_connection *conn, const char *path, st /* Use global passwords file */ if (!XX_httplib_fopen(conn, gpass, "r", filep)) { #ifdef DEBUG - mg_cry(conn, "fopen(%s): %s", gpass, strerror(ERRNO)); + httplib_cry(conn, "fopen(%s): %s", gpass, strerror(ERRNO)); #endif } /* Important: using local struct file to test path for is_directory @@ -53,7 +53,7 @@ void XX_httplib_open_auth_file( struct mg_connection *conn, const char *path, st if (truncated || !XX_httplib_fopen(conn, name, "r", filep)) { #ifdef DEBUG - mg_cry(conn, "fopen(%s): %s", name, strerror(ERRNO)); + httplib_cry(conn, "fopen(%s): %s", name, strerror(ERRNO)); #endif } } else { @@ -65,7 +65,7 @@ void XX_httplib_open_auth_file( struct mg_connection *conn, const char *path, st if (truncated || !XX_httplib_fopen(conn, name, "r", filep)) { #ifdef DEBUG - mg_cry(conn, "fopen(%s): %s", name, strerror(ERRNO)); + httplib_cry(conn, "fopen(%s): %s", name, strerror(ERRNO)); #endif } } diff --git a/src/httplib_opendir.c b/src/httplib_opendir.c index b4e8389d..9864849c 100644 --- a/src/httplib_opendir.c +++ b/src/httplib_opendir.c @@ -28,7 +28,7 @@ #if defined(_WIN32) /* Implementation of POSIX opendir/closedir/readdir for Windows. */ -DIR *XX_httplib_opendir( const struct mg_connection *conn, const char *name ) { +DIR *XX_httplib_opendir( const struct httplib_connection *conn, const char *name ) { DIR *dir = NULL; wchar_t wpath[PATH_MAX]; diff --git a/src/httplib_parse_auth_header.c b/src/httplib_parse_auth_header.c index a0ba8b2b..48730905 100644 --- a/src/httplib_parse_auth_header.c +++ b/src/httplib_parse_auth_header.c @@ -26,7 +26,7 @@ #include "httplib_string.h" /* Return 1 on success. Always initializes the ah structure. */ -int XX_httplib_parse_auth_header(struct mg_connection *conn, char *buf, size_t buf_size, struct ah *ah) { +int XX_httplib_parse_auth_header(struct httplib_connection *conn, char *buf, size_t buf_size, struct ah *ah) { char *name; char *value; @@ -37,7 +37,7 @@ int XX_httplib_parse_auth_header(struct mg_connection *conn, char *buf, size_t b if (!ah || !conn) return 0; memset(ah, 0, sizeof(*ah)); - if ((auth_header = mg_get_header(conn, "Authorization")) == NULL || mg_strncasecmp(auth_header, "Digest ", 7) != 0) return 0; + if ((auth_header = httplib_get_header(conn, "Authorization")) == NULL || httplib_strncasecmp(auth_header, "Digest ", 7) != 0) return 0; /* Make modifiable copy of the auth header */ XX_httplib_strlcpy(buf, auth_header + 7, buf_size); diff --git a/src/httplib_parse_http_headers.c b/src/httplib_parse_http_headers.c index 9e60159e..3fcb6410 100644 --- a/src/httplib_parse_http_headers.c +++ b/src/httplib_parse_http_headers.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* - * int XX_httplib_parse_http_headers( char **buf, struct mg_request_info *ri ); + * int XX_httplib_parse_http_headers( char **buf, struct httplib_request_info *ri ); * * The function XX_httplib_parse_http_headers() parses the HTTP headers from * the given buffer. The buf pointer is advanced to the point where parsing @@ -33,7 +33,7 @@ * headers read is returned, or a negative value if an error occured. */ -int XX_httplib_parse_http_headers( char **buf, struct mg_request_info *ri ) { +int XX_httplib_parse_http_headers( char **buf, struct httplib_request_info *ri ) { int i; diff --git a/src/httplib_parse_http_message.c b/src/httplib_parse_http_message.c index 29f61cf1..71509ae8 100644 --- a/src/httplib_parse_http_message.c +++ b/src/httplib_parse_http_message.c @@ -25,21 +25,21 @@ #include "httplib_main.h" /* - * int XX_httplib_parse_http_message( char *buf, int len, struct mg_request_info *ri ); + * int XX_httplib_parse_http_message( char *buf, int len, struct httplib_request_info *ri ); * * The function XX_httplib_parse_http_message() parses an HTTP request and - * fills in the mg_request_info structure. This function modifies the buffer by + * fills in the httplib_request_info structure. This function modifies the buffer by * NUL terminating HTTP request components, header names and header values. * Parameters: * buf (in/out) pointer to the HTTP header to parse and split * len (in) length of the HTTP header buffer - * ri (out) parsed header as a mg_request_info structure + * ri (out) parsed header as a httplib_request_info structure * The parameters buf and ri must be valid pointers (not NULL) with a length * larger than zero. On error the function return a negative value, otherwise * the length of the request is returned. */ -int XX_httplib_parse_http_message( char *buf, int len, struct mg_request_info *ri ) { +int XX_httplib_parse_http_message( char *buf, int len, struct httplib_request_info *ri ) { int is_request; int request_length; diff --git a/src/httplib_path_to_unicode.c b/src/httplib_path_to_unicode.c index 23182254..d96c1c3d 100644 --- a/src/httplib_path_to_unicode.c +++ b/src/httplib_path_to_unicode.c @@ -50,7 +50,7 @@ static void change_slashes_to_backslashes( char *path ) { -static int mg_wcscasecmp( const wchar_t *s1, const wchar_t *s2 ) { +static int httplib_wcscasecmp( const wchar_t *s1, const wchar_t *s2 ) { int diff; @@ -62,19 +62,19 @@ static int mg_wcscasecmp( const wchar_t *s1, const wchar_t *s2 ) { return diff; -} /* mg_wcscasecmp */ +} /* httplib_wcscasecmp */ /* Encode 'path' which is assumed UTF-8 string, into UNICODE string. * wbuf and wbuf_len is a target buffer and its length. */ -void XX_httplib_path_to_unicode( const struct mg_connection *conn, const char *path, wchar_t *wbuf, size_t wbuf_len ) { +void XX_httplib_path_to_unicode( const struct httplib_connection *conn, const char *path, wchar_t *wbuf, size_t wbuf_len ) { char buf[PATH_MAX]; char buf2[PATH_MAX]; wchar_t wbuf2[MAX_PATH + 1]; DWORD long_len; DWORD err; - int (*fcompare)(const wchar_t *, const wchar_t *) = mg_wcscasecmp; + int (*fcompare)(const wchar_t *, const wchar_t *) = httplib_wcscasecmp; XX_httplib_strlcpy(buf, path, sizeof(buf)); change_slashes_to_backslashes(buf); diff --git a/src/httplib_prepare_cgi_environment.c b/src/httplib_prepare_cgi_environment.c index b7048b8d..3ae1fdd7 100644 --- a/src/httplib_prepare_cgi_environment.c +++ b/src/httplib_prepare_cgi_environment.c @@ -29,7 +29,7 @@ #include "httplib_utils.h" /* - * void XX_httplib_prepare_cgi_environment( struct mg_connection *conn, const char *prog, struct cgi_environment *env ); + * void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const char *prog, struct cgi_environment *env ); * * The function XX_httplib_prepare_cgi_environment() is used to prepare all * environment variables before a CGI script is called. @@ -37,7 +37,7 @@ #if !defined(NO_CGI) -void XX_httplib_prepare_cgi_environment( struct mg_connection *conn, const char *prog, struct cgi_environment *env ) { +void XX_httplib_prepare_cgi_environment( struct httplib_connection *conn, const char *prog, struct cgi_environment *env ) { const char *s; struct vec var_vec; @@ -60,7 +60,7 @@ void XX_httplib_prepare_cgi_environment( struct mg_connection *conn, const char XX_httplib_addenv( env, "SERVER_NAME=%s", conn->ctx->config[AUTHENTICATION_DOMAIN] ); XX_httplib_addenv( env, "SERVER_ROOT=%s", conn->ctx->config[DOCUMENT_ROOT] ); XX_httplib_addenv( env, "DOCUMENT_ROOT=%s", conn->ctx->config[DOCUMENT_ROOT] ); - XX_httplib_addenv( env, "SERVER_SOFTWARE=%s/%s", "LibHTTP", mg_version() ); + XX_httplib_addenv( env, "SERVER_SOFTWARE=%s/%s", "LibHTTP", httplib_version() ); /* Prepare the environment block */ XX_httplib_addenv( env, "%s", "GATEWAY_INTERFACE=CGI/1.1" ); @@ -98,9 +98,9 @@ void XX_httplib_prepare_cgi_environment( struct mg_connection *conn, const char XX_httplib_addenv(env, "HTTPS=%s", (conn->ssl == NULL) ? "off" : "on"); - if ( (s = mg_get_header( conn, "Content-Type" ) ) != NULL ) XX_httplib_addenv( env, "CONTENT_TYPE=%s", s ); + if ( (s = httplib_get_header( conn, "Content-Type" ) ) != NULL ) XX_httplib_addenv( env, "CONTENT_TYPE=%s", s ); if ( conn->request_info.query_string != NULL ) XX_httplib_addenv( env, "QUERY_STRING=%s", conn->request_info.query_string ); - if ( (s = mg_get_header( conn, "Content-Length" ) ) != NULL ) XX_httplib_addenv( env, "CONTENT_LENGTH=%s", s ); + if ( (s = httplib_get_header( conn, "Content-Length" ) ) != NULL ) XX_httplib_addenv( env, "CONTENT_LENGTH=%s", s ); if ( (s = getenv( "PATH" )) != NULL ) XX_httplib_addenv( env, "PATH=%s", s ); if ( conn->path_info != NULL ) XX_httplib_addenv( env, "PATH_INFO=%s", conn->path_info ); @@ -132,7 +132,7 @@ void XX_httplib_prepare_cgi_environment( struct mg_connection *conn, const char XX_httplib_snprintf(conn, &truncated, http_var_name, sizeof(http_var_name), "HTTP_%s", conn->request_info.http_headers[i].name); if (truncated) { - mg_cry(conn, "%s: HTTP header variable too long [%s]", __func__, conn->request_info.http_headers[i].name); + httplib_cry(conn, "%s: HTTP header variable too long [%s]", __func__, conn->request_info.http_headers[i].name); continue; } diff --git a/src/httplib_print_dir_entry.c b/src/httplib_print_dir_entry.c index a7d8a32a..6e6cf78a 100644 --- a/src/httplib_print_dir_entry.c +++ b/src/httplib_print_dir_entry.c @@ -52,9 +52,9 @@ void XX_httplib_print_dir_entry( struct de *de ) { XX_httplib_strlcpy(mod, "01-Jan-1970 00:00", sizeof(mod)); mod[sizeof(mod) - 1] = '\0'; } - mg_url_encode(de->file_name, href, sizeof(href)); + httplib_url_encode(de->file_name, href, sizeof(href)); de->conn->num_bytes_sent += - mg_printf(de->conn, + httplib_printf(de->conn, "%s%s" " %s  %s\n", de->conn->request_info.local_uri, diff --git a/src/httplib_printf.c b/src/httplib_printf.c index d2cfa77f..770b319b 100644 --- a/src/httplib_printf.c +++ b/src/httplib_printf.c @@ -25,7 +25,7 @@ #include "httplib_main.h" #include "httplib_string.h" -int mg_printf( struct mg_connection *conn, const char *fmt, ... ) { +int httplib_printf( struct httplib_connection *conn, const char *fmt, ... ) { va_list ap; int result; @@ -36,4 +36,4 @@ int mg_printf( struct mg_connection *conn, const char *fmt, ... ) { return result; -} /* mg_printf */ +} /* httplib_printf */ diff --git a/src/httplib_process_new_connection.c b/src/httplib_process_new_connection.c index 1f35b19e..3f661421 100644 --- a/src/httplib_process_new_connection.c +++ b/src/httplib_process_new_connection.c @@ -27,17 +27,17 @@ #include "httplib_string.h" /* - * void XX_httplib_process_new_connection( struct mg_connection *conn ); + * void XX_httplib_process_new_connection( struct httplib_connection *conn ); * * The function XX_httplib_process_new_connection() is used to process a new * incoming connection on a socket. */ -void XX_httplib_process_new_connection( struct mg_connection *conn ) { +void XX_httplib_process_new_connection( struct httplib_connection *conn ) { if ( conn == NULL || conn->ctx == NULL ) return; - struct mg_request_info *ri = &conn->request_info; + struct httplib_request_info *ri = &conn->request_info; int keep_alive_enabled; int keep_alive; int discard_len; diff --git a/src/httplib_produce_socket.c b/src/httplib_produce_socket.c index 73773ab6..d7b99c5d 100644 --- a/src/httplib_produce_socket.c +++ b/src/httplib_produce_socket.c @@ -26,14 +26,14 @@ #include "httplib_pthread.h" /* - * void XX_httplib_produce_socket( struct mg_context *ctx, const struct socket *sp ); + * void XX_httplib_produce_socket( struct httplib_context *ctx, const struct socket *sp ); * * The function XX_httplib_produce_socket() is used to produce a socket. */ #if defined(ALTERNATIVE_QUEUE) -void XX_httplib_produce_socket( struct mg_context *ctx, const struct socket *sp ) { +void XX_httplib_produce_socket( struct httplib_context *ctx, const struct socket *sp ) { unsigned int i; @@ -48,7 +48,7 @@ void XX_httplib_produce_socket( struct mg_context *ctx, const struct socket *sp } } /* queue is full */ - mg_sleep(1); + httplib_sleep(1); } } /* XX_httplib_produce_socket */ @@ -56,7 +56,7 @@ void XX_httplib_produce_socket( struct mg_context *ctx, const struct socket *sp #else /* ALTERNATIVE_QUEUE */ /* Master thread adds accepted socket to a queue */ -void XX_httplib_produce_socket(struct mg_context *ctx, const struct socket *sp) { +void XX_httplib_produce_socket(struct httplib_context *ctx, const struct socket *sp) { #define QUEUE_SIZE(ctx) ((int)(ARRAY_SIZE(ctx->queue))) if (!ctx) return; diff --git a/src/httplib_pull.c b/src/httplib_pull.c index f76657d0..3136f4aa 100644 --- a/src/httplib_pull.c +++ b/src/httplib_pull.c @@ -28,7 +28,7 @@ /* Read from IO channel - opened file descriptor, socket, or SSL descriptor. * Return negative value on error, or number of bytes read on success. */ -int XX_httplib_pull( FILE *fp, struct mg_connection *conn, char *buf, int len, double timeout ) { +int XX_httplib_pull( FILE *fp, struct httplib_connection *conn, char *buf, int len, double timeout ) { int nread; int err; diff --git a/src/httplib_pull_all.c b/src/httplib_pull_all.c index ebe55ab0..5cf2f931 100644 --- a/src/httplib_pull_all.c +++ b/src/httplib_pull_all.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int XX_httplib_pull_all( FILE *fp, struct mg_connection *conn, char *buf, int len ) { +int XX_httplib_pull_all( FILE *fp, struct httplib_connection *conn, char *buf, int len ) { int n; int nread = 0; diff --git a/src/httplib_push_all.c b/src/httplib_push_all.c index 28e101a4..dec22a38 100644 --- a/src/httplib_push_all.c +++ b/src/httplib_push_all.c @@ -28,7 +28,7 @@ /* Write data to the IO channel - opened file descriptor, socket or SSL * descriptor. Return number of bytes written. */ -static int push(struct mg_context *ctx, FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int len, double timeout) { +static int push(struct httplib_context *ctx, FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int len, double timeout) { struct timespec start; struct timespec now; @@ -113,7 +113,7 @@ static int push(struct mg_context *ctx, FILE *fp, SOCKET sock, SSL *ssl, const c } -int64_t XX_httplib_push_all(struct mg_context *ctx, FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int64_t len) { +int64_t XX_httplib_push_all(struct httplib_context *ctx, FILE *fp, SOCKET sock, SSL *ssl, const char *buf, int64_t len) { double timeout = -1.0; int64_t n; diff --git a/src/httplib_put_dir.c b/src/httplib_put_dir.c index 101217ea..94c7940a 100644 --- a/src/httplib_put_dir.c +++ b/src/httplib_put_dir.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* - * int XX_httplib_put_dir( struct mg_connection *conn, const char *path ); + * int XX_httplib_put_dir( struct httplib_connection *conn, const char *path ); * * The function XX_httplib_put_dir() creates a directory mentioned in a PUT * request including all intermediate subdirectories. The following values can @@ -36,7 +36,7 @@ * Return -2 if path can not be created. */ -int XX_httplib_put_dir( struct mg_connection *conn, const char *path ) { +int XX_httplib_put_dir( struct httplib_connection *conn, const char *path ) { char buf[PATH_MAX]; const char *s; diff --git a/src/httplib_put_file.c b/src/httplib_put_file.c index 1e35118f..765fcdb7 100644 --- a/src/httplib_put_file.c +++ b/src/httplib_put_file.c @@ -26,7 +26,7 @@ #include "httplib_utils.h" /* - * void XX_httplib_put_file( struct mg_connection *conn, const char *path ); + * void XX_httplib_put_file( struct httplib_connection *conn, const char *path ); * * The function XX_httplib_put_file() processes a file PUT request coming from * a remote client. @@ -34,7 +34,7 @@ #if !defined(NO_FILES) -void XX_httplib_put_file( struct mg_connection *conn, const char *path ) { +void XX_httplib_put_file( struct httplib_connection *conn, const char *path ) { struct file file = STRUCT_FILE_INITIALIZER; const char *range; @@ -86,9 +86,9 @@ void XX_httplib_put_file( struct mg_connection *conn, const char *path ) { if (rc == 0) { /* XX_httplib_put_dir returns 0 if path is a directory */ XX_httplib_gmt_time_string(date, sizeof(date), &curtime); - mg_printf(conn, "HTTP/1.1 %d %s\r\n", conn->status_code, mg_get_response_code_text(NULL, conn->status_code)); + httplib_printf(conn, "HTTP/1.1 %d %s\r\n", conn->status_code, httplib_get_response_code_text(NULL, conn->status_code)); XX_httplib_send_no_cache_header(conn); - mg_printf(conn, "Date: %s\r\n" "Content-Length: 0\r\n" "Connection: %s\r\n\r\n", date, XX_httplib_suggest_connection_header(conn)); + httplib_printf(conn, "Date: %s\r\n" "Content-Length: 0\r\n" "Connection: %s\r\n\r\n", date, XX_httplib_suggest_connection_header(conn)); /* Request to create a directory has been fulfilled successfully. * No need to put a file. */ @@ -115,7 +115,7 @@ void XX_httplib_put_file( struct mg_connection *conn, const char *path ) { } XX_httplib_fclose_on_exec(&file, conn); - range = mg_get_header(conn, "Content-Range"); + range = httplib_get_header(conn, "Content-Range"); r1 = r2 = 0; if (range != NULL && XX_httplib_parse_range_header(range, &r1, &r2) > 0) { conn->status_code = 206; /* Partial content */ @@ -131,9 +131,9 @@ void XX_httplib_put_file( struct mg_connection *conn, const char *path ) { } XX_httplib_gmt_time_string(date, sizeof(date), &curtime); - mg_printf(conn, "HTTP/1.1 %d %s\r\n", conn->status_code, mg_get_response_code_text(NULL, conn->status_code)); + httplib_printf(conn, "HTTP/1.1 %d %s\r\n", conn->status_code, httplib_get_response_code_text(NULL, conn->status_code)); XX_httplib_send_no_cache_header(conn); - mg_printf(conn, "Date: %s\r\n" "Content-Length: 0\r\n" "Connection: %s\r\n\r\n", date, XX_httplib_suggest_connection_header(conn)); + httplib_printf(conn, "Date: %s\r\n" "Content-Length: 0\r\n" "Connection: %s\r\n\r\n", date, XX_httplib_suggest_connection_header(conn)); XX_httplib_fclose(&file); diff --git a/src/httplib_read.c b/src/httplib_read.c index f0c1fb6e..5e840b74 100644 --- a/src/httplib_read.c +++ b/src/httplib_read.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -static int mg_read_inner(struct mg_connection *conn, void *buf, size_t len) { +static int httplib_read_inner(struct httplib_connection *conn, void *buf, size_t len) { int64_t n; int64_t buffered_len; @@ -77,22 +77,22 @@ static int mg_read_inner(struct mg_connection *conn, void *buf, size_t len) { } return (int)nread; -} /* mg_read_inner */ +} /* httplib_read_inner */ -static char mg_getc(struct mg_connection *conn) { +static char httplib_getc(struct httplib_connection *conn) { char c; if (conn == NULL) return 0; conn->content_len++; - if (mg_read_inner(conn, &c, 1) <= 0) return (char)0; + if (httplib_read_inner(conn, &c, 1) <= 0) return (char)0; return c; -} /* mg_getc */ +} /* httplib_getc */ -int mg_read(struct mg_connection *conn, void *buf, size_t len) { +int httplib_read(struct httplib_connection *conn, void *buf, size_t len) { if (len > INT_MAX) len = INT_MAX; @@ -116,8 +116,7 @@ int mg_read(struct mg_connection *conn, void *buf, size_t len) { : (conn->chunk_remainder)); conn->content_len += (int)read_now; - read_ret = - mg_read_inner(conn, (char *)buf + all_read, read_now); + read_ret = httplib_read_inner(conn, (char *)buf + all_read, read_now); all_read += (size_t)read_ret; conn->chunk_remainder -= read_now; @@ -126,7 +125,7 @@ int mg_read(struct mg_connection *conn, void *buf, size_t len) { if (conn->chunk_remainder == 0) { /* the rest of the data in the current chunk has been read */ - if ((mg_getc(conn) != '\r') || (mg_getc(conn) != '\n')) { + if ((httplib_getc(conn) != '\r') || (httplib_getc(conn) != '\n')) { /* Protocol violation */ return -1; } @@ -140,7 +139,7 @@ int mg_read(struct mg_connection *conn, void *buf, size_t len) { unsigned long chunkSize = 0; for (i = 0; i < ((int)sizeof(lenbuf) - 1); i++) { - lenbuf[i] = mg_getc(conn); + lenbuf[i] = httplib_getc(conn); if (i > 0 && lenbuf[i] == '\r' && lenbuf[i - 1] != '\r') { continue; } @@ -172,6 +171,6 @@ int mg_read(struct mg_connection *conn, void *buf, size_t len) { return (int)all_read; } - return mg_read_inner(conn, buf, len); + return httplib_read_inner(conn, buf, len); -} /* mg_read */ +} /* httplib_read */ diff --git a/src/httplib_read_auth_file.c b/src/httplib_read_auth_file.c index 62553e4d..4271b927 100644 --- a/src/httplib_read_auth_file.c +++ b/src/httplib_read_auth_file.c @@ -59,19 +59,19 @@ int XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct is_authorized = XX_httplib_read_auth_file(&fp, workdata); XX_httplib_fclose(&fp); } else { - mg_cry(workdata->conn, "%s: cannot open authorization file: %s", __func__, workdata->buf); + httplib_cry(workdata->conn, "%s: cannot open authorization file: %s", __func__, workdata->buf); } continue; } /* everything is invalid for the moment (might change in the * future) */ - mg_cry(workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf); + httplib_cry(workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf); continue; } workdata->f_domain = strchr(workdata->f_user, ':'); if (workdata->f_domain == NULL) { - mg_cry(workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf); + httplib_cry(workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf); continue; } *(workdata->f_domain) = 0; @@ -79,7 +79,7 @@ int XX_httplib_read_auth_file( struct file *filep, struct read_auth_file_struct workdata->f_ha1 = strchr(workdata->f_domain, ':'); if (workdata->f_ha1 == NULL) { - mg_cry(workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf); + httplib_cry(workdata->conn, "%s: syntax error in authorization file: %s", __func__, workdata->buf); continue; } *(workdata->f_ha1) = 0; diff --git a/src/httplib_read_request.c b/src/httplib_read_request.c index 3a462f88..452f5244 100644 --- a/src/httplib_read_request.c +++ b/src/httplib_read_request.c @@ -26,7 +26,7 @@ #include "httplib_utils.h" /* - * int XX_httplib_read_request( FILE *fp, struct mg_connection *conn, char *buf, int bufsiz, int *nread ); + * int XX_httplib_read_request( FILE *fp, struct httplib_connection *conn, char *buf, int bufsiz, int *nread ); * * The function XX_httplib_read_request() keeps reading the input (which can * either be an opened file descriptor, a socket sock or an SSL descriptor ssl) @@ -36,14 +36,14 @@ * is incremented by the number of bytes read. */ -int XX_httplib_read_request( FILE *fp, struct mg_connection *conn, char *buf, int bufsiz, int *nread ) { +int XX_httplib_read_request( FILE *fp, struct httplib_connection *conn, char *buf, int bufsiz, int *nread ) { int request_len; int n = 0; struct timespec last_action_time; double request_timeout; - if (!conn) return 0; + if ( conn == NULL ) return 0; memset(&last_action_time, 0, sizeof(last_action_time)); diff --git a/src/httplib_read_websocket.c b/src/httplib_read_websocket.c index 480e2983..d7d241a1 100644 --- a/src/httplib_read_websocket.c +++ b/src/httplib_read_websocket.c @@ -26,14 +26,14 @@ #include "httplib_memory.h" /* - * void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_handler ws_data_handler, void *calback_data ); + * void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websocket_data_handler ws_data_handler, void *calback_data ); * * The function XX_httplib_read_websocket() reads from a websocket connection. */ #if defined(USE_WEBSOCKET) -void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_handler ws_data_handler, void *callback_data ) { +void XX_httplib_read_websocket( struct httplib_connection *conn, httplib_websocket_data_handler ws_data_handler, void *callback_data ) { /* Pointer to the beginning of the portion of the incoming websocket * message queue. @@ -105,7 +105,7 @@ void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_ha if (data == NULL) { /* Allocation failed, exit the loop and then close the * connection */ - mg_cry(conn, "websocket out of memory; closing connection"); + httplib_cry(conn, "websocket out of memory; closing connection"); break; } } @@ -132,7 +132,7 @@ void XX_httplib_read_websocket( struct mg_connection *conn, mg_websocket_data_ha len += (size_t)n; } if (error) { - mg_cry(conn, "Websocket pull failed; closing connection"); + httplib_cry(conn, "Websocket pull failed; closing connection"); break; } conn->data_len = conn->request_len; diff --git a/src/httplib_redirect_to_https_port.c b/src/httplib_redirect_to_https_port.c index 929ae7a3..5d8c21dc 100644 --- a/src/httplib_redirect_to_https_port.c +++ b/src/httplib_redirect_to_https_port.c @@ -26,19 +26,19 @@ #include "httplib_string.h" /* - * void XX_httplib_redirect_to_https_port( struct mg_connection *conn, int ssl_index ); + * void XX_httplib_redirect_to_https_port( struct httplib_connection *conn, int ssl_index ); * * The function XX_httplib_redirect_to_https_port() redirects a request to an * encrypted connection over HTTPS. */ -void XX_httplib_redirect_to_https_port( struct mg_connection *conn, int ssl_index ) { +void XX_httplib_redirect_to_https_port( struct httplib_connection *conn, int ssl_index ) { char host[1025]; const char *host_header; size_t hostlen; - host_header = mg_get_header(conn, "Host"); + host_header = httplib_get_header(conn, "Host"); hostlen = sizeof(host); if (host_header != NULL) { char *pos; @@ -55,7 +55,7 @@ void XX_httplib_redirect_to_https_port( struct mg_connection *conn, int ssl_inde /* Send host, port, uri and (if it exists) ?query_string */ if (conn) { - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 302 Found\r\nLocation: https://%s:%d%s%s%s\r\n\r\n", host, #if defined(USE_IPV6) diff --git a/src/httplib_refresh_trust.c b/src/httplib_refresh_trust.c index 7a2917e4..b2d3dfc0 100644 --- a/src/httplib_refresh_trust.c +++ b/src/httplib_refresh_trust.c @@ -27,7 +27,7 @@ #include "httplib_utils.h" /* - * int XX_httplib_refresh_trust( struct mg_connection *conn ); + * int XX_httplib_refresh_trust( struct httplib_connection *conn ); * * The function XX_httplib_refresh_trust() is used to reload a certificate if * it only has a short trust span. @@ -35,7 +35,7 @@ #if !defined(NO_SSL) -int XX_httplib_refresh_trust( struct mg_connection *conn ) { +int XX_httplib_refresh_trust( struct httplib_connection *conn ) { static int reload_lock = 0; static long int data_check = 0; @@ -59,7 +59,7 @@ int XX_httplib_refresh_trust( struct mg_connection *conn ) { should_verify_peer = (conn->ctx->config[SSL_DO_VERIFY_PEER] != NULL) - && (mg_strcasecmp(conn->ctx->config[SSL_DO_VERIFY_PEER], "yes") + && (httplib_strcasecmp(conn->ctx->config[SSL_DO_VERIFY_PEER], "yes") == 0); if (should_verify_peer) { @@ -67,7 +67,7 @@ int XX_httplib_refresh_trust( struct mg_connection *conn ) { char *ca_file = conn->ctx->config[SSL_CA_FILE]; if (SSL_CTX_load_verify_locations(conn->ctx->ssl_ctx, ca_file, ca_path) != 1) { - mg_cry( XX_httplib_fc(conn->ctx), + httplib_cry( XX_httplib_fc(conn->ctx), "SSL_CTX_load_verify_locations error: %s " "ssl_verify_peer requires setting " "either ssl_ca_path or ssl_ca_file. Is any of them " diff --git a/src/httplib_remove.c b/src/httplib_remove.c index 098a1199..7b48d53b 100644 --- a/src/httplib_remove.c +++ b/src/httplib_remove.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int XX_httplib_remove( const struct mg_connection *conn, const char *path ) { +int XX_httplib_remove( const struct httplib_connection *conn, const char *path ) { #if defined(_WIN32) diff --git a/src/httplib_remove_bad_file.c b/src/httplib_remove_bad_file.c index 5e4e5872..afa04cbb 100644 --- a/src/httplib_remove_bad_file.c +++ b/src/httplib_remove_bad_file.c @@ -25,16 +25,16 @@ #include "httplib_main.h" /* - * void XX_httplib_remove_bad_file( const struct mg_connection *conn, const char *path ); + * void XX_httplib_remove_bad_file( const struct httplib_connection *conn, const char *path ); * * The function XX_httplib_remove_bad_file() removes an invalid file and throws * an error message if this does not succeed. */ -void XX_httplib_remove_bad_file( const struct mg_connection *conn, const char *path ) { +void XX_httplib_remove_bad_file( const struct httplib_connection *conn, const char *path ) { int r = XX_httplib_remove( conn, path ); - if (r != 0) mg_cry(conn, "%s: Cannot remove invalid file %s", __func__, path); + if (r != 0) httplib_cry(conn, "%s: Cannot remove invalid file %s", __func__, path); } /* XX_httplib_remove_bad_file */ diff --git a/src/httplib_remove_directory.c b/src/httplib_remove_directory.c index e9d34196..34e7cc3a 100644 --- a/src/httplib_remove_directory.c +++ b/src/httplib_remove_directory.c @@ -25,7 +25,7 @@ #include "httplib_main.h" #include "httplib_string.h" -int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ) { +int XX_httplib_remove_directory( struct httplib_connection *conn, const char *dir ) { char path[PATH_MAX]; struct dirent *dp; @@ -60,7 +60,7 @@ int XX_httplib_remove_directory( struct mg_connection *conn, const char *dir ) { } if (!XX_httplib_stat(conn, path, &de.file)) { - mg_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); + httplib_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); ok = 0; } if (de.file.membuf == NULL) { diff --git a/src/httplib_reset_per_request_attributes.c b/src/httplib_reset_per_request_attributes.c index abcca02d..1fc4730f 100644 --- a/src/httplib_reset_per_request_attributes.c +++ b/src/httplib_reset_per_request_attributes.c @@ -25,13 +25,13 @@ #include "httplib_main.h" /* - * void XX_httplib_reset_per_request_attributes( struct mg_connection *conn ); + * void XX_httplib_reset_per_request_attributes( struct httplib_connection *conn ); * * The function XX_httplib_reset_per_request_attributes() resets the request * attributes of a connection. */ -void XX_httplib_reset_per_request_attributes( struct mg_connection *conn ) { +void XX_httplib_reset_per_request_attributes( struct httplib_connection *conn ) { if ( conn == NULL ) return; diff --git a/src/httplib_scan_directory.c b/src/httplib_scan_directory.c index 39327da4..863b02e1 100644 --- a/src/httplib_scan_directory.c +++ b/src/httplib_scan_directory.c @@ -25,7 +25,7 @@ #include "httplib_main.h" #include "httplib_string.h" -int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void *data, void (*cb)(struct de *, void *) ) { +int XX_httplib_scan_directory( struct httplib_connection *conn, const char *dir, void *data, void (*cb)(struct de *, void *) ) { char path[PATH_MAX]; struct dirent *dp; @@ -57,7 +57,7 @@ int XX_httplib_scan_directory( struct mg_connection *conn, const char *dir, void } if (!XX_httplib_stat(conn, path, &de.file)) { - mg_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); + httplib_cry(conn, "%s: XX_httplib_stat(%s) failed: %s", __func__, path, strerror(ERRNO)); } de.file_name = dp->d_name; cb(&de, data); diff --git a/src/httplib_send_authorization_request.c b/src/httplib_send_authorization_request.c index ce5bf6aa..03f6ffe9 100644 --- a/src/httplib_send_authorization_request.c +++ b/src/httplib_send_authorization_request.c @@ -26,7 +26,7 @@ #include "httplib_pthread.h" #include "httplib_utils.h" -void XX_httplib_send_authorization_request( struct mg_connection *conn ) { +void XX_httplib_send_authorization_request( struct httplib_connection *conn ) { char date[64]; time_t curtime; @@ -48,9 +48,9 @@ void XX_httplib_send_authorization_request( struct mg_connection *conn ) { XX_httplib_gmt_time_string(date, sizeof(date), &curtime); - mg_printf(conn, "HTTP/1.1 401 Unauthorized\r\n"); + httplib_printf(conn, "HTTP/1.1 401 Unauthorized\r\n"); XX_httplib_send_no_cache_header(conn); - mg_printf(conn, + httplib_printf(conn, "Date: %s\r\n" "Connection: %s\r\n" "Content-Length: 0\r\n" diff --git a/src/httplib_send_file.c b/src/httplib_send_file.c index 2f9fd224..bf61291c 100644 --- a/src/httplib_send_file.c +++ b/src/httplib_send_file.c @@ -24,28 +24,28 @@ #include "httplib_main.h" -void mg_send_file( struct mg_connection *conn, const char *path ) { +void httplib_send_file( struct httplib_connection *conn, const char *path ) { - mg_send_mime_file( conn, path, NULL ); + httplib_send_mime_file( conn, path, NULL ); -} /* mg_send_file */ +} /* httplib_send_file */ -void mg_send_mime_file( struct mg_connection *conn, const char *path, const char *mime_type ) { +void httplib_send_mime_file( struct httplib_connection *conn, const char *path, const char *mime_type ) { - mg_send_mime_file2( conn, path, mime_type, NULL ); + httplib_send_mime_file2( conn, path, mime_type, NULL ); -} /* mg_send_mime_file */ +} /* httplib_send_mime_file */ -void mg_send_mime_file2( struct mg_connection *conn, const char *path, const char *mime_type, const char *additional_headers ) { +void httplib_send_mime_file2( struct httplib_connection *conn, const char *path, const char *mime_type, const char *additional_headers ) { struct file file = STRUCT_FILE_INITIALIZER; if (XX_httplib_stat(conn, path, &file)) { if (file.is_directory) { if ( conn == NULL ) return; - if (!mg_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes")) { + if (!httplib_strcasecmp(conn->ctx->config[ENABLE_DIRECTORY_LISTING], "yes")) { XX_httplib_handle_directory_request(conn, path); } else { XX_httplib_send_http_error(conn, 403, "%s", "Error: Directory listing denied"); @@ -55,4 +55,4 @@ void mg_send_mime_file2( struct mg_connection *conn, const char *path, const cha } } else XX_httplib_send_http_error(conn, 404, "%s", "Error: File not found"); -} /* mg_send_mime_file2 */ +} /* httplib_send_mime_file2 */ diff --git a/src/httplib_send_file_data.c b/src/httplib_send_file_data.c index 60299b24..460dc7c2 100644 --- a/src/httplib_send_file_data.c +++ b/src/httplib_send_file_data.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* Send len bytes from the opened file to the client. */ -void XX_httplib_send_file_data( struct mg_connection *conn, struct file *filep, int64_t offset, int64_t len ) { +void XX_httplib_send_file_data( struct httplib_connection *conn, struct file *filep, int64_t offset, int64_t len ) { char buf[MG_BUF_LEN]; int to_read; @@ -44,14 +44,13 @@ void XX_httplib_send_file_data( struct mg_connection *conn, struct file *filep, if (len > size - offset) { len = size - offset; } - mg_write(conn, filep->membuf + offset, (size_t)len); + httplib_write(conn, filep->membuf + offset, (size_t)len); } else if (len > 0 && filep->fp != NULL) { /* file stored on disk */ #if defined(__linux__) /* sendfile is only available for Linux */ if ((conn->ssl == 0) && (conn->throttle == 0) - && (!mg_strcasecmp(conn->ctx->config[ALLOW_SENDFILE_CALL], - "yes"))) { + && (!httplib_strcasecmp(conn->ctx->config[ALLOW_SENDFILE_CALL], "yes"))) { off_t sf_offs = (off_t)offset; ssize_t sf_sent; int sf_file = fileno(filep->fp); @@ -93,7 +92,7 @@ void XX_httplib_send_file_data( struct mg_connection *conn, struct file *filep, } #endif if ((offset > 0) && (fseeko(filep->fp, offset, SEEK_SET) != 0)) { - mg_cry(conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO)); + httplib_cry(conn, "%s: fseeko() failed: %s", __func__, strerror(ERRNO)); XX_httplib_send_http_error( conn, 500, "%s", "Error: Unable to access file at requested position."); } else { while (len > 0) { @@ -107,7 +106,7 @@ void XX_httplib_send_file_data( struct mg_connection *conn, struct file *filep, if ((num_read = (int)fread(buf, 1, (size_t)to_read, filep->fp)) <= 0) break; /* Send read bytes to the client, exit the loop on error */ - if ((num_written = mg_write(conn, buf, (size_t)num_read)) != num_read) break; + if ((num_written = httplib_write(conn, buf, (size_t)num_read)) != num_read) break; /* Both read and were successful, adjust counters */ conn->num_bytes_sent += num_written; diff --git a/src/httplib_send_http_error.c b/src/httplib_send_http_error.c index 115d47fa..d33eb4d8 100644 --- a/src/httplib_send_http_error.c +++ b/src/httplib_send_http_error.c @@ -26,7 +26,7 @@ #include "httplib_string.h" #include "httplib_utils.h" -void XX_httplib_send_http_error( struct mg_connection *conn, int status, const char *fmt, ... ) { +void XX_httplib_send_http_error( struct httplib_connection *conn, int status, const char *fmt, ... ) { char buf[MG_BUF_LEN]; va_list ap; @@ -42,7 +42,7 @@ void XX_httplib_send_http_error( struct mg_connection *conn, int status, const c struct file error_page_file = STRUCT_FILE_INITIALIZER; const char *error_page_file_ext, *tstr; - const char *status_text = mg_get_response_code_text(conn, status); + const char *status_text = httplib_get_response_code_text(conn, status); if (conn == NULL) return; @@ -107,20 +107,20 @@ void XX_httplib_send_http_error( struct mg_connection *conn, int status, const c has_body = (status > 199 && status != 204 && status != 304); conn->must_close = 1; - mg_printf(conn, "HTTP/1.1 %d %s\r\n", status, status_text); + httplib_printf(conn, "HTTP/1.1 %d %s\r\n", status, status_text); XX_httplib_send_no_cache_header(conn); - if (has_body) mg_printf(conn, "%s", "Content-Type: text/plain; charset=utf-8\r\n"); - mg_printf(conn, "Date: %s\r\n" "Connection: close\r\n\r\n", date); + if (has_body) httplib_printf(conn, "%s", "Content-Type: text/plain; charset=utf-8\r\n"); + httplib_printf(conn, "Date: %s\r\n" "Connection: close\r\n\r\n", date); /* Errors 1xx, 204 and 304 MUST NOT send a body */ if (has_body) { - mg_printf(conn, "Error %d: %s\n", status, status_text); + httplib_printf(conn, "Error %d: %s\n", status, status_text); if (fmt != NULL) { va_start(ap, fmt); XX_httplib_vsnprintf(conn, NULL, buf, sizeof(buf), fmt, ap); va_end(ap); - mg_write(conn, buf, strlen(buf)); + httplib_write(conn, buf, strlen(buf)); } } else { diff --git a/src/httplib_send_no_cache_header.c b/src/httplib_send_no_cache_header.c index d587c4c2..ad21f0d6 100644 --- a/src/httplib_send_no_cache_header.c +++ b/src/httplib_send_no_cache_header.c @@ -24,10 +24,10 @@ #include "httplib_main.h" -int XX_httplib_send_no_cache_header( struct mg_connection *conn ) { +int XX_httplib_send_no_cache_header( struct httplib_connection *conn ) { /* Send all current and obsolete cache opt-out directives. */ - return mg_printf(conn, + return httplib_printf(conn, "Cache-Control: no-cache, no-store, " "must-revalidate, private, max-age=0\r\n" "Pragma: no-cache\r\n" diff --git a/src/httplib_send_options.c b/src/httplib_send_options.c index d7e2d4c5..fd984d4b 100644 --- a/src/httplib_send_options.c +++ b/src/httplib_send_options.c @@ -26,7 +26,7 @@ #include "httplib_utils.h" /* - * void XX_httplib_send_options( struct mg_connection *conn ); + * void XX_httplib_send_options( struct httplib_connection *conn ); * * The function XX_httplib_send_options() sends a list of allowed options a * client can use to connect to the server. @@ -34,7 +34,7 @@ #if !defined(NO_FILES) -void XX_httplib_send_options( struct mg_connection *conn ) { +void XX_httplib_send_options( struct httplib_connection *conn ) { char date[64]; time_t curtime; @@ -46,7 +46,7 @@ void XX_httplib_send_options( struct mg_connection *conn ) { conn->must_close = 1; XX_httplib_gmt_time_string( date, sizeof(date), &curtime ); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Date: %s\r\n" /* TODO: "Cache-Control" (?) */ diff --git a/src/httplib_send_static_cache_header.c b/src/httplib_send_static_cache_header.c index 39bf02ed..35763ad8 100644 --- a/src/httplib_send_static_cache_header.c +++ b/src/httplib_send_static_cache_header.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -int XX_httplib_send_static_cache_header(struct mg_connection *conn) { +int XX_httplib_send_static_cache_header(struct httplib_connection *conn) { #if !defined(NO_CACHING) /* Read the server config to check how long a file may be cached. @@ -46,7 +46,7 @@ int XX_httplib_send_static_cache_header(struct mg_connection *conn) { * year to 31622400 seconds. For the moment, we just send whatever has * been configured, still the behavior for >1 year should be considered * as undefined. */ - return mg_printf(conn, "Cache-Control: max-age=%u\r\n", (unsigned)max_age); + return httplib_printf(conn, "Cache-Control: max-age=%u\r\n", (unsigned)max_age); #else /* NO_CACHING */ return XX_httplib_send_no_cache_header(conn); #endif /* !NO_CACHING */ diff --git a/src/httplib_send_websocket_handshake.c b/src/httplib_send_websocket_handshake.c index 91e9d57c..6c799112 100644 --- a/src/httplib_send_websocket_handshake.c +++ b/src/httplib_send_websocket_handshake.c @@ -27,7 +27,7 @@ #include "httplib_utils.h" /* - * int XX_httplib_send_websocket_handshake( struct mg_connection *conn, const char *websock_key ); + * int XX_httplib_send_websocket_handshake( struct httplib_connection *conn, const char *websock_key ); * * The function XX_httplib_send_websocket_handshake() sends a handshake over * a websocket connection. @@ -35,7 +35,7 @@ #if defined(USE_WEBSOCKET) -int XX_httplib_send_websocket_handshake( struct mg_connection *conn, const char *websock_key ) { +int XX_httplib_send_websocket_handshake( struct httplib_connection *conn, const char *websock_key ) { static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; const char *protocol = NULL; @@ -57,13 +57,13 @@ int XX_httplib_send_websocket_handshake( struct mg_connection *conn, const char SHA1Final((unsigned char *)sha, &sha_ctx); XX_httplib_base64_encode((unsigned char *)sha, sizeof(sha), b64_sha); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 101 Switching Protocols\r\n" "Upgrade: websocket\r\n" "Connection: Upgrade\r\n" "Sec-WebSocket-Accept: %s\r\n", b64_sha); - protocol = mg_get_header(conn, "Sec-WebSocket-Protocol"); + protocol = httplib_get_header(conn, "Sec-WebSocket-Protocol"); if (protocol) { /* The protocol is a comma seperated list of names. */ /* The server must only return one value from this list. */ @@ -71,7 +71,7 @@ int XX_httplib_send_websocket_handshake( struct mg_connection *conn, const char const char *sep = strchr(protocol, ','); if (sep == NULL) { /* Just a single protocol -> accept it. */ - mg_printf(conn, "Sec-WebSocket-Protocol: %s\r\n\r\n", protocol); + httplib_printf(conn, "Sec-WebSocket-Protocol: %s\r\n\r\n", protocol); } else { /* Multiple protocols -> accept the first one. */ /* This is just a quick fix if the client offers multiple @@ -83,12 +83,12 @@ int XX_httplib_send_websocket_handshake( struct mg_connection *conn, const char * handshake by not returning a Sec-Websocket-Protocol header if * no subprotocol is acceptable. */ - mg_printf(conn, "Sec-WebSocket-Protocol: %.*s\r\n\r\n", (int)(sep - protocol), protocol); + httplib_printf(conn, "Sec-WebSocket-Protocol: %.*s\r\n\r\n", (int)(sep - protocol), protocol); } /* TODO: Real subprotocol negotiation instead of just taking the first * websocket subprotocol suggested by the client. */ } - else mg_printf(conn, "%s", "\r\n"); + else httplib_printf(conn, "%s", "\r\n"); return 1; diff --git a/src/httplib_set_acl_option.c b/src/httplib_set_acl_option.c index fe75c12f..ef8daa10 100644 --- a/src/httplib_set_acl_option.c +++ b/src/httplib_set_acl_option.c @@ -25,12 +25,12 @@ #include "httplib_main.h" /* - * int XX_httplib_set_acl_option( struct mg_context *ctx ); + * int XX_httplib_set_acl_option( struct httplib_context *ctx ); * * The function XX_httplib_set_acl_option() sets the ACL option for a context. */ -int XX_httplib_set_acl_option( struct mg_context *ctx ) { +int XX_httplib_set_acl_option( struct httplib_context *ctx ) { return XX_httplib_check_acl( ctx, (uint32_t)0x7f000001UL ) != -1; diff --git a/src/httplib_set_auth_handler.c b/src/httplib_set_auth_handler.c index 71e94177..91f2f62b 100644 --- a/src/httplib_set_auth_handler.c +++ b/src/httplib_set_auth_handler.c @@ -25,14 +25,14 @@ #include "httplib_main.h" /* - * void mg_set_auth_handler( struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata ); + * void httplib_set_auth_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ); * - * The function mg_set_auth_handler() sets the callback function which handles + * The function httplib_set_auth_handler() sets the callback function which handles * authorization requests. */ -void mg_set_auth_handler( struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata ) { +void httplib_set_auth_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ) { XX_httplib_set_handler_type( ctx, uri, AUTH_HANDLER, (handler == NULL), NULL, NULL, NULL, NULL, NULL, handler, cbdata ); -} /* mg_set_auth_handler */ +} /* httplib_set_auth_handler */ diff --git a/src/httplib_set_close_on_exec.c b/src/httplib_set_close_on_exec.c index 65ff30cd..60ef6ee6 100644 --- a/src/httplib_set_close_on_exec.c +++ b/src/httplib_set_close_on_exec.c @@ -27,7 +27,7 @@ #if defined(_WIN32) /* conn parameter may be NULL */ -void XX_httplib_set_close_on_exec( SOCKET sock, struct mg_connection *conn ) { +void XX_httplib_set_close_on_exec( SOCKET sock, struct httplib_connection *conn ) { (void)conn; /* Unused. */ #if defined(_WIN32_WCE) @@ -42,10 +42,10 @@ void XX_httplib_set_close_on_exec( SOCKET sock, struct mg_connection *conn ) { #else /* conn may be NULL */ -void XX_httplib_set_close_on_exec( SOCKET fd, struct mg_connection *conn ) { +void XX_httplib_set_close_on_exec( SOCKET fd, struct httplib_connection *conn ) { if (fcntl(fd, F_SETFD, FD_CLOEXEC) != 0) { - if (conn) { mg_cry(conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO)); } + if (conn) { httplib_cry(conn, "%s: fcntl(F_SETFD FD_CLOEXEC) failed: %s", __func__, strerror(ERRNO)); } } } /* XX_httplib_set_close_on_exec */ diff --git a/src/httplib_set_gpass_option.c b/src/httplib_set_gpass_option.c index 5967c0ee..ab5e51e2 100644 --- a/src/httplib_set_gpass_option.c +++ b/src/httplib_set_gpass_option.c @@ -25,13 +25,13 @@ #include "httplib_main.h" /* - * int XX_httplib_set_gpass_option( struct mg_context *ctx ); + * int XX_httplib_set_gpass_option( struct httplib_context *ctx ); * * The function XX_httplib_set_gpass_option() sets the global password file * oprion for a context. */ -int XX_httplib_set_gpass_option( struct mg_context *ctx ) { +int XX_httplib_set_gpass_option( struct httplib_context *ctx ) { if ( ctx == NULL ) return 0; @@ -40,7 +40,7 @@ int XX_httplib_set_gpass_option( struct mg_context *ctx ) { if (path != NULL && !XX_httplib_stat( XX_httplib_fc(ctx), path, &file)) { - mg_cry( XX_httplib_fc(ctx), "Cannot open %s: %s", path, strerror(ERRNO)); + httplib_cry( XX_httplib_fc(ctx), "Cannot open %s: %s", path, strerror(ERRNO)); return 0; } return 1; diff --git a/src/httplib_set_handler_type.c b/src/httplib_set_handler_type.c index e2f7d191..11049f48 100644 --- a/src/httplib_set_handler_type.c +++ b/src/httplib_set_handler_type.c @@ -33,9 +33,9 @@ * sets callback handlers to uri's. */ -void XX_httplib_set_handler_type( struct mg_context *ctx, const char *uri, int handler_type, int is_delete_request, mg_request_handler handler, mg_websocket_connect_handler connect_handler, mg_websocket_ready_handler ready_handler, mg_websocket_data_handler data_handler, mg_websocket_close_handler close_handler, mg_authorization_handler auth_handler, void *cbdata ) { +void XX_httplib_set_handler_type( struct httplib_context *ctx, const char *uri, int handler_type, int is_delete_request, httplib_request_handler handler, httplib_websocket_connect_handler connect_handler, httplib_websocket_ready_handler ready_handler, httplib_websocket_data_handler data_handler, httplib_websocket_close_handler close_handler, httplib_authorization_handler auth_handler, void *cbdata ) { - struct mg_handler_info *tmp_rh, **lastref; + struct httplib_handler_info *tmp_rh, **lastref; size_t urilen = strlen(uri); if (handler_type == WEBSOCKET_HANDLER) { @@ -69,7 +69,7 @@ void XX_httplib_set_handler_type( struct mg_context *ctx, const char *uri, int h if ( ctx == NULL ) return; - mg_lock_context(ctx); + httplib_lock_context(ctx); /* first try to find an existing handler */ lastref = &(ctx->handlers); @@ -95,7 +95,7 @@ void XX_httplib_set_handler_type( struct mg_context *ctx, const char *uri, int h XX_httplib_free(tmp_rh->uri); XX_httplib_free(tmp_rh); } - mg_unlock_context(ctx); + httplib_unlock_context(ctx); return; } } @@ -105,22 +105,21 @@ void XX_httplib_set_handler_type( struct mg_context *ctx, const char *uri, int h if (is_delete_request) { /* no handler to set, this was a remove request to a non-existing * handler */ - mg_unlock_context(ctx); + httplib_unlock_context(ctx); return; } - tmp_rh = - (struct mg_handler_info *)XX_httplib_calloc(sizeof(struct mg_handler_info), 1); + tmp_rh = (struct httplib_handler_info *)XX_httplib_calloc(sizeof(struct httplib_handler_info), 1); if (tmp_rh == NULL) { - mg_unlock_context(ctx); - mg_cry( XX_httplib_fc(ctx), "%s", "Cannot create new request handler struct, OOM"); + httplib_unlock_context(ctx); + httplib_cry( XX_httplib_fc(ctx), "%s", "Cannot create new request handler struct, OOM"); return; } tmp_rh->uri = XX_httplib_strdup(uri); if (!tmp_rh->uri) { - mg_unlock_context(ctx); + httplib_unlock_context(ctx); XX_httplib_free(tmp_rh); - mg_cry( XX_httplib_fc(ctx), "%s", "Cannot create new request handler struct, OOM"); + httplib_cry( XX_httplib_fc(ctx), "%s", "Cannot create new request handler struct, OOM"); return; } tmp_rh->uri_len = urilen; @@ -139,6 +138,6 @@ void XX_httplib_set_handler_type( struct mg_context *ctx, const char *uri, int h tmp_rh->next = NULL; *lastref = tmp_rh; - mg_unlock_context(ctx); + httplib_unlock_context(ctx); } /* XX_httplib_set_handler_type */ diff --git a/src/httplib_set_ports_option.c b/src/httplib_set_ports_option.c index 1987ffdd..c7838464 100644 --- a/src/httplib_set_ports_option.c +++ b/src/httplib_set_ports_option.c @@ -31,13 +31,13 @@ static int parse_port_string( const struct vec *vec, struct socket *so, int *ip_ /* - * int XX_httplib_set_ports_option( struct mg_context *ctx ); + * int XX_httplib_set_ports_option( struct httplib_context *ctx ); * * The function XX_httplib_set_ports_option() set the port options for a * context. */ -int XX_httplib_set_ports_option( struct mg_context *ctx ) { +int XX_httplib_set_ports_option( struct httplib_context *ctx ) { const char *list; int on = 1; @@ -67,7 +67,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { portsTotal++; if (!parse_port_string(&vec, &so, &ip_version)) { - mg_cry( XX_httplib_fc(ctx), + httplib_cry( XX_httplib_fc(ctx), "%.*s: invalid port spec (entry %i). Expecting list of: %s", (int)vec.len, vec.ptr, @@ -79,7 +79,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { #if !defined(NO_SSL) if (so.is_ssl && ctx->ssl_ctx == NULL) { - mg_cry( XX_httplib_fc(ctx), "Cannot add SSL socket (entry %i). Is -ssl_certificate option set?", portsTotal); + httplib_cry( XX_httplib_fc(ctx), "Cannot add SSL socket (entry %i). Is -ssl_certificate option set?", portsTotal); continue; } #endif @@ -87,7 +87,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { if ((so.sock = socket(so.lsa.sa.sa_family, SOCK_STREAM, 6)) == INVALID_SOCKET) { - mg_cry( XX_httplib_fc(ctx), "cannot create socket (entry %i)", portsTotal); + httplib_cry( XX_httplib_fc(ctx), "cannot create socket (entry %i)", portsTotal); continue; } @@ -99,18 +99,18 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { * Windows might need a few seconds before * the same port can be used again in the * same process, so a short Sleep may be - * required between mg_stop and mg_start. + * required between httplib_stop and httplib_start. */ if (setsockopt(so.sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (SOCK_OPT_TYPE)&on, sizeof(on)) != 0) { /* Set reuse option, but don't abort on errors. */ - mg_cry( XX_httplib_fc(ctx), "cannot set socket option SO_EXCLUSIVEADDRUSE (entry %i)", portsTotal); + httplib_cry( XX_httplib_fc(ctx), "cannot set socket option SO_EXCLUSIVEADDRUSE (entry %i)", portsTotal); } #else if (setsockopt(so.sock, SOL_SOCKET, SO_REUSEADDR, (SOCK_OPT_TYPE)&on, sizeof(on)) != 0) { /* Set reuse option, but don't abort on errors. */ - mg_cry( XX_httplib_fc(ctx), "cannot set socket option SO_REUSEADDR (entry %i)", portsTotal); + httplib_cry( XX_httplib_fc(ctx), "cannot set socket option SO_REUSEADDR (entry %i)", portsTotal); } #endif @@ -121,11 +121,11 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { && setsockopt(so.sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&off, sizeof(off)) != 0) { /* Set IPv6 only option, but don't abort on errors. */ - mg_cry( XX_httplib_fc(ctx), "cannot set socket option IPV6_V6ONLY (entry %i)", portsTotal); + httplib_cry( XX_httplib_fc(ctx), "cannot set socket option IPV6_V6ONLY (entry %i)", portsTotal); } } #else - mg_cry( XX_httplib_fc(ctx), "IPv6 not available"); + httplib_cry( XX_httplib_fc(ctx), "IPv6 not available"); closesocket(so.sock); so.sock = INVALID_SOCKET; continue; @@ -136,7 +136,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { len = sizeof(so.lsa.sin); if (bind(so.sock, &so.lsa.sa, len) != 0) { - mg_cry( XX_httplib_fc(ctx), "cannot bind to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno)); + httplib_cry( XX_httplib_fc(ctx), "cannot bind to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno)); closesocket(so.sock); so.sock = INVALID_SOCKET; continue; @@ -147,7 +147,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { len = sizeof(so.lsa.sin6); if (bind(so.sock, &so.lsa.sa, len) != 0) { - mg_cry( XX_httplib_fc(ctx), "cannot bind to IPv6 %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno)); + httplib_cry( XX_httplib_fc(ctx), "cannot bind to IPv6 %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno)); closesocket(so.sock); so.sock = INVALID_SOCKET; continue; @@ -155,13 +155,13 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { } #endif else { - mg_cry( XX_httplib_fc(ctx), "cannot bind: address family not supported (entry %i)", portsTotal); + httplib_cry( XX_httplib_fc(ctx), "cannot bind: address family not supported (entry %i)", portsTotal); continue; } if (listen(so.sock, SOMAXCONN) != 0) { - mg_cry( XX_httplib_fc(ctx), "cannot listen to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno)); + httplib_cry( XX_httplib_fc(ctx), "cannot listen to %.*s: %d (%s)", (int)vec.len, vec.ptr, (int)ERRNO, strerror(errno)); closesocket(so.sock); so.sock = INVALID_SOCKET; continue; @@ -171,7 +171,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { || usa.sa.sa_family != so.lsa.sa.sa_family) { int err = (int)ERRNO; - mg_cry( XX_httplib_fc(ctx), "call to getsockname failed %.*s: %d (%s)", (int)vec.len, vec.ptr, err, strerror(errno)); + httplib_cry( XX_httplib_fc(ctx), "call to getsockname failed %.*s: %d (%s)", (int)vec.len, vec.ptr, err, strerror(errno)); closesocket(so.sock); so.sock = INVALID_SOCKET; continue; @@ -189,7 +189,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { if ((ptr = XX_httplib_realloc(ctx->listening_sockets, (ctx->num_listening_sockets + 1) * sizeof(ctx->listening_sockets[0]))) == NULL) { - mg_cry( XX_httplib_fc(ctx), "%s", "Out of memory"); + httplib_cry( XX_httplib_fc(ctx), "%s", "Out of memory"); closesocket(so.sock); so.sock = INVALID_SOCKET; continue; @@ -200,7 +200,7 @@ int XX_httplib_set_ports_option( struct mg_context *ctx ) { (ctx->num_listening_sockets + 1) * sizeof(ctx->listening_socket_fds[0]))) == NULL) { - mg_cry( XX_httplib_fc(ctx), "%s", "Out of memory"); + httplib_cry( XX_httplib_fc(ctx), "%s", "Out of memory"); closesocket(so.sock); so.sock = INVALID_SOCKET; XX_httplib_free(ptr); @@ -276,7 +276,7 @@ static int parse_port_string( const struct vec *vec, struct socket *so, int *ip_ } else if (sscanf(vec->ptr, "[%49[^]]]:%u%n", buf, &port, &len) == 2 && XX_httplib_inet_pton( AF_INET6, buf, &so->lsa.sin6, sizeof(so->lsa.sin6))) { /* IPv6 address, examples: see above */ - /* so->lsa.sin6.sin6_family = AF_INET6; already set by mg_inet_pton + /* so->lsa.sin6.sin6_family = AF_INET6; already set by httplib_inet_pton */ so->lsa.sin6.sin6_port = htons((uint16_t)port); *ip_version = 6; diff --git a/src/httplib_set_request_handler.c b/src/httplib_set_request_handler.c index 724ad0bd..e13a3f03 100644 --- a/src/httplib_set_request_handler.c +++ b/src/httplib_set_request_handler.c @@ -25,14 +25,14 @@ #include "httplib_main.h" /* - * void mg_set_request_handler( struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata ); + * void httplib_set_request_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ); * - * The function mg_set_request_handler() sets a request handler for a specific + * The function httplib_set_request_handler() sets a request handler for a specific * uri in a server context. */ -void mg_set_request_handler( struct mg_context *ctx, const char *uri, mg_request_handler handler, void *cbdata ) { +void httplib_set_request_handler( struct httplib_context *ctx, const char *uri, httplib_request_handler handler, void *cbdata ) { XX_httplib_set_handler_type( ctx, uri, REQUEST_HANDLER, (handler == NULL), handler, NULL, NULL, NULL, NULL, NULL, cbdata ); -} /* mg_set_request_handler */ +} /* httplib_set_request_handler */ diff --git a/src/httplib_set_ssl_option.c b/src/httplib_set_ssl_option.c index c97d3dbe..032fe951 100644 --- a/src/httplib_set_ssl_option.c +++ b/src/httplib_set_ssl_option.c @@ -28,14 +28,14 @@ static void *ssllib_dll_handle; /* Store the ssl library handle. */ /* - * int XX_httplib_set_ssl_option( struct mg_context *ctx ); + * int XX_httplib_set_ssl_option( struct httplib_context *ctx ); * * The function XX_httplib_set_ssl_option() loads the SSL library in a dynamic * way. */ #if !defined(NO_SSL) -int XX_httplib_set_ssl_option( struct mg_context *ctx ) { +int XX_httplib_set_ssl_option( struct httplib_context *ctx ) { const char *pem; int callback_ret; @@ -70,7 +70,7 @@ int XX_httplib_set_ssl_option( struct mg_context *ctx ) { SSL_load_error_strings(); if ((ctx->ssl_ctx = SSL_CTX_new(SSLv23_server_method())) == NULL) { - mg_cry( XX_httplib_fc(ctx), "SSL_CTX_new (server) error: %s", XX_httplib_ssl_error()); + httplib_cry( XX_httplib_fc(ctx), "SSL_CTX_new (server) error: %s", XX_httplib_ssl_error()); return 0; } @@ -88,7 +88,7 @@ int XX_httplib_set_ssl_option( struct mg_context *ctx ) { * If it returns 1, LibHTTP assumes the calback already did this. * If it returns -1, initializing ssl fails. */ if (callback_ret < 0) { - mg_cry( XX_httplib_fc(ctx), "SSL callback returned error: %i", callback_ret); + httplib_cry( XX_httplib_fc(ctx), "SSL callback returned error: %i", callback_ret); return 0; } if (callback_ret > 0) { @@ -117,18 +117,18 @@ int XX_httplib_set_ssl_option( struct mg_context *ctx ) { should_verify_peer = (ctx->config[SSL_DO_VERIFY_PEER] != NULL) - && (mg_strcasecmp(ctx->config[SSL_DO_VERIFY_PEER], "yes") == 0); + && (httplib_strcasecmp(ctx->config[SSL_DO_VERIFY_PEER], "yes") == 0); use_default_verify_paths = (ctx->config[SSL_DEFAULT_VERIFY_PATHS] != NULL) - && (mg_strcasecmp(ctx->config[SSL_DEFAULT_VERIFY_PATHS], "yes") == 0); + && (httplib_strcasecmp(ctx->config[SSL_DEFAULT_VERIFY_PATHS], "yes") == 0); if (should_verify_peer) { ca_path = ctx->config[SSL_CA_PATH]; ca_file = ctx->config[SSL_CA_FILE]; if (SSL_CTX_load_verify_locations(ctx->ssl_ctx, ca_file, ca_path) != 1) { - mg_cry( XX_httplib_fc(ctx), + httplib_cry( XX_httplib_fc(ctx), "SSL_CTX_load_verify_locations error: %s " "ssl_verify_peer requires setting " "either ssl_ca_path or ssl_ca_file. Is any of them " @@ -144,7 +144,7 @@ int XX_httplib_set_ssl_option( struct mg_context *ctx ) { if (use_default_verify_paths && SSL_CTX_set_default_verify_paths(ctx->ssl_ctx) != 1) { - mg_cry( XX_httplib_fc(ctx), "SSL_CTX_set_default_verify_paths error: %s", XX_httplib_ssl_error()); + httplib_cry( XX_httplib_fc(ctx), "SSL_CTX_set_default_verify_paths error: %s", XX_httplib_ssl_error()); return 0; } @@ -156,7 +156,7 @@ int XX_httplib_set_ssl_option( struct mg_context *ctx ) { if (ctx->config[SSL_CIPHER_LIST] != NULL) { if (SSL_CTX_set_cipher_list(ctx->ssl_ctx, ctx->config[SSL_CIPHER_LIST]) != 1) { - mg_cry( XX_httplib_fc(ctx), "SSL_CTX_set_cipher_list error: %s", XX_httplib_ssl_error()); + httplib_cry( XX_httplib_fc(ctx), "SSL_CTX_set_cipher_list error: %s", XX_httplib_ssl_error()); } } diff --git a/src/httplib_set_uid_option.c b/src/httplib_set_uid_option.c index 448884e7..31faa418 100644 --- a/src/httplib_set_uid_option.c +++ b/src/httplib_set_uid_option.c @@ -25,7 +25,7 @@ #include "httplib_main.h" /* - * int XX_httplib_set_uid_option( struct mg_contect *ctx ); + * int XX_httplib_set_uid_option( struct httplib_contect *ctx ); * * The function XX_httplib_set_uid_option() runs on systems which support it * the context in the security environment of a specific user. @@ -33,7 +33,7 @@ #if !defined(_WIN32) -int XX_httplib_set_uid_option( struct mg_context *ctx ) { +int XX_httplib_set_uid_option( struct httplib_context *ctx ) { struct passwd *pw; @@ -44,10 +44,10 @@ int XX_httplib_set_uid_option( struct mg_context *ctx ) { if (uid == NULL) success = 1; else { - if ( (pw = getpwnam(uid)) == NULL ) mg_cry( XX_httplib_fc(ctx), "%s: unknown user [%s]", __func__, uid ); - else if ( setgid(pw->pw_gid) == -1 ) mg_cry( XX_httplib_fc(ctx), "%s: setgid(%s): %s", __func__, uid, strerror(errno) ); - else if ( setgroups(0, NULL) ) mg_cry( XX_httplib_fc(ctx), "%s: setgroups(): %s", __func__, strerror(errno) ); - else if ( setuid(pw->pw_uid) == -1 ) mg_cry( XX_httplib_fc(ctx), "%s: setuid(%s): %s", __func__, uid, strerror(errno) ); + if ( (pw = getpwnam(uid)) == NULL ) httplib_cry( XX_httplib_fc(ctx), "%s: unknown user [%s]", __func__, uid ); + else if ( setgid(pw->pw_gid) == -1 ) httplib_cry( XX_httplib_fc(ctx), "%s: setgid(%s): %s", __func__, uid, strerror(errno) ); + else if ( setgroups(0, NULL) ) httplib_cry( XX_httplib_fc(ctx), "%s: setgroups(): %s", __func__, strerror(errno) ); + else if ( setuid(pw->pw_uid) == -1 ) httplib_cry( XX_httplib_fc(ctx), "%s: setuid(%s): %s", __func__, uid, strerror(errno) ); else success = 1; } diff --git a/src/httplib_set_user_connection_data.c b/src/httplib_set_user_connection_data.c index 9766c326..5c48dbb7 100644 --- a/src/httplib_set_user_connection_data.c +++ b/src/httplib_set_user_connection_data.c @@ -24,7 +24,7 @@ #include "httplib_main.h" -void mg_set_user_connection_data(struct mg_connection *conn, void *data) { +void httplib_set_user_connection_data(struct httplib_connection *conn, void *data) { if (conn != NULL) conn->request_info.conn_data = data; diff --git a/src/httplib_set_websocket_handler.c b/src/httplib_set_websocket_handler.c index cdc8e351..66a56ba8 100644 --- a/src/httplib_set_websocket_handler.c +++ b/src/httplib_set_websocket_handler.c @@ -25,15 +25,15 @@ #include "httplib_main.h" /* - * void mg_set_websocket_handler(); + * void httplib_set_websocket_handler(); * - * the function mg_set_websocket_handler() sets callback functions for the + * the function httplib_set_websocket_handler() sets callback functions for the * processing of events from a websocket. */ -void mg_set_websocket_handler( struct mg_context *ctx, const char *uri, mg_websocket_connect_handler connect_handler, mg_websocket_ready_handler ready_handler, mg_websocket_data_handler data_handler, mg_websocket_close_handler close_handler, void *cbdata ) { +void httplib_set_websocket_handler( struct httplib_context *ctx, const char *uri, httplib_websocket_connect_handler connect_handler, httplib_websocket_ready_handler ready_handler, httplib_websocket_data_handler data_handler, httplib_websocket_close_handler close_handler, void *cbdata ) { int is_delete_request = (connect_handler == NULL) && (ready_handler == NULL) && (data_handler == NULL) && (close_handler == NULL); XX_httplib_set_handler_type(ctx, uri, WEBSOCKET_HANDLER, is_delete_request, NULL, connect_handler, ready_handler, data_handler, close_handler, NULL, cbdata); -} /* mg_set_websocket_handler */ +} /* httplib_set_websocket_handler */ diff --git a/src/httplib_should_decode_url.c b/src/httplib_should_decode_url.c index 32e69136..2142de81 100644 --- a/src/httplib_should_decode_url.c +++ b/src/httplib_should_decode_url.c @@ -24,10 +24,10 @@ #include "httplib_main.h" -int XX_httplib_should_decode_url( const struct mg_connection *conn ) { +int XX_httplib_should_decode_url( const struct httplib_connection *conn ) { if ( conn == NULL || conn->ctx == NULL ) return 0; - return (mg_strcasecmp(conn->ctx->config[DECODE_URL], "yes") == 0); + return (httplib_strcasecmp(conn->ctx->config[DECODE_URL], "yes") == 0); } /* XX_httplib_should_decode_url */ diff --git a/src/httplib_should_keep_alive.c b/src/httplib_should_keep_alive.c index eab89aac..5d02bae9 100644 --- a/src/httplib_should_keep_alive.c +++ b/src/httplib_should_keep_alive.c @@ -27,13 +27,13 @@ /* HTTP 1.1 assumes keep alive if "Connection:" header is not set * This function must tolerate situations when connection info is not * set up, for example if request parsing failed. */ -int XX_httplib_should_keep_alive( const struct mg_connection *conn ) { +int XX_httplib_should_keep_alive( const struct httplib_connection *conn ) { if (conn != NULL) { const char *http_version = conn->request_info.http_version; - const char *header = mg_get_header(conn, "Connection"); + const char *header = httplib_get_header(conn, "Connection"); if (conn->must_close || conn->internal_error || conn->status_code == 401 - || mg_strcasecmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes") != 0 + || httplib_strcasecmp(conn->ctx->config[ENABLE_KEEP_ALIVE], "yes") != 0 || (header != NULL && !XX_httplib_header_has_option(header, "keep-alive")) || (header == NULL && http_version && 0 != strcmp(http_version, "1.1"))) { diff --git a/src/httplib_snprintf.c b/src/httplib_snprintf.c index 13822e52..d6c9bf08 100644 --- a/src/httplib_snprintf.c +++ b/src/httplib_snprintf.c @@ -25,7 +25,7 @@ #include "httplib_main.h" #include "httplib_string.h" -void XX_httplib_snprintf( const struct mg_connection *conn, int *truncated, char *buf, size_t buflen, const char *fmt, ... ) { +void XX_httplib_snprintf( const struct httplib_connection *conn, int *truncated, char *buf, size_t buflen, const char *fmt, ... ) { va_list ap; diff --git a/src/httplib_spawn_process.c b/src/httplib_spawn_process.c index d61587ad..430f41ab 100644 --- a/src/httplib_spawn_process.c +++ b/src/httplib_spawn_process.c @@ -49,7 +49,7 @@ static void trim_trailing_whitespaces( char *s ) { } /* trim_trailing_whitespaces */ -pid_t XX_httplib_spawn_process( struct mg_connection *conn, const char *prog, char *envblk, char *envp[], int fdin[2], int fdout[2], int fderr[2], const char *dir ) { +pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *prog, char *envblk, char *envp[], int fdin[2], int fdout[2], int fderr[2], const char *dir ) { HANDLE me; char *p; @@ -129,8 +129,7 @@ pid_t XX_httplib_spawn_process( struct mg_connection *conn, const char *prog, ch } if (CreateProcessA(NULL, cmdline, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP, envblk, NULL, &si, &pi) == 0) { - mg_cry( - conn, "%s: CreateProcess(%s): %ld", __func__, cmdline, (long)ERRNO); + httplib_cry( conn, "%s: CreateProcess(%s): %ld", __func__, cmdline, (long)ERRNO); pi.hProcess = (pid_t)-1; /* goto spawn_cleanup; */ } @@ -151,7 +150,7 @@ spawn_cleanup: #else #ifndef NO_CGI -pid_t XX_httplib_spawn_process( struct mg_connection *conn, const char *prog, char *envblk, char *envp[], int fdin[2], int fdout[2], int fderr[2], const char *dir ) { +pid_t XX_httplib_spawn_process( struct httplib_connection *conn, const char *prog, char *envblk, char *envp[], int fdin[2], int fdout[2], int fderr[2], const char *dir ) { pid_t pid; const char *interp; @@ -165,10 +164,10 @@ pid_t XX_httplib_spawn_process( struct mg_connection *conn, const char *prog, ch XX_httplib_send_http_error(conn, 500, "Error: Creating CGI process\nfork(): %s", strerror(ERRNO)); } else if (pid == 0) { /* Child */ - if ( chdir( dir ) != 0 ) mg_cry(conn, "%s: chdir(%s): %s", __func__, dir, strerror(ERRNO)); - else if ( dup2( fdin[0], 0 ) == -1 ) mg_cry(conn, "%s: dup2(%d, 0): %s", __func__, fdin[0], strerror(ERRNO)); - else if ( dup2( fdout[1], 1 ) == -1 ) mg_cry(conn, "%s: dup2(%d, 1): %s", __func__, fdout[1], strerror(ERRNO)); - else if ( dup2( fderr[1], 2 ) == -1 ) mg_cry(conn, "%s: dup2(%d, 2): %s", __func__, fderr[1], strerror(ERRNO)); + if ( chdir( dir ) != 0 ) httplib_cry(conn, "%s: chdir(%s): %s", __func__, dir, strerror(ERRNO)); + else if ( dup2( fdin[0], 0 ) == -1 ) httplib_cry(conn, "%s: dup2(%d, 0): %s", __func__, fdin[0], strerror(ERRNO)); + else if ( dup2( fdout[1], 1 ) == -1 ) httplib_cry(conn, "%s: dup2(%d, 1): %s", __func__, fdout[1], strerror(ERRNO)); + else if ( dup2( fderr[1], 2 ) == -1 ) httplib_cry(conn, "%s: dup2(%d, 2): %s", __func__, fderr[1], strerror(ERRNO)); else { /* Keep stderr and stdout in two different pipes. * Stdout will be sent back to the client, @@ -191,11 +190,11 @@ pid_t XX_httplib_spawn_process( struct mg_connection *conn, const char *prog, ch interp = conn->ctx->config[CGI_INTERPRETER]; if (interp == NULL) { - (void)execle(prog, prog, NULL, envp); - mg_cry(conn, "%s: execle(%s): %s", __func__, prog, strerror(ERRNO)); + execle(prog, prog, NULL, envp); + httplib_cry(conn, "%s: execle(%s): %s", __func__, prog, strerror(ERRNO)); } else { - (void)execle(interp, interp, prog, NULL, envp); - mg_cry(conn, "%s: execle(%s %s): %s", __func__, interp, prog, strerror(ERRNO)); + execle(interp, interp, prog, NULL, envp); + httplib_cry(conn, "%s: execle(%s %s): %s", __func__, interp, prog, strerror(ERRNO)); } } exit(EXIT_FAILURE); diff --git a/src/httplib_ssi.c b/src/httplib_ssi.c index 491d3163..d8aada30 100644 --- a/src/httplib_ssi.c +++ b/src/httplib_ssi.c @@ -26,10 +26,10 @@ #include "httplib_string.h" #include "httplib_utils.h" -static void send_ssi_file(struct mg_connection *, const char *, struct file *, int); +static void send_ssi_file(struct httplib_connection *, const char *, struct file *, int); -static void do_ssi_include(struct mg_connection *conn, const char *ssi, char *tag, int include_level) { +static void do_ssi_include(struct httplib_connection *conn, const char *ssi, char *tag, int include_level) { char file_name[MG_BUF_LEN]; char path[512]; @@ -67,17 +67,17 @@ static void do_ssi_include(struct mg_connection *conn, const char *ssi, char *ta } } else { - mg_cry(conn, "Bad SSI #include: [%s]", tag); + httplib_cry(conn, "Bad SSI #include: [%s]", tag); return; } if (truncated) { - mg_cry(conn, "SSI #include path length overflow: [%s]", tag); + httplib_cry(conn, "SSI #include path length overflow: [%s]", tag); return; } if (!XX_httplib_fopen(conn, path, "rb", &file)) { - mg_cry(conn, "Cannot open SSI #include: [%s]: fopen(%s): %s", tag, path, strerror(ERRNO)); + httplib_cry(conn, "Cannot open SSI #include: [%s]: fopen(%s): %s", tag, path, strerror(ERRNO)); } else { XX_httplib_fclose_on_exec(&file, conn); if (XX_httplib_match_prefix(conn->ctx->config[SSI_EXTENSIONS], strlen(conn->ctx->config[SSI_EXTENSIONS]), path) > 0) { @@ -92,17 +92,17 @@ static void do_ssi_include(struct mg_connection *conn, const char *ssi, char *ta #if !defined(NO_POPEN) -static void do_ssi_exec(struct mg_connection *conn, char *tag) { +static void do_ssi_exec(struct httplib_connection *conn, char *tag) { char cmd[1024] = ""; struct file file = STRUCT_FILE_INITIALIZER; if (sscanf(tag, " \"%1023[^\"]\"", cmd) != 1) { - mg_cry(conn, "Bad SSI #exec: [%s]", tag); + httplib_cry(conn, "Bad SSI #exec: [%s]", tag); } else { cmd[1023] = 0; if ((file.fp = popen(cmd, "r")) == NULL) { - mg_cry(conn, "Cannot SSI #exec: [%s]: %s", cmd, strerror(ERRNO)); + httplib_cry(conn, "Cannot SSI #exec: [%s]: %s", cmd, strerror(ERRNO)); } else { XX_httplib_send_file_data(conn, &file, 0, INT64_MAX); pclose(file.fp); @@ -112,7 +112,7 @@ static void do_ssi_exec(struct mg_connection *conn, char *tag) { #endif /* !NO_POPEN */ -static int mg_fgetc( struct file *filep, int offset ) { +static int httplib_fgetc( struct file *filep, int offset ) { if ( filep == NULL ) return EOF; if ( filep->membuf != NULL && offset >= 0 && ((unsigned int)(offset)) < filep->size ) return ((const unsigned char *)filep->membuf)[offset]; @@ -120,10 +120,10 @@ static int mg_fgetc( struct file *filep, int offset ) { return EOF; -} /* mg_fgetc */ +} /* httplib_fgetc */ -static void send_ssi_file( struct mg_connection *conn, const char *path, struct file *filep, int include_level ) { +static void send_ssi_file( struct httplib_connection *conn, const char *path, struct file *filep, int include_level ) { char buf[MG_BUF_LEN]; int ch; @@ -132,12 +132,12 @@ static void send_ssi_file( struct mg_connection *conn, const char *path, struct int in_ssi_tag; if (include_level > 10) { - mg_cry(conn, "SSI #include level is too deep (%s)", path); + httplib_cry(conn, "SSI #include level is too deep (%s)", path); return; } in_ssi_tag = len = offset = 0; - while ((ch = mg_fgetc(filep, offset)) != EOF) { + while ((ch = httplib_fgetc(filep, offset)) != EOF) { if (in_ssi_tag && ch == '>') { in_ssi_tag = 0; buf[len++] = (char)ch; @@ -146,7 +146,7 @@ static void send_ssi_file( struct mg_connection *conn, const char *path, struct if (len > (int)sizeof(buf)) break; if (len < 6 || memcmp(buf, " TODO: mg_get_cookie returns -3, mg_get_var -2. This should be + --> TODO: httplib_get_cookie returns -3, httplib_get_var -2. This should be unified. */ ck_assert(ret < 0); /* key not found in string */ - ret = mg_get_var2("", 0, "notfound", buf, sizeof(buf), 0); + ret = httplib_get_var2("", 0, "notfound", buf, sizeof(buf), 0); ck_assert_int_eq(ret, -1); - ret = mg_get_var2( - longquery, strlen(longquery), "notfound", buf, sizeof(buf), 0); + ret = httplib_get_var2( longquery, strlen(longquery), "notfound", buf, sizeof(buf), 0); ck_assert_int_eq(ret, -1); /* key not found in string */ - ret = mg_get_var2( + ret = httplib_get_var2( shortquery, strlen(shortquery), "notfound", buf, sizeof(buf), 0); ck_assert_int_eq(ret, -1); /* key not found in string */ - ret = mg_get_var2("key1=1&key2=2&key3=3¬found=here", + ret = httplib_get_var2("key1=1&key2=2&key3=3¬found=here", strlen(shortquery), "notfound", buf, @@ -278,39 +276,38 @@ START_TEST(test_mg_get_var) ck_assert_int_eq(ret, -1); /* key not found in string */ - ret = mg_get_var2( + ret = httplib_get_var2( shortquery, strlen(shortquery), "key1", buf, sizeof(buf), 1); ck_assert_int_eq(ret, -1); /* keys are found as first, middle and last key */ memset(buf, 77, sizeof(buf)); - ret = mg_get_var2( + ret = httplib_get_var2( shortquery, strlen(shortquery), "key1", buf, sizeof(buf), 0); ck_assert_int_eq(ret, 1); ck_assert_str_eq("1", buf); memset(buf, 77, sizeof(buf)); - ret = mg_get_var2( + ret = httplib_get_var2( shortquery, strlen(shortquery), "key2", buf, sizeof(buf), 0); ck_assert_int_eq(ret, 1); ck_assert_str_eq("2", buf); memset(buf, 77, sizeof(buf)); - ret = mg_get_var2( + ret = httplib_get_var2( shortquery, strlen(shortquery), "key3", buf, sizeof(buf), 0); ck_assert_int_eq(ret, 1); ck_assert_str_eq("3", buf); - /* mg_get_var call mg_get_var2 with last argument 0 */ + /* httplib_get_var call httplib_get_var2 with last argument 0 */ memset(buf, 77, sizeof(buf)); - ret = mg_get_var(shortquery, strlen(shortquery), "key1", buf, sizeof(buf)); + ret = httplib_get_var(shortquery, strlen(shortquery), "key1", buf, sizeof(buf)); ck_assert_int_eq(ret, 1); ck_assert_str_eq("1", buf); /* longer value in the middle of a longer string */ memset(buf, 77, sizeof(buf)); - ret = - mg_get_var2(longquery, strlen(longquery), "key7", buf, sizeof(buf), 0); + ret = httplib_get_var2(longquery, strlen(longquery), "key7", buf, sizeof(buf), 0); ck_assert_int_eq(ret, 10); ck_assert_str_eq("this is it", buf); @@ -318,29 +315,29 @@ START_TEST(test_mg_get_var) */ memset(buf, 77, sizeof(buf)); ret = - mg_get_var2(longquery, strlen(longquery), "key7", buf, sizeof(buf), 1); + httplib_get_var2(longquery, strlen(longquery), "key7", buf, sizeof(buf), 1); ck_assert_int_eq(ret, 10); ck_assert_str_eq("that is it", buf); /* key with = but without value in the middle of a longer string */ memset(buf, 77, sizeof(buf)); ret = - mg_get_var2(longquery, strlen(longquery), "key5", buf, sizeof(buf), 0); + httplib_get_var2(longquery, strlen(longquery), "key5", buf, sizeof(buf), 0); ck_assert_int_eq(ret, 0); ck_assert_str_eq(buf, ""); /* key without = and without value in the middle of a longer string */ memset(buf, 77, sizeof(buf)); ret = - mg_get_var2(longquery, strlen(longquery), "key6", buf, sizeof(buf), 0); + httplib_get_var2(longquery, strlen(longquery), "key6", buf, sizeof(buf), 0); ck_assert_int_eq(ret, -1); ck_assert_str_eq(buf, ""); - /* TODO: this is the same situation as with mg_get_value */ + /* TODO: this is the same situation as with httplib_get_value */ } END_TEST -START_TEST(test_mg_md5) +START_TEST(test_httplib_md5) { char buf[33]; char *ret; @@ -351,43 +348,31 @@ START_TEST(test_mg_md5) "U123456789V123456789W123456789X123456789Y123456789Z"; memset(buf, 77, sizeof(buf)); - ret = mg_md5(buf, NULL); + ret = httplib_md5(buf, NULL); ck_assert_str_eq(buf, "d41d8cd98f00b204e9800998ecf8427e"); ck_assert_str_eq(ret, "d41d8cd98f00b204e9800998ecf8427e"); ck_assert_ptr_eq(ret, buf); memset(buf, 77, sizeof(buf)); - ret = mg_md5(buf, "The quick brown fox jumps over the lazy dog.", NULL); + ret = httplib_md5(buf, "The quick brown fox jumps over the lazy dog.", NULL); ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0"); ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0"); ck_assert_ptr_eq(ret, buf); memset(buf, 77, sizeof(buf)); - ret = mg_md5(buf, - "", - "The qu", - "ick bro", - "", - "wn fox ju", - "m", - "ps over the la", - "", - "", - "zy dog.", - "", - NULL); + ret = httplib_md5(buf, "", "The qu", "ick bro", "", "wn fox ju", "m", "ps over the la", "", "", "zy dog.", "", NULL); ck_assert_str_eq(buf, "e4d909c290d0fb1ca068ffaddf22cbd0"); ck_assert_str_eq(ret, "e4d909c290d0fb1ca068ffaddf22cbd0"); ck_assert_ptr_eq(ret, buf); memset(buf, 77, sizeof(buf)); - ret = mg_md5(buf, long_str, NULL); + ret = httplib_md5(buf, long_str, NULL); ck_assert_str_eq(buf, "1cb13cf9f16427807f081b2138241f08"); ck_assert_str_eq(ret, "1cb13cf9f16427807f081b2138241f08"); ck_assert_ptr_eq(ret, buf); memset(buf, 77, sizeof(buf)); - ret = mg_md5(buf, long_str + 1, NULL); + ret = httplib_md5(buf, long_str + 1, NULL); ck_assert_str_eq(buf, "cf62d3264334154f5779d3694cc5093f"); ck_assert_str_eq(ret, "cf62d3264334154f5779d3694cc5093f"); ck_assert_ptr_eq(ret, buf); @@ -395,65 +380,65 @@ START_TEST(test_mg_md5) END_TEST -START_TEST(test_mg_url_encode) +START_TEST(test_httplib_url_encode) { char buf[20]; int ret; memset(buf, 77, sizeof(buf)); - ret = mg_url_encode("abc", buf, sizeof(buf)); + ret = httplib_url_encode("abc", buf, sizeof(buf)); ck_assert_int_eq(3, ret); ck_assert_str_eq("abc", buf); memset(buf, 77, sizeof(buf)); - ret = mg_url_encode("a%b/c&d.e", buf, sizeof(buf)); + ret = httplib_url_encode("a%b/c&d.e", buf, sizeof(buf)); ck_assert_int_eq(15, ret); ck_assert_str_eq("a%25b%2fc%26d.e", buf); memset(buf, 77, sizeof(buf)); - ret = mg_url_encode("%%%", buf, 4); + ret = httplib_url_encode("%%%", buf, 4); ck_assert_int_eq(-1, ret); ck_assert_str_eq("%25", buf); } END_TEST -START_TEST(test_mg_url_decode) +START_TEST(test_httplib_url_decode) { char buf[20]; int ret; - ret = mg_url_decode("abc", 3, buf, sizeof(buf), 0); + ret = httplib_url_decode("abc", 3, buf, sizeof(buf), 0); ck_assert_int_eq(ret, 3); ck_assert_str_eq(buf, "abc"); - ret = mg_url_decode("abcdef", 3, buf, sizeof(buf), 0); + ret = httplib_url_decode("abcdef", 3, buf, sizeof(buf), 0); ck_assert_int_eq(ret, 3); ck_assert_str_eq(buf, "abc"); - ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 0); + ret = httplib_url_decode("x+y", 3, buf, sizeof(buf), 0); ck_assert_int_eq(ret, 3); ck_assert_str_eq(buf, "x+y"); - ret = mg_url_decode("x+y", 3, buf, sizeof(buf), 1); + ret = httplib_url_decode("x+y", 3, buf, sizeof(buf), 1); ck_assert_int_eq(ret, 3); ck_assert_str_eq(buf, "x y"); - ret = mg_url_decode("%25", 3, buf, sizeof(buf), 1); + ret = httplib_url_decode("%25", 3, buf, sizeof(buf), 1); ck_assert_int_eq(ret, 1); ck_assert_str_eq(buf, "%"); } END_TEST -START_TEST(test_mg_get_response_code_text) +START_TEST(test_httplib_get_response_code_text) { int i; size_t j, len; const char *resp; for (i = 100; i < 600; i++) { - resp = mg_get_response_code_text(NULL, i); + resp = httplib_get_response_code_text(NULL, i); ck_assert_ptr_ne(resp, NULL); len = strlen(resp); ck_assert_uint_gt(len, 1); @@ -494,37 +479,37 @@ make_public_func_suite(void) TCase *const tcase_md5 = tcase_create("MD5"); TCase *const tcase_aux = tcase_create("Aux functions"); - tcase_add_test(tcase_version, test_mg_version); + tcase_add_test(tcase_version, test_httplib_version); tcase_set_timeout(tcase_version, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_version); - tcase_add_test(tcase_get_valid_options, test_mg_get_valid_options); + tcase_add_test(tcase_get_valid_options, test_httplib_get_valid_options); tcase_set_timeout(tcase_get_valid_options, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_get_valid_options); - tcase_add_test(tcase_get_builtin_mime_type, test_mg_get_builtin_mime_type); + tcase_add_test(tcase_get_builtin_mime_type, test_httplib_get_builtin_mime_type); tcase_set_timeout(tcase_get_builtin_mime_type, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_get_builtin_mime_type); - tcase_add_test(tcase_strncasecmp, test_mg_strncasecmp); + tcase_add_test(tcase_strncasecmp, test_httplib_strncasecmp); tcase_set_timeout(tcase_strncasecmp, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_strncasecmp); - tcase_add_test(tcase_urlencodingdecoding, test_mg_url_encode); - tcase_add_test(tcase_urlencodingdecoding, test_mg_url_decode); + tcase_add_test(tcase_urlencodingdecoding, test_httplib_url_encode); + tcase_add_test(tcase_urlencodingdecoding, test_httplib_url_decode); tcase_set_timeout(tcase_urlencodingdecoding, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_urlencodingdecoding); - tcase_add_test(tcase_cookies, test_mg_get_cookie); - tcase_add_test(tcase_cookies, test_mg_get_var); + tcase_add_test(tcase_cookies, test_httplib_get_cookie); + tcase_add_test(tcase_cookies, test_httplib_get_var); tcase_set_timeout(tcase_cookies, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_cookies); - tcase_add_test(tcase_md5, test_mg_md5); + tcase_add_test(tcase_md5, test_httplib_md5); tcase_set_timeout(tcase_md5, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_md5); - tcase_add_test(tcase_aux, test_mg_get_response_code_text); + tcase_add_test(tcase_aux, test_httplib_get_response_code_text); tcase_set_timeout(tcase_aux, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_aux); diff --git a/test/public_server.c b/test/public_server.c index 1c0b42a1..e0ef641f 100644 --- a/test/public_server.c +++ b/test/public_server.c @@ -1,4 +1,6 @@ -/* Copyright (c) 2015-2016 the Civetweb developers +/* + * Copyright (C) 2016 Lammert Bies + * Copyright (c) 2015-2016 the Civetweb developers * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -234,7 +236,7 @@ START_TEST(test_threading) threading_data = NULL; - ok = mg_start_thread(test_thread_func_t, &threading_data); + ok = httplib_start_thread(test_thread_func_t, &threading_data); ck_assert_int_eq(ok, 0); wait_not_null(&threading_data); @@ -244,15 +246,15 @@ END_TEST static int -log_msg_func(const struct mg_connection *conn, const char *message) +log_msg_func(const struct httplib_connection *conn, const char *message) { - struct mg_context *ctx; + struct httplib_context *ctx; char *ud; ck_assert(conn != NULL); - ctx = mg_get_context(conn); + ctx = httplib_get_context(conn); ck_assert(ctx != NULL); - ud = (char *)mg_get_user_data(ctx); + ud = (char *)httplib_get_user_data(ctx); strncpy(ud, message, 255); ud[255] = 0; @@ -260,9 +262,9 @@ log_msg_func(const struct mg_connection *conn, const char *message) } -START_TEST(test_mg_start_stop_http_server) +START_TEST(test_httplib_start_stop_http_server) { - struct mg_context *ctx; + struct httplib_context *ctx; const char *OPTIONS[] = { #if !defined(NO_FILES) "document_root", @@ -275,14 +277,14 @@ START_TEST(test_mg_start_stop_http_server) size_t ports_cnt; int ports[16]; int ssl[16]; - struct mg_callbacks callbacks; + struct httplib_callbacks callbacks; char errmsg[256]; - struct mg_connection *client_conn; + struct httplib_connection *client_conn; char client_err[256]; - const struct mg_request_info *client_ri; + const struct httplib_request_info *client_ri; int client_res, ret; - struct mg_server_ports portinfo[8]; + struct httplib_server_ports portinfo[8]; memset(ports, 0, sizeof(ports)); memset(ssl, 0, sizeof(ssl)); @@ -293,20 +295,20 @@ START_TEST(test_mg_start_stop_http_server) callbacks.log_message = log_msg_func; mark_point(); - ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS); + ctx = httplib_start(&callbacks, (void *)errmsg, OPTIONS); test_sleep(1); ck_assert_str_eq(errmsg, ""); ck_assert(ctx != NULL); - ports_cnt = mg_get_ports(ctx, 16, ports, ssl); + ports_cnt = httplib_get_ports(ctx, 16, ports, ssl); ck_assert_uint_eq(ports_cnt, 1); ck_assert_int_eq(ports[0], 8080); ck_assert_int_eq(ssl[0], 0); ck_assert_int_eq(ports[1], 0); ck_assert_int_eq(ssl[1], 0); - ret = mg_get_server_ports(ctx, 0, portinfo); + ret = httplib_get_server_ports(ctx, 0, portinfo); ck_assert_int_lt(ret, 0); ck_assert_int_eq(portinfo[0].protocol, 0); ck_assert_int_eq(portinfo[0].port, 0); @@ -317,7 +319,7 @@ START_TEST(test_mg_start_stop_http_server) ck_assert_int_eq(portinfo[1].is_ssl, 0); ck_assert_int_eq(portinfo[1].is_redirect, 0); - ret = mg_get_server_ports(ctx, 4, portinfo); + ret = httplib_get_server_ports(ctx, 4, portinfo); ck_assert_int_eq(ret, 1); ck_assert_int_eq(portinfo[0].protocol, 1); ck_assert_int_eq(portinfo[0].port, 8080); @@ -332,16 +334,14 @@ START_TEST(test_mg_start_stop_http_server) /* HTTP 1.0 GET request */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n"); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET / HTTP/1.0\r\n\r\n"); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); #if defined(NO_FILES) @@ -349,28 +349,26 @@ START_TEST(test_mg_start_stop_http_server) #else ck_assert_str_eq(client_ri->uri, "200"); /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */ - client_res = (int)mg_read(client_conn, client_err, sizeof(client_err)); + client_res = (int)httplib_read(client_conn, client_err, sizeof(client_err)); ck_assert_int_gt(client_res, 0); ck_assert_int_le(client_res, sizeof(client_err)); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); /* HTTP 1.1 GET request */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET / HTTP/1.1\r\n"); - mg_printf(client_conn, "Host: localhost:8080\r\n"); - mg_printf(client_conn, "Connection: close\r\n\r\n"); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET / HTTP/1.1\r\n"); + httplib_printf(client_conn, "Host: localhost:8080\r\n"); + httplib_printf(client_conn, "Connection: close\r\n\r\n"); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); #if defined(NO_FILES) @@ -378,34 +376,32 @@ START_TEST(test_mg_start_stop_http_server) #else ck_assert_str_eq(client_ri->uri, "200"); /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */ - client_res = (int)mg_read(client_conn, client_err, sizeof(client_err)); + client_res = (int)httplib_read(client_conn, client_err, sizeof(client_err)); ck_assert_int_gt(client_res, 0); ck_assert_int_le(client_res, sizeof(client_err)); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); /* HTTP 1.7 GET request - this HTTP version does not exist */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET / HTTP/1.7\r\n"); - mg_printf(client_conn, "Host: localhost:8080\r\n"); - mg_printf(client_conn, "Connection: close\r\n\r\n"); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET / HTTP/1.7\r\n"); + httplib_printf(client_conn, "Host: localhost:8080\r\n"); + httplib_printf(client_conn, "Connection: close\r\n\r\n"); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); /* Response must be 505 HTTP Version not supported */ ck_assert_str_eq(client_ri->uri, "505"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); @@ -414,55 +410,53 @@ START_TEST(test_mg_start_stop_http_server) * Multiline header are obsolete with RFC 7230 section-3.2.4 * and must return "400 Bad Request" */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET / HTTP/1.1\r\n"); - mg_printf(client_conn, "Host: localhost:8080\r\n"); - mg_printf(client_conn, "X-Obsolete-Header: something\r\nfor nothing\r\n"); - mg_printf(client_conn, "Connection: close\r\n\r\n"); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET / HTTP/1.1\r\n"); + httplib_printf(client_conn, "Host: localhost:8080\r\n"); + httplib_printf(client_conn, "X-Obsolete-Header: something\r\nfor nothing\r\n"); + httplib_printf(client_conn, "Connection: close\r\n\r\n"); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); /* Response must be 400 Bad Request */ ck_assert_str_eq(client_ri->uri, "400"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); /* End test */ - mg_stop(ctx); + httplib_stop(ctx); } END_TEST -START_TEST(test_mg_start_stop_https_server) +START_TEST(test_httplib_start_stop_https_server) { #ifndef NO_SSL - struct mg_context *ctx; + struct httplib_context *ctx; size_t ports_cnt; int ports[16]; int ssl[16]; - struct mg_callbacks callbacks; + struct httplib_callbacks callbacks; char errmsg[256]; const char *OPTIONS[8]; /* initializer list here is rejected by CI test */ int opt_idx = 0; const char *ssl_cert = locate_ssl_cert(); - struct mg_connection *client_conn; + struct httplib_connection *client_conn; char client_err[256]; - const struct mg_request_info *client_ri; + const struct httplib_request_info *client_ri; int client_res, ret; - struct mg_server_ports portinfo[8]; + struct httplib_server_ports portinfo[8]; ck_assert(ssl_cert != NULL); @@ -489,12 +483,12 @@ START_TEST(test_mg_start_stop_https_server) callbacks.log_message = log_msg_func; mark_point(); - ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS); + ctx = httplib_start(&callbacks, (void *)errmsg, OPTIONS); test_sleep(1); ck_assert_str_eq(errmsg, ""); ck_assert(ctx != NULL); - ports_cnt = mg_get_ports(ctx, 16, ports, ssl); + ports_cnt = httplib_get_ports(ctx, 16, ports, ssl); ck_assert_uint_eq(ports_cnt, 2); ck_assert_int_eq(ports[0], 8080); ck_assert_int_eq(ssl[0], 0); @@ -504,7 +498,7 @@ START_TEST(test_mg_start_stop_https_server) ck_assert_int_eq(ssl[2], 0); - ret = mg_get_server_ports(ctx, 0, portinfo); + ret = httplib_get_server_ports(ctx, 0, portinfo); ck_assert_int_lt(ret, 0); ck_assert_int_eq(portinfo[0].protocol, 0); ck_assert_int_eq(portinfo[0].port, 0); @@ -515,7 +509,7 @@ START_TEST(test_mg_start_stop_https_server) ck_assert_int_eq(portinfo[1].is_ssl, 0); ck_assert_int_eq(portinfo[1].is_redirect, 0); - ret = mg_get_server_ports(ctx, 4, portinfo); + ret = httplib_get_server_ports(ctx, 4, portinfo); ck_assert_int_eq(ret, 2); ck_assert_int_eq(portinfo[0].protocol, 1); ck_assert_int_eq(portinfo[0].port, 8080); @@ -533,16 +527,14 @@ START_TEST(test_mg_start_stop_https_server) test_sleep(1); memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n"); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET / HTTP/1.0\r\n\r\n"); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); #if defined(NO_FILES) @@ -550,36 +542,36 @@ START_TEST(test_mg_start_stop_https_server) #else ck_assert_str_eq(client_ri->uri, "200"); /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */ - client_res = (int)mg_read(client_conn, client_err, sizeof(client_err)); + client_res = (int)httplib_read(client_conn, client_err, sizeof(client_err)); ck_assert_int_gt(client_res, 0); ck_assert_int_le(client_res, sizeof(client_err)); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); - mg_stop(ctx); + httplib_stop(ctx); #endif } END_TEST -START_TEST(test_mg_server_and_client_tls) +START_TEST(test_httplib_server_and_client_tls) { #ifndef NO_SSL - struct mg_context *ctx; + struct httplib_context *ctx; int ports_cnt; - struct mg_server_ports ports[16]; - struct mg_callbacks callbacks; + struct httplib_server_ports ports[16]; + struct httplib_callbacks callbacks; char errmsg[256]; - struct mg_connection *client_conn; + struct httplib_connection *client_conn; char client_err[256]; - const struct mg_request_info *client_ri; + const struct httplib_request_info *client_ri; int client_res; - struct mg_client_options client_options; + struct httplib_client_options client_options; const char *OPTIONS[32]; /* initializer list here is rejected by CI test */ int opt_idx = 0; @@ -623,12 +615,12 @@ START_TEST(test_mg_server_and_client_tls) callbacks.log_message = log_msg_func; mark_point(); - ctx = mg_start(&callbacks, (void *)errmsg, OPTIONS); + ctx = httplib_start(&callbacks, (void *)errmsg, OPTIONS); test_sleep(1); ck_assert_str_eq(errmsg, ""); ck_assert(ctx != NULL); - ports_cnt = mg_get_server_ports(ctx, 16, ports); + ports_cnt = httplib_get_server_ports(ctx, 16, ports); ck_assert_int_eq(ports_cnt, 2); ck_assert_int_eq(ports[0].protocol, 1); ck_assert_int_eq(ports[0].port, 8080); @@ -646,8 +638,7 @@ START_TEST(test_mg_server_and_client_tls) test_sleep(1); memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8443, 1, client_err, sizeof(client_err)); ck_assert(client_conn == NULL); ck_assert_str_ne(client_err, ""); @@ -658,17 +649,14 @@ START_TEST(test_mg_server_and_client_tls) client_options.client_cert = client_cert; client_options.server_cert = server_cert; - client_conn = mg_connect_client_secure(&client_options, - client_err, - sizeof(client_err)); + client_conn = httplib_connect_client_secure(&client_options, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET / HTTP/1.0\r\n\r\n"); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET / HTTP/1.0\r\n\r\n"); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); #if defined(NO_FILES) @@ -676,60 +664,60 @@ START_TEST(test_mg_server_and_client_tls) #else ck_assert_str_eq(client_ri->uri, "200"); /* TODO: ck_assert_str_eq(client_ri->request_method, "HTTP/1.0"); */ - client_res = (int)mg_read(client_conn, client_err, sizeof(client_err)); + client_res = (int)httplib_read(client_conn, client_err, sizeof(client_err)); ck_assert_int_gt(client_res, 0); ck_assert_int_le(client_res, sizeof(client_err)); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* TODO: A client API using a client certificate is missing */ test_sleep(1); - mg_stop(ctx); + httplib_stop(ctx); #endif } END_TEST -static struct mg_context *g_ctx; +static struct httplib_context *g_ctx; static int -request_test_handler(struct mg_connection *conn, void *cbdata) +request_test_handler(struct httplib_connection *conn, void *cbdata) { int i; char chunk_data[32]; - const struct mg_request_info *ri; - struct mg_context *ctx; + const struct httplib_request_info *ri; + struct httplib_context *ctx; void *ud, *cud; - ctx = mg_get_context(conn); - ud = mg_get_user_data(ctx); - ri = mg_get_request_info(conn); + ctx = httplib_get_context(conn); + ud = httplib_get_user_data(ctx); + ri = httplib_get_request_info(conn); ck_assert(ri != NULL); ck_assert(ctx == g_ctx); ck_assert(ud == &g_ctx); - mg_set_user_connection_data(conn, (void *)6543); - cud = mg_get_user_connection_data(conn); + httplib_set_user_connection_data(conn, (void *)6543); + cud = httplib_get_user_connection_data(conn); ck_assert_ptr_eq((void *)cud, (void *)6543); ck_assert_ptr_eq((void *)cbdata, (void *)7); strcpy(chunk_data, "123456789A123456789B123456789C"); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "Content-Type: text/plain\r\n\r\n"); for (i = 1; i <= 10; i++) { - mg_printf(conn, "%x\r\n", i); - mg_write(conn, chunk_data, (unsigned)i); - mg_printf(conn, "\r\n"); + httplib_printf(conn, "%x\r\n", i); + httplib_write(conn, chunk_data, (unsigned)i); + httplib_printf(conn, "\r\n"); } - mg_printf(conn, "0\r\n\r\n"); + httplib_printf(conn, "0\r\n\r\n"); return 1; } @@ -747,7 +735,7 @@ static const size_t websocket_goodbye_msg_len = static int -websock_server_connect(const struct mg_connection *conn, void *udata) +websock_server_connect(const struct httplib_connection *conn, void *udata) { (void)conn; @@ -759,29 +747,22 @@ websock_server_connect(const struct mg_connection *conn, void *udata) static void -websock_server_ready(struct mg_connection *conn, void *udata) +websock_server_ready(struct httplib_connection *conn, void *udata) { ck_assert_ptr_eq((void *)udata, (void *)7531); printf("Server: Websocket ready\n"); /* Send websocket welcome message */ - mg_lock_connection(conn); - mg_websocket_write(conn, - WEBSOCKET_OPCODE_TEXT, - websocket_welcome_msg, - websocket_welcome_msg_len); - mg_unlock_connection(conn); + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, websocket_welcome_msg, websocket_welcome_msg_len); + httplib_unlock_connection(conn); printf("Server: Websocket ready X\n"); } static int -websock_server_data(struct mg_connection *conn, - int bits, - char *data, - size_t data_len, - void *udata) +websock_server_data(struct httplib_connection *conn, int bits, char *data, size_t data_len, void *udata) { (void)bits; @@ -790,24 +771,21 @@ websock_server_data(struct mg_connection *conn, if (data_len == 3 && !memcmp(data, "bye", 3)) { /* Send websocket goodbye message */ - mg_lock_connection(conn); - mg_websocket_write(conn, - WEBSOCKET_OPCODE_TEXT, - websocket_goodbye_msg, - websocket_goodbye_msg_len); - mg_unlock_connection(conn); + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, websocket_goodbye_msg, websocket_goodbye_msg_len); + httplib_unlock_connection(conn); } else if (data_len == 5 && !memcmp(data, "data1", 5)) { - mg_lock_connection(conn); - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok1", 3); - mg_unlock_connection(conn); + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok1", 3); + httplib_unlock_connection(conn); } else if (data_len == 5 && !memcmp(data, "data2", 5)) { - mg_lock_connection(conn); - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok 2", 4); - mg_unlock_connection(conn); + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok 2", 4); + httplib_unlock_connection(conn); } else if (data_len == 5 && !memcmp(data, "data3", 5)) { - mg_lock_connection(conn); - mg_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok - 3", 6); - mg_unlock_connection(conn); + httplib_lock_connection(conn); + httplib_websocket_write(conn, WEBSOCKET_OPCODE_TEXT, "ok - 3", 6); + httplib_unlock_connection(conn); } else { #if defined(__MINGW32__) || defined(__GNUC__) @@ -836,7 +814,7 @@ websock_server_data(struct mg_connection *conn, } static void -websock_server_close(const struct mg_connection *conn, void *udata) +websock_server_close(const struct httplib_connection *conn, void *udata) { (void)conn; @@ -859,15 +837,10 @@ struct tclient_data { static int -websocket_client_data_handler(struct mg_connection *conn, - int flags, - char *data, - size_t data_len, - void *user_data) +websocket_client_data_handler(struct httplib_connection *conn, int flags, char *data, size_t data_len, void *user_data) { - struct mg_context *ctx = mg_get_context(conn); - struct tclient_data *pclient_data = - (struct tclient_data *)mg_get_user_data(ctx); + struct httplib_context *ctx = httplib_get_context(conn); + struct tclient_data *pclient_data = (struct tclient_data *)httplib_get_user_data(ctx); (void)user_data; /* TODO: check this */ @@ -888,12 +861,11 @@ websocket_client_data_handler(struct mg_connection *conn, static void -websocket_client_close_handler(const struct mg_connection *conn, - void *user_data) +websocket_client_close_handler(const struct httplib_connection *conn, void *user_data) { - struct mg_context *ctx = mg_get_context(conn); + struct httplib_context *ctx = httplib_get_context(conn); struct tclient_data *pclient_data = - (struct tclient_data *)mg_get_user_data(ctx); + (struct tclient_data *)httplib_get_user_data(ctx); (void)user_data; /* TODO: check this */ @@ -908,9 +880,9 @@ websocket_client_close_handler(const struct mg_connection *conn, START_TEST(test_request_handlers) { char ebuf[100]; - struct mg_context *ctx; - struct mg_connection *client_conn; - const struct mg_request_info *ri; + struct httplib_context *ctx; + struct httplib_connection *client_conn; + const struct httplib_request_info *ri; char uri[64]; char buf[1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 8]; const char *expected = @@ -956,9 +928,9 @@ START_TEST(test_request_handlers) struct tclient_data ws_client1_data = {NULL, 0, 0}; struct tclient_data ws_client2_data = {NULL, 0, 0}; struct tclient_data ws_client3_data = {NULL, 0, 0}; - struct mg_connection *ws_client1_conn = NULL; - struct mg_connection *ws_client2_conn = NULL; - struct mg_connection *ws_client3_conn = NULL; + struct httplib_connection *ws_client1_conn = NULL; + struct httplib_connection *ws_client2_conn = NULL; + struct httplib_connection *ws_client3_conn = NULL; #endif char cmd_buf[256]; @@ -982,43 +954,40 @@ START_TEST(test_request_handlers) ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL); mark_point(); - ctx = mg_start(NULL, &g_ctx, OPTIONS); + ctx = httplib_start(NULL, &g_ctx, OPTIONS); ck_assert(ctx != NULL); g_ctx = ctx; - opt = mg_get_option(ctx, "listening_ports"); + opt = httplib_get_option(ctx, "listening_ports"); ck_assert_str_eq(opt, HTTP_PORT); - opt = mg_get_option(ctx, "cgi_environment"); + opt = httplib_get_option(ctx, "cgi_environment"); ck_assert_str_eq(opt, ""); - opt = mg_get_option(ctx, "unknown_option_name"); + opt = httplib_get_option(ctx, "unknown_option_name"); ck_assert(opt == NULL); for (i = 0; i < 1000; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, request_test_handler, NULL); + httplib_set_request_handler(ctx, uri, request_test_handler, NULL); } for (i = 500; i < 800; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)1); + httplib_set_request_handler(ctx, uri, NULL, (void *)1); } for (i = 600; i >= 0; i--) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)2); + httplib_set_request_handler(ctx, uri, NULL, (void *)2); } for (i = 750; i <= 1000; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)3); + httplib_set_request_handler(ctx, uri, NULL, (void *)3); } for (i = 5; i < 9; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, - uri, - request_test_handler, - (void *)(ptrdiff_t)i); + httplib_set_request_handler(ctx, uri, request_test_handler, (void *)(ptrdiff_t)i); } #ifdef USE_WEBSOCKET - mg_set_websocket_handler(ctx, + httplib_set_websocket_handler(ctx, "/websocket", websock_server_connect, websock_server_ready, @@ -1028,43 +997,35 @@ START_TEST(test_request_handlers) #endif /* Try to load non existing file */ - client_conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /file/not/found HTTP/1.0\r\n\r\n"); + client_conn = httplib_download("localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "GET /file/not/found HTTP/1.0\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "404"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Get data from callback */ - client_conn = mg_download( - "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request); + client_conn = httplib_download( "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, (int)strlen(expected)); buf[i] = 0; ck_assert_str_eq(buf, expected); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Get data from callback using http://127.0.0.1 */ - client_conn = mg_download( - "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request); + client_conn = httplib_download( "127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", request); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); if ((i >= 0) && ((size_t)i < sizeof(buf))) { buf[i] = 0; } else { @@ -1078,78 +1039,73 @@ START_TEST(test_request_handlers) ck_assert_int_eq(i, (int)strlen(expected)); buf[i] = 0; ck_assert_str_eq(buf, expected); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); #if defined(USE_IPV6) /* Get data from callback using http://[::1] */ - client_conn = - mg_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request); + client_conn = httplib_download("[::1]", ipv6_port, 0, ebuf, sizeof(ebuf), "%s", request); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, (int)strlen(expected)); buf[i] = 0; ck_assert_str_eq(buf, expected); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); #endif #if !defined(NO_SSL) /* Get data from callback using https://127.0.0.1 */ - client_conn = mg_download( - "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request); + client_conn = httplib_download( "127.0.0.1", ipv4s_port, 1, ebuf, sizeof(ebuf), "%s", request); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, (int)strlen(expected)); buf[i] = 0; ck_assert_str_eq(buf, expected); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Get redirect from callback using http://127.0.0.1 */ - client_conn = mg_download( - "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request); + client_conn = httplib_download( "127.0.0.1", ipv4r_port, 0, ebuf, sizeof(ebuf), "%s", request); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "302"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, -1); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); #endif #if defined(USE_IPV6) && !defined(NO_SSL) /* Get data from callback using https://[::1] */ - client_conn = - mg_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request); + client_conn = httplib_download("[::1]", ipv6s_port, 1, ebuf, sizeof(ebuf), "%s", request); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, (int)strlen(expected)); buf[i] = 0; ck_assert_str_eq(buf, expected); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Get redirect from callback using http://127.0.0.1 */ - client_conn = - mg_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request); + client_conn = httplib_download("[::1]", ipv6r_port, 0, ebuf, sizeof(ebuf), "%s", request); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "302"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, -1); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); #endif /* It seems to be impossible to find out what the actual working @@ -1207,15 +1163,9 @@ START_TEST(test_request_handlers) expected_cgi_result = "CGI test"; /* Get static data */ - client_conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /test.txt HTTP/1.0\r\n\r\n"); + client_conn = httplib_download("localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "GET /test.txt HTTP/1.0\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); @@ -1223,14 +1173,14 @@ START_TEST(test_request_handlers) ck_assert_str_eq(ri->uri, "404"); #else ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, 17); if ((i >= 0) && (i < (int)sizeof(buf))) { buf[i] = 0; } ck_assert_str_eq(buf, plain_file_content); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Test with CGI test executable */ @@ -1243,49 +1193,29 @@ START_TEST(test_request_handlers) #if !defined(NO_CGI) && !defined(NO_FILES) && !defined(_WIN32) /* TODO: add test for windows, check with POST */ - client_conn = mg_download( - "localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "POST /cgi_test.cgi HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC"); + client_conn = httplib_download( "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "POST /cgi_test.cgi HTTP/1.0\r\nContent-Length: 3\r\n\r\nABC"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); #endif /* Get zipped static data - will not work if Accept-Encoding is not set */ - client_conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /test_gz.txt HTTP/1.0\r\n\r\n"); + client_conn = httplib_download("localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "GET /test_gz.txt HTTP/1.0\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "404"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Get zipped static data - with Accept-Encoding */ - client_conn = mg_download( - "localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n"); + client_conn = httplib_download( "localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "GET /test_gz.txt HTTP/1.0\r\nAccept-Encoding: gzip\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); @@ -1293,7 +1223,7 @@ START_TEST(test_request_handlers) ck_assert_str_eq(ri->uri, "404"); #else ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, 52); if ((i >= 0) && (i < (int)sizeof(buf))) { buf[i] = 0; @@ -1301,19 +1231,13 @@ START_TEST(test_request_handlers) ck_assert_int_eq(ri->content_length, 52); ck_assert_str_eq(buf, encoded_file_content); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Get CGI generated data */ #if !defined(NO_CGI) - client_conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /test.cgi HTTP/1.0\r\n\r\n"); + client_conn = httplib_download("localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "GET /test.cgi HTTP/1.0\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); @@ -1323,7 +1247,7 @@ START_TEST(test_request_handlers) (void)expected_cgi_result; (void)cgi_script_content; #else - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); if ((i >= 0) && (i < (int)sizeof(buf))) { while ((i > 0) && ((buf[i - 1] == '\r') || (buf[i - 1] == '\n'))) { i--; @@ -1333,7 +1257,7 @@ START_TEST(test_request_handlers) /* ck_assert_int_eq(i, (int)strlen(expected_cgi_result)); */ ck_assert_str_eq(buf, expected_cgi_result); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); #endif #else @@ -1341,62 +1265,43 @@ START_TEST(test_request_handlers) (void)cgi_script_content; #endif - /* Get directory listing */ - client_conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET / HTTP/1.0\r\n\r\n"); + /* Get directory listing */ client_conn = httplib_download("localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "GET / HTTP/1.0\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); #if defined(NO_FILES) ck_assert_str_eq(ri->uri, "404"); #else ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert(i > 6); buf[6] = 0; ck_assert_str_eq(buf, ""); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* POST to static file (will not work) */ - client_conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "POST /test.txt HTTP/1.0\r\n\r\n"); + client_conn = httplib_download("localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "POST /test.txt HTTP/1.0\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); #if defined(NO_FILES) ck_assert_str_eq(ri->uri, "404"); #else ck_assert_str_eq(ri->uri, "405"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert(i >= 29); buf[29] = 0; ck_assert_str_eq(buf, "Error 405: Method Not Allowed"); #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* PUT to static file (will not work) */ - client_conn = mg_download("localhost", - ipv4_port, - 0, - ebuf, - sizeof(ebuf), - "%s", - "PUT /test.txt HTTP/1.0\r\n\r\n"); + client_conn = httplib_download("localhost", ipv4_port, 0, ebuf, sizeof(ebuf), "%s", "PUT /test.txt HTTP/1.0\r\n\r\n"); ck_assert(client_conn != NULL); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); #if defined(NO_FILES) @@ -1404,63 +1309,59 @@ START_TEST(test_request_handlers) #else ck_assert_str_eq(ri->uri, "401"); /* not authorized */ #endif - mg_close_connection(client_conn); + httplib_close_connection(client_conn); - /* Get data from callback using mg_connect_client instead of mg_download */ + /* Get data from callback using httplib_connect_client instead of httplib_download */ memset(ebuf, 0, sizeof(ebuf)); - client_conn = - mg_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf)); + client_conn = httplib_connect_client("127.0.0.1", ipv4_port, 0, ebuf, sizeof(ebuf)); ck_assert(client_conn != NULL); ck_assert_str_eq(ebuf, ""); - mg_printf(client_conn, "%s", request); + httplib_printf(client_conn, "%s", request); - i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000); + i = httplib_get_response(client_conn, ebuf, sizeof(ebuf), 10000); ck_assert_int_ge(i, 0); ck_assert_str_eq(ebuf, ""); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, (int)strlen(expected)); buf[i] = 0; ck_assert_str_eq(buf, expected); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); - /* Get data from callback using mg_connect_client and absolute URI */ + /* Get data from callback using httplib_connect_client and absolute URI */ memset(ebuf, 0, sizeof(ebuf)); - client_conn = - mg_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf)); + client_conn = httplib_connect_client("localhost", ipv4_port, 0, ebuf, sizeof(ebuf)); ck_assert(client_conn != NULL); ck_assert_str_eq(ebuf, ""); - mg_printf(client_conn, - "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n", - ipv4_port); + httplib_printf(client_conn, "GET http://test.domain:%d/U7 HTTP/1.0\r\n\r\n", ipv4_port); - i = mg_get_response(client_conn, ebuf, sizeof(ebuf), 10000); + i = httplib_get_response(client_conn, ebuf, sizeof(ebuf), 10000); ck_assert_int_ge(i, 0); ck_assert_str_eq(ebuf, ""); - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - i = mg_read(client_conn, buf, sizeof(buf)); + i = httplib_read(client_conn, buf, sizeof(buf)); ck_assert_int_eq(i, (int)strlen(expected)); buf[i] = 0; ck_assert_str_eq(buf, expected); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Websocket test */ #ifdef USE_WEBSOCKET /* Then connect a first client */ ws_client1_conn = - mg_connect_websocket_client("localhost", + httplib_connect_websocket_client("localhost", ipv4_port, 0, ebuf, @@ -1489,10 +1390,7 @@ START_TEST(test_request_handlers) ws_client1_data.data = NULL; ws_client1_data.len = 0; - mg_websocket_client_write(ws_client1_conn, - WEBSOCKET_OPCODE_TEXT, - "data1", - 5); + httplib_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data1", 5); wait_not_null( &(ws_client1_data @@ -1511,7 +1409,7 @@ START_TEST(test_request_handlers) /* Now connect a second client */ #ifdef USE_IPV6 ws_client2_conn = - mg_connect_websocket_client("[::1]", + httplib_connect_websocket_client("[::1]", ipv6_port, 0, ebuf, @@ -1523,7 +1421,7 @@ START_TEST(test_request_handlers) &ws_client2_data); #else ws_client2_conn = - mg_connect_websocket_client("127.0.0.1", + httplib_connect_websocket_client("127.0.0.1", ipv4_port, 0, ebuf, @@ -1551,10 +1449,7 @@ START_TEST(test_request_handlers) ws_client2_data.data = NULL; ws_client2_data.len = 0; - mg_websocket_client_write(ws_client1_conn, - WEBSOCKET_OPCODE_TEXT, - "data2", - 5); + httplib_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "data2", 5); wait_not_null( &(ws_client1_data @@ -1571,7 +1466,7 @@ START_TEST(test_request_handlers) ws_client1_data.data = NULL; ws_client1_data.len = 0; - mg_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3); + httplib_websocket_client_write(ws_client1_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3); wait_not_null( &(ws_client1_data.data)); /* Wait for the websocket goodbye message */ @@ -1589,7 +1484,7 @@ START_TEST(test_request_handlers) ws_client1_data.data = NULL; ws_client1_data.len = 0; - mg_close_connection(ws_client1_conn); + httplib_close_connection(ws_client1_conn); test_sleep(3); /* Won't get any message */ @@ -1600,7 +1495,7 @@ START_TEST(test_request_handlers) ck_assert(ws_client2_data.data == NULL); ck_assert(ws_client2_data.len == 0); - mg_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3); + httplib_websocket_client_write(ws_client2_conn, WEBSOCKET_OPCODE_TEXT, "bye", 3); wait_not_null( &(ws_client2_data.data)); /* Wait for the websocket goodbye message */ @@ -1618,7 +1513,7 @@ START_TEST(test_request_handlers) ws_client2_data.data = NULL; ws_client2_data.len = 0; - mg_close_connection(ws_client2_conn); + httplib_close_connection(ws_client2_conn); test_sleep(3); /* Won't get any message */ @@ -1631,7 +1526,7 @@ START_TEST(test_request_handlers) /* Connect client 3 */ ws_client3_conn = - mg_connect_websocket_client("localhost", + httplib_connect_websocket_client("localhost", #if defined(NO_SSL) ipv4_port, 0, @@ -1670,7 +1565,7 @@ START_TEST(test_request_handlers) /* Close the server */ g_ctx = NULL; - mg_stop(ctx); + httplib_stop(ctx); mark_point(); #ifdef USE_WEBSOCKET @@ -1926,60 +1821,53 @@ field_store(const char *path, long long file_size, void *user_data) static int -FormGet(struct mg_connection *conn, void *cbdata) +FormGet(struct httplib_connection *conn, void *cbdata) { - const struct mg_request_info *req_info = mg_get_request_info(conn); + const struct httplib_request_info *req_info = httplib_get_request_info(conn); int ret; - struct mg_form_data_handler fdh = {field_found, field_get, NULL, NULL}; + struct httplib_form_data_handler fdh = {field_found, field_get, NULL, NULL}; (void)cbdata; ck_assert(req_info != NULL); - mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"); + httplib_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"); fdh.user_data = (void *)&g_field_found_return; /* Call the form handler */ g_field_step = 0; g_field_found_return = FORM_FIELD_STORAGE_GET; - ret = mg_handle_form_request(conn, &fdh); + ret = httplib_handle_form_request(conn, &fdh); g_field_found_return = -888; ck_assert_int_eq(ret, 22); ck_assert_int_eq(g_field_step, 22); - mg_printf(conn, "%i\r\n", ret); + httplib_printf(conn, "%i\r\n", ret); g_field_step = 1000; return 1; } -static int -FormStore(struct mg_connection *conn, - void *cbdata, - int ret_expected, - int field_step_expected) +static int FormStore(struct httplib_connection *conn, void *cbdata, int ret_expected, int field_step_expected) { - const struct mg_request_info *req_info = mg_get_request_info(conn); + const struct httplib_request_info *req_info = httplib_get_request_info(conn); int ret; - struct mg_form_data_handler fdh = {field_found, - field_get, - field_store, - NULL}; + struct httplib_form_data_handler fdh = {field_found, field_get, field_store, NULL}; (void)cbdata; ck_assert(req_info != NULL); - mg_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"); + httplib_printf(conn, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n"); fdh.user_data = (void *)&g_field_found_return; /* Call the form handler */ g_field_step = 100; g_field_found_return = FORM_FIELD_STORAGE_STORE; - ret = mg_handle_form_request(conn, &fdh); + ret = httplib_handle_form_request(conn, &fdh); ck_assert_int_eq(ret, ret_expected); ck_assert_int_eq(g_field_step, field_step_expected); - mg_printf(conn, "%i\r\n", ret); + httplib_printf(conn, "%i\r\n", ret); g_field_step = 1000; return 1; @@ -1987,34 +1875,34 @@ FormStore(struct mg_connection *conn, static int -FormStore1(struct mg_connection *conn, void *cbdata) +FormStore1(struct httplib_connection *conn, void *cbdata) { return FormStore(conn, cbdata, 3, 101); } static int -FormStore2(struct mg_connection *conn, void *cbdata) +FormStore2(struct httplib_connection *conn, void *cbdata) { return FormStore(conn, cbdata, 4, 102); } static void -send_chunk_string(struct mg_connection *conn, const char *txt) +send_chunk_string(struct httplib_connection *conn, const char *txt) { unsigned int chunk_len = (unsigned int)strlen(txt); - mg_printf(conn, "%x\r\n", chunk_len); - mg_write(conn, txt, chunk_len); - mg_printf(conn, "\r\n"); + httplib_printf(conn, "%x\r\n", chunk_len); + httplib_write(conn, txt, chunk_len); + httplib_printf(conn, "\r\n"); } START_TEST(test_handle_form) { - struct mg_context *ctx; - struct mg_connection *client_conn; - const struct mg_request_info *ri; + struct httplib_context *ctx; + struct httplib_connection *client_conn; + const struct httplib_request_info *ri; const char *OPTIONS[8]; const char *opt; int opt_idx = 0; @@ -2032,21 +1920,21 @@ START_TEST(test_handle_form) ck_assert(OPTIONS[sizeof(OPTIONS) / sizeof(OPTIONS[0]) - 2] == NULL); mark_point(); - ctx = mg_start(NULL, &g_ctx, OPTIONS); + ctx = httplib_start(NULL, &g_ctx, OPTIONS); ck_assert(ctx != NULL); g_ctx = ctx; - opt = mg_get_option(ctx, "listening_ports"); + opt = httplib_get_option(ctx, "listening_ports"); ck_assert_str_eq(opt, "8884"); - mg_set_request_handler(ctx, "/handle_form", FormGet, (void *)0); - mg_set_request_handler(ctx, "/handle_form_store", FormStore1, (void *)0); - mg_set_request_handler(ctx, "/handle_form_store2", FormStore2, (void *)0); + httplib_set_request_handler(ctx, "/handle_form", FormGet, (void *)0); + httplib_set_request_handler(ctx, "/handle_form_store", FormStore1, (void *)0); + httplib_set_request_handler(ctx, "/handle_form_store2", FormStore2, (void *)0); test_sleep(1); /* Handle form: "GET" */ - client_conn = mg_download("localhost", + client_conn = httplib_download("localhost", 8884, 0, ebuf, @@ -2071,15 +1959,15 @@ START_TEST(test_handle_form) break; } } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Handle form: "POST x-www-form-urlencoded" */ client_conn = - mg_download("localhost", + httplib_download("localhost", 8884, 0, ebuf, @@ -2104,11 +1992,11 @@ START_TEST(test_handle_form) break; } } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Handle form: "POST multipart/form-data" */ multipart_body = @@ -2207,7 +2095,7 @@ START_TEST(test_handle_form) ck_assert_uint_eq(body_len, 2374); /* not required */ client_conn = - mg_download("localhost", + httplib_download("localhost", 8884, 0, ebuf, @@ -2229,16 +2117,16 @@ START_TEST(test_handle_form) break; } } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Handle form: "POST multipart/form-data" with chunked transfer encoding */ client_conn = - mg_download("localhost", + httplib_download("localhost", 8884, 0, ebuf, @@ -2262,13 +2150,13 @@ START_TEST(test_handle_form) chunk_len = body_len - body_sent; } ck_assert_int_gt((int)chunk_len, 0); - mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len); - mg_write(client_conn, multipart_body + body_sent, chunk_len); - mg_printf(client_conn, "\r\n"); + httplib_printf(client_conn, "%x\r\n", (unsigned int)chunk_len); + httplib_write(client_conn, multipart_body + body_sent, chunk_len); + httplib_printf(client_conn, "\r\n"); body_sent += chunk_len; chunk_len = (chunk_len % 40) + 1; } - mg_printf(client_conn, "0\r\n"); + httplib_printf(client_conn, "0\r\n"); for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) { test_sleep(1); @@ -2276,17 +2164,17 @@ START_TEST(test_handle_form) break; } } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Now test form_store */ /* First test with GET */ - client_conn = mg_download("localhost", + client_conn = httplib_download("localhost", 8884, 0, ebuf, @@ -2309,16 +2197,16 @@ START_TEST(test_handle_form) break; } } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Handle form: "POST x-www-form-urlencoded", chunked, store */ client_conn = - mg_download("localhost", + httplib_download("localhost", 8884, 0, ebuf, @@ -2339,23 +2227,21 @@ START_TEST(test_handle_form) test_sleep(1); send_chunk_string(client_conn, "eak_field_handler=abort&"); send_chunk_string(client_conn, "dontread=xyz"); - mg_printf(client_conn, "0\r\n"); + httplib_printf(client_conn, "0\r\n"); for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) { test_sleep(1); - if (g_field_step == 1000) { - break; - } + if (g_field_step == 1000) break; } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Handle form: "POST multipart/form-data", chunked, store */ client_conn = - mg_download("localhost", + httplib_download("localhost", 8884, 0, ebuf, @@ -2399,24 +2285,22 @@ START_TEST(test_handle_form) send_chunk_string(client_conn, "xyz\r\n"); send_chunk_string(client_conn, "--multipart-form-data-boundary"); send_chunk_string(client_conn, "--see-RFC-2388--\r\n"); - mg_printf(client_conn, "0\r\n"); + httplib_printf(client_conn, "0\r\n"); for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) { test_sleep(1); - if (g_field_step == 1000) { - break; - } + if (g_field_step == 1000) break; } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Handle form: "POST multipart/form-data", chunked, store, with files */ client_conn = - mg_download("localhost", + httplib_download("localhost", 8884, 0, ebuf, @@ -2456,9 +2340,9 @@ START_TEST(test_handle_form) /* send some strings that are almost boundaries */ for (chunk_len = 1; chunk_len < strlen(boundary); chunk_len++) { /* chunks from 1 byte to strlen(boundary)-1 */ - mg_printf(client_conn, "%x\r\n", (unsigned int)chunk_len); - mg_write(client_conn, boundary, chunk_len); - mg_printf(client_conn, "\r\n"); + httplib_printf(client_conn, "%x\r\n", (unsigned int)chunk_len); + httplib_write(client_conn, boundary, chunk_len); + httplib_printf(client_conn, "\r\n"); body_sent += chunk_len; } } while (body_sent < 1024 * 1024); @@ -2490,24 +2374,22 @@ START_TEST(test_handle_form) send_chunk_string(client_conn, "xyz\r\n"); send_chunk_string(client_conn, "--multipart-form-data-boundary"); send_chunk_string(client_conn, "--see-RFC-2388--\r\n"); - mg_printf(client_conn, "0\r\n"); + httplib_printf(client_conn, "0\r\n"); for (sleep_cnt = 0; sleep_cnt < 30; sleep_cnt++) { test_sleep(1); - if (g_field_step == 1000) { - break; - } + if (g_field_step == 1000) break; } - ri = mg_get_request_info(client_conn); + ri = httplib_get_request_info(client_conn); ck_assert(ri != NULL); ck_assert_str_eq(ri->uri, "200"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); /* Close the server */ g_ctx = NULL; - mg_stop(ctx); + httplib_stop(ctx); mark_point(); } END_TEST @@ -2527,10 +2409,10 @@ START_TEST(test_http_auth) #endif NULL, }; - struct mg_context *ctx; - struct mg_connection *client_conn; + struct httplib_context *ctx; + struct httplib_connection *client_conn; char client_err[256], nonce[256]; - const struct mg_request_info *client_ri; + const struct httplib_request_info *client_ri; int client_res; FILE *f; const char *passwd_file = ".htpasswd"; @@ -2551,16 +2433,16 @@ START_TEST(test_http_auth) /* Start with default options */ mark_point(); - ctx = mg_start(NULL, NULL, OPTIONS); + ctx = httplib_start(NULL, NULL, OPTIONS); test_sleep(1); ck_assert(ctx != NULL); - domain = mg_get_option(ctx, "authentication_domain"); + domain = httplib_get_option(ctx, "authentication_domain"); ck_assert(domain != NULL); len = strlen(domain); ck_assert_uint_gt(len, 0); ck_assert_uint_lt(len, 64); - doc_root = mg_get_option(ctx, "document_root"); + doc_root = httplib_get_option(ctx, "document_root"); ck_assert_str_eq(doc_root, "."); /* Create a default file in the document root */ @@ -2576,56 +2458,51 @@ START_TEST(test_http_auth) /* Read file before a .htpasswd file has been created */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); ck_assert_str_eq(client_ri->uri, "200"); - client_res = (int)mg_read(client_conn, client_err, sizeof(client_err)); + client_res = (int)httplib_read(client_conn, client_err, sizeof(client_err)); ck_assert_int_gt(client_res, 0); ck_assert_int_le(client_res, sizeof(client_err)); ck_assert_str_eq(client_err, test_content); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); /* Create a .htpasswd file */ - client_res = mg_modify_passwords_file(passwd_file, domain, "user", "pass"); + client_res = httplib_modify_passwords_file(passwd_file, domain, "user", "pass"); ck_assert_int_eq(client_res, 1); - client_res = mg_modify_passwords_file(NULL, domain, "user", "pass"); + client_res = httplib_modify_passwords_file(NULL, domain, "user", "pass"); ck_assert_int_eq(client_res, 0); /* Filename is required */ test_sleep(1); /* Repeat test after .htpasswd is created */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); ck_assert_str_eq(client_ri->uri, "401"); auth_request = NULL; for (i = 0; i < client_ri->num_headers; i++) { - if (!mg_strcasecmp(client_ri->http_headers[i].name, - "WWW-Authenticate")) { + if (!httplib_strcasecmp(client_ri->http_headers[i].name, "WWW-Authenticate")) { ck_assert_ptr_eq(auth_request, NULL); auth_request = client_ri->http_headers[i].value; ck_assert_ptr_ne(auth_request, NULL); @@ -2634,7 +2511,7 @@ START_TEST(test_http_auth) ck_assert_ptr_ne(auth_request, NULL); str = "Digest qop=\"auth\", realm=\""; len = strlen(str); - ck_assert(!mg_strncasecmp(auth_request, str, len)); + ck_assert(!httplib_strncasecmp(auth_request, str, len)); ck_assert(!strncmp(auth_request + len, domain, strlen(domain))); len += strlen(domain); str = "\", nonce=\""; @@ -2660,43 +2537,30 @@ START_TEST(test_http_auth) sprintf(HA1, "%s:%s:%s", "user", domain, "pass"); sprintf(HA2, "%s:/%s", "GET", test_file); - HA1_md5_ret = mg_md5(HA1_md5_buf, HA1, NULL); - HA2_md5_ret = mg_md5(HA2_md5_buf, HA2, NULL); + HA1_md5_ret = httplib_md5(HA1_md5_buf, HA1, NULL); + HA2_md5_ret = httplib_md5(HA2_md5_buf, HA2, NULL); ck_assert_ptr_eq(HA1_md5_ret, HA1_md5_buf); ck_assert_ptr_eq(HA2_md5_ret, HA2_md5_buf); - HA_md5_ret = mg_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL); + HA_md5_ret = httplib_md5(HA_md5_buf, "user", ":", domain, ":", "pass", NULL); ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf); ck_assert_str_eq(HA1_md5_ret, HA_md5_buf); - HA_md5_ret = mg_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL); + HA_md5_ret = httplib_md5(HA_md5_buf, "GET", ":", "/", test_file, NULL); ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf); ck_assert_str_eq(HA2_md5_ret, HA_md5_buf); - HA_md5_ret = mg_md5(HA_md5_buf, - HA1_md5_buf, - ":", - nonce, - ":", - nc, - ":", - cnonce, - ":", - "auth", - ":", - HA2_md5_buf, - NULL); + HA_md5_ret = httplib_md5(HA_md5_buf, HA1_md5_buf, ":", nonce, ":", nc, ":", cnonce, ":", "auth", ":", HA2_md5_buf, NULL); ck_assert_ptr_eq(HA_md5_ret, HA_md5_buf); /* Retry with Authorization */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file); - mg_printf(client_conn, + httplib_printf(client_conn, "GET /%s HTTP/1.0\r\n", test_file); + httplib_printf(client_conn, "Authorization: Digest " "username=\"%s\", " "realm=\"%s\", " @@ -2713,25 +2577,24 @@ START_TEST(test_http_auth) nc, cnonce, HA_md5_buf); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); ck_assert_str_eq(client_ri->uri, "200"); - client_res = (int)mg_read(client_conn, client_err, sizeof(client_err)); + client_res = (int)httplib_read(client_conn, client_err, sizeof(client_err)); ck_assert_int_gt(client_res, 0); ck_assert_int_le(client_res, sizeof(client_err)); ck_assert_str_eq(client_err, test_content); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); /* Remove the user from the .htpasswd file again */ - client_res = mg_modify_passwords_file(passwd_file, domain, "user", NULL); + client_res = httplib_modify_passwords_file(passwd_file, domain, "user", NULL); ck_assert_int_eq(client_res, 1); test_sleep(1); @@ -2739,20 +2602,18 @@ START_TEST(test_http_auth) /* Try to access the file again. Expected: 401 error */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); ck_assert_str_eq(client_ri->uri, "401"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); @@ -2764,30 +2625,28 @@ START_TEST(test_http_auth) /* Access to the file must work like before */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + httplib_printf(client_conn, "GET /%s HTTP/1.0\r\n\r\n", test_file); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); ck_assert_str_eq(client_ri->uri, "200"); - client_res = (int)mg_read(client_conn, client_err, sizeof(client_err)); + client_res = (int)httplib_read(client_conn, client_err, sizeof(client_err)); ck_assert_int_gt(client_res, 0); ck_assert_int_le(client_res, sizeof(client_err)); ck_assert_str_eq(client_err, test_content); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); /* Stop the server and clean up */ - mg_stop(ctx); + httplib_stop(ctx); remove(test_file); #endif @@ -2797,7 +2656,7 @@ END_TEST START_TEST(test_keep_alive) { - struct mg_context *ctx; + struct httplib_context *ctx; const char *OPTIONS[] = { "listening_ports", "8080", @@ -2813,32 +2672,30 @@ START_TEST(test_keep_alive) #endif NULL }; - struct mg_connection *client_conn; + struct httplib_connection *client_conn; char client_err[256]; - const struct mg_request_info *client_ri; + const struct httplib_request_info *client_ri; int client_res, i; const char *connection_header; mark_point(); - ctx = mg_start(NULL, NULL, OPTIONS); + ctx = httplib_start(NULL, NULL, OPTIONS); ck_assert(ctx != NULL); test_sleep(1); /* HTTP 1.1 GET request */ memset(client_err, 0, sizeof(client_err)); - client_conn = - mg_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); + client_conn = httplib_connect_client("127.0.0.1", 8080, 0, client_err, sizeof(client_err)); ck_assert(client_conn != NULL); ck_assert_str_eq(client_err, ""); - mg_printf(client_conn, + httplib_printf(client_conn, "GET / HTTP/1.1\r\nHost: " "localhost:8080\r\nConnection: keep-alive\r\n\r\n"); - client_res = - mg_get_response(client_conn, client_err, sizeof(client_err), 10000); + client_res = httplib_get_response(client_conn, client_err, sizeof(client_err), 10000); ck_assert_int_ge(client_res, 0); ck_assert_str_eq(client_err, ""); - client_ri = mg_get_request_info(client_conn); + client_ri = httplib_get_request_info(client_conn); ck_assert(client_ri != NULL); #if defined(NO_FILES) @@ -2849,7 +2706,7 @@ START_TEST(test_keep_alive) connection_header = 0; for (i = 0; i < client_ri->num_headers; i++) { - if (!mg_strcasecmp(client_ri->http_headers[i].name, "Connection")) { + if (!httplib_strcasecmp(client_ri->http_headers[i].name, "Connection")) { ck_assert_ptr_eq(connection_header, NULL); connection_header = client_ri->http_headers[i].value; ck_assert_ptr_ne(connection_header, NULL); @@ -2858,7 +2715,7 @@ START_TEST(test_keep_alive) /* Error replies will close the connection, even if keep-alive is set. */ ck_assert_ptr_ne(connection_header, NULL); ck_assert_str_eq(connection_header, "close"); - mg_close_connection(client_conn); + httplib_close_connection(client_conn); test_sleep(1); @@ -2866,7 +2723,7 @@ START_TEST(test_keep_alive) * (will only work if NO_FILES is not set). */ /* Stop the server and clean up */ - mg_stop(ctx); + httplib_stop(ctx); } END_TEST @@ -2894,15 +2751,15 @@ make_public_server_suite(void) tcase_set_timeout(tcase_startthreads, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_startthreads); - tcase_add_test(tcase_startstophttp, test_mg_start_stop_http_server); + tcase_add_test(tcase_startstophttp, test_httplib_start_stop_http_server); tcase_set_timeout(tcase_startstophttp, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_startstophttp); - tcase_add_test(tcase_startstophttps, test_mg_start_stop_https_server); + tcase_add_test(tcase_startstophttps, test_httplib_start_stop_https_server); tcase_set_timeout(tcase_startstophttps, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_startstophttps); - tcase_add_test(tcase_serverandclienttls, test_mg_server_and_client_tls); + tcase_add_test(tcase_serverandclienttls, test_httplib_server_and_client_tls); tcase_set_timeout(tcase_serverandclienttls, civetweb_min_test_timeout); suite_add_tcase(suite, tcase_serverandclienttls); @@ -2938,10 +2795,10 @@ MAIN_PUBLIC_SERVER(void) { test_the_test_environment(0); test_threading(0); - test_mg_start_stop_http_server(0); - // test_mg_start_stop_https_server(0); + test_httplib_start_stop_http_server(0); + // test_httplib_start_stop_https_server(0); // test_request_handlers(0); - // test_mg_server_and_client_tls(0); + // test_httplib_server_and_client_tls(0); // test_handle_form(0); // test_http_auth(0); test_keep_alive(0); diff --git a/test/test.pl b/test/test.pl index e5034890..350df432 100755 --- a/test/test.pl +++ b/test/test.pl @@ -137,12 +137,12 @@ $SIG{PIPE} = 'IGNORE'; $SIG{ALRM} = sub { die "timeout\n" }; #local $| =1; -# Make sure we export only symbols that start with "mg_", and keep local +# Make sure we export only symbols that start with "httplib_", and keep local # symbols static. if ($^O =~ /darwin|bsd|linux/) { my $out = `(cc -c src/civetweb.c && nm src/civetweb.o) | grep ' T '`; foreach (split /\n/, $out) { - /T\s+_?mg_.+/ or fail("Exported symbol $_") + /T\s+_?httplib_.+/ or fail("Exported symbol $_") } } diff --git a/test/testmime.c b/test/testmime.c index 8c5a7925..db37e38c 100644 --- a/test/testmime.c +++ b/test/testmime.c @@ -56,7 +56,7 @@ int main( void ) { if ( p1 == NULL || p2 == NULL ) break; - if ( mg_strcasecmp( p1, p2 ) >= 0 ) { + if ( httplib_strcasecmp( p1, p2 ) >= 0 ) { printf( "Compare ERROR: \"%s\" not less than \"%s\"\n", p1, p2 ); problems++; diff --git a/test/unit_test.c b/test/unit_test.c index 19680ae0..0259e454 100644 --- a/test/unit_test.c +++ b/test/unit_test.c @@ -93,7 +93,7 @@ void check_func(int condition, const char *cond_txt, unsigned line) static void test_parse_http_message() { - struct mg_request_info ri; + struct httplib_request_info ri; char req1[] = "GET / HTTP/1.1\r\n\r\n"; char req2[] = "BLAH / HTTP/1.1\r\n\r\n"; char req3[] = "GET / HTTP/1.1\r\nBah\r\n"; @@ -136,8 +136,8 @@ static void test_parse_http_message() static void test_should_keep_alive(void) { - struct mg_connection conn; - struct mg_context ctx; + struct httplib_connection conn; + struct httplib_context ctx; char req1[] = "GET / HTTP/1.1\r\n\r\n"; char req2[] = "GET / HTTP/1.0\r\n\r\n"; char req3[] = "GET / HTTP/1.1\r\nConnection: close\r\n\r\n"; @@ -238,7 +238,7 @@ static char *read_file(const char *path, int *size) char *data = NULL; if ((fp = fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) { *size = (int)st.st_size; - data = mg_malloc(*size); + data = httplib_malloc(*size); ASSERT(data != NULL); ASSERT(fread(data, 1, *size, fp) == (size_t)*size); fclose(fp); @@ -256,7 +256,7 @@ static const char *upload_filename2 = "upload_test2.txt"; static const char *upload_ok_message = "upload successful"; static const char * -open_file_cb(const struct mg_connection *conn, const char *path, size_t *size) +open_file_cb(const struct httplib_connection *conn, const char *path, size_t *size) { (void)conn; if (!strcmp(path, "./blah")) { @@ -266,9 +266,9 @@ open_file_cb(const struct mg_connection *conn, const char *path, size_t *size) return NULL; } -static void upload_cb(struct mg_connection *conn, const char *path) +static void upload_cb(struct httplib_connection *conn, const char *path) { - const struct mg_request_info *ri = mg_get_request_info(conn); + const struct httplib_request_info *ri = httplib_get_request_info(conn); char *p1, *p2; int len1, len2; @@ -278,8 +278,8 @@ static void upload_cb(struct mg_connection *conn, const char *path) ASSERT((p2 = read_file(path, &len2)) != NULL); ASSERT(len1 == len2); ASSERT(memcmp(p1, p2, len1) == 0); - mg_free(p1); - mg_free(p2); + httplib_free(p1); + httplib_free(p2); remove(upload_filename); } else if (atoi(ri->query_string) == 2) { if (!strcmp(path, "./upload_test.txt")) { @@ -287,16 +287,16 @@ static void upload_cb(struct mg_connection *conn, const char *path) ASSERT((p2 = read_file(path, &len2)) != NULL); ASSERT(len1 == len2); ASSERT(memcmp(p1, p2, len1) == 0); - mg_free(p1); - mg_free(p2); + httplib_free(p1); + httplib_free(p2); remove(upload_filename); } else if (!strcmp(path, "./upload_test2.txt")) { ASSERT((p1 = read_file("README.md", &len1)) != NULL); ASSERT((p2 = read_file(path, &len2)) != NULL); ASSERT(len1 == len2); ASSERT(memcmp(p1, p2, len1) == 0); - mg_free(p1); - mg_free(p2); + httplib_free(p1); + httplib_free(p2); remove(upload_filename); } else { ASSERT(0); @@ -305,18 +305,15 @@ static void upload_cb(struct mg_connection *conn, const char *path) ASSERT(0); } - mg_printf(conn, - "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s", - (int)strlen(upload_ok_message), - upload_ok_message); + httplib_printf(conn, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\n\r\n%s", (int)strlen(upload_ok_message), upload_ok_message); } -static int begin_request_handler_cb(struct mg_connection *conn) +static int begin_request_handler_cb(struct httplib_connection *conn) { - const struct mg_request_info *ri = mg_get_request_info(conn); + const struct httplib_request_info *ri = httplib_get_request_info(conn); int req_len = (int)(ri->content_length); - const char *s_req_len = mg_get_header(conn, "Content-Length"); + const char *s_req_len = httplib_get_header(conn, "Content-Length"); char *data; long to_write, write_now; int bytes_read, bytes_written; @@ -330,7 +327,7 @@ static int begin_request_handler_cb(struct mg_connection *conn) } else { to_write = atol(ri->uri + 6); } - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Connection: close\r\n" "Content-Length: %li\r\n" @@ -338,7 +335,7 @@ static int begin_request_handler_cb(struct mg_connection *conn) to_write); while (to_write > 0) { write_now = to_write > fetch_data_size ? fetch_data_size : to_write; - bytes_written = mg_write(conn, fetch_data, write_now); + bytes_written = httplib_write(conn, fetch_data, write_now); ASSERT(bytes_written == write_now); if (bytes_written < 0) { ASSERT(0); @@ -352,32 +349,32 @@ static int begin_request_handler_cb(struct mg_connection *conn) if (!strcmp(ri->uri, "/content_length")) { if (req_len > 0) { - data = mg_malloc(req_len); + data = httplib_malloc(req_len); assert(data != NULL); - bytes_read = mg_read(conn, data, req_len); + bytes_read = httplib_read(conn, data, req_len); ASSERT(bytes_read == req_len); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Connection: close\r\n" "Content-Length: %d\r\n" /* The official definition */ "Content-Type: text/plain\r\n\r\n", bytes_read); - mg_write(conn, data, bytes_read); + httplib_write(conn, data, bytes_read); - mg_free(data); + httplib_free(data); } else { - data = mg_malloc(1024); + data = httplib_malloc(1024); assert(data != NULL); - bytes_read = mg_read(conn, data, 1024); + bytes_read = httplib_read(conn, data, 1024); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Connection: close\r\n" "Content-Type: text/plain\r\n\r\n"); - mg_write(conn, data, bytes_read); + httplib_write(conn, data, bytes_read); - mg_free(data); + httplib_free(data); } close_connection(conn); return 1; @@ -385,13 +382,13 @@ static int begin_request_handler_cb(struct mg_connection *conn) if (!strcmp(ri->uri, "/upload")) { ASSERT(ri->query_string != NULL); - ASSERT(mg_upload(conn, ".") == atoi(ri->query_string)); + ASSERT(httplib_upload(conn, ".") == atoi(ri->query_string)); } return 0; } -static int log_message_cb(const struct mg_connection *conn, const char *msg) +static int log_message_cb(const struct httplib_connection *conn, const char *msg) { (void)conn; printf("%s\n", msg); @@ -399,25 +396,20 @@ static int log_message_cb(const struct mg_connection *conn, const char *msg) } -int (*begin_request)(struct mg_connection *); -void (*end_request)(const struct mg_connection *, int reply_status_code); -int (*log_message)(const struct mg_connection *, const char *message); +int (*begin_request)(struct httplib_connection *); +void (*end_request)(const struct httplib_connection *, int reply_status_code); +int (*log_message)(const struct httplib_connection *, const char *message); int (*init_ssl)(void *ssl_context, void *user_data); -int (*websocket_connect)(const struct mg_connection *); -void (*websocket_ready)(struct mg_connection *); -int (*websocket_data)(struct mg_connection *, - int bits, - char *data, - size_t data_len); -void (*connection_close)(struct mg_connection *); -const char *(*open_file)(const struct mg_connection *, - const char *path, - size_t *data_len); -void (*init_lua)(struct mg_connection *, void *lua_context); -void (*upload)(struct mg_connection *, const char *file_name); -int (*http_error)(struct mg_connection *, int status); +int (*websocket_connect)(const struct httplib_connection *); +void (*websocket_ready)(struct httplib_connection *); +int (*websocket_data)(struct httplib_connection *, int bits, char *data, size_t data_len); +void (*connection_close)(struct httplib_connection *); +const char *(*open_file)(const struct httplib_connection *, const char *path, size_t *data_len); +void (*init_lua)(struct httplib_connection *, void *lua_context); +void (*upload)(struct httplib_connection *, const char *file_name); +int (*http_error)(struct httplib_connection *, int status); -static struct mg_callbacks CALLBACKS; +static struct httplib_callbacks CALLBACKS; static void init_CALLBACKS() { @@ -442,14 +434,14 @@ static const char *OPTIONS[] = { NULL, }; -static char *read_conn(struct mg_connection *conn, int *size) +static char *read_conn(struct httplib_connection *conn, int *size) { char buf[100], *data = NULL; int len; *size = 0; - while ((len = mg_read(conn, buf, sizeof(buf))) > 0) { + while ((len = httplib_read(conn, buf, sizeof(buf))) > 0) { *size += len; - data = mg_realloc(data, *size); + data = httplib_realloc(data, *size); ASSERT(data != NULL); memcpy(data + *size - len, buf, len); } @@ -457,26 +449,26 @@ static char *read_conn(struct mg_connection *conn, int *size) } #ifdef MEMORY_DEBUGGING -extern unsigned long mg_memory_debug_blockCount; -extern unsigned long mg_memory_debug_totalMemUsed; +extern unsigned long httplib_memory_debug_blockCount; +extern unsigned long httplib_memory_debug_totalMemUsed; #endif -static void ut_mg_stop(struct mg_context *ctx) +static void ut_httplib_stop(struct httplib_context *ctx) { - /* mg_stop for unit_test */ - mg_stop(ctx); + /* httplib_stop for unit_test */ + httplib_stop(ctx); #ifdef MEMORY_DEBUGGING - ASSERT(mg_memory_debug_blockCount == 0); - ASSERT(mg_memory_debug_totalMemUsed == 0); - mg_memory_debug_blockCount = 0; - mg_memory_debug_totalMemUsed = 0; + ASSERT(httplib_memory_debug_blockCount == 0); + ASSERT(httplib_memory_debug_totalMemUsed == 0); + httplib_memory_debug_blockCount = 0; + httplib_memory_debug_totalMemUsed = 0; #endif - mg_sleep(31000); /* This is required to ensure the operating system already + httplib_sleep(31000); /* This is required to ensure the operating system already allows to use the port again */ } -static void test_mg_download(int use_ssl) +static void test_httplib_download(int use_ssl) { const char *test_data = "123456789A123456789B"; @@ -484,9 +476,9 @@ static void test_mg_download(int use_ssl) char *p1, *p2, ebuf[100]; const char *h; int i, len1, len2, port; - struct mg_connection *conn; - struct mg_context *ctx; - const struct mg_request_info *ri; + struct httplib_connection *conn; + struct httplib_context *ctx; + const struct httplib_request_info *ri; if (use_ssl) { port = atoi(HTTPS_PORT); @@ -494,105 +486,63 @@ static void test_mg_download(int use_ssl) port = atoi(HTTP_PORT); } - ctx = mg_start(&CALLBACKS, NULL, OPTIONS); + ctx = httplib_start(&CALLBACKS, NULL, OPTIONS); ASSERT(ctx != NULL); - ASSERT(mg_download(NULL, port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == + ASSERT(httplib_download(NULL, port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL); - ASSERT(mg_download("localhost", 0, use_ssl, ebuf, sizeof(ebuf), "%s", "") == + ASSERT(httplib_download("localhost", 0, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL); ASSERT( - mg_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == + httplib_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "") == NULL); /* Fetch nonexistent file, should see 404 */ - ASSERT((conn = mg_download("localhost", - port, - use_ssl, - ebuf, - sizeof(ebuf), - "%s", - "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL); + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "GET /gimbec HTTP/1.0\r\n\r\n")) != NULL); ASSERT(strcmp(conn->request_info.uri, "404") == 0); - mg_close_connection(conn); + httplib_close_connection(conn); if (use_ssl) { - ASSERT((conn = mg_download("google.com", - 443, - 1, - ebuf, - sizeof(ebuf), - "%s", - "GET / HTTP/1.0\r\n\r\n")) != NULL); - mg_close_connection(conn); + ASSERT((conn = httplib_download("google.com", 443, 1, ebuf, sizeof(ebuf), "%s", "GET / HTTP/1.0\r\n\r\n")) != NULL); + httplib_close_connection(conn); } else { - ASSERT((conn = mg_download("google.com", - 80, - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET / HTTP/1.0\r\n\r\n")) != NULL); - mg_close_connection(conn); + ASSERT((conn = httplib_download("google.com", 80, 0, ebuf, sizeof(ebuf), "%s", "GET / HTTP/1.0\r\n\r\n")) != NULL); + httplib_close_connection(conn); } /* Fetch unit_test.c, should succeed */ - ASSERT((conn = mg_download("localhost", - port, - use_ssl, - ebuf, - sizeof(ebuf), - "%s", - "GET /unit_test.c HTTP/1.0\r\n\r\n")) != NULL); - ASSERT(&conn->request_info == mg_get_request_info(conn)); + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "GET /unit_test.c HTTP/1.0\r\n\r\n")) != NULL); + ASSERT(&conn->request_info == httplib_get_request_info(conn)); ASSERT(!strcmp(conn->request_info.uri, "200")); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT((p2 = read_file("unit_test.c", &len2)) != NULL); ASSERT(len1 == len2); ASSERT(memcmp(p1, p2, len1) == 0); - mg_free(p1); - mg_free(p2); - mg_close_connection(conn); + httplib_free(p1); + httplib_free(p2); + httplib_close_connection(conn); /* Fetch in-memory file, should succeed. */ - ASSERT((conn = mg_download("localhost", - port, - use_ssl, - ebuf, - sizeof(ebuf), - "%s", - "GET /blah HTTP/1.1\r\n\r\n")) != NULL); + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "GET /blah HTTP/1.1\r\n\r\n")) != NULL); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT(len1 == (int)strlen(inmemory_file_data)); ASSERT(memcmp(p1, inmemory_file_data, len1) == 0); - mg_free(p1); - mg_close_connection(conn); + httplib_free(p1); + httplib_close_connection(conn); /* Fetch in-memory data with no Content-Length, should succeed. */ - ASSERT((conn = mg_download("localhost", - port, - use_ssl, - ebuf, - sizeof(ebuf), - "%s", - "GET /data/all HTTP/1.1\r\n\r\n")) != NULL); + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "GET /data/all HTTP/1.1\r\n\r\n")) != NULL); ASSERT(conn->request_info.content_length == fetch_data_size); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT(len1 == (int)fetch_data_size); ASSERT(memcmp(p1, fetch_data, len1) == 0); - mg_free(p1); - mg_close_connection(conn); + httplib_free(p1); + httplib_close_connection(conn); /* Fetch in-memory data with no Content-Length, should succeed. */ for (i = 0; i <= 1024 * /* 1024 * */ 8; i += (i < 2 ? 1 : i)) { - ASSERT((conn = mg_download("localhost", - port, - use_ssl, - ebuf, - sizeof(ebuf), - "GET /data/%i HTTP/1.1\r\n\r\n", - i)) != NULL); + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "GET /data/%i HTTP/1.1\r\n\r\n", i)) != NULL); ASSERT(conn->request_info.content_length == i); len1 = -1; p1 = read_conn(conn, &len1); @@ -609,13 +559,13 @@ static void test_mg_download(int use_ssl) ASSERT(memcmp(p1, fetch_data, fetch_data_size) == 0); } - mg_free(p1); - mg_close_connection(conn); + httplib_free(p1); + httplib_close_connection(conn); } /* Fetch data with Content-Length, should succeed and return the defined * length. */ - ASSERT((conn = mg_download( + ASSERT((conn = httplib_download( "localhost", port, use_ssl, @@ -624,7 +574,7 @@ static void test_mg_download(int use_ssl) "POST /content_length HTTP/1.1\r\nContent-Length: %u\r\n\r\n%s", (unsigned)strlen(test_data), test_data)) != NULL); - h = mg_get_header(conn, "Content-Length"); + h = httplib_get_header(conn, "Content-Length"); ASSERT((h != NULL) && (atoi(h) == (int)strlen(test_data))); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT(len1 == (int)strlen(test_data)); @@ -633,13 +583,13 @@ static void test_mg_download(int use_ssl) ASSERT(strcmp(conn->request_info.request_method, "HTTP/1.1") == 0); ASSERT(strcmp(conn->request_info.uri, "200") == 0); ASSERT(strcmp(conn->request_info.http_version, "OK") == 0); - mg_free(p1); - mg_close_connection(conn); + httplib_free(p1); + httplib_close_connection(conn); /* A POST request without Content-Length set is only valid, if the request * used Transfer-Encoding: chunked. Otherwise, it is an HTTP protocol * violation. Here we send a chunked request, the reply is not chunked. */ - ASSERT((conn = mg_download("localhost", + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, @@ -650,17 +600,17 @@ static void test_mg_download(int use_ssl) "\r\n%x\r\n%s\r\n0\r\n\r\n", (uint32_t)strlen(test_data), test_data)) != NULL); - h = mg_get_header(conn, "Content-Length"); + h = httplib_get_header(conn, "Content-Length"); ASSERT(h == NULL); ASSERT(conn->request_info.content_length == -1); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT(len1 == (int)strlen(test_data)); ASSERT(memcmp(p1, test_data, len1) == 0); - mg_free(p1); - mg_close_connection(conn); + httplib_free(p1); + httplib_close_connection(conn); /* Another chunked POST request with different chunk sizes. */ - ASSERT((conn = mg_download("localhost", + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, @@ -683,78 +633,62 @@ static void test_mg_download(int use_ssl) test_data[6], (uint32_t)strlen(test_data + 7), test_data + 7)) != NULL); - h = mg_get_header(conn, "Content-Length"); + h = httplib_get_header(conn, "Content-Length"); ASSERT(h == NULL); ASSERT(conn->request_info.content_length == -1); ASSERT((p1 = read_conn(conn, &len1)) != NULL); ASSERT(len1 == (int)strlen(test_data)); ASSERT(memcmp(p1, test_data, len1) == 0); - mg_free(p1); - mg_close_connection(conn); + httplib_free(p1); + httplib_close_connection(conn); /* Test non existent */ - ASSERT((conn = mg_download("localhost", - port, - use_ssl, - ebuf, - sizeof(ebuf), - "%s", - "GET /non_exist HTTP/1.1\r\n\r\n")) != NULL); + ASSERT((conn = httplib_download("localhost", port, use_ssl, ebuf, sizeof(ebuf), "%s", "GET /non_exist HTTP/1.1\r\n\r\n")) != NULL); ASSERT(strcmp(conn->request_info.request_method, "HTTP/1.1") == 0); ASSERT(strcmp(conn->request_info.uri, "404") == 0); ASSERT(strcmp(conn->request_info.http_version, "Not Found") == 0); - mg_close_connection(conn); + httplib_close_connection(conn); if (use_ssl) { #ifndef NO_SSL /* Test SSL redirect */ - ASSERT((conn = mg_download("localhost", - atoi(HTTP_REDIRECT_PORT), - 0, - ebuf, - sizeof(ebuf), - "%s", - "GET /data/4711 HTTP/1.1\r\n\r\n")) != NULL); + ASSERT((conn = httplib_download("localhost", atoi(HTTP_REDIRECT_PORT), 0, ebuf, sizeof(ebuf), "%s", "GET /data/4711 HTTP/1.1\r\n\r\n")) != NULL); ASSERT(strcmp(conn->request_info.uri, "302") == 0); - h = mg_get_header(conn, "Location"); + h = httplib_get_header(conn, "Location"); ASSERT(h != NULL); ASSERT(strcmp(h, "https://127.0.0.1:" HTTPS_PORT "/data/4711") == 0); - mg_close_connection(conn); + httplib_close_connection(conn); #endif } /* Test new API */ ebuf[0] = 1; - conn = mg_connect_client("localhost", port, use_ssl, ebuf, sizeof(ebuf)); + conn = httplib_connect_client("localhost", port, use_ssl, ebuf, sizeof(ebuf)); ASSERT(conn != NULL); ASSERT(ebuf[0] == 0); - ri = mg_get_request_info(conn); + ri = httplib_get_request_info(conn); ASSERT(ri->content_length == 0); - i = mg_get_response(conn, ebuf, sizeof(ebuf), 1000); + i = httplib_get_response(conn, ebuf, sizeof(ebuf), 1000); ASSERT(ebuf[0] != 0); - ri = mg_get_request_info(conn); + ri = httplib_get_request_info(conn); ASSERT(ri->content_length == -1); - mg_printf(conn, "GET /index.html HTTP/1.1\r\n"); - mg_printf(conn, "Host: www.example.com\r\n"); - mg_printf(conn, "\r\n"); - i = mg_get_response(conn, ebuf, sizeof(ebuf), 1000); + httplib_printf(conn, "GET /index.html HTTP/1.1\r\n"); + httplib_printf(conn, "Host: www.example.com\r\n"); + httplib_printf(conn, "\r\n"); + i = httplib_get_response(conn, ebuf, sizeof(ebuf), 1000); ASSERT(ebuf[0] == 0); - ri = mg_get_request_info(conn); + ri = httplib_get_request_info(conn); ASSERT(ri->content_length > 0); - mg_read(conn, ebuf, sizeof(ebuf)); + httplib_read(conn, ebuf, sizeof(ebuf)); ASSERT(!strncmp(ebuf, "Error 404", 9)); - mg_close_connection(conn); + httplib_close_connection(conn); /* Stop the test server */ - ut_mg_stop(ctx); + ut_httplib_stop(ctx); } -static int websocket_data_handler(const struct mg_connection *conn, - int flags, - char *data, - size_t data_len, - void *cbdata) +static int websocket_data_handler(const struct httplib_connection *conn, int flags, char *data, size_t data_len, void *cbdata) { (void)conn; (void)flags; @@ -764,43 +698,43 @@ static int websocket_data_handler(const struct mg_connection *conn, return 1; } -static void test_mg_websocket_client_connect(int use_ssl) +static void test_httplib_websocket_client_connect(int use_ssl) { - struct mg_connection *conn; + struct httplib_connection *conn; char ebuf[100]; int port; - struct mg_context *ctx; + struct httplib_context *ctx; if (use_ssl) port = atoi(HTTPS_PORT); else port = atoi(HTTP_PORT); - ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL); + ASSERT((ctx = httplib_start(&CALLBACKS, NULL, OPTIONS)) != NULL); /* Try to connect to our own server */ /* Invalid port test */ - conn = mg_connect_websocket_client("localhost", + conn = httplib_connect_websocket_client("localhost", 0, use_ssl, ebuf, sizeof(ebuf), "/", "http://localhost", - (mg_websocket_data_handler)websocket_data_handler, + (httplib_websocket_data_handler)websocket_data_handler, NULL, NULL); ASSERT(conn == NULL); /* Should succeed, the default civetweb server should complete the handshake */ - conn = mg_connect_websocket_client("localhost", + conn = httplib_connect_websocket_client("localhost", port, use_ssl, ebuf, sizeof(ebuf), "/", "http://localhost", - (mg_websocket_data_handler)websocket_data_handler, + (httplib_websocket_data_handler)websocket_data_handler, NULL, NULL); ASSERT(conn != NULL); @@ -812,45 +746,45 @@ static void test_mg_websocket_client_connect(int use_ssl) } /* Not a websocket server path */ - conn = mg_connect_websocket_client("websocket.org", + conn = httplib_connect_websocket_client("websocket.org", port, use_ssl, ebuf, sizeof(ebuf), "/", "http://websocket.org", - (mg_websocket_data_handler)websocket_data_handler, + (httplib_websocket_data_handler)websocket_data_handler, NULL, NULL); ASSERT(conn == NULL); /* Invalid port test */ - conn = mg_connect_websocket_client("echo.websocket.org", + conn = httplib_connect_websocket_client("echo.websocket.org", 0, use_ssl, ebuf, sizeof(ebuf), "/", "http://websocket.org", - (mg_websocket_data_handler)websocket_data_handler, + (httplib_websocket_data_handler)websocket_data_handler, NULL, NULL); ASSERT(conn == NULL); /* Should succeed, echo.websocket.org echos the data back */ - conn = mg_connect_websocket_client("echo.websocket.org", + conn = httplib_connect_websocket_client("echo.websocket.org", port, use_ssl, ebuf, sizeof(ebuf), "/", "http://websocket.org", - (mg_websocket_data_handler)websocket_data_handler, + (httplib_websocket_data_handler)websocket_data_handler, NULL, NULL); ASSERT(conn != NULL); - ut_mg_stop(ctx); + ut_httplib_stop(ctx); } static int alloc_printf(char **out_buf, char *buf, size_t size, char *fmt, ...) @@ -863,19 +797,19 @@ static int alloc_printf(char **out_buf, char *buf, size_t size, char *fmt, ...) return ret; } -static void test_mg_upload(void) +static void test_httplib_upload(void) { static const char *boundary = "OOO___MY_BOUNDARY___OOO"; - struct mg_context *ctx; + struct httplib_context *ctx; #if 0 - struct mg_connection *conn; + struct httplib_connection *conn; char ebuf[100], buf[20], *file2_data; int file2_len; #endif char *file_data, *post_data; int file_len, post_data_len; - ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL); + ASSERT((ctx = httplib_start(&CALLBACKS, NULL, OPTIONS)) != NULL); /* Upload one file */ ASSERT((file_data = read_file("unit_test.c", &file_len)) != NULL); @@ -897,7 +831,7 @@ static void test_mg_upload(void) ASSERT(post_data_len > 0); #if 0 /* TODO (bel): ... */ - ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1, + ASSERT((conn = httplib_download("localhost", atoi(HTTPS_PORT), 1, ebuf, sizeof(ebuf), "POST /upload?1 HTTP/1.1\r\n" "Content-Length: %d\r\n" @@ -905,10 +839,10 @@ static void test_mg_upload(void) "boundary=%s\r\n\r\n" "%.*s", post_data_len, boundary, post_data_len, post_data)) != NULL); - mg_free(file_data), mg_free(post_data); - ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message)); + httplib_free(file_data), httplib_free(post_data); + ASSERT(httplib_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message)); ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0); - mg_close_connection(conn); + httplib_close_connection(conn); /* Upload two files */ ASSERT((file_data = read_file("include/civetweb.h", &file_len)) != NULL); @@ -937,7 +871,7 @@ static void test_mg_upload(void) file2_len, file2_data, boundary); ASSERT(post_data_len > 0); - ASSERT((conn = mg_download("localhost", atoi(HTTPS_PORT), 1, + ASSERT((conn = httplib_download("localhost", atoi(HTTPS_PORT), 1, ebuf, sizeof(ebuf), "POST /upload?2 HTTP/1.1\r\n" "Content-Length: %d\r\n" @@ -945,13 +879,13 @@ static void test_mg_upload(void) "boundary=%s\r\n\r\n" "%.*s", post_data_len, boundary, post_data_len, post_data)) != NULL); - mg_free(file_data), mg_free(file2_data), mg_free(post_data); - ASSERT(mg_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message)); + httplib_free(file_data), httplib_free(file2_data), httplib_free(post_data); + ASSERT(httplib_read(conn, buf, sizeof(buf)) == (int) strlen(upload_ok_message)); ASSERT(memcmp(buf, upload_ok_message, strlen(upload_ok_message)) == 0); - mg_close_connection(conn); + httplib_close_connection(conn); #endif - ut_mg_stop(ctx); + ut_httplib_stop(ctx); } static void test_base64_encode(void) @@ -967,29 +901,29 @@ static void test_base64_encode(void) } } -static void test_mg_get_var(void) +static void test_httplib_get_var(void) { static const char *post[] = {"a=1&&b=2&d&=&c=3%20&e=", "q=&st=2012%2F11%2F13+17%3A05&et=&team_id=", NULL}; char buf[20]; - ASSERT(mg_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "a", buf, sizeof(buf)) == 1); ASSERT(buf[0] == '1' && buf[1] == '\0'); - ASSERT(mg_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "b", buf, sizeof(buf)) == 1); ASSERT(buf[0] == '2' && buf[1] == '\0'); - ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "c", buf, sizeof(buf)) == 2); ASSERT(buf[0] == '3' && buf[1] == ' ' && buf[2] == '\0'); - ASSERT(mg_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "e", buf, sizeof(buf)) == 0); ASSERT(buf[0] == '\0'); - ASSERT(mg_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1); - ASSERT(mg_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "d", buf, sizeof(buf)) == -1); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "c", buf, 2) == -2); - ASSERT(mg_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2); - ASSERT(mg_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2); - ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2); - ASSERT(mg_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "x", NULL, 10) == -2); + ASSERT(httplib_get_var(post[0], strlen(post[0]), "x", buf, 0) == -2); + ASSERT(httplib_get_var(post[1], strlen(post[1]), "st", buf, 16) == -2); + ASSERT(httplib_get_var(post[1], strlen(post[1]), "st", buf, 17) == 16); } static void test_set_throttle(void) @@ -1027,7 +961,7 @@ static void check_lua_expr(lua_State *L, const char *expr, const char *value) const char *v, *var_name = "myVar"; char buf[100]; - mg_snprintf(buf, sizeof(buf), "%s = %s", var_name, expr); + httplib_snprintf(buf, sizeof(buf), "%s = %s", var_name, expr); (void)luaL_dostring(L, buf); lua_getglobal(L, var_name); v = lua_tostring(L, -1); @@ -1037,8 +971,8 @@ static void check_lua_expr(lua_State *L, const char *expr, const char *value) static void test_lua(void) { - static struct mg_connection conn; - static struct mg_context ctx; + static struct httplib_connection conn; + static struct httplib_context ctx; char http_request[] = "POST /foo/bar HTTP/1.1\r\n" "Content-Length: 12\r\n" @@ -1069,11 +1003,11 @@ static void test_lua(void) } #endif -static void test_mg_stat(void) +static void test_httplib_stat(void) { - static struct mg_context ctx; + static struct httplib_context ctx; struct file file = STRUCT_FILE_INITIALIZER; - ASSERT(!mg_stat(fc(&ctx), " does not exist ", &file)); + ASSERT(!httplib_stat(fc(&ctx), " does not exist ", &file)); } static void test_skip_quoted(void) @@ -1106,15 +1040,15 @@ static void test_alloc_vprintf(void) /* Pass small buffer, make sure alloc_printf allocates */ ASSERT(alloc_printf(&p, buf, 1, "%s", "hello") == 5); ASSERT(p != buf); - mg_free(p); + httplib_free(p); } static void test_request_replies(void) { char ebuf[100]; int i; - struct mg_connection *conn; - struct mg_context *ctx; + struct httplib_connection *conn; + struct httplib_context *ctx; static struct { const char *request, *reply_regex; } tests[] = { @@ -1123,36 +1057,24 @@ static void test_request_replies(void) {NULL, NULL}, }; - ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL); + ASSERT((ctx = httplib_start(&CALLBACKS, NULL, OPTIONS)) != NULL); for (i = 0; tests[i].request != NULL; i++) { - ASSERT((conn = mg_download("localhost", - atoi(HTTP_PORT), - 0, - ebuf, - sizeof(ebuf), - "%s", - tests[i].request)) != NULL); - mg_close_connection(conn); + ASSERT((conn = httplib_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s", tests[i].request)) != NULL); + httplib_close_connection(conn); } - ut_mg_stop(ctx); + ut_httplib_stop(ctx); #ifndef NO_SSL - ASSERT((ctx = mg_start(&CALLBACKS, NULL, OPTIONS)) != NULL); + ASSERT((ctx = httplib_start(&CALLBACKS, NULL, OPTIONS)) != NULL); for (i = 0; tests[i].request != NULL; i++) { - ASSERT((conn = mg_download("localhost", - atoi(HTTPS_PORT), - 1, - ebuf, - sizeof(ebuf), - "%s", - tests[i].request)) != NULL); - mg_close_connection(conn); + ASSERT((conn = httplib_download("localhost", atoi(HTTPS_PORT), 1, ebuf, sizeof(ebuf), "%s", tests[i].request)) != NULL); + httplib_close_connection(conn); } - ut_mg_stop(ctx); + ut_httplib_stop(ctx); #endif } -static int request_test_handler(struct mg_connection *conn, void *cbdata) +static int request_test_handler(struct httplib_connection *conn, void *cbdata) { int i; char chunk_data[32]; @@ -1160,18 +1082,18 @@ static int request_test_handler(struct mg_connection *conn, void *cbdata) ASSERT(cbdata == (void *)7); strcpy(chunk_data, "123456789A123456789B123456789C"); - mg_printf(conn, + httplib_printf(conn, "HTTP/1.1 200 OK\r\n" "Transfer-Encoding: chunked\r\n" "Content-Type: text/plain\r\n\r\n"); for (i = 0; i < 20; i++) { - mg_printf(conn, "%x\r\n", i); - mg_write(conn, chunk_data, i); - mg_printf(conn, "\r\n"); + httplib_printf(conn, "%x\r\n", i); + httplib_write(conn, chunk_data, i); + httplib_printf(conn, "\r\n"); } - mg_printf(conn, "0\r\n\r\n"); + httplib_printf(conn, "0\r\n\r\n"); return 1; } @@ -1180,139 +1102,132 @@ static int request_test_handler(struct mg_connection *conn, void *cbdata) static void test_request_handlers(void) { char ebuf[100]; - struct mg_context *ctx; - struct mg_connection *conn; + struct httplib_context *ctx; + struct httplib_connection *conn; char uri[64]; int i; const char *request = "GET /U7 HTTP/1.0\r\n\r\n"; - ctx = mg_start(NULL, NULL, OPTIONS); + ctx = httplib_start(NULL, NULL, OPTIONS); ASSERT(ctx != NULL); for (i = 0; i < 1000; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, request_test_handler, NULL); + httplib_set_request_handler(ctx, uri, request_test_handler, NULL); } for (i = 500; i < 800; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)1); + httplib_set_request_handler(ctx, uri, NULL, (void *)1); } for (i = 600; i >= 0; i--) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)2); + httplib_set_request_handler(ctx, uri, NULL, (void *)2); } for (i = 750; i <= 1000; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, NULL, (void *)3); + httplib_set_request_handler(ctx, uri, NULL, (void *)3); } for (i = 5; i < 9; i++) { sprintf(uri, "/U%u", i); - mg_set_request_handler(ctx, uri, request_test_handler, (void *)(intptr_t)i); + httplib_set_request_handler(ctx, uri, request_test_handler, (void *)(intptr_t)i); } - conn = mg_download( - "localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s", request); + conn = httplib_download( "localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s", request); ASSERT(conn != NULL); - mg_sleep(1000); - mg_close_connection(conn); + httplib_sleep(1000); + httplib_close_connection(conn); - ut_mg_stop(ctx); + ut_httplib_stop(ctx); } -static int api_callback(struct mg_connection *conn) +static int api_callback(struct httplib_connection *conn) { - const struct mg_request_info *ri = mg_get_request_info(conn); + const struct httplib_request_info *ri = httplib_get_request_info(conn); char post_data[100] = ""; ASSERT(ri->user_data == (void *)123); ASSERT(ri->num_headers == 2); - ASSERT(strcmp(mg_get_header(conn, "host"), "blah.com") == 0); - ASSERT(mg_read(conn, post_data, sizeof(post_data)) == 3); + ASSERT(strcmp(httplib_get_header(conn, "host"), "blah.com") == 0); + ASSERT(httplib_read(conn, post_data, sizeof(post_data)) == 3); ASSERT(memcmp(post_data, "b=1", 3) == 0); ASSERT(ri->query_string != NULL); ASSERT(ri->remote_addr[0] != 0); ASSERT(ri->remote_port > 0); ASSERT(strcmp(ri->http_version, "1.0") == 0); - mg_printf(conn, "HTTP/1.0 200 OK\r\n\r\n"); + httplib_printf(conn, "HTTP/1.0 200 OK\r\n\r\n"); return 1; } static void test_api_calls(void) { char ebuf[100]; - struct mg_callbacks callbacks; - struct mg_connection *conn; - struct mg_context *ctx; + struct httplib_callbacks callbacks; + struct httplib_connection *conn; + struct httplib_context *ctx; static const char *request = "POST /?a=%20&b=&c=xx HTTP/1.0\r\n" "Host: blah.com\n" /* More spaces before */ "content-length: 3\r\n" /* Lower case header name */ - "\r\nb=123456"; /* Content size > content-length, test for mg_read() */ + "\r\nb=123456"; /* Content size > content-length, test for httplib_read() */ memset(&callbacks, 0, sizeof(callbacks)); callbacks.begin_request = api_callback; - ASSERT((ctx = mg_start(&callbacks, (void *)123, OPTIONS)) != NULL); - ASSERT((conn = mg_download("localhost", - atoi(HTTP_PORT), - 0, - ebuf, - sizeof(ebuf), - "%s", - request)) != NULL); - mg_close_connection(conn); - ut_mg_stop(ctx); + ASSERT((ctx = httplib_start(&callbacks, (void *)123, OPTIONS)) != NULL); + ASSERT((conn = httplib_download("localhost", atoi(HTTP_PORT), 0, ebuf, sizeof(ebuf), "%s", request)) != NULL); + httplib_close_connection(conn); + ut_httplib_stop(ctx); } static void test_url_decode(void) { char buf[100]; - ASSERT(mg_url_decode("foo", 3, buf, 3, 0) == -1); /* No space for \0 */ - ASSERT(mg_url_decode("foo", 3, buf, 4, 0) == 3); + ASSERT(httplib_url_decode("foo", 3, buf, 3, 0) == -1); /* No space for \0 */ + ASSERT(httplib_url_decode("foo", 3, buf, 4, 0) == 3); ASSERT(strcmp(buf, "foo") == 0); - ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 0) == 2); + ASSERT(httplib_url_decode("a+", 2, buf, sizeof(buf), 0) == 2); ASSERT(strcmp(buf, "a+") == 0); - ASSERT(mg_url_decode("a+", 2, buf, sizeof(buf), 1) == 2); + ASSERT(httplib_url_decode("a+", 2, buf, sizeof(buf), 1) == 2); ASSERT(strcmp(buf, "a ") == 0); - ASSERT(mg_url_decode("%61", 1, buf, sizeof(buf), 1) == 1); + ASSERT(httplib_url_decode("%61", 1, buf, sizeof(buf), 1) == 1); ASSERT(strcmp(buf, "%") == 0); - ASSERT(mg_url_decode("%61", 2, buf, sizeof(buf), 1) == 2); + ASSERT(httplib_url_decode("%61", 2, buf, sizeof(buf), 1) == 2); ASSERT(strcmp(buf, "%6") == 0); - ASSERT(mg_url_decode("%61", 3, buf, sizeof(buf), 1) == 1); + ASSERT(httplib_url_decode("%61", 3, buf, sizeof(buf), 1) == 1); ASSERT(strcmp(buf, "a") == 0); } -static void test_mg_strcasestr(void) +static void test_httplib_strcasestr(void) { static const char *big1 = "abcdef"; - ASSERT(mg_strcasestr("Y", "X") == NULL); - ASSERT(mg_strcasestr("Y", "y") != NULL); - ASSERT(mg_strcasestr(big1, "X") == NULL); - ASSERT(mg_strcasestr(big1, "CD") == big1 + 2); - ASSERT(mg_strcasestr("aa", "AAB") == NULL); + ASSERT(httplib_strcasestr("Y", "X") == NULL); + ASSERT(httplib_strcasestr("Y", "y") != NULL); + ASSERT(httplib_strcasestr(big1, "X") == NULL); + ASSERT(httplib_strcasestr(big1, "CD") == big1 + 2); + ASSERT(httplib_strcasestr("aa", "AAB") == NULL); } -static void test_mg_get_cookie(void) +static void test_httplib_get_cookie(void) { char buf[20]; - ASSERT(mg_get_cookie("", "foo", NULL, sizeof(buf)) == -2); - ASSERT(mg_get_cookie("", "foo", buf, 0) == -2); - ASSERT(mg_get_cookie("", "foo", buf, sizeof(buf)) == -1); - ASSERT(mg_get_cookie("", NULL, buf, sizeof(buf)) == -1); - ASSERT(mg_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1); + ASSERT(httplib_get_cookie("", "foo", NULL, sizeof(buf)) == -2); + ASSERT(httplib_get_cookie("", "foo", buf, 0) == -2); + ASSERT(httplib_get_cookie("", "foo", buf, sizeof(buf)) == -1); + ASSERT(httplib_get_cookie("", NULL, buf, sizeof(buf)) == -1); + ASSERT(httplib_get_cookie("a=1; b=2; c; d", "a", buf, sizeof(buf)) == 1); ASSERT(strcmp(buf, "1") == 0); - ASSERT(mg_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1); + ASSERT(httplib_get_cookie("a=1; b=2; c; d", "b", buf, sizeof(buf)) == 1); ASSERT(strcmp(buf, "2") == 0); - ASSERT(mg_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3); + ASSERT(httplib_get_cookie("a=1; b=123", "b", buf, sizeof(buf)) == 3); ASSERT(strcmp(buf, "123") == 0); - ASSERT(mg_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1); + ASSERT(httplib_get_cookie("a=1; b=2; c; d", "c", buf, sizeof(buf)) == -1); } static void test_strtoll(void) @@ -1392,7 +1307,7 @@ static void test_md5(void) md5_val[15]); ASSERT(strcmp(md5_str, "d41d8cd98f00b204e9800998ecf8427e") == 0); - mg_md5(md5_str, "", NULL); + httplib_md5(md5_str, "", NULL); ASSERT(strcmp(md5_str, "d41d8cd98f00b204e9800998ecf8427e") == 0); md5_init(&md5_state); @@ -1418,10 +1333,10 @@ static void test_md5(void) md5_val[15]); ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6") == 0); - mg_md5(md5_str, test_str, NULL); + httplib_md5(md5_str, test_str, NULL); ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6") == 0); - mg_md5(md5_str, + httplib_md5(md5_str, "The", " ", "quick brown fox", @@ -1433,7 +1348,7 @@ static void test_md5(void) NULL); ASSERT(strcmp(md5_str, "9e107d9d372bb6826bd81d3542a419d6") == 0); - mg_md5(md5_str, "civetweb", NULL); + httplib_md5(md5_str, "civetweb", NULL); ASSERT(strcmp(md5_str, "95c098bd85b619b24a83d9cea5e8ba54") == 0); } @@ -1442,11 +1357,11 @@ int __cdecl main(void) char buffer[512]; FILE *f; - struct mg_context *ctx; + struct httplib_context *ctx; int i; /* print headline */ - printf("Civetweb %s unit test\n", mg_version()); + printf("Civetweb %s unit test\n", httplib_version()); #if defined(_WIN32) GetCurrentDirectoryA(sizeof(buffer), buffer); #else @@ -1469,48 +1384,48 @@ int __cdecl main(void) /* test local functions */ test_parse_port_string(); - test_mg_strcasestr(); + test_httplib_strcasestr(); test_alloc_vprintf(); test_base64_encode(); test_match_prefix(); test_remove_double_dots(); test_should_keep_alive(); test_parse_http_message(); - test_mg_get_var(); + test_httplib_get_var(); test_set_throttle(); test_next_option(); - test_mg_stat(); + test_httplib_stat(); test_skip_quoted(); test_url_decode(); - test_mg_get_cookie(); + test_httplib_get_cookie(); test_strtoll(); test_md5(); /* start stop server */ - ctx = mg_start(NULL, NULL, OPTIONS); + ctx = httplib_start(NULL, NULL, OPTIONS); REQUIRE(ctx != NULL); - mg_sleep(1000); - ut_mg_stop(ctx); + httplib_sleep(1000); + ut_httplib_stop(ctx); /* create test data */ - fetch_data = (char *)mg_malloc(fetch_data_size); + fetch_data = (char *)httplib_malloc(fetch_data_size); for (i = 0; i < fetch_data_size; i++) { fetch_data[i] = 'a' + i % 10; } /* tests with network access */ init_CALLBACKS(); - test_mg_download(0); + test_httplib_download(0); #ifndef NO_SSL - test_mg_download(1); + test_httplib_download(1); #endif - test_mg_websocket_client_connect(0); + test_httplib_websocket_client_connect(0); #ifndef NO_SSL - test_mg_websocket_client_connect(1); + test_httplib_websocket_client_connect(1); #endif - test_mg_upload(); + test_httplib_upload(); test_request_replies(); test_api_calls(); test_request_handlers(); @@ -1520,16 +1435,16 @@ int __cdecl main(void) #endif /* test completed */ - mg_free(fetch_data); + httplib_free(fetch_data); #ifdef MEMORY_DEBUGGING { - extern unsigned long mg_memory_debug_blockCount; - extern unsigned long mg_memory_debug_totalMemUsed; + extern unsigned long httplib_memory_debug_blockCount; + extern unsigned long httplib_memory_debug_totalMemUsed; printf("MEMORY DEBUGGING: %lu %lu\n", - mg_memory_debug_blockCount, - mg_memory_debug_totalMemUsed); + httplib_memory_debug_blockCount, + httplib_memory_debug_totalMemUsed); } #endif