UrlHelper.cpp
4.48 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
UrlHelper.cpp
Copyright (C) 2002-2004 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
*/
#include "UrlHelper.h"
#include "Tracer.h"
#include "stdHelpers.h"
#include <windows.h>
#include <sstream>
#include <iostream>
bool RemoveProtocolFromUrl(std::string const& url, std::string& protocol, std::string& rest) {
TraceFunc("RemoveProtocolFromUrl");
Trace(std::string("url='")+url+"'");
std::string::size_type pos_colon = url.find(":");
if (pos_colon == std::string::npos) {
rest = url;
return false;
}
if (url.size() < pos_colon + 2) {
rest = url;
return false;
}
if (url[pos_colon+1] != '/' ||
url[pos_colon+2] != '/') {
rest = url;
return false;
}
protocol = url.substr(0,pos_colon);
rest = url.substr(3+pos_colon); // Skipping three characters ( '://' )
return true;
}
void SplitGetReq(std::string get_req, std::string& path, std::map<std::string, std::string>& params) {
TraceFunc("SplitGetReq");
// Remove trailing newlines
if (get_req[get_req.size()-1] == '\x0d' ||
get_req[get_req.size()-1] == '\x0a')
get_req=get_req.substr(0, get_req.size()-1);
if (get_req[get_req.size()-1] == '\x0d' ||
get_req[get_req.size()-1] == '\x0a')
get_req=get_req.substr(0, get_req.size()-1);
// Remove potential Trailing HTTP/1.x
if (get_req.size() > 7) {
if (get_req.substr(get_req.size()-8, 7) == "HTTP/1.") {
get_req=get_req.substr(0, get_req.size()-9);
}
}
std::string::size_type qm = get_req.find("?");
if (qm != std::string::npos) {
std::string url_params = get_req.substr(qm+1);
path = get_req.substr(0, qm);
// Appending a '&' so that there are as many '&' as name-value pairs.
// It makes it easier to split the url for name value pairs, he he he
url_params += "&";
std::string::size_type next_amp = url_params.find("&");
while (next_amp != std::string::npos) {
std::string name_value = url_params.substr(0,next_amp);
url_params = url_params.substr(next_amp+1);
next_amp = url_params.find("&");
std::string::size_type pos_equal = name_value.find("=");
std::string nam = name_value.substr(0,pos_equal);
std::string val = name_value.substr(pos_equal+1);
std::string::size_type pos_plus;
while ( (pos_plus = val.find("+")) != std::string::npos ) {
val.replace(pos_plus, 1, " ");
}
// Replacing %xy notation
std::string::size_type pos_hex = 0;
while ( (pos_hex = val.find("%", pos_hex)) != std::string::npos ) {
std::stringstream h;
h << val.substr(pos_hex+1, 2);
h << std::hex;
int i;
h>>i;
std::stringstream f;
f << static_cast<char>(i);
std::string s;
f >> s;
val.replace(pos_hex, 3, s);
pos_hex ++;
}
params.insert(std::map<std::string,std::string>::value_type(nam, val));
}
}
else {
path = get_req;
}
}
void SplitUrl(std::string const& url, std::string& protocol, std::string& server, std::string& path) {
TraceFunc("SplitUrl");
RemoveProtocolFromUrl(url, protocol, server);
if (protocol == "http") {
std::string::size_type pos_slash = server.find("/");
if (pos_slash != std::string::npos) {
Trace("slash found");
path = server.substr(pos_slash);
server = server.substr(0, pos_slash);
}
else {
Trace("slash not found");
path = "/";
}
}
else if (protocol == "file") {
path = ReplaceInStr(server, "\\", "/");
server = "";
}
else {
std::cerr << "unknown protocol in SplitUrl: '" << protocol << "'" << std::endl;
}
}