summaryrefslogtreecommitdiff
path: root/miranda-wine/src/modules/netlib/netlibhttpproxy.c
blob: f3fc82ebfc4bd32b75d627b8bca7a9d0a1971461 (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
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
599
600
601
602
603
604
605
606
607
608
609
610
/*

Miranda IM: the free IM client for Microsoft* Windows*

Copyright 2000-2006 Miranda ICQ/IM project,
all portions of this codebase are copyrighted to the people
listed in contributors.txt.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
#include "commonheaders.h"
#include "netlib.h"

#define HTTPGETTIMEOUT   55000  //in ms. http GETs through most proxies will give up after a while so the request needs to be re-sent

static int HttpGatewaySendGet(struct NetlibConnection *nlc)
{
	NETLIBHTTPREQUEST nlhrSend={0};
	NETLIBHTTPHEADER httpHeaders[3];
	char szUrl[512];
	struct NetlibConnection nlcSend;

	nlc->s=socket(AF_INET,SOCK_STREAM,0);
	if(nlc->s==INVALID_SOCKET) {
		Netlib_Logf(nlc->nlu,"%s %d: %s() failed (%u)",__FILE__,__LINE__,"socket",WSAGetLastError());
		return 0;
	}

	if(connect(nlc->s,(SOCKADDR *)&nlc->sinProxy,sizeof(nlc->sinProxy))==SOCKET_ERROR) {
		Netlib_Logf(nlc->nlu,"%s %d: %s() failed (%u)",__FILE__,__LINE__,"connect",WSAGetLastError());
		return 0;
	}

	nlhrSend.cbSize=sizeof(nlhrSend);
	nlhrSend.nlc=nlc;

	/*
	 * Gena01 - one small change here, just in case there is a timeout or a problem and we died while
	 *          receiving
	 */
	nlhrSend.requestType=(nlc->nlhpi.szHttpGetUrl == NULL) ? REQUEST_POST : REQUEST_GET;
	nlhrSend.flags=NLHRF_GENERATEHOST|NLHRF_DUMPPROXY|NLHRF_SMARTAUTHHEADER;
	if (nlc->nlhpi.flags & NLHPIF_HTTP11) nlhrSend.flags |= NLHRF_HTTP11;

	/*
	 * Gena01 - fixing a possible crash, can't use GET Sequence if there is no GET URL
	 */
	if ((nlc->nlhpi.flags&NLHPIF_USEGETSEQUENCE) && (nlc->nlhpi.szHttpGetUrl != NULL)) {
		EnterCriticalSection(&nlc->csHttpSequenceNums);

		mir_snprintf(szUrl,SIZEOF(szUrl),"%s%u",nlc->nlhpi.szHttpGetUrl,nlc->nlhpi.firstGetSequence);
		nlc->nlhpi.firstGetSequence++;
		if(nlc->nlhpi.flags&NLHPIF_GETPOSTSAMESEQUENCE) nlc->nlhpi.firstPostSequence++;
		LeaveCriticalSection(&nlc->csHttpSequenceNums);
		nlhrSend.szUrl=szUrl;
	}
	else nlhrSend.szUrl=(nlc->nlhpi.szHttpGetUrl == NULL) ? nlc->nlhpi.szHttpPostUrl : nlc->nlhpi.szHttpGetUrl;
	nlhrSend.headers=httpHeaders;
	nlhrSend.headersCount= 3;
	httpHeaders[0].szName="User-Agent";
	httpHeaders[0].szValue=nlc->nlu->user.szHttpGatewayUserAgent;
	httpHeaders[1].szName="Cache-Control";
	httpHeaders[1].szValue="no-store, no-cache";
	httpHeaders[2].szName="Pragma";
	httpHeaders[2].szValue="no-cache";

	nlcSend=*nlc;
	nlcSend.usingHttpGateway=0;

	if (nlc->nlhpi.szHttpGetUrl != NULL) {

		Netlib_Logf(nlc->nlu,"%s %d: Sending data.[ICQ GET] ",__FILE__,__LINE__);
		if(NetlibHttpSendRequest((WPARAM)&nlcSend,(LPARAM)&nlhrSend)==SOCKET_ERROR) {
			nlc->usingHttpGateway=1;
			return 0;
		}
		nlc->dwLastGetSentTime=GetTickCount();
		return 1;
	}

	/*
	 * Gena01 - small addition here, if we doing a POST then insert our packet here
	 */
	if (nlc->pHttpProxyPacketQueue != NULL) {
			struct NetlibHTTPProxyPacketQueue *p = nlc->pHttpProxyPacketQueue;

        	nlc->pHttpProxyPacketQueue = nlc->pHttpProxyPacketQueue->next;

			nlhrSend.dataLength=p->dataBufferLen;
	        nlhrSend.pData=(char*)p->dataBuffer;

			mir_free(p);
	}

	if(NetlibHttpSendRequest((WPARAM)&nlcSend,(LPARAM)&nlhrSend)==SOCKET_ERROR) {
		struct NetlibHTTPProxyPacketQueue *p = nlc->pHttpProxyPacketQueue;
		
		mir_free(nlhrSend.pData);

		nlc->usingHttpGateway=1;

		/*
		 * Gena01 - we need to drop ALL pending packets. Connection died!
		 */
		while (p != NULL) {
			struct NetlibHTTPProxyPacketQueue *t = p;

			p = p->next;

			mir_free(t->dataBuffer);
			mir_free(t);
		}

		nlc->pHttpProxyPacketQueue = NULL; /* empty Queue */

		return 0;
	}
	mir_free(nlhrSend.pData);
	nlc->dwLastGetSentTime=GetTickCount();
	return 1;
}

