aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Client/PS3/Network/SonyHttp_PS3.cpp
blob: 4e681354fd9971cbd1ff9f8e55f86b5280cf9bbc (plain)
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
#include "stdafx.h"
#include "SonyHttp_PS3.h"



// static const int sc_HTTPPoolSize = (32 * 1024);
// static const int sc_SSLPoolSize   = (256 * 1024U);
// static const int sc_CookiePoolSize   = (256 * 1024U);
 static const int sc_HTTPPoolSize = (32 * 1024);
 static const int sc_SSLPoolSize   = (512 * 1024U);
 static const int sc_CookiePoolSize   = (256 * 1024U);

void* SonyHttp_PS3::uriPool = nullptr;
void* SonyHttp_PS3::httpPool = nullptr;
void* SonyHttp_PS3::sslPool = nullptr;
void* SonyHttp_PS3::cookiePool = nullptr;
CellHttpClientId SonyHttp_PS3::client;
CellHttpTransId SonyHttp_PS3::trans;
bool SonyHttp_PS3:: bInitialised = false;



bool SonyHttp_PS3::loadCerts(size_t *numBufPtr, CellHttpsData **caListPtr)
{
	CellHttpsData *caList = nullptr;
	size_t size = 0;
	int ret = 0;
	char *buf = nullptr;

	caList = static_cast<CellHttpsData *>(malloc(sizeof(CellHttpsData) * 2));
	if (nullptr == caList) {
		app.DebugPrintf("failed to malloc cert data");
		return false;
	}

	ret = cellSslCertificateLoader(CELL_SSL_LOAD_CERT_ALL, nullptr, 0, &size);
	if (ret < 0) {
		app.DebugPrintf("cellSslCertifacateLoader() failed(1): 0x%08x", ret);
		return ret;
	}

	buf = static_cast<char *>(malloc(size));
	if (nullptr == buf) {
		app.DebugPrintf("failed to malloc cert buffer");
		free(caList);
		return false;
	}

	ret = cellSslCertificateLoader(CELL_SSL_LOAD_CERT_ALL, buf, size, nullptr);
	if (ret < 0) {
		app.DebugPrintf("cellSslCertifacateLoader() failed(2): 0x%08x", ret);
		free(buf);
		free(caList);
		return false;
	}

	(&caList[0])->ptr = buf;
	(&caList[0])->size = size;

	*caListPtr = caList;
	*numBufPtr = 1;

	return true;
}


bool SonyHttp_PS3::init()
{
	int ret;
	client = 0;
	trans = 0;

	/*E startup procedures */
	httpPool = malloc(sc_HTTPPoolSize);
	if (httpPool == nullptr) {
		app.DebugPrintf("failed to malloc libhttp memory pool\n");
		return false;
	}
	ret = cellHttpInit(httpPool, sc_HTTPPoolSize);
	if (0 > ret) 
	{
		app.DebugPrintf("unable to start libhttp... (0x%x)\n", ret);
		return false;
	}

	cookiePool = malloc(sc_CookiePoolSize);
	if (cookiePool == nullptr) {
		app.DebugPrintf("failed to malloc ssl memory pool\n");
		return false;
	}
	ret = cellHttpInitCookie(cookiePool, sc_CookiePoolSize);
	if (ret < 0 && ret != CELL_HTTP_ERROR_ALREADY_INITIALIZED) 
	{
		app.DebugPrintf("cellHttpInitCookie failed... (0x%x)\n", ret);
		return false;
	}


	sslPool = malloc(sc_SSLPoolSize);
	if (sslPool == nullptr) {
		app.DebugPrintf("failed to malloc ssl memory pool\n");
		return false;
	}
	ret = cellSslInit(sslPool, sc_SSLPoolSize);
	if (0 > ret) 
	{
		app.DebugPrintf("unable to start cellSslInit... (0x%x)\n", ret);
		return false;
	}

	size_t numBuf = 0;
	CellHttpsData *caList = nullptr;
	if(!loadCerts(&numBuf, &caList))
		return false;

	ret = cellHttpsInit(numBuf, caList);
	free(caList->ptr);
	free(caList);
	if (ret < 0 && ret != CELL_HTTP_ERROR_ALREADY_INITIALIZED) 
	{
		app.DebugPrintf("unable to start https: 0x%08x", ret);
		return false;
	}

	ret = cellHttpCreateClient(&client);
	if (0 > ret) 
	{
		app.DebugPrintf("unable to create http client... (0x%x)\n", ret);
		return false;
	}
	bInitialised = true;
	return true;
}

void SonyHttp_PS3::shutdown()
{
	if (trans) 
	{
		cellHttpDestroyTransaction(trans);
		trans = 0;
	}
	if (client) 
	{
		cellHttpDestroyClient(client);
		client = 0;
	}
	cellHttpEnd();
	free(httpPool);
}


