-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_request_parser.c
151 lines (129 loc) · 5.16 KB
/
http_request_parser.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
/**MIT License
Copyright (c) 2018 Vysakh P Pillai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
**/
#include "http_request_parser.h"
#include "helperFunctions.h"
#include "http_config.h"
#include "http_common.h"
#include <string.h>
#include <stdio.h>
//list of file types supporting SSI
unsigned int httpFileType_SSIList_size = 4;
const char *const httpFileType_SSIList[] = {"shtml", "shtm", "ssi", "xml"};
//list of file types supporting CGI
unsigned int httpFileType_CGIList_size = 3;
const char *const httpFileType_CGIList[] = {"cgi", "sh", "exe"};
static httpRequest_method_t parseRequest_mapMethodToEnum(char *method)
{
/*since most requesters send method in caps, toUpper is not applied */
if (0 == strcmp(method, "GET"))
return GET;
else if (0 == strcmp(method, "POST"))
return POST;
else if (0 == strcmp(method, "PUT"))
return PUT;
else if (0 == strcmp(method, "HEAD"))
return HEAD;
else if (0 == strcmp(method, "DELETE"))
return DELETE;
else if (0 == strcmp(method, "OPTIONS"))
return OPTIONS;
else if (0 == strcmp(method, "CONNECT"))
return CONNECT;
else if (0 == strcmp(method, "TRACE"))
return TRACE;
else if (0 == strcmp(method, "PATCH"))
return PATCH;
else
return NONE;
}
//identify whether the requested file is CGI/SSI or just for regular contents.
static httpRequest_fileClass_t parseRequest_identifyFileClass(char *path)
{
char *fileType = strrchr(path, '.'); //can hit some real corner case where there is no extension and thre is a . in path
if (0 == fileType)
{
return httpFileClass_none;
}
else
{
fileType += 1; //to remove the .
}
unsigned int i = 0;
for (i = 0; i < httpFileType_SSIList_size; i++)
{
if (0 == strcmp(httpFileType_SSIList[i], fileType))
return httpFileClass_SSI;
}
for (i = 0; i < httpFileType_CGIList_size; i++)
{
if (0 == strcmp(httpFileType_CGIList[i], fileType))
return httpFileClass_CGI;
}
return httpFileClass_none;
}
/*
Function to parse incoming request buffer and identify the request method,
and path.
At this initial stage , query strings and headers will not be handled since
the initial version is targeting just GET requests.
*/
int parseRquest_identifyRequest(unsigned char *requestBuffer, http_request_t *httpRequest)
{
/*TODO: suppot Query parsing*/
const char *startOfPath = strchr((char*)requestBuffer, ' ') + 1;
const char *endOfPath;
if (strstr((char*)requestBuffer, "?"))
{ //if there is a query string, path ends at the start of query
endOfPath = strchr(startOfPath, '?');
}
else if (strstr((char*)requestBuffer, "HTTP/1."))
{ //if there is a HTTP version mentioned, path ends there
endOfPath = strchr(startOfPath, ' ');
}
else
{
endOfPath = strchr(startOfPath, '\n'); //if none of the above, path ends at newline
}
//getting enough memory
//TODO: will this work in embedded?
char *methordToken = strtok((char*)requestBuffer, " ");
char method[strlen(methordToken) + 1];
char path[endOfPath - startOfPath];
//copying internally
strncpy(method, methordToken, strlen(methordToken));
strncpy(path, startOfPath, endOfPath - startOfPath);
//null termination
method[sizeof(method)-1] = 0; //-1 to delete trailing space
path[sizeof(path)-1] = 0; //TODO: investigate trailing space
//parse and copy method for returning.
httpRequest->method = parseRequest_mapMethodToEnum(method);
//decode URL and copy path for returning.
char decodedURL[sizeof(path)];
url_decode(path, decodedURL);
if(0==strcmp(decodedURL,"/")){ //redirect / to configured ROOT page.
strncpy(httpRequest->httpFilePath, HTTP_SERVER_ROOT_PAGE, HTTP_MAX_PATH_LENGTH);
}
else{
strncpy(httpRequest->httpFilePath, decodedURL, HTTP_MAX_PATH_LENGTH);
}
//decode file type and add copy it for returning
httpRequest->fileClass = parseRequest_identifyFileClass(path);
//return success
return HTTP_SUCCESS;
}