/*
 * Gena01 - this is the old POST method, I renamed it and left it intact for ICQ support. it's called
 *          when we have both GET and POST URLs specified.
 */
int NetlibHttpGatewayOLDPost(struct NetlibConnection *nlc,const char *buf,int len,int flags)
{
	NETLIBHTTPREQUEST nlhrSend={0},*nlhrReply;
	NETLIBHTTPHEADER httpHeaders[4];
	char szUrl[512];
	struct NetlibConnection nlcSend={0};

	nlcSend.handleType=NLH_CONNECTION;
	nlcSend.nlu=nlc->nlu;
	nlcSend.hInstSecurityDll=nlc->hInstSecurityDll;
	nlcSend.s=socket(AF_INET,SOCK_STREAM,0);
	if(nlcSend.s==INVALID_SOCKET) {
		Netlib_Logf(nlc->nlu,"%s %d: %s() failed (%u)",__FILE__,__LINE__,"socket",WSAGetLastError());
		return SOCKET_ERROR;
	}
	nlcSend.hOkToCloseEvent=CreateEvent(NULL,TRUE,TRUE,NULL);
	nlcSend.dontCloseNow=0;
	NetlibInitializeNestedCS(&nlcSend.ncsRecv);
	NetlibInitializeNestedCS(&nlcSend.ncsSend);

	if(connect(nlcSend.s,(SOCKADDR *)&nlc->sinProxy,sizeof(nlc->sinProxy))==SOCKET_ERROR) {
		Netlib_Logf(nlc->nlu,"%s %d: %s() failed (%u)",__FILE__,__LINE__,"connect",WSAGetLastError());
		NetlibDeleteNestedCS(&nlcSend.ncsRecv);
		NetlibDeleteNestedCS(&nlcSend.ncsSend);
		CloseHandle(nlcSend.hOkToCloseEvent);
		closesocket(nlcSend.s);
		return SOCKET_ERROR;
	}

	nlhrSend.cbSize=sizeof(nlhrSend);
	nlhrSend.nlc=nlc;
	nlhrSend.requestType=REQUEST_POST;
	nlhrSend.flags=NLHRF_GENERATEHOST|NLHRF_DUMPPROXY|NLHRF_SMARTAUTHHEADER;
	if(flags&MSG_NODUMP) nlhrSend.flags|=NLHRF_NODUMP;
	if(nlc->nlhpi.flags&NLHPIF_USEPOSTSEQUENCE) {
		EnterCriticalSection(&nlc->csHttpSequenceNums);
		mir_snprintf(szUrl,SIZEOF(szUrl),"%s%u",nlc->nlhpi.szHttpPostUrl,nlc->nlhpi.firstPostSequence);
		nlc->nlhpi.firstPostSequence++;
		if(nlc->nlhpi.flags&NLHPIF_GETPOSTSAMESEQUENCE) nlc->nlhpi.firstGetSequence++;
		LeaveCriticalSection(&nlc->csHttpSequenceNums);
		nlhrSend.szUrl=szUrl;
	}
	else nlhrSend.szUrl=nlc->nlhpi.szHttpPostUrl;
	nlhrSend.headers=httpHeaders;
	nlhrSend.headersCount=3;
	httpHeaders[0].szName="User-Agent";
	httpHeaders[0].szValue=nlc->nlu->user.szHttpGatewayUserAgent;
	httpHeaders[1].szName="Cache-Control";
	httpHeaders[1].szValue="no-store, no-cache";
	httpHeaders[2].szName="Connection";
	httpHeaders[2].szValue="close";
	httpHeaders[3].szName="Pragma";
	httpHeaders[3].szValue="no-cache";
	nlhrSend.dataLength=len;
	nlhrSend.pData=(char*)buf;
	if(NetlibHttpSendRequest((WPARAM)&nlcSend,(LPARAM)&nlhrSend)==SOCKET_ERROR) {
		NetlibDeleteNestedCS(&nlcSend.ncsRecv);
		NetlibDeleteNestedCS(&nlcSend.ncsSend);
		CloseHandle(nlcSend.hOkToCloseEvent);
		closesocket(nlcSend.s);
		return SOCKET_ERROR;
	}

	nlhrReply=(NETLIBHTTPREQUEST*)NetlibHttpRecvHeaders((WPARAM)&nlcSend,flags&MSG_NODUMP?MSG_NODUMP:MSG_DUMPPROXY);
	if(nlhrReply==NULL
	   || nlhrReply->resultCode<200 || nlhrReply->resultCode>=300) {
		NetlibDeleteNestedCS(&nlcSend.ncsRecv);
		NetlibDeleteNestedCS(&nlcSend.ncsSend);
		CloseHandle(nlcSend.hOkToCloseEvent);
		closesocket(nlcSend.s);
		if(nlhrReply) {
			NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
			NetlibHttpSetLastErrorUsingHttpResult(nlhrReply->resultCode);
		}
		return SOCKET_ERROR;
	}
	NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);

	NetlibDeleteNestedCS(&nlcSend.ncsRecv);
	NetlibDeleteNestedCS(&nlcSend.ncsSend);
	CloseHandle(nlcSend.hOkToCloseEvent);
	closesocket(nlcSend.s);
	return len;
}

