Now working for simple, non-encrypted requests
making requests to something like example.com over a non-encrypted connection now works. Binary files are unlikely to work at the moment although I haven't tried. Also, non-encrypted doesn't work. I have also changed a little about how tests work. Requests tests now display much better.
This commit is contained in:
parent
a91a264a7a
commit
8a5bfe9b36
18 changed files with 725 additions and 148 deletions
105
src/request.c
105
src/request.c
|
@ -10,12 +10,13 @@ Request* newRequest(){
|
|||
|
||||
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
|
||||
// This may contain the method, the path, the domain and so on
|
||||
char *url = malloc(sizeof(char) *2048);
|
||||
memset(url, '\0', sizeof(char) * 2048);
|
||||
char *currentPos;
|
||||
char *currentPos = url;
|
||||
float version = 0;
|
||||
char protocol[6] = {'\0'};
|
||||
char host[254] = {'\0'};
|
||||
int port = -1; // 0 is a valid port number, -1 isn't
|
||||
//https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers
|
||||
char path[2000] = {'\0'};
|
||||
|
||||
|
@ -24,34 +25,64 @@ void requestFirstLine( Request *req, char line[] ){
|
|||
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;
|
||||
int protEnd = strpos(url, "://" );
|
||||
if ( protEnd > -1 ){
|
||||
req->protocol = strndup( url, protEnd );
|
||||
currentPos += protEnd + 3;
|
||||
} else {
|
||||
req->protocol = "";
|
||||
}
|
||||
|
||||
|
||||
sscanf( currentPos, "%253[^:/]", host );
|
||||
if ( strlen( host ) > 0 ){
|
||||
currentPos = currentPos + strlen(host);
|
||||
req->host = strdup(host);
|
||||
} else {
|
||||
req->host = "";
|
||||
}
|
||||
|
||||
|
||||
if ( currentPos[0] == ':' ){
|
||||
currentPos++;
|
||||
sscanf( currentPos, "%i", &port );
|
||||
req->port = port;
|
||||
currentPos += (int)floor( log10( port ) ) + 1;
|
||||
|
||||
} else {
|
||||
req->port = -1;
|
||||
}
|
||||
|
||||
|
||||
sscanf( currentPos, "%2000[^? ]", path );
|
||||
if ( strlen( path ) > 0 ){
|
||||
currentPos = currentPos + strlen(path);
|
||||
currentPos += strlen(path);
|
||||
req->path = strdup(path);
|
||||
} else {
|
||||
req->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);
|
||||
if ( strlen( currentPos ) > 0 ){
|
||||
req->queryString = strdup( currentPos );
|
||||
} else {
|
||||
req->queryString = "";
|
||||
}
|
||||
|
||||
//We try and work out port and protocol if we don't have them
|
||||
if ( req->port == -1){
|
||||
if ( strcmp( "https", req->protocol ) == 0 )
|
||||
req->port = 443;
|
||||
else
|
||||
req->port = 80;
|
||||
}
|
||||
|
||||
if ( strlen(req->protocol) == 0 ){
|
||||
if ( req->port == 443 )
|
||||
req->protocol = "https";
|
||||
else
|
||||
req->protocol = "http";
|
||||
}
|
||||
|
||||
//The query string is anything left
|
||||
req->queryString = malloc(sizeof(char) * strlen(currentPos));
|
||||
strcpy(req->queryString, currentPos);
|
||||
|
||||
|
||||
free(url);
|
||||
}
|
||||
|
@ -70,30 +101,38 @@ Request* newRequestFromSocket(int socket){
|
|||
//from the body (if there is a body)
|
||||
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);
|
||||
}
|
||||
|
||||
//TODO: make requests work with a body
|
||||
//contentLength = getHeader( req->headers, "content-length" );
|
||||
|
||||
//if ( contentLength != NULL ){
|
||||
// printf( "Content length is %i\n", atoi(contentLength->value) );
|
||||
//}
|
||||
|
||||
return req;
|
||||
}
|
||||
|
||||
char* requestToString( Request *req, bool proxy){
|
||||
char* requestToString( Request *req){
|
||||
unsigned int fullLength = strlen(req->method) + 1 + sizeof( req->path ) +
|
||||
//11 = [space]http/1.1\r\n
|
||||
sizeof( req->queryString ) + 11 + headerListCharLength( req->headers );
|
||||
if ( proxy )
|
||||
fullLength += sizeof(req->method) + 3 + sizeof(req->host);
|
||||
|
||||
if ( strcmp( req->method, "CONNECT" ) == 0 )
|
||||
fullLength += sizeof(req->host);
|
||||
|
||||
|
||||
char retString[fullLength];
|
||||
memset( retString, '\0', 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));
|
||||
if ( strcmp( req->method, "CONNECT" ) == 0 )
|
||||
sprintf(retString, "%s %s HTTP/%.1f\r\n%s\r\n", req->method,
|
||||
req->host, req->version, headersToString(req->headers));
|
||||
else
|
||||
sprintf(retString, "%s %s%s HTTP/%.1f\r\n%s", req->method,
|
||||
sprintf(retString, "%s %s%s HTTP/%.1f\r\n%s\r\n", req->method,
|
||||
req->path, req->queryString, req->version,
|
||||
headersToString(req->headers));
|
||||
// rsp->statusMessage,headersToString(rsp->headers),rsp->body);
|
||||
|
@ -111,5 +150,15 @@ void requestAddHeader( Request *req, char header[] ){
|
|||
}
|
||||
|
||||
|
||||
void freeRequest( Request *req ){
|
||||
free(req->method);
|
||||
free(req->protocol);
|
||||
free(req->host);
|
||||
free(req->path);
|
||||
freeHeaderList( req->headers );
|
||||
free(req->queryString);
|
||||
free(req->body);
|
||||
free(req);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue