93 lines
2.7 KiB
C
93 lines
2.7 KiB
C
#include "http_method.h"
|
|
#include "http_request.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
// GET / HTTP/1.1
|
|
// Host: localhost:8080
|
|
// Sec-Fetch-Dest: document
|
|
// User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)
|
|
// AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.0 Safari/605.1.15
|
|
// Upgrade-Insecure-Requests: 1
|
|
// Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
|
|
// Sec-Fetch-Site: none
|
|
// Sec-Fetch-Mode: navigate
|
|
// Accept-Language: en-GB,en;q=0.9
|
|
// Priority: u=0, i
|
|
// Accept-Encoding: gzip, deflate
|
|
// Connection: keep-alive
|
|
|
|
void print_request(const HttpRequest *__req) {
|
|
char 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,
|
|
__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);
|
|
}
|
|
|
|
void http_request_set(HttpRequest *__req, char *key, char *value) {
|
|
if (strcmp(key, "Host"))
|
|
__req->host = value;
|
|
else if (strcmp(key, "Sec-Fetch-Dest"))
|
|
__req->sec_fetch_dest = value;
|
|
else if (strcmp(key, "User-Agent"))
|
|
__req->user_agent = value;
|
|
else if (strcmp(key, "Upgrade-Insecure-Requests"))
|
|
__req->upgrade_insecure_requests = value;
|
|
else if (strcmp(key, "Accept"))
|
|
;
|
|
// __req->accept = value;
|
|
else if (strcmp(key, "Sec-Fetch-Site"))
|
|
__req->sec_fetch_site = value;
|
|
else if (strcmp(key, "Sec-Fetch-Mode"))
|
|
__req->sec_fetch_mode = value;
|
|
else if (strcmp(key, "Accept-Language"))
|
|
__req->accept_language = value;
|
|
else if (strcmp(key, "Accept-Encoding"))
|
|
__req->accept_encoding = value;
|
|
else if (strcmp(key, "Connection"))
|
|
__req->connection = value;
|
|
}
|
|
|
|
HttpRequest *handle_request(char *__req) {
|
|
HttpRequest *request = malloc(sizeof(HttpRequest));
|
|
unsigned int line_count = 0;
|
|
char *line_start = __req;
|
|
char *line_end;
|
|
while ((line_end = strchr(line_start, '\n')) != NULL) {
|
|
line_count++;
|
|
size_t l_length = line_end - line_start;
|
|
char *line = malloc(sizeof(char) * l_length);
|
|
if (line == NULL)
|
|
return NULL;
|
|
|
|
strncpy(line, line_start, l_length);
|
|
line[l_length] = 0;
|
|
if (line_count == 1) {
|
|
char method[10];
|
|
char *path = malloc(sizeof(char) * 1024);
|
|
if (sscanf(line, "%s %s", method, path) != 3) {
|
|
free(path);
|
|
return NULL;
|
|
}
|
|
printf("%s", path);
|
|
request->method = get_http_method(method);
|
|
request->path = path;
|
|
} else {
|
|
char *key = malloc(sizeof(char) * 20);
|
|
char *value = malloc(sizeof(char) * 256);
|
|
if (sscanf(line, "%s: %s", key, value) == 2) {
|
|
http_request_set(request, key, value);
|
|
}
|
|
free(key);
|
|
}
|
|
// TODO: Analyze line
|
|
free(line);
|
|
line_start = line_end + 1;
|
|
}
|
|
|
|
return request;
|
|
}
|