wininet: Don't strip Accept-Encoding headers from version 1.1 requests.
[wine.git] / dlls / wininet / http.c
blob2d16924173667ad10d440b8fab976457309e4147
1 /*
2 * Wininet - Http Implementation
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
11 * Ulrich Czekalla
12 * David Hammerton
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.
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.
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
29 #include "config.h"
30 #include "wine/port.h"
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>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wininet.h"
51 #include "winerror.h"
52 #define NO_SHLWAPI_STREAM
53 #define NO_SHLWAPI_REG
54 #define NO_SHLWAPI_STRFCNS
55 #define NO_SHLWAPI_GDI
56 #include "shlwapi.h"
57 #include "sspi.h"
58 #include "wincrypt.h"
60 #include "internet.h"
61 #include "wine/debug.h"
62 #include "wine/unicode.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
66 static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
67 static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
68 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
69 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
70 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
71 static const WCHAR szHost[] = { 'H','o','s','t',0 };
72 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
74 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
75 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
76 static const WCHAR szGET[] = { 'G','E','T', 0 };
78 #define MAXHOSTNAME 100
79 #define MAX_FIELD_VALUE_LEN 256
80 #define MAX_FIELD_LEN 256
82 #define HTTP_REFERER g_szReferer
83 #define HTTP_ACCEPT g_szAccept
84 #define HTTP_USERAGENT g_szUserAgent
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
94 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
96 struct HttpAuthInfo
98 LPWSTR scheme;
99 CredHandle cred;
100 CtxtHandle ctx;
101 TimeStamp exp;
102 ULONG attr;
103 ULONG max_token;
104 void *auth_data;
105 unsigned int auth_data_len;
106 BOOL finished; /* finished authenticating */
109 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
110 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
111 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
112 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
113 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
114 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
115 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
116 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
117 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
118 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
119 lpdwIndex);
120 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
121 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
122 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field);
123 static void HTTP_DrainContent(WININETHTTPREQW *req);
125 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
127 int HeaderIndex = 0;
128 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
129 if (HeaderIndex == -1)
130 return NULL;
131 else
132 return &req->pCustHeaders[HeaderIndex];
135 /***********************************************************************
136 * HTTP_Tokenize (internal)
138 * Tokenize a string, allocating memory for the tokens.
140 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
142 LPWSTR * token_array;
143 int tokens = 0;
144 int i;
145 LPCWSTR next_token;
147 /* empty string has no tokens */
148 if (*string)
149 tokens++;
150 /* count tokens */
151 for (i = 0; string[i]; i++)
152 if (!strncmpW(string+i, token_string, strlenW(token_string)))
154 DWORD j;
155 tokens++;
156 /* we want to skip over separators, but not the null terminator */
157 for (j = 0; j < strlenW(token_string) - 1; j++)
158 if (!string[i+j])
159 break;
160 i += j;
163 /* add 1 for terminating NULL */
164 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
165 token_array[tokens] = NULL;
166 if (!tokens)
167 return token_array;
168 for (i = 0; i < tokens; i++)
170 int len;
171 next_token = strstrW(string, token_string);
172 if (!next_token) next_token = string+strlenW(string);
173 len = next_token - string;
174 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
175 memcpy(token_array[i], string, len*sizeof(WCHAR));
176 token_array[i][len] = '\0';
177 string = next_token+strlenW(token_string);
179 return token_array;
182 /***********************************************************************
183 * HTTP_FreeTokens (internal)
185 * Frees memory returned from HTTP_Tokenize.
187 static void HTTP_FreeTokens(LPWSTR * token_array)
189 int i;
190 for (i = 0; token_array[i]; i++)
191 HeapFree(GetProcessHeap(), 0, token_array[i]);
192 HeapFree(GetProcessHeap(), 0, token_array);
195 /* **********************************************************************
197 * Helper functions for the HttpSendRequest(Ex) functions
200 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
202 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
203 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
205 TRACE("%p\n", lpwhr);
207 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
208 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
209 req->dwContentLength, req->bEndRequest);
211 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
214 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
216 static const WCHAR szSlash[] = { '/',0 };
217 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
219 /* If we don't have a path we set it to root */
220 if (NULL == lpwhr->lpszPath)
221 lpwhr->lpszPath = WININET_strdupW(szSlash);
222 else /* remove \r and \n*/
224 int nLen = strlenW(lpwhr->lpszPath);
225 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
227 nLen--;
228 lpwhr->lpszPath[nLen]='\0';
230 /* Replace '\' with '/' */
231 while (nLen>0) {
232 nLen--;
233 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
237 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
238 lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
239 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
241 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
242 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
243 *fixurl = '/';
244 strcpyW(fixurl + 1, lpwhr->lpszPath);
245 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
246 lpwhr->lpszPath = fixurl;
250 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
252 LPWSTR requestString;
253 DWORD len, n;
254 LPCWSTR *req;
255 UINT i;
256 LPWSTR p;
258 static const WCHAR szSpace[] = { ' ',0 };
259 static const WCHAR szcrlf[] = {'\r','\n', 0};
260 static const WCHAR szColon[] = { ':',' ',0 };
261 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
263 /* allocate space for an array of all the string pointers to be added */
264 len = (lpwhr->nCustHeaders)*4 + 10;
265 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
267 /* add the verb, path and HTTP version string */
268 n = 0;
269 req[n++] = verb;
270 req[n++] = szSpace;
271 req[n++] = path;
272 req[n++] = szSpace;
273 req[n++] = version;
275 /* Append custom request headers */
276 for (i = 0; i < lpwhr->nCustHeaders; i++)
278 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
280 req[n++] = szcrlf;
281 req[n++] = lpwhr->pCustHeaders[i].lpszField;
282 req[n++] = szColon;
283 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
285 TRACE("Adding custom header %s (%s)\n",
286 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
287 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
291 if( n >= len )
292 ERR("oops. buffer overrun\n");
294 req[n] = NULL;
295 requestString = HTTP_build_req( req, 4 );
296 HeapFree( GetProcessHeap(), 0, req );
299 * Set (header) termination string for request
300 * Make sure there's exactly two new lines at the end of the request
302 p = &requestString[strlenW(requestString)-1];
303 while ( (*p == '\n') || (*p == '\r') )
304 p--;
305 strcpyW( p+1, sztwocrlf );
307 return requestString;
310 static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr )
312 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
313 int HeaderIndex;
314 LPHTTPHEADERW setCookieHeader;
316 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
317 if (HeaderIndex == -1)
318 return;
319 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
321 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
323 int nPosStart = 0, nPosEnd = 0, len;
324 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
326 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
328 LPWSTR buf_cookie, cookie_name, cookie_data;
329 LPWSTR buf_url;
330 LPWSTR domain = NULL;
331 LPHTTPHEADERW Host;
333 int nEqualPos = 0;
334 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
335 setCookieHeader->lpszValue[nPosEnd] != '\0')
337 nPosEnd++;
339 if (setCookieHeader->lpszValue[nPosEnd] == ';')
341 /* fixme: not case sensitive, strcasestr is gnu only */
342 int nDomainPosEnd = 0;
343 int nDomainPosStart = 0, nDomainLength = 0;
344 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
345 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
346 if (lpszDomain)
347 { /* they have specified their own domain, lets use it */
348 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
349 lpszDomain[nDomainPosEnd] != '\0')
351 nDomainPosEnd++;
353 nDomainPosStart = strlenW(szDomain);
354 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
355 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
356 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
359 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
360 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
361 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
362 TRACE("%s\n", debugstr_w(buf_cookie));
363 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
365 nEqualPos++;
367 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
369 HeapFree(GetProcessHeap(), 0, buf_cookie);
370 break;
373 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
374 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
375 cookie_data = &buf_cookie[nEqualPos + 1];
377 Host = HTTP_GetHeader(lpwhr,szHost);
378 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
379 strlenW(lpwhr->lpszPath) + 9;
380 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
381 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
382 InternetSetCookieW(buf_url, cookie_name, cookie_data);
384 HeapFree(GetProcessHeap(), 0, buf_url);
385 HeapFree(GetProcessHeap(), 0, buf_cookie);
386 HeapFree(GetProcessHeap(), 0, cookie_name);
387 HeapFree(GetProcessHeap(), 0, domain);
388 nPosStart = nPosEnd;
393 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
395 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
396 return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
397 ((pszAuthValue[ARRAYSIZE(szBasic)] != ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
400 static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
401 struct HttpAuthInfo **ppAuthInfo,
402 LPWSTR domain_and_username, LPWSTR password )
404 SECURITY_STATUS sec_status;
405 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
406 BOOL first = FALSE;
408 TRACE("%s\n", debugstr_w(pszAuthValue));
410 if (!pAuthInfo)
412 TimeStamp exp;
414 first = TRUE;
415 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
416 if (!pAuthInfo)
417 return FALSE;
419 SecInvalidateHandle(&pAuthInfo->cred);
420 SecInvalidateHandle(&pAuthInfo->ctx);
421 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
422 pAuthInfo->attr = 0;
423 pAuthInfo->auth_data = NULL;
424 pAuthInfo->auth_data_len = 0;
425 pAuthInfo->finished = FALSE;
427 if (is_basic_auth_value(pszAuthValue))
429 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
430 pAuthInfo->scheme = WININET_strdupW(szBasic);
431 if (!pAuthInfo->scheme)
433 HeapFree(GetProcessHeap(), 0, pAuthInfo);
434 return FALSE;
437 else
439 PVOID pAuthData;
440 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
442 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
443 if (!pAuthInfo->scheme)
445 HeapFree(GetProcessHeap(), 0, pAuthInfo);
446 return FALSE;
449 if (domain_and_username)
451 WCHAR *user = strchrW(domain_and_username, '\\');
452 WCHAR *domain = domain_and_username;
454 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
456 pAuthData = &nt_auth_identity;
458 if (user) user++;
459 else
461 user = domain_and_username;
462 domain = NULL;
465 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
466 nt_auth_identity.User = user;
467 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
468 nt_auth_identity.Domain = domain;
469 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
470 nt_auth_identity.Password = password;
471 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
473 else
474 /* use default credentials */
475 pAuthData = NULL;
477 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
478 SECPKG_CRED_OUTBOUND, NULL,
479 pAuthData, NULL,
480 NULL, &pAuthInfo->cred,
481 &exp);
482 if (sec_status == SEC_E_OK)
484 PSecPkgInfoW sec_pkg_info;
485 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
486 if (sec_status == SEC_E_OK)
488 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
489 FreeContextBuffer(sec_pkg_info);
492 if (sec_status != SEC_E_OK)
494 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
495 debugstr_w(pAuthInfo->scheme), sec_status);
496 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
497 HeapFree(GetProcessHeap(), 0, pAuthInfo);
498 return FALSE;
501 *ppAuthInfo = pAuthInfo;
503 else if (pAuthInfo->finished)
504 return FALSE;
506 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
507 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
509 ERR("authentication scheme changed from %s to %s\n",
510 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
511 return FALSE;
514 if (is_basic_auth_value(pszAuthValue))
516 int userlen;
517 int passlen;
518 char *auth_data;
520 TRACE("basic authentication\n");
522 /* we don't cache credentials for basic authentication, so we can't
523 * retrieve them if the application didn't pass us any credentials */
524 if (!domain_and_username) return FALSE;
526 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
527 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
529 /* length includes a nul terminator, which will be re-used for the ':' */
530 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
531 if (!auth_data)
532 return FALSE;
534 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
535 auth_data[userlen] = ':';
536 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
538 pAuthInfo->auth_data = auth_data;
539 pAuthInfo->auth_data_len = userlen + 1 + passlen;
540 pAuthInfo->finished = TRUE;
542 return TRUE;
544 else
546 LPCWSTR pszAuthData;
547 SecBufferDesc out_desc, in_desc;
548 SecBuffer out, in;
549 unsigned char *buffer;
550 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
551 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
553 in.BufferType = SECBUFFER_TOKEN;
554 in.cbBuffer = 0;
555 in.pvBuffer = NULL;
557 in_desc.ulVersion = 0;
558 in_desc.cBuffers = 1;
559 in_desc.pBuffers = &in;
561 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
562 if (*pszAuthData == ' ')
564 pszAuthData++;
565 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
566 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
567 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
570 buffer = HeapAlloc(GetProcessHeap(), 0, pAuthInfo->max_token);
572 out.BufferType = SECBUFFER_TOKEN;
573 out.cbBuffer = pAuthInfo->max_token;
574 out.pvBuffer = buffer;
576 out_desc.ulVersion = 0;
577 out_desc.cBuffers = 1;
578 out_desc.pBuffers = &out;
580 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
581 first ? NULL : &pAuthInfo->ctx,
582 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
583 context_req, 0, SECURITY_NETWORK_DREP,
584 in.pvBuffer ? &in_desc : NULL,
585 0, &pAuthInfo->ctx, &out_desc,
586 &pAuthInfo->attr, &pAuthInfo->exp);
587 if (sec_status == SEC_E_OK)
589 pAuthInfo->finished = TRUE;
590 pAuthInfo->auth_data = out.pvBuffer;
591 pAuthInfo->auth_data_len = out.cbBuffer;
592 TRACE("sending last auth packet\n");
594 else if (sec_status == SEC_I_CONTINUE_NEEDED)
596 pAuthInfo->auth_data = out.pvBuffer;
597 pAuthInfo->auth_data_len = out.cbBuffer;
598 TRACE("sending next auth packet\n");
600 else
602 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
603 pAuthInfo->finished = TRUE;
604 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
605 return FALSE;
609 return TRUE;
612 /***********************************************************************
613 * HTTP_HttpAddRequestHeadersW (internal)
615 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
616 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
618 LPWSTR lpszStart;
619 LPWSTR lpszEnd;
620 LPWSTR buffer;
621 BOOL bSuccess = FALSE;
622 DWORD len;
624 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
626 if( dwHeaderLength == ~0U )
627 len = strlenW(lpszHeader);
628 else
629 len = dwHeaderLength;
630 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
631 lstrcpynW( buffer, lpszHeader, len + 1);
633 lpszStart = buffer;
637 LPWSTR * pFieldAndValue;
639 lpszEnd = lpszStart;
641 while (*lpszEnd != '\0')
643 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
644 break;
645 lpszEnd++;
648 if (*lpszStart == '\0')
649 break;
651 if (*lpszEnd == '\r')
653 *lpszEnd = '\0';
654 lpszEnd += 2; /* Jump over \r\n */
656 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
657 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
658 if (pFieldAndValue)
660 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
661 if (bSuccess)
662 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
663 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
664 HTTP_FreeTokens(pFieldAndValue);
667 lpszStart = lpszEnd;
668 } while (bSuccess);
670 HeapFree(GetProcessHeap(), 0, buffer);
672 return bSuccess;
675 /***********************************************************************
676 * HttpAddRequestHeadersW (WININET.@)
678 * Adds one or more HTTP header to the request handler
680 * NOTE
681 * On Windows if dwHeaderLength includes the trailing '\0', then
682 * HttpAddRequestHeadersW() adds it too. However this results in an
683 * invalid Http header which is rejected by some servers so we probably
684 * don't need to match Windows on that point.
686 * RETURNS
687 * TRUE on success
688 * FALSE on failure
691 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
692 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
694 BOOL bSuccess = FALSE;
695 LPWININETHTTPREQW lpwhr;
697 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
699 if (!lpszHeader)
700 return TRUE;
702 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
703 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
705 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
706 goto lend;
708 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
709 lend:
710 if( lpwhr )
711 WININET_Release( &lpwhr->hdr );
713 return bSuccess;
716 /***********************************************************************
717 * HttpAddRequestHeadersA (WININET.@)
719 * Adds one or more HTTP header to the request handler
721 * RETURNS
722 * TRUE on success
723 * FALSE on failure
726 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
727 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
729 DWORD len;
730 LPWSTR hdr;
731 BOOL r;
733 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
735 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
736 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
737 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
738 if( dwHeaderLength != ~0U )
739 dwHeaderLength = len;
741 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
743 HeapFree( GetProcessHeap(), 0, hdr );
745 return r;
748 /***********************************************************************
749 * HttpEndRequestA (WININET.@)
751 * Ends an HTTP request that was started by HttpSendRequestEx
753 * RETURNS
754 * TRUE if successful
755 * FALSE on failure
758 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
759 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
761 LPINTERNET_BUFFERSA ptr;
762 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
763 BOOL rc = FALSE;
765 TRACE("(%p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
766 dwContext);
768 ptr = lpBuffersOut;
769 if (ptr)
770 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
771 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
772 else
773 lpBuffersOutW = NULL;
775 ptrW = lpBuffersOutW;
776 while (ptr)
778 if (ptr->lpvBuffer && ptr->dwBufferLength)
779 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
780 ptrW->dwBufferLength = ptr->dwBufferLength;
781 ptrW->dwBufferTotal= ptr->dwBufferTotal;
783 if (ptr->Next)
784 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
785 sizeof(INTERNET_BUFFERSW));
787 ptr = ptr->Next;
788 ptrW = ptrW->Next;
791 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
793 if (lpBuffersOutW)
795 ptrW = lpBuffersOutW;
796 while (ptrW)
798 LPINTERNET_BUFFERSW ptrW2;
800 FIXME("Do we need to translate info out of these buffer?\n");
802 HeapFree(GetProcessHeap(),0,ptrW->lpvBuffer);
803 ptrW2 = ptrW->Next;
804 HeapFree(GetProcessHeap(),0,ptrW);
805 ptrW = ptrW2;
809 return rc;
812 /***********************************************************************
813 * HttpEndRequestW (WININET.@)
815 * Ends an HTTP request that was started by HttpSendRequestEx
817 * RETURNS
818 * TRUE if successful
819 * FALSE on failure
822 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
823 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
825 BOOL rc = FALSE;
826 LPWININETHTTPREQW lpwhr;
827 INT responseLen;
828 DWORD dwBufferSize;
830 TRACE("-->\n");
831 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
833 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
835 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
836 if (lpwhr)
837 WININET_Release( &lpwhr->hdr );
838 return FALSE;
841 lpwhr->hdr.dwFlags |= dwFlags;
842 lpwhr->hdr.dwContext = dwContext;
844 /* We appear to do nothing with lpBuffersOut.. is that correct? */
846 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
847 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
849 responseLen = HTTP_GetResponseHeaders(lpwhr);
850 if (responseLen)
851 rc = TRUE;
853 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
854 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
856 /* process cookies here. Is this right? */
857 HTTP_ProcessCookies(lpwhr);
859 dwBufferSize = sizeof(lpwhr->dwContentLength);
860 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
861 &lpwhr->dwContentLength,&dwBufferSize,NULL))
862 lpwhr->dwContentLength = -1;
864 if (lpwhr->dwContentLength == 0)
865 HTTP_FinishedReading(lpwhr);
867 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
869 DWORD dwCode,dwCodeLength=sizeof(DWORD);
870 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
871 (dwCode==302 || dwCode==301))
873 WCHAR szNewLocation[2048];
874 dwBufferSize=sizeof(szNewLocation);
875 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
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);
888 WININET_Release( &lpwhr->hdr );
889 TRACE("%i <--\n",rc);
890 return rc;
893 /***********************************************************************
894 * HttpOpenRequestW (WININET.@)
896 * Open a HTTP request handle
898 * RETURNS
899 * HINTERNET a HTTP request handle on success
900 * NULL on failure
903 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
904 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
905 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
906 DWORD dwFlags, DWORD_PTR dwContext)
908 LPWININETHTTPSESSIONW lpwhs;
909 HINTERNET handle = NULL;
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)
917 int i;
918 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
919 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
922 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
923 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
925 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
926 goto lend;
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.
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;
947 /***********************************************************************
948 * HttpOpenRequestA (WININET.@)
950 * Open a HTTP request handle
952 * RETURNS
953 * HINTERNET a HTTP request handle on success
954 * NULL on failure
957 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
958 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
959 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
960 DWORD dwFlags, DWORD_PTR dwContext)
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);
972 if (lpszVerb)
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);
981 if (lpszObjectName)
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 );
990 if (lpszVersion)
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 );
999 if (lpszReferrer)
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 );
1008 acceptTypesCount = 0;
1009 if (lpszAcceptTypes)
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])
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++;
1027 szAcceptTypes[acceptTypesCount] = NULL;
1029 else szAcceptTypes = 0;
1031 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1032 szVersion, szReferrer,
1033 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1035 end:
1036 if (szAcceptTypes)
1038 acceptTypesCount = 0;
1039 while (szAcceptTypes[acceptTypesCount])
1041 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1042 acceptTypesCount++;
1044 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1046 HeapFree(GetProcessHeap(), 0, szReferrer);
1047 HeapFree(GetProcessHeap(), 0, szVersion);
1048 HeapFree(GetProcessHeap(), 0, szObjectName);
1049 HeapFree(GetProcessHeap(), 0, szVerb);
1051 return rc;
1054 /***********************************************************************
1055 * HTTP_EncodeBase64
1057 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1059 UINT n = 0, x;
1060 static const CHAR HTTP_Base64Enc[] =
1061 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1063 while( len > 0 )
1065 /* first 6 bits, all from bin[0] */
1066 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1067 x = (bin[0] & 3) << 4;
1069 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1070 if( len == 1 )
1072 base64[n++] = HTTP_Base64Enc[x];
1073 base64[n++] = '=';
1074 base64[n++] = '=';
1075 break;
1077 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1078 x = ( bin[1] & 0x0f ) << 2;
1080 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1081 if( len == 2 )
1083 base64[n++] = HTTP_Base64Enc[x];
1084 base64[n++] = '=';
1085 break;
1087 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1089 /* last 6 bits, all from bin [2] */
1090 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1091 bin += 3;
1092 len -= 3;
1094 base64[n] = 0;
1095 return n;
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] =
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),
1131 #undef CH
1133 /***********************************************************************
1134 * HTTP_DecodeBase64
1136 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1138 unsigned int n = 0;
1140 while(*base64)
1142 signed char in[4];
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))
1149 WARN("invalid base64: %s\n", debugstr_w(base64));
1150 return 0;
1152 if (bin)
1153 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1154 n++;
1156 if ((base64[2] == '=') && (base64[3] == '='))
1157 break;
1158 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1159 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1161 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1162 return 0;
1164 if (bin)
1165 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1166 n++;
1168 if (base64[3] == '=')
1169 break;
1170 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1171 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1173 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1174 return 0;
1176 if (bin)
1177 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1178 n++;
1180 base64 += 4;
1183 return n;
1186 /***********************************************************************
1187 * HTTP_InsertAuthorizationForHeader
1189 * Insert or delete the authorization field in the request header.
1191 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1193 if (pAuthInfo)
1195 static const WCHAR wszSpace[] = {' ',0};
1196 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1197 unsigned int len;
1198 WCHAR *authorization = NULL;
1200 if (pAuthInfo->auth_data_len)
1202 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1203 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1204 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1205 if (!authorization)
1206 return FALSE;
1208 strcpyW(authorization, pAuthInfo->scheme);
1209 strcatW(authorization, wszSpace);
1210 HTTP_EncodeBase64(pAuthInfo->auth_data,
1211 pAuthInfo->auth_data_len,
1212 authorization+strlenW(authorization));
1214 /* clear the data as it isn't valid now that it has been sent to the
1215 * server, unless it's Basic authentication which doesn't do
1216 * connection tracking */
1217 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1219 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1220 pAuthInfo->auth_data = NULL;
1221 pAuthInfo->auth_data_len = 0;
1225 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1227 HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1229 HeapFree(GetProcessHeap(), 0, authorization);
1231 return TRUE;
1234 /***********************************************************************
1235 * HTTP_DealWithProxy
1237 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1238 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1240 WCHAR buf[MAXHOSTNAME];
1241 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1242 WCHAR* url;
1243 static WCHAR szNul[] = { 0 };
1244 URL_COMPONENTSW UrlComponents;
1245 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
1246 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
1247 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1248 int len;
1250 memset( &UrlComponents, 0, sizeof UrlComponents );
1251 UrlComponents.dwStructSize = sizeof UrlComponents;
1252 UrlComponents.lpszHostName = buf;
1253 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1255 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1256 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1257 sprintfW(proxy, szFormat1, hIC->lpszProxy);
1258 else
1259 strcpyW(proxy, hIC->lpszProxy);
1260 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1261 return FALSE;
1262 if( UrlComponents.dwHostNameLength == 0 )
1263 return FALSE;
1265 if( !lpwhr->lpszPath )
1266 lpwhr->lpszPath = szNul;
1267 TRACE("server=%s path=%s\n",
1268 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
1269 /* for constant 15 see above */
1270 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
1271 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1273 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1274 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1276 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
1278 if( lpwhr->lpszPath[0] != '/' )
1279 strcatW( url, szSlash );
1280 strcatW(url, lpwhr->lpszPath);
1281 if(lpwhr->lpszPath != szNul)
1282 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1283 lpwhr->lpszPath = url;
1285 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1286 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1287 lpwhs->nServerPort = UrlComponents.nPort;
1289 return TRUE;
1292 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1294 char szaddr[32];
1295 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1297 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1298 INTERNET_STATUS_RESOLVING_NAME,
1299 lpwhs->lpszServerName,
1300 strlenW(lpwhs->lpszServerName)+1);
1302 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1303 &lpwhs->socketAddress))
1305 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1306 return FALSE;
1309 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1310 szaddr, sizeof(szaddr));
1311 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1312 INTERNET_STATUS_NAME_RESOLVED,
1313 szaddr, strlen(szaddr)+1);
1314 return TRUE;
1318 /***********************************************************************
1319 * HTTPREQ_Destroy (internal)
1321 * Deallocate request handle
1324 static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1326 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1327 DWORD i;
1329 TRACE("\n");
1331 if(lpwhr->hCacheFile)
1332 CloseHandle(lpwhr->hCacheFile);
1334 if(lpwhr->lpszCacheFile) {
1335 DeleteFileW(lpwhr->lpszCacheFile); /* FIXME */
1336 HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1339 WININET_Release(&lpwhr->lpHttpSession->hdr);
1341 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1342 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1343 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1344 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1345 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1347 for (i = 0; i < lpwhr->nCustHeaders; i++)
1349 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1350 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1353 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1354 HeapFree(GetProcessHeap(), 0, lpwhr);
1357 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1359 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1360 LPWININETHTTPSESSIONW lpwhs = NULL;
1361 LPWININETAPPINFOW hIC = NULL;
1363 TRACE("%p\n",lpwhr);
1365 if (!NETCON_connected(&lpwhr->netConnection))
1366 return;
1368 if (lpwhr->pAuthInfo)
1370 if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1371 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1372 if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1373 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1375 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1376 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1377 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1378 lpwhr->pAuthInfo = NULL;
1380 if (lpwhr->pProxyAuthInfo)
1382 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1383 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1384 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1385 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1387 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1388 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1389 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1390 lpwhr->pProxyAuthInfo = NULL;
1393 lpwhs = lpwhr->lpHttpSession;
1394 hIC = lpwhs->lpAppInfo;
1396 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1397 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1399 NETCON_close(&lpwhr->netConnection);
1401 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1402 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1405 static DWORD HTTPREQ_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1407 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1409 switch(option) {
1410 case INTERNET_OPTION_HANDLE_TYPE:
1411 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1413 if (*size < sizeof(ULONG))
1414 return ERROR_INSUFFICIENT_BUFFER;
1416 *size = sizeof(DWORD);
1417 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1418 return ERROR_SUCCESS;
1420 case INTERNET_OPTION_URL: {
1421 WCHAR url[INTERNET_MAX_URL_LENGTH];
1422 HTTPHEADERW *host;
1423 DWORD len;
1425 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
1426 static const WCHAR hostW[] = {'H','o','s','t',0};
1428 TRACE("INTERNET_OPTION_URL\n");
1430 host = HTTP_GetHeader(req, hostW);
1431 sprintfW(url, formatW, host->lpszValue, req->lpszPath);
1432 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1434 if(unicode) {
1435 len = (strlenW(url)+1) * sizeof(WCHAR);
1436 if(*size < len)
1437 return ERROR_INSUFFICIENT_BUFFER;
1439 *size = len;
1440 strcpyW(buffer, url);
1441 return ERROR_SUCCESS;
1442 }else {
1443 len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1444 if(len > *size)
1445 return ERROR_INSUFFICIENT_BUFFER;
1447 *size = len;
1448 return ERROR_SUCCESS;
1452 case INTERNET_OPTION_DATAFILE_NAME: {
1453 DWORD req_size;
1455 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1457 if(!req->lpszCacheFile) {
1458 *size = 0;
1459 return ERROR_INTERNET_ITEM_NOT_FOUND;
1462 if(unicode) {
1463 req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1464 if(*size < req_size)
1465 return ERROR_INSUFFICIENT_BUFFER;
1467 *size = req_size;
1468 memcpy(buffer, req->lpszCacheFile, *size);
1469 return ERROR_SUCCESS;
1470 }else {
1471 req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1472 if (req_size > *size)
1473 return ERROR_INSUFFICIENT_BUFFER;
1475 *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1476 -1, buffer, *size, NULL, NULL);
1477 return ERROR_SUCCESS;
1481 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1482 PCCERT_CONTEXT context;
1484 if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1485 *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1486 return ERROR_INSUFFICIENT_BUFFER;
1489 context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1490 if(context) {
1491 INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1492 DWORD len;
1494 memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1495 info->ftExpiry = context->pCertInfo->NotAfter;
1496 info->ftStart = context->pCertInfo->NotBefore;
1497 if(unicode) {
1498 len = CertNameToStrW(context->dwCertEncodingType,
1499 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1500 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1501 if(info->lpszSubjectInfo)
1502 CertNameToStrW(context->dwCertEncodingType,
1503 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1504 info->lpszSubjectInfo, len);
1505 len = CertNameToStrW(context->dwCertEncodingType,
1506 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1507 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1508 if (info->lpszIssuerInfo)
1509 CertNameToStrW(context->dwCertEncodingType,
1510 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1511 info->lpszIssuerInfo, len);
1512 }else {
1513 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1515 len = CertNameToStrA(context->dwCertEncodingType,
1516 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1517 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1518 if(infoA->lpszSubjectInfo)
1519 CertNameToStrA(context->dwCertEncodingType,
1520 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1521 infoA->lpszSubjectInfo, len);
1522 len = CertNameToStrA(context->dwCertEncodingType,
1523 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1524 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1525 if(infoA->lpszIssuerInfo)
1526 CertNameToStrA(context->dwCertEncodingType,
1527 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1528 infoA->lpszIssuerInfo, len);
1532 * Contrary to MSDN, these do not appear to be set.
1533 * lpszProtocolName
1534 * lpszSignatureAlgName
1535 * lpszEncryptionAlgName
1536 * dwKeySize
1538 CertFreeCertificateContext(context);
1539 return ERROR_SUCCESS;
1544 FIXME("Not implemented option %d\n", option);
1545 return ERROR_INTERNET_INVALID_OPTION;
1548 static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1550 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1552 switch(option) {
1553 case INTERNET_OPTION_SEND_TIMEOUT:
1554 case INTERNET_OPTION_RECEIVE_TIMEOUT:
1555 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1557 if (size != sizeof(DWORD))
1558 return ERROR_INVALID_PARAMETER;
1560 return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1561 *(DWORD*)buffer);
1564 return ERROR_INTERNET_INVALID_OPTION;
1567 static DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1569 int bytes_read;
1571 if(!NETCON_recv(&req->netConnection, buffer, min(size, req->dwContentLength - req->dwContentRead),
1572 sync ? MSG_WAITALL : 0, &bytes_read)) {
1573 if(req->dwContentLength != -1 && req->dwContentRead != req->dwContentLength)
1574 ERR("not all data received %d/%d\n", req->dwContentRead, req->dwContentLength);
1576 /* always returns TRUE, even if the network layer returns an
1577 * error */
1578 *read = 0;
1579 HTTP_FinishedReading(req);
1580 return ERROR_SUCCESS;
1583 req->dwContentRead += bytes_read;
1584 *read = bytes_read;
1586 if(req->lpszCacheFile) {
1587 BOOL res;
1589 res = WriteFile(req->hCacheFile, buffer, bytes_read, NULL, NULL);
1590 if(!res)
1591 WARN("WriteFile failed: %u\n", GetLastError());
1594 if(!bytes_read && (req->dwContentRead == req->dwContentLength))
1595 HTTP_FinishedReading(req);
1597 return ERROR_SUCCESS;
1600 static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1602 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1604 return HTTPREQ_Read(req, buffer, size, read, TRUE);
1607 static void HTTPREQ_AsyncReadFileExProc(WORKREQUEST *workRequest)
1609 struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
1610 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1611 INTERNET_ASYNC_RESULT iar;
1612 DWORD res;
1614 TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
1616 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1617 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1619 iar.dwResult = res == ERROR_SUCCESS;
1620 iar.dwError = res;
1622 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1623 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1624 sizeof(INTERNET_ASYNC_RESULT));
1627 static DWORD HTTPREQ_ReadFileExA(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSA *buffers,
1628 DWORD flags, DWORD_PTR context)
1631 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1632 DWORD res;
1634 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1635 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1637 if (buffers->dwStructSize != sizeof(*buffers))
1638 return ERROR_INVALID_PARAMETER;
1640 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1642 /* FIXME: IRF_ASYNC may not be the right thing to test here;
1643 * hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC is probably better */
1644 if (flags & IRF_ASYNC) {
1645 DWORD available = 0;
1647 NETCON_query_data_available(&req->netConnection, &available);
1648 if (!available)
1650 WORKREQUEST workRequest;
1652 workRequest.asyncproc = HTTPREQ_AsyncReadFileExProc;
1653 workRequest.hdr = WININET_AddRef(&req->hdr);
1654 workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
1656 INTERNET_AsyncCall(&workRequest);
1658 return ERROR_IO_PENDING;
1662 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1663 !(flags & IRF_NO_WAIT));
1665 if (res == ERROR_SUCCESS) {
1666 DWORD size = buffers->dwBufferLength;
1667 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1668 &size, sizeof(size));
1671 return res;
1674 static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1676 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
1678 return NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written);
1681 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1683 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1684 INTERNET_ASYNC_RESULT iar;
1685 char buffer[4048];
1687 TRACE("%p\n", workRequest->hdr);
1689 iar.dwResult = NETCON_recv(&req->netConnection, buffer,
1690 min(sizeof(buffer), req->dwContentLength - req->dwContentRead),
1691 MSG_PEEK, (int *)&iar.dwError);
1693 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1694 sizeof(INTERNET_ASYNC_RESULT));
1697 static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1699 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1700 BYTE buffer[4048];
1701 BOOL async;
1703 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
1705 if(!NETCON_query_data_available(&req->netConnection, available) || *available)
1706 return ERROR_SUCCESS;
1708 /* Even if we are in async mode, we need to determine whether
1709 * there is actually more data available. We do this by trying
1710 * to peek only a single byte in async mode. */
1711 async = (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) != 0;
1713 if (NETCON_recv(&req->netConnection, buffer,
1714 min(async ? 1 : sizeof(buffer), req->dwContentLength - req->dwContentRead),
1715 MSG_PEEK, (int *)available) && async && *available)
1717 WORKREQUEST workRequest;
1719 *available = 0;
1720 workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
1721 workRequest.hdr = WININET_AddRef( &req->hdr );
1723 INTERNET_AsyncCall(&workRequest);
1725 return ERROR_IO_PENDING;
1728 return ERROR_SUCCESS;
1731 static const HANDLEHEADERVtbl HTTPREQVtbl = {
1732 HTTPREQ_Destroy,
1733 HTTPREQ_CloseConnection,
1734 HTTPREQ_QueryOption,
1735 HTTPREQ_SetOption,
1736 HTTPREQ_ReadFile,
1737 HTTPREQ_ReadFileExA,
1738 HTTPREQ_WriteFile,
1739 HTTPREQ_QueryDataAvailable,
1740 NULL
1743 /***********************************************************************
1744 * HTTP_HttpOpenRequestW (internal)
1746 * Open a HTTP request handle
1748 * RETURNS
1749 * HINTERNET a HTTP request handle on success
1750 * NULL on failure
1753 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1754 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1755 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1756 DWORD dwFlags, DWORD_PTR dwContext)
1758 LPWININETAPPINFOW hIC = NULL;
1759 LPWININETHTTPREQW lpwhr;
1760 LPWSTR lpszCookies;
1761 LPWSTR lpszUrl = NULL;
1762 DWORD nCookieSize;
1763 HINTERNET handle = NULL;
1764 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
1765 DWORD len;
1766 LPHTTPHEADERW Host;
1768 TRACE("-->\n");
1770 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1771 hIC = lpwhs->lpAppInfo;
1773 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1774 if (NULL == lpwhr)
1776 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1777 goto lend;
1779 lpwhr->hdr.htype = WH_HHTTPREQ;
1780 lpwhr->hdr.vtbl = &HTTPREQVtbl;
1781 lpwhr->hdr.dwFlags = dwFlags;
1782 lpwhr->hdr.dwContext = dwContext;
1783 lpwhr->hdr.refs = 1;
1784 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1785 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
1787 WININET_AddRef( &lpwhs->hdr );
1788 lpwhr->lpHttpSession = lpwhs;
1789 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
1791 handle = WININET_AllocHandle( &lpwhr->hdr );
1792 if (NULL == handle)
1794 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1795 goto lend;
1798 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1800 InternetCloseHandle( handle );
1801 handle = NULL;
1802 goto lend;
1805 if (lpszObjectName && *lpszObjectName) {
1806 HRESULT rc;
1808 len = 0;
1809 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1810 if (rc != E_POINTER)
1811 len = strlenW(lpszObjectName)+1;
1812 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1813 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1814 URL_ESCAPE_SPACES_ONLY);
1815 if (rc)
1817 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
1818 strcpyW(lpwhr->lpszPath,lpszObjectName);
1822 if (lpszReferrer && *lpszReferrer)
1823 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1825 if (lpszAcceptTypes)
1827 int i;
1828 for (i = 0; lpszAcceptTypes[i]; i++)
1830 if (!*lpszAcceptTypes[i]) continue;
1831 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
1832 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
1833 HTTP_ADDHDR_FLAG_REQ |
1834 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
1838 lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
1840 if (lpszVersion)
1841 lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
1842 else
1843 lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
1845 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1847 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1848 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1849 INTERNET_DEFAULT_HTTPS_PORT :
1850 INTERNET_DEFAULT_HTTP_PORT);
1851 lpwhs->nHostPort = lpwhs->nServerPort;
1853 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1854 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1856 if (hIC->lpszAgent)
1858 WCHAR *agent_header;
1859 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1861 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1862 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1863 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1865 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1866 HTTP_ADDREQ_FLAG_ADD);
1867 HeapFree(GetProcessHeap(), 0, agent_header);
1870 Host = HTTP_GetHeader(lpwhr,szHost);
1872 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1873 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1874 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1876 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1877 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1879 int cnt = 0;
1880 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1881 static const WCHAR szcrlf[] = {'\r','\n',0};
1883 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1885 cnt += sprintfW(lpszCookies, szCookie);
1886 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1887 strcatW(lpszCookies, szcrlf);
1889 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1890 HTTP_ADDREQ_FLAG_ADD);
1891 HeapFree(GetProcessHeap(), 0, lpszCookies);
1893 HeapFree(GetProcessHeap(), 0, lpszUrl);
1896 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1897 INTERNET_STATUS_HANDLE_CREATED, &handle,
1898 sizeof(handle));
1901 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1904 if (!HTTP_ResolveName(lpwhr))
1906 InternetCloseHandle( handle );
1907 handle = NULL;
1910 lend:
1911 if( lpwhr )
1912 WININET_Release( &lpwhr->hdr );
1914 TRACE("<-- %p (%p)\n", handle, lpwhr);
1915 return handle;
1918 /* read any content returned by the server so that the connection can be
1919 * reused */
1920 static void HTTP_DrainContent(WININETHTTPREQW *req)
1922 DWORD bytes_read;
1924 if (!NETCON_connected(&req->netConnection)) return;
1926 if (req->dwContentLength == -1)
1927 NETCON_close(&req->netConnection);
1931 char buffer[2048];
1932 if (HTTPREQ_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
1933 return;
1934 } while (bytes_read);
1937 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1938 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1939 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1940 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1941 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1942 static const WCHAR szAge[] = { 'A','g','e',0 };
1943 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1944 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1945 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1946 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1947 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1948 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1949 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1950 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1951 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1952 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1953 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1954 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 };
1955 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1956 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1957 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1958 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1959 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1960 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1961 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1962 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1963 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1964 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1965 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1966 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1967 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1968 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1969 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1970 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1971 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1972 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1973 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1974 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1975 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1976 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1977 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1978 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1979 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1980 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1981 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 };
1982 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1983 static const WCHAR szURI[] = { 'U','R','I',0 };
1984 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1985 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1986 static const WCHAR szVia[] = { 'V','i','a',0 };
1987 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1988 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1990 static const LPCWSTR header_lookup[] = {
1991 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
1992 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1993 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1994 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
1995 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1996 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1997 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1998 szAllow, /* HTTP_QUERY_ALLOW = 7 */
1999 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
2000 szDate, /* HTTP_QUERY_DATE = 9 */
2001 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
2002 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2003 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
2004 szURI, /* HTTP_QUERY_URI = 13 */
2005 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
2006 NULL, /* HTTP_QUERY_COST = 15 */
2007 NULL, /* HTTP_QUERY_LINK = 16 */
2008 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
2009 NULL, /* HTTP_QUERY_VERSION = 18 */
2010 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
2011 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
2012 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
2013 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2014 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
2015 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
2016 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2017 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2018 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2019 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
2020 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2021 NULL, /* HTTP_QUERY_FORWARDED = 30 */
2022 NULL, /* HTTP_QUERY_FROM = 31 */
2023 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2024 szLocation, /* HTTP_QUERY_LOCATION = 33 */
2025 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
2026 szReferer, /* HTTP_QUERY_REFERER = 35 */
2027 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
2028 szServer, /* HTTP_QUERY_SERVER = 37 */
2029 NULL, /* HTTP_TITLE = 38 */
2030 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
2031 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2032 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2033 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2034 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
2035 szCookie, /* HTTP_QUERY_COOKIE = 44 */
2036 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2037 NULL, /* HTTP_QUERY_REFRESH = 46 */
2038 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2039 szAge, /* HTTP_QUERY_AGE = 48 */
2040 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2041 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
2042 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2043 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2044 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2045 szETag, /* HTTP_QUERY_ETAG = 54 */
2046 szHost, /* HTTP_QUERY_HOST = 55 */
2047 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
2048 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2049 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
2050 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2051 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2052 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2053 szRange, /* HTTP_QUERY_RANGE = 62 */
2054 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2055 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
2056 szVary, /* HTTP_QUERY_VARY = 65 */
2057 szVia, /* HTTP_QUERY_VIA = 66 */
2058 szWarning, /* HTTP_QUERY_WARNING = 67 */
2059 szExpect, /* HTTP_QUERY_EXPECT = 68 */
2060 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2061 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2064 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2066 /***********************************************************************
2067 * HTTP_HttpQueryInfoW (internal)
2069 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
2070 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2072 LPHTTPHEADERW lphttpHdr = NULL;
2073 BOOL bSuccess = FALSE;
2074 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
2075 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2076 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2077 INT index = -1;
2079 /* Find requested header structure */
2080 switch (level)
2082 case HTTP_QUERY_CUSTOM:
2083 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2084 break;
2086 case HTTP_QUERY_RAW_HEADERS_CRLF:
2088 LPWSTR headers;
2089 DWORD len;
2090 BOOL ret;
2092 if (request_only)
2093 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
2094 else
2095 headers = lpwhr->lpszRawHeaders;
2097 len = strlenW(headers);
2098 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2100 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2101 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2102 ret = FALSE;
2103 } else
2105 memcpy(lpBuffer, headers, (len+1)*sizeof(WCHAR));
2106 *lpdwBufferLength = len * sizeof(WCHAR);
2108 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2109 ret = TRUE;
2112 if (request_only)
2113 HeapFree(GetProcessHeap(), 0, headers);
2114 return ret;
2116 case HTTP_QUERY_RAW_HEADERS:
2118 static const WCHAR szCrLf[] = {'\r','\n',0};
2119 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
2120 DWORD i, size = 0;
2121 LPWSTR pszString = (WCHAR*)lpBuffer;
2123 for (i = 0; ppszRawHeaderLines[i]; i++)
2124 size += strlenW(ppszRawHeaderLines[i]) + 1;
2126 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2128 HTTP_FreeTokens(ppszRawHeaderLines);
2129 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2130 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2131 return FALSE;
2134 for (i = 0; ppszRawHeaderLines[i]; i++)
2136 DWORD len = strlenW(ppszRawHeaderLines[i]);
2137 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2138 pszString += len+1;
2140 *pszString = '\0';
2142 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
2144 *lpdwBufferLength = size * sizeof(WCHAR);
2145 HTTP_FreeTokens(ppszRawHeaderLines);
2147 return TRUE;
2149 case HTTP_QUERY_STATUS_TEXT:
2150 if (lpwhr->lpszStatusText)
2152 DWORD len = strlenW(lpwhr->lpszStatusText);
2153 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2155 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2156 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2157 return FALSE;
2159 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
2160 *lpdwBufferLength = len * sizeof(WCHAR);
2162 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2164 return TRUE;
2166 break;
2167 case HTTP_QUERY_VERSION:
2168 if (lpwhr->lpszVersion)
2170 DWORD len = strlenW(lpwhr->lpszVersion);
2171 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2173 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2174 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2175 return FALSE;
2177 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
2178 *lpdwBufferLength = len * sizeof(WCHAR);
2180 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2182 return TRUE;
2184 break;
2185 default:
2186 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2188 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
2189 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2190 requested_index,request_only);
2193 if (index >= 0)
2194 lphttpHdr = &lpwhr->pCustHeaders[index];
2196 /* Ensure header satisfies requested attributes */
2197 if (!lphttpHdr ||
2198 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2199 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
2201 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2202 return bSuccess;
2205 if (lpdwIndex)
2206 (*lpdwIndex)++;
2208 /* coalesce value to requested type */
2209 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
2211 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2212 bSuccess = TRUE;
2214 TRACE(" returning number : %d\n", *(int *)lpBuffer);
2216 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
2218 time_t tmpTime;
2219 struct tm tmpTM;
2220 SYSTEMTIME *STHook;
2222 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2224 tmpTM = *gmtime(&tmpTime);
2225 STHook = (SYSTEMTIME *) lpBuffer;
2226 if(STHook==NULL)
2227 return bSuccess;
2229 STHook->wDay = tmpTM.tm_mday;
2230 STHook->wHour = tmpTM.tm_hour;
2231 STHook->wMilliseconds = 0;
2232 STHook->wMinute = tmpTM.tm_min;
2233 STHook->wDayOfWeek = tmpTM.tm_wday;
2234 STHook->wMonth = tmpTM.tm_mon + 1;
2235 STHook->wSecond = tmpTM.tm_sec;
2236 STHook->wYear = tmpTM.tm_year;
2238 bSuccess = TRUE;
2240 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2241 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2242 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2244 else if (lphttpHdr->lpszValue)
2246 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
2248 if (len > *lpdwBufferLength)
2250 *lpdwBufferLength = len;
2251 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2252 return bSuccess;
2255 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2256 *lpdwBufferLength = len - sizeof(WCHAR);
2257 bSuccess = TRUE;
2259 TRACE(" returning string : %s\n", debugstr_w(lpBuffer));
2261 return bSuccess;
2264 /***********************************************************************
2265 * HttpQueryInfoW (WININET.@)
2267 * Queries for information about an HTTP request
2269 * RETURNS
2270 * TRUE on success
2271 * FALSE on failure
2274 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2275 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2277 BOOL bSuccess = FALSE;
2278 LPWININETHTTPREQW lpwhr;
2280 if (TRACE_ON(wininet)) {
2281 #define FE(x) { x, #x }
2282 static const wininet_flag_info query_flags[] = {
2283 FE(HTTP_QUERY_MIME_VERSION),
2284 FE(HTTP_QUERY_CONTENT_TYPE),
2285 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2286 FE(HTTP_QUERY_CONTENT_ID),
2287 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2288 FE(HTTP_QUERY_CONTENT_LENGTH),
2289 FE(HTTP_QUERY_CONTENT_LANGUAGE),
2290 FE(HTTP_QUERY_ALLOW),
2291 FE(HTTP_QUERY_PUBLIC),
2292 FE(HTTP_QUERY_DATE),
2293 FE(HTTP_QUERY_EXPIRES),
2294 FE(HTTP_QUERY_LAST_MODIFIED),
2295 FE(HTTP_QUERY_MESSAGE_ID),
2296 FE(HTTP_QUERY_URI),
2297 FE(HTTP_QUERY_DERIVED_FROM),
2298 FE(HTTP_QUERY_COST),
2299 FE(HTTP_QUERY_LINK),
2300 FE(HTTP_QUERY_PRAGMA),
2301 FE(HTTP_QUERY_VERSION),
2302 FE(HTTP_QUERY_STATUS_CODE),
2303 FE(HTTP_QUERY_STATUS_TEXT),
2304 FE(HTTP_QUERY_RAW_HEADERS),
2305 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2306 FE(HTTP_QUERY_CONNECTION),
2307 FE(HTTP_QUERY_ACCEPT),
2308 FE(HTTP_QUERY_ACCEPT_CHARSET),
2309 FE(HTTP_QUERY_ACCEPT_ENCODING),
2310 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2311 FE(HTTP_QUERY_AUTHORIZATION),
2312 FE(HTTP_QUERY_CONTENT_ENCODING),
2313 FE(HTTP_QUERY_FORWARDED),
2314 FE(HTTP_QUERY_FROM),
2315 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2316 FE(HTTP_QUERY_LOCATION),
2317 FE(HTTP_QUERY_ORIG_URI),
2318 FE(HTTP_QUERY_REFERER),
2319 FE(HTTP_QUERY_RETRY_AFTER),
2320 FE(HTTP_QUERY_SERVER),
2321 FE(HTTP_QUERY_TITLE),
2322 FE(HTTP_QUERY_USER_AGENT),
2323 FE(HTTP_QUERY_WWW_AUTHENTICATE),
2324 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2325 FE(HTTP_QUERY_ACCEPT_RANGES),
2326 FE(HTTP_QUERY_SET_COOKIE),
2327 FE(HTTP_QUERY_COOKIE),
2328 FE(HTTP_QUERY_REQUEST_METHOD),
2329 FE(HTTP_QUERY_REFRESH),
2330 FE(HTTP_QUERY_CONTENT_DISPOSITION),
2331 FE(HTTP_QUERY_AGE),
2332 FE(HTTP_QUERY_CACHE_CONTROL),
2333 FE(HTTP_QUERY_CONTENT_BASE),
2334 FE(HTTP_QUERY_CONTENT_LOCATION),
2335 FE(HTTP_QUERY_CONTENT_MD5),
2336 FE(HTTP_QUERY_CONTENT_RANGE),
2337 FE(HTTP_QUERY_ETAG),
2338 FE(HTTP_QUERY_HOST),
2339 FE(HTTP_QUERY_IF_MATCH),
2340 FE(HTTP_QUERY_IF_NONE_MATCH),
2341 FE(HTTP_QUERY_IF_RANGE),
2342 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2343 FE(HTTP_QUERY_MAX_FORWARDS),
2344 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2345 FE(HTTP_QUERY_RANGE),
2346 FE(HTTP_QUERY_TRANSFER_ENCODING),
2347 FE(HTTP_QUERY_UPGRADE),
2348 FE(HTTP_QUERY_VARY),
2349 FE(HTTP_QUERY_VIA),
2350 FE(HTTP_QUERY_WARNING),
2351 FE(HTTP_QUERY_CUSTOM)
2353 static const wininet_flag_info modifier_flags[] = {
2354 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2355 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2356 FE(HTTP_QUERY_FLAG_NUMBER),
2357 FE(HTTP_QUERY_FLAG_COALESCE)
2359 #undef FE
2360 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2361 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
2362 DWORD i;
2364 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
2365 TRACE(" Attribute:");
2366 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2367 if (query_flags[i].val == info) {
2368 TRACE(" %s", query_flags[i].name);
2369 break;
2372 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
2373 TRACE(" Unknown (%08x)", info);
2376 TRACE(" Modifier:");
2377 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2378 if (modifier_flags[i].val & info_mod) {
2379 TRACE(" %s", modifier_flags[i].name);
2380 info_mod &= ~ modifier_flags[i].val;
2384 if (info_mod) {
2385 TRACE(" Unknown (%08x)", info_mod);
2387 TRACE("\n");
2390 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2391 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2393 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2394 goto lend;
2397 if (lpBuffer == NULL)
2398 *lpdwBufferLength = 0;
2399 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2400 lpBuffer, lpdwBufferLength, lpdwIndex);
2402 lend:
2403 if( lpwhr )
2404 WININET_Release( &lpwhr->hdr );
2406 TRACE("%d <--\n", bSuccess);
2407 return bSuccess;
2410 /***********************************************************************
2411 * HttpQueryInfoA (WININET.@)
2413 * Queries for information about an HTTP request
2415 * RETURNS
2416 * TRUE on success
2417 * FALSE on failure
2420 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2421 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2423 BOOL result;
2424 DWORD len;
2425 WCHAR* bufferW;
2427 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2428 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2430 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2431 lpdwBufferLength, lpdwIndex );
2434 if (lpBuffer)
2436 DWORD alloclen;
2437 len = (*lpdwBufferLength)*sizeof(WCHAR);
2438 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2440 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2441 if (alloclen < len)
2442 alloclen = len;
2444 else
2445 alloclen = len;
2446 bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
2447 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2448 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2449 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
2450 } else
2452 bufferW = NULL;
2453 len = 0;
2456 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2457 &len, lpdwIndex );
2458 if( result )
2460 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
2461 lpBuffer, *lpdwBufferLength, NULL, NULL );
2462 *lpdwBufferLength = len - 1;
2464 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
2466 else
2467 /* since the strings being returned from HttpQueryInfoW should be
2468 * only ASCII characters, it is reasonable to assume that all of
2469 * the Unicode characters can be reduced to a single byte */
2470 *lpdwBufferLength = len / sizeof(WCHAR);
2472 HeapFree(GetProcessHeap(), 0, bufferW );
2474 return result;
2477 /***********************************************************************
2478 * HttpSendRequestExA (WININET.@)
2480 * Sends the specified request to the HTTP server and allows chunked
2481 * transfers.
2483 * RETURNS
2484 * Success: TRUE
2485 * Failure: FALSE, call GetLastError() for more information.
2487 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2488 LPINTERNET_BUFFERSA lpBuffersIn,
2489 LPINTERNET_BUFFERSA lpBuffersOut,
2490 DWORD dwFlags, DWORD_PTR dwContext)
2492 INTERNET_BUFFERSW BuffersInW;
2493 BOOL rc = FALSE;
2494 DWORD headerlen;
2495 LPWSTR header = NULL;
2497 TRACE("(%p, %p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersIn,
2498 lpBuffersOut, dwFlags, dwContext);
2500 if (lpBuffersIn)
2502 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2503 if (lpBuffersIn->lpcszHeader)
2505 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2506 lpBuffersIn->dwHeadersLength,0,0);
2507 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2508 if (!(BuffersInW.lpcszHeader = header))
2510 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2511 return FALSE;
2513 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2514 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2515 header, headerlen);
2517 else
2518 BuffersInW.lpcszHeader = NULL;
2519 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2520 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2521 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2522 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2523 BuffersInW.Next = NULL;
2526 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2528 HeapFree(GetProcessHeap(),0,header);
2530 return rc;
2533 /***********************************************************************
2534 * HttpSendRequestExW (WININET.@)
2536 * Sends the specified request to the HTTP server and allows chunked
2537 * transfers
2539 * RETURNS
2540 * Success: TRUE
2541 * Failure: FALSE, call GetLastError() for more information.
2543 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2544 LPINTERNET_BUFFERSW lpBuffersIn,
2545 LPINTERNET_BUFFERSW lpBuffersOut,
2546 DWORD dwFlags, DWORD_PTR dwContext)
2548 BOOL ret = FALSE;
2549 LPWININETHTTPREQW lpwhr;
2550 LPWININETHTTPSESSIONW lpwhs;
2551 LPWININETAPPINFOW hIC;
2553 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2554 lpBuffersOut, dwFlags, dwContext);
2556 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2558 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2560 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2561 goto lend;
2564 lpwhs = lpwhr->lpHttpSession;
2565 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
2566 hIC = lpwhs->lpAppInfo;
2567 assert(hIC->hdr.htype == WH_HINIT);
2569 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2571 WORKREQUEST workRequest;
2572 struct WORKREQ_HTTPSENDREQUESTW *req;
2574 workRequest.asyncproc = AsyncHttpSendRequestProc;
2575 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2576 req = &workRequest.u.HttpSendRequestW;
2577 if (lpBuffersIn)
2579 if (lpBuffersIn->lpcszHeader)
2580 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2581 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2582 else
2583 req->lpszHeader = NULL;
2584 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2585 req->lpOptional = lpBuffersIn->lpvBuffer;
2586 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2587 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2589 else
2591 req->lpszHeader = NULL;
2592 req->dwHeaderLength = 0;
2593 req->lpOptional = NULL;
2594 req->dwOptionalLength = 0;
2595 req->dwContentLength = 0;
2598 req->bEndRequest = FALSE;
2600 INTERNET_AsyncCall(&workRequest);
2602 * This is from windows.
2604 INTERNET_SetLastError(ERROR_IO_PENDING);
2606 else
2608 if (lpBuffersIn)
2609 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2610 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2611 lpBuffersIn->dwBufferTotal, FALSE);
2612 else
2613 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
2616 lend:
2617 if ( lpwhr )
2618 WININET_Release( &lpwhr->hdr );
2620 TRACE("<---\n");
2621 return ret;
2624 /***********************************************************************
2625 * HttpSendRequestW (WININET.@)
2627 * Sends the specified request to the HTTP server
2629 * RETURNS
2630 * TRUE on success
2631 * FALSE on failure
2634 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
2635 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2637 LPWININETHTTPREQW lpwhr;
2638 LPWININETHTTPSESSIONW lpwhs = NULL;
2639 LPWININETAPPINFOW hIC = NULL;
2640 BOOL r;
2642 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2643 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
2645 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2646 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2648 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2649 r = FALSE;
2650 goto lend;
2653 lpwhs = lpwhr->lpHttpSession;
2654 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2656 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2657 r = FALSE;
2658 goto lend;
2661 hIC = lpwhs->lpAppInfo;
2662 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2664 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2665 r = FALSE;
2666 goto lend;
2669 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2671 WORKREQUEST workRequest;
2672 struct WORKREQ_HTTPSENDREQUESTW *req;
2674 workRequest.asyncproc = AsyncHttpSendRequestProc;
2675 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2676 req = &workRequest.u.HttpSendRequestW;
2677 if (lpszHeaders)
2679 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, dwHeaderLength * sizeof(WCHAR));
2680 memcpy(req->lpszHeader, lpszHeaders, dwHeaderLength * sizeof(WCHAR));
2682 else
2683 req->lpszHeader = 0;
2684 req->dwHeaderLength = dwHeaderLength;
2685 req->lpOptional = lpOptional;
2686 req->dwOptionalLength = dwOptionalLength;
2687 req->dwContentLength = dwOptionalLength;
2688 req->bEndRequest = TRUE;
2690 INTERNET_AsyncCall(&workRequest);
2692 * This is from windows.
2694 INTERNET_SetLastError(ERROR_IO_PENDING);
2695 r = FALSE;
2697 else
2699 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
2700 dwHeaderLength, lpOptional, dwOptionalLength,
2701 dwOptionalLength, TRUE);
2703 lend:
2704 if( lpwhr )
2705 WININET_Release( &lpwhr->hdr );
2706 return r;
2709 /***********************************************************************
2710 * HttpSendRequestA (WININET.@)
2712 * Sends the specified request to the HTTP server
2714 * RETURNS
2715 * TRUE on success
2716 * FALSE on failure
2719 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
2720 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2722 BOOL result;
2723 LPWSTR szHeaders=NULL;
2724 DWORD nLen=dwHeaderLength;
2725 if(lpszHeaders!=NULL)
2727 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2728 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2729 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
2731 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
2732 HeapFree(GetProcessHeap(),0,szHeaders);
2733 return result;
2736 static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
2738 LPHTTPHEADERW host_header;
2740 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2742 host_header = HTTP_GetHeader(req, szHost);
2743 if(!host_header)
2744 return FALSE;
2746 sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
2747 return TRUE;
2750 /***********************************************************************
2751 * HTTP_HandleRedirect (internal)
2753 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
2755 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2756 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
2757 WCHAR path[2048];
2759 if(lpszUrl[0]=='/')
2761 /* if it's an absolute path, keep the same session info */
2762 lstrcpynW(path, lpszUrl, 2048);
2764 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2766 TRACE("Redirect through proxy\n");
2767 lstrcpynW(path, lpszUrl, 2048);
2769 else
2771 URL_COMPONENTSW urlComponents;
2772 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2773 static WCHAR szHttp[] = {'h','t','t','p',0};
2774 static WCHAR szHttps[] = {'h','t','t','p','s',0};
2775 DWORD url_length = 0;
2776 LPWSTR orig_url;
2777 LPWSTR combined_url;
2779 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2780 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
2781 urlComponents.dwSchemeLength = 0;
2782 urlComponents.lpszHostName = lpwhs->lpszHostName;
2783 urlComponents.dwHostNameLength = 0;
2784 urlComponents.nPort = lpwhs->nHostPort;
2785 urlComponents.lpszUserName = lpwhs->lpszUserName;
2786 urlComponents.dwUserNameLength = 0;
2787 urlComponents.lpszPassword = NULL;
2788 urlComponents.dwPasswordLength = 0;
2789 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2790 urlComponents.dwUrlPathLength = 0;
2791 urlComponents.lpszExtraInfo = NULL;
2792 urlComponents.dwExtraInfoLength = 0;
2794 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2795 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2796 return FALSE;
2798 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
2800 /* convert from bytes to characters */
2801 url_length = url_length / sizeof(WCHAR) - 1;
2802 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2804 HeapFree(GetProcessHeap(), 0, orig_url);
2805 return FALSE;
2808 url_length = 0;
2809 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2810 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2812 HeapFree(GetProcessHeap(), 0, orig_url);
2813 return FALSE;
2815 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2817 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2819 HeapFree(GetProcessHeap(), 0, orig_url);
2820 HeapFree(GetProcessHeap(), 0, combined_url);
2821 return FALSE;
2823 HeapFree(GetProcessHeap(), 0, orig_url);
2825 userName[0] = 0;
2826 hostName[0] = 0;
2827 protocol[0] = 0;
2829 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2830 urlComponents.lpszScheme = protocol;
2831 urlComponents.dwSchemeLength = 32;
2832 urlComponents.lpszHostName = hostName;
2833 urlComponents.dwHostNameLength = MAXHOSTNAME;
2834 urlComponents.lpszUserName = userName;
2835 urlComponents.dwUserNameLength = 1024;
2836 urlComponents.lpszPassword = NULL;
2837 urlComponents.dwPasswordLength = 0;
2838 urlComponents.lpszUrlPath = path;
2839 urlComponents.dwUrlPathLength = 2048;
2840 urlComponents.lpszExtraInfo = NULL;
2841 urlComponents.dwExtraInfoLength = 0;
2842 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2844 HeapFree(GetProcessHeap(), 0, combined_url);
2845 return FALSE;
2848 HeapFree(GetProcessHeap(), 0, combined_url);
2850 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2851 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2853 TRACE("redirect from secure page to non-secure page\n");
2854 /* FIXME: warn about from secure redirect to non-secure page */
2855 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2857 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2858 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2860 TRACE("redirect from non-secure page to secure page\n");
2861 /* FIXME: notify about redirect to secure page */
2862 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2865 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2867 if (lstrlenW(protocol)>4) /*https*/
2868 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2869 else /*http*/
2870 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2873 #if 0
2875 * This upsets redirects to binary files on sourceforge.net
2876 * and gives an html page instead of the target file
2877 * Examination of the HTTP request sent by native wininet.dll
2878 * reveals that it doesn't send a referrer in that case.
2879 * Maybe there's a flag that enables this, or maybe a referrer
2880 * shouldn't be added in case of a redirect.
2883 /* consider the current host as the referrer */
2884 if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
2885 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2886 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2887 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2888 #endif
2890 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2891 lpwhs->lpszServerName = WININET_strdupW(hostName);
2892 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2893 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2894 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2896 int len;
2897 static const WCHAR fmt[] = {'%','s',':','%','i',0};
2898 len = lstrlenW(hostName);
2899 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2900 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2901 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2903 else
2904 lpwhs->lpszHostName = WININET_strdupW(hostName);
2906 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2909 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2910 lpwhs->lpszUserName = NULL;
2911 if (userName[0])
2912 lpwhs->lpszUserName = WININET_strdupW(userName);
2913 lpwhs->nServerPort = urlComponents.nPort;
2915 if (!HTTP_ResolveName(lpwhr))
2916 return FALSE;
2918 NETCON_close(&lpwhr->netConnection);
2920 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2921 return FALSE;
2924 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2925 lpwhr->lpszPath=NULL;
2926 if (*path)
2928 DWORD needed = 0;
2929 HRESULT rc;
2931 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2932 if (rc != E_POINTER)
2933 needed = strlenW(path)+1;
2934 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2935 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2936 URL_ESCAPE_SPACES_ONLY);
2937 if (rc)
2939 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
2940 strcpyW(lpwhr->lpszPath,path);
2944 return TRUE;
2947 /***********************************************************************
2948 * HTTP_build_req (internal)
2950 * concatenate all the strings in the request together
2952 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2954 LPCWSTR *t;
2955 LPWSTR str;
2957 for( t = list; *t ; t++ )
2958 len += strlenW( *t );
2959 len++;
2961 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2962 *str = 0;
2964 for( t = list; *t ; t++ )
2965 strcatW( str, *t );
2967 return str;
2970 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2972 LPWSTR lpszPath;
2973 LPWSTR requestString;
2974 INT len;
2975 INT cnt;
2976 INT responseLen;
2977 char *ascii_req;
2978 BOOL ret;
2979 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2980 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2981 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2983 TRACE("\n");
2985 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2986 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2987 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
2988 HeapFree( GetProcessHeap(), 0, lpszPath );
2990 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2991 NULL, 0, NULL, NULL );
2992 len--; /* the nul terminator isn't needed */
2993 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2994 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2995 ascii_req, len, NULL, NULL );
2996 HeapFree( GetProcessHeap(), 0, requestString );
2998 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
3000 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
3001 HeapFree( GetProcessHeap(), 0, ascii_req );
3002 if (!ret || cnt < 0)
3003 return FALSE;
3005 responseLen = HTTP_GetResponseHeaders( lpwhr );
3006 if (!responseLen)
3007 return FALSE;
3009 return TRUE;
3012 /***********************************************************************
3013 * HTTP_HttpSendRequestW (internal)
3015 * Sends the specified request to the HTTP server
3017 * RETURNS
3018 * TRUE on success
3019 * FALSE on failure
3022 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
3023 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
3024 DWORD dwContentLength, BOOL bEndRequest)
3026 INT cnt;
3027 BOOL bSuccess = FALSE;
3028 LPWSTR requestString = NULL;
3029 INT responseLen;
3030 BOOL loop_next;
3031 INTERNET_ASYNC_RESULT iar;
3032 static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
3033 static const WCHAR szPost[] = { 'P','O','S','T',0 };
3034 static const WCHAR szContentLength[] =
3035 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3036 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
3038 TRACE("--> %p\n", lpwhr);
3040 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
3042 /* Clear any error information */
3043 INTERNET_SetLastError(0);
3045 /* if the verb is NULL default to GET */
3046 if (!lpwhr->lpszVerb)
3047 lpwhr->lpszVerb = WININET_strdupW(szGET);
3049 if (dwContentLength || !strcmpW(lpwhr->lpszVerb, szPost))
3051 sprintfW(contentLengthStr, szContentLength, dwContentLength);
3052 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3057 DWORD len;
3058 char *ascii_req;
3060 loop_next = FALSE;
3062 /* like native, just in case the caller forgot to call InternetReadFile
3063 * for all the data */
3064 HTTP_DrainContent(lpwhr);
3065 lpwhr->dwContentRead = 0;
3067 if (TRACE_ON(wininet))
3069 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3070 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
3073 HTTP_FixURL(lpwhr);
3074 HTTP_ProcessHeader(lpwhr, szConnection,
3075 lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION ? szKeepAlive : szClose,
3076 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
3078 HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
3079 HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
3081 /* add the headers the caller supplied */
3082 if( lpszHeaders && dwHeaderLength )
3084 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
3085 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3088 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
3090 TRACE("Request header -> %s\n", debugstr_w(requestString) );
3092 /* Send the request and store the results */
3093 if (!HTTP_OpenConnection(lpwhr))
3094 goto lend;
3096 /* send the request as ASCII, tack on the optional data */
3097 if( !lpOptional )
3098 dwOptionalLength = 0;
3099 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3100 NULL, 0, NULL, NULL );
3101 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
3102 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3103 ascii_req, len, NULL, NULL );
3104 if( lpOptional )
3105 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
3106 len = (len + dwOptionalLength - 1);
3107 ascii_req[len] = 0;
3108 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
3110 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3111 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3113 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
3114 HeapFree( GetProcessHeap(), 0, ascii_req );
3116 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3117 INTERNET_STATUS_REQUEST_SENT,
3118 &len, sizeof(DWORD));
3120 if (bEndRequest)
3122 DWORD dwBufferSize;
3123 DWORD dwStatusCode;
3125 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3126 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3128 if (cnt < 0)
3129 goto lend;
3131 responseLen = HTTP_GetResponseHeaders(lpwhr);
3132 if (responseLen)
3133 bSuccess = TRUE;
3135 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3136 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
3137 sizeof(DWORD));
3139 HTTP_ProcessCookies(lpwhr);
3141 dwBufferSize = sizeof(lpwhr->dwContentLength);
3142 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
3143 &lpwhr->dwContentLength,&dwBufferSize,NULL))
3144 lpwhr->dwContentLength = -1;
3146 if (lpwhr->dwContentLength == 0)
3147 HTTP_FinishedReading(lpwhr);
3149 dwBufferSize = sizeof(dwStatusCode);
3150 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
3151 &dwStatusCode,&dwBufferSize,NULL))
3152 dwStatusCode = 0;
3154 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
3156 WCHAR szNewLocation[2048];
3157 dwBufferSize=sizeof(szNewLocation);
3158 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
3159 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
3161 HTTP_DrainContent(lpwhr);
3162 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3163 INTERNET_STATUS_REDIRECT, szNewLocation,
3164 dwBufferSize);
3165 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
3166 if (bSuccess)
3168 HeapFree(GetProcessHeap(), 0, requestString);
3169 loop_next = TRUE;
3173 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
3175 WCHAR szAuthValue[2048];
3176 dwBufferSize=2048;
3177 if (dwStatusCode == HTTP_STATUS_DENIED)
3179 DWORD dwIndex = 0;
3180 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3182 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3183 &lpwhr->pAuthInfo,
3184 lpwhr->lpHttpSession->lpszUserName,
3185 lpwhr->lpHttpSession->lpszPassword))
3187 loop_next = TRUE;
3188 break;
3192 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
3194 DWORD dwIndex = 0;
3195 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3197 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3198 &lpwhr->pProxyAuthInfo,
3199 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
3200 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
3202 loop_next = TRUE;
3203 break;
3209 else
3210 bSuccess = TRUE;
3212 while (loop_next);
3214 /* FIXME: Better check, when we have to create the cache file */
3215 if(bSuccess && (lpwhr->hdr.dwFlags & INTERNET_FLAG_NEED_FILE)) {
3216 WCHAR url[INTERNET_MAX_URL_LENGTH];
3217 WCHAR cacheFileName[MAX_PATH+1];
3218 BOOL b;
3220 b = HTTP_GetRequestURL(lpwhr, url);
3221 if(!b) {
3222 WARN("Could not get URL\n");
3223 goto lend;
3226 b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
3227 if(b) {
3228 lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
3229 lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
3230 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3231 if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
3232 WARN("Could not create file: %u\n", GetLastError());
3233 lpwhr->hCacheFile = NULL;
3235 }else {
3236 WARN("Could not create cache entry: %08x\n", GetLastError());
3240 lend:
3242 HeapFree(GetProcessHeap(), 0, requestString);
3244 /* TODO: send notification for P3P header */
3246 iar.dwResult = (DWORD)bSuccess;
3247 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
3249 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3250 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3251 sizeof(INTERNET_ASYNC_RESULT));
3253 TRACE("<--\n");
3254 return bSuccess;
3257 /***********************************************************************
3258 * HTTPSESSION_Destroy (internal)
3260 * Deallocate session handle
3263 static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3265 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3267 TRACE("%p\n", lpwhs);
3269 WININET_Release(&lpwhs->lpAppInfo->hdr);
3271 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3272 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3273 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3274 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3275 HeapFree(GetProcessHeap(), 0, lpwhs);
3278 static DWORD HTTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3280 switch(option) {
3281 case INTERNET_OPTION_HANDLE_TYPE:
3282 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3284 if (*size < sizeof(ULONG))
3285 return ERROR_INSUFFICIENT_BUFFER;
3287 *size = sizeof(DWORD);
3288 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
3289 return ERROR_SUCCESS;
3292 FIXME("Not implemented option %d\n", option);
3293 return ERROR_INTERNET_INVALID_OPTION;
3296 static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
3297 HTTPSESSION_Destroy,
3298 NULL,
3299 HTTPSESSION_QueryOption,
3300 NULL,
3301 NULL,
3302 NULL,
3303 NULL,
3304 NULL,
3305 NULL
3309 /***********************************************************************
3310 * HTTP_Connect (internal)
3312 * Create http session handle
3314 * RETURNS
3315 * HINTERNET a session handle on success
3316 * NULL on failure
3319 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
3320 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
3321 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
3322 DWORD dwInternalFlags)
3324 BOOL bSuccess = FALSE;
3325 LPWININETHTTPSESSIONW lpwhs = NULL;
3326 HINTERNET handle = NULL;
3328 TRACE("-->\n");
3330 if (!lpszServerName || !lpszServerName[0])
3332 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3333 goto lerror;
3336 assert( hIC->hdr.htype == WH_HINIT );
3338 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
3339 if (NULL == lpwhs)
3341 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3342 goto lerror;
3346 * According to my tests. The name is not resolved until a request is sent
3349 lpwhs->hdr.htype = WH_HHTTPSESSION;
3350 lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
3351 lpwhs->hdr.dwFlags = dwFlags;
3352 lpwhs->hdr.dwContext = dwContext;
3353 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
3354 lpwhs->hdr.refs = 1;
3355 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
3357 WININET_AddRef( &hIC->hdr );
3358 lpwhs->lpAppInfo = hIC;
3359 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
3361 handle = WININET_AllocHandle( &lpwhs->hdr );
3362 if (NULL == handle)
3364 ERR("Failed to alloc handle\n");
3365 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3366 goto lerror;
3369 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
3370 if(strchrW(hIC->lpszProxy, ' '))
3371 FIXME("Several proxies not implemented.\n");
3372 if(hIC->lpszProxyBypass)
3373 FIXME("Proxy bypass is ignored.\n");
3375 if (lpszServerName && lpszServerName[0])
3377 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
3378 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
3380 if (lpszUserName && lpszUserName[0])
3381 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
3382 if (lpszPassword && lpszPassword[0])
3383 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
3384 lpwhs->nServerPort = nServerPort;
3385 lpwhs->nHostPort = nServerPort;
3387 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3388 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
3390 INTERNET_SendCallback(&hIC->hdr, dwContext,
3391 INTERNET_STATUS_HANDLE_CREATED, &handle,
3392 sizeof(handle));
3395 bSuccess = TRUE;
3397 lerror:
3398 if( lpwhs )
3399 WININET_Release( &lpwhs->hdr );
3402 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3403 * windows
3406 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
3407 return handle;
3411 /***********************************************************************
3412 * HTTP_OpenConnection (internal)
3414 * Connect to a web server
3416 * RETURNS
3418 * TRUE on success
3419 * FALSE on failure
3421 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
3423 BOOL bSuccess = FALSE;
3424 LPWININETHTTPSESSIONW lpwhs;
3425 LPWININETAPPINFOW hIC = NULL;
3426 char szaddr[32];
3428 TRACE("-->\n");
3431 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3433 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3434 goto lend;
3437 if (NETCON_connected(&lpwhr->netConnection))
3439 bSuccess = TRUE;
3440 goto lend;
3443 lpwhs = lpwhr->lpHttpSession;
3445 hIC = lpwhs->lpAppInfo;
3446 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
3447 szaddr, sizeof(szaddr));
3448 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3449 INTERNET_STATUS_CONNECTING_TO_SERVER,
3450 szaddr,
3451 strlen(szaddr)+1);
3453 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
3454 SOCK_STREAM, 0))
3456 WARN("Socket creation failed\n");
3457 goto lend;
3460 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
3461 sizeof(lpwhs->socketAddress)))
3462 goto lend;
3464 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
3466 /* Note: we differ from Microsoft's WinINet here. they seem to have
3467 * a bug that causes no status callbacks to be sent when starting
3468 * a tunnel to a proxy server using the CONNECT verb. i believe our
3469 * behaviour to be more correct and to not cause any incompatibilities
3470 * because using a secure connection through a proxy server is a rare
3471 * case that would be hard for anyone to depend on */
3472 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
3473 goto lend;
3475 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
3477 WARN("Couldn't connect securely to host\n");
3478 goto lend;
3482 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3483 INTERNET_STATUS_CONNECTED_TO_SERVER,
3484 szaddr, strlen(szaddr)+1);
3486 bSuccess = TRUE;
3488 lend:
3489 TRACE("%d <--\n", bSuccess);
3490 return bSuccess;
3494 /***********************************************************************
3495 * HTTP_clear_response_headers (internal)
3497 * clear out any old response headers
3499 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
3501 DWORD i;
3503 for( i=0; i<lpwhr->nCustHeaders; i++)
3505 if( !lpwhr->pCustHeaders[i].lpszField )
3506 continue;
3507 if( !lpwhr->pCustHeaders[i].lpszValue )
3508 continue;
3509 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
3510 continue;
3511 HTTP_DeleteCustomHeader( lpwhr, i );
3512 i--;
3516 /***********************************************************************
3517 * HTTP_GetResponseHeaders (internal)
3519 * Read server response
3521 * RETURNS
3523 * TRUE on success
3524 * FALSE on error
3526 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
3528 INT cbreaks = 0;
3529 WCHAR buffer[MAX_REPLY_LEN];
3530 DWORD buflen = MAX_REPLY_LEN;
3531 BOOL bSuccess = FALSE;
3532 INT rc = 0;
3533 static const WCHAR szCrLf[] = {'\r','\n',0};
3534 static const WCHAR szHundred[] = {'1','0','0',0};
3535 char bufferA[MAX_REPLY_LEN];
3536 LPWSTR status_code, status_text;
3537 DWORD cchMaxRawHeaders = 1024;
3538 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3539 DWORD cchRawHeaders = 0;
3541 TRACE("-->\n");
3543 /* clear old response headers (eg. from a redirect response) */
3544 HTTP_clear_response_headers( lpwhr );
3546 if (!NETCON_connected(&lpwhr->netConnection))
3547 goto lend;
3549 do {
3551 * HACK peek at the buffer
3553 buflen = MAX_REPLY_LEN;
3554 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
3557 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3559 memset(buffer, 0, MAX_REPLY_LEN);
3560 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3561 goto lend;
3562 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3564 /* split the version from the status code */
3565 status_code = strchrW( buffer, ' ' );
3566 if( !status_code )
3567 goto lend;
3568 *status_code++=0;
3570 /* split the status code from the status text */
3571 status_text = strchrW( status_code, ' ' );
3572 if( !status_text )
3573 goto lend;
3574 *status_text++=0;
3576 TRACE("version [%s] status code [%s] status text [%s]\n",
3577 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3579 } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
3581 /* Add status code */
3582 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3583 HTTP_ADDHDR_FLAG_REPLACE);
3585 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3586 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3588 lpwhr->lpszVersion= WININET_strdupW(buffer);
3589 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3591 /* Restore the spaces */
3592 *(status_code-1) = ' ';
3593 *(status_text-1) = ' ';
3595 /* regenerate raw headers */
3596 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3598 cchMaxRawHeaders *= 2;
3599 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3601 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3602 cchRawHeaders += (buflen-1);
3603 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3604 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3605 lpszRawHeaders[cchRawHeaders] = '\0';
3607 /* Parse each response line */
3610 buflen = MAX_REPLY_LEN;
3611 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3613 LPWSTR * pFieldAndValue;
3615 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
3616 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3618 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3620 cchMaxRawHeaders *= 2;
3621 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3623 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3624 cchRawHeaders += (buflen-1);
3625 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3626 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3627 lpszRawHeaders[cchRawHeaders] = '\0';
3629 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3630 if (!pFieldAndValue)
3631 break;
3633 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
3634 HTTP_ADDREQ_FLAG_ADD );
3636 HTTP_FreeTokens(pFieldAndValue);
3638 else
3640 cbreaks++;
3641 if (cbreaks >= 2)
3642 break;
3644 }while(1);
3646 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3647 lpwhr->lpszRawHeaders = lpszRawHeaders;
3648 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
3649 bSuccess = TRUE;
3651 lend:
3653 TRACE("<--\n");
3654 if (bSuccess)
3655 return rc;
3656 else
3657 return 0;
3661 static void strip_spaces(LPWSTR start)
3663 LPWSTR str = start;
3664 LPWSTR end;
3666 while (*str == ' ' && *str != '\0')
3667 str++;
3669 if (str != start)
3670 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
3672 end = start + strlenW(start) - 1;
3673 while (end >= start && *end == ' ')
3675 *end = '\0';
3676 end--;
3681 /***********************************************************************
3682 * HTTP_InterpretHttpHeader (internal)
3684 * Parse server response
3686 * RETURNS
3688 * Pointer to array of field, value, NULL on success.
3689 * NULL on error.
3691 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
3693 LPWSTR * pTokenPair;
3694 LPWSTR pszColon;
3695 INT len;
3697 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
3699 pszColon = strchrW(buffer, ':');
3700 /* must have two tokens */
3701 if (!pszColon)
3703 HTTP_FreeTokens(pTokenPair);
3704 if (buffer[0])
3705 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
3706 return NULL;
3709 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
3710 if (!pTokenPair[0])
3712 HTTP_FreeTokens(pTokenPair);
3713 return NULL;
3715 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
3716 pTokenPair[0][pszColon - buffer] = '\0';
3718 /* skip colon */
3719 pszColon++;
3720 len = strlenW(pszColon);
3721 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
3722 if (!pTokenPair[1])
3724 HTTP_FreeTokens(pTokenPair);
3725 return NULL;
3727 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
3729 strip_spaces(pTokenPair[0]);
3730 strip_spaces(pTokenPair[1]);
3732 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
3733 return pTokenPair;
3736 /***********************************************************************
3737 * HTTP_ProcessHeader (internal)
3739 * Stuff header into header tables according to <dwModifier>
3743 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3745 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
3747 LPHTTPHEADERW lphttpHdr = NULL;
3748 BOOL bSuccess = FALSE;
3749 INT index = -1;
3750 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
3752 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
3754 /* REPLACE wins out over ADD */
3755 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3756 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
3758 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
3759 index = -1;
3760 else
3761 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
3763 if (index >= 0)
3765 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
3767 return FALSE;
3769 lphttpHdr = &lpwhr->pCustHeaders[index];
3771 else if (value)
3773 HTTPHEADERW hdr;
3775 hdr.lpszField = (LPWSTR)field;
3776 hdr.lpszValue = (LPWSTR)value;
3777 hdr.wFlags = hdr.wCount = 0;
3779 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3780 hdr.wFlags |= HDR_ISREQUEST;
3782 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3784 /* no value to delete */
3785 else return TRUE;
3787 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3788 lphttpHdr->wFlags |= HDR_ISREQUEST;
3789 else
3790 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
3792 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3794 HTTP_DeleteCustomHeader( lpwhr, index );
3796 if (value)
3798 HTTPHEADERW hdr;
3800 hdr.lpszField = (LPWSTR)field;
3801 hdr.lpszValue = (LPWSTR)value;
3802 hdr.wFlags = hdr.wCount = 0;
3804 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3805 hdr.wFlags |= HDR_ISREQUEST;
3807 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3810 return TRUE;
3812 else if (dwModifier & COALESCEFLAGS)
3814 LPWSTR lpsztmp;
3815 WCHAR ch = 0;
3816 INT len = 0;
3817 INT origlen = strlenW(lphttpHdr->lpszValue);
3818 INT valuelen = strlenW(value);
3820 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
3822 ch = ',';
3823 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3825 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3827 ch = ';';
3828 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3831 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
3833 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
3834 if (lpsztmp)
3836 lphttpHdr->lpszValue = lpsztmp;
3837 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
3838 if (ch > 0)
3840 lphttpHdr->lpszValue[origlen] = ch;
3841 origlen++;
3842 lphttpHdr->lpszValue[origlen] = ' ';
3843 origlen++;
3846 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
3847 lphttpHdr->lpszValue[len] = '\0';
3848 bSuccess = TRUE;
3850 else
3852 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
3853 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3856 TRACE("<-- %d\n",bSuccess);
3857 return bSuccess;
3861 /***********************************************************************
3862 * HTTP_FinishedReading (internal)
3864 * Called when all content from server has been read by client.
3867 BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
3869 WCHAR szVersion[10];
3870 DWORD dwBufferSize = sizeof(szVersion);
3872 TRACE("\n");
3874 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
3875 * the connection is keep-alive by default */
3876 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_VERSION, szVersion,
3877 &dwBufferSize, NULL) ||
3878 strcmpiW(szVersion, g_szHttp1_1))
3880 WCHAR szConnectionResponse[20];
3881 dwBufferSize = sizeof(szConnectionResponse);
3882 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse,
3883 &dwBufferSize, NULL) ||
3884 strcmpiW(szConnectionResponse, szKeepAlive))
3886 HTTPREQ_CloseConnection(&lpwhr->hdr);
3890 /* FIXME: store data in the URL cache here */
3892 return TRUE;
3896 /***********************************************************************
3897 * HTTP_GetCustomHeaderIndex (internal)
3899 * Return index of custom header from header array
3902 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
3903 int requested_index, BOOL request_only)
3905 DWORD index;
3907 TRACE("%s\n", debugstr_w(lpszField));
3909 for (index = 0; index < lpwhr->nCustHeaders; index++)
3911 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3912 continue;
3914 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3915 continue;
3917 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3918 continue;
3920 if (requested_index == 0)
3921 break;
3922 requested_index --;
3925 if (index >= lpwhr->nCustHeaders)
3926 index = -1;
3928 TRACE("Return: %d\n", index);
3929 return index;
3933 /***********************************************************************
3934 * HTTP_InsertCustomHeader (internal)
3936 * Insert header into array
3939 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3941 INT count;
3942 LPHTTPHEADERW lph = NULL;
3943 BOOL r = FALSE;
3945 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3946 count = lpwhr->nCustHeaders + 1;
3947 if (count > 1)
3948 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3949 else
3950 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3952 if (NULL != lph)
3954 lpwhr->pCustHeaders = lph;
3955 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3956 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3957 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3958 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3959 lpwhr->nCustHeaders++;
3960 r = TRUE;
3962 else
3964 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3967 return r;
3971 /***********************************************************************
3972 * HTTP_DeleteCustomHeader (internal)
3974 * Delete header from array
3975 * If this function is called, the indexs may change.
3977 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3979 if( lpwhr->nCustHeaders <= 0 )
3980 return FALSE;
3981 if( index >= lpwhr->nCustHeaders )
3982 return FALSE;
3983 lpwhr->nCustHeaders--;
3985 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3986 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3987 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3989 return TRUE;
3993 /***********************************************************************
3994 * HTTP_VerifyValidHeader (internal)
3996 * Verify the given header is not invalid for the given http request
3999 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
4001 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4002 if (!strcmpW(lpwhr->lpszVersion, g_szHttp1_0) && !strcmpiW(field, szAccept_Encoding))
4003 return FALSE;
4005 return TRUE;
4008 /***********************************************************************
4009 * IsHostInProxyBypassList (@)
4011 * Undocumented
4014 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
4016 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
4017 return FALSE;