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.
 
 

118 lines
3.0 KiB

#include "request.h"
Header* newHeader(char *str){
Header *header = malloc(sizeof(Header));
memset(header, 0, sizeof(Header));
int position = -1;
for ( unsigned int i = 0; i < strlen(str); i++ ){
if ( str[i] == ':' ){
position = i;
break;
}
}
if ( position == -1 ){
printf("Header without colon. Not sure what to do\n");
return NULL;
}
// We want to allocate 1 more than the length so we can have a \0 at the end
header->name = malloc( sizeof(char) * ( position + 1 ) );
memset(header->name, '\0', position+1);
strncpy( header->name, str, position );
for ( unsigned int i = position+1; i < strlen(str); i++ ){
if ( str[i] == '\t' || str[i] == ' ' ) continue;
position = i;
break;
}
//Anything left is the value
header->value = malloc( sizeof(char) * ( strlen(str) - position ) );
memset(header->value, '\0', ( strlen(str) - position ) );
strcpy( header->value, str + position );
return header;
}
Request* newRequest(){
Request *request = malloc(sizeof(Request));
memset(request, 0, sizeof(Request));
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 );
req->method = 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);
char hello[] = "this is a test";
printf("HERE\nLine is %s\n", line);
//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);
}
send(socket , "this is a test" , 15 , 0 );
printf("Hello message sent\n");
return req;
}