#include "request.h" 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); 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) //while ( valread > 2 ){ // printf("%s",line ); // //valread = fdReadLine( socket , line, 1024); //} return req; }