~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Wine Cross Reference
wine/dlls/wininet/http.c

Version: ~ [ wine-1.1.33 ] ~ [ wine-1.1.32 ] ~ [ wine-1.1.31 ] ~ [ wine-1.1.30 ] ~ [ wine-1.1.29 ] ~ [ wine-1.1.28 ] ~ [ wine-1.1.27 ] ~ [ wine-1.1.26 ] ~ [ wine-1.1.25 ] ~ [ wine-1.1.24 ] ~ [ wine-1.1.23 ] ~ [ wine-1.1.22 ] ~ [ wine-1.1.21 ] ~ [ wine-1.1.20 ] ~ [ wine-1.1.19 ] ~ [ wine-1.1.18 ] ~ [ wine-1.1.17 ] ~ [ wine-1.1.16 ] ~ [ wine-1.1.15 ] ~ [ wine-1.1.14 ] ~ [ wine-1.1.13 ] ~ [ wine-1.1.12 ] ~ [ wine-1.1.11 ] ~ [ wine-1.1.10 ] ~ [ wine-1.1.9 ] ~ [ wine-1.1.8 ] ~ [ wine-1.1.7 ] ~ [ wine-1.0.1 ] ~ [ wine-1.1.6 ] ~ [ wine-1.1.5 ] ~ [ wine-1.1.4 ] ~ [ wine-1.1.3 ] ~ [ wine-1.1.2 ] ~ [ wine-1.1.1 ] ~ [ wine-1.1.0 ] ~ [ wine-1.0 ] ~

  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};
 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 szHEAD[] = { 'H','E','A','D', 0 };
 83 static const WCHAR szCrLf[] = {'\r','\n', 0};
 84 
 85 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
 86 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
 87 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
 88 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
 89 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
 90 static const WCHAR szAge[] = { 'A','g','e',0 };
 91 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
 92 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
 93 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
 94 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
 95 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
 96 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
 97 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
 98 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
 99 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
