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