Yet Another Intercepting Proxy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.2 KiB

#include "request.h"
Request* newRequest(){
Request *request = malloc(sizeof(Request));
memset(request, 0, sizeof(Request));
3 years ago
request->headers = NULL;
return request;
}
void requestFirstLine( Request *req, char line[] ){
char method[20] = {'\0'}; //Get, post, etc.
char *url = malloc(sizeof(char) *2048); // This may contain the method, the path, the domain and so on
memset(url, '\0', sizeof(char) * 2048);
char *currentPos;
float version = 0;
char protocol[6] = {'\0'};
char host[254] = {'\0'};
//https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
char path[2000] = {'\0'};
sscanf( line, "%20s %2048s HTTP/%f", method, url, &version );
3 years ago
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
currentPos = url;
sscanf( currentPos, "%5[^:/]", protocol );
if ( strlen( protocol ) > 0 ){
currentPos = currentPos + strlen(protocol) + 3;
}
sscanf( currentPos, "%253[^:/]", host );
if ( strlen( host ) > 0 ){
currentPos = currentPos + strlen(host);
}
sscanf( currentPos, "%2000[^? ]", path );
if ( strlen( path ) > 0 ){
currentPos = currentPos + strlen(path);
}
req->protocol = malloc(sizeof(char) * strlen(protocol));
strcpy(req->protocol, protocol);
req->host = malloc(sizeof(char) * strlen(host));
strcpy(req->host, host);
req->path = malloc(sizeof(char) * strlen(path));
strcpy(req->path, path);
//The query string is anything left
req->queryString = malloc(sizeof(char) * strlen(currentPos));
strcpy(req->queryString, currentPos);
free(url);
}
Request* newRequestFromSocket(int socket){
Request *req = newRequest();
int valread;
char line[1024] = {0};
// The first line will give us some important information
valread = fdReadLine( socket, line, 1024);
requestFirstLine(req, line);
//a length of 2 will indicate an empty line which will split the headers
//from the body (if there is a body)
3 years ago
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;
}
3 years ago
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 );
}