|
|
@ -4,6 +4,7 @@ |
|
|
|
Request* newRequest(){ |
|
|
|
Request* newRequest(){ |
|
|
|
Request *request = malloc(sizeof(Request)); |
|
|
|
Request *request = malloc(sizeof(Request)); |
|
|
|
memset(request, 0, sizeof(Request)); |
|
|
|
memset(request, 0, sizeof(Request)); |
|
|
|
|
|
|
|
request->headers = NULL; |
|
|
|
return request; |
|
|
|
return request; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -19,7 +20,7 @@ void requestFirstLine( Request *req, char line[] ){ |
|
|
|
char path[2000] = {'\0'}; |
|
|
|
char path[2000] = {'\0'}; |
|
|
|
|
|
|
|
|
|
|
|
sscanf( line, "%20s %2048s HTTP/%f", method, url, &version ); |
|
|
|
sscanf( line, "%20s %2048s HTTP/%f", method, url, &version ); |
|
|
|
req->method = method; |
|
|
|
req->method = strdup(method); |
|
|
|
req->version = version; |
|
|
|
req->version = version; |
|
|
|
|
|
|
|
|
|
|
|
//We've pulled out the easy bits. Now to go through the url and pull out what we need
|
|
|
|
//We've pulled out the easy bits. Now to go through the url and pull out what we need
|
|
|
@ -67,9 +68,48 @@ Request* newRequestFromSocket(int socket){ |
|
|
|
|
|
|
|
|
|
|
|
//a length of 2 will indicate an empty line which will split the headers
|
|
|
|
//a length of 2 will indicate an empty line which will split the headers
|
|
|
|
//from the body (if there is a body)
|
|
|
|
//from the body (if there is a body)
|
|
|
|
//while ( valread > 2 ){
|
|
|
|
valread = fdReadLine( socket, line, 1024); |
|
|
|
// printf("%s",line );
|
|
|
|
while ( valread > 2 ){ |
|
|
|
// //valread = fdReadLine( socket , line, 1024);
|
|
|
|
printf("%s",line ); |
|
|
|
//}
|
|
|
|
requestAddHeader( req, line ); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//I believe at this point all the headers are done.
|
|
|
|
|
|
|
|
valread = fdReadLine( socket , line, 1024); |
|
|
|
|
|
|
|
} |
|
|
|
return req; |
|
|
|
return req; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char* requestToString( Request *req, bool proxy){ |
|
|
|
|
|
|
|
unsigned int fullLength = strlen(req->method) + 1 + sizeof( req->path ) + |
|
|
|
|
|
|
|
sizeof( req->queryString ) + 11 + headerListCharLength( req->headers ); |
|
|
|
|
|
|
|
if ( proxy ) |
|
|
|
|
|
|
|
fullLength += sizeof(req->method) + 3 + sizeof(req->host); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char retString[fullLength]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (proxy) |
|
|
|
|
|
|
|
sprintf(retString, "%s %s://%s%s%s HTTP/%.1f\r\n%s", req->method, |
|
|
|
|
|
|
|
req->method, req->host, req->path, req->queryString, req->version, |
|
|
|
|
|
|
|
headersToString(req->headers)); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
sprintf(retString, "%s %s%s HTTP/%.1f\r\n%s", req->method, |
|
|
|
|
|
|
|
req->path, req->queryString, req->version, |
|
|
|
|
|
|
|
headersToString(req->headers)); |
|
|
|
|
|
|
|
// rsp->statusMessage,headersToString(rsp->headers),rsp->body);
|
|
|
|
|
|
|
|
return strdup(retString); |
|
|
|
|
|
|
|
//return strdup(retString);
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void requestAddHeader( Request *req, char header[] ){ |
|
|
|
|
|
|
|
if ( req->headers == NULL ){ |
|
|
|
|
|
|
|
req->headers = malloc(sizeof( HeaderList )); |
|
|
|
|
|
|
|
req->headers->header = newHeader( header ); |
|
|
|
|
|
|
|
req->headers->next = NULL; |
|
|
|
|
|
|
|
} else |
|
|
|
|
|
|
|
addHeader( req->headers, header ); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|