Add http_match_host and update 060_virtual_hosts_over_https.c
This commit is contained in:
@@ -23,12 +23,8 @@ endif
|
|||||||
|
|
||||||
HTTPS ?= 0
|
HTTPS ?= 0
|
||||||
ifneq ($(HTTPS),0)
|
ifneq ($(HTTPS),0)
|
||||||
ifeq ($(OS),Windows_NT)
|
|
||||||
$(error "HTTPS not supported on Windows")
|
|
||||||
else
|
|
||||||
CFLAGS += -DHTTPS_ENABLED
|
CFLAGS += -DHTTPS_ENABLED
|
||||||
LFLAGS += -lssl -lcrypto
|
LFLAGS += -lssl -lcrypto
|
||||||
endif
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Installation directories
|
# Installation directories
|
||||||
|
|||||||
@@ -365,6 +365,7 @@ void print_bytes(HTTP_String prefix, HTTP_String src)
|
|||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#line 1 "src/parse.c"
|
#line 1 "src/parse.c"
|
||||||
|
#include <stdio.h> // snprintf
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -1667,6 +1668,22 @@ int http_get_param_i(HTTP_String body, HTTP_String str)
|
|||||||
return res;
|
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
|
// 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);
|
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);
|
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
|
#endif // PARSE_INCLUDED
|
||||||
|
|
||||||
|
|||||||
@@ -92,17 +92,15 @@ int main(void)
|
|||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
int idx = http_find_header(req->headers, req->num_headers, HTTP_STR("Host"));
|
if (http_match_host(req, HTTP_STR("websiteB.com"), 8080) ||
|
||||||
HTTP_ASSERT(idx != -1); // Requests without the host header are always rejected
|
http_match_host(req, HTTP_STR("websiteB.com"), 8443)) {
|
||||||
HTTP_String host = req->headers[idx].value;
|
|
||||||
|
|
||||||
if (http_streq(host, HTTP_STR("websiteB.com"))) {
|
|
||||||
|
|
||||||
http_response_builder_status(builder, 200);
|
http_response_builder_status(builder, 200);
|
||||||
http_response_builder_body(builder, HTTP_STR("Hello from websiteB.com!"));
|
http_response_builder_body(builder, HTTP_STR("Hello from websiteB.com!"));
|
||||||
http_response_builder_done(builder);
|
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_status(builder, 200);
|
||||||
http_response_builder_body(builder, HTTP_STR("Hello from websiteC.com!"));
|
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
|
handle 3xx client redirections
|
||||||
add discussion on string management
|
add discussion on string management
|
||||||
add discussion on error management
|
add discussion on error management
|
||||||
check that virtual hosts over HTTPS work
|
|
||||||
add timers
|
add timers
|
||||||
add debug and release builds
|
add debug and release builds
|
||||||
+17
@@ -1,3 +1,4 @@
|
|||||||
|
#include <stdio.h> // snprintf
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -1299,3 +1300,19 @@ int http_get_param_i(HTTP_String body, HTTP_String str)
|
|||||||
|
|
||||||
return res;
|
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);
|
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);
|
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
|
#endif // PARSE_INCLUDED
|
||||||
Reference in New Issue
Block a user