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
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
30 #include "wine/port.h"
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
36 #ifdef HAVE_ARPA_INET_H
37 # include <arpa/inet.h>
52 #define NO_SHLWAPI_STREAM
53 #define NO_SHLWAPI_REG
54 #define NO_SHLWAPI_STRFCNS
55 #define NO_SHLWAPI_GDI
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]))
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, BOOL clear);
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
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)
128 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
129 if (HeaderIndex == -1)
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;
147 /* empty string has no tokens */
151 for (i = 0; string[i]; i++)
152 if (!strncmpW(string+i, token_string, strlenW(token_string)))
156 /* we want to skip over separators, but not the null terminator */
157 for (j = 0; j < strlenW(token_string) - 1; j++)
163 /* add 1 for terminating NULL */
164 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
165 token_array[tokens] = NULL;
168 for (i = 0; i < tokens; i++)
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);
182 /***********************************************************************
183 * HTTP_FreeTokens (internal)
185 * Frees memory returned from HTTP_Tokenize.
187 static void HTTP_FreeTokens(LPWSTR * token_array)
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')))
228 lpwhr->lpszPath[nLen]='\0';
230 /* Replace '\' with '/' */
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));
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;
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 */
275 /* Append custom request headers */
276 for (i = 0; i < lpwhr->nCustHeaders; i++)
278 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
281 req[n++] = lpwhr->pCustHeaders[i].lpszField;
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));
292 ERR("oops. buffer overrun\n");
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') )
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 };
314 LPHTTPHEADERW setCookieHeader;
316 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
317 if (HeaderIndex == -1)
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;
330 LPWSTR domain = NULL;
334 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
335 setCookieHeader->lpszValue[nPosEnd] != '\0')
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);
347 { /* they have specified their own domain, lets use it */
348 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
349 lpszDomain[nDomainPosEnd] != '\0')
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')
367 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
369 HeapFree(GetProcessHeap(), 0, buf_cookie);
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);
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;
408 TRACE("%s\n", debugstr_w(pszAuthValue));
415 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
419 SecInvalidateHandle(&pAuthInfo->cred);
420 SecInvalidateHandle(&pAuthInfo->ctx);
421 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
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);
440 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
442 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
443 if (!pAuthInfo->scheme)
445 HeapFree(GetProcessHeap(), 0, pAuthInfo);
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;
461 user = domain_and_username;
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);
474 /* use default credentials */
477 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
478 SECPKG_CRED_OUTBOUND, NULL,
480 NULL, &pAuthInfo->cred,
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);
501 *ppAuthInfo = pAuthInfo;
503 else if (pAuthInfo->finished)
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));
514 if (is_basic_auth_value(pszAuthValue))
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);
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;
547 SecBufferDesc out_desc, in_desc;
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;
557 in_desc.ulVersion = 0;
558 in_desc.cBuffers = 1;
559 in_desc.pBuffers = ∈
561 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
562 if (*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");
602 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
603 pAuthInfo->finished = TRUE;
604 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
612 /***********************************************************************
613 * HTTP_HttpAddRequestHeadersW (internal)
615 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
616 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
621 BOOL bSuccess = FALSE;
624 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
626 if( dwHeaderLength == ~0U )
627 len = strlenW(lpszHeader);
629 len = dwHeaderLength;
630 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
631 lstrcpynW( buffer, lpszHeader, len + 1);
637 LPWSTR * pFieldAndValue;
641 while (*lpszEnd != '\0')
643 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
648 if (*lpszStart == '\0')
651 if (*lpszEnd == '\r')
654 lpszEnd += 2; /* Jump over \r\n */
656 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
657 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
660 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
662 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
663 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
664 HTTP_FreeTokens(pFieldAndValue);
670 HeapFree(GetProcessHeap(), 0, buffer);
675 /***********************************************************************
676 * HttpAddRequestHeadersW (WININET.@)
678 * Adds one or more HTTP header to the request handler
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.
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);
702 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
703 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
705 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
708 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
711 WININET_Release( &lpwhr->hdr );
716 /***********************************************************************
717 * HttpAddRequestHeadersA (WININET.@)
719 * Adds one or more HTTP header to the request handler
726 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
727 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
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 );
748 /***********************************************************************
749 * HttpEndRequestA (WININET.@)
751 * Ends an HTTP request that was started by HttpSendRequestEx
758 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
759 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
761 LPINTERNET_BUFFERSA ptr;
762 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
765 TRACE("(%p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
770 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
771 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
773 lpBuffersOutW = NULL;
775 ptrW = lpBuffersOutW;
778 if (ptr->lpvBuffer && ptr->dwBufferLength)
779 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
780 ptrW->dwBufferLength = ptr->dwBufferLength;
781 ptrW->dwBufferTotal= ptr->dwBufferTotal;
784 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
785 sizeof(INTERNET_BUFFERSW));
791 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
795 ptrW = lpBuffersOutW;
798 LPINTERNET_BUFFERSW ptrW2;
800 FIXME("Do we need to translate info out of these buffer?\n");
802 HeapFree(GetProcessHeap(),0,ptrW->lpvBuffer);
804 HeapFree(GetProcessHeap(),0,ptrW);
812 /***********************************************************************
813 * HttpEndRequestW (WININET.@)
815 * Ends an HTTP request that was started by HttpSendRequestEx
822 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
823 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
826 LPWININETHTTPREQW lpwhr;
831 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
833 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
835 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
837 WININET_Release( &lpwhr->hdr );
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, 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[INTERNET_MAX_URL_LENGTH];
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 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
882 INTERNET_STATUS_REDIRECT, szNewLocation,
884 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
886 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
891 WININET_Release( &lpwhr->hdr );
892 TRACE("%i <--\n",rc);
896 /***********************************************************************
897 * HttpOpenRequestW (WININET.@)
899 * Open a HTTP request handle
902 * HINTERNET a HTTP request handle on success
906 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
907 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
908 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
909 DWORD dwFlags, DWORD_PTR dwContext)
911 LPWININETHTTPSESSIONW lpwhs;
912 HINTERNET handle = NULL;
914 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
915 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
916 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
918 if(lpszAcceptTypes!=NULL)
921 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
922 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
925 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
926 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
928 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
933 * My tests seem to show that the windows version does not
934 * become asynchronous until after this point. And anyhow
935 * if this call was asynchronous then how would you get the
936 * necessary HINTERNET pointer returned by this function.
939 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
940 lpszVersion, lpszReferrer, lpszAcceptTypes,
944 WININET_Release( &lpwhs->hdr );
945 TRACE("returning %p\n", handle);
950 /***********************************************************************
951 * HttpOpenRequestA (WININET.@)
953 * Open a HTTP request handle
956 * HINTERNET a HTTP request handle on success
960 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
961 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
962 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
963 DWORD dwFlags, DWORD_PTR dwContext)
965 LPWSTR szVerb = NULL, szObjectName = NULL;
966 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
968 INT acceptTypesCount;
969 HINTERNET rc = FALSE;
970 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
971 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
972 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
977 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
978 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
981 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
986 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
987 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
990 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
995 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
996 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
999 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
1004 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
1005 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1008 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
1011 acceptTypesCount = 0;
1012 if (lpszAcceptTypes)
1014 /* find out how many there are */
1015 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
1017 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
1018 acceptTypesCount = 0;
1019 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
1021 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1023 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1024 if (!szAcceptTypes[acceptTypesCount] )
1026 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1027 -1, szAcceptTypes[acceptTypesCount], len );
1030 szAcceptTypes[acceptTypesCount] = NULL;
1032 else szAcceptTypes = 0;
1034 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1035 szVersion, szReferrer,
1036 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1041 acceptTypesCount = 0;
1042 while (szAcceptTypes[acceptTypesCount])
1044 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1047 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1049 HeapFree(GetProcessHeap(), 0, szReferrer);
1050 HeapFree(GetProcessHeap(), 0, szVersion);
1051 HeapFree(GetProcessHeap(), 0, szObjectName);
1052 HeapFree(GetProcessHeap(), 0, szVerb);
1057 /***********************************************************************
1060 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1063 static const CHAR HTTP_Base64Enc[] =
1064 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1068 /* first 6 bits, all from bin[0] */
1069 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1070 x = (bin[0] & 3) << 4;
1072 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1075 base64[n++] = HTTP_Base64Enc[x];
1080 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1081 x = ( bin[1] & 0x0f ) << 2;
1083 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1086 base64[n++] = HTTP_Base64Enc[x];
1090 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1092 /* last 6 bits, all from bin [2] */
1093 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1101 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1102 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1103 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1104 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1105 static const signed char HTTP_Base64Dec[256] =
1107 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1108 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1109 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1110 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1111 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1112 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1113 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1114 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1115 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1116 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1117 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1118 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1119 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1120 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1121 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1122 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1123 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1124 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1125 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1126 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1127 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1128 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1129 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1130 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1131 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1132 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1136 /***********************************************************************
1139 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1147 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1148 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1149 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1150 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1152 WARN("invalid base64: %s\n", debugstr_w(base64));
1156 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1159 if ((base64[2] == '=') && (base64[3] == '='))
1161 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1162 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1164 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1168 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1171 if (base64[3] == '=')
1173 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1174 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1176 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1180 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1189 /***********************************************************************
1190 * HTTP_InsertAuthorizationForHeader
1192 * Insert or delete the authorization field in the request header.
1194 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1198 static const WCHAR wszSpace[] = {' ',0};
1199 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1201 WCHAR *authorization = NULL;
1203 if (pAuthInfo->auth_data_len)
1205 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1206 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1207 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1211 strcpyW(authorization, pAuthInfo->scheme);
1212 strcatW(authorization, wszSpace);
1213 HTTP_EncodeBase64(pAuthInfo->auth_data,
1214 pAuthInfo->auth_data_len,
1215 authorization+strlenW(authorization));
1217 /* clear the data as it isn't valid now that it has been sent to the
1218 * server, unless it's Basic authentication which doesn't do
1219 * connection tracking */
1220 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1222 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1223 pAuthInfo->auth_data = NULL;
1224 pAuthInfo->auth_data_len = 0;
1228 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1230 HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1232 HeapFree(GetProcessHeap(), 0, authorization);
1237 static WCHAR *HTTP_BuildProxyRequestUrl(WININETHTTPREQW *req)
1239 WCHAR new_location[INTERNET_MAX_URL_LENGTH], *url;
1242 size = sizeof(new_location);
1243 if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_LOCATION, new_location, &size, NULL))
1245 if (!(url = HeapAlloc( GetProcessHeap(), 0, size + sizeof(WCHAR) ))) return NULL;
1246 strcpyW( url, new_location );
1250 static const WCHAR slash[] = { '/',0 };
1251 static const WCHAR format[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1252 WININETHTTPSESSIONW *session = req->lpHttpSession;
1254 size = 15; /* "http://" + sizeof(port#) + ":/\0" */
1255 size += strlenW( session->lpszHostName ) + strlenW( req->lpszPath );
1257 if (!(url = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return FALSE;
1259 sprintfW( url, format, session->lpszHostName, session->nHostPort );
1260 if (req->lpszPath[0] != '/') strcatW( url, slash );
1261 strcatW( url, req->lpszPath );
1263 TRACE("url=%s\n", debugstr_w(url));
1267 /***********************************************************************
1268 * HTTP_DealWithProxy
1270 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1271 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1273 WCHAR buf[MAXHOSTNAME];
1274 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1275 static WCHAR szNul[] = { 0 };
1276 URL_COMPONENTSW UrlComponents;
1277 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1278 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1280 memset( &UrlComponents, 0, sizeof UrlComponents );
1281 UrlComponents.dwStructSize = sizeof UrlComponents;
1282 UrlComponents.lpszHostName = buf;
1283 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1285 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1286 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1287 sprintfW(proxy, szFormat, hIC->lpszProxy);
1289 strcpyW(proxy, hIC->lpszProxy);
1290 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1292 if( UrlComponents.dwHostNameLength == 0 )
1295 if( !lpwhr->lpszPath )
1296 lpwhr->lpszPath = szNul;
1298 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1299 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1301 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1302 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1303 lpwhs->nServerPort = UrlComponents.nPort;
1305 TRACE("proxy server=%s port=%d\n", debugstr_w(lpwhs->lpszServerName), lpwhs->nServerPort);
1309 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1312 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1314 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1315 INTERNET_STATUS_RESOLVING_NAME,
1316 lpwhs->lpszServerName,
1317 strlenW(lpwhs->lpszServerName)+1);
1319 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1320 &lpwhs->socketAddress))
1322 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1326 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1327 szaddr, sizeof(szaddr));
1328 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1329 INTERNET_STATUS_NAME_RESOLVED,
1330 szaddr, strlen(szaddr)+1);
1335 /***********************************************************************
1336 * HTTPREQ_Destroy (internal)
1338 * Deallocate request handle
1341 static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1343 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1348 if(lpwhr->hCacheFile)
1349 CloseHandle(lpwhr->hCacheFile);
1351 if(lpwhr->lpszCacheFile) {
1352 DeleteFileW(lpwhr->lpszCacheFile); /* FIXME */
1353 HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1356 WININET_Release(&lpwhr->lpHttpSession->hdr);
1358 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1359 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1360 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1361 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1362 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1364 for (i = 0; i < lpwhr->nCustHeaders; i++)
1366 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1367 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1370 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1371 HeapFree(GetProcessHeap(), 0, lpwhr);
1374 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1376 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1377 LPWININETHTTPSESSIONW lpwhs = NULL;
1379 TRACE("%p\n",lpwhr);
1381 if (!NETCON_connected(&lpwhr->netConnection))
1384 if (lpwhr->pAuthInfo)
1386 if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1387 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1388 if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1389 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1391 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1392 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1393 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1394 lpwhr->pAuthInfo = NULL;
1396 if (lpwhr->pProxyAuthInfo)
1398 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1399 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1400 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1401 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1403 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1404 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1405 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1406 lpwhr->pProxyAuthInfo = NULL;
1409 lpwhs = lpwhr->lpHttpSession;
1411 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1412 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1414 NETCON_close(&lpwhr->netConnection);
1416 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1417 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1420 static DWORD HTTPREQ_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1422 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1425 case INTERNET_OPTION_HANDLE_TYPE:
1426 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1428 if (*size < sizeof(ULONG))
1429 return ERROR_INSUFFICIENT_BUFFER;
1431 *size = sizeof(DWORD);
1432 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1433 return ERROR_SUCCESS;
1435 case INTERNET_OPTION_URL: {
1436 WCHAR url[INTERNET_MAX_URL_LENGTH];
1440 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
1441 static const WCHAR hostW[] = {'H','o','s','t',0};
1443 TRACE("INTERNET_OPTION_URL\n");
1445 host = HTTP_GetHeader(req, hostW);
1446 sprintfW(url, formatW, host->lpszValue, req->lpszPath);
1447 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1450 len = (strlenW(url)+1) * sizeof(WCHAR);
1452 return ERROR_INSUFFICIENT_BUFFER;
1455 strcpyW(buffer, url);
1456 return ERROR_SUCCESS;
1458 len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1460 return ERROR_INSUFFICIENT_BUFFER;
1463 return ERROR_SUCCESS;
1467 case INTERNET_OPTION_DATAFILE_NAME: {
1470 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1472 if(!req->lpszCacheFile) {
1474 return ERROR_INTERNET_ITEM_NOT_FOUND;
1478 req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1479 if(*size < req_size)
1480 return ERROR_INSUFFICIENT_BUFFER;
1483 memcpy(buffer, req->lpszCacheFile, *size);
1484 return ERROR_SUCCESS;
1486 req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1487 if (req_size > *size)
1488 return ERROR_INSUFFICIENT_BUFFER;
1490 *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1491 -1, buffer, *size, NULL, NULL);
1492 return ERROR_SUCCESS;
1496 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1497 PCCERT_CONTEXT context;
1499 if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1500 *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1501 return ERROR_INSUFFICIENT_BUFFER;
1504 context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1506 INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1509 memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1510 info->ftExpiry = context->pCertInfo->NotAfter;
1511 info->ftStart = context->pCertInfo->NotBefore;
1513 len = CertNameToStrW(context->dwCertEncodingType,
1514 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1515 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1516 if(info->lpszSubjectInfo)
1517 CertNameToStrW(context->dwCertEncodingType,
1518 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1519 info->lpszSubjectInfo, len);
1520 len = CertNameToStrW(context->dwCertEncodingType,
1521 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1522 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1523 if (info->lpszIssuerInfo)
1524 CertNameToStrW(context->dwCertEncodingType,
1525 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1526 info->lpszIssuerInfo, len);
1528 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1530 len = CertNameToStrA(context->dwCertEncodingType,
1531 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1532 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1533 if(infoA->lpszSubjectInfo)
1534 CertNameToStrA(context->dwCertEncodingType,
1535 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1536 infoA->lpszSubjectInfo, len);
1537 len = CertNameToStrA(context->dwCertEncodingType,
1538 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1539 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1540 if(infoA->lpszIssuerInfo)
1541 CertNameToStrA(context->dwCertEncodingType,
1542 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1543 infoA->lpszIssuerInfo, len);
1547 * Contrary to MSDN, these do not appear to be set.
1549 * lpszSignatureAlgName
1550 * lpszEncryptionAlgName
1553 CertFreeCertificateContext(context);
1554 return ERROR_SUCCESS;
1559 FIXME("Not implemented option %d\n", option);
1560 return ERROR_INTERNET_INVALID_OPTION;
1563 static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1565 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1568 case INTERNET_OPTION_SEND_TIMEOUT:
1569 case INTERNET_OPTION_RECEIVE_TIMEOUT:
1570 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1572 if (size != sizeof(DWORD))
1573 return ERROR_INVALID_PARAMETER;
1575 return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1579 return ERROR_INTERNET_INVALID_OPTION;
1582 static DWORD HTTP_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1586 if(!NETCON_recv(&req->netConnection, buffer, min(size, req->dwContentLength - req->dwContentRead),
1587 sync ? MSG_WAITALL : 0, &bytes_read)) {
1588 if(req->dwContentLength != -1 && req->dwContentRead != req->dwContentLength)
1589 ERR("not all data received %d/%d\n", req->dwContentRead, req->dwContentLength);
1591 /* always return success, even if the network layer returns an error */
1593 HTTP_FinishedReading(req);
1594 return ERROR_SUCCESS;
1597 req->dwContentRead += bytes_read;
1600 if(req->lpszCacheFile) {
1603 res = WriteFile(req->hCacheFile, buffer, bytes_read, NULL, NULL);
1605 WARN("WriteFile failed: %u\n", GetLastError());
1608 if(!bytes_read && (req->dwContentRead == req->dwContentLength))
1609 HTTP_FinishedReading(req);
1611 return ERROR_SUCCESS;
1614 static DWORD get_chunk_size(const char *buffer)
1619 for (p = buffer; *p; p++)
1621 if (*p >= '0' && *p <= '9') size = size * 16 + *p - '0';
1622 else if (*p >= 'a' && *p <= 'f') size = size * 16 + *p - 'a' + 10;
1623 else if (*p >= 'A' && *p <= 'F') size = size * 16 + *p - 'A' + 10;
1624 else if (*p == ';') break;
1629 static DWORD HTTP_ReadChunked(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1631 char reply[MAX_REPLY_LEN], *p = buffer;
1632 DWORD buflen, to_read, to_write = size;
1638 if (*read == size) break;
1640 if (req->dwContentLength == ~0UL) /* new chunk */
1642 buflen = sizeof(reply);
1643 if (!NETCON_getNextLine(&req->netConnection, reply, &buflen)) break;
1645 if (!(req->dwContentLength = get_chunk_size(reply)))
1647 /* zero sized chunk marks end of transfer; read any trailing headers and return */
1648 HTTP_GetResponseHeaders(req, FALSE);
1652 to_read = min(to_write, req->dwContentLength - req->dwContentRead);
1654 if (!NETCON_recv(&req->netConnection, p, to_read, sync ? MSG_WAITALL : 0, &bytes_read))
1656 if (bytes_read != to_read)
1657 ERR("Not all data received %d/%d\n", bytes_read, to_read);
1659 /* always return success, even if the network layer returns an error */
1663 if (!bytes_read) break;
1665 req->dwContentRead += bytes_read;
1666 to_write -= bytes_read;
1667 *read += bytes_read;
1669 if (req->lpszCacheFile)
1671 if (!WriteFile(req->hCacheFile, p, bytes_read, NULL, NULL))
1672 WARN("WriteFile failed: %u\n", GetLastError());
1676 if (req->dwContentRead == req->dwContentLength) /* chunk complete */
1678 req->dwContentRead = 0;
1679 req->dwContentLength = ~0UL;
1681 buflen = sizeof(reply);
1682 if (!NETCON_getNextLine(&req->netConnection, reply, &buflen))
1684 ERR("Malformed chunk\n");
1690 if (!*read) HTTP_FinishedReading(req);
1691 return ERROR_SUCCESS;
1694 static DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1697 DWORD buflen = sizeof(encoding);
1698 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
1700 if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_TRANSFER_ENCODING, encoding, &buflen, NULL) &&
1701 !strcmpiW(encoding, szChunked))
1703 return HTTP_ReadChunked(req, buffer, size, read, sync);
1706 return HTTP_Read(req, buffer, size, read, sync);
1709 static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1711 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1712 return HTTPREQ_Read(req, buffer, size, read, TRUE);
1715 static void HTTPREQ_AsyncReadFileExProc(WORKREQUEST *workRequest)
1717 struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
1718 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1719 INTERNET_ASYNC_RESULT iar;
1722 TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
1724 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1725 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1727 iar.dwResult = res == ERROR_SUCCESS;
1730 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1731 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1732 sizeof(INTERNET_ASYNC_RESULT));
1735 static DWORD HTTPREQ_ReadFileExA(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSA *buffers,
1736 DWORD flags, DWORD_PTR context)
1739 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1742 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1743 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1745 if (buffers->dwStructSize != sizeof(*buffers))
1746 return ERROR_INVALID_PARAMETER;
1748 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1750 if (hdr->dwFlags & INTERNET_FLAG_ASYNC) {
1751 DWORD available = 0;
1753 NETCON_query_data_available(&req->netConnection, &available);
1756 WORKREQUEST workRequest;
1758 workRequest.asyncproc = HTTPREQ_AsyncReadFileExProc;
1759 workRequest.hdr = WININET_AddRef(&req->hdr);
1760 workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
1762 INTERNET_AsyncCall(&workRequest);
1764 return ERROR_IO_PENDING;
1768 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1769 !(flags & IRF_NO_WAIT));
1771 if (res == ERROR_SUCCESS) {
1772 DWORD size = buffers->dwBufferLength;
1773 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1774 &size, sizeof(size));
1780 static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1782 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
1784 return NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written);
1787 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1789 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1790 INTERNET_ASYNC_RESULT iar;
1793 TRACE("%p\n", workRequest->hdr);
1795 iar.dwResult = NETCON_recv(&req->netConnection, buffer,
1796 min(sizeof(buffer), req->dwContentLength - req->dwContentRead),
1797 MSG_PEEK, (int *)&iar.dwError);
1799 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1800 sizeof(INTERNET_ASYNC_RESULT));
1803 static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1805 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1809 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
1811 if(!NETCON_query_data_available(&req->netConnection, available) || *available)
1812 return ERROR_SUCCESS;
1814 /* Even if we are in async mode, we need to determine whether
1815 * there is actually more data available. We do this by trying
1816 * to peek only a single byte in async mode. */
1817 async = (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) != 0;
1819 if (NETCON_recv(&req->netConnection, buffer,
1820 min(async ? 1 : sizeof(buffer), req->dwContentLength - req->dwContentRead),
1821 MSG_PEEK, (int *)available) && async && *available)
1823 WORKREQUEST workRequest;
1826 workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
1827 workRequest.hdr = WININET_AddRef( &req->hdr );
1829 INTERNET_AsyncCall(&workRequest);
1831 return ERROR_IO_PENDING;
1834 return ERROR_SUCCESS;
1837 static const HANDLEHEADERVtbl HTTPREQVtbl = {
1839 HTTPREQ_CloseConnection,
1840 HTTPREQ_QueryOption,
1843 HTTPREQ_ReadFileExA,
1845 HTTPREQ_QueryDataAvailable,
1849 /***********************************************************************
1850 * HTTP_HttpOpenRequestW (internal)
1852 * Open a HTTP request handle
1855 * HINTERNET a HTTP request handle on success
1859 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1860 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1861 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1862 DWORD dwFlags, DWORD_PTR dwContext)
1864 LPWININETAPPINFOW hIC = NULL;
1865 LPWININETHTTPREQW lpwhr;
1867 LPWSTR lpszUrl = NULL;
1869 HINTERNET handle = NULL;
1870 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
1876 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1877 hIC = lpwhs->lpAppInfo;
1879 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1882 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1885 lpwhr->hdr.htype = WH_HHTTPREQ;
1886 lpwhr->hdr.vtbl = &HTTPREQVtbl;
1887 lpwhr->hdr.dwFlags = dwFlags;
1888 lpwhr->hdr.dwContext = dwContext;
1889 lpwhr->hdr.refs = 1;
1890 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1891 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
1893 WININET_AddRef( &lpwhs->hdr );
1894 lpwhr->lpHttpSession = lpwhs;
1895 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
1897 handle = WININET_AllocHandle( &lpwhr->hdr );
1900 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1904 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1906 InternetCloseHandle( handle );
1911 if (lpszObjectName && *lpszObjectName) {
1915 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1916 if (rc != E_POINTER)
1917 len = strlenW(lpszObjectName)+1;
1918 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1919 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1920 URL_ESCAPE_SPACES_ONLY);
1923 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
1924 strcpyW(lpwhr->lpszPath,lpszObjectName);
1928 if (lpszReferrer && *lpszReferrer)
1929 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1931 if (lpszAcceptTypes)
1934 for (i = 0; lpszAcceptTypes[i]; i++)
1936 if (!*lpszAcceptTypes[i]) continue;
1937 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
1938 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
1939 HTTP_ADDHDR_FLAG_REQ |
1940 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
1944 lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
1947 lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
1949 lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
1951 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1953 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1954 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1955 INTERNET_DEFAULT_HTTPS_PORT :
1956 INTERNET_DEFAULT_HTTP_PORT);
1958 if (lpwhs->nHostPort == INTERNET_INVALID_PORT_NUMBER)
1959 lpwhs->nHostPort = (dwFlags & INTERNET_FLAG_SECURE ?
1960 INTERNET_DEFAULT_HTTPS_PORT :
1961 INTERNET_DEFAULT_HTTP_PORT);
1963 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1964 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1966 Host = HTTP_GetHeader(lpwhr,szHost);
1968 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1969 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1970 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1972 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1973 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1976 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1977 static const WCHAR szcrlf[] = {'\r','\n',0};
1979 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1981 cnt += sprintfW(lpszCookies, szCookie);
1982 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1983 strcatW(lpszCookies, szcrlf);
1985 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1986 HTTP_ADDREQ_FLAG_ADD);
1987 HeapFree(GetProcessHeap(), 0, lpszCookies);
1989 HeapFree(GetProcessHeap(), 0, lpszUrl);
1992 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1993 INTERNET_STATUS_HANDLE_CREATED, &handle,
1997 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
2000 if (!HTTP_ResolveName(lpwhr))
2002 InternetCloseHandle( handle );
2008 WININET_Release( &lpwhr->hdr );
2010 TRACE("<-- %p (%p)\n", handle, lpwhr);
2014 /* read any content returned by the server so that the connection can be
2016 static void HTTP_DrainContent(WININETHTTPREQW *req)
2020 if (!NETCON_connected(&req->netConnection)) return;
2022 if (req->dwContentLength == -1)
2023 NETCON_close(&req->netConnection);
2028 if (HTTP_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
2030 } while (bytes_read);
2033 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
2034 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2035 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2036 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2037 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2038 static const WCHAR szAge[] = { 'A','g','e',0 };
2039 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
2040 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2041 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2042 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2043 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2044 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
2045 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2046 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2047 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2048 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2049 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2050 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 };
2051 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2052 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
2053 static const WCHAR szDate[] = { 'D','a','t','e',0 };
2054 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
2055 static const WCHAR szETag[] = { 'E','T','a','g',0 };
2056 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
2057 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2058 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
2059 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2060 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2061 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
2062 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2063 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2064 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
2065 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2066 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2067 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
2068 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2069 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
2070 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
2071 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
2072 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
2073 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2074 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
2075 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2076 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2077 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 };
2078 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
2079 static const WCHAR szURI[] = { 'U','R','I',0 };
2080 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2081 static const WCHAR szVary[] = { 'V','a','r','y',0 };
2082 static const WCHAR szVia[] = { 'V','i','a',0 };
2083 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
2084 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2086 static const LPCWSTR header_lookup[] = {
2087 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
2088 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
2089 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2090 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
2091 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2092 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
2093 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
2094 szAllow, /* HTTP_QUERY_ALLOW = 7 */
2095 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
2096 szDate, /* HTTP_QUERY_DATE = 9 */
2097 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
2098 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2099 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
2100 szURI, /* HTTP_QUERY_URI = 13 */
2101 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
2102 NULL, /* HTTP_QUERY_COST = 15 */
2103 NULL, /* HTTP_QUERY_LINK = 16 */
2104 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
2105 NULL, /* HTTP_QUERY_VERSION = 18 */
2106 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
2107 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
2108 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
2109 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2110 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
2111 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
2112 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2113 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2114 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2115 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
2116 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2117 NULL, /* HTTP_QUERY_FORWARDED = 30 */
2118 NULL, /* HTTP_QUERY_FROM = 31 */
2119 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2120 szLocation, /* HTTP_QUERY_LOCATION = 33 */
2121 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
2122 szReferer, /* HTTP_QUERY_REFERER = 35 */
2123 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
2124 szServer, /* HTTP_QUERY_SERVER = 37 */
2125 NULL, /* HTTP_TITLE = 38 */
2126 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
2127 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2128 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2129 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2130 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
2131 szCookie, /* HTTP_QUERY_COOKIE = 44 */
2132 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2133 NULL, /* HTTP_QUERY_REFRESH = 46 */
2134 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2135 szAge, /* HTTP_QUERY_AGE = 48 */
2136 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2137 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
2138 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2139 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2140 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2141 szETag, /* HTTP_QUERY_ETAG = 54 */
2142 szHost, /* HTTP_QUERY_HOST = 55 */
2143 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
2144 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2145 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
2146 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2147 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2148 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2149 szRange, /* HTTP_QUERY_RANGE = 62 */
2150 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2151 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
2152 szVary, /* HTTP_QUERY_VARY = 65 */
2153 szVia, /* HTTP_QUERY_VIA = 66 */
2154 szWarning, /* HTTP_QUERY_WARNING = 67 */
2155 szExpect, /* HTTP_QUERY_EXPECT = 68 */
2156 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2157 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2160 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2162 /***********************************************************************
2163 * HTTP_HttpQueryInfoW (internal)
2165 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
2166 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2168 LPHTTPHEADERW lphttpHdr = NULL;
2169 BOOL bSuccess = FALSE;
2170 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
2171 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2172 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2175 /* Find requested header structure */
2178 case HTTP_QUERY_CUSTOM:
2179 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2182 case HTTP_QUERY_RAW_HEADERS_CRLF:
2189 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
2191 headers = lpwhr->lpszRawHeaders;
2193 len = strlenW(headers);
2194 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2196 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2197 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2201 memcpy(lpBuffer, headers, (len+1)*sizeof(WCHAR));
2202 *lpdwBufferLength = len * sizeof(WCHAR);
2204 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2209 HeapFree(GetProcessHeap(), 0, headers);
2212 case HTTP_QUERY_RAW_HEADERS:
2214 static const WCHAR szCrLf[] = {'\r','\n',0};
2215 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
2217 LPWSTR pszString = (WCHAR*)lpBuffer;
2219 for (i = 0; ppszRawHeaderLines[i]; i++)
2220 size += strlenW(ppszRawHeaderLines[i]) + 1;
2222 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2224 HTTP_FreeTokens(ppszRawHeaderLines);
2225 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2226 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2230 for (i = 0; ppszRawHeaderLines[i]; i++)
2232 DWORD len = strlenW(ppszRawHeaderLines[i]);
2233 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2238 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
2240 *lpdwBufferLength = size * sizeof(WCHAR);
2241 HTTP_FreeTokens(ppszRawHeaderLines);
2245 case HTTP_QUERY_STATUS_TEXT:
2246 if (lpwhr->lpszStatusText)
2248 DWORD len = strlenW(lpwhr->lpszStatusText);
2249 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2251 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2252 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2255 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
2256 *lpdwBufferLength = len * sizeof(WCHAR);
2258 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2263 case HTTP_QUERY_VERSION:
2264 if (lpwhr->lpszVersion)
2266 DWORD len = strlenW(lpwhr->lpszVersion);
2267 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2269 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2270 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2273 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
2274 *lpdwBufferLength = len * sizeof(WCHAR);
2276 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2282 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2284 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
2285 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2286 requested_index,request_only);
2290 lphttpHdr = &lpwhr->pCustHeaders[index];
2292 /* Ensure header satisfies requested attributes */
2294 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2295 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
2297 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2304 /* coalesce value to requested type */
2305 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
2307 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2310 TRACE(" returning number : %d\n", *(int *)lpBuffer);
2312 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
2318 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2320 tmpTM = *gmtime(&tmpTime);
2321 STHook = (SYSTEMTIME *) lpBuffer;
2325 STHook->wDay = tmpTM.tm_mday;
2326 STHook->wHour = tmpTM.tm_hour;
2327 STHook->wMilliseconds = 0;
2328 STHook->wMinute = tmpTM.tm_min;
2329 STHook->wDayOfWeek = tmpTM.tm_wday;
2330 STHook->wMonth = tmpTM.tm_mon + 1;
2331 STHook->wSecond = tmpTM.tm_sec;
2332 STHook->wYear = tmpTM.tm_year;
2336 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2337 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2338 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2340 else if (lphttpHdr->lpszValue)
2342 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
2344 if (len > *lpdwBufferLength)
2346 *lpdwBufferLength = len;
2347 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2351 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2352 *lpdwBufferLength = len - sizeof(WCHAR);
2355 TRACE(" returning string : %s\n", debugstr_w(lpBuffer));
2360 /***********************************************************************
2361 * HttpQueryInfoW (WININET.@)
2363 * Queries for information about an HTTP request
2370 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2371 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2373 BOOL bSuccess = FALSE;
2374 LPWININETHTTPREQW lpwhr;
2376 if (TRACE_ON(wininet)) {
2377 #define FE(x) { x, #x }
2378 static const wininet_flag_info query_flags[] = {
2379 FE(HTTP_QUERY_MIME_VERSION),
2380 FE(HTTP_QUERY_CONTENT_TYPE),
2381 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2382 FE(HTTP_QUERY_CONTENT_ID),
2383 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2384 FE(HTTP_QUERY_CONTENT_LENGTH),
2385 FE(HTTP_QUERY_CONTENT_LANGUAGE),
2386 FE(HTTP_QUERY_ALLOW),
2387 FE(HTTP_QUERY_PUBLIC),
2388 FE(HTTP_QUERY_DATE),
2389 FE(HTTP_QUERY_EXPIRES),
2390 FE(HTTP_QUERY_LAST_MODIFIED),
2391 FE(HTTP_QUERY_MESSAGE_ID),
2393 FE(HTTP_QUERY_DERIVED_FROM),
2394 FE(HTTP_QUERY_COST),
2395 FE(HTTP_QUERY_LINK),
2396 FE(HTTP_QUERY_PRAGMA),
2397 FE(HTTP_QUERY_VERSION),
2398 FE(HTTP_QUERY_STATUS_CODE),
2399 FE(HTTP_QUERY_STATUS_TEXT),
2400 FE(HTTP_QUERY_RAW_HEADERS),
2401 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2402 FE(HTTP_QUERY_CONNECTION),
2403 FE(HTTP_QUERY_ACCEPT),
2404 FE(HTTP_QUERY_ACCEPT_CHARSET),
2405 FE(HTTP_QUERY_ACCEPT_ENCODING),
2406 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2407 FE(HTTP_QUERY_AUTHORIZATION),
2408 FE(HTTP_QUERY_CONTENT_ENCODING),
2409 FE(HTTP_QUERY_FORWARDED),
2410 FE(HTTP_QUERY_FROM),
2411 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2412 FE(HTTP_QUERY_LOCATION),
2413 FE(HTTP_QUERY_ORIG_URI),
2414 FE(HTTP_QUERY_REFERER),
2415 FE(HTTP_QUERY_RETRY_AFTER),
2416 FE(HTTP_QUERY_SERVER),
2417 FE(HTTP_QUERY_TITLE),
2418 FE(HTTP_QUERY_USER_AGENT),
2419 FE(HTTP_QUERY_WWW_AUTHENTICATE),
2420 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2421 FE(HTTP_QUERY_ACCEPT_RANGES),
2422 FE(HTTP_QUERY_SET_COOKIE),
2423 FE(HTTP_QUERY_COOKIE),
2424 FE(HTTP_QUERY_REQUEST_METHOD),
2425 FE(HTTP_QUERY_REFRESH),
2426 FE(HTTP_QUERY_CONTENT_DISPOSITION),
2428 FE(HTTP_QUERY_CACHE_CONTROL),
2429 FE(HTTP_QUERY_CONTENT_BASE),
2430 FE(HTTP_QUERY_CONTENT_LOCATION),
2431 FE(HTTP_QUERY_CONTENT_MD5),
2432 FE(HTTP_QUERY_CONTENT_RANGE),
2433 FE(HTTP_QUERY_ETAG),
2434 FE(HTTP_QUERY_HOST),
2435 FE(HTTP_QUERY_IF_MATCH),
2436 FE(HTTP_QUERY_IF_NONE_MATCH),
2437 FE(HTTP_QUERY_IF_RANGE),
2438 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2439 FE(HTTP_QUERY_MAX_FORWARDS),
2440 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2441 FE(HTTP_QUERY_RANGE),
2442 FE(HTTP_QUERY_TRANSFER_ENCODING),
2443 FE(HTTP_QUERY_UPGRADE),
2444 FE(HTTP_QUERY_VARY),
2446 FE(HTTP_QUERY_WARNING),
2447 FE(HTTP_QUERY_CUSTOM)
2449 static const wininet_flag_info modifier_flags[] = {
2450 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2451 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2452 FE(HTTP_QUERY_FLAG_NUMBER),
2453 FE(HTTP_QUERY_FLAG_COALESCE)
2456 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2457 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
2460 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
2461 TRACE(" Attribute:");
2462 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2463 if (query_flags[i].val == info) {
2464 TRACE(" %s", query_flags[i].name);
2468 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
2469 TRACE(" Unknown (%08x)", info);
2472 TRACE(" Modifier:");
2473 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2474 if (modifier_flags[i].val & info_mod) {
2475 TRACE(" %s", modifier_flags[i].name);
2476 info_mod &= ~ modifier_flags[i].val;
2481 TRACE(" Unknown (%08x)", info_mod);
2486 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2487 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2489 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2493 if (lpBuffer == NULL)
2494 *lpdwBufferLength = 0;
2495 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2496 lpBuffer, lpdwBufferLength, lpdwIndex);
2500 WININET_Release( &lpwhr->hdr );
2502 TRACE("%d <--\n", bSuccess);
2506 /***********************************************************************
2507 * HttpQueryInfoA (WININET.@)
2509 * Queries for information about an HTTP request
2516 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2517 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2523 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2524 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2526 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2527 lpdwBufferLength, lpdwIndex );
2533 len = (*lpdwBufferLength)*sizeof(WCHAR);
2534 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2536 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2542 bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
2543 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2544 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2545 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
2552 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2556 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
2557 lpBuffer, *lpdwBufferLength, NULL, NULL );
2558 *lpdwBufferLength = len - 1;
2560 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
2563 /* since the strings being returned from HttpQueryInfoW should be
2564 * only ASCII characters, it is reasonable to assume that all of
2565 * the Unicode characters can be reduced to a single byte */
2566 *lpdwBufferLength = len / sizeof(WCHAR);
2568 HeapFree(GetProcessHeap(), 0, bufferW );
2573 /***********************************************************************
2574 * HttpSendRequestExA (WININET.@)
2576 * Sends the specified request to the HTTP server and allows chunked
2581 * Failure: FALSE, call GetLastError() for more information.
2583 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2584 LPINTERNET_BUFFERSA lpBuffersIn,
2585 LPINTERNET_BUFFERSA lpBuffersOut,
2586 DWORD dwFlags, DWORD_PTR dwContext)
2588 INTERNET_BUFFERSW BuffersInW;
2591 LPWSTR header = NULL;
2593 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2594 lpBuffersOut, dwFlags, dwContext);
2598 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2599 if (lpBuffersIn->lpcszHeader)
2601 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2602 lpBuffersIn->dwHeadersLength,0,0);
2603 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2604 if (!(BuffersInW.lpcszHeader = header))
2606 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2609 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2610 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2614 BuffersInW.lpcszHeader = NULL;
2615 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2616 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2617 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2618 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2619 BuffersInW.Next = NULL;
2622 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2624 HeapFree(GetProcessHeap(),0,header);
2629 /***********************************************************************
2630 * HttpSendRequestExW (WININET.@)
2632 * Sends the specified request to the HTTP server and allows chunked
2637 * Failure: FALSE, call GetLastError() for more information.
2639 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2640 LPINTERNET_BUFFERSW lpBuffersIn,
2641 LPINTERNET_BUFFERSW lpBuffersOut,
2642 DWORD dwFlags, DWORD_PTR dwContext)
2645 LPWININETHTTPREQW lpwhr;
2646 LPWININETHTTPSESSIONW lpwhs;
2647 LPWININETAPPINFOW hIC;
2649 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2650 lpBuffersOut, dwFlags, dwContext);
2652 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2654 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2656 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2660 lpwhs = lpwhr->lpHttpSession;
2661 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
2662 hIC = lpwhs->lpAppInfo;
2663 assert(hIC->hdr.htype == WH_HINIT);
2665 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2667 WORKREQUEST workRequest;
2668 struct WORKREQ_HTTPSENDREQUESTW *req;
2670 workRequest.asyncproc = AsyncHttpSendRequestProc;
2671 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2672 req = &workRequest.u.HttpSendRequestW;
2675 if (lpBuffersIn->lpcszHeader)
2676 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2677 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2679 req->lpszHeader = NULL;
2680 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2681 req->lpOptional = lpBuffersIn->lpvBuffer;
2682 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2683 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2687 req->lpszHeader = NULL;
2688 req->dwHeaderLength = 0;
2689 req->lpOptional = NULL;
2690 req->dwOptionalLength = 0;
2691 req->dwContentLength = 0;
2694 req->bEndRequest = FALSE;
2696 INTERNET_AsyncCall(&workRequest);
2698 * This is from windows.
2700 INTERNET_SetLastError(ERROR_IO_PENDING);
2705 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2706 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2707 lpBuffersIn->dwBufferTotal, FALSE);
2709 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
2714 WININET_Release( &lpwhr->hdr );
2720 /***********************************************************************
2721 * HttpSendRequestW (WININET.@)
2723 * Sends the specified request to the HTTP server
2730 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
2731 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2733 LPWININETHTTPREQW lpwhr;
2734 LPWININETHTTPSESSIONW lpwhs = NULL;
2735 LPWININETAPPINFOW hIC = NULL;
2738 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2739 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
2741 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2742 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2744 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2749 lpwhs = lpwhr->lpHttpSession;
2750 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2752 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2757 hIC = lpwhs->lpAppInfo;
2758 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2760 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2765 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2767 WORKREQUEST workRequest;
2768 struct WORKREQ_HTTPSENDREQUESTW *req;
2770 workRequest.asyncproc = AsyncHttpSendRequestProc;
2771 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2772 req = &workRequest.u.HttpSendRequestW;
2775 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, dwHeaderLength * sizeof(WCHAR));
2776 memcpy(req->lpszHeader, lpszHeaders, dwHeaderLength * sizeof(WCHAR));
2779 req->lpszHeader = 0;
2780 req->dwHeaderLength = dwHeaderLength;
2781 req->lpOptional = lpOptional;
2782 req->dwOptionalLength = dwOptionalLength;
2783 req->dwContentLength = dwOptionalLength;
2784 req->bEndRequest = TRUE;
2786 INTERNET_AsyncCall(&workRequest);
2788 * This is from windows.
2790 INTERNET_SetLastError(ERROR_IO_PENDING);
2795 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
2796 dwHeaderLength, lpOptional, dwOptionalLength,
2797 dwOptionalLength, TRUE);
2801 WININET_Release( &lpwhr->hdr );
2805 /***********************************************************************
2806 * HttpSendRequestA (WININET.@)
2808 * Sends the specified request to the HTTP server
2815 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
2816 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2819 LPWSTR szHeaders=NULL;
2820 DWORD nLen=dwHeaderLength;
2821 if(lpszHeaders!=NULL)
2823 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2824 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2825 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
2827 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
2828 HeapFree(GetProcessHeap(),0,szHeaders);
2832 static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
2834 LPHTTPHEADERW host_header;
2836 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2838 host_header = HTTP_GetHeader(req, szHost);
2842 sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
2846 /***********************************************************************
2847 * HTTP_HandleRedirect (internal)
2849 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
2851 static const WCHAR szContentType[] = {'C','o','n','t','e','n','t','-','T','y','p','e',0};
2852 static const WCHAR szContentLength[] = {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
2853 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2854 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
2855 BOOL using_proxy = hIC->lpszProxy && hIC->lpszProxy[0];
2856 WCHAR path[INTERNET_MAX_URL_LENGTH];
2861 /* if it's an absolute path, keep the same session info */
2862 lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH);
2866 URL_COMPONENTSW urlComponents;
2867 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2868 static WCHAR szHttp[] = {'h','t','t','p',0};
2869 static WCHAR szHttps[] = {'h','t','t','p','s',0};
2870 DWORD url_length = 0;
2872 LPWSTR combined_url;
2874 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2875 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
2876 urlComponents.dwSchemeLength = 0;
2877 urlComponents.lpszHostName = lpwhs->lpszHostName;
2878 urlComponents.dwHostNameLength = 0;
2879 urlComponents.nPort = lpwhs->nHostPort;
2880 urlComponents.lpszUserName = lpwhs->lpszUserName;
2881 urlComponents.dwUserNameLength = 0;
2882 urlComponents.lpszPassword = NULL;
2883 urlComponents.dwPasswordLength = 0;
2884 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2885 urlComponents.dwUrlPathLength = 0;
2886 urlComponents.lpszExtraInfo = NULL;
2887 urlComponents.dwExtraInfoLength = 0;
2889 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2890 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2893 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
2895 /* convert from bytes to characters */
2896 url_length = url_length / sizeof(WCHAR) - 1;
2897 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2899 HeapFree(GetProcessHeap(), 0, orig_url);
2904 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2905 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2907 HeapFree(GetProcessHeap(), 0, orig_url);
2910 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2912 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2914 HeapFree(GetProcessHeap(), 0, orig_url);
2915 HeapFree(GetProcessHeap(), 0, combined_url);
2918 HeapFree(GetProcessHeap(), 0, orig_url);
2924 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2925 urlComponents.lpszScheme = protocol;
2926 urlComponents.dwSchemeLength = 32;
2927 urlComponents.lpszHostName = hostName;
2928 urlComponents.dwHostNameLength = MAXHOSTNAME;
2929 urlComponents.lpszUserName = userName;
2930 urlComponents.dwUserNameLength = 1024;
2931 urlComponents.lpszPassword = NULL;
2932 urlComponents.dwPasswordLength = 0;
2933 urlComponents.lpszUrlPath = path;
2934 urlComponents.dwUrlPathLength = 2048;
2935 urlComponents.lpszExtraInfo = NULL;
2936 urlComponents.dwExtraInfoLength = 0;
2937 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2939 HeapFree(GetProcessHeap(), 0, combined_url);
2943 HeapFree(GetProcessHeap(), 0, combined_url);
2945 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2946 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2948 TRACE("redirect from secure page to non-secure page\n");
2949 /* FIXME: warn about from secure redirect to non-secure page */
2950 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2952 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2953 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2955 TRACE("redirect from non-secure page to secure page\n");
2956 /* FIXME: notify about redirect to secure page */
2957 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2960 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2962 if (lstrlenW(protocol)>4) /*https*/
2963 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2965 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2970 * This upsets redirects to binary files on sourceforge.net
2971 * and gives an html page instead of the target file
2972 * Examination of the HTTP request sent by native wininet.dll
2973 * reveals that it doesn't send a referrer in that case.
2974 * Maybe there's a flag that enables this, or maybe a referrer
2975 * shouldn't be added in case of a redirect.
2978 /* consider the current host as the referrer */
2979 if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
2980 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2981 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2982 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2985 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2986 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2987 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2990 static const WCHAR fmt[] = {'%','s',':','%','i',0};
2991 len = lstrlenW(hostName);
2992 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2993 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2994 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2997 lpwhs->lpszHostName = WININET_strdupW(hostName);
2999 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
3001 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3002 lpwhs->lpszUserName = NULL;
3004 lpwhs->lpszUserName = WININET_strdupW(userName);
3008 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3009 lpwhs->lpszServerName = WININET_strdupW(hostName);
3010 lpwhs->nServerPort = urlComponents.nPort;
3012 if (!HTTP_ResolveName(lpwhr))
3015 NETCON_close(&lpwhr->netConnection);
3017 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3021 TRACE("Redirect through proxy\n");
3024 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
3025 lpwhr->lpszPath=NULL;
3031 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
3032 if (rc != E_POINTER)
3033 needed = strlenW(path)+1;
3034 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
3035 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
3036 URL_ESCAPE_SPACES_ONLY);
3039 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
3040 strcpyW(lpwhr->lpszPath,path);
3044 /* Remove custom content-type/length headers on redirects. */
3045 index = HTTP_GetCustomHeaderIndex(lpwhr, szContentType, 0, TRUE);
3047 HTTP_DeleteCustomHeader(lpwhr, index);
3048 index = HTTP_GetCustomHeaderIndex(lpwhr, szContentLength, 0, TRUE);
3050 HTTP_DeleteCustomHeader(lpwhr, index);
3055 /***********************************************************************
3056 * HTTP_build_req (internal)
3058 * concatenate all the strings in the request together
3060 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
3065 for( t = list; *t ; t++ )
3066 len += strlenW( *t );
3069 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
3072 for( t = list; *t ; t++ )
3078 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
3081 LPWSTR requestString;
3087 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
3088 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
3089 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3093 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
3094 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
3095 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
3096 HeapFree( GetProcessHeap(), 0, lpszPath );
3098 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3099 NULL, 0, NULL, NULL );
3100 len--; /* the nul terminator isn't needed */
3101 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
3102 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3103 ascii_req, len, NULL, NULL );
3104 HeapFree( GetProcessHeap(), 0, requestString );
3106 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
3108 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
3109 HeapFree( GetProcessHeap(), 0, ascii_req );
3110 if (!ret || cnt < 0)
3113 responseLen = HTTP_GetResponseHeaders( lpwhr, TRUE );
3120 /***********************************************************************
3121 * HTTP_HttpSendRequestW (internal)
3123 * Sends the specified request to the HTTP server
3130 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
3131 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
3132 DWORD dwContentLength, BOOL bEndRequest)
3135 BOOL bSuccess = FALSE;
3136 LPWSTR requestString = NULL;
3139 INTERNET_ASYNC_RESULT iar;
3140 static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
3141 static const WCHAR szPost[] = { 'P','O','S','T',0 };
3142 static const WCHAR szContentLength[] =
3143 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3144 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
3146 TRACE("--> %p\n", lpwhr);
3148 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
3150 /* Clear any error information */
3151 INTERNET_SetLastError(0);
3153 /* if the verb is NULL default to GET */
3154 if (!lpwhr->lpszVerb)
3155 lpwhr->lpszVerb = WININET_strdupW(szGET);
3157 if (dwContentLength || !strcmpW(lpwhr->lpszVerb, szPost))
3159 sprintfW(contentLengthStr, szContentLength, dwContentLength);
3160 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3162 if (lpwhr->lpHttpSession->lpAppInfo->lpszAgent)
3164 WCHAR *agent_header;
3165 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
3168 len = strlenW(lpwhr->lpHttpSession->lpAppInfo->lpszAgent) + strlenW(user_agent);
3169 agent_header = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3170 sprintfW(agent_header, user_agent, lpwhr->lpHttpSession->lpAppInfo->lpszAgent);
3172 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3173 HeapFree(GetProcessHeap(), 0, agent_header);
3183 /* like native, just in case the caller forgot to call InternetReadFile
3184 * for all the data */
3185 HTTP_DrainContent(lpwhr);
3186 lpwhr->dwContentRead = 0;
3188 if (TRACE_ON(wininet))
3190 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3191 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
3195 HTTP_ProcessHeader(lpwhr, szConnection,
3196 lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION ? szKeepAlive : szClose,
3197 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
3199 HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
3200 HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
3202 /* add the headers the caller supplied */
3203 if( lpszHeaders && dwHeaderLength )
3205 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
3206 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3209 if (lpwhr->lpHttpSession->lpAppInfo->lpszProxy && lpwhr->lpHttpSession->lpAppInfo->lpszProxy[0])
3211 WCHAR *url = HTTP_BuildProxyRequestUrl(lpwhr);
3212 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, url, lpwhr->lpszVersion);
3213 HeapFree(GetProcessHeap(), 0, url);
3216 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
3219 TRACE("Request header -> %s\n", debugstr_w(requestString) );
3221 /* Send the request and store the results */
3222 if (!HTTP_OpenConnection(lpwhr))
3225 /* send the request as ASCII, tack on the optional data */
3227 dwOptionalLength = 0;
3228 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3229 NULL, 0, NULL, NULL );
3230 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
3231 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3232 ascii_req, len, NULL, NULL );
3234 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
3235 len = (len + dwOptionalLength - 1);
3237 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
3239 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3240 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3242 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
3243 HeapFree( GetProcessHeap(), 0, ascii_req );
3245 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3246 INTERNET_STATUS_REQUEST_SENT,
3247 &len, sizeof(DWORD));
3254 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
3256 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3257 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3262 responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
3266 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3267 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
3270 HTTP_ProcessCookies(lpwhr);
3272 dwBufferSize = sizeof(lpwhr->dwContentLength);
3273 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
3274 &lpwhr->dwContentLength,&dwBufferSize,NULL))
3275 lpwhr->dwContentLength = -1;
3277 if (lpwhr->dwContentLength == 0)
3278 HTTP_FinishedReading(lpwhr);
3280 /* Correct the case where both a Content-Length and Transfer-encoding = chunked are set */
3282 dwBufferSize = sizeof(encoding);
3283 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_TRANSFER_ENCODING, encoding, &dwBufferSize, NULL) &&
3284 !strcmpiW(encoding, szChunked))
3286 lpwhr->dwContentLength = -1;
3289 dwBufferSize = sizeof(dwStatusCode);
3290 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
3291 &dwStatusCode,&dwBufferSize,NULL))
3294 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
3296 WCHAR szNewLocation[INTERNET_MAX_URL_LENGTH];
3297 dwBufferSize=sizeof(szNewLocation);
3298 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
3299 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
3301 HTTP_DrainContent(lpwhr);
3302 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3303 INTERNET_STATUS_REDIRECT, szNewLocation,
3305 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
3308 HeapFree(GetProcessHeap(), 0, requestString);
3313 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
3315 WCHAR szAuthValue[2048];
3317 if (dwStatusCode == HTTP_STATUS_DENIED)
3320 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3322 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3324 lpwhr->lpHttpSession->lpszUserName,
3325 lpwhr->lpHttpSession->lpszPassword))
3332 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
3335 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3337 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3338 &lpwhr->pProxyAuthInfo,
3339 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
3340 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
3354 /* FIXME: Better check, when we have to create the cache file */
3355 if(bSuccess && (lpwhr->hdr.dwFlags & INTERNET_FLAG_NEED_FILE)) {
3356 WCHAR url[INTERNET_MAX_URL_LENGTH];
3357 WCHAR cacheFileName[MAX_PATH+1];
3360 b = HTTP_GetRequestURL(lpwhr, url);
3362 WARN("Could not get URL\n");
3366 b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
3368 lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
3369 lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
3370 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3371 if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
3372 WARN("Could not create file: %u\n", GetLastError());
3373 lpwhr->hCacheFile = NULL;
3376 WARN("Could not create cache entry: %08x\n", GetLastError());
3382 HeapFree(GetProcessHeap(), 0, requestString);
3384 /* TODO: send notification for P3P header */
3386 iar.dwResult = (DWORD)bSuccess;
3387 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
3389 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3390 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3391 sizeof(INTERNET_ASYNC_RESULT));
3397 /***********************************************************************
3398 * HTTPSESSION_Destroy (internal)
3400 * Deallocate session handle
3403 static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3405 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3407 TRACE("%p\n", lpwhs);
3409 WININET_Release(&lpwhs->lpAppInfo->hdr);
3411 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3412 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3413 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3414 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3415 HeapFree(GetProcessHeap(), 0, lpwhs);
3418 static DWORD HTTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3421 case INTERNET_OPTION_HANDLE_TYPE:
3422 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3424 if (*size < sizeof(ULONG))
3425 return ERROR_INSUFFICIENT_BUFFER;
3427 *size = sizeof(DWORD);
3428 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
3429 return ERROR_SUCCESS;
3432 FIXME("Not implemented option %d\n", option);
3433 return ERROR_INTERNET_INVALID_OPTION;
3436 static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
3437 HTTPSESSION_Destroy,
3439 HTTPSESSION_QueryOption,
3449 /***********************************************************************
3450 * HTTP_Connect (internal)
3452 * Create http session handle
3455 * HINTERNET a session handle on success
3459 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
3460 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
3461 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
3462 DWORD dwInternalFlags)
3464 LPWININETHTTPSESSIONW lpwhs = NULL;
3465 HINTERNET handle = NULL;
3469 if (!lpszServerName || !lpszServerName[0])
3471 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3475 assert( hIC->hdr.htype == WH_HINIT );
3477 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
3480 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3485 * According to my tests. The name is not resolved until a request is sent
3488 lpwhs->hdr.htype = WH_HHTTPSESSION;
3489 lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
3490 lpwhs->hdr.dwFlags = dwFlags;
3491 lpwhs->hdr.dwContext = dwContext;
3492 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
3493 lpwhs->hdr.refs = 1;
3494 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
3496 WININET_AddRef( &hIC->hdr );
3497 lpwhs->lpAppInfo = hIC;
3498 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
3500 handle = WININET_AllocHandle( &lpwhs->hdr );
3503 ERR("Failed to alloc handle\n");
3504 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3508 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
3509 if(strchrW(hIC->lpszProxy, ' '))
3510 FIXME("Several proxies not implemented.\n");
3511 if(hIC->lpszProxyBypass)
3512 FIXME("Proxy bypass is ignored.\n");
3514 if (lpszServerName && lpszServerName[0])
3516 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
3517 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
3519 if (lpszUserName && lpszUserName[0])
3520 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
3521 if (lpszPassword && lpszPassword[0])
3522 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
3523 lpwhs->nServerPort = nServerPort;
3524 lpwhs->nHostPort = nServerPort;
3526 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3527 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
3529 INTERNET_SendCallback(&hIC->hdr, dwContext,
3530 INTERNET_STATUS_HANDLE_CREATED, &handle,
3536 WININET_Release( &lpwhs->hdr );
3539 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3543 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
3548 /***********************************************************************
3549 * HTTP_OpenConnection (internal)
3551 * Connect to a web server
3558 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
3560 BOOL bSuccess = FALSE;
3561 LPWININETHTTPSESSIONW lpwhs;
3562 LPWININETAPPINFOW hIC = NULL;
3568 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3570 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3574 if (NETCON_connected(&lpwhr->netConnection))
3580 lpwhs = lpwhr->lpHttpSession;
3582 hIC = lpwhs->lpAppInfo;
3583 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
3584 szaddr, sizeof(szaddr));
3585 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3586 INTERNET_STATUS_CONNECTING_TO_SERVER,
3590 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
3593 WARN("Socket creation failed\n");
3597 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
3598 sizeof(lpwhs->socketAddress)))
3601 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
3603 /* Note: we differ from Microsoft's WinINet here. they seem to have
3604 * a bug that causes no status callbacks to be sent when starting
3605 * a tunnel to a proxy server using the CONNECT verb. i believe our
3606 * behaviour to be more correct and to not cause any incompatibilities
3607 * because using a secure connection through a proxy server is a rare
3608 * case that would be hard for anyone to depend on */
3609 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
3612 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
3614 WARN("Couldn't connect securely to host\n");
3619 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3620 INTERNET_STATUS_CONNECTED_TO_SERVER,
3621 szaddr, strlen(szaddr)+1);
3626 TRACE("%d <--\n", bSuccess);
3631 /***********************************************************************
3632 * HTTP_clear_response_headers (internal)
3634 * clear out any old response headers
3636 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
3640 for( i=0; i<lpwhr->nCustHeaders; i++)
3642 if( !lpwhr->pCustHeaders[i].lpszField )
3644 if( !lpwhr->pCustHeaders[i].lpszValue )
3646 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
3648 HTTP_DeleteCustomHeader( lpwhr, i );
3653 /***********************************************************************
3654 * HTTP_GetResponseHeaders (internal)
3656 * Read server response
3663 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear)
3666 WCHAR buffer[MAX_REPLY_LEN];
3667 DWORD buflen = MAX_REPLY_LEN;
3668 BOOL bSuccess = FALSE;
3670 static const WCHAR szCrLf[] = {'\r','\n',0};
3671 static const WCHAR szHundred[] = {'1','0','0',0};
3672 char bufferA[MAX_REPLY_LEN];
3673 LPWSTR status_code, status_text;
3674 DWORD cchMaxRawHeaders = 1024;
3675 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3676 DWORD cchRawHeaders = 0;
3680 /* clear old response headers (eg. from a redirect response) */
3681 if (clear) HTTP_clear_response_headers( lpwhr );
3683 if (!NETCON_connected(&lpwhr->netConnection))
3688 * HACK peek at the buffer
3690 buflen = MAX_REPLY_LEN;
3691 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
3694 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3696 memset(buffer, 0, MAX_REPLY_LEN);
3697 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3699 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3701 /* split the version from the status code */
3702 status_code = strchrW( buffer, ' ' );
3707 /* split the status code from the status text */
3708 status_text = strchrW( status_code, ' ' );
3713 TRACE("version [%s] status code [%s] status text [%s]\n",
3714 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3716 } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
3718 /* Add status code */
3719 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3720 HTTP_ADDHDR_FLAG_REPLACE);
3722 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3723 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3725 lpwhr->lpszVersion= WININET_strdupW(buffer);
3726 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3728 /* Restore the spaces */
3729 *(status_code-1) = ' ';
3730 *(status_text-1) = ' ';
3732 /* regenerate raw headers */
3733 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3735 cchMaxRawHeaders *= 2;
3736 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3738 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3739 cchRawHeaders += (buflen-1);
3740 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3741 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3742 lpszRawHeaders[cchRawHeaders] = '\0';
3744 /* Parse each response line */
3747 buflen = MAX_REPLY_LEN;
3748 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3750 LPWSTR * pFieldAndValue;
3752 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
3753 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3755 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3757 cchMaxRawHeaders *= 2;
3758 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3760 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3761 cchRawHeaders += (buflen-1);
3762 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3763 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3764 lpszRawHeaders[cchRawHeaders] = '\0';
3766 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3767 if (!pFieldAndValue)
3770 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
3771 HTTP_ADDREQ_FLAG_ADD );
3773 HTTP_FreeTokens(pFieldAndValue);
3783 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3784 lpwhr->lpszRawHeaders = lpszRawHeaders;
3785 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
3798 static void strip_spaces(LPWSTR start)
3803 while (*str == ' ' && *str != '\0')
3807 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
3809 end = start + strlenW(start) - 1;
3810 while (end >= start && *end == ' ')
3818 /***********************************************************************
3819 * HTTP_InterpretHttpHeader (internal)
3821 * Parse server response
3825 * Pointer to array of field, value, NULL on success.
3828 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
3830 LPWSTR * pTokenPair;
3834 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
3836 pszColon = strchrW(buffer, ':');
3837 /* must have two tokens */
3840 HTTP_FreeTokens(pTokenPair);
3842 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
3846 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
3849 HTTP_FreeTokens(pTokenPair);
3852 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
3853 pTokenPair[0][pszColon - buffer] = '\0';
3857 len = strlenW(pszColon);
3858 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
3861 HTTP_FreeTokens(pTokenPair);
3864 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
3866 strip_spaces(pTokenPair[0]);
3867 strip_spaces(pTokenPair[1]);
3869 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
3873 /***********************************************************************
3874 * HTTP_ProcessHeader (internal)
3876 * Stuff header into header tables according to <dwModifier>
3880 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3882 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
3884 LPHTTPHEADERW lphttpHdr = NULL;
3885 BOOL bSuccess = FALSE;
3887 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
3889 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
3891 /* REPLACE wins out over ADD */
3892 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3893 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
3895 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
3898 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
3902 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
3906 lphttpHdr = &lpwhr->pCustHeaders[index];
3912 hdr.lpszField = (LPWSTR)field;
3913 hdr.lpszValue = (LPWSTR)value;
3914 hdr.wFlags = hdr.wCount = 0;
3916 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3917 hdr.wFlags |= HDR_ISREQUEST;
3919 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3921 /* no value to delete */
3924 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3925 lphttpHdr->wFlags |= HDR_ISREQUEST;
3927 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
3929 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3931 HTTP_DeleteCustomHeader( lpwhr, index );
3937 hdr.lpszField = (LPWSTR)field;
3938 hdr.lpszValue = (LPWSTR)value;
3939 hdr.wFlags = hdr.wCount = 0;
3941 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3942 hdr.wFlags |= HDR_ISREQUEST;
3944 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3949 else if (dwModifier & COALESCEFLAGS)
3954 INT origlen = strlenW(lphttpHdr->lpszValue);
3955 INT valuelen = strlenW(value);
3957 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
3960 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3962 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3965 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3968 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
3970 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
3973 lphttpHdr->lpszValue = lpsztmp;
3974 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
3977 lphttpHdr->lpszValue[origlen] = ch;
3979 lphttpHdr->lpszValue[origlen] = ' ';
3983 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
3984 lphttpHdr->lpszValue[len] = '\0';
3989 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
3990 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3993 TRACE("<-- %d\n",bSuccess);
3998 /***********************************************************************
3999 * HTTP_FinishedReading (internal)
4001 * Called when all content from server has been read by client.
4004 BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
4006 WCHAR szVersion[10];
4007 DWORD dwBufferSize = sizeof(szVersion);
4011 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
4012 * the connection is keep-alive by default */
4013 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_VERSION, szVersion,
4014 &dwBufferSize, NULL) ||
4015 strcmpiW(szVersion, g_szHttp1_1))
4017 WCHAR szConnectionResponse[20];
4018 dwBufferSize = sizeof(szConnectionResponse);
4019 if ((!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) ||
4020 strcmpiW(szConnectionResponse, szKeepAlive)) &&
4021 (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) ||
4022 strcmpiW(szConnectionResponse, szKeepAlive)))
4024 HTTPREQ_CloseConnection(&lpwhr->hdr);
4028 /* FIXME: store data in the URL cache here */
4034 /***********************************************************************
4035 * HTTP_GetCustomHeaderIndex (internal)
4037 * Return index of custom header from header array
4040 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
4041 int requested_index, BOOL request_only)
4045 TRACE("%s\n", debugstr_w(lpszField));
4047 for (index = 0; index < lpwhr->nCustHeaders; index++)
4049 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
4052 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4055 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4058 if (requested_index == 0)
4063 if (index >= lpwhr->nCustHeaders)
4066 TRACE("Return: %d\n", index);
4071 /***********************************************************************
4072 * HTTP_InsertCustomHeader (internal)
4074 * Insert header into array
4077 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
4080 LPHTTPHEADERW lph = NULL;
4083 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
4084 count = lpwhr->nCustHeaders + 1;
4086 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
4088 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
4092 lpwhr->pCustHeaders = lph;
4093 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
4094 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
4095 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
4096 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
4097 lpwhr->nCustHeaders++;
4102 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4109 /***********************************************************************
4110 * HTTP_DeleteCustomHeader (internal)
4112 * Delete header from array
4113 * If this function is called, the indexs may change.
4115 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
4117 if( lpwhr->nCustHeaders <= 0 )
4119 if( index >= lpwhr->nCustHeaders )
4121 lpwhr->nCustHeaders--;
4123 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
4124 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
4125 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
4131 /***********************************************************************
4132 * HTTP_VerifyValidHeader (internal)
4134 * Verify the given header is not invalid for the given http request
4137 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
4139 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4140 if (!strcmpW(lpwhr->lpszVersion, g_szHttp1_0) && !strcmpiW(field, szAccept_Encoding))
4146 /***********************************************************************
4147 * IsHostInProxyBypassList (@)
4152 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
4154 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);