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