Work on requests
This commit adds tests for a request and the implementation. The first line of a request should now be decoded correctly
This commit is contained in:
parent
66d4702297
commit
0e2b9dae2b
4 changed files with 224 additions and 11 deletions
|
@ -47,23 +47,72 @@ Request* newRequest(){
|
|||
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};
|
||||
valread = fdReadLine( socket , line, 1024);
|
||||
char hello[] = "this is a test";
|
||||
|
||||
// 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);
|
||||
//valread = fdReadLine( socket , line, 1024);
|
||||
}
|
||||
send(socket , hello , strlen(hello) , 0 );
|
||||
send(socket , "this is a test" , 15 , 0 );
|
||||
printf("Hello message sent\n");
|
||||
return req;
|
||||
}
|
||||
|
|
|
@ -7,8 +7,6 @@
|
|||
#include "readline.h"
|
||||
#include <netinet/in.h>
|
||||
|
||||
typedef enum {HTTP,HTTPS} HTTPProtocol;
|
||||
typedef enum {GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH} HTTPMethod;
|
||||
|
||||
/*
|
||||
* A struct reperesenting an http header
|
||||
|
@ -33,9 +31,9 @@ struct HeaderList {
|
|||
*/
|
||||
typedef struct {
|
||||
// Common versions are: 0.9, 1.0, 1.1, 2.0
|
||||
double version;
|
||||
HTTPMethod method;
|
||||
HTTPProtocol protocol;
|
||||
float version;
|
||||
char *method;
|
||||
char *protocol;
|
||||
char *host;
|
||||
char *path;
|
||||
HeaderList *headers;
|
||||
|
@ -45,8 +43,10 @@ typedef struct {
|
|||
|
||||
Header* newHeader();
|
||||
Request* newRequest();
|
||||
void requestFirstLine( Request *req, char line[] );
|
||||
Request* newRequestFromSocket(int socket);
|
||||
void* addHeader(Request *req);
|
||||
void* addHeader(Request *req, char line[]);
|
||||
void* interpretLine1(Request *req, char line[]);
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue