Work on requests
This commit is contained in:
parent
d49e86faff
commit
8912994e1d
3 changed files with 93 additions and 5 deletions
|
@ -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 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <netinet/in.h>
|
||||
#include "readline.h"
|
||||
|
@ -28,6 +29,13 @@ typedef struct {
|
|||
Request* newRequest();
|
||||
void requestFirstLine( Request *req, char line[] );
|
||||
Request* newRequestFromSocket(int socket);
|
||||
/*
|
||||
* requestToString
|
||||
* @prarm req the request to convert
|
||||
* @param proxy whether we want a proxy request (adds the host to the first line)
|
||||
*/
|
||||
char* requestToString( Request *req, bool proxy );
|
||||
void requestAddHeader( Request *req, char header[] );
|
||||
//void* requestAddHeader(Request *req, char line[]);
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue