aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/mptrack/HTTP.h
blob: 91ae1afec41a6c587315b616d99f8d6003084e07 (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
/*
 * HTTP.h
 * ------
 * Purpose: Simple HTTP client interface.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#pragma once

#include "openmpt/all/BuildSettings.hpp"

#include <functional>
#include <iosfwd>
#include <optional>


OPENMPT_NAMESPACE_BEGIN


struct URI
{
	mpt::ustring scheme;
	mpt::ustring username;
	mpt::ustring password;
	mpt::ustring host;
	mpt::ustring port;
	mpt::ustring path;
	mpt::ustring query;
	mpt::ustring fragment;
};

class bad_uri
	: public std::runtime_error
{
public:
	bad_uri(const std::string &msg)
		: std::runtime_error(msg)
	{
	}
};

URI ParseURI(mpt::ustring str);


namespace HTTP
{


class exception
	: public std::runtime_error
{
private:
	mpt::ustring message;
public:
	exception(const mpt::ustring &m);
	mpt::ustring GetMessage() const;
};


class status_exception
	: public std::runtime_error
{
public:
	status_exception(uint64 status)
		: std::runtime_error(MPT_AFORMAT("HTTP status {}")(status))
	{
		return;
	}
};


class Abort
	: public exception
{
public:
	Abort() :
		exception(U_("Operation aborted."))
	{
		return;
	}
};


struct NativeHandle;


class Handle
{
private:
	std::unique_ptr<NativeHandle> handle;
public:
	Handle();
	Handle(const Handle &) = delete;
	Handle & operator=(const Handle &) = delete;
	explicit operator bool() const;
	bool operator!() const;
	Handle(NativeHandle h);
	Handle & operator=(NativeHandle h);
	operator NativeHandle ();
	~Handle();
};


class InternetSession
{
private:
	Handle internet;
public:
	InternetSession(mpt::ustring userAgent);
	operator NativeHandle ();
	template <typename TRequest>
	auto operator()(const TRequest &request) -> decltype(request(*this))
	{
		return request(*this);
	}
	template <typename TRequest>
	auto Request(const TRequest &request) -> decltype(request(*this))
	{
		return request(*this);
	}
};


enum class Protocol
{
	HTTP,
	HTTPS,
};


enum class Port : uint16
{
	Default = 0,
	HTTP    = 80,
	HTTPS   = 443,
};


enum class Method
{
	Get,
	Head,
	Post,
	Put,
	Delete,
	Trace,
	Options,
	Connect,
	Patch,
};


using Query = std::vector<std::pair<mpt::ustring, mpt::ustring>>;


namespace MimeType {
	inline std::string Text() { return "text/plain"; }
	inline std::string JSON() { return "application/json"; }
	inline std::string Binary() { return "application/octet-stream"; }
}


using AcceptMimeTypes = std::vector<std::string>;
namespace MimeTypes {
	inline AcceptMimeTypes Text() { return {"text/*"}; }
	inline AcceptMimeTypes JSON() { return {MimeType::JSON()}; }
	inline AcceptMimeTypes Binary() { return {MimeType::Binary()}; }
}


using Headers = std::vector<std::pair<std::string, std::string>>;


enum Flags
{
	None         = 0x00u,
	NoCache      = 0x01u,
	AutoRedirect = 0x02u,
};


struct Result
{
	uint64 Status = 0;
	std::optional<uint64> ContentLength;
	std::vector<std::byte> Data;

	void CheckStatus(uint64 expected) const
	{
		if(Status != expected)
		{
			throw status_exception(Status);
		}
	}

};


enum class Progress
{
	Start = 1,
	ConnectionEstablished = 2,
	RequestOpened = 3,
	RequestSent = 4,
	ResponseReceived = 5,
	TransferBegin = 6,
	TransferRunning = 7,
	TransferDone = 8,
};


struct Request
{

	Protocol protocol = Protocol::HTTPS;
	mpt::ustring host;
	Port port = Port::Default;
	mpt::ustring username;
	mpt::ustring password;
	Method method = Method::Get;
	mpt::ustring path = U_("/");
	Query query;
	mpt::ustring referrer;
	AcceptMimeTypes acceptMimeTypes;
	Flags flags = None;
	Headers headers;
	std::string dataMimeType;
	mpt::const_byte_span data;

	std::ostream *outputStream = nullptr;
	std::function<void(Progress, uint64, std::optional<uint64>)> progressCallback = nullptr;

	Request &SetURI(const URI &uri);

#if defined(MPT_BUILD_RETRO)
	Request &InsecureTLSDowngradeWindowsXP();
#endif // MPT_BUILD_RETRO

	Result operator()(InternetSession &internet) const;

private:

	void progress(Progress progress, uint64 transferred, std::optional<uint64> expectedSize) const;

};


Result SimpleGet(InternetSession &internet, Protocol protocol, const mpt::ustring &host, const mpt::ustring &path);


} // namespace HTTP


OPENMPT_NAMESPACE_END