wininet: Don't delete the cache file when closing a request.
[wine] / dlls / wininet / http.c
1 /*
2  * Wininet - Http Implementation
3  *
4  * Copyright 1999 Corel Corporation
5  * Copyright 2002 CodeWeavers Inc.
6  * Copyright 2002 TransGaming Technologies Inc.
7  * Copyright 2004 Mike McCormack for CodeWeavers
8  * Copyright 2005 Aric Stewart for CodeWeavers
9  * Copyright 2006 Robert Shearman for CodeWeavers
10  *
11  * Ulrich Czekalla
12  * David Hammerton
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #if defined(__MINGW32__) || defined (_MSC_VER)
33 #include <ws2tcpip.h>
34 #endif
35
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 #endif
40 #ifdef HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
42 #endif
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <time.h>
50 #include <assert.h>
51 #ifdef HAVE_ZLIB
52 #  include <zlib.h>
53 #endif
54
55 #include "windef.h"
56 #include "winbase.h"
57 #include "wininet.h"
58 #include "winerror.h"
59 #define NO_SHLWAPI_STREAM
60 #define NO_SHLWAPI_REG
61 #define NO_SHLWAPI_STRFCNS
62 #define NO_SHLWAPI_GDI
63 #include "shlwapi.h"
64 #include "sspi.h"
65 #include "wincrypt.h"
66
67 #include "internet.h"
68 #include "wine/debug.h"
69 #include "wine/exception.h"
70 #include "wine/unicode.h"
71
72 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
73
74 static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
75 static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
76 static const WCHAR hostW[] = { 'H','o','s','t',0 };
77 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
78 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
79 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
80 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
81 static const WCHAR szGET[] = { 'G','E','T', 0 };
82 static const WCHAR szCrLf[] = {'\r','\n', 0};
83
84 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
85 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
86 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
87 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
88 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
89 static const WCHAR szAge[] = { 'A','g','e',0 };
90 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
91 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
92 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
93 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
94 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
95 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
96 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
97 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
98 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
99 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
100 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
101 static const WCHAR szContent_Transfer_Encoding[] = { 'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
102 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
103 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
104 static const WCHAR szDate[] = { 'D','a','t','e',0 };
105 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
106 static const WCHAR szETag[] = { 'E','T','a','g',0 };
107 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
108 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
109 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
110 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
111 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
112 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
113 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
114 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
115 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
116 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
117 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
118 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
119 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
120 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
121 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
122 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
123 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
124 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
125 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
126 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
127 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
128 static const WCHAR szUnless_Modified_Since[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
129 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
130 static const WCHAR szURI[] = { 'U','R','I',0 };
131 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
132 static const WCHAR szVary[] = { 'V','a','r','y',0 };
133 static const WCHAR szVia[] = { 'V','i','a',0 };
134 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
135 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
136
137 #define MAXHOSTNAME 100
138 #define MAX_FIELD_VALUE_LEN 256
139 #define MAX_FIELD_LEN 256
140
141 #define HTTP_REFERER    szReferer
142 #define HTTP_ACCEPT     szAccept
143 #define HTTP_USERAGENT  szUser_Agent
144
145 #define HTTP_ADDHDR_FLAG_ADD                            0x20000000
146 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW                     0x10000000
147 #define HTTP_ADDHDR_FLAG_COALESCE                       0x40000000
148 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA            0x40000000
149 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON        0x01000000
150 #define HTTP_ADDHDR_FLAG_REPLACE                        0x80000000
151 #define HTTP_ADDHDR_FLAG_REQ                            0x02000000
152
153 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
154
155 struct HttpAuthInfo
156 {
157     LPWSTR scheme;
158     CredHandle cred;
159     CtxtHandle ctx;
160     TimeStamp exp;
161     ULONG attr;
162     ULONG max_token;
163     void *auth_data;
164     unsigned int auth_data_len;
165     BOOL finished; /* finished authenticating */
166 };
167
168
169 struct gzip_stream_t {
170 #ifdef HAVE_ZLIB
171     z_stream zstream;
172 #endif
173     BYTE buf[8192];
174     DWORD buf_size;
175     DWORD buf_pos;
176     BOOL end_of_data;
177 };
178
179 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
180 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear);
181 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
182 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
183 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
184 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
185 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
186 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
187 static BOOL HTTP_HttpQueryInfoW(LPWININETHTTPREQW, DWORD, LPVOID, LPDWORD, LPDWORD);
188 static LPWSTR HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
189 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
190 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
191 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field);
192 static void HTTP_DrainContent(WININETHTTPREQW *req);
193 static BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr);
194
195 static LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
196 {
197     int HeaderIndex = 0;
198     HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
199     if (HeaderIndex == -1)
200         return NULL;
201     else
202         return &req->pCustHeaders[HeaderIndex];
203 }
204
205 #ifdef HAVE_ZLIB
206
207 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
208 {
209     return HeapAlloc(GetProcessHeap(), 0, items*size);
210 }
211
212 static void wininet_zfree(voidpf opaque, voidpf address)
213 {
214     HeapFree(GetProcessHeap(), 0, address);
215 }
216
217 static void init_gzip_stream(WININETHTTPREQW *req)
218 {
219     gzip_stream_t *gzip_stream;
220     int zres;
221
222     gzip_stream = HeapAlloc(GetProcessHeap(), 0, sizeof(gzip_stream_t));
223     gzip_stream->zstream.zalloc = wininet_zalloc;
224     gzip_stream->zstream.zfree = wininet_zfree;
225     gzip_stream->zstream.opaque = NULL;
226     gzip_stream->zstream.next_in = NULL;
227     gzip_stream->zstream.avail_in = 0;
228     gzip_stream->zstream.next_out = NULL;
229     gzip_stream->zstream.avail_out = 0;
230     gzip_stream->buf_pos = 0;
231     gzip_stream->buf_size = 0;
232     gzip_stream->end_of_data = FALSE;
233
234     zres = inflateInit2(&gzip_stream->zstream, 0x1f);
235     if(zres != Z_OK) {
236         ERR("inflateInit failed: %d\n", zres);
237         HeapFree(GetProcessHeap(), 0, gzip_stream);
238         return;
239     }
240
241     req->gzip_stream = gzip_stream;
242 }
243
244 #else
245
246 static void init_gzip_stream(WININETHTTPREQW *req)
247 {
248     ERR("gzip stream not supported, missing zlib.\n");
249 }
250
251 #endif
252
253 /* set the request content length based on the headers */
254 static DWORD set_content_length( LPWININETHTTPREQW lpwhr )
255 {
256     static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
257     WCHAR encoding[20];
258     DWORD size;
259
260     size = sizeof(lpwhr->dwContentLength);
261     if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
262                              &lpwhr->dwContentLength, &size, NULL))
263         lpwhr->dwContentLength = ~0u;
264
265     size = sizeof(encoding);
266     if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) &&
267         !strcmpiW(encoding, szChunked))
268     {
269         lpwhr->dwContentLength = ~0u;
270         lpwhr->read_chunked = TRUE;
271     }
272
273     if(lpwhr->decoding) {
274         int encoding_idx;
275
276         static const WCHAR gzipW[] = {'g','z','i','p',0};
277
278         encoding_idx = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Encoding, 0, FALSE);
279         if(encoding_idx != -1 && !strcmpiW(lpwhr->pCustHeaders[encoding_idx].lpszValue, gzipW))
280             init_gzip_stream(lpwhr);
281     }
282
283     return lpwhr->dwContentLength;
284 }
285
286 /***********************************************************************
287  *           HTTP_Tokenize (internal)
288  *
289  *  Tokenize a string, allocating memory for the tokens.
290  */
291 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
292 {
293     LPWSTR * token_array;
294     int tokens = 0;
295     int i;
296     LPCWSTR next_token;
297
298     if (string)
299     {
300         /* empty string has no tokens */
301         if (*string)
302             tokens++;
303         /* count tokens */
304         for (i = 0; string[i]; i++)
305         {
306             if (!strncmpW(string+i, token_string, strlenW(token_string)))
307             {
308                 DWORD j;
309                 tokens++;
310                 /* we want to skip over separators, but not the null terminator */
311                 for (j = 0; j < strlenW(token_string) - 1; j++)
312                     if (!string[i+j])
313                         break;
314                 i += j;
315             }
316         }
317     }
318
319     /* add 1 for terminating NULL */
320     token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
321     token_array[tokens] = NULL;
322     if (!tokens)
323         return token_array;
324     for (i = 0; i < tokens; i++)
325     {
326         int len;
327         next_token = strstrW(string, token_string);
328         if (!next_token) next_token = string+strlenW(string);
329         len = next_token - string;
330         token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
331         memcpy(token_array[i], string, len*sizeof(WCHAR));
332         token_array[i][len] = '\0';
333         string = next_token+strlenW(token_string);
334     }
335     return token_array;
336 }
337
338 /***********************************************************************
339  *           HTTP_FreeTokens (internal)
340  *
341  *  Frees memory returned from HTTP_Tokenize.
342  */
343 static void HTTP_FreeTokens(LPWSTR * token_array)
344 {
345     int i;
346     for (i = 0; token_array[i]; i++)
347         HeapFree(GetProcessHeap(), 0, token_array[i]);
348     HeapFree(GetProcessHeap(), 0, token_array);
349 }
350
351 /* **********************************************************************
352  * 
353  * Helper functions for the HttpSendRequest(Ex) functions
354  * 
355  */
356 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
357 {
358     struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
359     LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
360
361     TRACE("%p\n", lpwhr);
362
363     HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
364             req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
365             req->dwContentLength, req->bEndRequest);
366
367     HeapFree(GetProcessHeap(), 0, req->lpszHeader);
368 }
369
370 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
371 {
372     static const WCHAR szSlash[] = { '/',0 };
373     static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
374
375     /* If we don't have a path we set it to root */
376     if (NULL == lpwhr->lpszPath)
377         lpwhr->lpszPath = WININET_strdupW(szSlash);
378     else /* remove \r and \n*/
379     {
380         int nLen = strlenW(lpwhr->lpszPath);
381         while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
382         {
383             nLen--;
384             lpwhr->lpszPath[nLen]='\0';
385         }
386         /* Replace '\' with '/' */
387         while (nLen>0) {
388             nLen--;
389             if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
390         }
391     }
392
393     if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
394                        lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
395        && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
396     {
397         WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0, 
398                              (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
399         *fixurl = '/';
400         strcpyW(fixurl + 1, lpwhr->lpszPath);
401         HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
402         lpwhr->lpszPath = fixurl;
403     }
404 }
405
406 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
407 {
408     LPWSTR requestString;
409     DWORD len, n;
410     LPCWSTR *req;
411     UINT i;
412     LPWSTR p;
413
414     static const WCHAR szSpace[] = { ' ',0 };
415     static const WCHAR szColon[] = { ':',' ',0 };
416     static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
417
418     /* allocate space for an array of all the string pointers to be added */
419     len = (lpwhr->nCustHeaders)*4 + 10;
420     req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
421
422     /* add the verb, path and HTTP version string */
423     n = 0;
424     req[n++] = verb;
425     req[n++] = szSpace;
426     req[n++] = path;
427     req[n++] = szSpace;
428     req[n++] = version;
429
430     /* Append custom request headers */
431     for (i = 0; i < lpwhr->nCustHeaders; i++)
432     {
433         if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
434         {
435             req[n++] = szCrLf;
436             req[n++] = lpwhr->pCustHeaders[i].lpszField;
437             req[n++] = szColon;
438             req[n++] = lpwhr->pCustHeaders[i].lpszValue;
439
440             TRACE("Adding custom header %s (%s)\n",
441                    debugstr_w(lpwhr->pCustHeaders[i].lpszField),
442                    debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
443         }
444     }
445
446     if( n >= len )
447         ERR("oops. buffer overrun\n");
448
449     req[n] = NULL;
450     requestString = HTTP_build_req( req, 4 );
451     HeapFree( GetProcessHeap(), 0, req );
452
453     /*
454      * Set (header) termination string for request
455      * Make sure there's exactly two new lines at the end of the request
456      */
457     p = &requestString[strlenW(requestString)-1];
458     while ( (*p == '\n') || (*p == '\r') )
459        p--;
460     strcpyW( p+1, sztwocrlf );
461     
462     return requestString;
463 }
464
465 static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr )
466 {
467     int HeaderIndex;
468     int numCookies = 0;
469     LPHTTPHEADERW setCookieHeader;
470
471     while((HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, numCookies, FALSE)) != -1)
472     {
473         setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
474
475         if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
476         {
477             int len;
478             static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','%','s',0};
479             LPWSTR buf_url;
480             LPHTTPHEADERW Host;
481
482             Host = HTTP_GetHeader(lpwhr, hostW);
483             len = lstrlenW(Host->lpszValue) + 9 + lstrlenW(lpwhr->lpszPath);
484             buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
485             sprintfW(buf_url, szFmt, Host->lpszValue, lpwhr->lpszPath);
486             InternetSetCookieW(buf_url, NULL, setCookieHeader->lpszValue);
487
488             HeapFree(GetProcessHeap(), 0, buf_url);
489         }
490         numCookies++;
491     }
492 }
493
494 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
495 {
496     static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
497     return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
498         ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
499 }
500
501 static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
502                                   struct HttpAuthInfo **ppAuthInfo,
503                                   LPWSTR domain_and_username, LPWSTR password )
504 {
505     SECURITY_STATUS sec_status;
506     struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
507     BOOL first = FALSE;
508
509     TRACE("%s\n", debugstr_w(pszAuthValue));
510
511     if (!pAuthInfo)
512     {
513         TimeStamp exp;
514
515         first = TRUE;
516         pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
517         if (!pAuthInfo)
518             return FALSE;
519
520         SecInvalidateHandle(&pAuthInfo->cred);
521         SecInvalidateHandle(&pAuthInfo->ctx);
522         memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
523         pAuthInfo->attr = 0;
524         pAuthInfo->auth_data = NULL;
525         pAuthInfo->auth_data_len = 0;
526         pAuthInfo->finished = FALSE;
527
528         if (is_basic_auth_value(pszAuthValue))
529         {
530             static const WCHAR szBasic[] = {'B','a','s','i','c',0};
531             pAuthInfo->scheme = WININET_strdupW(szBasic);
532             if (!pAuthInfo->scheme)
533             {
534                 HeapFree(GetProcessHeap(), 0, pAuthInfo);
535                 return FALSE;
536             }
537         }
538         else
539         {
540             PVOID pAuthData;
541             SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
542
543             pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
544             if (!pAuthInfo->scheme)
545             {
546                 HeapFree(GetProcessHeap(), 0, pAuthInfo);
547                 return FALSE;
548             }
549
550             if (domain_and_username)
551             {
552                 WCHAR *user = strchrW(domain_and_username, '\\');
553                 WCHAR *domain = domain_and_username;
554
555                 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
556
557                 pAuthData = &nt_auth_identity;
558
559                 if (user) user++;
560                 else
561                 {
562                     user = domain_and_username;
563                     domain = NULL;
564                 }
565
566                 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
567                 nt_auth_identity.User = user;
568                 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
569                 nt_auth_identity.Domain = domain;
570                 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
571                 nt_auth_identity.Password = password;
572                 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
573             }
574             else
575                 /* use default credentials */
576                 pAuthData = NULL;
577
578             sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
579                                                    SECPKG_CRED_OUTBOUND, NULL,
580                                                    pAuthData, NULL,
581                                                    NULL, &pAuthInfo->cred,
582                                                    &exp);
583             if (sec_status == SEC_E_OK)
584             {
585                 PSecPkgInfoW sec_pkg_info;
586                 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
587                 if (sec_status == SEC_E_OK)
588                 {
589                     pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
590                     FreeContextBuffer(sec_pkg_info);
591                 }
592             }
593             if (sec_status != SEC_E_OK)
594             {
595                 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
596                      debugstr_w(pAuthInfo->scheme), sec_status);
597                 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
598                 HeapFree(GetProcessHeap(), 0, pAuthInfo);
599                 return FALSE;
600             }
601         }
602         *ppAuthInfo = pAuthInfo;
603     }
604     else if (pAuthInfo->finished)
605         return FALSE;
606
607     if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
608         strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
609     {
610         ERR("authentication scheme changed from %s to %s\n",
611             debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
612         return FALSE;
613     }
614
615     if (is_basic_auth_value(pszAuthValue))
616     {
617         int userlen;
618         int passlen;
619         char *auth_data;
620
621         TRACE("basic authentication\n");
622
623         /* we don't cache credentials for basic authentication, so we can't
624          * retrieve them if the application didn't pass us any credentials */
625         if (!domain_and_username) return FALSE;
626
627         userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
628         passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
629
630         /* length includes a nul terminator, which will be re-used for the ':' */
631         auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
632         if (!auth_data)
633             return FALSE;
634
635         WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
636         auth_data[userlen] = ':';
637         WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
638
639         pAuthInfo->auth_data = auth_data;
640         pAuthInfo->auth_data_len = userlen + 1 + passlen;
641         pAuthInfo->finished = TRUE;
642
643         return TRUE;
644     }
645     else
646     {
647         LPCWSTR pszAuthData;
648         SecBufferDesc out_desc, in_desc;
649         SecBuffer out, in;
650         unsigned char *buffer;
651         ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
652             ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
653
654         in.BufferType = SECBUFFER_TOKEN;
655         in.cbBuffer = 0;
656         in.pvBuffer = NULL;
657
658         in_desc.ulVersion = 0;
659         in_desc.cBuffers = 1;
660         in_desc.pBuffers = &in;
661
662         pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
663         if (*pszAuthData == ' ')
664         {
665             pszAuthData++;
666             in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
667             in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
668             HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
669         }
670
671         buffer = HeapAlloc(GetProcessHeap(), 0, pAuthInfo->max_token);
672
673         out.BufferType = SECBUFFER_TOKEN;
674         out.cbBuffer = pAuthInfo->max_token;
675         out.pvBuffer = buffer;
676
677         out_desc.ulVersion = 0;
678         out_desc.cBuffers = 1;
679         out_desc.pBuffers = &out;
680
681         sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
682                                                 first ? NULL : &pAuthInfo->ctx,
683                                                 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
684                                                 context_req, 0, SECURITY_NETWORK_DREP,
685                                                 in.pvBuffer ? &in_desc : NULL,
686                                                 0, &pAuthInfo->ctx, &out_desc,
687                                                 &pAuthInfo->attr, &pAuthInfo->exp);
688         if (sec_status == SEC_E_OK)
689         {
690             pAuthInfo->finished = TRUE;
691             pAuthInfo->auth_data = out.pvBuffer;
692             pAuthInfo->auth_data_len = out.cbBuffer;
693             TRACE("sending last auth packet\n");
694         }
695         else if (sec_status == SEC_I_CONTINUE_NEEDED)
696         {
697             pAuthInfo->auth_data = out.pvBuffer;
698             pAuthInfo->auth_data_len = out.cbBuffer;
699             TRACE("sending next auth packet\n");
700         }
701         else
702         {
703             ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
704             pAuthInfo->finished = TRUE;
705             HeapFree(GetProcessHeap(), 0, out.pvBuffer);
706             return FALSE;
707         }
708     }
709
710     return TRUE;
711 }
712
713 /***********************************************************************
714  *           HTTP_HttpAddRequestHeadersW (internal)
715  */
716 static BOOL HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
717         LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
718 {
719     LPWSTR lpszStart;
720     LPWSTR lpszEnd;
721     LPWSTR buffer;
722     BOOL bSuccess = FALSE;
723     DWORD len;
724
725     TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
726
727     if( dwHeaderLength == ~0U )
728         len = strlenW(lpszHeader);
729     else
730         len = dwHeaderLength;
731     buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
732     lstrcpynW( buffer, lpszHeader, len + 1);
733
734     lpszStart = buffer;
735
736     do
737     {
738         LPWSTR * pFieldAndValue;
739
740         lpszEnd = lpszStart;
741
742         while (*lpszEnd != '\0')
743         {
744             if (*lpszEnd == '\r' || *lpszEnd == '\n')
745                  break;
746             lpszEnd++;
747         }
748
749         if (*lpszStart == '\0')
750             break;
751
752         if (*lpszEnd == '\r' || *lpszEnd == '\n')
753         {
754             *lpszEnd = '\0';
755             lpszEnd++; /* Jump over newline */
756         }
757         TRACE("interpreting header %s\n", debugstr_w(lpszStart));
758         if (*lpszStart == '\0')
759         {
760             /* Skip 0-length headers */
761             lpszStart = lpszEnd;
762             bSuccess = TRUE;
763             continue;
764         }
765         pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
766         if (pFieldAndValue)
767         {
768             bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
769             if (bSuccess)
770                 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
771                     pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
772             HTTP_FreeTokens(pFieldAndValue);
773         }
774
775         lpszStart = lpszEnd;
776     } while (bSuccess);
777
778     HeapFree(GetProcessHeap(), 0, buffer);
779
780     return bSuccess;
781 }
782
783 /***********************************************************************
784  *           HttpAddRequestHeadersW (WININET.@)
785  *
786  * Adds one or more HTTP header to the request handler
787  *
788  * NOTE
789  * On Windows if dwHeaderLength includes the trailing '\0', then
790  * HttpAddRequestHeadersW() adds it too. However this results in an
791  * invalid Http header which is rejected by some servers so we probably
792  * don't need to match Windows on that point.
793  *
794  * RETURNS
795  *    TRUE  on success
796  *    FALSE on failure
797  *
798  */
799 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
800         LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
801 {
802     BOOL bSuccess = FALSE;
803     LPWININETHTTPREQW lpwhr;
804
805     TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
806
807     if (!lpszHeader) 
808       return TRUE;
809
810     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
811     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
812     {
813         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
814         goto lend;
815     }
816     bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
817 lend:
818     if( lpwhr )
819         WININET_Release( &lpwhr->hdr );
820
821     return bSuccess;
822 }
823
824 /***********************************************************************
825  *           HttpAddRequestHeadersA (WININET.@)
826  *
827  * Adds one or more HTTP header to the request handler
828  *
829  * RETURNS
830  *    TRUE  on success
831  *    FALSE on failure
832  *
833  */
834 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
835         LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
836 {
837     DWORD len;
838     LPWSTR hdr;
839     BOOL r;
840
841     TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
842
843     len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
844     hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
845     MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
846     if( dwHeaderLength != ~0U )
847         dwHeaderLength = len;
848
849     r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
850
851     HeapFree( GetProcessHeap(), 0, hdr );
852
853     return r;
854 }
855
856 /***********************************************************************
857  *           HttpEndRequestA (WININET.@)
858  *
859  * Ends an HTTP request that was started by HttpSendRequestEx
860  *
861  * RETURNS
862  *    TRUE      if successful
863  *    FALSE     on failure
864  *
865  */
866 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, 
867         LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
868 {
869     TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
870
871     if (lpBuffersOut)
872     {
873         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
874         return FALSE;
875     }
876
877     return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
878 }
879
880 static BOOL HTTP_HttpEndRequestW(LPWININETHTTPREQW lpwhr, DWORD dwFlags, DWORD_PTR dwContext)
881 {
882     BOOL rc = FALSE;
883     INT responseLen;
884     DWORD dwBufferSize;
885     INTERNET_ASYNC_RESULT iar;
886
887     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
888                   INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
889
890     responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
891     if (responseLen)
892         rc = TRUE;
893
894     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
895                   INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
896
897     /* process cookies here. Is this right? */
898     HTTP_ProcessCookies(lpwhr);
899
900     if (!set_content_length( lpwhr )) HTTP_FinishedReading(lpwhr);
901
902     if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
903     {
904         DWORD dwCode,dwCodeLength = sizeof(DWORD);
905         if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE, &dwCode, &dwCodeLength, NULL) &&
906             (dwCode == 302 || dwCode == 301 || dwCode == 303))
907         {
908             WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
909             dwBufferSize=sizeof(szNewLocation);
910             if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL))
911             {
912                 /* redirects are always GETs */
913                 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
914                 lpwhr->lpszVerb = WININET_strdupW(szGET);
915                 HTTP_DrainContent(lpwhr);
916                 if ((new_url = HTTP_GetRedirectURL( lpwhr, szNewLocation )))
917                 {
918                     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REDIRECT,
919                                           new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
920                     rc = HTTP_HandleRedirect(lpwhr, new_url);
921                     if (rc)
922                         rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
923                     HeapFree( GetProcessHeap(), 0, new_url );
924                 }
925             }
926         }
927     }
928
929     iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
930     iar.dwError = rc ? 0 : INTERNET_GetLastError();
931
932     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
933                           INTERNET_STATUS_REQUEST_COMPLETE, &iar,
934                           sizeof(INTERNET_ASYNC_RESULT));
935     return rc;
936 }
937
938 static void AsyncHttpEndRequestProc(WORKREQUEST *work)
939 {
940     struct WORKREQ_HTTPENDREQUESTW const *req = &work->u.HttpEndRequestW;
941     LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)work->hdr;
942
943     TRACE("%p\n", lpwhr);
944
945     HTTP_HttpEndRequestW(lpwhr, req->dwFlags, req->dwContext);
946 }
947
948 /***********************************************************************
949  *           HttpEndRequestW (WININET.@)
950  *
951  * Ends an HTTP request that was started by HttpSendRequestEx
952  *
953  * RETURNS
954  *    TRUE      if successful
955  *    FALSE     on failure
956  *
957  */
958 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, 
959         LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
960 {
961     BOOL rc = FALSE;
962     LPWININETHTTPREQW lpwhr;
963
964     TRACE("-->\n");
965
966     if (lpBuffersOut)
967     {
968         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
969         return FALSE;
970     }
971
972     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
973
974     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
975     {
976         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
977         if (lpwhr)
978             WININET_Release( &lpwhr->hdr );
979         return FALSE;
980     }
981     lpwhr->hdr.dwFlags |= dwFlags;
982
983     if (lpwhr->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
984     {
985         WORKREQUEST work;
986         struct WORKREQ_HTTPENDREQUESTW *request;
987
988         work.asyncproc = AsyncHttpEndRequestProc;
989         work.hdr = WININET_AddRef( &lpwhr->hdr );
990
991         request = &work.u.HttpEndRequestW;
992         request->dwFlags = dwFlags;
993         request->dwContext = dwContext;
994
995         INTERNET_AsyncCall(&work);
996         INTERNET_SetLastError(ERROR_IO_PENDING);
997     }
998     else
999         rc = HTTP_HttpEndRequestW(lpwhr, dwFlags, dwContext);
1000
1001     WININET_Release( &lpwhr->hdr );
1002     TRACE("%i <--\n",rc);
1003     return rc;
1004 }
1005
1006 /***********************************************************************
1007  *           HttpOpenRequestW (WININET.@)
1008  *
1009  * Open a HTTP request handle
1010  *
1011  * RETURNS
1012  *    HINTERNET  a HTTP request handle on success
1013  *    NULL       on failure
1014  *
1015  */
1016 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
1017         LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1018         LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1019         DWORD dwFlags, DWORD_PTR dwContext)
1020 {
1021     LPWININETHTTPSESSIONW lpwhs;
1022     HINTERNET handle = NULL;
1023
1024     TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1025           debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
1026           debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
1027           dwFlags, dwContext);
1028     if(lpszAcceptTypes!=NULL)
1029     {
1030         int i;
1031         for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1032             TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
1033     }    
1034
1035     lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
1036     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
1037     {
1038         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1039         goto lend;
1040     }
1041
1042     /*
1043      * My tests seem to show that the windows version does not
1044      * become asynchronous until after this point. And anyhow
1045      * if this call was asynchronous then how would you get the
1046      * necessary HINTERNET pointer returned by this function.
1047      *
1048      */
1049     handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
1050                                    lpszVersion, lpszReferrer, lpszAcceptTypes,
1051                                    dwFlags, dwContext);
1052 lend:
1053     if( lpwhs )
1054         WININET_Release( &lpwhs->hdr );
1055     TRACE("returning %p\n", handle);
1056     return handle;
1057 }
1058
1059
1060 /***********************************************************************
1061  *           HttpOpenRequestA (WININET.@)
1062  *
1063  * Open a HTTP request handle
1064  *
1065  * RETURNS
1066  *    HINTERNET  a HTTP request handle on success
1067  *    NULL       on failure
1068  *
1069  */
1070 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1071         LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1072         LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1073         DWORD dwFlags, DWORD_PTR dwContext)
1074 {
1075     LPWSTR szVerb = NULL, szObjectName = NULL;
1076     LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1077     INT len, acceptTypesCount;
1078     HINTERNET rc = FALSE;
1079     LPCSTR *types;
1080
1081     TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1082           debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1083           debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1084           dwFlags, dwContext);
1085
1086     if (lpszVerb)
1087     {
1088         len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
1089         szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
1090         if ( !szVerb )
1091             goto end;
1092         MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
1093     }
1094
1095     if (lpszObjectName)
1096     {
1097         len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
1098         szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
1099         if ( !szObjectName )
1100             goto end;
1101         MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
1102     }
1103
1104     if (lpszVersion)
1105     {
1106         len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
1107         szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1108         if ( !szVersion )
1109             goto end;
1110         MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
1111     }
1112
1113     if (lpszReferrer)
1114     {
1115         len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
1116         szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1117         if ( !szReferrer )
1118             goto end;
1119         MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
1120     }
1121
1122     if (lpszAcceptTypes)
1123     {
1124         acceptTypesCount = 0;
1125         types = lpszAcceptTypes;
1126         while (*types)
1127         {
1128             __TRY
1129             {
1130                 /* find out how many there are */
1131                 if (*types && **types)
1132                 {
1133                     TRACE("accept type: %s\n", debugstr_a(*types));
1134                     acceptTypesCount++;
1135                 }
1136             }
1137             __EXCEPT_PAGE_FAULT
1138             {
1139                 WARN("invalid accept type pointer\n");
1140             }
1141             __ENDTRY;
1142             types++;
1143         }
1144         szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
1145         if (!szAcceptTypes) goto end;
1146
1147         acceptTypesCount = 0;
1148         types = lpszAcceptTypes;
1149         while (*types)
1150         {
1151             __TRY
1152             {
1153                 if (*types && **types)
1154                 {
1155                     len = MultiByteToWideChar(CP_ACP, 0, *types, -1, NULL, 0 );
1156                     szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1157
1158                     MultiByteToWideChar(CP_ACP, 0, *types, -1, szAcceptTypes[acceptTypesCount], len);
1159                     acceptTypesCount++;
1160                 }
1161             }
1162             __EXCEPT_PAGE_FAULT
1163             {
1164                 /* ignore invalid pointer */
1165             }
1166             __ENDTRY;
1167             types++;
1168         }
1169         szAcceptTypes[acceptTypesCount] = NULL;
1170     }
1171
1172     rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1173                           szVersion, szReferrer,
1174                           (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1175
1176 end:
1177     if (szAcceptTypes)
1178     {
1179         acceptTypesCount = 0;
1180         while (szAcceptTypes[acceptTypesCount])
1181         {
1182             HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1183             acceptTypesCount++;
1184         }
1185         HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1186     }
1187     HeapFree(GetProcessHeap(), 0, szReferrer);
1188     HeapFree(GetProcessHeap(), 0, szVersion);
1189     HeapFree(GetProcessHeap(), 0, szObjectName);
1190     HeapFree(GetProcessHeap(), 0, szVerb);
1191
1192     return rc;
1193 }
1194
1195 /***********************************************************************
1196  *  HTTP_EncodeBase64
1197  */
1198 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1199 {
1200     UINT n = 0, x;
1201     static const CHAR HTTP_Base64Enc[] =
1202         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1203
1204     while( len > 0 )
1205     {
1206         /* first 6 bits, all from bin[0] */
1207         base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1208         x = (bin[0] & 3) << 4;
1209
1210         /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1211         if( len == 1 )
1212         {
1213             base64[n++] = HTTP_Base64Enc[x];
1214             base64[n++] = '=';
1215             base64[n++] = '=';
1216             break;
1217         }
1218         base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1219         x = ( bin[1] & 0x0f ) << 2;
1220
1221         /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1222         if( len == 2 )
1223         {
1224             base64[n++] = HTTP_Base64Enc[x];
1225             base64[n++] = '=';
1226             break;
1227         }
1228         base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1229
1230         /* last 6 bits, all from bin [2] */
1231         base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1232         bin += 3;
1233         len -= 3;
1234     }
1235     base64[n] = 0;
1236     return n;
1237 }
1238
1239 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1240                ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1241                ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1242                ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1243 static const signed char HTTP_Base64Dec[256] =
1244 {
1245     CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1246     CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1247     CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1248     CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1249     CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1250     CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1251     CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1252     CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1253     CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1254     CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1255     CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1256     CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1257     CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1258     CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1259     CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1260     CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1261     CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1262     CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1263     CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1264     CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1265     CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1266     CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1267     CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1268     CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1269     CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1270     CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1271 };
1272 #undef CH
1273
1274 /***********************************************************************
1275  *  HTTP_DecodeBase64
1276  */
1277 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1278 {
1279     unsigned int n = 0;
1280
1281     while(*base64)
1282     {
1283         signed char in[4];
1284
1285         if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1286             ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1287             base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1288             ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1289         {
1290             WARN("invalid base64: %s\n", debugstr_w(base64));
1291             return 0;
1292         }
1293         if (bin)
1294             bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1295         n++;
1296
1297         if ((base64[2] == '=') && (base64[3] == '='))
1298             break;
1299         if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1300             ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1301         {
1302             WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1303             return 0;
1304         }
1305         if (bin)
1306             bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1307         n++;
1308
1309         if (base64[3] == '=')
1310             break;
1311         if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1312             ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1313         {
1314             WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1315             return 0;
1316         }
1317         if (bin)
1318             bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1319         n++;
1320
1321         base64 += 4;
1322     }
1323
1324     return n;
1325 }
1326
1327 /***********************************************************************
1328  *  HTTP_InsertAuthorization
1329  *
1330  *   Insert or delete the authorization field in the request header.
1331  */
1332 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1333 {
1334     if (pAuthInfo)
1335     {
1336         static const WCHAR wszSpace[] = {' ',0};
1337         static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1338         unsigned int len;
1339         WCHAR *authorization = NULL;
1340
1341         if (pAuthInfo->auth_data_len)
1342         {
1343             /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1344             len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1345             authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1346             if (!authorization)
1347                 return FALSE;
1348
1349             strcpyW(authorization, pAuthInfo->scheme);
1350             strcatW(authorization, wszSpace);
1351             HTTP_EncodeBase64(pAuthInfo->auth_data,
1352                               pAuthInfo->auth_data_len,
1353                               authorization+strlenW(authorization));
1354
1355             /* clear the data as it isn't valid now that it has been sent to the
1356              * server, unless it's Basic authentication which doesn't do
1357              * connection tracking */
1358             if (strcmpiW(pAuthInfo->scheme, wszBasic))
1359             {
1360                 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1361                 pAuthInfo->auth_data = NULL;
1362                 pAuthInfo->auth_data_len = 0;
1363             }
1364         }
1365
1366         TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1367
1368         HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1369
1370         HeapFree(GetProcessHeap(), 0, authorization);
1371     }
1372     return TRUE;
1373 }
1374
1375 static WCHAR *HTTP_BuildProxyRequestUrl(WININETHTTPREQW *req)
1376 {
1377     WCHAR new_location[INTERNET_MAX_URL_LENGTH], *url;
1378     DWORD size;
1379
1380     size = sizeof(new_location);
1381     if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_LOCATION, new_location, &size, NULL))
1382     {
1383         if (!(url = HeapAlloc( GetProcessHeap(), 0, size + sizeof(WCHAR) ))) return NULL;
1384         strcpyW( url, new_location );
1385     }
1386     else
1387     {
1388         static const WCHAR slash[] = { '/',0 };
1389         static const WCHAR format[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1390         static const WCHAR formatSSL[] = { 'h','t','t','p','s',':','/','/','%','s',':','%','d',0 };
1391         WININETHTTPSESSIONW *session = req->lpHttpSession;
1392
1393         size = 16; /* "https://" + sizeof(port#) + ":/\0" */
1394         size += strlenW( session->lpszHostName ) + strlenW( req->lpszPath );
1395
1396         if (!(url = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
1397
1398         if (req->hdr.dwFlags & INTERNET_FLAG_SECURE)
1399             sprintfW( url, formatSSL, session->lpszHostName, session->nHostPort );
1400         else
1401             sprintfW( url, format, session->lpszHostName, session->nHostPort );
1402         if (req->lpszPath[0] != '/') strcatW( url, slash );
1403         strcatW( url, req->lpszPath );
1404     }
1405     TRACE("url=%s\n", debugstr_w(url));
1406     return url;
1407 }
1408
1409 /***********************************************************************
1410  *           HTTP_DealWithProxy
1411  */
1412 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1413     LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1414 {
1415     WCHAR buf[MAXHOSTNAME];
1416     WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1417     static WCHAR szNul[] = { 0 };
1418     URL_COMPONENTSW UrlComponents;
1419     static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1420     static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1421
1422     memset( &UrlComponents, 0, sizeof UrlComponents );
1423     UrlComponents.dwStructSize = sizeof UrlComponents;
1424     UrlComponents.lpszHostName = buf;
1425     UrlComponents.dwHostNameLength = MAXHOSTNAME;
1426
1427     if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1428                                  hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1429         sprintfW(proxy, szFormat, hIC->lpszProxy);
1430     else
1431         strcpyW(proxy, hIC->lpszProxy);
1432     if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1433         return FALSE;
1434     if( UrlComponents.dwHostNameLength == 0 )
1435         return FALSE;
1436
1437     if( !lpwhr->lpszPath )
1438         lpwhr->lpszPath = szNul;
1439
1440     if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1441         UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1442
1443     HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1444     lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1445     lpwhs->nServerPort = UrlComponents.nPort;
1446
1447     TRACE("proxy server=%s port=%d\n", debugstr_w(lpwhs->lpszServerName), lpwhs->nServerPort);
1448     return TRUE;
1449 }
1450
1451 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1452 {
1453     char szaddr[32];
1454     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1455
1456     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1457                           INTERNET_STATUS_RESOLVING_NAME,
1458                           lpwhs->lpszServerName,
1459                           strlenW(lpwhs->lpszServerName)+1);
1460
1461     if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1462                     &lpwhs->socketAddress))
1463     {
1464         INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1465         return FALSE;
1466     }
1467
1468     inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1469               szaddr, sizeof(szaddr));
1470     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1471                           INTERNET_STATUS_NAME_RESOLVED,
1472                           szaddr, strlen(szaddr)+1);
1473
1474     TRACE("resolved %s to %s\n", debugstr_w(lpwhs->lpszServerName), szaddr);
1475     return TRUE;
1476 }
1477
1478
1479 /***********************************************************************
1480  *           HTTPREQ_Destroy (internal)
1481  *
1482  * Deallocate request handle
1483  *
1484  */
1485 static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1486 {
1487     LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1488     DWORD i;
1489
1490     TRACE("\n");
1491
1492     if(lpwhr->hCacheFile)
1493         CloseHandle(lpwhr->hCacheFile);
1494
1495     HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1496
1497     DeleteCriticalSection( &lpwhr->read_section );
1498     WININET_Release(&lpwhr->lpHttpSession->hdr);
1499
1500     if (lpwhr->pAuthInfo)
1501     {
1502         if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1503             DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1504         if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1505             FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1506
1507         HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1508         HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1509         HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1510         lpwhr->pAuthInfo = NULL;
1511     }
1512
1513     if (lpwhr->pProxyAuthInfo)
1514     {
1515         if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1516             DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1517         if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1518             FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1519
1520         HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1521         HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1522         HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1523         lpwhr->pProxyAuthInfo = NULL;
1524     }
1525
1526     HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1527     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1528     HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1529     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1530     HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1531
1532     for (i = 0; i < lpwhr->nCustHeaders; i++)
1533     {
1534         HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1535         HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1536     }
1537
1538     HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1539     HeapFree(GetProcessHeap(), 0, lpwhr);
1540 }
1541
1542 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1543 {
1544     LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1545
1546     TRACE("%p\n",lpwhr);
1547
1548 #ifdef HAVE_ZLIB
1549     if(lpwhr->gzip_stream) {
1550         inflateEnd(&lpwhr->gzip_stream->zstream);
1551         HeapFree(GetProcessHeap(), 0, lpwhr->gzip_stream);
1552         lpwhr->gzip_stream = NULL;
1553     }
1554 #endif
1555
1556     if (!NETCON_connected(&lpwhr->netConnection))
1557         return;
1558
1559     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1560                           INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1561
1562     NETCON_close(&lpwhr->netConnection);
1563
1564     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1565                           INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1566 }
1567
1568 static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
1569 {
1570     LPHTTPHEADERW host_header;
1571
1572     static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
1573
1574     host_header = HTTP_GetHeader(req, hostW);
1575     if(!host_header)
1576         return FALSE;
1577
1578     sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
1579     return TRUE;
1580 }
1581
1582 static DWORD HTTPREQ_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1583 {
1584     WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1585
1586     switch(option) {
1587     case INTERNET_OPTION_HANDLE_TYPE:
1588         TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1589
1590         if (*size < sizeof(ULONG))
1591             return ERROR_INSUFFICIENT_BUFFER;
1592
1593         *size = sizeof(DWORD);
1594         *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1595         return ERROR_SUCCESS;
1596
1597     case INTERNET_OPTION_URL: {
1598         WCHAR url[INTERNET_MAX_URL_LENGTH];
1599         HTTPHEADERW *host;
1600         DWORD len;
1601         WCHAR *pch;
1602
1603         static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1604
1605         TRACE("INTERNET_OPTION_URL\n");
1606
1607         host = HTTP_GetHeader(req, hostW);
1608         strcpyW(url, httpW);
1609         strcatW(url, host->lpszValue);
1610         if (NULL != (pch = strchrW(url + strlenW(httpW), ':')))
1611             *pch = 0;
1612         strcatW(url, req->lpszPath);
1613
1614         TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1615
1616         if(unicode) {
1617             len = (strlenW(url)+1) * sizeof(WCHAR);
1618             if(*size < len)
1619                 return ERROR_INSUFFICIENT_BUFFER;
1620
1621             *size = len;
1622             strcpyW(buffer, url);
1623             return ERROR_SUCCESS;
1624         }else {
1625             len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1626             if(len > *size)
1627                 return ERROR_INSUFFICIENT_BUFFER;
1628
1629             *size = len;
1630             return ERROR_SUCCESS;
1631         }
1632     }
1633
1634     case INTERNET_OPTION_CACHE_TIMESTAMPS: {
1635         INTERNET_CACHE_ENTRY_INFOW *info;
1636         INTERNET_CACHE_TIMESTAMPS *ts = buffer;
1637         WCHAR url[INTERNET_MAX_URL_LENGTH];
1638         DWORD nbytes, error;
1639         BOOL ret;
1640
1641         TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
1642
1643         if (*size < sizeof(*ts))
1644         {
1645             *size = sizeof(*ts);
1646             return ERROR_INSUFFICIENT_BUFFER;
1647         }
1648         nbytes = 0;
1649         HTTP_GetRequestURL(req, url);
1650         ret = GetUrlCacheEntryInfoW(url, NULL, &nbytes);
1651         error = GetLastError();
1652         if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
1653         {
1654             if (!(info = HeapAlloc(GetProcessHeap(), 0, nbytes)))
1655                 return ERROR_OUTOFMEMORY;
1656
1657             GetUrlCacheEntryInfoW(url, info, &nbytes);
1658
1659             ts->ftExpires = info->ExpireTime;
1660             ts->ftLastModified = info->LastModifiedTime;
1661
1662             HeapFree(GetProcessHeap(), 0, info);
1663             *size = sizeof(*ts);
1664             return ERROR_SUCCESS;
1665         }
1666         return error;
1667     }
1668
1669     case INTERNET_OPTION_DATAFILE_NAME: {
1670         DWORD req_size;
1671
1672         TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1673
1674         if(!req->lpszCacheFile) {
1675             *size = 0;
1676             return ERROR_INTERNET_ITEM_NOT_FOUND;
1677         }
1678
1679         if(unicode) {
1680             req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1681             if(*size < req_size)
1682                 return ERROR_INSUFFICIENT_BUFFER;
1683
1684             *size = req_size;
1685             memcpy(buffer, req->lpszCacheFile, *size);
1686             return ERROR_SUCCESS;
1687         }else {
1688             req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1689             if (req_size > *size)
1690                 return ERROR_INSUFFICIENT_BUFFER;
1691
1692             *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1693                     -1, buffer, *size, NULL, NULL);
1694             return ERROR_SUCCESS;
1695         }
1696     }
1697
1698     case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1699         PCCERT_CONTEXT context;
1700
1701         if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1702             *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1703             return ERROR_INSUFFICIENT_BUFFER;
1704         }
1705
1706         context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1707         if(context) {
1708             INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1709             DWORD len;
1710
1711             memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1712             info->ftExpiry = context->pCertInfo->NotAfter;
1713             info->ftStart = context->pCertInfo->NotBefore;
1714             if(unicode) {
1715                 len = CertNameToStrW(context->dwCertEncodingType,
1716                         &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1717                 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1718                 if(info->lpszSubjectInfo)
1719                     CertNameToStrW(context->dwCertEncodingType,
1720                              &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1721                              info->lpszSubjectInfo, len);
1722                 len = CertNameToStrW(context->dwCertEncodingType,
1723                          &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1724                 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1725                 if (info->lpszIssuerInfo)
1726                     CertNameToStrW(context->dwCertEncodingType,
1727                              &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1728                              info->lpszIssuerInfo, len);
1729             }else {
1730                 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1731
1732                 len = CertNameToStrA(context->dwCertEncodingType,
1733                          &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1734                 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1735                 if(infoA->lpszSubjectInfo)
1736                     CertNameToStrA(context->dwCertEncodingType,
1737                              &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1738                              infoA->lpszSubjectInfo, len);
1739                 len = CertNameToStrA(context->dwCertEncodingType,
1740                          &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1741                 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1742                 if(infoA->lpszIssuerInfo)
1743                     CertNameToStrA(context->dwCertEncodingType,
1744                              &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1745                              infoA->lpszIssuerInfo, len);
1746             }
1747
1748             /*
1749              * Contrary to MSDN, these do not appear to be set.
1750              * lpszProtocolName
1751              * lpszSignatureAlgName
1752              * lpszEncryptionAlgName
1753              * dwKeySize
1754              */
1755             CertFreeCertificateContext(context);
1756             return ERROR_SUCCESS;
1757         }
1758     }
1759     }
1760
1761     return INET_QueryOption(option, buffer, size, unicode);
1762 }
1763
1764 static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1765 {
1766     WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1767
1768     switch(option) {
1769     case INTERNET_OPTION_SEND_TIMEOUT:
1770     case INTERNET_OPTION_RECEIVE_TIMEOUT:
1771         TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1772
1773         if (size != sizeof(DWORD))
1774             return ERROR_INVALID_PARAMETER;
1775
1776         return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1777                     *(DWORD*)buffer);
1778
1779     case INTERNET_OPTION_USERNAME:
1780         HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszUserName);
1781         if (!(req->lpHttpSession->lpszUserName = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1782         return ERROR_SUCCESS;
1783
1784     case INTERNET_OPTION_PASSWORD:
1785         HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszPassword);
1786         if (!(req->lpHttpSession->lpszPassword = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1787         return ERROR_SUCCESS;
1788     case INTERNET_OPTION_HTTP_DECODING:
1789         if(size != sizeof(BOOL))
1790             return ERROR_INVALID_PARAMETER;
1791         req->decoding = *(BOOL*)buffer;
1792         return ERROR_SUCCESS;
1793     }
1794
1795     return ERROR_INTERNET_INVALID_OPTION;
1796 }
1797
1798 /* read some more data into the read buffer (the read section must be held) */
1799 static BOOL read_more_data( WININETHTTPREQW *req, int maxlen )
1800 {
1801     int len;
1802
1803     if (req->read_pos)
1804     {
1805         /* move existing data to the start of the buffer */
1806         if(req->read_size)
1807             memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
1808         req->read_pos = 0;
1809     }
1810
1811     if (maxlen == -1) maxlen = sizeof(req->read_buf);
1812
1813     if(!NETCON_recv( &req->netConnection, req->read_buf + req->read_size,
1814                      maxlen - req->read_size, 0, &len ))
1815         return FALSE;
1816
1817     req->read_size += len;
1818     return TRUE;
1819 }
1820
1821 /* remove some amount of data from the read buffer (the read section must be held) */
1822 static void remove_data( WININETHTTPREQW *req, int count )
1823 {
1824     if (!(req->read_size -= count)) req->read_pos = 0;
1825     else req->read_pos += count;
1826 }
1827
1828 static BOOL read_line( WININETHTTPREQW *req, LPSTR buffer, DWORD *len )
1829 {
1830     int count, bytes_read, pos = 0;
1831
1832     EnterCriticalSection( &req->read_section );
1833     for (;;)
1834     {
1835         BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
1836
1837         if (eol)
1838         {
1839             count = eol - (req->read_buf + req->read_pos);
1840             bytes_read = count + 1;
1841         }
1842         else count = bytes_read = req->read_size;
1843
1844         count = min( count, *len - pos );
1845         memcpy( buffer + pos, req->read_buf + req->read_pos, count );
1846         pos += count;
1847         remove_data( req, bytes_read );
1848         if (eol) break;
1849
1850         if (!read_more_data( req, -1 ) || !req->read_size)
1851         {
1852             *len = 0;
1853             TRACE( "returning empty string\n" );
1854             LeaveCriticalSection( &req->read_section );
1855             return FALSE;
1856         }
1857     }
1858     LeaveCriticalSection( &req->read_section );
1859
1860     if (pos < *len)
1861     {
1862         if (pos && buffer[pos - 1] == '\r') pos--;
1863         *len = pos + 1;
1864     }
1865     buffer[*len - 1] = 0;
1866     TRACE( "returning %s\n", debugstr_a(buffer));
1867     return TRUE;
1868 }
1869
1870 /* discard data contents until we reach end of line (the read section must be held) */
1871 static BOOL discard_eol( WININETHTTPREQW *req )
1872 {
1873     do
1874     {
1875         BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
1876         if (eol)
1877         {
1878             remove_data( req, (eol + 1) - (req->read_buf + req->read_pos) );
1879             break;
1880         }
1881         req->read_pos = req->read_size = 0;  /* discard everything */
1882         if (!read_more_data( req, -1 )) return FALSE;
1883     } while (req->read_size);
1884     return TRUE;
1885 }
1886
1887 /* read the size of the next chunk (the read section must be held) */
1888 static BOOL start_next_chunk( WININETHTTPREQW *req )
1889 {
1890     DWORD chunk_size = 0;
1891
1892     if (!req->dwContentLength) return TRUE;
1893     if (req->dwContentLength == req->dwContentRead)
1894     {
1895         /* read terminator for the previous chunk */
1896         if (!discard_eol( req )) return FALSE;
1897         req->dwContentLength = ~0u;
1898         req->dwContentRead = 0;
1899     }
1900     for (;;)
1901     {
1902         while (req->read_size)
1903         {
1904             char ch = req->read_buf[req->read_pos];
1905             if (ch >= '0' && ch <= '9') chunk_size = chunk_size * 16 + ch - '0';
1906             else if (ch >= 'a' && ch <= 'f') chunk_size = chunk_size * 16 + ch - 'a' + 10;
1907             else if (ch >= 'A' && ch <= 'F') chunk_size = chunk_size * 16 + ch - 'A' + 10;
1908             else if (ch == ';' || ch == '\r' || ch == '\n')
1909             {
1910                 TRACE( "reading %u byte chunk\n", chunk_size );
1911                 req->dwContentLength = chunk_size;
1912                 req->dwContentRead = 0;
1913                 if (!discard_eol( req )) return FALSE;
1914                 return TRUE;
1915             }
1916             remove_data( req, 1 );
1917         }
1918         if (!read_more_data( req, -1 )) return FALSE;
1919         if (!req->read_size)
1920         {
1921             req->dwContentLength = req->dwContentRead = 0;
1922             return TRUE;
1923         }
1924     }
1925 }
1926
1927 /* check if we have reached the end of the data to read (the read section must be held) */
1928 static BOOL end_of_read_data( WININETHTTPREQW *req )
1929 {
1930     if (req->gzip_stream) return req->gzip_stream->end_of_data && !req->gzip_stream->buf_size;
1931     if (req->read_chunked) return (req->dwContentLength == 0);
1932     if (req->dwContentLength == ~0u) return FALSE;
1933     return (req->dwContentLength == req->dwContentRead);
1934 }
1935
1936 /* fetch some more data into the read buffer (the read section must be held) */
1937 static BOOL refill_buffer( WININETHTTPREQW *req )
1938 {
1939     int len = sizeof(req->read_buf);
1940
1941     if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
1942     {
1943         if (!start_next_chunk( req )) return FALSE;
1944     }
1945
1946     if (req->dwContentLength != ~0u) len = min( len, req->dwContentLength - req->dwContentRead );
1947     if (len <= req->read_size) return TRUE;
1948
1949     if (!read_more_data( req, len )) return FALSE;
1950     if (!req->read_size) req->dwContentLength = req->dwContentRead = 0;
1951     return TRUE;
1952 }
1953
1954 static DWORD read_gzip_data(WININETHTTPREQW *req, BYTE *buf, int size, BOOL sync, int *read_ret)
1955 {
1956     DWORD ret = ERROR_SUCCESS;
1957     int read = 0;
1958
1959 #ifdef HAVE_ZLIB
1960     z_stream *zstream = &req->gzip_stream->zstream;
1961     int zres;
1962
1963     while(read < size && !req->gzip_stream->end_of_data) {
1964         if(!req->read_size) {
1965             if(!sync || !refill_buffer(req))
1966                 break;
1967         }
1968
1969         zstream->next_in = req->read_buf+req->read_pos;
1970         zstream->avail_in = req->read_size;
1971         zstream->next_out = buf+read;
1972         zstream->avail_out = size-read;
1973         zres = inflate(zstream, Z_FULL_FLUSH);
1974         read = size - zstream->avail_out;
1975         remove_data(req, req->read_size-zstream->avail_in);
1976         if(zres == Z_STREAM_END) {
1977             TRACE("end of data\n");
1978             req->gzip_stream->end_of_data = TRUE;
1979         }else if(zres != Z_OK) {
1980             WARN("inflate failed %d\n", zres);
1981             if(!read)
1982                 ret = ERROR_INTERNET_DECODING_FAILED;
1983             break;
1984         }
1985     }
1986 #endif
1987
1988     *read_ret = read;
1989     return ret;
1990 }
1991
1992 static void refill_gzip_buffer(WININETHTTPREQW *req)
1993 {
1994     DWORD res;
1995     int len;
1996
1997     if(!req->gzip_stream || !req->read_size || req->gzip_stream->buf_size == sizeof(req->gzip_stream->buf))
1998         return;
1999
2000     if(req->gzip_stream->buf_pos) {
2001         if(req->gzip_stream->buf_size)
2002             memmove(req->gzip_stream->buf, req->gzip_stream->buf + req->gzip_stream->buf_pos, req->gzip_stream->buf_size);
2003         req->gzip_stream->buf_pos = 0;
2004     }
2005
2006     res = read_gzip_data(req, req->gzip_stream->buf + req->gzip_stream->buf_size,
2007             sizeof(req->gzip_stream->buf) - req->gzip_stream->buf_size, FALSE, &len);
2008     if(res == ERROR_SUCCESS)
2009         req->gzip_stream->buf_size += len;
2010 }
2011
2012 /* return the size of data available to be read immediately (the read section must be held) */
2013 static DWORD get_avail_data( WININETHTTPREQW *req )
2014 {
2015     if (req->gzip_stream) {
2016         refill_gzip_buffer(req);
2017         return req->gzip_stream->buf_size;
2018     }
2019     if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
2020         return 0;
2021     return min( req->read_size, req->dwContentLength - req->dwContentRead );
2022 }
2023
2024 static void HTTP_ReceiveRequestData(WININETHTTPREQW *req, BOOL first_notif)
2025 {
2026     INTERNET_ASYNC_RESULT iar;
2027
2028     TRACE("%p\n", req);
2029
2030     EnterCriticalSection( &req->read_section );
2031     if (refill_buffer( req )) {
2032         iar.dwResult = (DWORD_PTR)req->hdr.hInternet;
2033         iar.dwError = first_notif ? 0 : get_avail_data(req);
2034     }else {
2035         iar.dwResult = 0;
2036         iar.dwError = INTERNET_GetLastError();
2037     }
2038     LeaveCriticalSection( &req->read_section );
2039
2040     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2041                           sizeof(INTERNET_ASYNC_RESULT));
2042 }
2043
2044 /* read data from the http connection (the read section must be held) */
2045 static DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
2046 {
2047     BOOL finished_reading = FALSE;
2048     int len, bytes_read = 0;
2049     DWORD ret = ERROR_SUCCESS;
2050
2051     EnterCriticalSection( &req->read_section );
2052
2053     if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
2054     {
2055         if (!start_next_chunk( req )) goto done;
2056     }
2057
2058     if(req->gzip_stream) {
2059         if(req->gzip_stream->buf_size) {
2060             bytes_read = min(req->gzip_stream->buf_size, size);
2061             memcpy(buffer, req->gzip_stream->buf + req->gzip_stream->buf_pos, bytes_read);
2062             req->gzip_stream->buf_pos += bytes_read;
2063             req->gzip_stream->buf_size -= bytes_read;
2064         }else if(!req->read_size && !req->gzip_stream->end_of_data) {
2065             refill_buffer(req);
2066         }
2067
2068         if(size > bytes_read) {
2069             ret = read_gzip_data(req, (BYTE*)buffer+bytes_read, size-bytes_read, sync, &len);
2070             if(ret == ERROR_SUCCESS)
2071                 bytes_read += len;
2072         }
2073
2074         finished_reading = req->gzip_stream->end_of_data && !req->gzip_stream->buf_size;
2075     }else {
2076         if (req->dwContentLength != ~0u) size = min( size, req->dwContentLength - req->dwContentRead );
2077
2078         if (req->read_size) {
2079             bytes_read = min( req->read_size, size );
2080             memcpy( buffer, req->read_buf + req->read_pos, bytes_read );
2081             remove_data( req, bytes_read );
2082         }
2083
2084         if (size > bytes_read && (!bytes_read || sync)) {
2085             if (NETCON_recv( &req->netConnection, (char *)buffer + bytes_read, size - bytes_read,
2086                              sync ? MSG_WAITALL : 0, &len))
2087                 bytes_read += len;
2088             /* always return success, even if the network layer returns an error */
2089         }
2090
2091         finished_reading = !bytes_read && req->dwContentRead == req->dwContentLength;
2092     }
2093 done:
2094     req->dwContentRead += bytes_read;
2095     *read = bytes_read;
2096
2097     TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read, req->dwContentRead, req->dwContentLength );
2098     LeaveCriticalSection( &req->read_section );
2099
2100     if(ret == ERROR_SUCCESS && req->lpszCacheFile) {
2101         BOOL res;
2102         DWORD dwBytesWritten;
2103
2104         res = WriteFile(req->hCacheFile, buffer, bytes_read, &dwBytesWritten, NULL);
2105         if(!res)
2106             WARN("WriteFile failed: %u\n", GetLastError());
2107     }
2108
2109     if(finished_reading)
2110         HTTP_FinishedReading(req);
2111
2112     return ret;
2113 }
2114
2115
2116 static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
2117 {
2118     WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
2119     return HTTPREQ_Read(req, buffer, size, read, TRUE);
2120 }
2121
2122 static void HTTPREQ_AsyncReadFileExAProc(WORKREQUEST *workRequest)
2123 {
2124     struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
2125     WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
2126     INTERNET_ASYNC_RESULT iar;
2127     DWORD res;
2128
2129     TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
2130
2131     res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
2132             data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
2133
2134     iar.dwResult = res == ERROR_SUCCESS;
2135     iar.dwError = res;
2136
2137     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2138                           INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2139                           sizeof(INTERNET_ASYNC_RESULT));
2140 }
2141
2142 static DWORD HTTPREQ_ReadFileExA(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSA *buffers,
2143         DWORD flags, DWORD_PTR context)
2144 {
2145
2146     WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
2147     DWORD res;
2148
2149     if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
2150         FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
2151
2152     if (buffers->dwStructSize != sizeof(*buffers))
2153         return ERROR_INVALID_PARAMETER;
2154
2155     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2156
2157     if ((hdr->dwFlags & INTERNET_FLAG_ASYNC) && !get_avail_data(req))
2158     {
2159         WORKREQUEST workRequest;
2160
2161         if (TryEnterCriticalSection( &req->read_section ))
2162         {
2163             if (get_avail_data(req))
2164             {
2165                 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength,
2166                                    &buffers->dwBufferLength, FALSE);
2167                 LeaveCriticalSection( &req->read_section );
2168                 goto done;
2169             }
2170             LeaveCriticalSection( &req->read_section );
2171         }
2172
2173         workRequest.asyncproc = HTTPREQ_AsyncReadFileExAProc;
2174         workRequest.hdr = WININET_AddRef(&req->hdr);
2175         workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
2176
2177         INTERNET_AsyncCall(&workRequest);
2178
2179         return ERROR_IO_PENDING;
2180     }
2181
2182     res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
2183             !(flags & IRF_NO_WAIT));
2184
2185 done:
2186     if (res == ERROR_SUCCESS) {
2187         DWORD size = buffers->dwBufferLength;
2188         INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2189                 &size, sizeof(size));
2190     }
2191
2192     return res;
2193 }
2194
2195 static void HTTPREQ_AsyncReadFileExWProc(WORKREQUEST *workRequest)
2196 {
2197     struct WORKREQ_INTERNETREADFILEEXW const *data = &workRequest->u.InternetReadFileExW;
2198     WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
2199     INTERNET_ASYNC_RESULT iar;
2200     DWORD res;
2201
2202     TRACE("INTERNETREADFILEEXW %p\n", workRequest->hdr);
2203
2204     res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
2205             data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
2206
2207     iar.dwResult = res == ERROR_SUCCESS;
2208     iar.dwError = res;
2209
2210     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2211                           INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2212                           sizeof(INTERNET_ASYNC_RESULT));
2213 }
2214
2215 static DWORD HTTPREQ_ReadFileExW(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSW *buffers,
2216         DWORD flags, DWORD_PTR context)
2217 {
2218
2219     WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
2220     DWORD res;
2221
2222     if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
2223         FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
2224
2225     if (buffers->dwStructSize != sizeof(*buffers))
2226         return ERROR_INVALID_PARAMETER;
2227
2228     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2229
2230     if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
2231     {
2232         WORKREQUEST workRequest;
2233
2234         if (TryEnterCriticalSection( &req->read_section ))
2235         {
2236             if (get_avail_data(req))
2237             {
2238                 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength,
2239                                    &buffers->dwBufferLength, FALSE);
2240                 LeaveCriticalSection( &req->read_section );
2241                 goto done;
2242             }
2243             LeaveCriticalSection( &req->read_section );
2244         }
2245
2246         workRequest.asyncproc = HTTPREQ_AsyncReadFileExWProc;
2247         workRequest.hdr = WININET_AddRef(&req->hdr);
2248         workRequest.u.InternetReadFileExW.lpBuffersOut = buffers;
2249
2250         INTERNET_AsyncCall(&workRequest);
2251
2252         return ERROR_IO_PENDING;
2253     }
2254
2255     res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
2256             !(flags & IRF_NO_WAIT));
2257
2258 done:
2259     if (res == ERROR_SUCCESS) {
2260         DWORD size = buffers->dwBufferLength;
2261         INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2262                 &size, sizeof(size));
2263     }
2264
2265     return res;
2266 }
2267
2268 static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
2269 {
2270     BOOL ret;
2271     LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
2272
2273     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2274
2275     *written = 0;
2276     if ((ret = NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written)))
2277         lpwhr->dwBytesWritten += *written;
2278
2279     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
2280     return ret;
2281 }
2282
2283 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
2284 {
2285     WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
2286
2287     HTTP_ReceiveRequestData(req, FALSE);
2288 }
2289
2290 static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
2291 {
2292     WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
2293
2294     TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
2295
2296     if (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2297     {
2298         WORKREQUEST workRequest;
2299
2300         /* never wait, if we can't enter the section we queue an async request right away */
2301         if (TryEnterCriticalSection( &req->read_section ))
2302         {
2303             if ((*available = get_avail_data( req ))) goto done;
2304             if (end_of_read_data( req )) goto done;
2305             LeaveCriticalSection( &req->read_section );
2306         }
2307
2308         workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
2309         workRequest.hdr = WININET_AddRef( &req->hdr );
2310
2311         INTERNET_AsyncCall(&workRequest);
2312
2313         return ERROR_IO_PENDING;
2314     }
2315
2316     EnterCriticalSection( &req->read_section );
2317
2318     if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
2319     {
2320         refill_buffer( req );
2321         *available = get_avail_data( req );
2322     }
2323
2324 done:
2325     if (*available == sizeof(req->read_buf) && !req->gzip_stream)  /* check if we have even more pending in the socket */
2326     {
2327         DWORD extra;
2328         if (NETCON_query_data_available(&req->netConnection, &extra))
2329             *available = min( *available + extra, req->dwContentLength - req->dwContentRead );
2330     }
2331     LeaveCriticalSection( &req->read_section );
2332
2333     TRACE( "returning %u\n", *available );
2334     return ERROR_SUCCESS;
2335 }
2336
2337 static const HANDLEHEADERVtbl HTTPREQVtbl = {
2338     HTTPREQ_Destroy,
2339     HTTPREQ_CloseConnection,
2340     HTTPREQ_QueryOption,
2341     HTTPREQ_SetOption,
2342     HTTPREQ_ReadFile,
2343     HTTPREQ_ReadFileExA,
2344     HTTPREQ_ReadFileExW,
2345     HTTPREQ_WriteFile,
2346     HTTPREQ_QueryDataAvailable,
2347     NULL
2348 };
2349
2350 /***********************************************************************
2351  *           HTTP_HttpOpenRequestW (internal)
2352  *
2353  * Open a HTTP request handle
2354  *
2355  * RETURNS
2356  *    HINTERNET  a HTTP request handle on success
2357  *    NULL       on failure
2358  *
2359  */
2360 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
2361         LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
2362         LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
2363         DWORD dwFlags, DWORD_PTR dwContext)
2364 {
2365     LPWININETAPPINFOW hIC = NULL;
2366     LPWININETHTTPREQW lpwhr;
2367     LPWSTR lpszHostName = NULL;
2368     HINTERNET handle = NULL;
2369     static const WCHAR szHostForm[] = {'%','s',':','%','u',0};
2370     DWORD len;
2371
2372     TRACE("-->\n");
2373
2374     assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
2375     hIC = lpwhs->lpAppInfo;
2376
2377     lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
2378     if (NULL == lpwhr)
2379     {
2380         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2381         goto lend;
2382     }
2383     lpwhr->hdr.htype = WH_HHTTPREQ;
2384     lpwhr->hdr.vtbl = &HTTPREQVtbl;
2385     lpwhr->hdr.dwFlags = dwFlags;
2386     lpwhr->hdr.dwContext = dwContext;
2387     lpwhr->hdr.refs = 1;
2388     lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
2389     lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
2390     lpwhr->dwContentLength = ~0u;
2391     InitializeCriticalSection( &lpwhr->read_section );
2392
2393     WININET_AddRef( &lpwhs->hdr );
2394     lpwhr->lpHttpSession = lpwhs;
2395     list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
2396
2397     lpszHostName = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) *
2398             (strlenW(lpwhs->lpszHostName) + 7 /* length of ":65535" + 1 */));
2399     if (NULL == lpszHostName)
2400     {
2401         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2402         goto lend;
2403     }
2404
2405     handle = WININET_AllocHandle( &lpwhr->hdr );
2406     if (NULL == handle)
2407     {
2408         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2409         goto lend;
2410     }
2411
2412     if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
2413     {
2414         InternetCloseHandle( handle );
2415         handle = NULL;
2416         goto lend;
2417     }
2418
2419     if (lpszObjectName && *lpszObjectName) {
2420         HRESULT rc;
2421
2422         len = 0;
2423         rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
2424         if (rc != E_POINTER)
2425             len = strlenW(lpszObjectName)+1;
2426         lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2427         rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
2428                    URL_ESCAPE_SPACES_ONLY);
2429         if (rc != S_OK)
2430         {
2431             ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
2432             strcpyW(lpwhr->lpszPath,lpszObjectName);
2433         }
2434     }else {
2435         static const WCHAR slashW[] = {'/',0};
2436
2437         lpwhr->lpszPath = WININET_strdupW(slashW);
2438     }
2439
2440     if (lpszReferrer && *lpszReferrer)
2441         HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2442
2443     if (lpszAcceptTypes)
2444     {
2445         int i;
2446         for (i = 0; lpszAcceptTypes[i]; i++)
2447         {
2448             if (!*lpszAcceptTypes[i]) continue;
2449             HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
2450                                HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
2451                                HTTP_ADDHDR_FLAG_REQ |
2452                                (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
2453         }
2454     }
2455
2456     lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
2457
2458     if (lpszVersion)
2459         lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
2460     else
2461         lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
2462
2463     if (lpwhs->nHostPort != INTERNET_INVALID_PORT_NUMBER &&
2464         lpwhs->nHostPort != INTERNET_DEFAULT_HTTP_PORT &&
2465         lpwhs->nHostPort != INTERNET_DEFAULT_HTTPS_PORT)
2466     {
2467         sprintfW(lpszHostName, szHostForm, lpwhs->lpszHostName, lpwhs->nHostPort);
2468         HTTP_ProcessHeader(lpwhr, hostW, lpszHostName,
2469                 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2470     }
2471     else
2472         HTTP_ProcessHeader(lpwhr, hostW, lpwhs->lpszHostName,
2473                 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2474
2475     if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
2476         lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
2477                         INTERNET_DEFAULT_HTTPS_PORT :
2478                         INTERNET_DEFAULT_HTTP_PORT);
2479
2480     if (lpwhs->nHostPort == INTERNET_INVALID_PORT_NUMBER)
2481         lpwhs->nHostPort = (dwFlags & INTERNET_FLAG_SECURE ?
2482                         INTERNET_DEFAULT_HTTPS_PORT :
2483                         INTERNET_DEFAULT_HTTP_PORT);
2484
2485     if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2486         HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
2487
2488     INTERNET_SendCallback(&lpwhs->hdr, dwContext,
2489                           INTERNET_STATUS_HANDLE_CREATED, &handle,
2490                           sizeof(handle));
2491
2492 lend:
2493     HeapFree(GetProcessHeap(), 0, lpszHostName);
2494     if( lpwhr )
2495         WININET_Release( &lpwhr->hdr );
2496
2497     TRACE("<-- %p (%p)\n", handle, lpwhr);
2498     return handle;
2499 }
2500
2501 /* read any content returned by the server so that the connection can be
2502  * reused */
2503 static void HTTP_DrainContent(WININETHTTPREQW *req)
2504 {
2505     DWORD bytes_read;
2506
2507     if (!NETCON_connected(&req->netConnection)) return;
2508
2509     if (req->dwContentLength == -1)
2510     {
2511         NETCON_close(&req->netConnection);
2512         return;
2513     }
2514
2515     do
2516     {
2517         char buffer[2048];
2518         if (HTTPREQ_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
2519             return;
2520     } while (bytes_read);
2521 }
2522
2523 static const LPCWSTR header_lookup[] = {
2524     szMime_Version,             /* HTTP_QUERY_MIME_VERSION = 0 */
2525     szContent_Type,             /* HTTP_QUERY_CONTENT_TYPE = 1 */
2526     szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2527     szContent_ID,               /* HTTP_QUERY_CONTENT_ID = 3 */
2528     NULL,                       /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2529     szContent_Length,           /* HTTP_QUERY_CONTENT_LENGTH =  5 */
2530     szContent_Language,         /* HTTP_QUERY_CONTENT_LANGUAGE =  6 */
2531     szAllow,                    /* HTTP_QUERY_ALLOW = 7 */
2532     szPublic,                   /* HTTP_QUERY_PUBLIC = 8 */
2533     szDate,                     /* HTTP_QUERY_DATE = 9 */
2534     szExpires,                  /* HTTP_QUERY_EXPIRES = 10 */
2535     szLast_Modified,            /* HTTP_QUERY_LAST_MODIFIED = 11 */
2536     NULL,                       /* HTTP_QUERY_MESSAGE_ID = 12 */
2537     szURI,                      /* HTTP_QUERY_URI = 13 */
2538     szFrom,                     /* HTTP_QUERY_DERIVED_FROM = 14 */
2539     NULL,                       /* HTTP_QUERY_COST = 15 */
2540     NULL,                       /* HTTP_QUERY_LINK = 16 */
2541     szPragma,                   /* HTTP_QUERY_PRAGMA = 17 */
2542     NULL,                       /* HTTP_QUERY_VERSION = 18 */
2543     szStatus,                   /* HTTP_QUERY_STATUS_CODE = 19 */
2544     NULL,                       /* HTTP_QUERY_STATUS_TEXT = 20 */
2545     NULL,                       /* HTTP_QUERY_RAW_HEADERS = 21 */
2546     NULL,                       /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2547     szConnection,               /* HTTP_QUERY_CONNECTION = 23 */
2548     szAccept,                   /* HTTP_QUERY_ACCEPT = 24 */
2549     szAccept_Charset,           /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2550     szAccept_Encoding,          /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2551     szAccept_Language,          /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2552     szAuthorization,            /* HTTP_QUERY_AUTHORIZATION = 28 */
2553     szContent_Encoding,         /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2554     NULL,                       /* HTTP_QUERY_FORWARDED = 30 */
2555     NULL,                       /* HTTP_QUERY_FROM = 31 */
2556     szIf_Modified_Since,        /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2557     szLocation,                 /* HTTP_QUERY_LOCATION = 33 */
2558     NULL,                       /* HTTP_QUERY_ORIG_URI = 34 */
2559     szReferer,                  /* HTTP_QUERY_REFERER = 35 */
2560     szRetry_After,              /* HTTP_QUERY_RETRY_AFTER = 36 */
2561     szServer,                   /* HTTP_QUERY_SERVER = 37 */
2562     NULL,                       /* HTTP_TITLE = 38 */
2563     szUser_Agent,               /* HTTP_QUERY_USER_AGENT = 39 */
2564     szWWW_Authenticate,         /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2565     szProxy_Authenticate,       /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2566     szAccept_Ranges,            /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2567     szSet_Cookie,               /* HTTP_QUERY_SET_COOKIE = 43 */
2568     szCookie,                   /* HTTP_QUERY_COOKIE = 44 */
2569     NULL,                       /* HTTP_QUERY_REQUEST_METHOD = 45 */
2570     NULL,                       /* HTTP_QUERY_REFRESH = 46 */
2571     NULL,                       /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2572     szAge,                      /* HTTP_QUERY_AGE = 48 */
2573     szCache_Control,            /* HTTP_QUERY_CACHE_CONTROL = 49 */
2574     szContent_Base,             /* HTTP_QUERY_CONTENT_BASE = 50 */
2575     szContent_Location,         /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2576     szContent_MD5,              /* HTTP_QUERY_CONTENT_MD5 = 52 */
2577     szContent_Range,            /* HTTP_QUERY_CONTENT_RANGE = 53 */
2578     szETag,                     /* HTTP_QUERY_ETAG = 54 */
2579     hostW,                      /* HTTP_QUERY_HOST = 55 */
2580     szIf_Match,                 /* HTTP_QUERY_IF_MATCH = 56 */
2581     szIf_None_Match,            /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2582     szIf_Range,                 /* HTTP_QUERY_IF_RANGE = 58 */
2583     szIf_Unmodified_Since,      /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2584     szMax_Forwards,             /* HTTP_QUERY_MAX_FORWARDS = 60 */
2585     szProxy_Authorization,      /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2586     szRange,                    /* HTTP_QUERY_RANGE = 62 */
2587     szTransfer_Encoding,        /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2588     szUpgrade,                  /* HTTP_QUERY_UPGRADE = 64 */
2589     szVary,                     /* HTTP_QUERY_VARY = 65 */
2590     szVia,                      /* HTTP_QUERY_VIA = 66 */
2591     szWarning,                  /* HTTP_QUERY_WARNING = 67 */
2592     szExpect,                   /* HTTP_QUERY_EXPECT = 68 */
2593     szProxy_Connection,         /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2594     szUnless_Modified_Since,    /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2595 };
2596
2597 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2598
2599 /***********************************************************************
2600  *           HTTP_HttpQueryInfoW (internal)
2601  */
2602 static BOOL HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
2603         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2604 {
2605     LPHTTPHEADERW lphttpHdr = NULL;
2606     BOOL bSuccess = FALSE;
2607     BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
2608     INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2609     DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2610     INT index = -1;
2611
2612     /* Find requested header structure */
2613     switch (level)
2614     {
2615     case HTTP_QUERY_CUSTOM:
2616         if (!lpBuffer) return FALSE;
2617         index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2618         break;
2619
2620     case HTTP_QUERY_CONTENT_LENGTH:
2621         if(lpwhr->gzip_stream) {
2622             INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2623             return FALSE;
2624         }
2625
2626         index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2627                                           requested_index,request_only);
2628         break;
2629
2630     case HTTP_QUERY_RAW_HEADERS_CRLF:
2631         {
2632             LPWSTR headers;
2633             DWORD len = 0;
2634             BOOL ret = FALSE;
2635
2636             if (request_only)
2637                 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
2638             else
2639                 headers = lpwhr->lpszRawHeaders;
2640
2641             if (headers)
2642                 len = strlenW(headers) * sizeof(WCHAR);
2643
2644             if (len + sizeof(WCHAR) > *lpdwBufferLength)
2645             {
2646                 len += sizeof(WCHAR);
2647                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2648                 ret = FALSE;
2649             }
2650             else if (lpBuffer)
2651             {
2652                 if (headers)
2653                     memcpy(lpBuffer, headers, len + sizeof(WCHAR));
2654                 else
2655                 {
2656                     len = strlenW(szCrLf) * sizeof(WCHAR);
2657                     memcpy(lpBuffer, szCrLf, sizeof(szCrLf));
2658                 }
2659                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
2660                 ret = TRUE;
2661             }
2662             *lpdwBufferLength = len;
2663
2664             if (request_only)
2665                 HeapFree(GetProcessHeap(), 0, headers);
2666             return ret;
2667         }
2668     case HTTP_QUERY_RAW_HEADERS:
2669         {
2670             LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
2671             DWORD i, size = 0;
2672             LPWSTR pszString = lpBuffer;
2673
2674             for (i = 0; ppszRawHeaderLines[i]; i++)
2675                 size += strlenW(ppszRawHeaderLines[i]) + 1;
2676
2677             if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2678             {
2679                 HTTP_FreeTokens(ppszRawHeaderLines);
2680                 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2681                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2682                 return FALSE;
2683             }
2684             if (pszString)
2685             {
2686                 for (i = 0; ppszRawHeaderLines[i]; i++)
2687                 {
2688                     DWORD len = strlenW(ppszRawHeaderLines[i]);
2689                     memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2690                     pszString += len+1;
2691                 }
2692                 *pszString = '\0';
2693                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, size));
2694             }
2695             *lpdwBufferLength = size * sizeof(WCHAR);
2696             HTTP_FreeTokens(ppszRawHeaderLines);
2697
2698             return TRUE;
2699         }
2700     case HTTP_QUERY_STATUS_TEXT:
2701         if (lpwhr->lpszStatusText)
2702         {
2703             DWORD len = strlenW(lpwhr->lpszStatusText);
2704             if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2705             {
2706                 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2707                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2708                 return FALSE;
2709             }
2710             if (lpBuffer)
2711             {
2712                 memcpy(lpBuffer, lpwhr->lpszStatusText, (len + 1) * sizeof(WCHAR));
2713                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2714             }
2715             *lpdwBufferLength = len * sizeof(WCHAR);
2716             return TRUE;
2717         }
2718         break;
2719     case HTTP_QUERY_VERSION:
2720         if (lpwhr->lpszVersion)
2721         {
2722             DWORD len = strlenW(lpwhr->lpszVersion);
2723             if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2724             {
2725                 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2726                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2727                 return FALSE;
2728             }
2729             if (lpBuffer)
2730             {
2731                 memcpy(lpBuffer, lpwhr->lpszVersion, (len + 1) * sizeof(WCHAR));
2732                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2733             }
2734             *lpdwBufferLength = len * sizeof(WCHAR);
2735             return TRUE;
2736         }
2737         break;
2738     case HTTP_QUERY_CONTENT_ENCODING:
2739         index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[lpwhr->gzip_stream ? HTTP_QUERY_CONTENT_TYPE : level],
2740                 requested_index,request_only);
2741         break;
2742     default:
2743         assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2744
2745         if (level < LAST_TABLE_HEADER && header_lookup[level])
2746             index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2747                                               requested_index,request_only);
2748     }
2749
2750     if (index >= 0)
2751         lphttpHdr = &lpwhr->pCustHeaders[index];
2752
2753     /* Ensure header satisfies requested attributes */
2754     if (!lphttpHdr ||
2755         ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2756          (~lphttpHdr->wFlags & HDR_ISREQUEST)))
2757     {
2758         INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2759         return bSuccess;
2760     }
2761
2762     if (lpdwIndex && level != HTTP_QUERY_STATUS_CODE) (*lpdwIndex)++;
2763
2764     /* coalesce value to requested type */
2765     if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
2766     {
2767         *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2768         TRACE(" returning number: %d\n", *(int *)lpBuffer);
2769         bSuccess = TRUE;
2770     }
2771     else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
2772     {
2773         time_t tmpTime;
2774         struct tm tmpTM;
2775         SYSTEMTIME *STHook;
2776
2777         tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2778
2779         tmpTM = *gmtime(&tmpTime);
2780         STHook = (SYSTEMTIME *)lpBuffer;
2781         STHook->wDay = tmpTM.tm_mday;
2782         STHook->wHour = tmpTM.tm_hour;
2783         STHook->wMilliseconds = 0;
2784         STHook->wMinute = tmpTM.tm_min;
2785         STHook->wDayOfWeek = tmpTM.tm_wday;
2786         STHook->wMonth = tmpTM.tm_mon + 1;
2787         STHook->wSecond = tmpTM.tm_sec;
2788         STHook->wYear = tmpTM.tm_year;
2789         bSuccess = TRUE;
2790         
2791         TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2792               STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2793               STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2794     }
2795     else if (lphttpHdr->lpszValue)
2796     {
2797         DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
2798
2799         if (len > *lpdwBufferLength)
2800         {
2801             *lpdwBufferLength = len;
2802             INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2803             return bSuccess;
2804         }
2805         if (lpBuffer)
2806         {
2807             memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2808             TRACE(" returning string: %s\n", debugstr_w(lpBuffer));
2809         }
2810         *lpdwBufferLength = len - sizeof(WCHAR);
2811         bSuccess = TRUE;
2812     }
2813     return bSuccess;
2814 }
2815
2816 /***********************************************************************
2817  *           HttpQueryInfoW (WININET.@)
2818  *
2819  * Queries for information about an HTTP request
2820  *
2821  * RETURNS
2822  *    TRUE  on success
2823  *    FALSE on failure
2824  *
2825  */
2826 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2827         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2828 {
2829     BOOL bSuccess = FALSE;
2830     LPWININETHTTPREQW lpwhr;
2831
2832     if (TRACE_ON(wininet)) {
2833 #define FE(x) { x, #x }
2834         static const wininet_flag_info query_flags[] = {
2835             FE(HTTP_QUERY_MIME_VERSION),
2836             FE(HTTP_QUERY_CONTENT_TYPE),
2837             FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2838             FE(HTTP_QUERY_CONTENT_ID),
2839             FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2840             FE(HTTP_QUERY_CONTENT_LENGTH),
2841             FE(HTTP_QUERY_CONTENT_LANGUAGE),
2842             FE(HTTP_QUERY_ALLOW),
2843             FE(HTTP_QUERY_PUBLIC),
2844             FE(HTTP_QUERY_DATE),
2845             FE(HTTP_QUERY_EXPIRES),
2846             FE(HTTP_QUERY_LAST_MODIFIED),
2847             FE(HTTP_QUERY_MESSAGE_ID),
2848             FE(HTTP_QUERY_URI),
2849             FE(HTTP_QUERY_DERIVED_FROM),
2850             FE(HTTP_QUERY_COST),
2851             FE(HTTP_QUERY_LINK),
2852             FE(HTTP_QUERY_PRAGMA),
2853             FE(HTTP_QUERY_VERSION),
2854             FE(HTTP_QUERY_STATUS_CODE),
2855             FE(HTTP_QUERY_STATUS_TEXT),
2856             FE(HTTP_QUERY_RAW_HEADERS),
2857             FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2858             FE(HTTP_QUERY_CONNECTION),
2859             FE(HTTP_QUERY_ACCEPT),
2860             FE(HTTP_QUERY_ACCEPT_CHARSET),
2861             FE(HTTP_QUERY_ACCEPT_ENCODING),
2862             FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2863             FE(HTTP_QUERY_AUTHORIZATION),
2864             FE(HTTP_QUERY_CONTENT_ENCODING),
2865             FE(HTTP_QUERY_FORWARDED),
2866             FE(HTTP_QUERY_FROM),
2867             FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2868             FE(HTTP_QUERY_LOCATION),
2869             FE(HTTP_QUERY_ORIG_URI),
2870             FE(HTTP_QUERY_REFERER),
2871             FE(HTTP_QUERY_RETRY_AFTER),
2872             FE(HTTP_QUERY_SERVER),
2873             FE(HTTP_QUERY_TITLE),
2874             FE(HTTP_QUERY_USER_AGENT),
2875             FE(HTTP_QUERY_WWW_AUTHENTICATE),
2876             FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2877             FE(HTTP_QUERY_ACCEPT_RANGES),
2878         FE(HTTP_QUERY_SET_COOKIE),
2879         FE(HTTP_QUERY_COOKIE),
2880             FE(HTTP_QUERY_REQUEST_METHOD),
2881             FE(HTTP_QUERY_REFRESH),
2882             FE(HTTP_QUERY_CONTENT_DISPOSITION),
2883             FE(HTTP_QUERY_AGE),
2884             FE(HTTP_QUERY_CACHE_CONTROL),
2885             FE(HTTP_QUERY_CONTENT_BASE),
2886             FE(HTTP_QUERY_CONTENT_LOCATION),
2887             FE(HTTP_QUERY_CONTENT_MD5),
2888             FE(HTTP_QUERY_CONTENT_RANGE),
2889             FE(HTTP_QUERY_ETAG),
2890             FE(HTTP_QUERY_HOST),
2891             FE(HTTP_QUERY_IF_MATCH),
2892             FE(HTTP_QUERY_IF_NONE_MATCH),
2893             FE(HTTP_QUERY_IF_RANGE),
2894             FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2895             FE(HTTP_QUERY_MAX_FORWARDS),
2896             FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2897             FE(HTTP_QUERY_RANGE),
2898             FE(HTTP_QUERY_TRANSFER_ENCODING),
2899             FE(HTTP_QUERY_UPGRADE),
2900             FE(HTTP_QUERY_VARY),
2901             FE(HTTP_QUERY_VIA),
2902             FE(HTTP_QUERY_WARNING),
2903             FE(HTTP_QUERY_CUSTOM)
2904         };
2905         static const wininet_flag_info modifier_flags[] = {
2906             FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2907             FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2908             FE(HTTP_QUERY_FLAG_NUMBER),
2909             FE(HTTP_QUERY_FLAG_COALESCE)
2910         };
2911 #undef FE
2912         DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2913         DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
2914         DWORD i;
2915
2916         TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
2917         TRACE("  Attribute:");
2918         for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2919             if (query_flags[i].val == info) {
2920                 TRACE(" %s", query_flags[i].name);
2921                 break;
2922             }
2923         }
2924         if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
2925             TRACE(" Unknown (%08x)", info);
2926         }
2927
2928         TRACE(" Modifier:");
2929         for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2930             if (modifier_flags[i].val & info_mod) {
2931                 TRACE(" %s", modifier_flags[i].name);
2932                 info_mod &= ~ modifier_flags[i].val;
2933             }
2934         }
2935         
2936         if (info_mod) {
2937             TRACE(" Unknown (%08x)", info_mod);
2938         }
2939         TRACE("\n");
2940     }
2941     
2942     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2943     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
2944     {
2945         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2946         goto lend;
2947     }
2948
2949     if (lpBuffer == NULL)
2950         *lpdwBufferLength = 0;
2951     bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2952                                     lpBuffer, lpdwBufferLength, lpdwIndex);
2953
2954 lend:
2955     if( lpwhr )
2956          WININET_Release( &lpwhr->hdr );
2957
2958     TRACE("%d <--\n", bSuccess);
2959     return bSuccess;
2960 }
2961
2962 /***********************************************************************
2963  *           HttpQueryInfoA (WININET.@)
2964  *
2965  * Queries for information about an HTTP request
2966  *
2967  * RETURNS
2968  *    TRUE  on success
2969  *    FALSE on failure
2970  *
2971  */
2972 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2973         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2974 {
2975     BOOL result;
2976     DWORD len;
2977     WCHAR* bufferW;
2978
2979     if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2980        (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2981     {
2982         return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2983                                lpdwBufferLength, lpdwIndex );
2984     }
2985
2986     if (lpBuffer)
2987     {
2988         DWORD alloclen;
2989         len = (*lpdwBufferLength)*sizeof(WCHAR);
2990         if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2991         {
2992             alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2993             if (alloclen < len)
2994                 alloclen = len;
2995         }
2996         else
2997             alloclen = len;
2998         bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
2999         /* buffer is in/out because of HTTP_QUERY_CUSTOM */
3000         if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
3001             MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
3002     } else
3003     {
3004         bufferW = NULL;
3005         len = 0;
3006     }
3007
3008     result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
3009                            &len, lpdwIndex );
3010     if( result )
3011     {
3012         len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
3013                                      lpBuffer, *lpdwBufferLength, NULL, NULL );
3014         *lpdwBufferLength = len - 1;
3015
3016         TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
3017     }
3018     else
3019         /* since the strings being returned from HttpQueryInfoW should be
3020          * only ASCII characters, it is reasonable to assume that all of
3021          * the Unicode characters can be reduced to a single byte */
3022         *lpdwBufferLength = len / sizeof(WCHAR);
3023
3024     HeapFree(GetProcessHeap(), 0, bufferW );
3025
3026     return result;
3027 }
3028
3029 /***********************************************************************
3030  *           HttpSendRequestExA (WININET.@)
3031  *
3032  * Sends the specified request to the HTTP server and allows chunked
3033  * transfers.
3034  *
3035  * RETURNS
3036  *  Success: TRUE
3037  *  Failure: FALSE, call GetLastError() for more information.
3038  */
3039 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
3040                                LPINTERNET_BUFFERSA lpBuffersIn,
3041                                LPINTERNET_BUFFERSA lpBuffersOut,
3042                                DWORD dwFlags, DWORD_PTR dwContext)
3043 {
3044     INTERNET_BUFFERSW BuffersInW;
3045     BOOL rc = FALSE;
3046     DWORD headerlen;
3047     LPWSTR header = NULL;
3048
3049     TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
3050             lpBuffersOut, dwFlags, dwContext);
3051
3052     if (lpBuffersIn)
3053     {
3054         BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
3055         if (lpBuffersIn->lpcszHeader)
3056         {
3057             headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
3058                     lpBuffersIn->dwHeadersLength,0,0);
3059             header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
3060             if (!(BuffersInW.lpcszHeader = header))
3061             {
3062                 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3063                 return FALSE;
3064             }
3065             BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
3066                     lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
3067                     header, headerlen);
3068         }
3069         else
3070             BuffersInW.lpcszHeader = NULL;
3071         BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
3072         BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
3073         BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
3074         BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
3075         BuffersInW.Next = NULL;
3076     }
3077
3078     rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
3079
3080     HeapFree(GetProcessHeap(),0,header);
3081
3082     return rc;
3083 }
3084
3085 /***********************************************************************
3086  *           HttpSendRequestExW (WININET.@)
3087  *
3088  * Sends the specified request to the HTTP server and allows chunked
3089  * transfers
3090  *
3091  * RETURNS
3092  *  Success: TRUE
3093  *  Failure: FALSE, call GetLastError() for more information.
3094  */
3095 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
3096                    LPINTERNET_BUFFERSW lpBuffersIn,
3097                    LPINTERNET_BUFFERSW lpBuffersOut,
3098                    DWORD dwFlags, DWORD_PTR dwContext)
3099 {
3100     BOOL ret = FALSE;
3101     LPWININETHTTPREQW lpwhr;
3102     LPWININETHTTPSESSIONW lpwhs;
3103     LPWININETAPPINFOW hIC;
3104
3105     TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
3106             lpBuffersOut, dwFlags, dwContext);
3107
3108     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
3109
3110     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3111     {
3112         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3113         goto lend;
3114     }
3115
3116     lpwhs = lpwhr->lpHttpSession;
3117     assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
3118     hIC = lpwhs->lpAppInfo;
3119     assert(hIC->hdr.htype == WH_HINIT);
3120
3121     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3122     {
3123         WORKREQUEST workRequest;
3124         struct WORKREQ_HTTPSENDREQUESTW *req;
3125
3126         workRequest.asyncproc = AsyncHttpSendRequestProc;
3127         workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
3128         req = &workRequest.u.HttpSendRequestW;
3129         if (lpBuffersIn)
3130         {
3131             if (lpBuffersIn->lpcszHeader)
3132                 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
3133                 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
3134             else
3135                 req->lpszHeader = NULL;
3136             req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
3137             req->lpOptional = lpBuffersIn->lpvBuffer;
3138             req->dwOptionalLength = lpBuffersIn->dwBufferLength;
3139             req->dwContentLength = lpBuffersIn->dwBufferTotal;
3140         }
3141         else
3142         {
3143             req->lpszHeader = NULL;
3144             req->dwHeaderLength = 0;
3145             req->lpOptional = NULL;
3146             req->dwOptionalLength = 0;
3147             req->dwContentLength = 0;
3148         }
3149
3150         req->bEndRequest = FALSE;
3151
3152         INTERNET_AsyncCall(&workRequest);
3153         /*
3154          * This is from windows.
3155          */
3156         INTERNET_SetLastError(ERROR_IO_PENDING);
3157     }
3158     else
3159     {
3160         if (lpBuffersIn)
3161             ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
3162                                         lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
3163                                         lpBuffersIn->dwBufferTotal, FALSE);
3164         else
3165             ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
3166     }
3167
3168 lend:
3169     if ( lpwhr )
3170         WININET_Release( &lpwhr->hdr );
3171
3172     TRACE("<---\n");
3173     return ret;
3174 }
3175
3176 /***********************************************************************
3177  *           HttpSendRequestW (WININET.@)
3178  *
3179  * Sends the specified request to the HTTP server
3180  *
3181  * RETURNS
3182  *    TRUE  on success
3183  *    FALSE on failure
3184  *
3185  */
3186 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
3187         DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
3188 {
3189     LPWININETHTTPREQW lpwhr;
3190     LPWININETHTTPSESSIONW lpwhs = NULL;
3191     LPWININETAPPINFOW hIC = NULL;
3192     BOOL r;
3193
3194     TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
3195             debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
3196
3197     lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
3198     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3199     {
3200         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3201         r = FALSE;
3202         goto lend;
3203     }
3204
3205     lpwhs = lpwhr->lpHttpSession;
3206     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
3207     {
3208         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3209         r = FALSE;
3210         goto lend;
3211     }
3212
3213     hIC = lpwhs->lpAppInfo;
3214     if (NULL == hIC ||  hIC->hdr.htype != WH_HINIT)
3215     {
3216         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3217         r = FALSE;
3218         goto lend;
3219     }
3220
3221     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3222     {
3223         WORKREQUEST workRequest;
3224         struct WORKREQ_HTTPSENDREQUESTW *req;
3225
3226         workRequest.asyncproc = AsyncHttpSendRequestProc;
3227         workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
3228         req = &workRequest.u.HttpSendRequestW;
3229         if (lpszHeaders)
3230         {
3231             DWORD size;
3232
3233             if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
3234             else size = dwHeaderLength * sizeof(WCHAR);
3235
3236             req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, size);
3237             memcpy(req->lpszHeader, lpszHeaders, size);
3238         }
3239         else
3240             req->lpszHeader = 0;
3241         req->dwHeaderLength = dwHeaderLength;
3242         req->lpOptional = lpOptional;
3243         req->dwOptionalLength = dwOptionalLength;
3244         req->dwContentLength = dwOptionalLength;
3245         req->bEndRequest = TRUE;
3246
3247         INTERNET_AsyncCall(&workRequest);
3248         /*
3249          * This is from windows.
3250          */
3251         INTERNET_SetLastError(ERROR_IO_PENDING);
3252         r = FALSE;
3253     }
3254     else
3255     {
3256         r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
3257                 dwHeaderLength, lpOptional, dwOptionalLength,
3258                 dwOptionalLength, TRUE);
3259     }
3260 lend:
3261     if( lpwhr )
3262         WININET_Release( &lpwhr->hdr );
3263     return r;
3264 }
3265
3266 /***********************************************************************
3267  *           HttpSendRequestA (WININET.@)
3268  *
3269  * Sends the specified request to the HTTP server
3270  *
3271  * RETURNS
3272  *    TRUE  on success
3273  *    FALSE on failure
3274  *
3275  */
3276 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
3277         DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
3278 {
3279     BOOL result;
3280     LPWSTR szHeaders=NULL;
3281     DWORD nLen=dwHeaderLength;
3282     if(lpszHeaders!=NULL)
3283     {
3284         nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
3285         szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
3286         MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
3287     }
3288     result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
3289     HeapFree(GetProcessHeap(),0,szHeaders);
3290     return result;
3291 }
3292
3293 /***********************************************************************
3294  *           HTTP_GetRedirectURL (internal)
3295  */
3296 static LPWSTR HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
3297 {
3298     static WCHAR szHttp[] = {'h','t','t','p',0};
3299     static WCHAR szHttps[] = {'h','t','t','p','s',0};
3300     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3301     URL_COMPONENTSW urlComponents;
3302     DWORD url_length = 0;
3303     LPWSTR orig_url;
3304     LPWSTR combined_url;
3305
3306     urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3307     urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
3308     urlComponents.dwSchemeLength = 0;
3309     urlComponents.lpszHostName = lpwhs->lpszHostName;
3310     urlComponents.dwHostNameLength = 0;
3311     urlComponents.nPort = lpwhs->nHostPort;
3312     urlComponents.lpszUserName = lpwhs->lpszUserName;
3313     urlComponents.dwUserNameLength = 0;
3314     urlComponents.lpszPassword = NULL;
3315     urlComponents.dwPasswordLength = 0;
3316     urlComponents.lpszUrlPath = lpwhr->lpszPath;
3317     urlComponents.dwUrlPathLength = 0;
3318     urlComponents.lpszExtraInfo = NULL;
3319     urlComponents.dwExtraInfoLength = 0;
3320
3321     if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
3322         (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
3323         return NULL;
3324
3325     orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
3326
3327     /* convert from bytes to characters */
3328     url_length = url_length / sizeof(WCHAR) - 1;
3329     if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
3330     {
3331         HeapFree(GetProcessHeap(), 0, orig_url);
3332         return NULL;
3333     }
3334
3335     url_length = 0;
3336     if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
3337         (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
3338     {
3339         HeapFree(GetProcessHeap(), 0, orig_url);
3340         return NULL;
3341     }
3342     combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
3343
3344     if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
3345     {
3346         HeapFree(GetProcessHeap(), 0, orig_url);
3347         HeapFree(GetProcessHeap(), 0, combined_url);
3348         return NULL;
3349     }
3350     HeapFree(GetProcessHeap(), 0, orig_url);
3351     return combined_url;
3352 }
3353
3354
3355 /***********************************************************************
3356  *           HTTP_HandleRedirect (internal)
3357  */
3358 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
3359 {
3360     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3361     LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
3362     BOOL using_proxy = hIC->lpszProxy && hIC->lpszProxy[0];
3363     WCHAR path[INTERNET_MAX_URL_LENGTH];
3364     int index;
3365
3366     if(lpszUrl[0]=='/')
3367     {
3368         /* if it's an absolute path, keep the same session info */
3369         lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH);
3370     }
3371     else
3372     {
3373         URL_COMPONENTSW urlComponents;
3374         WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
3375         static WCHAR szHttp[] = {'h','t','t','p',0};
3376         static WCHAR szHttps[] = {'h','t','t','p','s',0};
3377
3378         userName[0] = 0;
3379         hostName[0] = 0;
3380         protocol[0] = 0;
3381
3382         urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3383         urlComponents.lpszScheme = protocol;
3384         urlComponents.dwSchemeLength = 32;
3385         urlComponents.lpszHostName = hostName;
3386         urlComponents.dwHostNameLength = MAXHOSTNAME;
3387         urlComponents.lpszUserName = userName;
3388         urlComponents.dwUserNameLength = 1024;
3389         urlComponents.lpszPassword = NULL;
3390         urlComponents.dwPasswordLength = 0;
3391         urlComponents.lpszUrlPath = path;
3392         urlComponents.dwUrlPathLength = 2048;
3393         urlComponents.lpszExtraInfo = NULL;
3394         urlComponents.dwExtraInfoLength = 0;
3395         if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3396             return FALSE;
3397
3398         if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
3399             (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3400         {
3401             TRACE("redirect from secure page to non-secure page\n");
3402             /* FIXME: warn about from secure redirect to non-secure page */
3403             lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
3404         }
3405         if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
3406             !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3407         {
3408             TRACE("redirect from non-secure page to secure page\n");
3409             /* FIXME: notify about redirect to secure page */
3410             lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
3411         }
3412
3413         if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
3414         {
3415             if (lstrlenW(protocol)>4) /*https*/
3416                 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
3417             else /*http*/
3418                 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
3419         }
3420
3421 #if 0
3422         /*
3423          * This upsets redirects to binary files on sourceforge.net 
3424          * and gives an html page instead of the target file
3425          * Examination of the HTTP request sent by native wininet.dll
3426          * reveals that it doesn't send a referrer in that case.
3427          * Maybe there's a flag that enables this, or maybe a referrer
3428          * shouldn't be added in case of a redirect.
3429          */
3430
3431         /* consider the current host as the referrer */
3432         if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
3433             HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
3434                            HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
3435                            HTTP_ADDHDR_FLAG_ADD_IF_NEW);
3436 #endif
3437         
3438         HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3439         if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
3440             urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
3441         {
3442             int len;
3443             static const WCHAR fmt[] = {'%','s',':','%','i',0};
3444             len = lstrlenW(hostName);
3445             len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
3446             lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
3447             sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
3448         }
3449         else
3450             lpwhs->lpszHostName = WININET_strdupW(hostName);
3451
3452         HTTP_ProcessHeader(lpwhr, hostW, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
3453
3454         HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3455         lpwhs->lpszUserName = NULL;
3456         if (userName[0])
3457             lpwhs->lpszUserName = WININET_strdupW(userName);
3458
3459         if (!using_proxy)
3460         {
3461             if (strcmpiW(lpwhs->lpszServerName, hostName) || lpwhs->nServerPort != urlComponents.nPort)
3462             {
3463                 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3464                 lpwhs->lpszServerName = WININET_strdupW(hostName);
3465                 lpwhs->nServerPort = urlComponents.nPort;
3466
3467                 NETCON_close(&lpwhr->netConnection);
3468                 if (!HTTP_ResolveName(lpwhr)) return FALSE;
3469                 if (!NETCON_init(&lpwhr->netConnection, lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)) return FALSE;
3470                 lpwhr->read_pos = lpwhr->read_size = 0;
3471                 lpwhr->read_chunked = FALSE;
3472             }
3473         }
3474         else
3475             TRACE("Redirect through proxy\n");
3476     }
3477
3478     HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
3479     lpwhr->lpszPath=NULL;
3480     if (*path)
3481     {
3482         DWORD needed = 0;
3483         HRESULT rc;
3484
3485         rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
3486         if (rc != E_POINTER)
3487             needed = strlenW(path)+1;
3488         lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
3489         rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
3490                         URL_ESCAPE_SPACES_ONLY);
3491         if (rc != S_OK)
3492         {
3493             ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
3494             strcpyW(lpwhr->lpszPath,path);
3495         }
3496     }
3497
3498     /* Remove custom content-type/length headers on redirects.  */
3499     index = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Type, 0, TRUE);
3500     if (0 <= index)
3501         HTTP_DeleteCustomHeader(lpwhr, index);
3502     index = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Length, 0, TRUE);
3503     if (0 <= index)
3504         HTTP_DeleteCustomHeader(lpwhr, index);
3505
3506     return TRUE;
3507 }
3508
3509 /***********************************************************************
3510  *           HTTP_build_req (internal)
3511  *
3512  *  concatenate all the strings in the request together
3513  */
3514 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
3515 {
3516     LPCWSTR *t;
3517     LPWSTR str;
3518
3519     for( t = list; *t ; t++  )
3520         len += strlenW( *t );
3521     len++;
3522
3523     str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
3524     *str = 0;
3525
3526     for( t = list; *t ; t++ )
3527         strcatW( str, *t );
3528
3529     return str;
3530 }
3531
3532 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
3533 {
3534     LPWSTR lpszPath;
3535     LPWSTR requestString;
3536     INT len;
3537     INT cnt;
3538     INT responseLen;
3539     char *ascii_req;
3540     BOOL ret;
3541     static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
3542     static const WCHAR szFormat[] = {'%','s',':','%','d',0};
3543     LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3544
3545     TRACE("\n");
3546
3547     lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
3548     sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
3549     requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
3550     HeapFree( GetProcessHeap(), 0, lpszPath );
3551
3552     len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3553                                 NULL, 0, NULL, NULL );
3554     len--; /* the nul terminator isn't needed */
3555     ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
3556     WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3557                             ascii_req, len, NULL, NULL );
3558     HeapFree( GetProcessHeap(), 0, requestString );
3559
3560     TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
3561
3562     ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
3563     HeapFree( GetProcessHeap(), 0, ascii_req );
3564     if (!ret || cnt < 0)
3565         return FALSE;
3566
3567     responseLen = HTTP_GetResponseHeaders( lpwhr, TRUE );
3568     if (!responseLen)
3569         return FALSE;
3570
3571     return TRUE;
3572 }
3573
3574 static void HTTP_InsertCookies(LPWININETHTTPREQW lpwhr)
3575 {
3576     static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3577     LPWSTR lpszCookies, lpszUrl = NULL;
3578     DWORD nCookieSize, size;
3579     LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr, hostW);
3580
3581     size = (strlenW(Host->lpszValue) + strlenW(szUrlForm) + strlenW(lpwhr->lpszPath)) * sizeof(WCHAR);
3582     if (!(lpszUrl = HeapAlloc(GetProcessHeap(), 0, size))) return;
3583     sprintfW( lpszUrl, szUrlForm, Host->lpszValue, lpwhr->lpszPath);
3584
3585     if (InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
3586     {
3587         int cnt = 0;
3588         static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
3589
3590         size = sizeof(szCookie) + nCookieSize * sizeof(WCHAR) + sizeof(szCrLf);
3591         if ((lpszCookies = HeapAlloc(GetProcessHeap(), 0, size)))
3592         {
3593             cnt += sprintfW(lpszCookies, szCookie);
3594             InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
3595             strcatW(lpszCookies, szCrLf);
3596
3597             HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies), HTTP_ADDREQ_FLAG_REPLACE);
3598             HeapFree(GetProcessHeap(), 0, lpszCookies);
3599         }
3600     }
3601     HeapFree(GetProcessHeap(), 0, lpszUrl);
3602 }
3603
3604 /***********************************************************************
3605  *           HTTP_HttpSendRequestW (internal)
3606  *
3607  * Sends the specified request to the HTTP server
3608  *
3609  * RETURNS
3610  *    TRUE  on success
3611  *    FALSE on failure
3612  *
3613  */
3614 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
3615         DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
3616         DWORD dwContentLength, BOOL bEndRequest)
3617 {
3618     INT cnt;
3619     BOOL bSuccess = FALSE, redirected = FALSE;
3620     LPWSTR requestString = NULL;
3621     INT responseLen;
3622     BOOL loop_next;
3623     INTERNET_ASYNC_RESULT iar;
3624     static const WCHAR szPost[] = { 'P','O','S','T',0 };
3625     static const WCHAR szContentLength[] =
3626         { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3627     WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
3628
3629     TRACE("--> %p\n", lpwhr);
3630
3631     assert(lpwhr->hdr.htype == WH_HHTTPREQ);
3632
3633     /* if the verb is NULL default to GET */
3634     if (!lpwhr->lpszVerb)
3635         lpwhr->lpszVerb = WININET_strdupW(szGET);
3636
3637     if (dwContentLength || strcmpW(lpwhr->lpszVerb, szGET))
3638     {
3639         sprintfW(contentLengthStr, szContentLength, dwContentLength);
3640         HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_REPLACE);
3641         lpwhr->dwBytesToWrite = dwContentLength;
3642     }
3643     if (lpwhr->lpHttpSession->lpAppInfo->lpszAgent)
3644     {
3645         WCHAR *agent_header;
3646         static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
3647         int len;
3648
3649         len = strlenW(lpwhr->lpHttpSession->lpAppInfo->lpszAgent) + strlenW(user_agent);
3650         agent_header = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3651         sprintfW(agent_header, user_agent, lpwhr->lpHttpSession->lpAppInfo->lpszAgent);
3652
3653         HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3654         HeapFree(GetProcessHeap(), 0, agent_header);
3655     }
3656     if (lpwhr->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
3657     {
3658         static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
3659         HTTP_HttpAddRequestHeadersW(lpwhr, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3660     }
3661     if ((lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && !strcmpW(lpwhr->lpszVerb, szPost))
3662     {
3663         static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
3664                                               ' ','n','o','-','c','a','c','h','e','\r','\n',0};
3665         HTTP_HttpAddRequestHeadersW(lpwhr, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3666     }
3667
3668     do
3669     {
3670         DWORD len;
3671         char *ascii_req;
3672
3673         loop_next = FALSE;
3674
3675         /* like native, just in case the caller forgot to call InternetReadFile
3676          * for all the data */
3677         HTTP_DrainContent(lpwhr);
3678         lpwhr->dwContentRead = 0;
3679
3680         if (TRACE_ON(wininet))
3681         {
3682             LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr, hostW);
3683             TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
3684         }
3685
3686         HTTP_FixURL(lpwhr);
3687         if (lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
3688         {
3689             HTTP_ProcessHeader(lpwhr, szConnection, szKeepAlive, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
3690         }
3691         HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
3692         HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
3693
3694         if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
3695             HTTP_InsertCookies(lpwhr);
3696
3697         /* add the headers the caller supplied */
3698         if( lpszHeaders && dwHeaderLength )
3699         {
3700             HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
3701                         HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3702         }
3703
3704         if (lpwhr->lpHttpSession->lpAppInfo->lpszProxy && lpwhr->lpHttpSession->lpAppInfo->lpszProxy[0])
3705         {
3706             WCHAR *url = HTTP_BuildProxyRequestUrl(lpwhr);
3707             requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, url, lpwhr->lpszVersion);
3708             HeapFree(GetProcessHeap(), 0, url);
3709         }
3710         else
3711             requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
3712
3713  
3714         TRACE("Request header -> %s\n", debugstr_w(requestString) );
3715
3716         /* Send the request and store the results */
3717         if (!HTTP_OpenConnection(lpwhr))
3718             goto lend;
3719
3720         /* send the request as ASCII, tack on the optional data */
3721         if (!lpOptional || redirected)
3722             dwOptionalLength = 0;
3723         len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3724                                    NULL, 0, NULL, NULL );
3725         ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
3726         WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3727                              ascii_req, len, NULL, NULL );
3728         if( lpOptional )
3729             memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
3730         len = (len + dwOptionalLength - 1);
3731         ascii_req[len] = 0;
3732         TRACE("full request -> %s\n", debugstr_a(ascii_req) );
3733
3734         INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3735                               INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3736
3737         NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
3738         HeapFree( GetProcessHeap(), 0, ascii_req );
3739
3740         lpwhr->dwBytesWritten = dwOptionalLength;
3741
3742         INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3743                               INTERNET_STATUS_REQUEST_SENT,
3744                               &len, sizeof(DWORD));
3745
3746         if (bEndRequest)
3747         {
3748             DWORD dwBufferSize;
3749             DWORD dwStatusCode;
3750
3751             INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3752                                 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3753     
3754             if (cnt < 0)
3755                 goto lend;
3756     
3757             responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
3758             if (responseLen)
3759                 bSuccess = TRUE;
3760     
3761             INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3762                                 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
3763                                 sizeof(DWORD));
3764
3765             HTTP_ProcessCookies(lpwhr);
3766
3767             if (!set_content_length( lpwhr )) HTTP_FinishedReading(lpwhr);
3768
3769             dwBufferSize = sizeof(dwStatusCode);
3770             if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
3771                                      &dwStatusCode,&dwBufferSize,NULL))
3772                 dwStatusCode = 0;
3773
3774             if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
3775             {
3776                 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
3777                 dwBufferSize=sizeof(szNewLocation);
3778                 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
3779                     HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
3780                 {
3781                     /* redirects are always GETs */
3782                     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
3783                     lpwhr->lpszVerb = WININET_strdupW(szGET);
3784
3785                     HTTP_DrainContent(lpwhr);
3786                     if ((new_url = HTTP_GetRedirectURL( lpwhr, szNewLocation )))
3787                     {
3788                         INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REDIRECT,
3789                                               new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
3790                         bSuccess = HTTP_HandleRedirect(lpwhr, new_url);
3791                         if (bSuccess)
3792                         {
3793                             HeapFree(GetProcessHeap(), 0, requestString);
3794                             loop_next = TRUE;
3795                         }
3796                         HeapFree( GetProcessHeap(), 0, new_url );
3797                     }
3798                     redirected = TRUE;
3799                 }
3800             }
3801             if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
3802             {
3803                 WCHAR szAuthValue[2048];
3804                 dwBufferSize=2048;
3805                 if (dwStatusCode == HTTP_STATUS_DENIED)
3806                 {
3807                     DWORD dwIndex = 0;
3808                     while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3809                     {
3810                         if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3811                                                  &lpwhr->pAuthInfo,
3812                                                  lpwhr->lpHttpSession->lpszUserName,
3813                                                  lpwhr->lpHttpSession->lpszPassword))
3814                         {
3815                             loop_next = TRUE;
3816                             break;
3817                         }
3818                     }
3819                 }
3820                 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
3821                 {
3822                     DWORD dwIndex = 0;
3823                     while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3824                     {
3825                         if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3826                                                  &lpwhr->pProxyAuthInfo,
3827                                                  lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
3828                                                  lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
3829                         {
3830                             loop_next = TRUE;
3831                             break;
3832                         }
3833                     }
3834                 }
3835             }
3836         }
3837         else
3838             bSuccess = TRUE;
3839     }
3840     while (loop_next);
3841
3842     if(bSuccess) {
3843         WCHAR url[INTERNET_MAX_URL_LENGTH];
3844         WCHAR cacheFileName[MAX_PATH+1];
3845         BOOL b;
3846
3847         b = HTTP_GetRequestURL(lpwhr, url);
3848         if(!b) {
3849             WARN("Could not get URL\n");
3850             goto lend;
3851         }
3852
3853         b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
3854         if(b) {
3855             lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
3856             lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
3857                       NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3858             if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
3859                 WARN("Could not create file: %u\n", GetLastError());
3860                 lpwhr->hCacheFile = NULL;
3861             }
3862         }else {
3863             WARN("Could not create cache entry: %08x\n", GetLastError());
3864         }
3865     }
3866
3867 lend:
3868
3869     HeapFree(GetProcessHeap(), 0, requestString);
3870
3871     /* TODO: send notification for P3P header */
3872
3873     if (lpwhr->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3874     {
3875         if (bSuccess)
3876         {
3877             if (lpwhr->dwBytesWritten == lpwhr->dwBytesToWrite) HTTP_ReceiveRequestData(lpwhr, TRUE);
3878             else
3879             {
3880                 iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
3881                 iar.dwError = 0;
3882
3883                 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3884                                   INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3885                                   sizeof(INTERNET_ASYNC_RESULT));
3886             }
3887         }
3888         else
3889         {
3890             iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
3891             iar.dwError = INTERNET_GetLastError();
3892
3893             INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3894                                   INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3895                                   sizeof(INTERNET_ASYNC_RESULT));
3896         }
3897     }
3898
3899     TRACE("<--\n");
3900     if (bSuccess) INTERNET_SetLastError(ERROR_SUCCESS);
3901     return bSuccess;
3902 }
3903
3904 /***********************************************************************
3905  *           HTTPSESSION_Destroy (internal)
3906  *
3907  * Deallocate session handle
3908  *
3909  */
3910 static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3911 {
3912     LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3913
3914     TRACE("%p\n", lpwhs);
3915
3916     WININET_Release(&lpwhs->lpAppInfo->hdr);
3917
3918     HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3919     HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3920     HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3921     HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3922     HeapFree(GetProcessHeap(), 0, lpwhs);
3923 }
3924
3925 static DWORD HTTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3926 {
3927     switch(option) {
3928     case INTERNET_OPTION_HANDLE_TYPE:
3929         TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3930
3931         if (*size < sizeof(ULONG))
3932             return ERROR_INSUFFICIENT_BUFFER;
3933
3934         *size = sizeof(DWORD);
3935         *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
3936         return ERROR_SUCCESS;
3937     }
3938
3939     return INET_QueryOption(option, buffer, size, unicode);
3940 }
3941
3942 static DWORD HTTPSESSION_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
3943 {
3944     WININETHTTPSESSIONW *ses = (WININETHTTPSESSIONW*)hdr;
3945
3946     switch(option) {
3947     case INTERNET_OPTION_USERNAME:
3948     {
3949         HeapFree(GetProcessHeap(), 0, ses->lpszUserName);
3950         if (!(ses->lpszUserName = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
3951         return ERROR_SUCCESS;
3952     }
3953     case INTERNET_OPTION_PASSWORD:
3954     {
3955         HeapFree(GetProcessHeap(), 0, ses->lpszPassword);
3956         if (!(ses->lpszPassword = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
3957         return ERROR_SUCCESS;
3958     }
3959     default: break;
3960     }
3961
3962     return ERROR_INTERNET_INVALID_OPTION;
3963 }
3964
3965 static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
3966     HTTPSESSION_Destroy,
3967     NULL,
3968     HTTPSESSION_QueryOption,
3969     HTTPSESSION_SetOption,
3970     NULL,
3971     NULL,
3972     NULL,
3973     NULL,
3974     NULL
3975 };
3976
3977
3978 /***********************************************************************
3979  *           HTTP_Connect  (internal)
3980  *
3981  * Create http session handle
3982  *
3983  * RETURNS
3984  *   HINTERNET a session handle on success
3985  *   NULL on failure
3986  *
3987  */
3988 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
3989         INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
3990         LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
3991         DWORD dwInternalFlags)
3992 {
3993     LPWININETHTTPSESSIONW lpwhs = NULL;
3994     HINTERNET handle = NULL;
3995
3996     TRACE("-->\n");
3997
3998     if (!lpszServerName || !lpszServerName[0])
3999     {
4000         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4001         goto lerror;
4002     }
4003
4004     assert( hIC->hdr.htype == WH_HINIT );
4005
4006     lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
4007     if (NULL == lpwhs)
4008     {
4009         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4010         goto lerror;
4011     }
4012
4013    /*
4014     * According to my tests. The name is not resolved until a request is sent
4015     */
4016
4017     lpwhs->hdr.htype = WH_HHTTPSESSION;
4018     lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
4019     lpwhs->hdr.dwFlags = dwFlags;
4020     lpwhs->hdr.dwContext = dwContext;
4021     lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
4022     lpwhs->hdr.refs = 1;
4023     lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
4024
4025     WININET_AddRef( &hIC->hdr );
4026     lpwhs->lpAppInfo = hIC;
4027     list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
4028
4029     handle = WININET_AllocHandle( &lpwhs->hdr );
4030     if (NULL == handle)
4031     {
4032         ERR("Failed to alloc handle\n");
4033         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4034         goto lerror;
4035     }
4036
4037     if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
4038         if(strchrW(hIC->lpszProxy, ' '))
4039             FIXME("Several proxies not implemented.\n");
4040         if(hIC->lpszProxyBypass)
4041             FIXME("Proxy bypass is ignored.\n");
4042     }
4043     if (lpszServerName && lpszServerName[0])
4044     {
4045         lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
4046         lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
4047     }
4048     if (lpszUserName && lpszUserName[0])
4049         lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
4050     if (lpszPassword && lpszPassword[0])
4051         lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
4052     lpwhs->nServerPort = nServerPort;
4053     lpwhs->nHostPort = nServerPort;
4054
4055     /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
4056     if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
4057     {
4058         INTERNET_SendCallback(&hIC->hdr, dwContext,
4059                               INTERNET_STATUS_HANDLE_CREATED, &handle,
4060                               sizeof(handle));
4061     }
4062
4063 lerror:
4064     if( lpwhs )
4065         WININET_Release( &lpwhs->hdr );
4066
4067 /*
4068  * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
4069  * windows
4070  */
4071
4072     TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
4073     return handle;
4074 }
4075
4076
4077 /***********************************************************************
4078  *           HTTP_OpenConnection (internal)
4079  *
4080  * Connect to a web server
4081  *
4082  * RETURNS
4083  *
4084  *   TRUE  on success
4085  *   FALSE on failure
4086  */
4087 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
4088 {
4089     BOOL bSuccess = FALSE;
4090     LPWININETHTTPSESSIONW lpwhs;
4091     LPWININETAPPINFOW hIC = NULL;
4092     char szaddr[32];
4093
4094     TRACE("-->\n");
4095
4096
4097     if (lpwhr->hdr.htype != WH_HHTTPREQ)
4098     {
4099         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
4100         goto lend;
4101     }
4102
4103     if (NETCON_connected(&lpwhr->netConnection))
4104     {
4105         bSuccess = TRUE;
4106         goto lend;
4107     }
4108     if (!HTTP_ResolveName(lpwhr)) goto lend;
4109
4110     lpwhs = lpwhr->lpHttpSession;
4111
4112     hIC = lpwhs->lpAppInfo;
4113     inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
4114               szaddr, sizeof(szaddr));
4115     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
4116                           INTERNET_STATUS_CONNECTING_TO_SERVER,
4117                           szaddr,
4118                           strlen(szaddr)+1);
4119
4120     if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
4121                          SOCK_STREAM, 0))
4122     {
4123         WARN("Socket creation failed: %u\n", INTERNET_GetLastError());
4124         goto lend;
4125     }
4126
4127     if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
4128                       sizeof(lpwhs->socketAddress)))
4129        goto lend;
4130
4131     if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
4132     {
4133         /* Note: we differ from Microsoft's WinINet here. they seem to have
4134          * a bug that causes no status callbacks to be sent when starting
4135          * a tunnel to a proxy server using the CONNECT verb. i believe our
4136          * behaviour to be more correct and to not cause any incompatibilities
4137          * because using a secure connection through a proxy server is a rare
4138          * case that would be hard for anyone to depend on */
4139         if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
4140             goto lend;
4141
4142         if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
4143         {
4144             WARN("Couldn't connect securely to host\n");
4145             goto lend;
4146         }
4147     }
4148
4149     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
4150                           INTERNET_STATUS_CONNECTED_TO_SERVER,
4151                           szaddr, strlen(szaddr)+1);
4152
4153     bSuccess = TRUE;
4154
4155 lend:
4156     lpwhr->read_pos = lpwhr->read_size = 0;
4157     lpwhr->read_chunked = FALSE;
4158
4159     TRACE("%d <--\n", bSuccess);
4160     return bSuccess;
4161 }
4162
4163
4164 /***********************************************************************
4165  *           HTTP_clear_response_headers (internal)
4166  *
4167  * clear out any old response headers
4168  */
4169 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
4170 {
4171     DWORD i;
4172
4173     for( i=0; i<lpwhr->nCustHeaders; i++)
4174     {
4175         if( !lpwhr->pCustHeaders[i].lpszField )
4176             continue;
4177         if( !lpwhr->pCustHeaders[i].lpszValue )
4178             continue;
4179         if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
4180             continue;
4181         HTTP_DeleteCustomHeader( lpwhr, i );
4182         i--;
4183     }
4184 }
4185
4186 /***********************************************************************
4187  *           HTTP_GetResponseHeaders (internal)
4188  *
4189  * Read server response
4190  *
4191  * RETURNS
4192  *
4193  *   TRUE  on success
4194  *   FALSE on error
4195  */
4196 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear)
4197 {
4198     INT cbreaks = 0;
4199     WCHAR buffer[MAX_REPLY_LEN];
4200     DWORD buflen = MAX_REPLY_LEN;
4201     BOOL bSuccess = FALSE;
4202     INT  rc = 0;
4203     static const WCHAR szHundred[] = {'1','0','0',0};
4204     char bufferA[MAX_REPLY_LEN];
4205     LPWSTR status_code, status_text;
4206     DWORD cchMaxRawHeaders = 1024;
4207     LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
4208     LPWSTR temp;
4209     DWORD cchRawHeaders = 0;
4210
4211     TRACE("-->\n");
4212
4213     /* clear old response headers (eg. from a redirect response) */
4214     if (clear) HTTP_clear_response_headers( lpwhr );
4215
4216     if (!NETCON_connected(&lpwhr->netConnection))
4217         goto lend;
4218
4219     do {
4220         /*
4221          * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
4222          */
4223         buflen = MAX_REPLY_LEN;
4224         if (!read_line(lpwhr, bufferA, &buflen))
4225             goto lend;
4226         rc += buflen;
4227         MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
4228
4229         /* split the version from the status code */
4230         status_code = strchrW( buffer, ' ' );
4231         if( !status_code )
4232             goto lend;
4233         *status_code++=0;
4234
4235         /* split the status code from the status text */
4236         status_text = strchrW( status_code, ' ' );
4237         if( !status_text )
4238             goto lend;
4239         *status_text++=0;
4240
4241         TRACE("version [%s] status code [%s] status text [%s]\n",
4242            debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
4243
4244     } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
4245
4246     /* Add status code */
4247     HTTP_ProcessHeader(lpwhr, szStatus, status_code,
4248             HTTP_ADDHDR_FLAG_REPLACE);
4249
4250     HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
4251     HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
4252
4253     lpwhr->lpszVersion= WININET_strdupW(buffer);
4254     lpwhr->lpszStatusText = WININET_strdupW(status_text);
4255
4256     /* Restore the spaces */
4257     *(status_code-1) = ' ';
4258     *(status_text-1) = ' ';
4259
4260     /* regenerate raw headers */
4261     while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
4262         cchMaxRawHeaders *= 2;
4263     temp = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
4264     if (temp == NULL) goto lend;
4265     lpszRawHeaders = temp;
4266     memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
4267     cchRawHeaders += (buflen-1);
4268     memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
4269     cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
4270     lpszRawHeaders[cchRawHeaders] = '\0';
4271
4272     /* Parse each response line */
4273     do
4274     {
4275         buflen = MAX_REPLY_LEN;
4276         if (read_line(lpwhr, bufferA, &buflen))
4277         {
4278             LPWSTR * pFieldAndValue;
4279
4280             TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
4281
4282             if (!bufferA[0]) break;
4283             MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
4284
4285             pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
4286             if (pFieldAndValue)
4287             {
4288                 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
4289                     cchMaxRawHeaders *= 2;
4290                 temp = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
4291                 if (temp == NULL) goto lend;
4292                 lpszRawHeaders = temp;
4293                 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
4294                 cchRawHeaders += (buflen-1);
4295                 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
4296                 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
4297                 lpszRawHeaders[cchRawHeaders] = '\0';
4298
4299                 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
4300                                    HTTP_ADDREQ_FLAG_ADD );
4301
4302                 HTTP_FreeTokens(pFieldAndValue);
4303             }
4304         }
4305         else
4306         {
4307             cbreaks++;
4308             if (cbreaks >= 2)
4309                break;
4310         }
4311     }while(1);
4312
4313     HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
4314     lpwhr->lpszRawHeaders = lpszRawHeaders;
4315     TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
4316     bSuccess = TRUE;
4317
4318 lend:
4319
4320     TRACE("<--\n");
4321     if (bSuccess)
4322         return rc;
4323     else
4324     {
4325         HeapFree(GetProcessHeap(), 0, lpszRawHeaders);
4326         return 0;
4327     }
4328 }
4329
4330
4331 static void strip_spaces(LPWSTR start)
4332 {
4333     LPWSTR str = start;
4334     LPWSTR end;
4335
4336     while (*str == ' ' && *str != '\0')
4337         str++;
4338
4339     if (str != start)
4340         memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
4341
4342     end = start + strlenW(start) - 1;
4343     while (end >= start && *end == ' ')
4344     {
4345         *end = '\0';
4346         end--;
4347     }
4348 }
4349
4350
4351 /***********************************************************************
4352  *           HTTP_InterpretHttpHeader (internal)
4353  *
4354  * Parse server response
4355  *
4356  * RETURNS
4357  *
4358  *   Pointer to array of field, value, NULL on success.
4359  *   NULL on error.
4360  */
4361 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
4362 {
4363     LPWSTR * pTokenPair;
4364     LPWSTR pszColon;
4365     INT len;
4366
4367     pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
4368
4369     pszColon = strchrW(buffer, ':');
4370     /* must have two tokens */
4371     if (!pszColon)
4372     {
4373         HTTP_FreeTokens(pTokenPair);
4374         if (buffer[0])
4375             TRACE("No ':' in line: %s\n", debugstr_w(buffer));
4376         return NULL;
4377     }
4378
4379     pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
4380     if (!pTokenPair[0])
4381     {
4382         HTTP_FreeTokens(pTokenPair);
4383         return NULL;
4384     }
4385     memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
4386     pTokenPair[0][pszColon - buffer] = '\0';
4387
4388     /* skip colon */
4389     pszColon++;
4390     len = strlenW(pszColon);
4391     pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
4392     if (!pTokenPair[1])
4393     {
4394         HTTP_FreeTokens(pTokenPair);
4395         return NULL;
4396     }
4397     memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
4398
4399     strip_spaces(pTokenPair[0]);
4400     strip_spaces(pTokenPair[1]);
4401
4402     TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
4403     return pTokenPair;
4404 }
4405
4406 /***********************************************************************
4407  *           HTTP_ProcessHeader (internal)
4408  *
4409  * Stuff header into header tables according to <dwModifier>
4410  *
4411  */
4412
4413 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4414
4415 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
4416 {
4417     LPHTTPHEADERW lphttpHdr = NULL;
4418     BOOL bSuccess = FALSE;
4419     INT index = -1;
4420     BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
4421
4422     TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
4423
4424     /* REPLACE wins out over ADD */
4425     if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
4426         dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
4427     
4428     if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
4429         index = -1;
4430     else
4431         index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
4432
4433     if (index >= 0)
4434     {
4435         if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
4436         {
4437             return FALSE;
4438         }
4439         lphttpHdr = &lpwhr->pCustHeaders[index];
4440     }
4441     else if (value)
4442     {
4443         HTTPHEADERW hdr;
4444
4445         hdr.lpszField = (LPWSTR)field;
4446         hdr.lpszValue = (LPWSTR)value;
4447         hdr.wFlags = hdr.wCount = 0;
4448
4449         if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4450             hdr.wFlags |= HDR_ISREQUEST;
4451
4452         return HTTP_InsertCustomHeader(lpwhr, &hdr);
4453     }
4454     /* no value to delete */
4455     else return TRUE;
4456
4457     if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4458             lphttpHdr->wFlags |= HDR_ISREQUEST;
4459     else
4460         lphttpHdr->wFlags &= ~HDR_ISREQUEST;
4461
4462     if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
4463     {
4464         HTTP_DeleteCustomHeader( lpwhr, index );
4465
4466         if (value)
4467         {
4468             HTTPHEADERW hdr;
4469
4470             hdr.lpszField = (LPWSTR)field;
4471             hdr.lpszValue = (LPWSTR)value;
4472             hdr.wFlags = hdr.wCount = 0;
4473
4474             if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4475                 hdr.wFlags |= HDR_ISREQUEST;
4476
4477             return HTTP_InsertCustomHeader(lpwhr, &hdr);
4478         }
4479
4480         return TRUE;
4481     }
4482     else if (dwModifier & COALESCEFLAGS)
4483     {
4484         LPWSTR lpsztmp;
4485         WCHAR ch = 0;
4486         INT len = 0;
4487         INT origlen = strlenW(lphttpHdr->lpszValue);
4488         INT valuelen = strlenW(value);
4489
4490         if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
4491         {
4492             ch = ',';
4493             lphttpHdr->wFlags |= HDR_COMMADELIMITED;
4494         }
4495         else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4496         {
4497             ch = ';';
4498             lphttpHdr->wFlags |= HDR_COMMADELIMITED;
4499         }
4500
4501         len = origlen + valuelen + ((ch > 0) ? 2 : 0);
4502
4503         lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
4504         if (lpsztmp)
4505         {
4506             lphttpHdr->lpszValue = lpsztmp;
4507     /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
4508             if (ch > 0)
4509             {
4510                 lphttpHdr->lpszValue[origlen] = ch;
4511                 origlen++;
4512                 lphttpHdr->lpszValue[origlen] = ' ';
4513                 origlen++;
4514             }
4515
4516             memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
4517             lphttpHdr->lpszValue[len] = '\0';
4518             bSuccess = TRUE;
4519         }
4520         else
4521         {
4522             WARN("HeapReAlloc (%d bytes) failed\n",len+1);
4523             INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4524         }
4525     }
4526     TRACE("<-- %d\n",bSuccess);
4527     return bSuccess;
4528 }
4529
4530
4531 /***********************************************************************
4532  *           HTTP_FinishedReading (internal)
4533  *
4534  * Called when all content from server has been read by client.
4535  *
4536  */
4537 static BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
4538 {
4539     WCHAR szVersion[10];
4540     WCHAR szConnectionResponse[20];
4541     DWORD dwBufferSize = sizeof(szVersion);
4542     BOOL keepalive = FALSE;
4543
4544     TRACE("\n");
4545
4546     /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
4547      * the connection is keep-alive by default */
4548     if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_VERSION, szVersion,
4549                              &dwBufferSize, NULL) &&
4550         !strcmpiW(szVersion, g_szHttp1_1))
4551     {
4552         keepalive = TRUE;
4553     }
4554
4555     dwBufferSize = sizeof(szConnectionResponse);
4556     if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) ||
4557         HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL))
4558     {
4559         keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
4560     }
4561
4562     if (!keepalive)
4563     {
4564         HTTPREQ_CloseConnection(&lpwhr->hdr);
4565     }
4566
4567     /* FIXME: store data in the URL cache here */
4568
4569     return TRUE;
4570 }
4571
4572
4573 /***********************************************************************
4574  *           HTTP_GetCustomHeaderIndex (internal)
4575  *
4576  * Return index of custom header from header array
4577  *
4578  */
4579 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
4580                                      int requested_index, BOOL request_only)
4581 {
4582     DWORD index;
4583
4584     TRACE("%s\n", debugstr_w(lpszField));
4585
4586     for (index = 0; index < lpwhr->nCustHeaders; index++)
4587     {
4588         if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
4589             continue;
4590
4591         if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4592             continue;
4593
4594         if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4595             continue;
4596
4597         if (requested_index == 0)
4598             break;
4599         requested_index --;
4600     }
4601
4602     if (index >= lpwhr->nCustHeaders)
4603         index = -1;
4604
4605     TRACE("Return: %d\n", index);
4606     return index;
4607 }
4608
4609
4610 /***********************************************************************
4611  *           HTTP_InsertCustomHeader (internal)
4612  *
4613  * Insert header into array
4614  *
4615  */
4616 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
4617 {
4618     INT count;
4619     LPHTTPHEADERW lph = NULL;
4620     BOOL r = FALSE;
4621
4622     TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
4623     count = lpwhr->nCustHeaders + 1;
4624     if (count > 1)
4625         lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
4626     else
4627         lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
4628
4629     if (NULL != lph)
4630     {
4631         lpwhr->pCustHeaders = lph;
4632         lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
4633         lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
4634         lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
4635         lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
4636         lpwhr->nCustHeaders++;
4637         r = TRUE;
4638     }
4639     else
4640     {
4641         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4642     }
4643
4644     return r;
4645 }
4646
4647
4648 /***********************************************************************
4649  *           HTTP_DeleteCustomHeader (internal)
4650  *
4651  * Delete header from array
4652  *  If this function is called, the indexs may change.
4653  */
4654 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
4655 {
4656     if( lpwhr->nCustHeaders <= 0 )
4657         return FALSE;
4658     if( index >= lpwhr->nCustHeaders )
4659         return FALSE;
4660     lpwhr->nCustHeaders--;
4661
4662     HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[index].lpszField);
4663     HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[index].lpszValue);
4664
4665     memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
4666              (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
4667     memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
4668
4669     return TRUE;
4670 }
4671
4672
4673 /***********************************************************************
4674  *           HTTP_VerifyValidHeader (internal)
4675  *
4676  * Verify the given header is not invalid for the given http request
4677  *
4678  */
4679 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
4680 {
4681     /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4682     if (!strcmpW(lpwhr->lpszVersion, g_szHttp1_0) && !strcmpiW(field, szAccept_Encoding))
4683         return FALSE;
4684
4685     return TRUE;
4686 }
4687
4688 /***********************************************************************
4689  *          IsHostInProxyBypassList (@)
4690  *
4691  * Undocumented
4692  *
4693  */
4694 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
4695 {
4696    FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
4697    return FALSE;
4698 }