push 52cba0a224aad01bcbdb26d79e43229ba950650e
[wine/hacks.git] / dlls / wininet / http.c
blob212ff9f791300199291f810ef39f861fa734b4b1
1 /*
2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
8 * Copyright 2005 Aric Stewart for CodeWeavers
9 * Copyright 2006 Robert Shearman for CodeWeavers
11 * Ulrich Czekalla
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
32 #if defined(__MINGW32__) || defined (_MSC_VER)
33 #include <ws2tcpip.h>
34 #endif
36 #include <sys/types.h>
37 #ifdef HAVE_SYS_SOCKET_H
38 # include <sys/socket.h>
39 #endif
40 #ifdef HAVE_ARPA_INET_H
41 # include <arpa/inet.h>
42 #endif
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #ifdef HAVE_UNISTD_H
47 # include <unistd.h>
48 #endif
49 #include <time.h>
50 #include <assert.h>
52 #include "windef.h"
53 #include "winbase.h"
54 #include "wininet.h"
55 #include "winerror.h"
56 #define NO_SHLWAPI_STREAM
57 #define NO_SHLWAPI_REG
58 #define NO_SHLWAPI_STRFCNS
59 #define NO_SHLWAPI_GDI
60 #include "shlwapi.h"
61 #include "sspi.h"
62 #include "wincrypt.h"
64 #include "internet.h"
65 #include "wine/debug.h"
66 #include "wine/exception.h"
67 #include "wine/unicode.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
71 static const WCHAR g_szHttp1_0[] = {'H','T','T','P','/','1','.','0',0};
72 static const WCHAR g_szHttp1_1[] = {'H','T','T','P','/','1','.','1',0};
73 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
74 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
75 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
76 static const WCHAR szHost[] = { 'H','o','s','t',0 };
77 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
78 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
79 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
80 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
81 static const WCHAR szGET[] = { 'G','E','T', 0 };
82 static const WCHAR szCrLf[] = {'\r','\n', 0};
84 #define MAXHOSTNAME 100
85 #define MAX_FIELD_VALUE_LEN 256
86 #define MAX_FIELD_LEN 256
88 #define HTTP_REFERER g_szReferer
89 #define HTTP_ACCEPT g_szAccept
90 #define HTTP_USERAGENT g_szUserAgent
92 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
93 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
94 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
95 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
96 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
97 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
98 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
100 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
102 struct HttpAuthInfo
104 LPWSTR scheme;
105 CredHandle cred;
106 CtxtHandle ctx;
107 TimeStamp exp;
108 ULONG attr;
109 ULONG max_token;
110 void *auth_data;
111 unsigned int auth_data_len;
112 BOOL finished; /* finished authenticating */
115 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
116 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear);
117 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
118 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
119 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
120 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
121 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
122 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
123 static BOOL HTTP_HttpQueryInfoW(LPWININETHTTPREQW, DWORD, LPVOID, LPDWORD, LPDWORD);
124 static LPWSTR HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
125 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
126 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
127 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field);
128 static void HTTP_DrainContent(WININETHTTPREQW *req);
129 static BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr);
131 static LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
133 int HeaderIndex = 0;
134 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
135 if (HeaderIndex == -1)
136 return NULL;
137 else
138 return &req->pCustHeaders[HeaderIndex];
141 /* set the request content length based on the headers */
142 static DWORD set_content_length( LPWININETHTTPREQW lpwhr )
144 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
145 WCHAR encoding[20];
146 DWORD size;
148 size = sizeof(lpwhr->dwContentLength);
149 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
150 &lpwhr->dwContentLength, &size, NULL))
151 lpwhr->dwContentLength = ~0u;
153 size = sizeof(encoding);
154 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_TRANSFER_ENCODING, encoding, &size, NULL) &&
155 !strcmpiW(encoding, szChunked))
157 lpwhr->dwContentLength = ~0u;
158 lpwhr->read_chunked = TRUE;
161 return lpwhr->dwContentLength;
164 /***********************************************************************
165 * HTTP_Tokenize (internal)
167 * Tokenize a string, allocating memory for the tokens.
169 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
171 LPWSTR * token_array;
172 int tokens = 0;
173 int i;
174 LPCWSTR next_token;
176 if (string)
178 /* empty string has no tokens */
179 if (*string)
180 tokens++;
181 /* count tokens */
182 for (i = 0; string[i]; i++)
184 if (!strncmpW(string+i, token_string, strlenW(token_string)))
186 DWORD j;
187 tokens++;
188 /* we want to skip over separators, but not the null terminator */
189 for (j = 0; j < strlenW(token_string) - 1; j++)
190 if (!string[i+j])
191 break;
192 i += j;
197 /* add 1 for terminating NULL */
198 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
199 token_array[tokens] = NULL;
200 if (!tokens)
201 return token_array;
202 for (i = 0; i < tokens; i++)
204 int len;
205 next_token = strstrW(string, token_string);
206 if (!next_token) next_token = string+strlenW(string);
207 len = next_token - string;
208 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
209 memcpy(token_array[i], string, len*sizeof(WCHAR));
210 token_array[i][len] = '\0';
211 string = next_token+strlenW(token_string);
213 return token_array;
216 /***********************************************************************
217 * HTTP_FreeTokens (internal)
219 * Frees memory returned from HTTP_Tokenize.
221 static void HTTP_FreeTokens(LPWSTR * token_array)
223 int i;
224 for (i = 0; token_array[i]; i++)
225 HeapFree(GetProcessHeap(), 0, token_array[i]);
226 HeapFree(GetProcessHeap(), 0, token_array);
229 /* **********************************************************************
231 * Helper functions for the HttpSendRequest(Ex) functions
234 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
236 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
237 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
239 TRACE("%p\n", lpwhr);
241 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
242 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
243 req->dwContentLength, req->bEndRequest);
245 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
248 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
250 static const WCHAR szSlash[] = { '/',0 };
251 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
253 /* If we don't have a path we set it to root */
254 if (NULL == lpwhr->lpszPath)
255 lpwhr->lpszPath = WININET_strdupW(szSlash);
256 else /* remove \r and \n*/
258 int nLen = strlenW(lpwhr->lpszPath);
259 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
261 nLen--;
262 lpwhr->lpszPath[nLen]='\0';
264 /* Replace '\' with '/' */
265 while (nLen>0) {
266 nLen--;
267 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
271 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
272 lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
273 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
275 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
276 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
277 *fixurl = '/';
278 strcpyW(fixurl + 1, lpwhr->lpszPath);
279 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
280 lpwhr->lpszPath = fixurl;
284 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
286 LPWSTR requestString;
287 DWORD len, n;
288 LPCWSTR *req;
289 UINT i;
290 LPWSTR p;
292 static const WCHAR szSpace[] = { ' ',0 };
293 static const WCHAR szColon[] = { ':',' ',0 };
294 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
296 /* allocate space for an array of all the string pointers to be added */
297 len = (lpwhr->nCustHeaders)*4 + 10;
298 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
300 /* add the verb, path and HTTP version string */
301 n = 0;
302 req[n++] = verb;
303 req[n++] = szSpace;
304 req[n++] = path;
305 req[n++] = szSpace;
306 req[n++] = version;
308 /* Append custom request headers */
309 for (i = 0; i < lpwhr->nCustHeaders; i++)
311 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
313 req[n++] = szCrLf;
314 req[n++] = lpwhr->pCustHeaders[i].lpszField;
315 req[n++] = szColon;
316 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
318 TRACE("Adding custom header %s (%s)\n",
319 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
320 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
324 if( n >= len )
325 ERR("oops. buffer overrun\n");
327 req[n] = NULL;
328 requestString = HTTP_build_req( req, 4 );
329 HeapFree( GetProcessHeap(), 0, req );
332 * Set (header) termination string for request
333 * Make sure there's exactly two new lines at the end of the request
335 p = &requestString[strlenW(requestString)-1];
336 while ( (*p == '\n') || (*p == '\r') )
337 p--;
338 strcpyW( p+1, sztwocrlf );
340 return requestString;
343 static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr )
345 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
346 int HeaderIndex;
347 int numCookies = 0;
348 LPHTTPHEADERW setCookieHeader;
350 while((HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, numCookies, FALSE)) != -1)
352 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
354 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
356 int len;
357 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','%','s',0};
358 LPWSTR buf_url;
359 LPHTTPHEADERW Host;
361 Host = HTTP_GetHeader(lpwhr,szHost);
362 len = lstrlenW(Host->lpszValue) + 9 + lstrlenW(lpwhr->lpszPath);
363 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
364 sprintfW(buf_url, szFmt, Host->lpszValue, lpwhr->lpszPath);
365 InternetSetCookieW(buf_url, NULL, setCookieHeader->lpszValue);
367 HeapFree(GetProcessHeap(), 0, buf_url);
369 numCookies++;
373 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
375 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
376 return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
377 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
380 static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
381 struct HttpAuthInfo **ppAuthInfo,
382 LPWSTR domain_and_username, LPWSTR password )
384 SECURITY_STATUS sec_status;
385 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
386 BOOL first = FALSE;
388 TRACE("%s\n", debugstr_w(pszAuthValue));
390 if (!pAuthInfo)
392 TimeStamp exp;
394 first = TRUE;
395 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
396 if (!pAuthInfo)
397 return FALSE;
399 SecInvalidateHandle(&pAuthInfo->cred);
400 SecInvalidateHandle(&pAuthInfo->ctx);
401 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
402 pAuthInfo->attr = 0;
403 pAuthInfo->auth_data = NULL;
404 pAuthInfo->auth_data_len = 0;
405 pAuthInfo->finished = FALSE;
407 if (is_basic_auth_value(pszAuthValue))
409 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
410 pAuthInfo->scheme = WININET_strdupW(szBasic);
411 if (!pAuthInfo->scheme)
413 HeapFree(GetProcessHeap(), 0, pAuthInfo);
414 return FALSE;
417 else
419 PVOID pAuthData;
420 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
422 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
423 if (!pAuthInfo->scheme)
425 HeapFree(GetProcessHeap(), 0, pAuthInfo);
426 return FALSE;
429 if (domain_and_username)
431 WCHAR *user = strchrW(domain_and_username, '\\');
432 WCHAR *domain = domain_and_username;
434 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
436 pAuthData = &nt_auth_identity;
438 if (user) user++;
439 else
441 user = domain_and_username;
442 domain = NULL;
445 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
446 nt_auth_identity.User = user;
447 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
448 nt_auth_identity.Domain = domain;
449 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
450 nt_auth_identity.Password = password;
451 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
453 else
454 /* use default credentials */
455 pAuthData = NULL;
457 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
458 SECPKG_CRED_OUTBOUND, NULL,
459 pAuthData, NULL,
460 NULL, &pAuthInfo->cred,
461 &exp);
462 if (sec_status == SEC_E_OK)
464 PSecPkgInfoW sec_pkg_info;
465 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
466 if (sec_status == SEC_E_OK)
468 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
469 FreeContextBuffer(sec_pkg_info);
472 if (sec_status != SEC_E_OK)
474 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
475 debugstr_w(pAuthInfo->scheme), sec_status);
476 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
477 HeapFree(GetProcessHeap(), 0, pAuthInfo);
478 return FALSE;
481 *ppAuthInfo = pAuthInfo;
483 else if (pAuthInfo->finished)
484 return FALSE;
486 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
487 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
489 ERR("authentication scheme changed from %s to %s\n",
490 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
491 return FALSE;
494 if (is_basic_auth_value(pszAuthValue))
496 int userlen;
497 int passlen;
498 char *auth_data;
500 TRACE("basic authentication\n");
502 /* we don't cache credentials for basic authentication, so we can't
503 * retrieve them if the application didn't pass us any credentials */
504 if (!domain_and_username) return FALSE;
506 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
507 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
509 /* length includes a nul terminator, which will be re-used for the ':' */
510 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
511 if (!auth_data)
512 return FALSE;
514 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
515 auth_data[userlen] = ':';
516 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
518 pAuthInfo->auth_data = auth_data;
519 pAuthInfo->auth_data_len = userlen + 1 + passlen;
520 pAuthInfo->finished = TRUE;
522 return TRUE;
524 else
526 LPCWSTR pszAuthData;
527 SecBufferDesc out_desc, in_desc;
528 SecBuffer out, in;
529 unsigned char *buffer;
530 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
531 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
533 in.BufferType = SECBUFFER_TOKEN;
534 in.cbBuffer = 0;
535 in.pvBuffer = NULL;
537 in_desc.ulVersion = 0;
538 in_desc.cBuffers = 1;
539 in_desc.pBuffers = &in;
541 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
542 if (*pszAuthData == ' ')
544 pszAuthData++;
545 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
546 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
547 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
550 buffer = HeapAlloc(GetProcessHeap(), 0, pAuthInfo->max_token);
552 out.BufferType = SECBUFFER_TOKEN;
553 out.cbBuffer = pAuthInfo->max_token;
554 out.pvBuffer = buffer;
556 out_desc.ulVersion = 0;
557 out_desc.cBuffers = 1;
558 out_desc.pBuffers = &out;
560 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
561 first ? NULL : &pAuthInfo->ctx,
562 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
563 context_req, 0, SECURITY_NETWORK_DREP,
564 in.pvBuffer ? &in_desc : NULL,
565 0, &pAuthInfo->ctx, &out_desc,
566 &pAuthInfo->attr, &pAuthInfo->exp);
567 if (sec_status == SEC_E_OK)
569 pAuthInfo->finished = TRUE;
570 pAuthInfo->auth_data = out.pvBuffer;
571 pAuthInfo->auth_data_len = out.cbBuffer;
572 TRACE("sending last auth packet\n");
574 else if (sec_status == SEC_I_CONTINUE_NEEDED)
576 pAuthInfo->auth_data = out.pvBuffer;
577 pAuthInfo->auth_data_len = out.cbBuffer;
578 TRACE("sending next auth packet\n");
580 else
582 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
583 pAuthInfo->finished = TRUE;
584 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
585 return FALSE;
589 return TRUE;
592 /***********************************************************************
593 * HTTP_HttpAddRequestHeadersW (internal)
595 static BOOL HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
596 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
598 LPWSTR lpszStart;
599 LPWSTR lpszEnd;
600 LPWSTR buffer;
601 BOOL bSuccess = FALSE;
602 DWORD len;
604 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
606 if( dwHeaderLength == ~0U )
607 len = strlenW(lpszHeader);
608 else
609 len = dwHeaderLength;
610 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
611 lstrcpynW( buffer, lpszHeader, len + 1);
613 lpszStart = buffer;
617 LPWSTR * pFieldAndValue;
619 lpszEnd = lpszStart;
621 while (*lpszEnd != '\0')
623 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
624 break;
625 lpszEnd++;
628 if (*lpszStart == '\0')
629 break;
631 if (*lpszEnd == '\r')
633 *lpszEnd = '\0';
634 lpszEnd += 2; /* Jump over \r\n */
636 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
637 if (*lpszStart == '\0')
639 /* Skip 0-length headers */
640 lpszStart = lpszEnd;
641 bSuccess = TRUE;
642 continue;
644 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
645 if (pFieldAndValue)
647 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
648 if (bSuccess)
649 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
650 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
651 HTTP_FreeTokens(pFieldAndValue);
654 lpszStart = lpszEnd;
655 } while (bSuccess);
657 HeapFree(GetProcessHeap(), 0, buffer);
659 return bSuccess;
662 /***********************************************************************
663 * HttpAddRequestHeadersW (WININET.@)
665 * Adds one or more HTTP header to the request handler
667 * NOTE
668 * On Windows if dwHeaderLength includes the trailing '\0', then
669 * HttpAddRequestHeadersW() adds it too. However this results in an
670 * invalid Http header which is rejected by some servers so we probably
671 * don't need to match Windows on that point.
673 * RETURNS
674 * TRUE on success
675 * FALSE on failure
678 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
679 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
681 BOOL bSuccess = FALSE;
682 LPWININETHTTPREQW lpwhr;
684 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
686 if (!lpszHeader)
687 return TRUE;
689 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
690 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
692 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
693 goto lend;
695 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
696 lend:
697 if( lpwhr )
698 WININET_Release( &lpwhr->hdr );
700 return bSuccess;
703 /***********************************************************************
704 * HttpAddRequestHeadersA (WININET.@)
706 * Adds one or more HTTP header to the request handler
708 * RETURNS
709 * TRUE on success
710 * FALSE on failure
713 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
714 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
716 DWORD len;
717 LPWSTR hdr;
718 BOOL r;
720 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
722 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
723 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
724 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
725 if( dwHeaderLength != ~0U )
726 dwHeaderLength = len;
728 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
730 HeapFree( GetProcessHeap(), 0, hdr );
732 return r;
735 /***********************************************************************
736 * HttpEndRequestA (WININET.@)
738 * Ends an HTTP request that was started by HttpSendRequestEx
740 * RETURNS
741 * TRUE if successful
742 * FALSE on failure
745 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
746 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
748 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
750 if (lpBuffersOut)
752 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
753 return FALSE;
756 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
759 static BOOL HTTP_HttpEndRequestW(LPWININETHTTPREQW lpwhr, DWORD dwFlags, DWORD_PTR dwContext)
761 BOOL rc = FALSE;
762 INT responseLen;
763 DWORD dwBufferSize;
764 INTERNET_ASYNC_RESULT iar;
766 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
767 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
769 responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
770 if (responseLen)
771 rc = TRUE;
773 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
774 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
776 /* process cookies here. Is this right? */
777 HTTP_ProcessCookies(lpwhr);
779 if (!set_content_length( lpwhr )) HTTP_FinishedReading(lpwhr);
781 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
783 DWORD dwCode,dwCodeLength = sizeof(DWORD);
784 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE, &dwCode, &dwCodeLength, NULL) &&
785 (dwCode == 302 || dwCode == 301 || dwCode == 303))
787 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
788 dwBufferSize=sizeof(szNewLocation);
789 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL))
791 /* redirects are always GETs */
792 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
793 lpwhr->lpszVerb = WININET_strdupW(szGET);
794 HTTP_DrainContent(lpwhr);
795 if ((new_url = HTTP_GetRedirectURL( lpwhr, szNewLocation )))
797 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REDIRECT,
798 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
799 rc = HTTP_HandleRedirect(lpwhr, new_url);
800 if (rc)
801 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
802 HeapFree( GetProcessHeap(), 0, new_url );
808 iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
809 iar.dwError = rc ? 0 : INTERNET_GetLastError();
811 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
812 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
813 sizeof(INTERNET_ASYNC_RESULT));
814 return rc;
817 static void AsyncHttpEndRequestProc(WORKREQUEST *work)
819 struct WORKREQ_HTTPENDREQUESTW const *req = &work->u.HttpEndRequestW;
820 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)work->hdr;
822 TRACE("%p\n", lpwhr);
824 HTTP_HttpEndRequestW(lpwhr, req->dwFlags, req->dwContext);
827 /***********************************************************************
828 * HttpEndRequestW (WININET.@)
830 * Ends an HTTP request that was started by HttpSendRequestEx
832 * RETURNS
833 * TRUE if successful
834 * FALSE on failure
837 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
838 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
840 BOOL rc = FALSE;
841 LPWININETHTTPREQW lpwhr;
843 TRACE("-->\n");
845 if (lpBuffersOut)
847 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
848 return FALSE;
851 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
853 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
855 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
856 if (lpwhr)
857 WININET_Release( &lpwhr->hdr );
858 return FALSE;
860 lpwhr->hdr.dwFlags |= dwFlags;
862 if (lpwhr->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
864 WORKREQUEST work;
865 struct WORKREQ_HTTPENDREQUESTW *request;
867 work.asyncproc = AsyncHttpEndRequestProc;
868 work.hdr = WININET_AddRef( &lpwhr->hdr );
870 request = &work.u.HttpEndRequestW;
871 request->dwFlags = dwFlags;
872 request->dwContext = dwContext;
874 INTERNET_AsyncCall(&work);
875 INTERNET_SetLastError(ERROR_IO_PENDING);
877 else
878 rc = HTTP_HttpEndRequestW(lpwhr, dwFlags, dwContext);
880 WININET_Release( &lpwhr->hdr );
881 TRACE("%i <--\n",rc);
882 return rc;
885 /***********************************************************************
886 * HttpOpenRequestW (WININET.@)
888 * Open a HTTP request handle
890 * RETURNS
891 * HINTERNET a HTTP request handle on success
892 * NULL on failure
895 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
896 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
897 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
898 DWORD dwFlags, DWORD_PTR dwContext)
900 LPWININETHTTPSESSIONW lpwhs;
901 HINTERNET handle = NULL;
903 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
904 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
905 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
906 dwFlags, dwContext);
907 if(lpszAcceptTypes!=NULL)
909 int i;
910 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
911 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
914 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
915 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
917 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
918 goto lend;
922 * My tests seem to show that the windows version does not
923 * become asynchronous until after this point. And anyhow
924 * if this call was asynchronous then how would you get the
925 * necessary HINTERNET pointer returned by this function.
928 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
929 lpszVersion, lpszReferrer, lpszAcceptTypes,
930 dwFlags, dwContext);
931 lend:
932 if( lpwhs )
933 WININET_Release( &lpwhs->hdr );
934 TRACE("returning %p\n", handle);
935 return handle;
939 /***********************************************************************
940 * HttpOpenRequestA (WININET.@)
942 * Open a HTTP request handle
944 * RETURNS
945 * HINTERNET a HTTP request handle on success
946 * NULL on failure
949 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
950 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
951 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
952 DWORD dwFlags, DWORD_PTR dwContext)
954 LPWSTR szVerb = NULL, szObjectName = NULL;
955 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
956 INT len, acceptTypesCount;
957 HINTERNET rc = FALSE;
958 LPCSTR *types;
960 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
961 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
962 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
963 dwFlags, dwContext);
965 if (lpszVerb)
967 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
968 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
969 if ( !szVerb )
970 goto end;
971 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
974 if (lpszObjectName)
976 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
977 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
978 if ( !szObjectName )
979 goto end;
980 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
983 if (lpszVersion)
985 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
986 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
987 if ( !szVersion )
988 goto end;
989 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
992 if (lpszReferrer)
994 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
995 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
996 if ( !szReferrer )
997 goto end;
998 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
1001 if (lpszAcceptTypes)
1003 acceptTypesCount = 0;
1004 types = lpszAcceptTypes;
1005 while (*types)
1007 __TRY
1009 /* find out how many there are */
1010 if (*types && **types)
1012 TRACE("accept type: %s\n", debugstr_a(*types));
1013 acceptTypesCount++;
1016 __EXCEPT_PAGE_FAULT
1018 WARN("invalid accept type pointer\n");
1020 __ENDTRY;
1021 types++;
1023 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
1024 if (!szAcceptTypes) goto end;
1026 acceptTypesCount = 0;
1027 types = lpszAcceptTypes;
1028 while (*types)
1030 __TRY
1032 if (*types && **types)
1034 len = MultiByteToWideChar(CP_ACP, 0, *types, -1, NULL, 0 );
1035 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1037 MultiByteToWideChar(CP_ACP, 0, *types, -1, szAcceptTypes[acceptTypesCount], len);
1038 acceptTypesCount++;
1041 __EXCEPT_PAGE_FAULT
1043 /* ignore invalid pointer */
1045 __ENDTRY;
1046 types++;
1048 szAcceptTypes[acceptTypesCount] = NULL;
1051 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1052 szVersion, szReferrer,
1053 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1055 end:
1056 if (szAcceptTypes)
1058 acceptTypesCount = 0;
1059 while (szAcceptTypes[acceptTypesCount])
1061 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1062 acceptTypesCount++;
1064 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1066 HeapFree(GetProcessHeap(), 0, szReferrer);
1067 HeapFree(GetProcessHeap(), 0, szVersion);
1068 HeapFree(GetProcessHeap(), 0, szObjectName);
1069 HeapFree(GetProcessHeap(), 0, szVerb);
1071 return rc;
1074 /***********************************************************************
1075 * HTTP_EncodeBase64
1077 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1079 UINT n = 0, x;
1080 static const CHAR HTTP_Base64Enc[] =
1081 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1083 while( len > 0 )
1085 /* first 6 bits, all from bin[0] */
1086 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1087 x = (bin[0] & 3) << 4;
1089 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1090 if( len == 1 )
1092 base64[n++] = HTTP_Base64Enc[x];
1093 base64[n++] = '=';
1094 base64[n++] = '=';
1095 break;
1097 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1098 x = ( bin[1] & 0x0f ) << 2;
1100 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1101 if( len == 2 )
1103 base64[n++] = HTTP_Base64Enc[x];
1104 base64[n++] = '=';
1105 break;
1107 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1109 /* last 6 bits, all from bin [2] */
1110 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1111 bin += 3;
1112 len -= 3;
1114 base64[n] = 0;
1115 return n;
1118 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1119 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1120 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1121 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1122 static const signed char HTTP_Base64Dec[256] =
1124 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1125 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1126 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1127 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1128 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1129 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1130 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1131 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1132 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1133 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1134 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1135 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1136 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1137 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1138 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1139 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1140 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1141 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1142 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1143 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1144 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1145 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1146 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1147 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1148 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1149 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1151 #undef CH
1153 /***********************************************************************
1154 * HTTP_DecodeBase64
1156 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1158 unsigned int n = 0;
1160 while(*base64)
1162 signed char in[4];
1164 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1165 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1166 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1167 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1169 WARN("invalid base64: %s\n", debugstr_w(base64));
1170 return 0;
1172 if (bin)
1173 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1174 n++;
1176 if ((base64[2] == '=') && (base64[3] == '='))
1177 break;
1178 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1179 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1181 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1182 return 0;
1184 if (bin)
1185 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1186 n++;
1188 if (base64[3] == '=')
1189 break;
1190 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1191 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1193 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1194 return 0;
1196 if (bin)
1197 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1198 n++;
1200 base64 += 4;
1203 return n;
1206 /***********************************************************************
1207 * HTTP_InsertAuthorization
1209 * Insert or delete the authorization field in the request header.
1211 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1213 if (pAuthInfo)
1215 static const WCHAR wszSpace[] = {' ',0};
1216 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1217 unsigned int len;
1218 WCHAR *authorization = NULL;
1220 if (pAuthInfo->auth_data_len)
1222 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1223 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1224 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1225 if (!authorization)
1226 return FALSE;
1228 strcpyW(authorization, pAuthInfo->scheme);
1229 strcatW(authorization, wszSpace);
1230 HTTP_EncodeBase64(pAuthInfo->auth_data,
1231 pAuthInfo->auth_data_len,
1232 authorization+strlenW(authorization));
1234 /* clear the data as it isn't valid now that it has been sent to the
1235 * server, unless it's Basic authentication which doesn't do
1236 * connection tracking */
1237 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1239 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1240 pAuthInfo->auth_data = NULL;
1241 pAuthInfo->auth_data_len = 0;
1245 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1247 HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1249 HeapFree(GetProcessHeap(), 0, authorization);
1251 return TRUE;
1254 static WCHAR *HTTP_BuildProxyRequestUrl(WININETHTTPREQW *req)
1256 WCHAR new_location[INTERNET_MAX_URL_LENGTH], *url;
1257 DWORD size;
1259 size = sizeof(new_location);
1260 if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_LOCATION, new_location, &size, NULL))
1262 if (!(url = HeapAlloc( GetProcessHeap(), 0, size + sizeof(WCHAR) ))) return NULL;
1263 strcpyW( url, new_location );
1265 else
1267 static const WCHAR slash[] = { '/',0 };
1268 static const WCHAR format[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1269 static const WCHAR formatSSL[] = { 'h','t','t','p','s',':','/','/','%','s',':','%','d',0 };
1270 WININETHTTPSESSIONW *session = req->lpHttpSession;
1272 size = 16; /* "https://" + sizeof(port#) + ":/\0" */
1273 size += strlenW( session->lpszHostName ) + strlenW( req->lpszPath );
1275 if (!(url = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
1277 if (req->hdr.dwFlags & INTERNET_FLAG_SECURE)
1278 sprintfW( url, formatSSL, session->lpszHostName, session->nHostPort );
1279 else
1280 sprintfW( url, format, session->lpszHostName, session->nHostPort );
1281 if (req->lpszPath[0] != '/') strcatW( url, slash );
1282 strcatW( url, req->lpszPath );
1284 TRACE("url=%s\n", debugstr_w(url));
1285 return url;
1288 /***********************************************************************
1289 * HTTP_DealWithProxy
1291 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1292 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1294 WCHAR buf[MAXHOSTNAME];
1295 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1296 static WCHAR szNul[] = { 0 };
1297 URL_COMPONENTSW UrlComponents;
1298 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1299 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1301 memset( &UrlComponents, 0, sizeof UrlComponents );
1302 UrlComponents.dwStructSize = sizeof UrlComponents;
1303 UrlComponents.lpszHostName = buf;
1304 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1306 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1307 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1308 sprintfW(proxy, szFormat, hIC->lpszProxy);
1309 else
1310 strcpyW(proxy, hIC->lpszProxy);
1311 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1312 return FALSE;
1313 if( UrlComponents.dwHostNameLength == 0 )
1314 return FALSE;
1316 if( !lpwhr->lpszPath )
1317 lpwhr->lpszPath = szNul;
1319 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1320 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1322 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1323 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1324 lpwhs->nServerPort = UrlComponents.nPort;
1326 TRACE("proxy server=%s port=%d\n", debugstr_w(lpwhs->lpszServerName), lpwhs->nServerPort);
1327 return TRUE;
1330 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1332 char szaddr[32];
1333 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1335 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1336 INTERNET_STATUS_RESOLVING_NAME,
1337 lpwhs->lpszServerName,
1338 strlenW(lpwhs->lpszServerName)+1);
1340 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1341 &lpwhs->socketAddress))
1343 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1344 return FALSE;
1347 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1348 szaddr, sizeof(szaddr));
1349 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1350 INTERNET_STATUS_NAME_RESOLVED,
1351 szaddr, strlen(szaddr)+1);
1353 TRACE("resolved %s to %s\n", debugstr_w(lpwhs->lpszServerName), szaddr);
1354 return TRUE;
1358 /***********************************************************************
1359 * HTTPREQ_Destroy (internal)
1361 * Deallocate request handle
1364 static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1366 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1367 DWORD i;
1369 TRACE("\n");
1371 if(lpwhr->hCacheFile)
1372 CloseHandle(lpwhr->hCacheFile);
1374 if(lpwhr->lpszCacheFile) {
1375 DeleteFileW(lpwhr->lpszCacheFile); /* FIXME */
1376 HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1379 WININET_Release(&lpwhr->lpHttpSession->hdr);
1381 if (lpwhr->pAuthInfo)
1383 if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1384 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1385 if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1386 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1388 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1389 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1390 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1391 lpwhr->pAuthInfo = NULL;
1394 if (lpwhr->pProxyAuthInfo)
1396 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1397 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1398 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1399 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1401 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1402 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1403 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1404 lpwhr->pProxyAuthInfo = NULL;
1407 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1408 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1409 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1410 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1411 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1413 for (i = 0; i < lpwhr->nCustHeaders; i++)
1415 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1416 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1419 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1420 HeapFree(GetProcessHeap(), 0, lpwhr);
1423 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1425 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1427 TRACE("%p\n",lpwhr);
1429 if (!NETCON_connected(&lpwhr->netConnection))
1430 return;
1432 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1433 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1435 NETCON_close(&lpwhr->netConnection);
1437 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1438 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1441 static DWORD HTTPREQ_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1443 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1445 switch(option) {
1446 case INTERNET_OPTION_HANDLE_TYPE:
1447 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1449 if (*size < sizeof(ULONG))
1450 return ERROR_INSUFFICIENT_BUFFER;
1452 *size = sizeof(DWORD);
1453 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1454 return ERROR_SUCCESS;
1456 case INTERNET_OPTION_URL: {
1457 WCHAR url[INTERNET_MAX_URL_LENGTH];
1458 HTTPHEADERW *host;
1459 DWORD len;
1460 WCHAR *pch;
1462 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1463 static const WCHAR hostW[] = {'H','o','s','t',0};
1465 TRACE("INTERNET_OPTION_URL\n");
1467 host = HTTP_GetHeader(req, hostW);
1468 strcpyW(url, httpW);
1469 strcatW(url, host->lpszValue);
1470 if (NULL != (pch = strchrW(url + strlenW(httpW), ':')))
1471 *pch = 0;
1472 strcatW(url, req->lpszPath);
1474 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1476 if(unicode) {
1477 len = (strlenW(url)+1) * sizeof(WCHAR);
1478 if(*size < len)
1479 return ERROR_INSUFFICIENT_BUFFER;
1481 *size = len;
1482 strcpyW(buffer, url);
1483 return ERROR_SUCCESS;
1484 }else {
1485 len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1486 if(len > *size)
1487 return ERROR_INSUFFICIENT_BUFFER;
1489 *size = len;
1490 return ERROR_SUCCESS;
1494 case INTERNET_OPTION_DATAFILE_NAME: {
1495 DWORD req_size;
1497 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1499 if(!req->lpszCacheFile) {
1500 *size = 0;
1501 return ERROR_INTERNET_ITEM_NOT_FOUND;
1504 if(unicode) {
1505 req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1506 if(*size < req_size)
1507 return ERROR_INSUFFICIENT_BUFFER;
1509 *size = req_size;
1510 memcpy(buffer, req->lpszCacheFile, *size);
1511 return ERROR_SUCCESS;
1512 }else {
1513 req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1514 if (req_size > *size)
1515 return ERROR_INSUFFICIENT_BUFFER;
1517 *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1518 -1, buffer, *size, NULL, NULL);
1519 return ERROR_SUCCESS;
1523 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1524 PCCERT_CONTEXT context;
1526 if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1527 *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1528 return ERROR_INSUFFICIENT_BUFFER;
1531 context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1532 if(context) {
1533 INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1534 DWORD len;
1536 memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1537 info->ftExpiry = context->pCertInfo->NotAfter;
1538 info->ftStart = context->pCertInfo->NotBefore;
1539 if(unicode) {
1540 len = CertNameToStrW(context->dwCertEncodingType,
1541 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1542 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1543 if(info->lpszSubjectInfo)
1544 CertNameToStrW(context->dwCertEncodingType,
1545 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1546 info->lpszSubjectInfo, len);
1547 len = CertNameToStrW(context->dwCertEncodingType,
1548 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1549 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1550 if (info->lpszIssuerInfo)
1551 CertNameToStrW(context->dwCertEncodingType,
1552 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1553 info->lpszIssuerInfo, len);
1554 }else {
1555 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1557 len = CertNameToStrA(context->dwCertEncodingType,
1558 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1559 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1560 if(infoA->lpszSubjectInfo)
1561 CertNameToStrA(context->dwCertEncodingType,
1562 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1563 infoA->lpszSubjectInfo, len);
1564 len = CertNameToStrA(context->dwCertEncodingType,
1565 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1566 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1567 if(infoA->lpszIssuerInfo)
1568 CertNameToStrA(context->dwCertEncodingType,
1569 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1570 infoA->lpszIssuerInfo, len);
1574 * Contrary to MSDN, these do not appear to be set.
1575 * lpszProtocolName
1576 * lpszSignatureAlgName
1577 * lpszEncryptionAlgName
1578 * dwKeySize
1580 CertFreeCertificateContext(context);
1581 return ERROR_SUCCESS;
1586 return INET_QueryOption(option, buffer, size, unicode);
1589 static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1591 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1593 switch(option) {
1594 case INTERNET_OPTION_SEND_TIMEOUT:
1595 case INTERNET_OPTION_RECEIVE_TIMEOUT:
1596 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1598 if (size != sizeof(DWORD))
1599 return ERROR_INVALID_PARAMETER;
1601 return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1602 *(DWORD*)buffer);
1604 case INTERNET_OPTION_USERNAME:
1605 HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszUserName);
1606 if (!(req->lpHttpSession->lpszUserName = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1607 return ERROR_SUCCESS;
1609 case INTERNET_OPTION_PASSWORD:
1610 HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszPassword);
1611 if (!(req->lpHttpSession->lpszPassword = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1612 return ERROR_SUCCESS;
1615 return ERROR_INTERNET_INVALID_OPTION;
1618 /* read some more data into the read buffer */
1619 static BOOL read_more_data( WININETHTTPREQW *req, int maxlen )
1621 int len;
1623 if (req->read_size && req->read_pos)
1625 /* move existing data to the start of the buffer */
1626 memmove( req->read_buf, req->read_buf + req->read_pos, req->read_size );
1627 req->read_pos = 0;
1630 if (maxlen == -1) maxlen = sizeof(req->read_buf);
1631 if (!NETCON_recv( &req->netConnection, req->read_buf + req->read_size,
1632 maxlen - req->read_size, 0, &len )) return FALSE;
1633 req->read_size += len;
1634 return TRUE;
1637 /* remove some amount of data from the read buffer */
1638 static void remove_data( WININETHTTPREQW *req, int count )
1640 if (!(req->read_size -= count)) req->read_pos = 0;
1641 else req->read_pos += count;
1644 static BOOL read_line( WININETHTTPREQW *req, LPSTR buffer, DWORD *len )
1646 int count, bytes_read, pos = 0;
1648 for (;;)
1650 char *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
1652 if (eol)
1654 count = eol - (req->read_buf + req->read_pos);
1655 bytes_read = count + 1;
1657 else count = bytes_read = req->read_size;
1659 count = min( count, *len - pos );
1660 memcpy( buffer + pos, req->read_buf + req->read_pos, count );
1661 pos += count;
1662 remove_data( req, bytes_read );
1663 if (eol) break;
1665 if (!read_more_data( req, -1 )) return FALSE;
1666 if (!req->read_size)
1668 *len = 0;
1669 TRACE( "returning empty string\n" );
1670 return FALSE;
1674 if (pos < *len)
1676 if (pos && buffer[pos - 1] == '\r') pos--;
1677 *len = pos + 1;
1679 buffer[*len - 1] = 0;
1680 TRACE( "returning %s\n", debugstr_a(buffer));
1681 return TRUE;
1684 /* discard data contents until we reach end of line */
1685 static BOOL discard_eol( WININETHTTPREQW *req )
1689 char *eol = memchr( req->read_buf + req->read_pos, '\n', req->read_size );
1690 if (eol)
1692 remove_data( req, (eol + 1) - (req->read_buf + req->read_pos) );
1693 break;
1695 req->read_pos = req->read_size = 0; /* discard everything */
1696 if (!read_more_data( req, -1 )) return FALSE;
1697 } while (req->read_size);
1698 return TRUE;
1701 /* read the size of the next chunk */
1702 static BOOL start_next_chunk( WININETHTTPREQW *req )
1704 DWORD chunk_size = 0;
1706 if (!req->dwContentLength) return TRUE;
1707 if (req->dwContentLength == req->dwContentRead)
1709 /* read terminator for the previous chunk */
1710 if (!discard_eol( req )) return FALSE;
1711 req->dwContentLength = ~0u;
1712 req->dwContentRead = 0;
1714 for (;;)
1716 while (req->read_size)
1718 char ch = req->read_buf[req->read_pos];
1719 if (ch >= '0' && ch <= '9') chunk_size = chunk_size * 16 + ch - '0';
1720 else if (ch >= 'a' && ch <= 'f') chunk_size = chunk_size * 16 + ch - 'a' + 10;
1721 else if (ch >= 'A' && ch <= 'F') chunk_size = chunk_size * 16 + ch - 'A' + 10;
1722 else if (ch == ';' || ch == '\r' || ch == '\n')
1724 TRACE( "reading %u byte chunk\n", chunk_size );
1725 req->dwContentLength = chunk_size;
1726 req->dwContentRead = 0;
1727 if (!discard_eol( req )) return FALSE;
1728 return TRUE;
1730 remove_data( req, 1 );
1732 if (!read_more_data( req, -1 )) return FALSE;
1733 if (!req->read_size)
1735 req->dwContentLength = req->dwContentRead = 0;
1736 return TRUE;
1741 /* return the size of data available to be read immediately */
1742 static DWORD get_avail_data( WININETHTTPREQW *req )
1744 if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
1745 return 0;
1746 return min( req->read_size, req->dwContentLength - req->dwContentRead );
1749 /* check if we have reached the end of the data to read */
1750 static BOOL end_of_read_data( WININETHTTPREQW *req )
1752 if (req->read_chunked) return (req->dwContentLength == 0);
1753 if (req->dwContentLength == ~0u) return FALSE;
1754 return (req->dwContentLength == req->dwContentRead);
1757 static BOOL refill_buffer( WININETHTTPREQW *req )
1759 int len = sizeof(req->read_buf);
1761 if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
1763 if (!start_next_chunk( req )) return FALSE;
1766 if (req->dwContentLength != ~0u) len = min( len, req->dwContentLength - req->dwContentRead );
1767 if (len <= req->read_size) return TRUE;
1769 if (!read_more_data( req, len )) return FALSE;
1770 if (!req->read_size) req->dwContentLength = req->dwContentRead = 0;
1771 return TRUE;
1774 static void HTTP_ReceiveRequestData(WININETHTTPREQW *req, BOOL first_notif)
1776 INTERNET_ASYNC_RESULT iar;
1778 TRACE("%p\n", req);
1780 if (refill_buffer( req )) {
1781 iar.dwResult = (DWORD_PTR)req->hdr.hInternet;
1782 iar.dwError = first_notif ? 0 : get_avail_data(req);
1783 }else {
1784 iar.dwResult = 0;
1785 iar.dwError = INTERNET_GetLastError();
1788 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1789 sizeof(INTERNET_ASYNC_RESULT));
1792 static DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1794 int len, bytes_read = 0;
1796 if (req->read_chunked && (req->dwContentLength == ~0u || req->dwContentLength == req->dwContentRead))
1798 if (!start_next_chunk( req )) goto done;
1800 if (req->dwContentLength != ~0u) size = min( size, req->dwContentLength - req->dwContentRead );
1802 if (req->read_size)
1804 bytes_read = min( req->read_size, size );
1805 memcpy( buffer, req->read_buf + req->read_pos, bytes_read );
1806 remove_data( req, bytes_read );
1809 if (size > bytes_read && (!bytes_read || sync))
1811 if (NETCON_recv( &req->netConnection, (char *)buffer + bytes_read, size - bytes_read,
1812 sync ? MSG_WAITALL : 0, &len))
1813 bytes_read += len;
1814 /* always return success, even if the network layer returns an error */
1816 done:
1817 req->dwContentRead += bytes_read;
1818 *read = bytes_read;
1820 TRACE( "retrieved %u bytes (%u/%u)\n", bytes_read, req->dwContentRead, req->dwContentLength );
1821 if(req->lpszCacheFile) {
1822 BOOL res;
1823 DWORD dwBytesWritten;
1825 res = WriteFile(req->hCacheFile, buffer, bytes_read, &dwBytesWritten, NULL);
1826 if(!res)
1827 WARN("WriteFile failed: %u\n", GetLastError());
1830 if(!bytes_read && (req->dwContentRead == req->dwContentLength))
1831 HTTP_FinishedReading(req);
1833 return ERROR_SUCCESS;
1837 static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1839 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1840 return HTTPREQ_Read(req, buffer, size, read, TRUE);
1843 static void HTTPREQ_AsyncReadFileExAProc(WORKREQUEST *workRequest)
1845 struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
1846 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1847 INTERNET_ASYNC_RESULT iar;
1848 DWORD res;
1850 TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
1852 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1853 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1855 iar.dwResult = res == ERROR_SUCCESS;
1856 iar.dwError = res;
1858 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1859 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1860 sizeof(INTERNET_ASYNC_RESULT));
1863 static DWORD HTTPREQ_ReadFileExA(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSA *buffers,
1864 DWORD flags, DWORD_PTR context)
1867 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1868 DWORD res;
1870 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1871 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1873 if (buffers->dwStructSize != sizeof(*buffers))
1874 return ERROR_INVALID_PARAMETER;
1876 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1878 if ((hdr->dwFlags & INTERNET_FLAG_ASYNC) && !get_avail_data(req))
1880 WORKREQUEST workRequest;
1882 workRequest.asyncproc = HTTPREQ_AsyncReadFileExAProc;
1883 workRequest.hdr = WININET_AddRef(&req->hdr);
1884 workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
1886 INTERNET_AsyncCall(&workRequest);
1888 return ERROR_IO_PENDING;
1891 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1892 !(flags & IRF_NO_WAIT));
1894 if (res == ERROR_SUCCESS) {
1895 DWORD size = buffers->dwBufferLength;
1896 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1897 &size, sizeof(size));
1900 return res;
1903 static void HTTPREQ_AsyncReadFileExWProc(WORKREQUEST *workRequest)
1905 struct WORKREQ_INTERNETREADFILEEXW const *data = &workRequest->u.InternetReadFileExW;
1906 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1907 INTERNET_ASYNC_RESULT iar;
1908 DWORD res;
1910 TRACE("INTERNETREADFILEEXW %p\n", workRequest->hdr);
1912 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1913 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1915 iar.dwResult = res == ERROR_SUCCESS;
1916 iar.dwError = res;
1918 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1919 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1920 sizeof(INTERNET_ASYNC_RESULT));
1923 static DWORD HTTPREQ_ReadFileExW(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSW *buffers,
1924 DWORD flags, DWORD_PTR context)
1927 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1928 DWORD res;
1930 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1931 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1933 if (buffers->dwStructSize != sizeof(*buffers))
1934 return ERROR_INVALID_PARAMETER;
1936 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1938 if ((hdr->dwFlags & INTERNET_FLAG_ASYNC) && !get_avail_data(req))
1940 WORKREQUEST workRequest;
1942 workRequest.asyncproc = HTTPREQ_AsyncReadFileExWProc;
1943 workRequest.hdr = WININET_AddRef(&req->hdr);
1944 workRequest.u.InternetReadFileExW.lpBuffersOut = buffers;
1946 INTERNET_AsyncCall(&workRequest);
1948 return ERROR_IO_PENDING;
1951 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1952 !(flags & IRF_NO_WAIT));
1954 if (res == ERROR_SUCCESS) {
1955 DWORD size = buffers->dwBufferLength;
1956 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1957 &size, sizeof(size));
1960 return res;
1963 static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1965 BOOL ret;
1966 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
1968 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1970 *written = 0;
1971 if ((ret = NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written)))
1972 lpwhr->dwBytesWritten += *written;
1974 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
1975 return ret;
1978 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1980 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1982 HTTP_ReceiveRequestData(req, FALSE);
1985 static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1987 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1989 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
1991 if (!(*available = get_avail_data( req )))
1993 if (end_of_read_data( req )) return ERROR_SUCCESS;
1995 if (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1997 WORKREQUEST workRequest;
1999 workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
2000 workRequest.hdr = WININET_AddRef( &req->hdr );
2002 INTERNET_AsyncCall(&workRequest);
2004 return ERROR_IO_PENDING;
2006 else
2008 refill_buffer( req );
2009 *available = get_avail_data( req );
2013 if (*available == sizeof(req->read_buf)) /* check if we have even more pending in the socket */
2015 DWORD extra;
2016 if (NETCON_query_data_available(&req->netConnection, &extra))
2017 *available = min( *available + extra, req->dwContentLength - req->dwContentRead );
2020 TRACE( "returning %u\n", *available );
2021 return ERROR_SUCCESS;
2024 static const HANDLEHEADERVtbl HTTPREQVtbl = {
2025 HTTPREQ_Destroy,
2026 HTTPREQ_CloseConnection,
2027 HTTPREQ_QueryOption,
2028 HTTPREQ_SetOption,
2029 HTTPREQ_ReadFile,
2030 HTTPREQ_ReadFileExA,
2031 HTTPREQ_ReadFileExW,
2032 HTTPREQ_WriteFile,
2033 HTTPREQ_QueryDataAvailable,
2034 NULL
2037 /***********************************************************************
2038 * HTTP_HttpOpenRequestW (internal)
2040 * Open a HTTP request handle
2042 * RETURNS
2043 * HINTERNET a HTTP request handle on success
2044 * NULL on failure
2047 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
2048 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
2049 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
2050 DWORD dwFlags, DWORD_PTR dwContext)
2052 LPWININETAPPINFOW hIC = NULL;
2053 LPWININETHTTPREQW lpwhr;
2054 LPWSTR lpszHostName = NULL;
2055 HINTERNET handle = NULL;
2056 static const WCHAR szHostForm[] = {'%','s',':','%','u',0};
2057 DWORD len;
2059 TRACE("-->\n");
2061 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
2062 hIC = lpwhs->lpAppInfo;
2064 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
2065 if (NULL == lpwhr)
2067 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2068 goto lend;
2070 lpwhr->hdr.htype = WH_HHTTPREQ;
2071 lpwhr->hdr.vtbl = &HTTPREQVtbl;
2072 lpwhr->hdr.dwFlags = dwFlags;
2073 lpwhr->hdr.dwContext = dwContext;
2074 lpwhr->hdr.refs = 1;
2075 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
2076 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
2077 lpwhr->dwContentLength = ~0u;
2079 WININET_AddRef( &lpwhs->hdr );
2080 lpwhr->lpHttpSession = lpwhs;
2081 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
2083 lpszHostName = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) *
2084 (strlenW(lpwhs->lpszHostName) + 7 /* length of ":65535" + 1 */));
2085 if (NULL == lpszHostName)
2087 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2088 goto lend;
2091 handle = WININET_AllocHandle( &lpwhr->hdr );
2092 if (NULL == handle)
2094 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2095 goto lend;
2098 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
2100 InternetCloseHandle( handle );
2101 handle = NULL;
2102 goto lend;
2105 if (lpszObjectName && *lpszObjectName) {
2106 HRESULT rc;
2108 len = 0;
2109 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
2110 if (rc != E_POINTER)
2111 len = strlenW(lpszObjectName)+1;
2112 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2113 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
2114 URL_ESCAPE_SPACES_ONLY);
2115 if (rc != S_OK)
2117 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
2118 strcpyW(lpwhr->lpszPath,lpszObjectName);
2122 if (lpszReferrer && *lpszReferrer)
2123 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2125 if (lpszAcceptTypes)
2127 int i;
2128 for (i = 0; lpszAcceptTypes[i]; i++)
2130 if (!*lpszAcceptTypes[i]) continue;
2131 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
2132 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
2133 HTTP_ADDHDR_FLAG_REQ |
2134 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
2138 lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
2140 if (lpszVersion)
2141 lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
2142 else
2143 lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
2145 if (lpwhs->nHostPort != INTERNET_INVALID_PORT_NUMBER &&
2146 lpwhs->nHostPort != INTERNET_DEFAULT_HTTP_PORT &&
2147 lpwhs->nHostPort != INTERNET_DEFAULT_HTTPS_PORT)
2149 sprintfW(lpszHostName, szHostForm, lpwhs->lpszHostName, lpwhs->nHostPort);
2150 HTTP_ProcessHeader(lpwhr, szHost, lpszHostName,
2151 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2153 else
2154 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName,
2155 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2157 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
2158 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
2159 INTERNET_DEFAULT_HTTPS_PORT :
2160 INTERNET_DEFAULT_HTTP_PORT);
2162 if (lpwhs->nHostPort == INTERNET_INVALID_PORT_NUMBER)
2163 lpwhs->nHostPort = (dwFlags & INTERNET_FLAG_SECURE ?
2164 INTERNET_DEFAULT_HTTPS_PORT :
2165 INTERNET_DEFAULT_HTTP_PORT);
2167 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2168 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
2170 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
2171 INTERNET_STATUS_HANDLE_CREATED, &handle,
2172 sizeof(handle));
2174 lend:
2175 HeapFree(GetProcessHeap(), 0, lpszHostName);
2176 if( lpwhr )
2177 WININET_Release( &lpwhr->hdr );
2179 TRACE("<-- %p (%p)\n", handle, lpwhr);
2180 return handle;
2183 /* read any content returned by the server so that the connection can be
2184 * reused */
2185 static void HTTP_DrainContent(WININETHTTPREQW *req)
2187 DWORD bytes_read;
2189 if (!NETCON_connected(&req->netConnection)) return;
2191 if (req->dwContentLength == -1)
2193 NETCON_close(&req->netConnection);
2194 return;
2199 char buffer[2048];
2200 if (HTTPREQ_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
2201 return;
2202 } while (bytes_read);
2205 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
2206 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2207 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2208 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2209 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2210 static const WCHAR szAge[] = { 'A','g','e',0 };
2211 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
2212 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2213 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2214 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2215 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2216 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
2217 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2218 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2219 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2220 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2221 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2222 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 };
2223 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2224 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
2225 static const WCHAR szDate[] = { 'D','a','t','e',0 };
2226 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
2227 static const WCHAR szETag[] = { 'E','T','a','g',0 };
2228 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
2229 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2230 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
2231 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2232 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2233 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
2234 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2235 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2236 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
2237 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2238 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2239 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
2240 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2241 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
2242 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
2243 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
2244 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
2245 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2246 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
2247 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2248 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2249 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 };
2250 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
2251 static const WCHAR szURI[] = { 'U','R','I',0 };
2252 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2253 static const WCHAR szVary[] = { 'V','a','r','y',0 };
2254 static const WCHAR szVia[] = { 'V','i','a',0 };
2255 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
2256 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2258 static const LPCWSTR header_lookup[] = {
2259 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
2260 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
2261 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2262 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
2263 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2264 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
2265 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
2266 szAllow, /* HTTP_QUERY_ALLOW = 7 */
2267 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
2268 szDate, /* HTTP_QUERY_DATE = 9 */
2269 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
2270 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2271 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
2272 szURI, /* HTTP_QUERY_URI = 13 */
2273 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
2274 NULL, /* HTTP_QUERY_COST = 15 */
2275 NULL, /* HTTP_QUERY_LINK = 16 */
2276 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
2277 NULL, /* HTTP_QUERY_VERSION = 18 */
2278 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
2279 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
2280 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
2281 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2282 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
2283 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
2284 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2285 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2286 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2287 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
2288 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2289 NULL, /* HTTP_QUERY_FORWARDED = 30 */
2290 NULL, /* HTTP_QUERY_FROM = 31 */
2291 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2292 szLocation, /* HTTP_QUERY_LOCATION = 33 */
2293 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
2294 szReferer, /* HTTP_QUERY_REFERER = 35 */
2295 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
2296 szServer, /* HTTP_QUERY_SERVER = 37 */
2297 NULL, /* HTTP_TITLE = 38 */
2298 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
2299 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2300 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2301 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2302 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
2303 szCookie, /* HTTP_QUERY_COOKIE = 44 */
2304 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2305 NULL, /* HTTP_QUERY_REFRESH = 46 */
2306 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2307 szAge, /* HTTP_QUERY_AGE = 48 */
2308 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2309 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
2310 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2311 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2312 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2313 szETag, /* HTTP_QUERY_ETAG = 54 */
2314 szHost, /* HTTP_QUERY_HOST = 55 */
2315 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
2316 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2317 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
2318 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2319 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2320 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2321 szRange, /* HTTP_QUERY_RANGE = 62 */
2322 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2323 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
2324 szVary, /* HTTP_QUERY_VARY = 65 */
2325 szVia, /* HTTP_QUERY_VIA = 66 */
2326 szWarning, /* HTTP_QUERY_WARNING = 67 */
2327 szExpect, /* HTTP_QUERY_EXPECT = 68 */
2328 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2329 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2332 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2334 /***********************************************************************
2335 * HTTP_HttpQueryInfoW (internal)
2337 static BOOL HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
2338 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2340 LPHTTPHEADERW lphttpHdr = NULL;
2341 BOOL bSuccess = FALSE;
2342 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
2343 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2344 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2345 INT index = -1;
2347 /* Find requested header structure */
2348 switch (level)
2350 case HTTP_QUERY_CUSTOM:
2351 if (!lpBuffer) return FALSE;
2352 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2353 break;
2355 case HTTP_QUERY_RAW_HEADERS_CRLF:
2357 LPWSTR headers;
2358 DWORD len = 0;
2359 BOOL ret = FALSE;
2361 if (request_only)
2362 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
2363 else
2364 headers = lpwhr->lpszRawHeaders;
2366 if (headers)
2367 len = strlenW(headers) * sizeof(WCHAR);
2369 if (len + sizeof(WCHAR) > *lpdwBufferLength)
2371 len += sizeof(WCHAR);
2372 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2373 ret = FALSE;
2375 else if (lpBuffer)
2377 if (headers)
2378 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
2379 else
2381 len = strlenW(szCrLf) * sizeof(WCHAR);
2382 memcpy(lpBuffer, szCrLf, sizeof(szCrLf));
2384 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
2385 ret = TRUE;
2387 *lpdwBufferLength = len;
2389 if (request_only)
2390 HeapFree(GetProcessHeap(), 0, headers);
2391 return ret;
2393 case HTTP_QUERY_RAW_HEADERS:
2395 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
2396 DWORD i, size = 0;
2397 LPWSTR pszString = lpBuffer;
2399 for (i = 0; ppszRawHeaderLines[i]; i++)
2400 size += strlenW(ppszRawHeaderLines[i]) + 1;
2402 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2404 HTTP_FreeTokens(ppszRawHeaderLines);
2405 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2406 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2407 return FALSE;
2409 if (pszString)
2411 for (i = 0; ppszRawHeaderLines[i]; i++)
2413 DWORD len = strlenW(ppszRawHeaderLines[i]);
2414 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2415 pszString += len+1;
2417 *pszString = '\0';
2418 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, size));
2420 *lpdwBufferLength = size * sizeof(WCHAR);
2421 HTTP_FreeTokens(ppszRawHeaderLines);
2423 return TRUE;
2425 case HTTP_QUERY_STATUS_TEXT:
2426 if (lpwhr->lpszStatusText)
2428 DWORD len = strlenW(lpwhr->lpszStatusText);
2429 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2431 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2432 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2433 return FALSE;
2435 if (lpBuffer)
2437 memcpy(lpBuffer, lpwhr->lpszStatusText, (len + 1) * sizeof(WCHAR));
2438 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2440 *lpdwBufferLength = len * sizeof(WCHAR);
2441 return TRUE;
2443 break;
2444 case HTTP_QUERY_VERSION:
2445 if (lpwhr->lpszVersion)
2447 DWORD len = strlenW(lpwhr->lpszVersion);
2448 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2450 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2451 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2452 return FALSE;
2454 if (lpBuffer)
2456 memcpy(lpBuffer, lpwhr->lpszVersion, (len + 1) * sizeof(WCHAR));
2457 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2459 *lpdwBufferLength = len * sizeof(WCHAR);
2460 return TRUE;
2462 break;
2463 default:
2464 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2466 if (level < LAST_TABLE_HEADER && header_lookup[level])
2467 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2468 requested_index,request_only);
2471 if (index >= 0)
2472 lphttpHdr = &lpwhr->pCustHeaders[index];
2474 /* Ensure header satisfies requested attributes */
2475 if (!lphttpHdr ||
2476 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2477 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
2479 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2480 return bSuccess;
2483 if (lpdwIndex && level != HTTP_QUERY_STATUS_CODE) (*lpdwIndex)++;
2485 /* coalesce value to requested type */
2486 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
2488 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2489 TRACE(" returning number: %d\n", *(int *)lpBuffer);
2490 bSuccess = TRUE;
2492 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
2494 time_t tmpTime;
2495 struct tm tmpTM;
2496 SYSTEMTIME *STHook;
2498 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2500 tmpTM = *gmtime(&tmpTime);
2501 STHook = (SYSTEMTIME *)lpBuffer;
2502 STHook->wDay = tmpTM.tm_mday;
2503 STHook->wHour = tmpTM.tm_hour;
2504 STHook->wMilliseconds = 0;
2505 STHook->wMinute = tmpTM.tm_min;
2506 STHook->wDayOfWeek = tmpTM.tm_wday;
2507 STHook->wMonth = tmpTM.tm_mon + 1;
2508 STHook->wSecond = tmpTM.tm_sec;
2509 STHook->wYear = tmpTM.tm_year;
2510 bSuccess = TRUE;
2512 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2513 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2514 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2516 else if (lphttpHdr->lpszValue)
2518 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
2520 if (len > *lpdwBufferLength)
2522 *lpdwBufferLength = len;
2523 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2524 return bSuccess;
2526 if (lpBuffer)
2528 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2529 TRACE(" returning string: %s\n", debugstr_w(lpBuffer));
2531 *lpdwBufferLength = len - sizeof(WCHAR);
2532 bSuccess = TRUE;
2534 return bSuccess;
2537 /***********************************************************************
2538 * HttpQueryInfoW (WININET.@)
2540 * Queries for information about an HTTP request
2542 * RETURNS
2543 * TRUE on success
2544 * FALSE on failure
2547 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2548 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2550 BOOL bSuccess = FALSE;
2551 LPWININETHTTPREQW lpwhr;
2553 if (TRACE_ON(wininet)) {
2554 #define FE(x) { x, #x }
2555 static const wininet_flag_info query_flags[] = {
2556 FE(HTTP_QUERY_MIME_VERSION),
2557 FE(HTTP_QUERY_CONTENT_TYPE),
2558 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2559 FE(HTTP_QUERY_CONTENT_ID),
2560 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2561 FE(HTTP_QUERY_CONTENT_LENGTH),
2562 FE(HTTP_QUERY_CONTENT_LANGUAGE),
2563 FE(HTTP_QUERY_ALLOW),
2564 FE(HTTP_QUERY_PUBLIC),
2565 FE(HTTP_QUERY_DATE),
2566 FE(HTTP_QUERY_EXPIRES),
2567 FE(HTTP_QUERY_LAST_MODIFIED),
2568 FE(HTTP_QUERY_MESSAGE_ID),
2569 FE(HTTP_QUERY_URI),
2570 FE(HTTP_QUERY_DERIVED_FROM),
2571 FE(HTTP_QUERY_COST),
2572 FE(HTTP_QUERY_LINK),
2573 FE(HTTP_QUERY_PRAGMA),
2574 FE(HTTP_QUERY_VERSION),
2575 FE(HTTP_QUERY_STATUS_CODE),
2576 FE(HTTP_QUERY_STATUS_TEXT),
2577 FE(HTTP_QUERY_RAW_HEADERS),
2578 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2579 FE(HTTP_QUERY_CONNECTION),
2580 FE(HTTP_QUERY_ACCEPT),
2581 FE(HTTP_QUERY_ACCEPT_CHARSET),
2582 FE(HTTP_QUERY_ACCEPT_ENCODING),
2583 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2584 FE(HTTP_QUERY_AUTHORIZATION),
2585 FE(HTTP_QUERY_CONTENT_ENCODING),
2586 FE(HTTP_QUERY_FORWARDED),
2587 FE(HTTP_QUERY_FROM),
2588 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2589 FE(HTTP_QUERY_LOCATION),
2590 FE(HTTP_QUERY_ORIG_URI),
2591 FE(HTTP_QUERY_REFERER),
2592 FE(HTTP_QUERY_RETRY_AFTER),
2593 FE(HTTP_QUERY_SERVER),
2594 FE(HTTP_QUERY_TITLE),
2595 FE(HTTP_QUERY_USER_AGENT),
2596 FE(HTTP_QUERY_WWW_AUTHENTICATE),
2597 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2598 FE(HTTP_QUERY_ACCEPT_RANGES),
2599 FE(HTTP_QUERY_SET_COOKIE),
2600 FE(HTTP_QUERY_COOKIE),
2601 FE(HTTP_QUERY_REQUEST_METHOD),
2602 FE(HTTP_QUERY_REFRESH),
2603 FE(HTTP_QUERY_CONTENT_DISPOSITION),
2604 FE(HTTP_QUERY_AGE),
2605 FE(HTTP_QUERY_CACHE_CONTROL),
2606 FE(HTTP_QUERY_CONTENT_BASE),
2607 FE(HTTP_QUERY_CONTENT_LOCATION),
2608 FE(HTTP_QUERY_CONTENT_MD5),
2609 FE(HTTP_QUERY_CONTENT_RANGE),
2610 FE(HTTP_QUERY_ETAG),
2611 FE(HTTP_QUERY_HOST),
2612 FE(HTTP_QUERY_IF_MATCH),
2613 FE(HTTP_QUERY_IF_NONE_MATCH),
2614 FE(HTTP_QUERY_IF_RANGE),
2615 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2616 FE(HTTP_QUERY_MAX_FORWARDS),
2617 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2618 FE(HTTP_QUERY_RANGE),
2619 FE(HTTP_QUERY_TRANSFER_ENCODING),
2620 FE(HTTP_QUERY_UPGRADE),
2621 FE(HTTP_QUERY_VARY),
2622 FE(HTTP_QUERY_VIA),
2623 FE(HTTP_QUERY_WARNING),
2624 FE(HTTP_QUERY_CUSTOM)
2626 static const wininet_flag_info modifier_flags[] = {
2627 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2628 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2629 FE(HTTP_QUERY_FLAG_NUMBER),
2630 FE(HTTP_QUERY_FLAG_COALESCE)
2632 #undef FE
2633 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2634 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
2635 DWORD i;
2637 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
2638 TRACE(" Attribute:");
2639 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2640 if (query_flags[i].val == info) {
2641 TRACE(" %s", query_flags[i].name);
2642 break;
2645 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
2646 TRACE(" Unknown (%08x)", info);
2649 TRACE(" Modifier:");
2650 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2651 if (modifier_flags[i].val & info_mod) {
2652 TRACE(" %s", modifier_flags[i].name);
2653 info_mod &= ~ modifier_flags[i].val;
2657 if (info_mod) {
2658 TRACE(" Unknown (%08x)", info_mod);
2660 TRACE("\n");
2663 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2664 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2666 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2667 goto lend;
2670 if (lpBuffer == NULL)
2671 *lpdwBufferLength = 0;
2672 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2673 lpBuffer, lpdwBufferLength, lpdwIndex);
2675 lend:
2676 if( lpwhr )
2677 WININET_Release( &lpwhr->hdr );
2679 TRACE("%d <--\n", bSuccess);
2680 return bSuccess;
2683 /***********************************************************************
2684 * HttpQueryInfoA (WININET.@)
2686 * Queries for information about an HTTP request
2688 * RETURNS
2689 * TRUE on success
2690 * FALSE on failure
2693 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2694 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2696 BOOL result;
2697 DWORD len;
2698 WCHAR* bufferW;
2700 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2701 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2703 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2704 lpdwBufferLength, lpdwIndex );
2707 if (lpBuffer)
2709 DWORD alloclen;
2710 len = (*lpdwBufferLength)*sizeof(WCHAR);
2711 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2713 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2714 if (alloclen < len)
2715 alloclen = len;
2717 else
2718 alloclen = len;
2719 bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
2720 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2721 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2722 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
2723 } else
2725 bufferW = NULL;
2726 len = 0;
2729 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2730 &len, lpdwIndex );
2731 if( result )
2733 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
2734 lpBuffer, *lpdwBufferLength, NULL, NULL );
2735 *lpdwBufferLength = len - 1;
2737 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
2739 else
2740 /* since the strings being returned from HttpQueryInfoW should be
2741 * only ASCII characters, it is reasonable to assume that all of
2742 * the Unicode characters can be reduced to a single byte */
2743 *lpdwBufferLength = len / sizeof(WCHAR);
2745 HeapFree(GetProcessHeap(), 0, bufferW );
2747 return result;
2750 /***********************************************************************
2751 * HttpSendRequestExA (WININET.@)
2753 * Sends the specified request to the HTTP server and allows chunked
2754 * transfers.
2756 * RETURNS
2757 * Success: TRUE
2758 * Failure: FALSE, call GetLastError() for more information.
2760 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2761 LPINTERNET_BUFFERSA lpBuffersIn,
2762 LPINTERNET_BUFFERSA lpBuffersOut,
2763 DWORD dwFlags, DWORD_PTR dwContext)
2765 INTERNET_BUFFERSW BuffersInW;
2766 BOOL rc = FALSE;
2767 DWORD headerlen;
2768 LPWSTR header = NULL;
2770 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2771 lpBuffersOut, dwFlags, dwContext);
2773 if (lpBuffersIn)
2775 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2776 if (lpBuffersIn->lpcszHeader)
2778 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2779 lpBuffersIn->dwHeadersLength,0,0);
2780 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2781 if (!(BuffersInW.lpcszHeader = header))
2783 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2784 return FALSE;
2786 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2787 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2788 header, headerlen);
2790 else
2791 BuffersInW.lpcszHeader = NULL;
2792 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2793 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2794 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2795 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2796 BuffersInW.Next = NULL;
2799 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2801 HeapFree(GetProcessHeap(),0,header);
2803 return rc;
2806 /***********************************************************************
2807 * HttpSendRequestExW (WININET.@)
2809 * Sends the specified request to the HTTP server and allows chunked
2810 * transfers
2812 * RETURNS
2813 * Success: TRUE
2814 * Failure: FALSE, call GetLastError() for more information.
2816 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2817 LPINTERNET_BUFFERSW lpBuffersIn,
2818 LPINTERNET_BUFFERSW lpBuffersOut,
2819 DWORD dwFlags, DWORD_PTR dwContext)
2821 BOOL ret = FALSE;
2822 LPWININETHTTPREQW lpwhr;
2823 LPWININETHTTPSESSIONW lpwhs;
2824 LPWININETAPPINFOW hIC;
2826 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2827 lpBuffersOut, dwFlags, dwContext);
2829 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2831 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2833 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2834 goto lend;
2837 lpwhs = lpwhr->lpHttpSession;
2838 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
2839 hIC = lpwhs->lpAppInfo;
2840 assert(hIC->hdr.htype == WH_HINIT);
2842 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2844 WORKREQUEST workRequest;
2845 struct WORKREQ_HTTPSENDREQUESTW *req;
2847 workRequest.asyncproc = AsyncHttpSendRequestProc;
2848 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2849 req = &workRequest.u.HttpSendRequestW;
2850 if (lpBuffersIn)
2852 if (lpBuffersIn->lpcszHeader)
2853 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2854 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2855 else
2856 req->lpszHeader = NULL;
2857 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2858 req->lpOptional = lpBuffersIn->lpvBuffer;
2859 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2860 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2862 else
2864 req->lpszHeader = NULL;
2865 req->dwHeaderLength = 0;
2866 req->lpOptional = NULL;
2867 req->dwOptionalLength = 0;
2868 req->dwContentLength = 0;
2871 req->bEndRequest = FALSE;
2873 INTERNET_AsyncCall(&workRequest);
2875 * This is from windows.
2877 INTERNET_SetLastError(ERROR_IO_PENDING);
2879 else
2881 if (lpBuffersIn)
2882 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2883 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2884 lpBuffersIn->dwBufferTotal, FALSE);
2885 else
2886 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
2889 lend:
2890 if ( lpwhr )
2891 WININET_Release( &lpwhr->hdr );
2893 TRACE("<---\n");
2894 return ret;
2897 /***********************************************************************
2898 * HttpSendRequestW (WININET.@)
2900 * Sends the specified request to the HTTP server
2902 * RETURNS
2903 * TRUE on success
2904 * FALSE on failure
2907 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
2908 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2910 LPWININETHTTPREQW lpwhr;
2911 LPWININETHTTPSESSIONW lpwhs = NULL;
2912 LPWININETAPPINFOW hIC = NULL;
2913 BOOL r;
2915 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2916 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
2918 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2919 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2921 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2922 r = FALSE;
2923 goto lend;
2926 lpwhs = lpwhr->lpHttpSession;
2927 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2929 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2930 r = FALSE;
2931 goto lend;
2934 hIC = lpwhs->lpAppInfo;
2935 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2937 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2938 r = FALSE;
2939 goto lend;
2942 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2944 WORKREQUEST workRequest;
2945 struct WORKREQ_HTTPSENDREQUESTW *req;
2947 workRequest.asyncproc = AsyncHttpSendRequestProc;
2948 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2949 req = &workRequest.u.HttpSendRequestW;
2950 if (lpszHeaders)
2952 DWORD size;
2954 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
2955 else size = dwHeaderLength * sizeof(WCHAR);
2957 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, size);
2958 memcpy(req->lpszHeader, lpszHeaders, size);
2960 else
2961 req->lpszHeader = 0;
2962 req->dwHeaderLength = dwHeaderLength;
2963 req->lpOptional = lpOptional;
2964 req->dwOptionalLength = dwOptionalLength;
2965 req->dwContentLength = dwOptionalLength;
2966 req->bEndRequest = TRUE;
2968 INTERNET_AsyncCall(&workRequest);
2970 * This is from windows.
2972 INTERNET_SetLastError(ERROR_IO_PENDING);
2973 r = FALSE;
2975 else
2977 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
2978 dwHeaderLength, lpOptional, dwOptionalLength,
2979 dwOptionalLength, TRUE);
2981 lend:
2982 if( lpwhr )
2983 WININET_Release( &lpwhr->hdr );
2984 return r;
2987 /***********************************************************************
2988 * HttpSendRequestA (WININET.@)
2990 * Sends the specified request to the HTTP server
2992 * RETURNS
2993 * TRUE on success
2994 * FALSE on failure
2997 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
2998 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
3000 BOOL result;
3001 LPWSTR szHeaders=NULL;
3002 DWORD nLen=dwHeaderLength;
3003 if(lpszHeaders!=NULL)
3005 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
3006 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
3007 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
3009 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
3010 HeapFree(GetProcessHeap(),0,szHeaders);
3011 return result;
3014 static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
3016 LPHTTPHEADERW host_header;
3018 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3020 host_header = HTTP_GetHeader(req, szHost);
3021 if(!host_header)
3022 return FALSE;
3024 sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
3025 return TRUE;
3028 /***********************************************************************
3029 * HTTP_GetRedirectURL (internal)
3031 static LPWSTR HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
3033 static WCHAR szHttp[] = {'h','t','t','p',0};
3034 static WCHAR szHttps[] = {'h','t','t','p','s',0};
3035 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3036 URL_COMPONENTSW urlComponents;
3037 DWORD url_length = 0;
3038 LPWSTR orig_url;
3039 LPWSTR combined_url;
3041 if (lpszUrl[0]=='/') return WININET_strdupW( lpszUrl );
3043 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3044 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
3045 urlComponents.dwSchemeLength = 0;
3046 urlComponents.lpszHostName = lpwhs->lpszHostName;
3047 urlComponents.dwHostNameLength = 0;
3048 urlComponents.nPort = lpwhs->nHostPort;
3049 urlComponents.lpszUserName = lpwhs->lpszUserName;
3050 urlComponents.dwUserNameLength = 0;
3051 urlComponents.lpszPassword = NULL;
3052 urlComponents.dwPasswordLength = 0;
3053 urlComponents.lpszUrlPath = lpwhr->lpszPath;
3054 urlComponents.dwUrlPathLength = 0;
3055 urlComponents.lpszExtraInfo = NULL;
3056 urlComponents.dwExtraInfoLength = 0;
3058 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
3059 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
3060 return NULL;
3062 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
3064 /* convert from bytes to characters */
3065 url_length = url_length / sizeof(WCHAR) - 1;
3066 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
3068 HeapFree(GetProcessHeap(), 0, orig_url);
3069 return NULL;
3072 url_length = 0;
3073 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
3074 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
3076 HeapFree(GetProcessHeap(), 0, orig_url);
3077 return NULL;
3079 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
3081 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
3083 HeapFree(GetProcessHeap(), 0, orig_url);
3084 HeapFree(GetProcessHeap(), 0, combined_url);
3085 return NULL;
3087 HeapFree(GetProcessHeap(), 0, orig_url);
3088 return combined_url;
3092 /***********************************************************************
3093 * HTTP_HandleRedirect (internal)
3095 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
3097 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3098 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
3099 BOOL using_proxy = hIC->lpszProxy && hIC->lpszProxy[0];
3100 WCHAR path[INTERNET_MAX_URL_LENGTH];
3101 int index;
3103 if(lpszUrl[0]=='/')
3105 /* if it's an absolute path, keep the same session info */
3106 lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH);
3108 else
3110 URL_COMPONENTSW urlComponents;
3111 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
3112 static WCHAR szHttp[] = {'h','t','t','p',0};
3113 static WCHAR szHttps[] = {'h','t','t','p','s',0};
3115 userName[0] = 0;
3116 hostName[0] = 0;
3117 protocol[0] = 0;
3119 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3120 urlComponents.lpszScheme = protocol;
3121 urlComponents.dwSchemeLength = 32;
3122 urlComponents.lpszHostName = hostName;
3123 urlComponents.dwHostNameLength = MAXHOSTNAME;
3124 urlComponents.lpszUserName = userName;
3125 urlComponents.dwUserNameLength = 1024;
3126 urlComponents.lpszPassword = NULL;
3127 urlComponents.dwPasswordLength = 0;
3128 urlComponents.lpszUrlPath = path;
3129 urlComponents.dwUrlPathLength = 2048;
3130 urlComponents.lpszExtraInfo = NULL;
3131 urlComponents.dwExtraInfoLength = 0;
3132 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3133 return FALSE;
3135 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
3136 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3138 TRACE("redirect from secure page to non-secure page\n");
3139 /* FIXME: warn about from secure redirect to non-secure page */
3140 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
3142 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
3143 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3145 TRACE("redirect from non-secure page to secure page\n");
3146 /* FIXME: notify about redirect to secure page */
3147 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
3150 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
3152 if (lstrlenW(protocol)>4) /*https*/
3153 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
3154 else /*http*/
3155 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
3158 #if 0
3160 * This upsets redirects to binary files on sourceforge.net
3161 * and gives an html page instead of the target file
3162 * Examination of the HTTP request sent by native wininet.dll
3163 * reveals that it doesn't send a referrer in that case.
3164 * Maybe there's a flag that enables this, or maybe a referrer
3165 * shouldn't be added in case of a redirect.
3168 /* consider the current host as the referrer */
3169 if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
3170 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
3171 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
3172 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
3173 #endif
3175 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3176 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
3177 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
3179 int len;
3180 static const WCHAR fmt[] = {'%','s',':','%','i',0};
3181 len = lstrlenW(hostName);
3182 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
3183 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
3184 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
3186 else
3187 lpwhs->lpszHostName = WININET_strdupW(hostName);
3189 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
3191 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3192 lpwhs->lpszUserName = NULL;
3193 if (userName[0])
3194 lpwhs->lpszUserName = WININET_strdupW(userName);
3196 if (!using_proxy)
3198 if (strcmpiW(lpwhs->lpszServerName, hostName) || lpwhs->nServerPort != urlComponents.nPort)
3200 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3201 lpwhs->lpszServerName = WININET_strdupW(hostName);
3202 lpwhs->nServerPort = urlComponents.nPort;
3204 NETCON_close(&lpwhr->netConnection);
3205 if (!HTTP_ResolveName(lpwhr)) return FALSE;
3206 if (!NETCON_init(&lpwhr->netConnection, lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)) return FALSE;
3207 lpwhr->read_pos = lpwhr->read_size = 0;
3208 lpwhr->read_chunked = FALSE;
3211 else
3212 TRACE("Redirect through proxy\n");
3215 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
3216 lpwhr->lpszPath=NULL;
3217 if (*path)
3219 DWORD needed = 0;
3220 HRESULT rc;
3222 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
3223 if (rc != E_POINTER)
3224 needed = strlenW(path)+1;
3225 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
3226 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
3227 URL_ESCAPE_SPACES_ONLY);
3228 if (rc != S_OK)
3230 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
3231 strcpyW(lpwhr->lpszPath,path);
3235 /* Remove custom content-type/length headers on redirects. */
3236 index = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Type, 0, TRUE);
3237 if (0 <= index)
3238 HTTP_DeleteCustomHeader(lpwhr, index);
3239 index = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Length, 0, TRUE);
3240 if (0 <= index)
3241 HTTP_DeleteCustomHeader(lpwhr, index);
3243 return TRUE;
3246 /***********************************************************************
3247 * HTTP_build_req (internal)
3249 * concatenate all the strings in the request together
3251 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
3253 LPCWSTR *t;
3254 LPWSTR str;
3256 for( t = list; *t ; t++ )
3257 len += strlenW( *t );
3258 len++;
3260 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
3261 *str = 0;
3263 for( t = list; *t ; t++ )
3264 strcatW( str, *t );
3266 return str;
3269 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
3271 LPWSTR lpszPath;
3272 LPWSTR requestString;
3273 INT len;
3274 INT cnt;
3275 INT responseLen;
3276 char *ascii_req;
3277 BOOL ret;
3278 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
3279 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
3280 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3282 TRACE("\n");
3284 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
3285 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
3286 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
3287 HeapFree( GetProcessHeap(), 0, lpszPath );
3289 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3290 NULL, 0, NULL, NULL );
3291 len--; /* the nul terminator isn't needed */
3292 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
3293 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3294 ascii_req, len, NULL, NULL );
3295 HeapFree( GetProcessHeap(), 0, requestString );
3297 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
3299 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
3300 HeapFree( GetProcessHeap(), 0, ascii_req );
3301 if (!ret || cnt < 0)
3302 return FALSE;
3304 responseLen = HTTP_GetResponseHeaders( lpwhr, TRUE );
3305 if (!responseLen)
3306 return FALSE;
3308 return TRUE;
3311 static void HTTP_InsertCookies(LPWININETHTTPREQW lpwhr)
3313 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3314 LPWSTR lpszCookies, lpszUrl = NULL;
3315 DWORD nCookieSize, size;
3316 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3318 size = (strlenW(Host->lpszValue) + strlenW(szUrlForm) + strlenW(lpwhr->lpszPath)) * sizeof(WCHAR);
3319 if (!(lpszUrl = HeapAlloc(GetProcessHeap(), 0, size))) return;
3320 sprintfW( lpszUrl, szUrlForm, Host->lpszValue, lpwhr->lpszPath);
3322 if (InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
3324 int cnt = 0;
3325 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
3327 size = sizeof(szCookie) + nCookieSize * sizeof(WCHAR) + sizeof(szCrLf);
3328 if ((lpszCookies = HeapAlloc(GetProcessHeap(), 0, size)))
3330 cnt += sprintfW(lpszCookies, szCookie);
3331 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
3332 strcatW(lpszCookies, szCrLf);
3334 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies), HTTP_ADDREQ_FLAG_REPLACE);
3335 HeapFree(GetProcessHeap(), 0, lpszCookies);
3338 HeapFree(GetProcessHeap(), 0, lpszUrl);
3341 /***********************************************************************
3342 * HTTP_HttpSendRequestW (internal)
3344 * Sends the specified request to the HTTP server
3346 * RETURNS
3347 * TRUE on success
3348 * FALSE on failure
3351 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
3352 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
3353 DWORD dwContentLength, BOOL bEndRequest)
3355 INT cnt;
3356 BOOL bSuccess = FALSE, redirected = FALSE;
3357 LPWSTR requestString = NULL;
3358 INT responseLen;
3359 BOOL loop_next;
3360 INTERNET_ASYNC_RESULT iar;
3361 static const WCHAR szPost[] = { 'P','O','S','T',0 };
3362 static const WCHAR szContentLength[] =
3363 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3364 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
3366 TRACE("--> %p\n", lpwhr);
3368 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
3370 /* if the verb is NULL default to GET */
3371 if (!lpwhr->lpszVerb)
3372 lpwhr->lpszVerb = WININET_strdupW(szGET);
3374 if (dwContentLength || strcmpW(lpwhr->lpszVerb, szGET))
3376 sprintfW(contentLengthStr, szContentLength, dwContentLength);
3377 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_REPLACE);
3378 lpwhr->dwBytesToWrite = dwContentLength;
3380 if (lpwhr->lpHttpSession->lpAppInfo->lpszAgent)
3382 WCHAR *agent_header;
3383 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
3384 int len;
3386 len = strlenW(lpwhr->lpHttpSession->lpAppInfo->lpszAgent) + strlenW(user_agent);
3387 agent_header = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3388 sprintfW(agent_header, user_agent, lpwhr->lpHttpSession->lpAppInfo->lpszAgent);
3390 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3391 HeapFree(GetProcessHeap(), 0, agent_header);
3393 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
3395 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
3396 HTTP_HttpAddRequestHeadersW(lpwhr, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3398 if ((lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && !strcmpW(lpwhr->lpszVerb, szPost))
3400 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
3401 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
3402 HTTP_HttpAddRequestHeadersW(lpwhr, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3407 DWORD len;
3408 char *ascii_req;
3410 loop_next = FALSE;
3412 /* like native, just in case the caller forgot to call InternetReadFile
3413 * for all the data */
3414 HTTP_DrainContent(lpwhr);
3415 lpwhr->dwContentRead = 0;
3417 if (TRACE_ON(wininet))
3419 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3420 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
3423 HTTP_FixURL(lpwhr);
3424 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
3426 HTTP_ProcessHeader(lpwhr, szConnection, szKeepAlive, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
3428 HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
3429 HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
3431 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
3432 HTTP_InsertCookies(lpwhr);
3434 /* add the headers the caller supplied */
3435 if( lpszHeaders && dwHeaderLength )
3437 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
3438 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3441 if (lpwhr->lpHttpSession->lpAppInfo->lpszProxy && lpwhr->lpHttpSession->lpAppInfo->lpszProxy[0])
3443 WCHAR *url = HTTP_BuildProxyRequestUrl(lpwhr);
3444 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, url, lpwhr->lpszVersion);
3445 HeapFree(GetProcessHeap(), 0, url);
3447 else
3448 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
3451 TRACE("Request header -> %s\n", debugstr_w(requestString) );
3453 /* Send the request and store the results */
3454 if (!HTTP_OpenConnection(lpwhr))
3455 goto lend;
3457 /* send the request as ASCII, tack on the optional data */
3458 if (!lpOptional || redirected)
3459 dwOptionalLength = 0;
3460 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3461 NULL, 0, NULL, NULL );
3462 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
3463 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3464 ascii_req, len, NULL, NULL );
3465 if( lpOptional )
3466 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
3467 len = (len + dwOptionalLength - 1);
3468 ascii_req[len] = 0;
3469 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
3471 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3472 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3474 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
3475 HeapFree( GetProcessHeap(), 0, ascii_req );
3477 lpwhr->dwBytesWritten = dwOptionalLength;
3479 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3480 INTERNET_STATUS_REQUEST_SENT,
3481 &len, sizeof(DWORD));
3483 if (bEndRequest)
3485 DWORD dwBufferSize;
3486 DWORD dwStatusCode;
3488 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3489 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3491 if (cnt < 0)
3492 goto lend;
3494 responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
3495 if (responseLen)
3496 bSuccess = TRUE;
3498 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3499 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
3500 sizeof(DWORD));
3502 HTTP_ProcessCookies(lpwhr);
3504 if (!set_content_length( lpwhr )) HTTP_FinishedReading(lpwhr);
3506 dwBufferSize = sizeof(dwStatusCode);
3507 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
3508 &dwStatusCode,&dwBufferSize,NULL))
3509 dwStatusCode = 0;
3511 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
3513 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
3514 dwBufferSize=sizeof(szNewLocation);
3515 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
3516 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
3518 /* redirects are always GETs */
3519 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
3520 lpwhr->lpszVerb = WININET_strdupW(szGET);
3522 HTTP_DrainContent(lpwhr);
3523 if ((new_url = HTTP_GetRedirectURL( lpwhr, szNewLocation )))
3525 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REDIRECT,
3526 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
3527 bSuccess = HTTP_HandleRedirect(lpwhr, new_url);
3528 if (bSuccess)
3530 HeapFree(GetProcessHeap(), 0, requestString);
3531 loop_next = TRUE;
3533 HeapFree( GetProcessHeap(), 0, new_url );
3535 redirected = TRUE;
3538 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
3540 WCHAR szAuthValue[2048];
3541 dwBufferSize=2048;
3542 if (dwStatusCode == HTTP_STATUS_DENIED)
3544 DWORD dwIndex = 0;
3545 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3547 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3548 &lpwhr->pAuthInfo,
3549 lpwhr->lpHttpSession->lpszUserName,
3550 lpwhr->lpHttpSession->lpszPassword))
3552 loop_next = TRUE;
3553 break;
3557 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
3559 DWORD dwIndex = 0;
3560 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3562 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3563 &lpwhr->pProxyAuthInfo,
3564 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
3565 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
3567 loop_next = TRUE;
3568 break;
3574 else
3575 bSuccess = TRUE;
3577 while (loop_next);
3579 /* FIXME: Better check, when we have to create the cache file */
3580 if(bSuccess && (lpwhr->hdr.dwFlags & INTERNET_FLAG_NEED_FILE)) {
3581 WCHAR url[INTERNET_MAX_URL_LENGTH];
3582 WCHAR cacheFileName[MAX_PATH+1];
3583 BOOL b;
3585 b = HTTP_GetRequestURL(lpwhr, url);
3586 if(!b) {
3587 WARN("Could not get URL\n");
3588 goto lend;
3591 b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
3592 if(b) {
3593 lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
3594 lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
3595 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3596 if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
3597 WARN("Could not create file: %u\n", GetLastError());
3598 lpwhr->hCacheFile = NULL;
3600 }else {
3601 WARN("Could not create cache entry: %08x\n", GetLastError());
3605 lend:
3607 HeapFree(GetProcessHeap(), 0, requestString);
3609 /* TODO: send notification for P3P header */
3611 if (lpwhr->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3613 if (bSuccess)
3615 if (lpwhr->dwBytesWritten == lpwhr->dwBytesToWrite) HTTP_ReceiveRequestData(lpwhr, TRUE);
3616 else
3618 iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
3619 iar.dwError = 0;
3621 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3622 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3623 sizeof(INTERNET_ASYNC_RESULT));
3626 else
3628 iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
3629 iar.dwError = INTERNET_GetLastError();
3631 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3632 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3633 sizeof(INTERNET_ASYNC_RESULT));
3637 TRACE("<--\n");
3638 if (bSuccess) INTERNET_SetLastError(ERROR_SUCCESS);
3639 return bSuccess;
3642 /***********************************************************************
3643 * HTTPSESSION_Destroy (internal)
3645 * Deallocate session handle
3648 static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3650 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3652 TRACE("%p\n", lpwhs);
3654 WININET_Release(&lpwhs->lpAppInfo->hdr);
3656 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3657 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3658 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3659 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3660 HeapFree(GetProcessHeap(), 0, lpwhs);
3663 static DWORD HTTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3665 switch(option) {
3666 case INTERNET_OPTION_HANDLE_TYPE:
3667 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3669 if (*size < sizeof(ULONG))
3670 return ERROR_INSUFFICIENT_BUFFER;
3672 *size = sizeof(DWORD);
3673 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
3674 return ERROR_SUCCESS;
3677 return INET_QueryOption(option, buffer, size, unicode);
3680 static DWORD HTTPSESSION_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
3682 WININETHTTPSESSIONW *ses = (WININETHTTPSESSIONW*)hdr;
3684 switch(option) {
3685 case INTERNET_OPTION_USERNAME:
3687 HeapFree(GetProcessHeap(), 0, ses->lpszUserName);
3688 if (!(ses->lpszUserName = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
3689 return ERROR_SUCCESS;
3691 case INTERNET_OPTION_PASSWORD:
3693 HeapFree(GetProcessHeap(), 0, ses->lpszPassword);
3694 if (!(ses->lpszPassword = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
3695 return ERROR_SUCCESS;
3697 default: break;
3700 return ERROR_INTERNET_INVALID_OPTION;
3703 static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
3704 HTTPSESSION_Destroy,
3705 NULL,
3706 HTTPSESSION_QueryOption,
3707 HTTPSESSION_SetOption,
3708 NULL,
3709 NULL,
3710 NULL,
3711 NULL,
3712 NULL
3716 /***********************************************************************
3717 * HTTP_Connect (internal)
3719 * Create http session handle
3721 * RETURNS
3722 * HINTERNET a session handle on success
3723 * NULL on failure
3726 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
3727 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
3728 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
3729 DWORD dwInternalFlags)
3731 LPWININETHTTPSESSIONW lpwhs = NULL;
3732 HINTERNET handle = NULL;
3734 TRACE("-->\n");
3736 if (!lpszServerName || !lpszServerName[0])
3738 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3739 goto lerror;
3742 assert( hIC->hdr.htype == WH_HINIT );
3744 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
3745 if (NULL == lpwhs)
3747 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3748 goto lerror;
3752 * According to my tests. The name is not resolved until a request is sent
3755 lpwhs->hdr.htype = WH_HHTTPSESSION;
3756 lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
3757 lpwhs->hdr.dwFlags = dwFlags;
3758 lpwhs->hdr.dwContext = dwContext;
3759 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
3760 lpwhs->hdr.refs = 1;
3761 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
3763 WININET_AddRef( &hIC->hdr );
3764 lpwhs->lpAppInfo = hIC;
3765 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
3767 handle = WININET_AllocHandle( &lpwhs->hdr );
3768 if (NULL == handle)
3770 ERR("Failed to alloc handle\n");
3771 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3772 goto lerror;
3775 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
3776 if(strchrW(hIC->lpszProxy, ' '))
3777 FIXME("Several proxies not implemented.\n");
3778 if(hIC->lpszProxyBypass)
3779 FIXME("Proxy bypass is ignored.\n");
3781 if (lpszServerName && lpszServerName[0])
3783 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
3784 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
3786 if (lpszUserName && lpszUserName[0])
3787 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
3788 if (lpszPassword && lpszPassword[0])
3789 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
3790 lpwhs->nServerPort = nServerPort;
3791 lpwhs->nHostPort = nServerPort;
3793 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3794 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
3796 INTERNET_SendCallback(&hIC->hdr, dwContext,
3797 INTERNET_STATUS_HANDLE_CREATED, &handle,
3798 sizeof(handle));
3801 lerror:
3802 if( lpwhs )
3803 WININET_Release( &lpwhs->hdr );
3806 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3807 * windows
3810 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
3811 return handle;
3815 /***********************************************************************
3816 * HTTP_OpenConnection (internal)
3818 * Connect to a web server
3820 * RETURNS
3822 * TRUE on success
3823 * FALSE on failure
3825 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
3827 BOOL bSuccess = FALSE;
3828 LPWININETHTTPSESSIONW lpwhs;
3829 LPWININETAPPINFOW hIC = NULL;
3830 char szaddr[32];
3832 TRACE("-->\n");
3835 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3837 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3838 goto lend;
3841 if (NETCON_connected(&lpwhr->netConnection))
3843 bSuccess = TRUE;
3844 goto lend;
3846 if (!HTTP_ResolveName(lpwhr)) goto lend;
3848 lpwhs = lpwhr->lpHttpSession;
3850 hIC = lpwhs->lpAppInfo;
3851 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
3852 szaddr, sizeof(szaddr));
3853 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3854 INTERNET_STATUS_CONNECTING_TO_SERVER,
3855 szaddr,
3856 strlen(szaddr)+1);
3858 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
3859 SOCK_STREAM, 0))
3861 WARN("Socket creation failed: %u\n", INTERNET_GetLastError());
3862 goto lend;
3865 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
3866 sizeof(lpwhs->socketAddress)))
3867 goto lend;
3869 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
3871 /* Note: we differ from Microsoft's WinINet here. they seem to have
3872 * a bug that causes no status callbacks to be sent when starting
3873 * a tunnel to a proxy server using the CONNECT verb. i believe our
3874 * behaviour to be more correct and to not cause any incompatibilities
3875 * because using a secure connection through a proxy server is a rare
3876 * case that would be hard for anyone to depend on */
3877 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
3878 goto lend;
3880 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
3882 WARN("Couldn't connect securely to host\n");
3883 goto lend;
3887 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3888 INTERNET_STATUS_CONNECTED_TO_SERVER,
3889 szaddr, strlen(szaddr)+1);
3891 bSuccess = TRUE;
3893 lend:
3894 lpwhr->read_pos = lpwhr->read_size = 0;
3895 lpwhr->read_chunked = FALSE;
3897 TRACE("%d <--\n", bSuccess);
3898 return bSuccess;
3902 /***********************************************************************
3903 * HTTP_clear_response_headers (internal)
3905 * clear out any old response headers
3907 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
3909 DWORD i;
3911 for( i=0; i<lpwhr->nCustHeaders; i++)
3913 if( !lpwhr->pCustHeaders[i].lpszField )
3914 continue;
3915 if( !lpwhr->pCustHeaders[i].lpszValue )
3916 continue;
3917 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
3918 continue;
3919 HTTP_DeleteCustomHeader( lpwhr, i );
3920 i--;
3924 /***********************************************************************
3925 * HTTP_GetResponseHeaders (internal)
3927 * Read server response
3929 * RETURNS
3931 * TRUE on success
3932 * FALSE on error
3934 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear)
3936 INT cbreaks = 0;
3937 WCHAR buffer[MAX_REPLY_LEN];
3938 DWORD buflen = MAX_REPLY_LEN;
3939 BOOL bSuccess = FALSE;
3940 INT rc = 0;
3941 static const WCHAR szHundred[] = {'1','0','0',0};
3942 char bufferA[MAX_REPLY_LEN];
3943 LPWSTR status_code, status_text;
3944 DWORD cchMaxRawHeaders = 1024;
3945 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3946 DWORD cchRawHeaders = 0;
3948 TRACE("-->\n");
3950 /* clear old response headers (eg. from a redirect response) */
3951 if (clear) HTTP_clear_response_headers( lpwhr );
3953 if (!NETCON_connected(&lpwhr->netConnection))
3954 goto lend;
3956 do {
3958 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3960 buflen = MAX_REPLY_LEN;
3961 if (!read_line(lpwhr, bufferA, &buflen))
3962 goto lend;
3963 rc += buflen;
3964 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3966 /* split the version from the status code */
3967 status_code = strchrW( buffer, ' ' );
3968 if( !status_code )
3969 goto lend;
3970 *status_code++=0;
3972 /* split the status code from the status text */
3973 status_text = strchrW( status_code, ' ' );
3974 if( !status_text )
3975 goto lend;
3976 *status_text++=0;
3978 TRACE("version [%s] status code [%s] status text [%s]\n",
3979 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3981 } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
3983 /* Add status code */
3984 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3985 HTTP_ADDHDR_FLAG_REPLACE);
3987 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3988 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3990 lpwhr->lpszVersion= WININET_strdupW(buffer);
3991 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3993 /* Restore the spaces */
3994 *(status_code-1) = ' ';
3995 *(status_text-1) = ' ';
3997 /* regenerate raw headers */
3998 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
4000 cchMaxRawHeaders *= 2;
4001 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
4003 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
4004 cchRawHeaders += (buflen-1);
4005 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
4006 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
4007 lpszRawHeaders[cchRawHeaders] = '\0';
4009 /* Parse each response line */
4012 buflen = MAX_REPLY_LEN;
4013 if (read_line(lpwhr, bufferA, &buflen))
4015 LPWSTR * pFieldAndValue;
4017 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
4019 if (!bufferA[0]) break;
4020 if (!strchr(bufferA, ':'))
4022 WARN("invalid header\n");
4023 continue;
4025 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
4027 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
4029 cchMaxRawHeaders *= 2;
4030 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
4032 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
4033 cchRawHeaders += (buflen-1);
4034 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
4035 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
4036 lpszRawHeaders[cchRawHeaders] = '\0';
4038 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
4039 if (!pFieldAndValue)
4040 break;
4042 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
4043 HTTP_ADDREQ_FLAG_ADD );
4045 HTTP_FreeTokens(pFieldAndValue);
4047 else
4049 cbreaks++;
4050 if (cbreaks >= 2)
4051 break;
4053 }while(1);
4055 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
4056 lpwhr->lpszRawHeaders = lpszRawHeaders;
4057 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
4058 bSuccess = TRUE;
4060 lend:
4062 TRACE("<--\n");
4063 if (bSuccess)
4064 return rc;
4065 else
4067 HeapFree(GetProcessHeap(), 0, lpszRawHeaders);
4068 return 0;
4073 static void strip_spaces(LPWSTR start)
4075 LPWSTR str = start;
4076 LPWSTR end;
4078 while (*str == ' ' && *str != '\0')
4079 str++;
4081 if (str != start)
4082 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
4084 end = start + strlenW(start) - 1;
4085 while (end >= start && *end == ' ')
4087 *end = '\0';
4088 end--;
4093 /***********************************************************************
4094 * HTTP_InterpretHttpHeader (internal)
4096 * Parse server response
4098 * RETURNS
4100 * Pointer to array of field, value, NULL on success.
4101 * NULL on error.
4103 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
4105 LPWSTR * pTokenPair;
4106 LPWSTR pszColon;
4107 INT len;
4109 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
4111 pszColon = strchrW(buffer, ':');
4112 /* must have two tokens */
4113 if (!pszColon)
4115 HTTP_FreeTokens(pTokenPair);
4116 if (buffer[0])
4117 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
4118 return NULL;
4121 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
4122 if (!pTokenPair[0])
4124 HTTP_FreeTokens(pTokenPair);
4125 return NULL;
4127 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
4128 pTokenPair[0][pszColon - buffer] = '\0';
4130 /* skip colon */
4131 pszColon++;
4132 len = strlenW(pszColon);
4133 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
4134 if (!pTokenPair[1])
4136 HTTP_FreeTokens(pTokenPair);
4137 return NULL;
4139 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
4141 strip_spaces(pTokenPair[0]);
4142 strip_spaces(pTokenPair[1]);
4144 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
4145 return pTokenPair;
4148 /***********************************************************************
4149 * HTTP_ProcessHeader (internal)
4151 * Stuff header into header tables according to <dwModifier>
4155 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4157 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
4159 LPHTTPHEADERW lphttpHdr = NULL;
4160 BOOL bSuccess = FALSE;
4161 INT index = -1;
4162 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
4164 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
4166 /* REPLACE wins out over ADD */
4167 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
4168 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
4170 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
4171 index = -1;
4172 else
4173 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
4175 if (index >= 0)
4177 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
4179 return FALSE;
4181 lphttpHdr = &lpwhr->pCustHeaders[index];
4183 else if (value)
4185 HTTPHEADERW hdr;
4187 hdr.lpszField = (LPWSTR)field;
4188 hdr.lpszValue = (LPWSTR)value;
4189 hdr.wFlags = hdr.wCount = 0;
4191 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4192 hdr.wFlags |= HDR_ISREQUEST;
4194 return HTTP_InsertCustomHeader(lpwhr, &hdr);
4196 /* no value to delete */
4197 else return TRUE;
4199 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4200 lphttpHdr->wFlags |= HDR_ISREQUEST;
4201 else
4202 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
4204 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
4206 HTTP_DeleteCustomHeader( lpwhr, index );
4208 if (value)
4210 HTTPHEADERW hdr;
4212 hdr.lpszField = (LPWSTR)field;
4213 hdr.lpszValue = (LPWSTR)value;
4214 hdr.wFlags = hdr.wCount = 0;
4216 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4217 hdr.wFlags |= HDR_ISREQUEST;
4219 return HTTP_InsertCustomHeader(lpwhr, &hdr);
4222 return TRUE;
4224 else if (dwModifier & COALESCEFLAGS)
4226 LPWSTR lpsztmp;
4227 WCHAR ch = 0;
4228 INT len = 0;
4229 INT origlen = strlenW(lphttpHdr->lpszValue);
4230 INT valuelen = strlenW(value);
4232 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
4234 ch = ',';
4235 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
4237 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4239 ch = ';';
4240 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
4243 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
4245 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
4246 if (lpsztmp)
4248 lphttpHdr->lpszValue = lpsztmp;
4249 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
4250 if (ch > 0)
4252 lphttpHdr->lpszValue[origlen] = ch;
4253 origlen++;
4254 lphttpHdr->lpszValue[origlen] = ' ';
4255 origlen++;
4258 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
4259 lphttpHdr->lpszValue[len] = '\0';
4260 bSuccess = TRUE;
4262 else
4264 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
4265 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4268 TRACE("<-- %d\n",bSuccess);
4269 return bSuccess;
4273 /***********************************************************************
4274 * HTTP_FinishedReading (internal)
4276 * Called when all content from server has been read by client.
4279 static BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
4281 WCHAR szVersion[10];
4282 WCHAR szConnectionResponse[20];
4283 DWORD dwBufferSize = sizeof(szVersion);
4284 BOOL keepalive = FALSE;
4286 TRACE("\n");
4288 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
4289 * the connection is keep-alive by default */
4290 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_VERSION, szVersion,
4291 &dwBufferSize, NULL) &&
4292 !strcmpiW(szVersion, g_szHttp1_1))
4294 keepalive = TRUE;
4297 dwBufferSize = sizeof(szConnectionResponse);
4298 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) ||
4299 HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL))
4301 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
4304 if (!keepalive)
4306 HTTPREQ_CloseConnection(&lpwhr->hdr);
4309 /* FIXME: store data in the URL cache here */
4311 return TRUE;
4315 /***********************************************************************
4316 * HTTP_GetCustomHeaderIndex (internal)
4318 * Return index of custom header from header array
4321 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
4322 int requested_index, BOOL request_only)
4324 DWORD index;
4326 TRACE("%s\n", debugstr_w(lpszField));
4328 for (index = 0; index < lpwhr->nCustHeaders; index++)
4330 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
4331 continue;
4333 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4334 continue;
4336 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4337 continue;
4339 if (requested_index == 0)
4340 break;
4341 requested_index --;
4344 if (index >= lpwhr->nCustHeaders)
4345 index = -1;
4347 TRACE("Return: %d\n", index);
4348 return index;
4352 /***********************************************************************
4353 * HTTP_InsertCustomHeader (internal)
4355 * Insert header into array
4358 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
4360 INT count;
4361 LPHTTPHEADERW lph = NULL;
4362 BOOL r = FALSE;
4364 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
4365 count = lpwhr->nCustHeaders + 1;
4366 if (count > 1)
4367 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
4368 else
4369 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
4371 if (NULL != lph)
4373 lpwhr->pCustHeaders = lph;
4374 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
4375 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
4376 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
4377 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
4378 lpwhr->nCustHeaders++;
4379 r = TRUE;
4381 else
4383 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4386 return r;
4390 /***********************************************************************
4391 * HTTP_DeleteCustomHeader (internal)
4393 * Delete header from array
4394 * If this function is called, the indexs may change.
4396 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
4398 if( lpwhr->nCustHeaders <= 0 )
4399 return FALSE;
4400 if( index >= lpwhr->nCustHeaders )
4401 return FALSE;
4402 lpwhr->nCustHeaders--;
4404 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[index].lpszField);
4405 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[index].lpszValue);
4407 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
4408 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
4409 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
4411 return TRUE;
4415 /***********************************************************************
4416 * HTTP_VerifyValidHeader (internal)
4418 * Verify the given header is not invalid for the given http request
4421 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
4423 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4424 if (!strcmpW(lpwhr->lpszVersion, g_szHttp1_0) && !strcmpiW(field, szAccept_Encoding))
4425 return FALSE;
4427 return TRUE;
4430 /***********************************************************************
4431 * IsHostInProxyBypassList (@)
4433 * Undocumented
4436 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
4438 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
4439 return FALSE;