You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.6 KiB
70 lines
1.6 KiB
3 years ago
|
#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;
|
||
|
}
|