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.
123 lines
2.8 KiB
123 lines
2.8 KiB
#include "requestresponse.h" |
|
|
|
Header* newHeader(char *str){ |
|
Header *header = malloc(sizeof(Header)); |
|
memset(header, 0, sizeof(Header)); |
|
|
|
|
|
char *position = str; |
|
char *end = str + strlen(str) - 1; |
|
|
|
//Let's loose any trailing whitespace |
|
while ( ( *end == '\n' || *end == '\r' ) && end > str ) end--; |
|
|
|
while ( *position != ':' && position < end ) position++; |
|
|
|
|
|
if ( position == end ){ |
|
printf("Header without colon. Not sure what to do\n"); |
|
return NULL; |
|
} |
|
|
|
|
|
header->name = malloc( sizeof(char) * ( position + 1 - str ) ); |
|
memset(header->name, '\0', position + 1 - str ); |
|
strncpy( header->name, str, position - str ); |
|
|
|
//Skip over the colon |
|
position++; |
|
//Skip over any trailing whitespace |
|
while ( *position == '\t' || *position == ' ' ) position++; |
|
|
|
//Add 2 when allocating so that we have space for a \0 at the end |
|
header->value = malloc( sizeof(char) * ( end + 2 - position ) ); |
|
memset(header->value, '\0', ( end + 2 - position ) ); |
|
strncpy( header->value, position, end + 1 - position ); |
|
|
|
return header; |
|
|
|
|
|
} |
|
|
|
|
|
void freeHeader(Header *header){ |
|
free(header->name); |
|
free(header->value); |
|
free(header); |
|
} |
|
|
|
|
|
void addHeader(HeaderList *headers, char *str){ |
|
HeaderList *newListItem = malloc(sizeof(HeaderList)); |
|
|
|
newListItem->header = newHeader(str); |
|
newListItem->next = NULL; |
|
|
|
HeaderList *last = headers; |
|
|
|
while ( last->next != NULL ) last = last->next; |
|
|
|
last->next = newListItem; |
|
} |
|
|
|
void freeHeaderList(HeaderList *headers){ |
|
HeaderList *current; |
|
while ( headers != NULL ){ |
|
current = headers; |
|
freeHeader( current->header ); |
|
headers = headers->next; |
|
free( current ); |
|
} |
|
} |
|
|
|
|
|
// Will return the first header in a header list with the name matching name |
|
// It matches name case insensitively as header names are case insensitive |
|
|
|
Header* getHeader(HeaderList *headers, char *name){ |
|
HeaderList *curr = headers; |
|
while ( curr != NULL ){ |
|
// Header names are case insensitive |
|
if ( strcasecmp( curr->header->name, name) == 0 ){ |
|
return curr->header; |
|
} |
|
curr = curr->next; |
|
} |
|
return NULL; |
|
} |
|
|
|
|
|
unsigned int countHeaders(HeaderList *headers){ |
|
unsigned int count = 0; |
|
while (headers != NULL){ |
|
count++; |
|
headers = headers->next; |
|
} |
|
return count; |
|
} |
|
|
|
unsigned int headerListCharLength(HeaderList *headers){ |
|
unsigned int length = 0; |
|
while (headers != NULL){ |
|
// 4 = colon + space + \r + \n |
|
length += strlen(headers->header->name) + strlen(headers->header->value) + 4; |
|
headers = headers->next; |
|
} |
|
return length; |
|
} |
|
|
|
|
|
char* headersToString(HeaderList *headers){ |
|
int len = headerListCharLength(headers) + 1; //null pointer |
|
HeaderList *curr = headers; |
|
char *retStr = malloc( len * sizeof(char) ); |
|
memset(retStr, '\0', len * sizeof(char) ); |
|
while ( curr != NULL ){ |
|
strcat( retStr, curr->header->name ); |
|
strcat( retStr, ": " ); |
|
strcat( retStr, curr->header->value ); |
|
strcat( retStr, "\r\n" ); |
|
curr = curr->next; |
|
} |
|
return retStr; |
|
}
|
|
|