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