diff --git a/Makefile b/Makefile index ab3ce4d..be85af3 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,26 @@ LFLAGS = -lssl -lcrypto CFILES = $(shell find src -name "*.c") HFILES = $(shell find src -name "*.h") -all: chttp.c chttp.h +EXAMPLES_CLIENT := $(basename $(shell ls examples/client/*.c 2>/dev/null)) +EXAMPLES_SERVER := $(basename $(shell ls examples/server/*.c 2>/dev/null)) +EXAMPLES_ENGINE := $(basename $(shell ls examples/engine/*.c 2>/dev/null)) + +all: chttp.c chttp.h examples chttp.c chttp.h: $(HFILES) $(CFILES) python misc/amalg.py +examples: $(EXAMPLES_CLIENT) $(EXAMPLES_SERVER) $(EXAMPLES_ENGINE) + +examples/client/%: examples/client/%.c chttp.c chttp.h + $(CC) $(CFLAGS) $< chttp.c -o $@ $(LFLAGS) + +examples/server/%: examples/server/%.c chttp.c chttp.h + $(CC) $(CFLAGS) $< chttp.c -o $@ $(LFLAGS) + +examples/engine/%: examples/engine/%.c chttp.c chttp.h + $(CC) $(CFLAGS) $< chttp.c -o $@ $(LFLAGS) + clean: rm -f client_example server_example + rm -f examples/client/* examples/server/* examples/engine/* diff --git a/README.md b/README.md index dba586e..b90f1e8 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ int main(void) And this is an HTTP server: ```c -#include +#include int main(void) { diff --git a/chttp.c b/chttp.c index 96c6861..5fec3ac 100644 --- a/chttp.c +++ b/chttp.c @@ -1,9 +1,9 @@ /* - * HTTP Library - Amalgamated Source + * cHTTP Library - Amalgamated Source * Generated automatically - do not edit manually */ -#include "http.h" +#include "chttp.h" #include #include @@ -2075,16 +2075,14 @@ void http_engine_status(HTTP_Engine *eng, int status) eng->state = HTTP_ENGINE_STATE_SERVER_PREP_HEADER; } -void http_engine_header(HTTP_Engine *eng, const char *src, int len) +void http_engine_header(HTTP_Engine *eng, HTTP_String str) { if ((eng->state & HTTP_ENGINE_STATEBIT_PREP_HEADER) == 0) return; - if (len < 0) len = strlen(src); - // TODO: Check that the header is valid - byte_queue_write(&eng->output, src, len); + byte_queue_write(&eng->output, str.ptr, str.len); byte_queue_write(&eng->output, "\r\n", 2); } @@ -2149,16 +2147,14 @@ static void complete_message_body(HTTP_Engine *eng) byte_queue_patch(&eng->output, eng->content_length_value_offset, tmp + i, 10 - i); } -void http_engine_body(HTTP_Engine *eng, void *src, int len) +void http_engine_body(HTTP_Engine *eng, HTTP_String str) { - if (len < 0) len = strlen(src); - - http_engine_bodycap(eng, len); + http_engine_bodycap(eng, str.len); int cap; char *buf = http_engine_bodybuf(eng, &cap); if (buf) { - memcpy(buf, src, len); - http_engine_bodyack(eng, len); + memcpy(buf, str.ptr, str.len); + http_engine_bodyack(eng, str.len); } } @@ -3013,15 +3009,13 @@ void socket_free(Socket *sock) { } } -#define COUNT(X) (sizeof(X) / sizeof((X)[0])) - -int socket_wait(Socket **socks, int num_socks) +int socket_wait(Socket **socks, int num_socks) // TODO: is this used? { if (num_socks <= 0) return -1; struct pollfd polled[100]; // TODO: make this value configurable - if (num_socks > (int) COUNT(polled)) + if (num_socks > (int) HTTP_COUNT(polled)) return -1; for (;;) { @@ -3370,7 +3364,7 @@ void http_request_line(HTTP_RequestHandle handle, HTTP_Method method, HTTP_Strin http_engine_url(&conn->engine, method, url, 1); } -void http_request_header(HTTP_RequestHandle handle, char *header, int len) +void http_request_header(HTTP_RequestHandle handle, HTTP_String str) { ClientConnection *conn = handle2clientconn(handle); if (conn == NULL) @@ -3378,10 +3372,10 @@ void http_request_header(HTTP_RequestHandle handle, char *header, int len) if (conn->state != CLIENT_CONNECTION_INIT) return; - http_engine_header(&conn->engine, header, len); + http_engine_header(&conn->engine, str); } -void http_request_body(HTTP_RequestHandle handle, char *body, int len) +void http_request_body(HTTP_RequestHandle handle, HTTP_String str) { ClientConnection *conn = handle2clientconn(handle); if (conn == NULL) @@ -3389,7 +3383,7 @@ void http_request_body(HTTP_RequestHandle handle, char *body, int len) if (conn->state != CLIENT_CONNECTION_INIT) return; - http_engine_body(&conn->engine, body, len); + http_engine_body(&conn->engine, str); } void http_request_submit(HTTP_RequestHandle handle) @@ -3429,6 +3423,85 @@ void http_request_free(HTTP_RequestHandle handle) socket_free(&conn->socket); conn->state = CLIENT_CONNECTION_FREE; client->num_conns--; +} + +static HTTP_Client *default_client___; // TODO: deinitialize the default client when http_global_free is called + +static HTTP_Client *get_default_client(void) +{ + if (default_client___ == NULL) + default_client___ = http_client_init(); + return default_client___; +} + +HTTP_Response *http_get(HTTP_String url, HTTP_String *headers, int num_headers, HTTP_RequestHandle *phandle) +{ + HTTP_Client *client = get_default_client(); + if (client == NULL) + return NULL; + + HTTP_RequestHandle handle; + int ret = http_client_request(client, &handle); + if (ret < 0) + return NULL; + + http_request_line(handle, HTTP_METHOD_GET, url); + + for (int i = 0; i < num_headers; i++) + http_request_header(handle, headers[i]); + + http_request_submit(handle); + + ret = http_client_wait(client, NULL); // TODO: it's assumed there is only one request pending + if (ret < 0) { + http_request_free(handle); // TODO: currently free only works on completed request handles + return NULL; + } + + HTTP_Response *res = http_request_result(handle); + if (res == NULL) { + http_request_free(handle); + return NULL; + } + + *phandle = handle; + return res; +} + +HTTP_Response *http_post(HTTP_String url, HTTP_String *headers, int num_headers, HTTP_String body, HTTP_RequestHandle *phandle) +{ + HTTP_Client *client = get_default_client(); + if (client == NULL) + return NULL; + + HTTP_RequestHandle handle; + int ret = http_client_request(client, &handle); + if (ret < 0) + return NULL; + + http_request_line(handle, HTTP_METHOD_GET, url); + + for (int i = 0; i < num_headers; i++) + http_request_header(handle, headers[i]); + + http_request_body(handle, body); + + http_request_submit(handle); + + ret = http_client_wait(client, NULL); // TODO: it's assumed there is only one request pending + if (ret < 0) { + http_request_free(handle); // TODO: currently free only works on completed request handles + return NULL; + } + + HTTP_Response *res = http_request_result(handle); + if (res == NULL) { + http_request_free(handle); + return NULL; + } + + *phandle = handle; + return res; }////////////////////////////////////////////////////////////////////// // src/server.c ////////////////////////////////////////////////////////////////////// @@ -3559,7 +3632,6 @@ HTTP_Server *http_server_init_ex(HTTP_String addr, uint16_t port, } } - server->num_websites = 0; server->num_conns = 0; server->ready_head = 0; server->ready_count = 0; @@ -3794,16 +3866,13 @@ void http_response_header(HTTP_ResponseHandle res, const char *fmt, ...) va_end(args); } -void http_response_body(HTTP_ResponseHandle res, char *src, int len) +void http_response_body(HTTP_ResponseHandle res, HTTP_String str) { Connection *conn = handle2conn(res); if (conn == NULL) return; - if (len < 0) - len = strlen(src); - - http_engine_body(&conn->engine, src, len); + http_engine_body(&conn->engine, str); } void http_response_bodycap(HTTP_ResponseHandle res, int mincap) @@ -4225,7 +4294,7 @@ static int serve_dynamic_route(Route *route, HTTP_Request *req, HTTP_ResponseHan int path_len = sanitize_path(req->url.path, path_mem, (int) sizeof(path_mem)); if (path_len < 0) { http_response_status(res, 400); - http_response_body(res, "Invalid path", -1); + http_response_body(res, HTTP_STR("Invalid path")); http_response_done(res); return 1; } @@ -4270,7 +4339,7 @@ int http_serve(char *addr, int port, HTTP_Router *router) { int ret; - HTTP_Server *server = http_server_init((HTTP_String) { addr, strlen(addr) }, port, 0, (HTTP_String) {}, (HTTP_String) {}); + HTTP_Server *server = http_server_init_ex((HTTP_String) { addr, strlen(addr) }, port, 0, (HTTP_String) {}, (HTTP_String) {}); if (server == NULL) { http_router_free(router); return -1; diff --git a/chttp.h b/chttp.h index 14cb620..500ddd0 100644 --- a/chttp.h +++ b/chttp.h @@ -1,5 +1,5 @@ /* - * HTTP Library - Amalgamated Header + * cHTTP Library - Amalgamated Header * Generated automatically - do not edit manually */ @@ -61,6 +61,9 @@ HTTP_String http_trim(HTTP_String s); // Returns the number of items of a static array. #define HTTP_COUNT(X) (sizeof(X) / sizeof((X)[0])) +// TODO: comment +#define HTTP_UNPACK(X) (X).len, (X).ptr + // Macro used to make invariants of the code more explicit. // // Say you have some function that operates on two integers @@ -288,8 +291,8 @@ HTTP_Response* http_engine_getres (HTTP_Engine *eng); void http_engine_url (HTTP_Engine *eng, HTTP_Method method, HTTP_String url, int minor); void http_engine_status (HTTP_Engine *eng, int status); -void http_engine_header (HTTP_Engine *eng, const char *src, int len); -void http_engine_body (HTTP_Engine *eng, void *src, int len); +void http_engine_header (HTTP_Engine *eng, HTTP_String str); +void http_engine_body (HTTP_Engine *eng, HTTP_String str); void http_engine_bodycap (HTTP_Engine *eng, int mincap); char* http_engine_bodybuf (HTTP_Engine *eng, int *cap); void http_engine_bodyack (HTTP_Engine *eng, int num); @@ -361,15 +364,11 @@ void http_request_line(HTTP_RequestHandle handle, HTTP_Method method, HTTP_Strin // Append a header to the specified request. You must call // this after http_request_line and may do so multiple times. -// -// TODO: use HTTP_String instead of char*+int -void http_request_header(HTTP_RequestHandle handle, char *header, int len); +void http_request_header(HTTP_RequestHandle handle, HTTP_String str); // Append some data to the request's body. You must call // this after either http_request_line or http_request_header. -// -// TODO: use HTTP_String instead of char*+int -void http_request_body(HTTP_RequestHandle handle, char *body, int len); +void http_request_body(HTTP_RequestHandle handle, HTTP_String str); // Mark the initialization of the request as completed and // perform the request. @@ -390,6 +389,16 @@ HTTP_Response *http_request_result(HTTP_RequestHandle handle); // TODO: allow aborting pending requests void http_request_free(HTTP_RequestHandle handle); +// TODO: comment +HTTP_Response *http_get(HTTP_String url, + HTTP_String *headers, int num_headers, + HTTP_RequestHandle *phandle); + +// TODO: comment +HTTP_Response *http_post(HTTP_String url, + HTTP_String *headers, int num_headers, + HTTP_String body, HTTP_RequestHandle *phandle); + ////////////////////////////////////////////////////////////////////// // src/server.h ////////////////////////////////////////////////////////////////////// @@ -412,7 +421,7 @@ int http_server_wait (HTTP_Server *server, HTTP_Request **req, H int http_server_add_website (HTTP_Server *server, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file); void http_response_status (HTTP_ResponseHandle res, int status); void http_response_header (HTTP_ResponseHandle res, const char *fmt, ...); -void http_response_body (HTTP_ResponseHandle res, char *src, int len); +void http_response_body (HTTP_ResponseHandle res, HTTP_String str); void http_response_bodycap (HTTP_ResponseHandle res, int mincap); char* http_response_bodybuf (HTTP_ResponseHandle res, int *cap); void http_response_bodyack (HTTP_ResponseHandle res, int num); diff --git a/examples/client/000_get_request b/examples/client/000_get_request new file mode 100644 index 0000000..ba61598 Binary files /dev/null and b/examples/client/000_get_request differ diff --git a/examples/client/000_get_request.c b/examples/client/000_get_request.c index 88148e3..4417d67 100644 --- a/examples/client/000_get_request.c +++ b/examples/client/000_get_request.c @@ -1,3 +1,4 @@ +#include #include // This is an example of how to use cHTTP to perform @@ -46,7 +47,7 @@ int main(void) ); } - printf("body: %.*s\n", res->body.len, res->body.ptr); + printf("body: %.*s\n", HTTP_UNPACK(res->body)); // When we are done reading from the response object // we must free the request's resources. diff --git a/examples/client/client_example b/examples/client/client_example new file mode 100644 index 0000000..c0dbefb Binary files /dev/null and b/examples/client/client_example differ diff --git a/examples/client/client_example.c b/examples/client/client_example.c index 064415f..e575226 100644 --- a/examples/client/client_example.c +++ b/examples/client/client_example.c @@ -1,8 +1,7 @@ -#include #include #include #include -#include "../http.h" +#include int main(int argc, char **argv) { if (argc < 2) { diff --git a/examples/client/web_crawler b/examples/client/web_crawler new file mode 100644 index 0000000..dd4fec8 Binary files /dev/null and b/examples/client/web_crawler differ diff --git a/examples/client/web_crawler.c b/examples/client/web_crawler.c index 2e531fb..b106ae7 100644 --- a/examples/client/web_crawler.c +++ b/examples/client/web_crawler.c @@ -2,9 +2,7 @@ #include #include #include -#include - -#define COUNT(X) (sizeof(X) / sizeof((X)[0])) +#include #define BLK "\e[0;30m" #define RED "\e[0;31m" @@ -149,7 +147,7 @@ int main(int argc, char **argv) return -1; } http_request_line(req, HTTP_METHOD_GET, start_url); - http_request_header(req, "User-Agent: Simple crawler", -1); + http_request_header(req, HTTP_STR("User-Agent: Simple crawler")); http_request_submit(req); for (;;) { @@ -188,7 +186,7 @@ int main(int argc, char **argv) continue; http_request_line(req, HTTP_METHOD_GET, url); - http_request_header(req, "User-Agent: Simple crawler", -1); + http_request_header(req, HTTP_STR("User-Agent: Simple crawler")); http_request_submit(req); } } diff --git a/examples/engine/blocking_server.c b/examples/engine/blocking_server.c index df75f9f..0a73c45 100644 --- a/examples/engine/blocking_server.c +++ b/examples/engine/blocking_server.c @@ -18,7 +18,7 @@ #define CLOSE_SOCKET close #endif -#include +#include // This example showcases how to use the engine interface // to build a blocking HTTP server that works on Windows diff --git a/examples/engine/iocp_server.c b/examples/engine/iocp_server.c index 84de3cd..81fbee4 100644 --- a/examples/engine/iocp_server.c +++ b/examples/engine/iocp_server.c @@ -1,7 +1,7 @@ #include #include #include -#include +#include #define MAX_CLIENTS (1<<10) diff --git a/examples/server/000_http_server b/examples/server/000_http_server new file mode 100644 index 0000000..c03d7ee Binary files /dev/null and b/examples/server/000_http_server differ diff --git a/examples/server/000_http_server.c b/examples/server/000_http_server.c index 82bf942..ffee546 100644 --- a/examples/server/000_http_server.c +++ b/examples/server/000_http_server.c @@ -1,5 +1,6 @@ +#include #include -#include +#include // This example shows how to set up a basic HTTP server @@ -30,7 +31,7 @@ int main(void) HTTP_ResponseHandle res; // Block until a request is available - int ret = http_server_wait(server, &res, &res); + int ret = http_server_wait(server, &req, &res); // The wait functions returns 0 on success and -1 // on error. By "error" I mean an unrecoverable diff --git a/examples/server/010_zero_copy_api b/examples/server/010_zero_copy_api new file mode 100644 index 0000000..5955f8c Binary files /dev/null and b/examples/server/010_zero_copy_api differ diff --git a/examples/server/010_zero_copy_api.c b/examples/server/010_zero_copy_api.c index ad442e7..239ebe2 100644 --- a/examples/server/010_zero_copy_api.c +++ b/examples/server/010_zero_copy_api.c @@ -1,4 +1,5 @@ -#include +#include +#include // This example shows how to generate response bodies // using the zero-copy API. @@ -18,7 +19,7 @@ int main(void) HTTP_Request *req; HTTP_ResponseHandle res; - int ret = http_server_wait(server, &res, &res); + int ret = http_server_wait(server, &req, &res); if (ret < 0) return -1; http_response_status(res, 200); diff --git a/examples/server/020_response_undo b/examples/server/020_response_undo new file mode 100644 index 0000000..0b2c31f Binary files /dev/null and b/examples/server/020_response_undo differ diff --git a/examples/server/020_response_undo.c b/examples/server/020_response_undo.c index b468293..2f8a31d 100644 --- a/examples/server/020_response_undo.c +++ b/examples/server/020_response_undo.c @@ -1,4 +1,5 @@ -#include +#include +#include // This example shows how undo a response that is being built // when an error occurs. @@ -14,7 +15,7 @@ int main(void) HTTP_Request *req; HTTP_ResponseHandle res; - int ret = http_server_wait(server, &res, &res); + int ret = http_server_wait(server, &req, &res); if (ret < 0) return -1; // Say we are building a request.. diff --git a/examples/server/030_using_workers.c b/examples/server/030_using_workers.c index 22a4176..9cc03fe 100644 --- a/examples/server/030_using_workers.c +++ b/examples/server/030_using_workers.c @@ -1,5 +1,5 @@ #include -#include +#include // NOTE: This example doesn't work yet! diff --git a/examples/server/050_https_server.c b/examples/server/050_https_server.c index 9fd333d..cbd00de 100644 --- a/examples/server/050_https_server.c +++ b/examples/server/050_https_server.c @@ -1,5 +1,5 @@ #include -#include +#include // This example shows how to set up an HTTPS (HTTP over TLS) // server. diff --git a/examples/server/060_virtual_hosts_over_https.c b/examples/server/060_virtual_hosts_over_https.c index 44d2520..39a5c2b 100644 --- a/examples/server/060_virtual_hosts_over_https.c +++ b/examples/server/060_virtual_hosts_over_https.c @@ -1,4 +1,4 @@ -#include +#include // This is an example of how to serve different websites // over a single HTTPS server instance. @@ -29,7 +29,7 @@ int main(void) // First, set up an HTTPS server instance with one // of the certificate. This will act as default certificate // when ecrypted connections don't target a specific domain. - HTTP_Server *server = http_server_init( + HTTP_Server *server = http_server_init_ex( HTTP_STR("127.0.0.1"), 8080, 8443, HTTP_STR("websiteA_cert.pem"), HTTP_STR("websiteA_key.pem") diff --git a/misc/amalg.py b/misc/amalg.py index 96610fe..46b2ad5 100644 --- a/misc/amalg.py +++ b/misc/amalg.py @@ -198,7 +198,7 @@ def main(): print("Writing chttp.h...") with open("chttp.h", 'w', encoding='utf-8') as f: f.write('/*\n') - f.write(' * HTTP Library - Amalgamated Header\n') + f.write(' * cHTTP Library - Amalgamated Header\n') f.write(' * Generated automatically - do not edit manually\n') f.write(' */\n\n') f.write('#ifndef HTTP_AMALGAMATION_H\n') @@ -215,10 +215,10 @@ def main(): print("Writing chttp.c...") with open("chttp.c", 'w', encoding='utf-8') as f: f.write('/*\n') - f.write(' * HTTP Library - Amalgamated Source\n') + f.write(' * cHTTP Library - Amalgamated Source\n') f.write(' * Generated automatically - do not edit manually\n') f.write(' */\n\n') - f.write('#include "http.h"\n\n') + f.write('#include "chttp.h"\n\n') f.write(source) print("Amalgamation complete!") diff --git a/src/client.c b/src/client.c index de1b99f..19b6ccf 100644 --- a/src/client.c +++ b/src/client.c @@ -311,7 +311,7 @@ void http_request_line(HTTP_RequestHandle handle, HTTP_Method method, HTTP_Strin http_engine_url(&conn->engine, method, url, 1); } -void http_request_header(HTTP_RequestHandle handle, char *header, int len) +void http_request_header(HTTP_RequestHandle handle, HTTP_String str) { ClientConnection *conn = handle2clientconn(handle); if (conn == NULL) @@ -319,10 +319,10 @@ void http_request_header(HTTP_RequestHandle handle, char *header, int len) if (conn->state != CLIENT_CONNECTION_INIT) return; - http_engine_header(&conn->engine, header, len); + http_engine_header(&conn->engine, str); } -void http_request_body(HTTP_RequestHandle handle, char *body, int len) +void http_request_body(HTTP_RequestHandle handle, HTTP_String str) { ClientConnection *conn = handle2clientconn(handle); if (conn == NULL) @@ -330,7 +330,7 @@ void http_request_body(HTTP_RequestHandle handle, char *body, int len) if (conn->state != CLIENT_CONNECTION_INIT) return; - http_engine_body(&conn->engine, body, len); + http_engine_body(&conn->engine, str); } void http_request_submit(HTTP_RequestHandle handle) diff --git a/src/client.h b/src/client.h index 693dc2b..4cdaea2 100644 --- a/src/client.h +++ b/src/client.h @@ -65,15 +65,11 @@ void http_request_line(HTTP_RequestHandle handle, HTTP_Method method, HTTP_Strin // Append a header to the specified request. You must call // this after http_request_line and may do so multiple times. -// -// TODO: use HTTP_String instead of char*+int -void http_request_header(HTTP_RequestHandle handle, char *header, int len); +void http_request_header(HTTP_RequestHandle handle, HTTP_String str); // Append some data to the request's body. You must call // this after either http_request_line or http_request_header. -// -// TODO: use HTTP_String instead of char*+int -void http_request_body(HTTP_RequestHandle handle, char *body, int len); +void http_request_body(HTTP_RequestHandle handle, HTTP_String str); // Mark the initialization of the request as completed and // perform the request. diff --git a/src/engine.c b/src/engine.c index 7745f95..5ffd1ea 100644 --- a/src/engine.c +++ b/src/engine.c @@ -774,16 +774,14 @@ void http_engine_status(HTTP_Engine *eng, int status) eng->state = HTTP_ENGINE_STATE_SERVER_PREP_HEADER; } -void http_engine_header(HTTP_Engine *eng, const char *src, int len) +void http_engine_header(HTTP_Engine *eng, HTTP_String str) { if ((eng->state & HTTP_ENGINE_STATEBIT_PREP_HEADER) == 0) return; - if (len < 0) len = strlen(src); - // TODO: Check that the header is valid - byte_queue_write(&eng->output, src, len); + byte_queue_write(&eng->output, str.ptr, str.len); byte_queue_write(&eng->output, "\r\n", 2); } @@ -848,16 +846,14 @@ static void complete_message_body(HTTP_Engine *eng) byte_queue_patch(&eng->output, eng->content_length_value_offset, tmp + i, 10 - i); } -void http_engine_body(HTTP_Engine *eng, void *src, int len) +void http_engine_body(HTTP_Engine *eng, HTTP_String str) { - if (len < 0) len = strlen(src); - - http_engine_bodycap(eng, len); + http_engine_bodycap(eng, str.len); int cap; char *buf = http_engine_bodybuf(eng, &cap); if (buf) { - memcpy(buf, src, len); - http_engine_bodyack(eng, len); + memcpy(buf, str.ptr, str.len); + http_engine_bodyack(eng, str.len); } } diff --git a/src/engine.h b/src/engine.h index 43472bc..39a7011 100644 --- a/src/engine.h +++ b/src/engine.h @@ -108,8 +108,8 @@ HTTP_Response* http_engine_getres (HTTP_Engine *eng); void http_engine_url (HTTP_Engine *eng, HTTP_Method method, HTTP_String url, int minor); void http_engine_status (HTTP_Engine *eng, int status); -void http_engine_header (HTTP_Engine *eng, const char *src, int len); -void http_engine_body (HTTP_Engine *eng, void *src, int len); +void http_engine_header (HTTP_Engine *eng, HTTP_String str); +void http_engine_body (HTTP_Engine *eng, HTTP_String str); void http_engine_bodycap (HTTP_Engine *eng, int mincap); char* http_engine_bodybuf (HTTP_Engine *eng, int *cap); void http_engine_bodyack (HTTP_Engine *eng, int num); diff --git a/src/router.c b/src/router.c index fde1bfe..405704d 100644 --- a/src/router.c +++ b/src/router.c @@ -358,7 +358,7 @@ static int serve_dynamic_route(Route *route, HTTP_Request *req, HTTP_ResponseHan int path_len = sanitize_path(req->url.path, path_mem, (int) sizeof(path_mem)); if (path_len < 0) { http_response_status(res, 400); - http_response_body(res, "Invalid path", -1); + http_response_body(res, HTTP_STR("Invalid path")); http_response_done(res); return 1; } @@ -403,7 +403,7 @@ int http_serve(char *addr, int port, HTTP_Router *router) { int ret; - HTTP_Server *server = http_server_init((HTTP_String) { addr, strlen(addr) }, port, 0, (HTTP_String) {}, (HTTP_String) {}); + HTTP_Server *server = http_server_init_ex((HTTP_String) { addr, strlen(addr) }, port, 0, (HTTP_String) {}, (HTTP_String) {}); if (server == NULL) { http_router_free(router); return -1; diff --git a/src/server.c b/src/server.c index 96d22f1..f387098 100644 --- a/src/server.c +++ b/src/server.c @@ -133,7 +133,6 @@ HTTP_Server *http_server_init_ex(HTTP_String addr, uint16_t port, } } - server->num_websites = 0; server->num_conns = 0; server->ready_head = 0; server->ready_count = 0; @@ -368,16 +367,13 @@ void http_response_header(HTTP_ResponseHandle res, const char *fmt, ...) va_end(args); } -void http_response_body(HTTP_ResponseHandle res, char *src, int len) +void http_response_body(HTTP_ResponseHandle res, HTTP_String str) { Connection *conn = handle2conn(res); if (conn == NULL) return; - if (len < 0) - len = strlen(src); - - http_engine_body(&conn->engine, src, len); + http_engine_body(&conn->engine, str); } void http_response_bodycap(HTTP_ResponseHandle res, int mincap) diff --git a/src/server.h b/src/server.h index 63aa4ff..337abb2 100644 --- a/src/server.h +++ b/src/server.h @@ -22,7 +22,7 @@ int http_server_wait (HTTP_Server *server, HTTP_Request **req, H int http_server_add_website (HTTP_Server *server, HTTP_String domain, HTTP_String cert_file, HTTP_String key_file); void http_response_status (HTTP_ResponseHandle res, int status); void http_response_header (HTTP_ResponseHandle res, const char *fmt, ...); -void http_response_body (HTTP_ResponseHandle res, char *src, int len); +void http_response_body (HTTP_ResponseHandle res, HTTP_String str); void http_response_bodycap (HTTP_ResponseHandle res, int mincap); char* http_response_bodybuf (HTTP_ResponseHandle res, int *cap); void http_response_bodyack (HTTP_ResponseHandle res, int num);