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