If file non existant crashing resolved

This commit is contained in:
cdricms
2024-11-15 10:56:07 +01:00
parent 9ccde76edc
commit ce2edb6293
8 changed files with 209 additions and 74 deletions

View File

@@ -40,7 +40,7 @@ void http_request_set(HttpRequest *__req, char *key, char *value) {
else if (!strcmp(key, "Upgrade-Insecure-Requests"))
__req->upgrade_insecure_requests = value;
else if (!strcmp(key, "Accept"))
;
free(value);
// __req->accept = value;
else if (!strcmp(key, "Sec-Fetch-Site"))
__req->sec_fetch_site = value;
@@ -52,9 +52,12 @@ void http_request_set(HttpRequest *__req, char *key, char *value) {
__req->accept_encoding = value;
else if (!strcmp(key, "Connection"))
__req->connection = value;
else
free(value);
}
HttpRequest *handle_request(char *__req) {
printf("%s\n", __req);
HttpRequest *request = malloc(sizeof(HttpRequest));
unsigned int line_count = 0;
char *line_start = __req;
@@ -62,9 +65,7 @@ HttpRequest *handle_request(char *__req) {
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;
char line[l_length];
strncpy(line, line_start, l_length);
line[l_length - 1] = 0;
@@ -79,20 +80,49 @@ HttpRequest *handle_request(char *__req) {
request->path = path;
} else {
char key[100] = {0};
char *value = malloc(sizeof(char) * 256);
char *colon_pos = strchr(line, ':');
if (colon_pos != NULL) {
size_t k_length = colon_pos - line;
strncpy(key, line, k_length);
size_t v_length = l_length - k_length - 3;
char *value = malloc(sizeof(char) * v_length);
strcpy(value, colon_pos + 2);
http_request_set(request, key, value);
}
}
// TODO: Analyze line
free(line);
line_start = line_end + 1;
}
return request;
}
void free_request(HttpRequest *__req) {
if (__req == NULL)
return;
if (__req->path != NULL)
free(__req->path);
if (__req->host != NULL)
free(__req->host);
if (__req->sec_fetch_dest != NULL)
free(__req->sec_fetch_dest);
if (__req->user_agent != NULL)
free(__req->user_agent);
if (__req->upgrade_insecure_requests != NULL)
free(__req->upgrade_insecure_requests);
if (__req->accept != NULL)
free(__req->accept);
if (__req->sec_fetch_site != NULL)
free(__req->sec_fetch_site);
if (__req->sec_fetch_mode != NULL)
free(__req->sec_fetch_mode);
if (__req->accept_language != NULL)
free(__req->accept_language);
if (__req->accept_encoding != NULL)
free(__req->accept_encoding);
if (__req->connection != NULL)
free(__req->connection);
free(__req);
}