Repository navigation
Expand file tree
/
Copy pathExport.cpp
More file actions
598 lines (525 loc) · 16.5 KB
/
Copy pathExport.cpp
File metadata and controls
598 lines (525 loc) · 16.5 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
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
#include <cstdint>
#include <cstddef>
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libSceHttp/src/HttpErrors.hpp"
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <limits>
#include <map>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// No network is emulated: contexts, templates and requests can be created, but any request
// that would touch the network fails with the library's network error.
static std::atomic<int> g_nextHandle{1};
using HttpRequestStatusCallback = void (APS5_VABI *)(int, int, void*);
struct RequestStatusRegistration {
HttpRequestStatusCallback callback;
void* argument;
};
static std::mutex g_statusMutex;
static std::map<int, RequestStatusRegistration> g_statusCallbacks;
struct HttpsCaList {
void* certs;
int certsNum;
};
#ifdef _WIN32
struct HostMutex {
SRWLOCK native = SRWLOCK_INIT;
HostMutex() = default;
HostMutex(const HostMutex&) = delete;
HostMutex& operator=(const HostMutex&) = delete;
void lock() { AcquireSRWLockExclusive(&native); }
void unlock() { ReleaseSRWLockExclusive(&native); }
};
#else
using HostMutex = std::mutex;
#endif
class HostCondition {
public:
HostCondition() = default;
HostCondition(const HostCondition&) = delete;
HostCondition& operator=(const HostCondition&) = delete;
void NotifyAll() {
#ifdef _WIN32
WakeAllConditionVariable(&native);
#else
native.notify_all();
#endif
}
void Wait(std::unique_lock<HostMutex>& lock) {
#ifdef _WIN32
if (!SleepConditionVariableSRW(&native, &lock.mutex()->native, INFINITE, 0))
throw std::runtime_error("libSceHttp: native condition wait failed");
#else
native.wait(lock);
#endif
}
void WaitUntil(std::unique_lock<HostMutex>& lock, std::chrono::steady_clock::time_point deadline) {
#ifdef _WIN32
const auto now = std::chrono::steady_clock::now();
if (deadline <= now) return;
const auto milliseconds = std::chrono::ceil<std::chrono::milliseconds>(deadline - now).count();
if (!SleepConditionVariableSRW(&native, &lock.mutex()->native, static_cast<DWORD>(milliseconds), 0) &&
GetLastError() != ERROR_TIMEOUT)
throw std::runtime_error("libSceHttp: native condition wait failed");
#else
native.wait_until(lock, deadline);
#endif
}
private:
#ifdef _WIN32
CONDITION_VARIABLE native = CONDITION_VARIABLE_INIT;
#else
std::condition_variable native;
#endif
};
struct HttpEpoll {
HostMutex mutex;
HostCondition wake;
bool aborted = false;
bool destroyed = false;
};
static std::mutex g_stateMutex;
static std::unordered_set<int> g_contexts;
static std::unordered_map<HttpEpollHandle, std::shared_ptr<HttpEpoll>> g_epolls;
static std::shared_ptr<HttpEpoll> FindEpoll(HttpEpollHandle eh) {
const auto it = g_epolls.find(eh);
return it == g_epolls.end() ? nullptr : it->second;
}
static void MarkDestroyed(HttpEpoll& epoll) {
std::lock_guard lock(epoll.mutex);
epoll.destroyed = true;
epoll.wake.NotifyAll();
}
extern "C" {
int APS5_VABI sceHttpAbortRequest(int request_id) {
(void)request_id;
return 0;
}
int APS5_VABI sceHttpAddRequestHeader(int id, const char* name, const char* value, uint32_t mode) {
(void)id;
(void)name;
(void)value;
(void)mode;
return 0;
}
int APS5_VABI sceHttpCreateConnection(int tmpl_id, const char* server_name, const char* scheme, uint16_t port, int enable_keep_alive) {
(void)tmpl_id;
(void)server_name;
(void)scheme;
(void)port;
(void)enable_keep_alive;
return g_nextHandle.fetch_add(1, std::memory_order_relaxed);
}
int APS5_VABI sceHttpCreateConnectionWithURL(int tmpl_id, const char* url, int enable_keep_alive) {
(void)tmpl_id;
(void)url;
(void)enable_keep_alive;
return g_nextHandle.fetch_add(1, std::memory_order_relaxed);
}
int APS5_VABI sceHttpCreateEpoll(int http_ctx_id, HttpEpollHandle* eh) {
std::lock_guard lock(g_stateMutex);
if (g_contexts.empty()) return ERROR_BEFORE_INIT;
if (!g_contexts.contains(http_ctx_id)) return ERROR_INVALID_ID;
if (!eh) return ERROR_INVALID_VALUE;
auto epoll = std::make_shared<HttpEpoll>();
*eh = epoll.get();
g_epolls.emplace(epoll.get(), std::move(epoll));
return 0;
}
int APS5_VABI sceHttpCreateRequest(int conn_id, int method, const char* path, uint64_t content_length) {
(void)conn_id;
(void)method;
(void)path;
(void)content_length;
return g_nextHandle.fetch_add(1, std::memory_order_relaxed);
}
int APS5_VABI sceHttpCreateRequestWithURL2(int conn_id, const char* method, const char* url, uint64_t content_length) {
(void)conn_id;
(void)method;
(void)url;
(void)content_length;
return g_nextHandle.fetch_add(1, std::memory_order_relaxed);
}
int APS5_VABI sceHttpCreateTemplate(int http_ctx_id, const char* user_agent, int http_ver, int is_auto_proxy_conf) {
(void)http_ctx_id;
(void)user_agent;
(void)http_ver;
(void)is_auto_proxy_conf;
return g_nextHandle.fetch_add(1, std::memory_order_relaxed);
}
int APS5_VABI sceHttpDeleteConnection(int conn_id) {
(void)conn_id;
return 0;
}
int APS5_VABI sceHttpDeleteRequest(int req_id) {
std::lock_guard lock(g_statusMutex);
g_statusCallbacks.erase(req_id);
return 0;
}
int APS5_VABI sceHttpDeleteTemplate(int tmpl_id) {
(void)tmpl_id;
return 0;
}
int APS5_VABI sceHttpDestroyEpoll(int http_ctx_id, HttpEpollHandle eh) {
std::shared_ptr<HttpEpoll> epoll;
{
std::lock_guard lock(g_stateMutex);
if (g_contexts.empty()) return ERROR_BEFORE_INIT;
if (!g_contexts.contains(http_ctx_id)) return ERROR_INVALID_ID;
if (!eh) return ERROR_INVALID_VALUE;
epoll = FindEpoll(eh);
if (!epoll) return ERROR_INVALID_ID;
g_epolls.erase(eh);
}
MarkDestroyed(*epoll);
return 0;
}
int APS5_VABI sceHttpGetAllResponseHeaders(int request_id, char** header, size_t* header_size) {
(void)request_id;
(void)header;
(void)header_size;
return ERROR_NETWORK;
}
int APS5_VABI sceHttpGetResponseContentLength(int request_id, int* result, uint64_t* content_length) {
(void)request_id;
(void)result;
(void)content_length;
return ERROR_NETWORK;
}
int APS5_VABI sceHttpGetStatusCode(int request_id, int* status_code) {
(void)request_id;
(void)status_code;
return ERROR_NETWORK;
}
int APS5_VABI sceHttpInit_nid_postfix(int memid, int ssl_ctx_id, uint64_t pool_size) {
(void)memid;
(void)ssl_ctx_id;
(void)pool_size;
const int id = g_nextHandle.fetch_add(1, std::memory_order_relaxed);
std::lock_guard lock(g_stateMutex);
g_contexts.insert(id);
return id;
}
int APS5_VABI sceHttpsDisableOption(int id, uint32_t ssl_flags) {
(void)id;
(void)ssl_flags;
return 0;
}
int APS5_VABI sceHttpSendRequest(int request_id, const void* post_data, size_t size) {
(void)request_id;
(void)post_data;
(void)size;
return ERROR_NETWORK;
}
int APS5_VABI sceHttpSetAuthEnabled(int id, int enable) {
(void)id;
(void)enable;
return 0;
}
int APS5_VABI sceHttpSetAuthInfoCallback(int id, HttpAuthInfoCallback callback, void* userArg) {
(void)id;
(void)callback;
(void)userArg;
return 0;
}
int APS5_VABI sceHttpSetCookieEnabled(int id, int enable) {
(void)id;
if (static_cast<uint32_t>(enable) > 1) return ERROR_INVALID_VALUE;
if (enable != 0) NotImplemented_nid_no_patch(__func__);
return 0;
}
int APS5_VABI sceHttpSetAutoRedirect(int id, int enable) {
(void)id;
(void)enable;
return 0;
}
int APS5_VABI sceHttpSetConnectTimeOut(int id, uint32_t usec) {
(void)id;
(void)usec;
return 0;
}
int APS5_VABI sceHttpSetEpoll(int id, HttpEpollHandle eh, void* user_arg) {
(void)id;
(void)eh;
(void)user_arg;
return 0;
}
int APS5_VABI sceHttpSetNonblock(int id, int enable) {
(void)id;
(void)enable;
return 0;
}
int APS5_VABI sceHttpSetRecvTimeOut(int id, uint32_t usec) {
(void)id;
(void)usec;
return 0;
}
int APS5_VABI sceHttpSetRecvBlockSize(int id, uint32_t block_size) {
(void)id;
(void)block_size;
return 0;
}
int APS5_VABI sceHttpSetResponseHeaderMaxSize(int id, uint64_t header_size) {
(void)id;
(void)header_size;
return 0;
}
int APS5_VABI sceHttpSetRequestContentLength(int request_id, uint64_t content_length) {
(void)request_id;
(void)content_length;
return 0;
}
int APS5_VABI sceHttpSetResolveRetry(int id, int32_t retry) {
(void)id;
(void)retry;
return 0;
}
int APS5_VABI sceHttpSetResolveTimeOut(int id, uint32_t usec) {
(void)id;
(void)usec;
return 0;
}
int APS5_VABI sceHttpSetSendTimeOut(int id, uint32_t usec) {
(void)id;
(void)usec;
return 0;
}
int APS5_VABI sceHttpsSetMinSslVersion(int id, uint32_t ssl_version) {
(void)id;
(void)ssl_version;
return 0;
}
int APS5_VABI sceHttpsSetSslCallback(int id, HttpsCallback cbfunc, void* user_arg) {
(void)id;
(void)cbfunc;
(void)user_arg;
return 0;
}
int APS5_VABI sceHttpTerm_nid_postfix(int http_ctx_id) {
std::vector<std::shared_ptr<HttpEpoll>> epolls;
{
std::lock_guard lock(g_stateMutex);
if (g_contexts.empty()) return ERROR_BEFORE_INIT;
if (g_contexts.erase(http_ctx_id) == 0) return ERROR_INVALID_ID;
if (!g_contexts.empty()) return 0;
for (auto& [handle, epoll] : g_epolls) epolls.push_back(std::move(epoll));
g_epolls.clear();
}
for (const auto& epoll : epolls) MarkDestroyed(*epoll);
return 0;
}
int APS5_VABI sceHttpUnsetEpoll(int id) {
(void)id;
return 0;
}
int APS5_VABI sceHttpWaitRequest(HttpEpollHandle eh, HttpNBEvent* nbev, int maxevents, int timeout) {
std::shared_ptr<HttpEpoll> epoll;
{
std::lock_guard lock(g_stateMutex);
if (g_contexts.empty()) return ERROR_BEFORE_INIT;
if (!eh || !nbev || maxevents <= 0) return ERROR_INVALID_VALUE;
epoll = FindEpoll(eh);
if (!epoll) return ERROR_INVALID_ID;
}
std::unique_lock lock(epoll->mutex);
const auto woken = [&] { return epoll->aborted || epoll->destroyed; };
if (timeout < 0) {
while (!woken()) epoll->wake.Wait(lock);
} else {
const auto deadline = std::chrono::steady_clock::now() + std::chrono::microseconds(timeout);
while (!woken() && std::chrono::steady_clock::now() < deadline) epoll->wake.WaitUntil(lock, deadline);
}
return epoll->aborted ? ERROR_ABORTED : 0;
}
int APS5_VABI sceHttpCreateRequestWithURL(int conn_id, int method, const char* url, uint64_t content_length) {
(void)conn_id;
(void)method;
(void)url;
(void)content_length;
return g_nextHandle.fetch_add(1, std::memory_order_relaxed);
}
int APS5_VABI sceHttpReadData(int request_id, void* data, size_t size) {
(void)request_id;
(void)data;
(void)size;
return ERROR_NETWORK;
}
int APS5_VABI sceHttpRedirectCacheFlush(int http_ctx_id) {
(void)http_ctx_id;
return 0;
}
int APS5_VABI sceHttpSetChunkedTransferEnabled(int id, int enable) {
(void)id;
(void)enable;
return 0;
}
int APS5_VABI sceHttpSetInflateGZIPEnabled(int id, int enable) {
(void)id;
if (static_cast<uint32_t>(enable) > 1) return ERROR_INVALID_VALUE;
return 0;
}
int APS5_VABI sceHttpSetRequestStatusCallback(int id, HttpRequestStatusCallback callback, void* argument) {
if (id <= 0 || id >= g_nextHandle.load(std::memory_order_relaxed)) return static_cast<int>(0x80431100u);
std::lock_guard lock(g_statusMutex);
if (callback) g_statusCallbacks[id] = {callback, argument};
else g_statusCallbacks.erase(id);
return 0;
}
int APS5_VABI sceHttpParseResponseHeader(const char* header, std::size_t headerLen, const char* fieldStr,
const char** fieldValue, std::size_t* valueLen) {
constexpr int invalidResponse = static_cast<int>(0x80432060);
constexpr int invalidValue = static_cast<int>(0x804321FE);
constexpr int notFound = static_cast<int>(0x80432025);
if (!header) return invalidResponse;
if (!fieldStr || !fieldValue || !valueLen) return invalidValue;
std::size_t fieldLen = 0;
while (fieldLen < 0xfff && fieldStr[fieldLen] != '\0') ++fieldLen;
const std::string_view input(header, headerLen);
const auto isSpace = [](char c) { return c == ' ' || (c >= '\t' && c <= '\r'); };
const auto lower = [](char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; };
std::size_t start = 0;
bool found = false;
while (start < headerLen) {
if (!isSpace(input[start]) && fieldLen < headerLen - start && input[start + fieldLen] == ':') {
std::size_t i = 0;
while (i < fieldLen && lower(input[start + i]) == lower(fieldStr[i])) ++i;
if (i == fieldLen) {
start += fieldLen + 1;
found = true;
break;
}
}
const auto newline = input.find('\n', start);
if (newline == std::string_view::npos) break;
start = newline + 1;
}
if (!found) return notFound;
while (start < headerLen && isSpace(input[start])) {
if (input[start++] == '\n') break;
}
std::size_t end = headerLen;
std::size_t consumed = headerLen;
std::size_t scan = start;
while (scan < headerLen) {
const auto newline = input.find('\n', scan);
if (newline == std::string_view::npos) break;
const auto next = newline + 1;
if (next < headerLen && (input[next] == ' ' || input[next] == '\t')) {
scan = next;
continue;
}
end = newline > start && input[newline - 1] == '\r' ? newline - 1 : newline;
consumed = next;
break;
}
if (end == start) {
*fieldValue = nullptr;
*valueLen = 0;
return 0;
}
if (consumed > static_cast<std::size_t>(std::numeric_limits<int>::max())) {
throw std::out_of_range("sceHttpParseResponseHeader: consumed byte count exceeds int");
}
*fieldValue = header + start;
*valueLen = end - start;
return static_cast<int>(consumed);
}
int APS5_VABI sceHttpCreateRequest2(int conn_id, const char* method, const char* path, uint64_t content_length) {
(void)conn_id;
(void)method;
(void)path;
(void)content_length;
return g_nextHandle.fetch_add(1, std::memory_order_relaxed);
}
int APS5_VABI sceHttpsEnableOption(int id, uint32_t ssl_flags) {
(void)id;
(void)ssl_flags;
return 0;
}
int APS5_VABI sceHttpsLoadCert(int http_ctx_id, int num, void* ca_list, void* cert, void* key) {
(void)http_ctx_id;
(void)num;
(void)ca_list;
(void)cert;
(void)key;
return 0;
}
int APS5_VABI sceHttpsUnloadCert(int http_ctx_id) {
(void)http_ctx_id;
return 0;
}
int APS5_VABI sceHttpGetLastErrno(int request_id, int* errno_out) {
(void)request_id;
if (errno_out == nullptr) {
return ERROR_INVALID_VALUE;
}
*errno_out = 0;
return 0;
}
int APS5_VABI sceHttpsGetSslError(int id, int* err_num, uint32_t* detail) {
(void)id;
if (err_num == nullptr || detail == nullptr) {
return ERROR_INVALID_VALUE;
}
*err_num = 0;
*detail = 0;
return 0;
}
int APS5_VABI sceHttpSetCookieRecvCallback(int id, HttpCookieRecvCallback cbfunc, void* user_arg) {
(void)id;
(void)cbfunc;
(void)user_arg;
return 0;
}
int APS5_VABI sceHttpsSetSslVersion(int id, int ssl_version) {
(void)id;
(void)ssl_version;
return 0;
}
int APS5_VABI sceHttpSetRedirectCallback(int id, HttpRedirectCallback cbfunc, void* user_arg) {
(void)id;
(void)cbfunc;
(void)user_arg;
return 0;
}
int APS5_VABI sceHttpAbortWaitRequest(HttpEpollHandle eh) {
std::shared_ptr<HttpEpoll> epoll;
{
std::lock_guard lock(g_stateMutex);
if (g_contexts.empty()) return ERROR_BEFORE_INIT;
if (!eh) return ERROR_INVALID_VALUE;
epoll = FindEpoll(eh);
if (!epoll) return ERROR_INVALID_ID;
}
std::lock_guard lock(epoll->mutex);
epoll->aborted = true;
epoll->wake.NotifyAll();
return 0;
}
int APS5_VABI sceHttpsGetCaList(int http_ctx_id, HttpsCaList* ca_list) {
(void)http_ctx_id;
if (!ca_list) return ERROR_INVALID_VALUE;
ca_list->certs = nullptr;
ca_list->certsNum = 0;
return 0;
}
int APS5_VABI sceHttpsFreeCaList(int http_ctx_id, HttpsCaList* ca_list) {
(void)http_ctx_id;
if (!ca_list) return ERROR_INVALID_VALUE;
ca_list->certs = nullptr;
ca_list->certsNum = 0;
return 0;
}
}