aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/poco/Net/src/HTTPRequest.cpp
blob: 52b3cf2f714df0caf7690f64fc989ab90e61c2a2 (plain) (blame)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//
// HTTPRequest.cpp
//
// Library: Net
// Package: HTTP
// Module:  HTTPRequest
//
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/NetException.h"
#include "Poco/Net/NameValueCollection.h"
#include "Poco/NumberFormatter.h"
#include "Poco/Ascii.h"
#include "Poco/String.h"


using Poco::NumberFormatter;


namespace Poco {
namespace Net {


const std::string HTTPRequest::HTTP_GET            = "GET";
const std::string HTTPRequest::HTTP_HEAD           = "HEAD";
const std::string HTTPRequest::HTTP_PUT            = "PUT";
const std::string HTTPRequest::HTTP_POST           = "POST";
const std::string HTTPRequest::HTTP_OPTIONS        = "OPTIONS";
const std::string HTTPRequest::HTTP_DELETE         = "DELETE";
const std::string HTTPRequest::HTTP_TRACE          = "TRACE";
const std::string HTTPRequest::HTTP_CONNECT        = "CONNECT";
const std::string HTTPRequest::HTTP_PATCH          = "PATCH";
const std::string HTTPRequest::HOST                = "Host";
const std::string HTTPRequest::COOKIE              = "Cookie";
const std::string HTTPRequest::AUTHORIZATION       = "Authorization";
const std::string HTTPRequest::PROXY_AUTHORIZATION = "Proxy-Authorization";
const std::string HTTPRequest::UPGRADE             = "Upgrade";
const std::string HTTPRequest::EXPECT              = "Expect";


HTTPRequest::HTTPRequest():
	_method(HTTP_GET),
	_uri("/")
{
}

	
HTTPRequest::HTTPRequest(const std::string& version):
	HTTPMessage(version),
	_method(HTTP_GET),
	_uri("/")
{
}

	
HTTPRequest::HTTPRequest(const std::string& method, const std::string& uri):
	_method(method),
	_uri(uri)
{
}


HTTPRequest::HTTPRequest(const std::string& method, const std::string& uri, const std::string& version):
	HTTPMessage(version),
	_method(method),
	_uri(uri)
{
}


HTTPRequest::~HTTPRequest()
{
}


void HTTPRequest::setMethod(const std::string& method)
{
	_method = method;
}


void HTTPRequest::setURI(const std::string& uri)
{
	_uri = uri;
}


void HTTPRequest::setHost(const std::string& host)
{
	set(HOST, host);
}

	
void HTTPRequest::setHost(const std::string& host, Poco::UInt16 port)
{
	std::string value;
	if (host.find(':') != std::string::npos)
	{
		// IPv6 address
		value.append("[");
		value.append(host);
		value.append("]");
	}
	else
	{
		value.append(host);
	}    

	if (port != 80 && port != 443)
	{
		value.append(":");
		NumberFormatter::append(value, port);
	}
	setHost(value);
}

	
const std::string& HTTPRequest::getHost() const
{
	return get(HOST);
}


void HTTPRequest::setCookies(const NameValueCollection& cookies)
{
	std::string cookie;
	cookie.reserve(64);
	for (NameValueCollection::ConstIterator it = cookies.begin(); it != cookies.end(); ++it)
	{
		if (it != cookies.begin())
			cookie.append("; ");
		cookie.append(it->first);
		cookie.append("=");
		cookie.append(it->second);
	}
	add(COOKIE, cookie);
}

	
void HTTPRequest::getCookies(NameValueCollection& cookies) const
{
	NameValueCollection::ConstIterator it = find(COOKIE);
	while (it != end() && Poco::icompare(it->first, COOKIE) == 0)
	{
		splitParameters(it->second.begin(), it->second.end(), cookies);
		++it;
	}
}


bool HTTPRequest::hasCredentials() const
{
	return has(AUTHORIZATION);
}

	
void HTTPRequest::getCredentials(std::string& scheme, std::string& authInfo) const
{
	getCredentials(AUTHORIZATION, scheme, authInfo);
}

	
void HTTPRequest::setCredentials(const std::string& scheme, const std::string& authInfo)
{
	setCredentials(AUTHORIZATION, scheme, authInfo);
}


bool HTTPRequest::hasProxyCredentials() const
{
	return has(PROXY_AUTHORIZATION);
}

	
void HTTPRequest::getProxyCredentials(std::string& scheme, std::string& authInfo) const
{
	getCredentials(PROXY_AUTHORIZATION, scheme, authInfo);
}

	
void HTTPRequest::setProxyCredentials(const std::string& scheme, const std::string& authInfo)
{
	setCredentials(PROXY_AUTHORIZATION, scheme, authInfo);
}


void HTTPRequest::write(std::ostream& ostr) const
{
	ostr << _method << " " << _uri << " " << getVersion() << "\r\n";
	HTTPMessage::write(ostr);
	ostr << "\r\n";
}


void HTTPRequest::read(std::istream& istr)
{
	static const int eof = std::char_traits<char>::eof();

	std::string method;
	std::string uri;
	std::string version;
	method.reserve(16);
	uri.reserve(64);
	version.reserve(16);
	int ch = istr.get();
	if (istr.bad()) throw NetException("Error reading HTTP request header");
	if (ch == eof) throw NoMessageException();
	while (Poco::Ascii::isSpace(ch)) ch = istr.get();
	if (ch == eof) throw MessageException("No HTTP request header");
	while (!Poco::Ascii::isSpace(ch) && ch != eof && method.length() < MAX_METHOD_LENGTH) { method += (char) ch; ch = istr.get(); }
	if (!Poco::Ascii::isSpace(ch)) throw MessageException("HTTP request method invalid or too long");
	while (Poco::Ascii::isSpace(ch)) ch = istr.get();
	while (!Poco::Ascii::isSpace(ch) && ch != eof && uri.length() < MAX_URI_LENGTH) { uri += (char) ch; ch = istr.get(); }
	if (!Poco::Ascii::isSpace(ch)) throw MessageException("HTTP request URI invalid or too long");
	while (Poco::Ascii::isSpace(ch)) ch = istr.get();
	while (!Poco::Ascii::isSpace(ch) && ch != eof && version.length() < MAX_VERSION_LENGTH) { version += (char) ch; ch = istr.get(); }
	if (!Poco::Ascii::isSpace(ch)) throw MessageException("Invalid HTTP version string");
	while (ch != '\n' && ch != eof) { ch = istr.get(); }
	HTTPMessage::read(istr);
	ch = istr.get();
	while (ch != '\n' && ch != eof) { ch = istr.get(); }
	setMethod(method);
	setURI(uri);
	setVersion(version);
}


void HTTPRequest::getCredentials(const std::string& header, std::string& scheme, std::string& authInfo) const
{
	scheme.clear();
	authInfo.clear();
	if (has(header))
	{
		const std::string& auth = get(header);
		std::string::const_iterator it  = auth.begin();
		std::string::const_iterator end = auth.end();
		while (it != end && Poco::Ascii::isSpace(*it)) ++it;
		while (it != end && !Poco::Ascii::isSpace(*it)) scheme += *it++;
		while (it != end && Poco::Ascii::isSpace(*it)) ++it;
		while (it != end) authInfo += *it++;
	}
	else throw NotAuthenticatedException();
}

	
void HTTPRequest::setCredentials(const std::string& header, const std::string& scheme, const std::string& authInfo)
{
	std::string auth(scheme);
	auth.append(" ");
	auth.append(authInfo);
	set(header, auth);
}


bool HTTPRequest::getExpectContinue() const
{
	const std::string& expect = get(EXPECT, EMPTY);
	return !expect.empty() && icompare(expect, "100-continue") == 0;
}


void HTTPRequest::setExpectContinue(bool expectContinue)
{
	if (expectContinue)
		set(EXPECT, "100-continue");
	else
		erase(EXPECT);
}


} } // namespace Poco::Net