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_1[] = {'H','T','T','P','/','1','.','1',0};
67 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
68 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
69 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
70 static const WCHAR szHost[] = { 'H','o','s','t',0 };
71 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
72 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
74 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
75 static const WCHAR szGET[] = { 'G','E','T', 0 };
77 #define MAXHOSTNAME 100
78 #define MAX_FIELD_VALUE_LEN 256
79 #define MAX_FIELD_LEN 256
81 #define HTTP_REFERER g_szReferer
82 #define HTTP_ACCEPT g_szAccept
83 #define HTTP_USERAGENT g_szUserAgent
85 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
86 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
87 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
88 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
89 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
90 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
91 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
93 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
104 unsigned int auth_data_len;
105 BOOL finished; /* finished authenticating */
108 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
109 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
110 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
111 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
112 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
113 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
114 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
115 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
116 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
117 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
119 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
120 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
121 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field);
122 static void HTTP_DrainContent(WININETHTTPREQW *req);
124 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
127 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
128 if (HeaderIndex == -1)
131 return &req->pCustHeaders[HeaderIndex];
134 /***********************************************************************
135 * HTTP_Tokenize (internal)
137 * Tokenize a string, allocating memory for the tokens.
139 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
141 LPWSTR * token_array;
146 /* empty string has no tokens */
150 for (i = 0; string[i]; i++)
151 if (!strncmpW(string+i, token_string, strlenW(token_string)))
155 /* we want to skip over separators, but not the null terminator */
156 for (j = 0; j < strlenW(token_string) - 1; j++)
162 /* add 1 for terminating NULL */
163 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
164 token_array[tokens] = NULL;
167 for (i = 0; i < tokens; i++)
170 next_token = strstrW(string, token_string);
171 if (!next_token) next_token = string+strlenW(string);
172 len = next_token - string;
173 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
174 memcpy(token_array[i], string, len*sizeof(WCHAR));
175 token_array[i][len] = '\0';
176 string = next_token+strlenW(token_string);
181 /***********************************************************************
182 * HTTP_FreeTokens (internal)
184 * Frees memory returned from HTTP_Tokenize.
186 static void HTTP_FreeTokens(LPWSTR * token_array)
189 for (i = 0; token_array[i]; i++)
190 HeapFree(GetProcessHeap(), 0, token_array[i]);
191 HeapFree(GetProcessHeap(), 0, token_array);
194 /* **********************************************************************
196 * Helper functions for the HttpSendRequest(Ex) functions
199 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
201 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
202 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
204 TRACE("%p\n", lpwhr);
206 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
207 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
208 req->dwContentLength, req->bEndRequest);
210 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
213 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
215 static const WCHAR szSlash[] = { '/',0 };
216 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
218 /* If we don't have a path we set it to root */
219 if (NULL == lpwhr->lpszPath)
220 lpwhr->lpszPath = WININET_strdupW(szSlash);
221 else /* remove \r and \n*/
223 int nLen = strlenW(lpwhr->lpszPath);
224 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
227 lpwhr->lpszPath[nLen]='\0';
229 /* Replace '\' with '/' */
232 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
236 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
237 lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
238 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
240 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
241 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
243 strcpyW(fixurl + 1, lpwhr->lpszPath);
244 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
245 lpwhr->lpszPath = fixurl;
249 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
251 LPWSTR requestString;
257 static const WCHAR szSpace[] = { ' ',0 };
258 static const WCHAR szcrlf[] = {'\r','\n', 0};
259 static const WCHAR szColon[] = { ':',' ',0 };
260 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
262 /* allocate space for an array of all the string pointers to be added */
263 len = (lpwhr->nCustHeaders)*4 + 10;
264 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
266 /* add the verb, path and HTTP version string */
274 /* Append custom request headers */
275 for (i = 0; i < lpwhr->nCustHeaders; i++)
277 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
280 req[n++] = lpwhr->pCustHeaders[i].lpszField;
282 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
284 TRACE("Adding custom header %s (%s)\n",
285 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
286 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
291 ERR("oops. buffer overrun\n");
294 requestString = HTTP_build_req( req, 4 );
295 HeapFree( GetProcessHeap(), 0, req );
298 * Set (header) termination string for request
299 * Make sure there's exactly two new lines at the end of the request
301 p = &requestString[strlenW(requestString)-1];
302 while ( (*p == '\n') || (*p == '\r') )
304 strcpyW( p+1, sztwocrlf );
306 return requestString;
309 static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr )
311 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
313 LPHTTPHEADERW setCookieHeader;
315 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
316 if (HeaderIndex == -1)
318 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
320 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
322 int nPosStart = 0, nPosEnd = 0, len;
323 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
325 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
327 LPWSTR buf_cookie, cookie_name, cookie_data;
329 LPWSTR domain = NULL;
333 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
334 setCookieHeader->lpszValue[nPosEnd] != '\0')
338 if (setCookieHeader->lpszValue[nPosEnd] == ';')
340 /* fixme: not case sensitive, strcasestr is gnu only */
341 int nDomainPosEnd = 0;
342 int nDomainPosStart = 0, nDomainLength = 0;
343 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
344 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
346 { /* they have specified their own domain, lets use it */
347 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
348 lpszDomain[nDomainPosEnd] != '\0')
352 nDomainPosStart = strlenW(szDomain);
353 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
354 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
355 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
358 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
359 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
360 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
361 TRACE("%s\n", debugstr_w(buf_cookie));
362 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
366 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
368 HeapFree(GetProcessHeap(), 0, buf_cookie);
372 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
373 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
374 cookie_data = &buf_cookie[nEqualPos + 1];
376 Host = HTTP_GetHeader(lpwhr,szHost);
377 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
378 strlenW(lpwhr->lpszPath) + 9;
379 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
380 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
381 InternetSetCookieW(buf_url, cookie_name, cookie_data);
383 HeapFree(GetProcessHeap(), 0, buf_url);
384 HeapFree(GetProcessHeap(), 0, buf_cookie);
385 HeapFree(GetProcessHeap(), 0, cookie_name);
386 HeapFree(GetProcessHeap(), 0, domain);
392 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
394 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
395 return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
396 ((pszAuthValue[ARRAYSIZE(szBasic)] != ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
399 static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
400 struct HttpAuthInfo **ppAuthInfo,
401 LPWSTR domain_and_username, LPWSTR password )
403 SECURITY_STATUS sec_status;
404 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
407 TRACE("%s\n", debugstr_w(pszAuthValue));
414 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
418 SecInvalidateHandle(&pAuthInfo->cred);
419 SecInvalidateHandle(&pAuthInfo->ctx);
420 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
422 pAuthInfo->auth_data = NULL;
423 pAuthInfo->auth_data_len = 0;
424 pAuthInfo->finished = FALSE;
426 if (is_basic_auth_value(pszAuthValue))
428 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
429 pAuthInfo->scheme = WININET_strdupW(szBasic);
430 if (!pAuthInfo->scheme)
432 HeapFree(GetProcessHeap(), 0, pAuthInfo);
439 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
441 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
442 if (!pAuthInfo->scheme)
444 HeapFree(GetProcessHeap(), 0, pAuthInfo);
448 if (domain_and_username)
450 WCHAR *user = strchrW(domain_and_username, '\\');
451 WCHAR *domain = domain_and_username;
453 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
455 pAuthData = &nt_auth_identity;
460 user = domain_and_username;
464 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
465 nt_auth_identity.User = user;
466 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
467 nt_auth_identity.Domain = domain;
468 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
469 nt_auth_identity.Password = password;
470 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
473 /* use default credentials */
476 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
477 SECPKG_CRED_OUTBOUND, NULL,
479 NULL, &pAuthInfo->cred,
481 if (sec_status == SEC_E_OK)
483 PSecPkgInfoW sec_pkg_info;
484 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
485 if (sec_status == SEC_E_OK)
487 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
488 FreeContextBuffer(sec_pkg_info);
491 if (sec_status != SEC_E_OK)
493 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
494 debugstr_w(pAuthInfo->scheme), sec_status);
495 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
496 HeapFree(GetProcessHeap(), 0, pAuthInfo);
500 *ppAuthInfo = pAuthInfo;
502 else if (pAuthInfo->finished)
505 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
506 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
508 ERR("authentication scheme changed from %s to %s\n",
509 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
513 if (is_basic_auth_value(pszAuthValue))
519 TRACE("basic authentication\n");
521 /* we don't cache credentials for basic authentication, so we can't
522 * retrieve them if the application didn't pass us any credentials */
523 if (!domain_and_username) return FALSE;
525 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
526 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
528 /* length includes a nul terminator, which will be re-used for the ':' */
529 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
533 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
534 auth_data[userlen] = ':';
535 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
537 pAuthInfo->auth_data = auth_data;
538 pAuthInfo->auth_data_len = userlen + 1 + passlen;
539 pAuthInfo->finished = TRUE;
546 SecBufferDesc out_desc, in_desc;
548 unsigned char *buffer;
549 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
550 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
552 in.BufferType = SECBUFFER_TOKEN;
556 in_desc.ulVersion = 0;
557 in_desc.cBuffers = 1;
558 in_desc.pBuffers = ∈
560 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
561 if (*pszAuthData == ' ')
564 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
565 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
566 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
569 buffer = HeapAlloc(GetProcessHeap(), 0, pAuthInfo->max_token);
571 out.BufferType = SECBUFFER_TOKEN;
572 out.cbBuffer = pAuthInfo->max_token;
573 out.pvBuffer = buffer;
575 out_desc.ulVersion = 0;
576 out_desc.cBuffers = 1;
577 out_desc.pBuffers = &out;
579 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
580 first ? NULL : &pAuthInfo->ctx,
581 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
582 context_req, 0, SECURITY_NETWORK_DREP,
583 in.pvBuffer ? &in_desc : NULL,
584 0, &pAuthInfo->ctx, &out_desc,
585 &pAuthInfo->attr, &pAuthInfo->exp);
586 if (sec_status == SEC_E_OK)
588 pAuthInfo->finished = TRUE;
589 pAuthInfo->auth_data = out.pvBuffer;
590 pAuthInfo->auth_data_len = out.cbBuffer;
591 TRACE("sending last auth packet\n");
593 else if (sec_status == SEC_I_CONTINUE_NEEDED)
595 pAuthInfo->auth_data = out.pvBuffer;
596 pAuthInfo->auth_data_len = out.cbBuffer;
597 TRACE("sending next auth packet\n");
601 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
602 pAuthInfo->finished = TRUE;
603 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
611 /***********************************************************************
612 * HTTP_HttpAddRequestHeadersW (internal)
614 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
615 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
620 BOOL bSuccess = FALSE;
623 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
625 if( dwHeaderLength == ~0U )
626 len = strlenW(lpszHeader);
628 len = dwHeaderLength;
629 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
630 lstrcpynW( buffer, lpszHeader, len + 1);
636 LPWSTR * pFieldAndValue;
640 while (*lpszEnd != '\0')
642 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
647 if (*lpszStart == '\0')
650 if (*lpszEnd == '\r')
653 lpszEnd += 2; /* Jump over \r\n */
655 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
656 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
659 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
661 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
662 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
663 HTTP_FreeTokens(pFieldAndValue);
669 HeapFree(GetProcessHeap(), 0, buffer);
674 /***********************************************************************
675 * HttpAddRequestHeadersW (WININET.@)
677 * Adds one or more HTTP header to the request handler
680 * On Windows if dwHeaderLength includes the trailing '\0', then
681 * HttpAddRequestHeadersW() adds it too. However this results in an
682 * invalid Http header which is rejected by some servers so we probably
683 * don't need to match Windows on that point.
690 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
691 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
693 BOOL bSuccess = FALSE;
694 LPWININETHTTPREQW lpwhr;
696 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
701 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
702 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
704 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
707 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
710 WININET_Release( &lpwhr->hdr );
715 /***********************************************************************
716 * HttpAddRequestHeadersA (WININET.@)
718 * Adds one or more HTTP header to the request handler
725 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
726 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
732 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
734 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
735 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
736 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
737 if( dwHeaderLength != ~0U )
738 dwHeaderLength = len;
740 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
742 HeapFree( GetProcessHeap(), 0, hdr );
747 /***********************************************************************
748 * HttpEndRequestA (WININET.@)
750 * Ends an HTTP request that was started by HttpSendRequestEx
757 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
758 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
760 LPINTERNET_BUFFERSA ptr;
761 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
764 TRACE("(%p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
769 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
770 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
772 lpBuffersOutW = NULL;
774 ptrW = lpBuffersOutW;
777 if (ptr->lpvBuffer && ptr->dwBufferLength)
778 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
779 ptrW->dwBufferLength = ptr->dwBufferLength;
780 ptrW->dwBufferTotal= ptr->dwBufferTotal;
783 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
784 sizeof(INTERNET_BUFFERSW));
790 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
794 ptrW = lpBuffersOutW;
797 LPINTERNET_BUFFERSW ptrW2;
799 FIXME("Do we need to translate info out of these buffer?\n");
801 HeapFree(GetProcessHeap(),0,ptrW->lpvBuffer);
803 HeapFree(GetProcessHeap(),0,ptrW);
811 /***********************************************************************
812 * HttpEndRequestW (WININET.@)
814 * Ends an HTTP request that was started by HttpSendRequestEx
821 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
822 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
825 LPWININETHTTPREQW lpwhr;
830 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
832 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
834 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
836 WININET_Release( &lpwhr->hdr );
840 lpwhr->hdr.dwFlags |= dwFlags;
841 lpwhr->hdr.dwContext = dwContext;
843 /* We appear to do nothing with lpBuffersOut.. is that correct? */
845 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
846 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
848 responseLen = HTTP_GetResponseHeaders(lpwhr);
852 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
853 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
855 /* process cookies here. Is this right? */
856 HTTP_ProcessCookies(lpwhr);
858 dwBufferSize = sizeof(lpwhr->dwContentLength);
859 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
860 &lpwhr->dwContentLength,&dwBufferSize,NULL))
861 lpwhr->dwContentLength = -1;
863 if (lpwhr->dwContentLength == 0)
864 HTTP_FinishedReading(lpwhr);
866 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
868 DWORD dwCode,dwCodeLength=sizeof(DWORD);
869 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
870 (dwCode==302 || dwCode==301))
872 WCHAR szNewLocation[2048];
873 dwBufferSize=sizeof(szNewLocation);
874 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
876 /* redirects are always GETs */
877 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
878 lpwhr->lpszVerb = WININET_strdupW(szGET);
879 HTTP_DrainContent(lpwhr);
880 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
882 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
887 WININET_Release( &lpwhr->hdr );
888 TRACE("%i <--\n",rc);
892 /***********************************************************************
893 * HttpOpenRequestW (WININET.@)
895 * Open a HTTP request handle
898 * HINTERNET a HTTP request handle on success
902 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
903 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
904 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
905 DWORD dwFlags, DWORD_PTR dwContext)
907 LPWININETHTTPSESSIONW lpwhs;
908 HINTERNET handle = NULL;
910 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
911 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
912 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
914 if(lpszAcceptTypes!=NULL)
917 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
918 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
921 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
922 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
924 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
929 * My tests seem to show that the windows version does not
930 * become asynchronous until after this point. And anyhow
931 * if this call was asynchronous then how would you get the
932 * necessary HINTERNET pointer returned by this function.
935 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
936 lpszVersion, lpszReferrer, lpszAcceptTypes,
940 WININET_Release( &lpwhs->hdr );
941 TRACE("returning %p\n", handle);
946 /***********************************************************************
947 * HttpOpenRequestA (WININET.@)
949 * Open a HTTP request handle
952 * HINTERNET a HTTP request handle on success
956 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
957 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
958 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
959 DWORD dwFlags, DWORD_PTR dwContext)
961 LPWSTR szVerb = NULL, szObjectName = NULL;
962 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
964 INT acceptTypesCount;
965 HINTERNET rc = FALSE;
966 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
967 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
968 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
973 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
974 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
977 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
982 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
983 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
986 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
991 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
992 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
995 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
1000 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
1001 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1004 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
1007 acceptTypesCount = 0;
1008 if (lpszAcceptTypes)
1010 /* find out how many there are */
1011 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
1013 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
1014 acceptTypesCount = 0;
1015 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
1017 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1019 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1020 if (!szAcceptTypes[acceptTypesCount] )
1022 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1023 -1, szAcceptTypes[acceptTypesCount], len );
1026 szAcceptTypes[acceptTypesCount] = NULL;
1028 else szAcceptTypes = 0;
1030 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1031 szVersion, szReferrer,
1032 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1037 acceptTypesCount = 0;
1038 while (szAcceptTypes[acceptTypesCount])
1040 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1043 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1045 HeapFree(GetProcessHeap(), 0, szReferrer);
1046 HeapFree(GetProcessHeap(), 0, szVersion);
1047 HeapFree(GetProcessHeap(), 0, szObjectName);
1048 HeapFree(GetProcessHeap(), 0, szVerb);
1053 /***********************************************************************
1056 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1059 static const CHAR HTTP_Base64Enc[] =
1060 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1064 /* first 6 bits, all from bin[0] */
1065 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1066 x = (bin[0] & 3) << 4;
1068 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1071 base64[n++] = HTTP_Base64Enc[x];
1076 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1077 x = ( bin[1] & 0x0f ) << 2;
1079 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1082 base64[n++] = HTTP_Base64Enc[x];
1086 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1088 /* last 6 bits, all from bin [2] */
1089 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1097 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1098 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1099 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1100 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1101 static const signed char HTTP_Base64Dec[256] =
1103 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1104 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1105 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1106 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1107 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1108 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1109 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1110 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1111 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1112 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1113 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1114 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1115 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1116 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1117 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1118 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1119 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1120 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1121 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1122 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1123 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1124 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1125 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1126 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1127 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1128 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1132 /***********************************************************************
1135 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1143 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1144 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1145 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1146 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1148 WARN("invalid base64: %s\n", debugstr_w(base64));
1152 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1155 if ((base64[2] == '=') && (base64[3] == '='))
1157 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1158 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1160 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1164 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1167 if (base64[3] == '=')
1169 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1170 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1172 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1176 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1185 /***********************************************************************
1186 * HTTP_InsertAuthorizationForHeader
1188 * Insert or delete the authorization field in the request header.
1190 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1194 static const WCHAR wszSpace[] = {' ',0};
1195 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1197 WCHAR *authorization = NULL;
1199 if (pAuthInfo->auth_data_len)
1201 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1202 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1203 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1207 strcpyW(authorization, pAuthInfo->scheme);
1208 strcatW(authorization, wszSpace);
1209 HTTP_EncodeBase64(pAuthInfo->auth_data,
1210 pAuthInfo->auth_data_len,
1211 authorization+strlenW(authorization));
1213 /* clear the data as it isn't valid now that it has been sent to the
1214 * server, unless it's Basic authentication which doesn't do
1215 * connection tracking */
1216 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1218 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1219 pAuthInfo->auth_data = NULL;
1220 pAuthInfo->auth_data_len = 0;
1224 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1226 HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1228 HeapFree(GetProcessHeap(), 0, authorization);
1233 /***********************************************************************
1234 * HTTP_DealWithProxy
1236 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1237 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1239 WCHAR buf[MAXHOSTNAME];
1240 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1242 static WCHAR szNul[] = { 0 };
1243 URL_COMPONENTSW UrlComponents;
1244 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
1245 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
1246 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1249 memset( &UrlComponents, 0, sizeof UrlComponents );
1250 UrlComponents.dwStructSize = sizeof UrlComponents;
1251 UrlComponents.lpszHostName = buf;
1252 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1254 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1255 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1256 sprintfW(proxy, szFormat1, hIC->lpszProxy);
1258 strcpyW(proxy, hIC->lpszProxy);
1259 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1261 if( UrlComponents.dwHostNameLength == 0 )
1264 if( !lpwhr->lpszPath )
1265 lpwhr->lpszPath = szNul;
1266 TRACE("server=%s path=%s\n",
1267 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
1268 /* for constant 15 see above */
1269 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
1270 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1272 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1273 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1275 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
1277 if( lpwhr->lpszPath[0] != '/' )
1278 strcatW( url, szSlash );
1279 strcatW(url, lpwhr->lpszPath);
1280 if(lpwhr->lpszPath != szNul)
1281 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1282 lpwhr->lpszPath = url;
1284 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1285 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1286 lpwhs->nServerPort = UrlComponents.nPort;
1291 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1294 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1296 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1297 INTERNET_STATUS_RESOLVING_NAME,
1298 lpwhs->lpszServerName,
1299 strlenW(lpwhs->lpszServerName)+1);
1301 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1302 &lpwhs->socketAddress))
1304 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1308 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1309 szaddr, sizeof(szaddr));
1310 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1311 INTERNET_STATUS_NAME_RESOLVED,
1312 szaddr, strlen(szaddr)+1);
1317 /***********************************************************************
1318 * HTTPREQ_Destroy (internal)
1320 * Deallocate request handle
1323 static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1325 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1330 if(lpwhr->hCacheFile)
1331 CloseHandle(lpwhr->hCacheFile);
1333 if(lpwhr->lpszCacheFile) {
1334 DeleteFileW(lpwhr->lpszCacheFile); /* FIXME */
1335 HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1338 WININET_Release(&lpwhr->lpHttpSession->hdr);
1340 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1341 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1342 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1343 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1344 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1346 for (i = 0; i < lpwhr->nCustHeaders; i++)
1348 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1349 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1352 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1353 HeapFree(GetProcessHeap(), 0, lpwhr);
1356 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1358 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1359 LPWININETHTTPSESSIONW lpwhs = NULL;
1360 LPWININETAPPINFOW hIC = NULL;
1362 TRACE("%p\n",lpwhr);
1364 if (!NETCON_connected(&lpwhr->netConnection))
1367 if (lpwhr->pAuthInfo)
1369 if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1370 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1371 if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1372 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1374 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1375 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1376 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1377 lpwhr->pAuthInfo = NULL;
1379 if (lpwhr->pProxyAuthInfo)
1381 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1382 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1383 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1384 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1386 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1387 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1388 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1389 lpwhr->pProxyAuthInfo = NULL;
1392 lpwhs = lpwhr->lpHttpSession;
1393 hIC = lpwhs->lpAppInfo;
1395 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1396 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1398 NETCON_close(&lpwhr->netConnection);
1400 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1401 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1404 static DWORD HTTPREQ_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1406 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1409 case INTERNET_OPTION_HANDLE_TYPE:
1410 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1412 if (*size < sizeof(ULONG))
1413 return ERROR_INSUFFICIENT_BUFFER;
1415 *size = sizeof(DWORD);
1416 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1417 return ERROR_SUCCESS;
1419 case INTERNET_OPTION_URL: {
1420 WCHAR url[INTERNET_MAX_URL_LENGTH];
1424 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
1425 static const WCHAR hostW[] = {'H','o','s','t',0};
1427 TRACE("INTERNET_OPTION_URL\n");
1429 host = HTTP_GetHeader(req, hostW);
1430 sprintfW(url, formatW, host->lpszValue, req->lpszPath);
1431 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1434 len = (strlenW(url)+1) * sizeof(WCHAR);
1436 return ERROR_INSUFFICIENT_BUFFER;
1439 strcpyW(buffer, url);
1440 return ERROR_SUCCESS;
1442 len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1444 return ERROR_INSUFFICIENT_BUFFER;
1447 return ERROR_SUCCESS;
1451 case INTERNET_OPTION_DATAFILE_NAME: {
1454 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1456 if(!req->lpszCacheFile) {
1458 return ERROR_INTERNET_ITEM_NOT_FOUND;
1462 req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1463 if(*size < req_size)
1464 return ERROR_INSUFFICIENT_BUFFER;
1467 memcpy(buffer, req->lpszCacheFile, *size);
1468 return ERROR_SUCCESS;
1470 req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1471 if (req_size > *size)
1472 return ERROR_INSUFFICIENT_BUFFER;
1474 *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1475 -1, buffer, *size, NULL, NULL);
1476 return ERROR_SUCCESS;
1480 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1481 PCCERT_CONTEXT context;
1483 if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1484 *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1485 return ERROR_INSUFFICIENT_BUFFER;
1488 context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1490 INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1493 memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1494 info->ftExpiry = context->pCertInfo->NotAfter;
1495 info->ftStart = context->pCertInfo->NotBefore;
1497 len = CertNameToStrW(context->dwCertEncodingType,
1498 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1499 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1500 if(info->lpszSubjectInfo)
1501 CertNameToStrW(context->dwCertEncodingType,
1502 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1503 info->lpszSubjectInfo, len);
1504 len = CertNameToStrW(context->dwCertEncodingType,
1505 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1506 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1507 if (info->lpszIssuerInfo)
1508 CertNameToStrW(context->dwCertEncodingType,
1509 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1510 info->lpszIssuerInfo, len);
1512 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1514 len = CertNameToStrA(context->dwCertEncodingType,
1515 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1516 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1517 if(infoA->lpszSubjectInfo)
1518 CertNameToStrA(context->dwCertEncodingType,
1519 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1520 infoA->lpszSubjectInfo, len);
1521 len = CertNameToStrA(context->dwCertEncodingType,
1522 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1523 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1524 if(infoA->lpszIssuerInfo)
1525 CertNameToStrA(context->dwCertEncodingType,
1526 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1527 infoA->lpszIssuerInfo, len);
1531 * Contrary to MSDN, these do not appear to be set.
1533 * lpszSignatureAlgName
1534 * lpszEncryptionAlgName
1537 CertFreeCertificateContext(context);
1538 return ERROR_SUCCESS;
1543 FIXME("Not implemented option %d\n", option);
1544 return ERROR_INTERNET_INVALID_OPTION;
1547 static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1549 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1552 case INTERNET_OPTION_SEND_TIMEOUT:
1553 case INTERNET_OPTION_RECEIVE_TIMEOUT:
1554 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1556 if (size != sizeof(DWORD))
1557 return ERROR_INVALID_PARAMETER;
1559 return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1563 return ERROR_INTERNET_INVALID_OPTION;
1566 static DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1570 if(!NETCON_recv(&req->netConnection, buffer, min(size, req->dwContentLength - req->dwContentRead),
1571 sync ? MSG_WAITALL : 0, &bytes_read)) {
1572 if(req->dwContentLength != -1 && req->dwContentRead != req->dwContentLength)
1573 ERR("not all data received %d/%d\n", req->dwContentRead, req->dwContentLength);
1575 /* always returns TRUE, even if the network layer returns an
1578 HTTP_FinishedReading(req);
1579 return ERROR_SUCCESS;
1582 req->dwContentRead += bytes_read;
1585 if(req->lpszCacheFile) {
1588 res = WriteFile(req->hCacheFile, buffer, bytes_read, NULL, NULL);
1590 WARN("WriteFile failed: %u\n", GetLastError());
1593 if(!bytes_read && (req->dwContentRead == req->dwContentLength))
1594 HTTP_FinishedReading(req);
1596 return ERROR_SUCCESS;
1599 static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1601 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1603 return HTTPREQ_Read(req, buffer, size, read, TRUE);
1606 static void HTTPREQ_AsyncReadFileExProc(WORKREQUEST *workRequest)
1608 struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
1609 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1610 INTERNET_ASYNC_RESULT iar;
1613 TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
1615 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1616 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1618 iar.dwResult = res == ERROR_SUCCESS;
1621 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1622 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1623 sizeof(INTERNET_ASYNC_RESULT));
1626 static DWORD HTTPREQ_ReadFileExA(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSA *buffers,
1627 DWORD flags, DWORD_PTR context)
1630 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1633 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1634 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1636 if (buffers->dwStructSize != sizeof(*buffers))
1637 return ERROR_INVALID_PARAMETER;
1639 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1641 /* FIXME: IRF_ASYNC may not be the right thing to test here;
1642 * hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC is probably better */
1643 if (flags & IRF_ASYNC) {
1644 DWORD available = 0;
1646 NETCON_query_data_available(&req->netConnection, &available);
1649 WORKREQUEST workRequest;
1651 workRequest.asyncproc = HTTPREQ_AsyncReadFileExProc;
1652 workRequest.hdr = WININET_AddRef(&req->hdr);
1653 workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
1655 INTERNET_AsyncCall(&workRequest);
1657 return ERROR_IO_PENDING;
1661 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1662 !(flags & IRF_NO_WAIT));
1664 if (res == ERROR_SUCCESS) {
1665 DWORD size = buffers->dwBufferLength;
1666 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1667 &size, sizeof(size));
1673 static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1675 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
1677 return NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written);
1680 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1682 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1683 INTERNET_ASYNC_RESULT iar;
1686 TRACE("%p\n", workRequest->hdr);
1688 iar.dwResult = NETCON_recv(&req->netConnection, buffer,
1689 min(sizeof(buffer), req->dwContentLength - req->dwContentRead),
1690 MSG_PEEK, (int *)&iar.dwError);
1692 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1693 sizeof(INTERNET_ASYNC_RESULT));
1696 static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1698 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1702 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
1704 if(!NETCON_query_data_available(&req->netConnection, available) || *available)
1705 return ERROR_SUCCESS;
1707 /* Even if we are in async mode, we need to determine whether
1708 * there is actually more data available. We do this by trying
1709 * to peek only a single byte in async mode. */
1710 async = (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) != 0;
1712 if (NETCON_recv(&req->netConnection, buffer,
1713 min(async ? 1 : sizeof(buffer), req->dwContentLength - req->dwContentRead),
1714 MSG_PEEK, (int *)available) && async && *available)
1716 WORKREQUEST workRequest;
1719 workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
1720 workRequest.hdr = WININET_AddRef( &req->hdr );
1722 INTERNET_AsyncCall(&workRequest);
1724 return ERROR_IO_PENDING;
1727 return ERROR_SUCCESS;
1730 static const HANDLEHEADERVtbl HTTPREQVtbl = {
1732 HTTPREQ_CloseConnection,
1733 HTTPREQ_QueryOption,
1736 HTTPREQ_ReadFileExA,
1738 HTTPREQ_QueryDataAvailable,
1742 /***********************************************************************
1743 * HTTP_HttpOpenRequestW (internal)
1745 * Open a HTTP request handle
1748 * HINTERNET a HTTP request handle on success
1752 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1753 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1754 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1755 DWORD dwFlags, DWORD_PTR dwContext)
1757 LPWININETAPPINFOW hIC = NULL;
1758 LPWININETHTTPREQW lpwhr;
1760 LPWSTR lpszUrl = NULL;
1762 HINTERNET handle = NULL;
1763 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
1769 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1770 hIC = lpwhs->lpAppInfo;
1772 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1775 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1778 lpwhr->hdr.htype = WH_HHTTPREQ;
1779 lpwhr->hdr.vtbl = &HTTPREQVtbl;
1780 lpwhr->hdr.dwFlags = dwFlags;
1781 lpwhr->hdr.dwContext = dwContext;
1782 lpwhr->hdr.refs = 1;
1783 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1784 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
1786 WININET_AddRef( &lpwhs->hdr );
1787 lpwhr->lpHttpSession = lpwhs;
1788 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
1790 handle = WININET_AllocHandle( &lpwhr->hdr );
1793 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1797 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1799 InternetCloseHandle( handle );
1804 if (lpszObjectName && *lpszObjectName) {
1808 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1809 if (rc != E_POINTER)
1810 len = strlenW(lpszObjectName)+1;
1811 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1812 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1813 URL_ESCAPE_SPACES_ONLY);
1816 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
1817 strcpyW(lpwhr->lpszPath,lpszObjectName);
1821 if (lpszReferrer && *lpszReferrer)
1822 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1824 if (lpszAcceptTypes)
1827 for (i = 0; lpszAcceptTypes[i]; i++)
1829 if (!*lpszAcceptTypes[i]) continue;
1830 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
1831 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
1832 HTTP_ADDHDR_FLAG_REQ |
1833 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
1837 lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
1840 lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
1842 lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
1844 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
1846 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1847 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1848 INTERNET_DEFAULT_HTTPS_PORT :
1849 INTERNET_DEFAULT_HTTP_PORT);
1850 lpwhs->nHostPort = lpwhs->nServerPort;
1852 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1853 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1857 WCHAR *agent_header;
1858 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1860 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1861 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1862 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1864 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1865 HTTP_ADDREQ_FLAG_ADD);
1866 HeapFree(GetProcessHeap(), 0, agent_header);
1869 Host = HTTP_GetHeader(lpwhr,szHost);
1871 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1872 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1873 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1875 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1876 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1879 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1880 static const WCHAR szcrlf[] = {'\r','\n',0};
1882 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1884 cnt += sprintfW(lpszCookies, szCookie);
1885 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1886 strcatW(lpszCookies, szcrlf);
1888 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1889 HTTP_ADDREQ_FLAG_ADD);
1890 HeapFree(GetProcessHeap(), 0, lpszCookies);
1892 HeapFree(GetProcessHeap(), 0, lpszUrl);
1895 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1896 INTERNET_STATUS_HANDLE_CREATED, &handle,
1900 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1903 if (!HTTP_ResolveName(lpwhr))
1905 InternetCloseHandle( handle );
1911 WININET_Release( &lpwhr->hdr );
1913 TRACE("<-- %p (%p)\n", handle, lpwhr);
1917 /* read any content returned by the server so that the connection can be
1919 static void HTTP_DrainContent(WININETHTTPREQW *req)
1923 if (!NETCON_connected(&req->netConnection)) return;
1925 if (req->dwContentLength == -1)
1926 NETCON_close(&req->netConnection);
1931 if (HTTPREQ_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
1933 } while (bytes_read);
1936 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1937 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1938 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1939 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1940 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1941 static const WCHAR szAge[] = { 'A','g','e',0 };
1942 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1943 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1944 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1945 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1946 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1947 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1948 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1949 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1950 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1951 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1952 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1953 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 };
1954 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1955 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1956 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1957 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1958 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1959 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1960 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1961 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1962 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1963 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1964 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1965 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1966 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1967 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1968 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1969 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1970 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1971 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1972 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1973 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1974 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1975 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1976 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1977 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1978 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1979 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1980 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 };
1981 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1982 static const WCHAR szURI[] = { 'U','R','I',0 };
1983 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1984 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1985 static const WCHAR szVia[] = { 'V','i','a',0 };
1986 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1987 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1989 static const LPCWSTR header_lookup[] = {
1990 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
1991 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1992 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1993 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
1994 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1995 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1996 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1997 szAllow, /* HTTP_QUERY_ALLOW = 7 */
1998 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
1999 szDate, /* HTTP_QUERY_DATE = 9 */
2000 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
2001 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2002 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
2003 szURI, /* HTTP_QUERY_URI = 13 */
2004 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
2005 NULL, /* HTTP_QUERY_COST = 15 */
2006 NULL, /* HTTP_QUERY_LINK = 16 */
2007 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
2008 NULL, /* HTTP_QUERY_VERSION = 18 */
2009 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
2010 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
2011 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
2012 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2013 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
2014 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
2015 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2016 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2017 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2018 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
2019 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2020 NULL, /* HTTP_QUERY_FORWARDED = 30 */
2021 NULL, /* HTTP_QUERY_FROM = 31 */
2022 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2023 szLocation, /* HTTP_QUERY_LOCATION = 33 */
2024 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
2025 szReferer, /* HTTP_QUERY_REFERER = 35 */
2026 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
2027 szServer, /* HTTP_QUERY_SERVER = 37 */
2028 NULL, /* HTTP_TITLE = 38 */
2029 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
2030 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2031 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2032 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2033 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
2034 szCookie, /* HTTP_QUERY_COOKIE = 44 */
2035 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2036 NULL, /* HTTP_QUERY_REFRESH = 46 */
2037 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2038 szAge, /* HTTP_QUERY_AGE = 48 */
2039 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2040 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
2041 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2042 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2043 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2044 szETag, /* HTTP_QUERY_ETAG = 54 */
2045 szHost, /* HTTP_QUERY_HOST = 55 */
2046 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
2047 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2048 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
2049 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2050 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2051 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2052 szRange, /* HTTP_QUERY_RANGE = 62 */
2053 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2054 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
2055 szVary, /* HTTP_QUERY_VARY = 65 */
2056 szVia, /* HTTP_QUERY_VIA = 66 */
2057 szWarning, /* HTTP_QUERY_WARNING = 67 */
2058 szExpect, /* HTTP_QUERY_EXPECT = 68 */
2059 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2060 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2063 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2065 /***********************************************************************
2066 * HTTP_HttpQueryInfoW (internal)
2068 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
2069 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2071 LPHTTPHEADERW lphttpHdr = NULL;
2072 BOOL bSuccess = FALSE;
2073 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
2074 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2075 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2078 /* Find requested header structure */
2081 case HTTP_QUERY_CUSTOM:
2082 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2085 case HTTP_QUERY_RAW_HEADERS_CRLF:
2092 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
2094 headers = lpwhr->lpszRawHeaders;
2096 len = strlenW(headers);
2097 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2099 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2100 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2104 memcpy(lpBuffer, headers, (len+1)*sizeof(WCHAR));
2105 *lpdwBufferLength = len * sizeof(WCHAR);
2107 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2112 HeapFree(GetProcessHeap(), 0, headers);
2115 case HTTP_QUERY_RAW_HEADERS:
2117 static const WCHAR szCrLf[] = {'\r','\n',0};
2118 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
2120 LPWSTR pszString = (WCHAR*)lpBuffer;
2122 for (i = 0; ppszRawHeaderLines[i]; i++)
2123 size += strlenW(ppszRawHeaderLines[i]) + 1;
2125 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2127 HTTP_FreeTokens(ppszRawHeaderLines);
2128 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2129 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2133 for (i = 0; ppszRawHeaderLines[i]; i++)
2135 DWORD len = strlenW(ppszRawHeaderLines[i]);
2136 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2141 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
2143 *lpdwBufferLength = size * sizeof(WCHAR);
2144 HTTP_FreeTokens(ppszRawHeaderLines);
2148 case HTTP_QUERY_STATUS_TEXT:
2149 if (lpwhr->lpszStatusText)
2151 DWORD len = strlenW(lpwhr->lpszStatusText);
2152 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2154 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2155 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2158 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
2159 *lpdwBufferLength = len * sizeof(WCHAR);
2161 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2166 case HTTP_QUERY_VERSION:
2167 if (lpwhr->lpszVersion)
2169 DWORD len = strlenW(lpwhr->lpszVersion);
2170 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2172 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2173 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2176 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
2177 *lpdwBufferLength = len * sizeof(WCHAR);
2179 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
2185 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2187 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
2188 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2189 requested_index,request_only);
2193 lphttpHdr = &lpwhr->pCustHeaders[index];
2195 /* Ensure header satisfies requested attributes */
2197 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2198 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
2200 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2207 /* coalesce value to requested type */
2208 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
2210 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2213 TRACE(" returning number : %d\n", *(int *)lpBuffer);
2215 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
2221 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2223 tmpTM = *gmtime(&tmpTime);
2224 STHook = (SYSTEMTIME *) lpBuffer;
2228 STHook->wDay = tmpTM.tm_mday;
2229 STHook->wHour = tmpTM.tm_hour;
2230 STHook->wMilliseconds = 0;
2231 STHook->wMinute = tmpTM.tm_min;
2232 STHook->wDayOfWeek = tmpTM.tm_wday;
2233 STHook->wMonth = tmpTM.tm_mon + 1;
2234 STHook->wSecond = tmpTM.tm_sec;
2235 STHook->wYear = tmpTM.tm_year;
2239 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2240 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2241 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2243 else if (lphttpHdr->lpszValue)
2245 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
2247 if (len > *lpdwBufferLength)
2249 *lpdwBufferLength = len;
2250 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2254 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2255 *lpdwBufferLength = len - sizeof(WCHAR);
2258 TRACE(" returning string : %s\n", debugstr_w(lpBuffer));
2263 /***********************************************************************
2264 * HttpQueryInfoW (WININET.@)
2266 * Queries for information about an HTTP request
2273 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2274 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2276 BOOL bSuccess = FALSE;
2277 LPWININETHTTPREQW lpwhr;
2279 if (TRACE_ON(wininet)) {
2280 #define FE(x) { x, #x }
2281 static const wininet_flag_info query_flags[] = {
2282 FE(HTTP_QUERY_MIME_VERSION),
2283 FE(HTTP_QUERY_CONTENT_TYPE),
2284 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2285 FE(HTTP_QUERY_CONTENT_ID),
2286 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2287 FE(HTTP_QUERY_CONTENT_LENGTH),
2288 FE(HTTP_QUERY_CONTENT_LANGUAGE),
2289 FE(HTTP_QUERY_ALLOW),
2290 FE(HTTP_QUERY_PUBLIC),
2291 FE(HTTP_QUERY_DATE),
2292 FE(HTTP_QUERY_EXPIRES),
2293 FE(HTTP_QUERY_LAST_MODIFIED),
2294 FE(HTTP_QUERY_MESSAGE_ID),
2296 FE(HTTP_QUERY_DERIVED_FROM),
2297 FE(HTTP_QUERY_COST),
2298 FE(HTTP_QUERY_LINK),
2299 FE(HTTP_QUERY_PRAGMA),
2300 FE(HTTP_QUERY_VERSION),
2301 FE(HTTP_QUERY_STATUS_CODE),
2302 FE(HTTP_QUERY_STATUS_TEXT),
2303 FE(HTTP_QUERY_RAW_HEADERS),
2304 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2305 FE(HTTP_QUERY_CONNECTION),
2306 FE(HTTP_QUERY_ACCEPT),
2307 FE(HTTP_QUERY_ACCEPT_CHARSET),
2308 FE(HTTP_QUERY_ACCEPT_ENCODING),
2309 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2310 FE(HTTP_QUERY_AUTHORIZATION),
2311 FE(HTTP_QUERY_CONTENT_ENCODING),
2312 FE(HTTP_QUERY_FORWARDED),
2313 FE(HTTP_QUERY_FROM),
2314 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2315 FE(HTTP_QUERY_LOCATION),
2316 FE(HTTP_QUERY_ORIG_URI),
2317 FE(HTTP_QUERY_REFERER),
2318 FE(HTTP_QUERY_RETRY_AFTER),
2319 FE(HTTP_QUERY_SERVER),
2320 FE(HTTP_QUERY_TITLE),
2321 FE(HTTP_QUERY_USER_AGENT),
2322 FE(HTTP_QUERY_WWW_AUTHENTICATE),
2323 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2324 FE(HTTP_QUERY_ACCEPT_RANGES),
2325 FE(HTTP_QUERY_SET_COOKIE),
2326 FE(HTTP_QUERY_COOKIE),
2327 FE(HTTP_QUERY_REQUEST_METHOD),
2328 FE(HTTP_QUERY_REFRESH),
2329 FE(HTTP_QUERY_CONTENT_DISPOSITION),
2331 FE(HTTP_QUERY_CACHE_CONTROL),
2332 FE(HTTP_QUERY_CONTENT_BASE),
2333 FE(HTTP_QUERY_CONTENT_LOCATION),
2334 FE(HTTP_QUERY_CONTENT_MD5),
2335 FE(HTTP_QUERY_CONTENT_RANGE),
2336 FE(HTTP_QUERY_ETAG),
2337 FE(HTTP_QUERY_HOST),
2338 FE(HTTP_QUERY_IF_MATCH),
2339 FE(HTTP_QUERY_IF_NONE_MATCH),
2340 FE(HTTP_QUERY_IF_RANGE),
2341 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2342 FE(HTTP_QUERY_MAX_FORWARDS),
2343 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2344 FE(HTTP_QUERY_RANGE),
2345 FE(HTTP_QUERY_TRANSFER_ENCODING),
2346 FE(HTTP_QUERY_UPGRADE),
2347 FE(HTTP_QUERY_VARY),
2349 FE(HTTP_QUERY_WARNING),
2350 FE(HTTP_QUERY_CUSTOM)
2352 static const wininet_flag_info modifier_flags[] = {
2353 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2354 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2355 FE(HTTP_QUERY_FLAG_NUMBER),
2356 FE(HTTP_QUERY_FLAG_COALESCE)
2359 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2360 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
2363 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
2364 TRACE(" Attribute:");
2365 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2366 if (query_flags[i].val == info) {
2367 TRACE(" %s", query_flags[i].name);
2371 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
2372 TRACE(" Unknown (%08x)", info);
2375 TRACE(" Modifier:");
2376 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2377 if (modifier_flags[i].val & info_mod) {
2378 TRACE(" %s", modifier_flags[i].name);
2379 info_mod &= ~ modifier_flags[i].val;
2384 TRACE(" Unknown (%08x)", info_mod);
2389 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2390 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2392 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2396 if (lpBuffer == NULL)
2397 *lpdwBufferLength = 0;
2398 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2399 lpBuffer, lpdwBufferLength, lpdwIndex);
2403 WININET_Release( &lpwhr->hdr );
2405 TRACE("%d <--\n", bSuccess);
2409 /***********************************************************************
2410 * HttpQueryInfoA (WININET.@)
2412 * Queries for information about an HTTP request
2419 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2420 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2426 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2427 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2429 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2430 lpdwBufferLength, lpdwIndex );
2436 len = (*lpdwBufferLength)*sizeof(WCHAR);
2437 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2439 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2445 bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
2446 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2447 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2448 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
2455 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2459 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
2460 lpBuffer, *lpdwBufferLength, NULL, NULL );
2461 *lpdwBufferLength = len - 1;
2463 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
2466 /* since the strings being returned from HttpQueryInfoW should be
2467 * only ASCII characters, it is reasonable to assume that all of
2468 * the Unicode characters can be reduced to a single byte */
2469 *lpdwBufferLength = len / sizeof(WCHAR);
2471 HeapFree(GetProcessHeap(), 0, bufferW );
2476 /***********************************************************************
2477 * HttpSendRequestExA (WININET.@)
2479 * Sends the specified request to the HTTP server and allows chunked
2484 * Failure: FALSE, call GetLastError() for more information.
2486 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2487 LPINTERNET_BUFFERSA lpBuffersIn,
2488 LPINTERNET_BUFFERSA lpBuffersOut,
2489 DWORD dwFlags, DWORD_PTR dwContext)
2491 INTERNET_BUFFERSW BuffersInW;
2494 LPWSTR header = NULL;
2496 TRACE("(%p, %p, %p, %08x, %08lx): stub\n", hRequest, lpBuffersIn,
2497 lpBuffersOut, dwFlags, dwContext);
2501 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2502 if (lpBuffersIn->lpcszHeader)
2504 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2505 lpBuffersIn->dwHeadersLength,0,0);
2506 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2507 if (!(BuffersInW.lpcszHeader = header))
2509 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2512 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2513 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2517 BuffersInW.lpcszHeader = NULL;
2518 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2519 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2520 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2521 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2522 BuffersInW.Next = NULL;
2525 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2527 HeapFree(GetProcessHeap(),0,header);
2532 /***********************************************************************
2533 * HttpSendRequestExW (WININET.@)
2535 * Sends the specified request to the HTTP server and allows chunked
2540 * Failure: FALSE, call GetLastError() for more information.
2542 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2543 LPINTERNET_BUFFERSW lpBuffersIn,
2544 LPINTERNET_BUFFERSW lpBuffersOut,
2545 DWORD dwFlags, DWORD_PTR dwContext)
2548 LPWININETHTTPREQW lpwhr;
2549 LPWININETHTTPSESSIONW lpwhs;
2550 LPWININETAPPINFOW hIC;
2552 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2553 lpBuffersOut, dwFlags, dwContext);
2555 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2557 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2559 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2563 lpwhs = lpwhr->lpHttpSession;
2564 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
2565 hIC = lpwhs->lpAppInfo;
2566 assert(hIC->hdr.htype == WH_HINIT);
2568 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2570 WORKREQUEST workRequest;
2571 struct WORKREQ_HTTPSENDREQUESTW *req;
2573 workRequest.asyncproc = AsyncHttpSendRequestProc;
2574 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2575 req = &workRequest.u.HttpSendRequestW;
2578 if (lpBuffersIn->lpcszHeader)
2579 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2580 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2582 req->lpszHeader = NULL;
2583 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2584 req->lpOptional = lpBuffersIn->lpvBuffer;
2585 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2586 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2590 req->lpszHeader = NULL;
2591 req->dwHeaderLength = 0;
2592 req->lpOptional = NULL;
2593 req->dwOptionalLength = 0;
2594 req->dwContentLength = 0;
2597 req->bEndRequest = FALSE;
2599 INTERNET_AsyncCall(&workRequest);
2601 * This is from windows.
2603 INTERNET_SetLastError(ERROR_IO_PENDING);
2608 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2609 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2610 lpBuffersIn->dwBufferTotal, FALSE);
2612 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
2617 WININET_Release( &lpwhr->hdr );
2623 /***********************************************************************
2624 * HttpSendRequestW (WININET.@)
2626 * Sends the specified request to the HTTP server
2633 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
2634 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2636 LPWININETHTTPREQW lpwhr;
2637 LPWININETHTTPSESSIONW lpwhs = NULL;
2638 LPWININETAPPINFOW hIC = NULL;
2641 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2642 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
2644 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2645 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2647 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2652 lpwhs = lpwhr->lpHttpSession;
2653 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2655 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2660 hIC = lpwhs->lpAppInfo;
2661 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2663 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2668 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2670 WORKREQUEST workRequest;
2671 struct WORKREQ_HTTPSENDREQUESTW *req;
2673 workRequest.asyncproc = AsyncHttpSendRequestProc;
2674 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2675 req = &workRequest.u.HttpSendRequestW;
2678 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, dwHeaderLength * sizeof(WCHAR));
2679 memcpy(req->lpszHeader, lpszHeaders, dwHeaderLength * sizeof(WCHAR));
2682 req->lpszHeader = 0;
2683 req->dwHeaderLength = dwHeaderLength;
2684 req->lpOptional = lpOptional;
2685 req->dwOptionalLength = dwOptionalLength;
2686 req->dwContentLength = dwOptionalLength;
2687 req->bEndRequest = TRUE;
2689 INTERNET_AsyncCall(&workRequest);
2691 * This is from windows.
2693 INTERNET_SetLastError(ERROR_IO_PENDING);
2698 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
2699 dwHeaderLength, lpOptional, dwOptionalLength,
2700 dwOptionalLength, TRUE);
2704 WININET_Release( &lpwhr->hdr );
2708 /***********************************************************************
2709 * HttpSendRequestA (WININET.@)
2711 * Sends the specified request to the HTTP server
2718 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
2719 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2722 LPWSTR szHeaders=NULL;
2723 DWORD nLen=dwHeaderLength;
2724 if(lpszHeaders!=NULL)
2726 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2727 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2728 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
2730 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
2731 HeapFree(GetProcessHeap(),0,szHeaders);
2735 static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
2737 LPHTTPHEADERW host_header;
2739 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2741 host_header = HTTP_GetHeader(req, szHost);
2745 sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
2749 /***********************************************************************
2750 * HTTP_HandleRedirect (internal)
2752 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
2754 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2755 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
2760 /* if it's an absolute path, keep the same session info */
2761 lstrcpynW(path, lpszUrl, 2048);
2763 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2765 TRACE("Redirect through proxy\n");
2766 lstrcpynW(path, lpszUrl, 2048);
2770 URL_COMPONENTSW urlComponents;
2771 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2772 static WCHAR szHttp[] = {'h','t','t','p',0};
2773 static WCHAR szHttps[] = {'h','t','t','p','s',0};
2774 DWORD url_length = 0;
2776 LPWSTR combined_url;
2778 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2779 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
2780 urlComponents.dwSchemeLength = 0;
2781 urlComponents.lpszHostName = lpwhs->lpszHostName;
2782 urlComponents.dwHostNameLength = 0;
2783 urlComponents.nPort = lpwhs->nHostPort;
2784 urlComponents.lpszUserName = lpwhs->lpszUserName;
2785 urlComponents.dwUserNameLength = 0;
2786 urlComponents.lpszPassword = NULL;
2787 urlComponents.dwPasswordLength = 0;
2788 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2789 urlComponents.dwUrlPathLength = 0;
2790 urlComponents.lpszExtraInfo = NULL;
2791 urlComponents.dwExtraInfoLength = 0;
2793 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2794 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2797 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
2799 /* convert from bytes to characters */
2800 url_length = url_length / sizeof(WCHAR) - 1;
2801 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2803 HeapFree(GetProcessHeap(), 0, orig_url);
2808 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2809 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2811 HeapFree(GetProcessHeap(), 0, orig_url);
2814 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2816 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2818 HeapFree(GetProcessHeap(), 0, orig_url);
2819 HeapFree(GetProcessHeap(), 0, combined_url);
2822 HeapFree(GetProcessHeap(), 0, orig_url);
2828 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2829 urlComponents.lpszScheme = protocol;
2830 urlComponents.dwSchemeLength = 32;
2831 urlComponents.lpszHostName = hostName;
2832 urlComponents.dwHostNameLength = MAXHOSTNAME;
2833 urlComponents.lpszUserName = userName;
2834 urlComponents.dwUserNameLength = 1024;
2835 urlComponents.lpszPassword = NULL;
2836 urlComponents.dwPasswordLength = 0;
2837 urlComponents.lpszUrlPath = path;
2838 urlComponents.dwUrlPathLength = 2048;
2839 urlComponents.lpszExtraInfo = NULL;
2840 urlComponents.dwExtraInfoLength = 0;
2841 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2843 HeapFree(GetProcessHeap(), 0, combined_url);
2847 HeapFree(GetProcessHeap(), 0, combined_url);
2849 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2850 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2852 TRACE("redirect from secure page to non-secure page\n");
2853 /* FIXME: warn about from secure redirect to non-secure page */
2854 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2856 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2857 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2859 TRACE("redirect from non-secure page to secure page\n");
2860 /* FIXME: notify about redirect to secure page */
2861 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2864 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2866 if (lstrlenW(protocol)>4) /*https*/
2867 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2869 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2874 * This upsets redirects to binary files on sourceforge.net
2875 * and gives an html page instead of the target file
2876 * Examination of the HTTP request sent by native wininet.dll
2877 * reveals that it doesn't send a referrer in that case.
2878 * Maybe there's a flag that enables this, or maybe a referrer
2879 * shouldn't be added in case of a redirect.
2882 /* consider the current host as the referrer */
2883 if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
2884 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2885 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2886 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2889 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2890 lpwhs->lpszServerName = WININET_strdupW(hostName);
2891 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2892 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2893 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2896 static const WCHAR fmt[] = {'%','s',':','%','i',0};
2897 len = lstrlenW(hostName);
2898 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2899 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2900 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2903 lpwhs->lpszHostName = WININET_strdupW(hostName);
2905 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2908 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2909 lpwhs->lpszUserName = NULL;
2911 lpwhs->lpszUserName = WININET_strdupW(userName);
2912 lpwhs->nServerPort = urlComponents.nPort;
2914 if (!HTTP_ResolveName(lpwhr))
2917 NETCON_close(&lpwhr->netConnection);
2919 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2923 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2924 lpwhr->lpszPath=NULL;
2930 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2931 if (rc != E_POINTER)
2932 needed = strlenW(path)+1;
2933 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2934 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2935 URL_ESCAPE_SPACES_ONLY);
2938 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
2939 strcpyW(lpwhr->lpszPath,path);
2946 /***********************************************************************
2947 * HTTP_build_req (internal)
2949 * concatenate all the strings in the request together
2951 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2956 for( t = list; *t ; t++ )
2957 len += strlenW( *t );
2960 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2963 for( t = list; *t ; t++ )
2969 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2972 LPWSTR requestString;
2978 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2979 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2980 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2984 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2985 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2986 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
2987 HeapFree( GetProcessHeap(), 0, lpszPath );
2989 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2990 NULL, 0, NULL, NULL );
2991 len--; /* the nul terminator isn't needed */
2992 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2993 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2994 ascii_req, len, NULL, NULL );
2995 HeapFree( GetProcessHeap(), 0, requestString );
2997 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2999 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
3000 HeapFree( GetProcessHeap(), 0, ascii_req );
3001 if (!ret || cnt < 0)
3004 responseLen = HTTP_GetResponseHeaders( lpwhr );
3011 /***********************************************************************
3012 * HTTP_HttpSendRequestW (internal)
3014 * Sends the specified request to the HTTP server
3021 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
3022 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
3023 DWORD dwContentLength, BOOL bEndRequest)
3026 BOOL bSuccess = FALSE;
3027 LPWSTR requestString = NULL;
3030 INTERNET_ASYNC_RESULT iar;
3031 static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
3032 static const WCHAR szPost[] = { 'P','O','S','T',0 };
3033 static const WCHAR szContentLength[] =
3034 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3035 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
3037 TRACE("--> %p\n", lpwhr);
3039 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
3041 /* Clear any error information */
3042 INTERNET_SetLastError(0);
3044 /* if the verb is NULL default to GET */
3045 if (!lpwhr->lpszVerb)
3046 lpwhr->lpszVerb = WININET_strdupW(szGET);
3048 if (dwContentLength || !strcmpW(lpwhr->lpszVerb, szPost))
3050 sprintfW(contentLengthStr, szContentLength, dwContentLength);
3051 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3061 /* like native, just in case the caller forgot to call InternetReadFile
3062 * for all the data */
3063 HTTP_DrainContent(lpwhr);
3064 lpwhr->dwContentRead = 0;
3066 if (TRACE_ON(wininet))
3068 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3069 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
3073 HTTP_ProcessHeader(lpwhr, szConnection,
3074 lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION ? szKeepAlive : szClose,
3075 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
3077 HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
3078 HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
3080 /* add the headers the caller supplied */
3081 if( lpszHeaders && dwHeaderLength )
3083 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
3084 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3087 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
3089 TRACE("Request header -> %s\n", debugstr_w(requestString) );
3091 /* Send the request and store the results */
3092 if (!HTTP_OpenConnection(lpwhr))
3095 /* send the request as ASCII, tack on the optional data */
3097 dwOptionalLength = 0;
3098 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3099 NULL, 0, NULL, NULL );
3100 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
3101 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3102 ascii_req, len, NULL, NULL );
3104 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
3105 len = (len + dwOptionalLength - 1);
3107 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
3109 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3110 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3112 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
3113 HeapFree( GetProcessHeap(), 0, ascii_req );
3115 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3116 INTERNET_STATUS_REQUEST_SENT,
3117 &len, sizeof(DWORD));
3124 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3125 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3130 responseLen = HTTP_GetResponseHeaders(lpwhr);
3134 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3135 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
3138 HTTP_ProcessCookies(lpwhr);
3140 dwBufferSize = sizeof(lpwhr->dwContentLength);
3141 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
3142 &lpwhr->dwContentLength,&dwBufferSize,NULL))
3143 lpwhr->dwContentLength = -1;
3145 if (lpwhr->dwContentLength == 0)
3146 HTTP_FinishedReading(lpwhr);
3148 dwBufferSize = sizeof(dwStatusCode);
3149 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
3150 &dwStatusCode,&dwBufferSize,NULL))
3153 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
3155 WCHAR szNewLocation[2048];
3156 dwBufferSize=sizeof(szNewLocation);
3157 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
3158 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
3160 HTTP_DrainContent(lpwhr);
3161 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3162 INTERNET_STATUS_REDIRECT, szNewLocation,
3164 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
3167 HeapFree(GetProcessHeap(), 0, requestString);
3172 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
3174 WCHAR szAuthValue[2048];
3176 if (dwStatusCode == HTTP_STATUS_DENIED)
3179 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3181 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3183 lpwhr->lpHttpSession->lpszUserName,
3184 lpwhr->lpHttpSession->lpszPassword))
3191 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
3194 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3196 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3197 &lpwhr->pProxyAuthInfo,
3198 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
3199 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
3213 /* FIXME: Better check, when we have to create the cache file */
3214 if(bSuccess && (lpwhr->hdr.dwFlags & INTERNET_FLAG_NEED_FILE)) {
3215 WCHAR url[INTERNET_MAX_URL_LENGTH];
3216 WCHAR cacheFileName[MAX_PATH+1];
3219 b = HTTP_GetRequestURL(lpwhr, url);
3221 WARN("Could not get URL\n");
3225 b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
3227 lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
3228 lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
3229 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3230 if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
3231 WARN("Could not create file: %u\n", GetLastError());
3232 lpwhr->hCacheFile = NULL;
3235 WARN("Could not create cache entry: %08x\n", GetLastError());
3241 HeapFree(GetProcessHeap(), 0, requestString);
3243 /* TODO: send notification for P3P header */
3245 iar.dwResult = (DWORD)bSuccess;
3246 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
3248 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3249 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3250 sizeof(INTERNET_ASYNC_RESULT));
3256 /***********************************************************************
3257 * HTTPSESSION_Destroy (internal)
3259 * Deallocate session handle
3262 static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3264 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3266 TRACE("%p\n", lpwhs);
3268 WININET_Release(&lpwhs->lpAppInfo->hdr);
3270 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3271 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3272 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3273 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3274 HeapFree(GetProcessHeap(), 0, lpwhs);
3277 static DWORD HTTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3280 case INTERNET_OPTION_HANDLE_TYPE:
3281 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3283 if (*size < sizeof(ULONG))
3284 return ERROR_INSUFFICIENT_BUFFER;
3286 *size = sizeof(DWORD);
3287 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
3288 return ERROR_SUCCESS;
3291 FIXME("Not implemented option %d\n", option);
3292 return ERROR_INTERNET_INVALID_OPTION;
3295 static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
3296 HTTPSESSION_Destroy,
3298 HTTPSESSION_QueryOption,
3308 /***********************************************************************
3309 * HTTP_Connect (internal)
3311 * Create http session handle
3314 * HINTERNET a session handle on success
3318 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
3319 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
3320 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
3321 DWORD dwInternalFlags)
3323 BOOL bSuccess = FALSE;
3324 LPWININETHTTPSESSIONW lpwhs = NULL;
3325 HINTERNET handle = NULL;
3329 if (!lpszServerName || !lpszServerName[0])
3331 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3335 assert( hIC->hdr.htype == WH_HINIT );
3337 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
3340 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3345 * According to my tests. The name is not resolved until a request is sent
3348 lpwhs->hdr.htype = WH_HHTTPSESSION;
3349 lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
3350 lpwhs->hdr.dwFlags = dwFlags;
3351 lpwhs->hdr.dwContext = dwContext;
3352 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
3353 lpwhs->hdr.refs = 1;
3354 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
3356 WININET_AddRef( &hIC->hdr );
3357 lpwhs->lpAppInfo = hIC;
3358 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
3360 handle = WININET_AllocHandle( &lpwhs->hdr );
3363 ERR("Failed to alloc handle\n");
3364 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3368 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
3369 if(strchrW(hIC->lpszProxy, ' '))
3370 FIXME("Several proxies not implemented.\n");
3371 if(hIC->lpszProxyBypass)
3372 FIXME("Proxy bypass is ignored.\n");
3374 if (lpszServerName && lpszServerName[0])
3376 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
3377 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
3379 if (lpszUserName && lpszUserName[0])
3380 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
3381 if (lpszPassword && lpszPassword[0])
3382 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
3383 lpwhs->nServerPort = nServerPort;
3384 lpwhs->nHostPort = nServerPort;
3386 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3387 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
3389 INTERNET_SendCallback(&hIC->hdr, dwContext,
3390 INTERNET_STATUS_HANDLE_CREATED, &handle,
3398 WININET_Release( &lpwhs->hdr );
3401 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3405 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
3410 /***********************************************************************
3411 * HTTP_OpenConnection (internal)
3413 * Connect to a web server
3420 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
3422 BOOL bSuccess = FALSE;
3423 LPWININETHTTPSESSIONW lpwhs;
3424 LPWININETAPPINFOW hIC = NULL;
3430 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3432 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3436 if (NETCON_connected(&lpwhr->netConnection))
3442 lpwhs = lpwhr->lpHttpSession;
3444 hIC = lpwhs->lpAppInfo;
3445 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
3446 szaddr, sizeof(szaddr));
3447 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3448 INTERNET_STATUS_CONNECTING_TO_SERVER,
3452 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
3455 WARN("Socket creation failed\n");
3459 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
3460 sizeof(lpwhs->socketAddress)))
3463 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
3465 /* Note: we differ from Microsoft's WinINet here. they seem to have
3466 * a bug that causes no status callbacks to be sent when starting
3467 * a tunnel to a proxy server using the CONNECT verb. i believe our
3468 * behaviour to be more correct and to not cause any incompatibilities
3469 * because using a secure connection through a proxy server is a rare
3470 * case that would be hard for anyone to depend on */
3471 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
3474 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
3476 WARN("Couldn't connect securely to host\n");
3481 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3482 INTERNET_STATUS_CONNECTED_TO_SERVER,
3483 szaddr, strlen(szaddr)+1);
3488 TRACE("%d <--\n", bSuccess);
3493 /***********************************************************************
3494 * HTTP_clear_response_headers (internal)
3496 * clear out any old response headers
3498 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
3502 for( i=0; i<lpwhr->nCustHeaders; i++)
3504 if( !lpwhr->pCustHeaders[i].lpszField )
3506 if( !lpwhr->pCustHeaders[i].lpszValue )
3508 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
3510 HTTP_DeleteCustomHeader( lpwhr, i );
3515 /***********************************************************************
3516 * HTTP_GetResponseHeaders (internal)
3518 * Read server response
3525 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
3528 WCHAR buffer[MAX_REPLY_LEN];
3529 DWORD buflen = MAX_REPLY_LEN;
3530 BOOL bSuccess = FALSE;
3532 static const WCHAR szCrLf[] = {'\r','\n',0};
3533 static const WCHAR szHundred[] = {'1','0','0',0};
3534 char bufferA[MAX_REPLY_LEN];
3535 LPWSTR status_code, status_text;
3536 DWORD cchMaxRawHeaders = 1024;
3537 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3538 DWORD cchRawHeaders = 0;
3542 /* clear old response headers (eg. from a redirect response) */
3543 HTTP_clear_response_headers( lpwhr );
3545 if (!NETCON_connected(&lpwhr->netConnection))
3550 * HACK peek at the buffer
3552 buflen = MAX_REPLY_LEN;
3553 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
3556 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3558 memset(buffer, 0, MAX_REPLY_LEN);
3559 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3561 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3563 /* split the version from the status code */
3564 status_code = strchrW( buffer, ' ' );
3569 /* split the status code from the status text */
3570 status_text = strchrW( status_code, ' ' );
3575 TRACE("version [%s] status code [%s] status text [%s]\n",
3576 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3578 } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
3580 /* Add status code */
3581 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3582 HTTP_ADDHDR_FLAG_REPLACE);
3584 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3585 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3587 lpwhr->lpszVersion= WININET_strdupW(buffer);
3588 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3590 /* Restore the spaces */
3591 *(status_code-1) = ' ';
3592 *(status_text-1) = ' ';
3594 /* regenerate raw headers */
3595 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3597 cchMaxRawHeaders *= 2;
3598 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3600 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3601 cchRawHeaders += (buflen-1);
3602 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3603 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3604 lpszRawHeaders[cchRawHeaders] = '\0';
3606 /* Parse each response line */
3609 buflen = MAX_REPLY_LEN;
3610 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3612 LPWSTR * pFieldAndValue;
3614 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
3615 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3617 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3619 cchMaxRawHeaders *= 2;
3620 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3622 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3623 cchRawHeaders += (buflen-1);
3624 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3625 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3626 lpszRawHeaders[cchRawHeaders] = '\0';
3628 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3629 if (!pFieldAndValue)
3632 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
3633 HTTP_ADDREQ_FLAG_ADD );
3635 HTTP_FreeTokens(pFieldAndValue);
3645 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3646 lpwhr->lpszRawHeaders = lpszRawHeaders;
3647 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
3660 static void strip_spaces(LPWSTR start)
3665 while (*str == ' ' && *str != '\0')
3669 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
3671 end = start + strlenW(start) - 1;
3672 while (end >= start && *end == ' ')
3680 /***********************************************************************
3681 * HTTP_InterpretHttpHeader (internal)
3683 * Parse server response
3687 * Pointer to array of field, value, NULL on success.
3690 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
3692 LPWSTR * pTokenPair;
3696 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
3698 pszColon = strchrW(buffer, ':');
3699 /* must have two tokens */
3702 HTTP_FreeTokens(pTokenPair);
3704 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
3708 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
3711 HTTP_FreeTokens(pTokenPair);
3714 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
3715 pTokenPair[0][pszColon - buffer] = '\0';
3719 len = strlenW(pszColon);
3720 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
3723 HTTP_FreeTokens(pTokenPair);
3726 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
3728 strip_spaces(pTokenPair[0]);
3729 strip_spaces(pTokenPair[1]);
3731 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
3735 /***********************************************************************
3736 * HTTP_ProcessHeader (internal)
3738 * Stuff header into header tables according to <dwModifier>
3742 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3744 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
3746 LPHTTPHEADERW lphttpHdr = NULL;
3747 BOOL bSuccess = FALSE;
3749 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
3751 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
3753 /* REPLACE wins out over ADD */
3754 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3755 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
3757 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
3760 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
3764 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
3768 lphttpHdr = &lpwhr->pCustHeaders[index];
3774 hdr.lpszField = (LPWSTR)field;
3775 hdr.lpszValue = (LPWSTR)value;
3776 hdr.wFlags = hdr.wCount = 0;
3778 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3779 hdr.wFlags |= HDR_ISREQUEST;
3781 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3783 /* no value to delete */
3786 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3787 lphttpHdr->wFlags |= HDR_ISREQUEST;
3789 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
3791 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3793 HTTP_DeleteCustomHeader( lpwhr, index );
3799 hdr.lpszField = (LPWSTR)field;
3800 hdr.lpszValue = (LPWSTR)value;
3801 hdr.wFlags = hdr.wCount = 0;
3803 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3804 hdr.wFlags |= HDR_ISREQUEST;
3806 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3811 else if (dwModifier & COALESCEFLAGS)
3816 INT origlen = strlenW(lphttpHdr->lpszValue);
3817 INT valuelen = strlenW(value);
3819 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
3822 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3824 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3827 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3830 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
3832 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
3835 lphttpHdr->lpszValue = lpsztmp;
3836 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
3839 lphttpHdr->lpszValue[origlen] = ch;
3841 lphttpHdr->lpszValue[origlen] = ' ';
3845 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
3846 lphttpHdr->lpszValue[len] = '\0';
3851 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
3852 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3855 TRACE("<-- %d\n",bSuccess);
3860 /***********************************************************************
3861 * HTTP_FinishedReading (internal)
3863 * Called when all content from server has been read by client.
3866 BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
3868 WCHAR szVersion[10];
3869 DWORD dwBufferSize = sizeof(szVersion);
3873 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
3874 * the connection is keep-alive by default */
3875 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_VERSION, szVersion,
3876 &dwBufferSize, NULL) ||
3877 strcmpiW(szVersion, g_szHttp1_1))
3879 WCHAR szConnectionResponse[20];
3880 dwBufferSize = sizeof(szConnectionResponse);
3881 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse,
3882 &dwBufferSize, NULL) ||
3883 strcmpiW(szConnectionResponse, szKeepAlive))
3885 HTTPREQ_CloseConnection(&lpwhr->hdr);
3889 /* FIXME: store data in the URL cache here */
3895 /***********************************************************************
3896 * HTTP_GetCustomHeaderIndex (internal)
3898 * Return index of custom header from header array
3901 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
3902 int requested_index, BOOL request_only)
3906 TRACE("%s\n", debugstr_w(lpszField));
3908 for (index = 0; index < lpwhr->nCustHeaders; index++)
3910 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3913 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3916 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3919 if (requested_index == 0)
3924 if (index >= lpwhr->nCustHeaders)
3927 TRACE("Return: %d\n", index);
3932 /***********************************************************************
3933 * HTTP_InsertCustomHeader (internal)
3935 * Insert header into array
3938 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3941 LPHTTPHEADERW lph = NULL;
3944 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3945 count = lpwhr->nCustHeaders + 1;
3947 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3949 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3953 lpwhr->pCustHeaders = lph;
3954 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3955 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3956 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3957 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3958 lpwhr->nCustHeaders++;
3963 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3970 /***********************************************************************
3971 * HTTP_DeleteCustomHeader (internal)
3973 * Delete header from array
3974 * If this function is called, the indexs may change.
3976 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3978 if( lpwhr->nCustHeaders <= 0 )
3980 if( index >= lpwhr->nCustHeaders )
3982 lpwhr->nCustHeaders--;
3984 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3985 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3986 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3992 /***********************************************************************
3993 * HTTP_VerifyValidHeader (internal)
3995 * Verify the given header is not invalid for the given http request
3998 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
4002 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4003 if (strcmpiW(field,szAccept_Encoding)==0)
4009 /***********************************************************************
4010 * IsHostInProxyBypassList (@)
4015 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
4017 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);