Add http_match_host and update 060_virtual_hosts_over_https.c
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -365,6 +365,7 @@ void print_bytes(HTTP_String prefix, HTTP_String src)
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#line 1 "src/parse.c"
|
||||
#include <stdio.h> // snprintf
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
@@ -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
|
||||
////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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!"));
|
||||
|
||||
@@ -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
|
||||
+18
-1
@@ -1,3 +1,4 @@
|
||||
#include <stdio.h> // snprintf
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user