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