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