summaryrefslogtreecommitdiffstats
path: root/src/HTTP/UrlClient.cpp
blob: 9346882f13b26e82c3dbce605b1cf2146608bb93 (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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675

// UrlClient.cpp

// Implements the cUrlClient class for high-level URL interaction

#include "Globals.h"
#include "UrlClient.h"
#include "UrlParser.h"
#include "HTTPMessageParser.h"
#include "../PolarSSL++/X509Cert.h"
#include "../PolarSSL++/CryptoKey.h"





// fwd:
class cSchemeHandler;
typedef SharedPtr<cSchemeHandler> cSchemeHandlerPtr;





class cUrlClientRequest:
	public cNetwork::cConnectCallbacks,
	public cTCPLink::cCallbacks
{
	friend class cHttpSchemeHandler;

public:
	static std::pair<bool, AString> Request(
		const AString & a_Method,
		const AString & a_URL,
		cUrlClient::cCallbacks & a_Callbacks,
		AStringMap && a_Headers,
		const AString & a_Body,
		AStringMap && a_Options
	)
	{
		// Create a new instance of cUrlClientRequest, wrapped in a SharedPtr, so that it has a controlled lifetime.
		// Cannot use std::make_shared, because the constructor is not public
		SharedPtr<cUrlClientRequest> ptr (new cUrlClientRequest(
			a_Method, a_URL, a_Callbacks, std::move(a_Headers), a_Body, std::move(a_Options)
		));
		return ptr->DoRequest(ptr);
	}


	/** Calls the error callback with the specified message, if it exists, and terminates the request. */
	void CallErrorCallback(const AString & a_ErrorMessage)
	{
		// Call the error callback:
		m_Callbacks.OnError(a_ErrorMessage);

		// Terminate the request's TCP link:
		auto link = m_Link;
		if (link != nullptr)
		{
			link->Close();
		}
		m_Self.reset();
	}


	cUrlClient::cCallbacks & GetCallbacks() { return m_Callbacks; }

	void RedirectTo(const AString & a_RedirectUrl);

	bool ShouldAllowRedirects() const;

	cX509CertPtr GetOwnCert() const
	{
		auto itr = m_Options.find("OwnCert");
		if (itr == m_Options.end())
		{
			return nullptr;
		}
		cX509CertPtr cert;
		if (!cert->Parse(itr->second.data(), itr->second.size()))
		{
			LOGD("OwnCert failed to parse");
			return nullptr;
		}
		return cert;
	}

	cCryptoKeyPtr GetOwnPrivKey() const
	{
		auto itr = m_Options.find("OwnPrivKey");
		if (itr == m_Options.end())
		{
			return nullptr;
		}
		cCryptoKeyPtr key;
		auto passItr = m_Options.find("OwnPrivKeyPassword");
		auto pass = (passItr == m_Options.end()) ? AString() : passItr->second;
		if (!key->ParsePrivate(itr->second.data(), itr->second.size(), pass))
		{
			return nullptr;
		}
		return key;
	}

protected:

	/** Method to be used for the request */
	AString m_Method;

	/** URL that will be requested. */
	AString m_Url;

	/** Individual components of the URL that will be requested. */
	AString m_UrlScheme, m_UrlUsername, m_UrlPassword, m_UrlHost, m_UrlPath, m_UrlQuery, m_UrlFragment;
	UInt16 m_UrlPort;

	/** Callbacks that report progress and results of the request. */
	cUrlClient::cCallbacks & m_Callbacks;

	/** Extra headers to be sent with the request (besides the normal ones). */
	AStringMap m_Headers;

	/** Body to be sent with the request, if any. */
	AString m_Body;

	/** Extra options to be used for the request. */
	AStringMap m_Options;

	/** SharedPtr to self, so that this object can keep itself alive for as long as it needs,
	and pass self as callbacks to cNetwork functions. */
	SharedPtr<cUrlClientRequest> m_Self;

	/** The handler that "talks" the protocol specified in m_UrlScheme, handles all the sending and receiving. */
	SharedPtr<cSchemeHandler> m_SchemeHandler;

	/** The link handling the request. */
	cTCPLinkPtr m_Link;

	/** The number of redirect attempts that will still be followed.
	If the response specifies a redirect and this is nonzero, the redirect is followed.
	If the response specifies a redirect and this is zero, a redirect loop is reported as an error. */
	int m_NumRemainingRedirects;


	cUrlClientRequest(
		const AString & a_Method,
		const AString & a_Url,
		cUrlClient::cCallbacks & a_Callbacks,
		AStringMap && a_Headers,
		const AString & a_Body,
		AStringMap && a_Options
	):
		m_Method(a_Method),
		m_Url(a_Url),
		m_Callbacks(a_Callbacks),
		m_Headers(std::move(a_Headers)),
		m_Body(a_Body),
		m_Options(std::move(a_Options))
	{
		m_NumRemainingRedirects = GetStringMapInteger(m_Options, "MaxRedirects", 30);
	}


	std::pair<bool, AString> DoRequest(SharedPtr<cUrlClientRequest> a_Self);


	// cNetwork::cConnectCallbacks override: TCP link connected:
	virtual void OnConnected(cTCPLink & a_Link) override;

	// cNetwork::cConnectCallbacks override: An error has occurred:
	virtual void OnError(int a_ErrorCode, const AString & a_ErrorMsg) override
	{
		m_Callbacks.OnError(Printf("Network error %d (%s)", a_ErrorCode, a_ErrorMsg.c_str()));
		m_Self.reset();
	}


	// cTCPLink::cCallbacks override: TCP link created
	virtual void OnLinkCreated(cTCPLinkPtr a_Link) override
	{
		m_Link = a_Link;
	}


	// cTCPLink::cCallbacks override: TLS handshake completed on the link:
	virtual void OnTlsHandshakeCompleted(void) override;

	/** Called when there's data incoming from the remote peer. */
	virtual void OnReceivedData(const char * a_Data, size_t a_Length) override;


	/** Called when the remote end closes the connection.
	The link is still available for connection information query (IP / port).
	Sending data on the link is not an error, but the data won't be delivered. */
	virtual void OnRemoteClosed(void) override;
};





/** Represents a base class for an object that "talks" a specified URL protocol, such as HTTP or FTP.
Also provides a static factory method for creating an instance based on the scheme.
A descendant of this class is created for each request and handles all of the request's aspects,
from right after connecting to the TCP link till the link is closed.
For an example of a specific handler, see the cHttpSchemeHandler class. */
class cSchemeHandler abstract
{
public:
	cSchemeHandler(cUrlClientRequest & a_ParentRequest):
		m_ParentRequest(a_ParentRequest)
	{
	}

	// Force a virtual destructor in all descendants
	virtual ~cSchemeHandler() {}

	/** Creates and returns a new handler for the specified scheme.
	a_ParentRequest is the request which is to be handled by the handler. */
	static cSchemeHandlerPtr Create(const AString & a_Scheme, cUrlClientRequest & a_ParentRequest);

	/** Called when the link gets established. */
	virtual void OnConnected(cTCPLink & a_Link) = 0;

	/** Called when there's data incoming from the remote peer. */
	virtual void OnReceivedData(const char * a_Data, size_t a_Length) = 0;

	/** Called when the TLS handshake has completed on the underlying link. */
	virtual void OnTlsHandshakeCompleted(void) = 0;

	/** Called when the remote end closes the connection.
	The link is still available for connection information query (IP / port).
	Sending data on the link is not an error, but the data won't be delivered. */
	virtual void OnRemoteClosed(void) = 0;

protected:
	cUrlClientRequest & m_ParentRequest;
};





/** cSchemeHandler descendant that handles HTTP (and HTTPS) requests. */
class cHttpSchemeHandler:
	public cSchemeHandler,
	protected cHTTPMessageParser::cCallbacks
{
	typedef cSchemeHandler Super;

public:
	cHttpSchemeHandler(cUrlClientRequest & a_ParentRequest, bool a_IsTls):
		Super(a_ParentRequest),
		m_Parser(*this),
		m_IsTls(a_IsTls),
		m_IsRedirect(false)
	{
	}


	virtual void OnConnected(cTCPLink & a_Link) override
	{
		m_Link = &a_Link;
		if (m_IsTls)
		{
			m_Link->StartTLSClient(m_ParentRequest.GetOwnCert(), m_ParentRequest.GetOwnPrivKey());
		}
		else
		{
			SendRequest();
		}
	}


	/** Sends the HTTP request over the link.
	Common code for both HTTP and HTTPS. */
	void SendRequest()
	{
		// Compose the request line:
		auto requestLine = m_ParentRequest.m_UrlPath;
		if (requestLine.empty())
		{
			requestLine = "/";
		}
		if (!m_ParentRequest.m_UrlQuery.empty())
		{
			requestLine.push_back('?');
			requestLine.append(m_ParentRequest.m_UrlQuery);
		}
		m_Link->Send(Printf("%s %s HTTP/1.1\r\n", m_ParentRequest.m_Method.c_str(), requestLine.c_str()));

		// Send the headers:
		m_Link->Send(Printf("Host: %s\r\n", m_ParentRequest.m_UrlHost.c_str()));
		m_Link->Send(Printf("Content-Length: %u\r\n", static_cast<unsigned>(m_ParentRequest.m_Body.size())));
		for (auto itr = m_ParentRequest.m_Headers.cbegin(), end = m_ParentRequest.m_Headers.cend(); itr != end; ++itr)
		{
			m_Link->Send(Printf("%s: %s\r\n", itr->first.c_str(), itr->second.c_str()));
		}  // for itr - m_Headers[]
		m_Link->Send("\r\n", 2);

		// Send the body:
		m_Link->Send(m_ParentRequest.m_Body);

		// Notify the callbacks that the request has been sent:
		m_ParentRequest.GetCallbacks().OnRequestSent();
	}


	virtual void OnReceivedData(const char * a_Data, size_t a_Length) override
	{
		auto res = m_Parser.Parse(a_Data, a_Length);
		if (res == AString::npos)
		{
			m_ParentRequest.CallErrorCallback("Failed to parse HTTP response");
			return;
		}
	}


	virtual void OnTlsHandshakeCompleted(void) override
	{
		SendRequest();
	}


	virtual void OnRemoteClosed(void) override
	{
		m_Link = nullptr;
	}


	// cHTTPResponseParser::cCallbacks overrides:
	virtual void OnError(const AString & a_ErrorDescription) override
	{
		m_ParentRequest.CallErrorCallback(a_ErrorDescription);
		m_Link = nullptr;
	}


	virtual void OnFirstLine(const AString & a_FirstLine) override
	{
		// Find the first space, parse the result code between it and the second space:
		auto idxFirstSpace = a_FirstLine.find(' ');
		if (idxFirstSpace == AString::npos)
		{
			m_ParentRequest.CallErrorCallback(Printf("Failed to parse HTTP status line \"%s\", no space delimiter.", a_FirstLine.c_str()));
			return;
		}
		auto idxSecondSpace = a_FirstLine.find(' ', idxFirstSpace + 1);
		if (idxSecondSpace == AString::npos)
		{
			m_ParentRequest.CallErrorCallback(Printf("Failed to parse HTTP status line \"%s\", missing second space delimiter.", a_FirstLine.c_str()));
			return;
		}
		int resultCode;
		auto resultCodeStr = a_FirstLine.substr(idxFirstSpace + 1, idxSecondSpace - idxFirstSpace - 1);
		if (!StringToInteger(resultCodeStr, resultCode))
		{
			m_ParentRequest.CallErrorCallback(Printf("Failed to parse HTTP result code from response \"%s\"", resultCodeStr.c_str()));
			return;
		}

		// Check for redirects, follow if allowed by the options:
		switch (resultCode)
		{
			case cUrlClient::HTTP_STATUS_MULTIPLE_CHOICES:
			case cUrlClient::HTTP_STATUS_MOVED_PERMANENTLY:
			case cUrlClient::HTTP_STATUS_FOUND:
			case cUrlClient::HTTP_STATUS_SEE_OTHER:
			case cUrlClient::HTTP_STATUS_TEMPORARY_REDIRECT:
			{
				m_IsRedirect = true;
				return;
			}
		}
		m_ParentRequest.GetCallbacks().OnStatusLine(a_FirstLine.substr(1, idxFirstSpace), resultCode, a_FirstLine.substr(idxSecondSpace + 1));
	}


	virtual void OnHeaderLine(const AString & a_Key, const AString & a_Value) override
	{
		if (m_IsRedirect)
		{
			if (a_Key == "Location")
			{
				m_RedirectLocation = a_Value;
			}
		}
		else
		{
			m_ParentRequest.GetCallbacks().OnHeader(a_Key, a_Value);
		}
	}


	/** Called when all the headers have been parsed. */
	virtual void OnHeadersFinished(void) override
	{
		if (!m_IsRedirect)
		{
			m_ParentRequest.GetCallbacks().OnHeadersFinished();
		}
	}


	/** Called for each chunk of the incoming body data. */
	virtual void OnBodyData(const void * a_Data, size_t a_Size) override
	{
		if (!m_IsRedirect)
		{
			m_ParentRequest.GetCallbacks().OnBodyData(a_Data, a_Size);
		}
	}


	/** Called when the entire body has been reported by OnBodyData(). */
	virtual void OnBodyFinished(void) override
	{
		if (m_IsRedirect)
		{
			if (m_RedirectLocation.empty())
			{
				m_ParentRequest.CallErrorCallback("Invalid redirect, there's no location to redirect to");
			}
			else
			{
				m_ParentRequest.RedirectTo(m_RedirectLocation);
			}
		}
		else
		{
			m_ParentRequest.GetCallbacks().OnBodyFinished();
		}
	}

protected:

	/** The network link. */
	cTCPLink * m_Link;

	/** Parser of the HTTP response message. */
	cHTTPMessageParser m_Parser;

	/** If true, the TLS should be started on the link before sending the request (used for https). */
	bool m_IsTls;

	/** Set to true if the first line contains a redirecting HTTP status code and the options specify to follow redirects.
	If true, and the parent request allows redirects, neither headers not the body contents are reported through the callbacks,
	and after the entire request is parsed, the redirect is attempted. */
	bool m_IsRedirect;

	/** The Location where the request should be redirected.
	Only used when m_IsRedirect is true. */
	AString m_RedirectLocation;
};





////////////////////////////////////////////////////////////////////////////////
// cSchemeHandler:

cSchemeHandlerPtr cSchemeHandler::Create(const AString & a_Scheme, cUrlClientRequest & a_ParentRequest)
{
	auto lowerScheme = StrToLower(a_Scheme);
	if (lowerScheme == "http")
	{
		return std::make_shared<cHttpSchemeHandler>(a_ParentRequest, false);
	}
	else if (lowerScheme == "https")
	{
		return std::make_shared<cHttpSchemeHandler>(a_ParentRequest, true);
	}

	return nullptr;
}





////////////////////////////////////////////////////////////////////////////////
// cUrlClientRequest:

void cUrlClientRequest::RedirectTo(const AString & a_RedirectUrl)
{
	// Check that redirection is allowed:
	m_Callbacks.OnRedirecting(a_RedirectUrl);
	if (!ShouldAllowRedirects())
	{
		CallErrorCallback(Printf("Redirect to \"%s\" not allowed", a_RedirectUrl.c_str()));
		return;
	}

	// Do the actual redirect:
	m_Link->Close();
	m_Url = a_RedirectUrl;
	m_NumRemainingRedirects = m_NumRemainingRedirects - 1;
	auto res = DoRequest(m_Self);
	if (!res.first)
	{
		m_Callbacks.OnError(Printf("Redirection failed: %s", res.second.c_str()));
		return;
	}
}





bool cUrlClientRequest::ShouldAllowRedirects() const
{
	return (m_NumRemainingRedirects > 0);
}





void cUrlClientRequest::OnConnected(cTCPLink & a_Link)
{
	m_Callbacks.OnConnected(a_Link);
	m_SchemeHandler->OnConnected(a_Link);
}





void cUrlClientRequest::OnTlsHandshakeCompleted(void)
{
	// Notify the scheme handler and the callbacks:
	m_SchemeHandler->OnTlsHandshakeCompleted();
	m_Callbacks.OnTlsHandshakeCompleted();
}





void cUrlClientRequest::OnReceivedData(const char * a_Data, size_t a_Length)
{
	auto handler = m_SchemeHandler;
	if (handler != nullptr)
	{
		handler->OnReceivedData(a_Data, a_Length);
	}
}





void cUrlClientRequest::OnRemoteClosed()
{
	// Notify the callback:
	auto handler = m_SchemeHandler;
	if (handler != nullptr)
	{
		handler->OnRemoteClosed();
	}

	// Let ourselves be deleted
	m_Self.reset();
}





std::pair<bool, AString> cUrlClientRequest::DoRequest(SharedPtr<cUrlClientRequest> a_Self)
{
	// We need a shared pointer to self, care must be taken not to pass any other ptr:
	ASSERT(a_Self.get() == this);

	m_Self = a_Self;

	// Parse the URL:
	auto res = cUrlParser::Parse(m_Url, m_UrlScheme, m_UrlUsername, m_UrlPassword, m_UrlHost, m_UrlPort, m_UrlPath, m_UrlQuery, m_UrlFragment);
	if (!res.first)
	{
		return res;
	}

	// Get a handler that will work with the specified scheme:
	m_SchemeHandler = cSchemeHandler::Create(m_UrlScheme, *this);
	if (m_SchemeHandler == nullptr)
	{
		return std::make_pair(false, Printf("Unknown Url scheme: %s", m_UrlScheme.c_str()));
	}

	if (!cNetwork::Connect(m_UrlHost, m_UrlPort, m_Self, m_Self))
	{
		return std::make_pair(false, "Network connection failed");
	}
	return std::make_pair(true, AString());
}





////////////////////////////////////////////////////////////////////////////////
// cUrlClient:

std::pair<bool, AString> cUrlClient::Request(
	const AString & a_Method,
	const AString & a_URL,
	cCallbacks & a_Callbacks,
	AStringMap && a_Headers,
	AString && a_Body,
	AStringMap && a_Options
)
{
	return cUrlClientRequest::Request(
		a_Method, a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options)
	);
}





std::pair<bool, AString> cUrlClient::Get(
	const AString & a_URL,
	cCallbacks & a_Callbacks,
	AStringMap a_Headers,
	AString a_Body,
	AStringMap a_Options
)
{
	return cUrlClientRequest::Request(
		"GET", a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options)
	);
}





std::pair<bool, AString> cUrlClient::Post(
	const AString & a_URL,
	cCallbacks & a_Callbacks,
	AStringMap && a_Headers,
	AString && a_Body,
	AStringMap && a_Options
)
{
	return cUrlClientRequest::Request(
		"POST", a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options)
	);
}





std::pair<bool, AString> cUrlClient::Put(
	const AString & a_URL,
	cCallbacks & a_Callbacks,
	AStringMap && a_Headers,
	AString && a_Body,
	AStringMap && a_Options
)
{
	return cUrlClientRequest::Request(
		"PUT", a_URL, a_Callbacks, std::move(a_Headers), std::move(a_Body), std::move(a_Options)
	);
}