dmpspserverrequest.cpp
3.4 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
/**************************************************************************
* file: dmpspserverrequest.cpp
* Author: wanzhongping
* Date: 2021-01-14 21:59:49
* Email: zhongpingw@chinadci.com
* copyright: 广州城市信息研究所有限公司
***************************************************************************/
#include "dmpspserverrequest.h"
#include <boost/regex.hpp>
#include "dmpserverutils.h"
DmpSpServerRequest::DmpSpServerRequest(SP_HttpRequest* request)
{
const char* h = request->getHeaderValue("host");
std::string host(h);
std::string::size_type p = host.find(":");
if (p != std::string::npos) {
domain_= host.substr(0, p);
port_ = host.substr(p+1,host.length());
}
else {
domain_ = host;
}
const char* med = request->getMethod();
if (med)
{
if (strcmp(med,"POST") == 0)
{
setMethod(POST_METHOD);
data_ = request->getContent();
dataLen_ = request->getContentLength();
}
else
{
setMethod(GET_METHOD);
}
}
//获取querystring
std::string url(request->getURL());
std::string deurl;
this->UrlDecode(url,deurl);
std::string::size_type pos = deurl.find("?");
if (pos != std::string::npos) {
std::string path = deurl.substr(0, pos);
setPath(path);
std::string queryString;
queryString = deurl.substr(pos+1,deurl.length());
setQuery(queryString);
}
else {
isRestful_ = true;
setPath(deurl);
}
// for(int i=0; i< request->getParamCount(); i++) {
// SetParameter(request->getParamName(i), request->getParamValue(i));
// }
//添加请求头信息
for(int i=0; i< request->getHeaderCount(); i++) {
setHeader(request->getHeaderName(i),request->getHeaderValue(i));
}
}
const void * DmpSpServerRequest::GetData() const
{
return data_;
}
int DmpSpServerRequest::GetDataLength() const
{
return dataLen_;
}
bool DmpSpServerRequest::UrlDecode(const std::string& src, std::string& dst)
{
if(src.size() == 0)
return false;
int hex = 0;
for (size_t i = 0; i < src.length(); ++i)
{
switch (src[i])
{
case '+':
dst += ' ';
break;
case '%':
{
if (isxdigit(src[i + 1]) && isxdigit(src[i + 2]))
{
std::string hexStr = src.substr(i + 1, 2);
hex = strtol(hexStr.c_str(), 0, 16);
//字母和数字[0-9a-zA-Z]、一些特殊符号[$-_.+!*'(),] 、以及某些保留字[$&+,/:;=?@]
//可以不经过编码直接用于URL
if (!((hex >= 48 && hex <= 57) || //0-9
(hex >=97 && hex <= 122) || //a-z
(hex >=65 && hex <= 90) || //A-Z
//一些特殊符号及保留字[$-_.+!*'(),] [$&+,/:;=?@]
hex == 0x21 || hex == 0x24 || hex == 0x26 || hex == 0x27 || hex == 0x28 || hex == 0x29
|| hex == 0x2a || hex == 0x2b|| hex == 0x2c || hex == 0x2d || hex == 0x2e || hex == 0x2f
|| hex == 0x3A || hex == 0x3B|| hex == 0x3D || hex == 0x3f || hex == 0x40 || hex == 0x5f
))
{
dst += char(hex);
i += 2;
}
else
dst += '%';
}
else
dst += '%';
}
break;
default:
dst += src[i];
break;
}
}
return true;
}