int NetlibHttpGatewayPost(struct NetlibConnection *nlc,const char *buf,int len,int flags)
{
		struct NetlibHTTPProxyPacketQueue *p;

        if (nlc->nlhpi.szHttpGetUrl != NULL)
               return NetlibHttpGatewayOLDPost(nlc, buf, len, flags);

        /*
         * Gena01 - many changes here, do compare against the other version.
         *
         * Change #1: simplify to use similar code to GET
         * Change #2: we need to allow to parse POST reply if szHttpGetUrl is NULL
         * Change #3: Keep connection open if we need to.
         *
         * Impact: NONE! Since currently miranda doesn't allow szHttpGetUrl to be NULL, it will not connect
         *         with the new plugins that use this code.
         */

         p = mir_alloc(sizeof(struct NetlibHTTPProxyPacketQueue));
         p->dataBuffer = mir_alloc(len);
         memcpy(p->dataBuffer, buf, len);
         p->dataBufferLen = len;
         p->next = NULL;

         /*
          * Now check to see where to insert this in our queue
          */
        if (nlc->pHttpProxyPacketQueue == NULL) {
                nlc->pHttpProxyPacketQueue = p;
        } else {
                struct NetlibHTTPProxyPacketQueue *t = nlc->pHttpProxyPacketQueue;

                while (t->next != NULL)
                        t = t->next;

                t->next = p;
        }


        /*
         * Gena01 - fake a Send!! tell 'em all is ok. We catch errors in Recv.
         */
        return len;
}

