-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttprequest.cpp
More file actions
139 lines (128 loc) · 3.3 KB
/
httprequest.cpp
File metadata and controls
139 lines (128 loc) · 3.3 KB
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
#include "httprequest.h"
/* const string body;
string method;
string path;
string version;
map<string, string> header;
map<string, string> args;
bool keep_alive;
*/
int HttpRequest::parse_headers(string &str){
if(str.empty()){
return -1;
}
vector<string> res = split(str,"\r\n");
//pv(res);
int str_size = str.size();
int flag =1;
size_t pos;
string key, val;
for(vector<string>::iterator it=res.begin(); it!=res.end(); it++){
if(flag){
if(it->empty()){
flag = 0;
}else{
if((pos=it->find(":"))!=string::npos){
key = it->substr(0,pos);
val = it->substr(pos+1);
if(key=="Connection" && val=="keep-alive"){
keep_alive = true;
}
headers.insert(pair<string, string>(key,val));
}else{
cout<<"error at parse headers:"<<*it<<endl;
continue;
}
}
}else{
//cout<<"setbody"<<endl;
body = *it;
}
}
return 0;
}
int HttpRequest::parse_basic(string &str){
if(str.empty()){
return -1;
}
vector<string> res = split(str, " ");//copy构造函数和 copy赋值函数
if(res.size()==3){
method = res[0];
parse_path(res[1]);
version = res[2];
}else if(res.size()==2){
method = res[0];
parse_path(res[1]);
}else{
return -1;
}
return 0;
}
int HttpRequest::parse_args(string & str){
if(str.empty()){
return -1;
}
//先替换转义字符串, 然后处理参数
return 0;
}
int HttpRequest::parse_path(string & str){
string tpath(""), query(""); //what is tpath used for? why not path=subresult?
size_t pos;
if((pos = str.find('?'))!=string::npos){
query = str.substr(pos+1);
parse_args(query);
}
tpath = str.substr(0, pos);
path=tpath;
return 0;
}
HttpRequest::HttpRequest(string &str){
int pos = str.find("\r\n");
int status = -1;
//Question:parse_basic(str.substr(pos)); return error?
if(pos!=string::npos){
ok = true;
string tmp = str.substr(0,pos);
/* Question:
* string::substr(size_t pos = 0, size_t len = npos);
* 定义的时候, 默认参数右边必定是默认参数
*
* 调用的时候, 默认参数必须按顺序赋值,不能跳过其中一个?????这个设计不好吧?
* */
status = parse_basic(tmp);
if(status<0){
//error
}
tmp = str.substr(pos+2);
status = parse_headers(tmp);
if(status<0){
//error;
}
}else{
ok = false;
cout<<"get the end"<<endl;
};
}
HttpRequest::~HttpRequest(){
}
const string & HttpRequest::getmethod(){
return method;
};
const string & HttpRequest::getpath(){
return path;
};
const string & HttpRequest::getversion(){
return version;
};
const map<string, string> & HttpRequest::getheaders(){
return headers;
};
const map<string, string> & HttpRequest::getargs(){
return args;
};
const bool HttpRequest::is_alive(){
return keep_alive;
}
const string & HttpRequest::getbody(){
return body;
}