dmpserverrequest.cpp
2.2 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
/**************************************************************************
* file: dmpserverrequest.cpp
* Author: wanzhongping
* Date: 2021-01-08 11:10:59
* Email: zhongpingw@chinadci.com
* copyright: 广州城市信息研究所有限公司
***************************************************************************/
#include "dmpserverrequest.h"
#include <boost/regex.hpp>
#include <boost/make_shared.hpp>
#include "dmplogger.h"
DmpServerRequest::DmpServerRequest()
{
}
DmpServerRequest::DmpServerRequest(const std::string &url, Method method, const Headers &headers)
:url_(url)
,method_(method)
,headers_(headers)
{
}
void DmpServerRequest::set_url(const std::string &url)
{
url_ = url;
if (!ParseUrl(url_,protocol_,domain_,port_,path_,query_)) {
LOGGER_ERROR("非法的URL!");
return;
}
set_query(query_);
}
void DmpServerRequest::set_query(const std::string &query_string)
{
query_ = query_string;
params_.SetQueryString(query_);
}
void DmpServerRequest::set_method(Method method)
{
method_ = method;
}
void DmpServerRequest::SetParameter(const std::string &name, const std::string &value)
{
params_.Add(name, value);
}
bool DmpServerRequest::ParseUrl(const std::string& url,
std::string &protocol,
std::string &domain,
std::string &port,
std::string &path,
std::string &query_string)
{
if (url.empty()) {
return false;
}
boost::regex ex("(http|https)://([^/ :]+):?([^/ ]*)(/?[^ #?]*)\\x3f?([^ #]*)#?([^ ]*)");
boost::cmatch what;
if (regex_match(url.c_str(), what, ex))
{
protocol = std::string(what[1].first, what[1].second);
domain = std::string(what[2].first, what[2].second);
port = std::string(what[3].first, what[3].second);
path = std::string(what[4].first, what[4].second);
query_string = std::string(what[5].first, what[5].second);
return true;
}
return false;
}
void DmpServerRequest::set_header(const std::string &name, const std::string &value)
{
headers_[name] = value;
}