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