This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle.cc
128 lines (117 loc) · 2.66 KB
/
handle.cc
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
#include "http.hpp"
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
using namespace std;
void parse_request(http_context &http_context, const char *buf)
{
size_t i = 0, j = 0;
char method[8] = {};
char *url = new char[1024];
char *query = new char[1024];
size_t buflen = strlen(buf);
while (i < buflen && j < 7 && !isspace(buf[i]))
{
method[j++] = buf[i++];
}
method[j] = '\0';
http_context.raw_method = method;
if (!strcmp(method, "GET"))
{
http_context.method = GET;
}
else if (!strcmp(method, "POST"))
{
http_context.method = POST;
}
else if (!strcmp(method, "PUT"))
{
http_context.method = PUT;
}
else if (!strcmp(method, "DELETE"))
{
http_context.method = DELETE;
}
else if (!strcmp(method, "PATCH"))
{
http_context.method = PATCH;
}
else if (!strcmp(method, "HEAD"))
{
http_context.method = HEAD;
}
else if (!strcmp(method, "OPTIONS"))
{
http_context.method = OPTIONS;
}
else if (!strcmp(method, "TRACE"))
{
http_context.method = TRACE;
}
else
{
http_context.method = UNKNOWN;
}
while (i < buflen && isspace(buf[i]))
i++;
j = 0;
int k = 0;
while (i < buflen && j < 1023 && !isspace(buf[i]))
{
url[j++] = buf[i++];
if (buf[i] == '?')
{
k = 0;
i++;
while (i < strlen(buf) && k < 1023 && !isspace(buf[i]))
{
query[k++] = buf[i++];
}
}
}
url[j] = '\0';
query[k] = '\0';
http_context.path = url;
http_context.query_string = query;
while (i < buflen && buf[i] != '\n')
{
i++;
}
i++;
while (i < buflen)
{
char *key = new char[64];
char *value = new char[1024];
j = 0;
memset(key, '\0', 64);
memset(value, '\0', 1024);
while (i < buflen && buf[i] != ':')
{
key[j++] = buf[i++];
}
key[j] = '\0';
j = 0;
while (i < buflen && (buf[i] == ':' || isspace(buf[i])))
i++;
while (i < buflen && buf[i] != '\n')
{
value[j++] = buf[i++];
}
if (value[j - 1] == '\r')
{
value[j - 1] = 0;
}
value[j] = '\0';
http_context.headers.insert(pair<string, string>(key, value));
i = i + 1;
delete[] key;
delete[] value;
if (buf[i + 1] == '\n')
break;
}
delete[] url;
delete[] query;
http_context.body = &buf[i + 2];
}