|
|
|
@ -4,6 +4,7 @@ |
|
|
|
|
Request* newRequest(){ |
|
|
|
|
Request *request = malloc(sizeof(Request)); |
|
|
|
|
memset(request, 0, sizeof(Request)); |
|
|
|
|
request->headers = NULL; |
|
|
|
|
return request; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -19,7 +20,7 @@ void requestFirstLine( Request *req, char line[] ){ |
|
|
|
|
char path[2000] = {'\0'}; |
|
|
|
|
|
|
|
|
|
sscanf( line, "%20s %2048s HTTP/%f", method, url, &version ); |
|
|
|
|
req->method = method; |
|
|
|
|
req->method = strdup(method); |
|
|
|
|
req->version = version; |
|
|
|
|
|
|
|
|
|
//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
|
|
|
|
|
//from the body (if there is a body)
|
|
|
|
|
//while ( valread > 2 ){
|
|
|
|
|
// printf("%s",line );
|
|
|
|
|
// //valread = fdReadLine( socket , line, 1024);
|
|
|
|
|
//}
|
|
|
|
|
valread = fdReadLine( socket, line, 1024); |
|
|
|
|
while ( valread > 2 ){ |
|
|
|
|
printf("%s",line ); |
|
|
|
|
requestAddHeader( req, line ); |
|
|
|
|
|
|
|
|
|
//I believe at this point all the headers are done.
|
|
|
|
|
valread = fdReadLine( socket , line, 1024); |
|
|
|
|
} |
|
|
|
|
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 ); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|