forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.h
369 lines (326 loc) · 13.2 KB
/
http.h
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
// Copyright (c) 2015-2022 Vector 35 Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
#pragma once
#include <vector>
#include <string>
#include <optional>
#include <unordered_map>
#include <functional>
#include <utility>
#include <stdint.h>
#ifdef BINARYNINJACORE_LIBRARY
#include "downloadprovider.h"
#include "json/json.h"
#else
#include "binaryninjaapi.h"
#endif
#ifdef BINARYNINJACORE_LIBRARY
namespace BinaryNinjaCore::Http
#else
namespace BinaryNinja::Http
#endif
{
#ifdef BINARYNINJACORE_LIBRARY
#define _STD_STRING string
#define _STD_VECTOR vector
#define _STD_UNORDERED_MAP unordered_map
#else
#define _STD_STRING std::string
#define _STD_VECTOR std::vector
#define _STD_UNORDERED_MAP std::unordered_map
#endif
enum ResponseCode
{
Continue = 100,
SwitchingProtocols = 101,
OK = 200,
Created = 201,
Accepted = 202,
NonAuthoritativeInformation = 203,
NoContent = 204,
ResetContent = 205,
PartialContent = 206,
MultipleChoices = 300,
MovedPermanently = 301,
Found = 302,
SeeOther = 303,
NotModified = 304,
UseProxy = 305,
TemporaryRedirect = 307,
BadRequest = 400,
Unauthorized = 401,
PaymentRequired = 402,
Forbidden = 403,
NotFound = 404,
MethodNotAllowed = 405,
NotAcceptable = 406,
ProxyAuthenticationRequired = 407,
RequestTimeout = 408,
Conflict = 409,
Gone = 410,
LengthRequired = 411,
PreconditionFailed = 412,
RequestEntityTooLarge = 413,
RequestURITooLong = 414,
UnsupportedMediaType = 415,
RequestedRangeNotSatisfiable = 416,
ExpectationFailed = 417,
ImATeapot = 418,
InternalServerError = 500,
NotImplemented = 501,
BadGateway = 502,
ServiceUnavailable = 503,
GatewayTimeout = 504,
HTTPVersionNotSupported = 505,
};
/*!
Basic HTTP response structure
*/
struct Response
{
DownloadInstance::Response response;
_STD_VECTOR<uint8_t> body;
_STD_STRING error;
/*!
Get response body as uint8_t vector
\return Response body bytes
*/
_STD_VECTOR<uint8_t> GetRaw() const noexcept;
/*!
Get response body as text string
\return Response body string
*/
_STD_STRING GetString() const noexcept;
/*!
Get response body as a json value
\return Response body json
\throws runtime_error On JSON parse error
*/
Json::Value GetJson() const;
/*!
Get response body as a json value without throwing
\param value Output json value
\return True if successful
*/
bool GetJson(Json::Value& value) const noexcept;
};
/*!
Structure for multipart form fields
*/
struct MultipartField
{
_STD_STRING name;
_STD_VECTOR<uint8_t> content;
std::optional<_STD_STRING> filename;
/*!
Construct a Multipart Field structure with a UTF-8 string encoded body
\param name Name of the field to be sent in a POST request
\param content Contents of the field
*/
MultipartField(_STD_STRING name, const _STD_STRING& content) : name(std::move(name)), content({}), filename({})
{
std::copy(content.begin(), content.end(), std::back_inserter(this->content));
}
/*!
Construct a Multipart Field structure for a file with a UTF-8 string encoded body
\param name Name of the field to be sent in a POST request
\param content Contents of the field
\param filename Filename associated with the contents
*/
MultipartField(_STD_STRING name, const _STD_STRING& content, _STD_STRING filename) :
name(std::move(name)), content({}), filename(std::move(filename))
{
std::copy(content.begin(), content.end(), std::back_inserter(this->content));
}
/*!
Construct a Multipart Field structure with a binary blob body
\param name Name of the field to be sent in a POST request
\param content Contents of the field
*/
MultipartField(_STD_STRING name, _STD_VECTOR<uint8_t> content) :
name(std::move(name)), content(std::move(content)), filename({})
{}
/*!
Construct a Multipart Field structure for a file with a binary blob body
\param name Name of the field to be sent in a POST request
\param content Contents of the field
\param filename Filename associated with the contents
*/
MultipartField(_STD_STRING name, _STD_VECTOR<uint8_t> content, _STD_STRING filename) :
name(std::move(name)), content(std::move(content)), filename(std::move(filename))
{}
};
/*!
Structure containing HTTP metadata for requests
*/
struct Request
{
_STD_STRING m_method;
_STD_STRING m_url;
_STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> m_headers;
_STD_VECTOR<uint8_t> m_body;
std::function<bool(size_t, size_t)> m_downloadProgress;
std::function<bool(size_t, size_t)> m_uploadProgress;
/*!
Construct an arbitrary HTTP request with an empty body
\param method Request method eg GET
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
*/
Request(_STD_STRING method, _STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers = {},
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params = {},
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
/*!
Construct an arbitrary HTTP request with a binary data body
\param method Request method eg GET
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param body Content body (binary data)
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
*/
Request(_STD_STRING method, _STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers,
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params, _STD_VECTOR<uint8_t> body,
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
/*!
Construct an arbitrary HTTP request with url encoded form fields as the body
\param method Request method eg GET
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param formFields HTTP form fields, keys/values (both must be strings)
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
*/
Request(_STD_STRING method, _STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers,
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params,
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> formFields,
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
/*!
Construct an arbitrary HTTP request with Multipart encoded form fields as the body
\param method Request method eg GET
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param formFields HTTP form fields, keys/values (values can be arbitrary data)
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
*/
Request(_STD_STRING method, _STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers,
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params, _STD_VECTOR<MultipartField> formFields,
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
/*!
Construct an HTTP GET request
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
\return Request structure with specified fields
*/
static Request Get(_STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers = {},
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params = {},
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
/*!
Construct an HTTP POST request with a binary data body
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param body Request body data
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
\return Request structure with specified fields
*/
static Request Post(_STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers = {},
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params = {}, _STD_VECTOR<uint8_t> body = {},
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
/*!
Construct an HTTP POST request with url encoded form fields as the body
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param formFields HTTP form fields, keys/values (both must be strings)
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
\return Request structure with specified fields
*/
static Request Post(_STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers,
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params,
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> formFields,
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
/*!
Construct an HTTP POST request with Multipart encoded form fields as the body
\param url Target URL eg https://binary.ninja
\param headers Header keys/values
\param params Query parameters, keys/values
\param formFields HTTP form fields, keys/values (values can be arbitrary data)
\param downloadProgress Function to call for download progress updates
\param uploadProgress Function to call for upload progress updates
\return Request structure with specified fields
*/
static Request Post(_STD_STRING url, _STD_UNORDERED_MAP<_STD_STRING, _STD_STRING> headers,
_STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>> params, _STD_VECTOR<MultipartField> formFields,
std::function<bool(size_t, size_t)> downloadProgress = {},
std::function<bool(size_t, size_t)> uploadProgress = {});
};
/*!
Convert a string to a URLEncoded form-field safe form
\param str Input string
\return URLEncoded string
*/
_STD_STRING UrlEncode(const _STD_STRING& str);
/*!
Convert a list of key/value pair strings into a URLEncoded form body
\param fields Input key/value pairs
\return URLEncoded form body
*/
_STD_STRING UrlEncode(const _STD_VECTOR<std::pair<_STD_STRING, _STD_STRING>>& fields);
/*!
Convert a list of form fields (potentially containing binary data) into a multipart encoded
form body
\param fields Input fields
\param boundary Output boundary between fields in the body (for Content-Type header)
\return Multipart encoded form body
*/
_STD_VECTOR<uint8_t> MultipartEncode(const _STD_VECTOR<MultipartField>& fields, _STD_STRING& boundary);
/*!
Perform an HTTP request as specified by a Request, storing results in a Response
\param instance DownloadInstance instance
\param request Input Request structure with fields
\param response Output Response structure with body
\return Zero or greater on success
*/
int Perform(const Ref<DownloadInstance>& instance, const Request& request, Response& response);
#undef _STD_VECTOR
#undef _STD_SET
#undef _STD_UNORDERED_MAP
#undef _STD_MAP
} // namespace BinaryNinjaCore::Http