-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
194 lines (176 loc) · 4.7 KB
/
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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "parser.h"
int Parser(clientSock *client,requestLine *argu)
{
char cmpletReq[BUF_SIZE];
if(bufParse(client,argu,cmpletReq)==1)
{
if( (cmpletReq != NULL) && argu->status != 411) {
httpParser(cmpletReq,argu);
}
return 1;
}
else{
return 0;
}
}
static int BadRq(requestLine *argu, char *key)
{
char *headerSet[] = {
"Content-Length:",
"Content-Type:",
"Last-Modifed:",
"Connection:",
"Date:",
"Host:",
"User-Agent:",
"Accept:",
"Accept-Language:",
"Accept-Encoding:",
"Cache-Control:",
"Accept-Encoding:"
};
int n = 12;
int i;
// printf("key: %s\n", key);
for(i = 0; i < n; i ++)
{
// printf("headerset :: %s\n", headerSet[i]);
if(!strcmp(headerSet[i],key))
{
return 1;
}
}
return 0;
}
int httpParser(char *buf, requestLine *argu)
{
int lineSize;
char *temp;
char *currPtr;
if(!(currPtr = strstr(buf,"\r\n")))
{
argu->status = 500;
return 0;
}
if((lineSize = strlen(buf)-strlen(currPtr))<=0)
{
argu->status = 500;
return 0;
}
if(!(temp = malloc(lineSize+1)))
{
argu->status = 500;
return 0;
}
char *t = strncpy(temp,buf,lineSize);
t[lineSize] = 0;
httpLineParser(temp,lineSize, argu);
if(!temp)
{
free(temp);
}
char *leftLine;
int length = strlen(currPtr)-2;
leftLine = (char *)malloc(length+1);
strncpy(leftLine,currPtr+2,length);
leftLine[length]='\0';
char *pch;
pch = strtok(leftLine,"\n");
printf("req->status: %d\n",argu->status);
printf("leftLine:: %s\n",leftLine );
while(pch != NULL)
{
char key[20];
char value[40];
sscanf(pch,"%s %s",key,value);
if(0 == BadRq(argu,key))
{
argu->status = 400;
}
if(strcmp(key,"Content-Type:")==0)
{
strcpy(argu->contentType,value);
}else if(strcmp(key,"Content-Length:")==0)
{
argu->contentLength = atoi(value);
}
else if(strcmp(key,"Connection:")==0)
{
strcpy(argu->connect,value);
}
pch = strtok(NULL,"\n");
}
free(leftLine);
printf("req->status: %d\n",argu->status);
return 1;
}
int bufParse(clientSock *client, requestLine *req, char *cmpletReq)
{
printf("buf parse\n");
if(strcmp(client->buf,"\r\n") == 0) /* Request is not a complete request */
{
memset(client->buf,0,BUF_SIZE);
client->alreadyRead=0;
return 0;
}
char *temp = strstr(client->buf,"\r\n\r\n");
if(temp)
{
int lenReq = strlen(client->buf)-strlen(temp);
if(lenReq > 8182) /* Request length exceeds the limitation 8192 */
{
printf("long request line\n");
memset(client->buf, 0, BUF_SIZE);
req->status = 411;
}
else /* Normal request */
{
assert(req->status == 200 && "status must be 200");
char *end = strncpy(cmpletReq, client->buf, lenReq+2);
end[lenReq+2] = 0; /* null terminate */
client->alreadyRead = 0;
memset(client->buf, 0, BUF_SIZE);
}
return 1;
} else{
req->status = 400;
}
return 0;
}
void httpLineParser(char *temp, int lens, requestLine *argu){
printf("temp :: %s\n", temp);
sscanf(temp,"%s %s %s", argu->method, argu->uri, argu->version);
printf("line parser result: %s, %s, %s\n", argu->method, argu->uri, argu->version);
parseUri(argu);
}
void parseUri(requestLine *argu){
int tmpNum=0;
int *tmpPort=NULL;
if (strstr(argu->uri, "http://")) {
if (sscanf(argu->uri, "http://%8192[^:]:%i/%8192[^\n]", argu->host, tmpPort, argu->relativePath) == 3)
{
tmpNum = 1;
}else if (sscanf(argu->uri, "http://%8192[^/]/%8192[^\n]", argu->host, argu->relativePath) == 2)
{
tmpNum = 2;
}else if (sscanf(argu->uri, "http://%8192[^:]:%i[^\n]", argu->host, tmpPort) == 2)
{
tmpNum = 3;
}else if (sscanf(argu->uri, "http://%8192[^/]", argu->host) == 1)
{
tmpNum = 4;
}
}
else if (!strstr(argu->uri, "/")) {
sprintf(argu->relativePath, "/");
}
else {
strcpy(argu->relativePath, argu->uri);
}
if (tmpNum == 4) {
sprintf(argu->relativePath, "/index.html");
}
else if (!strcmp(argu->relativePath, "/")) {
strcat(argu->relativePath, "index.html");
}
}