Semi-dynamic routing
This commit is contained in:
@@ -43,21 +43,21 @@ void http_method(HttpMethod __method, char *__out, size_t __size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
HttpMethod get_http_method(char *__met) {
|
HttpMethod get_http_method(char *__met) {
|
||||||
if (strcmp(__met, "GET"))
|
if (!strcmp(__met, "GET"))
|
||||||
return HTTP_METHOD_GET;
|
return HTTP_METHOD_GET;
|
||||||
if (strcmp(__met, "POST"))
|
if (!strcmp(__met, "POST"))
|
||||||
return HTTP_METHOD_POST;
|
return HTTP_METHOD_POST;
|
||||||
if (strcmp(__met, "PUT"))
|
if (!strcmp(__met, "PUT"))
|
||||||
return HTTP_METHOD_PUT;
|
return HTTP_METHOD_PUT;
|
||||||
if (strcmp(__met, "DELETE"))
|
if (!strcmp(__met, "DELETE"))
|
||||||
return HTTP_METHOD_DELETE;
|
return HTTP_METHOD_DELETE;
|
||||||
if (strcmp(__met, "PATCH"))
|
if (!strcmp(__met, "PATCH"))
|
||||||
return HTTP_METHOD_PATCH;
|
return HTTP_METHOD_PATCH;
|
||||||
if (strcmp(__met, "HEAD"))
|
if (!strcmp(__met, "HEAD"))
|
||||||
return HTTP_METHOD_HEAD;
|
return HTTP_METHOD_HEAD;
|
||||||
if (strcmp(__met, "OPTIONS"))
|
if (!strcmp(__met, "OPTIONS"))
|
||||||
return HTTP_METHOD_OPTIONS;
|
return HTTP_METHOD_OPTIONS;
|
||||||
if (strcmp(__met, "CONNECT"))
|
if (!strcmp(__met, "CONNECT"))
|
||||||
return HTTP_METHOD_CONNECT;
|
return HTTP_METHOD_CONNECT;
|
||||||
return HTTP_METHOD_TRACE;
|
return HTTP_METHOD_TRACE;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
#include "http_method.h"
|
|
||||||
#include "http_request.h"
|
#include "http_request.h"
|
||||||
|
#include "http_method.h"
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -20,34 +20,37 @@
|
|||||||
void print_request(const HttpRequest *__req) {
|
void print_request(const HttpRequest *__req) {
|
||||||
char method[10];
|
char method[10];
|
||||||
http_method(__req->method, method, 10);
|
http_method(__req->method, method, 10);
|
||||||
printf("%s %s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n", method,
|
printf("Method: %s Path: %s\nHost: %s\nSec-Fetch-Dest: %s\nUser-Agent: "
|
||||||
__req->path, __req->host, __req->sec_fetch_dest, __req->user_agent,
|
"%s\nUpgrade-Insecure-Requests: %s\nAccept: %s\nSec-Fetch-Site: "
|
||||||
__req->upgrade_insecure_requests, "OK", __req->sec_fetch_site,
|
"%s\nSec-Fetch-Mode: %s\nAccept-Language: %s\nPriority: "
|
||||||
__req->sec_fetch_mode, __req->accept_language,
|
"%s\nAccept-Encoding: %s\n",
|
||||||
|
method, __req->path, __req->host, __req->sec_fetch_dest,
|
||||||
|
__req->user_agent, __req->upgrade_insecure_requests, "OK",
|
||||||
|
__req->sec_fetch_site, __req->sec_fetch_mode, __req->accept_language,
|
||||||
__req->accept_encoding, __req->connection);
|
__req->accept_encoding, __req->connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void http_request_set(HttpRequest *__req, char *key, char *value) {
|
void http_request_set(HttpRequest *__req, char *key, char *value) {
|
||||||
if (strcmp(key, "Host"))
|
if (!strcmp(key, "Host"))
|
||||||
__req->host = value;
|
__req->host = value;
|
||||||
else if (strcmp(key, "Sec-Fetch-Dest"))
|
else if (!strcmp(key, "Sec-Fetch-Dest"))
|
||||||
__req->sec_fetch_dest = value;
|
__req->sec_fetch_dest = value;
|
||||||
else if (strcmp(key, "User-Agent"))
|
else if (!strcmp(key, "User-Agent"))
|
||||||
__req->user_agent = value;
|
__req->user_agent = value;
|
||||||
else if (strcmp(key, "Upgrade-Insecure-Requests"))
|
else if (!strcmp(key, "Upgrade-Insecure-Requests"))
|
||||||
__req->upgrade_insecure_requests = value;
|
__req->upgrade_insecure_requests = value;
|
||||||
else if (strcmp(key, "Accept"))
|
else if (!strcmp(key, "Accept"))
|
||||||
;
|
;
|
||||||
// __req->accept = value;
|
// __req->accept = value;
|
||||||
else if (strcmp(key, "Sec-Fetch-Site"))
|
else if (!strcmp(key, "Sec-Fetch-Site"))
|
||||||
__req->sec_fetch_site = value;
|
__req->sec_fetch_site = value;
|
||||||
else if (strcmp(key, "Sec-Fetch-Mode"))
|
else if (!strcmp(key, "Sec-Fetch-Mode"))
|
||||||
__req->sec_fetch_mode = value;
|
__req->sec_fetch_mode = value;
|
||||||
else if (strcmp(key, "Accept-Language"))
|
else if (!strcmp(key, "Accept-Language"))
|
||||||
__req->accept_language = value;
|
__req->accept_language = value;
|
||||||
else if (strcmp(key, "Accept-Encoding"))
|
else if (!strcmp(key, "Accept-Encoding"))
|
||||||
__req->accept_encoding = value;
|
__req->accept_encoding = value;
|
||||||
else if (strcmp(key, "Connection"))
|
else if (!strcmp(key, "Connection"))
|
||||||
__req->connection = value;
|
__req->connection = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,24 +67,27 @@ HttpRequest *handle_request(char *__req) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
strncpy(line, line_start, l_length);
|
strncpy(line, line_start, l_length);
|
||||||
line[l_length] = 0;
|
line[l_length - 1] = 0;
|
||||||
if (line_count == 1) {
|
if (line_count == 1) {
|
||||||
char method[10];
|
char method[10] = {0};
|
||||||
char *path = malloc(sizeof(char) * 1024);
|
char *path = malloc(sizeof(char) * 1024);
|
||||||
if (sscanf(line, "%s %s", method, path) != 3) {
|
if (sscanf(line, "%s %s", method, path) != 2) {
|
||||||
free(path);
|
free(path);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
printf("%s", path);
|
|
||||||
request->method = get_http_method(method);
|
request->method = get_http_method(method);
|
||||||
request->path = path;
|
request->path = path;
|
||||||
} else {
|
} else {
|
||||||
char *key = malloc(sizeof(char) * 20);
|
char key[100] = {0};
|
||||||
char *value = malloc(sizeof(char) * 256);
|
char *value = malloc(sizeof(char) * 256);
|
||||||
if (sscanf(line, "%s: %s", key, value) == 2) {
|
char *colon_pos = strchr(line, ':');
|
||||||
|
if (colon_pos != NULL) {
|
||||||
|
size_t k_length = colon_pos - line;
|
||||||
|
strncpy(key, line, k_length);
|
||||||
|
|
||||||
|
strcpy(value, colon_pos + 2);
|
||||||
http_request_set(request, key, value);
|
http_request_set(request, key, value);
|
||||||
}
|
}
|
||||||
free(key);
|
|
||||||
}
|
}
|
||||||
// TODO: Analyze line
|
// TODO: Analyze line
|
||||||
free(line);
|
free(line);
|
||||||
|
|||||||
14
main.c
14
main.c
@@ -7,6 +7,8 @@
|
|||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
#define DEFAULT_HTML "index.html"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
||||||
const int PORT = 8080;
|
const int PORT = 8080;
|
||||||
@@ -50,11 +52,17 @@ int main() {
|
|||||||
1024 - 1); // subtract 1 for the null
|
1024 - 1); // subtract 1 for the null
|
||||||
// terminator at the end
|
// terminator at the end
|
||||||
HttpRequest *req = handle_request(request);
|
HttpRequest *req = handle_request(request);
|
||||||
// print_request(req);
|
HttpResponse *res;
|
||||||
HttpResponse *res = from_file("./index.html");
|
if (!strcmp(req->path, "/")) {
|
||||||
|
res = from_file("./" DEFAULT_HTML);
|
||||||
|
} else {
|
||||||
|
char path[] = ".";
|
||||||
|
strcat(path, req->path);
|
||||||
|
res = from_file(path);
|
||||||
|
}
|
||||||
|
free(req);
|
||||||
http_respond(*res, clientfd);
|
http_respond(*res, clientfd);
|
||||||
free_response(res);
|
free_response(res);
|
||||||
printf("Hello message sent\n");
|
|
||||||
|
|
||||||
// closing the connected socket
|
// closing the connected socket
|
||||||
close(clientfd);
|
close(clientfd);
|
||||||
|
|||||||
@@ -23,6 +23,8 @@ links { "CWebLibrary" }
|
|||||||
|
|
||||||
filter "configurations:Debug"
|
filter "configurations:Debug"
|
||||||
symbols "On"
|
symbols "On"
|
||||||
|
buildoptions { "-g3", "-O0", "-fno-inline" } -- Ensure debug symbols are generated for static library
|
||||||
|
optimize "Off"
|
||||||
|
|
||||||
filter "configurations:Release"
|
filter "configurations:Release"
|
||||||
optimize "On"
|
optimize "On"
|
||||||
|
|||||||
Reference in New Issue
Block a user