blob: 40070fbe2af7471b4bfcb7dff583623660a55410 [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"
Guy Albertelliaafec982001-11-06 22:31:19 +000058
Ulrich Czekallac2757242000-06-11 20:04:44 +000059#include "internet.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000060#include "wine/debug.h"
Alberto Massarid476a5a2002-11-12 02:13:04 +000061#include "wine/unicode.h"
Ulrich Czekallac2757242000-06-11 20:04:44 +000062
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000063WINE_DEFAULT_DEBUG_CHANNEL(wininet);
Ulrich Czekallac2757242000-06-11 20:04:44 +000064
Hans Leidekkerd0033db2008-02-17 20:41:42 +010065static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
Francois Gouget6a6c85c2004-04-19 20:12:14 +000066static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
67static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
68static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
Aric Stewart1e946d32005-12-13 17:07:41 +010069static const WCHAR szHost[] = { 'H','o','s','t',0 };
Rob Shearman4b507682007-05-21 14:26:26 +010070static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
Aric Stewart1e946d32005-12-13 17:07:41 +010071static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
72static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
Rob Shearman272954b2007-01-04 18:23:17 +000073static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
Jacek Cabanf9791342008-02-13 13:34:05 +010074static const WCHAR szGET[] = { 'G','E','T', 0 };
Mike McCormacka4e902c2004-03-30 04:36:09 +000075
Ulrich Czekallac2757242000-06-11 20:04:44 +000076#define MAXHOSTNAME 100
77#define MAX_FIELD_VALUE_LEN 256
78#define MAX_FIELD_LEN 256
79
Mike McCormacka4e902c2004-03-30 04:36:09 +000080#define HTTP_REFERER g_szReferer
81#define HTTP_ACCEPT g_szAccept
82#define HTTP_USERAGENT g_szUserAgent
Ulrich Czekallac2757242000-06-11 20:04:44 +000083
84#define HTTP_ADDHDR_FLAG_ADD 0x20000000
85#define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
86#define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
87#define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
88#define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
89#define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
90#define HTTP_ADDHDR_FLAG_REQ 0x02000000
91
Rob Shearman4b507682007-05-21 14:26:26 +010092#define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
93
94struct HttpAuthInfo
95{
96 LPWSTR scheme;
97 CredHandle cred;
98 CtxtHandle ctx;
99 TimeStamp exp;
100 ULONG attr;
101 void *auth_data;
102 unsigned int auth_data_len;
103 BOOL finished; /* finished authenticating */
104};
Ulrich Czekallac2757242000-06-11 20:04:44 +0000105
Jacek Caban02708c62005-10-26 10:07:58 +0000106static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
107static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
108static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
Jacek Caban02708c62005-10-26 10:07:58 +0000109static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
110static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
Aric Stewart1e946d32005-12-13 17:07:41 +0100111static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
Jacek Caban02708c62005-10-26 10:07:58 +0000112static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
Aric Stewartbe918f42005-11-21 15:17:55 +0000113static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
Aric Stewart1fc760d2005-11-28 17:31:02 +0100114static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
115 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
116 lpdwIndex);
Rob Shearman4319ec62006-12-07 00:53:27 +0000117static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
Rob Shearman4b507682007-05-21 14:26:26 +0100118static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
Aric Stewartc8dfc022007-07-26 08:59:00 -0500119static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field);
Aric Stewart1e946d32005-12-13 17:07:41 +0100120
121
122LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
123{
124 int HeaderIndex = 0;
125 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
126 if (HeaderIndex == -1)
127 return NULL;
128 else
129 return &req->pCustHeaders[HeaderIndex];
130}
Ulrich Czekallac2757242000-06-11 20:04:44 +0000131
132/***********************************************************************
Robert Shearmandee87512004-07-19 20:09:20 +0000133 * HTTP_Tokenize (internal)
134 *
135 * Tokenize a string, allocating memory for the tokens.
136 */
137static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
138{
139 LPWSTR * token_array;
140 int tokens = 0;
141 int i;
142 LPCWSTR next_token;
143
144 /* empty string has no tokens */
145 if (*string)
146 tokens++;
147 /* count tokens */
148 for (i = 0; string[i]; i++)
149 if (!strncmpW(string+i, token_string, strlenW(token_string)))
150 {
Mike McCormack13b6ce6d2004-08-09 18:54:23 +0000151 DWORD j;
Robert Shearmandee87512004-07-19 20:09:20 +0000152 tokens++;
153 /* we want to skip over separators, but not the null terminator */
154 for (j = 0; j < strlenW(token_string) - 1; j++)
155 if (!string[i+j])
156 break;
157 i += j;
158 }
159
160 /* add 1 for terminating NULL */
161 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
162 token_array[tokens] = NULL;
163 if (!tokens)
164 return token_array;
165 for (i = 0; i < tokens; i++)
166 {
167 int len;
168 next_token = strstrW(string, token_string);
169 if (!next_token) next_token = string+strlenW(string);
170 len = next_token - string;
171 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
172 memcpy(token_array[i], string, len*sizeof(WCHAR));
173 token_array[i][len] = '\0';
174 string = next_token+strlenW(token_string);
175 }
176 return token_array;
177}
178
179/***********************************************************************
180 * HTTP_FreeTokens (internal)
181 *
182 * Frees memory returned from HTTP_Tokenize.
183 */
184static void HTTP_FreeTokens(LPWSTR * token_array)
185{
186 int i;
187 for (i = 0; token_array[i]; i++)
188 HeapFree(GetProcessHeap(), 0, token_array[i]);
189 HeapFree(GetProcessHeap(), 0, token_array);
190}
191
Aric Stewartbe918f42005-11-21 15:17:55 +0000192/* **********************************************************************
193 *
194 * Helper functions for the HttpSendRequest(Ex) functions
195 *
196 */
Jacek Cabanc1568452006-12-25 21:33:36 +0100197static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
198{
199 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
200 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
201
202 TRACE("%p\n", lpwhr);
203
204 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
205 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
206 req->dwContentLength, req->bEndRequest);
207
208 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
209}
210
Aric Stewartbe918f42005-11-21 15:17:55 +0000211static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
212{
213 static const WCHAR szSlash[] = { '/',0 };
214 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
215
216 /* If we don't have a path we set it to root */
217 if (NULL == lpwhr->lpszPath)
218 lpwhr->lpszPath = WININET_strdupW(szSlash);
219 else /* remove \r and \n*/
220 {
221 int nLen = strlenW(lpwhr->lpszPath);
222 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
223 {
224 nLen--;
225 lpwhr->lpszPath[nLen]='\0';
226 }
227 /* Replace '\' with '/' */
228 while (nLen>0) {
229 nLen--;
230 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
231 }
232 }
233
234 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
Juan Langb210e3a2007-11-07 14:34:49 -0800235 lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
Aric Stewartbe918f42005-11-21 15:17:55 +0000236 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
237 {
238 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
239 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
240 *fixurl = '/';
241 strcpyW(fixurl + 1, lpwhr->lpszPath);
242 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
243 lpwhr->lpszPath = fixurl;
244 }
245}
246
Hans Leidekkerd0033db2008-02-17 20:41:42 +0100247static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
Aric Stewartbe918f42005-11-21 15:17:55 +0000248{
249 LPWSTR requestString;
250 DWORD len, n;
251 LPCWSTR *req;
Gerald Pfeifer3f1a20b2008-01-12 20:14:04 +0100252 UINT i;
Aric Stewartbe918f42005-11-21 15:17:55 +0000253 LPWSTR p;
254
255 static const WCHAR szSpace[] = { ' ',0 };
256 static const WCHAR szcrlf[] = {'\r','\n', 0};
257 static const WCHAR szColon[] = { ':',' ',0 };
258 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
259
260 /* allocate space for an array of all the string pointers to be added */
Hans Leidekkerd0033db2008-02-17 20:41:42 +0100261 len = (lpwhr->nCustHeaders)*4 + 10;
Aric Stewartbe918f42005-11-21 15:17:55 +0000262 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
263
Robert Shearman0e7c41e2005-11-28 11:55:16 +0100264 /* add the verb, path and HTTP version string */
Aric Stewartbe918f42005-11-21 15:17:55 +0000265 n = 0;
Robert Shearman0e7c41e2005-11-28 11:55:16 +0100266 req[n++] = verb;
Aric Stewartbe918f42005-11-21 15:17:55 +0000267 req[n++] = szSpace;
Robert Shearman0e7c41e2005-11-28 11:55:16 +0100268 req[n++] = path;
Hans Leidekkerd0033db2008-02-17 20:41:42 +0100269 req[n++] = szSpace;
270 req[n++] = version;
Aric Stewartbe918f42005-11-21 15:17:55 +0000271
Austin English0e4adae2008-01-04 13:37:14 -0600272 /* Append custom request headers */
Aric Stewartbe918f42005-11-21 15:17:55 +0000273 for (i = 0; i < lpwhr->nCustHeaders; i++)
274 {
275 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
276 {
277 req[n++] = szcrlf;
278 req[n++] = lpwhr->pCustHeaders[i].lpszField;
279 req[n++] = szColon;
280 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
281
282 TRACE("Adding custom header %s (%s)\n",
283 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
284 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
285 }
286 }
287
288 if( n >= len )
289 ERR("oops. buffer overrun\n");
290
291 req[n] = NULL;
292 requestString = HTTP_build_req( req, 4 );
293 HeapFree( GetProcessHeap(), 0, req );
294
295 /*
296 * Set (header) termination string for request
297 * Make sure there's exactly two new lines at the end of the request
298 */
299 p = &requestString[strlenW(requestString)-1];
300 while ( (*p == '\n') || (*p == '\r') )
301 p--;
302 strcpyW( p+1, sztwocrlf );
303
304 return requestString;
305}
306
Hans Leidekker97171132008-02-17 21:29:45 +0100307static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr )
Aric Stewartbe918f42005-11-21 15:17:55 +0000308{
Aric Stewart1e946d32005-12-13 17:07:41 +0100309 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
310 int HeaderIndex;
311 LPHTTPHEADERW setCookieHeader;
312
313 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
314 if (HeaderIndex == -1)
315 return;
316 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
Aric Stewartbe918f42005-11-21 15:17:55 +0000317
Robert Shearman225a9fa2005-11-28 11:24:41 +0100318 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
Aric Stewartbe918f42005-11-21 15:17:55 +0000319 {
Aric Stewartbe918f42005-11-21 15:17:55 +0000320 int nPosStart = 0, nPosEnd = 0, len;
321 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
322
Aric Stewartbe918f42005-11-21 15:17:55 +0000323 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
324 {
325 LPWSTR buf_cookie, cookie_name, cookie_data;
326 LPWSTR buf_url;
327 LPWSTR domain = NULL;
Aric Stewart1e946d32005-12-13 17:07:41 +0100328 LPHTTPHEADERW Host;
329
Aric Stewartbe918f42005-11-21 15:17:55 +0000330 int nEqualPos = 0;
331 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
332 setCookieHeader->lpszValue[nPosEnd] != '\0')
333 {
334 nPosEnd++;
335 }
336 if (setCookieHeader->lpszValue[nPosEnd] == ';')
337 {
338 /* fixme: not case sensitive, strcasestr is gnu only */
339 int nDomainPosEnd = 0;
340 int nDomainPosStart = 0, nDomainLength = 0;
341 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
342 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
343 if (lpszDomain)
344 { /* they have specified their own domain, lets use it */
345 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
346 lpszDomain[nDomainPosEnd] != '\0')
347 {
348 nDomainPosEnd++;
349 }
350 nDomainPosStart = strlenW(szDomain);
351 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
352 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
353 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
354 }
355 }
356 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
357 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
358 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
359 TRACE("%s\n", debugstr_w(buf_cookie));
360 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
361 {
362 nEqualPos++;
363 }
364 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
365 {
366 HeapFree(GetProcessHeap(), 0, buf_cookie);
367 break;
368 }
369
370 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
371 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
372 cookie_data = &buf_cookie[nEqualPos + 1];
373
Aric Stewart1e946d32005-12-13 17:07:41 +0100374 Host = HTTP_GetHeader(lpwhr,szHost);
375 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
Aric Stewartbe918f42005-11-21 15:17:55 +0000376 strlenW(lpwhr->lpszPath) + 9;
377 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
Aric Stewart1e946d32005-12-13 17:07:41 +0100378 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
Aric Stewartbe918f42005-11-21 15:17:55 +0000379 InternetSetCookieW(buf_url, cookie_name, cookie_data);
380
381 HeapFree(GetProcessHeap(), 0, buf_url);
382 HeapFree(GetProcessHeap(), 0, buf_cookie);
383 HeapFree(GetProcessHeap(), 0, cookie_name);
384 HeapFree(GetProcessHeap(), 0, domain);
385 nPosStart = nPosEnd;
386 }
387 }
388}
389
Rob Shearman4b507682007-05-21 14:26:26 +0100390static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
391{
392 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
393 return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
394 ((pszAuthValue[ARRAYSIZE(szBasic)] != ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
395}
396
Rob Shearmancb289692007-06-05 19:49:58 +0100397static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
398 struct HttpAuthInfo **ppAuthInfo,
399 LPWSTR domain_and_username, LPWSTR password )
Rob Shearman4b507682007-05-21 14:26:26 +0100400{
401 SECURITY_STATUS sec_status;
Rob Shearmancb289692007-06-05 19:49:58 +0100402 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
Rob Shearman7b948712007-05-26 08:49:12 +0100403 BOOL first = FALSE;
Rob Shearman4b507682007-05-21 14:26:26 +0100404
405 TRACE("%s\n", debugstr_w(pszAuthValue));
406
Rob Shearman847cc512007-05-21 14:28:02 +0100407 if (!domain_and_username) return FALSE;
408
Rob Shearman4b507682007-05-21 14:26:26 +0100409 if (!pAuthInfo)
410 {
411 TimeStamp exp;
412
Rob Shearman7b948712007-05-26 08:49:12 +0100413 first = TRUE;
Rob Shearman4b507682007-05-21 14:26:26 +0100414 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
415 if (!pAuthInfo)
416 return FALSE;
417
Rob Shearman4b507682007-05-21 14:26:26 +0100418 SecInvalidateHandle(&pAuthInfo->cred);
419 SecInvalidateHandle(&pAuthInfo->ctx);
420 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
421 pAuthInfo->attr = 0;
422 pAuthInfo->auth_data = NULL;
423 pAuthInfo->auth_data_len = 0;
424 pAuthInfo->finished = FALSE;
425
Rob Shearman4d1b8b12007-05-25 12:06:11 +0100426 if (is_basic_auth_value(pszAuthValue))
427 {
428 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
429 pAuthInfo->scheme = WININET_strdupW(szBasic);
430 if (!pAuthInfo->scheme)
431 {
432 HeapFree(GetProcessHeap(), 0, pAuthInfo);
433 return FALSE;
434 }
435 }
436 else
Rob Shearman4b507682007-05-21 14:26:26 +0100437 {
438 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
Rob Shearman847cc512007-05-21 14:28:02 +0100439 WCHAR *user = strchrW(domain_and_username, '\\');
440 WCHAR *domain = domain_and_username;
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 Shearman4b507682007-05-21 14:26:26 +0100449 if (user) user++;
450 else
451 {
Rob Shearman847cc512007-05-21 14:28:02 +0100452 user = domain_and_username;
Rob Shearman4b507682007-05-21 14:26:26 +0100453 domain = NULL;
454 }
455 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
456 nt_auth_identity.User = user;
457 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
458 nt_auth_identity.Domain = domain;
459 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
Rob Shearman847cc512007-05-21 14:28:02 +0100460 nt_auth_identity.Password = password;
Rob Shearman4b507682007-05-21 14:26:26 +0100461 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
462
463 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
464
465 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
466 SECPKG_CRED_OUTBOUND, NULL,
467 &nt_auth_identity, NULL,
468 NULL, &pAuthInfo->cred,
469 &exp);
470 if (sec_status != SEC_E_OK)
471 {
472 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
473 debugstr_w(pAuthInfo->scheme), sec_status);
474 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
475 HeapFree(GetProcessHeap(), 0, pAuthInfo);
476 return FALSE;
477 }
478 }
Rob Shearmancb289692007-06-05 19:49:58 +0100479 *ppAuthInfo = pAuthInfo;
Rob Shearman4b507682007-05-21 14:26:26 +0100480 }
481 else if (pAuthInfo->finished)
482 return FALSE;
483
484 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
485 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
486 {
487 ERR("authentication scheme changed from %s to %s\n",
488 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
489 return FALSE;
490 }
491
492 if (is_basic_auth_value(pszAuthValue))
493 {
Rob Shearman847cc512007-05-21 14:28:02 +0100494 int userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
495 int passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
496 char *auth_data;
497
498 TRACE("basic authentication\n");
499
500 /* length includes a nul terminator, which will be re-used for the ':' */
501 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
502 if (!auth_data)
503 return FALSE;
504
505 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
506 auth_data[userlen] = ':';
507 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
508
509 pAuthInfo->auth_data = auth_data;
510 pAuthInfo->auth_data_len = userlen + 1 + passlen;
511 pAuthInfo->finished = TRUE;
512
513 return TRUE;
Rob Shearman4b507682007-05-21 14:26:26 +0100514 }
515 else
516 {
517 LPCWSTR pszAuthData;
518 SecBufferDesc out_desc, in_desc;
519 SecBuffer out, in;
520 unsigned char *buffer;
521 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
522 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
523
524 in.BufferType = SECBUFFER_TOKEN;
525 in.cbBuffer = 0;
526 in.pvBuffer = NULL;
527
528 in_desc.ulVersion = 0;
529 in_desc.cBuffers = 1;
530 in_desc.pBuffers = &in;
531
532 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
533 if (*pszAuthData == ' ')
534 {
535 pszAuthData++;
536 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
537 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
538 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
539 }
540
541 buffer = HeapAlloc(GetProcessHeap(), 0, 0x100);
542
543 out.BufferType = SECBUFFER_TOKEN;
544 out.cbBuffer = 0x100;
545 out.pvBuffer = buffer;
546
547 out_desc.ulVersion = 0;
548 out_desc.cBuffers = 1;
549 out_desc.pBuffers = &out;
550
Rob Shearman7b948712007-05-26 08:49:12 +0100551 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
Rob Shearman5edcf3a2008-01-24 19:30:11 +0000552 first ? NULL : &pAuthInfo->ctx,
553 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
Rob Shearman4b507682007-05-21 14:26:26 +0100554 context_req, 0, SECURITY_NETWORK_DREP,
555 in.pvBuffer ? &in_desc : NULL,
556 0, &pAuthInfo->ctx, &out_desc,
557 &pAuthInfo->attr, &pAuthInfo->exp);
558 if (sec_status == SEC_E_OK)
559 {
560 pAuthInfo->finished = TRUE;
561 pAuthInfo->auth_data = out.pvBuffer;
562 pAuthInfo->auth_data_len = out.cbBuffer;
563 TRACE("sending last auth packet\n");
564 }
565 else if (sec_status == SEC_I_CONTINUE_NEEDED)
566 {
567 pAuthInfo->auth_data = out.pvBuffer;
568 pAuthInfo->auth_data_len = out.cbBuffer;
569 TRACE("sending next auth packet\n");
570 }
571 else
572 {
573 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
574 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
575 return FALSE;
576 }
577 }
578
579 return TRUE;
580}
581
Robert Shearmandee87512004-07-19 20:09:20 +0000582/***********************************************************************
Mike McCormackb288f712004-06-14 17:57:26 +0000583 * HTTP_HttpAddRequestHeadersW (internal)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000584 */
Mike McCormackb288f712004-06-14 17:57:26 +0000585static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
Mike McCormacka4e902c2004-03-30 04:36:09 +0000586 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000587{
Mike McCormacka4e902c2004-03-30 04:36:09 +0000588 LPWSTR lpszStart;
589 LPWSTR lpszEnd;
590 LPWSTR buffer;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000591 BOOL bSuccess = FALSE;
Mike McCormack08c6c692004-08-10 23:41:35 +0000592 DWORD len;
David Hammerton852c7ae2003-06-20 23:26:56 +0000593
Francois Gougetabfa73b2008-02-19 00:18:02 +0100594 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
Mike McCormack08c6c692004-08-10 23:41:35 +0000595
Alexandre Julliard7c1925a02005-09-13 15:00:32 +0000596 if( dwHeaderLength == ~0U )
Mike McCormack08c6c692004-08-10 23:41:35 +0000597 len = strlenW(lpszHeader);
598 else
599 len = dwHeaderLength;
600 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
Peter Berg Larsene732fc02005-03-28 14:17:51 +0000601 lstrcpynW( buffer, lpszHeader, len + 1);
Mike McCormack08c6c692004-08-10 23:41:35 +0000602
Ulrich Czekallac2757242000-06-11 20:04:44 +0000603 lpszStart = buffer;
604
605 do
606 {
Robert Shearmanb72a6822004-09-23 22:53:50 +0000607 LPWSTR * pFieldAndValue;
608
Ulrich Czekallac2757242000-06-11 20:04:44 +0000609 lpszEnd = lpszStart;
610
611 while (*lpszEnd != '\0')
612 {
613 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
614 break;
615 lpszEnd++;
616 }
617
Robert Shearman4cd38b42004-07-13 23:34:28 +0000618 if (*lpszStart == '\0')
Ulrich Czekallac2757242000-06-11 20:04:44 +0000619 break;
620
Robert Shearman4cd38b42004-07-13 23:34:28 +0000621 if (*lpszEnd == '\r')
622 {
623 *lpszEnd = '\0';
624 lpszEnd += 2; /* Jump over \r\n */
625 }
Mike McCormacka4e902c2004-03-30 04:36:09 +0000626 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
Robert Shearmanb72a6822004-09-23 22:53:50 +0000627 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
628 if (pFieldAndValue)
629 {
Aric Stewartc8dfc022007-07-26 08:59:00 -0500630 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
631 if (bSuccess)
632 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
633 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
Robert Shearmanb72a6822004-09-23 22:53:50 +0000634 HTTP_FreeTokens(pFieldAndValue);
635 }
Ulrich Czekallac2757242000-06-11 20:04:44 +0000636
Robert Shearman4cd38b42004-07-13 23:34:28 +0000637 lpszStart = lpszEnd;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000638 } while (bSuccess);
639
640 HeapFree(GetProcessHeap(), 0, buffer);
Mike McCormackb288f712004-06-14 17:57:26 +0000641
642 return bSuccess;
643}
644
645/***********************************************************************
646 * HttpAddRequestHeadersW (WININET.@)
647 *
648 * Adds one or more HTTP header to the request handler
649 *
Francois Gougetabfa73b2008-02-19 00:18:02 +0100650 * NOTE
651 * On Windows if dwHeaderLength includes the trailing '\0', then
652 * HttpAddRequestHeadersW() adds it too. However this results in an
653 * invalid Http header which is rejected by some servers so we probably
654 * don't need to match Windows on that point.
655 *
Mike McCormackb288f712004-06-14 17:57:26 +0000656 * RETURNS
657 * TRUE on success
658 * FALSE on failure
659 *
660 */
661BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
662 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
663{
664 BOOL bSuccess = FALSE;
665 LPWININETHTTPREQW lpwhr;
666
Francois Gougetabfa73b2008-02-19 00:18:02 +0100667 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
Mike McCormackb288f712004-06-14 17:57:26 +0000668
669 if (!lpszHeader)
670 return TRUE;
671
672 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
673 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
674 {
675 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +0000676 goto lend;
Mike McCormackb288f712004-06-14 17:57:26 +0000677 }
678 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
Mike McCormack3a1391b2004-07-19 21:49:39 +0000679lend:
680 if( lpwhr )
681 WININET_Release( &lpwhr->hdr );
Mike McCormackb288f712004-06-14 17:57:26 +0000682
Ulrich Czekallac2757242000-06-11 20:04:44 +0000683 return bSuccess;
684}
685
Chris Morgana8b32162002-09-27 22:05:23 +0000686/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +0000687 * HttpAddRequestHeadersA (WININET.@)
688 *
689 * Adds one or more HTTP header to the request handler
690 *
691 * RETURNS
692 * TRUE on success
693 * FALSE on failure
694 *
695 */
696BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
697 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
698{
699 DWORD len;
700 LPWSTR hdr;
701 BOOL r;
702
Francois Gougetabfa73b2008-02-19 00:18:02 +0100703 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
Mike McCormacka4e902c2004-03-30 04:36:09 +0000704
705 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
706 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
707 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
Alexandre Julliard7c1925a02005-09-13 15:00:32 +0000708 if( dwHeaderLength != ~0U )
Mike McCormacka4e902c2004-03-30 04:36:09 +0000709 dwHeaderLength = len;
710
711 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
712
713 HeapFree( GetProcessHeap(), 0, hdr );
714
715 return r;
716}
717
Rob Shearman272954b2007-01-04 18:23:17 +0000718/* read any content returned by the server so that the connection can be
Austin English0e4adae2008-01-04 13:37:14 -0600719 * reused */
Rob Shearman272954b2007-01-04 18:23:17 +0000720static void HTTP_DrainContent(LPWININETHTTPREQW lpwhr)
721{
722 DWORD bytes_read;
Rob Shearman76507d42007-05-28 11:13:36 +0100723
724 if (!NETCON_connected(&lpwhr->netConnection)) return;
725
726 if (lpwhr->dwContentLength == -1)
727 NETCON_close(&lpwhr->netConnection);
728
Rob Shearman272954b2007-01-04 18:23:17 +0000729 do
730 {
731 char buffer[2048];
Jacek Caban9a5c0462008-03-02 19:53:35 +0100732 if (!INTERNET_ReadFile(&lpwhr->hdr, buffer, sizeof(buffer), &bytes_read, TRUE))
Rob Shearman272954b2007-01-04 18:23:17 +0000733 return;
734 } while (bytes_read);
735}
736
Mike McCormacka4e902c2004-03-30 04:36:09 +0000737/***********************************************************************
Chris Morgana8b32162002-09-27 22:05:23 +0000738 * HttpEndRequestA (WININET.@)
739 *
740 * Ends an HTTP request that was started by HttpSendRequestEx
741 *
742 * RETURNS
743 * TRUE if successful
744 * FALSE on failure
745 *
746 */
Aric Stewartbe918f42005-11-21 15:17:55 +0000747BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
Francois Gougetd4337f22007-08-30 16:21:33 +0200748 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
Chris Morgana8b32162002-09-27 22:05:23 +0000749{
Aric Stewartbe918f42005-11-21 15:17:55 +0000750 LPINTERNET_BUFFERSA ptr;
751 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
752 BOOL rc = FALSE;
753
Francois Gougetd4337f22007-08-30 16:21:33 +0200754 TRACE("(%p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
Aric Stewartbe918f42005-11-21 15:17:55 +0000755 dwContext);
756
757 ptr = lpBuffersOut;
758 if (ptr)
759 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
760 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
761 else
762 lpBuffersOutW = NULL;
763
764 ptrW = lpBuffersOutW;
765 while (ptr)
766 {
767 if (ptr->lpvBuffer && ptr->dwBufferLength)
768 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
769 ptrW->dwBufferLength = ptr->dwBufferLength;
770 ptrW->dwBufferTotal= ptr->dwBufferTotal;
771
772 if (ptr->Next)
773 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
774 sizeof(INTERNET_BUFFERSW));
775
776 ptr = ptr->Next;
777 ptrW = ptrW->Next;
778 }
779
780 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
781
782 if (lpBuffersOutW)
783 {
784 ptrW = lpBuffersOutW;
785 while (ptrW)
786 {
787 LPINTERNET_BUFFERSW ptrW2;
788
789 FIXME("Do we need to translate info out of these buffer?\n");
790
Andrew Talbot45481db2008-02-04 22:07:57 +0000791 HeapFree(GetProcessHeap(),0,ptrW->lpvBuffer);
Aric Stewartbe918f42005-11-21 15:17:55 +0000792 ptrW2 = ptrW->Next;
793 HeapFree(GetProcessHeap(),0,ptrW);
794 ptrW = ptrW2;
795 }
796 }
797
798 return rc;
Chris Morgana8b32162002-09-27 22:05:23 +0000799}
800
801/***********************************************************************
802 * HttpEndRequestW (WININET.@)
803 *
804 * Ends an HTTP request that was started by HttpSendRequestEx
805 *
806 * RETURNS
807 * TRUE if successful
808 * FALSE on failure
809 *
810 */
Aric Stewartbe918f42005-11-21 15:17:55 +0000811BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
Francois Gougetd4337f22007-08-30 16:21:33 +0200812 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
Chris Morgana8b32162002-09-27 22:05:23 +0000813{
Aric Stewartbe918f42005-11-21 15:17:55 +0000814 BOOL rc = FALSE;
815 LPWININETHTTPREQW lpwhr;
816 INT responseLen;
Rob Shearmanac1b5272007-01-04 18:21:49 +0000817 DWORD dwBufferSize;
Aric Stewartbe918f42005-11-21 15:17:55 +0000818
819 TRACE("-->\n");
820 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
821
822 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
823 {
824 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Nigel Liang7d2ac202007-10-17 17:07:41 -0700825 if (lpwhr)
826 WININET_Release( &lpwhr->hdr );
Aric Stewartbe918f42005-11-21 15:17:55 +0000827 return FALSE;
828 }
829
830 lpwhr->hdr.dwFlags |= dwFlags;
831 lpwhr->hdr.dwContext = dwContext;
832
Rob Shearmanac1b5272007-01-04 18:21:49 +0000833 /* We appear to do nothing with lpBuffersOut.. is that correct? */
834
Aric Stewartbe918f42005-11-21 15:17:55 +0000835 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
836 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
837
838 responseLen = HTTP_GetResponseHeaders(lpwhr);
839 if (responseLen)
840 rc = TRUE;
841
842 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
843 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
844
Hans Leidekker97171132008-02-17 21:29:45 +0100845 /* process cookies here. Is this right? */
846 HTTP_ProcessCookies(lpwhr);
Aric Stewartbe918f42005-11-21 15:17:55 +0000847
Rob Shearmanac1b5272007-01-04 18:21:49 +0000848 dwBufferSize = sizeof(lpwhr->dwContentLength);
849 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
850 &lpwhr->dwContentLength,&dwBufferSize,NULL))
851 lpwhr->dwContentLength = -1;
Aric Stewartbe918f42005-11-21 15:17:55 +0000852
Rob Shearman272954b2007-01-04 18:23:17 +0000853 if (lpwhr->dwContentLength == 0)
854 HTTP_FinishedReading(lpwhr);
855
Aric Stewart1fc760d2005-11-28 17:31:02 +0100856 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
857 {
Rob Shearmanac1b5272007-01-04 18:21:49 +0000858 DWORD dwCode,dwCodeLength=sizeof(DWORD);
859 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
Aric Stewart1fc760d2005-11-28 17:31:02 +0100860 (dwCode==302 || dwCode==301))
861 {
862 WCHAR szNewLocation[2048];
Rob Shearman9efe0832007-01-12 19:19:18 -0600863 dwBufferSize=sizeof(szNewLocation);
Rob Shearmanac1b5272007-01-04 18:21:49 +0000864 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
Aric Stewart1fc760d2005-11-28 17:31:02 +0100865 {
Aric Stewart1fc760d2005-11-28 17:31:02 +0100866 /* redirects are always GETs */
867 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
Jacek Cabanf9791342008-02-13 13:34:05 +0100868 lpwhr->lpszVerb = WININET_strdupW(szGET);
Rob Shearman272954b2007-01-04 18:23:17 +0000869 HTTP_DrainContent(lpwhr);
Rob Shearman4319ec62006-12-07 00:53:27 +0000870 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
871 if (rc)
872 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
Aric Stewart1fc760d2005-11-28 17:31:02 +0100873 }
874 }
875 }
876
Nigel Liang7d2ac202007-10-17 17:07:41 -0700877 WININET_Release( &lpwhr->hdr );
Aric Stewartbe918f42005-11-21 15:17:55 +0000878 TRACE("%i <--\n",rc);
879 return rc;
Chris Morgana8b32162002-09-27 22:05:23 +0000880}
Ulrich Czekallac2757242000-06-11 20:04:44 +0000881
882/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +0000883 * HttpOpenRequestW (WININET.@)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000884 *
885 * Open a HTTP request handle
886 *
887 * RETURNS
888 * HINTERNET a HTTP request handle on success
889 * NULL on failure
890 *
891 */
Mike McCormacka4e902c2004-03-30 04:36:09 +0000892HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
893 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
894 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
Francois Gougetd4337f22007-08-30 16:21:33 +0200895 DWORD dwFlags, DWORD_PTR dwContext)
Ulrich Czekallac2757242000-06-11 20:04:44 +0000896{
Mike McCormacka4e902c2004-03-30 04:36:09 +0000897 LPWININETHTTPSESSIONW lpwhs;
Mike McCormack7cc70c02004-02-07 01:03:41 +0000898 HINTERNET handle = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000899
Francois Gougetd4337f22007-08-30 16:21:33 +0200900 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
Mike McCormacka4e902c2004-03-30 04:36:09 +0000901 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
902 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
David Hammerton852c7ae2003-06-20 23:26:56 +0000903 dwFlags, dwContext);
Alberto Massaribc8bd722002-12-06 23:20:31 +0000904 if(lpszAcceptTypes!=NULL)
905 {
906 int i;
907 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
Mike McCormacka4e902c2004-03-30 04:36:09 +0000908 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
Alberto Massaribc8bd722002-12-06 23:20:31 +0000909 }
Ulrich Czekallac2757242000-06-11 20:04:44 +0000910
Mike McCormacka4e902c2004-03-30 04:36:09 +0000911 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
Ulrich Czekallac2757242000-06-11 20:04:44 +0000912 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
913 {
914 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +0000915 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000916 }
Ulrich Czekallac2757242000-06-11 20:04:44 +0000917
Aric Stewartff9b9d42002-06-21 23:59:49 +0000918 /*
919 * My tests seem to show that the windows version does not
920 * become asynchronous until after this point. And anyhow
921 * if this call was asynchronous then how would you get the
922 * necessary HINTERNET pointer returned by this function.
923 *
Aric Stewartff9b9d42002-06-21 23:59:49 +0000924 */
Mike McCormack3a1391b2004-07-19 21:49:39 +0000925 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
926 lpszVersion, lpszReferrer, lpszAcceptTypes,
927 dwFlags, dwContext);
928lend:
929 if( lpwhs )
930 WININET_Release( &lpwhs->hdr );
Mike McCormack7cc70c02004-02-07 01:03:41 +0000931 TRACE("returning %p\n", handle);
932 return handle;
Ulrich Czekallac2757242000-06-11 20:04:44 +0000933}
934
David Hammerton852c7ae2003-06-20 23:26:56 +0000935
Alberto Massarid476a5a2002-11-12 02:13:04 +0000936/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +0000937 * HttpOpenRequestA (WININET.@)
Alberto Massarid476a5a2002-11-12 02:13:04 +0000938 *
939 * Open a HTTP request handle
940 *
941 * RETURNS
942 * HINTERNET a HTTP request handle on success
943 * NULL on failure
944 *
945 */
Mike McCormacka4e902c2004-03-30 04:36:09 +0000946HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
947 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
948 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
Francois Gougetd4337f22007-08-30 16:21:33 +0200949 DWORD dwFlags, DWORD_PTR dwContext)
Alberto Massarid476a5a2002-11-12 02:13:04 +0000950{
Mike McCormacka4e902c2004-03-30 04:36:09 +0000951 LPWSTR szVerb = NULL, szObjectName = NULL;
952 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
David Hammerton852c7ae2003-06-20 23:26:56 +0000953 INT len;
954 INT acceptTypesCount;
955 HINTERNET rc = FALSE;
Francois Gougetd4337f22007-08-30 16:21:33 +0200956 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
Mike McCormacka4e902c2004-03-30 04:36:09 +0000957 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
958 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
David Hammerton852c7ae2003-06-20 23:26:56 +0000959 dwFlags, dwContext);
Alberto Massaribc8bd722002-12-06 23:20:31 +0000960
David Hammerton852c7ae2003-06-20 23:26:56 +0000961 if (lpszVerb)
962 {
Mike McCormacka4e902c2004-03-30 04:36:09 +0000963 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
964 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
Mike McCormack43629c92003-08-15 03:47:30 +0000965 if ( !szVerb )
David Hammerton852c7ae2003-06-20 23:26:56 +0000966 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +0000967 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
David Hammerton852c7ae2003-06-20 23:26:56 +0000968 }
969
970 if (lpszObjectName)
971 {
Mike McCormacka4e902c2004-03-30 04:36:09 +0000972 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
973 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
Mike McCormack43629c92003-08-15 03:47:30 +0000974 if ( !szObjectName )
David Hammerton852c7ae2003-06-20 23:26:56 +0000975 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +0000976 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
David Hammerton852c7ae2003-06-20 23:26:56 +0000977 }
978
979 if (lpszVersion)
980 {
Mike McCormacka4e902c2004-03-30 04:36:09 +0000981 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
982 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
Mike McCormack43629c92003-08-15 03:47:30 +0000983 if ( !szVersion )
David Hammerton852c7ae2003-06-20 23:26:56 +0000984 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +0000985 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
David Hammerton852c7ae2003-06-20 23:26:56 +0000986 }
987
988 if (lpszReferrer)
989 {
Mike McCormacka4e902c2004-03-30 04:36:09 +0000990 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
991 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
Mike McCormack43629c92003-08-15 03:47:30 +0000992 if ( !szReferrer )
David Hammerton852c7ae2003-06-20 23:26:56 +0000993 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +0000994 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
David Hammerton852c7ae2003-06-20 23:26:56 +0000995 }
996
997 acceptTypesCount = 0;
998 if (lpszAcceptTypes)
999 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00001000 /* find out how many there are */
Hans Leidekker2024f682007-02-12 15:19:17 +01001001 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
Mike McCormacka4e902c2004-03-30 04:36:09 +00001002 acceptTypesCount++;
1003 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
David Hammerton852c7ae2003-06-20 23:26:56 +00001004 acceptTypesCount = 0;
Hans Leidekker2024f682007-02-12 15:19:17 +01001005 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
David Hammerton852c7ae2003-06-20 23:26:56 +00001006 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00001007 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1008 -1, NULL, 0 );
1009 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
Mike McCormack43629c92003-08-15 03:47:30 +00001010 if (!szAcceptTypes[acceptTypesCount] )
David Hammerton852c7ae2003-06-20 23:26:56 +00001011 goto end;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001012 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1013 -1, szAcceptTypes[acceptTypesCount], len );
David Hammerton852c7ae2003-06-20 23:26:56 +00001014 acceptTypesCount++;
1015 }
Mike McCormacka4e902c2004-03-30 04:36:09 +00001016 szAcceptTypes[acceptTypesCount] = NULL;
David Hammerton852c7ae2003-06-20 23:26:56 +00001017 }
1018 else szAcceptTypes = 0;
1019
Mike McCormacka4e902c2004-03-30 04:36:09 +00001020 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1021 szVersion, szReferrer,
1022 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
David Hammerton852c7ae2003-06-20 23:26:56 +00001023
1024end:
1025 if (szAcceptTypes)
1026 {
1027 acceptTypesCount = 0;
1028 while (szAcceptTypes[acceptTypesCount])
1029 {
1030 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1031 acceptTypesCount++;
1032 }
1033 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1034 }
Michael Stefaniuc7cb43c92004-12-21 14:42:35 +00001035 HeapFree(GetProcessHeap(), 0, szReferrer);
1036 HeapFree(GetProcessHeap(), 0, szVersion);
1037 HeapFree(GetProcessHeap(), 0, szObjectName);
1038 HeapFree(GetProcessHeap(), 0, szVerb);
David Hammerton852c7ae2003-06-20 23:26:56 +00001039
1040 return rc;
Alberto Massarid476a5a2002-11-12 02:13:04 +00001041}
Ulrich Czekallac2757242000-06-11 20:04:44 +00001042
1043/***********************************************************************
Rob Shearmana9ebc702007-01-12 19:17:20 -06001044 * HTTP_EncodeBase64
Mike McCormacka1c16d22003-07-22 03:17:52 +00001045 */
Rob Shearmana9ebc702007-01-12 19:17:20 -06001046static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001047{
1048 UINT n = 0, x;
Andrew Talbot46fc9c22007-02-24 21:55:12 +00001049 static const CHAR HTTP_Base64Enc[] =
Mike McCormacka1c16d22003-07-22 03:17:52 +00001050 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1051
Rob Shearmana9ebc702007-01-12 19:17:20 -06001052 while( len > 0 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001053 {
1054 /* first 6 bits, all from bin[0] */
1055 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1056 x = (bin[0] & 3) << 4;
1057
1058 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
Rob Shearmana9ebc702007-01-12 19:17:20 -06001059 if( len == 1 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001060 {
1061 base64[n++] = HTTP_Base64Enc[x];
1062 base64[n++] = '=';
1063 base64[n++] = '=';
1064 break;
1065 }
1066 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1067 x = ( bin[1] & 0x0f ) << 2;
1068
1069 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
Rob Shearmana9ebc702007-01-12 19:17:20 -06001070 if( len == 2 )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001071 {
1072 base64[n++] = HTTP_Base64Enc[x];
1073 base64[n++] = '=';
1074 break;
1075 }
1076 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1077
1078 /* last 6 bits, all from bin [2] */
1079 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1080 bin += 3;
Rob Shearmana9ebc702007-01-12 19:17:20 -06001081 len -= 3;
Mike McCormacka1c16d22003-07-22 03:17:52 +00001082 }
1083 base64[n] = 0;
1084 return n;
1085}
1086
Rob Shearman4b507682007-05-21 14:26:26 +01001087#define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1088 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1089 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1090 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1091static const signed char HTTP_Base64Dec[256] =
1092{
1093 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1094 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1095 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1096 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1097 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1098 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1099 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1100 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1101 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1102 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1103 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1104 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1105 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1106 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1107 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1108 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1109 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1110 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1111 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1112 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1113 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1114 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1115 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1116 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1117 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1118 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1119};
1120#undef CH
1121
1122/***********************************************************************
1123 * HTTP_DecodeBase64
1124 */
1125static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1126{
1127 unsigned int n = 0;
1128
1129 while(*base64)
1130 {
1131 signed char in[4];
1132
Rob Shearmanf8f9dbb2008-02-15 10:06:32 +00001133 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
Rob Shearman4b507682007-05-21 14:26:26 +01001134 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
Rob Shearmanf8f9dbb2008-02-15 10:06:32 +00001135 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
Rob Shearman4b507682007-05-21 14:26:26 +01001136 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1137 {
1138 WARN("invalid base64: %s\n", debugstr_w(base64));
1139 return 0;
1140 }
1141 if (bin)
1142 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1143 n++;
1144
1145 if ((base64[2] == '=') && (base64[3] == '='))
1146 break;
1147 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1148 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1149 {
1150 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1151 return 0;
1152 }
1153 if (bin)
1154 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1155 n++;
1156
1157 if (base64[3] == '=')
1158 break;
1159 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1160 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1161 {
1162 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1163 return 0;
1164 }
1165 if (bin)
1166 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1167 n++;
1168
1169 base64 += 4;
1170 }
1171
1172 return n;
1173}
1174
Mike McCormacka1c16d22003-07-22 03:17:52 +00001175/***********************************************************************
Rob Shearmancb289692007-06-05 19:49:58 +01001176 * HTTP_InsertAuthorizationForHeader
Rob Shearman4b507682007-05-21 14:26:26 +01001177 *
1178 * Insert or delete the authorization field in the request header.
1179 */
Hans Leidekkerb069ef42008-02-01 14:40:15 +01001180static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR header, BOOL first )
Rob Shearman4b507682007-05-21 14:26:26 +01001181{
1182 WCHAR *authorization = NULL;
Hans Leidekkerb069ef42008-02-01 14:40:15 +01001183 struct HttpAuthInfo *pAuthInfo = lpwhr->pAuthInfo;
1184 DWORD flags;
Rob Shearman4b507682007-05-21 14:26:26 +01001185
Rob Shearmancb289692007-06-05 19:49:58 +01001186 if (pAuthInfo && pAuthInfo->auth_data_len)
Rob Shearman4b507682007-05-21 14:26:26 +01001187 {
1188 static const WCHAR wszSpace[] = {' ',0};
Rob Shearman01826e02007-11-27 14:19:50 +00001189 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
Rob Shearman4b507682007-05-21 14:26:26 +01001190 unsigned int len;
1191
1192 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
Rob Shearmancb289692007-06-05 19:49:58 +01001193 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
Rob Shearman4b507682007-05-21 14:26:26 +01001194 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1195 if (!authorization)
1196 return FALSE;
1197
Rob Shearmancb289692007-06-05 19:49:58 +01001198 strcpyW(authorization, pAuthInfo->scheme);
Rob Shearman4b507682007-05-21 14:26:26 +01001199 strcatW(authorization, wszSpace);
Rob Shearmancb289692007-06-05 19:49:58 +01001200 HTTP_EncodeBase64(pAuthInfo->auth_data,
1201 pAuthInfo->auth_data_len,
Rob Shearman4b507682007-05-21 14:26:26 +01001202 authorization+strlenW(authorization));
1203
1204 /* clear the data as it isn't valid now that it has been sent to the
Rob Shearman01826e02007-11-27 14:19:50 +00001205 * server, unless it's Basic authentication which doesn't do
1206 * connection tracking */
1207 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1208 {
1209 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1210 pAuthInfo->auth_data = NULL;
1211 pAuthInfo->auth_data_len = 0;
1212 }
Rob Shearman4b507682007-05-21 14:26:26 +01001213 }
1214
1215 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1216
Hans Leidekkerb069ef42008-02-01 14:40:15 +01001217 /* make sure not to overwrite any caller supplied authorization header */
1218 flags = HTTP_ADDHDR_FLAG_REQ;
1219 flags |= first ? HTTP_ADDHDR_FLAG_ADD_IF_NEW : HTTP_ADDHDR_FLAG_REPLACE;
1220
1221 HTTP_ProcessHeader(lpwhr, header, authorization, flags);
Rob Shearman4b507682007-05-21 14:26:26 +01001222
1223 HeapFree(GetProcessHeap(), 0, authorization);
Rob Shearman4b507682007-05-21 14:26:26 +01001224 return TRUE;
1225}
1226
1227/***********************************************************************
Mike McCormacka1c16d22003-07-22 03:17:52 +00001228 * HTTP_DealWithProxy
1229 */
Mike McCormack09d2d472004-03-25 05:29:47 +00001230static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
Mike McCormacka4e902c2004-03-30 04:36:09 +00001231 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
Mike McCormacka1c16d22003-07-22 03:17:52 +00001232{
Mike McCormacka4e902c2004-03-30 04:36:09 +00001233 WCHAR buf[MAXHOSTNAME];
1234 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
Francois Gouget8fc374d2004-04-20 02:14:00 +00001235 WCHAR* url;
Hans Leidekker781f3f72006-10-13 15:43:54 +02001236 static WCHAR szNul[] = { 0 };
Mike McCormacka4e902c2004-03-30 04:36:09 +00001237 URL_COMPONENTSW UrlComponents;
Francois Gouget8fc374d2004-04-20 02:14:00 +00001238 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
Mike McCormacka9b405c2004-06-01 20:19:56 +00001239 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
Francois Gouget8fc374d2004-04-20 02:14:00 +00001240 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
Mike McCormacka4e902c2004-03-30 04:36:09 +00001241 int len;
Mike McCormacka1c16d22003-07-22 03:17:52 +00001242
1243 memset( &UrlComponents, 0, sizeof UrlComponents );
1244 UrlComponents.dwStructSize = sizeof UrlComponents;
1245 UrlComponents.lpszHostName = buf;
1246 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1247
Mike McCormacka4e902c2004-03-30 04:36:09 +00001248 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
Robert Shearmancc44f3a2005-11-28 11:31:11 +01001249 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
Mike McCormacka4e902c2004-03-30 04:36:09 +00001250 sprintfW(proxy, szFormat1, hIC->lpszProxy);
Uwe Bonnes599c4522003-12-15 19:47:31 +00001251 else
Robert Shearmancc44f3a2005-11-28 11:31:11 +01001252 strcpyW(proxy, hIC->lpszProxy);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001253 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
Mike McCormacka1c16d22003-07-22 03:17:52 +00001254 return FALSE;
1255 if( UrlComponents.dwHostNameLength == 0 )
1256 return FALSE;
1257
1258 if( !lpwhr->lpszPath )
Hans Leidekker781f3f72006-10-13 15:43:54 +02001259 lpwhr->lpszPath = szNul;
Francois Gougetaab5e582007-01-18 11:40:15 +01001260 TRACE("server=%s path=%s\n",
Robert Shearman7bb34632005-11-28 10:40:42 +01001261 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
Mike McCormacka1c16d22003-07-22 03:17:52 +00001262 /* for constant 15 see above */
Robert Shearman7bb34632005-11-28 10:40:42 +01001263 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001264 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
Mike McCormacka1c16d22003-07-22 03:17:52 +00001265
1266 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1267 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1268
Robert Shearman35921102005-11-28 11:53:05 +01001269 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001270
Mike McCormacka1c16d22003-07-22 03:17:52 +00001271 if( lpwhr->lpszPath[0] != '/' )
Mike McCormacka4e902c2004-03-30 04:36:09 +00001272 strcatW( url, szSlash );
1273 strcatW(url, lpwhr->lpszPath);
Mike McCormacka1c16d22003-07-22 03:17:52 +00001274 if(lpwhr->lpszPath != szNul)
1275 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1276 lpwhr->lpszPath = url;
Robert Shearman7bb34632005-11-28 10:40:42 +01001277
1278 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001279 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
Mike McCormacka1c16d22003-07-22 03:17:52 +00001280 lpwhs->nServerPort = UrlComponents.nPort;
1281
1282 return TRUE;
1283}
1284
Rob Shearman72575a02006-12-07 00:52:50 +00001285static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1286{
1287 char szaddr[32];
1288 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1289
1290 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1291 INTERNET_STATUS_RESOLVING_NAME,
1292 lpwhs->lpszServerName,
1293 strlenW(lpwhs->lpszServerName)+1);
1294
1295 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1296 &lpwhs->socketAddress))
1297 {
1298 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1299 return FALSE;
1300 }
1301
1302 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1303 szaddr, sizeof(szaddr));
1304 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1305 INTERNET_STATUS_NAME_RESOLVED,
1306 szaddr, strlen(szaddr)+1);
1307 return TRUE;
1308}
1309
Jacek Caban5a535d62008-02-26 20:20:41 +01001310
1311/***********************************************************************
1312 * HTTPREQ_Destroy (internal)
1313 *
1314 * Deallocate request handle
1315 *
1316 */
1317static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1318{
1319 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1320 DWORD i;
1321
1322 TRACE("\n");
1323
1324 if(lpwhr->hCacheFile)
1325 CloseHandle(lpwhr->hCacheFile);
1326
1327 if(lpwhr->lpszCacheFile) {
1328 DeleteFileW(lpwhr->lpszCacheFile); /* FIXME */
1329 HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1330 }
1331
1332 WININET_Release(&lpwhr->lpHttpSession->hdr);
1333
1334 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1335 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1336 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1337 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1338 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1339
1340 for (i = 0; i < lpwhr->nCustHeaders; i++)
1341 {
1342 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1343 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1344 }
1345
1346 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1347 HeapFree(GetProcessHeap(), 0, lpwhr);
1348}
1349
Jacek Caban7dc9bf62008-02-26 20:21:00 +01001350static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1351{
1352 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1353 LPWININETHTTPSESSIONW lpwhs = NULL;
1354 LPWININETAPPINFOW hIC = NULL;
1355
1356 TRACE("%p\n",lpwhr);
1357
1358 if (!NETCON_connected(&lpwhr->netConnection))
1359 return;
1360
1361 if (lpwhr->pAuthInfo)
1362 {
1363 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1364 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1365
1366 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1367 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1368 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1369 lpwhr->pAuthInfo = NULL;
1370 }
1371 if (lpwhr->pProxyAuthInfo)
1372 {
1373 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1374 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1375
1376 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1377 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1378 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1379 lpwhr->pProxyAuthInfo = NULL;
1380 }
1381
1382 lpwhs = lpwhr->lpHttpSession;
1383 hIC = lpwhs->lpAppInfo;
1384
1385 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1386 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1387
1388 NETCON_close(&lpwhr->netConnection);
1389
1390 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1391 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1392}
1393
Jacek Caban0e33eee2008-02-26 20:22:02 +01001394static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1395{
1396 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1397
1398 switch(option) {
1399 case INTERNET_OPTION_SEND_TIMEOUT:
1400 case INTERNET_OPTION_RECEIVE_TIMEOUT:
1401 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1402
1403 if (size != sizeof(DWORD))
1404 return ERROR_INVALID_PARAMETER;
1405
1406 return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1407 *(DWORD*)buffer);
1408 }
1409
1410 return ERROR_INTERNET_INVALID_OPTION;
1411}
1412
Jacek Caban3b4ca692008-03-02 19:35:11 +01001413DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1414{
1415 int bytes_read;
1416
1417 if(!NETCON_recv(&req->netConnection, buffer, min(size, req->dwContentLength - req->dwContentRead),
1418 sync ? MSG_WAITALL : 0, &bytes_read)) {
1419 if(req->dwContentLength != -1 && req->dwContentRead != req->dwContentLength)
1420 ERR("not all data received %d/%d\n", req->dwContentRead, req->dwContentLength);
1421
1422 /* always returns TRUE, even if the network layer returns an
1423 * error */
1424 *read = 0;
1425 HTTP_FinishedReading(req);
1426 return ERROR_SUCCESS;
1427 }
1428
1429 req->dwContentRead += bytes_read;
1430 *read = bytes_read;
1431
1432 if(req->lpszCacheFile) {
1433 BOOL res;
1434
1435 res = WriteFile(req->hCacheFile, buffer, bytes_read, NULL, NULL);
1436 if(!res)
1437 WARN("WriteFile failed: %u\n", GetLastError());
1438 }
1439
1440 if(!bytes_read && (req->dwContentRead == req->dwContentLength))
1441 HTTP_FinishedReading(req);
1442
1443 return ERROR_SUCCESS;
1444}
1445
1446static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1447{
1448 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1449
1450 return HTTPREQ_Read(req, buffer, size, read, TRUE);
1451}
1452
Jacek Caban1ffcfbc2008-02-26 20:21:34 +01001453static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1454{
1455 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
1456
1457 return NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written);
1458}
1459
Jacek Caban33141842008-02-29 12:57:57 +01001460static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1461{
1462 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1463 INTERNET_ASYNC_RESULT iar;
1464 char buffer[4048];
1465
1466 TRACE("%p\n", workRequest->hdr);
1467
1468 iar.dwResult = NETCON_recv(&req->netConnection, buffer,
1469 min(sizeof(buffer), req->dwContentLength - req->dwContentRead),
1470 MSG_PEEK, (int *)&iar.dwError);
1471
1472 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1473 sizeof(INTERNET_ASYNC_RESULT));
1474}
1475
1476static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1477{
1478 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1479 BYTE buffer[4048];
1480 BOOL async;
1481
1482 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
1483
1484 if(!NETCON_query_data_available(&req->netConnection, available) || *available)
1485 return ERROR_SUCCESS;
1486
1487 /* Even if we are in async mode, we need to determine whether
1488 * there is actually more data available. We do this by trying
1489 * to peek only a single byte in async mode. */
1490 async = (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) != 0;
1491
1492 if (NETCON_recv(&req->netConnection, buffer,
1493 min(async ? 1 : sizeof(buffer), req->dwContentLength - req->dwContentRead),
1494 MSG_PEEK, (int *)available) && async && *available)
1495 {
1496 WORKREQUEST workRequest;
1497
1498 *available = 0;
1499 workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
1500 workRequest.hdr = WININET_AddRef( &req->hdr );
1501
1502 INTERNET_AsyncCall(&workRequest);
1503
1504 return ERROR_IO_PENDING;
1505 }
1506
1507 return ERROR_SUCCESS;
1508}
1509
Jacek Caban5a535d62008-02-26 20:20:41 +01001510static const HANDLEHEADERVtbl HTTPREQVtbl = {
Jacek Caban7dc9bf62008-02-26 20:21:00 +01001511 HTTPREQ_Destroy,
Jacek Caban1ffcfbc2008-02-26 20:21:34 +01001512 HTTPREQ_CloseConnection,
Jacek Caban0e33eee2008-02-26 20:22:02 +01001513 HTTPREQ_SetOption,
Jacek Caban3b4ca692008-03-02 19:35:11 +01001514 HTTPREQ_ReadFile,
Jacek Caban8c45eec2008-02-27 18:55:09 +01001515 HTTPREQ_WriteFile,
Jacek Caban33141842008-02-29 12:57:57 +01001516 HTTPREQ_QueryDataAvailable,
Jacek Caban8c45eec2008-02-27 18:55:09 +01001517 NULL
Jacek Caban5a535d62008-02-26 20:20:41 +01001518};
1519
Mike McCormacka1c16d22003-07-22 03:17:52 +00001520/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +00001521 * HTTP_HttpOpenRequestW (internal)
Ulrich Czekallac2757242000-06-11 20:04:44 +00001522 *
1523 * Open a HTTP request handle
1524 *
1525 * RETURNS
1526 * HINTERNET a HTTP request handle on success
1527 * NULL on failure
1528 *
1529 */
Mike McCormack3a1391b2004-07-19 21:49:39 +00001530HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
Mike McCormacka4e902c2004-03-30 04:36:09 +00001531 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1532 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
Francois Gougetd4337f22007-08-30 16:21:33 +02001533 DWORD dwFlags, DWORD_PTR dwContext)
Ulrich Czekallac2757242000-06-11 20:04:44 +00001534{
Mike McCormack09d2d472004-03-25 05:29:47 +00001535 LPWININETAPPINFOW hIC = NULL;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001536 LPWININETHTTPREQW lpwhr;
1537 LPWSTR lpszCookies;
1538 LPWSTR lpszUrl = NULL;
David Hammerton852c7ae2003-06-20 23:26:56 +00001539 DWORD nCookieSize;
Mike McCormack3a1391b2004-07-19 21:49:39 +00001540 HINTERNET handle = NULL;
Francois Gouget6a6c85c2004-04-19 20:12:14 +00001541 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
Mike McCormacka4e902c2004-03-30 04:36:09 +00001542 DWORD len;
Aric Stewart1e946d32005-12-13 17:07:41 +01001543 LPHTTPHEADERW Host;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001544
Francois Gouget0edbaf72005-11-10 12:14:56 +00001545 TRACE("-->\n");
Ulrich Czekallac2757242000-06-11 20:04:44 +00001546
Mike McCormack3a1391b2004-07-19 21:49:39 +00001547 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
Jacek Cabana541a442006-10-29 18:49:39 +01001548 hIC = lpwhs->lpAppInfo;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001549
Mike McCormacka4e902c2004-03-30 04:36:09 +00001550 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
Ulrich Czekallac2757242000-06-11 20:04:44 +00001551 if (NULL == lpwhr)
1552 {
1553 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Mike McCormack3a1391b2004-07-19 21:49:39 +00001554 goto lend;
Mike McCormack7cc70c02004-02-07 01:03:41 +00001555 }
Mike McCormack3a1391b2004-07-19 21:49:39 +00001556 lpwhr->hdr.htype = WH_HHTTPREQ;
Jacek Caban5a535d62008-02-26 20:20:41 +01001557 lpwhr->hdr.vtbl = &HTTPREQVtbl;
Mike McCormack3a1391b2004-07-19 21:49:39 +00001558 lpwhr->hdr.dwFlags = dwFlags;
1559 lpwhr->hdr.dwContext = dwContext;
1560 lpwhr->hdr.dwRefCount = 1;
Robert Shearmaned517f32004-09-20 19:10:31 +00001561 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
Jacek Caban5c53baa2006-10-30 22:14:59 +01001562 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
Mike McCormack3a1391b2004-07-19 21:49:39 +00001563
Jacek Cabana9bdc012006-10-29 18:50:25 +01001564 WININET_AddRef( &lpwhs->hdr );
1565 lpwhr->lpHttpSession = lpwhs;
Misha Koshelev728e5fa2007-09-20 20:59:40 -05001566 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
Jacek Cabana9bdc012006-10-29 18:50:25 +01001567
Mike McCormack7cc70c02004-02-07 01:03:41 +00001568 handle = WININET_AllocHandle( &lpwhr->hdr );
1569 if (NULL == handle)
1570 {
1571 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Mike McCormack3a1391b2004-07-19 21:49:39 +00001572 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001573 }
1574
Robert Shearman9981f332006-03-06 17:31:09 +00001575 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1576 {
1577 InternetCloseHandle( handle );
1578 handle = NULL;
1579 goto lend;
1580 }
Ulrich Czekallac2757242000-06-11 20:04:44 +00001581
Jacek Cabanf9791342008-02-13 13:34:05 +01001582 if (lpszObjectName && *lpszObjectName) {
Aric Stewartff9b9d42002-06-21 23:59:49 +00001583 HRESULT rc;
Mike McCormacka4e902c2004-03-30 04:36:09 +00001584
1585 len = 0;
1586 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
Aric Stewartff9b9d42002-06-21 23:59:49 +00001587 if (rc != E_POINTER)
Mike McCormacka4e902c2004-03-30 04:36:09 +00001588 len = strlenW(lpszObjectName)+1;
1589 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1590 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
Huw D M Davies0aebee92001-01-21 21:09:00 +00001591 URL_ESCAPE_SPACES_ONLY);
Aric Stewartff9b9d42002-06-21 23:59:49 +00001592 if (rc)
1593 {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02001594 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001595 strcpyW(lpwhr->lpszPath,lpszObjectName);
Aric Stewartff9b9d42002-06-21 23:59:49 +00001596 }
Huw D M Davies0aebee92001-01-21 21:09:00 +00001597 }
Ulrich Czekallac2757242000-06-11 20:04:44 +00001598
Jacek Cabanf9791342008-02-13 13:34:05 +01001599 if (lpszReferrer && *lpszReferrer)
Hans Leidekker4d84eb22008-01-04 13:14:00 +01001600 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
Alberto Massaribc8bd722002-12-06 23:20:31 +00001601
Hans Leidekker2024f682007-02-12 15:19:17 +01001602 if (lpszAcceptTypes)
Alberto Massaribc8bd722002-12-06 23:20:31 +00001603 {
1604 int i;
Hans Leidekker2024f682007-02-12 15:19:17 +01001605 for (i = 0; lpszAcceptTypes[i]; i++)
1606 {
1607 if (!*lpszAcceptTypes[i]) continue;
Rob Shearmana9c2cfa2007-01-07 12:53:50 +00001608 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
Hans Leidekker2024f682007-02-12 15:19:17 +01001609 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
1610 HTTP_ADDHDR_FLAG_REQ |
1611 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
1612 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00001613 }
Ulrich Czekallac2757242000-06-11 20:04:44 +00001614
Jacek Cabanf9791342008-02-13 13:34:05 +01001615 lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001616
Hans Leidekkerd0033db2008-02-17 20:41:42 +01001617 if (lpszVersion)
1618 lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
1619 else
1620 lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
1621
Hans Leidekker4d84eb22008-01-04 13:14:00 +01001622 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
Robert Shearman37f2cc82004-09-13 19:33:17 +00001623
Mike McCormack403e58f2005-10-19 19:07:08 +00001624 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1625 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1626 INTERNET_DEFAULT_HTTPS_PORT :
1627 INTERNET_DEFAULT_HTTP_PORT);
Robert Shearman35921102005-11-28 11:53:05 +01001628 lpwhs->nHostPort = lpwhs->nServerPort;
Mike McCormack403e58f2005-10-19 19:07:08 +00001629
Mike McCormacka1c16d22003-07-22 03:17:52 +00001630 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1631 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
Ulrich Czekallac2757242000-06-11 20:04:44 +00001632
David Hammerton852c7ae2003-06-20 23:26:56 +00001633 if (hIC->lpszAgent)
1634 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00001635 WCHAR *agent_header;
Francois Gouget6a6c85c2004-04-19 20:12:14 +00001636 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 +00001637
Mike McCormacka4e902c2004-03-30 04:36:09 +00001638 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1639 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1640 sprintfW(agent_header, user_agent, hIC->lpszAgent );
Mike McCormack09d2d472004-03-25 05:29:47 +00001641
Mike McCormackb288f712004-06-14 17:57:26 +00001642 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
David Hammerton852c7ae2003-06-20 23:26:56 +00001643 HTTP_ADDREQ_FLAG_ADD);
1644 HeapFree(GetProcessHeap(), 0, agent_header);
1645 }
1646
Aric Stewart1e946d32005-12-13 17:07:41 +01001647 Host = HTTP_GetHeader(lpwhr,szHost);
1648
1649 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
Mike McCormacka4e902c2004-03-30 04:36:09 +00001650 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
Aric Stewart1e946d32005-12-13 17:07:41 +01001651 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
Mike McCormacka4e902c2004-03-30 04:36:09 +00001652
Robert Shearmand6315922004-07-19 21:18:33 +00001653 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1654 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
David Hammerton852c7ae2003-06-20 23:26:56 +00001655 {
1656 int cnt = 0;
Francois Gouget6a6c85c2004-04-19 20:12:14 +00001657 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1658 static const WCHAR szcrlf[] = {'\r','\n',0};
David Hammerton852c7ae2003-06-20 23:26:56 +00001659
Mike McCormacka4e902c2004-03-30 04:36:09 +00001660 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
David Hammerton852c7ae2003-06-20 23:26:56 +00001661
Mike McCormacka4e902c2004-03-30 04:36:09 +00001662 cnt += sprintfW(lpszCookies, szCookie);
1663 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1664 strcatW(lpszCookies, szcrlf);
David Hammerton852c7ae2003-06-20 23:26:56 +00001665
Mike McCormackb288f712004-06-14 17:57:26 +00001666 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
David Hammerton852c7ae2003-06-20 23:26:56 +00001667 HTTP_ADDREQ_FLAG_ADD);
1668 HeapFree(GetProcessHeap(), 0, lpszCookies);
1669 }
1670 HeapFree(GetProcessHeap(), 0, lpszUrl);
1671
1672
Robert Shearmande2666f2005-11-29 10:44:05 +01001673 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1674 INTERNET_STATUS_HANDLE_CREATED, &handle,
1675 sizeof(handle));
Ulrich Czekallac2757242000-06-11 20:04:44 +00001676
Aric Stewartff9b9d42002-06-21 23:59:49 +00001677 /*
1678 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1679 */
1680
Rob Shearman72575a02006-12-07 00:52:50 +00001681 if (!HTTP_ResolveName(lpwhr))
Ulrich Czekallac2757242000-06-11 20:04:44 +00001682 {
Mike McCormack3a1391b2004-07-19 21:49:39 +00001683 InternetCloseHandle( handle );
1684 handle = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001685 }
1686
Mike McCormack3a1391b2004-07-19 21:49:39 +00001687lend:
1688 if( lpwhr )
1689 WININET_Release( &lpwhr->hdr );
1690
1691 TRACE("<-- %p (%p)\n", handle, lpwhr);
Mike McCormack7cc70c02004-02-07 01:03:41 +00001692 return handle;
Ulrich Czekallac2757242000-06-11 20:04:44 +00001693}
1694
Aric Stewart1e946d32005-12-13 17:07:41 +01001695static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1696static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1697static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1698static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1699static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1700static const WCHAR szAge[] = { 'A','g','e',0 };
1701static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
Aric Stewart1e946d32005-12-13 17:07:41 +01001702static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1703static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1704static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1705static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1706static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1707static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1708static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1709static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1710static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1711static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1712static 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 };
1713static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1714static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1715static const WCHAR szDate[] = { 'D','a','t','e',0 };
1716static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1717static const WCHAR szETag[] = { 'E','T','a','g',0 };
1718static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1719static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1720static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1721static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1722static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1723static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1724static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1725static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1726static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1727static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1728static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1729static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1730static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1731static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1732static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1733static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1734static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1735static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1736static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1737static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1738static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1739static 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 };
1740static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1741static const WCHAR szURI[] = { 'U','R','I',0 };
1742static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1743static const WCHAR szVary[] = { 'V','a','r','y',0 };
1744static const WCHAR szVia[] = { 'V','i','a',0 };
1745static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1746static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1747
Mike McCormack7f5e2732006-03-30 18:02:54 +09001748static const LPCWSTR header_lookup[] = {
1749 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
1750 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1751 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1752 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
1753 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1754 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1755 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1756 szAllow, /* HTTP_QUERY_ALLOW = 7 */
1757 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
1758 szDate, /* HTTP_QUERY_DATE = 9 */
1759 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
1760 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
1761 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
1762 szURI, /* HTTP_QUERY_URI = 13 */
1763 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
1764 NULL, /* HTTP_QUERY_COST = 15 */
1765 NULL, /* HTTP_QUERY_LINK = 16 */
1766 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
1767 NULL, /* HTTP_QUERY_VERSION = 18 */
1768 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
1769 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
1770 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
1771 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
1772 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
1773 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
1774 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
1775 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
1776 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
1777 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
1778 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
1779 NULL, /* HTTP_QUERY_FORWARDED = 30 */
1780 NULL, /* HTTP_QUERY_FROM = 31 */
1781 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
1782 szLocation, /* HTTP_QUERY_LOCATION = 33 */
1783 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
1784 szReferer, /* HTTP_QUERY_REFERER = 35 */
1785 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
1786 szServer, /* HTTP_QUERY_SERVER = 37 */
1787 NULL, /* HTTP_TITLE = 38 */
1788 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
1789 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
1790 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
1791 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
1792 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
1793 szCookie, /* HTTP_QUERY_COOKIE = 44 */
1794 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
1795 NULL, /* HTTP_QUERY_REFRESH = 46 */
1796 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
1797 szAge, /* HTTP_QUERY_AGE = 48 */
1798 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
1799 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
1800 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
1801 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
1802 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
1803 szETag, /* HTTP_QUERY_ETAG = 54 */
1804 szHost, /* HTTP_QUERY_HOST = 55 */
1805 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
1806 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
1807 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
1808 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
1809 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
1810 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
1811 szRange, /* HTTP_QUERY_RANGE = 62 */
1812 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
1813 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
1814 szVary, /* HTTP_QUERY_VARY = 65 */
1815 szVia, /* HTTP_QUERY_VIA = 66 */
1816 szWarning, /* HTTP_QUERY_WARNING = 67 */
1817 szExpect, /* HTTP_QUERY_EXPECT = 68 */
1818 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
1819 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
Aric Stewart1e946d32005-12-13 17:07:41 +01001820};
1821
Mike McCormack7f5e2732006-03-30 18:02:54 +09001822#define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
1823
Alexandre Julliard48243e32004-07-15 18:57:32 +00001824/***********************************************************************
Mike McCormackb288f712004-06-14 17:57:26 +00001825 * HTTP_HttpQueryInfoW (internal)
1826 */
Mike McCormack9b95bb52005-06-13 19:05:42 +00001827static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
Mike McCormackb288f712004-06-14 17:57:26 +00001828 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1829{
1830 LPHTTPHEADERW lphttpHdr = NULL;
1831 BOOL bSuccess = FALSE;
Aric Stewart1e946d32005-12-13 17:07:41 +01001832 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
Mike McCormackae300882006-03-30 18:01:48 +09001833 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
1834 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
1835 INT index = -1;
Mike McCormackb288f712004-06-14 17:57:26 +00001836
1837 /* Find requested header structure */
Mike McCormackae300882006-03-30 18:01:48 +09001838 switch (level)
Mike McCormackb288f712004-06-14 17:57:26 +00001839 {
Mike McCormackae300882006-03-30 18:01:48 +09001840 case HTTP_QUERY_CUSTOM:
1841 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
1842 break;
Mike McCormackb288f712004-06-14 17:57:26 +00001843
Mike McCormackae300882006-03-30 18:01:48 +09001844 case HTTP_QUERY_RAW_HEADERS_CRLF:
Robert Shearmandee87512004-07-19 20:09:20 +00001845 {
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07001846 LPWSTR headers;
1847 DWORD len;
1848 BOOL ret;
1849
1850 if (request_only)
Hans Leidekkerd0033db2008-02-17 20:41:42 +01001851 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07001852 else
1853 headers = lpwhr->lpszRawHeaders;
1854
1855 len = strlenW(headers);
Robert Shearmandee87512004-07-19 20:09:20 +00001856 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1857 {
1858 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1859 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07001860 ret = FALSE;
1861 } else
1862 {
1863 memcpy(lpBuffer, headers, (len+1)*sizeof(WCHAR));
1864 *lpdwBufferLength = len * sizeof(WCHAR);
1865
1866 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1867 ret = TRUE;
Robert Shearmandee87512004-07-19 20:09:20 +00001868 }
Robert Shearman4385d302004-07-21 21:17:03 +00001869
Mikołaj Zalewskiab7d1772007-08-15 16:29:58 -07001870 if (request_only)
1871 HeapFree(GetProcessHeap(), 0, headers);
1872 return ret;
Robert Shearmandee87512004-07-19 20:09:20 +00001873 }
Mike McCormackae300882006-03-30 18:01:48 +09001874 case HTTP_QUERY_RAW_HEADERS:
Robert Shearmandee87512004-07-19 20:09:20 +00001875 {
1876 static const WCHAR szCrLf[] = {'\r','\n',0};
1877 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00001878 DWORD i, size = 0;
Robert Shearmandee87512004-07-19 20:09:20 +00001879 LPWSTR pszString = (WCHAR*)lpBuffer;
1880
1881 for (i = 0; ppszRawHeaderLines[i]; i++)
1882 size += strlenW(ppszRawHeaderLines[i]) + 1;
1883
1884 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1885 {
1886 HTTP_FreeTokens(ppszRawHeaderLines);
1887 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1888 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1889 return FALSE;
1890 }
1891
1892 for (i = 0; ppszRawHeaderLines[i]; i++)
1893 {
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00001894 DWORD len = strlenW(ppszRawHeaderLines[i]);
Robert Shearmandee87512004-07-19 20:09:20 +00001895 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1896 pszString += len+1;
1897 }
1898 *pszString = '\0';
1899
1900 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1901
Robert Shearman4385d302004-07-21 21:17:03 +00001902 *lpdwBufferLength = size * sizeof(WCHAR);
Robert Shearmandee87512004-07-19 20:09:20 +00001903 HTTP_FreeTokens(ppszRawHeaderLines);
1904
1905 return TRUE;
1906 }
Mike McCormackae300882006-03-30 18:01:48 +09001907 case HTTP_QUERY_STATUS_TEXT:
Mike McCormack739cd1e2006-05-30 22:55:42 +09001908 if (lpwhr->lpszStatusText)
Aric Stewart1e946d32005-12-13 17:07:41 +01001909 {
1910 DWORD len = strlenW(lpwhr->lpszStatusText);
1911 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1912 {
1913 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1914 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1915 return FALSE;
1916 }
1917 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1918 *lpdwBufferLength = len * sizeof(WCHAR);
1919
1920 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1921
1922 return TRUE;
1923 }
Mike McCormack739cd1e2006-05-30 22:55:42 +09001924 break;
Mike McCormackae300882006-03-30 18:01:48 +09001925 case HTTP_QUERY_VERSION:
Mike McCormack739cd1e2006-05-30 22:55:42 +09001926 if (lpwhr->lpszVersion)
Aric Stewart1e946d32005-12-13 17:07:41 +01001927 {
1928 DWORD len = strlenW(lpwhr->lpszVersion);
1929 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1930 {
1931 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1932 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1933 return FALSE;
1934 }
1935 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1936 *lpdwBufferLength = len * sizeof(WCHAR);
1937
1938 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1939
1940 return TRUE;
1941 }
Mike McCormack739cd1e2006-05-30 22:55:42 +09001942 break;
Mike McCormackae300882006-03-30 18:01:48 +09001943 default:
Mike McCormack7f5e2732006-03-30 18:02:54 +09001944 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
1945
1946 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
1947 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
1948 requested_index,request_only);
Mike McCormackb288f712004-06-14 17:57:26 +00001949 }
1950
Mike McCormackae300882006-03-30 18:01:48 +09001951 if (index >= 0)
1952 lphttpHdr = &lpwhr->pCustHeaders[index];
1953
Austin English0e4adae2008-01-04 13:37:14 -06001954 /* Ensure header satisfies requested attributes */
Mike McCormackae300882006-03-30 18:01:48 +09001955 if (!lphttpHdr ||
1956 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1957 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
Alexandre Julliard48243e32004-07-15 18:57:32 +00001958 {
Francois Gougete66d1cd2007-01-09 12:30:45 +01001959 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
Mike McCormackae300882006-03-30 18:01:48 +09001960 return bSuccess;
Alexandre Julliard48243e32004-07-15 18:57:32 +00001961 }
Mike McCormackb288f712004-06-14 17:57:26 +00001962
Mike McCormackae300882006-03-30 18:01:48 +09001963 if (lpdwIndex)
1964 (*lpdwIndex)++;
1965
Austin English0e4adae2008-01-04 13:37:14 -06001966 /* coalesce value to requested type */
Mike McCormackb288f712004-06-14 17:57:26 +00001967 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1968 {
1969 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1970 bSuccess = TRUE;
1971
1972 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1973 }
1974 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1975 {
1976 time_t tmpTime;
1977 struct tm tmpTM;
1978 SYSTEMTIME *STHook;
1979
1980 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1981
1982 tmpTM = *gmtime(&tmpTime);
1983 STHook = (SYSTEMTIME *) lpBuffer;
1984 if(STHook==NULL)
1985 return bSuccess;
1986
1987 STHook->wDay = tmpTM.tm_mday;
1988 STHook->wHour = tmpTM.tm_hour;
1989 STHook->wMilliseconds = 0;
1990 STHook->wMinute = tmpTM.tm_min;
1991 STHook->wDayOfWeek = tmpTM.tm_wday;
1992 STHook->wMonth = tmpTM.tm_mon + 1;
1993 STHook->wSecond = tmpTM.tm_sec;
1994 STHook->wYear = tmpTM.tm_year;
1995
1996 bSuccess = TRUE;
1997
1998 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1999 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2000 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2001 }
Aric Stewart7bca41a2005-12-08 12:44:45 +01002002 else if (lphttpHdr->lpszValue)
Mike McCormackb288f712004-06-14 17:57:26 +00002003 {
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00002004 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
Mike McCormackb288f712004-06-14 17:57:26 +00002005
2006 if (len > *lpdwBufferLength)
2007 {
2008 *lpdwBufferLength = len;
2009 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2010 return bSuccess;
2011 }
2012
2013 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2014 *lpdwBufferLength = len - sizeof(WCHAR);
2015 bSuccess = TRUE;
2016
Francois Gougetaab5e582007-01-18 11:40:15 +01002017 TRACE(" returning string : %s\n", debugstr_w(lpBuffer));
Mike McCormackb288f712004-06-14 17:57:26 +00002018 }
2019 return bSuccess;
2020}
2021
2022/***********************************************************************
Mike McCormack1baf39f2004-03-30 20:37:49 +00002023 * HttpQueryInfoW (WININET.@)
Ulrich Czekallac2757242000-06-11 20:04:44 +00002024 *
2025 * Queries for information about an HTTP request
2026 *
2027 * RETURNS
2028 * TRUE on success
2029 * FALSE on failure
2030 *
2031 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002032BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
Ulrich Czekallac2757242000-06-11 20:04:44 +00002033 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2034{
Ulrich Czekallac2757242000-06-11 20:04:44 +00002035 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002036 LPWININETHTTPREQW lpwhr;
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00002037
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002038 if (TRACE_ON(wininet)) {
2039#define FE(x) { x, #x }
2040 static const wininet_flag_info query_flags[] = {
2041 FE(HTTP_QUERY_MIME_VERSION),
2042 FE(HTTP_QUERY_CONTENT_TYPE),
2043 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2044 FE(HTTP_QUERY_CONTENT_ID),
2045 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2046 FE(HTTP_QUERY_CONTENT_LENGTH),
2047 FE(HTTP_QUERY_CONTENT_LANGUAGE),
2048 FE(HTTP_QUERY_ALLOW),
2049 FE(HTTP_QUERY_PUBLIC),
2050 FE(HTTP_QUERY_DATE),
2051 FE(HTTP_QUERY_EXPIRES),
2052 FE(HTTP_QUERY_LAST_MODIFIED),
2053 FE(HTTP_QUERY_MESSAGE_ID),
2054 FE(HTTP_QUERY_URI),
2055 FE(HTTP_QUERY_DERIVED_FROM),
2056 FE(HTTP_QUERY_COST),
2057 FE(HTTP_QUERY_LINK),
2058 FE(HTTP_QUERY_PRAGMA),
2059 FE(HTTP_QUERY_VERSION),
2060 FE(HTTP_QUERY_STATUS_CODE),
2061 FE(HTTP_QUERY_STATUS_TEXT),
2062 FE(HTTP_QUERY_RAW_HEADERS),
2063 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2064 FE(HTTP_QUERY_CONNECTION),
2065 FE(HTTP_QUERY_ACCEPT),
2066 FE(HTTP_QUERY_ACCEPT_CHARSET),
2067 FE(HTTP_QUERY_ACCEPT_ENCODING),
2068 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2069 FE(HTTP_QUERY_AUTHORIZATION),
2070 FE(HTTP_QUERY_CONTENT_ENCODING),
2071 FE(HTTP_QUERY_FORWARDED),
2072 FE(HTTP_QUERY_FROM),
2073 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2074 FE(HTTP_QUERY_LOCATION),
2075 FE(HTTP_QUERY_ORIG_URI),
2076 FE(HTTP_QUERY_REFERER),
2077 FE(HTTP_QUERY_RETRY_AFTER),
2078 FE(HTTP_QUERY_SERVER),
2079 FE(HTTP_QUERY_TITLE),
2080 FE(HTTP_QUERY_USER_AGENT),
2081 FE(HTTP_QUERY_WWW_AUTHENTICATE),
2082 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2083 FE(HTTP_QUERY_ACCEPT_RANGES),
Aric Stewart1e946d32005-12-13 17:07:41 +01002084 FE(HTTP_QUERY_SET_COOKIE),
2085 FE(HTTP_QUERY_COOKIE),
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002086 FE(HTTP_QUERY_REQUEST_METHOD),
2087 FE(HTTP_QUERY_REFRESH),
2088 FE(HTTP_QUERY_CONTENT_DISPOSITION),
2089 FE(HTTP_QUERY_AGE),
2090 FE(HTTP_QUERY_CACHE_CONTROL),
2091 FE(HTTP_QUERY_CONTENT_BASE),
2092 FE(HTTP_QUERY_CONTENT_LOCATION),
2093 FE(HTTP_QUERY_CONTENT_MD5),
2094 FE(HTTP_QUERY_CONTENT_RANGE),
2095 FE(HTTP_QUERY_ETAG),
2096 FE(HTTP_QUERY_HOST),
2097 FE(HTTP_QUERY_IF_MATCH),
2098 FE(HTTP_QUERY_IF_NONE_MATCH),
2099 FE(HTTP_QUERY_IF_RANGE),
2100 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2101 FE(HTTP_QUERY_MAX_FORWARDS),
2102 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2103 FE(HTTP_QUERY_RANGE),
2104 FE(HTTP_QUERY_TRANSFER_ENCODING),
2105 FE(HTTP_QUERY_UPGRADE),
2106 FE(HTTP_QUERY_VARY),
2107 FE(HTTP_QUERY_VIA),
2108 FE(HTTP_QUERY_WARNING),
2109 FE(HTTP_QUERY_CUSTOM)
2110 };
2111 static const wininet_flag_info modifier_flags[] = {
2112 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2113 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2114 FE(HTTP_QUERY_FLAG_NUMBER),
2115 FE(HTTP_QUERY_FLAG_COALESCE)
2116 };
2117#undef FE
2118 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2119 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00002120 DWORD i;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002121
Hans Leidekkercd2c4582006-10-05 13:18:56 +02002122 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002123 TRACE(" Attribute:");
2124 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2125 if (query_flags[i].val == info) {
Diego Pettenò869a66a2005-01-07 17:09:39 +00002126 TRACE(" %s", query_flags[i].name);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002127 break;
2128 }
2129 }
2130 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02002131 TRACE(" Unknown (%08x)", info);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002132 }
2133
Diego Pettenò869a66a2005-01-07 17:09:39 +00002134 TRACE(" Modifier:");
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002135 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2136 if (modifier_flags[i].val & info_mod) {
Diego Pettenò869a66a2005-01-07 17:09:39 +00002137 TRACE(" %s", modifier_flags[i].name);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002138 info_mod &= ~ modifier_flags[i].val;
2139 }
2140 }
2141
2142 if (info_mod) {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02002143 TRACE(" Unknown (%08x)", info_mod);
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002144 }
Diego Pettenò869a66a2005-01-07 17:09:39 +00002145 TRACE("\n");
Lionel Ulmer1d5e6b62004-02-09 22:01:49 +00002146 }
2147
Mike McCormacka4e902c2004-03-30 04:36:09 +00002148 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
Ulrich Czekallac2757242000-06-11 20:04:44 +00002149 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2150 {
2151 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002152 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002153 }
2154
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002155 if (lpBuffer == NULL)
2156 *lpdwBufferLength = 0;
Mike McCormackb288f712004-06-14 17:57:26 +00002157 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2158 lpBuffer, lpdwBufferLength, lpdwIndex);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002159
Mike McCormack3a1391b2004-07-19 21:49:39 +00002160lend:
2161 if( lpwhr )
2162 WININET_Release( &lpwhr->hdr );
2163
Ulrich Czekallac2757242000-06-11 20:04:44 +00002164 TRACE("%d <--\n", bSuccess);
2165 return bSuccess;
2166}
2167
Alberto Massarid476a5a2002-11-12 02:13:04 +00002168/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +00002169 * HttpQueryInfoA (WININET.@)
Alberto Massarid476a5a2002-11-12 02:13:04 +00002170 *
2171 * Queries for information about an HTTP request
2172 *
2173 * RETURNS
2174 * TRUE on success
2175 * FALSE on failure
2176 *
2177 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002178BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
Alberto Massarid476a5a2002-11-12 02:13:04 +00002179 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2180{
2181 BOOL result;
Mike McCormack1baf39f2004-03-30 20:37:49 +00002182 DWORD len;
2183 WCHAR* bufferW;
2184
Alberto Massarid476a5a2002-11-12 02:13:04 +00002185 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2186 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2187 {
Mike McCormack1baf39f2004-03-30 20:37:49 +00002188 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2189 lpdwBufferLength, lpdwIndex );
Alberto Massarid476a5a2002-11-12 02:13:04 +00002190 }
Mike McCormack1baf39f2004-03-30 20:37:49 +00002191
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002192 if (lpBuffer)
2193 {
Rob Shearman719cd822008-02-18 19:37:35 +00002194 DWORD alloclen;
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002195 len = (*lpdwBufferLength)*sizeof(WCHAR);
Rob Shearman719cd822008-02-18 19:37:35 +00002196 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2197 {
2198 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2199 if (alloclen < len)
2200 alloclen = len;
2201 }
2202 else
2203 alloclen = len;
2204 bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002205 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2206 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
Rob Shearman719cd822008-02-18 19:37:35 +00002207 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
Mikołaj Zalewski3fa49f02007-08-15 16:55:15 -07002208 } else
2209 {
2210 bufferW = NULL;
2211 len = 0;
2212 }
2213
Mike McCormack1baf39f2004-03-30 20:37:49 +00002214 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2215 &len, lpdwIndex );
2216 if( result )
Alberto Massarid476a5a2002-11-12 02:13:04 +00002217 {
Robert Shearman4385d302004-07-21 21:17:03 +00002218 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
Mike McCormack1baf39f2004-03-30 20:37:49 +00002219 lpBuffer, *lpdwBufferLength, NULL, NULL );
Robert Shearman4385d302004-07-21 21:17:03 +00002220 *lpdwBufferLength = len - 1;
2221
2222 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
Alberto Massarid476a5a2002-11-12 02:13:04 +00002223 }
Robert Shearman907ac442004-07-20 01:21:08 +00002224 else
2225 /* since the strings being returned from HttpQueryInfoW should be
2226 * only ASCII characters, it is reasonable to assume that all of
2227 * the Unicode characters can be reduced to a single byte */
2228 *lpdwBufferLength = len / sizeof(WCHAR);
Robert Shearman4385d302004-07-21 21:17:03 +00002229
Mike McCormack1baf39f2004-03-30 20:37:49 +00002230 HeapFree(GetProcessHeap(), 0, bufferW );
2231
Alberto Massarid476a5a2002-11-12 02:13:04 +00002232 return result;
2233}
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002234
2235/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +00002236 * HttpSendRequestExA (WININET.@)
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002237 *
2238 * Sends the specified request to the HTTP server and allows chunked
Robert Shearman7a2357a2006-02-14 11:34:14 +01002239 * transfers.
2240 *
2241 * RETURNS
2242 * Success: TRUE
2243 * Failure: FALSE, call GetLastError() for more information.
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002244 */
2245BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2246 LPINTERNET_BUFFERSA lpBuffersIn,
2247 LPINTERNET_BUFFERSA lpBuffersOut,
Francois Gougetd4337f22007-08-30 16:21:33 +02002248 DWORD dwFlags, DWORD_PTR dwContext)
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002249{
Robert Shearman43503572005-11-29 11:42:23 +01002250 INTERNET_BUFFERSW BuffersInW;
Aric Stewartbe918f42005-11-21 15:17:55 +00002251 BOOL rc = FALSE;
Robert Shearman43503572005-11-29 11:42:23 +01002252 DWORD headerlen;
Hans Leidekker781f3f72006-10-13 15:43:54 +02002253 LPWSTR header = NULL;
Aric Stewartbe918f42005-11-21 15:17:55 +00002254
Francois Gougetd4337f22007-08-30 16:21:33 +02002255 TRACE("(%p, %p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersIn,
Aric Stewartbe918f42005-11-21 15:17:55 +00002256 lpBuffersOut, dwFlags, dwContext);
2257
Robert Shearman43503572005-11-29 11:42:23 +01002258 if (lpBuffersIn)
Aric Stewartbe918f42005-11-21 15:17:55 +00002259 {
Robert Shearman43503572005-11-29 11:42:23 +01002260 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2261 if (lpBuffersIn->lpcszHeader)
Aric Stewartbe918f42005-11-21 15:17:55 +00002262 {
Robert Shearman43503572005-11-29 11:42:23 +01002263 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2264 lpBuffersIn->dwHeadersLength,0,0);
Hans Leidekker781f3f72006-10-13 15:43:54 +02002265 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2266 if (!(BuffersInW.lpcszHeader = header))
Robert Shearman43503572005-11-29 11:42:23 +01002267 {
Francois Gougete66d1cd2007-01-09 12:30:45 +01002268 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Robert Shearman43503572005-11-29 11:42:23 +01002269 return FALSE;
2270 }
2271 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2272 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
Hans Leidekker781f3f72006-10-13 15:43:54 +02002273 header, headerlen);
Aric Stewartbe918f42005-11-21 15:17:55 +00002274 }
Aric Stewartd30cec72005-12-08 11:54:24 +01002275 else
2276 BuffersInW.lpcszHeader = NULL;
Robert Shearman43503572005-11-29 11:42:23 +01002277 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2278 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2279 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2280 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2281 BuffersInW.Next = NULL;
Aric Stewartbe918f42005-11-21 15:17:55 +00002282 }
2283
Robert Shearman43503572005-11-29 11:42:23 +01002284 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2285
Hans Leidekker781f3f72006-10-13 15:43:54 +02002286 HeapFree(GetProcessHeap(),0,header);
Robert Shearman43503572005-11-29 11:42:23 +01002287
Aric Stewartbe918f42005-11-21 15:17:55 +00002288 return rc;
Huw D M Daviesf9b6d7b2000-10-28 00:30:23 +00002289}
2290
Ulrich Czekallac2757242000-06-11 20:04:44 +00002291/***********************************************************************
Hans Leidekker6a367db2005-02-01 18:50:53 +00002292 * HttpSendRequestExW (WININET.@)
2293 *
2294 * Sends the specified request to the HTTP server and allows chunked
2295 * transfers
Robert Shearman7a2357a2006-02-14 11:34:14 +01002296 *
2297 * RETURNS
2298 * Success: TRUE
2299 * Failure: FALSE, call GetLastError() for more information.
Hans Leidekker6a367db2005-02-01 18:50:53 +00002300 */
2301BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2302 LPINTERNET_BUFFERSW lpBuffersIn,
2303 LPINTERNET_BUFFERSW lpBuffersOut,
Francois Gougetd4337f22007-08-30 16:21:33 +02002304 DWORD dwFlags, DWORD_PTR dwContext)
Hans Leidekker6a367db2005-02-01 18:50:53 +00002305{
Nigel Liang6f446272007-11-03 18:24:49 -07002306 BOOL ret = FALSE;
Aric Stewartbe918f42005-11-21 15:17:55 +00002307 LPWININETHTTPREQW lpwhr;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002308 LPWININETHTTPSESSIONW lpwhs;
2309 LPWININETAPPINFOW hIC;
Aric Stewartbe918f42005-11-21 15:17:55 +00002310
Francois Gougetd4337f22007-08-30 16:21:33 +02002311 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
Aric Stewartbe918f42005-11-21 15:17:55 +00002312 lpBuffersOut, dwFlags, dwContext);
2313
2314 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2315
2316 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2317 {
2318 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Nigel Liang6f446272007-11-03 18:24:49 -07002319 goto lend;
Aric Stewartbe918f42005-11-21 15:17:55 +00002320 }
2321
Jacek Caban61a1cef2006-10-29 18:51:15 +01002322 lpwhs = lpwhr->lpHttpSession;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002323 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
Jacek Cabana541a442006-10-29 18:49:39 +01002324 hIC = lpwhs->lpAppInfo;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002325 assert(hIC->hdr.htype == WH_HINIT);
2326
2327 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
Aric Stewartbe918f42005-11-21 15:17:55 +00002328 {
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002329 WORKREQUEST workRequest;
2330 struct WORKREQ_HTTPSENDREQUESTW *req;
Aric Stewartbe918f42005-11-21 15:17:55 +00002331
Jacek Cabanc1568452006-12-25 21:33:36 +01002332 workRequest.asyncproc = AsyncHttpSendRequestProc;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002333 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2334 req = &workRequest.u.HttpSendRequestW;
Aric Stewart21712d32006-01-13 13:52:38 +01002335 if (lpBuffersIn)
2336 {
2337 if (lpBuffersIn->lpcszHeader)
2338 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2339 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2340 else
2341 req->lpszHeader = NULL;
2342 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2343 req->lpOptional = lpBuffersIn->lpvBuffer;
2344 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2345 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2346 }
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002347 else
Aric Stewart21712d32006-01-13 13:52:38 +01002348 {
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002349 req->lpszHeader = NULL;
Aric Stewart21712d32006-01-13 13:52:38 +01002350 req->dwHeaderLength = 0;
2351 req->lpOptional = NULL;
2352 req->dwOptionalLength = 0;
2353 req->dwContentLength = 0;
2354 }
2355
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002356 req->bEndRequest = FALSE;
2357
2358 INTERNET_AsyncCall(&workRequest);
2359 /*
2360 * This is from windows.
2361 */
Francois Gougete66d1cd2007-01-09 12:30:45 +01002362 INTERNET_SetLastError(ERROR_IO_PENDING);
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002363 }
2364 else
Aric Stewartbe918f42005-11-21 15:17:55 +00002365 {
Juan Langd857c042007-10-22 10:23:54 -07002366 if (lpBuffersIn)
2367 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2368 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2369 lpBuffersIn->dwBufferTotal, FALSE);
2370 else
2371 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
Aric Stewartbe918f42005-11-21 15:17:55 +00002372 }
Nigel Liang6f446272007-11-03 18:24:49 -07002373
2374lend:
2375 if ( lpwhr )
2376 WININET_Release( &lpwhr->hdr );
2377
Aric Stewartbe918f42005-11-21 15:17:55 +00002378 TRACE("<---\n");
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002379 return ret;
Hans Leidekker6a367db2005-02-01 18:50:53 +00002380}
2381
2382/***********************************************************************
Dimitrie O. Paunf3b681a2004-06-28 20:28:38 +00002383 * HttpSendRequestW (WININET.@)
Ulrich Czekallac2757242000-06-11 20:04:44 +00002384 *
2385 * Sends the specified request to the HTTP server
2386 *
2387 * RETURNS
2388 * TRUE on success
2389 * FALSE on failure
2390 *
2391 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002392BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
Ulrich Czekallac2757242000-06-11 20:04:44 +00002393 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00002394{
Mike McCormacka4e902c2004-03-30 04:36:09 +00002395 LPWININETHTTPREQW lpwhr;
2396 LPWININETHTTPSESSIONW lpwhs = NULL;
Mike McCormack09d2d472004-03-25 05:29:47 +00002397 LPWININETAPPINFOW hIC = NULL;
Mike McCormack3a1391b2004-07-19 21:49:39 +00002398 BOOL r;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002399
Rob Shearman2e21d092007-05-25 12:07:26 +01002400 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2401 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002402
Mike McCormacka4e902c2004-03-30 04:36:09 +00002403 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
Ulrich Czekallac2757242000-06-11 20:04:44 +00002404 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2405 {
2406 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002407 r = FALSE;
2408 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002409 }
2410
Jacek Caban61a1cef2006-10-29 18:51:15 +01002411 lpwhs = lpwhr->lpHttpSession;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002412 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2413 {
2414 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002415 r = FALSE;
2416 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002417 }
2418
Jacek Cabana541a442006-10-29 18:49:39 +01002419 hIC = lpwhs->lpAppInfo;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002420 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2421 {
2422 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002423 r = FALSE;
2424 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002425 }
2426
2427 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2428 {
2429 WORKREQUEST workRequest;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002430 struct WORKREQ_HTTPSENDREQUESTW *req;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002431
Jacek Cabanc1568452006-12-25 21:33:36 +01002432 workRequest.asyncproc = AsyncHttpSendRequestProc;
2433 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
Mike McCormacka4e902c2004-03-30 04:36:09 +00002434 req = &workRequest.u.HttpSendRequestW;
Aric Stewartff9b9d42002-06-21 23:59:49 +00002435 if (lpszHeaders)
Rob Shearman2e21d092007-05-25 12:07:26 +01002436 {
2437 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, dwHeaderLength * sizeof(WCHAR));
2438 memcpy(req->lpszHeader, lpszHeaders, dwHeaderLength * sizeof(WCHAR));
2439 }
Aric Stewartff9b9d42002-06-21 23:59:49 +00002440 else
Mike McCormackb8921a22003-09-25 20:25:22 +00002441 req->lpszHeader = 0;
2442 req->dwHeaderLength = dwHeaderLength;
2443 req->lpOptional = lpOptional;
2444 req->dwOptionalLength = dwOptionalLength;
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002445 req->dwContentLength = dwOptionalLength;
2446 req->bEndRequest = TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002447
Aric Stewartff9b9d42002-06-21 23:59:49 +00002448 INTERNET_AsyncCall(&workRequest);
2449 /*
Alberto Massaribc8bd722002-12-06 23:20:31 +00002450 * This is from windows.
Aric Stewartff9b9d42002-06-21 23:59:49 +00002451 */
Francois Gougete66d1cd2007-01-09 12:30:45 +01002452 INTERNET_SetLastError(ERROR_IO_PENDING);
Mike McCormack3a1391b2004-07-19 21:49:39 +00002453 r = FALSE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002454 }
2455 else
2456 {
Mike McCormack3a1391b2004-07-19 21:49:39 +00002457 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002458 dwHeaderLength, lpOptional, dwOptionalLength,
2459 dwOptionalLength, TRUE);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002460 }
Mike McCormack3a1391b2004-07-19 21:49:39 +00002461lend:
2462 if( lpwhr )
2463 WININET_Release( &lpwhr->hdr );
2464 return r;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002465}
2466
Alberto Massarid476a5a2002-11-12 02:13:04 +00002467/***********************************************************************
Dimitrie O. Paunf3b681a2004-06-28 20:28:38 +00002468 * HttpSendRequestA (WININET.@)
Alberto Massarid476a5a2002-11-12 02:13:04 +00002469 *
2470 * Sends the specified request to the HTTP server
2471 *
2472 * RETURNS
2473 * TRUE on success
2474 * FALSE on failure
2475 *
2476 */
Mike McCormacka4e902c2004-03-30 04:36:09 +00002477BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
Alberto Massarid476a5a2002-11-12 02:13:04 +00002478 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2479{
2480 BOOL result;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002481 LPWSTR szHeaders=NULL;
Alberto Massarid476a5a2002-11-12 02:13:04 +00002482 DWORD nLen=dwHeaderLength;
2483 if(lpszHeaders!=NULL)
2484 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00002485 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2486 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2487 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
Alberto Massarid476a5a2002-11-12 02:13:04 +00002488 }
Mike McCormacka4e902c2004-03-30 04:36:09 +00002489 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00002490 HeapFree(GetProcessHeap(),0,szHeaders);
Alberto Massarid476a5a2002-11-12 02:13:04 +00002491 return result;
2492}
Ulrich Czekallac2757242000-06-11 20:04:44 +00002493
Jacek Caband7a49e82008-02-13 13:32:49 +01002494static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
2495{
2496 LPHTTPHEADERW host_header;
2497
2498 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2499
2500 host_header = HTTP_GetHeader(req, szHost);
2501 if(!host_header)
2502 return FALSE;
2503
2504 sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
2505 return TRUE;
2506}
2507
Ulrich Czekallac2757242000-06-11 20:04:44 +00002508/***********************************************************************
Alberto Massaribc8bd722002-12-06 23:20:31 +00002509 * HTTP_HandleRedirect (internal)
2510 */
Rob Shearman4319ec62006-12-07 00:53:27 +00002511static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
Alberto Massaribc8bd722002-12-06 23:20:31 +00002512{
Jacek Caban61a1cef2006-10-29 18:51:15 +01002513 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
Jacek Cabana541a442006-10-29 18:49:39 +01002514 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002515 WCHAR path[2048];
Mike McCormack7cc70c02004-02-07 01:03:41 +00002516
Alberto Massaribc8bd722002-12-06 23:20:31 +00002517 if(lpszUrl[0]=='/')
2518 {
2519 /* if it's an absolute path, keep the same session info */
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002520 lstrcpynW(path, lpszUrl, 2048);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002521 }
Mike McCormacka1c16d22003-07-22 03:17:52 +00002522 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2523 {
2524 TRACE("Redirect through proxy\n");
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002525 lstrcpynW(path, lpszUrl, 2048);
Mike McCormacka1c16d22003-07-22 03:17:52 +00002526 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00002527 else
2528 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00002529 URL_COMPONENTSW urlComponents;
2530 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
Hans Leidekker781f3f72006-10-13 15:43:54 +02002531 static WCHAR szHttp[] = {'h','t','t','p',0};
2532 static WCHAR szHttps[] = {'h','t','t','p','s',0};
Robert Shearman05900252006-03-09 15:11:59 +00002533 DWORD url_length = 0;
2534 LPWSTR orig_url;
2535 LPWSTR combined_url;
2536
2537 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
Hans Leidekker781f3f72006-10-13 15:43:54 +02002538 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
Robert Shearman05900252006-03-09 15:11:59 +00002539 urlComponents.dwSchemeLength = 0;
2540 urlComponents.lpszHostName = lpwhs->lpszHostName;
2541 urlComponents.dwHostNameLength = 0;
2542 urlComponents.nPort = lpwhs->nHostPort;
2543 urlComponents.lpszUserName = lpwhs->lpszUserName;
2544 urlComponents.dwUserNameLength = 0;
2545 urlComponents.lpszPassword = NULL;
2546 urlComponents.dwPasswordLength = 0;
2547 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2548 urlComponents.dwUrlPathLength = 0;
2549 urlComponents.lpszExtraInfo = NULL;
2550 urlComponents.dwExtraInfoLength = 0;
2551
2552 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2553 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2554 return FALSE;
2555
Robert Shearman80e4fb52006-03-21 13:44:36 +00002556 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
Robert Shearman05900252006-03-09 15:11:59 +00002557
Robert Shearman80e4fb52006-03-21 13:44:36 +00002558 /* convert from bytes to characters */
2559 url_length = url_length / sizeof(WCHAR) - 1;
Robert Shearman05900252006-03-09 15:11:59 +00002560 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2561 {
2562 HeapFree(GetProcessHeap(), 0, orig_url);
2563 return FALSE;
2564 }
2565
2566 url_length = 0;
2567 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2568 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2569 {
2570 HeapFree(GetProcessHeap(), 0, orig_url);
2571 return FALSE;
2572 }
2573 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2574
2575 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2576 {
2577 HeapFree(GetProcessHeap(), 0, orig_url);
2578 HeapFree(GetProcessHeap(), 0, combined_url);
2579 return FALSE;
2580 }
2581 HeapFree(GetProcessHeap(), 0, orig_url);
2582
Aric Stewart1fc760d2005-11-28 17:31:02 +01002583 userName[0] = 0;
2584 hostName[0] = 0;
2585 protocol[0] = 0;
2586
Mike McCormacka4e902c2004-03-30 04:36:09 +00002587 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002588 urlComponents.lpszScheme = protocol;
2589 urlComponents.dwSchemeLength = 32;
2590 urlComponents.lpszHostName = hostName;
2591 urlComponents.dwHostNameLength = MAXHOSTNAME;
2592 urlComponents.lpszUserName = userName;
2593 urlComponents.dwUserNameLength = 1024;
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002594 urlComponents.lpszPassword = NULL;
2595 urlComponents.dwPasswordLength = 0;
Alberto Massaribc8bd722002-12-06 23:20:31 +00002596 urlComponents.lpszUrlPath = path;
2597 urlComponents.dwUrlPathLength = 2048;
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002598 urlComponents.lpszExtraInfo = NULL;
2599 urlComponents.dwExtraInfoLength = 0;
Robert Shearman05900252006-03-09 15:11:59 +00002600 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2601 {
2602 HeapFree(GetProcessHeap(), 0, combined_url);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002603 return FALSE;
Robert Shearman05900252006-03-09 15:11:59 +00002604 }
Jacek Cabanf9791342008-02-13 13:34:05 +01002605
Robert Shearman05900252006-03-09 15:11:59 +00002606 HeapFree(GetProcessHeap(), 0, combined_url);
Robert Shearmanefac01b2005-11-29 11:25:31 +01002607
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002608 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
Robert Shearmanefac01b2005-11-29 11:25:31 +01002609 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2610 {
2611 TRACE("redirect from secure page to non-secure page\n");
2612 /* FIXME: warn about from secure redirect to non-secure page */
2613 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2614 }
Robert Shearmandc5f1cb2005-11-30 12:01:50 +01002615 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
Robert Shearmanefac01b2005-11-29 11:25:31 +01002616 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2617 {
2618 TRACE("redirect from non-secure page to secure page\n");
2619 /* FIXME: notify about redirect to secure page */
2620 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2621 }
2622
Alberto Massaribc8bd722002-12-06 23:20:31 +00002623 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
Aric Stewart1fc760d2005-11-28 17:31:02 +01002624 {
2625 if (lstrlenW(protocol)>4) /*https*/
2626 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2627 else /*http*/
2628 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2629 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00002630
Mike McCormacka1c16d22003-07-22 03:17:52 +00002631#if 0
2632 /*
2633 * This upsets redirects to binary files on sourceforge.net
2634 * and gives an html page instead of the target file
2635 * Examination of the HTTP request sent by native wininet.dll
2636 * reveals that it doesn't send a referrer in that case.
2637 * Maybe there's a flag that enables this, or maybe a referrer
2638 * shouldn't be added in case of a redirect.
2639 */
2640
2641 /* consider the current host as the referrer */
Jacek Cabanf9791342008-02-13 13:34:05 +01002642 if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
Mike McCormacka1c16d22003-07-22 03:17:52 +00002643 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2644 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2645 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2646#endif
Alberto Massaribc8bd722002-12-06 23:20:31 +00002647
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00002648 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
Robert Shearman8a8ce9c2005-11-29 11:35:19 +01002649 lpwhs->lpszServerName = WININET_strdupW(hostName);
2650 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
Aric Stewart1fc760d2005-11-28 17:31:02 +01002651 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2652 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2653 {
2654 int len;
Andrew Ziem70533212006-05-24 00:22:38 -06002655 static const WCHAR fmt[] = {'%','s',':','%','i',0};
Aric Stewart1fc760d2005-11-28 17:31:02 +01002656 len = lstrlenW(hostName);
Robert Shearman8a8ce9c2005-11-29 11:35:19 +01002657 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2658 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2659 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
Aric Stewart1fc760d2005-11-28 17:31:02 +01002660 }
2661 else
Robert Shearman8a8ce9c2005-11-29 11:35:19 +01002662 lpwhs->lpszHostName = WININET_strdupW(hostName);
Aric Stewart1fc760d2005-11-28 17:31:02 +01002663
Aric Stewart1e946d32005-12-13 17:07:41 +01002664 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 +01002665
2666
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00002667 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
Robert Shearmanef209362006-03-10 12:28:52 +00002668 lpwhs->lpszUserName = NULL;
2669 if (userName[0])
2670 lpwhs->lpszUserName = WININET_strdupW(userName);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002671 lpwhs->nServerPort = urlComponents.nPort;
2672
Rob Shearman72575a02006-12-07 00:52:50 +00002673 if (!HTTP_ResolveName(lpwhr))
Alberto Massaribc8bd722002-12-06 23:20:31 +00002674 return FALSE;
Alberto Massaribc8bd722002-12-06 23:20:31 +00002675
Robert Shearmanefac01b2005-11-29 11:25:31 +01002676 NETCON_close(&lpwhr->netConnection);
Robert Shearman9981f332006-03-06 17:31:09 +00002677
2678 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2679 return FALSE;
Alberto Massaribc8bd722002-12-06 23:20:31 +00002680 }
2681
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00002682 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002683 lpwhr->lpszPath=NULL;
Jacek Cabanf9791342008-02-13 13:34:05 +01002684 if (*path)
Alberto Massaribc8bd722002-12-06 23:20:31 +00002685 {
2686 DWORD needed = 0;
2687 HRESULT rc;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002688
2689 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002690 if (rc != E_POINTER)
Mike McCormacka4e902c2004-03-30 04:36:09 +00002691 needed = strlenW(path)+1;
2692 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2693 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
Alberto Massaribc8bd722002-12-06 23:20:31 +00002694 URL_ESCAPE_SPACES_ONLY);
2695 if (rc)
2696 {
Hans Leidekkercd2c4582006-10-05 13:18:56 +02002697 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
Mike McCormacka4e902c2004-03-30 04:36:09 +00002698 strcpyW(lpwhr->lpszPath,path);
Alberto Massaribc8bd722002-12-06 23:20:31 +00002699 }
2700 }
2701
Rob Shearman4319ec62006-12-07 00:53:27 +00002702 return TRUE;
Alberto Massaribc8bd722002-12-06 23:20:31 +00002703}
2704
2705/***********************************************************************
Mike McCormacka4969062004-07-04 00:24:47 +00002706 * HTTP_build_req (internal)
2707 *
2708 * concatenate all the strings in the request together
2709 */
2710static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2711{
2712 LPCWSTR *t;
2713 LPWSTR str;
2714
2715 for( t = list; *t ; t++ )
2716 len += strlenW( *t );
2717 len++;
2718
2719 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2720 *str = 0;
2721
2722 for( t = list; *t ; t++ )
2723 strcatW( str, *t );
2724
2725 return str;
2726}
2727
Robert Shearman0e7c41e2005-11-28 11:55:16 +01002728static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2729{
2730 LPWSTR lpszPath;
2731 LPWSTR requestString;
2732 INT len;
2733 INT cnt;
2734 INT responseLen;
2735 char *ascii_req;
2736 BOOL ret;
2737 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2738 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
Jacek Caban61a1cef2006-10-29 18:51:15 +01002739 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
Robert Shearman0e7c41e2005-11-28 11:55:16 +01002740
2741 TRACE("\n");
2742
2743 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2744 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
Hans Leidekkerd0033db2008-02-17 20:41:42 +01002745 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
Robert Shearman0e7c41e2005-11-28 11:55:16 +01002746 HeapFree( GetProcessHeap(), 0, lpszPath );
2747
2748 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2749 NULL, 0, NULL, NULL );
2750 len--; /* the nul terminator isn't needed */
2751 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2752 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2753 ascii_req, len, NULL, NULL );
2754 HeapFree( GetProcessHeap(), 0, requestString );
2755
2756 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2757
2758 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2759 HeapFree( GetProcessHeap(), 0, ascii_req );
2760 if (!ret || cnt < 0)
2761 return FALSE;
2762
2763 responseLen = HTTP_GetResponseHeaders( lpwhr );
2764 if (!responseLen)
2765 return FALSE;
2766
2767 return TRUE;
2768}
2769
Mike McCormacka4969062004-07-04 00:24:47 +00002770/***********************************************************************
Mike McCormacka4e902c2004-03-30 04:36:09 +00002771 * HTTP_HttpSendRequestW (internal)
Ulrich Czekallac2757242000-06-11 20:04:44 +00002772 *
2773 * Sends the specified request to the HTTP server
2774 *
2775 * RETURNS
2776 * TRUE on success
2777 * FALSE on failure
2778 *
2779 */
Mike McCormack3a1391b2004-07-19 21:49:39 +00002780BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002781 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2782 DWORD dwContentLength, BOOL bEndRequest)
Ulrich Czekallac2757242000-06-11 20:04:44 +00002783{
2784 INT cnt;
Ulrich Czekallac2757242000-06-11 20:04:44 +00002785 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00002786 LPWSTR requestString = NULL;
Aric Stewartff9b9d42002-06-21 23:59:49 +00002787 INT responseLen;
Rob Shearman14fb4182007-01-04 18:21:13 +00002788 BOOL loop_next;
Robert Shearmaned517f32004-09-20 19:10:31 +00002789 INTERNET_ASYNC_RESULT iar;
Rob Shearmancf43b162007-01-04 18:21:34 +00002790 static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
Hans Leidekkerb0912d12008-02-02 16:08:28 +01002791 static const WCHAR szPost[] = { 'P','O','S','T',0 };
Hans Leidekker64359c22007-10-28 16:32:46 +01002792 static const WCHAR szContentLength[] =
2793 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
2794 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
Ulrich Czekallac2757242000-06-11 20:04:44 +00002795
Mike McCormack3a1391b2004-07-19 21:49:39 +00002796 TRACE("--> %p\n", lpwhr);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002797
Mike McCormack3a1391b2004-07-19 21:49:39 +00002798 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002799
Ulrich Czekallac2757242000-06-11 20:04:44 +00002800 /* Clear any error information */
2801 INTERNET_SetLastError(0);
2802
Jacek Cabanf9791342008-02-13 13:34:05 +01002803 /* if the verb is NULL default to GET */
2804 if (!lpwhr->lpszVerb)
2805 lpwhr->lpszVerb = WININET_strdupW(szGET);
2806
Hans Leidekkerb0912d12008-02-02 16:08:28 +01002807 if (dwContentLength || !strcmpW(lpwhr->lpszVerb, szPost))
2808 {
2809 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2810 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2811 }
David Hammerton6226f3f2003-08-05 18:31:02 +00002812
David Hammerton852c7ae2003-06-20 23:26:56 +00002813 do
Nikolas Zimmermann76598822001-10-04 18:12:41 +00002814 {
Aric Stewartbe918f42005-11-21 15:17:55 +00002815 DWORD len;
Mike McCormacka4969062004-07-04 00:24:47 +00002816 char *ascii_req;
2817
Rob Shearman14fb4182007-01-04 18:21:13 +00002818 loop_next = FALSE;
Rob Shearman272954b2007-01-04 18:23:17 +00002819
2820 /* like native, just in case the caller forgot to call InternetReadFile
2821 * for all the data */
2822 HTTP_DrainContent(lpwhr);
Rob Shearmanac1b5272007-01-04 18:21:49 +00002823 lpwhr->dwContentRead = 0;
Rob Shearman14fb4182007-01-04 18:21:13 +00002824
Rob Shearman4319ec62006-12-07 00:53:27 +00002825 if (TRACE_ON(wininet))
2826 {
2827 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
2828 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2829 }
Nikolas Zimmermann76598822001-10-04 18:12:41 +00002830
Aric Stewartbe918f42005-11-21 15:17:55 +00002831 HTTP_FixURL(lpwhr);
Hans Leidekkerbb9413d2007-08-09 21:43:25 +02002832 HTTP_ProcessHeader(lpwhr, szConnection,
2833 lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION ? szKeepAlive : szClose,
2834 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
2835
Hans Leidekkerb069ef42008-02-01 14:40:15 +01002836 HTTP_InsertAuthorization(lpwhr, szAuthorization, !loop_next);
2837 HTTP_InsertAuthorization(lpwhr, szProxy_Authorization, !loop_next);
Ulrich Czekallac2757242000-06-11 20:04:44 +00002838
Mike McCormackf1d7b142004-07-21 19:36:34 +00002839 /* add the headers the caller supplied */
Mike McCormack08c6c692004-08-10 23:41:35 +00002840 if( lpszHeaders && dwHeaderLength )
Mike McCormackf1d7b142004-07-21 19:36:34 +00002841 {
Mike McCormack08c6c692004-08-10 23:41:35 +00002842 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2843 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
Mike McCormackf1d7b142004-07-21 19:36:34 +00002844 }
2845
Hans Leidekkerd0033db2008-02-17 20:41:42 +01002846 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
Mike McCormacka4969062004-07-04 00:24:47 +00002847
2848 TRACE("Request header -> %s\n", debugstr_w(requestString) );
Aric Stewartff9b9d42002-06-21 23:59:49 +00002849
David Hammerton852c7ae2003-06-20 23:26:56 +00002850 /* Send the request and store the results */
2851 if (!HTTP_OpenConnection(lpwhr))
2852 goto lend;
Aric Stewartff9b9d42002-06-21 23:59:49 +00002853
Mike McCormacka4969062004-07-04 00:24:47 +00002854 /* send the request as ASCII, tack on the optional data */
2855 if( !lpOptional )
2856 dwOptionalLength = 0;
2857 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2858 NULL, 0, NULL, NULL );
2859 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2860 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2861 ascii_req, len, NULL, NULL );
2862 if( lpOptional )
Mike McCormackf1d7b142004-07-21 19:36:34 +00002863 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2864 len = (len + dwOptionalLength - 1);
2865 ascii_req[len] = 0;
Aric Stewart44cbdf22005-10-19 18:28:35 +00002866 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
Mike McCormackf1d7b142004-07-21 19:36:34 +00002867
Robert Shearmande2666f2005-11-29 10:44:05 +01002868 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2869 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
Mike McCormackf1d7b142004-07-21 19:36:34 +00002870
Mike McCormacka4969062004-07-04 00:24:47 +00002871 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2872 HeapFree( GetProcessHeap(), 0, ascii_req );
David Hammerton852c7ae2003-06-20 23:26:56 +00002873
Robert Shearmande2666f2005-11-29 10:44:05 +01002874 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2875 INTERNET_STATUS_REQUEST_SENT,
2876 &len, sizeof(DWORD));
David Hammerton852c7ae2003-06-20 23:26:56 +00002877
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002878 if (bEndRequest)
2879 {
Rob Shearmanac1b5272007-01-04 18:21:49 +00002880 DWORD dwBufferSize;
Rob Shearmancb289692007-06-05 19:49:58 +01002881 DWORD dwStatusCode;
Rob Shearmanac1b5272007-01-04 18:21:49 +00002882
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002883 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2884 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2885
2886 if (cnt < 0)
2887 goto lend;
2888
2889 responseLen = HTTP_GetResponseHeaders(lpwhr);
2890 if (responseLen)
2891 bSuccess = TRUE;
2892
2893 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2894 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2895 sizeof(DWORD));
Rob Shearman4319ec62006-12-07 00:53:27 +00002896
Hans Leidekker97171132008-02-17 21:29:45 +01002897 HTTP_ProcessCookies(lpwhr);
Rob Shearman4319ec62006-12-07 00:53:27 +00002898
Rob Shearmanac1b5272007-01-04 18:21:49 +00002899 dwBufferSize = sizeof(lpwhr->dwContentLength);
2900 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2901 &lpwhr->dwContentLength,&dwBufferSize,NULL))
2902 lpwhr->dwContentLength = -1;
2903
Rob Shearman272954b2007-01-04 18:23:17 +00002904 if (lpwhr->dwContentLength == 0)
2905 HTTP_FinishedReading(lpwhr);
2906
Rob Shearmancb289692007-06-05 19:49:58 +01002907 dwBufferSize = sizeof(dwStatusCode);
2908 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
2909 &dwStatusCode,&dwBufferSize,NULL))
2910 dwStatusCode = 0;
2911
Rob Shearman4319ec62006-12-07 00:53:27 +00002912 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2913 {
Rob Shearman4319ec62006-12-07 00:53:27 +00002914 WCHAR szNewLocation[2048];
Rob Shearman9efe0832007-01-12 19:19:18 -06002915 dwBufferSize=sizeof(szNewLocation);
Rob Shearmancb289692007-06-05 19:49:58 +01002916 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
Rob Shearmanac1b5272007-01-04 18:21:49 +00002917 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
Rob Shearman4319ec62006-12-07 00:53:27 +00002918 {
Rob Shearman272954b2007-01-04 18:23:17 +00002919 HTTP_DrainContent(lpwhr);
Rob Shearman4319ec62006-12-07 00:53:27 +00002920 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2921 INTERNET_STATUS_REDIRECT, szNewLocation,
2922 dwBufferSize);
2923 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
2924 if (bSuccess)
2925 {
2926 HeapFree(GetProcessHeap(), 0, requestString);
2927 loop_next = TRUE;
2928 }
2929 }
2930 }
Rob Shearman4b507682007-05-21 14:26:26 +01002931 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
2932 {
Rob Shearman4b507682007-05-21 14:26:26 +01002933 WCHAR szAuthValue[2048];
2934 dwBufferSize=2048;
Rob Shearmancb289692007-06-05 19:49:58 +01002935 if (dwStatusCode == HTTP_STATUS_DENIED)
Rob Shearman4b507682007-05-21 14:26:26 +01002936 {
2937 DWORD dwIndex = 0;
2938 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
2939 {
Rob Shearmancb289692007-06-05 19:49:58 +01002940 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
2941 &lpwhr->pAuthInfo,
2942 lpwhr->lpHttpSession->lpszUserName,
2943 lpwhr->lpHttpSession->lpszPassword))
2944 {
2945 loop_next = TRUE;
2946 break;
2947 }
2948 }
2949 }
2950 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
2951 {
2952 DWORD dwIndex = 0;
2953 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
2954 {
2955 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
2956 &lpwhr->pProxyAuthInfo,
2957 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
2958 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
Rob Shearman4b507682007-05-21 14:26:26 +01002959 {
2960 loop_next = TRUE;
2961 break;
2962 }
2963 }
2964 }
2965 }
Robert Shearmanf6252cf2005-11-30 12:31:22 +01002966 }
2967 else
2968 bSuccess = TRUE;
David Hammerton852c7ae2003-06-20 23:26:56 +00002969 }
2970 while (loop_next);
Aric Stewartff9b9d42002-06-21 23:59:49 +00002971
Jacek Caband7a49e82008-02-13 13:32:49 +01002972 /* FIXME: Better check, when we have to create the cache file */
2973 if(bSuccess && (lpwhr->hdr.dwFlags & INTERNET_FLAG_NEED_FILE)) {
2974 WCHAR url[INTERNET_MAX_URL_LENGTH];
2975 WCHAR cacheFileName[MAX_PATH+1];
2976 BOOL b;
2977
2978 b = HTTP_GetRequestURL(lpwhr, url);
2979 if(!b) {
2980 WARN("Could not get URL\n");
2981 goto lend;
2982 }
2983
2984 b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
2985 if(b) {
2986 lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
2987 lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
2988 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
2989 if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
2990 WARN("Could not create file: %u\n", GetLastError());
2991 lpwhr->hCacheFile = NULL;
2992 }
2993 }else {
2994 WARN("Could not create cache entry: %08x\n", GetLastError());
2995 }
2996 }
2997
Ulrich Czekallac2757242000-06-11 20:04:44 +00002998lend:
2999
Michael Stefaniuc7cb43c92004-12-21 14:42:35 +00003000 HeapFree(GetProcessHeap(), 0, requestString);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003001
Alberto Massaribc8bd722002-12-06 23:20:31 +00003002 /* TODO: send notification for P3P header */
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00003003
Robert Shearmaned517f32004-09-20 19:10:31 +00003004 iar.dwResult = (DWORD)bSuccess;
3005 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
Aric Stewartff9b9d42002-06-21 23:59:49 +00003006
Robert Shearmande2666f2005-11-29 10:44:05 +01003007 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3008 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3009 sizeof(INTERNET_ASYNC_RESULT));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003010
3011 TRACE("<--\n");
3012 return bSuccess;
3013}
3014
Ulrich Czekallac2757242000-06-11 20:04:44 +00003015/***********************************************************************
Jacek Caban5a535d62008-02-26 20:20:41 +01003016 * HTTPSESSION_Destroy (internal)
3017 *
3018 * Deallocate session handle
3019 *
3020 */
3021static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3022{
3023 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3024
3025 TRACE("%p\n", lpwhs);
3026
3027 WININET_Release(&lpwhs->lpAppInfo->hdr);
3028
3029 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3030 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3031 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3032 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3033 HeapFree(GetProcessHeap(), 0, lpwhs);
3034}
3035
3036
3037static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
Jacek Caban7dc9bf62008-02-26 20:21:00 +01003038 HTTPSESSION_Destroy,
Jacek Caban1ffcfbc2008-02-26 20:21:34 +01003039 NULL,
Jacek Caban0e33eee2008-02-26 20:22:02 +01003040 NULL,
Jacek Caban8c45eec2008-02-27 18:55:09 +01003041 NULL,
Jacek Caban33141842008-02-29 12:57:57 +01003042 NULL,
Jacek Caban3b4ca692008-03-02 19:35:11 +01003043 NULL,
Jacek Caban7dc9bf62008-02-26 20:21:00 +01003044 NULL
Jacek Caban5a535d62008-02-26 20:20:41 +01003045};
3046
3047
3048/***********************************************************************
Ulrich Czekallac2757242000-06-11 20:04:44 +00003049 * HTTP_Connect (internal)
3050 *
3051 * Create http session handle
3052 *
3053 * RETURNS
3054 * HINTERNET a session handle on success
3055 * NULL on failure
3056 *
3057 */
Mike McCormack3a1391b2004-07-19 21:49:39 +00003058HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
Mike McCormacka4e902c2004-03-30 04:36:09 +00003059 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
Francois Gougetd4337f22007-08-30 16:21:33 +02003060 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
Kevin Koltzau917df922004-05-13 05:17:25 +00003061 DWORD dwInternalFlags)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003062{
3063 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003064 LPWININETHTTPSESSIONW lpwhs = NULL;
Mike McCormack7cc70c02004-02-07 01:03:41 +00003065 HINTERNET handle = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003066
Aric Stewartff9b9d42002-06-21 23:59:49 +00003067 TRACE("-->\n");
Ulrich Czekallac2757242000-06-11 20:04:44 +00003068
Rob Shearman59ab0cf2008-01-31 14:47:25 +00003069 if (!lpszServerName || !lpszServerName[0])
3070 {
3071 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3072 goto lerror;
3073 }
3074
Mike McCormack3a1391b2004-07-19 21:49:39 +00003075 assert( hIC->hdr.htype == WH_HINIT );
Ulrich Czekallac2757242000-06-11 20:04:44 +00003076
Mike McCormacka4e902c2004-03-30 04:36:09 +00003077 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003078 if (NULL == lpwhs)
3079 {
3080 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3081 goto lerror;
3082 }
3083
Aric Stewartff9b9d42002-06-21 23:59:49 +00003084 /*
3085 * According to my tests. The name is not resolved until a request is sent
3086 */
Ulrich Czekallac2757242000-06-11 20:04:44 +00003087
Ulrich Czekallac2757242000-06-11 20:04:44 +00003088 lpwhs->hdr.htype = WH_HHTTPSESSION;
Jacek Caban5a535d62008-02-26 20:20:41 +01003089 lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003090 lpwhs->hdr.dwFlags = dwFlags;
3091 lpwhs->hdr.dwContext = dwContext;
Jacek Caban5c53baa2006-10-30 22:14:59 +01003092 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
Mike McCormack3a1391b2004-07-19 21:49:39 +00003093 lpwhs->hdr.dwRefCount = 1;
Robert Shearmaned517f32004-09-20 19:10:31 +00003094 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
Mike McCormack3a1391b2004-07-19 21:49:39 +00003095
Jacek Cabanc2506172006-10-29 18:48:48 +01003096 WININET_AddRef( &hIC->hdr );
3097 lpwhs->lpAppInfo = hIC;
Misha Koshelev728e5fa2007-09-20 20:59:40 -05003098 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
Jacek Cabanc2506172006-10-29 18:48:48 +01003099
Mike McCormack3a1391b2004-07-19 21:49:39 +00003100 handle = WININET_AllocHandle( &lpwhs->hdr );
3101 if (NULL == handle)
3102 {
3103 ERR("Failed to alloc handle\n");
3104 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3105 goto lerror;
3106 }
3107
Dominik Strasser94c02fe2003-04-14 21:32:36 +00003108 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
Mike McCormack09d2d472004-03-25 05:29:47 +00003109 if(strchrW(hIC->lpszProxy, ' '))
Dominik Strasser94c02fe2003-04-14 21:32:36 +00003110 FIXME("Several proxies not implemented.\n");
3111 if(hIC->lpszProxyBypass)
3112 FIXME("Proxy bypass is ignored.\n");
3113 }
Robert Shearmanef209362006-03-10 12:28:52 +00003114 if (lpszServerName && lpszServerName[0])
Robert Shearman7bb34632005-11-28 10:40:42 +01003115 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00003116 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
Robert Shearman7bb34632005-11-28 10:40:42 +01003117 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
3118 }
Robert Shearmanef209362006-03-10 12:28:52 +00003119 if (lpszUserName && lpszUserName[0])
Mike McCormacka4e902c2004-03-30 04:36:09 +00003120 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
Rob Shearman4b507682007-05-21 14:26:26 +01003121 if (lpszPassword && lpszPassword[0])
3122 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003123 lpwhs->nServerPort = nServerPort;
Robert Shearman35921102005-11-28 11:53:05 +01003124 lpwhs->nHostPort = nServerPort;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003125
Kevin Koltzau917df922004-05-13 05:17:25 +00003126 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
Robert Shearmaned517f32004-09-20 19:10:31 +00003127 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003128 {
Robert Shearmande2666f2005-11-29 10:44:05 +01003129 INTERNET_SendCallback(&hIC->hdr, dwContext,
3130 INTERNET_STATUS_HANDLE_CREATED, &handle,
3131 sizeof(handle));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003132 }
3133
3134 bSuccess = TRUE;
3135
3136lerror:
Mike McCormack3a1391b2004-07-19 21:49:39 +00003137 if( lpwhs )
3138 WININET_Release( &lpwhs->hdr );
Ulrich Czekallac2757242000-06-11 20:04:44 +00003139
Aric Stewartff9b9d42002-06-21 23:59:49 +00003140/*
Francois Gouget93416cd2005-03-23 13:15:18 +00003141 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
Aric Stewartff9b9d42002-06-21 23:59:49 +00003142 * windows
3143 */
Vincent BĂ©ron9a624912002-05-31 23:06:46 +00003144
Mike McCormack3a1391b2004-07-19 21:49:39 +00003145 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
Mike McCormack7cc70c02004-02-07 01:03:41 +00003146 return handle;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003147}
3148
3149
3150/***********************************************************************
3151 * HTTP_OpenConnection (internal)
3152 *
3153 * Connect to a web server
3154 *
3155 * RETURNS
3156 *
3157 * TRUE on success
3158 * FALSE on failure
3159 */
Jacek Caban02708c62005-10-26 10:07:58 +00003160static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003161{
3162 BOOL bSuccess = FALSE;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003163 LPWININETHTTPSESSIONW lpwhs;
Mike McCormack09d2d472004-03-25 05:29:47 +00003164 LPWININETAPPINFOW hIC = NULL;
Jacek Cabanad023172006-01-05 14:37:06 +01003165 char szaddr[32];
Ulrich Czekallac2757242000-06-11 20:04:44 +00003166
Aric Stewartff9b9d42002-06-21 23:59:49 +00003167 TRACE("-->\n");
3168
Ulrich Czekallac2757242000-06-11 20:04:44 +00003169
3170 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3171 {
3172 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3173 goto lend;
3174 }
3175
Rob Shearman272954b2007-01-04 18:23:17 +00003176 if (NETCON_connected(&lpwhr->netConnection))
3177 {
3178 bSuccess = TRUE;
3179 goto lend;
3180 }
3181
Jacek Caban61a1cef2006-10-29 18:51:15 +01003182 lpwhs = lpwhr->lpHttpSession;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003183
Jacek Cabana541a442006-10-29 18:49:39 +01003184 hIC = lpwhs->lpAppInfo;
Jacek Cabanad023172006-01-05 14:37:06 +01003185 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
3186 szaddr, sizeof(szaddr));
Robert Shearmande2666f2005-11-29 10:44:05 +01003187 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3188 INTERNET_STATUS_CONNECTING_TO_SERVER,
Jacek Cabanad023172006-01-05 14:37:06 +01003189 szaddr,
3190 strlen(szaddr)+1);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003191
Robert Shearmane6157dd2005-11-15 18:16:55 +00003192 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
David Hammerton852c7ae2003-06-20 23:26:56 +00003193 SOCK_STREAM, 0))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003194 {
3195 WARN("Socket creation failed\n");
3196 goto lend;
3197 }
3198
David Hammerton852c7ae2003-06-20 23:26:56 +00003199 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
3200 sizeof(lpwhs->socketAddress)))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003201 goto lend;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003202
Robert Shearman13d371d2005-11-28 11:54:31 +01003203 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
3204 {
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003205 /* Note: we differ from Microsoft's WinINet here. they seem to have
3206 * a bug that causes no status callbacks to be sent when starting
3207 * a tunnel to a proxy server using the CONNECT verb. i believe our
3208 * behaviour to be more correct and to not cause any incompatibilities
3209 * because using a secure connection through a proxy server is a rare
3210 * case that would be hard for anyone to depend on */
3211 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
3212 goto lend;
3213
Robert Shearman13d371d2005-11-28 11:54:31 +01003214 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
3215 {
3216 WARN("Couldn't connect securely to host\n");
3217 goto lend;
3218 }
3219 }
3220
Robert Shearmande2666f2005-11-29 10:44:05 +01003221 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3222 INTERNET_STATUS_CONNECTED_TO_SERVER,
Jacek Cabanad023172006-01-05 14:37:06 +01003223 szaddr, strlen(szaddr)+1);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003224
Ulrich Czekallac2757242000-06-11 20:04:44 +00003225 bSuccess = TRUE;
3226
3227lend:
Aric Stewartff9b9d42002-06-21 23:59:49 +00003228 TRACE("%d <--\n", bSuccess);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003229 return bSuccess;
3230}
3231
3232
3233/***********************************************************************
Alexandre Julliard48243e32004-07-15 18:57:32 +00003234 * HTTP_clear_response_headers (internal)
3235 *
3236 * clear out any old response headers
3237 */
3238static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
3239{
3240 DWORD i;
3241
Alexandre Julliard48243e32004-07-15 18:57:32 +00003242 for( i=0; i<lpwhr->nCustHeaders; i++)
3243 {
3244 if( !lpwhr->pCustHeaders[i].lpszField )
3245 continue;
3246 if( !lpwhr->pCustHeaders[i].lpszValue )
3247 continue;
3248 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
3249 continue;
Robert Shearman7707a762005-03-10 11:14:24 +00003250 HTTP_DeleteCustomHeader( lpwhr, i );
3251 i--;
Alexandre Julliard48243e32004-07-15 18:57:32 +00003252 }
3253}
3254
3255/***********************************************************************
Ulrich Czekallac2757242000-06-11 20:04:44 +00003256 * HTTP_GetResponseHeaders (internal)
3257 *
3258 * Read server response
3259 *
3260 * RETURNS
3261 *
3262 * TRUE on success
3263 * FALSE on error
3264 */
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003265static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003266{
3267 INT cbreaks = 0;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003268 WCHAR buffer[MAX_REPLY_LEN];
Ulrich Czekallac2757242000-06-11 20:04:44 +00003269 DWORD buflen = MAX_REPLY_LEN;
3270 BOOL bSuccess = FALSE;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003271 INT rc = 0;
Robert Shearmandee87512004-07-19 20:09:20 +00003272 static const WCHAR szCrLf[] = {'\r','\n',0};
Hans Leidekker2617fb62008-02-17 20:41:56 +01003273 static const WCHAR szHundred[] = {'1','0','0',0};
Mike McCormacka4e902c2004-03-30 04:36:09 +00003274 char bufferA[MAX_REPLY_LEN];
Alexandre Julliard48243e32004-07-15 18:57:32 +00003275 LPWSTR status_code, status_text;
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00003276 DWORD cchMaxRawHeaders = 1024;
Robert Shearmandee87512004-07-19 20:09:20 +00003277 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00003278 DWORD cchRawHeaders = 0;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003279
Aric Stewartff9b9d42002-06-21 23:59:49 +00003280 TRACE("-->\n");
Ulrich Czekallac2757242000-06-11 20:04:44 +00003281
Alexandre Julliard48243e32004-07-15 18:57:32 +00003282 /* clear old response headers (eg. from a redirect response) */
3283 HTTP_clear_response_headers( lpwhr );
3284
David Hammerton852c7ae2003-06-20 23:26:56 +00003285 if (!NETCON_connected(&lpwhr->netConnection))
Ulrich Czekallac2757242000-06-11 20:04:44 +00003286 goto lend;
3287
Hans Leidekker2617fb62008-02-17 20:41:56 +01003288 do {
3289 /*
3290 * HACK peek at the buffer
3291 */
3292 buflen = MAX_REPLY_LEN;
3293 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003294
Hans Leidekker2617fb62008-02-17 20:41:56 +01003295 /*
3296 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3297 */
3298 memset(buffer, 0, MAX_REPLY_LEN);
3299 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3300 goto lend;
3301 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3302
3303 /* split the version from the status code */
3304 status_code = strchrW( buffer, ' ' );
3305 if( !status_code )
3306 goto lend;
3307 *status_code++=0;
3308
3309 /* split the status code from the status text */
3310 status_text = strchrW( status_code, ' ' );
3311 if( !status_text )
3312 goto lend;
3313 *status_text++=0;
3314
3315 TRACE("version [%s] status code [%s] status text [%s]\n",
3316 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3317
3318 } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
Ulrich Czekallac2757242000-06-11 20:04:44 +00003319
Maarten Lankhorst5132fb42008-02-25 11:05:41 -08003320 /* Add status code */
3321 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3322 HTTP_ADDHDR_FLAG_REPLACE);
3323
3324 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3325 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3326
3327 lpwhr->lpszVersion= WININET_strdupW(buffer);
3328 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3329
3330 /* Restore the spaces */
3331 *(status_code-1) = ' ';
3332 *(status_text-1) = ' ';
3333
Robert Shearmandee87512004-07-19 20:09:20 +00003334 /* regenerate raw headers */
3335 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3336 {
3337 cchMaxRawHeaders *= 2;
3338 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3339 }
3340 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3341 cchRawHeaders += (buflen-1);
3342 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3343 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3344 lpszRawHeaders[cchRawHeaders] = '\0';
3345
Ulrich Czekallac2757242000-06-11 20:04:44 +00003346 /* Parse each response line */
3347 do
3348 {
3349 buflen = MAX_REPLY_LEN;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003350 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
Robert Shearmanb72a6822004-09-23 22:53:50 +00003351 {
3352 LPWSTR * pFieldAndValue;
3353
Francois Gougetda8b3dd2005-01-26 21:09:04 +00003354 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
Mike McCormacka4e902c2004-03-30 04:36:09 +00003355 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
Robert Shearmandee87512004-07-19 20:09:20 +00003356
3357 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3358 {
3359 cchMaxRawHeaders *= 2;
3360 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3361 }
3362 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3363 cchRawHeaders += (buflen-1);
3364 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3365 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3366 lpszRawHeaders[cchRawHeaders] = '\0';
3367
Robert Shearmanb72a6822004-09-23 22:53:50 +00003368 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3369 if (!pFieldAndValue)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003370 break;
3371
Robert Shearmanb72a6822004-09-23 22:53:50 +00003372 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
Aric Stewart1e946d32005-12-13 17:07:41 +01003373 HTTP_ADDREQ_FLAG_ADD );
Robert Shearmanb72a6822004-09-23 22:53:50 +00003374
3375 HTTP_FreeTokens(pFieldAndValue);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003376 }
3377 else
3378 {
3379 cbreaks++;
3380 if (cbreaks >= 2)
3381 break;
3382 }
3383 }while(1);
3384
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +00003385 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
Robert Shearmandee87512004-07-19 20:09:20 +00003386 lpwhr->lpszRawHeaders = lpszRawHeaders;
3387 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003388 bSuccess = TRUE;
3389
3390lend:
3391
Aric Stewartff9b9d42002-06-21 23:59:49 +00003392 TRACE("<--\n");
3393 if (bSuccess)
3394 return rc;
3395 else
Robert Shearman0e7c41e2005-11-28 11:55:16 +01003396 return 0;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003397}
3398
3399
Robert Shearmanb72a6822004-09-23 22:53:50 +00003400static void strip_spaces(LPWSTR start)
3401{
3402 LPWSTR str = start;
3403 LPWSTR end;
3404
3405 while (*str == ' ' && *str != '\0')
3406 str++;
3407
3408 if (str != start)
3409 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
3410
3411 end = start + strlenW(start) - 1;
3412 while (end >= start && *end == ' ')
3413 {
3414 *end = '\0';
3415 end--;
3416 }
3417}
3418
3419
Ulrich Czekallac2757242000-06-11 20:04:44 +00003420/***********************************************************************
3421 * HTTP_InterpretHttpHeader (internal)
3422 *
3423 * Parse server response
3424 *
3425 * RETURNS
3426 *
Robert Shearmanb72a6822004-09-23 22:53:50 +00003427 * Pointer to array of field, value, NULL on success.
3428 * NULL on error.
Ulrich Czekallac2757242000-06-11 20:04:44 +00003429 */
Jacek Caban02708c62005-10-26 10:07:58 +00003430static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003431{
Robert Shearmanb72a6822004-09-23 22:53:50 +00003432 LPWSTR * pTokenPair;
3433 LPWSTR pszColon;
3434 INT len;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003435
Robert Shearmanb72a6822004-09-23 22:53:50 +00003436 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003437
Robert Shearmanb72a6822004-09-23 22:53:50 +00003438 pszColon = strchrW(buffer, ':');
3439 /* must have two tokens */
3440 if (!pszColon)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003441 {
Robert Shearmanb72a6822004-09-23 22:53:50 +00003442 HTTP_FreeTokens(pTokenPair);
Robert Shearman7707a762005-03-10 11:14:24 +00003443 if (buffer[0])
3444 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
Robert Shearmanb72a6822004-09-23 22:53:50 +00003445 return NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003446 }
3447
Robert Shearmanb72a6822004-09-23 22:53:50 +00003448 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
3449 if (!pTokenPair[0])
Ulrich Czekallac2757242000-06-11 20:04:44 +00003450 {
Robert Shearmanb72a6822004-09-23 22:53:50 +00003451 HTTP_FreeTokens(pTokenPair);
3452 return NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003453 }
Robert Shearmanb72a6822004-09-23 22:53:50 +00003454 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
3455 pTokenPair[0][pszColon - buffer] = '\0';
Ulrich Czekallac2757242000-06-11 20:04:44 +00003456
Robert Shearmanb72a6822004-09-23 22:53:50 +00003457 /* skip colon */
3458 pszColon++;
3459 len = strlenW(pszColon);
3460 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
3461 if (!pTokenPair[1])
3462 {
3463 HTTP_FreeTokens(pTokenPair);
3464 return NULL;
3465 }
3466 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
3467
3468 strip_spaces(pTokenPair[0]);
3469 strip_spaces(pTokenPair[1]);
3470
3471 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
3472 return pTokenPair;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003473}
3474
Ulrich Czekallac2757242000-06-11 20:04:44 +00003475/***********************************************************************
3476 * HTTP_ProcessHeader (internal)
3477 *
3478 * Stuff header into header tables according to <dwModifier>
3479 *
3480 */
3481
Juan Langa1ab4a72007-11-07 14:45:06 -08003482#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 +00003483
Jacek Caban02708c62005-10-26 10:07:58 +00003484static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003485{
Mike McCormacka4e902c2004-03-30 04:36:09 +00003486 LPHTTPHEADERW lphttpHdr = NULL;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003487 BOOL bSuccess = FALSE;
Aric Stewart1e946d32005-12-13 17:07:41 +01003488 INT index = -1;
Aric Stewart1e946d32005-12-13 17:07:41 +01003489 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003490
Hans Leidekkercd2c4582006-10-05 13:18:56 +02003491 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003492
Aric Stewart1e946d32005-12-13 17:07:41 +01003493 /* REPLACE wins out over ADD */
3494 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3495 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
3496
3497 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
3498 index = -1;
3499 else
3500 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
3501
3502 if (index >= 0)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003503 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003504 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
Aric Stewartff9b9d42002-06-21 23:59:49 +00003505 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003506 return FALSE;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003507 }
Aric Stewart1e946d32005-12-13 17:07:41 +01003508 lphttpHdr = &lpwhr->pCustHeaders[index];
3509 }
3510 else if (value)
3511 {
3512 HTTPHEADERW hdr;
3513
3514 hdr.lpszField = (LPWSTR)field;
3515 hdr.lpszValue = (LPWSTR)value;
3516 hdr.wFlags = hdr.wCount = 0;
3517
3518 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3519 hdr.wFlags |= HDR_ISREQUEST;
3520
3521 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3522 }
Rob Shearman7b002a32007-01-12 19:16:49 -06003523 /* no value to delete */
3524 else return TRUE;
Aric Stewart1e946d32005-12-13 17:07:41 +01003525
3526 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3527 lphttpHdr->wFlags |= HDR_ISREQUEST;
3528 else
3529 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
3530
3531 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3532 {
3533 HTTP_DeleteCustomHeader( lpwhr, index );
3534
3535 if (value)
Aric Stewartff9b9d42002-06-21 23:59:49 +00003536 {
Mike McCormacka4e902c2004-03-30 04:36:09 +00003537 HTTPHEADERW hdr;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003538
Mike McCormacka4e902c2004-03-30 04:36:09 +00003539 hdr.lpszField = (LPWSTR)field;
3540 hdr.lpszValue = (LPWSTR)value;
Aric Stewartff9b9d42002-06-21 23:59:49 +00003541 hdr.wFlags = hdr.wCount = 0;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003542
3543 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3544 hdr.wFlags |= HDR_ISREQUEST;
3545
Mike McCormacka1c16d22003-07-22 03:17:52 +00003546 return HTTP_InsertCustomHeader(lpwhr, &hdr);
Aric Stewartff9b9d42002-06-21 23:59:49 +00003547 }
Aric Stewart1e946d32005-12-13 17:07:41 +01003548
3549 return TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003550 }
Juan Langa1ab4a72007-11-07 14:45:06 -08003551 else if (dwModifier & COALESCEFLAGS)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003552 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003553 LPWSTR lpsztmp;
3554 WCHAR ch = 0;
3555 INT len = 0;
3556 INT origlen = strlenW(lphttpHdr->lpszValue);
3557 INT valuelen = strlenW(value);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003558
Aric Stewart1e946d32005-12-13 17:07:41 +01003559 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003560 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003561 ch = ',';
3562 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3563 }
3564 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3565 {
3566 ch = ';';
3567 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003568 }
3569
Aric Stewart1e946d32005-12-13 17:07:41 +01003570 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
3571
Juan Lang9adf83e2007-11-07 14:58:14 -08003572 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
Aric Stewart1e946d32005-12-13 17:07:41 +01003573 if (lpsztmp)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003574 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003575 lphttpHdr->lpszValue = lpsztmp;
3576 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
3577 if (ch > 0)
3578 {
3579 lphttpHdr->lpszValue[origlen] = ch;
3580 origlen++;
3581 lphttpHdr->lpszValue[origlen] = ' ';
3582 origlen++;
3583 }
3584
3585 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
3586 lphttpHdr->lpszValue[len] = '\0';
Ulrich Czekallac2757242000-06-11 20:04:44 +00003587 bSuccess = TRUE;
3588 }
3589 else
3590 {
Aric Stewart1e946d32005-12-13 17:07:41 +01003591 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003592 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3593 }
3594 }
Alberto Massaribc8bd722002-12-06 23:20:31 +00003595 TRACE("<-- %d\n",bSuccess);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003596 return bSuccess;
3597}
3598
3599
3600/***********************************************************************
Rob Shearman272954b2007-01-04 18:23:17 +00003601 * HTTP_FinishedReading (internal)
3602 *
3603 * Called when all content from server has been read by client.
3604 *
3605 */
3606BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
3607{
3608 WCHAR szConnectionResponse[20];
Rob Shearman9efe0832007-01-12 19:19:18 -06003609 DWORD dwBufferSize = sizeof(szConnectionResponse);
Rob Shearman272954b2007-01-04 18:23:17 +00003610
3611 TRACE("\n");
3612
3613 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse,
3614 &dwBufferSize, NULL) ||
3615 strcmpiW(szConnectionResponse, szKeepAlive))
3616 {
Jacek Caban7dc9bf62008-02-26 20:21:00 +01003617 HTTPREQ_CloseConnection(&lpwhr->hdr);
Rob Shearman272954b2007-01-04 18:23:17 +00003618 }
3619
3620 /* FIXME: store data in the URL cache here */
3621
3622 return TRUE;
3623}
3624
Ulrich Czekallac2757242000-06-11 20:04:44 +00003625
3626/***********************************************************************
3627 * HTTP_GetCustomHeaderIndex (internal)
3628 *
3629 * Return index of custom header from header array
3630 *
3631 */
Mike McCormack92ddc1c2006-03-30 18:20:04 +09003632static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
3633 int requested_index, BOOL request_only)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003634{
Mike McCormack13b6ce6d2004-08-09 18:54:23 +00003635 DWORD index;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003636
Mike McCormacka4e902c2004-03-30 04:36:09 +00003637 TRACE("%s\n", debugstr_w(lpszField));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003638
3639 for (index = 0; index < lpwhr->nCustHeaders; index++)
3640 {
Mike McCormack92ddc1c2006-03-30 18:20:04 +09003641 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3642 continue;
3643
3644 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3645 continue;
3646
3647 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
Aric Stewart1e946d32005-12-13 17:07:41 +01003648 continue;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003649
Aric Stewart1e946d32005-12-13 17:07:41 +01003650 if (requested_index == 0)
Mike McCormack92ddc1c2006-03-30 18:20:04 +09003651 break;
3652 requested_index --;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003653 }
3654
3655 if (index >= lpwhr->nCustHeaders)
3656 index = -1;
3657
Hans Leidekkercd2c4582006-10-05 13:18:56 +02003658 TRACE("Return: %d\n", index);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003659 return index;
3660}
3661
3662
3663/***********************************************************************
3664 * HTTP_InsertCustomHeader (internal)
3665 *
3666 * Insert header into array
3667 *
3668 */
Jacek Caban02708c62005-10-26 10:07:58 +00003669static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003670{
3671 INT count;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003672 LPHTTPHEADERW lph = NULL;
Mike McCormacka1c16d22003-07-22 03:17:52 +00003673 BOOL r = FALSE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003674
Mike McCormacka4e902c2004-03-30 04:36:09 +00003675 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
Ulrich Czekallac2757242000-06-11 20:04:44 +00003676 count = lpwhr->nCustHeaders + 1;
3677 if (count > 1)
Mike McCormacka4e902c2004-03-30 04:36:09 +00003678 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003679 else
Mike McCormacka4e902c2004-03-30 04:36:09 +00003680 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003681
3682 if (NULL != lph)
3683 {
3684 lpwhr->pCustHeaders = lph;
Mike McCormacka4e902c2004-03-30 04:36:09 +00003685 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3686 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003687 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3688 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3689 lpwhr->nCustHeaders++;
Mike McCormacka1c16d22003-07-22 03:17:52 +00003690 r = TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003691 }
3692 else
3693 {
3694 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
Ulrich Czekallac2757242000-06-11 20:04:44 +00003695 }
3696
Mike McCormacka1c16d22003-07-22 03:17:52 +00003697 return r;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003698}
3699
3700
3701/***********************************************************************
3702 * HTTP_DeleteCustomHeader (internal)
3703 *
3704 * Delete header from array
Mike McCormacka1c16d22003-07-22 03:17:52 +00003705 * If this function is called, the indexs may change.
Ulrich Czekallac2757242000-06-11 20:04:44 +00003706 */
Jacek Caban02708c62005-10-26 10:07:58 +00003707static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
Ulrich Czekallac2757242000-06-11 20:04:44 +00003708{
Mike McCormacka1c16d22003-07-22 03:17:52 +00003709 if( lpwhr->nCustHeaders <= 0 )
3710 return FALSE;
Robert Shearman7707a762005-03-10 11:14:24 +00003711 if( index >= lpwhr->nCustHeaders )
Mike McCormacka1c16d22003-07-22 03:17:52 +00003712 return FALSE;
3713 lpwhr->nCustHeaders--;
3714
3715 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
Mike McCormacka4e902c2004-03-30 04:36:09 +00003716 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3717 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
Mike McCormacka1c16d22003-07-22 03:17:52 +00003718
3719 return TRUE;
Ulrich Czekallac2757242000-06-11 20:04:44 +00003720}
Alberto Massarib09eef22002-11-13 04:08:26 +00003721
Aric Stewartc8dfc022007-07-26 08:59:00 -05003722
3723/***********************************************************************
3724 * HTTP_VerifyValidHeader (internal)
3725 *
3726 * Verify the given header is not invalid for the given http request
3727 *
3728 */
3729static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
3730{
3731 BOOL rc = TRUE;
3732
3733 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
3734 if (strcmpiW(field,szAccept_Encoding)==0)
3735 return FALSE;
3736
3737 return rc;
3738}
3739
Alberto Massarib09eef22002-11-13 04:08:26 +00003740/***********************************************************************
3741 * IsHostInProxyBypassList (@)
3742 *
3743 * Undocumented
3744 *
3745 */
3746BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3747{
Hans Leidekkercd2c4582006-10-05 13:18:56 +02003748 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
Alberto Massarib09eef22002-11-13 04:08:26 +00003749 return FALSE;
3750}