Moves some logic out of proxy and into main

Also fixes some segfaults caused by trying to free memory that wasn't
allocated with strdup or malloc etc.

Fixes some tests
This commit is contained in:
Jonathan Hodgson 2022-01-19 12:56:11 +00:00
parent 8a5bfe9b36
commit 6eaad263be
10 changed files with 162 additions and 121 deletions

View file

@ -4,7 +4,13 @@
Request* newRequest(){
Request *request = malloc(sizeof(Request));
memset(request, 0, sizeof(Request));
request->method = NULL;
request->protocol = NULL;
request->host = NULL;
request->path = NULL;
request->headers = NULL;
request->queryString = NULL;
request->body = NULL;
return request;
}
@ -30,7 +36,7 @@ void requestFirstLine( Request *req, char line[] ){
req->protocol = strndup( url, protEnd );
currentPos += protEnd + 3;
} else {
req->protocol = "";
req->protocol = strdup("");
}
@ -39,7 +45,7 @@ void requestFirstLine( Request *req, char line[] ){
currentPos = currentPos + strlen(host);
req->host = strdup(host);
} else {
req->host = "";
req->host = strdup("");
}
@ -59,13 +65,13 @@ void requestFirstLine( Request *req, char line[] ){
currentPos += strlen(path);
req->path = strdup(path);
} else {
req->path = "";
req->path = strdup("");
}
if ( strlen( currentPos ) > 0 ){
req->queryString = strdup( currentPos );
} else {
req->queryString = "";
req->queryString = strdup("");
}
//We try and work out port and protocol if we don't have them
@ -78,9 +84,9 @@ void requestFirstLine( Request *req, char line[] ){
if ( strlen(req->protocol) == 0 ){
if ( req->port == 443 )
req->protocol = "https";
req->protocol = strdup("https");
else
req->protocol = "http";
req->protocol = strdup("http");
}
@ -151,6 +157,7 @@ void requestAddHeader( Request *req, char header[] ){
void freeRequest( Request *req ){
if ( req == NULL ) return;
free(req->method);
free(req->protocol);
free(req->host);