-
Notifications
You must be signed in to change notification settings - Fork 4
/
html_parser.cpp
76 lines (70 loc) · 1.65 KB
/
html_parser.cpp
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
#include "html_parser.h"
html_parser::html_parser(char * buffer,int buffer_length)
{
if(buffer_length==0) return ;
std::vector <std::string> lines;
int header_ends=0;
for (int i = 0; i < buffer_length; i++)
{
std::string a;
while(i<buffer_length && buffer[i]!='\n' && buffer[i]!=EOF)
{
a+=buffer[i];
i++;
}
if((a.size()==0 || a[0]=='\n') && header_ends==0)
{
header_ends=lines.size();
}
lines.push_back(a);
}
std::string tmp[3];
int tmp_cnt=0;
for (int i = 0; i < lines[0].size(); i++)
{
if(lines[0][i]==' ')
tmp_cnt++;
else
tmp[tmp_cnt].push_back(lines[0][i]);
}
if(tmp[0]=="GET")
request_type = 0;
else if(tmp[0]=="POST")
request_type = 1;
else if(tmp[0]=="PUT")
request_type = 2;
url = tmp[1];
if(request_type==0)
url_parser(url);
if(request_type!=0)
{
int t=buffer_length--;
while(t>=0 && buffer[t]!='\n' && buffer[t]!='=')
t--;
t++;
while(t<buffer_length)
{
text+=buffer[t];
t++;
}
text+=buffer[t];
}
}
int html_parser:: get_request_type()
{
return request_type;
}
void html_parser::url_parser(std::string url)
{
if(url.find("name")==std::string::npos) return ;
request_inputs["name"] = url.substr(url.find("name")+5);
}
std::string html_parser:: get_input(std::string a)
{
if(request_inputs.count(a)==0) return "";
return request_inputs[a];
}
std::string html_parser::get_text()
{
return text;
}