summaryrefslogtreecommitdiff
path: root/plugins/IMO2sProxy/src/imolib/io_layer_win32.c
blob: 1fd425ced72328bbba9d8c351393ec916d9278df (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
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
/* Module:  io_layer_win32.c
   Purpose: IO Layer for Internet communication using WININET (Win32)
   Author:  leecher
   Date:    30.08.2009
*/
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <wininet.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "fifo.h"
#include "io_layer.h"

#ifndef LongToPtr
#define DWORD_PTR DWORD
#endif

/* Set this to use asynchronous I/O
   This is needed as synchronous WININET functions tend to block forever on connection
   loss or other occasions leading to a deadlock of the whole application
 */
#define ASYNC
#define ASYNC_CONN_TIMEOUT	5000	// Connection timeout in ms
#define ASYNC_REQ_TIMEOUT	40000	// HttpSendRequest timeout in ms in async mode (Should be at least 30 sec!)

#pragma comment(lib,"wininet.lib")

#define INET_FLAGS INTERNET_FLAG_IGNORE_CERT_CN_INVALID | INTERNET_FLAG_IGNORE_CERT_DATE_INVALID | \
		INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_RELOAD  | INTERNET_FLAG_NO_UI | \
		INTERNET_FLAG_PRAGMA_NOCACHE | INTERNET_FLAG_SECURE

typedef struct
{
	IOLAYER vtbl;
	HINTERNET hInet;
	HINTERNET hRequest;
#ifdef ASYNC
	INTERNET_ASYNC_RESULT stAsyncRes;
	HANDLE hConnectedEvent;
	HANDLE hRequestCompleteEvent;
#endif
	TYP_FIFO *hResult;
	LPVOID lpErrorBuf;
} IOLAYER_INST;

static void IoLayer_Exit (IOLAYER *hPIO);
static char *IoLayer_Post(IOLAYER *hPIO, char *pszURL, char *pszPostFields, unsigned int cbPostFields, unsigned int *pdwLength);
static char *IoLayer_Get(IOLAYER *hIO, char *pszURL, unsigned int *pdwLength);
static void IoLayer_Cancel(IOLAYER *hIO);
static char *IoLayer_GetLastError(IOLAYER *hIO);
static char *IoLayer_EscapeString(IOLAYER *hPIO, char *pszData);
static void IoLayer_FreeEscapeString(char *pszData);
static void FetchLastError (IOLAYER_INST *hIO);
#ifdef ASYNC
static void __stdcall Callback(HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus,
              LPVOID lpStatusInfo, DWORD dwStatusInfoLen);
#endif

// -----------------------------------------------------------------------------
// Interface
// -----------------------------------------------------------------------------

IOLAYER *IoLayerW32_Init(void)
{
	IOLAYER_INST *hIO;
	
	if (!(hIO = calloc(1, sizeof(IOLAYER_INST))))
		return NULL;
		
	// Init Inet
	if (!(hIO->hInet = InternetOpen ("Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13",
		INTERNET_OPEN_TYPE_PRECONFIG, NULL, "<local>", 
#ifdef ASYNC
		INTERNET_FLAG_ASYNC
#else
		0
#endif
		)))
	{
		free (hIO);
		return NULL;
	}
	
	if (!(hIO->hResult = Fifo_Init(1024)))
	{
		IoLayer_Exit((IOLAYER*)hIO);
		return NULL;
	}

#ifdef ASYNC
    if (InternetSetStatusCallback(hIO->hInet, (INTERNET_STATUS_CALLBACK)&Callback) == INTERNET_INVALID_STATUS_CALLBACK)
	{
		IoLayer_Exit((IOLAYER*)hIO);
		return NULL;
	}

    hIO->hConnectedEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    hIO->hRequestCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
#endif

	//InternetSetCookie ("https://o.imo.im/", "proto", "prpl-skype");

	// Init Vtbl
	hIO->vtbl.Exit = IoLayer_Exit;
	hIO->vtbl.Post = IoLayer_Post;
	hIO->vtbl.Get = IoLayer_Get;
	hIO->vtbl.Cancel = IoLayer_Cancel;
	hIO->vtbl.GetLastError = IoLayer_GetLastError;
	hIO->vtbl.EscapeString = IoLayer_EscapeString;
	hIO->vtbl.FreeEscapeString = IoLayer_FreeEscapeString;

	return (IOLAYER*)hIO;
}
// -----------------------------------------------------------------------------

static void IoLayer_Exit (IOLAYER *hPIO)
{
	IOLAYER_INST *hIO = (IOLAYER_INST*)hPIO;

	if (hIO->hInet) InternetCloseHandle (hIO->hInet);
	if (hIO->lpErrorBuf) LocalFree(hIO->lpErrorBuf);
	if (hIO->hResult) Fifo_Exit(hIO->hResult);

#ifdef ASYNC
	if (hIO->hConnectedEvent) CloseHandle (hIO->hConnectedEvent);
	if (hIO->hRequestCompleteEvent) CloseHandle (hIO->hRequestCompleteEvent);
#endif
	free (hIO);
}

// -----------------------------------------------------------------------------

static char *IoLayer_Post(IOLAYER *hPIO, char *pszURL, char *pszPostFields, unsigned int cbPostFields, unsigned int *pdwLength)
{
	IOLAYER_INST *hIO = (IOLAYER_INST*)hPIO;
	URL_COMPONENTS urlInfo = {0};
	HINTERNET hUrl;
	DWORD dwFlags = 0, cbFlags = sizeof(dwFlags), dwRemaining = 0;
	char szHostName[INTERNET_MAX_HOST_NAME_LENGTH],
		szURLPath[INTERNET_MAX_URL_LENGTH], *p;
#ifdef ASYNC
    INTERNET_BUFFERS InetBuff={0};
    InetBuff.dwStructSize = sizeof(InetBuff);

	ResetEvent (hIO->hConnectedEvent);
	ResetEvent (hIO->hRequestCompleteEvent);
#endif

//OutputDebugString(pszPostFields);
	urlInfo.dwStructSize = sizeof (URL_COMPONENTS);
	urlInfo.lpszHostName = szHostName;
	urlInfo.dwHostNameLength = sizeof(szHostName);
	urlInfo.lpszUrlPath = szURLPath;
	urlInfo.dwUrlPathLength = sizeof(szURLPath);
	if (!InternetCrackUrl(pszURL, strlen(pszURL), 0, &urlInfo)) return NULL;
	/*
	if (!pszPostFields)
	{
		if (pszPostFields=strchr (pszURL, '?'))
			cbPostFields = strlen(pszPostFields);
	}
	*/
	if (!(hUrl = InternetConnect (hIO->hInet, szHostName, 
		INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)hIO)))
	{
#ifdef ASYNC
		if (GetLastError() == ERROR_IO_PENDING)
		{
			if (WaitForSingleObject(hIO->hConnectedEvent, ASYNC_CONN_TIMEOUT) == WAIT_OBJECT_0)
			{
				hUrl = (HINTERNET)hIO->stAsyncRes.dwResult;
				hIO->stAsyncRes.dwResult = 0;
				SetLastError(hIO->stAsyncRes.dwError);
			}
		}
		if (!hUrl)
#endif
		{
			FetchLastError (hIO);
			return NULL;
		}
	}
	
	hIO->hRequest = HttpOpenRequest (hUrl, pszPostFields?"POST":"GET", szURLPath, NULL, "https://imo.im/", NULL, 
		INET_FLAGS, (DWORD_PTR)hIO);
	if (!hIO->hRequest)
	{
#ifdef ASYNC
		if (GetLastError() == ERROR_IO_PENDING)
		{
			if (WaitForSingleObject(hIO->hConnectedEvent, ASYNC_CONN_TIMEOUT) == WAIT_OBJECT_0)
			{
				hIO->hRequest = (HINTERNET)hIO->stAsyncRes.dwResult;
				hIO->stAsyncRes.dwResult = 0;
				SetLastError(hIO->stAsyncRes.dwError);
			}
		}
		if (!hUrl)
#endif
		{
			FetchLastError (hIO);
			InternetCloseHandle (hUrl);
			return NULL;
		}
	}
	
	InternetQueryOption (hIO->hRequest, INTERNET_OPTION_SECURITY_FLAGS, (LPVOID)&dwFlags, &cbFlags); 
	dwFlags |= SECURITY_FLAG_IGNORE_UNKNOWN_CA;
	InternetSetOption (hIO->hRequest, INTERNET_OPTION_SECURITY_FLAGS, &dwFlags, sizeof (dwFlags));

	/*
	{
		char szCookies[4096];
		DWORD cbCookies, dwIndex=0;

		OutputDebugString ("Sending headers:\n");
		do
		{
			cbCookies=sizeof(szCookies);
			if (!HttpQueryInfo (hIO->hRequest, HTTP_QUERY_FLAG_REQUEST_HEADERS|HTTP_QUERY_RAW_HEADERS_CRLF, szCookies, &cbCookies, &dwIndex) ||
				GetLastError() != ERROR_SUCCESS)
				break;
			OutputDebugString (szCookies);
		} while (1);
	}
	*/

// ASYNC: This needs to block for at least 30 seconds on poll!
	if (!(HttpSendRequest (hIO->hRequest, "Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n"
		"X-Requested-With: XMLHttpRequest", -1,
		pszPostFields, cbPostFields)))
	{
#ifdef ASYNC
		BOOL bRes = FALSE;

		if (GetLastError() == ERROR_IO_PENDING)
		{
			if (WaitForSingleObject(hIO->hRequestCompleteEvent, ASYNC_REQ_TIMEOUT) == WAIT_OBJECT_0)
			{
				bRes = hIO->stAsyncRes.dwResult;
				hIO->stAsyncRes.dwResult = 0;
				SetLastError(hIO->stAsyncRes.dwError);
			}
		}
		if (!bRes)
#endif
		{
			FetchLastError (hIO);
			InternetCloseHandle (hIO->hRequest);
			hIO->hRequest = NULL;
			InternetCloseHandle (hUrl);
			return NULL;
		}
	}
#ifdef ASYNC
	else WaitForSingleObject(hIO->hRequestCompleteEvent, ASYNC_REQ_TIMEOUT);
#endif

	/*
	{
		char szCookies[4096];
		DWORD cbCookies, dwIndex=0;

		OutputDebugString ("Received headers:\n");
		do
		{
			cbCookies=sizeof(szCookies);
			if (!HttpQueryInfo (hIO->hRequest, HTTP_QUERY_FLAG_REQUEST_HEADERS|HTTP_QUERY_RAW_HEADERS_CRLF, szCookies, &cbCookies, &dwIndex) ||
				GetLastError() != ERROR_SUCCESS)
				break;
			OutputDebugString (szCookies);
		} while (1);
	}
	*/

#ifdef ASYNC
	do
	{
		if (!InternetQueryDataAvailable (hIO->hRequest, &InetBuff.dwBufferLength, 0, 0))
		{
			BOOL bRes = FALSE;

			if (GetLastError() == ERROR_IO_PENDING)
			{
				if (WaitForSingleObject(hIO->hRequestCompleteEvent, ASYNC_REQ_TIMEOUT) == WAIT_OBJECT_0)
				{
					bRes = hIO->stAsyncRes.dwResult;
					hIO->stAsyncRes.dwResult = 0;
					SetLastError(hIO->stAsyncRes.dwError);
				}
			}
			if (!bRes) break;
		}
		if (InetBuff.dwBufferLength == 0) break;
		if (InetBuff.lpvBuffer = Fifo_AllocBuffer (hIO->hResult, InetBuff.dwBufferLength))
		{
			if (!InternetReadFileEx (hIO->hRequest, &InetBuff, 0, (DWORD_PTR)hIO))
			{
				BOOL bRes=FALSE;

				if (GetLastError() == ERROR_IO_PENDING)
				{
					if (WaitForSingleObject(hIO->hRequestCompleteEvent, ASYNC_REQ_TIMEOUT) == WAIT_OBJECT_0)
					{
						bRes = hIO->stAsyncRes.dwResult;
						hIO->stAsyncRes.dwResult = 0;
						SetLastError(hIO->stAsyncRes.dwError);
					}
				}
				if (!bRes) break;
			}
		}
	}
	while(InetBuff.dwBufferLength);
#else
	while (InternetQueryDataAvailable (hIO->hRequest, &dwRemaining, 0, 0) && dwRemaining > 0)
	{
		if (p = Fifo_AllocBuffer (hIO->hResult, dwRemaining))
			InternetReadFile (hIO->hRequest, p, dwRemaining, &dwRemaining);
	}
#endif

	if (!pdwLength)
	{
		// Get string
		Fifo_Add (hIO->hResult, "", 1);
		p = Fifo_Get (hIO->hResult, NULL);
	}
	else
	{
		// Get binary, return size of buffer
		*pdwLength = (unsigned int)-1;
		p = Fifo_Get (hIO->hResult, pdwLength);
	}
	InternetCloseHandle (hIO->hRequest);
	hIO->hRequest = NULL;
	InternetCloseHandle (hUrl);
OutputDebugString(p);
OutputDebugString("\n");
	return p;
}