#define NETLIBHTTP_RETRYCOUNT   3
#define NETLIBHTTP_RETRYTIMEOUT 5000

int NetlibHttpGatewayRecv(struct NetlibConnection *nlc,char *buf,int len,int flags)
{
	DWORD dwTimeNow;
	int timedout;
	NETLIBHTTPREQUEST *nlhrReply;
	PBYTE dataBuffer;
	int contentLength,i,bytesRecved;
	int recvResult;
	int retryCount;

	/*
	 * Gena01 - we need to send packet here, since we didn't do it before.
	 */
	if ((nlc->nlhpi.szHttpGetUrl == NULL) && (nlc->s == INVALID_SOCKET) && nlc->dataBuffer == NULL )  {

			if ( nlc->pollingTimeout == 0 )
				nlc->pollingTimeout = 30;

			/* We Need to sleep/wait for the data to send before we do receive */
			for ( retryCount = 0; retryCount < nlc->pollingTimeout; retryCount++ )
			{
				if ( nlc->pHttpProxyPacketQueue != NULL )
					break;

				if ( SleepEx( 1000, TRUE ))
					return SOCKET_ERROR;
			}

/*			if ( retryCount == nlc->pollingTimeout )
			{	SetLastError( ERROR_TIMEOUT );
				return SOCKET_ERROR;
			}
*/
			if ( nlc->pHttpProxyPacketQueue == 0 && nlc->nlu->user.pfnHttpGatewayWrapSend != NULL )
				nlc->nlu->user.pfnHttpGatewayWrapSend((HANDLE)nlc,"",0,MSG_NOHTTPGATEWAYWRAP,NetlibSend);

			if(!HttpGatewaySendGet(nlc)) {
				return SOCKET_ERROR;
			}
	}
	/********************/
	if(nlc->dataBuffer) {
		if(nlc->dataBufferLen<=len) {
			contentLength=nlc->dataBufferLen;
			CopyMemory(buf,nlc->dataBuffer,nlc->dataBufferLen);
			if(!(flags&MSG_PEEK)) {
				mir_free(nlc->dataBuffer);
				nlc->dataBuffer=NULL;
				nlc->dataBufferLen=0;
			}
			return contentLength;
		}
		CopyMemory(buf,nlc->dataBuffer,len);
		if(!(flags&MSG_PEEK)) {
			nlc->dataBufferLen-=len;
			MoveMemory(nlc->dataBuffer,nlc->dataBuffer+len,nlc->dataBufferLen);
			nlc->dataBuffer=(PBYTE)mir_realloc(nlc->dataBuffer,nlc->dataBufferLen);
		}
		return len;
	}
	for( retryCount = 0;;) {
		timedout=0;
		dwTimeNow=GetTickCount();
		if(dwTimeNow>=nlc->dwLastGetSentTime+HTTPGETTIMEOUT) timedout=1;
		else if(!WaitUntilReadable(nlc->s,nlc->dwLastGetSentTime+HTTPGETTIMEOUT-dwTimeNow)) {
			if(GetLastError()==ERROR_TIMEOUT) timedout=1;
			else return SOCKET_ERROR;
		}
		if(timedout) {
			closesocket(nlc->s);
			nlc->s=INVALID_SOCKET;
			if(!HttpGatewaySendGet(nlc)) return SOCKET_ERROR;
			retryCount = 0;
			continue;
		}
		nlhrReply=(NETLIBHTTPREQUEST*)NetlibHttpRecvHeaders((WPARAM)nlc,flags|MSG_RAW|MSG_DUMPPROXY);
		if(nlhrReply==NULL) return SOCKET_ERROR;
        // ignore 1xx result codes
        if (nlhrReply->resultCode < 200)
		{
			NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
			continue;
		}
		// 0.3.1+
		// Attempt to retry NETLIBHTTP_RETRYCOUNT times if the result code is >300
        if (nlhrReply->resultCode >= 300)
        {
			if (retryCount < NETLIBHTTP_RETRYCOUNT) {
				NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
				Netlib_Logf(nlc->nlu, "Error received from proxy, retrying");
				retryCount++;
				closesocket(nlc->s);
				nlc->s = INVALID_SOCKET;
				Sleep(NETLIBHTTP_RETRYTIMEOUT); // wait 5 seconds
				// retry the connection
				Netlib_Logf(nlc->nlu,"%s %d: ResultCode?? Doing GET.",__FILE__,__LINE__);
				if(HttpGatewaySendGet(nlc))
					continue;
				SetLastError(ERROR_GEN_FAILURE);
				return SOCKET_ERROR;
			}
        }
		retryCount = 0;
		contentLength=-1;
		for(i=0;i<nlhrReply->headersCount;i++)
		{
			if(!lstrcmpiA(nlhrReply->headers[i].szName,"Content-Length")) {
				contentLength=atoi(nlhrReply->headers[i].szValue);
				break;
			}
		}

		/*
		if(contentLength<0) {
			NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
			SetLastError(ERROR_INVALID_DATA);
			return SOCKET_ERROR;
		}*/
		if(contentLength==0 && nlc->nlu->user.szHttpGatewayHello != NULL)
		{
			NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
			continue;
		}


		if (contentLength < 0) {
			/* create initial buffer */
			contentLength = 2048;

			dataBuffer=(PBYTE)mir_alloc(contentLength);

			/* error and exit */
			if(dataBuffer==NULL) {
				NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
				SetLastError(ERROR_NOT_ENOUGH_MEMORY);
				return SOCKET_ERROR;
			}

			/* now we need to get the bytes and add them to our buffer */
			bytesRecved = 0;

			do {
				recvResult=NLRecv(nlc,dataBuffer+bytesRecved,contentLength-bytesRecved,MSG_RAW|MSG_DUMPPROXY);
				if(recvResult==SOCKET_ERROR) {
					mir_free(dataBuffer);
					NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
					if(recvResult==0) SetLastError(ERROR_HANDLE_EOF);
					return SOCKET_ERROR;
				}
				bytesRecved+=recvResult;
			} while (recvResult > 0);
			 contentLength = bytesRecved;
		} else {
			if(contentLength > 0) {
				dataBuffer=(PBYTE)mir_alloc(contentLength);
				if(dataBuffer==NULL) {
					NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
					SetLastError(ERROR_NOT_ENOUGH_MEMORY);
					return SOCKET_ERROR;
				}
				for(bytesRecved=0;bytesRecved<contentLength;) {
					recvResult=NLRecv(nlc,dataBuffer+bytesRecved,contentLength-bytesRecved,MSG_RAW|MSG_DUMPPROXY);
					if(recvResult==0 || recvResult==SOCKET_ERROR) {
						mir_free(dataBuffer);
						NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
						if(recvResult==0) SetLastError(ERROR_HANDLE_EOF);
						return SOCKET_ERROR;
					}
					bytesRecved+=recvResult;
			}	}
			else dataBuffer = NULL;
		}

		closesocket(nlc->s);
		nlc->s=INVALID_SOCKET;

		/*
		 * Gena01 - ok, ICQ does it here so that when we enter this function again we have reply
		 *			pending. This is quite clever, since GET always gets replies from ICQ server
		 *
		 */
		if (nlc->nlhpi.szHttpGetUrl != NULL)  {
			Netlib_Logf(nlc->nlu,"%s %d: Doing GET, Again????",__FILE__,__LINE__);

		if(!HttpGatewaySendGet(nlc)) {
			mir_free(dataBuffer);
			NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
			return SOCKET_ERROR;
		}
		}

		if(nlc->nlu->user.pfnHttpGatewayUnwrapRecv && !(flags&MSG_NOHTTPGATEWAYWRAP)) {
			PBYTE newBuffer;
			newBuffer=nlc->nlu->user.pfnHttpGatewayUnwrapRecv(nlhrReply,dataBuffer,contentLength,&contentLength,mir_realloc);
			if(newBuffer==NULL) {
				mir_free(dataBuffer);
				NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
				return SOCKET_ERROR;
			}
			dataBuffer=newBuffer;
		}
		NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
		if(contentLength>0) break;
		if((contentLength==0)&&(nlc->nlhpi.szHttpGetUrl==NULL))
			break;
		mir_free(dataBuffer);
	}
	if(contentLength<=len) {
		if(flags&MSG_PEEK) {
			nlc->dataBuffer=dataBuffer;
			nlc->dataBufferLen=contentLength;
		}
		CopyMemory(buf,dataBuffer,contentLength);
		if(!(flags&MSG_PEEK)) mir_free(dataBuffer);
		return contentLength;
	}
	CopyMemory(buf,dataBuffer,len);
	if(!(flags&MSG_PEEK)) {
		MoveMemory(dataBuffer,dataBuffer+len,contentLength-len);
		dataBuffer=(PBYTE)mir_realloc(dataBuffer,contentLength-len);
		nlc->dataBufferLen=contentLength-len;
	}
	else nlc->dataBufferLen=contentLength;
	nlc->dataBuffer=dataBuffer;

	Netlib_Logf(nlc->nlu,"%s %d: NetlibHTTPGatewayRecv EXIT!",__FILE__,__LINE__);
	return len;
}

