-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuws_header.c
99 lines (98 loc) · 3.6 KB
/
uws_header.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "uws_header.h"
#include "uws_utils.h"
#include "uws_memory.h"
char* get_header_param(char* key, struct http_header *http_header){
int i = 0;
if(http_header->params == NULL)
return NULL;
while(i < http_header->used_len){
if(strcmp(key, http_header->params[i].name) == 0) {
return http_header->params[i].value;
}
i++;
}
return NULL;
}
void add_header_param(char *key, char *value, struct http_header *http_header){
int i = 0;
if(http_header->params == NULL)
{
http_header->params = (Http_Param*) uws_malloc(INIT_PARAMS_NUM * sizeof(Http_Param));
http_header->used_len = 0;
http_header->max_len = INIT_PARAMS_NUM;
}
for(i = 0; i < http_header->used_len; i++) {
if(strcmp(key, http_header->params[i].name) == 0) {
uws_free(http_header->params[i].value);
http_header->params[i].value = uws_strdup(value);
return;
}
}
if(http_header->used_len == http_header->max_len - 1)
{
http_header->max_len *= 2;
Http_Param* tmp = http_header->params;
http_header->params = (Http_Param*)uws_realloc(http_header->params, http_header->used_len * sizeof(Http_Param), http_header->max_len * sizeof(Http_Param));
}
Http_Param* new_param = &http_header->params[http_header->used_len];
new_param->name = uws_strdup(key);
new_param->value = uws_strdup(value);
http_header->used_len++;
}
void push_header_param(char *key, char *value, struct http_header *http_header){
if(http_header->params == NULL)
{
http_header->params = (Http_Param*) uws_malloc(INIT_PARAMS_NUM * sizeof(Http_Param));
http_header->used_len = 0;
http_header->max_len = INIT_PARAMS_NUM;
}
if(http_header->used_len == http_header->max_len - 1)
{
http_header->max_len *= 2;
Http_Param* tmp = http_header->params;
http_header->params = (Http_Param*)uws_malloc(http_header->max_len * sizeof(Http_Param));
memcpy(http_header->params, tmp, http_header->used_len);
uws_free(tmp);
}
Http_Param* new_param = &http_header->params[http_header->used_len];
new_param->name = uws_strdup(key);
new_param->value = uws_strdup(value);
http_header->used_len++;
}
void free_header_params(struct http_header *http_header)
{
int i = 0;
for(i = 0; i < http_header->used_len; i++) {
uws_free(http_header->params[i].name);
uws_free(http_header->params[i].value);
}
uws_free(http_header->params);
http_header->params = NULL;
http_header->used_len = 0;
}
char* str_response_header(struct http_header *header) {
char *response_str = (char*) uws_malloc(HEADER_LEN * sizeof(char));
int i;
sprintf(response_str, "%s %d %s", header->http_ver, header->status_code, header->status);
strcat(response_str, "\r\n");
for(i = 0; i < header->used_len; i++) {
strcat(response_str, header->params[i].name);
strcat(response_str, ": ");
strcat(response_str, header->params[i].value);
strcat(response_str, "\r\n");
}
return response_str;
}
char* str_request_header(struct http_header *header) {
char *request_str = (char*) uws_malloc(HEADER_LEN * sizeof(char));
int i;
sprintf(request_str, "%s %s %s", header->method, header->url, header->http_ver);
strcat(request_str, "\r\n");
for(i = 0; i < header->used_len; i++) {
strcat(request_str, header->params[i].name);
strcat(request_str, ": ");
strcat(request_str, header->params[i].value);
strcat(request_str, "\r\n");
}
return request_str;
}