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