-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttpParser.c
179 lines (138 loc) · 5.2 KB
/
httpParser.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
A simple HTTP Web Server.
Copyright (C) 04/07/15 José Luis Valencia Herrera
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "httpParser.h"
static int setRequestMethod(HttpRequestData_t *requestData, const char *requestMethod);
static int setHttpVersion(HttpRequestData_t *requestData, char *requestVersion);
void httpParser_Init() {
}
int httpParser_GetRequestLines(char *message, char linesBuffer[][MAX_HTTP_REQUEST_SIZE], const size_t linesBufferLen, char **saveState) {
char *result = NULL;
int linesParsed = 0;
result = strtok_r(message, "\n", saveState);
if (result) {
do {
if (linesParsed >= linesBufferLen) {
break;
}
strncpy(linesBuffer[linesParsed], result, MAX_HTTP_REQUEST_SIZE);
++linesParsed;
result = strtok_r(NULL, "\n", saveState);
}
while (result);
}
return linesParsed;
}
int httpParser_ParseRequestLine(char *requestLine, HttpRequestData_t *requestData, char **saveState) {
char *result = NULL;
result = strtok_r(requestLine, " ", saveState);
if (!result) return 0;
if (!setRequestMethod(requestData, result)) return 0;
result = strtok_r(NULL, " ", saveState);
if (!result) return 0;
strncpy(requestData->requestURI, result, MAX_REQUEST_URI_LEN);
result = strtok_r(NULL, " ", saveState);
if (!result) return 0;
if (!setHttpVersion(requestData, result)) return 0;
return 1;
}
static int setRequestMethod(HttpRequestData_t *requestData, const char *requestMethod) {
if (!requestMethod || !requestData) {
return 0;
}
if (0 == strcmp(requestMethod, "GET")) {
requestData->requestMethod = HTTP_REQM_GET;
}
else if (0 == strcmp(requestMethod, "HEAD")) {
requestData->requestMethod = HTTP_REQM_HEAD;
}
else if (0 == strcmp(requestMethod, "POST")) {
requestData->requestMethod = HTTP_REQM_POST;
}
else if (0 == strcmp(requestMethod, "PUT")) {
requestData->requestMethod = HTTP_REQM_PUT;
}
else if (0 == strcmp(requestMethod, "DELETE")) {
requestData->requestMethod = HTTP_REQM_DELETE;
}
else if (0 == strcmp(requestMethod, "CONNECT")) {
requestData->requestMethod = HTTP_REQM_CONNECT;
}
else if (0 == strcmp(requestMethod, "OPTIONS")) {
requestData->requestMethod = HTTP_REQM_OPTIONS;
}
else if (0 == strcmp(requestMethod, "TRACE")) {
requestData->requestMethod = HTTP_REQM_TRACE;
}
else {
return 0;
}
return 1;
}
static int setHttpVersion(HttpRequestData_t *requestData, char *requestVersion) {
char *result = NULL;
char *saveState = NULL;
if (!requestVersion || !requestData) {
return 0;
}
result = strtok_r(requestVersion, "/", &saveState);
if (!result) return 0;
if (0 != strcmp(result, "HTTP")) return 0;
result = strtok_r(NULL, "/", &saveState);
if (!result) return 0;
requestData->httpVersion = atof(result);
return 1;
}
int httpParser_GenerateHttpResponse(char *replyBuffer, size_t bufferLen, const HttpStatusCode_t statusCode, const HttpContentType_t contentType, const char *content, const int contentLength) {
return snprintf(replyBuffer, bufferLen, "HTTP/1.1 %d OK\r\nContent-Length: %d\r\nContent-Type: %s\r\nConnection: Closed\r\n\r\n%s\r\n", statusCode, contentLength, httpParser_GetContentTypeAsString(contentType), content);
}
HttpContentType_t httpParser_DetermineContentTypeFromFileExtension(const char *filePath) {
char *token = NULL;
char *saveState = NULL;
char tmpFileBuffer[MAX_REQUEST_URI_LEN];
char auxBuffer[BUFSIZ];
HttpContentType_t contentType;
memset(auxBuffer, 0, BUFSIZ);
memcpy(tmpFileBuffer, filePath, MAX_REQUEST_URI_LEN);
token = strtok_r(tmpFileBuffer, ".", &saveState);
while(token) {
memcpy(auxBuffer, token, BUFSIZ);
token = strtok_r(NULL, ".", &saveState);
}
if (strcmp(auxBuffer, "html") == 0) {
contentType = HTTP_CONTENT_TEXT_HTML;
}
else if (strcmp(auxBuffer, "xml") == 0) {
contentType = HTTP_CONTENT_TEXT_XML;
}
else {
contentType = HTTP_CONTENT_TEXT_PLAIN;
}
return contentType;
}
char *httpParser_GetContentTypeAsString(const HttpContentType_t contentType) {
switch(contentType) {
case HTTP_CONTENT_TEXT_HTML:
return "text/html";
case HTTP_CONTENT_TEXT_XML:
return "text/xml";
case HTTP_CONTENT_TEXT_PLAIN:
default:
return "text/plain";
}
}