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