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