diff --git a/Makefile b/Makefile index 83b9936..89daa0f 100644 --- a/Makefile +++ b/Makefile @@ -23,12 +23,8 @@ endif HTTPS ?= 0 ifneq ($(HTTPS),0) - ifeq ($(OS),Windows_NT) - $(error "HTTPS not supported on Windows") - else - CFLAGS += -DHTTPS_ENABLED - LFLAGS += -lssl -lcrypto - endif + CFLAGS += -DHTTPS_ENABLED + LFLAGS += -lssl -lcrypto endif # Installation directories diff --git a/chttp.c b/chttp.c index d18276a..13a0424 100644 --- a/chttp.c +++ b/chttp.c @@ -365,6 +365,7 @@ void print_bytes(HTTP_String prefix, HTTP_String src) //////////////////////////////////////////////////////////////////////////////////////// #line 1 "src/parse.c" +#include // snprintf #include #include #include @@ -1667,6 +1668,22 @@ int http_get_param_i(HTTP_String body, HTTP_String str) return res; } +bool http_match_host(HTTP_Request *req, HTTP_String domain, int port) +{ + int idx = http_find_header(req->headers, req->num_headers, HTTP_STR("Host")); + HTTP_ASSERT(idx != -1); // Requests without the host header are always rejected + + char tmp[1<<8]; + if (port > -1 && port != 80) { + int ret = snprintf(tmp, sizeof(tmp), "%.*s:%d", domain.len, domain.ptr, port); + HTTP_ASSERT(ret > 0); + domain = (HTTP_String) { tmp, ret }; + } + + HTTP_String host = req->headers[idx].value; + return http_streq(host, domain); +} + //////////////////////////////////////////////////////////////////////////////////////// // src/engine.c //////////////////////////////////////////////////////////////////////////////////////// diff --git a/chttp.h b/chttp.h index 74476ef..0c8548e 100644 --- a/chttp.h +++ b/chttp.h @@ -193,6 +193,9 @@ HTTP_String http_get_cookie (HTTP_Request *req, HTTP_String name); HTTP_String http_get_param (HTTP_String body, HTTP_String str, char *mem, int cap); int http_get_param_i (HTTP_String body, HTTP_String str); +// Checks whether the request was meant for the host with the given +// domain an port. If port is -1, the default value of 80 is assumed. +bool http_match_host(HTTP_Request *req, HTTP_String domain, int port); #endif // PARSE_INCLUDED diff --git a/examples/server/060_virtual_hosts_over_https.c b/examples/server/060_virtual_hosts_over_https.c index 746c246..4ee9770 100644 --- a/examples/server/060_virtual_hosts_over_https.c +++ b/examples/server/060_virtual_hosts_over_https.c @@ -92,17 +92,15 @@ int main(void) if (ret < 0) break; - int idx = http_find_header(req->headers, req->num_headers, HTTP_STR("Host")); - HTTP_ASSERT(idx != -1); // Requests without the host header are always rejected - HTTP_String host = req->headers[idx].value; - - if (http_streq(host, HTTP_STR("websiteB.com"))) { + if (http_match_host(req, HTTP_STR("websiteB.com"), 8080) || + http_match_host(req, HTTP_STR("websiteB.com"), 8443)) { http_response_builder_status(builder, 200); http_response_builder_body(builder, HTTP_STR("Hello from websiteB.com!")); http_response_builder_done(builder); - } else if (http_streq(host, HTTP_STR("websiteC.com"))) { + } else if (http_match_host(req, HTTP_STR("websiteC.com"), 8080) || + http_match_host(req, HTTP_STR("websiteC.com"), 8443)) { http_response_builder_status(builder, 200); http_response_builder_body(builder, HTTP_STR("Hello from websiteC.com!")); diff --git a/misc/TODO.md b/misc/TODO.md index ad65462..57f57ff 100644 --- a/misc/TODO.md +++ b/misc/TODO.md @@ -4,6 +4,5 @@ Find a way to compile OpenSSL on windows handle 3xx client redirections add discussion on string management add discussion on error management -check that virtual hosts over HTTPS work add timers add debug and release builds \ No newline at end of file diff --git a/src/parse.c b/src/parse.c index c989c3e..8d4c6dd 100644 --- a/src/parse.c +++ b/src/parse.c @@ -1,3 +1,4 @@ +#include // snprintf #include #include #include @@ -1298,4 +1299,20 @@ int http_get_param_i(HTTP_String body, HTTP_String str) } while (cur < out.len && is_digit(out.ptr[cur])); return res; -} \ No newline at end of file +} + +bool http_match_host(HTTP_Request *req, HTTP_String domain, int port) +{ + int idx = http_find_header(req->headers, req->num_headers, HTTP_STR("Host")); + HTTP_ASSERT(idx != -1); // Requests without the host header are always rejected + + char tmp[1<<8]; + if (port > -1 && port != 80) { + int ret = snprintf(tmp, sizeof(tmp), "%.*s:%d", domain.len, domain.ptr, port); + HTTP_ASSERT(ret > 0); + domain = (HTTP_String) { tmp, ret }; + } + + HTTP_String host = req->headers[idx].value; + return http_streq(host, domain); +} diff --git a/src/parse.h b/src/parse.h index 289c27f..f63e8f4 100644 --- a/src/parse.h +++ b/src/parse.h @@ -96,5 +96,8 @@ HTTP_String http_get_cookie (HTTP_Request *req, HTTP_String name); HTTP_String http_get_param (HTTP_String body, HTTP_String str, char *mem, int cap); int http_get_param_i (HTTP_String body, HTTP_String str); +// Checks whether the request was meant for the host with the given +// domain an port. If port is -1, the default value of 80 is assumed. +bool http_match_host(HTTP_Request *req, HTTP_String domain, int port); #endif // PARSE_INCLUDED \ No newline at end of file