-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrequestinfo.h
209 lines (173 loc) · 4.81 KB
/
requestinfo.h
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#pragma once
#include <string>
#include <map>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include "httputil.h"
#include "urlparser.h"
enum HTTP_METHOD
{
METHOD_UNKNOWN,
METHOD_POST,
METHOD_GET,
METHOD_PUT,
METHOD_DELETE,
METHOD_HEAD,
};
class CAsyncHttpClient;
class RequestInfo
{
friend class CAsyncHttpClient;
public:
//default url_encode the path part in url
RequestInfo()
: m_method(METHOD_GET)
, m_http_ver("HTTP/1.1")
, m_should_encode_path(true)
{
}
void set_url(const std::string& url)
{
m_urlparser.parse(url);
}
void set_method(const HTTP_METHOD& m)
{
m_method = m;
}
void set_http_ver(const std::string& v)
{
m_http_ver = v;
}
void set_query_param(const std::string& query_param)
{
m_query_param = query_param;
}
void add_header(const std::string& key, const std::string& value)
{
m_headers[key] = value;
}
void add_body(const std::string& s)
{
m_body += s;
}
void add_body(const void *p, const size_t len)
{
m_body += std::string(reinterpret_cast<const char *>(p), len);
}
//enable/disable path url_encode in build_as_string
void enbale_path_encode(const bool enabled)
{
m_should_encode_path = enabled;
}
private:
std::string gethostname() const
{
return m_urlparser.host_part;
}
//for getaddrinfo
std::string getservicename() const
{
if (m_urlparser.port)//use port
{
return boost::lexical_cast<std::string>(m_urlparser.port);
}
else if (!m_urlparser.proto.empty())//use proto
{
return m_urlparser.proto;
}
else//if no port, no proto, use http
{
return "http";
}
}
std::string build_as_string() const
{
std::stringstream ss;
switch (m_method)
{
case METHOD_POST:
ss << "POST";
break;
case METHOD_GET:
ss << "GET";
break;
case METHOD_PUT:
ss << "PUT";
break;
case METHOD_DELETE:
ss << "DELETE";
break;
case METHOD_HEAD:
ss << "HEAD";
break;
default:
break;
}
ss << " ";
//construct complete query_param
std::string query_param_all;
if (!m_urlparser.query_param.empty() && !m_query_param.empty())
{
query_param_all = m_urlparser.query_param + "&" + m_query_param;
}
else
{
query_param_all = m_urlparser.query_param + m_query_param;
}
//in the first line, use the complete url for dealing with proxy
//http://www.jmarshall.com/easy/http/#proxies
if (!m_urlparser.proto.empty())
{
ss << m_urlparser.proto << "://";
}
ss << m_urlparser.host_all;
if (m_should_encode_path)
{
std::string encoded_path;
std::vector<std::string> paths;
boost::algorithm::split(paths, m_urlparser.path, boost::algorithm::is_any_of("/"));
for (std::vector<std::string>::const_iterator iter_path = paths.begin();
iter_path != paths.end();
++iter_path)
{
encoded_path += HttpUtil::url_encode(*iter_path) + "/";
}
boost::algorithm::trim_if(encoded_path, boost::algorithm::is_any_of("/"));
ss << "/" << encoded_path;
}
else
{
ss << m_urlparser.path;
}
if (!query_param_all.empty())
{
ss << "?" << query_param_all;
}
ss << " " << m_http_ver << "\r\n";
for (std::map<std::string, std::string>::const_iterator iterKey = m_headers.begin();
iterKey != m_headers.end();
++iterKey)
{
ss << iterKey->first << ": " << iterKey->second << "\r\n";
}
if (0 == m_headers.count("Host"))
{
ss << "Host: " << m_urlparser.host_all << "\r\n";
}
if (0 == m_headers.count("Content-Length") && !m_body.empty())
{
ss << "Content-Length: " << m_body.size() << "\r\n";
}
ss << "\r\n";
ss << m_body;
return ss.str();
}
private:
HTTP_METHOD m_method;
std::string m_http_ver;
std::string m_query_param;
std::map<std::string, std::string> m_headers;
std::string m_body;
bool m_should_encode_path;
UrlParser m_urlparser;
};