100 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
101 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
102 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 };
103 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
104 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
105 static const WCHAR szDate[] = { 'D','a','t','e',0 };
106 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
107 static const WCHAR szETag[] = { 'E','T','a','g',0 };
108 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
109 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
110 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
111 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
112 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
113 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
114 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
115 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
116 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
117 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
118 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
119 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
120 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
121 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
122 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
123 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
124 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
125 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
126 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
127 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
128 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
129 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 };
130 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
131 static const WCHAR szURI[] = { 'U','R','I',0 };
132 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
133 static const WCHAR szVary[] = { 'V','a','r','y',0 };
134 static const WCHAR szVia[] = { 'V','i','a',0 };
135 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
136 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
137 
138 #define MAXHOSTNAME 100
139 #define MAX_FIELD_VALUE_LEN 256
140 #define MAX_FIELD_LEN 256
141 
142 #define HTTP_REFERER    szReferer
143 #define HTTP_ACCEPT     szAccept
144 #define HTTP_USERAGENT  szUser_Agent
145 
146 #define HTTP_ADDHDR_FLAG_ADD                            0x20000000
147 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW                     0x10000000
148 #define HTTP_ADDHDR_FLAG_COALESCE                       0x40000000
149 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA            0x40000000
150 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON        0x01000000
151 #define HTTP_ADDHDR_FLAG_REPLACE                        0x80000000
152 #define HTTP_ADDHDR_FLAG_REQ                            0x02000000
153 
154 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
155 
156 struct HttpAuthInfo
157 {
158     LPWSTR scheme;
159     CredHandle cred;
160     CtxtHandle ctx;
161     TimeStamp exp;
162     ULONG attr;
163     ULONG max_token;
164     void *auth_data;
165     unsigned int auth_data_len;
166     BOOL finished; /* finished authenticating */
167 };
168 
169 
170 struct gzip_stream_t {
171 #ifdef HAVE_ZLIB
172     z_stream zstream;
173 #endif
174     BYTE buf[8192];
175     DWORD buf_size;
176     DWORD buf_pos;
177     BOOL end_of_data;
178 };
179 
180 static BOOL HTTP_OpenConnection(http_request_t *req);
181 static BOOL HTTP_GetResponseHeaders(http_request_t *req, BOOL clear);
182 static BOOL HTTP_ProcessHeader(http_request_t *req, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
183 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
184 static BOOL HTTP_InsertCustomHeader(http_request_t *req, LPHTTPHEADERW lpHdr);
185 static INT HTTP_GetCustomHeaderIndex(http_request_t *req, LPCWSTR lpszField, INT index, BOOL Request);
186 static BOOL HTTP_DeleteCustomHeader(http_request_t *req, DWORD index);
187 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
188 static BOOL HTTP_HttpQueryInfoW(http_request_t*, DWORD, LPVOID, LPDWORD, LPDWORD);
189 static LPWSTR HTTP_GetRedirectURL(http_request_t *req, LPCWSTR lpszUrl);
190 static BOOL HTTP_HandleRedirect(http_request_t *req, LPCWSTR lpszUrl);
191 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
192 static BOOL HTTP_VerifyValidHeader(http_request_t *req, LPCWSTR field);
193 static void HTTP_DrainContent(http_request_t *req);
194 static BOOL HTTP_FinishedReading(http_request_t *req);
195 
196 static LPHTTPHEADERW HTTP_GetHeader(http_request_t *req, LPCWSTR head)
197 {
198     int HeaderIndex = 0;
199     HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
200     if (HeaderIndex == -1)
201         return NULL;
202     else
203         return &req->pCustHeaders[HeaderIndex];
204 }
205 
206 #ifdef HAVE_ZLIB
207 
208 static voidpf wininet_zalloc(voidpf opaque, uInt items, uInt size)
209 {
210     return HeapAlloc(GetProcessHeap(), 0, items*size);
211 }
212 
213 static void wininet_zfree(voidpf opaque, voidpf address)
214 {
215     HeapFree(GetProcessHeap(), 0, address);
216 }
217 
218 static void init_gzip_stream(http_request_t *req)
219 {
220     gzip_stream_t *gzip_stream;
221     int index, zres;
222 
223     gzip_stream = HeapAlloc(GetProcessHeap(), 0, sizeof(gzip_stream_t));
224     gzip_stream->zstream.zalloc = wininet_zalloc;
225     gzip_stream->zstream.zfree = wininet_zfree;
226     gzip_stream->zstream.opaque = NULL;
227     gzip_stream->zstream.next_in = NULL;
228     gzip_stream->zstream.avail_in = 0;
229     gzip_stream->zstream.next_out = NULL;
230     gzip_stream->zstream.avail_out = 0;
231     gzip_stream->buf_pos = 0;
232     gzip_stream->buf_size = 0;
233     gzip_stream->end_of_data = FALSE;
234 
235     zres = inflateInit2(&gzip_stream->zstream, 0x1f);
236     if(zres != Z_OK) {
237         ERR("inflateInit failed: %d\n", zres);
238         HeapFree(GetProcessHeap(), 0, gzip_stream);
239         return;
240     }
241 
242     req->gzip_stream = gzip_stream;
243 
244     index = HTTP_GetCustomHeaderIndex(req, szContent_Length, 0, FALSE);
245     if(index != -1)
246         HTTP_DeleteCustomHeader(req, index);
247 }
248 
249 #else
250 
251 static void init_gzip_stream(http_request_t *req)
252 {
253     ERR("gzip stream not supported, missing zlib.\n");
254 }
255 
256 #endif
257 
258 /* set the request content length based on the headers */
259 static DWORD set_content_length( http_request_t *lpwhr )
260 {
261     static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
262     WCHAR encoding[20];
263     DWORD size;
264 
265     size = sizeof(lpwhr->dwContentLength);
266     if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
267                              &lpwhr->dwContentLength, &size, NULL))
268         lpwhr->dwContentLength = ~0u;
269 
270     size = sizeof(encoding);
271     if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) &&
272         !strcmpiW(encoding, szChunked))
273     {
274         lpwhr->dwContentLength = ~0u;
275         lpwhr->read_chunked = TRUE;
276     }
277 
278     if(lpwhr->decoding) {
279         int encoding_idx;
280 
281         static const WCHAR gzipW[] = {'g','z','i','p',0};
282 
283         encoding_idx = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Encoding, 0, FALSE);
284         if(encoding_idx != -1 && !strcmpiW(lpwhr->pCustHeaders[encoding_idx].lpszValue, gzipW))
285             init_gzip_stream(lpwhr);
286     }
287 
288     return lpwhr->dwContentLength;
289 }
290 
291 /***********************************************************************
292  *           HTTP_Tokenize (internal)
293  *
294  *  Tokenize a string, allocating memory for the tokens.
295  */
296 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
297 {
298     LPWSTR * token_array;
299     int tokens = 0;
300     int i;
301     LPCWSTR next_token;
302 
303     if (string)
304     {
305         /* empty string has no tokens */
306         if (*string)
307             tokens++;
308         /* count tokens */
309         for (i = 0; string[i]; i++)
310         {
311             if (!strncmpW(string+i, token_string, strlenW(token_string)))
312             {
313                 DWORD j;
314                 tokens++;
315                 /* we want to skip over separators, but not the null terminator */
316                 for (j = 0; j < strlenW(token_string) - 1; j++)
317                     if (!string[i+j])
318                         break;
319                 i += j;
320             }
321         }
322     }
323 
324     /* add 1 for terminating NULL */
325     token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
326     token_array[tokens] = NULL;
327     if (!tokens)
328         return token_array;
329     for (i = 0; i < tokens; i++)
330     {
331         int len;
332         next_token = strstrW(string, token_string);
333         if (!next_token) next_token = string+strlenW(string);
334         len = next_token - string;
335         token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
336         memcpy(token_array[i], string, len*sizeof(WCHAR));
337         token_array[i][len] = '\0';
338         string = next_token+strlenW(token_string);
339     }
340     return token_array;
341 }
342 
343 /***********************************************************************
344  *           HTTP_FreeTokens (internal)
345  *
346  *  Frees memory returned from HTTP_Tokenize.
347  */
348 static void HTTP_FreeTokens(LPWSTR * token_array)
349 {
350     int i;
351     for (i = 0; token_array[i]; i++)
352         HeapFree(GetProcessHeap(), 0, token_array[i]);
353     HeapFree(GetProcessHeap(), 0, token_array);
354 }
355 
356 /* **********************************************************************
357  * 
358  * Helper functions for the HttpSendRequest(Ex) functions
359  * 
360  */
361 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
362 {
363     struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
364     http_request_t *lpwhr = (http_request_t*) workRequest->hdr;
365 
366     TRACE("%p\n", lpwhr);
367 
368     HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
369             req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
370             req->dwContentLength, req->bEndRequest);
371 
372     HeapFree(GetProcessHeap(), 0, req->lpszHeader);
373 }
374 
375 static void HTTP_FixURL(http_request_t *lpwhr)
376 {
377     static const WCHAR szSlash[] = { '/',0 };
378     static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
379 
380     /* If we don't have a path we set it to root */
381     if (NULL == lpwhr->lpszPath)
382         lpwhr->lpszPath = heap_strdupW(szSlash);
383     else /* remove \r and \n*/
384     {
385         int nLen = strlenW(lpwhr->lpszPath);
386         while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
387         {
388             nLen--;
389             lpwhr->lpszPath[nLen]='\0';
390         }
391         /* Replace '\' with '/' */
392         while (nLen>0) {
393             nLen--;
394             if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
395         }
396     }
397 
398     if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
399                        lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
400        && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
401     {
402         WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0, 
403                              (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
404         *fixurl = '/';
405         strcpyW(fixurl + 1, lpwhr->lpszPath);
406         HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
407         lpwhr->lpszPath = fixurl;
408     }
409 }
410 
411 static LPWSTR HTTP_BuildHeaderRequestString( http_request_t *lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
412 {
413     LPWSTR requestString;
414     DWORD len, n;
415     LPCWSTR *req;
416     UINT i;
417     LPWSTR p;
418 
419     static const WCHAR szSpace[] = { ' ',0 };
420     static const WCHAR szColon[] = { ':',' ',0 };
421     static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
422 
423     /* allocate space for an array of all the string pointers to be added */
424     len = (lpwhr->nCustHeaders)*4 + 10;
425     req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
426 
427     /* add the verb, path and HTTP version string */
428     n = 0;
429     req[n++] = verb;
430     req[n++] = szSpace;
431     req[n++] = path;
432     req[n++] = szSpace;
433     req[n++] = version;
434 
435     /* Append custom request headers */
436     for (i = 0; i < lpwhr->nCustHeaders; i++)
437     {
438         if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
439         {
440             req[n++] = szCrLf;
441             req[n++] = lpwhr->pCustHeaders[i].lpszField;
442             req[n++] = szColon;
443             req[n++] = lpwhr->pCustHeaders[i].lpszValue;
444 
445             TRACE("Adding custom header %s (%s)\n",
446                    debugstr_w(lpwhr->pCustHeaders[i].lpszField),
447                    debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
448         }
449     }
450 
451     if( n >= len )
452         ERR("oops. buffer overrun\n");
453 
454     req[n] = NULL;
455     requestString = HTTP_build_req( req, 4 );
456     HeapFree( GetProcessHeap(), 0, req );
457 
458     /*
459      * Set (header) termination string for request
460      * Make sure there's exactly two new lines at the end of the request
461      */
462     p = &requestString[strlenW(requestString)-1];
463     while ( (*p == '\n') || (*p == '\r') )
464        p--;
465     strcpyW( p+1, sztwocrlf );
466     
467     return requestString;
468 }
469 
470 static void HTTP_ProcessCookies( http_request_t *lpwhr )
471 {
472     int HeaderIndex;
473     int numCookies = 0;
474     LPHTTPHEADERW setCookieHeader;
475 
476     while((HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, numCookies, FALSE)) != -1)
477     {
478         setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
479 
480         if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
481         {
482             int len;
483             static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','%','s',0};
484             LPWSTR buf_url;
485             LPHTTPHEADERW Host;
486 
487             Host = HTTP_GetHeader(lpwhr, hostW);
488             len = lstrlenW(Host->lpszValue) + 9 + lstrlenW(lpwhr->lpszPath);
489             buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
490             sprintfW(buf_url, szFmt, Host->lpszValue, lpwhr->lpszPath);
491             InternetSetCookieW(buf_url, NULL, setCookieHeader->lpszValue);
492 
493             HeapFree(GetProcessHeap(), 0, buf_url);
494         }
495         numCookies++;
496     }
497 }
498 
499 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
500 {
501     static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
502     return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
503         ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
504 }
505 
506 static BOOL HTTP_DoAuthorization( http_request_t *lpwhr, LPCWSTR pszAuthValue,
507                                   struct HttpAuthInfo **ppAuthInfo,
508                                   LPWSTR domain_and_username, LPWSTR password )
509 {
510     SECURITY_STATUS sec_status;
511     struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
512     BOOL first = FALSE;
513 
514     TRACE("%s\n", debugstr_w(pszAuthValue));
515 
516     if (!pAuthInfo)
517     {
518         TimeStamp exp;
519 
520         first = TRUE;
521         pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
522         if (!pAuthInfo)
523             return FALSE;
524 
525         SecInvalidateHandle(&pAuthInfo->cred);
526         SecInvalidateHandle(&pAuthInfo->ctx);
527         memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
528         pAuthInfo->attr = 0;
529         pAuthInfo->auth_data = NULL;
530         pAuthInfo->auth_data_len = 0;
531         pAuthInfo->finished = FALSE;
532 
533         if (is_basic_auth_value(pszAuthValue))
534         {
535             static const WCHAR szBasic[] = {'B','a','s','i','c',0};
536             pAuthInfo->scheme = heap_strdupW(szBasic);
537             if (!pAuthInfo->scheme)
538             {
539                 HeapFree(GetProcessHeap(), 0, pAuthInfo);
540                 return FALSE;
541             }
542         }
543         else
544         {
545             PVOID pAuthData;
546             SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
547 
548             pAuthInfo->scheme = heap_strdupW(pszAuthValue);
549             if (!pAuthInfo->scheme)
550             {
551                 HeapFree(GetProcessHeap(), 0, pAuthInfo);
552                 return FALSE;
553             }
554 
555             if (domain_and_username)
556             {
557                 WCHAR *user = strchrW(domain_and_username, '\\');
558                 WCHAR *domain = domain_and_username;
559 
560                 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
561 
562                 pAuthData = &nt_auth_identity;
563 
564                 if (user) user++;
565                 else
566                 {
567                     user = domain_and_username;
568                     domain = NULL;
569                 }
570 
571                 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
572                 nt_auth_identity.User = user;
573                 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
574                 nt_auth_identity.Domain = domain;
575                 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
576                 nt_auth_identity.Password = password;
577                 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
578             }
579             else
580                 /* use default credentials */
581                 pAuthData = NULL;
582 
583             sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
584                                                    SECPKG_CRED_OUTBOUND, NULL,
585                                                    pAuthData, NULL,
586                                                    NULL, &pAuthInfo->cred,
587                                                    &exp);
588             if (sec_status == SEC_E_OK)
589             {
590                 PSecPkgInfoW sec_pkg_info;
591                 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
592                 if (sec_status == SEC_E_OK)
593                 {
594                     pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
595                     FreeContextBuffer(sec_pkg_info);
596                 }
597             }
598             if (sec_status != SEC_E_OK)
599             {
600                 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
601                      debugstr_w(pAuthInfo->scheme), sec_status);
602                 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
603                 HeapFree(GetProcessHeap(), 0, pAuthInfo);
604                 return FALSE;
605             }
606         }
607         *ppAuthInfo = pAuthInfo;
608     }
609     else if (pAuthInfo->finished)
610         return FALSE;
611 
612     if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
613         strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
614     {
615         ERR("authentication scheme changed from %s to %s\n",
616             debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
617         return FALSE;
618     }
619 
620     if (is_basic_auth_value(pszAuthValue))
621     {
622         int userlen;
623         int passlen;
624         char *auth_data;
625 
626         TRACE("basic authentication\n");
627 
628         /* we don't cache credentials for basic authentication, so we can't
629          * retrieve them if the application didn't pass us any credentials */
630         if (!domain_and_username) return FALSE;
631 
632         userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
633         passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
634 
635         /* length includes a nul terminator, which will be re-used for the ':' */
636         auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
637         if (!auth_data)
638             return FALSE;
639 
640         WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
641         auth_data[userlen] = ':';
642         WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
643 
644         pAuthInfo->auth_data = auth_data;
645         pAuthInfo->auth_data_len = userlen + 1 + passlen;
646         pAuthInfo->finished = TRUE;
647 
648         return TRUE;
649     }
650     else
651     {
652         LPCWSTR pszAuthData;
653         SecBufferDesc out_desc, in_desc;
654         SecBuffer out, in;
655         unsigned char *buffer;
656         ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
657             ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
658 
659         in.BufferType = SECBUFFER_TOKEN;
660         in.cbBuffer = 0;
661         in.pvBuffer = NULL;
662 
663         in_desc.ulVersion = 0;
664         in_desc.cBuffers = 1;
665         in_desc.pBuffers = &in;
666 
667         pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
668         if (*pszAuthData == ' ')
669         {
670             pszAuthData++;
671             in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
672             in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
673             HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
674         }
675 
676         buffer = HeapAlloc(GetProcessHeap(), 0, pAuthInfo->max_token);
677 
678         out.BufferType = SECBUFFER_TOKEN;
679         out.cbBuffer = pAuthInfo->max_token;
680         out.pvBuffer = buffer;
681 
682         out_desc.ulVersion = 0;
683         out_desc.cBuffers = 1;
684         out_desc.pBuffers = &out;
685 
686         sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
687                                                 first ? NULL : &pAuthInfo->ctx,
688                                                 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
689                                                 context_req, 0, SECURITY_NETWORK_DREP,
690                                                 in.pvBuffer ? &in_desc : NULL,
691                                                 0, &pAuthInfo->ctx, &out_desc,
692                                                 &pAuthInfo->attr, &pAuthInfo->exp);
693         if (sec_status == SEC_E_OK)
694         {
695             pAuthInfo->finished = TRUE;
696             pAuthInfo->auth_data = out.pvBuffer;
697             pAuthInfo->auth_data_len = out.cbBuffer;
698             TRACE("sending last auth packet\n");
699         }
700         else if (sec_status == SEC_I_CONTINUE_NEEDED)
701         {
702             pAuthInfo->auth_data = out.pvBuffer;
703             pAuthInfo->auth_data_len = out.cbBuffer;
704             TRACE("sending next auth packet\n");
705         }
706         else
707         {
708             ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
709             pAuthInfo->finished = TRUE;
710             HeapFree(GetProcessHeap(), 0, out.pvBuffer);
711             return FALSE;
712         }
713     }
714 
715     return TRUE;
716 }
717 
718 /***********************************************************************
719  *           HTTP_HttpAddRequestHeadersW (internal)
720  */
721 static BOOL HTTP_HttpAddRequestHeadersW(http_request_t *lpwhr,
722         LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
723 {
724     LPWSTR lpszStart;
725     LPWSTR lpszEnd;
726     LPWSTR buffer;
727     BOOL bSuccess = FALSE;
728     DWORD len;
729 
730     TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
731 
732     if( dwHeaderLength == ~0U )
733         len = strlenW(lpszHeader);
734     else
735         len = dwHeaderLength;
736     buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
737     lstrcpynW( buffer, lpszHeader, len + 1);
738 
739     lpszStart = buffer;
740 
741     do
742     {
743         LPWSTR * pFieldAndValue;
744 
745         lpszEnd = lpszStart;
746 
747         while (*lpszEnd != '\0')
748         {
749             if (*lpszEnd == '\r' || *lpszEnd == '\n')
750                  break;
751             lpszEnd++;
752         }
753 
754         if (*lpszStart == '\0')
755             break;
756 
757         if (*lpszEnd == '\r' || *lpszEnd == '\n')
758         {
759             *lpszEnd = '\0';
760             lpszEnd++; /* Jump over newline */
761         }
762         TRACE("interpreting header %s\n", debugstr_w(lpszStart));
763         if (*lpszStart == '\0')
764         {
765             /* Skip 0-length headers */
766             lpszStart = lpszEnd;
767             bSuccess = TRUE;
768             continue;
769         }
770         pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
771         if (pFieldAndValue)
772         {
773             bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
774             if (bSuccess)
775                 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
776                     pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
777             HTTP_FreeTokens(pFieldAndValue);
778         }
779 
780         lpszStart = lpszEnd;
781     } while (bSuccess);
782 
783     HeapFree(GetProcessHeap(), 0, buffer);
784 
785     return bSuccess;
786 }
787 
788 /***********************************************************************
789  *           HttpAddRequestHeadersW (WININET.@)
790  *
791  * Adds one or more HTTP header to the request handler
792  *
793  * NOTE
794  * On Windows if dwHeaderLength includes the trailing '\0', then
795  * HttpAddRequestHeadersW() adds it too. However this results in an
796  * invalid Http header which is rejected by some servers so we probably
797  * don't need to match Windows on that point.
798  *
799  * RETURNS
800  *    TRUE  on success
801  *    FALSE on failure
802  *
803  */
804 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
805         LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
806 {
807     BOOL bSuccess = FALSE;
808     http_request_t *lpwhr;
809 
810     TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
811 
812     if (!lpszHeader) 
813       return TRUE;
814 
815     lpwhr = (http_request_t*) WININET_GetObject( hHttpRequest );
816     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
817     {
818         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
819         goto lend;
820     }
821     bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
822 lend:
823     if( lpwhr )
824         WININET_Release( &lpwhr->hdr );
825 
826     return bSuccess;
827 }
828 
829 /***********************************************************************
830  *           HttpAddRequestHeadersA (WININET.@)
831  *
832  * Adds one or more HTTP header to the request handler
833  *
834  * RETURNS
835  *    TRUE  on success
836  *    FALSE on failure
837  *
838  */
839 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
840         LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
841 {
842     DWORD len;
843     LPWSTR hdr;
844     BOOL r;
845 
846     TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
847 
848     len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
849     hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
850     MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
851     if( dwHeaderLength != ~0U )
852         dwHeaderLength = len;
853 
854     r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
855 
856     HeapFree( GetProcessHeap(), 0, hdr );
857 
858     return r;
859 }
860 
861 /***********************************************************************
862  *           HttpEndRequestA (WININET.@)
863  *
864  * Ends an HTTP request that was started by HttpSendRequestEx
865  *
866  * RETURNS
867  *    TRUE      if successful
868  *    FALSE     on failure
869  *
870  */
871 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, 
872         LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
873 {
874     TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
875 
876     if (lpBuffersOut)
877     {
878         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
879         return FALSE;
880     }
881 
882     return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
883 }
884 
885 static BOOL HTTP_HttpEndRequestW(http_request_t *lpwhr, DWORD dwFlags, DWORD_PTR dwContext)
886 {
887     BOOL rc = FALSE;
888     INT responseLen;
889     DWORD dwBufferSize;
890     INTERNET_ASYNC_RESULT iar;
891 
892     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
893                   INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
894 
895     responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
896     if (responseLen)
897         rc = TRUE;
898 
899     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
900                   INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
901 
902     /* process cookies here. Is this right? */
903     HTTP_ProcessCookies(lpwhr);
904 
905     if (!set_content_length( lpwhr )) HTTP_FinishedReading(lpwhr);
906 
907     if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
908     {
909         DWORD dwCode,dwCodeLength = sizeof(DWORD);
910         if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE, &dwCode, &dwCodeLength, NULL) &&
911             (dwCode == 302 || dwCode == 301 || dwCode == 303))
912         {
913             WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
914             dwBufferSize=sizeof(szNewLocation);
915             if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL))
916             {
917                 if (strcmpW(lpwhr->lpszVerb, szGET) && strcmpW(lpwhr->lpszVerb, szHEAD))
918                 {
919                     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
920                     lpwhr->lpszVerb = heap_strdupW(szGET);
921                 }
922                 HTTP_DrainContent(lpwhr);
923                 if ((new_url = HTTP_GetRedirectURL( lpwhr, szNewLocation )))
924                 {
925                     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REDIRECT,
926                                           new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
927                     rc = HTTP_HandleRedirect(lpwhr, new_url);
928                     if (rc)
929                         rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
930                     HeapFree( GetProcessHeap(), 0, new_url );
931                 }
932             }
933         }
934     }
935 
936     iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
937     iar.dwError = rc ? 0 : INTERNET_GetLastError();
938 
939     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
940                           INTERNET_STATUS_REQUEST_COMPLETE, &iar,
941                           sizeof(INTERNET_ASYNC_RESULT));
942     return rc;
943 }
944 
945 static void AsyncHttpEndRequestProc(WORKREQUEST *work)
946 {
947     struct WORKREQ_HTTPENDREQUESTW const *req = &work->u.HttpEndRequestW;
948     http_request_t *lpwhr = (http_request_t*)work->hdr;
949 
950     TRACE("%p\n", lpwhr);
951 
952     HTTP_HttpEndRequestW(lpwhr, req->dwFlags, req->dwContext);
953 }
954 
955 /***********************************************************************
956  *           HttpEndRequestW (WININET.@)
957  *
958  * Ends an HTTP request that was started by HttpSendRequestEx
959  *
960  * RETURNS
961  *    TRUE      if successful
962  *    FALSE     on failure
963  *
964  */
965 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, 
966         LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
967 {
968     BOOL rc = FALSE;
969     http_request_t *lpwhr;
970 
971     TRACE("-->\n");
972 
973     if (lpBuffersOut)
974     {
975         INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
976         return FALSE;
977     }
978 
979     lpwhr = (http_request_t*) WININET_GetObject( hRequest );
980 
981     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
982     {
983         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
984         if (lpwhr)
985             WININET_Release( &lpwhr->hdr );
986         return FALSE;
987     }
988     lpwhr->hdr.dwFlags |= dwFlags;
989 
990     if (lpwhr->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
991     {
992         WORKREQUEST work;
993         struct WORKREQ_HTTPENDREQUESTW *request;
994 
995         work.asyncproc = AsyncHttpEndRequestProc;
996         work.hdr = WININET_AddRef( &lpwhr->hdr );
997 
998         request = &work.u.HttpEndRequestW;
999         request->dwFlags = dwFlags;
1000         request->dwContext = dwContext;
1001 
1002         INTERNET_AsyncCall(&work);
1003         INTERNET_SetLastError(ERROR_IO_PENDING);
1004     }
1005     else
1006         rc = HTTP_HttpEndRequestW(lpwhr, dwFlags, dwContext);
1007 
1008     WININET_Release( &lpwhr->hdr );
1009     TRACE("%i <--\n",rc);
1010     return rc;
1011 }
1012 
1013 /***********************************************************************
1014  *           HttpOpenRequestW (WININET.@)
1015  *
1016  * Open a HTTP request handle
1017  *
1018  * RETURNS
1019  *    HINTERNET  a HTTP request handle on success
1020  *    NULL       on failure
1021  *
1022  */
1023 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
1024         LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1025         LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1026         DWORD dwFlags, DWORD_PTR dwContext)
1027 {
1028     http_session_t *lpwhs;
1029     HINTERNET handle = NULL;
1030 
1031     TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1032           debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
1033           debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
1034           dwFlags, dwContext);
1035     if(lpszAcceptTypes!=NULL)
1036     {
1037         int i;
1038         for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1039             TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
1040     }    
1041 
1042     lpwhs = (http_session_t*) WININET_GetObject( hHttpSession );
1043     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
1044     {
1045         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1046         goto lend;
1047     }
1048 
1049     /*
1050      * My tests seem to show that the windows version does not
1051      * become asynchronous until after this point. And anyhow
1052      * if this call was asynchronous then how would you get the
1053      * necessary HINTERNET pointer returned by this function.
1054      *
1055      */
1056     handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
1057                                    lpszVersion, lpszReferrer, lpszAcceptTypes,
1058                                    dwFlags, dwContext);
1059 lend:
1060     if( lpwhs )
1061         WININET_Release( &lpwhs->hdr );
1062     TRACE("returning %p\n", handle);
1063     return handle;
1064 }
1065 
1066 
1067 /***********************************************************************
1068  *           HttpOpenRequestA (WININET.@)
1069  *
1070  * Open a HTTP request handle
1071  *
1072  * RETURNS
1073  *    HINTERNET  a HTTP request handle on success
1074  *    NULL       on failure
1075  *
1076  */
1077 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
1078         LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
1079         LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
1080         DWORD dwFlags, DWORD_PTR dwContext)
1081 {
1082     LPWSTR szVerb = NULL, szObjectName = NULL;
1083     LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
1084     INT acceptTypesCount;
1085     HINTERNET rc = FALSE;
1086     LPCSTR *types;
1087 
1088     TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
1089           debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
1090           debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
1091           dwFlags, dwContext);
1092 
1093     if (lpszVerb)
1094     {
1095         szVerb = heap_strdupAtoW(lpszVerb);
1096         if ( !szVerb )
1097             goto end;
1098     }
1099 
1100     if (lpszObjectName)
1101     {
1102         szObjectName = heap_strdupAtoW(lpszObjectName);
1103         if ( !szObjectName )
1104             goto end;
1105     }
1106 
1107     if (lpszVersion)
1108     {
1109         szVersion = heap_strdupAtoW(lpszVersion);
1110         if ( !szVersion )
1111             goto end;
1112     }
1113 
1114     if (lpszReferrer)
1115     {
1116         szReferrer = heap_strdupAtoW(lpszReferrer);
1117         if ( !szReferrer )
1118             goto end;
1119     }
1120 
1121     if (lpszAcceptTypes)
1122     {
1123         acceptTypesCount = 0;
1124         types = lpszAcceptTypes;
1125         while (*types)
1126         {
1127             __TRY
1128             {
1129                 /* find out how many there are */
1130                 if (*types && **types)
1131                 {
1132                     TRACE("accept type: %s\n", debugstr_a(*types));
1133                     acceptTypesCount++;
1134                 }
1135             }
1136             __EXCEPT_PAGE_FAULT
1137             {
1138                 WARN("invalid accept type pointer\n");
1139             }
1140             __ENDTRY;
1141             types++;
1142         }
1143         szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
1144         if (!szAcceptTypes) goto end;
1145 
1146         acceptTypesCount = 0;
1147         types = lpszAcceptTypes;
1148         while (*types)
1149         {
1150             __TRY
1151             {
1152                 if (*types && **types)
1153                     szAcceptTypes[acceptTypesCount++] = heap_strdupAtoW(*types);
1154             }
1155             __EXCEPT_PAGE_FAULT
1156             {
1157                 /* ignore invalid pointer */
1158             }
1159             __ENDTRY;
1160             types++;
1161         }
1162         szAcceptTypes[acceptTypesCount] = NULL;
1163     }
1164 
1165     rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1166                           szVersion, szReferrer,
1167                           (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1168 
1169 end:
1170     if (szAcceptTypes)
1171     {
1172         acceptTypesCount = 0;
1173         while (szAcceptTypes[acceptTypesCount])
1174         {
1175             HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1176             acceptTypesCount++;
1177         }
1178         HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1179     }
1180     HeapFree(GetProcessHeap(), 0, szReferrer);
1181     HeapFree(GetProcessHeap(), 0, szVersion);
1182     HeapFree(GetProcessHeap(), 0, szObjectName);
1183     HeapFree(GetProcessHeap(), 0, szVerb);
1184 
1185     return rc;
1186 }
1187 
1188 /***********************************************************************
1189  *  HTTP_EncodeBase64
1190  */
1191 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1192 {
1193     UINT n = 0, x;
1194     static const CHAR HTTP_Base64Enc[] =
1195         "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1196 
1197     while( len > 0 )
1198     {
1199         /* first 6 bits, all from bin[0] */
1200         base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1201         x = (bin[0] & 3) << 4;
1202 
1203         /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1204         if( len == 1 )
1205         {
1206             base64[n++] = HTTP_Base64Enc[x];
1207             base64[n++] = '=';
1208             base64[n++] = '=';
1209             break;
1210         }
1211         base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1212         x = ( bin[1] & 0x0f ) << 2;
1213 
1214         /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1215         if( len == 2 )
1216         {
1217             base64[n++] = HTTP_Base64Enc[x];
1218             base64[n++] = '=';
1219             break;
1220         }
1221         base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1222 
1223         /* last 6 bits, all from bin [2] */
1224         base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1225         bin += 3;
1226         len -= 3;
1227     }
1228     base64[n] = 0;
1229     return n;
1230 }
1231 
1232 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1233                ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1234                ((x) >= '' && (x) <= '9') ? (x) - '' + 52 : \
1235                ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1236 static const signed char HTTP_Base64Dec[256] =
1237 {
1238     CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1239     CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1240     CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1241     CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1242     CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1243     CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1244     CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1245     CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1246     CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1247     CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1248     CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1249     CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1250     CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1251     CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1252     CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1253     CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1254     CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1255     CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1256     CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1257     CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1258     CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1259     CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1260     CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1261     CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1262     CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1263     CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1264 };
1265 #undef CH
1266 
1267 /***********************************************************************
1268  *  HTTP_DecodeBase64
1269  */
1270 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1271 {
1272     unsigned int n = 0;
1273 
1274     while(*base64)
1275     {
1276         signed char in[4];
1277 
1278         if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1279             ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1280             base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1281             ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1282         {
1283             WARN("invalid base64: %s\n", debugstr_w(base64));
1284             return 0;
1285         }
1286         if (bin)
1287             bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1288         n++;
1289 
1290         if ((base64[2] == '=') && (base64[3] == '='))
1291             break;
1292         if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1293             ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1294         {
1295             WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1296             return 0;
1297         }
1298         if (bin)
1299             bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1300         n++;
1301 
1302         if (base64[3] == '=')
1303             break;
1304         if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1305             ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1306         {
1307             WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1308             return 0;
1309         }
1310         if (bin)
1311             bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1312         n++;
1313 
1314         base64 += 4;
1315     }
1316 
1317     return n;
1318 }
1319 
1320 /***********************************************************************
1321  *  HTTP_InsertAuthorization
1322  *
1323  *   Insert or delete the authorization field in the request header.
1324  */
1325 static BOOL HTTP_InsertAuthorization( http_request_t *lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1326 {
1327     if (pAuthInfo)
1328     {
1329         static const WCHAR wszSpace[] = {' ',0};
1330         static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1331         unsigned int len;
1332         WCHAR *authorization = NULL;
1333 
1334         if (pAuthInfo->auth_data_len)
1335         {
1336             /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1337             len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1338             authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1339             if (!authorization)
1340                 return FALSE;
1341 
1342             strcpyW(authorization, pAuthInfo->scheme);
1343             strcatW(authorization, wszSpace);
1344             HTTP_EncodeBase64(pAuthInfo->auth_data,
1345                               pAuthInfo->auth_data_len,
1346                               authorization+strlenW(authorization));
1347 
1348             /* clear the data as it isn't valid now that it has been sent to the
1349              * server, unless it's Basic authentication which doesn't do
1350              * connection tracking */
1351             if (strcmpiW(pAuthInfo->scheme, wszBasic))
1352             {
1353                 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1354                 pAuthInfo->auth_data = NULL;
1355                 pAuthInfo->auth_data_len = 0;
1356             }
1357         }
1358 
1359         TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1360 
1361         HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1362 
1363         HeapFree(GetProcessHeap(), 0, authorization);
1364     }
1365     return TRUE;
1366 }
1367 
1368 static WCHAR *HTTP_BuildProxyRequestUrl(http_request_t *req)
1369 {
1370     WCHAR new_location[INTERNET_MAX_URL_LENGTH], *url;
1371     DWORD size;
1372 
1373     size = sizeof(new_location);
1374     if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_LOCATION, new_location, &size, NULL))
1375     {
1376         if (!(url = HeapAlloc( GetProcessHeap(), 0, size + sizeof(WCHAR) ))) return NULL;
1377         strcpyW( url, new_location );
1378     }
1379     else
1380     {
1381         static const WCHAR slash[] = { '/',0 };
1382         static const WCHAR format[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1383         static const WCHAR formatSSL[] = { 'h','t','t','p','s',':','/','/','%','s',':','%','d',0 };
1384         http_session_t *session = req->lpHttpSession;
1385 
1386         size = 16; /* "https://" + sizeof(port#) + ":/\0" */
1387         size += strlenW( session->lpszHostName ) + strlenW( req->lpszPath );
1388 
1389         if (!(url = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
1390 
1391         if (req->hdr.dwFlags & INTERNET_FLAG_SECURE)
1392             sprintfW( url, formatSSL, session->lpszHostName, session->nHostPort );
1393         else
1394             sprintfW( url, format, session->lpszHostName, session->nHostPort );
1395         if (req->lpszPath[0] != '/') strcatW( url, slash );
1396         strcatW( url, req->lpszPath );
1397     }
1398     TRACE("url=%s\n", debugstr_w(url));
1399     return url;
1400 }
1401 
1402 /***********************************************************************
1403  *           HTTP_DealWithProxy
1404  */
1405 static BOOL HTTP_DealWithProxy(appinfo_t *hIC, http_session_t *lpwhs, http_request_t *lpwhr)
1406 {
1407     WCHAR buf[MAXHOSTNAME];
1408     WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1409     static WCHAR szNul[] = { 0 };
1410     URL_COMPONENTSW UrlComponents;
1411     static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1412     static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1413 
1414     memset( &UrlComponents, 0, sizeof UrlComponents );
1415     UrlComponents.dwStructSize = sizeof UrlComponents;
1416     UrlComponents.lpszHostName = buf;
1417     UrlComponents.dwHostNameLength = MAXHOSTNAME;
1418 
1419     if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1420                                  hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1421         sprintfW(proxy, szFormat, hIC->lpszProxy);
1422     else
1423         strcpyW(proxy, hIC->lpszProxy);
1424     if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1425         return FALSE;
1426     if( UrlComponents.dwHostNameLength == 0 )
1427         return FALSE;
1428 
1429     if( !lpwhr->lpszPath )
1430         lpwhr->lpszPath = szNul;
1431 
1432     if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1433         UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1434 
1435     HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1436     lpwhs->lpszServerName = heap_strdupW(UrlComponents.lpszHostName);
1437     lpwhs->nServerPort = UrlComponents.nPort;
1438 
1439     TRACE("proxy server=%s port=%d\n", debugstr_w(lpwhs->lpszServerName), lpwhs->nServerPort);
1440     return TRUE;
1441 }
1442 
1443 #ifndef INET6_ADDRSTRLEN
1444 #define INET6_ADDRSTRLEN 46
1445 #endif
1446 
1447 static BOOL HTTP_ResolveName(http_request_t *lpwhr)
1448 {
1449     char szaddr[INET6_ADDRSTRLEN];
1450     http_session_t *lpwhs = lpwhr->lpHttpSession;
1451     const void *addr;
1452 
1453     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1454                           INTERNET_STATUS_RESOLVING_NAME,
1455                           lpwhs->lpszServerName,
1456                           strlenW(lpwhs->lpszServerName)+1);
1457 
1458     lpwhs->sa_len = sizeof(lpwhs->socketAddress);
1459     if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1460                     (struct sockaddr *)&lpwhs->socketAddress, &lpwhs->sa_len))
1461     {
1462         INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1463         return FALSE;
1464     }
1465 
1466     switch (lpwhs->socketAddress.ss_family)
1467     {
1468     case AF_INET:
1469         addr = &((struct sockaddr_in *)&lpwhs->socketAddress)->sin_addr;
1470         break;
1471     case AF_INET6:
1472         addr = &((struct sockaddr_in6 *)&lpwhs->socketAddress)->sin6_addr;
1473         break;
1474     default:
1475         WARN("unsupported family %d\n", lpwhs->socketAddress.ss_family);
1476         INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1477         return FALSE;
1478     }
1479     inet_ntop(lpwhs->socketAddress.ss_family, addr, szaddr, sizeof(szaddr));
1480     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1481                           INTERNET_STATUS_NAME_RESOLVED,
1482                           szaddr, strlen(szaddr)+1);
1483 
1484     TRACE("resolved %s to %s\n", debugstr_w(lpwhs->lpszServerName), szaddr);
1485     return TRUE;
1486 }
1487 
1488 
1489 /***********************************************************************
1490  *           HTTPREQ_Destroy (internal)
1491  *
1492  * Deallocate request handle
1493  *
1494  */
1495 static void HTTPREQ_Destroy(object_header_t *hdr)
1496 {
1497     http_request_t *lpwhr = (http_request_t*) hdr;
1498     DWORD i;
1499 
1500     TRACE("\n");
1501 
1502     if(lpwhr->hCacheFile)
1503         CloseHandle(lpwhr->hCacheFile);
1504 
1505     HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1506 
1507     DeleteCriticalSection( &lpwhr->read_section );
1508     WININET_Release(&lpwhr->lpHttpSession->hdr);
1509 
1510     if (lpwhr->pAuthInfo)
1511     {
1512         if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1513             DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1514         if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1515             FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1516 
1517         HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1518         HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1519         HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1520         lpwhr->pAuthInfo = NULL;
1521     }
1522 
1523     if (lpwhr->pProxyAuthInfo)
1524     {
1525         if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1526             DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1527         if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1528             FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1529 
1530         HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1531         HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1532         HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1533         lpwhr->pProxyAuthInfo = NULL;
1534     }
1535 
1536     HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1537     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1538     HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1539     HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1540     HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1541 
1542     for (i = 0; i < lpwhr->nCustHeaders; i++)
1543     {
1544         HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1545         HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1546     }
1547 
1548     HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1549     HeapFree(GetProcessHeap(), 0, lpwhr);
1550 }
1551 
1552 static void HTTPREQ_CloseConnection(object_header_t *hdr)
1553 {
1554     http_request_t *lpwhr = (http_request_t*) hdr;
1555 
1556     TRACE("%p\n",lpwhr);
1557 
1558 #ifdef HAVE_ZLIB
1559     if(lpwhr->gzip_stream) {
1560         inflateEnd(&lpwhr->gzip_stream->zstream);
1561         HeapFree(GetProcessHeap(), 0, lpwhr->gzip_stream);
1562         lpwhr->gzip_stream = NULL;
1563     }
1564 #endif
1565 
1566     if (!NETCON_connected(&lpwhr->netConnection))
1567         return;
1568 
1569     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1570                           INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1571 
1572     NETCON_close(&lpwhr->netConnection);
1573 
1574     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1575                           INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1576 }
1577 
1578 static BOOL HTTP_GetRequestURL(http_request_t *req, LPWSTR buf)
1579 {
1580     LPHTTPHEADERW host_header;
1581 
1582     static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
1583 
1584     host_header = HTTP_GetHeader(req, hostW);
1585     if(!host_header)
1586         return FALSE;
1587 
1588     sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
1589     return TRUE;
1590 }
1591 
1592 static DWORD HTTPREQ_QueryOption(object_header_t *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1593 {
1594     http_request_t *req = (http_request_t*)hdr;
1595 
1596     switch(option) {
1597     case INTERNET_OPTION_SECURITY_FLAGS:
1598     {
1599         http_session_t *lpwhs;
1600         lpwhs = req->lpHttpSession;
1601 
1602         if (*size < sizeof(ULONG))
1603             return ERROR_INSUFFICIENT_BUFFER;
1604 
1605         *size = sizeof(DWORD);
1606         if (lpwhs->hdr.dwFlags & INTERNET_FLAG_SECURE)
1607             *(DWORD*)buffer = SECURITY_FLAG_SECURE;
1608         else
1609             *(DWORD*)buffer = 0;
1610         FIXME("Semi-STUB INTERNET_OPTION_SECURITY_FLAGS: %x\n",*(DWORD*)buffer);
1611         return ERROR_SUCCESS;
1612     }
1613 
1614     case INTERNET_OPTION_HANDLE_TYPE:
1615         TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1616 
1617         if (*size < sizeof(ULONG))
1618             return ERROR_INSUFFICIENT_BUFFER;
1619 
1620         *size = sizeof(DWORD);
1621         *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1622         return ERROR_SUCCESS;
1623 
1624     case INTERNET_OPTION_URL: {
1625         WCHAR url[INTERNET_MAX_URL_LENGTH];
1626         HTTPHEADERW *host;
1627         DWORD len;
1628         WCHAR *pch;
1629 
1630         static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1631 
1632         TRACE("INTERNET_OPTION_URL\n");
1633 
1634         host = HTTP_GetHeader(req, hostW);
1635         strcpyW(url, httpW);
1636         strcatW(url, host->lpszValue);
1637         if (NULL != (pch = strchrW(url + strlenW(httpW), ':')))
1638             *pch = 0;
1639         strcatW(url, req->lpszPath);
1640 
1641         TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1642 
1643         if(unicode) {
1644             len = (strlenW(url)+1) * sizeof(WCHAR);
1645             if(*size < len)
1646                 return ERROR_INSUFFICIENT_BUFFER;
1647 
1648             *size = len;
1649             strcpyW(buffer, url);
1650             return ERROR_SUCCESS;
1651         }else {
1652             len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1653             if(len > *size)
1654                 return ERROR_INSUFFICIENT_BUFFER;
1655 
1656             *size = len;
1657             return ERROR_SUCCESS;
1658         }
1659     }
1660 
1661     case INTERNET_OPTION_CACHE_TIMESTAMPS: {
1662         INTERNET_CACHE_ENTRY_INFOW *info;
1663         INTERNET_CACHE_TIMESTAMPS *ts = buffer;
1664         WCHAR url[INTERNET_MAX_URL_LENGTH];
1665         DWORD nbytes, error;
1666         BOOL ret;
1667 
1668         TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
1669 
1670         if (*size < sizeof(*ts))
1671         {
1672             *size = sizeof(*ts);
1673             return ERROR_INSUFFICIENT_BUFFER;
1674         }
1675         nbytes = 0;
1676         HTTP_GetRequestURL(req, url);
1677         ret = GetUrlCacheEntryInfoW(url, NULL, &nbytes);
1678         error = GetLastError();
1679         if (!ret && error == ERROR_INSUFFICIENT_BUFFER)
1680         {
1681             if (!(info = HeapAlloc(GetProcessHeap(), 0, nbytes)))
1682                 return ERROR_OUTOFMEMORY;
1683 
1684             GetUrlCacheEntryInfoW(url, info, &nbytes);
1685 
1686             ts->ftExpires = info->ExpireTime;
1687             ts->ftLastModified = info->LastModifiedTime;
1688 
1689             HeapFree(GetProcessHeap(), 0, info);
1690             *size = sizeof(*ts);
1691             return ERROR_SUCCESS;
1692         }
1693         return error;
1694     }
1695 
1696     case INTERNET_OPTION_DATAFILE_NAME: {
1697         DWORD req_size;
1698 
1699         TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1700 
1701         if(!req->lpszCacheFile) {
1702             *size = 0;
1703             return ERROR_INTERNET_ITEM_NOT_FOUND;
1704         }
1705 
1706         if(unicode) {
1707             req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1708             if(*size < req_size)
1709                 return ERROR_INSUFFICIENT_BUFFER;
1710 
1711             *size = req_size;
1712             memcpy(buffer, req->lpszCacheFile, *size);
1713             return ERROR_SUCCESS;
1714         }else {
1715             req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1716             if (req_size > *size)
1717                 return ERROR_INSUFFICIENT_BUFFER;
1718 
1719             *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1720                     -1, buffer, *size, NULL, NULL);
1721             return ERROR_SUCCESS;
1722         }
1723     }
1724 
1725     case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1726         PCCERT_CONTEXT context;
1727 
1728         if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1729             *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1730             return ERROR_INSUFFICIENT_BUFFER;
1731         }
1732 
1733         context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1734         if(context) {
1735             INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1736             DWORD len;
1737 
1738             memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1739             info->ftExpiry = context->pCertInfo->NotAfter;
1740             info->ftStart = context->pCertInfo->NotBefore;
1741             if(unicode) {
1742                 len = CertNameToStrW(context->dwCertEncodingType,
1743                         &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1744                 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1745                 if(info->lpszSubjectInfo)
1746                     CertNameToStrW(context->dwCertEncodingType,
1747                              &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1748                              info->lpszSubjectInfo, len);
1749                 len = CertNameToStrW(context->dwCertEncodingType,
1750                          &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1751                 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1752                 if (info->lpszIssuerInfo)
1753                     CertNameToStrW(context->dwCertEncodingType,
1754                              &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1755                              info->lpszIssuerInfo, len);
1756             }else {
1757                 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1758 
1759                 len = CertNameToStrA(context->dwCertEncodingType,
1760                          &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1761                 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1762                 if(infoA->lpszSubjectInfo)
1763                     CertNameToStrA(context->dwCertEncodingType,
1764                              &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1765                              infoA->lpszSubjectInfo, len);
1766                 len = CertNameToStrA(context->dwCertEncodingType,
1767                          &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1768                 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1769                 if(infoA->lpszIssuerInfo)
1770                     CertNameToStrA(context->dwCertEncodingType,
1771                              &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1772                              infoA->lpszIssuerInfo, len);
1773             }
1774 
1775             /*
1776              * Contrary to MSDN, these do not appear to be set.
1777              * lpszProtocolName
1778              * lpszSignatureAlgName
1779              * lpszEncryptionAlgName
1780              * dwKeySize
1781              */
1782             CertFreeCertificateContext(context);
1783             return ERROR_SUCCESS;
1784         }
1785     }
1786     }
1787 
1788     return INET_QueryOption(option, buffer, size, unicode);
1789 }
1790 
1791 static DWORD HTTPREQ_SetOption(object_header_t *hdr, DWORD option, void *buffer, DWORD size)
1792 {
1793     http_request_t *req = (http_request_t*)hdr;
1794 
1795     switch(option) {
1796     case INTERNET_OPTION_SEND_TIMEOUT:
1797     case INTERNET_OPTION_RECEIVE_TIMEOUT:
1798         TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1799 
1800         if (size != sizeof(DWORD))
1801             return ERROR_INVALID_PARAMETER;
1802 
1803         return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1804                     *(DWORD*)buffer);
1805 
1806     case INTERNET_OPTION_USERNAME:
1807         HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszUserName);
1808         if (!(req->lpHttpSession->lpszUserName = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1809         return ERROR_SUCCESS;
1810 
1811     case INTERNET_OPTION_PASSWORD:
1812         HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszPassword);
1813         if (!(req->lpHttpSession->lpszPassword = heap_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1814         return ERROR_SUCCESS;
1815     case INTERNET_OPTION_HTTP_DECODING:
1816         if(size != sizeof(BOOL))
1817             return ERROR_INVALID_PARAMETER;
1818         req->decoding = *(BOOL*)buffer;
1819         return ERROR_SUCCESS;
1820     }
1821 
1822     return ERROR_INTERNET_INVALID_OPTION;
1823 }
1824 
1825 /* read some more data into the read buffer (the read section must be held) */
1826 static BOOL read_more_data( http_request_t *req, int maxlen )
1827 {
1828     int len;
1829 
1830     if (req->read_pos)
1831     {
1832         /* move existing data to the start of the buffer */
1833         if(req->read_size)
1834             memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
1835         req->read_pos = 0;
1836     }
1837 
1838     if (maxlen == -1) maxlen = sizeof(req->read_buf);
1839 
1840     if(!NETCON_recv( &req->netConnection, req->read_buf + req->read_size,
1841                      maxlen - req->read_size, 0, &len ))
1842         return FALSE;
1843 
1844     req->read_size += len;
1845     return TRUE;
1846 }
1847 
1848 /* remove some amount of data from the read buffer (the read section must be held) */
1849 static void remove_data( http_request_t *req, int count )
1850 {
1851     if (!(req->read_size -= count)) req->read_pos = 0;
1852     else req->read_pos += count;
1853 }
1854 
1855 static BOOL read_line( http_request_t *req, LPSTR buffer, DWORD *len )
1856 {
1857     int count, bytes_read, pos = 0;
1858 
1859     EnterCriticalSection( &req->read_section );
1860     for (;;)
1861     {
1862         BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
1863 
1864         if (eol)
1865         {
1866             count = eol - (req->read_buf + req->read_pos);
1867             bytes_read = count + 1;
1868         }
1869         else count = bytes_read = req->read_size;
1870 
1871         count = min( count, *len - pos );
1872         memcpy( buffer + pos, req->read_buf + req->read_pos, count );
1873         pos += count;
1874         remove_data( req, bytes_read );
1875         if (eol) break;
1876 
1877         if (!read_more_data( req, -1 ) || !req->read_size)
1878         {
1879             *len = 0;
1880             TRACE( "returning empty string\n" );
1881             LeaveCriticalSection( &req->read_section );
1882             return FALSE;
1883         }
1884     }
1885     LeaveCriticalSection( &req->read_section );
1886 
1887     if (pos < *len)
1888     {
1889         if (pos && buffer[pos - 1] == '\r') pos--;
1890         *len = pos + 1;
1891     }
1892     buffer[*len - 1] = 0;
1893     TRACE( "returning %s\n", debugstr_a(buffer));
1894     return TRUE;
1895 }
1896 
1897 /* discard data contents until we reach end of line (the read section must be held) */
1898 static BOOL discard_eol( http_request_t *req )
1899 {
1900     do
1901     {
1902         BYTE *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
1903         if (eol)
1904         {
1905             remove_data( req, (eol + 1) - (req->read_buf + req->read_pos) );
1906             break;
1907         }
1908         req->read_pos = req->read_size = 0;  /* discard everything */
1909         if (!read_more_data( req, -1 )) return FALSE;
1910     } while (req->read_size);
1911     return TRUE;
1912 }
1913 
1914 /* read the size of the next chunk (the read section must be held) */
1915 static BOOL start_next_chunk( http_request_t *req )
1916 {
1917     DWORD chunk_size = 0;
1918 
1919     if (!req->dwContentLength) return TRUE;
1920     if (req->dwContentLength == req->dwContentRead)
1921     {
1922         /* read terminator for the previous chunk */
1923         if (!discard_eol( req )) return FALSE;
1924         req->dwContentLength = ~0u;
1925         req->dwContentRead = 0;
1926     }
1927     for (;;)
1928     {
1929         while (req->read_size)
1930         {
1931             char ch = req->read_buf[req->read_pos];
1932             if (ch >= '' && ch <= '9') chunk_size = chunk_size * 16 + ch - '';
1933             else if (ch >= 'a' && ch <= 'f') chunk_size = chunk_size * 16 + ch - 'a' + 10;
1934             else if (ch >= 'A' && ch <= 'F') chunk_size = chunk_size * 16 + ch - 'A' + 10;
1935             else if (ch == ';' || ch == '\r' || ch == '\n')
1936             {
1937                 TRACE( "reading %u byte chunk\n", chunk_size );
1938                 req->dwContentLength = chunk_size;
1939                 req->dwContentRead = 0;
1940                 if (!discard_eol( req )) return FALSE;
1941                 return TRUE;
1942             }
1943             remove_data( req, 1 );
1944         }
1945         if (!read_more_data( req, -1 )) return FALSE;
1946         if (!req->read_size)
1947         {
1948             req->dwContentLength = req->dwContentRead = 0;
1949             return TRUE;
1950         }
1951     }
1952 }
1953 
1954 /* check if we have reached the end of the data to read (the read section must be held) */
1955 static BOOL end_of_read_data( http_request_t *req )
1956 {
1957     if (req->gzip_stream) return req->gzip_stream->end_of_data && !req->gzip_stream->buf_size;
1958     if (req->read_chunked) return (req->dwContentLength == 0);
1959     if (req->dwContentLength == ~0u) return FALSE;
1960     return (req->dwContentLength == req->dwContentRead);
1961 }
1962 
1963 /* fetch some more data into the read buffer (the read section must be held) */
1964 static BOOL refill_buffer( http_request_t *req )
1965 {
1966     int len = sizeof(req->read_buf);
1967 
1968     if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
1969     {
1970         if (!start_next_chunk( req )) return FALSE;
1971     }
1972 
1973     if (req->dwContentLength != ~0u) len = min( len, req->dwContentLength - req->dwContentRead );
1974     if (len <= req->read_size) return TRUE;
1975 
1976     if (!read_more_data( req, len )) return FALSE;
1977     if (!req->read_size) req->dwContentLength = req->dwContentRead = 0;
1978     return TRUE;
1979 }
1980 
1981 static DWORD read_gzip_data(http_request_t *req, BYTE *buf, int size, BOOL sync, int *read_ret)
1982 {
1983     DWORD ret = ERROR_SUCCESS;
1984     int read = 0;
1985 
1986 #ifdef HAVE_ZLIB
1987     z_stream *zstream = &req->gzip_stream->zstream;
1988     int zres;
1989 
1990     while(read < size && !req->gzip_stream->end_of_data) {
1991         if(!req->read_size) {
1992             if(!sync || !refill_buffer(req))
1993                 break;
1994         }
1995 
1996         zstream->next_in = req->read_buf+req->read_pos;
1997         zstream->avail_in = req->read_size;
1998         zstream->next_out = buf+read;
1999         zstream->avail_out = size-read;
2000         zres = inflate(zstream, Z_FULL_FLUSH);
2001         read = size - zstream->avail_out;
2002         remove_data(req, req->read_size-zstream->avail_in);
2003         if(zres == Z_STREAM_END) {
2004             TRACE("end of data\n");
2005             req->gzip_stream->end_of_data = TRUE;
2006         }else if(zres != Z_OK) {
2007             WARN("inflate failed %d\n", zres);
2008             if(!read)
2009                 ret = ERROR_INTERNET_DECODING_FAILED;
2010             break;
2011         }
2012     }
2013 #endif
2014 
2015     *read_ret = read;
2016     return ret;
2017 }
2018 
2019 static void refill_gzip_buffer(http_request_t *req)
2020 {
2021     DWORD res;
2022     int len;
2023 
2024     if(!req->gzip_stream || !req->read_size || req->gzip_stream->buf_size == sizeof(req->gzip_stream->buf))
2025         return;
2026 
2027     if(req->gzip_stream->buf_pos) {
2028         if(req->gzip_stream->buf_size)
2029             memmove(req->gzip_stream->buf, req->gzip_stream->buf + req->gzip_stream->buf_pos, req->gzip_stream->buf_size);
2030         req->gzip_stream->buf_pos = 0;
2031     }
2032 
2033     res = read_gzip_data(req, req->gzip_stream->buf + req->gzip_stream->buf_size,
2034             sizeof(req->gzip_stream->buf) - req->gzip_stream->buf_size, FALSE, &len);
2035     if(res == ERROR_SUCCESS)
2036         req->gzip_stream->buf_size += len;
2037 }
2038 
2039 /* return the size of data available to be read immediately (the read section must be held) */
2040 static DWORD get_avail_data( http_request_t *req )
2041 {
2042     if (req->gzip_stream) {
2043         refill_gzip_buffer(req);
2044         return req->gzip_stream->buf_size;
2045     }
2046     if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
2047         return 0;
2048     return min( req->read_size, req->dwContentLength - req->dwContentRead );
2049 }
2050 
2051 static void HTTP_ReceiveRequestData(http_request_t *req, BOOL first_notif)
2052 {
2053     INTERNET_ASYNC_RESULT iar;
2054 
2055     TRACE("%p\n", req);
2056 
2057     EnterCriticalSection( &req->read_section );
2058     if (refill_buffer( req )) {
2059         iar.dwResult = (DWORD_PTR)req->hdr.hInternet;
2060         iar.dwError = first_notif ? 0 : get_avail_data(req);
2061     }else {
2062         iar.dwResult = 0;
2063         iar.dwError = INTERNET_GetLastError();
2064     }
2065     LeaveCriticalSection( &req->read_section );
2066 
2067     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2068                           sizeof(INTERNET_ASYNC_RESULT));
2069 }
2070 
2071 /* read data from the http connection (the read section must be held) */
2072 static DWORD HTTPREQ_Read(http_request_t *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
2073 {
2074     BOOL finished_reading = FALSE;
2075     int len, bytes_read = 0;
2076     DWORD ret = ERROR_SUCCESS;
2077 
2078     EnterCriticalSection( &req->read_section );
2079 
2080     if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
2081     {
2082         if (!start_next_chunk( req )) goto done;
2083     }
2084 
2085     if(req->gzip_stream) {
2086         if(req->gzip_stream->buf_size) {
2087             bytes_read = min(req->gzip_stream->buf_size, size);
2088             memcpy(buffer, req->gzip_stream->buf + req->gzip_stream->buf_pos, bytes_read);
2089             req->gzip_stream->buf_pos += bytes_read;
2090             req->gzip_stream->buf_size -= bytes_read;
2091         }else if(!req->read_size && !req->gzip_stream->end_of_data) {
2092             refill_buffer(req);
2093         }
2094 
2095         if(size > bytes_read) {
2096             ret = read_gzip_data(req, (BYTE*)buffer+bytes_read, size-bytes_read, sync, &len);
2097             if(ret == ERROR_SUCCESS)
2098                 bytes_read += len;
2099         }
2100 
2101         finished_reading = req->gzip_stream->end_of_data && !req->gzip_stream->buf_size;
2102     }else {
2103         if (req->dwContentLength != ~0u) size = min( size, req->dwContentLength - req->dwContentRead );
2104 
2105         if (req->read_size) {
2106             bytes_read = min( req->read_size, size );
2107             memcpy( buffer, req->read_buf + req->read_pos, bytes_read );
2108             remove_data( req, bytes_read );
2109         }
2110 
2111         if (size > bytes_read && (!bytes_read || sync)) {
2112             if (NETCON_recv( &req->netConnection, (char *)buffer + bytes_read, size - bytes_read,
2113                              sync ? MSG_WAITALL : 0, &len))
2114                 bytes_read += len;
2115             /* always return success, even if the network layer returns an error */
2116         }
2117 
2118         finished_reading = !bytes_read && req->dwContentRead == req->dwContentLength;
2119     }
2120 done:
2121     req->dwContentRead += bytes_read;
2122     *read = bytes_read;
2123 
2124     TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read, req->dwContentRead, req->dwContentLength );
2125     LeaveCriticalSection( &req->read_section );
2126 
2127     if(ret == ERROR_SUCCESS && req->lpszCacheFile) {
2128         BOOL res;
2129         DWORD dwBytesWritten;
2130 
2131         res = WriteFile(req->hCacheFile, buffer, bytes_read, &dwBytesWritten, NULL);
2132         if(!res)
2133             WARN("WriteFile failed: %u\n", GetLastError());
2134     }
2135 
2136     if(finished_reading)
2137         HTTP_FinishedReading(req);
2138 
2139     return ret;
2140 }
2141 
2142 
2143 static DWORD HTTPREQ_ReadFile(object_header_t *hdr, void *buffer, DWORD size, DWORD *read)
2144 {
2145     http_request_t *req = (http_request_t*)hdr;
2146     return HTTPREQ_Read(req, buffer, size, read, TRUE);
2147 }
2148 
2149 static void HTTPREQ_AsyncReadFileExAProc(WORKREQUEST *workRequest)
2150 {
2151     struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
2152     http_request_t *req = (http_request_t*)workRequest->hdr;
2153     INTERNET_ASYNC_RESULT iar;
2154     DWORD res;
2155 
2156     TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
2157 
2158     res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
2159             data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
2160 
2161     iar.dwResult = res == ERROR_SUCCESS;
2162     iar.dwError = res;
2163 
2164     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2165                           INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2166                           sizeof(INTERNET_ASYNC_RESULT));
2167 }
2168 
2169 static DWORD HTTPREQ_ReadFileExA(object_header_t *hdr, INTERNET_BUFFERSA *buffers,
2170         DWORD flags, DWORD_PTR context)
2171 {
2172     http_request_t *req = (http_request_t*)hdr;
2173     DWORD res;
2174 
2175     if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
2176         FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
2177 
2178     if (buffers->dwStructSize != sizeof(*buffers))
2179         return ERROR_INVALID_PARAMETER;
2180 
2181     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2182 
2183     if ((hdr->dwFlags & INTERNET_FLAG_ASYNC) && !get_avail_data(req))
2184     {
2185         WORKREQUEST workRequest;
2186 
2187         if (TryEnterCriticalSection( &req->read_section ))
2188         {
2189             if (get_avail_data(req))
2190             {
2191                 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength,
2192                                    &buffers->dwBufferLength, FALSE);
2193                 LeaveCriticalSection( &req->read_section );
2194                 goto done;
2195             }
2196             LeaveCriticalSection( &req->read_section );
2197         }
2198 
2199         workRequest.asyncproc = HTTPREQ_AsyncReadFileExAProc;
2200         workRequest.hdr = WININET_AddRef(&req->hdr);
2201         workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
2202 
2203         INTERNET_AsyncCall(&workRequest);
2204 
2205         return ERROR_IO_PENDING;
2206     }
2207 
2208     res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
2209             !(flags & IRF_NO_WAIT));
2210 
2211 done:
2212     if (res == ERROR_SUCCESS) {
2213         DWORD size = buffers->dwBufferLength;
2214         INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2215                 &size, sizeof(size));
2216     }
2217 
2218     return res;
2219 }
2220 
2221 static void HTTPREQ_AsyncReadFileExWProc(WORKREQUEST *workRequest)
2222 {
2223     struct WORKREQ_INTERNETREADFILEEXW const *data = &workRequest->u.InternetReadFileExW;
2224     http_request_t *req = (http_request_t*)workRequest->hdr;
2225     INTERNET_ASYNC_RESULT iar;
2226     DWORD res;
2227 
2228     TRACE("INTERNETREADFILEEXW %p\n", workRequest->hdr);
2229 
2230     res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
2231             data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
2232 
2233     iar.dwResult = res == ERROR_SUCCESS;
2234     iar.dwError = res;
2235 
2236     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
2237                           INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2238                           sizeof(INTERNET_ASYNC_RESULT));
2239 }
2240 
2241 static DWORD HTTPREQ_ReadFileExW(object_header_t *hdr, INTERNET_BUFFERSW *buffers,
2242         DWORD flags, DWORD_PTR context)
2243 {
2244 
2245     http_request_t *req = (http_request_t*)hdr;
2246     DWORD res;
2247 
2248     if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
2249         FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
2250 
2251     if (buffers->dwStructSize != sizeof(*buffers))
2252         return ERROR_INVALID_PARAMETER;
2253 
2254     INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2255 
2256     if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
2257     {
2258         WORKREQUEST workRequest;
2259 
2260         if (TryEnterCriticalSection( &req->read_section ))
2261         {
2262             if (get_avail_data(req))
2263             {
2264                 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength,
2265                                    &buffers->dwBufferLength, FALSE);
2266                 LeaveCriticalSection( &req->read_section );
2267                 goto done;
2268             }
2269             LeaveCriticalSection( &req->read_section );
2270         }
2271 
2272         workRequest.asyncproc = HTTPREQ_AsyncReadFileExWProc;
2273         workRequest.hdr = WININET_AddRef(&req->hdr);
2274         workRequest.u.InternetReadFileExW.lpBuffersOut = buffers;
2275 
2276         INTERNET_AsyncCall(&workRequest);
2277 
2278         return ERROR_IO_PENDING;
2279     }
2280 
2281     res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
2282             !(flags & IRF_NO_WAIT));
2283 
2284 done:
2285     if (res == ERROR_SUCCESS) {
2286         DWORD size = buffers->dwBufferLength;
2287         INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
2288                 &size, sizeof(size));
2289     }
2290 
2291     return res;
2292 }
2293 
2294 static BOOL HTTPREQ_WriteFile(object_header_t *hdr, const void *buffer, DWORD size, DWORD *written)
2295 {
2296     BOOL ret;
2297     http_request_t *lpwhr = (http_request_t*)hdr;
2298 
2299     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2300 
2301     *written = 0;
2302     if ((ret = NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written)))
2303         lpwhr->dwBytesWritten += *written;
2304 
2305     INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
2306     return ret;
2307 }
2308 
2309 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
2310 {
2311     http_request_t *req = (http_request_t*)workRequest->hdr;
2312 
2313     HTTP_ReceiveRequestData(req, FALSE);
2314 }
2315 
2316 static DWORD HTTPREQ_QueryDataAvailable(object_header_t *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
2317 {
2318     http_request_t *req = (http_request_t*)hdr;
2319 
2320     TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
2321 
2322     if (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2323     {
2324         WORKREQUEST workRequest;
2325 
2326         /* never wait, if we can't enter the section we queue an async request right away */
2327         if (TryEnterCriticalSection( &req->read_section ))
2328         {
2329             if ((*available = get_avail_data( req ))) goto done;
2330             if (end_of_read_data( req )) goto done;
2331             LeaveCriticalSection( &req->read_section );
2332         }
2333 
2334         workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
2335         workRequest.hdr = WININET_AddRef( &req->hdr );
2336 
2337         INTERNET_AsyncCall(&workRequest);
2338 
2339         return ERROR_IO_PENDING;
2340     }
2341 
2342     EnterCriticalSection( &req->read_section );
2343 
2344     if (!(*available = get_avail_data( req )) && !end_of_read_data( req ))
2345     {
2346         refill_buffer( req );
2347         *available = get_avail_data( req );
2348     }
2349 
2350 done:
2351     if (*available == sizeof(req->read_buf) && !req->gzip_stream)  /* check if we have even more pending in the socket */
2352     {
2353         DWORD extra;
2354         if (NETCON_query_data_available(&req->netConnection, &extra))
2355             *available = min( *available + extra, req->dwContentLength - req->dwContentRead );
2356     }
2357     LeaveCriticalSection( &req->read_section );
2358 
2359     TRACE( "returning %u\n", *available );
2360     return ERROR_SUCCESS;
2361 }
2362 
2363 static const object_vtbl_t HTTPREQVtbl = {
2364     HTTPREQ_Destroy,
2365     HTTPREQ_CloseConnection,
2366     HTTPREQ_QueryOption,
2367     HTTPREQ_SetOption,
2368     HTTPREQ_ReadFile,
2369     HTTPREQ_ReadFileExA,
2370     HTTPREQ_ReadFileExW,
2371     HTTPREQ_WriteFile,
2372     HTTPREQ_QueryDataAvailable,
2373     NULL
2374 };
2375 
2376 /***********************************************************************
2377  *           HTTP_HttpOpenRequestW (internal)
2378  *
2379  * Open a HTTP request handle
2380  *
2381  * RETURNS
2382  *    HINTERNET  a HTTP request handle on success
2383  *    NULL       on failure
2384  *
2385  */
2386 HINTERNET WINAPI HTTP_HttpOpenRequestW(http_session_t *lpwhs,
2387         LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
2388         LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
2389         DWORD dwFlags, DWORD_PTR dwContext)
2390 {
2391     appinfo_t *hIC = NULL;
2392     http_request_t *lpwhr;
2393     LPWSTR lpszHostName = NULL;
2394     HINTERNET handle = NULL;
2395     static const WCHAR szHostForm[] = {'%','s',':','%','u',0};
2396     DWORD len;
2397 
2398     TRACE("-->\n");
2399 
2400     assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
2401     hIC = lpwhs->lpAppInfo;
2402 
2403     lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(http_request_t));
2404     if (NULL == lpwhr)
2405     {
2406         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2407         goto lend;
2408     }
2409     lpwhr->hdr.htype = WH_HHTTPREQ;
2410     lpwhr->hdr.vtbl = &HTTPREQVtbl;
2411     lpwhr->hdr.dwFlags = dwFlags;
2412     lpwhr->hdr.dwContext = dwContext;
2413     lpwhr->hdr.refs = 1;
2414     lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
2415     lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
2416     lpwhr->dwContentLength = ~0u;
2417     InitializeCriticalSection( &lpwhr->read_section );
2418 
2419     WININET_AddRef( &lpwhs->hdr );
2420     lpwhr->lpHttpSession = lpwhs;
2421     list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
2422 
2423     lpszHostName = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) *
2424             (strlenW(lpwhs->lpszHostName) + 7 /* length of ":65535" + 1 */));
2425     if (NULL == lpszHostName)
2426     {
2427         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2428         goto lend;
2429     }
2430 
2431     handle = WININET_AllocHandle( &lpwhr->hdr );
2432     if (NULL == handle)
2433     {
2434         INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2435         goto lend;
2436     }
2437 
2438     if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
2439     {
2440         InternetCloseHandle( handle );
2441         handle = NULL;
2442         goto lend;
2443     }
2444 
2445     if (lpszObjectName && *lpszObjectName) {
2446         HRESULT rc;
2447 
2448         len = 0;
2449         rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
2450         if (rc != E_POINTER)
2451             len = strlenW(lpszObjectName)+1;
2452         lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2453         rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
2454                    URL_ESCAPE_SPACES_ONLY);
2455         if (rc != S_OK)
2456         {
2457             ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
2458             strcpyW(lpwhr->lpszPath,lpszObjectName);
2459         }
2460     }else {
2461         static const WCHAR slashW[] = {'/',0};
2462 
2463         lpwhr->lpszPath = heap_strdupW(slashW);
2464     }
2465 
2466     if (lpszReferrer && *lpszReferrer)
2467         HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2468 
2469     if (lpszAcceptTypes)
2470     {
2471         int i;
2472         for (i = 0; lpszAcceptTypes[i]; i++)
2473         {
2474             if (!*lpszAcceptTypes[i]) continue;
2475             HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
2476                                HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
2477                                HTTP_ADDHDR_FLAG_REQ |
2478                                (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
2479         }
2480     }
2481 
2482     lpwhr->lpszVerb = heap_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
2483     lpwhr->lpszVersion = heap_strdupW(lpszVersion ? lpszVersion : g_szHttp1_1);
2484 
2485     if (lpwhs->nHostPort != INTERNET_INVALID_PORT_NUMBER &&
2486         lpwhs->nHostPort != INTERNET_DEFAULT_HTTP_PORT &&
2487         lpwhs->nHostPort != INTERNET_DEFAULT_HTTPS_PORT)
2488     {
2489         sprintfW(lpszHostName, szHostForm, lpwhs->lpszHostName, lpwhs->nHostPort);
2490         HTTP_ProcessHeader(lpwhr, hostW, lpszHostName,
2491                 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2492     }
2493     else
2494         HTTP_ProcessHeader(lpwhr, hostW, lpwhs->lpszHostName,
2495                 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2496 
2497     if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
2498         lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
2499                         INTERNET_DEFAULT_HTTPS_PORT :
2500                         INTERNET_DEFAULT_HTTP_PORT);
2501 
2502     if (lpwhs->nHostPort == INTERNET_INVALID_PORT_NUMBER)
2503         lpwhs->nHostPort = (dwFlags & INTERNET_FLAG_SECURE ?
2504                         INTERNET_DEFAULT_HTTPS_PORT :
2505                         INTERNET_DEFAULT_HTTP_PORT);
2506 
2507     if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2508         HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
2509 
2510     INTERNET_SendCallback(&lpwhs->hdr, dwContext,
2511                           INTERNET_STATUS_HANDLE_CREATED, &handle,
2512                           sizeof(handle));
2513 
2514 lend:
2515     HeapFree(GetProcessHeap(), 0, lpszHostName);
2516     if( lpwhr )
2517         WININET_Release( &lpwhr->hdr );
2518 
2519     TRACE("<-- %p (%p)\n", handle, lpwhr);
2520     return handle;
2521 }
2522 
2523 /* read any content returned by the server so that the connection can be
2524  * reused */
2525 static void HTTP_DrainContent(http_request_t *req)
2526 {
2527     DWORD bytes_read;
2528 
2529     if (!NETCON_connected(&req->netConnection)) return;
2530 
2531     if (req->dwContentLength == -1)
2532     {
2533         NETCON_close(&req->netConnection);
2534         return;
2535     }
2536     if (!strcmpW(req->lpszVerb, szHEAD)) return;
2537 
2538     do
2539     {
2540         char buffer[2048];
2541         if (HTTPREQ_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
2542             return;
2543     } while (bytes_read);
2544 }
2545 
2546 static const LPCWSTR header_lookup[] = {
2547     szMime_Version,             /* HTTP_QUERY_MIME_VERSION = 0 */
2548     szContent_Type,             /* HTTP_QUERY_CONTENT_TYPE = 1 */
2549     szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2550     szContent_ID,               /* HTTP_QUERY_CONTENT_ID = 3 */
2551     NULL,                       /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2552     szContent_Length,           /* HTTP_QUERY_CONTENT_LENGTH =  5 */
2553     szContent_Language,         /* HTTP_QUERY_CONTENT_LANGUAGE =  6 */
2554     szAllow,                    /* HTTP_QUERY_ALLOW = 7 */
2555     szPublic,                   /* HTTP_QUERY_PUBLIC = 8 */
2556     szDate,                     /* HTTP_QUERY_DATE = 9 */
2557     szExpires,                  /* HTTP_QUERY_EXPIRES = 10 */
2558     szLast_Modified,            /* HTTP_QUERY_LAST_MODIFIED = 11 */
2559     NULL,                       /* HTTP_QUERY_MESSAGE_ID = 12 */
2560     szURI,                      /* HTTP_QUERY_URI = 13 */
2561     szFrom,                     /* HTTP_QUERY_DERIVED_FROM = 14 */
2562     NULL,                       /* HTTP_QUERY_COST = 15 */
2563     NULL,                       /* HTTP_QUERY_LINK = 16 */
2564     szPragma,                   /* HTTP_QUERY_PRAGMA = 17 */
2565     NULL,                       /* HTTP_QUERY_VERSION = 18 */
2566     szStatus,                   /* HTTP_QUERY_STATUS_CODE = 19 */
2567     NULL,                       /* HTTP_QUERY_STATUS_TEXT = 20 */
2568     NULL,                       /* HTTP_QUERY_RAW_HEADERS = 21 */
2569     NULL,                       /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2570     szConnection,               /* HTTP_QUERY_CONNECTION = 23 */
2571     szAccept,                   /* HTTP_QUERY_ACCEPT = 24 */
2572     szAccept_Charset,           /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2573     szAccept_Encoding,          /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2574     szAccept_Language,          /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2575     szAuthorization,            /* HTTP_QUERY_AUTHORIZATION = 28 */
2576     szContent_Encoding,         /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2577     NULL,                       /* HTTP_QUERY_FORWARDED = 30 */
2578     NULL,                       /* HTTP_QUERY_FROM = 31 */
2579     szIf_Modified_Since,        /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2580     szLocation,                 /* HTTP_QUERY_LOCATION = 33 */
2581     NULL,                       /* HTTP_QUERY_ORIG_URI = 34 */
2582     szReferer,                  /* HTTP_QUERY_REFERER = 35 */
2583     szRetry_After,              /* HTTP_QUERY_RETRY_AFTER = 36 */
2584     szServer,                   /* HTTP_QUERY_SERVER = 37 */
2585     NULL,                       /* HTTP_TITLE = 38 */
2586     szUser_Agent,               /* HTTP_QUERY_USER_AGENT = 39 */
2587     szWWW_Authenticate,         /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2588     szProxy_Authenticate,       /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2589     szAccept_Ranges,            /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2590     szSet_Cookie,               /* HTTP_QUERY_SET_COOKIE = 43 */
2591     szCookie,                   /* HTTP_QUERY_COOKIE = 44 */
2592     NULL,                       /* HTTP_QUERY_REQUEST_METHOD = 45 */
2593     NULL,                       /* HTTP_QUERY_REFRESH = 46 */
2594     NULL,                       /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2595     szAge,                      /* HTTP_QUERY_AGE = 48 */
2596     szCache_Control,            /* HTTP_QUERY_CACHE_CONTROL = 49 */
2597     szContent_Base,             /* HTTP_QUERY_CONTENT_BASE = 50 */
2598     szContent_Location,         /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2599     szContent_MD5,              /* HTTP_QUERY_CONTENT_MD5 = 52 */
2600     szContent_Range,            /* HTTP_QUERY_CONTENT_RANGE = 53 */
2601     szETag,                     /* HTTP_QUERY_ETAG = 54 */
2602     hostW,                      /* HTTP_QUERY_HOST = 55 */
2603     szIf_Match,                 /* HTTP_QUERY_IF_MATCH = 56 */
2604     szIf_None_Match,            /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2605     szIf_Range,                 /* HTTP_QUERY_IF_RANGE = 58 */
2606     szIf_Unmodified_Since,      /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2607     szMax_Forwards,             /* HTTP_QUERY_MAX_FORWARDS = 60 */
2608     szProxy_Authorization,      /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2609     szRange,                    /* HTTP_QUERY_RANGE = 62 */
2610     szTransfer_Encoding,        /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2611     szUpgrade,                  /* HTTP_QUERY_UPGRADE = 64 */
2612     szVary,                     /* HTTP_QUERY_VARY = 65 */
2613     szVia,                      /* HTTP_QUERY_VIA = 66 */
2614     szWarning,                  /* HTTP_QUERY_WARNING = 67 */
2615     szExpect,                   /* HTTP_QUERY_EXPECT = 68 */
2616     szProxy_Connection,         /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2617     szUnless_Modified_Since,    /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2618 };
2619 
2620 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2621 
2622 /***********************************************************************
2623  *           HTTP_HttpQueryInfoW (internal)
2624  */
2625 static BOOL HTTP_HttpQueryInfoW(http_request_t *lpwhr, DWORD dwInfoLevel,
2626         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2627 {
2628     LPHTTPHEADERW lphttpHdr = NULL;
2629     BOOL bSuccess = FALSE;
2630     BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
2631     INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2632     DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2633     INT index = -1;
2634 
2635     /* Find requested header structure */
2636     switch (level)
2637     {
2638     case HTTP_QUERY_CUSTOM:
2639         if (!lpBuffer) return FALSE;
2640         index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2641         break;
2642     case HTTP_QUERY_RAW_HEADERS_CRLF:
2643         {
2644             LPWSTR headers;
2645             DWORD len = 0;
2646             BOOL ret = FALSE;
2647 
2648             if (request_only)
2649                 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
2650             else
2651                 headers = lpwhr->lpszRawHeaders;
2652 
2653             if (headers)
2654                 len = strlenW(headers) * sizeof(WCHAR);
2655 
2656             if (len + sizeof(WCHAR) > *lpdwBufferLength)
2657             {
2658                 len += sizeof(WCHAR);
2659                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2660                 ret = FALSE;
2661             }
2662             else if (lpBuffer)
2663             {
2664                 if (headers)
2665                     memcpy(lpBuffer, headers, len + sizeof(WCHAR));
2666                 else
2667                 {
2668                     len = strlenW(szCrLf) * sizeof(WCHAR);
2669                     memcpy(lpBuffer, szCrLf, sizeof(szCrLf));
2670                 }
2671                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
2672                 ret = TRUE;
2673             }
2674             *lpdwBufferLength = len;
2675 
2676             if (request_only)
2677                 HeapFree(GetProcessHeap(), 0, headers);
2678             return ret;
2679         }
2680     case HTTP_QUERY_RAW_HEADERS:
2681         {
2682             LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
2683             DWORD i, size = 0;
2684             LPWSTR pszString = lpBuffer;
2685 
2686             for (i = 0; ppszRawHeaderLines[i]; i++)
2687                 size += strlenW(ppszRawHeaderLines[i]) + 1;
2688 
2689             if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2690             {
2691                 HTTP_FreeTokens(ppszRawHeaderLines);
2692                 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2693                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2694                 return FALSE;
2695             }
2696             if (pszString)
2697             {
2698                 for (i = 0; ppszRawHeaderLines[i]; i++)
2699                 {
2700                     DWORD len = strlenW(ppszRawHeaderLines[i]);
2701                     memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2702                     pszString += len+1;
2703                 }
2704                 *pszString = '\0';
2705                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, size));
2706             }
2707             *lpdwBufferLength = size * sizeof(WCHAR);
2708             HTTP_FreeTokens(ppszRawHeaderLines);
2709 
2710             return TRUE;
2711         }
2712     case HTTP_QUERY_STATUS_TEXT:
2713         if (lpwhr->lpszStatusText)
2714         {
2715             DWORD len = strlenW(lpwhr->lpszStatusText);
2716             if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2717             {
2718                 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2719                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2720                 return FALSE;
2721             }
2722             if (lpBuffer)
2723             {
2724                 memcpy(lpBuffer, lpwhr->lpszStatusText, (len + 1) * sizeof(WCHAR));
2725                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2726             }
2727             *lpdwBufferLength = len * sizeof(WCHAR);
2728             return TRUE;
2729         }
2730         break;
2731     case HTTP_QUERY_VERSION:
2732         if (lpwhr->lpszVersion)
2733         {
2734             DWORD len = strlenW(lpwhr->lpszVersion);
2735             if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2736             {
2737                 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2738                 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2739                 return FALSE;
2740             }
2741             if (lpBuffer)
2742             {
2743                 memcpy(lpBuffer, lpwhr->lpszVersion, (len + 1) * sizeof(WCHAR));
2744                 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2745             }
2746             *lpdwBufferLength = len * sizeof(WCHAR);
2747             return TRUE;
2748         }
2749         break;
2750     case HTTP_QUERY_CONTENT_ENCODING:
2751         index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[lpwhr->gzip_stream ? HTTP_QUERY_CONTENT_TYPE : level],
2752                 requested_index,request_only);
2753         break;
2754     default:
2755         assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2756 
2757         if (level < LAST_TABLE_HEADER && header_lookup[level])
2758             index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2759                                               requested_index,request_only);
2760     }
2761 
2762     if (index >= 0)
2763         lphttpHdr = &lpwhr->pCustHeaders[index];
2764 
2765     /* Ensure header satisfies requested attributes */
2766     if (!lphttpHdr ||
2767         ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2768          (~lphttpHdr->wFlags & HDR_ISREQUEST)))
2769     {
2770         INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2771         return bSuccess;
2772     }
2773 
2774     if (lpdwIndex && level != HTTP_QUERY_STATUS_CODE) (*lpdwIndex)++;
2775 
2776     /* coalesce value to requested type */
2777     if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
2778     {
2779         *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2780         TRACE(" returning number: %d\n", *(int *)lpBuffer);
2781         bSuccess = TRUE;
2782     }
2783     else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
2784     {
2785         time_t tmpTime;
2786         struct tm tmpTM;
2787         SYSTEMTIME *STHook;
2788 
2789         tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2790 
2791         tmpTM = *gmtime(&tmpTime);
2792         STHook = (SYSTEMTIME *)lpBuffer;
2793         STHook->wDay = tmpTM.tm_mday;
2794         STHook->wHour = tmpTM.tm_hour;
2795         STHook->wMilliseconds = 0;
2796         STHook->wMinute = tmpTM.tm_min;
2797         STHook->wDayOfWeek = tmpTM.tm_wday;
2798         STHook->wMonth = tmpTM.tm_mon + 1;
2799         STHook->wSecond = tmpTM.tm_sec;
2800         STHook->wYear = tmpTM.tm_year;
2801         bSuccess = TRUE;
2802         
2803         TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2804               STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2805               STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2806     }
2807     else if (lphttpHdr->lpszValue)
2808     {
2809         DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
2810 
2811         if (len > *lpdwBufferLength)
2812         {
2813             *lpdwBufferLength = len;
2814             INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2815             return bSuccess;
2816         }
2817         if (lpBuffer)
2818         {
2819             memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2820             TRACE(" returning string: %s\n", debugstr_w(lpBuffer));
2821         }
2822         *lpdwBufferLength = len - sizeof(WCHAR);
2823         bSuccess = TRUE;
2824     }
2825     return bSuccess;
2826 }
2827 
2828 /***********************************************************************
2829  *           HttpQueryInfoW (WININET.@)
2830  *
2831  * Queries for information about an HTTP request
2832  *
2833  * RETURNS
2834  *    TRUE  on success
2835  *    FALSE on failure
2836  *
2837  */
2838 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2839         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2840 {
2841     BOOL bSuccess = FALSE;
2842     http_request_t *lpwhr;
2843 
2844     if (TRACE_ON(wininet)) {
2845 #define FE(x) { x, #x }
2846         static const wininet_flag_info query_flags[] = {
2847             FE(HTTP_QUERY_MIME_VERSION),
2848             FE(HTTP_QUERY_CONTENT_TYPE),
2849             FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2850             FE(HTTP_QUERY_CONTENT_ID),
2851             FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2852             FE(HTTP_QUERY_CONTENT_LENGTH),
2853             FE(HTTP_QUERY_CONTENT_LANGUAGE),
2854             FE(HTTP_QUERY_ALLOW),
2855             FE(HTTP_QUERY_PUBLIC),
2856             FE(HTTP_QUERY_DATE),
2857             FE(HTTP_QUERY_EXPIRES),
2858             FE(HTTP_QUERY_LAST_MODIFIED),
2859             FE(HTTP_QUERY_MESSAGE_ID),
2860             FE(HTTP_QUERY_URI),
2861             FE(HTTP_QUERY_DERIVED_FROM),
2862             FE(HTTP_QUERY_COST),
2863             FE(HTTP_QUERY_LINK),
2864             FE(HTTP_QUERY_PRAGMA),
2865             FE(HTTP_QUERY_VERSION),
2866             FE(HTTP_QUERY_STATUS_CODE),
2867             FE(HTTP_QUERY_STATUS_TEXT),
2868             FE(HTTP_QUERY_RAW_HEADERS),
2869             FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2870             FE(HTTP_QUERY_CONNECTION),
2871             FE(HTTP_QUERY_ACCEPT),
2872             FE(HTTP_QUERY_ACCEPT_CHARSET),
2873             FE(HTTP_QUERY_ACCEPT_ENCODING),
2874             FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2875             FE(HTTP_QUERY_AUTHORIZATION),
2876             FE(HTTP_QUERY_CONTENT_ENCODING),
2877             FE(HTTP_QUERY_FORWARDED),
2878             FE(HTTP_QUERY_FROM),
2879             FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2880             FE(HTTP_QUERY_LOCATION),
2881             FE(HTTP_QUERY_ORIG_URI),
2882             FE(HTTP_QUERY_REFERER),
2883             FE(HTTP_QUERY_RETRY_AFTER),
2884             FE(HTTP_QUERY_SERVER),
2885             FE(HTTP_QUERY_TITLE),
2886             FE(HTTP_QUERY_USER_AGENT),
2887             FE(HTTP_QUERY_WWW_AUTHENTICATE),
2888             FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2889             FE(HTTP_QUERY_ACCEPT_RANGES),
2890         FE(HTTP_QUERY_SET_COOKIE),
2891         FE(HTTP_QUERY_COOKIE),
2892             FE(HTTP_QUERY_REQUEST_METHOD),
2893             FE(HTTP_QUERY_REFRESH),
2894             FE(HTTP_QUERY_CONTENT_DISPOSITION),
2895             FE(HTTP_QUERY_AGE),
2896             FE(HTTP_QUERY_CACHE_CONTROL),
2897             FE(HTTP_QUERY_CONTENT_BASE),
2898             FE(HTTP_QUERY_CONTENT_LOCATION),
2899             FE(HTTP_QUERY_CONTENT_MD5),
2900             FE(HTTP_QUERY_CONTENT_RANGE),
2901             FE(HTTP_QUERY_ETAG),
2902             FE(HTTP_QUERY_HOST),
2903             FE(HTTP_QUERY_IF_MATCH),
2904             FE(HTTP_QUERY_IF_NONE_MATCH),
2905             FE(HTTP_QUERY_IF_RANGE),
2906             FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2907             FE(HTTP_QUERY_MAX_FORWARDS),
2908             FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2909             FE(HTTP_QUERY_RANGE),
2910             FE(HTTP_QUERY_TRANSFER_ENCODING),
2911             FE(HTTP_QUERY_UPGRADE),
2912             FE(HTTP_QUERY_VARY),
2913             FE(HTTP_QUERY_VIA),
2914             FE(HTTP_QUERY_WARNING),
2915             FE(HTTP_QUERY_CUSTOM)
2916         };
2917         static const wininet_flag_info modifier_flags[] = {
2918             FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2919             FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2920             FE(HTTP_QUERY_FLAG_NUMBER),
2921             FE(HTTP_QUERY_FLAG_COALESCE)
2922         };
2923 #undef FE
2924         DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2925         DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
2926         DWORD i;
2927 
2928         TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
2929         TRACE("  Attribute:");
2930         for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2931             if (query_flags[i].val == info) {
2932                 TRACE(" %s", query_flags[i].name);
2933                 break;
2934             }
2935         }
2936         if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
2937             TRACE(" Unknown (%08x)", info);
2938         }
2939 
2940         TRACE(" Modifier:");
2941         for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2942             if (modifier_flags[i].val & info_mod) {
2943                 TRACE(" %s", modifier_flags[i].name);
2944                 info_mod &= ~ modifier_flags[i].val;
2945             }
2946         }
2947         
2948         if (info_mod) {
2949             TRACE(" Unknown (%08x)", info_mod);
2950         }
2951         TRACE("\n");
2952     }
2953     
2954     lpwhr = (http_request_t*) WININET_GetObject( hHttpRequest );
2955     if (NULL == lpwhr ||  lpwhr->hdr.htype != WH_HHTTPREQ)
2956     {
2957         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2958         goto lend;
2959     }
2960 
2961     if (lpBuffer == NULL)
2962         *lpdwBufferLength = 0;
2963     bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2964                                     lpBuffer, lpdwBufferLength, lpdwIndex);
2965 
2966 lend:
2967     if( lpwhr )
2968          WININET_Release( &lpwhr->hdr );
2969 
2970     TRACE("%d <--\n", bSuccess);
2971     return bSuccess;
2972 }
2973 
2974 /***********************************************************************
2975  *           HttpQueryInfoA (WININET.@)
2976  *
2977  * Queries for information about an HTTP request
2978  *
2979  * RETURNS
2980  *    TRUE  on success
2981  *    FALSE on failure
2982  *
2983  */
2984 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2985         LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2986 {
2987     BOOL result;
2988     DWORD len;
2989     WCHAR* bufferW;
2990 
2991     if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2992        (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2993     {
2994         return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2995                                lpdwBufferLength, lpdwIndex );
2996     }
2997 
2998     if (lpBuffer)
2999     {
3000         DWORD alloclen;
3001         len = (*lpdwBufferLength)*sizeof(WCHAR);
3002         if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
3003         {
3004             alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
3005             if (alloclen < len)
3006                 alloclen = len;
3007         }
3008         else
3009             alloclen = len;
3010         bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
3011         /* buffer is in/out because of HTTP_QUERY_CUSTOM */
3012         if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
3013             MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
3014     } else
3015     {
3016         bufferW = NULL;
3017         len = 0;
3018     }
3019 
3020     result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
3021                            &len, lpdwIndex );
3022     if( result )
3023     {
3024         len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
3025                                      lpBuffer, *lpdwBufferLength, NULL, NULL );
3026         *lpdwBufferLength = len - 1;
3027 
3028         TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
3029     }
3030     else
3031         /* since the strings being returned from HttpQueryInfoW should be
3032          * only ASCII characters, it is reasonable to assume that all of
3033          * the Unicode characters can be reduced to a single byte */
3034         *lpdwBufferLength = len / sizeof(WCHAR);
3035 
3036     HeapFree(GetProcessHeap(), 0, bufferW );
3037 
3038     return result;
3039 }
3040 
3041 /***********************************************************************
3042  *           HttpSendRequestExA (WININET.@)
3043  *
3044  * Sends the specified request to the HTTP server and allows chunked
3045  * transfers.
3046  *
3047  * RETURNS
3048  *  Success: TRUE
3049  *  Failure: FALSE, call GetLastError() for more information.
3050  */
3051 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
3052                                LPINTERNET_BUFFERSA lpBuffersIn,
3053                                LPINTERNET_BUFFERSA lpBuffersOut,
3054                                DWORD dwFlags, DWORD_PTR dwContext)
3055 {
3056     INTERNET_BUFFERSW BuffersInW;
3057     BOOL rc = FALSE;
3058     DWORD headerlen;
3059     LPWSTR header = NULL;
3060 
3061     TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
3062             lpBuffersOut, dwFlags, dwContext);
3063 
3064     if (lpBuffersIn)
3065     {
3066         BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
3067         if (lpBuffersIn->lpcszHeader)
3068         {
3069             headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
3070                     lpBuffersIn->dwHeadersLength,0,0);
3071             header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
3072             if (!(BuffersInW.lpcszHeader = header))
3073             {
3074                 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3075                 return FALSE;
3076             }
3077             BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
3078                     lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
3079                     header, headerlen);
3080         }
3081         else
3082             BuffersInW.lpcszHeader = NULL;
3083         BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
3084         BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
3085         BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
3086         BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
3087         BuffersInW.Next = NULL;
3088     }
3089 
3090     rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
3091 
3092     HeapFree(GetProcessHeap(),0,header);
3093 
3094     return rc;
3095 }
3096 
3097 /***********************************************************************
3098  *           HttpSendRequestExW (WININET.@)
3099  *
3100  * Sends the specified request to the HTTP server and allows chunked
3101  * transfers
3102  *
3103  * RETURNS
3104  *  Success: TRUE
3105  *  Failure: FALSE, call GetLastError() for more information.
3106  */
3107 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
3108                    LPINTERNET_BUFFERSW lpBuffersIn,
3109                    LPINTERNET_BUFFERSW lpBuffersOut,
3110                    DWORD dwFlags, DWORD_PTR dwContext)
3111 {
3112     BOOL ret = FALSE;
3113     http_request_t *lpwhr;
3114     http_session_t *lpwhs;
3115     appinfo_t *hIC;
3116 
3117     TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
3118             lpBuffersOut, dwFlags, dwContext);
3119 
3120     lpwhr = (http_request_t*) WININET_GetObject( hRequest );
3121 
3122     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3123     {
3124         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3125         goto lend;
3126     }
3127 
3128     lpwhs = lpwhr->lpHttpSession;
3129     assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
3130     hIC = lpwhs->lpAppInfo;
3131     assert(hIC->hdr.htype == WH_HINIT);
3132 
3133     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3134     {
3135         WORKREQUEST workRequest;
3136         struct WORKREQ_HTTPSENDREQUESTW *req;
3137 
3138         workRequest.asyncproc = AsyncHttpSendRequestProc;
3139         workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
3140         req = &workRequest.u.HttpSendRequestW;
3141         if (lpBuffersIn)
3142         {
3143             /* FIXME: this should use dwHeadersLength or may not be necessary at all */
3144             req->lpszHeader = heap_strdupW(lpBuffersIn->lpcszHeader);
3145             req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
3146             req->lpOptional = lpBuffersIn->lpvBuffer;
3147             req->dwOptionalLength = lpBuffersIn->dwBufferLength;
3148             req->dwContentLength = lpBuffersIn->dwBufferTotal;
3149         }
3150         else
3151         {
3152             req->lpszHeader = NULL;
3153             req->dwHeaderLength = 0;
3154             req->lpOptional = NULL;
3155             req->dwOptionalLength = 0;
3156             req->dwContentLength = 0;
3157         }
3158 
3159         req->bEndRequest = FALSE;
3160 
3161         INTERNET_AsyncCall(&workRequest);
3162         /*
3163          * This is from windows.
3164          */
3165         INTERNET_SetLastError(ERROR_IO_PENDING);
3166     }
3167     else
3168     {
3169         if (lpBuffersIn)
3170             ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
3171                                         lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
3172                                         lpBuffersIn->dwBufferTotal, FALSE);
3173         else
3174             ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
3175     }
3176 
3177 lend:
3178     if ( lpwhr )
3179         WININET_Release( &lpwhr->hdr );
3180 
3181     TRACE("<---\n");
3182     return ret;
3183 }
3184 
3185 /***********************************************************************
3186  *           HttpSendRequestW (WININET.@)
3187  *
3188  * Sends the specified request to the HTTP server
3189  *
3190  * RETURNS
3191  *    TRUE  on success
3192  *    FALSE on failure
3193  *
3194  */
3195 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
3196         DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
3197 {
3198     http_request_t *lpwhr;
3199     http_session_t *lpwhs = NULL;
3200     appinfo_t *hIC = NULL;
3201     BOOL r;
3202 
3203     TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
3204             debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
3205 
3206     lpwhr = (http_request_t*) WININET_GetObject( hHttpRequest );
3207     if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3208     {
3209         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3210         r = FALSE;
3211         goto lend;
3212     }
3213 
3214     lpwhs = lpwhr->lpHttpSession;
3215     if (NULL == lpwhs ||  lpwhs->hdr.htype != WH_HHTTPSESSION)
3216     {
3217         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3218         r = FALSE;
3219         goto lend;
3220     }
3221 
3222     hIC = lpwhs->lpAppInfo;
3223     if (NULL == hIC ||  hIC->hdr.htype != WH_HINIT)
3224     {
3225         INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
3226         r = FALSE;
3227         goto lend;
3228     }
3229 
3230     if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3231     {
3232         WORKREQUEST workRequest;
3233         struct WORKREQ_HTTPSENDREQUESTW *req;
3234 
3235         workRequest.asyncproc = AsyncHttpSendRequestProc;
3236         workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
3237         req = &workRequest.u.HttpSendRequestW;
3238         if (lpszHeaders)
3239         {
3240             DWORD size;
3241 
3242             if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
3243             else size = dwHeaderLength * sizeof(WCHAR);
3244 
3245             req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, size);
3246             memcpy(req->lpszHeader, lpszHeaders, size);
3247         }
3248         else
3249             req->lpszHeader = 0;
3250         req->dwHeaderLength = dwHeaderLength;
3251         req->lpOptional = lpOptional;
3252         req->dwOptionalLength = dwOptionalLength;
3253         req->dwContentLength = dwOptionalLength;
3254         req->bEndRequest = TRUE;
3255 
3256         INTERNET_AsyncCall(&workRequest);
3257         /*
3258          * This is from windows.
3259          */
3260         INTERNET_SetLastError(ERROR_IO_PENDING);
3261         r = FALSE;
3262     }
3263     else
3264     {
3265         r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
3266                 dwHeaderLength, lpOptional, dwOptionalLength,
3267                 dwOptionalLength, TRUE);
3268     }
3269 lend:
3270     if( lpwhr )
3271         WININET_Release( &lpwhr->hdr );
3272     return r;
3273 }
3274 
3275 /***********************************************************************
3276  *           HttpSendRequestA (WININET.@)
3277  *
3278  * Sends the specified request to the HTTP server
3279  *
3280  * RETURNS
3281  *    TRUE  on success
3282  *    FALSE on failure
3283  *
3284  */