-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.c
152 lines (108 loc) · 2.75 KB
/
http.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "http.h"
#include "utils.h"
const char HTTP_PROTOCOL_1_1[] = "HTTP/1.1";
const char HTTP_METHOD_GET[] = "GET";
HttpRequest *http_request_new()
{
HttpRequest *request = malloc(sizeof(HttpRequest));
memset(request, 0, sizeof(*request));
return request;
}
char http_header_parse_field(HttpHeader *header, const char *field)
{
unsigned int fieldLen = strlen(field);
char *key = strtok((char *)field, ":");
if (strlen(key) == fieldLen)
{
header->protocol = strtok(key, " ");
header->status = atoi(strtok(NULL, " "));
header->statusMsg = strtok(NULL, "\r");
}
else
{
char *value = strtok(NULL, "\r");
if (!strcmp(key, "Server"))
header->server = value;
else if (!strcmp(key, "Last-Modified"))
header->lastModified = value;
else if (!strcmp(key, "ETag"))
header->eTag = value;
else if (!strcmp(key, "Content-Type"))
header->contentType = value;
else if (!strcmp(key, "Expires"))
header->expires = value;
else if (!strcmp(key, "Cache-Control"))
header->cacheControl = value;
else if (!strcmp(key, "Pragma"))
header->pragma = value;
else if (!strcmp(key, "Date"))
header->date = value;
else if (!strcmp(key, "Content-Length"))
header->contentLength = atoi(value);
else if (!strcmp(key, "Connection"))
header->connection = value;
else if (!strcmp(key, "Set-Cookie"))
header->setCookie = value;
}
return 0;
}
void http_request_free(HttpRequest *request)
{
if (request->method)
free_null(request->method);
if (request->protocol)
free_null(request->protocol);
if (request->uri)
free_null(request->uri);
if (request->host)
free_null(request->host);
free_null(request);
}
Uri *uri_new()
{
Uri *uri = malloc(sizeof(Uri));
memset(uri, 0, sizeof(*uri));
return uri;
}
Uri *uri_parse(const char *uriStr)
{
Uri *uri = uri_new();
unsigned int protocolEnd = 0;
unsigned int hostEnd = 0;
for (unsigned int index = 1; index < strlen(uriStr); index++)
{
// We have found a protocol.
if (uriStr[index] == ':')
{
uri->protocol = malloc(index + 1);
protocolEnd = index + 3;
strncpy(uri->protocol, uriStr, index);
uri->protocol[index] = '\0';
break;
}
}
for (unsigned int index = 1; index + protocolEnd < strlen(uriStr); index++)
{
if (uriStr[index + protocolEnd - 1] != '/' && uriStr[index + protocolEnd] == '/')
{
uri->host = malloc(index + 1);
hostEnd = index + protocolEnd;
strncpy(uri->host, uriStr + protocolEnd, index);
uri->host[index] = '\0';
break;
}
}
uri->path = malloc(strlen(uriStr) - hostEnd + 1);
strcpy(uri->path, uriStr + hostEnd);
return uri;
}
void uri_free(Uri *uri)
{
if (uri->protocol)
free_null(uri->protocol);
if (uri->host)
free_null(uri->host);
if (uri->path)
free_null(uri->path);
free_null(uri);
}