advapi32: Remove a useless macro.
[wine] / dlls / wininet / http.c
1 /*
2  * Wininet - Http Implementation
3  *
4  * Copyright 1999 Corel Corporation
5  * Copyright 2002 CodeWeavers Inc.
6  * Copyright 2002 TransGaming Technologies Inc.
7  * Copyright 2004 Mike McCormack for CodeWeavers
8  * Copyright 2005 Aric Stewart for CodeWeavers
9  * Copyright 2006 Robert Shearman for CodeWeavers
10  *
11  * Ulrich Czekalla
12  * David Hammerton
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 # include <arpa/inet.h>
38 #endif
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #include <time.h>
46 #include <assert.h>
47
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wininet.h"
51 #include "winreg.h"
52 #include "winerror.h"
53 #define NO_SHLWAPI_STREAM
54 #define NO_SHLWAPI_REG
55 #define NO_SHLWAPI_STRFCNS
56 #define NO_SHLWAPI_GDI
57 #include "shlwapi.h"
58
59 #include "internet.h"
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
62
63 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
64
65 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
66 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
67 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
68 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
69 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
70 static const WCHAR szHost[] = { 'H','o','s','t',0 };
71 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
72 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
73 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
74
75 #define MAXHOSTNAME 100
76 #define MAX_FIELD_VALUE_LEN 256
77 #define MAX_FIELD_LEN 256
78
79 #define HTTP_REFERER    g_szReferer
80 #define HTTP_ACCEPT     g_szAccept
81 #define HTTP_USERAGENT  g_szUserAgent
82
83 #define HTTP_ADDHDR_FLAG_ADD                            0x20000000
84 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW                     0x10000000
85 #define HTTP_ADDHDR_FLAG_COALESCE                       0x40000000
86 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA            0x40000000
87 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON        0x01000000
88 #define HTTP_ADDHDR_FLAG_REPLACE                        0x80000000
89 #define HTTP_ADDHDR_FLAG_REQ                            0x02000000
90
91
92 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
93 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
94 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
95 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
96 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
97 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
98 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
99 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
100 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
101 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
102 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
103                        LPCWSTR username, LPCWSTR password );
104 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
105         dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
106         lpdwIndex);
107 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
108
109
110 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
111 {
112     int HeaderIndex = 0;
113     HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
114     if (HeaderIndex == -1)
115         return NULL;
116     else
117         return &req->pCustHeaders[HeaderIndex];
118 }
119
120 /***********************************************************************
121  *           HTTP_Tokenize (internal)
122  *
123  *  Tokenize a string, allocating memory for the tokens.
124  */
125 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
126 {
127     LPWSTR * token_array;
128     int tokens = 0;
129     int i;
130     LPCWSTR next_token;
131
132     /* empty string has no tokens */
133     if (*string)
134         tokens++;
135     /* count tokens */
136     for (i = 0; string[i]; i++)
137         if (!strncmpW(string+i, token_string, strlenW(token_string)))
138         {
139             DWORD j;
140             tokens++;
141             /* we want to skip over separators, but not the null terminator */
142             for (j = 0; j < strlenW(token_string) - 1; j++)
143                 if (!string[i+j])
144                     break;
145             i += j;
146         }
147
148     /* add 1 for terminating NULL */
149     token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
150     token_array[tokens] = NULL;
151     if (!tokens)
152         return token_array;
153     for (i = 0; i < tokens; i++)
154     {
155         int len;
156         next_token = strstrW(string, token_string);
157         if (!next_token) next_token = string+strlenW(string);
158         len = next_token - string;
159         token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
160         memcpy(token_array[i], string, len*sizeof(WCHAR));
161         token_array[i][len] = '\0';
162         string = next_token+strlenW(token_string);
163     }
164     return token_array;
165 }
166
167 /***********************************************************************
168  *           HTTP_FreeTokens (internal)
169  *
170  *  Frees memory returned from HTTP_Tokenize.
171  */
172 static void HTTP_FreeTokens(LPWSTR * token_array)
173 {
174     int i;
175     for (i = 0; token_array[i]; i++)
176         HeapFree(GetProcessHeap(), 0, token_array[i]);
177     HeapFree(GetProcessHeap(), 0, token_array);
178 }
179
180 /* **********************************************************************
181  * 
182  * Helper functions for the HttpSendRequest(Ex) functions
183  * 
184  */
185 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
186 {
187     struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
188     LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
189
190     TRACE("%p\n", lpwhr);
191
192     HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
193             req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
194             req->dwContentLength, req->bEndRequest);
195
196     HeapFree(GetProcessHeap(), 0, req->lpszHeader);
197 }
198
199 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
200 {
201     /* if the verb is NULL default to GET */
202     if (NULL == lpwhr->lpszVerb)
203     {
204             static const WCHAR szGET[] = { 'G','E','T', 0 };
205             lpwhr->lpszVerb = WININET_strdupW(szGET);
206     }
207 }
208
209 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
210 {
211     static const WCHAR szSlash[] = { '/',0 };
212     static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
213
214     /* If we don't have a path we set it to root */
215     if (NULL == lpwhr->lpszPath)
216         lpwhr->lpszPath = WININET_strdupW(szSlash);
217     else /* remove \r and \n*/
218     {
219         int nLen = strlenW(lpwhr->lpszPath);
220         while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
221         {
222             nLen--;
223             lpwhr->lpszPath[nLen]='\0';
224         }
225         /* Replace '\' with '/' */
226         while (nLen>0) {
227             nLen--;
228             if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
229         }
230     }
231
232     if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
233                        lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
234        && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
235     {
236         WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0, 
237                              (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
238         *fixurl = '/';
239         strcpyW(fixurl + 1, lpwhr->lpszPath);
240         HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
241         lpwhr->lpszPath = fixurl;
242     }
243 }
244
245 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
246 {
247     LPWSTR requestString;
248     DWORD len, n;
249     LPCWSTR *req;
250     INT i;
251     LPWSTR p;
252
253     static const WCHAR szSpace[] = { ' ',0 };
254     static const WCHAR szcrlf[] = {'\r','\n', 0};
255     static const WCHAR szColon[] = { ':',' ',0 };
256     static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
257
258     /* allocate space for an array of all the string pointers to be added */
259     len = (lpwhr->nCustHeaders)*4 + 9;
260     req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
261
262     /* add the verb, path and HTTP version string */
263     n = 0;
264     req[n++] = verb;
265     req[n++] = szSpace;
266     req[n++] = path;
267     req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
268
269     /* Append custom request heades */
270     for (i = 0; i < lpwhr->nCustHeaders; i++)
271     {
272         if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
273         {
274             req[n++] = szcrlf;
275             req[n++] = lpwhr->pCustHeaders[i].lpszField;
276             req[n++] = szColon;
277             req[n++] = lpwhr->pCustHeaders[i].lpszValue;
278
279             TRACE("Adding custom header %s (%s)\n",
280                    debugstr_w(lpwhr->pCustHeaders[i].lpszField),
281                    debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
282         }
283     }
284
285     if( n >= len )
286         ERR("oops. buffer overrun\n");
287
288     req[n] = NULL;
289     requestString = HTTP_build_req( req, 4 );
290     HeapFree( GetProcessHeap(), 0, req );
291
292     /*
293      * Set (header) termination string for request
294      * Make sure there's exactly two new lines at the end of the request
295      */
296     p = &requestString[strlenW(requestString)-1];
297     while ( (*p == '\n') || (*p == '\r') )
298        p--;
299     strcpyW( p+1, sztwocrlf );
300     
301     return requestString;
302 }
303
304 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
305 {
306     static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
307     int HeaderIndex;
308     LPHTTPHEADERW setCookieHeader;
309
310     HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
311     if (HeaderIndex == -1)
312             return;
313     setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
314
315     if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
316     {
317         int nPosStart = 0, nPosEnd = 0, len;
318         static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
319
320         while (setCookieHeader->lpszValue[nPosEnd] != '\0')
321         {
322             LPWSTR buf_cookie, cookie_name, cookie_data;
323             LPWSTR buf_url;
324             LPWSTR domain = NULL;
325             LPHTTPHEADERW Host;
326
327             int nEqualPos = 0;
328             while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
329                    setCookieHeader->lpszValue[nPosEnd] != '\0')
330             {
331                 nPosEnd++;
332             }
333             if (setCookieHeader->lpszValue[nPosEnd] == ';')
334             {
335                 /* fixme: not case sensitive, strcasestr is gnu only */
336                 int nDomainPosEnd = 0;
337                 int nDomainPosStart = 0, nDomainLength = 0;
338                 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
339                 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
340                 if (lpszDomain)
341                 { /* they have specified their own domain, lets use it */
342                     while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
343                            lpszDomain[nDomainPosEnd] != '\0')
344                     {
345                         nDomainPosEnd++;
346                     }
347                     nDomainPosStart = strlenW(szDomain);
348                     nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
349                     domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
350                     lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
351                 }
352             }
353             if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
354             buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
355             lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
356             TRACE("%s\n", debugstr_w(buf_cookie));
357             while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
358             {
359                 nEqualPos++;
360             }
361             if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
362             {
363                 HeapFree(GetProcessHeap(), 0, buf_cookie);
364                 break;
365             }
366
367             cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
368             lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
369             cookie_data = &buf_cookie[nEqualPos + 1];
370
371             Host = HTTP_GetHeader(lpwhr,szHost);
372             len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) + 
373                 strlenW(lpwhr->lpszPath) + 9;
374             buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
375             sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
376             InternetSetCookieW(buf_url, cookie_name, cookie_data);
377
378             HeapFree(GetProcessHeap(), 0, buf_url);
379             HeapFree(GetProcessHeap(), 0, buf_cookie);
380             HeapFree(GetProcessHeap(), 0, cookie_name);
381             HeapFree(GetProcessHeap(), 0, domain);
382             nPosStart = nPosEnd;
383         }
384     }
385 }
386
387 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
388 {
389     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
390     LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
391
392     assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
393     assert(hIC->hdr.htype == WH_HINIT);
394
395     if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
396         HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
397                 hIC->lpszProxyPassword);
398 }
399
400 /***********************************************************************
401  *           HTTP_HttpAddRequestHeadersW (internal)
402  */
403 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
404         LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
405 {
406     LPWSTR lpszStart;
407     LPWSTR lpszEnd;
408     LPWSTR buffer;
409     BOOL bSuccess = FALSE;
410     DWORD len;
411
412     TRACE("copying header: %s\n", debugstr_w(lpszHeader));
413
414     if( dwHeaderLength == ~0U )
415         len = strlenW(lpszHeader);
416     else
417         len = dwHeaderLength;
418     buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
419     lstrcpynW( buffer, lpszHeader, len + 1);
420
421     lpszStart = buffer;
422
423     do
424     {
425         LPWSTR * pFieldAndValue;
426
427         lpszEnd = lpszStart;
428
429         while (*lpszEnd != '\0')
430         {
431             if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
432                  break;
433             lpszEnd++;
434         }
435
436         if (*lpszStart == '\0')
437             break;
438
439         if (*lpszEnd == '\r')
440         {
441             *lpszEnd = '\0';
442             lpszEnd += 2; /* Jump over \r\n */
443         }
444         TRACE("interpreting header %s\n", debugstr_w(lpszStart));
445         pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
446         if (pFieldAndValue)
447         {
448             bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
449                 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
450             HTTP_FreeTokens(pFieldAndValue);
451         }
452
453         lpszStart = lpszEnd;
454     } while (bSuccess);
455
456     HeapFree(GetProcessHeap(), 0, buffer);
457
458     return bSuccess;
459 }
460
461 /***********************************************************************
462  *           HttpAddRequestHeadersW (WININET.@)
463  *
464  * Adds one or more HTTP header to the request handler
465  *
466  * RETURNS
467  *    TRUE  on success
468  *    FALSE on failure
469  *
470  */
471 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
472         LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
473 {
474     BOOL bSuccess = FALSE;
475     LPWININETHTTPREQW lpwhr;
476
477     TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
478           dwModifier);
479
480     if (!lpszHeader) 
481       return TRUE;
482
483     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
484     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
485     {
486         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
487         goto lend;
488     }
489     bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
490 lend:
491     if( lpwhr )
492         WININET_Release( &lpwhr->hdr );
493
494     return bSuccess;
495 }
496
497 /***********************************************************************
498  *           HttpAddRequestHeadersA (WININET.@)
499  *
500  * Adds one or more HTTP header to the request handler
501  *
502  * RETURNS
503  *    TRUE  on success
504  *    FALSE on failure
505  *
506  */
507 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
508         LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
509 {
510     DWORD len;
511     LPWSTR hdr;
512     BOOL r;
513
514     TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
515           dwModifier);
516
517     len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
518     hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
519     MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
520     if( dwHeaderLength != ~0U )
521         dwHeaderLength = len;
522
523     r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
524
525     HeapFree( GetProcessHeap(), 0, hdr );
526
527     return r;
528 }
529
530 /* read any content returned by the server so that the connection can be
531  * resued */
532 static void HTTP_DrainContent(LPWININETHTTPREQW lpwhr)
533 {
534     DWORD bytes_read;
535     do
536     {
537         char buffer[2048];
538         if (!INTERNET_ReadFile(&lpwhr->hdr, buffer, sizeof(buffer), &bytes_read,
539                                TRUE, FALSE))
540             return;
541     } while (bytes_read);
542 }
543
544 /***********************************************************************
545  *           HttpEndRequestA (WININET.@)
546  *
547  * Ends an HTTP request that was started by HttpSendRequestEx
548  *
549  * RETURNS
550  *    TRUE      if successful
551  *    FALSE     on failure
552  *
553  */
554 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, 
555         LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
556 {
557     LPINTERNET_BUFFERSA ptr;
558     LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
559     BOOL rc = FALSE;
560
561     TRACE("(%p, %p, %08x, %08x): stub\n", hRequest, lpBuffersOut, dwFlags,
562             dwContext);
563
564     ptr = lpBuffersOut;
565     if (ptr)
566         lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
567                 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
568     else
569         lpBuffersOutW = NULL;
570
571     ptrW = lpBuffersOutW;
572     while (ptr)
573     {
574         if (ptr->lpvBuffer && ptr->dwBufferLength)
575             ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
576         ptrW->dwBufferLength = ptr->dwBufferLength;
577         ptrW->dwBufferTotal= ptr->dwBufferTotal;
578
579         if (ptr->Next)
580             ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
581                     sizeof(INTERNET_BUFFERSW));
582
583         ptr = ptr->Next;
584         ptrW = ptrW->Next;
585     }
586
587     rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
588
589     if (lpBuffersOutW)
590     {
591         ptrW = lpBuffersOutW;
592         while (ptrW)
593         {
594             LPINTERNET_BUFFERSW ptrW2;
595
596             FIXME("Do we need to translate info out of these buffer?\n");
597
598             HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
599             ptrW2 = ptrW->Next;
600             HeapFree(GetProcessHeap(),0,ptrW);
601             ptrW = ptrW2;
602         }
603     }
604
605     return rc;
606 }
607
608 /***********************************************************************
609  *           HttpEndRequestW (WININET.@)
610  *
611  * Ends an HTTP request that was started by HttpSendRequestEx
612  *
613  * RETURNS
614  *    TRUE      if successful
615  *    FALSE     on failure
616  *
617  */
618 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, 
619         LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
620 {
621     BOOL rc = FALSE;
622     LPWININETHTTPREQW lpwhr;
623     INT responseLen;
624     DWORD dwBufferSize;
625
626     TRACE("-->\n");
627     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
628
629     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
630     {
631         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
632         return FALSE;
633     }
634
635     lpwhr->hdr.dwFlags |= dwFlags;
636     lpwhr->hdr.dwContext = dwContext;
637
638     /* We appear to do nothing with lpBuffersOut.. is that correct? */
639
640     SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
641             INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
642
643     responseLen = HTTP_GetResponseHeaders(lpwhr);
644     if (responseLen)
645             rc = TRUE;
646
647     SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
648             INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
649
650     /* process headers here. Is this right? */
651     HTTP_ProcessHeaders(lpwhr);
652
653     dwBufferSize = sizeof(lpwhr->dwContentLength);
654     if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
655                              &lpwhr->dwContentLength,&dwBufferSize,NULL))
656         lpwhr->dwContentLength = -1;
657
658     if (lpwhr->dwContentLength == 0)
659         HTTP_FinishedReading(lpwhr);
660
661     if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
662     {
663         DWORD dwCode,dwCodeLength=sizeof(DWORD);
664         if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
665             (dwCode==302 || dwCode==301))
666         {
667             WCHAR szNewLocation[2048];
668             dwBufferSize=2048;
669             if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
670             {
671                     static const WCHAR szGET[] = { 'G','E','T', 0 };
672                 /* redirects are always GETs */
673                 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
674                     lpwhr->lpszVerb = WININET_strdupW(szGET);
675                 HTTP_DrainContent(lpwhr);
676                 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
677                 if (rc)
678                     rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
679             }
680         }
681     }
682
683     TRACE("%i <--\n",rc);
684     return rc;
685 }
686
687 /***********************************************************************
688  *           HttpOpenRequestW (WININET.@)
689  *
690  * Open a HTTP request handle
691  *
692  * RETURNS
693  *    HINTERNET  a HTTP request handle on success
694  *    NULL       on failure
695  *
696  */
697 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
698         LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
699         LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
700         DWORD dwFlags, DWORD dwContext)
701 {
702     LPWININETHTTPSESSIONW lpwhs;
703     HINTERNET handle = NULL;
704
705     TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession,
706           debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
707           debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
708           dwFlags, dwContext);
709     if(lpszAcceptTypes!=NULL)
710     {
711         int i;
712         for(i=0;lpszAcceptTypes[i]!=NULL;i++)
713             TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
714     }    
715
716     lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
717     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
718     {
719         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
720         goto lend;
721     }
722
723     /*
724      * My tests seem to show that the windows version does not
725      * become asynchronous until after this point. And anyhow
726      * if this call was asynchronous then how would you get the
727      * necessary HINTERNET pointer returned by this function.
728      *
729      */
730     handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
731                                    lpszVersion, lpszReferrer, lpszAcceptTypes,
732                                    dwFlags, dwContext);
733 lend:
734     if( lpwhs )
735         WININET_Release( &lpwhs->hdr );
736     TRACE("returning %p\n", handle);
737     return handle;
738 }
739
740
741 /***********************************************************************
742  *           HttpOpenRequestA (WININET.@)
743  *
744  * Open a HTTP request handle
745  *
746  * RETURNS
747  *    HINTERNET  a HTTP request handle on success
748  *    NULL       on failure
749  *
750  */
751 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
752         LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
753         LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
754         DWORD dwFlags, DWORD dwContext)
755 {
756     LPWSTR szVerb = NULL, szObjectName = NULL;
757     LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
758     INT len;
759     INT acceptTypesCount;
760     HINTERNET rc = FALSE;
761     TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession,
762           debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
763           debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
764           dwFlags, dwContext);
765
766     if (lpszVerb)
767     {
768         len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
769         szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
770         if ( !szVerb )
771             goto end;
772         MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
773     }
774
775     if (lpszObjectName)
776     {
777         len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
778         szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
779         if ( !szObjectName )
780             goto end;
781         MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
782     }
783
784     if (lpszVersion)
785     {
786         len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
787         szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
788         if ( !szVersion )
789             goto end;
790         MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
791     }
792
793     if (lpszReferrer)
794     {
795         len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
796         szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
797         if ( !szReferrer )
798             goto end;
799         MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
800     }
801
802     acceptTypesCount = 0;
803     if (lpszAcceptTypes)
804     {
805         /* find out how many there are */
806         while (lpszAcceptTypes[acceptTypesCount]) 
807             acceptTypesCount++;
808         szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
809         acceptTypesCount = 0;
810         while (lpszAcceptTypes[acceptTypesCount])
811         {
812             len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
813                                 -1, NULL, 0 );
814             szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
815             if (!szAcceptTypes[acceptTypesCount] )
816                 goto end;
817             MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
818                                 -1, szAcceptTypes[acceptTypesCount], len );
819             acceptTypesCount++;
820         }
821         szAcceptTypes[acceptTypesCount] = NULL;
822     }
823     else szAcceptTypes = 0;
824
825     rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
826                           szVersion, szReferrer,
827                           (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
828
829 end:
830     if (szAcceptTypes)
831     {
832         acceptTypesCount = 0;
833         while (szAcceptTypes[acceptTypesCount])
834         {
835             HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
836             acceptTypesCount++;
837         }
838         HeapFree(GetProcessHeap(), 0, szAcceptTypes);
839     }
840     HeapFree(GetProcessHeap(), 0, szReferrer);
841     HeapFree(GetProcessHeap(), 0, szVersion);
842     HeapFree(GetProcessHeap(), 0, szObjectName);
843     HeapFree(GetProcessHeap(), 0, szVerb);
844
845     return rc;
846 }
847
848 /***********************************************************************
849  *  HTTP_Base64
850  */
851 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
852 {
853     UINT n = 0, x;
854     static LPCSTR HTTP_Base64Enc = 
855         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
856
857     while( bin[0] )
858     {
859         /* first 6 bits, all from bin[0] */
860         base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
861         x = (bin[0] & 3) << 4;
862
863         /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
864         if( !bin[1] )
865         {
866             base64[n++] = HTTP_Base64Enc[x];
867             base64[n++] = '=';
868             base64[n++] = '=';
869             break;
870         }
871         base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
872         x = ( bin[1] & 0x0f ) << 2;
873
874         /* next 6 bits 4 from bin[1] and 2 from bin[2] */
875         if( !bin[2] )
876         {
877             base64[n++] = HTTP_Base64Enc[x];
878             base64[n++] = '=';
879             break;
880         }
881         base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
882
883         /* last 6 bits, all from bin [2] */
884         base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
885         bin += 3;
886     }
887     base64[n] = 0;
888     return n;
889 }
890
891 /***********************************************************************
892  *  HTTP_EncodeBasicAuth
893  *
894  *  Encode the basic authentication string for HTTP 1.1
895  */
896 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
897 {
898     UINT len;
899     LPWSTR in, out;
900     static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
901     static const WCHAR szColon[] = {':',0};
902
903     len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
904     in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
905     if( !in )
906         return NULL;
907
908     len = lstrlenW(szBasic) +
909           (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
910     out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
911     if( out )
912     {
913         lstrcpyW( in, username );
914         lstrcatW( in, szColon );
915         lstrcatW( in, password );
916         lstrcpyW( out, szBasic );
917         HTTP_Base64( in, &out[strlenW(out)] );
918     }
919     HeapFree( GetProcessHeap(), 0, in );
920
921     return out;
922 }
923
924 /***********************************************************************
925  *  HTTP_InsertProxyAuthorization
926  *
927  *   Insert the basic authorization field in the request header
928  */
929 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
930                        LPCWSTR username, LPCWSTR password )
931 {
932     WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
933     BOOL ret = TRUE;
934
935     if (!authorization)
936         return FALSE;
937
938     TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
939
940     HTTP_ProcessHeader(lpwhr, szProxy_Authorization, authorization,
941             HTTP_ADDHDR_FLAG_REPLACE);
942
943     HeapFree( GetProcessHeap(), 0, authorization );
944     
945     return ret;
946 }
947
948 /***********************************************************************
949  *           HTTP_DealWithProxy
950  */
951 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
952     LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
953 {
954     WCHAR buf[MAXHOSTNAME];
955     WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
956     WCHAR* url;
957     static WCHAR szNul[] = { 0 };
958     URL_COMPONENTSW UrlComponents;
959     static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
960     static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
961     static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
962     int len;
963
964     memset( &UrlComponents, 0, sizeof UrlComponents );
965     UrlComponents.dwStructSize = sizeof UrlComponents;
966     UrlComponents.lpszHostName = buf;
967     UrlComponents.dwHostNameLength = MAXHOSTNAME;
968
969     if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
970                                  hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
971         sprintfW(proxy, szFormat1, hIC->lpszProxy);
972     else
973         strcpyW(proxy, hIC->lpszProxy);
974     if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
975         return FALSE;
976     if( UrlComponents.dwHostNameLength == 0 )
977         return FALSE;
978
979     if( !lpwhr->lpszPath )
980         lpwhr->lpszPath = szNul;
981     TRACE("server='%s' path='%s'\n",
982           debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
983     /* for constant 15 see above */
984     len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
985     url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
986
987     if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
988         UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
989
990     sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
991
992     if( lpwhr->lpszPath[0] != '/' )
993         strcatW( url, szSlash );
994     strcatW(url, lpwhr->lpszPath);
995     if(lpwhr->lpszPath != szNul)
996         HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
997     lpwhr->lpszPath = url;
998
999     HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1000     lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1001     lpwhs->nServerPort = UrlComponents.nPort;
1002
1003     return TRUE;
1004 }
1005
1006 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1007 {
1008     char szaddr[32];
1009     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1010
1011     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1012                           INTERNET_STATUS_RESOLVING_NAME,
1013                           lpwhs->lpszServerName,
1014                           strlenW(lpwhs->lpszServerName)+1);
1015
1016     if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1017                     &lpwhs->socketAddress))
1018     {
1019         INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1020         return FALSE;
1021     }
1022
1023     inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1024               szaddr, sizeof(szaddr));
1025     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1026                           INTERNET_STATUS_NAME_RESOLVED,
1027                           szaddr, strlen(szaddr)+1);
1028     return TRUE;
1029 }
1030
1031 /***********************************************************************
1032  *           HTTP_HttpOpenRequestW (internal)
1033  *
1034  * Open a HTTP request handle
1035  *
1036  * RETURNS
1037  *    HINTERNET  a HTTP request handle on success
1038  *    NULL       on failure
1039  *
1040  */
1041 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1042         LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1043         LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1044         DWORD dwFlags, DWORD dwContext)
1045 {
1046     LPWININETAPPINFOW hIC = NULL;
1047     LPWININETHTTPREQW lpwhr;
1048     LPWSTR lpszCookies;
1049     LPWSTR lpszUrl = NULL;
1050     DWORD nCookieSize;
1051     HINTERNET handle = NULL;
1052     static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
1053     DWORD len;
1054     LPHTTPHEADERW Host;
1055
1056     TRACE("-->\n");
1057
1058     assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1059     hIC = lpwhs->lpAppInfo;
1060
1061     lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1062     if (NULL == lpwhr)
1063     {
1064         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1065         goto lend;
1066     }
1067     lpwhr->hdr.htype = WH_HHTTPREQ;
1068     lpwhr->hdr.dwFlags = dwFlags;
1069     lpwhr->hdr.dwContext = dwContext;
1070     lpwhr->hdr.dwRefCount = 1;
1071     lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1072     lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1073     lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
1074
1075     WININET_AddRef( &lpwhs->hdr );
1076     lpwhr->lpHttpSession = lpwhs;
1077
1078     handle = WININET_AllocHandle( &lpwhr->hdr );
1079     if (NULL == handle)
1080     {
1081         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1082         goto lend;
1083     }
1084
1085     if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1086     {
1087         InternetCloseHandle( handle );
1088         handle = NULL;
1089         goto lend;
1090     }
1091
1092     if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1093         HRESULT rc;
1094
1095         len = 0;
1096         rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1097         if (rc != E_POINTER)
1098             len = strlenW(lpszObjectName)+1;
1099         lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1100         rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1101                    URL_ESCAPE_SPACES_ONLY);
1102         if (rc)
1103         {
1104             ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
1105             strcpyW(lpwhr->lpszPath,lpszObjectName);
1106         }
1107     }
1108
1109     if (NULL != lpszReferrer && strlenW(lpszReferrer))
1110         HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1111
1112     if(lpszAcceptTypes!=NULL)
1113     {
1114         int i;
1115         for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1116             HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1117     }
1118
1119     if (NULL == lpszVerb)
1120     {
1121         static const WCHAR szGet[] = {'G','E','T',0};
1122         lpwhr->lpszVerb = WININET_strdupW(szGet);
1123     }
1124     else if (strlenW(lpszVerb))
1125         lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1126
1127     if (NULL != lpszReferrer && strlenW(lpszReferrer))
1128     {
1129         WCHAR buf[MAXHOSTNAME];
1130         URL_COMPONENTSW UrlComponents;
1131
1132         memset( &UrlComponents, 0, sizeof UrlComponents );
1133         UrlComponents.dwStructSize = sizeof UrlComponents;
1134         UrlComponents.lpszHostName = buf;
1135         UrlComponents.dwHostNameLength = MAXHOSTNAME;
1136
1137         InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1138         if (strlenW(UrlComponents.lpszHostName))
1139             HTTP_ProcessHeader(lpwhr, szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1140     }
1141     else
1142         HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1143
1144     if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1145         lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1146                         INTERNET_DEFAULT_HTTPS_PORT :
1147                         INTERNET_DEFAULT_HTTP_PORT);
1148     lpwhs->nHostPort = lpwhs->nServerPort;
1149
1150     if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1151         HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1152
1153     if (hIC->lpszAgent)
1154     {
1155         WCHAR *agent_header;
1156         static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1157
1158         len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1159         agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1160         sprintfW(agent_header, user_agent, hIC->lpszAgent );
1161
1162         HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1163                                HTTP_ADDREQ_FLAG_ADD);
1164         HeapFree(GetProcessHeap(), 0, agent_header);
1165     }
1166
1167     Host = HTTP_GetHeader(lpwhr,szHost);
1168
1169     len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1170     lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1171     sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1172
1173     if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1174         InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1175     {
1176         int cnt = 0;
1177         static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1178         static const WCHAR szcrlf[] = {'\r','\n',0};
1179
1180         lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1181
1182         cnt += sprintfW(lpszCookies, szCookie);
1183         InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1184         strcatW(lpszCookies, szcrlf);
1185
1186         HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1187                                HTTP_ADDREQ_FLAG_ADD);
1188         HeapFree(GetProcessHeap(), 0, lpszCookies);
1189     }
1190     HeapFree(GetProcessHeap(), 0, lpszUrl);
1191
1192
1193     INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1194                           INTERNET_STATUS_HANDLE_CREATED, &handle,
1195                           sizeof(handle));
1196
1197     /*
1198      * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1199      */
1200
1201     if (!HTTP_ResolveName(lpwhr))
1202     {
1203         InternetCloseHandle( handle );
1204         handle = NULL;
1205     }
1206
1207 lend:
1208     if( lpwhr )
1209         WININET_Release( &lpwhr->hdr );
1210
1211     TRACE("<-- %p (%p)\n", handle, lpwhr);
1212     return handle;
1213 }
1214
1215 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1216 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1217 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1218 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1219 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1220 static const WCHAR szAge[] = { 'A','g','e',0 };
1221 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1222 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1223 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1224 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1225 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1226 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1227 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1228 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1229 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1230 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1231 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1232 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1233 static const WCHAR szContent_Transfer_Encoding[] = { 'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1234 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1235 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1236 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1237 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1238 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1239 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1240 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1241 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1242 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1243 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1244 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1245 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1246 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1247 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1248 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1249 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1250 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1251 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1252 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1253 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1254 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1255 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1256 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1257 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1258 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1259 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1260 static const WCHAR szUnless_Modified_Since[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1261 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1262 static const WCHAR szURI[] = { 'U','R','I',0 };
1263 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1264 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1265 static const WCHAR szVia[] = { 'V','i','a',0 };
1266 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1267 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1268
1269 static const LPCWSTR header_lookup[] = {
1270     szMime_Version,             /* HTTP_QUERY_MIME_VERSION = 0 */
1271     szContent_Type,             /* HTTP_QUERY_CONTENT_TYPE = 1 */
1272     szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1273     szContent_ID,               /* HTTP_QUERY_CONTENT_ID = 3 */
1274     NULL,                       /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1275     szContent_Length,           /* HTTP_QUERY_CONTENT_LENGTH =  5 */
1276     szContent_Language,         /* HTTP_QUERY_CONTENT_LANGUAGE =  6 */
1277     szAllow,                    /* HTTP_QUERY_ALLOW = 7 */
1278     szPublic,                   /* HTTP_QUERY_PUBLIC = 8 */
1279     szDate,                     /* HTTP_QUERY_DATE = 9 */
1280     szExpires,                  /* HTTP_QUERY_EXPIRES = 10 */
1281     szLast_Modified,            /* HTTP_QUERY_LAST_MODIFIED = 11 */
1282     NULL,                       /* HTTP_QUERY_MESSAGE_ID = 12 */
1283     szURI,                      /* HTTP_QUERY_URI = 13 */
1284     szFrom,                     /* HTTP_QUERY_DERIVED_FROM = 14 */
1285     NULL,                       /* HTTP_QUERY_COST = 15 */
1286     NULL,                       /* HTTP_QUERY_LINK = 16 */
1287     szPragma,                   /* HTTP_QUERY_PRAGMA = 17 */
1288     NULL,                       /* HTTP_QUERY_VERSION = 18 */
1289     szStatus,                   /* HTTP_QUERY_STATUS_CODE = 19 */
1290     NULL,                       /* HTTP_QUERY_STATUS_TEXT = 20 */
1291     NULL,                       /* HTTP_QUERY_RAW_HEADERS = 21 */
1292     NULL,                       /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
1293     szConnection,               /* HTTP_QUERY_CONNECTION = 23 */
1294     szAccept,                   /* HTTP_QUERY_ACCEPT = 24 */
1295     szAccept_Charset,           /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
1296     szAccept_Encoding,          /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
1297     szAccept_Language,          /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
1298     szAuthorization,            /* HTTP_QUERY_AUTHORIZATION = 28 */
1299     szContent_Encoding,         /* HTTP_QUERY_CONTENT_ENCODING = 29 */
1300     NULL,                       /* HTTP_QUERY_FORWARDED = 30 */
1301     NULL,                       /* HTTP_QUERY_FROM = 31 */
1302     szIf_Modified_Since,        /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
1303     szLocation,                 /* HTTP_QUERY_LOCATION = 33 */
1304     NULL,                       /* HTTP_QUERY_ORIG_URI = 34 */
1305     szReferer,                  /* HTTP_QUERY_REFERER = 35 */
1306     szRetry_After,              /* HTTP_QUERY_RETRY_AFTER = 36 */
1307     szServer,                   /* HTTP_QUERY_SERVER = 37 */
1308     NULL,                       /* HTTP_TITLE = 38 */
1309     szUser_Agent,               /* HTTP_QUERY_USER_AGENT = 39 */
1310     szWWW_Authenticate,         /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
1311     szProxy_Authenticate,       /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
1312     szAccept_Ranges,            /* HTTP_QUERY_ACCEPT_RANGES = 42 */
1313     szSet_Cookie,               /* HTTP_QUERY_SET_COOKIE = 43 */
1314     szCookie,                   /* HTTP_QUERY_COOKIE = 44 */
1315     NULL,                       /* HTTP_QUERY_REQUEST_METHOD = 45 */
1316     NULL,                       /* HTTP_QUERY_REFRESH = 46 */
1317     NULL,                       /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
1318     szAge,                      /* HTTP_QUERY_AGE = 48 */
1319     szCache_Control,            /* HTTP_QUERY_CACHE_CONTROL = 49 */
1320     szContent_Base,             /* HTTP_QUERY_CONTENT_BASE = 50 */
1321     szContent_Location,         /* HTTP_QUERY_CONTENT_LOCATION = 51 */
1322     szContent_MD5,              /* HTTP_QUERY_CONTENT_MD5 = 52 */
1323     szContent_Range,            /* HTTP_QUERY_CONTENT_RANGE = 53 */
1324     szETag,                     /* HTTP_QUERY_ETAG = 54 */
1325     szHost,                     /* HTTP_QUERY_HOST = 55 */
1326     szIf_Match,                 /* HTTP_QUERY_IF_MATCH = 56 */
1327     szIf_None_Match,            /* HTTP_QUERY_IF_NONE_MATCH = 57 */
1328     szIf_Range,                 /* HTTP_QUERY_IF_RANGE = 58 */
1329     szIf_Unmodified_Since,      /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
1330     szMax_Forwards,             /* HTTP_QUERY_MAX_FORWARDS = 60 */
1331     szProxy_Authorization,      /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
1332     szRange,                    /* HTTP_QUERY_RANGE = 62 */
1333     szTransfer_Encoding,        /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
1334     szUpgrade,                  /* HTTP_QUERY_UPGRADE = 64 */
1335     szVary,                     /* HTTP_QUERY_VARY = 65 */
1336     szVia,                      /* HTTP_QUERY_VIA = 66 */
1337     szWarning,                  /* HTTP_QUERY_WARNING = 67 */
1338     szExpect,                   /* HTTP_QUERY_EXPECT = 68 */
1339     szProxy_Connection,         /* HTTP_QUERY_PROXY_CONNECTION = 69 */
1340     szUnless_Modified_Since,    /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
1341 };
1342
1343 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
1344
1345 /***********************************************************************
1346  *           HTTP_HttpQueryInfoW (internal)
1347  */
1348 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1349         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1350 {
1351     LPHTTPHEADERW lphttpHdr = NULL;
1352     BOOL bSuccess = FALSE;
1353     BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1354     INT requested_index = lpdwIndex ? *lpdwIndex : 0;
1355     INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
1356     INT index = -1;
1357
1358     /* Find requested header structure */
1359     switch (level)
1360     {
1361     case HTTP_QUERY_CUSTOM:
1362         index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
1363         break;
1364
1365     case HTTP_QUERY_RAW_HEADERS_CRLF:
1366         {
1367             DWORD len = strlenW(lpwhr->lpszRawHeaders);
1368             if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1369             {
1370                 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1371                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1372                 return FALSE;
1373             }
1374             memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1375             *lpdwBufferLength = len * sizeof(WCHAR);
1376
1377             TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1378
1379             return TRUE;
1380         }
1381     case HTTP_QUERY_RAW_HEADERS:
1382         {
1383             static const WCHAR szCrLf[] = {'\r','\n',0};
1384             LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1385             DWORD i, size = 0;
1386             LPWSTR pszString = (WCHAR*)lpBuffer;
1387
1388             for (i = 0; ppszRawHeaderLines[i]; i++)
1389                 size += strlenW(ppszRawHeaderLines[i]) + 1;
1390
1391             if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1392             {
1393                 HTTP_FreeTokens(ppszRawHeaderLines);
1394                 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1395                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1396                 return FALSE;
1397             }
1398
1399             for (i = 0; ppszRawHeaderLines[i]; i++)
1400             {
1401                 DWORD len = strlenW(ppszRawHeaderLines[i]);
1402                 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1403                 pszString += len+1;
1404             }
1405             *pszString = '\0';
1406
1407             TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1408
1409             *lpdwBufferLength = size * sizeof(WCHAR);
1410             HTTP_FreeTokens(ppszRawHeaderLines);
1411
1412             return TRUE;
1413         }
1414     case HTTP_QUERY_STATUS_TEXT:
1415         if (lpwhr->lpszStatusText)
1416         {
1417             DWORD len = strlenW(lpwhr->lpszStatusText);
1418             if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1419             {
1420                 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1421                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1422                 return FALSE;
1423             }
1424             memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1425             *lpdwBufferLength = len * sizeof(WCHAR);
1426
1427             TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1428
1429             return TRUE;
1430         }
1431         break;
1432     case HTTP_QUERY_VERSION:
1433         if (lpwhr->lpszVersion)
1434         {
1435             DWORD len = strlenW(lpwhr->lpszVersion);
1436             if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1437             {
1438                 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1439                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1440                 return FALSE;
1441             }
1442             memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1443             *lpdwBufferLength = len * sizeof(WCHAR);
1444
1445             TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1446
1447             return TRUE;
1448         }
1449         break;
1450     default:
1451         assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
1452
1453         if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
1454             index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
1455                                               requested_index,request_only);
1456     }
1457
1458     if (index >= 0)
1459         lphttpHdr = &lpwhr->pCustHeaders[index];
1460
1461     /* Ensure header satisifies requested attributes */
1462     if (!lphttpHdr ||
1463         ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1464          (~lphttpHdr->wFlags & HDR_ISREQUEST)))
1465     {
1466         SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1467         return bSuccess;
1468     }
1469
1470     if (lpdwIndex)
1471         (*lpdwIndex)++;
1472
1473     /* coalesce value to reuqested type */
1474     if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1475     {
1476         *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1477         bSuccess = TRUE;
1478
1479         TRACE(" returning number : %d\n", *(int *)lpBuffer);
1480     }
1481     else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1482     {
1483         time_t tmpTime;
1484         struct tm tmpTM;
1485         SYSTEMTIME *STHook;
1486
1487         tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1488
1489         tmpTM = *gmtime(&tmpTime);
1490         STHook = (SYSTEMTIME *) lpBuffer;
1491         if(STHook==NULL)
1492             return bSuccess;
1493
1494         STHook->wDay = tmpTM.tm_mday;
1495         STHook->wHour = tmpTM.tm_hour;
1496         STHook->wMilliseconds = 0;
1497         STHook->wMinute = tmpTM.tm_min;
1498         STHook->wDayOfWeek = tmpTM.tm_wday;
1499         STHook->wMonth = tmpTM.tm_mon + 1;
1500         STHook->wSecond = tmpTM.tm_sec;
1501         STHook->wYear = tmpTM.tm_year;
1502         
1503         bSuccess = TRUE;
1504         
1505         TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n", 
1506               STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1507               STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1508     }
1509     else if (lphttpHdr->lpszValue)
1510     {
1511         DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1512
1513         if (len > *lpdwBufferLength)
1514         {
1515             *lpdwBufferLength = len;
1516             INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1517             return bSuccess;
1518         }
1519
1520         memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1521         *lpdwBufferLength = len - sizeof(WCHAR);
1522         bSuccess = TRUE;
1523
1524         TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1525     }
1526     return bSuccess;
1527 }
1528
1529 /***********************************************************************
1530  *           HttpQueryInfoW (WININET.@)
1531  *
1532  * Queries for information about an HTTP request
1533  *
1534  * RETURNS
1535  *    TRUE  on success
1536  *    FALSE on failure
1537  *
1538  */
1539 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1540         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1541 {
1542     BOOL bSuccess = FALSE;
1543     LPWININETHTTPREQW lpwhr;
1544
1545     if (TRACE_ON(wininet)) {
1546 #define FE(x) { x, #x }
1547         static const wininet_flag_info query_flags[] = {
1548             FE(HTTP_QUERY_MIME_VERSION),
1549             FE(HTTP_QUERY_CONTENT_TYPE),
1550             FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1551             FE(HTTP_QUERY_CONTENT_ID),
1552             FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1553             FE(HTTP_QUERY_CONTENT_LENGTH),
1554             FE(HTTP_QUERY_CONTENT_LANGUAGE),
1555             FE(HTTP_QUERY_ALLOW),
1556             FE(HTTP_QUERY_PUBLIC),
1557             FE(HTTP_QUERY_DATE),
1558             FE(HTTP_QUERY_EXPIRES),
1559             FE(HTTP_QUERY_LAST_MODIFIED),
1560             FE(HTTP_QUERY_MESSAGE_ID),
1561             FE(HTTP_QUERY_URI),
1562             FE(HTTP_QUERY_DERIVED_FROM),
1563             FE(HTTP_QUERY_COST),
1564             FE(HTTP_QUERY_LINK),
1565             FE(HTTP_QUERY_PRAGMA),
1566             FE(HTTP_QUERY_VERSION),
1567             FE(HTTP_QUERY_STATUS_CODE),
1568             FE(HTTP_QUERY_STATUS_TEXT),
1569             FE(HTTP_QUERY_RAW_HEADERS),
1570             FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1571             FE(HTTP_QUERY_CONNECTION),
1572             FE(HTTP_QUERY_ACCEPT),
1573             FE(HTTP_QUERY_ACCEPT_CHARSET),
1574             FE(HTTP_QUERY_ACCEPT_ENCODING),
1575             FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1576             FE(HTTP_QUERY_AUTHORIZATION),
1577             FE(HTTP_QUERY_CONTENT_ENCODING),
1578             FE(HTTP_QUERY_FORWARDED),
1579             FE(HTTP_QUERY_FROM),
1580             FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1581             FE(HTTP_QUERY_LOCATION),
1582             FE(HTTP_QUERY_ORIG_URI),
1583             FE(HTTP_QUERY_REFERER),
1584             FE(HTTP_QUERY_RETRY_AFTER),
1585             FE(HTTP_QUERY_SERVER),
1586             FE(HTTP_QUERY_TITLE),
1587             FE(HTTP_QUERY_USER_AGENT),
1588             FE(HTTP_QUERY_WWW_AUTHENTICATE),
1589             FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1590             FE(HTTP_QUERY_ACCEPT_RANGES),
1591         FE(HTTP_QUERY_SET_COOKIE),
1592         FE(HTTP_QUERY_COOKIE),
1593             FE(HTTP_QUERY_REQUEST_METHOD),
1594             FE(HTTP_QUERY_REFRESH),
1595             FE(HTTP_QUERY_CONTENT_DISPOSITION),
1596             FE(HTTP_QUERY_AGE),
1597             FE(HTTP_QUERY_CACHE_CONTROL),
1598             FE(HTTP_QUERY_CONTENT_BASE),
1599             FE(HTTP_QUERY_CONTENT_LOCATION),
1600             FE(HTTP_QUERY_CONTENT_MD5),
1601             FE(HTTP_QUERY_CONTENT_RANGE),
1602             FE(HTTP_QUERY_ETAG),
1603             FE(HTTP_QUERY_HOST),
1604             FE(HTTP_QUERY_IF_MATCH),
1605             FE(HTTP_QUERY_IF_NONE_MATCH),
1606             FE(HTTP_QUERY_IF_RANGE),
1607             FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1608             FE(HTTP_QUERY_MAX_FORWARDS),
1609             FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1610             FE(HTTP_QUERY_RANGE),
1611             FE(HTTP_QUERY_TRANSFER_ENCODING),
1612             FE(HTTP_QUERY_UPGRADE),
1613             FE(HTTP_QUERY_VARY),
1614             FE(HTTP_QUERY_VIA),
1615             FE(HTTP_QUERY_WARNING),
1616             FE(HTTP_QUERY_CUSTOM)
1617         };
1618         static const wininet_flag_info modifier_flags[] = {
1619             FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1620             FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1621             FE(HTTP_QUERY_FLAG_NUMBER),
1622             FE(HTTP_QUERY_FLAG_COALESCE)
1623         };
1624 #undef FE
1625         DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1626         DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1627         DWORD i;
1628
1629         TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1630         TRACE("  Attribute:");
1631         for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1632             if (query_flags[i].val == info) {
1633                 TRACE(" %s", query_flags[i].name);
1634                 break;
1635             }
1636         }
1637         if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1638             TRACE(" Unknown (%08x)", info);
1639         }
1640
1641         TRACE(" Modifier:");
1642         for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1643             if (modifier_flags[i].val & info_mod) {
1644                 TRACE(" %s", modifier_flags[i].name);
1645                 info_mod &= ~ modifier_flags[i].val;
1646             }
1647         }
1648         
1649         if (info_mod) {
1650             TRACE(" Unknown (%08x)", info_mod);
1651         }
1652         TRACE("\n");
1653     }
1654     
1655     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1656     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
1657     {
1658         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1659         goto lend;
1660     }
1661
1662     bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1663                                     lpBuffer, lpdwBufferLength, lpdwIndex);
1664
1665 lend:
1666     if( lpwhr )
1667          WININET_Release( &lpwhr->hdr );
1668
1669     TRACE("%d <--\n", bSuccess);
1670     return bSuccess;
1671 }
1672
1673 /***********************************************************************
1674  *           HttpQueryInfoA (WININET.@)
1675  *
1676  * Queries for information about an HTTP request
1677  *
1678  * RETURNS
1679  *    TRUE  on success
1680  *    FALSE on failure
1681  *
1682  */
1683 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1684         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1685 {
1686     BOOL result;
1687     DWORD len;
1688     WCHAR* bufferW;
1689
1690     if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1691        (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1692     {
1693         return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1694                                lpdwBufferLength, lpdwIndex );
1695     }
1696
1697     len = (*lpdwBufferLength)*sizeof(WCHAR);
1698     bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1699     /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1700     if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
1701         MultiByteToWideChar(CP_ACP,0,lpBuffer,-1,bufferW,len);
1702     result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1703                            &len, lpdwIndex );
1704     if( result )
1705     {
1706         len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1707                                      lpBuffer, *lpdwBufferLength, NULL, NULL );
1708         *lpdwBufferLength = len - 1;
1709
1710         TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1711     }
1712     else
1713         /* since the strings being returned from HttpQueryInfoW should be
1714          * only ASCII characters, it is reasonable to assume that all of
1715          * the Unicode characters can be reduced to a single byte */
1716         *lpdwBufferLength = len / sizeof(WCHAR);
1717
1718     HeapFree(GetProcessHeap(), 0, bufferW );
1719
1720     return result;
1721 }
1722
1723 /***********************************************************************
1724  *           HttpSendRequestExA (WININET.@)
1725  *
1726  * Sends the specified request to the HTTP server and allows chunked
1727  * transfers.
1728  *
1729  * RETURNS
1730  *  Success: TRUE
1731  *  Failure: FALSE, call GetLastError() for more information.
1732  */
1733 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1734                                LPINTERNET_BUFFERSA lpBuffersIn,
1735                                LPINTERNET_BUFFERSA lpBuffersOut,
1736                                DWORD dwFlags, DWORD dwContext)
1737 {
1738     INTERNET_BUFFERSW BuffersInW;
1739     BOOL rc = FALSE;
1740     DWORD headerlen;
1741     LPWSTR header = NULL;
1742
1743     TRACE("(%p, %p, %p, %08x, %08x): stub\n", hRequest, lpBuffersIn,
1744             lpBuffersOut, dwFlags, dwContext);
1745
1746     if (lpBuffersIn)
1747     {
1748         BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1749         if (lpBuffersIn->lpcszHeader)
1750         {
1751             headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
1752                     lpBuffersIn->dwHeadersLength,0,0);
1753             header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
1754             if (!(BuffersInW.lpcszHeader = header))
1755             {
1756                 SetLastError(ERROR_OUTOFMEMORY);
1757                 return FALSE;
1758             }
1759             BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1760                     lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1761                     header, headerlen);
1762         }
1763         else
1764             BuffersInW.lpcszHeader = NULL;
1765         BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
1766         BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
1767         BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
1768         BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
1769         BuffersInW.Next = NULL;
1770     }
1771
1772     rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
1773
1774     HeapFree(GetProcessHeap(),0,header);
1775
1776     return rc;
1777 }
1778
1779 /***********************************************************************
1780  *           HttpSendRequestExW (WININET.@)
1781  *
1782  * Sends the specified request to the HTTP server and allows chunked
1783  * transfers
1784  *
1785  * RETURNS
1786  *  Success: TRUE
1787  *  Failure: FALSE, call GetLastError() for more information.
1788  */
1789 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1790                    LPINTERNET_BUFFERSW lpBuffersIn,
1791                    LPINTERNET_BUFFERSW lpBuffersOut,
1792                    DWORD dwFlags, DWORD dwContext)
1793 {
1794     BOOL ret;
1795     LPWININETHTTPREQW lpwhr;
1796     LPWININETHTTPSESSIONW lpwhs;
1797     LPWININETAPPINFOW hIC;
1798
1799     TRACE("(%p, %p, %p, %08x, %08x)\n", hRequest, lpBuffersIn,
1800             lpBuffersOut, dwFlags, dwContext);
1801
1802     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1803
1804     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1805     {
1806         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1807         return FALSE;
1808     }
1809
1810     lpwhs = lpwhr->lpHttpSession;
1811     assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
1812     hIC = lpwhs->lpAppInfo;
1813     assert(hIC->hdr.htype == WH_HINIT);
1814
1815     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1816     {
1817         WORKREQUEST workRequest;
1818         struct WORKREQ_HTTPSENDREQUESTW *req;
1819
1820         workRequest.asyncproc = AsyncHttpSendRequestProc;
1821         workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1822         req = &workRequest.u.HttpSendRequestW;
1823         if (lpBuffersIn)
1824         {
1825             if (lpBuffersIn->lpcszHeader)
1826                 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1827                 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
1828             else
1829                 req->lpszHeader = NULL;
1830             req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
1831             req->lpOptional = lpBuffersIn->lpvBuffer;
1832             req->dwOptionalLength = lpBuffersIn->dwBufferLength;
1833             req->dwContentLength = lpBuffersIn->dwBufferTotal;
1834         }
1835         else
1836         {
1837             req->lpszHeader = NULL;
1838             req->dwHeaderLength = 0;
1839             req->lpOptional = NULL;
1840             req->dwOptionalLength = 0;
1841             req->dwContentLength = 0;
1842         }
1843
1844         req->bEndRequest = FALSE;
1845
1846         INTERNET_AsyncCall(&workRequest);
1847         /*
1848          * This is from windows.
1849          */
1850         SetLastError(ERROR_IO_PENDING);
1851         ret = FALSE;
1852     }
1853     else
1854     {
1855         ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1856                                     lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
1857                                     lpBuffersIn->dwBufferTotal, FALSE);
1858     }
1859  
1860     WININET_Release(&lpwhr->hdr);
1861     TRACE("<---\n");
1862     return ret;
1863 }
1864
1865 /***********************************************************************
1866  *           HttpSendRequestW (WININET.@)
1867  *
1868  * Sends the specified request to the HTTP server
1869  *
1870  * RETURNS
1871  *    TRUE  on success
1872  *    FALSE on failure
1873  *
1874  */
1875 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1876         DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1877 {
1878     LPWININETHTTPREQW lpwhr;
1879     LPWININETHTTPSESSIONW lpwhs = NULL;
1880     LPWININETAPPINFOW hIC = NULL;
1881     BOOL r;
1882
1883     TRACE("%p, %p (%s), %i, %p, %i)\n", hHttpRequest,
1884             lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1885
1886     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1887     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1888     {
1889         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1890         r = FALSE;
1891         goto lend;
1892     }
1893
1894     lpwhs = lpwhr->lpHttpSession;
1895     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
1896     {
1897         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1898         r = FALSE;
1899         goto lend;
1900     }
1901
1902     hIC = lpwhs->lpAppInfo;
1903     if (NULL == hIC ||  hIC->hdr.htype != WH_HINIT)
1904     {
1905         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1906         r = FALSE;
1907         goto lend;
1908     }
1909
1910     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1911     {
1912         WORKREQUEST workRequest;
1913         struct WORKREQ_HTTPSENDREQUESTW *req;
1914
1915         workRequest.asyncproc = AsyncHttpSendRequestProc;
1916         workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1917         req = &workRequest.u.HttpSendRequestW;
1918         if (lpszHeaders)
1919             req->lpszHeader = WININET_strdupW(lpszHeaders);
1920         else
1921             req->lpszHeader = 0;
1922         req->dwHeaderLength = dwHeaderLength;
1923         req->lpOptional = lpOptional;
1924         req->dwOptionalLength = dwOptionalLength;
1925         req->dwContentLength = dwOptionalLength;
1926         req->bEndRequest = TRUE;
1927
1928         INTERNET_AsyncCall(&workRequest);
1929         /*
1930          * This is from windows.
1931          */
1932         SetLastError(ERROR_IO_PENDING);
1933         r = FALSE;
1934     }
1935     else
1936     {
1937         r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1938                 dwHeaderLength, lpOptional, dwOptionalLength,
1939                 dwOptionalLength, TRUE);
1940     }
1941 lend:
1942     if( lpwhr )
1943         WININET_Release( &lpwhr->hdr );
1944     return r;
1945 }
1946
1947 /***********************************************************************
1948  *           HttpSendRequestA (WININET.@)
1949  *
1950  * Sends the specified request to the HTTP server
1951  *
1952  * RETURNS
1953  *    TRUE  on success
1954  *    FALSE on failure
1955  *
1956  */
1957 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1958         DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1959 {
1960     BOOL result;
1961     LPWSTR szHeaders=NULL;
1962     DWORD nLen=dwHeaderLength;
1963     if(lpszHeaders!=NULL)
1964     {
1965         nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1966         szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1967         MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1968     }
1969     result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1970     HeapFree(GetProcessHeap(),0,szHeaders);
1971     return result;
1972 }
1973
1974 /***********************************************************************
1975  *           HTTP_HandleRedirect (internal)
1976  */
1977 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
1978 {
1979     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1980     LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
1981     WCHAR path[2048];
1982
1983     if(lpszUrl[0]=='/')
1984     {
1985         /* if it's an absolute path, keep the same session info */
1986         lstrcpynW(path, lpszUrl, 2048);
1987     }
1988     else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1989     {
1990         TRACE("Redirect through proxy\n");
1991         lstrcpynW(path, lpszUrl, 2048);
1992     }
1993     else
1994     {
1995         URL_COMPONENTSW urlComponents;
1996         WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1997         static WCHAR szHttp[] = {'h','t','t','p',0};
1998         static WCHAR szHttps[] = {'h','t','t','p','s',0};
1999         DWORD url_length = 0;
2000         LPWSTR orig_url;
2001         LPWSTR combined_url;
2002
2003         urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2004         urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
2005         urlComponents.dwSchemeLength = 0;
2006         urlComponents.lpszHostName = lpwhs->lpszHostName;
2007         urlComponents.dwHostNameLength = 0;
2008         urlComponents.nPort = lpwhs->nHostPort;
2009         urlComponents.lpszUserName = lpwhs->lpszUserName;
2010         urlComponents.dwUserNameLength = 0;
2011         urlComponents.lpszPassword = NULL;
2012         urlComponents.dwPasswordLength = 0;
2013         urlComponents.lpszUrlPath = lpwhr->lpszPath;
2014         urlComponents.dwUrlPathLength = 0;
2015         urlComponents.lpszExtraInfo = NULL;
2016         urlComponents.dwExtraInfoLength = 0;
2017
2018         if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2019             (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2020             return FALSE;
2021
2022         orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
2023
2024         /* convert from bytes to characters */
2025         url_length = url_length / sizeof(WCHAR) - 1;
2026         if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2027         {
2028             HeapFree(GetProcessHeap(), 0, orig_url);
2029             return FALSE;
2030         }
2031
2032         url_length = 0;
2033         if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2034             (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2035         {
2036             HeapFree(GetProcessHeap(), 0, orig_url);
2037             return FALSE;
2038         }
2039         combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2040
2041         if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2042         {
2043             HeapFree(GetProcessHeap(), 0, orig_url);
2044             HeapFree(GetProcessHeap(), 0, combined_url);
2045             return FALSE;
2046         }
2047         HeapFree(GetProcessHeap(), 0, orig_url);
2048
2049         userName[0] = 0;
2050         hostName[0] = 0;
2051         protocol[0] = 0;
2052
2053         urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2054         urlComponents.lpszScheme = protocol;
2055         urlComponents.dwSchemeLength = 32;
2056         urlComponents.lpszHostName = hostName;
2057         urlComponents.dwHostNameLength = MAXHOSTNAME;
2058         urlComponents.lpszUserName = userName;
2059         urlComponents.dwUserNameLength = 1024;
2060         urlComponents.lpszPassword = NULL;
2061         urlComponents.dwPasswordLength = 0;
2062         urlComponents.lpszUrlPath = path;
2063         urlComponents.dwUrlPathLength = 2048;
2064         urlComponents.lpszExtraInfo = NULL;
2065         urlComponents.dwExtraInfoLength = 0;
2066         if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2067         {
2068             HeapFree(GetProcessHeap(), 0, combined_url);
2069             return FALSE;
2070         }
2071         HeapFree(GetProcessHeap(), 0, combined_url);
2072
2073         if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2074             (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2075         {
2076             TRACE("redirect from secure page to non-secure page\n");
2077             /* FIXME: warn about from secure redirect to non-secure page */
2078             lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2079         }
2080         if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2081             !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2082         {
2083             TRACE("redirect from non-secure page to secure page\n");
2084             /* FIXME: notify about redirect to secure page */
2085             lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2086         }
2087
2088         if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2089         {
2090             if (lstrlenW(protocol)>4) /*https*/
2091                 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2092             else /*http*/
2093                 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2094         }
2095
2096 #if 0
2097         /*
2098          * This upsets redirects to binary files on sourceforge.net 
2099          * and gives an html page instead of the target file
2100          * Examination of the HTTP request sent by native wininet.dll
2101          * reveals that it doesn't send a referrer in that case.
2102          * Maybe there's a flag that enables this, or maybe a referrer
2103          * shouldn't be added in case of a redirect.
2104          */
2105
2106         /* consider the current host as the referrer */
2107         if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2108             HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2109                            HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2110                            HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2111 #endif
2112         
2113         HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2114         lpwhs->lpszServerName = WININET_strdupW(hostName);
2115         HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2116         if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2117                 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2118         {
2119             int len;
2120             static const WCHAR fmt[] = {'%','s',':','%','i',0};
2121             len = lstrlenW(hostName);
2122             len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2123             lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2124             sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2125         }
2126         else
2127             lpwhs->lpszHostName = WININET_strdupW(hostName);
2128
2129         HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2130
2131         
2132         HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2133         lpwhs->lpszUserName = NULL;
2134         if (userName[0])
2135             lpwhs->lpszUserName = WININET_strdupW(userName);
2136         lpwhs->nServerPort = urlComponents.nPort;
2137
2138         if (!HTTP_ResolveName(lpwhr))
2139             return FALSE;
2140
2141         NETCON_close(&lpwhr->netConnection);
2142
2143         if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2144             return FALSE;
2145     }
2146
2147     HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2148     lpwhr->lpszPath=NULL;
2149     if (strlenW(path))
2150     {
2151         DWORD needed = 0;
2152         HRESULT rc;
2153
2154         rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2155         if (rc != E_POINTER)
2156             needed = strlenW(path)+1;
2157         lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2158         rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2159                         URL_ESCAPE_SPACES_ONLY);
2160         if (rc)
2161         {
2162             ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
2163             strcpyW(lpwhr->lpszPath,path);
2164         }
2165     }
2166
2167     return TRUE;
2168 }
2169
2170 /***********************************************************************
2171  *           HTTP_build_req (internal)
2172  *
2173  *  concatenate all the strings in the request together
2174  */
2175 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2176 {
2177     LPCWSTR *t;
2178     LPWSTR str;
2179
2180     for( t = list; *t ; t++  )
2181         len += strlenW( *t );
2182     len++;
2183
2184     str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2185     *str = 0;
2186
2187     for( t = list; *t ; t++ )
2188         strcatW( str, *t );
2189
2190     return str;
2191 }
2192
2193 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2194 {
2195     LPWSTR lpszPath;
2196     LPWSTR requestString;
2197     INT len;
2198     INT cnt;
2199     INT responseLen;
2200     char *ascii_req;
2201     BOOL ret;
2202     static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2203     static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2204     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2205
2206     TRACE("\n");
2207
2208     lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2209     sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2210     requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2211     HeapFree( GetProcessHeap(), 0, lpszPath );
2212
2213     len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2214                                 NULL, 0, NULL, NULL );
2215     len--; /* the nul terminator isn't needed */
2216     ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2217     WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2218                             ascii_req, len, NULL, NULL );
2219     HeapFree( GetProcessHeap(), 0, requestString );
2220
2221     TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2222
2223     ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2224     HeapFree( GetProcessHeap(), 0, ascii_req );
2225     if (!ret || cnt < 0)
2226         return FALSE;
2227
2228     responseLen = HTTP_GetResponseHeaders( lpwhr );
2229     if (!responseLen)
2230         return FALSE;
2231
2232     return TRUE;
2233 }
2234
2235 /***********************************************************************
2236  *           HTTP_HttpSendRequestW (internal)
2237  *
2238  * Sends the specified request to the HTTP server
2239  *
2240  * RETURNS
2241  *    TRUE  on success
2242  *    FALSE on failure
2243  *
2244  */
2245 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2246         DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2247         DWORD dwContentLength, BOOL bEndRequest)
2248 {
2249     INT cnt;
2250     BOOL bSuccess = FALSE;
2251     LPWSTR requestString = NULL;
2252     INT responseLen;
2253     BOOL loop_next;
2254     INTERNET_ASYNC_RESULT iar;
2255     static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
2256
2257     TRACE("--> %p\n", lpwhr);
2258
2259     assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2260
2261     /* Clear any error information */
2262     INTERNET_SetLastError(0);
2263
2264     HTTP_FixVerb(lpwhr);
2265     
2266     /* if we are using optional stuff, we must add the fixed header of that option length */
2267     if (dwContentLength > 0)
2268     {
2269         static const WCHAR szContentLength[] = {
2270             'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2271         WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2272         sprintfW(contentLengthStr, szContentLength, dwContentLength);
2273         HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L,
2274                 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2275     }
2276
2277     do
2278     {
2279         DWORD len;
2280         char *ascii_req;
2281
2282         loop_next = FALSE;
2283
2284         /* like native, just in case the caller forgot to call InternetReadFile
2285          * for all the data */
2286         HTTP_DrainContent(lpwhr);
2287         lpwhr->dwContentRead = 0;
2288
2289         if (TRACE_ON(wininet))
2290         {
2291             LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
2292             TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2293         }
2294
2295         HTTP_FixURL(lpwhr);
2296
2297         /* add the headers the caller supplied */
2298         if( lpszHeaders && dwHeaderLength )
2299         {
2300             HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2301                         HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2302         }
2303
2304         HTTP_ProcessHeader(lpwhr, szConnection, szClose,
2305                            HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
2306
2307         /* if there's a proxy username and password, add it to the headers */
2308         HTTP_AddProxyInfo(lpwhr);
2309
2310         requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2311  
2312         TRACE("Request header -> %s\n", debugstr_w(requestString) );
2313
2314         /* Send the request and store the results */
2315         if (!HTTP_OpenConnection(lpwhr))
2316             goto lend;
2317
2318         /* send the request as ASCII, tack on the optional data */
2319         if( !lpOptional )
2320             dwOptionalLength = 0;
2321         len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2322                                    NULL, 0, NULL, NULL );
2323         ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2324         WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2325                              ascii_req, len, NULL, NULL );
2326         if( lpOptional )
2327             memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2328         len = (len + dwOptionalLength - 1);
2329         ascii_req[len] = 0;
2330         TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2331
2332         INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2333                               INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2334
2335         NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2336         HeapFree( GetProcessHeap(), 0, ascii_req );
2337
2338         INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2339                               INTERNET_STATUS_REQUEST_SENT,
2340                               &len, sizeof(DWORD));
2341
2342         if (bEndRequest)
2343         {
2344             DWORD dwBufferSize;
2345
2346             INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2347                                 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2348     
2349             if (cnt < 0)
2350                 goto lend;
2351     
2352             responseLen = HTTP_GetResponseHeaders(lpwhr);
2353             if (responseLen)
2354                 bSuccess = TRUE;
2355     
2356             INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2357                                 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2358                                 sizeof(DWORD));
2359
2360             HTTP_ProcessHeaders(lpwhr);
2361
2362             dwBufferSize = sizeof(lpwhr->dwContentLength);
2363             if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2364                                      &lpwhr->dwContentLength,&dwBufferSize,NULL))
2365                 lpwhr->dwContentLength = -1;
2366
2367             if (lpwhr->dwContentLength == 0)
2368                 HTTP_FinishedReading(lpwhr);
2369
2370             if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2371             {
2372                 DWORD dwCode,dwCodeLength=sizeof(DWORD);
2373                 WCHAR szNewLocation[2048];
2374                 dwBufferSize=2048;
2375                 if (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
2376                     (dwCode==HTTP_STATUS_REDIRECT || dwCode==HTTP_STATUS_MOVED) &&
2377                     HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
2378                 {
2379                     HTTP_DrainContent(lpwhr);
2380                     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2381                                           INTERNET_STATUS_REDIRECT, szNewLocation,
2382                                           dwBufferSize);
2383                     bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
2384                     if (bSuccess)
2385                     {
2386                         HeapFree(GetProcessHeap(), 0, requestString);
2387                         loop_next = TRUE;
2388                     }
2389                 }
2390             }
2391
2392         }
2393         else
2394             bSuccess = TRUE;
2395     }
2396     while (loop_next);
2397
2398 lend:
2399
2400     HeapFree(GetProcessHeap(), 0, requestString);
2401
2402     /* TODO: send notification for P3P header */
2403
2404     iar.dwResult = (DWORD)bSuccess;
2405     iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2406
2407     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2408                           INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2409                           sizeof(INTERNET_ASYNC_RESULT));
2410
2411     TRACE("<--\n");
2412     return bSuccess;
2413 }
2414
2415 /***********************************************************************
2416  *           HTTP_Connect  (internal)
2417  *
2418  * Create http session handle
2419  *
2420  * RETURNS
2421  *   HINTERNET a session handle on success
2422  *   NULL on failure
2423  *
2424  */
2425 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2426         INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2427         LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2428         DWORD dwInternalFlags)
2429 {
2430     BOOL bSuccess = FALSE;
2431     LPWININETHTTPSESSIONW lpwhs = NULL;
2432     HINTERNET handle = NULL;
2433
2434     TRACE("-->\n");
2435
2436     assert( hIC->hdr.htype == WH_HINIT );
2437
2438     hIC->hdr.dwContext = dwContext;
2439     
2440     lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2441     if (NULL == lpwhs)
2442     {
2443         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2444         goto lerror;
2445     }
2446
2447    /*
2448     * According to my tests. The name is not resolved until a request is sent
2449     */
2450
2451     lpwhs->hdr.htype = WH_HHTTPSESSION;
2452     lpwhs->hdr.dwFlags = dwFlags;
2453     lpwhs->hdr.dwContext = dwContext;
2454     lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
2455     lpwhs->hdr.dwRefCount = 1;
2456     lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2457     lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2458
2459     WININET_AddRef( &hIC->hdr );
2460     lpwhs->lpAppInfo = hIC;
2461
2462     handle = WININET_AllocHandle( &lpwhs->hdr );
2463     if (NULL == handle)
2464     {
2465         ERR("Failed to alloc handle\n");
2466         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2467         goto lerror;
2468     }
2469
2470     if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2471         if(strchrW(hIC->lpszProxy, ' '))
2472             FIXME("Several proxies not implemented.\n");
2473         if(hIC->lpszProxyBypass)
2474             FIXME("Proxy bypass is ignored.\n");
2475     }
2476     if (lpszServerName && lpszServerName[0])
2477     {
2478         lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2479         lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2480     }
2481     if (lpszUserName && lpszUserName[0])
2482         lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2483     lpwhs->nServerPort = nServerPort;
2484     lpwhs->nHostPort = nServerPort;
2485
2486     /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2487     if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2488     {
2489         INTERNET_SendCallback(&hIC->hdr, dwContext,
2490                               INTERNET_STATUS_HANDLE_CREATED, &handle,
2491                               sizeof(handle));
2492     }
2493
2494     bSuccess = TRUE;
2495
2496 lerror:
2497     if( lpwhs )
2498         WININET_Release( &lpwhs->hdr );
2499
2500 /*
2501  * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2502  * windows
2503  */
2504
2505     TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2506     return handle;
2507 }
2508
2509
2510 /***********************************************************************
2511  *           HTTP_OpenConnection (internal)
2512  *
2513  * Connect to a web server
2514  *
2515  * RETURNS
2516  *
2517  *   TRUE  on success
2518  *   FALSE on failure
2519  */
2520 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2521 {
2522     BOOL bSuccess = FALSE;
2523     LPWININETHTTPSESSIONW lpwhs;
2524     LPWININETAPPINFOW hIC = NULL;
2525     char szaddr[32];
2526
2527     TRACE("-->\n");
2528
2529
2530     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
2531     {
2532         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2533         goto lend;
2534     }
2535
2536     if (NETCON_connected(&lpwhr->netConnection))
2537     {
2538         bSuccess = TRUE;
2539         goto lend;
2540     }
2541
2542     lpwhs = lpwhr->lpHttpSession;
2543
2544     hIC = lpwhs->lpAppInfo;
2545     inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2546               szaddr, sizeof(szaddr));
2547     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2548                           INTERNET_STATUS_CONNECTING_TO_SERVER,
2549                           szaddr,
2550                           strlen(szaddr)+1);
2551
2552     if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2553                          SOCK_STREAM, 0))
2554     {
2555         WARN("Socket creation failed\n");
2556         goto lend;
2557     }
2558
2559     if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2560                       sizeof(lpwhs->socketAddress)))
2561        goto lend;
2562
2563     if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2564     {
2565         /* Note: we differ from Microsoft's WinINet here. they seem to have
2566          * a bug that causes no status callbacks to be sent when starting
2567          * a tunnel to a proxy server using the CONNECT verb. i believe our
2568          * behaviour to be more correct and to not cause any incompatibilities
2569          * because using a secure connection through a proxy server is a rare
2570          * case that would be hard for anyone to depend on */
2571         if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2572             goto lend;
2573
2574         if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2575         {
2576             WARN("Couldn't connect securely to host\n");
2577             goto lend;
2578         }
2579     }
2580
2581     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2582                           INTERNET_STATUS_CONNECTED_TO_SERVER,
2583                           szaddr, strlen(szaddr)+1);
2584
2585     bSuccess = TRUE;
2586
2587 lend:
2588     TRACE("%d <--\n", bSuccess);
2589     return bSuccess;
2590 }
2591
2592
2593 /***********************************************************************
2594  *           HTTP_clear_response_headers (internal)
2595  *
2596  * clear out any old response headers
2597  */
2598 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2599 {
2600     DWORD i;
2601
2602     for( i=0; i<lpwhr->nCustHeaders; i++)
2603     {
2604         if( !lpwhr->pCustHeaders[i].lpszField )
2605             continue;
2606         if( !lpwhr->pCustHeaders[i].lpszValue )
2607             continue;
2608         if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2609             continue;
2610         HTTP_DeleteCustomHeader( lpwhr, i );
2611         i--;
2612     }
2613 }
2614
2615 /***********************************************************************
2616  *           HTTP_GetResponseHeaders (internal)
2617  *
2618  * Read server response
2619  *
2620  * RETURNS
2621  *
2622  *   TRUE  on success
2623  *   FALSE on error
2624  */
2625 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2626 {
2627     INT cbreaks = 0;
2628     WCHAR buffer[MAX_REPLY_LEN];
2629     DWORD buflen = MAX_REPLY_LEN;
2630     BOOL bSuccess = FALSE;
2631     INT  rc = 0;
2632     static const WCHAR szCrLf[] = {'\r','\n',0};
2633     char bufferA[MAX_REPLY_LEN];
2634     LPWSTR status_code, status_text;
2635     DWORD cchMaxRawHeaders = 1024;
2636     LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2637     DWORD cchRawHeaders = 0;
2638
2639     TRACE("-->\n");
2640
2641     /* clear old response headers (eg. from a redirect response) */
2642     HTTP_clear_response_headers( lpwhr );
2643
2644     if (!NETCON_connected(&lpwhr->netConnection))
2645         goto lend;
2646
2647     /*
2648      * HACK peek at the buffer
2649      */
2650     NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2651
2652     /*
2653      * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2654      */
2655     buflen = MAX_REPLY_LEN;
2656     memset(buffer, 0, MAX_REPLY_LEN);
2657     if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2658         goto lend;
2659     MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2660
2661     /* regenerate raw headers */
2662     while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2663     {
2664         cchMaxRawHeaders *= 2;
2665         lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2666     }
2667     memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2668     cchRawHeaders += (buflen-1);
2669     memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2670     cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2671     lpszRawHeaders[cchRawHeaders] = '\0';
2672
2673     /* split the version from the status code */
2674     status_code = strchrW( buffer, ' ' );
2675     if( !status_code )
2676         goto lend;
2677     *status_code++=0;
2678
2679     /* split the status code from the status text */
2680     status_text = strchrW( status_code, ' ' );
2681     if( !status_text )
2682         goto lend;
2683     *status_text++=0;
2684
2685     TRACE("version [%s] status code [%s] status text [%s]\n",
2686          debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2687
2688     HTTP_ProcessHeader(lpwhr, szStatus, status_code,
2689             HTTP_ADDHDR_FLAG_REPLACE);
2690
2691     HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
2692     HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
2693
2694     lpwhr->lpszVersion= WININET_strdupW(buffer);
2695     lpwhr->lpszStatusText = WININET_strdupW(status_text);
2696
2697     /* Parse each response line */
2698     do
2699     {
2700         buflen = MAX_REPLY_LEN;
2701         if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2702         {
2703             LPWSTR * pFieldAndValue;
2704
2705             TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2706             MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2707
2708             while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2709             {
2710                 cchMaxRawHeaders *= 2;
2711                 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2712             }
2713             memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2714             cchRawHeaders += (buflen-1);
2715             memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2716             cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2717             lpszRawHeaders[cchRawHeaders] = '\0';
2718
2719             pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2720             if (!pFieldAndValue)
2721                 break;
2722
2723             HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1], 
2724                 HTTP_ADDREQ_FLAG_ADD );
2725
2726             HTTP_FreeTokens(pFieldAndValue);
2727         }
2728         else
2729         {
2730             cbreaks++;
2731             if (cbreaks >= 2)
2732                break;
2733         }
2734     }while(1);
2735
2736     HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2737     lpwhr->lpszRawHeaders = lpszRawHeaders;
2738     TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2739     bSuccess = TRUE;
2740
2741 lend:
2742
2743     TRACE("<--\n");
2744     if (bSuccess)
2745         return rc;
2746     else
2747         return 0;
2748 }
2749
2750
2751 static void strip_spaces(LPWSTR start)
2752 {
2753     LPWSTR str = start;
2754     LPWSTR end;
2755
2756     while (*str == ' ' && *str != '\0')
2757         str++;
2758
2759     if (str != start)
2760         memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2761
2762     end = start + strlenW(start) - 1;
2763     while (end >= start && *end == ' ')
2764     {
2765         *end = '\0';
2766         end--;
2767     }
2768 }
2769
2770
2771 /***********************************************************************
2772  *           HTTP_InterpretHttpHeader (internal)
2773  *
2774  * Parse server response
2775  *
2776  * RETURNS
2777  *
2778  *   Pointer to array of field, value, NULL on success.
2779  *   NULL on error.
2780  */
2781 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2782 {
2783     LPWSTR * pTokenPair;
2784     LPWSTR pszColon;
2785     INT len;
2786
2787     pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2788
2789     pszColon = strchrW(buffer, ':');
2790     /* must have two tokens */
2791     if (!pszColon)
2792     {
2793         HTTP_FreeTokens(pTokenPair);
2794         if (buffer[0])
2795             TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2796         return NULL;
2797     }
2798
2799     pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2800     if (!pTokenPair[0])
2801     {
2802         HTTP_FreeTokens(pTokenPair);
2803         return NULL;
2804     }
2805     memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2806     pTokenPair[0][pszColon - buffer] = '\0';
2807
2808     /* skip colon */
2809     pszColon++;
2810     len = strlenW(pszColon);
2811     pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2812     if (!pTokenPair[1])
2813     {
2814         HTTP_FreeTokens(pTokenPair);
2815         return NULL;
2816     }
2817     memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2818
2819     strip_spaces(pTokenPair[0]);
2820     strip_spaces(pTokenPair[1]);
2821
2822     TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2823     return pTokenPair;
2824 }
2825
2826 /***********************************************************************
2827  *           HTTP_ProcessHeader (internal)
2828  *
2829  * Stuff header into header tables according to <dwModifier>
2830  *
2831  */
2832
2833 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2834
2835 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2836 {
2837     LPHTTPHEADERW lphttpHdr = NULL;
2838     BOOL bSuccess = FALSE;
2839     INT index = -1;
2840     BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
2841
2842     TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
2843
2844     /* REPLACE wins out over ADD */
2845     if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2846         dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
2847     
2848     if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
2849         index = -1;
2850     else
2851         index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
2852
2853     if (index >= 0)
2854     {
2855         if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2856         {
2857             return FALSE;
2858         }
2859         lphttpHdr = &lpwhr->pCustHeaders[index];
2860     }
2861     else if (value)
2862     {
2863         HTTPHEADERW hdr;
2864
2865         hdr.lpszField = (LPWSTR)field;
2866         hdr.lpszValue = (LPWSTR)value;
2867         hdr.wFlags = hdr.wCount = 0;
2868
2869         if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2870             hdr.wFlags |= HDR_ISREQUEST;
2871
2872         return HTTP_InsertCustomHeader(lpwhr, &hdr);
2873     }
2874
2875     if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2876             lphttpHdr->wFlags |= HDR_ISREQUEST;
2877     else
2878         lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2879
2880     if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2881     {
2882         HTTP_DeleteCustomHeader( lpwhr, index );
2883
2884         if (value)
2885         {
2886             HTTPHEADERW hdr;
2887
2888             hdr.lpszField = (LPWSTR)field;
2889             hdr.lpszValue = (LPWSTR)value;
2890             hdr.wFlags = hdr.wCount = 0;
2891
2892             if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2893                 hdr.wFlags |= HDR_ISREQUEST;
2894
2895             return HTTP_InsertCustomHeader(lpwhr, &hdr);
2896         }
2897
2898         return TRUE;
2899     }
2900     else if (dwModifier & COALESCEFLASG)
2901     {
2902         LPWSTR lpsztmp;
2903         WCHAR ch = 0;
2904         INT len = 0;
2905         INT origlen = strlenW(lphttpHdr->lpszValue);
2906         INT valuelen = strlenW(value);
2907
2908         if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2909         {
2910             ch = ',';
2911             lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2912         }
2913         else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2914         {
2915             ch = ';';
2916             lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2917         }
2918
2919         len = origlen + valuelen + ((ch > 0) ? 2 : 0);
2920
2921         lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,  lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2922         if (lpsztmp)
2923         {
2924             lphttpHdr->lpszValue = lpsztmp;
2925     /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2926             if (ch > 0)
2927             {
2928                 lphttpHdr->lpszValue[origlen] = ch;
2929                 origlen++;
2930                 lphttpHdr->lpszValue[origlen] = ' ';
2931                 origlen++;
2932             }
2933
2934             memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2935             lphttpHdr->lpszValue[len] = '\0';
2936             bSuccess = TRUE;
2937         }
2938         else
2939         {
2940             WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2941             INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2942         }
2943     }
2944     TRACE("<-- %d\n",bSuccess);
2945     return bSuccess;
2946 }
2947
2948
2949 /***********************************************************************
2950  *           HTTP_CloseConnection (internal)
2951  *
2952  * Close socket connection
2953  *
2954  */
2955 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2956 {
2957     LPWININETHTTPSESSIONW lpwhs = NULL;
2958     LPWININETAPPINFOW hIC = NULL;
2959
2960     TRACE("%p\n",lpwhr);
2961
2962     if (!NETCON_connected(&lpwhr->netConnection))
2963         return;
2964
2965     lpwhs = lpwhr->lpHttpSession;
2966     hIC = lpwhs->lpAppInfo;
2967
2968     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2969                           INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2970
2971     NETCON_close(&lpwhr->netConnection);
2972
2973     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2974                           INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2975 }
2976
2977
2978 /***********************************************************************
2979  *           HTTP_FinishedReading (internal)
2980  *
2981  * Called when all content from server has been read by client.
2982  *
2983  */
2984 BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
2985 {
2986     WCHAR szConnectionResponse[20];
2987     DWORD dwBufferSize = sizeof(szConnectionResponse)/sizeof(szConnectionResponse[0]);
2988
2989     TRACE("\n");
2990
2991     if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse,
2992                              &dwBufferSize, NULL) ||
2993         strcmpiW(szConnectionResponse, szKeepAlive))
2994     {
2995         HTTP_CloseConnection(lpwhr);
2996     }
2997
2998     /* FIXME: store data in the URL cache here */
2999
3000     return TRUE;
3001 }
3002
3003 /***********************************************************************
3004  *           HTTP_CloseHTTPRequestHandle (internal)
3005  *
3006  * Deallocate request handle
3007  *
3008  */
3009 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
3010 {
3011     DWORD i;
3012     LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
3013
3014     TRACE("\n");
3015
3016     WININET_Release(&lpwhr->lpHttpSession->hdr);
3017
3018     HTTP_CloseConnection(lpwhr);
3019
3020     HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
3021     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
3022     HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3023     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
3024     HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
3025
3026     for (i = 0; i < lpwhr->nCustHeaders; i++)
3027     {
3028         HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
3029         HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
3030     }
3031
3032     HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
3033     HeapFree(GetProcessHeap(), 0, lpwhr);
3034 }
3035
3036
3037 /***********************************************************************
3038  *           HTTP_CloseHTTPSessionHandle (internal)
3039  *
3040  * Deallocate session handle
3041  *
3042  */
3043 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
3044 {
3045     LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3046
3047     TRACE("%p\n", lpwhs);
3048
3049     WININET_Release(&lpwhs->lpAppInfo->hdr);
3050
3051     HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3052     HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3053     HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3054     HeapFree(GetProcessHeap(), 0, lpwhs);
3055 }
3056
3057
3058 /***********************************************************************
3059  *           HTTP_GetCustomHeaderIndex (internal)
3060  *
3061  * Return index of custom header from header array
3062  *
3063  */
3064 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
3065                                      int requested_index, BOOL request_only)
3066 {
3067     DWORD index;
3068
3069     TRACE("%s\n", debugstr_w(lpszField));
3070
3071     for (index = 0; index < lpwhr->nCustHeaders; index++)
3072     {
3073         if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3074             continue;
3075
3076         if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3077             continue;
3078
3079         if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3080             continue;
3081
3082         if (requested_index == 0)
3083             break;
3084         requested_index --;
3085     }
3086
3087     if (index >= lpwhr->nCustHeaders)
3088         index = -1;
3089
3090     TRACE("Return: %d\n", index);
3091     return index;
3092 }
3093
3094
3095 /***********************************************************************
3096  *           HTTP_InsertCustomHeader (internal)
3097  *
3098  * Insert header into array
3099  *
3100  */
3101 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3102 {
3103     INT count;
3104     LPHTTPHEADERW lph = NULL;
3105     BOOL r = FALSE;
3106
3107     TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3108     count = lpwhr->nCustHeaders + 1;
3109     if (count > 1)
3110         lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3111     else
3112         lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3113
3114     if (NULL != lph)
3115     {
3116         lpwhr->pCustHeaders = lph;
3117         lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3118         lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3119         lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3120         lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3121         lpwhr->nCustHeaders++;
3122         r = TRUE;
3123     }
3124     else
3125     {
3126         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3127     }
3128
3129     return r;
3130 }
3131
3132
3133 /***********************************************************************
3134  *           HTTP_DeleteCustomHeader (internal)
3135  *
3136  * Delete header from array
3137  *  If this function is called, the indexs may change.
3138  */
3139 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3140 {
3141     if( lpwhr->nCustHeaders <= 0 )
3142         return FALSE;
3143     if( index >= lpwhr->nCustHeaders )
3144         return FALSE;
3145     lpwhr->nCustHeaders--;
3146
3147     memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3148              (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3149     memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3150
3151     return TRUE;
3152 }
3153
3154 /***********************************************************************
3155  *          IsHostInProxyBypassList (@)
3156  *
3157  * Undocumented
3158  *
3159  */
3160 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3161 {
3162    FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
3163    return FALSE;
3164 }