blob: a197c43e5ede9212f3ae0c10994b4649a4c62dba [file] [log] [blame]
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00001/*
Ulrich Czekallac2757242000-06-11 20:04:44 +00002 * Wininet - Http Implementation
3 *
4 * Copyright 1999 Corel Corporation
Aric Stewartff9b9d42002-06-21 23:59:49 +00005 * Copyright 2002 CodeWeavers Inc.
David Hammerton852c7ae2003-06-20 23:26:56 +00006 * Copyright 2002 TransGaming Technologies Inc.
Francois Gougetad5ff7c2004-02-09 22:07:42 +00007 * Copyright 2004 Mike McCormack for CodeWeavers
Aric Stewartbe918f42005-11-21 15:17:55 +00008 * Copyright 2005 Aric Stewart for CodeWeavers
Rob Shearman272954b2007-01-04 18:23:17 +00009 * Copyright 2006 Robert Shearman for CodeWeavers
Ulrich Czekallac2757242000-06-11 20:04:44 +000010 *
11 * Ulrich Czekalla
David Hammerton852c7ae2003-06-20 23:26:56 +000012 * David Hammerton
Ulrich Czekallac2757242000-06-11 20:04:44 +000013 *
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000014 * 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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020026 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Ulrich Czekallac2757242000-06-11 20:04:44 +000027 */
28
Patrik Stridvall4710be22000-06-23 15:47:14 +000029#include "config.h"
Alexandre Julliard754e7aa2004-09-03 18:30:28 +000030#include "wine/port.h"
Patrik Stridvall4710be22000-06-23 15:47:14 +000031
Ulrich Czekallac2757242000-06-11 20:04:44 +000032#include <sys/types.h>
Patrik Stridvall4710be22000-06-23 15:47:14 +000033#ifdef HAVE_SYS_SOCKET_H
34# include <sys/socket.h>
35#endif
Jacek Cabanad023172006-01-05 14:37:06 +010036#ifdef HAVE_ARPA_INET_H
37# include <arpa/inet.h>
38#endif
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000039#include <stdarg.h>
Ulrich Czekallac2757242000-06-11 20:04:44 +000040#include <stdio.h>
41#include <stdlib.h>
Patrik Stridvalld016f812002-08-17 00:43:16 +000042#ifdef HAVE_UNISTD_H
43# include <unistd.h>
44#endif
Chris Morganb9807b42001-02-15 21:24:07 +000045#include <time.h>
Mike McCormack3a1391b2004-07-19 21:49:39 +000046#include <assert.h>
Ulrich Czekallac2757242000-06-11 20:04:44 +000047
Guy Albertelliaafec982001-11-06 22:31:19 +000048#include "windef.h"
49#include "winbase.h"
50#include "wininet.h"
Guy Albertelliaafec982001-11-06 22:31:19 +000051#include "winerror.h"
Jon Griffiths603f20f2001-12-11 00:30:17 +000052#define NO_SHLWAPI_STREAM
Robert Shearmandc5f1cb2005-11-30 12:01:50 +010053#define NO_SHLWAPI_REG
54#define NO_SHLWAPI_STRFCNS
55#define NO_SHLWAPI_GDI
Guy Albertelliaafec982001-11-06 22:31:19 +000056#include "shlwapi.h"
Rob Shearman4b507682007-05-21 14:26:26 +010057#include "sspi.h"
Jacek Caban7e63f952008-03-12 02:24:23 +010058#include "wincrypt.h"
Guy Albertelliaafec982001-11-06 22:31:19 +000059
Ulrich Czekallac2757242000-06-11 20:04:44 +000060#include "internet.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000061#include "wine/debug.h"
Alberto Massarid476a5a2002-11-12 02:13:04 +000062#include "wine/unicode.h"
Ulrich Czekallac2757242000-06-11 20:04:44 +000063
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000064WINE_DEFAULT_DEBUG_CHANNEL(wininet);
Ulrich Czekallac2757242000-06-11 20:04:44 +000065
Hans Leidekker3a577112008-03-24 21:31:53 +010066static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
Hans Leidekkerd0033db2008-02-17 20:41:42 +010067static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
Francois Gouget6a6c85c2004-04-19 20:12:14 +000068static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
69static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
70static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
Aric Stewart1e946d32005-12-13 17:07:41 +010071static const WCHAR szHost[] = { 'H','o','s','t',0 };
Rob Shearman4b507682007-05-21 14:26:26 +010072static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
Aric Stewart1e946d32005-12-13 17:07:41 +010073static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
74static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
Rob Shearman272954b2007-01-04 18:23:17 +000075static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
Jacek Cabanf9791342008-02-13 13:34:05 +010076static const WCHAR szGET[] = { 'G','E','T', 0 };
Mike McCormacka4e902c2004-03-30 04:36:09 +000077
Ulrich Czekallac2757242000-06-11 20:04:44 +000078#define MAXHOSTNAME 100
79#define MAX_FIELD_VALUE_LEN 256
80#define MAX_FIELD_LEN 256
81
Mike McCormacka4e902c2004-03-30 04:36:09 +000082#define HTTP_REFERER g_szReferer
83#define HTTP_ACCEPT g_szAccept
84#define HTTP_USERAGENT g_szUserAgent
Ulrich Czekallac2757242000-06-11 20:04:44 +000085
86#define HTTP_ADDHDR_FLAG_ADD 0x20000000
87#define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
88#define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
89#define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
90#define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
91#define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
92#define HTTP_ADDHDR_FLAG_REQ 0x02000000
93
Rob Shearman4b507682007-05-21 14:26:26 +010094#define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
95
96struct HttpAuthInfo
97{
98 LPWSTR scheme;
99 CredHandle cred;
100 CtxtHandle ctx;
101 TimeStamp exp;
102 ULONG attr;
Rob Shearman0be05ab2008-03-10 16:41:44 +0000103 ULONG max_token;
Rob Shearman4b507682007-05-21 14:26:26 +0100104 void *auth_data;
105 unsigned int auth_data_len;
106 BOOL finished; /* finished authenticating */
107};
Ulrich Czekallac2757242000-06-11 20:04:44 +0000108
Jacek Caban02708c62005-10-26 10:07:58 +0000109static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
Hans Leidekker09366012008-03-26 11:31:21 +0100110static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear);
Jacek Caban02708c62005-10-26 10:07:58 +0000111static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
Jacek Caban02708c62005-10-26 10:07:58 +0000112static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
113static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
Aric Stewart1e946d32005-12-13 17:07:41 +0100114static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
Jacek Caban02708c62005-10-26 10:07:58 +0000115static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
Aric Stewartbe918f42005-11-21 15:17:55 +0000116static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
Aric Stewart1fc760d2005-11-28 17:31:02 +0100117static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
118 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
119 lpdwIndex);
Rob Shearman4319ec62006-12-07 00:53:27 +0000120static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
Rob Shearman4b507682007-05-21 14:26:26 +0100121static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
Aric Stewartc8dfc022007-07-26 08:59:00 -0500122static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field);
Jacek Cabanca39a552008-03-02 19:55:04 +0100123static void HTTP_DrainContent(WININETHTTPREQW *req);
Aric Stewart1e946d32005-12-13 17:07:41 +0100124
125LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
126{
127 int HeaderIndex = 0;
128 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
129 if (HeaderIndex == -1)
130 return NULL;
131 else
132 return &req->pCustHeaders[HeaderIndex];
133}
Ulrich Czekallac2757242000-06-11 20:04:44 +0000134
135/***********************************************************************
Robert Shearmandee87512004-07-19 20:09:20 +0000136 * HTTP_Tokenize (internal)
137 *
138 * Tokenize a string, allocating memory for the tokens.
139 */
140static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
141{
142 LPWSTR * token_array;
143 int tokens = 0;
144 int i;
145 LPCWSTR next_token;
146
147 /* empty string has no tokens */
148 if (*string)
149 tokens++;
150 /* count tokens */
151 for (i = 0; string[i]; i++)
152 if (!strncmpW(string+i, token_string, strlenW(token_string)))
153 {
Mike McCormack13b6ce6d2004-08-09 18:54:23 +0000154 DWORD j;
Robert Shearmandee87512004-07-19 20:09:20 +0000155 tokens++;
156 /* we want to skip over separators, but not the null terminator */
157 for (j = 0; j < strlenW(token_string) - 1; j++)
158 if (!string[i+j])
159 break;
160 i += j;
161 }
162
163 /* add 1 for terminating NULL */
164 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
165 token_array[tokens] = NULL;
166 if (!tokens)
167 return token_array;
168 for (i = 0; i < tokens; i++)
169 {
170 int len;
171 next_token = strstrW(string, token_string);
172 if (!next_token) next_token = string+strlenW(string);
173 len = next_token - string;
174 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
175 memcpy(token_array[i], string, len*sizeof(WCHAR));
176 token_array[i][len] = '\0';
177 string = next_token+strlenW(token_string);
178 }
179 return token_array;
180}
181
182/***********************************************************************
183 * HTTP_FreeTokens (internal)
184 *
185 * Frees memory returned from HTTP_Tokenize.
186 */
187static void HTTP_FreeTokens(LPWSTR * token_array)
188{
189 int i;
190 for (i = 0; token_array[i]; i++)
191 HeapFree(GetProcessHeap(), 0, token_array[i]);
192 HeapFree(GetProcessHeap(), 0, token_array);
193}
194
Aric Stewartbe918f42005-11-21 15:17:55 +0000195/* **********************************************************************
196 *
197 * Helper functions for the HttpSendRequest(Ex) functions
198 *
199 */
Jacek Cabanc1568452006-12-25 21:33:36 +0100200static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
201{
202 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
203 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
204
205 TRACE("%p\n", lpwhr);
206
207 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
208 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
209 req->dwContentLength, req->bEndRequest);
210
211 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
212}
213
Aric Stewartbe918f42005-11-21 15:17:55 +0000214static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
215{
216 static const WCHAR szSlash[] = { '/',0 };
217 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
218
219 /* If we don't have a path we set it to root */
220 if (NULL == lpwhr->lpszPath)
221 lpwhr->lpszPath = WININET_strdupW(szSlash);
222 else /* remove \r and \n*/
223 {
224 int nLen = strlenW(lpwhr->lpszPath);
225 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
226 {
227 nLen--;
228 lpwhr->lpszPath[nLen]='\0';
229 }
230 /* Replace '\' with '/' */
231 while (nLen>0) {
232 nLen--;
233 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
234 }
235 }
236
237 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
Juan Langb210e3a2007-11-07 14:34:49 -0800238 lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
Aric Stewartbe918f42005-11-21 15:17:55 +0000239 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
240 {
241 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
242 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
243 *fixurl = '/';
244 strcpyW(fixurl + 1, lpwhr->lpszPath);
245 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
246 lpwhr->lpszPath = fixurl;
247 }
248}
249
Hans Leidekkerd0033db2008-02-17 20:41:42 +0100250static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
Aric Stewartbe918f42005-11-21 15:17:55 +0000251{
252 LPWSTR requestString;
253 DWORD len, n;
254 LPCWSTR *req;
Gerald Pfeifer3f1a20b2008-01-12 20:14:04 +0100255 UINT i;
Aric Stewartbe918f42005-11-21 15:17:55 +0000256 LPWSTR p;
257
258 static const WCHAR szSpace[] = { ' ',0 };
259 static const WCHAR szcrlf[] = {'\r','\n', 0};
260 static const WCHAR szColon[] = { ':',' ',0 };
261 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
262
263 /* allocate space for an array of all the string pointers to be added */
Hans Leidekkerd0033db2008-02-17 20:41:42 +0100264 len = (lpwhr->nCustHeaders)*4 + 10;
Aric Stewartbe918f42005-11-21 15:17:55 +0000265 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
266
Robert Shearman0e7c41e2005-11-28 11:55:16 +0100267 /* add the verb, path and HTTP version string */
Aric Stewartbe918f42005-11-21 15:17:55 +0000268 n = 0;
Robert Shearman0e7c41e2005-11-28 11:55:16 +0100269 req[n++] = verb;
Aric Stewartbe918f42005-11-21 15:17:55 +0000270 req[n++] = szSpace;
Robert Shearman0e7c41e2005-11-28 11:55:16 +0100271 req[n++] = path;
Hans Leidekkerd0033db2008-02-17 20:41:42 +0100272 req[n++] = szSpace;
273 req[n++] = version;
Aric Stewartbe918f42005-11-21 15:17:55 +0000274
Austin English0e4adae2008-01-04 13:37:14 -0600275 /* Append custom request headers */
Aric Stewartbe918f42005-11-21 15:17:55 +0000276 for (i = 0; i < lpwhr->nCustHeaders; i++)
277 {
278 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
279 {
280 req[n++] = szcrlf;
281 req[n++] = lpwhr->pCustHeaders[i].lpszField;
282 req[n++] = szColon;
283 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
284
285 TRACE("Adding custom header %s (%s)\n",
286 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
287 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
288 }
289 }
290
291 if( n >= len )
292 ERR("oops. buffer overrun\n");
293
294 req[n] = NULL;
295 requestString = HTTP_build_req( req, 4 );
296 HeapFree( GetProcessHeap(), 0, req );
297
298 /*
299 * Set (header) termination string for request
300 * Make sure there's exactly two new lines at the end of the request
301 */
302 p = &requestString[strlenW(requestString)-1];
303 while ( (*p == '\n') || (*p == '\r') )
304 p--;
305 strcpyW( p+1, sztwocrlf );
306
307 return requestString;
308}
309
Hans Leidekker97171132008-02-17 21:29:45 +0100310static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr )
Aric Stewartbe918f42005-11-21 15:17:55 +0000311{
Aric Stewart1e946d32005-12-13 17:07:41 +0100312 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
313 int HeaderIndex;
314 LPHTTPHEADERW setCookieHeader;
315
316 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
317 if (HeaderIndex == -1)
318 return;
319 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
Aric Stewartbe918f42005-11-21 15:17:55 +0000320
Robert Shearman225a9fa2005-11-28 11:24:41 +0100321 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
Aric Stewartbe918f42005-11-21 15:17:55 +0000322 {
Aric Stewartbe918f42005-11-21 15:17:55 +0000323 int nPosStart = 0, nPosEnd = 0, len;
324 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
325
Aric Stewartbe918f42005-11-21 15:17:55 +0000326 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
327 {
328 LPWSTR buf_cookie, cookie_name, cookie_data;
329 LPWSTR buf_url;
330 LPWSTR domain = NULL;
Aric Stewart1e946d32005-12-13 17:07:41 +0100331 LPHTTPHEADERW Host;
332
Aric Stewartbe918f42005-11-21 15:17:55 +0000333 int nEqualPos = 0;
334 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
335 setCookieHeader->lpszValue[nPosEnd] != '\0')
336 {
337 nPosEnd++;
338 }
339 if (setCookieHeader->lpszValue[nPosEnd] == ';')
340 {
341 /* fixme: not case sensitive, strcasestr is gnu only */
342 int nDomainPosEnd = 0;
343 int nDomainPosStart = 0, nDomainLength = 0;
344 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
345 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
346 if (lpszDomain)
347 { /* they have specified their own domain, lets use it */
348 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
349 lpszDomain[nDomainPosEnd] != '\0')
350 {
351 nDomainPosEnd++;
352 }
353 nDomainPosStart = strlenW(szDomain);
354 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
355 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
356 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
357 }
358 }
359 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
360 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
361 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
362 TRACE("%s\n", debugstr_w(buf_cookie));
363 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
364 {
365 nEqualPos++;
366 }
367 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
368 {
369 HeapFree(GetProcessHeap(), 0, buf_cookie);
370 break;
371 }
372
373 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
374 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
375 cookie_data = &buf_cookie[nEqualPos + 1];
376
Aric Stewart1e946d32005-12-13 17:07:41 +0100377 Host = HTTP_GetHeader(lpwhr,szHost);
378 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
Aric Stewartbe918f42005-11-21 15:17:55 +0000379 strlenW(lpwhr->lpszPath) + 9;
380 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
Aric Stewart1e946d32005-12-13 17:07:41 +0100381 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
Aric Stewartbe918f42005-11-21 15:17:55 +0000382 InternetSetCookieW(buf_url, cookie_name, cookie_data);
383
384 HeapFree(GetProcessHeap(), 0, buf_url);
385 HeapFree(GetProcessHeap(), 0, buf_cookie);
386 HeapFree(GetProcessHeap(), 0, cookie_name);
387 HeapFree(GetProcessHeap(), 0, domain);
388 nPosStart = nPosEnd;
389 }
390 }
391}
392
Rob Shearman4b507682007-05-21 14:26:26 +0100393static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
394{
395 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
396 return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
397 ((pszAuthValue[ARRAYSIZE(szBasic)] != ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
398}
399
Rob Shearmancb289692007-06-05 19:49:58 +0100400static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
401 struct HttpAuthInfo **ppAuthInfo,
402 LPWSTR domain_and_username, LPWSTR password )
Rob Shearman4b507682007-05-21 14:26:26 +0100403{
404 SECURITY_STATUS sec_status;
Rob Shearmancb289692007-06-05 19:49:58 +0100405 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
Rob Shearman7b948712007-05-26 08:49:12 +0100406 BOOL first = FALSE;
Rob Shearman4b507682007-05-21 14:26:26 +0100407
408 TRACE("%s\n", debugstr_w(pszAuthValue));
409
410 if (!pAuthInfo)
411 {
412 TimeStamp exp;
413
Rob Shearman7b948712007-05-26 08:49:12 +0100414 first = TRUE;
Rob Shearman4b507682007-05-21 14:26:26 +0100415 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
416 if (!pAuthInfo)
417 return FALSE;
418
Rob Shearman4b507682007-05-21 14:26:26 +0100419 SecInvalidateHandle(&pAuthInfo->cred);
420 SecInvalidateHandle(&pAuthInfo->ctx);
421 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
422 pAuthInfo->attr = 0;
423 pAuthInfo->auth_data = NULL;
424 pAuthInfo->auth_data_len = 0;
425 pAuthInfo->finished = FALSE;
426
Rob Shearman4d1b8b12007-05-25 12:06:11 +0100427 if (is_basic_auth_value(pszAuthValue))
428 {
429 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
430 pAuthInfo->scheme = WININET_strdupW(szBasic);
431 if (!pAuthInfo->scheme)
432 {
433 HeapFree(GetProcessHeap(), 0, pAuthInfo);
434 return FALSE;
435 }
436 }
437 else
Rob Shearman4b507682007-05-21 14:26:26 +0100438 {
Rob Shearman7631bdf2008-03-10 16:40:23 +0000439 PVOID pAuthData;
Rob Shearman4b507682007-05-21 14:26:26 +0100440 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
Rob Shearman4b507682007-05-21 14:26:26 +0100441
Rob Shearman4d1b8b12007-05-25 12:06:11 +0100442 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
443 if (!pAuthInfo->scheme)
444 {
445 HeapFree(GetProcessHeap(), 0, pAuthInfo);
446 return FALSE;
447 }
448
Rob Shearman7631bdf2008-03-10 16:40:23 +0000449 if (domain_and_username)
Rob Shearman4b507682007-05-21 14:26:26 +0100450 {
Rob Shearman7631bdf2008-03-10 16:40:23 +0000451 WCHAR *user = strchrW(domain_and_username, '\\');
452 WCHAR *domain = domain_and_username;
Rob Shearman4b507682007-05-21 14:26:26 +0100453
Rob Shearman7631bdf2008-03-10 16:40:23 +0000454 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
455
456 pAuthData = &nt_auth_identity;
457
458 if (user) user++;
459 else
460 {
461 user = domain_and_username;
462 domain = NULL;
463 }
464
465 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
466 nt_auth_identity.User = user;
467 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
468 nt_auth_identity.Domain = domain;
469 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
470 nt_auth_identity.Password = password;
471 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
472 }
473 else
474 /* use default credentials */
475 pAuthData = NULL;
Rob Shearman4b507682007-05-21 14:26:26 +0100476
477 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
478 SECPKG_CRED_OUTBOUND, NULL,
Rob Shearman7631bdf2008-03-10 16:40:23 +0000479 pAuthData, NULL,
Rob Shearman4b507682007-05-21 14:26:26 +0100480 NULL, &pAuthInfo->cred,
481 &exp);
Rob Shearman0be05ab2008-03-10 16:41:44 +0000482 if (sec_status == SEC_E_OK)
483 {
484 PSecPkgInfoW sec_pkg_info;
485 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
486 if (sec_status == SEC_E_OK)
487 {
488 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
489 FreeContextBuffer(sec_pkg_info);
490 }
491 }
Rob Shearman4b507682007-05-21 14:26:26 +0100492 if (sec_status != SEC_E_OK)
493 {
494 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
495 debugstr_w(pAuthInfo->scheme), sec_status);
496 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
497 HeapFree(GetProcessHeap(), 0, pAuthInfo);
498 return FALSE;
499 }
500 }
Rob Shearmancb289692007-06-05 19:49:58 +0100501 *ppAuthInfo = pAuthInfo;
Rob Shearman4b507682007-05-21 14:26:26 +0100502 }
503 else if (pAuthInfo->finished)
504 return FALSE;
505
506 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
507 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
508 {
509 ERR("authentication scheme changed from %s to %s\n",
510 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
511 return FALSE;
512 }
513
514 if (is_basic_auth_value(pszAuthValue))
515 {
Rob Shearman7631bdf2008-03-10 16:40:23 +0000516 int userlen;
517 int passlen;
Rob Shearman847cc512007-05-21 14:28:02 +0100518 char *auth_data;
519
520 TRACE("basic authentication\n");
521
Rob Shearman7631bdf2008-03-10 16:40:23 +0000522 /* we don't cache credentials for basic authentication, so we can't
523 * retrieve them if the application didn't pass us any credentials */
524 if (!domain_and_username) return FALSE;
525
526 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
527 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
528
Rob Shearman847cc512007-05-21 14:28:02 +0100529 /* length includes a nul terminator, which will be re-used for the ':' */
530 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
531 if (!auth_data)
532 return FALSE;
533
534 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
535 auth_data[userlen] = ':';
536 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
537
538 pAuthInfo->auth_data = auth_data;
539 pAuthInfo->auth_data_len = userlen + 1 + passlen;
540 pAuthInfo->finished = TRUE;
541
542 return TRUE;
Rob Shearman4b507682007-05-21 14:26:26 +0100543 }
544 else
545 {
546 LPCWSTR pszAuthData;
547 SecBufferDesc out_desc, in_desc;
548 SecBuffer out, in;
549 unsigned char *buffer;
550 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
551 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
552
553 in.BufferType = SECBUFFER_TOKEN;
554 in.cbBuffer = 0;
555 in.pvBuffer = NULL;
556
557 in_desc.ulVersion = 0;
558 in_desc.cBuffers = 1;
559 in_desc.pBuffers = &in;
560
561 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
562 if (*pszAuthData == ' ')
563 {
564 pszAuthData++;
565 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
566 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
567 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
568 }
569
Rob Shearman0be05ab2008-03-10 16:41:44 +0000570 buffer = HeapAlloc(GetProcessHeap(), 0, pAuthInfo->max_token);
Rob Shearman4b507682007-05-21 14:26:26 +0100571
572 out.BufferType = SECBUFFER_TOKEN;
Rob Shearman0be05ab2008-03-10 16:41:44 +0000573 out.cbBuffer = pAuthInfo->max_token;
Rob Shearman4b507682007-05-21 14:26:26 +0100574 out.pvBuffer = buffer;
575
576 out_desc.ulVersion = 0;
577 out_desc.cBuffers = 1;
578 out_desc.pBuffers = &out;
579
Rob Shearman7b948712007-05-26 08:49:12 +0100580 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
Rob Shearman5edcf3a2008-01-24 19:30:11 +0000581 first ? NULL : &pAuthInfo->ctx,
582 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
Rob Shearman4b507682007-05-21 14:26:26 +0100583 context_req, 0, SECURITY_NETWORK_DREP,
584 in.pvBuffer ? &in_desc : NULL,
585 0, &pAuthInfo->ctx, &out_desc,
586 &pAuthInfo->attr, &pAuthInfo->exp);
587 if (sec_status == SEC_E_OK)
588 {
589 pAuthInfo->finished = TRUE;
590 pAuthInfo->auth_data = out.pvBuffer;
591 pAuthInfo->auth_data_len = out.cbBuffer;
592 TRACE("sending last auth packet\n");
593 }
594 else if (sec_status == SEC_I_CONTINUE_NEEDED)
595 {
596 pAuthInfo->auth_data = out.pvBuffer;
597 pAuthInfo->auth_data_len = out.cbBuffer;
598 TRACE("sending next auth packet\n");
599 }
600 else
601 {
602 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
Rob Shearmandb5745c2008-03-14 18:09:20 +0000603 pAuthInfo->finished = TRUE;
Rob Shearman4b507682007-05-21 14:26:26 +0100604 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
605 return FALSE;
606 }
607 }
608
609 return TRUE;
610}
611
Robert Shearmandee87512004-07-19 20:09:20 +0000612/***********************************************************************
Mike McCormackb288f712004-06-14 17:57:26 +0000613 * HTTP_HttpAddRequestHeadersW (internal)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000614 */
Mike McCormackb288f712004-06-14 17:57:26 +0000615static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
Mike McCormacka4e902c2004-03-30 04:36:09 +0000616 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000617{
Mike McCormacka4e902c2004-03-30 04:36:09 +0000618 LPWSTR lpszStart;
619 LPWSTR lpszEnd;
620 LPWSTR buffer;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000621 BOOL bSuccess = FALSE;
Mike McCormack08c6c692004-08-10 23:41:35 +0000622 DWORD len;
David Hammerton852c7ae2003-06-20 23:26:56 +0000623
Francois Gougetabfa73b2008-02-19 00:18:02 +0100624 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
Mike McCormack08c6c692004-08-10 23:41:35 +0000625
Alexandre Julliard7c1925a02005-09-13 15:00:32 +0000626 if( dwHeaderLength == ~0U )
Mike McCormack08c6c692004-08-10 23:41:35 +0000627 len = strlenW(lpszHeader);
628 else
629 len = dwHeaderLength;
630 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
Peter Berg Larsene732fc02005-03-28 14:17:51 +0000631 lstrcpynW( buffer, lpszHeader, len + 1);
Mike McCormack08c6c692004-08-10 23:41:35 +0000632
Ulrich Czekallac2757242000-06-11 20:04:44 +0000633 lpszStart = buffer;
634
635 do
636 {
Robert Shearmanb72a6822004-09-23 22:53:50 +0000637 LPWSTR * pFieldAndValue;
638
Ulrich Czekallac2757242000-06-11 20:04:44 +0000639 lpszEnd = lpszStart;
640
641 while (*lpszEnd != '\0')
642 {
643 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
644 break;
645 lpszEnd++;
646 }
647
Robert Shearman4cd38b42004-07-13 23:34:28 +0000648 if (*lpszStart == '\0')
Ulrich Czekallac2757242000-06-11 20:04:44 +0000649 break;
650
Robert Shearman4cd38b42004-07-13 23:34:28 +0000651 if (*lpszEnd == '\r')
652 {
653 *lpszEnd = '\0';
654 lpszEnd += 2; /* Jump over \r\n */
655 }
Mike McCormacka4e902c2004-03-30 04:36:09 +0000656 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
Robert Shearmanb72a6822004-09-23 22:53:50 +0000657 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
658 if (pFieldAndValue)
659 {
Aric Stewartc8dfc022007-07-26 08:59:00 -0500660 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
661 if (bSuccess)
662 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
663 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
Robert Shearmanb72a6822004-09-23 22:53:50 +0000664 HTTP_FreeTokens(pFieldAndValue);
665 }
Ulrich Czekallac2757242000-06-11 20:04:44 +0000666
Robert Shearman4cd38b42004-07-13 23:34:28 +0000667 lpszStart = lpszEnd;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000668 } while (bSuccess);
669
670 HeapFree(GetProcessHeap(), 0, buffer);
Mike McCormackb288f712004-06-14 17:57:26 +0000671
672 return bSuccess;
673}
674
675/***********************************************************************
676 * HttpAddRequestHeadersW (WININET.@)
677 *
678 * Adds one or more HTTP header to the request handler
679 *
Francois Gougetabfa73b2008-02-19 00:18:02 +0100680 * NOTE
681 * On Windows if dwHeaderLength includes the trailing '\0', then
682 * HttpAddRequestHeadersW() adds it too. However this results in an
683 * invalid Http header which is rejected by some servers so we probably
684 * don't need to match Windows on that point.
685 *
Mike McCormackb288f712004-06-14 17:57:26 +0000686 * RETURNS
687 * TRUE on success
688 * FALSE on failure
689 *
690 */
691BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
692 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
693{
694 BOOL bSuccess = FALSE;
695 LPWININETHTTPREQW lpwhr;
696
Francois Gougetabfa73b2008-02-19 00:18:02 +0100697 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
Mike McCormackb288f712004-06-14 17:57:26 +0000698
699 if (!lpszHeader)
700 return TRUE;
701
702 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
703 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
704 {
705 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +0000706 goto lend;
Mike McCormackb288f712004-06-14 17:57:26 +0000707 }
708 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
Mike McCormack3a1391b2004-07-19 21:49:39 +0000709lend:
710 if( lpwhr )
711 WININET_Release( &lpwhr->hdr );
Mike McCormackb288f712004-06-14 17:57:26 +0000712
Ulrich Czekallac2757242000-06-11 20:04:44 +0000713 return bSuccess;
714}
715
Chris Morgana8b32162002-09-27 22:05:23 +0000716/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +0000717 * HttpAddRequestHeadersA (WININET.@)
718 *
719 * Adds one or more HTTP header to the request handler
720 *
721 * RETURNS
722 * TRUE on success
723 * FALSE on failure
724 *
725 */
726BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
727 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
728{
729 DWORD len;
730 LPWSTR hdr;
731 BOOL r;
732
Francois Gougetabfa73b2008-02-19 00:18:02 +0100733 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
Mike McCormacka4e902c2004-03-30 04:36:09 +0000734
735 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
736 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
737 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
Alexandre Julliard7c1925a02005-09-13 15:00:32 +0000738 if( dwHeaderLength != ~0U )
Mike McCormacka4e902c2004-03-30 04:36:09 +0000739 dwHeaderLength = len;
740
741 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
742
743 HeapFree( GetProcessHeap(), 0, hdr );
744
745 return r;
746}
747
748/***********************************************************************
Chris Morgana8b32162002-09-27 22:05:23 +0000749 * HttpEndRequestA (WININET.@)
750 *
751 * Ends an HTTP request that was started by HttpSendRequestEx
752 *
753 * RETURNS
754 * TRUE if successful
755 * FALSE on failure
756 *
757 */
Aric Stewartbe918f42005-11-21 15:17:55 +0000758BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
Francois Gougetd4337f22007-08-30 16:21:33 +0200759 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
Chris Morgana8b32162002-09-27 22:05:23 +0000760{
Aric Stewartbe918f42005-11-21 15:17:55 +0000761 LPINTERNET_BUFFERSA ptr;
762 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
763 BOOL rc = FALSE;
764
Francois Gougetd4337f22007-08-30 16:21:33 +0200765 TRACE("(%p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
Aric Stewartbe918f42005-11-21 15:17:55 +0000766 dwContext);
767
768 ptr = lpBuffersOut;
769 if (ptr)
770 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
771 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
772 else
773 lpBuffersOutW = NULL;
774
775 ptrW = lpBuffersOutW;
776 while (ptr)
777 {
778 if (ptr->lpvBuffer && ptr->dwBufferLength)
779 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
780 ptrW->dwBufferLength = ptr->dwBufferLength;
781 ptrW->dwBufferTotal= ptr->dwBufferTotal;
782
783 if (ptr->Next)
784 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
785 sizeof(INTERNET_BUFFERSW));
786
787 ptr = ptr->Next;
788 ptrW = ptrW->Next;
789 }
790
791 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
792
793 if (lpBuffersOutW)
794 {
795 ptrW = lpBuffersOutW;
796 while (ptrW)
797 {
798 LPINTERNET_BUFFERSW ptrW2;
799
800 FIXME("Do we need to translate info out of these buffer?\n");
801
Andrew Talbot45481db2008-02-04 22:07:57 +0000802 HeapFree(GetProcessHeap(),0,ptrW->lpvBuffer);
Aric Stewartbe918f42005-11-21 15:17:55 +0000803 ptrW2 = ptrW->Next;
804 HeapFree(GetProcessHeap(),0,ptrW);
805 ptrW = ptrW2;
806 }
807 }
808
809 return rc;
Chris Morgana8b32162002-09-27 22:05:23 +0000810}
811
812/***********************************************************************
813 * HttpEndRequestW (WININET.@)
814 *
815 * Ends an HTTP request that was started by HttpSendRequestEx
816 *
817 * RETURNS
818 * TRUE if successful
819 * FALSE on failure
820 *
821 */
Aric Stewartbe918f42005-11-21 15:17:55 +0000822BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
Francois Gougetd4337f22007-08-30 16:21:33 +0200823 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
Chris Morgana8b32162002-09-27 22:05:23 +0000824{
Aric Stewartbe918f42005-11-21 15:17:55 +0000825 BOOL rc = FALSE;
826 LPWININETHTTPREQW lpwhr;
827 INT responseLen;
Rob Shearmanac1b5272007-01-04 18:21:49 +0000828 DWORD dwBufferSize;
Aric Stewartbe918f42005-11-21 15:17:55 +0000829
830 TRACE("-->\n");
831 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
832
833 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
834 {
835 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Nigel Liang7d2ac202007-10-17 17:07:41 -0700836 if (lpwhr)
837 WININET_Release( &lpwhr->hdr );
Aric Stewartbe918f42005-11-21 15:17:55 +0000838 return FALSE;
839 }
840
841 lpwhr->hdr.dwFlags |= dwFlags;
842 lpwhr->hdr.dwContext = dwContext;
843
Rob Shearmanac1b5272007-01-04 18:21:49 +0000844 /* We appear to do nothing with lpBuffersOut.. is that correct? */
845
Aric Stewartbe918f42005-11-21 15:17:55 +0000846 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
847 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
848
Hans Leidekker09366012008-03-26 11:31:21 +0100849 responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
Aric Stewartbe918f42005-11-21 15:17:55 +0000850 if (responseLen)
851 rc = TRUE;
852
853 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
854 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
855
Hans Leidekker97171132008-02-17 21:29:45 +0100856 /* process cookies here. Is this right? */
857 HTTP_ProcessCookies(lpwhr);
Aric Stewartbe918f42005-11-21 15:17:55 +0000858
Rob Shearmanac1b5272007-01-04 18:21:49 +0000859 dwBufferSize = sizeof(lpwhr->dwContentLength);
860 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
861 &lpwhr->dwContentLength,&dwBufferSize,NULL))
862 lpwhr->dwContentLength = -1;
Aric Stewartbe918f42005-11-21 15:17:55 +0000863
Rob Shearman272954b2007-01-04 18:23:17 +0000864 if (lpwhr->dwContentLength == 0)
865 HTTP_FinishedReading(lpwhr);
866
Aric Stewart1fc760d2005-11-28 17:31:02 +0100867 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
868 {
Rob Shearmanac1b5272007-01-04 18:21:49 +0000869 DWORD dwCode,dwCodeLength=sizeof(DWORD);
870 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
Aric Stewart1fc760d2005-11-28 17:31:02 +0100871 (dwCode==302 || dwCode==301))
872 {
873 WCHAR szNewLocation[2048];
Rob Shearman9efe0832007-01-12 19:19:18 -0600874 dwBufferSize=sizeof(szNewLocation);
Rob Shearmanac1b5272007-01-04 18:21:49 +0000875 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
Aric Stewart1fc760d2005-11-28 17:31:02 +0100876 {
Aric Stewart1fc760d2005-11-28 17:31:02 +0100877 /* redirects are always GETs */
878 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
Jacek Cabanf9791342008-02-13 13:34:05 +0100879 lpwhr->lpszVerb = WININET_strdupW(szGET);
Rob Shearman272954b2007-01-04 18:23:17 +0000880 HTTP_DrainContent(lpwhr);
Rob Shearman4319ec62006-12-07 00:53:27 +0000881 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
882 if (rc)
883 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
Aric Stewart1fc760d2005-11-28 17:31:02 +0100884 }
885 }
886 }
887
Nigel Liang7d2ac202007-10-17 17:07:41 -0700888 WININET_Release( &lpwhr->hdr );
Aric Stewartbe918f42005-11-21 15:17:55 +0000889 TRACE("%i <--\n",rc);
890 return rc;
Chris Morgana8b32162002-09-27 22:05:23 +0000891}
Ulrich Czekallac2757242000-06-11 20:04:44 +0000892
893/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +0000894 * HttpOpenRequestW (WININET.@)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000895 *
896 * Open a HTTP request handle
897 *
898 * RETURNS
899 * HINTERNET a HTTP request handle on success
900 * NULL on failure
901 *
902 */
Mike McCormacka4e902c2004-03-30 04:36:09 +0000903HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
904 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
905 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
Francois Gougetd4337f22007-08-30 16:21:33 +0200906 DWORD dwFlags, DWORD_PTR dwContext)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000907{
Mike McCormacka4e902c2004-03-30 04:36:09 +0000908 LPWININETHTTPSESSIONW lpwhs;
Mike McCormack7cc70c02004-02-07 01:03:41 +0000909 HINTERNET handle = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000910
Francois Gougetd4337f22007-08-30 16:21:33 +0200911 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
Mike McCormacka4e902c2004-03-30 04:36:09 +0000912 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
913 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
David Hammerton852c7ae2003-06-20 23:26:56 +0000914 dwFlags, dwContext);
Alberto Massaribc8bd722002-12-06 23:20:31 +0000915 if(lpszAcceptTypes!=NULL)
916 {
917 int i;
918 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
Mike McCormacka4e902c2004-03-30 04:36:09 +0000919 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
Alberto Massaribc8bd722002-12-06 23:20:31 +0000920 }
Ulrich Czekallac2757242000-06-11 20:04:44 +0000921
Mike McCormacka4e902c2004-03-30 04:36:09 +0000922 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
Ulrich Czekallac2757242000-06-11 20:04:44 +0000923 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
924 {
925 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +0000926 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000927 }
Ulrich Czekallac2757242000-06-11 20:04:44 +0000928
Aric Stewartff9b9d42002-06-21 23:59:49 +0000929 /*
930 * My tests seem to show that the windows version does not
931 * become asynchronous until after this point. And anyhow
932 * if this call was asynchronous then how would you get the
933 * necessary HINTERNET pointer returned by this function.
934 *
Aric Stewartff9b9d42002-06-21 23:59:49 +0000935 */
Mike McCormack3a1391b2004-07-19 21:49:39 +0000936 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
937 lpszVersion, lpszReferrer, lpszAcceptTypes,
938 dwFlags, dwContext);
939lend:
940 if( lpwhs )
941 WININET_Release( &lpwhs->hdr );
Mike McCormack7cc70c02004-02-07 01:03:41 +0000942 TRACE("returning %p\n", handle);
943 return handle;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000944}
945
David Hammerton852c7ae2003-06-20 23:26:56 +0000946
Alberto Massarid476a5a2002-11-12 02:13:04 +0000947/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +0000948 * HttpOpenRequestA (WININET.@)
Alberto Massarid476a5a2002-11-12 02:13:04 +0000949 *
950 * Open a HTTP request handle
951 *
952 * RETURNS
953 * HINTERNET a HTTP request handle on success
954 * NULL on failure
955 *
956 */
Mike McCormacka4e902c2004-03-30 04:36:09 +0000957HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
958 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
959 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
Francois Gougetd4337f22007-08-30 16:21:33 +0200960 DWORD dwFlags, DWORD_PTR dwContext)
Alberto Massarid476a5a2002-11-12 02:13:04 +0000961{
Mike McCormacka4e902c2004-03-30 04:36:09 +0000962 LPWSTR szVerb = NULL, szObjectName = NULL;
963 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
David Hammerton852c7ae2003-06-20 23:26:56 +0000964 INT len;
965 INT acceptTypesCount;
966 HINTERNET rc = FALSE;
Francois Gougetd4337f22007-08-30 16:21:33 +0200967 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
Mike McCormacka4e902c2004-03-30 04:36:09 +0000968 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
969 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
David Hammerton852c7ae2003-06-20 23:26:56 +0000970 dwFlags, dwContext);
Alberto Massaribc8bd722002-12-06 23:20:31 +0000971
David Hammerton852c7ae2003-06-20 23:26:56 +0000972 if (lpszVerb)
973 {
Mike McCormacka4e902c2004-03-30 04:36:09 +0000974 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
975 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
Mike McCormack43629c92003-08-15 03:47:30 +0000976 if ( !szVerb )
David Hammerton852c7ae2003-06-20 23:26:56 +0000977 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +0000978 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
David Hammerton852c7ae2003-06-20 23:26:56 +0000979 }
980
981 if (lpszObjectName)
982 {
Mike McCormacka4e902c2004-03-30 04:36:09 +0000983 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
984 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
Mike McCormack43629c92003-08-15 03:47:30 +0000985 if ( !szObjectName )
David Hammerton852c7ae2003-06-20 23:26:56 +0000986 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +0000987 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
David Hammerton852c7ae2003-06-20 23:26:56 +0000988 }
989
990 if (lpszVersion)
991 {
Mike McCormacka4e902c2004-03-30 04:36:09 +0000992 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
993 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
Mike McCormack43629c92003-08-15 03:47:30 +0000994 if ( !szVersion )
David Hammerton852c7ae2003-06-20 23:26:56 +0000995 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +0000996 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
David Hammerton852c7ae2003-06-20 23:26:56 +0000997 }
998
999 if (lpszReferrer)
1000 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00001001 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
1002 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
Mike McCormack43629c92003-08-15 03:47:30 +00001003 if ( !szReferrer )
David Hammerton852c7ae2003-06-20 23:26:56 +00001004 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001005 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
David Hammerton852c7ae2003-06-20 23:26:56 +00001006 }
1007
1008 acceptTypesCount = 0;
1009 if (lpszAcceptTypes)
1010 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00001011 /* find out how many there are */
Hans Leidekker2024f682007-02-12 15:19:17 +01001012 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
Mike McCormacka4e902c2004-03-30 04:36:09 +00001013 acceptTypesCount++;
1014 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
David Hammerton852c7ae2003-06-20 23:26:56 +00001015 acceptTypesCount = 0;
Hans Leidekker2024f682007-02-12 15:19:17 +01001016 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
David Hammerton852c7ae2003-06-20 23:26:56 +00001017 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00001018 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1019 -1, NULL, 0 );
1020 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
Mike McCormack43629c92003-08-15 03:47:30 +00001021 if (!szAcceptTypes[acceptTypesCount] )
David Hammerton852c7ae2003-06-20 23:26:56 +00001022 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001023 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1024 -1, szAcceptTypes[acceptTypesCount], len );
David Hammerton852c7ae2003-06-20 23:26:56 +00001025 acceptTypesCount++;
1026 }
Mike McCormacka4e902c2004-03-30 04:36:09 +00001027 szAcceptTypes[acceptTypesCount] = NULL;
David Hammerton852c7ae2003-06-20 23:26:56 +00001028 }
1029 else szAcceptTypes = 0;
1030
Mike McCormacka4e902c2004-03-30 04:36:09 +00001031 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1032 szVersion, szReferrer,
1033 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
David Hammerton852c7ae2003-06-20 23:26:56 +00001034
1035end:
1036 if (szAcceptTypes)
1037 {
1038 acceptTypesCount = 0;
1039 while (szAcceptTypes[acceptTypesCount])
1040 {
1041 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1042 acceptTypesCount++;
1043 }
1044 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1045 }
Michael Stefaniuc7cb43c92004-12-21 14:42:35 +00001046 HeapFree(GetProcessHeap(), 0, szReferrer);
1047 HeapFree(GetProcessHeap(), 0, szVersion);
1048 HeapFree(GetProcessHeap(), 0, szObjectName);
1049 HeapFree(GetProcessHeap(), 0, szVerb);
David Hammerton852c7ae2003-06-20 23:26:56 +00001050
1051 return rc;
Alberto Massarid476a5a2002-11-12 02:13:04 +00001052}
Ulrich Czekallac2757242000-06-11 20:04:44 +00001053
1054/***********************************************************************
Rob Shearmana9ebc702007-01-12 19:17:20 -06001055 * HTTP_EncodeBase64
Mike McCormacka1c16d22003-07-22 03:17:52 +00001056 */
Rob Shearmana9ebc702007-01-12 19:17:20 -06001057static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001058{
1059 UINT n = 0, x;
Andrew Talbot46fc9c22007-02-24 21:55:12 +00001060 static const CHAR HTTP_Base64Enc[] =
Mike McCormacka1c16d22003-07-22 03:17:52 +00001061 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1062
Rob Shearmana9ebc702007-01-12 19:17:20 -06001063 while( len > 0 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001064 {
1065 /* first 6 bits, all from bin[0] */
1066 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1067 x = (bin[0] & 3) << 4;
1068
1069 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
Rob Shearmana9ebc702007-01-12 19:17:20 -06001070 if( len == 1 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001071 {
1072 base64[n++] = HTTP_Base64Enc[x];
1073 base64[n++] = '=';
1074 base64[n++] = '=';
1075 break;
1076 }
1077 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1078 x = ( bin[1] & 0x0f ) << 2;
1079
1080 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
Rob Shearmana9ebc702007-01-12 19:17:20 -06001081 if( len == 2 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001082 {
1083 base64[n++] = HTTP_Base64Enc[x];
1084 base64[n++] = '=';
1085 break;
1086 }
1087 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1088
1089 /* last 6 bits, all from bin [2] */
1090 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1091 bin += 3;
Rob Shearmana9ebc702007-01-12 19:17:20 -06001092 len -= 3;
Mike McCormacka1c16d22003-07-22 03:17:52 +00001093 }
1094 base64[n] = 0;
1095 return n;
1096}
1097
Rob Shearman4b507682007-05-21 14:26:26 +01001098#define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1099 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1100 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1101 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1102static const signed char HTTP_Base64Dec[256] =
1103{
1104 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1105 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1106 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1107 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1108 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1109 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1110 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1111 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1112 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1113 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1114 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1115 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1116 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1117 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1118 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1119 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1120 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1121 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1122 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1123 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1124 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1125 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1126 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1127 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1128 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1129 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1130};
1131#undef CH
1132
1133/***********************************************************************
1134 * HTTP_DecodeBase64
1135 */
1136static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1137{
1138 unsigned int n = 0;
1139
1140 while(*base64)
1141 {
1142 signed char in[4];
1143
Rob Shearmanf8f9dbb2008-02-15 10:06:32 +00001144 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
Rob Shearman4b507682007-05-21 14:26:26 +01001145 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
Rob Shearmanf8f9dbb2008-02-15 10:06:32 +00001146 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
Rob Shearman4b507682007-05-21 14:26:26 +01001147 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1148 {
1149 WARN("invalid base64: %s\n", debugstr_w(base64));
1150 return 0;
1151 }
1152 if (bin)
1153 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1154 n++;
1155
1156 if ((base64[2] == '=') && (base64[3] == '='))
1157 break;
1158 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1159 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1160 {
1161 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1162 return 0;
1163 }
1164 if (bin)
1165 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1166 n++;
1167
1168 if (base64[3] == '=')
1169 break;
1170 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1171 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1172 {
1173 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1174 return 0;
1175 }
1176 if (bin)
1177 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1178 n++;
1179
1180 base64 += 4;
1181 }
1182
1183 return n;
1184}
1185
Mike McCormacka1c16d22003-07-22 03:17:52 +00001186/***********************************************************************
Rob Shearmancb289692007-06-05 19:49:58 +01001187 * HTTP_InsertAuthorizationForHeader
Rob Shearman4b507682007-05-21 14:26:26 +01001188 *
1189 * Insert or delete the authorization field in the request header.
1190 */
Rob Shearmanebaa4d92008-03-12 13:27:46 +00001191static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
Rob Shearman4b507682007-05-21 14:26:26 +01001192{
Rob Shearmanebaa4d92008-03-12 13:27:46 +00001193 if (pAuthInfo)
Rob Shearman4b507682007-05-21 14:26:26 +01001194 {
1195 static const WCHAR wszSpace[] = {' ',0};
Rob Shearman01826e02007-11-27 14:19:50 +00001196 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
Rob Shearman4b507682007-05-21 14:26:26 +01001197 unsigned int len;
Rob Shearmanebaa4d92008-03-12 13:27:46 +00001198 WCHAR *authorization = NULL;
Rob Shearman4b507682007-05-21 14:26:26 +01001199
Rob Shearmanebaa4d92008-03-12 13:27:46 +00001200 if (pAuthInfo->auth_data_len)
Rob Shearman01826e02007-11-27 14:19:50 +00001201 {
Rob Shearmanebaa4d92008-03-12 13:27:46 +00001202 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1203 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1204 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1205 if (!authorization)
1206 return FALSE;
1207
1208 strcpyW(authorization, pAuthInfo->scheme);
1209 strcatW(authorization, wszSpace);
1210 HTTP_EncodeBase64(pAuthInfo->auth_data,
1211 pAuthInfo->auth_data_len,
1212 authorization+strlenW(authorization));
1213
1214 /* clear the data as it isn't valid now that it has been sent to the
1215 * server, unless it's Basic authentication which doesn't do
1216 * connection tracking */
1217 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1218 {
1219 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1220 pAuthInfo->auth_data = NULL;
1221 pAuthInfo->auth_data_len = 0;
1222 }
Rob Shearman01826e02007-11-27 14:19:50 +00001223 }
Rob Shearmanebaa4d92008-03-12 13:27:46 +00001224
1225 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1226
1227 HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1228
1229 HeapFree(GetProcessHeap(), 0, authorization);
Rob Shearman4b507682007-05-21 14:26:26 +01001230 }
Rob Shearman4b507682007-05-21 14:26:26 +01001231 return TRUE;
1232}
1233
Hans Leidekker0ffe9f52008-03-30 19:16:52 +01001234static WCHAR *HTTP_BuildProxyRequestUrl(WININETHTTPREQW *req)
1235{
1236 WCHAR new_location[2048], *url;
1237 DWORD size;
1238
1239 size = sizeof(new_location);
1240 if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_LOCATION, new_location, &size, NULL))
1241 {
1242 if (!(url = HeapAlloc( GetProcessHeap(), 0, size + sizeof(WCHAR) ))) return NULL;
1243 strcpyW( url, new_location );
1244 }
1245 else
1246 {
1247 static const WCHAR slash[] = { '/',0 };
1248 static const WCHAR format[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1249 WININETHTTPSESSIONW *session = req->lpHttpSession;
1250
1251 size = 15; /* "http://" + sizeof(port#) + ":/\0" */
1252 size += strlenW( session->lpszHostName ) + strlenW( req->lpszPath );
1253
1254 if (!(url = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
1255
1256 sprintfW( url, format, session->lpszHostName, session->nHostPort );
1257 if (req->lpszPath[0] != '/') strcatW( url, slash );
1258 strcatW( url, req->lpszPath );
1259 }
1260 TRACE("url=%s\n", debugstr_w(url));
1261 return url;
1262}
1263
Rob Shearman4b507682007-05-21 14:26:26 +01001264/***********************************************************************
Mike McCormacka1c16d22003-07-22 03:17:52 +00001265 * HTTP_DealWithProxy
1266 */
Mike McCormack09d2d472004-03-25 05:29:47 +00001267static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
Mike McCormacka4e902c2004-03-30 04:36:09 +00001268 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
Mike McCormacka1c16d22003-07-22 03:17:52 +00001269{
Mike McCormacka4e902c2004-03-30 04:36:09 +00001270 WCHAR buf[MAXHOSTNAME];
1271 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
Francois Gouget8fc374d2004-04-20 02:14:00 +00001272 WCHAR* url;
Hans Leidekker781f3f72006-10-13 15:43:54 +02001273 static WCHAR szNul[] = { 0 };
Mike McCormacka4e902c2004-03-30 04:36:09 +00001274 URL_COMPONENTSW UrlComponents;
Hans Leidekker0ffe9f52008-03-30 19:16:52 +01001275 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1276 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
Mike McCormacka4e902c2004-03-30 04:36:09 +00001277 int len;
Mike McCormacka1c16d22003-07-22 03:17:52 +00001278
1279 memset( &UrlComponents, 0, sizeof UrlComponents );
1280 UrlComponents.dwStructSize = sizeof UrlComponents;
1281 UrlComponents.lpszHostName = buf;
1282 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1283
Mike McCormacka4e902c2004-03-30 04:36:09 +00001284 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
Robert Shearmancc44f3a2005-11-28 11:31:11 +01001285 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
Hans Leidekker0ffe9f52008-03-30 19:16:52 +01001286 sprintfW(proxy, szFormat, hIC->lpszProxy);
Uwe Bonnes599c4522003-12-15 19:47:31 +00001287 else
Robert Shearmancc44f3a2005-11-28 11:31:11 +01001288 strcpyW(proxy, hIC->lpszProxy);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001289 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001290 return FALSE;
1291 if( UrlComponents.dwHostNameLength == 0 )
1292 return FALSE;
1293
1294 if( !lpwhr->lpszPath )
Hans Leidekker781f3f72006-10-13 15:43:54 +02001295 lpwhr->lpszPath = szNul;
Francois Gougetaab5e582007-01-18 11:40:15 +01001296 TRACE("server=%s path=%s\n",
Robert Shearman7bb34632005-11-28 10:40:42 +01001297 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
Mike McCormacka1c16d22003-07-22 03:17:52 +00001298 /* for constant 15 see above */
Robert Shearman7bb34632005-11-28 10:40:42 +01001299 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001300 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
Mike McCormacka1c16d22003-07-22 03:17:52 +00001301
1302 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1303 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1304
Robert Shearman7bb34632005-11-28 10:40:42 +01001305 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001306 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
Mike McCormacka1c16d22003-07-22 03:17:52 +00001307 lpwhs->nServerPort = UrlComponents.nPort;
1308
1309 return TRUE;
1310}
1311
Rob Shearman72575a02006-12-07 00:52:50 +00001312static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1313{
1314 char szaddr[32];
1315 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1316
1317 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1318 INTERNET_STATUS_RESOLVING_NAME,
1319 lpwhs->lpszServerName,
1320 strlenW(lpwhs->lpszServerName)+1);
1321
1322 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1323 &lpwhs->socketAddress))
1324 {
1325 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1326 return FALSE;
1327 }
1328
1329 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1330 szaddr, sizeof(szaddr));
1331 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1332 INTERNET_STATUS_NAME_RESOLVED,
1333 szaddr, strlen(szaddr)+1);
1334 return TRUE;
1335}
1336
Jacek Caban5a535d62008-02-26 20:20:41 +01001337
1338/***********************************************************************
1339 * HTTPREQ_Destroy (internal)
1340 *
1341 * Deallocate request handle
1342 *
1343 */
1344static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1345{
1346 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1347 DWORD i;
1348
1349 TRACE("\n");
1350
1351 if(lpwhr->hCacheFile)
1352 CloseHandle(lpwhr->hCacheFile);
1353
1354 if(lpwhr->lpszCacheFile) {
1355 DeleteFileW(lpwhr->lpszCacheFile); /* FIXME */
1356 HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1357 }
1358
1359 WININET_Release(&lpwhr->lpHttpSession->hdr);
1360
1361 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1362 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1363 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1364 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1365 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1366
1367 for (i = 0; i < lpwhr->nCustHeaders; i++)
1368 {
1369 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1370 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1371 }
1372
1373 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1374 HeapFree(GetProcessHeap(), 0, lpwhr);
1375}
1376
Jacek Caban7dc9bf62008-02-26 20:21:00 +01001377static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1378{
1379 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1380 LPWININETHTTPSESSIONW lpwhs = NULL;
1381 LPWININETAPPINFOW hIC = NULL;
1382
1383 TRACE("%p\n",lpwhr);
1384
1385 if (!NETCON_connected(&lpwhr->netConnection))
1386 return;
1387
1388 if (lpwhr->pAuthInfo)
1389 {
Rob Shearman8c6ac002008-03-10 16:39:40 +00001390 if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1391 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1392 if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1393 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
Jacek Caban7dc9bf62008-02-26 20:21:00 +01001394
1395 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1396 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1397 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1398 lpwhr->pAuthInfo = NULL;
1399 }
1400 if (lpwhr->pProxyAuthInfo)
1401 {
Rob Shearman8c6ac002008-03-10 16:39:40 +00001402 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1403 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1404 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1405 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
Jacek Caban7dc9bf62008-02-26 20:21:00 +01001406
1407 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1408 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1409 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1410 lpwhr->pProxyAuthInfo = NULL;
1411 }
1412
1413 lpwhs = lpwhr->lpHttpSession;
1414 hIC = lpwhs->lpAppInfo;
1415
1416 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1417 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1418
1419 NETCON_close(&lpwhr->netConnection);
1420
1421 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1422 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1423}
1424
Jacek Cabane2933c22008-03-12 02:23:20 +01001425static DWORD HTTPREQ_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1426{
Jacek Caban0e010d82008-03-12 02:23:48 +01001427 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1428
Jacek Cabane2933c22008-03-12 02:23:20 +01001429 switch(option) {
1430 case INTERNET_OPTION_HANDLE_TYPE:
1431 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1432
1433 if (*size < sizeof(ULONG))
1434 return ERROR_INSUFFICIENT_BUFFER;
1435
1436 *size = sizeof(DWORD);
1437 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1438 return ERROR_SUCCESS;
Jacek Caban0e010d82008-03-12 02:23:48 +01001439
1440 case INTERNET_OPTION_URL: {
1441 WCHAR url[INTERNET_MAX_URL_LENGTH];
1442 HTTPHEADERW *host;
1443 DWORD len;
1444
1445 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
1446 static const WCHAR hostW[] = {'H','o','s','t',0};
1447
1448 TRACE("INTERNET_OPTION_URL\n");
1449
1450 host = HTTP_GetHeader(req, hostW);
1451 sprintfW(url, formatW, host->lpszValue, req->lpszPath);
1452 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1453
1454 if(unicode) {
1455 len = (strlenW(url)+1) * sizeof(WCHAR);
1456 if(*size < len)
1457 return ERROR_INSUFFICIENT_BUFFER;
1458
1459 *size = len;
1460 strcpyW(buffer, url);
1461 return ERROR_SUCCESS;
1462 }else {
1463 len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1464 if(len > *size)
1465 return ERROR_INSUFFICIENT_BUFFER;
1466
1467 *size = len;
1468 return ERROR_SUCCESS;
1469 }
1470 }
Jacek Cabance6a2282008-03-12 02:24:06 +01001471
1472 case INTERNET_OPTION_DATAFILE_NAME: {
1473 DWORD req_size;
1474
1475 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1476
1477 if(!req->lpszCacheFile) {
1478 *size = 0;
1479 return ERROR_INTERNET_ITEM_NOT_FOUND;
1480 }
1481
1482 if(unicode) {
1483 req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1484 if(*size < req_size)
1485 return ERROR_INSUFFICIENT_BUFFER;
1486
1487 *size = req_size;
1488 memcpy(buffer, req->lpszCacheFile, *size);
1489 return ERROR_SUCCESS;
1490 }else {
1491 req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1492 if (req_size > *size)
1493 return ERROR_INSUFFICIENT_BUFFER;
1494
1495 *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1496 -1, buffer, *size, NULL, NULL);
1497 return ERROR_SUCCESS;
1498 }
1499 }
Jacek Caban7e63f952008-03-12 02:24:23 +01001500
1501 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1502 PCCERT_CONTEXT context;
1503
1504 if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1505 *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1506 return ERROR_INSUFFICIENT_BUFFER;
1507 }
1508
1509 context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1510 if(context) {
1511 INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1512 DWORD len;
1513
1514 memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1515 info->ftExpiry = context->pCertInfo->NotAfter;
1516 info->ftStart = context->pCertInfo->NotBefore;
1517 if(unicode) {
1518 len = CertNameToStrW(context->dwCertEncodingType,
1519 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1520 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1521 if(info->lpszSubjectInfo)
1522 CertNameToStrW(context->dwCertEncodingType,
1523 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1524 info->lpszSubjectInfo, len);
1525 len = CertNameToStrW(context->dwCertEncodingType,
1526 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1527 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1528 if (info->lpszIssuerInfo)
1529 CertNameToStrW(context->dwCertEncodingType,
1530 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1531 info->lpszIssuerInfo, len);
1532 }else {
1533 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1534
1535 len = CertNameToStrA(context->dwCertEncodingType,
1536 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1537 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1538 if(infoA->lpszSubjectInfo)
1539 CertNameToStrA(context->dwCertEncodingType,
1540 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1541 infoA->lpszSubjectInfo, len);
1542 len = CertNameToStrA(context->dwCertEncodingType,
1543 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1544 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1545 if(infoA->lpszIssuerInfo)
1546 CertNameToStrA(context->dwCertEncodingType,
1547 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1548 infoA->lpszIssuerInfo, len);
1549 }
1550
1551 /*
1552 * Contrary to MSDN, these do not appear to be set.
1553 * lpszProtocolName
1554 * lpszSignatureAlgName
1555 * lpszEncryptionAlgName
1556 * dwKeySize
1557 */
1558 CertFreeCertificateContext(context);
1559 return ERROR_SUCCESS;
1560 }
1561 }
Jacek Cabane2933c22008-03-12 02:23:20 +01001562 }
1563
1564 FIXME("Not implemented option %d\n", option);
1565 return ERROR_INTERNET_INVALID_OPTION;
1566}
1567
Jacek Caban0e33eee2008-02-26 20:22:02 +01001568static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1569{
1570 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1571
1572 switch(option) {
1573 case INTERNET_OPTION_SEND_TIMEOUT:
1574 case INTERNET_OPTION_RECEIVE_TIMEOUT:
1575 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1576
1577 if (size != sizeof(DWORD))
1578 return ERROR_INVALID_PARAMETER;
1579
1580 return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1581 *(DWORD*)buffer);
1582 }
1583
1584 return ERROR_INTERNET_INVALID_OPTION;
1585}
1586
Hans Leidekker058761f2008-03-26 22:22:04 +01001587static DWORD HTTP_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
Jacek Caban3b4ca692008-03-02 19:35:11 +01001588{
1589 int bytes_read;
1590
1591 if(!NETCON_recv(&req->netConnection, buffer, min(size, req->dwContentLength - req->dwContentRead),
1592 sync ? MSG_WAITALL : 0, &bytes_read)) {
1593 if(req->dwContentLength != -1 && req->dwContentRead != req->dwContentLength)
1594 ERR("not all data received %d/%d\n", req->dwContentRead, req->dwContentLength);
1595
Hans Leidekker09366012008-03-26 11:31:21 +01001596 /* always return success, even if the network layer returns an error */
Jacek Caban3b4ca692008-03-02 19:35:11 +01001597 *read = 0;
1598 HTTP_FinishedReading(req);
1599 return ERROR_SUCCESS;
1600 }
1601
1602 req->dwContentRead += bytes_read;
1603 *read = bytes_read;
1604
1605 if(req->lpszCacheFile) {
1606 BOOL res;
1607
1608 res = WriteFile(req->hCacheFile, buffer, bytes_read, NULL, NULL);
1609 if(!res)
1610 WARN("WriteFile failed: %u\n", GetLastError());
1611 }
1612
1613 if(!bytes_read && (req->dwContentRead == req->dwContentLength))
1614 HTTP_FinishedReading(req);
1615
1616 return ERROR_SUCCESS;
1617}
1618
Hans Leidekker09366012008-03-26 11:31:21 +01001619static DWORD get_chunk_size(const char *buffer)
1620{
1621 const char *p;
1622 DWORD size = 0;
1623
1624 for (p = buffer; *p; p++)
1625 {
1626 if (*p >= '0' && *p <= '9') size = size * 16 + *p - '0';
1627 else if (*p >= 'a' && *p <= 'f') size = size * 16 + *p - 'a' + 10;
1628 else if (*p >= 'A' && *p <= 'F') size = size * 16 + *p - 'A' + 10;
1629 else if (*p == ';') break;
1630 }
1631 return size;
1632}
1633
Hans Leidekker058761f2008-03-26 22:22:04 +01001634static DWORD HTTP_ReadChunked(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
Hans Leidekker09366012008-03-26 11:31:21 +01001635{
1636 char reply[MAX_REPLY_LEN], *p = buffer;
1637 DWORD buflen, to_write = size;
1638 size_t to_read;
1639 int bytes_read;
1640
1641 *read = 0;
1642 for (;;)
1643 {
1644 if (*read == size) break;
1645
1646 if (req->dwContentLength == ~0UL) /* new chunk */
1647 {
1648 buflen = sizeof(reply);
1649 if (!NETCON_getNextLine(&req->netConnection, reply, &buflen)) break;
1650
1651 if (!(req->dwContentLength = get_chunk_size(reply)))
1652 {
1653 /* zero sized chunk marks end of transfer; read any trailing headers and return */
1654 HTTP_GetResponseHeaders(req, FALSE);
1655 break;
1656 }
1657 }
1658 to_read = min(to_write, req->dwContentLength - req->dwContentRead);
1659
1660 if (!NETCON_recv(&req->netConnection, p, to_read, sync ? MSG_WAITALL : 0, &bytes_read))
1661 {
1662 if (bytes_read != to_read)
1663 ERR("Not all data received %d/%d\n", bytes_read, to_read);
1664
1665 /* always return success, even if the network layer returns an error */
1666 *read = 0;
1667 break;
1668 }
1669 if (!bytes_read) break;
1670
1671 req->dwContentRead += bytes_read;
1672 to_write -= bytes_read;
1673 *read += bytes_read;
1674
1675 if (req->lpszCacheFile)
1676 {
1677 if (!WriteFile(req->hCacheFile, p, bytes_read, NULL, NULL))
1678 WARN("WriteFile failed: %u\n", GetLastError());
1679 }
1680 p += bytes_read;
1681
1682 if (req->dwContentRead == req->dwContentLength) /* chunk complete */
1683 {
1684 req->dwContentRead = 0;
1685 req->dwContentLength = ~0UL;
1686
1687 buflen = sizeof(reply);
1688 if (!NETCON_getNextLine(&req->netConnection, reply, &buflen))
1689 {
1690 ERR("Malformed chunk\n");
1691 *read = 0;
1692 break;
1693 }
1694 }
1695 }
1696 if (!*read) HTTP_FinishedReading(req);
1697 return ERROR_SUCCESS;
1698}
1699
Hans Leidekker058761f2008-03-26 22:22:04 +01001700static DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
Jacek Caban3b4ca692008-03-02 19:35:11 +01001701{
Hans Leidekker058761f2008-03-26 22:22:04 +01001702 WCHAR encoding[20];
Hans Leidekker09366012008-03-26 11:31:21 +01001703 DWORD buflen = sizeof(encoding);
1704 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
Jacek Caban3b4ca692008-03-02 19:35:11 +01001705
Hans Leidekker09366012008-03-26 11:31:21 +01001706 if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_TRANSFER_ENCODING, encoding, &buflen, NULL) &&
1707 !strcmpiW(encoding, szChunked))
1708 {
Hans Leidekker058761f2008-03-26 22:22:04 +01001709 return HTTP_ReadChunked(req, buffer, size, read, sync);
Hans Leidekker09366012008-03-26 11:31:21 +01001710 }
1711 else
Hans Leidekker058761f2008-03-26 22:22:04 +01001712 return HTTP_Read(req, buffer, size, read, sync);
1713}
1714
1715static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1716{
1717 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1718 return HTTPREQ_Read(req, buffer, size, read, TRUE);
Jacek Caban3b4ca692008-03-02 19:35:11 +01001719}
1720
Jacek Caband597fd12008-03-03 18:07:20 +01001721static void HTTPREQ_AsyncReadFileExProc(WORKREQUEST *workRequest)
1722{
1723 struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
1724 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1725 INTERNET_ASYNC_RESULT iar;
1726 DWORD res;
1727
1728 TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
1729
1730 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1731 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1732
1733 iar.dwResult = res == ERROR_SUCCESS;
1734 iar.dwError = res;
1735
1736 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1737 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1738 sizeof(INTERNET_ASYNC_RESULT));
1739}
1740
1741static DWORD HTTPREQ_ReadFileExA(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSA *buffers,
1742 DWORD flags, DWORD_PTR context)
1743{
1744
1745 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1746 DWORD res;
1747
1748 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1749 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1750
1751 if (buffers->dwStructSize != sizeof(*buffers))
1752 return ERROR_INVALID_PARAMETER;
1753
1754 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1755
Dan Hipschman09295932008-03-27 13:52:24 -07001756 if (hdr->dwFlags & INTERNET_FLAG_ASYNC) {
Jacek Caband597fd12008-03-03 18:07:20 +01001757 DWORD available = 0;
1758
1759 NETCON_query_data_available(&req->netConnection, &available);
1760 if (!available)
1761 {
1762 WORKREQUEST workRequest;
1763
1764 workRequest.asyncproc = HTTPREQ_AsyncReadFileExProc;
1765 workRequest.hdr = WININET_AddRef(&req->hdr);
1766 workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
1767
1768 INTERNET_AsyncCall(&workRequest);
1769
1770 return ERROR_IO_PENDING;
1771 }
1772 }
1773
1774 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1775 !(flags & IRF_NO_WAIT));
1776
1777 if (res == ERROR_SUCCESS) {
1778 DWORD size = buffers->dwBufferLength;
1779 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1780 &size, sizeof(size));
1781 }
1782
1783 return res;
1784}
1785
Jacek Caban1ffcfbc2008-02-26 20:21:34 +01001786static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1787{
1788 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
1789
1790 return NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written);
1791}
1792
Jacek Caban33141842008-02-29 12:57:57 +01001793static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1794{
1795 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1796 INTERNET_ASYNC_RESULT iar;
1797 char buffer[4048];
1798
1799 TRACE("%p\n", workRequest->hdr);
1800
1801 iar.dwResult = NETCON_recv(&req->netConnection, buffer,
1802 min(sizeof(buffer), req->dwContentLength - req->dwContentRead),
1803 MSG_PEEK, (int *)&iar.dwError);
1804
1805 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1806 sizeof(INTERNET_ASYNC_RESULT));
1807}
1808
1809static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1810{
1811 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1812 BYTE buffer[4048];
1813 BOOL async;
1814
1815 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
1816
1817 if(!NETCON_query_data_available(&req->netConnection, available) || *available)
1818 return ERROR_SUCCESS;
1819
1820 /* Even if we are in async mode, we need to determine whether
1821 * there is actually more data available. We do this by trying
1822 * to peek only a single byte in async mode. */
1823 async = (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) != 0;
1824
1825 if (NETCON_recv(&req->netConnection, buffer,
1826 min(async ? 1 : sizeof(buffer), req->dwContentLength - req->dwContentRead),
1827 MSG_PEEK, (int *)available) && async && *available)
1828 {
1829 WORKREQUEST workRequest;
1830
1831 *available = 0;
1832 workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
1833 workRequest.hdr = WININET_AddRef( &req->hdr );
1834
1835 INTERNET_AsyncCall(&workRequest);
1836
1837 return ERROR_IO_PENDING;
1838 }
1839
1840 return ERROR_SUCCESS;
1841}
1842
Jacek Caban5a535d62008-02-26 20:20:41 +01001843static const HANDLEHEADERVtbl HTTPREQVtbl = {
Jacek Caban7dc9bf62008-02-26 20:21:00 +01001844 HTTPREQ_Destroy,
Jacek Caban1ffcfbc2008-02-26 20:21:34 +01001845 HTTPREQ_CloseConnection,
Jacek Cabane2933c22008-03-12 02:23:20 +01001846 HTTPREQ_QueryOption,
Jacek Caban0e33eee2008-02-26 20:22:02 +01001847 HTTPREQ_SetOption,
Jacek Caban3b4ca692008-03-02 19:35:11 +01001848 HTTPREQ_ReadFile,
Jacek Caband597fd12008-03-03 18:07:20 +01001849 HTTPREQ_ReadFileExA,
Jacek Caban8c45eec2008-02-27 18:55:09 +01001850 HTTPREQ_WriteFile,
Jacek Caban33141842008-02-29 12:57:57 +01001851 HTTPREQ_QueryDataAvailable,
Jacek Caban8c45eec2008-02-27 18:55:09 +01001852 NULL
Jacek Caban5a535d62008-02-26 20:20:41 +01001853};
1854
Mike McCormacka1c16d22003-07-22 03:17:52 +00001855/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +00001856 * HTTP_HttpOpenRequestW (internal)
Ulrich Czekallac2757242000-06-11 20:04:44 +00001857 *
1858 * Open a HTTP request handle
1859 *
1860 * RETURNS
1861 * HINTERNET a HTTP request handle on success
1862 * NULL on failure
1863 *
1864 */
Mike McCormack3a1391b2004-07-19 21:49:39 +00001865HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
Mike McCormacka4e902c2004-03-30 04:36:09 +00001866 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1867 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
Francois Gougetd4337f22007-08-30 16:21:33 +02001868 DWORD dwFlags, DWORD_PTR dwContext)
Ulrich Czekallac2757242000-06-11 20:04:44 +00001869{
Mike McCormack09d2d472004-03-25 05:29:47 +00001870 LPWININETAPPINFOW hIC = NULL;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001871 LPWININETHTTPREQW lpwhr;
1872 LPWSTR lpszCookies;
1873 LPWSTR lpszUrl = NULL;
David Hammerton852c7ae2003-06-20 23:26:56 +00001874 DWORD nCookieSize;
Mike McCormack3a1391b2004-07-19 21:49:39 +00001875 HINTERNET handle = NULL;
Francois Gouget6a6c85c2004-04-19 20:12:14 +00001876 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
Mike McCormacka4e902c2004-03-30 04:36:09 +00001877 DWORD len;
Aric Stewart1e946d32005-12-13 17:07:41 +01001878 LPHTTPHEADERW Host;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001879
Francois Gouget0edbaf72005-11-10 12:14:56 +00001880 TRACE("-->\n");
Ulrich Czekallac2757242000-06-11 20:04:44 +00001881
Mike McCormack3a1391b2004-07-19 21:49:39 +00001882 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
Jacek Cabana541a442006-10-29 18:49:39 +01001883 hIC = lpwhs->lpAppInfo;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001884
Mike McCormacka4e902c2004-03-30 04:36:09 +00001885 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
Ulrich Czekallac2757242000-06-11 20:04:44 +00001886 if (NULL == lpwhr)
1887 {
1888 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Mike McCormack3a1391b2004-07-19 21:49:39 +00001889 goto lend;
Mike McCormack7cc70c02004-02-07 01:03:41 +00001890 }
Mike McCormack3a1391b2004-07-19 21:49:39 +00001891 lpwhr->hdr.htype = WH_HHTTPREQ;
Jacek Caban5a535d62008-02-26 20:20:41 +01001892 lpwhr->hdr.vtbl = &HTTPREQVtbl;
Mike McCormack3a1391b2004-07-19 21:49:39 +00001893 lpwhr->hdr.dwFlags = dwFlags;
1894 lpwhr->hdr.dwContext = dwContext;
Rob Shearmanbdc81d92008-03-10 16:39:15 +00001895 lpwhr->hdr.refs = 1;
Robert Shearmaned517f32004-09-20 19:10:31 +00001896 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
Jacek Caban5c53baa2006-10-30 22:14:59 +01001897 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
Mike McCormack3a1391b2004-07-19 21:49:39 +00001898
Jacek Cabana9bdc012006-10-29 18:50:25 +01001899 WININET_AddRef( &lpwhs->hdr );
1900 lpwhr->lpHttpSession = lpwhs;
Misha Koshelev728e5fa2007-09-20 20:59:40 -05001901 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
Jacek Cabana9bdc012006-10-29 18:50:25 +01001902
Mike McCormack7cc70c02004-02-07 01:03:41 +00001903 handle = WININET_AllocHandle( &lpwhr->hdr );
1904 if (NULL == handle)
1905 {
1906 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Mike McCormack3a1391b2004-07-19 21:49:39 +00001907 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001908 }
1909
Robert Shearman9981f332006-03-06 17:31:09 +00001910 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1911 {
1912 InternetCloseHandle( handle );
1913 handle = NULL;
1914 goto lend;
1915 }
Ulrich Czekallac2757242000-06-11 20:04:44 +00001916
Jacek Cabanf9791342008-02-13 13:34:05 +01001917 if (lpszObjectName && *lpszObjectName) {
Aric Stewartff9b9d42002-06-21 23:59:49 +00001918 HRESULT rc;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001919
1920 len = 0;
1921 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
Aric Stewartff9b9d42002-06-21 23:59:49 +00001922 if (rc != E_POINTER)
Mike McCormacka4e902c2004-03-30 04:36:09 +00001923 len = strlenW(lpszObjectName)+1;
1924 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1925 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
Huw D M Davies0aebee92001-01-21 21:09:00 +00001926 URL_ESCAPE_SPACES_ONLY);
Aric Stewartff9b9d42002-06-21 23:59:49 +00001927 if (rc)
1928 {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02001929 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001930 strcpyW(lpwhr->lpszPath,lpszObjectName);
Aric Stewartff9b9d42002-06-21 23:59:49 +00001931 }
Huw D M Davies0aebee92001-01-21 21:09:00 +00001932 }
Ulrich Czekallac2757242000-06-11 20:04:44 +00001933
Jacek Cabanf9791342008-02-13 13:34:05 +01001934 if (lpszReferrer && *lpszReferrer)
Hans Leidekker4d84eb22008-01-04 13:14:00 +01001935 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
Alberto Massaribc8bd722002-12-06 23:20:31 +00001936
Hans Leidekker2024f682007-02-12 15:19:17 +01001937 if (lpszAcceptTypes)
Alberto Massaribc8bd722002-12-06 23:20:31 +00001938 {
1939 int i;
Hans Leidekker2024f682007-02-12 15:19:17 +01001940 for (i = 0; lpszAcceptTypes[i]; i++)
1941 {
1942 if (!*lpszAcceptTypes[i]) continue;
Rob Shearmana9c2cfa2007-01-07 12:53:50 +00001943 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
Hans Leidekker2024f682007-02-12 15:19:17 +01001944 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
1945 HTTP_ADDHDR_FLAG_REQ |
1946 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
1947 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00001948 }
Ulrich Czekallac2757242000-06-11 20:04:44 +00001949
Jacek Cabanf9791342008-02-13 13:34:05 +01001950 lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001951
Hans Leidekkerd0033db2008-02-17 20:41:42 +01001952 if (lpszVersion)
1953 lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
1954 else
1955 lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
1956
Hans Leidekker4d84eb22008-01-04 13:14:00 +01001957 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
Robert Shearman37f2cc82004-09-13 19:33:17 +00001958
Mike McCormack403e58f2005-10-19 19:07:08 +00001959 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1960 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1961 INTERNET_DEFAULT_HTTPS_PORT :
1962 INTERNET_DEFAULT_HTTP_PORT);
Hans Leidekkere4c59c22008-03-30 19:17:31 +01001963
1964 if (lpwhs->nHostPort == INTERNET_INVALID_PORT_NUMBER)
1965 lpwhs->nHostPort = (dwFlags & INTERNET_FLAG_SECURE ?
1966 INTERNET_DEFAULT_HTTPS_PORT :
1967 INTERNET_DEFAULT_HTTP_PORT);
Mike McCormack403e58f2005-10-19 19:07:08 +00001968
Mike McCormacka1c16d22003-07-22 03:17:52 +00001969 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1970 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
Ulrich Czekallac2757242000-06-11 20:04:44 +00001971
David Hammerton852c7ae2003-06-20 23:26:56 +00001972 if (hIC->lpszAgent)
1973 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00001974 WCHAR *agent_header;
Francois Gouget6a6c85c2004-04-19 20:12:14 +00001975 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
Mike McCormack09d2d472004-03-25 05:29:47 +00001976
Mike McCormacka4e902c2004-03-30 04:36:09 +00001977 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1978 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1979 sprintfW(agent_header, user_agent, hIC->lpszAgent );
Mike McCormack09d2d472004-03-25 05:29:47 +00001980
Mike McCormackb288f712004-06-14 17:57:26 +00001981 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
David Hammerton852c7ae2003-06-20 23:26:56 +00001982 HTTP_ADDREQ_FLAG_ADD);
1983 HeapFree(GetProcessHeap(), 0, agent_header);
1984 }
1985
Aric Stewart1e946d32005-12-13 17:07:41 +01001986 Host = HTTP_GetHeader(lpwhr,szHost);
1987
1988 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001989 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
Aric Stewart1e946d32005-12-13 17:07:41 +01001990 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
Mike McCormacka4e902c2004-03-30 04:36:09 +00001991
Robert Shearmand6315922004-07-19 21:18:33 +00001992 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1993 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
David Hammerton852c7ae2003-06-20 23:26:56 +00001994 {
1995 int cnt = 0;
Francois Gouget6a6c85c2004-04-19 20:12:14 +00001996 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1997 static const WCHAR szcrlf[] = {'\r','\n',0};
David Hammerton852c7ae2003-06-20 23:26:56 +00001998
Mike McCormacka4e902c2004-03-30 04:36:09 +00001999 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
David Hammerton852c7ae2003-06-20 23:26:56 +00002000
Mike McCormacka4e902c2004-03-30 04:36:09 +00002001 cnt += sprintfW(lpszCookies, szCookie);
2002 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
2003 strcatW(lpszCookies, szcrlf);
David Hammerton852c7ae2003-06-20 23:26:56 +00002004
Mike McCormackb288f712004-06-14 17:57:26 +00002005 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
David Hammerton852c7ae2003-06-20 23:26:56 +00002006 HTTP_ADDREQ_FLAG_ADD);
2007 HeapFree(GetProcessHeap(), 0, lpszCookies);
2008 }
2009 HeapFree(GetProcessHeap(), 0, lpszUrl);
2010
2011
Robert Shearmande2666f2005-11-29 10:44:05 +01002012 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
2013 INTERNET_STATUS_HANDLE_CREATED, &handle,
2014 sizeof(handle));
Ulrich Czekallac2757242000-06-11 20:04:44 +00002015
Aric Stewartff9b9d42002-06-21 23:59:49 +00002016 /*
2017 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
2018 */
2019
Rob Shearman72575a02006-12-07 00:52:50 +00002020 if (!HTTP_ResolveName(lpwhr))
Ulrich Czekallac2757242000-06-11 20:04:44 +00002021 {
Mike McCormack3a1391b2004-07-19 21:49:39 +00002022 InternetCloseHandle( handle );
2023 handle = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002024 }
2025
Mike McCormack3a1391b2004-07-19 21:49:39 +00002026lend:
2027 if( lpwhr )
2028 WININET_Release( &lpwhr->hdr );
2029
2030 TRACE("<-- %p (%p)\n", handle, lpwhr);
Mike McCormack7cc70c02004-02-07 01:03:41 +00002031 return handle;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002032}
2033
Jacek Cabanca39a552008-03-02 19:55:04 +01002034/* read any content returned by the server so that the connection can be
2035 * reused */
2036static void HTTP_DrainContent(WININETHTTPREQW *req)
2037{
2038 DWORD bytes_read;
2039
2040 if (!NETCON_connected(&req->netConnection)) return;
2041
2042 if (req->dwContentLength == -1)
2043 NETCON_close(&req->netConnection);
2044
2045 do
2046 {
2047 char buffer[2048];
Hans Leidekker058761f2008-03-26 22:22:04 +01002048 if (HTTP_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
Jacek Cabanca39a552008-03-02 19:55:04 +01002049 return;
2050 } while (bytes_read);
2051}
2052
Aric Stewart1e946d32005-12-13 17:07:41 +01002053static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
2054static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2055static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2056static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2057static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2058static const WCHAR szAge[] = { 'A','g','e',0 };
2059static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
Aric Stewart1e946d32005-12-13 17:07:41 +01002060static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2061static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2062static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2063static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2064static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
2065static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2066static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2067static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2068static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2069static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2070static 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 };
2071static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2072static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
2073static const WCHAR szDate[] = { 'D','a','t','e',0 };
2074static const WCHAR szFrom[] = { 'F','r','o','m',0 };
2075static const WCHAR szETag[] = { 'E','T','a','g',0 };
2076static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
2077static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2078static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
2079static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2080static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2081static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
2082static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2083static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2084static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
2085static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2086static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2087static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
2088static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2089static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
2090static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
2091static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
2092static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
2093static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2094static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
2095static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2096static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2097static 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 };
2098static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
2099static const WCHAR szURI[] = { 'U','R','I',0 };
2100static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2101static const WCHAR szVary[] = { 'V','a','r','y',0 };
2102static const WCHAR szVia[] = { 'V','i','a',0 };
2103static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
2104static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2105
Mike McCormack7f5e2732006-03-30 18:02:54 +09002106static const LPCWSTR header_lookup[] = {
2107 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
2108 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
2109 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2110 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
2111 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2112 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
2113 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
2114 szAllow, /* HTTP_QUERY_ALLOW = 7 */
2115 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
2116 szDate, /* HTTP_QUERY_DATE = 9 */
2117 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
2118 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2119 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
2120 szURI, /* HTTP_QUERY_URI = 13 */
2121 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
2122 NULL, /* HTTP_QUERY_COST = 15 */
2123 NULL, /* HTTP_QUERY_LINK = 16 */
2124 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
2125 NULL, /* HTTP_QUERY_VERSION = 18 */
2126 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
2127 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
2128 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
2129 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2130 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
2131 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
2132 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2133 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2134 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2135 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
2136 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2137 NULL, /* HTTP_QUERY_FORWARDED = 30 */
2138 NULL, /* HTTP_QUERY_FROM = 31 */
2139 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2140 szLocation, /* HTTP_QUERY_LOCATION = 33 */
2141 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
2142 szReferer, /* HTTP_QUERY_REFERER = 35 */
2143 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
2144 szServer, /* HTTP_QUERY_SERVER = 37 */
2145 NULL, /* HTTP_TITLE = 38 */
2146 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
2147 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2148 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2149 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2150 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
2151 szCookie, /* HTTP_QUERY_COOKIE = 44 */
2152 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2153 NULL, /* HTTP_QUERY_REFRESH = 46 */
2154 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2155 szAge, /* HTTP_QUERY_AGE = 48 */
2156 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2157 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
2158 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2159 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2160 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2161 szETag, /* HTTP_QUERY_ETAG = 54 */
2162 szHost, /* HTTP_QUERY_HOST = 55 */
2163 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
2164 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2165 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
2166 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2167 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2168 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2169 szRange, /* HTTP_QUERY_RANGE = 62 */
2170 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2171 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
2172 szVary, /* HTTP_QUERY_VARY = 65 */
2173 szVia, /* HTTP_QUERY_VIA = 66 */
2174 szWarning, /* HTTP_QUERY_WARNING = 67 */
2175 szExpect, /* HTTP_QUERY_EXPECT = 68 */
2176 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2177 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
Aric Stewart1e946d32005-12-13 17:07:41 +01002178};
2179
Mike McCormack7f5e2732006-03-30 18:02:54 +09002180#define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2181
Alexandre Julliard48243e32004-07-15 18:57:32 +00002182/***********************************************************************
Mike McCormackb288f712004-06-14 17:57:26 +00002183 * HTTP_HttpQueryInfoW (internal)
2184 */
Mike McCormack9b95bb52005-06-13 19:05:42 +00002185static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
Mike McCormackb288f712004-06-14 17:57:26 +00002186 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2187{
2188 LPHTTPHEADERW lphttpHdr = NULL;
2189 BOOL bSuccess = FALSE;
Aric Stewart1e946d32005-12-13 17:07:41 +01002190 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
Mike McCormackae300882006-03-30 18:01:48 +09002191 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2192 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2193 INT index = -1;
Mike McCormackb288f712004-06-14 17:57:26 +00002194
2195 /* Find requested header structure */
Mike McCormackae300882006-03-30 18:01:48 +09002196 switch (level)
Mike McCormackb288f712004-06-14 17:57:26 +00002197 {
Mike McCormackae300882006-03-30 18:01:48 +09002198 case HTTP_QUERY_CUSTOM:
2199 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2200 break;
Mike McCormackb288f712004-06-14 17:57:26 +00002201
Mike McCormackae300882006-03-30 18:01:48 +09002202 case HTTP_QUERY_RAW_HEADERS_CRLF:
Robert Shearmandee87512004-07-19 20:09:20 +00002203 {
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07002204 LPWSTR headers;
2205 DWORD len;
2206 BOOL ret;
2207
2208 if (request_only)
Hans Leidekkerd0033db2008-02-17 20:41:42 +01002209 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07002210 else
2211 headers = lpwhr->lpszRawHeaders;
2212
2213 len = strlenW(headers);
Robert Shearmandee87512004-07-19 20:09:20 +00002214 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2215 {
2216 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2217 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07002218 ret = FALSE;
2219 } else
2220 {
2221 memcpy(lpBuffer, headers, (len+1)*sizeof(WCHAR));
2222 *lpdwBufferLength = len * sizeof(WCHAR);
2223
2224 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2225 ret = TRUE;
Robert Shearmandee87512004-07-19 20:09:20 +00002226 }
Robert Shearman4385d302004-07-21 21:17:03 +00002227
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07002228 if (request_only)
2229 HeapFree(GetProcessHeap(), 0, headers);
2230 return ret;
Robert Shearmandee87512004-07-19 20:09:20 +00002231 }
Mike McCormackae300882006-03-30 18:01:48 +09002232 case HTTP_QUERY_RAW_HEADERS:
Robert Shearmandee87512004-07-19 20:09:20 +00002233 {
2234 static const WCHAR szCrLf[] = {'\r','\n',0};
2235 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00002236 DWORD i, size = 0;
Robert Shearmandee87512004-07-19 20:09:20 +00002237 LPWSTR pszString = (WCHAR*)lpBuffer;
2238
2239 for (i = 0; ppszRawHeaderLines[i]; i++)
2240 size += strlenW(ppszRawHeaderLines[i]) + 1;
2241
2242 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2243 {
2244 HTTP_FreeTokens(ppszRawHeaderLines);
2245 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2246 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2247 return FALSE;
2248 }
2249
2250 for (i = 0; ppszRawHeaderLines[i]; i++)
2251 {
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00002252 DWORD len = strlenW(ppszRawHeaderLines[i]);
Robert Shearmandee87512004-07-19 20:09:20 +00002253 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2254 pszString += len+1;
2255 }
2256 *pszString = '\0';
2257
2258 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
2259
Robert Shearman4385d302004-07-21 21:17:03 +00002260 *lpdwBufferLength = size * sizeof(WCHAR);
Robert Shearmandee87512004-07-19 20:09:20 +00002261 HTTP_FreeTokens(ppszRawHeaderLines);
2262
2263 return TRUE;
2264 }
Mike McCormackae300882006-03-30 18:01:48 +09002265 case HTTP_QUERY_STATUS_TEXT:
Mike McCormack739cd1e2006-05-30 22:55:42 +09002266 if (lpwhr->lpszStatusText)
Aric Stewart1e946d32005-12-13 17:07:41 +01002267 {
2268 DWORD len = strlenW(lpwhr->lpszStatusText);
2269 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2270 {
2271 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2272 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2273 return FALSE;
2274 }
2275 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
2276 *lpdwBufferLength = len * sizeof(WCHAR);
2277
2278 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2279
2280 return TRUE;
2281 }
Mike McCormack739cd1e2006-05-30 22:55:42 +09002282 break;
Mike McCormackae300882006-03-30 18:01:48 +09002283 case HTTP_QUERY_VERSION:
Mike McCormack739cd1e2006-05-30 22:55:42 +09002284 if (lpwhr->lpszVersion)
Aric Stewart1e946d32005-12-13 17:07:41 +01002285 {
2286 DWORD len = strlenW(lpwhr->lpszVersion);
2287 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2288 {
2289 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2290 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2291 return FALSE;
2292 }
2293 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
2294 *lpdwBufferLength = len * sizeof(WCHAR);
2295
2296 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2297
2298 return TRUE;
2299 }
Mike McCormack739cd1e2006-05-30 22:55:42 +09002300 break;
Mike McCormackae300882006-03-30 18:01:48 +09002301 default:
Mike McCormack7f5e2732006-03-30 18:02:54 +09002302 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2303
2304 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
2305 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2306 requested_index,request_only);
Mike McCormackb288f712004-06-14 17:57:26 +00002307 }
2308
Mike McCormackae300882006-03-30 18:01:48 +09002309 if (index >= 0)
2310 lphttpHdr = &lpwhr->pCustHeaders[index];
2311
Austin English0e4adae2008-01-04 13:37:14 -06002312 /* Ensure header satisfies requested attributes */
Mike McCormackae300882006-03-30 18:01:48 +09002313 if (!lphttpHdr ||
2314 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2315 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
Alexandre Julliard48243e32004-07-15 18:57:32 +00002316 {
Francois Gougete66d1cd2007-01-09 12:30:45 +01002317 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
Mike McCormackae300882006-03-30 18:01:48 +09002318 return bSuccess;
Alexandre Julliard48243e32004-07-15 18:57:32 +00002319 }
Mike McCormackb288f712004-06-14 17:57:26 +00002320
Mike McCormackae300882006-03-30 18:01:48 +09002321 if (lpdwIndex)
2322 (*lpdwIndex)++;
2323
Austin English0e4adae2008-01-04 13:37:14 -06002324 /* coalesce value to requested type */
Mike McCormackb288f712004-06-14 17:57:26 +00002325 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
2326 {
2327 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2328 bSuccess = TRUE;
2329
2330 TRACE(" returning number : %d\n", *(int *)lpBuffer);
2331 }
2332 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
2333 {
2334 time_t tmpTime;
2335 struct tm tmpTM;
2336 SYSTEMTIME *STHook;
2337
2338 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2339
2340 tmpTM = *gmtime(&tmpTime);
2341 STHook = (SYSTEMTIME *) lpBuffer;
2342 if(STHook==NULL)
2343 return bSuccess;
2344
2345 STHook->wDay = tmpTM.tm_mday;
2346 STHook->wHour = tmpTM.tm_hour;
2347 STHook->wMilliseconds = 0;
2348 STHook->wMinute = tmpTM.tm_min;
2349 STHook->wDayOfWeek = tmpTM.tm_wday;
2350 STHook->wMonth = tmpTM.tm_mon + 1;
2351 STHook->wSecond = tmpTM.tm_sec;
2352 STHook->wYear = tmpTM.tm_year;
2353
2354 bSuccess = TRUE;
2355
2356 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2357 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2358 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2359 }
Aric Stewart7bca41a2005-12-08 12:44:45 +01002360 else if (lphttpHdr->lpszValue)
Mike McCormackb288f712004-06-14 17:57:26 +00002361 {
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00002362 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
Mike McCormackb288f712004-06-14 17:57:26 +00002363
2364 if (len > *lpdwBufferLength)
2365 {
2366 *lpdwBufferLength = len;
2367 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2368 return bSuccess;
2369 }
2370
2371 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2372 *lpdwBufferLength = len - sizeof(WCHAR);
2373 bSuccess = TRUE;
2374
Francois Gougetaab5e582007-01-18 11:40:15 +01002375 TRACE(" returning string : %s\n", debugstr_w(lpBuffer));
Mike McCormackb288f712004-06-14 17:57:26 +00002376 }
2377 return bSuccess;
2378}
2379
2380/***********************************************************************
Mike McCormack1baf39f2004-03-30 20:37:49 +00002381 * HttpQueryInfoW (WININET.@)
Ulrich Czekallac2757242000-06-11 20:04:44 +00002382 *
2383 * Queries for information about an HTTP request
2384 *
2385 * RETURNS
2386 * TRUE on success
2387 * FALSE on failure
2388 *
2389 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002390BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
Ulrich Czekallac2757242000-06-11 20:04:44 +00002391 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2392{
Ulrich Czekallac2757242000-06-11 20:04:44 +00002393 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002394 LPWININETHTTPREQW lpwhr;
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00002395
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002396 if (TRACE_ON(wininet)) {
2397#define FE(x) { x, #x }
2398 static const wininet_flag_info query_flags[] = {
2399 FE(HTTP_QUERY_MIME_VERSION),
2400 FE(HTTP_QUERY_CONTENT_TYPE),
2401 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2402 FE(HTTP_QUERY_CONTENT_ID),
2403 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2404 FE(HTTP_QUERY_CONTENT_LENGTH),
2405 FE(HTTP_QUERY_CONTENT_LANGUAGE),
2406 FE(HTTP_QUERY_ALLOW),
2407 FE(HTTP_QUERY_PUBLIC),
2408 FE(HTTP_QUERY_DATE),
2409 FE(HTTP_QUERY_EXPIRES),
2410 FE(HTTP_QUERY_LAST_MODIFIED),
2411 FE(HTTP_QUERY_MESSAGE_ID),
2412 FE(HTTP_QUERY_URI),
2413 FE(HTTP_QUERY_DERIVED_FROM),
2414 FE(HTTP_QUERY_COST),
2415 FE(HTTP_QUERY_LINK),
2416 FE(HTTP_QUERY_PRAGMA),
2417 FE(HTTP_QUERY_VERSION),
2418 FE(HTTP_QUERY_STATUS_CODE),
2419 FE(HTTP_QUERY_STATUS_TEXT),
2420 FE(HTTP_QUERY_RAW_HEADERS),
2421 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2422 FE(HTTP_QUERY_CONNECTION),
2423 FE(HTTP_QUERY_ACCEPT),
2424 FE(HTTP_QUERY_ACCEPT_CHARSET),
2425 FE(HTTP_QUERY_ACCEPT_ENCODING),
2426 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2427 FE(HTTP_QUERY_AUTHORIZATION),
2428 FE(HTTP_QUERY_CONTENT_ENCODING),
2429 FE(HTTP_QUERY_FORWARDED),
2430 FE(HTTP_QUERY_FROM),
2431 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2432 FE(HTTP_QUERY_LOCATION),
2433 FE(HTTP_QUERY_ORIG_URI),
2434 FE(HTTP_QUERY_REFERER),
2435 FE(HTTP_QUERY_RETRY_AFTER),
2436 FE(HTTP_QUERY_SERVER),
2437 FE(HTTP_QUERY_TITLE),
2438 FE(HTTP_QUERY_USER_AGENT),
2439 FE(HTTP_QUERY_WWW_AUTHENTICATE),
2440 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2441 FE(HTTP_QUERY_ACCEPT_RANGES),
Aric Stewart1e946d32005-12-13 17:07:41 +01002442 FE(HTTP_QUERY_SET_COOKIE),
2443 FE(HTTP_QUERY_COOKIE),
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002444 FE(HTTP_QUERY_REQUEST_METHOD),
2445 FE(HTTP_QUERY_REFRESH),
2446 FE(HTTP_QUERY_CONTENT_DISPOSITION),
2447 FE(HTTP_QUERY_AGE),
2448 FE(HTTP_QUERY_CACHE_CONTROL),
2449 FE(HTTP_QUERY_CONTENT_BASE),
2450 FE(HTTP_QUERY_CONTENT_LOCATION),
2451 FE(HTTP_QUERY_CONTENT_MD5),
2452 FE(HTTP_QUERY_CONTENT_RANGE),
2453 FE(HTTP_QUERY_ETAG),
2454 FE(HTTP_QUERY_HOST),
2455 FE(HTTP_QUERY_IF_MATCH),
2456 FE(HTTP_QUERY_IF_NONE_MATCH),
2457 FE(HTTP_QUERY_IF_RANGE),
2458 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2459 FE(HTTP_QUERY_MAX_FORWARDS),
2460 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2461 FE(HTTP_QUERY_RANGE),
2462 FE(HTTP_QUERY_TRANSFER_ENCODING),
2463 FE(HTTP_QUERY_UPGRADE),
2464 FE(HTTP_QUERY_VARY),
2465 FE(HTTP_QUERY_VIA),
2466 FE(HTTP_QUERY_WARNING),
2467 FE(HTTP_QUERY_CUSTOM)
2468 };
2469 static const wininet_flag_info modifier_flags[] = {
2470 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2471 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2472 FE(HTTP_QUERY_FLAG_NUMBER),
2473 FE(HTTP_QUERY_FLAG_COALESCE)
2474 };
2475#undef FE
2476 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2477 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00002478 DWORD i;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002479
Hans Leidekkercd2c4582006-10-05 13:18:56 +02002480 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002481 TRACE(" Attribute:");
2482 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2483 if (query_flags[i].val == info) {
Diego Pettenò869a66a2005-01-07 17:09:39 +00002484 TRACE(" %s", query_flags[i].name);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002485 break;
2486 }
2487 }
2488 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02002489 TRACE(" Unknown (%08x)", info);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002490 }
2491
Diego Pettenò869a66a2005-01-07 17:09:39 +00002492 TRACE(" Modifier:");
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002493 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2494 if (modifier_flags[i].val & info_mod) {
Diego Pettenò869a66a2005-01-07 17:09:39 +00002495 TRACE(" %s", modifier_flags[i].name);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002496 info_mod &= ~ modifier_flags[i].val;
2497 }
2498 }
2499
2500 if (info_mod) {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02002501 TRACE(" Unknown (%08x)", info_mod);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002502 }
Diego Pettenò869a66a2005-01-07 17:09:39 +00002503 TRACE("\n");
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002504 }
2505
Mike McCormacka4e902c2004-03-30 04:36:09 +00002506 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
Ulrich Czekallac2757242000-06-11 20:04:44 +00002507 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2508 {
2509 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002510 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002511 }
2512
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002513 if (lpBuffer == NULL)
2514 *lpdwBufferLength = 0;
Mike McCormackb288f712004-06-14 17:57:26 +00002515 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2516 lpBuffer, lpdwBufferLength, lpdwIndex);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002517
Mike McCormack3a1391b2004-07-19 21:49:39 +00002518lend:
2519 if( lpwhr )
2520 WININET_Release( &lpwhr->hdr );
2521
Ulrich Czekallac2757242000-06-11 20:04:44 +00002522 TRACE("%d <--\n", bSuccess);
2523 return bSuccess;
2524}
2525
Alberto Massarid476a5a2002-11-12 02:13:04 +00002526/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +00002527 * HttpQueryInfoA (WININET.@)
Alberto Massarid476a5a2002-11-12 02:13:04 +00002528 *
2529 * Queries for information about an HTTP request
2530 *
2531 * RETURNS
2532 * TRUE on success
2533 * FALSE on failure
2534 *
2535 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002536BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
Alberto Massarid476a5a2002-11-12 02:13:04 +00002537 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2538{
2539 BOOL result;
Mike McCormack1baf39f2004-03-30 20:37:49 +00002540 DWORD len;
2541 WCHAR* bufferW;
2542
Alberto Massarid476a5a2002-11-12 02:13:04 +00002543 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2544 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2545 {
Mike McCormack1baf39f2004-03-30 20:37:49 +00002546 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2547 lpdwBufferLength, lpdwIndex );
Alberto Massarid476a5a2002-11-12 02:13:04 +00002548 }
Mike McCormack1baf39f2004-03-30 20:37:49 +00002549
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002550 if (lpBuffer)
2551 {
Rob Shearman719cd822008-02-18 19:37:35 +00002552 DWORD alloclen;
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002553 len = (*lpdwBufferLength)*sizeof(WCHAR);
Rob Shearman719cd822008-02-18 19:37:35 +00002554 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2555 {
2556 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2557 if (alloclen < len)
2558 alloclen = len;
2559 }
2560 else
2561 alloclen = len;
2562 bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002563 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2564 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
Rob Shearman719cd822008-02-18 19:37:35 +00002565 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002566 } else
2567 {
2568 bufferW = NULL;
2569 len = 0;
2570 }
2571
Mike McCormack1baf39f2004-03-30 20:37:49 +00002572 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2573 &len, lpdwIndex );
2574 if( result )
Alberto Massarid476a5a2002-11-12 02:13:04 +00002575 {
Robert Shearman4385d302004-07-21 21:17:03 +00002576 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
Mike McCormack1baf39f2004-03-30 20:37:49 +00002577 lpBuffer, *lpdwBufferLength, NULL, NULL );
Robert Shearman4385d302004-07-21 21:17:03 +00002578 *lpdwBufferLength = len - 1;
2579
2580 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
Alberto Massarid476a5a2002-11-12 02:13:04 +00002581 }
Robert Shearman907ac442004-07-20 01:21:08 +00002582 else
2583 /* since the strings being returned from HttpQueryInfoW should be
2584 * only ASCII characters, it is reasonable to assume that all of
2585 * the Unicode characters can be reduced to a single byte */
2586 *lpdwBufferLength = len / sizeof(WCHAR);
Robert Shearman4385d302004-07-21 21:17:03 +00002587
Mike McCormack1baf39f2004-03-30 20:37:49 +00002588 HeapFree(GetProcessHeap(), 0, bufferW );
2589
Alberto Massarid476a5a2002-11-12 02:13:04 +00002590 return result;
2591}
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002592
2593/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00002594 * HttpSendRequestExA (WININET.@)
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002595 *
2596 * Sends the specified request to the HTTP server and allows chunked
Robert Shearman7a2357a2006-02-14 11:34:14 +01002597 * transfers.
2598 *
2599 * RETURNS
2600 * Success: TRUE
2601 * Failure: FALSE, call GetLastError() for more information.
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002602 */
2603BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2604 LPINTERNET_BUFFERSA lpBuffersIn,
2605 LPINTERNET_BUFFERSA lpBuffersOut,
Francois Gougetd4337f22007-08-30 16:21:33 +02002606 DWORD dwFlags, DWORD_PTR dwContext)
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002607{
Robert Shearman43503572005-11-29 11:42:23 +01002608 INTERNET_BUFFERSW BuffersInW;
Aric Stewartbe918f42005-11-21 15:17:55 +00002609 BOOL rc = FALSE;
Robert Shearman43503572005-11-29 11:42:23 +01002610 DWORD headerlen;
Hans Leidekker781f3f72006-10-13 15:43:54 +02002611 LPWSTR header = NULL;
Aric Stewartbe918f42005-11-21 15:17:55 +00002612
Dan Hipschmanaaecdf72008-03-27 13:51:13 -07002613 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
Aric Stewartbe918f42005-11-21 15:17:55 +00002614 lpBuffersOut, dwFlags, dwContext);
2615
Robert Shearman43503572005-11-29 11:42:23 +01002616 if (lpBuffersIn)
Aric Stewartbe918f42005-11-21 15:17:55 +00002617 {
Robert Shearman43503572005-11-29 11:42:23 +01002618 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2619 if (lpBuffersIn->lpcszHeader)
Aric Stewartbe918f42005-11-21 15:17:55 +00002620 {
Robert Shearman43503572005-11-29 11:42:23 +01002621 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2622 lpBuffersIn->dwHeadersLength,0,0);
Hans Leidekker781f3f72006-10-13 15:43:54 +02002623 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2624 if (!(BuffersInW.lpcszHeader = header))
Robert Shearman43503572005-11-29 11:42:23 +01002625 {
Francois Gougete66d1cd2007-01-09 12:30:45 +01002626 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Robert Shearman43503572005-11-29 11:42:23 +01002627 return FALSE;
2628 }
2629 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2630 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
Hans Leidekker781f3f72006-10-13 15:43:54 +02002631 header, headerlen);
Aric Stewartbe918f42005-11-21 15:17:55 +00002632 }
Aric Stewartd30cec72005-12-08 11:54:24 +01002633 else
2634 BuffersInW.lpcszHeader = NULL;
Robert Shearman43503572005-11-29 11:42:23 +01002635 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2636 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2637 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2638 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2639 BuffersInW.Next = NULL;
Aric Stewartbe918f42005-11-21 15:17:55 +00002640 }
2641
Robert Shearman43503572005-11-29 11:42:23 +01002642 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2643
Hans Leidekker781f3f72006-10-13 15:43:54 +02002644 HeapFree(GetProcessHeap(),0,header);
Robert Shearman43503572005-11-29 11:42:23 +01002645
Aric Stewartbe918f42005-11-21 15:17:55 +00002646 return rc;
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002647}
2648
Ulrich Czekallac2757242000-06-11 20:04:44 +00002649/***********************************************************************
Hans Leidekker6a367db2005-02-01 18:50:53 +00002650 * HttpSendRequestExW (WININET.@)
2651 *
2652 * Sends the specified request to the HTTP server and allows chunked
2653 * transfers
Robert Shearman7a2357a2006-02-14 11:34:14 +01002654 *
2655 * RETURNS
2656 * Success: TRUE
2657 * Failure: FALSE, call GetLastError() for more information.
Hans Leidekker6a367db2005-02-01 18:50:53 +00002658 */
2659BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2660 LPINTERNET_BUFFERSW lpBuffersIn,
2661 LPINTERNET_BUFFERSW lpBuffersOut,
Francois Gougetd4337f22007-08-30 16:21:33 +02002662 DWORD dwFlags, DWORD_PTR dwContext)
Hans Leidekker6a367db2005-02-01 18:50:53 +00002663{
Nigel Liang6f446272007-11-03 18:24:49 -07002664 BOOL ret = FALSE;
Aric Stewartbe918f42005-11-21 15:17:55 +00002665 LPWININETHTTPREQW lpwhr;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002666 LPWININETHTTPSESSIONW lpwhs;
2667 LPWININETAPPINFOW hIC;
Aric Stewartbe918f42005-11-21 15:17:55 +00002668
Francois Gougetd4337f22007-08-30 16:21:33 +02002669 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
Aric Stewartbe918f42005-11-21 15:17:55 +00002670 lpBuffersOut, dwFlags, dwContext);
2671
2672 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2673
2674 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2675 {
2676 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Nigel Liang6f446272007-11-03 18:24:49 -07002677 goto lend;
Aric Stewartbe918f42005-11-21 15:17:55 +00002678 }
2679
Jacek Caban61a1cef2006-10-29 18:51:15 +01002680 lpwhs = lpwhr->lpHttpSession;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002681 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
Jacek Cabana541a442006-10-29 18:49:39 +01002682 hIC = lpwhs->lpAppInfo;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002683 assert(hIC->hdr.htype == WH_HINIT);
2684
2685 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
Aric Stewartbe918f42005-11-21 15:17:55 +00002686 {
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002687 WORKREQUEST workRequest;
2688 struct WORKREQ_HTTPSENDREQUESTW *req;
Aric Stewartbe918f42005-11-21 15:17:55 +00002689
Jacek Cabanc1568452006-12-25 21:33:36 +01002690 workRequest.asyncproc = AsyncHttpSendRequestProc;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002691 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2692 req = &workRequest.u.HttpSendRequestW;
Aric Stewart21712d32006-01-13 13:52:38 +01002693 if (lpBuffersIn)
2694 {
2695 if (lpBuffersIn->lpcszHeader)
2696 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2697 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2698 else
2699 req->lpszHeader = NULL;
2700 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2701 req->lpOptional = lpBuffersIn->lpvBuffer;
2702 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2703 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2704 }
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002705 else
Aric Stewart21712d32006-01-13 13:52:38 +01002706 {
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002707 req->lpszHeader = NULL;
Aric Stewart21712d32006-01-13 13:52:38 +01002708 req->dwHeaderLength = 0;
2709 req->lpOptional = NULL;
2710 req->dwOptionalLength = 0;
2711 req->dwContentLength = 0;
2712 }
2713
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002714 req->bEndRequest = FALSE;
2715
2716 INTERNET_AsyncCall(&workRequest);
2717 /*
2718 * This is from windows.
2719 */
Francois Gougete66d1cd2007-01-09 12:30:45 +01002720 INTERNET_SetLastError(ERROR_IO_PENDING);
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002721 }
2722 else
Aric Stewartbe918f42005-11-21 15:17:55 +00002723 {
Juan Langd857c042007-10-22 10:23:54 -07002724 if (lpBuffersIn)
2725 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2726 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2727 lpBuffersIn->dwBufferTotal, FALSE);
2728 else
2729 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
Aric Stewartbe918f42005-11-21 15:17:55 +00002730 }
Nigel Liang6f446272007-11-03 18:24:49 -07002731
2732lend:
2733 if ( lpwhr )
2734 WININET_Release( &lpwhr->hdr );
2735
Aric Stewartbe918f42005-11-21 15:17:55 +00002736 TRACE("<---\n");
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002737 return ret;
Hans Leidekker6a367db2005-02-01 18:50:53 +00002738}
2739
2740/***********************************************************************
Dimitrie O. Paunf3b681a2004-06-28 20:28:38 +00002741 * HttpSendRequestW (WININET.@)
Ulrich Czekallac2757242000-06-11 20:04:44 +00002742 *
2743 * Sends the specified request to the HTTP server
2744 *
2745 * RETURNS
2746 * TRUE on success
2747 * FALSE on failure
2748 *
2749 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002750BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
Ulrich Czekallac2757242000-06-11 20:04:44 +00002751 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00002752{
Mike McCormacka4e902c2004-03-30 04:36:09 +00002753 LPWININETHTTPREQW lpwhr;
2754 LPWININETHTTPSESSIONW lpwhs = NULL;
Mike McCormack09d2d472004-03-25 05:29:47 +00002755 LPWININETAPPINFOW hIC = NULL;
Mike McCormack3a1391b2004-07-19 21:49:39 +00002756 BOOL r;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002757
Rob Shearman2e21d092007-05-25 12:07:26 +01002758 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2759 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002760
Mike McCormacka4e902c2004-03-30 04:36:09 +00002761 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
Ulrich Czekallac2757242000-06-11 20:04:44 +00002762 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2763 {
2764 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002765 r = FALSE;
2766 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002767 }
2768
Jacek Caban61a1cef2006-10-29 18:51:15 +01002769 lpwhs = lpwhr->lpHttpSession;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002770 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2771 {
2772 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002773 r = FALSE;
2774 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002775 }
2776
Jacek Cabana541a442006-10-29 18:49:39 +01002777 hIC = lpwhs->lpAppInfo;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002778 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2779 {
2780 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002781 r = FALSE;
2782 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002783 }
2784
2785 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2786 {
2787 WORKREQUEST workRequest;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002788 struct WORKREQ_HTTPSENDREQUESTW *req;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002789
Jacek Cabanc1568452006-12-25 21:33:36 +01002790 workRequest.asyncproc = AsyncHttpSendRequestProc;
2791 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
Mike McCormacka4e902c2004-03-30 04:36:09 +00002792 req = &workRequest.u.HttpSendRequestW;
Aric Stewartff9b9d42002-06-21 23:59:49 +00002793 if (lpszHeaders)
Rob Shearman2e21d092007-05-25 12:07:26 +01002794 {
2795 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, dwHeaderLength * sizeof(WCHAR));
2796 memcpy(req->lpszHeader, lpszHeaders, dwHeaderLength * sizeof(WCHAR));
2797 }
Aric Stewartff9b9d42002-06-21 23:59:49 +00002798 else
Mike McCormackb8921a22003-09-25 20:25:22 +00002799 req->lpszHeader = 0;
2800 req->dwHeaderLength = dwHeaderLength;
2801 req->lpOptional = lpOptional;
2802 req->dwOptionalLength = dwOptionalLength;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002803 req->dwContentLength = dwOptionalLength;
2804 req->bEndRequest = TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002805
Aric Stewartff9b9d42002-06-21 23:59:49 +00002806 INTERNET_AsyncCall(&workRequest);
2807 /*
Alberto Massaribc8bd722002-12-06 23:20:31 +00002808 * This is from windows.
Aric Stewartff9b9d42002-06-21 23:59:49 +00002809 */
Francois Gougete66d1cd2007-01-09 12:30:45 +01002810 INTERNET_SetLastError(ERROR_IO_PENDING);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002811 r = FALSE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002812 }
2813 else
2814 {
Mike McCormack3a1391b2004-07-19 21:49:39 +00002815 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002816 dwHeaderLength, lpOptional, dwOptionalLength,
2817 dwOptionalLength, TRUE);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002818 }
Mike McCormack3a1391b2004-07-19 21:49:39 +00002819lend:
2820 if( lpwhr )
2821 WININET_Release( &lpwhr->hdr );
2822 return r;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002823}
2824
Alberto Massarid476a5a2002-11-12 02:13:04 +00002825/***********************************************************************
Dimitrie O. Paunf3b681a2004-06-28 20:28:38 +00002826 * HttpSendRequestA (WININET.@)
Alberto Massarid476a5a2002-11-12 02:13:04 +00002827 *
2828 * Sends the specified request to the HTTP server
2829 *
2830 * RETURNS
2831 * TRUE on success
2832 * FALSE on failure
2833 *
2834 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002835BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
Alberto Massarid476a5a2002-11-12 02:13:04 +00002836 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2837{
2838 BOOL result;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002839 LPWSTR szHeaders=NULL;
Alberto Massarid476a5a2002-11-12 02:13:04 +00002840 DWORD nLen=dwHeaderLength;
2841 if(lpszHeaders!=NULL)
2842 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00002843 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2844 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2845 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
Alberto Massarid476a5a2002-11-12 02:13:04 +00002846 }
Mike McCormacka4e902c2004-03-30 04:36:09 +00002847 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00002848 HeapFree(GetProcessHeap(),0,szHeaders);
Alberto Massarid476a5a2002-11-12 02:13:04 +00002849 return result;
2850}
Ulrich Czekallac2757242000-06-11 20:04:44 +00002851
Jacek Caband7a49e82008-02-13 13:32:49 +01002852static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
2853{
2854 LPHTTPHEADERW host_header;
2855
2856 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2857
2858 host_header = HTTP_GetHeader(req, szHost);
2859 if(!host_header)
2860 return FALSE;
2861
2862 sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
2863 return TRUE;
2864}
2865
Ulrich Czekallac2757242000-06-11 20:04:44 +00002866/***********************************************************************
Alberto Massaribc8bd722002-12-06 23:20:31 +00002867 * HTTP_HandleRedirect (internal)
2868 */
Rob Shearman4319ec62006-12-07 00:53:27 +00002869static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
Alberto Massaribc8bd722002-12-06 23:20:31 +00002870{
Jacek Caban61a1cef2006-10-29 18:51:15 +01002871 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
Jacek Cabana541a442006-10-29 18:49:39 +01002872 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002873 WCHAR path[2048];
Mike McCormack7cc70c02004-02-07 01:03:41 +00002874
Alberto Massaribc8bd722002-12-06 23:20:31 +00002875 if(lpszUrl[0]=='/')
2876 {
2877 /* if it's an absolute path, keep the same session info */
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002878 lstrcpynW(path, lpszUrl, 2048);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002879 }
Mike McCormacka1c16d22003-07-22 03:17:52 +00002880 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2881 {
2882 TRACE("Redirect through proxy\n");
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002883 lstrcpynW(path, lpszUrl, 2048);
Mike McCormacka1c16d22003-07-22 03:17:52 +00002884 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00002885 else
2886 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00002887 URL_COMPONENTSW urlComponents;
2888 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
Hans Leidekker781f3f72006-10-13 15:43:54 +02002889 static WCHAR szHttp[] = {'h','t','t','p',0};
2890 static WCHAR szHttps[] = {'h','t','t','p','s',0};
Robert Shearman05900252006-03-09 15:11:59 +00002891 DWORD url_length = 0;
2892 LPWSTR orig_url;
2893 LPWSTR combined_url;
2894
2895 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
Hans Leidekker781f3f72006-10-13 15:43:54 +02002896 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
Robert Shearman05900252006-03-09 15:11:59 +00002897 urlComponents.dwSchemeLength = 0;
2898 urlComponents.lpszHostName = lpwhs->lpszHostName;
2899 urlComponents.dwHostNameLength = 0;
2900 urlComponents.nPort = lpwhs->nHostPort;
2901 urlComponents.lpszUserName = lpwhs->lpszUserName;
2902 urlComponents.dwUserNameLength = 0;
2903 urlComponents.lpszPassword = NULL;
2904 urlComponents.dwPasswordLength = 0;
2905 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2906 urlComponents.dwUrlPathLength = 0;
2907 urlComponents.lpszExtraInfo = NULL;
2908 urlComponents.dwExtraInfoLength = 0;
2909
2910 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2911 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2912 return FALSE;
2913
Robert Shearman80e4fb52006-03-21 13:44:36 +00002914 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
Robert Shearman05900252006-03-09 15:11:59 +00002915
Robert Shearman80e4fb52006-03-21 13:44:36 +00002916 /* convert from bytes to characters */
2917 url_length = url_length / sizeof(WCHAR) - 1;
Robert Shearman05900252006-03-09 15:11:59 +00002918 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2919 {
2920 HeapFree(GetProcessHeap(), 0, orig_url);
2921 return FALSE;
2922 }
2923
2924 url_length = 0;
2925 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2926 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2927 {
2928 HeapFree(GetProcessHeap(), 0, orig_url);
2929 return FALSE;
2930 }
2931 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2932
2933 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2934 {
2935 HeapFree(GetProcessHeap(), 0, orig_url);
2936 HeapFree(GetProcessHeap(), 0, combined_url);
2937 return FALSE;
2938 }
2939 HeapFree(GetProcessHeap(), 0, orig_url);
2940
Aric Stewart1fc760d2005-11-28 17:31:02 +01002941 userName[0] = 0;
2942 hostName[0] = 0;
2943 protocol[0] = 0;
2944
Mike McCormacka4e902c2004-03-30 04:36:09 +00002945 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002946 urlComponents.lpszScheme = protocol;
2947 urlComponents.dwSchemeLength = 32;
2948 urlComponents.lpszHostName = hostName;
2949 urlComponents.dwHostNameLength = MAXHOSTNAME;
2950 urlComponents.lpszUserName = userName;
2951 urlComponents.dwUserNameLength = 1024;
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002952 urlComponents.lpszPassword = NULL;
2953 urlComponents.dwPasswordLength = 0;
Alberto Massaribc8bd722002-12-06 23:20:31 +00002954 urlComponents.lpszUrlPath = path;
2955 urlComponents.dwUrlPathLength = 2048;
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002956 urlComponents.lpszExtraInfo = NULL;
2957 urlComponents.dwExtraInfoLength = 0;
Robert Shearman05900252006-03-09 15:11:59 +00002958 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2959 {
2960 HeapFree(GetProcessHeap(), 0, combined_url);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002961 return FALSE;
Robert Shearman05900252006-03-09 15:11:59 +00002962 }
Jacek Cabanf9791342008-02-13 13:34:05 +01002963
Robert Shearman05900252006-03-09 15:11:59 +00002964 HeapFree(GetProcessHeap(), 0, combined_url);
Robert Shearmanefac01b2005-11-29 11:25:31 +01002965
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002966 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
Robert Shearmanefac01b2005-11-29 11:25:31 +01002967 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2968 {
2969 TRACE("redirect from secure page to non-secure page\n");
2970 /* FIXME: warn about from secure redirect to non-secure page */
2971 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2972 }
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002973 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
Robert Shearmanefac01b2005-11-29 11:25:31 +01002974 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2975 {
2976 TRACE("redirect from non-secure page to secure page\n");
2977 /* FIXME: notify about redirect to secure page */
2978 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2979 }
2980
Alberto Massaribc8bd722002-12-06 23:20:31 +00002981 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
Aric Stewart1fc760d2005-11-28 17:31:02 +01002982 {
2983 if (lstrlenW(protocol)>4) /*https*/
2984 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2985 else /*http*/
2986 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2987 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00002988
Mike McCormacka1c16d22003-07-22 03:17:52 +00002989#if 0
2990 /*
2991 * This upsets redirects to binary files on sourceforge.net
2992 * and gives an html page instead of the target file
2993 * Examination of the HTTP request sent by native wininet.dll
2994 * reveals that it doesn't send a referrer in that case.
2995 * Maybe there's a flag that enables this, or maybe a referrer
2996 * shouldn't be added in case of a redirect.
2997 */
2998
2999 /* consider the current host as the referrer */
Jacek Cabanf9791342008-02-13 13:34:05 +01003000 if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
Mike McCormacka1c16d22003-07-22 03:17:52 +00003001 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
3002 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
3003 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
3004#endif
Alberto Massaribc8bd722002-12-06 23:20:31 +00003005
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00003006 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
Robert Shearman8a8ce9c2005-11-29 11:35:19 +01003007 lpwhs->lpszServerName = WININET_strdupW(hostName);
3008 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
Aric Stewart1fc760d2005-11-28 17:31:02 +01003009 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
3010 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
3011 {
3012 int len;
Andrew Ziem70533212006-05-24 00:22:38 -06003013 static const WCHAR fmt[] = {'%','s',':','%','i',0};
Aric Stewart1fc760d2005-11-28 17:31:02 +01003014 len = lstrlenW(hostName);
Robert Shearman8a8ce9c2005-11-29 11:35:19 +01003015 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
3016 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
3017 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
Aric Stewart1fc760d2005-11-28 17:31:02 +01003018 }
3019 else
Robert Shearman8a8ce9c2005-11-29 11:35:19 +01003020 lpwhs->lpszHostName = WININET_strdupW(hostName);
Aric Stewart1fc760d2005-11-28 17:31:02 +01003021
Aric Stewart1e946d32005-12-13 17:07:41 +01003022 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
Aric Stewart1fc760d2005-11-28 17:31:02 +01003023
3024
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00003025 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
Robert Shearmanef209362006-03-10 12:28:52 +00003026 lpwhs->lpszUserName = NULL;
3027 if (userName[0])
3028 lpwhs->lpszUserName = WININET_strdupW(userName);
Alberto Massaribc8bd722002-12-06 23:20:31 +00003029 lpwhs->nServerPort = urlComponents.nPort;
3030
Rob Shearman72575a02006-12-07 00:52:50 +00003031 if (!HTTP_ResolveName(lpwhr))
Alberto Massaribc8bd722002-12-06 23:20:31 +00003032 return FALSE;
Alberto Massaribc8bd722002-12-06 23:20:31 +00003033
Robert Shearmanefac01b2005-11-29 11:25:31 +01003034 NETCON_close(&lpwhr->netConnection);
Robert Shearman9981f332006-03-06 17:31:09 +00003035
3036 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3037 return FALSE;
Alberto Massaribc8bd722002-12-06 23:20:31 +00003038 }
3039
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00003040 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
Alberto Massaribc8bd722002-12-06 23:20:31 +00003041 lpwhr->lpszPath=NULL;
Jacek Cabanf9791342008-02-13 13:34:05 +01003042 if (*path)
Alberto Massaribc8bd722002-12-06 23:20:31 +00003043 {
3044 DWORD needed = 0;
3045 HRESULT rc;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003046
3047 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
Alberto Massaribc8bd722002-12-06 23:20:31 +00003048 if (rc != E_POINTER)
Mike McCormacka4e902c2004-03-30 04:36:09 +00003049 needed = strlenW(path)+1;
3050 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
3051 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
Alberto Massaribc8bd722002-12-06 23:20:31 +00003052 URL_ESCAPE_SPACES_ONLY);
3053 if (rc)
3054 {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02003055 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
Mike McCormacka4e902c2004-03-30 04:36:09 +00003056 strcpyW(lpwhr->lpszPath,path);
Alberto Massaribc8bd722002-12-06 23:20:31 +00003057 }
3058 }
3059
Rob Shearman4319ec62006-12-07 00:53:27 +00003060 return TRUE;
Alberto Massaribc8bd722002-12-06 23:20:31 +00003061}
3062
3063/***********************************************************************
Mike McCormacka4969062004-07-04 00:24:47 +00003064 * HTTP_build_req (internal)
3065 *
3066 * concatenate all the strings in the request together
3067 */
3068static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
3069{
3070 LPCWSTR *t;
3071 LPWSTR str;
3072
3073 for( t = list; *t ; t++ )
3074 len += strlenW( *t );
3075 len++;
3076
3077 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
3078 *str = 0;
3079
3080 for( t = list; *t ; t++ )
3081 strcatW( str, *t );
3082
3083 return str;
3084}
3085
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003086static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
3087{
3088 LPWSTR lpszPath;
3089 LPWSTR requestString;
3090 INT len;
3091 INT cnt;
3092 INT responseLen;
3093 char *ascii_req;
3094 BOOL ret;
3095 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
3096 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
Jacek Caban61a1cef2006-10-29 18:51:15 +01003097 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003098
3099 TRACE("\n");
3100
3101 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
3102 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
Hans Leidekkerd0033db2008-02-17 20:41:42 +01003103 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003104 HeapFree( GetProcessHeap(), 0, lpszPath );
3105
3106 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3107 NULL, 0, NULL, NULL );
3108 len--; /* the nul terminator isn't needed */
3109 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
3110 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3111 ascii_req, len, NULL, NULL );
3112 HeapFree( GetProcessHeap(), 0, requestString );
3113
3114 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
3115
3116 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
3117 HeapFree( GetProcessHeap(), 0, ascii_req );
3118 if (!ret || cnt < 0)
3119 return FALSE;
3120
Hans Leidekker09366012008-03-26 11:31:21 +01003121 responseLen = HTTP_GetResponseHeaders( lpwhr, TRUE );
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003122 if (!responseLen)
3123 return FALSE;
3124
3125 return TRUE;
3126}
3127
Mike McCormacka4969062004-07-04 00:24:47 +00003128/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +00003129 * HTTP_HttpSendRequestW (internal)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003130 *
3131 * Sends the specified request to the HTTP server
3132 *
3133 * RETURNS
3134 * TRUE on success
3135 * FALSE on failure
3136 *
3137 */
Mike McCormack3a1391b2004-07-19 21:49:39 +00003138BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
Robert Shearmanf6252cf2005-11-30 12:31:22 +01003139 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
3140 DWORD dwContentLength, BOOL bEndRequest)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003141{
3142 INT cnt;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003143 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003144 LPWSTR requestString = NULL;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003145 INT responseLen;
Rob Shearman14fb4182007-01-04 18:21:13 +00003146 BOOL loop_next;
Robert Shearmaned517f32004-09-20 19:10:31 +00003147 INTERNET_ASYNC_RESULT iar;
Rob Shearmancf43b162007-01-04 18:21:34 +00003148 static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
Hans Leidekkerb0912d12008-02-02 16:08:28 +01003149 static const WCHAR szPost[] = { 'P','O','S','T',0 };
Hans Leidekker64359c22007-10-28 16:32:46 +01003150 static const WCHAR szContentLength[] =
3151 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3152 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
Ulrich Czekallac2757242000-06-11 20:04:44 +00003153
Mike McCormack3a1391b2004-07-19 21:49:39 +00003154 TRACE("--> %p\n", lpwhr);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003155
Mike McCormack3a1391b2004-07-19 21:49:39 +00003156 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003157
Ulrich Czekallac2757242000-06-11 20:04:44 +00003158 /* Clear any error information */
3159 INTERNET_SetLastError(0);
3160
Jacek Cabanf9791342008-02-13 13:34:05 +01003161 /* if the verb is NULL default to GET */
3162 if (!lpwhr->lpszVerb)
3163 lpwhr->lpszVerb = WININET_strdupW(szGET);
3164
Hans Leidekkerb0912d12008-02-02 16:08:28 +01003165 if (dwContentLength || !strcmpW(lpwhr->lpszVerb, szPost))
3166 {
3167 sprintfW(contentLengthStr, szContentLength, dwContentLength);
3168 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3169 }
David Hammerton6226f3f2003-08-05 18:31:02 +00003170
David Hammerton852c7ae2003-06-20 23:26:56 +00003171 do
Nikolas Zimmermann76598822001-10-04 18:12:41 +00003172 {
Aric Stewartbe918f42005-11-21 15:17:55 +00003173 DWORD len;
Mike McCormacka4969062004-07-04 00:24:47 +00003174 char *ascii_req;
3175
Rob Shearman14fb4182007-01-04 18:21:13 +00003176 loop_next = FALSE;
Rob Shearman272954b2007-01-04 18:23:17 +00003177
3178 /* like native, just in case the caller forgot to call InternetReadFile
3179 * for all the data */
3180 HTTP_DrainContent(lpwhr);
Rob Shearmanac1b5272007-01-04 18:21:49 +00003181 lpwhr->dwContentRead = 0;
Rob Shearman14fb4182007-01-04 18:21:13 +00003182
Rob Shearman4319ec62006-12-07 00:53:27 +00003183 if (TRACE_ON(wininet))
3184 {
3185 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3186 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
3187 }
Nikolas Zimmermann76598822001-10-04 18:12:41 +00003188
Aric Stewartbe918f42005-11-21 15:17:55 +00003189 HTTP_FixURL(lpwhr);
Hans Leidekkerbb9413d2007-08-09 21:43:25 +02003190 HTTP_ProcessHeader(lpwhr, szConnection,
3191 lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION ? szKeepAlive : szClose,
3192 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
3193
Rob Shearmanebaa4d92008-03-12 13:27:46 +00003194 HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
3195 HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003196
Mike McCormackf1d7b142004-07-21 19:36:34 +00003197 /* add the headers the caller supplied */
Mike McCormack08c6c692004-08-10 23:41:35 +00003198 if( lpszHeaders && dwHeaderLength )
Mike McCormackf1d7b142004-07-21 19:36:34 +00003199 {
Mike McCormack08c6c692004-08-10 23:41:35 +00003200 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
3201 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
Mike McCormackf1d7b142004-07-21 19:36:34 +00003202 }
3203
Hans Leidekker0ffe9f52008-03-30 19:16:52 +01003204 if (lpwhr->lpHttpSession->lpAppInfo->lpszProxy && lpwhr->lpHttpSession->lpAppInfo->lpszProxy[0])
3205 {
3206 WCHAR *url = HTTP_BuildProxyRequestUrl(lpwhr);
3207 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, url, lpwhr->lpszVersion);
3208 HeapFree(GetProcessHeap(), 0, url);
3209 }
3210 else
3211 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
3212
Mike McCormacka4969062004-07-04 00:24:47 +00003213
3214 TRACE("Request header -> %s\n", debugstr_w(requestString) );
Aric Stewartff9b9d42002-06-21 23:59:49 +00003215
David Hammerton852c7ae2003-06-20 23:26:56 +00003216 /* Send the request and store the results */
3217 if (!HTTP_OpenConnection(lpwhr))
3218 goto lend;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003219
Mike McCormacka4969062004-07-04 00:24:47 +00003220 /* send the request as ASCII, tack on the optional data */
3221 if( !lpOptional )
3222 dwOptionalLength = 0;
3223 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3224 NULL, 0, NULL, NULL );
3225 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
3226 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3227 ascii_req, len, NULL, NULL );
3228 if( lpOptional )
Mike McCormackf1d7b142004-07-21 19:36:34 +00003229 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
3230 len = (len + dwOptionalLength - 1);
3231 ascii_req[len] = 0;
Aric Stewart44cbdf22005-10-19 18:28:35 +00003232 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
Mike McCormackf1d7b142004-07-21 19:36:34 +00003233
Robert Shearmande2666f2005-11-29 10:44:05 +01003234 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3235 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
Mike McCormackf1d7b142004-07-21 19:36:34 +00003236
Mike McCormacka4969062004-07-04 00:24:47 +00003237 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
3238 HeapFree( GetProcessHeap(), 0, ascii_req );
David Hammerton852c7ae2003-06-20 23:26:56 +00003239
Robert Shearmande2666f2005-11-29 10:44:05 +01003240 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3241 INTERNET_STATUS_REQUEST_SENT,
3242 &len, sizeof(DWORD));
David Hammerton852c7ae2003-06-20 23:26:56 +00003243
Robert Shearmanf6252cf2005-11-30 12:31:22 +01003244 if (bEndRequest)
3245 {
Rob Shearmanac1b5272007-01-04 18:21:49 +00003246 DWORD dwBufferSize;
Rob Shearmancb289692007-06-05 19:49:58 +01003247 DWORD dwStatusCode;
Rob Shearmanac1b5272007-01-04 18:21:49 +00003248
Robert Shearmanf6252cf2005-11-30 12:31:22 +01003249 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3250 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3251
3252 if (cnt < 0)
3253 goto lend;
3254
Hans Leidekker09366012008-03-26 11:31:21 +01003255 responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
Robert Shearmanf6252cf2005-11-30 12:31:22 +01003256 if (responseLen)
3257 bSuccess = TRUE;
3258
3259 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3260 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
3261 sizeof(DWORD));
Rob Shearman4319ec62006-12-07 00:53:27 +00003262
Hans Leidekker97171132008-02-17 21:29:45 +01003263 HTTP_ProcessCookies(lpwhr);
Rob Shearman4319ec62006-12-07 00:53:27 +00003264
Rob Shearmanac1b5272007-01-04 18:21:49 +00003265 dwBufferSize = sizeof(lpwhr->dwContentLength);
3266 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
3267 &lpwhr->dwContentLength,&dwBufferSize,NULL))
3268 lpwhr->dwContentLength = -1;
3269
Rob Shearman272954b2007-01-04 18:23:17 +00003270 if (lpwhr->dwContentLength == 0)
3271 HTTP_FinishedReading(lpwhr);
3272
Rob Shearmancb289692007-06-05 19:49:58 +01003273 dwBufferSize = sizeof(dwStatusCode);
3274 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
3275 &dwStatusCode,&dwBufferSize,NULL))
3276 dwStatusCode = 0;
3277
Rob Shearman4319ec62006-12-07 00:53:27 +00003278 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
3279 {
Rob Shearman4319ec62006-12-07 00:53:27 +00003280 WCHAR szNewLocation[2048];
Rob Shearman9efe0832007-01-12 19:19:18 -06003281 dwBufferSize=sizeof(szNewLocation);
Rob Shearmancb289692007-06-05 19:49:58 +01003282 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
Rob Shearmanac1b5272007-01-04 18:21:49 +00003283 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
Rob Shearman4319ec62006-12-07 00:53:27 +00003284 {
Rob Shearman272954b2007-01-04 18:23:17 +00003285 HTTP_DrainContent(lpwhr);
Rob Shearman4319ec62006-12-07 00:53:27 +00003286 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3287 INTERNET_STATUS_REDIRECT, szNewLocation,
3288 dwBufferSize);
3289 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
3290 if (bSuccess)
3291 {
3292 HeapFree(GetProcessHeap(), 0, requestString);
3293 loop_next = TRUE;
3294 }
3295 }
3296 }
Rob Shearman4b507682007-05-21 14:26:26 +01003297 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
3298 {
Rob Shearman4b507682007-05-21 14:26:26 +01003299 WCHAR szAuthValue[2048];
3300 dwBufferSize=2048;
Rob Shearmancb289692007-06-05 19:49:58 +01003301 if (dwStatusCode == HTTP_STATUS_DENIED)
Rob Shearman4b507682007-05-21 14:26:26 +01003302 {
3303 DWORD dwIndex = 0;
3304 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3305 {
Rob Shearmancb289692007-06-05 19:49:58 +01003306 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3307 &lpwhr->pAuthInfo,
3308 lpwhr->lpHttpSession->lpszUserName,
3309 lpwhr->lpHttpSession->lpszPassword))
3310 {
3311 loop_next = TRUE;
3312 break;
3313 }
3314 }
3315 }
3316 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
3317 {
3318 DWORD dwIndex = 0;
3319 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3320 {
3321 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3322 &lpwhr->pProxyAuthInfo,
3323 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
3324 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
Rob Shearman4b507682007-05-21 14:26:26 +01003325 {
3326 loop_next = TRUE;
3327 break;
3328 }
3329 }
3330 }
3331 }
Robert Shearmanf6252cf2005-11-30 12:31:22 +01003332 }
3333 else
3334 bSuccess = TRUE;
David Hammerton852c7ae2003-06-20 23:26:56 +00003335 }
3336 while (loop_next);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003337
Jacek Caband7a49e82008-02-13 13:32:49 +01003338 /* FIXME: Better check, when we have to create the cache file */
3339 if(bSuccess && (lpwhr->hdr.dwFlags & INTERNET_FLAG_NEED_FILE)) {
3340 WCHAR url[INTERNET_MAX_URL_LENGTH];
3341 WCHAR cacheFileName[MAX_PATH+1];
3342 BOOL b;
3343
3344 b = HTTP_GetRequestURL(lpwhr, url);
3345 if(!b) {
3346 WARN("Could not get URL\n");
3347 goto lend;
3348 }
3349
3350 b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
3351 if(b) {
3352 lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
3353 lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
3354 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3355 if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
3356 WARN("Could not create file: %u\n", GetLastError());
3357 lpwhr->hCacheFile = NULL;
3358 }
3359 }else {
3360 WARN("Could not create cache entry: %08x\n", GetLastError());
3361 }
3362 }
3363
Ulrich Czekallac2757242000-06-11 20:04:44 +00003364lend:
3365
Michael Stefaniuc7cb43c92004-12-21 14:42:35 +00003366 HeapFree(GetProcessHeap(), 0, requestString);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003367
Alberto Massaribc8bd722002-12-06 23:20:31 +00003368 /* TODO: send notification for P3P header */
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00003369
Robert Shearmaned517f32004-09-20 19:10:31 +00003370 iar.dwResult = (DWORD)bSuccess;
3371 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
Aric Stewartff9b9d42002-06-21 23:59:49 +00003372
Robert Shearmande2666f2005-11-29 10:44:05 +01003373 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3374 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3375 sizeof(INTERNET_ASYNC_RESULT));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003376
3377 TRACE("<--\n");
3378 return bSuccess;
3379}
3380
Ulrich Czekallac2757242000-06-11 20:04:44 +00003381/***********************************************************************
Jacek Caban5a535d62008-02-26 20:20:41 +01003382 * HTTPSESSION_Destroy (internal)
3383 *
3384 * Deallocate session handle
3385 *
3386 */
3387static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3388{
3389 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3390
3391 TRACE("%p\n", lpwhs);
3392
3393 WININET_Release(&lpwhs->lpAppInfo->hdr);
3394
3395 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3396 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3397 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3398 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3399 HeapFree(GetProcessHeap(), 0, lpwhs);
3400}
3401
Jacek Cabane2933c22008-03-12 02:23:20 +01003402static DWORD HTTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3403{
3404 switch(option) {
3405 case INTERNET_OPTION_HANDLE_TYPE:
3406 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3407
3408 if (*size < sizeof(ULONG))
3409 return ERROR_INSUFFICIENT_BUFFER;
3410
3411 *size = sizeof(DWORD);
3412 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
3413 return ERROR_SUCCESS;
3414 }
3415
3416 FIXME("Not implemented option %d\n", option);
3417 return ERROR_INTERNET_INVALID_OPTION;
3418}
Jacek Caban5a535d62008-02-26 20:20:41 +01003419
3420static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
Jacek Caban7dc9bf62008-02-26 20:21:00 +01003421 HTTPSESSION_Destroy,
Jacek Caban1ffcfbc2008-02-26 20:21:34 +01003422 NULL,
Jacek Cabane2933c22008-03-12 02:23:20 +01003423 HTTPSESSION_QueryOption,
Jacek Caban0e33eee2008-02-26 20:22:02 +01003424 NULL,
Jacek Caban8c45eec2008-02-27 18:55:09 +01003425 NULL,
Jacek Caban33141842008-02-29 12:57:57 +01003426 NULL,
Jacek Caban3b4ca692008-03-02 19:35:11 +01003427 NULL,
Jacek Caband597fd12008-03-03 18:07:20 +01003428 NULL,
Jacek Caban7dc9bf62008-02-26 20:21:00 +01003429 NULL
Jacek Caban5a535d62008-02-26 20:20:41 +01003430};
3431
3432
3433/***********************************************************************
Ulrich Czekallac2757242000-06-11 20:04:44 +00003434 * HTTP_Connect (internal)
3435 *
3436 * Create http session handle
3437 *
3438 * RETURNS
3439 * HINTERNET a session handle on success
3440 * NULL on failure
3441 *
3442 */
Mike McCormack3a1391b2004-07-19 21:49:39 +00003443HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
Mike McCormacka4e902c2004-03-30 04:36:09 +00003444 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
Francois Gougetd4337f22007-08-30 16:21:33 +02003445 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
Kevin Koltzau917df922004-05-13 05:17:25 +00003446 DWORD dwInternalFlags)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003447{
3448 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003449 LPWININETHTTPSESSIONW lpwhs = NULL;
Mike McCormack7cc70c02004-02-07 01:03:41 +00003450 HINTERNET handle = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003451
Aric Stewartff9b9d42002-06-21 23:59:49 +00003452 TRACE("-->\n");
Ulrich Czekallac2757242000-06-11 20:04:44 +00003453
Rob Shearman59ab0cf2008-01-31 14:47:25 +00003454 if (!lpszServerName || !lpszServerName[0])
3455 {
3456 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3457 goto lerror;
3458 }
3459
Mike McCormack3a1391b2004-07-19 21:49:39 +00003460 assert( hIC->hdr.htype == WH_HINIT );
Ulrich Czekallac2757242000-06-11 20:04:44 +00003461
Mike McCormacka4e902c2004-03-30 04:36:09 +00003462 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003463 if (NULL == lpwhs)
3464 {
3465 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3466 goto lerror;
3467 }
3468
Aric Stewartff9b9d42002-06-21 23:59:49 +00003469 /*
3470 * According to my tests. The name is not resolved until a request is sent
3471 */
Ulrich Czekallac2757242000-06-11 20:04:44 +00003472
Ulrich Czekallac2757242000-06-11 20:04:44 +00003473 lpwhs->hdr.htype = WH_HHTTPSESSION;
Jacek Caban5a535d62008-02-26 20:20:41 +01003474 lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003475 lpwhs->hdr.dwFlags = dwFlags;
3476 lpwhs->hdr.dwContext = dwContext;
Jacek Caban5c53baa2006-10-30 22:14:59 +01003477 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
Rob Shearmanbdc81d92008-03-10 16:39:15 +00003478 lpwhs->hdr.refs = 1;
Robert Shearmaned517f32004-09-20 19:10:31 +00003479 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
Mike McCormack3a1391b2004-07-19 21:49:39 +00003480
Jacek Cabanc2506172006-10-29 18:48:48 +01003481 WININET_AddRef( &hIC->hdr );
3482 lpwhs->lpAppInfo = hIC;
Misha Koshelev728e5fa2007-09-20 20:59:40 -05003483 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
Jacek Cabanc2506172006-10-29 18:48:48 +01003484
Mike McCormack3a1391b2004-07-19 21:49:39 +00003485 handle = WININET_AllocHandle( &lpwhs->hdr );
3486 if (NULL == handle)
3487 {
3488 ERR("Failed to alloc handle\n");
3489 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3490 goto lerror;
3491 }
3492
Dominik Strasser94c02fe2003-04-14 21:32:36 +00003493 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
Mike McCormack09d2d472004-03-25 05:29:47 +00003494 if(strchrW(hIC->lpszProxy, ' '))
Dominik Strasser94c02fe2003-04-14 21:32:36 +00003495 FIXME("Several proxies not implemented.\n");
3496 if(hIC->lpszProxyBypass)
3497 FIXME("Proxy bypass is ignored.\n");
3498 }
Robert Shearmanef209362006-03-10 12:28:52 +00003499 if (lpszServerName && lpszServerName[0])
Robert Shearman7bb34632005-11-28 10:40:42 +01003500 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00003501 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
Robert Shearman7bb34632005-11-28 10:40:42 +01003502 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
3503 }
Robert Shearmanef209362006-03-10 12:28:52 +00003504 if (lpszUserName && lpszUserName[0])
Mike McCormacka4e902c2004-03-30 04:36:09 +00003505 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
Rob Shearman4b507682007-05-21 14:26:26 +01003506 if (lpszPassword && lpszPassword[0])
3507 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003508 lpwhs->nServerPort = nServerPort;
Robert Shearman35921102005-11-28 11:53:05 +01003509 lpwhs->nHostPort = nServerPort;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003510
Kevin Koltzau917df922004-05-13 05:17:25 +00003511 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
Robert Shearmaned517f32004-09-20 19:10:31 +00003512 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003513 {
Robert Shearmande2666f2005-11-29 10:44:05 +01003514 INTERNET_SendCallback(&hIC->hdr, dwContext,
3515 INTERNET_STATUS_HANDLE_CREATED, &handle,
3516 sizeof(handle));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003517 }
3518
3519 bSuccess = TRUE;
3520
3521lerror:
Mike McCormack3a1391b2004-07-19 21:49:39 +00003522 if( lpwhs )
3523 WININET_Release( &lpwhs->hdr );
Ulrich Czekallac2757242000-06-11 20:04:44 +00003524
Aric Stewartff9b9d42002-06-21 23:59:49 +00003525/*
Francois Gouget93416cd2005-03-23 13:15:18 +00003526 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
Aric Stewartff9b9d42002-06-21 23:59:49 +00003527 * windows
3528 */
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00003529
Mike McCormack3a1391b2004-07-19 21:49:39 +00003530 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
Mike McCormack7cc70c02004-02-07 01:03:41 +00003531 return handle;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003532}
3533
3534
3535/***********************************************************************
3536 * HTTP_OpenConnection (internal)
3537 *
3538 * Connect to a web server
3539 *
3540 * RETURNS
3541 *
3542 * TRUE on success
3543 * FALSE on failure
3544 */
Jacek Caban02708c62005-10-26 10:07:58 +00003545static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003546{
3547 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003548 LPWININETHTTPSESSIONW lpwhs;
Mike McCormack09d2d472004-03-25 05:29:47 +00003549 LPWININETAPPINFOW hIC = NULL;
Jacek Cabanad023172006-01-05 14:37:06 +01003550 char szaddr[32];
Ulrich Czekallac2757242000-06-11 20:04:44 +00003551
Aric Stewartff9b9d42002-06-21 23:59:49 +00003552 TRACE("-->\n");
3553
Ulrich Czekallac2757242000-06-11 20:04:44 +00003554
3555 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3556 {
3557 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3558 goto lend;
3559 }
3560
Rob Shearman272954b2007-01-04 18:23:17 +00003561 if (NETCON_connected(&lpwhr->netConnection))
3562 {
3563 bSuccess = TRUE;
3564 goto lend;
3565 }
3566
Jacek Caban61a1cef2006-10-29 18:51:15 +01003567 lpwhs = lpwhr->lpHttpSession;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003568
Jacek Cabana541a442006-10-29 18:49:39 +01003569 hIC = lpwhs->lpAppInfo;
Jacek Cabanad023172006-01-05 14:37:06 +01003570 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
3571 szaddr, sizeof(szaddr));
Robert Shearmande2666f2005-11-29 10:44:05 +01003572 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3573 INTERNET_STATUS_CONNECTING_TO_SERVER,
Jacek Cabanad023172006-01-05 14:37:06 +01003574 szaddr,
3575 strlen(szaddr)+1);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003576
Robert Shearmane6157dd2005-11-15 18:16:55 +00003577 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
David Hammerton852c7ae2003-06-20 23:26:56 +00003578 SOCK_STREAM, 0))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003579 {
3580 WARN("Socket creation failed\n");
3581 goto lend;
3582 }
3583
David Hammerton852c7ae2003-06-20 23:26:56 +00003584 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
3585 sizeof(lpwhs->socketAddress)))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003586 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003587
Robert Shearman13d371d2005-11-28 11:54:31 +01003588 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
3589 {
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003590 /* Note: we differ from Microsoft's WinINet here. they seem to have
3591 * a bug that causes no status callbacks to be sent when starting
3592 * a tunnel to a proxy server using the CONNECT verb. i believe our
3593 * behaviour to be more correct and to not cause any incompatibilities
3594 * because using a secure connection through a proxy server is a rare
3595 * case that would be hard for anyone to depend on */
3596 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
3597 goto lend;
3598
Robert Shearman13d371d2005-11-28 11:54:31 +01003599 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
3600 {
3601 WARN("Couldn't connect securely to host\n");
3602 goto lend;
3603 }
3604 }
3605
Robert Shearmande2666f2005-11-29 10:44:05 +01003606 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3607 INTERNET_STATUS_CONNECTED_TO_SERVER,
Jacek Cabanad023172006-01-05 14:37:06 +01003608 szaddr, strlen(szaddr)+1);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003609
Ulrich Czekallac2757242000-06-11 20:04:44 +00003610 bSuccess = TRUE;
3611
3612lend:
Aric Stewartff9b9d42002-06-21 23:59:49 +00003613 TRACE("%d <--\n", bSuccess);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003614 return bSuccess;
3615}
3616
3617
3618/***********************************************************************
Alexandre Julliard48243e32004-07-15 18:57:32 +00003619 * HTTP_clear_response_headers (internal)
3620 *
3621 * clear out any old response headers
3622 */
3623static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
3624{
3625 DWORD i;
3626
Alexandre Julliard48243e32004-07-15 18:57:32 +00003627 for( i=0; i<lpwhr->nCustHeaders; i++)
3628 {
3629 if( !lpwhr->pCustHeaders[i].lpszField )
3630 continue;
3631 if( !lpwhr->pCustHeaders[i].lpszValue )
3632 continue;
3633 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
3634 continue;
Robert Shearman7707a762005-03-10 11:14:24 +00003635 HTTP_DeleteCustomHeader( lpwhr, i );
3636 i--;
Alexandre Julliard48243e32004-07-15 18:57:32 +00003637 }
3638}
3639
3640/***********************************************************************
Ulrich Czekallac2757242000-06-11 20:04:44 +00003641 * HTTP_GetResponseHeaders (internal)
3642 *
3643 * Read server response
3644 *
3645 * RETURNS
3646 *
3647 * TRUE on success
3648 * FALSE on error
3649 */
Hans Leidekker09366012008-03-26 11:31:21 +01003650static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003651{
3652 INT cbreaks = 0;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003653 WCHAR buffer[MAX_REPLY_LEN];
Ulrich Czekallac2757242000-06-11 20:04:44 +00003654 DWORD buflen = MAX_REPLY_LEN;
3655 BOOL bSuccess = FALSE;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003656 INT rc = 0;
Robert Shearmandee87512004-07-19 20:09:20 +00003657 static const WCHAR szCrLf[] = {'\r','\n',0};
Hans Leidekker2617fb62008-02-17 20:41:56 +01003658 static const WCHAR szHundred[] = {'1','0','0',0};
Mike McCormacka4e902c2004-03-30 04:36:09 +00003659 char bufferA[MAX_REPLY_LEN];
Alexandre Julliard48243e32004-07-15 18:57:32 +00003660 LPWSTR status_code, status_text;
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00003661 DWORD cchMaxRawHeaders = 1024;
Robert Shearmandee87512004-07-19 20:09:20 +00003662 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00003663 DWORD cchRawHeaders = 0;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003664
Aric Stewartff9b9d42002-06-21 23:59:49 +00003665 TRACE("-->\n");
Ulrich Czekallac2757242000-06-11 20:04:44 +00003666
Alexandre Julliard48243e32004-07-15 18:57:32 +00003667 /* clear old response headers (eg. from a redirect response) */
Hans Leidekker09366012008-03-26 11:31:21 +01003668 if (clear) HTTP_clear_response_headers( lpwhr );
Alexandre Julliard48243e32004-07-15 18:57:32 +00003669
David Hammerton852c7ae2003-06-20 23:26:56 +00003670 if (!NETCON_connected(&lpwhr->netConnection))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003671 goto lend;
3672
Hans Leidekker2617fb62008-02-17 20:41:56 +01003673 do {
3674 /*
3675 * HACK peek at the buffer
3676 */
3677 buflen = MAX_REPLY_LEN;
3678 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003679
Hans Leidekker2617fb62008-02-17 20:41:56 +01003680 /*
3681 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3682 */
3683 memset(buffer, 0, MAX_REPLY_LEN);
3684 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3685 goto lend;
3686 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3687
3688 /* split the version from the status code */
3689 status_code = strchrW( buffer, ' ' );
3690 if( !status_code )
3691 goto lend;
3692 *status_code++=0;
3693
3694 /* split the status code from the status text */
3695 status_text = strchrW( status_code, ' ' );
3696 if( !status_text )
3697 goto lend;
3698 *status_text++=0;
3699
3700 TRACE("version [%s] status code [%s] status text [%s]\n",
3701 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3702
3703 } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
Ulrich Czekallac2757242000-06-11 20:04:44 +00003704
Maarten Lankhorst5132fb42008-02-25 11:05:41 -08003705 /* Add status code */
3706 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3707 HTTP_ADDHDR_FLAG_REPLACE);
3708
3709 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3710 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3711
3712 lpwhr->lpszVersion= WININET_strdupW(buffer);
3713 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3714
3715 /* Restore the spaces */
3716 *(status_code-1) = ' ';
3717 *(status_text-1) = ' ';
3718
Robert Shearmandee87512004-07-19 20:09:20 +00003719 /* regenerate raw headers */
3720 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3721 {
3722 cchMaxRawHeaders *= 2;
3723 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3724 }
3725 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3726 cchRawHeaders += (buflen-1);
3727 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3728 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3729 lpszRawHeaders[cchRawHeaders] = '\0';
3730
Ulrich Czekallac2757242000-06-11 20:04:44 +00003731 /* Parse each response line */
3732 do
3733 {
3734 buflen = MAX_REPLY_LEN;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003735 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
Robert Shearmanb72a6822004-09-23 22:53:50 +00003736 {
3737 LPWSTR * pFieldAndValue;
3738
Francois Gougetda8b3dd2005-01-26 21:09:04 +00003739 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
Mike McCormacka4e902c2004-03-30 04:36:09 +00003740 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
Robert Shearmandee87512004-07-19 20:09:20 +00003741
3742 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3743 {
3744 cchMaxRawHeaders *= 2;
3745 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3746 }
3747 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3748 cchRawHeaders += (buflen-1);
3749 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3750 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3751 lpszRawHeaders[cchRawHeaders] = '\0';
3752
Robert Shearmanb72a6822004-09-23 22:53:50 +00003753 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3754 if (!pFieldAndValue)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003755 break;
3756
Robert Shearmanb72a6822004-09-23 22:53:50 +00003757 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
Aric Stewart1e946d32005-12-13 17:07:41 +01003758 HTTP_ADDREQ_FLAG_ADD );
Robert Shearmanb72a6822004-09-23 22:53:50 +00003759
3760 HTTP_FreeTokens(pFieldAndValue);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003761 }
3762 else
3763 {
3764 cbreaks++;
3765 if (cbreaks >= 2)
3766 break;
3767 }
3768 }while(1);
3769
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00003770 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
Robert Shearmandee87512004-07-19 20:09:20 +00003771 lpwhr->lpszRawHeaders = lpszRawHeaders;
3772 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003773 bSuccess = TRUE;
3774
3775lend:
3776
Aric Stewartff9b9d42002-06-21 23:59:49 +00003777 TRACE("<--\n");
3778 if (bSuccess)
3779 return rc;
3780 else
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003781 return 0;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003782}
3783
3784
Robert Shearmanb72a6822004-09-23 22:53:50 +00003785static void strip_spaces(LPWSTR start)
3786{
3787 LPWSTR str = start;
3788 LPWSTR end;
3789
3790 while (*str == ' ' && *str != '\0')
3791 str++;
3792
3793 if (str != start)
3794 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
3795
3796 end = start + strlenW(start) - 1;
3797 while (end >= start && *end == ' ')
3798 {
3799 *end = '\0';
3800 end--;
3801 }
3802}
3803
3804
Ulrich Czekallac2757242000-06-11 20:04:44 +00003805/***********************************************************************
3806 * HTTP_InterpretHttpHeader (internal)
3807 *
3808 * Parse server response
3809 *
3810 * RETURNS
3811 *
Robert Shearmanb72a6822004-09-23 22:53:50 +00003812 * Pointer to array of field, value, NULL on success.
3813 * NULL on error.
Ulrich Czekallac2757242000-06-11 20:04:44 +00003814 */
Jacek Caban02708c62005-10-26 10:07:58 +00003815static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003816{
Robert Shearmanb72a6822004-09-23 22:53:50 +00003817 LPWSTR * pTokenPair;
3818 LPWSTR pszColon;
3819 INT len;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003820
Robert Shearmanb72a6822004-09-23 22:53:50 +00003821 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003822
Robert Shearmanb72a6822004-09-23 22:53:50 +00003823 pszColon = strchrW(buffer, ':');
3824 /* must have two tokens */
3825 if (!pszColon)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003826 {
Robert Shearmanb72a6822004-09-23 22:53:50 +00003827 HTTP_FreeTokens(pTokenPair);
Robert Shearman7707a762005-03-10 11:14:24 +00003828 if (buffer[0])
3829 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
Robert Shearmanb72a6822004-09-23 22:53:50 +00003830 return NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003831 }
3832
Robert Shearmanb72a6822004-09-23 22:53:50 +00003833 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
3834 if (!pTokenPair[0])
Ulrich Czekallac2757242000-06-11 20:04:44 +00003835 {
Robert Shearmanb72a6822004-09-23 22:53:50 +00003836 HTTP_FreeTokens(pTokenPair);
3837 return NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003838 }
Robert Shearmanb72a6822004-09-23 22:53:50 +00003839 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
3840 pTokenPair[0][pszColon - buffer] = '\0';
Ulrich Czekallac2757242000-06-11 20:04:44 +00003841
Robert Shearmanb72a6822004-09-23 22:53:50 +00003842 /* skip colon */
3843 pszColon++;
3844 len = strlenW(pszColon);
3845 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
3846 if (!pTokenPair[1])
3847 {
3848 HTTP_FreeTokens(pTokenPair);
3849 return NULL;
3850 }
3851 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
3852
3853 strip_spaces(pTokenPair[0]);
3854 strip_spaces(pTokenPair[1]);
3855
3856 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
3857 return pTokenPair;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003858}
3859
Ulrich Czekallac2757242000-06-11 20:04:44 +00003860/***********************************************************************
3861 * HTTP_ProcessHeader (internal)
3862 *
3863 * Stuff header into header tables according to <dwModifier>
3864 *
3865 */
3866
Juan Langa1ab4a72007-11-07 14:45:06 -08003867#define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003868
Jacek Caban02708c62005-10-26 10:07:58 +00003869static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003870{
Mike McCormacka4e902c2004-03-30 04:36:09 +00003871 LPHTTPHEADERW lphttpHdr = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003872 BOOL bSuccess = FALSE;
Aric Stewart1e946d32005-12-13 17:07:41 +01003873 INT index = -1;
Aric Stewart1e946d32005-12-13 17:07:41 +01003874 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003875
Hans Leidekkercd2c4582006-10-05 13:18:56 +02003876 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003877
Aric Stewart1e946d32005-12-13 17:07:41 +01003878 /* REPLACE wins out over ADD */
3879 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3880 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
3881
3882 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
3883 index = -1;
3884 else
3885 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
3886
3887 if (index >= 0)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003888 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003889 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
Aric Stewartff9b9d42002-06-21 23:59:49 +00003890 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003891 return FALSE;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003892 }
Aric Stewart1e946d32005-12-13 17:07:41 +01003893 lphttpHdr = &lpwhr->pCustHeaders[index];
3894 }
3895 else if (value)
3896 {
3897 HTTPHEADERW hdr;
3898
3899 hdr.lpszField = (LPWSTR)field;
3900 hdr.lpszValue = (LPWSTR)value;
3901 hdr.wFlags = hdr.wCount = 0;
3902
3903 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3904 hdr.wFlags |= HDR_ISREQUEST;
3905
3906 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3907 }
Rob Shearman7b002a32007-01-12 19:16:49 -06003908 /* no value to delete */
3909 else return TRUE;
Aric Stewart1e946d32005-12-13 17:07:41 +01003910
3911 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3912 lphttpHdr->wFlags |= HDR_ISREQUEST;
3913 else
3914 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
3915
3916 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3917 {
3918 HTTP_DeleteCustomHeader( lpwhr, index );
3919
3920 if (value)
Aric Stewartff9b9d42002-06-21 23:59:49 +00003921 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00003922 HTTPHEADERW hdr;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003923
Mike McCormacka4e902c2004-03-30 04:36:09 +00003924 hdr.lpszField = (LPWSTR)field;
3925 hdr.lpszValue = (LPWSTR)value;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003926 hdr.wFlags = hdr.wCount = 0;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003927
3928 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3929 hdr.wFlags |= HDR_ISREQUEST;
3930
Mike McCormacka1c16d22003-07-22 03:17:52 +00003931 return HTTP_InsertCustomHeader(lpwhr, &hdr);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003932 }
Aric Stewart1e946d32005-12-13 17:07:41 +01003933
3934 return TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003935 }
Juan Langa1ab4a72007-11-07 14:45:06 -08003936 else if (dwModifier & COALESCEFLAGS)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003937 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003938 LPWSTR lpsztmp;
3939 WCHAR ch = 0;
3940 INT len = 0;
3941 INT origlen = strlenW(lphttpHdr->lpszValue);
3942 INT valuelen = strlenW(value);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003943
Aric Stewart1e946d32005-12-13 17:07:41 +01003944 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003945 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003946 ch = ',';
3947 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3948 }
3949 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3950 {
3951 ch = ';';
3952 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003953 }
3954
Aric Stewart1e946d32005-12-13 17:07:41 +01003955 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
3956
Juan Lang9adf83e2007-11-07 14:58:14 -08003957 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
Aric Stewart1e946d32005-12-13 17:07:41 +01003958 if (lpsztmp)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003959 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003960 lphttpHdr->lpszValue = lpsztmp;
3961 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
3962 if (ch > 0)
3963 {
3964 lphttpHdr->lpszValue[origlen] = ch;
3965 origlen++;
3966 lphttpHdr->lpszValue[origlen] = ' ';
3967 origlen++;
3968 }
3969
3970 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
3971 lphttpHdr->lpszValue[len] = '\0';
Ulrich Czekallac2757242000-06-11 20:04:44 +00003972 bSuccess = TRUE;
3973 }
3974 else
3975 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003976 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003977 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3978 }
3979 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00003980 TRACE("<-- %d\n",bSuccess);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003981 return bSuccess;
3982}
3983
3984
3985/***********************************************************************
Rob Shearman272954b2007-01-04 18:23:17 +00003986 * HTTP_FinishedReading (internal)
3987 *
3988 * Called when all content from server has been read by client.
3989 *
3990 */
3991BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
3992{
Rob Shearmane74514b2008-03-14 18:10:08 +00003993 WCHAR szVersion[10];
3994 DWORD dwBufferSize = sizeof(szVersion);
Rob Shearman272954b2007-01-04 18:23:17 +00003995
3996 TRACE("\n");
3997
Rob Shearmane74514b2008-03-14 18:10:08 +00003998 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
3999 * the connection is keep-alive by default */
4000 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_VERSION, szVersion,
Rob Shearman272954b2007-01-04 18:23:17 +00004001 &dwBufferSize, NULL) ||
Rob Shearmane74514b2008-03-14 18:10:08 +00004002 strcmpiW(szVersion, g_szHttp1_1))
Rob Shearman272954b2007-01-04 18:23:17 +00004003 {
Rob Shearmane74514b2008-03-14 18:10:08 +00004004 WCHAR szConnectionResponse[20];
4005 dwBufferSize = sizeof(szConnectionResponse);
Hans Leidekker454fae82008-03-30 19:16:34 +01004006 if ((!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) ||
4007 strcmpiW(szConnectionResponse, szKeepAlive)) &&
4008 (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) ||
4009 strcmpiW(szConnectionResponse, szKeepAlive)))
Rob Shearmane74514b2008-03-14 18:10:08 +00004010 {
4011 HTTPREQ_CloseConnection(&lpwhr->hdr);
4012 }
Rob Shearman272954b2007-01-04 18:23:17 +00004013 }
4014
4015 /* FIXME: store data in the URL cache here */
4016
4017 return TRUE;
4018}
4019
Ulrich Czekallac2757242000-06-11 20:04:44 +00004020
4021/***********************************************************************
4022 * HTTP_GetCustomHeaderIndex (internal)
4023 *
4024 * Return index of custom header from header array
4025 *
4026 */
Mike McCormack92ddc1c2006-03-30 18:20:04 +09004027static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
4028 int requested_index, BOOL request_only)
Ulrich Czekallac2757242000-06-11 20:04:44 +00004029{
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00004030 DWORD index;
Ulrich Czekallac2757242000-06-11 20:04:44 +00004031
Mike McCormacka4e902c2004-03-30 04:36:09 +00004032 TRACE("%s\n", debugstr_w(lpszField));
Ulrich Czekallac2757242000-06-11 20:04:44 +00004033
4034 for (index = 0; index < lpwhr->nCustHeaders; index++)
4035 {
Mike McCormack92ddc1c2006-03-30 18:20:04 +09004036 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
4037 continue;
4038
4039 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4040 continue;
4041
4042 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
Aric Stewart1e946d32005-12-13 17:07:41 +01004043 continue;
Ulrich Czekallac2757242000-06-11 20:04:44 +00004044
Aric Stewart1e946d32005-12-13 17:07:41 +01004045 if (requested_index == 0)
Mike McCormack92ddc1c2006-03-30 18:20:04 +09004046 break;
4047 requested_index --;
Ulrich Czekallac2757242000-06-11 20:04:44 +00004048 }
4049
4050 if (index >= lpwhr->nCustHeaders)
4051 index = -1;
4052
Hans Leidekkercd2c4582006-10-05 13:18:56 +02004053 TRACE("Return: %d\n", index);
Ulrich Czekallac2757242000-06-11 20:04:44 +00004054 return index;
4055}
4056
4057
4058/***********************************************************************
4059 * HTTP_InsertCustomHeader (internal)
4060 *
4061 * Insert header into array
4062 *
4063 */
Jacek Caban02708c62005-10-26 10:07:58 +00004064static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
Ulrich Czekallac2757242000-06-11 20:04:44 +00004065{
4066 INT count;
Mike McCormacka4e902c2004-03-30 04:36:09 +00004067 LPHTTPHEADERW lph = NULL;
Mike McCormacka1c16d22003-07-22 03:17:52 +00004068 BOOL r = FALSE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00004069
Mike McCormacka4e902c2004-03-30 04:36:09 +00004070 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
Ulrich Czekallac2757242000-06-11 20:04:44 +00004071 count = lpwhr->nCustHeaders + 1;
4072 if (count > 1)
Mike McCormacka4e902c2004-03-30 04:36:09 +00004073 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
Ulrich Czekallac2757242000-06-11 20:04:44 +00004074 else
Mike McCormacka4e902c2004-03-30 04:36:09 +00004075 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
Ulrich Czekallac2757242000-06-11 20:04:44 +00004076
4077 if (NULL != lph)
4078 {
4079 lpwhr->pCustHeaders = lph;
Mike McCormacka4e902c2004-03-30 04:36:09 +00004080 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
4081 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
Ulrich Czekallac2757242000-06-11 20:04:44 +00004082 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
4083 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
4084 lpwhr->nCustHeaders++;
Mike McCormacka1c16d22003-07-22 03:17:52 +00004085 r = TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00004086 }
4087 else
4088 {
4089 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Ulrich Czekallac2757242000-06-11 20:04:44 +00004090 }
4091
Mike McCormacka1c16d22003-07-22 03:17:52 +00004092 return r;
Ulrich Czekallac2757242000-06-11 20:04:44 +00004093}
4094
4095
4096/***********************************************************************
4097 * HTTP_DeleteCustomHeader (internal)
4098 *
4099 * Delete header from array
Mike McCormacka1c16d22003-07-22 03:17:52 +00004100 * If this function is called, the indexs may change.
Ulrich Czekallac2757242000-06-11 20:04:44 +00004101 */
Jacek Caban02708c62005-10-26 10:07:58 +00004102static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
Ulrich Czekallac2757242000-06-11 20:04:44 +00004103{
Mike McCormacka1c16d22003-07-22 03:17:52 +00004104 if( lpwhr->nCustHeaders <= 0 )
4105 return FALSE;
Robert Shearman7707a762005-03-10 11:14:24 +00004106 if( index >= lpwhr->nCustHeaders )
Mike McCormacka1c16d22003-07-22 03:17:52 +00004107 return FALSE;
4108 lpwhr->nCustHeaders--;
4109
4110 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
Mike McCormacka4e902c2004-03-30 04:36:09 +00004111 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
4112 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
Mike McCormacka1c16d22003-07-22 03:17:52 +00004113
4114 return TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00004115}
Alberto Massarib09eef22002-11-13 04:08:26 +00004116
Aric Stewartc8dfc022007-07-26 08:59:00 -05004117
4118/***********************************************************************
4119 * HTTP_VerifyValidHeader (internal)
4120 *
4121 * Verify the given header is not invalid for the given http request
4122 *
4123 */
4124static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
4125{
Aric Stewartc8dfc022007-07-26 08:59:00 -05004126 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
Hans Leidekker3a577112008-03-24 21:31:53 +01004127 if (!strcmpW(lpwhr->lpszVersion, g_szHttp1_0) && !strcmpiW(field, szAccept_Encoding))
Aric Stewartc8dfc022007-07-26 08:59:00 -05004128 return FALSE;
4129
Hans Leidekker3a577112008-03-24 21:31:53 +01004130 return TRUE;
Aric Stewartc8dfc022007-07-26 08:59:00 -05004131}
4132
Alberto Massarib09eef22002-11-13 04:08:26 +00004133/***********************************************************************
4134 * IsHostInProxyBypassList (@)
4135 *
4136 * Undocumented
4137 *
4138 */
4139BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
4140{
Hans Leidekkercd2c4582006-10-05 13:18:56 +02004141 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
Alberto Massarib09eef22002-11-13 04:08:26 +00004142 return FALSE;
4143}