int NetlibInitHttpConnection(struct NetlibConnection *nlc,struct NetlibUser *nlu,NETLIBOPENCONNECTION *nloc)
{
	NETLIBHTTPREQUEST nlhrSend={0},*nlhrReply=NULL;
	NETLIBHTTPHEADER httpHeaders[3];

	nlc->nlhpi.firstGetSequence=nlc->nlhpi.firstPostSequence=1;

        /*
         * Gena01 - ok we set nlhrReply to be null, also if the szHttpGatewayHello is NULL, then
         *          we don't send any requests/replies. We do have a socket open though. Could we
         *          re-use it maybe?
         */
        if (nlu->user.szHttpGatewayHello != NULL) {
	nlhrSend.cbSize=sizeof(nlhrSend);
	nlhrSend.nlc=nlc;
	nlhrSend.requestType=REQUEST_GET;
	nlhrSend.flags=NLHRF_GENERATEHOST|NLHRF_DUMPPROXY|NLHRF_SMARTAUTHHEADER;
	if (nlc->nlhpi.flags & NLHPIF_HTTP11) nlhrSend.flags |= NLHRF_HTTP11;

	nlhrSend.szUrl=nlu->user.szHttpGatewayHello;
	nlhrSend.headers=httpHeaders;
    nlhrSend.headersCount=3;
	httpHeaders[0].szName="User-Agent";
	httpHeaders[0].szValue=nlu->user.szHttpGatewayUserAgent;
    httpHeaders[1].szName="Cache-Control";
    httpHeaders[1].szValue="no-store, no-cache";
    httpHeaders[2].szName="Pragma";
    httpHeaders[2].szValue="no-cache";
	if(NetlibHttpSendRequest((WPARAM)nlc,(LPARAM)&nlhrSend)==SOCKET_ERROR)
		return 0;

	nlhrReply=(NETLIBHTTPREQUEST*)NetlibHttpRecvHeaders((WPARAM)nlc,MSG_DUMPPROXY);
	if(nlhrReply==NULL) return 0;

	if(nlhrReply->resultCode<200 || nlhrReply->resultCode>=300) {
		NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
		NetlibHttpSetLastErrorUsingHttpResult(nlhrReply->resultCode);
		return 0;
	}
        }
	if(!nlu->user.pfnHttpGatewayInit(nlc,nloc,nlhrReply)) {
		NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);
		return 0;
	}
	NetlibHttpFreeRequestStruct(0,(LPARAM)nlhrReply);

	/*
	 * Gena01 - Ok, we should be able to use just POST. Needed for Yahoo, NO GET requests
	 */
	if(nlc->nlhpi.szHttpPostUrl==NULL) {
		SetLastError(ERROR_BAD_FORMAT);
		return 0;
	}
	closesocket(nlc->s);
	nlc->s=INVALID_SOCKET;

	nlc->usingHttpGateway=1;

	/* don't send anything if only using POST? */
	if(nlc->nlhpi.szHttpGetUrl!= NULL)
	if(!HttpGatewaySendGet(nlc))
		return 0;

	//now properly connected
	if(nlu->user.pfnHttpGatewayBegin)
		if(!nlu->user.pfnHttpGatewayBegin(nlc,nloc))
			return 0;
	return 1;
}

