I have made a start

I have done some work on opening a socket and waiting for a connection.
This can be read line by line and I have started a request struct that
it will accept.

Also started on some docs. Not much is yet working. I am going to start
learning µnit for unit tests:

https://nemequ.github.io/munit/
This commit is contained in:
Jonathan Hodgson 2021-12-27 21:43:11 +00:00
parent 4e17e706fa
commit f48a110429
15 changed files with 599 additions and 1 deletions

69
src/request.c Normal file
View file

@ -0,0 +1,69 @@
#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;
}
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
//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 , hello , strlen(hello) , 0 );
printf("Hello message sent\n");
return req;
}