bool SonyHttp_PS3::parseUri(const char* szUri, CellHttpUri& parsedUri)
{
	/*E the main part */
	size_t poolSize = 0;
	int ret = cellHttpUtilParseUri(nullptr, szUri, nullptr, 0, &poolSize);
	if (0 > ret) 
	{
		app.DebugPrintf("error parsing URI... (0x%x)\n\n", ret);
		return false;
	}
	if (nullptr == (uriPool = malloc(poolSize))) 
	{
		app.DebugPrintf("error mallocing uriPool (%d)\n", poolSize);
		return false;
	}
	ret = cellHttpUtilParseUri(&parsedUri, szUri, uriPool, poolSize, nullptr);
	if (0 > ret) 
	{
		free(uriPool);
		app.DebugPrintf("error parsing URI... (0x%x)\n\n", ret);
		return false;
	}
	return true;
}

void* SonyHttp_PS3::getData(const char* url, int* pDataSize)
{
	CellHttpUri uri;
	int ret;
	bool has_cl = true;
	uint64_t length = 0;
	uint64_t recvd;
	const char *serverName;
	size_t localRecv = 0;

	if(!parseUri(url, uri))
		return nullptr;

	app.DebugPrintf("  scheme:   %s\n", uri.scheme);
	app.DebugPrintf("  hostname: %s\n", uri.hostname);
	app.DebugPrintf("  port:     %d\n", uri.port);
	app.DebugPrintf("  path:     %s\n", uri.path);
	app.DebugPrintf("  username: %s\n", uri.username);
	app.DebugPrintf("  password: %s\n", uri.password);

	ret = cellHttpCreateTransaction(&trans, client, CELL_HTTP_METHOD_GET, &uri);
	free(uriPool);
	if (0 > ret) 
	{
		app.DebugPrintf("failed to create http transaction... (0x%x)\n\n", ret);
		return nullptr;
	}

	ret = cellHttpSendRequest(trans, nullptr, 0, nullptr);
	if (0 > ret) 
	{
		app.DebugPrintf("failed to complete http transaction... (0x%x)\n\n", ret);
		/*E continue down to check status code, just in case */
		cellHttpDestroyTransaction(trans);
		trans = 0;
	}

	{
		int code = 0;
		ret = cellHttpResponseGetStatusCode(trans, &code);
		if (0 > ret)
		{
			app.DebugPrintf("failed to receive http response... (0x%x)\n\n", ret);
			cellHttpDestroyTransaction(trans);
			trans = 0;
			return nullptr;
		}
		app.DebugPrintf("Status Code is %d\n", code);
	}

	ret = cellHttpResponseGetContentLength(trans, &length);
	if (0 > ret) 
	{
		if (ret == CELL_HTTP_ERROR_NO_CONTENT_LENGTH) 
		{
			app.DebugPrintf("Only supporting data that has a content length : CELL_HTTP_ERROR_NO_CONTENT_LENGTH\n", ret);
			cellHttpDestroyTransaction(trans);
			trans = 0;
			return nullptr;
		} 
		else 
		{
			app.DebugPrintf("error in receiving content length... (0x%x)\n\n", ret);
			cellHttpDestroyTransaction(trans);
			trans = 0;
			return nullptr;
		}
	}

	int bufferSize = length;
	char* buffer = static_cast<char *>(malloc(bufferSize + 1));

	recvd = 0;

	// 4J-PB - seems to be some limit to a read with cellHttpRecvResponse, even though it claims it's read all the data, it hasn't
	// reducing this to read 10k at a time.
	int iMaxRead=10240;
	app.DebugPrintf("\n===BEGINNING OF TRANSMISSION===\n");
	while(recvd<length)
	{	
		ret = cellHttpRecvResponse(trans, &buffer[recvd], iMaxRead, &localRecv);
		if (0 > ret) 
		{
			app.DebugPrintf("error receiving body... (0x%x)\n\n", ret);
			free(buffer);
			cellHttpDestroyTransaction(trans);
			trans = 0;
			return nullptr;
		} 
		else
		{
			if(localRecv==0) break;
		}
		recvd += localRecv;
	}
	buffer[bufferSize] = '\0';
	ret = 0;
	app.DebugPrintf("\n===END OF TRANSMISSION===\n\n");
	*pDataSize = bufferSize;
	cellHttpDestroyTransaction(trans);
	trans = 0;
	return buffer;
}

bool SonyHttp_PS3::getDataFromURL( const char* szURL, void** ppOutData, int* pDataSize)
{
	if(!bInitialised)
		return false;
	*ppOutData = getData(szURL, pDataSize);
	if(*ppOutData)
		return true;
	return false;
}