forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvitahttp.cpp
More file actions
228 lines (192 loc) · 5.36 KB
/
Copy pathvitahttp.cpp
File metadata and controls
228 lines (192 loc) · 5.36 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
#include "vitahttp.hpp"
#include <psp2/io/fcntl.h>
#include <psp2/net/http.h>
#include <psp2/net/net.h>
#include <psp2/net/netctl.h>
#include <fmt/format.h>
#include <boost/scope_exit.hpp>
#define PKGI_USER_AGENT "libhttp/3.65 (PS Vita)"
struct pkgi_http
{
int used;
int tmpl;
int conn;
int req;
};
namespace
{
static pkgi_http g_http[4];
}
VitaHttp::~VitaHttp()
{
if (_http)
{
LOG("HTTP connection closed");
sceHttpDeleteRequest(_http->req);
sceHttpDeleteConnection(_http->conn);
sceHttpDeleteTemplate(_http->tmpl);
_http->used = 0;
}
}
void VitaHttp::start(const std::string& url, uint64_t offset)
{
if (_http)
throw HttpError("HTTP connection already started");
LOG("HTTP GET request");
pkgi_http* http = NULL;
for (size_t i = 0; i < 4; i++)
{
if (g_http[i].used == 0)
{
http = g_http + i;
break;
}
}
if (!http)
throw HttpError("internal error: too many simultaneous http requests");
int tmpl = -1;
int conn = -1;
int req = -1;
LOGF("HTTP GET: {}", url);
if ((tmpl = sceHttpCreateTemplate(
PKGI_USER_AGENT, SCE_HTTP_VERSION_1_1, SCE_TRUE)) < 0)
throw HttpError(fmt::format(
"sceHttpCreateTemplate failed: {:#08x}",
static_cast<uint32_t>(tmpl)));
BOOST_SCOPE_EXIT_ALL(&)
{
if (tmpl > 0)
sceHttpDeleteTemplate(tmpl);
};
// sceHttpSetRecvTimeOut(tmpl, 10 * 1000 * 1000);
if ((conn = sceHttpCreateConnectionWithURL(tmpl, url.c_str(), SCE_FALSE)) <
0)
throw HttpError(fmt::format(
"sceHttpCreateConnectionWithURL failed: {:#08x}",
static_cast<uint32_t>(conn)));
BOOST_SCOPE_EXIT_ALL(&)
{
if (conn > 0)
sceHttpDeleteConnection(conn);
};
if ((req = sceHttpCreateRequestWithURL(
conn, SCE_HTTP_METHOD_GET, url.c_str(), 0)) < 0)
throw HttpError(fmt::format(
"sceHttpCreateRequestWithURL failed: {:#08x}",
static_cast<uint32_t>(req)));
BOOST_SCOPE_EXIT_ALL(&)
{
if (req > 0)
sceHttpDeleteRequest(req);
};
int err;
if (offset != 0)
{
char range[64];
pkgi_snprintf(range, sizeof(range), "bytes=%llu-", offset);
if ((err = sceHttpAddRequestHeader(
req, "Range", range, SCE_HTTP_HEADER_ADD)) < 0)
throw HttpError(fmt::format(
"sceHttpAddRequestHeader failed: {:#08x}",
static_cast<uint32_t>(err)));
}
if ((err = sceHttpSendRequest(req, NULL, 0)) < 0)
{
std::string err_msg;
switch (static_cast<uint32_t>(err))
{
case 0x80431063:
err_msg = "Network error";
break;
case 0x80431068:
err_msg = "Network timeout";
break;
case 0x80431082:
err_msg = "Request blocked";
break;
case 0x80436007:
err_msg = "Host not found";
break;
case 0x80431084:
err_msg = "Proxy error";
break;
case 0x80431075:
err_msg = "SSL error";
break;
default:
err_msg = "";
}
throw formatEx<HttpError>(
"sceHttpSendRequest failed: {:#08x}\n{}",
static_cast<uint32_t>(err),
err_msg);
}
http->used = 1;
http->tmpl = tmpl;
http->conn = conn;
http->req = req;
tmpl = conn = req = -1;
_http = http;
}
int64_t VitaHttp::read(uint8_t* buffer, uint64_t size)
{
check_status();
int read = sceHttpReadData(_http->req, buffer, size);
if (read < 0)
throw HttpError(fmt::format(
"HTTP download error {:#08x}",
static_cast<uint32_t>(static_cast<int32_t>(read))));
return read;
}
void VitaHttp::abort()
{
if (_http)
{
const auto err = sceHttpAbortRequest(_http->req);
if (err)
LOGFW("HTTP abort failed: err={:#08x}", static_cast<uint32_t>(err));
}
}
int64_t VitaHttp::get_length()
{
check_status();
int res;
uint64_t content_length;
res = sceHttpGetResponseContentLength(_http->req, &content_length);
if (res < 0)
throw HttpError(fmt::format(
"sceHttpGetResponseContentLength failed: {:#08x}",
static_cast<uint32_t>(res)));
if (res == (int)SCE_HTTP_ERROR_NO_CONTENT_LENGTH ||
res == (int)SCE_HTTP_ERROR_CHUNK_ENC)
{
LOG("HTTP response has no Content-Length (chunked transfer assumed)");
return 0;
}
LOGF("HTTP Content-Length: {} bytes", content_length);
return content_length;
}
int VitaHttp::get_status()
{
int res;
int status;
if ((res = sceHttpGetStatusCode(_http->req, &status)) < 0)
throw HttpError(fmt::format(
"sceHttpGetStatusCode failed: {:#08x}",
static_cast<uint32_t>(res)));
return status;
}
void VitaHttp::check_status()
{
if (_status_checked)
return;
_status_checked = true;
const auto status = get_status();
LOGF("HTTP response status: {}", status);
if (status != 200 && status != 206)
throw HttpError(fmt::format("bad http status: {}", status));
}
VitaHttp::operator bool() const
{
return _http;
}