// -----------------------------------------------------------------------------

static char *IoLayer_Get(IOLAYER *hIO, char *pszURL, unsigned int *pdwLength)
{
	return IoLayer_Post (hIO, pszURL, NULL, 0, pdwLength);
}

// -----------------------------------------------------------------------------

static void IoLayer_Cancel(IOLAYER *hPIO)
{
	IOLAYER_INST *hIO = (IOLAYER_INST*)hPIO;

#ifdef ASYNC
	if (hIO->hRequest)
	{
		hIO->stAsyncRes.dwResult = 0;
		hIO->stAsyncRes.dwError = ERROR_CANCELLED;
		SetEvent(hIO->hRequestCompleteEvent);
	}
#else
	if (hIO->hRequest && InternetCloseHandle(hIO->hRequest))
		hIO->hRequest = NULL;
#endif
}

// -----------------------------------------------------------------------------

static char *IoLayer_GetLastError(IOLAYER *hIO)
{
	return (char*)((IOLAYER_INST*)hIO)->lpErrorBuf;
}

// -----------------------------------------------------------------------------

static char *IoLayer_EscapeString(IOLAYER *hPIO, char *pszData)
{
	IOLAYER_INST *hIO = (IOLAYER_INST*)hPIO;
	TYP_FIFO *hFifo;
	char szBuf[8], *pszRet;
	unsigned char *p;

	if (!(hFifo = Fifo_Init(strlen(pszData)))) return NULL;
	for (p=pszData; *p; p++)
	{
		if (isalnum(*p)) Fifo_Add (hFifo, p, 1);
		else {
			wsprintf (szBuf, "%%%02X", *p);
			Fifo_Add (hFifo, szBuf, 3);
		}
	}
	Fifo_Add (hFifo, "", 1);
	if (pszRet = Fifo_Get(hFifo, NULL))
		pszRet = strdup(pszRet);
	Fifo_Exit(hFifo);
	return pszRet;
}

// -----------------------------------------------------------------------------

static void IoLayer_FreeEscapeString(char *pszData)
{
	free (pszData);
}

// -----------------------------------------------------------------------------
// Static
// -----------------------------------------------------------------------------

static void FetchLastError (IOLAYER_INST *hIO)
{
	if (hIO->lpErrorBuf) LocalFree(hIO->lpErrorBuf);
	FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | 
		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
		GetLastError(), MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
		(LPTSTR)&hIO->lpErrorBuf, 0, NULL);
}

#ifdef ASYNC
static void __stdcall Callback(HINTERNET hInternet, DWORD dwContext, DWORD dwInternetStatus,
              LPVOID lpStatusInfo, DWORD dwStatusInfoLen)
{
	IOLAYER_INST *hIO = (IOLAYER_INST*)dwContext;


    switch(dwInternetStatus)
    {
    case INTERNET_STATUS_HANDLE_CREATED:
        SetEvent (hIO->hConnectedEvent);
		break;
    case INTERNET_STATUS_REQUEST_COMPLETE:
		hIO->stAsyncRes = *((INTERNET_ASYNC_RESULT *)lpStatusInfo);
        SetEvent(hIO->hRequestCompleteEvent);
        break;
    }
}
#endif