int NetlibHttpGatewaySetInfo(WPARAM wParam,LPARAM lParam)
{
	NETLIBHTTPPROXYINFO *nlhpi=(NETLIBHTTPPROXYINFO*)lParam;
	struct NetlibConnection *nlc=(struct NetlibConnection*)wParam;

	if(GetNetlibHandleType(nlc)!=NLH_CONNECTION || nlhpi==NULL || nlhpi->cbSize!=sizeof(NETLIBHTTPPROXYINFO) || nlhpi->szHttpPostUrl==NULL) {
		SetLastError(ERROR_INVALID_PARAMETER);
		return 0;
	}
	if(nlc->nlhpi.szHttpGetUrl) mir_free(nlc->nlhpi.szHttpGetUrl);
	if(nlc->nlhpi.szHttpPostUrl) mir_free(nlc->nlhpi.szHttpPostUrl);
	nlc->nlhpi=*nlhpi;

	if (nlc->nlhpi.szHttpGetUrl)
	nlc->nlhpi.szHttpGetUrl=mir_strdup(nlc->nlhpi.szHttpGetUrl);

	nlc->nlhpi.szHttpPostUrl=mir_strdup(nlc->nlhpi.szHttpPostUrl);
	return 1;
}

int NetlibHttpSetSticky(WPARAM wParam, LPARAM lParam)
{
	struct NetlibUser * nu = (struct NetlibUser*)wParam;
	if (GetNetlibHandleType(nu)!=NLH_USER) return ERROR_INVALID_PARAMETER;
	if (nu->szStickyHeaders) { mir_free(nu->szStickyHeaders); nu->szStickyHeaders=NULL; }
	if (lParam) {
		nu->szStickyHeaders=mir_strdup((char*)lParam); // pointer is ours
	}
	return 0;
}

int NetlibHttpSetPollingTimeout(WPARAM wParam, LPARAM lParam)
{
	int oldTimeout;
	struct NetlibConnection *nlc=(struct NetlibConnection*)wParam;
	if (GetNetlibHandleType(nlc)!=NLH_CONNECTION) return -1;
	oldTimeout = nlc->pollingTimeout;
	nlc->pollingTimeout = lParam;
	return oldTimeout;
}