Split out some stuff that is shared between request and response

I now have files with the infinitely imaginative names
requestrespons.{c,h,test.c}.
This commit is contained in:
Jonathan Hodgson 2022-01-10 09:36:18 +00:00
parent 0e2b9dae2b
commit 48e3092317
9 changed files with 533 additions and 70 deletions

View file

@ -1,45 +1,5 @@
#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));
@ -101,18 +61,15 @@ Request* newRequestFromSocket(int socket){
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";
valread = fdReadLine( socket, line, 1024);
printf("HERE\nLine is %s\n", line);
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);
}
send(socket , "this is a test" , 15 , 0 );
printf("Hello message sent\n");
//while ( valread > 2 ){
// printf("%s",line );
// //valread = fdReadLine( socket , line, 1024);
//}
return req;
}