wininet: Pass the complete redirected URL in INTERNET_STATUS_REDIRECT callbacks.
[wine.git] / dlls / wininet / http.c
blobd3918a301c65a6980164ddba9de087667a199759
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 /***********************************************************************
142 * HTTP_Tokenize (internal)
144 * Tokenize a string, allocating memory for the tokens.
146 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
148 LPWSTR * token_array;
149 int tokens = 0;
150 int i;
151 LPCWSTR next_token;
153 if (string)
155 /* empty string has no tokens */
156 if (*string)
157 tokens++;
158 /* count tokens */
159 for (i = 0; string[i]; i++)
161 if (!strncmpW(string+i, token_string, strlenW(token_string)))
163 DWORD j;
164 tokens++;
165 /* we want to skip over separators, but not the null terminator */
166 for (j = 0; j < strlenW(token_string) - 1; j++)
167 if (!string[i+j])
168 break;
169 i += j;
174 /* add 1 for terminating NULL */
175 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
176 token_array[tokens] = NULL;
177 if (!tokens)
178 return token_array;
179 for (i = 0; i < tokens; i++)
181 int len;
182 next_token = strstrW(string, token_string);
183 if (!next_token) next_token = string+strlenW(string);
184 len = next_token - string;
185 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
186 memcpy(token_array[i], string, len*sizeof(WCHAR));
187 token_array[i][len] = '\0';
188 string = next_token+strlenW(token_string);
190 return token_array;
193 /***********************************************************************
194 * HTTP_FreeTokens (internal)
196 * Frees memory returned from HTTP_Tokenize.
198 static void HTTP_FreeTokens(LPWSTR * token_array)
200 int i;
201 for (i = 0; token_array[i]; i++)
202 HeapFree(GetProcessHeap(), 0, token_array[i]);
203 HeapFree(GetProcessHeap(), 0, token_array);
206 /* **********************************************************************
208 * Helper functions for the HttpSendRequest(Ex) functions
211 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
213 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
214 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
216 TRACE("%p\n", lpwhr);
218 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
219 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
220 req->dwContentLength, req->bEndRequest);
222 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
225 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
227 static const WCHAR szSlash[] = { '/',0 };
228 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
230 /* If we don't have a path we set it to root */
231 if (NULL == lpwhr->lpszPath)
232 lpwhr->lpszPath = WININET_strdupW(szSlash);
233 else /* remove \r and \n*/
235 int nLen = strlenW(lpwhr->lpszPath);
236 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
238 nLen--;
239 lpwhr->lpszPath[nLen]='\0';
241 /* Replace '\' with '/' */
242 while (nLen>0) {
243 nLen--;
244 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
248 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
249 lpwhr->lpszPath, strlenW(lpwhr->lpszPath), szHttp, strlenW(szHttp) )
250 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
252 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
253 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
254 *fixurl = '/';
255 strcpyW(fixurl + 1, lpwhr->lpszPath);
256 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
257 lpwhr->lpszPath = fixurl;
261 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, LPCWSTR version )
263 LPWSTR requestString;
264 DWORD len, n;
265 LPCWSTR *req;
266 UINT i;
267 LPWSTR p;
269 static const WCHAR szSpace[] = { ' ',0 };
270 static const WCHAR szColon[] = { ':',' ',0 };
271 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
273 /* allocate space for an array of all the string pointers to be added */
274 len = (lpwhr->nCustHeaders)*4 + 10;
275 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
277 /* add the verb, path and HTTP version string */
278 n = 0;
279 req[n++] = verb;
280 req[n++] = szSpace;
281 req[n++] = path;
282 req[n++] = szSpace;
283 req[n++] = version;
285 /* Append custom request headers */
286 for (i = 0; i < lpwhr->nCustHeaders; i++)
288 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
290 req[n++] = szCrLf;
291 req[n++] = lpwhr->pCustHeaders[i].lpszField;
292 req[n++] = szColon;
293 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
295 TRACE("Adding custom header %s (%s)\n",
296 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
297 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
301 if( n >= len )
302 ERR("oops. buffer overrun\n");
304 req[n] = NULL;
305 requestString = HTTP_build_req( req, 4 );
306 HeapFree( GetProcessHeap(), 0, req );
309 * Set (header) termination string for request
310 * Make sure there's exactly two new lines at the end of the request
312 p = &requestString[strlenW(requestString)-1];
313 while ( (*p == '\n') || (*p == '\r') )
314 p--;
315 strcpyW( p+1, sztwocrlf );
317 return requestString;
320 static void HTTP_ProcessCookies( LPWININETHTTPREQW lpwhr )
322 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
323 int HeaderIndex;
324 int numCookies = 0;
325 LPHTTPHEADERW setCookieHeader;
327 while((HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, numCookies, FALSE)) != -1)
329 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
331 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
333 int len;
334 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','%','s',0};
335 LPWSTR buf_url;
336 LPHTTPHEADERW Host;
338 Host = HTTP_GetHeader(lpwhr,szHost);
339 len = lstrlenW(Host->lpszValue) + 9 + lstrlenW(lpwhr->lpszPath);
340 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
341 sprintfW(buf_url, szFmt, Host->lpszValue, lpwhr->lpszPath);
342 InternetSetCookieW(buf_url, NULL, setCookieHeader->lpszValue);
344 HeapFree(GetProcessHeap(), 0, buf_url);
346 numCookies++;
350 static inline BOOL is_basic_auth_value( LPCWSTR pszAuthValue )
352 static const WCHAR szBasic[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
353 return !strncmpiW(pszAuthValue, szBasic, ARRAYSIZE(szBasic)) &&
354 ((pszAuthValue[ARRAYSIZE(szBasic)] == ' ') || !pszAuthValue[ARRAYSIZE(szBasic)]);
357 static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue,
358 struct HttpAuthInfo **ppAuthInfo,
359 LPWSTR domain_and_username, LPWSTR password )
361 SECURITY_STATUS sec_status;
362 struct HttpAuthInfo *pAuthInfo = *ppAuthInfo;
363 BOOL first = FALSE;
365 TRACE("%s\n", debugstr_w(pszAuthValue));
367 if (!pAuthInfo)
369 TimeStamp exp;
371 first = TRUE;
372 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
373 if (!pAuthInfo)
374 return FALSE;
376 SecInvalidateHandle(&pAuthInfo->cred);
377 SecInvalidateHandle(&pAuthInfo->ctx);
378 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
379 pAuthInfo->attr = 0;
380 pAuthInfo->auth_data = NULL;
381 pAuthInfo->auth_data_len = 0;
382 pAuthInfo->finished = FALSE;
384 if (is_basic_auth_value(pszAuthValue))
386 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
387 pAuthInfo->scheme = WININET_strdupW(szBasic);
388 if (!pAuthInfo->scheme)
390 HeapFree(GetProcessHeap(), 0, pAuthInfo);
391 return FALSE;
394 else
396 PVOID pAuthData;
397 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
399 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
400 if (!pAuthInfo->scheme)
402 HeapFree(GetProcessHeap(), 0, pAuthInfo);
403 return FALSE;
406 if (domain_and_username)
408 WCHAR *user = strchrW(domain_and_username, '\\');
409 WCHAR *domain = domain_and_username;
411 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
413 pAuthData = &nt_auth_identity;
415 if (user) user++;
416 else
418 user = domain_and_username;
419 domain = NULL;
422 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
423 nt_auth_identity.User = user;
424 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
425 nt_auth_identity.Domain = domain;
426 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
427 nt_auth_identity.Password = password;
428 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
430 else
431 /* use default credentials */
432 pAuthData = NULL;
434 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
435 SECPKG_CRED_OUTBOUND, NULL,
436 pAuthData, NULL,
437 NULL, &pAuthInfo->cred,
438 &exp);
439 if (sec_status == SEC_E_OK)
441 PSecPkgInfoW sec_pkg_info;
442 sec_status = QuerySecurityPackageInfoW(pAuthInfo->scheme, &sec_pkg_info);
443 if (sec_status == SEC_E_OK)
445 pAuthInfo->max_token = sec_pkg_info->cbMaxToken;
446 FreeContextBuffer(sec_pkg_info);
449 if (sec_status != SEC_E_OK)
451 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
452 debugstr_w(pAuthInfo->scheme), sec_status);
453 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
454 HeapFree(GetProcessHeap(), 0, pAuthInfo);
455 return FALSE;
458 *ppAuthInfo = pAuthInfo;
460 else if (pAuthInfo->finished)
461 return FALSE;
463 if ((strlenW(pszAuthValue) < strlenW(pAuthInfo->scheme)) ||
464 strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
466 ERR("authentication scheme changed from %s to %s\n",
467 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
468 return FALSE;
471 if (is_basic_auth_value(pszAuthValue))
473 int userlen;
474 int passlen;
475 char *auth_data;
477 TRACE("basic authentication\n");
479 /* we don't cache credentials for basic authentication, so we can't
480 * retrieve them if the application didn't pass us any credentials */
481 if (!domain_and_username) return FALSE;
483 userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
484 passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
486 /* length includes a nul terminator, which will be re-used for the ':' */
487 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
488 if (!auth_data)
489 return FALSE;
491 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
492 auth_data[userlen] = ':';
493 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
495 pAuthInfo->auth_data = auth_data;
496 pAuthInfo->auth_data_len = userlen + 1 + passlen;
497 pAuthInfo->finished = TRUE;
499 return TRUE;
501 else
503 LPCWSTR pszAuthData;
504 SecBufferDesc out_desc, in_desc;
505 SecBuffer out, in;
506 unsigned char *buffer;
507 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
508 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
510 in.BufferType = SECBUFFER_TOKEN;
511 in.cbBuffer = 0;
512 in.pvBuffer = NULL;
514 in_desc.ulVersion = 0;
515 in_desc.cBuffers = 1;
516 in_desc.pBuffers = &in;
518 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
519 if (*pszAuthData == ' ')
521 pszAuthData++;
522 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
523 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
524 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
527 buffer = HeapAlloc(GetProcessHeap(), 0, pAuthInfo->max_token);
529 out.BufferType = SECBUFFER_TOKEN;
530 out.cbBuffer = pAuthInfo->max_token;
531 out.pvBuffer = buffer;
533 out_desc.ulVersion = 0;
534 out_desc.cBuffers = 1;
535 out_desc.pBuffers = &out;
537 sec_status = InitializeSecurityContextW(first ? &pAuthInfo->cred : NULL,
538 first ? NULL : &pAuthInfo->ctx,
539 first ? lpwhr->lpHttpSession->lpszServerName : NULL,
540 context_req, 0, SECURITY_NETWORK_DREP,
541 in.pvBuffer ? &in_desc : NULL,
542 0, &pAuthInfo->ctx, &out_desc,
543 &pAuthInfo->attr, &pAuthInfo->exp);
544 if (sec_status == SEC_E_OK)
546 pAuthInfo->finished = TRUE;
547 pAuthInfo->auth_data = out.pvBuffer;
548 pAuthInfo->auth_data_len = out.cbBuffer;
549 TRACE("sending last auth packet\n");
551 else if (sec_status == SEC_I_CONTINUE_NEEDED)
553 pAuthInfo->auth_data = out.pvBuffer;
554 pAuthInfo->auth_data_len = out.cbBuffer;
555 TRACE("sending next auth packet\n");
557 else
559 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
560 pAuthInfo->finished = TRUE;
561 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
562 return FALSE;
566 return TRUE;
569 /***********************************************************************
570 * HTTP_HttpAddRequestHeadersW (internal)
572 static BOOL HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
573 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
575 LPWSTR lpszStart;
576 LPWSTR lpszEnd;
577 LPWSTR buffer;
578 BOOL bSuccess = FALSE;
579 DWORD len;
581 TRACE("copying header: %s\n", debugstr_wn(lpszHeader, dwHeaderLength));
583 if( dwHeaderLength == ~0U )
584 len = strlenW(lpszHeader);
585 else
586 len = dwHeaderLength;
587 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
588 lstrcpynW( buffer, lpszHeader, len + 1);
590 lpszStart = buffer;
594 LPWSTR * pFieldAndValue;
596 lpszEnd = lpszStart;
598 while (*lpszEnd != '\0')
600 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
601 break;
602 lpszEnd++;
605 if (*lpszStart == '\0')
606 break;
608 if (*lpszEnd == '\r')
610 *lpszEnd = '\0';
611 lpszEnd += 2; /* Jump over \r\n */
613 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
614 if (*lpszStart == '\0')
616 /* Skip 0-length headers */
617 lpszStart = lpszEnd;
618 bSuccess = TRUE;
619 continue;
621 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
622 if (pFieldAndValue)
624 bSuccess = HTTP_VerifyValidHeader(lpwhr, pFieldAndValue[0]);
625 if (bSuccess)
626 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
627 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
628 HTTP_FreeTokens(pFieldAndValue);
631 lpszStart = lpszEnd;
632 } while (bSuccess);
634 HeapFree(GetProcessHeap(), 0, buffer);
636 return bSuccess;
639 /***********************************************************************
640 * HttpAddRequestHeadersW (WININET.@)
642 * Adds one or more HTTP header to the request handler
644 * NOTE
645 * On Windows if dwHeaderLength includes the trailing '\0', then
646 * HttpAddRequestHeadersW() adds it too. However this results in an
647 * invalid Http header which is rejected by some servers so we probably
648 * don't need to match Windows on that point.
650 * RETURNS
651 * TRUE on success
652 * FALSE on failure
655 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
656 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
658 BOOL bSuccess = FALSE;
659 LPWININETHTTPREQW lpwhr;
661 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_wn(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
663 if (!lpszHeader)
664 return TRUE;
666 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
667 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
669 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
670 goto lend;
672 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
673 lend:
674 if( lpwhr )
675 WININET_Release( &lpwhr->hdr );
677 return bSuccess;
680 /***********************************************************************
681 * HttpAddRequestHeadersA (WININET.@)
683 * Adds one or more HTTP header to the request handler
685 * RETURNS
686 * TRUE on success
687 * FALSE on failure
690 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
691 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
693 DWORD len;
694 LPWSTR hdr;
695 BOOL r;
697 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_an(lpszHeader, dwHeaderLength), dwHeaderLength, dwModifier);
699 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
700 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
701 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
702 if( dwHeaderLength != ~0U )
703 dwHeaderLength = len;
705 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
707 HeapFree( GetProcessHeap(), 0, hdr );
709 return r;
712 /***********************************************************************
713 * HttpEndRequestA (WININET.@)
715 * Ends an HTTP request that was started by HttpSendRequestEx
717 * RETURNS
718 * TRUE if successful
719 * FALSE on failure
722 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
723 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
725 TRACE("(%p, %p, %08x, %08lx)\n", hRequest, lpBuffersOut, dwFlags, dwContext);
727 if (lpBuffersOut)
729 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
730 return FALSE;
733 return HttpEndRequestW(hRequest, NULL, dwFlags, dwContext);
736 static BOOL HTTP_HttpEndRequestW(LPWININETHTTPREQW lpwhr, DWORD dwFlags, DWORD_PTR dwContext)
738 BOOL rc = FALSE;
739 INT responseLen;
740 DWORD dwBufferSize;
741 INTERNET_ASYNC_RESULT iar;
743 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
744 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
746 responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
747 if (responseLen)
748 rc = TRUE;
750 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
751 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
753 /* process cookies here. Is this right? */
754 HTTP_ProcessCookies(lpwhr);
756 dwBufferSize = sizeof(lpwhr->dwContentLength);
757 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
758 &lpwhr->dwContentLength, &dwBufferSize, NULL))
759 lpwhr->dwContentLength = -1;
761 if (lpwhr->dwContentLength == 0)
762 HTTP_FinishedReading(lpwhr);
764 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
766 DWORD dwCode,dwCodeLength = sizeof(DWORD);
767 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE, &dwCode, &dwCodeLength, NULL) &&
768 (dwCode == 302 || dwCode == 301 || dwCode == 303))
770 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
771 dwBufferSize=sizeof(szNewLocation);
772 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_LOCATION, szNewLocation, &dwBufferSize, NULL))
774 /* redirects are always GETs */
775 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
776 lpwhr->lpszVerb = WININET_strdupW(szGET);
777 HTTP_DrainContent(lpwhr);
778 if ((new_url = HTTP_GetRedirectURL( lpwhr, szNewLocation )))
780 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REDIRECT,
781 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
782 rc = HTTP_HandleRedirect(lpwhr, new_url);
783 if (rc)
784 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
785 HeapFree( GetProcessHeap(), 0, new_url );
791 iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
792 iar.dwError = rc ? 0 : INTERNET_GetLastError();
794 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
795 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
796 sizeof(INTERNET_ASYNC_RESULT));
797 return rc;
800 static void AsyncHttpEndRequestProc(WORKREQUEST *work)
802 struct WORKREQ_HTTPENDREQUESTW const *req = &work->u.HttpEndRequestW;
803 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)work->hdr;
805 TRACE("%p\n", lpwhr);
807 HTTP_HttpEndRequestW(lpwhr, req->dwFlags, req->dwContext);
810 /***********************************************************************
811 * HttpEndRequestW (WININET.@)
813 * Ends an HTTP request that was started by HttpSendRequestEx
815 * RETURNS
816 * TRUE if successful
817 * FALSE on failure
820 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
821 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD_PTR dwContext)
823 BOOL rc = FALSE;
824 LPWININETHTTPREQW lpwhr;
826 TRACE("-->\n");
828 if (lpBuffersOut)
830 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
831 return FALSE;
834 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
836 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
838 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
839 if (lpwhr)
840 WININET_Release( &lpwhr->hdr );
841 return FALSE;
843 lpwhr->hdr.dwFlags |= dwFlags;
845 if (lpwhr->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
847 WORKREQUEST work;
848 struct WORKREQ_HTTPENDREQUESTW *request;
850 work.asyncproc = AsyncHttpEndRequestProc;
851 work.hdr = WININET_AddRef( &lpwhr->hdr );
853 request = &work.u.HttpEndRequestW;
854 request->dwFlags = dwFlags;
855 request->dwContext = dwContext;
857 INTERNET_AsyncCall(&work);
858 INTERNET_SetLastError(ERROR_IO_PENDING);
860 else
861 rc = HTTP_HttpEndRequestW(lpwhr, dwFlags, dwContext);
863 WININET_Release( &lpwhr->hdr );
864 TRACE("%i <--\n",rc);
865 return rc;
868 /***********************************************************************
869 * HttpOpenRequestW (WININET.@)
871 * Open a HTTP request handle
873 * RETURNS
874 * HINTERNET a HTTP request handle on success
875 * NULL on failure
878 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
879 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
880 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
881 DWORD dwFlags, DWORD_PTR dwContext)
883 LPWININETHTTPSESSIONW lpwhs;
884 HINTERNET handle = NULL;
886 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
887 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
888 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
889 dwFlags, dwContext);
890 if(lpszAcceptTypes!=NULL)
892 int i;
893 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
894 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
897 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
898 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
900 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
901 goto lend;
905 * My tests seem to show that the windows version does not
906 * become asynchronous until after this point. And anyhow
907 * if this call was asynchronous then how would you get the
908 * necessary HINTERNET pointer returned by this function.
911 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
912 lpszVersion, lpszReferrer, lpszAcceptTypes,
913 dwFlags, dwContext);
914 lend:
915 if( lpwhs )
916 WININET_Release( &lpwhs->hdr );
917 TRACE("returning %p\n", handle);
918 return handle;
922 /***********************************************************************
923 * HttpOpenRequestA (WININET.@)
925 * Open a HTTP request handle
927 * RETURNS
928 * HINTERNET a HTTP request handle on success
929 * NULL on failure
932 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
933 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
934 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
935 DWORD dwFlags, DWORD_PTR dwContext)
937 LPWSTR szVerb = NULL, szObjectName = NULL;
938 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
939 INT len, acceptTypesCount;
940 HINTERNET rc = FALSE;
941 LPCSTR *types;
943 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession,
944 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
945 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
946 dwFlags, dwContext);
948 if (lpszVerb)
950 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
951 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
952 if ( !szVerb )
953 goto end;
954 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
957 if (lpszObjectName)
959 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
960 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
961 if ( !szObjectName )
962 goto end;
963 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
966 if (lpszVersion)
968 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
969 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
970 if ( !szVersion )
971 goto end;
972 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
975 if (lpszReferrer)
977 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
978 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
979 if ( !szReferrer )
980 goto end;
981 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
984 if (lpszAcceptTypes)
986 acceptTypesCount = 0;
987 types = lpszAcceptTypes;
988 while (*types)
990 __TRY
992 /* find out how many there are */
993 if (*types && **types)
995 TRACE("accept type: %s\n", debugstr_a(*types));
996 acceptTypesCount++;
999 __EXCEPT_PAGE_FAULT
1001 WARN("invalid accept type pointer\n");
1003 __ENDTRY;
1004 types++;
1006 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
1007 if (!szAcceptTypes) goto end;
1009 acceptTypesCount = 0;
1010 types = lpszAcceptTypes;
1011 while (*types)
1013 __TRY
1015 if (*types && **types)
1017 len = MultiByteToWideChar(CP_ACP, 0, *types, -1, NULL, 0 );
1018 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
1020 MultiByteToWideChar(CP_ACP, 0, *types, -1, szAcceptTypes[acceptTypesCount], len);
1021 acceptTypesCount++;
1024 __EXCEPT_PAGE_FAULT
1026 /* ignore invalid pointer */
1028 __ENDTRY;
1029 types++;
1031 szAcceptTypes[acceptTypesCount] = NULL;
1034 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1035 szVersion, szReferrer,
1036 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1038 end:
1039 if (szAcceptTypes)
1041 acceptTypesCount = 0;
1042 while (szAcceptTypes[acceptTypesCount])
1044 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1045 acceptTypesCount++;
1047 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1049 HeapFree(GetProcessHeap(), 0, szReferrer);
1050 HeapFree(GetProcessHeap(), 0, szVersion);
1051 HeapFree(GetProcessHeap(), 0, szObjectName);
1052 HeapFree(GetProcessHeap(), 0, szVerb);
1054 return rc;
1057 /***********************************************************************
1058 * HTTP_EncodeBase64
1060 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1062 UINT n = 0, x;
1063 static const CHAR HTTP_Base64Enc[] =
1064 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1066 while( len > 0 )
1068 /* first 6 bits, all from bin[0] */
1069 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1070 x = (bin[0] & 3) << 4;
1072 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1073 if( len == 1 )
1075 base64[n++] = HTTP_Base64Enc[x];
1076 base64[n++] = '=';
1077 base64[n++] = '=';
1078 break;
1080 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1081 x = ( bin[1] & 0x0f ) << 2;
1083 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1084 if( len == 2 )
1086 base64[n++] = HTTP_Base64Enc[x];
1087 base64[n++] = '=';
1088 break;
1090 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1092 /* last 6 bits, all from bin [2] */
1093 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1094 bin += 3;
1095 len -= 3;
1097 base64[n] = 0;
1098 return n;
1101 #define CH(x) (((x) >= 'A' && (x) <= 'Z') ? (x) - 'A' : \
1102 ((x) >= 'a' && (x) <= 'z') ? (x) - 'a' + 26 : \
1103 ((x) >= '0' && (x) <= '9') ? (x) - '0' + 52 : \
1104 ((x) == '+') ? 62 : ((x) == '/') ? 63 : -1)
1105 static const signed char HTTP_Base64Dec[256] =
1107 CH( 0),CH( 1),CH( 2),CH( 3),CH( 4),CH( 5),CH( 6),CH( 7),CH( 8),CH( 9),
1108 CH(10),CH(11),CH(12),CH(13),CH(14),CH(15),CH(16),CH(17),CH(18),CH(19),
1109 CH(20),CH(21),CH(22),CH(23),CH(24),CH(25),CH(26),CH(27),CH(28),CH(29),
1110 CH(30),CH(31),CH(32),CH(33),CH(34),CH(35),CH(36),CH(37),CH(38),CH(39),
1111 CH(40),CH(41),CH(42),CH(43),CH(44),CH(45),CH(46),CH(47),CH(48),CH(49),
1112 CH(50),CH(51),CH(52),CH(53),CH(54),CH(55),CH(56),CH(57),CH(58),CH(59),
1113 CH(60),CH(61),CH(62),CH(63),CH(64),CH(65),CH(66),CH(67),CH(68),CH(69),
1114 CH(70),CH(71),CH(72),CH(73),CH(74),CH(75),CH(76),CH(77),CH(78),CH(79),
1115 CH(80),CH(81),CH(82),CH(83),CH(84),CH(85),CH(86),CH(87),CH(88),CH(89),
1116 CH(90),CH(91),CH(92),CH(93),CH(94),CH(95),CH(96),CH(97),CH(98),CH(99),
1117 CH(100),CH(101),CH(102),CH(103),CH(104),CH(105),CH(106),CH(107),CH(108),CH(109),
1118 CH(110),CH(111),CH(112),CH(113),CH(114),CH(115),CH(116),CH(117),CH(118),CH(119),
1119 CH(120),CH(121),CH(122),CH(123),CH(124),CH(125),CH(126),CH(127),CH(128),CH(129),
1120 CH(130),CH(131),CH(132),CH(133),CH(134),CH(135),CH(136),CH(137),CH(138),CH(139),
1121 CH(140),CH(141),CH(142),CH(143),CH(144),CH(145),CH(146),CH(147),CH(148),CH(149),
1122 CH(150),CH(151),CH(152),CH(153),CH(154),CH(155),CH(156),CH(157),CH(158),CH(159),
1123 CH(160),CH(161),CH(162),CH(163),CH(164),CH(165),CH(166),CH(167),CH(168),CH(169),
1124 CH(170),CH(171),CH(172),CH(173),CH(174),CH(175),CH(176),CH(177),CH(178),CH(179),
1125 CH(180),CH(181),CH(182),CH(183),CH(184),CH(185),CH(186),CH(187),CH(188),CH(189),
1126 CH(190),CH(191),CH(192),CH(193),CH(194),CH(195),CH(196),CH(197),CH(198),CH(199),
1127 CH(200),CH(201),CH(202),CH(203),CH(204),CH(205),CH(206),CH(207),CH(208),CH(209),
1128 CH(210),CH(211),CH(212),CH(213),CH(214),CH(215),CH(216),CH(217),CH(218),CH(219),
1129 CH(220),CH(221),CH(222),CH(223),CH(224),CH(225),CH(226),CH(227),CH(228),CH(229),
1130 CH(230),CH(231),CH(232),CH(233),CH(234),CH(235),CH(236),CH(237),CH(238),CH(239),
1131 CH(240),CH(241),CH(242),CH(243),CH(244),CH(245),CH(246),CH(247),CH(248), CH(249),
1132 CH(250),CH(251),CH(252),CH(253),CH(254),CH(255),
1134 #undef CH
1136 /***********************************************************************
1137 * HTTP_DecodeBase64
1139 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1141 unsigned int n = 0;
1143 while(*base64)
1145 signed char in[4];
1147 if (base64[0] >= ARRAYSIZE(HTTP_Base64Dec) ||
1148 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1149 base64[1] >= ARRAYSIZE(HTTP_Base64Dec) ||
1150 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1152 WARN("invalid base64: %s\n", debugstr_w(base64));
1153 return 0;
1155 if (bin)
1156 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1157 n++;
1159 if ((base64[2] == '=') && (base64[3] == '='))
1160 break;
1161 if (base64[2] > ARRAYSIZE(HTTP_Base64Dec) ||
1162 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1164 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1165 return 0;
1167 if (bin)
1168 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1169 n++;
1171 if (base64[3] == '=')
1172 break;
1173 if (base64[3] > ARRAYSIZE(HTTP_Base64Dec) ||
1174 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1176 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1177 return 0;
1179 if (bin)
1180 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1181 n++;
1183 base64 += 4;
1186 return n;
1189 /***********************************************************************
1190 * HTTP_InsertAuthorization
1192 * Insert or delete the authorization field in the request header.
1194 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr, struct HttpAuthInfo *pAuthInfo, LPCWSTR header )
1196 if (pAuthInfo)
1198 static const WCHAR wszSpace[] = {' ',0};
1199 static const WCHAR wszBasic[] = {'B','a','s','i','c',0};
1200 unsigned int len;
1201 WCHAR *authorization = NULL;
1203 if (pAuthInfo->auth_data_len)
1205 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1206 len = strlenW(pAuthInfo->scheme)+1+((pAuthInfo->auth_data_len+2)*4)/3;
1207 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1208 if (!authorization)
1209 return FALSE;
1211 strcpyW(authorization, pAuthInfo->scheme);
1212 strcatW(authorization, wszSpace);
1213 HTTP_EncodeBase64(pAuthInfo->auth_data,
1214 pAuthInfo->auth_data_len,
1215 authorization+strlenW(authorization));
1217 /* clear the data as it isn't valid now that it has been sent to the
1218 * server, unless it's Basic authentication which doesn't do
1219 * connection tracking */
1220 if (strcmpiW(pAuthInfo->scheme, wszBasic))
1222 HeapFree(GetProcessHeap(), 0, pAuthInfo->auth_data);
1223 pAuthInfo->auth_data = NULL;
1224 pAuthInfo->auth_data_len = 0;
1228 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1230 HTTP_ProcessHeader(lpwhr, header, authorization, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
1232 HeapFree(GetProcessHeap(), 0, authorization);
1234 return TRUE;
1237 static WCHAR *HTTP_BuildProxyRequestUrl(WININETHTTPREQW *req)
1239 WCHAR new_location[INTERNET_MAX_URL_LENGTH], *url;
1240 DWORD size;
1242 size = sizeof(new_location);
1243 if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_LOCATION, new_location, &size, NULL))
1245 if (!(url = HeapAlloc( GetProcessHeap(), 0, size + sizeof(WCHAR) ))) return NULL;
1246 strcpyW( url, new_location );
1248 else
1250 static const WCHAR slash[] = { '/',0 };
1251 static const WCHAR format[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1252 static const WCHAR formatSSL[] = { 'h','t','t','p','s',':','/','/','%','s',':','%','d',0 };
1253 WININETHTTPSESSIONW *session = req->lpHttpSession;
1255 size = 16; /* "https://" + sizeof(port#) + ":/\0" */
1256 size += strlenW( session->lpszHostName ) + strlenW( req->lpszPath );
1258 if (!(url = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) ))) return NULL;
1260 if (req->hdr.dwFlags & INTERNET_FLAG_SECURE)
1261 sprintfW( url, formatSSL, session->lpszHostName, session->nHostPort );
1262 else
1263 sprintfW( url, format, session->lpszHostName, session->nHostPort );
1264 if (req->lpszPath[0] != '/') strcatW( url, slash );
1265 strcatW( url, req->lpszPath );
1267 TRACE("url=%s\n", debugstr_w(url));
1268 return url;
1271 /***********************************************************************
1272 * HTTP_DealWithProxy
1274 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1275 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1277 WCHAR buf[MAXHOSTNAME];
1278 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1279 static WCHAR szNul[] = { 0 };
1280 URL_COMPONENTSW UrlComponents;
1281 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 };
1282 static const WCHAR szFormat[] = { 'h','t','t','p',':','/','/','%','s',0 };
1284 memset( &UrlComponents, 0, sizeof UrlComponents );
1285 UrlComponents.dwStructSize = sizeof UrlComponents;
1286 UrlComponents.lpszHostName = buf;
1287 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1289 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1290 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1291 sprintfW(proxy, szFormat, hIC->lpszProxy);
1292 else
1293 strcpyW(proxy, hIC->lpszProxy);
1294 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1295 return FALSE;
1296 if( UrlComponents.dwHostNameLength == 0 )
1297 return FALSE;
1299 if( !lpwhr->lpszPath )
1300 lpwhr->lpszPath = szNul;
1302 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1303 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1305 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1306 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1307 lpwhs->nServerPort = UrlComponents.nPort;
1309 TRACE("proxy server=%s port=%d\n", debugstr_w(lpwhs->lpszServerName), lpwhs->nServerPort);
1310 return TRUE;
1313 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1315 char szaddr[32];
1316 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1318 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1319 INTERNET_STATUS_RESOLVING_NAME,
1320 lpwhs->lpszServerName,
1321 strlenW(lpwhs->lpszServerName)+1);
1323 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1324 &lpwhs->socketAddress))
1326 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1327 return FALSE;
1330 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1331 szaddr, sizeof(szaddr));
1332 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1333 INTERNET_STATUS_NAME_RESOLVED,
1334 szaddr, strlen(szaddr)+1);
1336 TRACE("resolved %s to %s\n", debugstr_w(lpwhs->lpszServerName), szaddr);
1337 return TRUE;
1341 /***********************************************************************
1342 * HTTPREQ_Destroy (internal)
1344 * Deallocate request handle
1347 static void HTTPREQ_Destroy(WININETHANDLEHEADER *hdr)
1349 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1350 DWORD i;
1352 TRACE("\n");
1354 if(lpwhr->hCacheFile)
1355 CloseHandle(lpwhr->hCacheFile);
1357 if(lpwhr->lpszCacheFile) {
1358 DeleteFileW(lpwhr->lpszCacheFile); /* FIXME */
1359 HeapFree(GetProcessHeap(), 0, lpwhr->lpszCacheFile);
1362 WININET_Release(&lpwhr->lpHttpSession->hdr);
1364 if (lpwhr->pAuthInfo)
1366 if (SecIsValidHandle(&lpwhr->pAuthInfo->ctx))
1367 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
1368 if (SecIsValidHandle(&lpwhr->pAuthInfo->cred))
1369 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
1371 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1372 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
1373 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
1374 lpwhr->pAuthInfo = NULL;
1377 if (lpwhr->pProxyAuthInfo)
1379 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->ctx))
1380 DeleteSecurityContext(&lpwhr->pProxyAuthInfo->ctx);
1381 if (SecIsValidHandle(&lpwhr->pProxyAuthInfo->cred))
1382 FreeCredentialsHandle(&lpwhr->pProxyAuthInfo->cred);
1384 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->auth_data);
1385 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo->scheme);
1386 HeapFree(GetProcessHeap(), 0, lpwhr->pProxyAuthInfo);
1387 lpwhr->pProxyAuthInfo = NULL;
1390 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1391 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
1392 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
1393 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
1394 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
1396 for (i = 0; i < lpwhr->nCustHeaders; i++)
1398 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
1399 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
1402 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
1403 HeapFree(GetProcessHeap(), 0, lpwhr);
1406 static void HTTPREQ_CloseConnection(WININETHANDLEHEADER *hdr)
1408 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
1410 TRACE("%p\n",lpwhr);
1412 if (!NETCON_connected(&lpwhr->netConnection))
1413 return;
1415 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1416 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
1418 NETCON_close(&lpwhr->netConnection);
1420 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1421 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
1424 static DWORD HTTPREQ_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
1426 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1428 switch(option) {
1429 case INTERNET_OPTION_HANDLE_TYPE:
1430 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
1432 if (*size < sizeof(ULONG))
1433 return ERROR_INSUFFICIENT_BUFFER;
1435 *size = sizeof(DWORD);
1436 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_HTTP_REQUEST;
1437 return ERROR_SUCCESS;
1439 case INTERNET_OPTION_URL: {
1440 WCHAR url[INTERNET_MAX_URL_LENGTH];
1441 HTTPHEADERW *host;
1442 DWORD len;
1443 WCHAR *pch;
1445 static const WCHAR httpW[] = {'h','t','t','p',':','/','/',0};
1446 static const WCHAR hostW[] = {'H','o','s','t',0};
1448 TRACE("INTERNET_OPTION_URL\n");
1450 host = HTTP_GetHeader(req, hostW);
1451 strcpyW(url, httpW);
1452 strcatW(url, host->lpszValue);
1453 if (NULL != (pch = strchrW(url + strlenW(httpW), ':')))
1454 *pch = 0;
1455 strcatW(url, req->lpszPath);
1457 TRACE("INTERNET_OPTION_URL: %s\n",debugstr_w(url));
1459 if(unicode) {
1460 len = (strlenW(url)+1) * sizeof(WCHAR);
1461 if(*size < len)
1462 return ERROR_INSUFFICIENT_BUFFER;
1464 *size = len;
1465 strcpyW(buffer, url);
1466 return ERROR_SUCCESS;
1467 }else {
1468 len = WideCharToMultiByte(CP_ACP, 0, url, -1, buffer, *size, NULL, NULL);
1469 if(len > *size)
1470 return ERROR_INSUFFICIENT_BUFFER;
1472 *size = len;
1473 return ERROR_SUCCESS;
1477 case INTERNET_OPTION_DATAFILE_NAME: {
1478 DWORD req_size;
1480 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
1482 if(!req->lpszCacheFile) {
1483 *size = 0;
1484 return ERROR_INTERNET_ITEM_NOT_FOUND;
1487 if(unicode) {
1488 req_size = (lstrlenW(req->lpszCacheFile)+1) * sizeof(WCHAR);
1489 if(*size < req_size)
1490 return ERROR_INSUFFICIENT_BUFFER;
1492 *size = req_size;
1493 memcpy(buffer, req->lpszCacheFile, *size);
1494 return ERROR_SUCCESS;
1495 }else {
1496 req_size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile, -1, NULL, 0, NULL, NULL);
1497 if (req_size > *size)
1498 return ERROR_INSUFFICIENT_BUFFER;
1500 *size = WideCharToMultiByte(CP_ACP, 0, req->lpszCacheFile,
1501 -1, buffer, *size, NULL, NULL);
1502 return ERROR_SUCCESS;
1506 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT: {
1507 PCCERT_CONTEXT context;
1509 if(*size < sizeof(INTERNET_CERTIFICATE_INFOW)) {
1510 *size = sizeof(INTERNET_CERTIFICATE_INFOW);
1511 return ERROR_INSUFFICIENT_BUFFER;
1514 context = (PCCERT_CONTEXT)NETCON_GetCert(&(req->netConnection));
1515 if(context) {
1516 INTERNET_CERTIFICATE_INFOW *info = (INTERNET_CERTIFICATE_INFOW*)buffer;
1517 DWORD len;
1519 memset(info, 0, sizeof(INTERNET_CERTIFICATE_INFOW));
1520 info->ftExpiry = context->pCertInfo->NotAfter;
1521 info->ftStart = context->pCertInfo->NotBefore;
1522 if(unicode) {
1523 len = CertNameToStrW(context->dwCertEncodingType,
1524 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1525 info->lpszSubjectInfo = LocalAlloc(0, len*sizeof(WCHAR));
1526 if(info->lpszSubjectInfo)
1527 CertNameToStrW(context->dwCertEncodingType,
1528 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1529 info->lpszSubjectInfo, len);
1530 len = CertNameToStrW(context->dwCertEncodingType,
1531 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1532 info->lpszIssuerInfo = LocalAlloc(0, len*sizeof(WCHAR));
1533 if (info->lpszIssuerInfo)
1534 CertNameToStrW(context->dwCertEncodingType,
1535 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1536 info->lpszIssuerInfo, len);
1537 }else {
1538 INTERNET_CERTIFICATE_INFOA *infoA = (INTERNET_CERTIFICATE_INFOA*)info;
1540 len = CertNameToStrA(context->dwCertEncodingType,
1541 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR, NULL, 0);
1542 infoA->lpszSubjectInfo = LocalAlloc(0, len);
1543 if(infoA->lpszSubjectInfo)
1544 CertNameToStrA(context->dwCertEncodingType,
1545 &context->pCertInfo->Subject, CERT_SIMPLE_NAME_STR,
1546 infoA->lpszSubjectInfo, len);
1547 len = CertNameToStrA(context->dwCertEncodingType,
1548 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR, NULL, 0);
1549 infoA->lpszIssuerInfo = LocalAlloc(0, len);
1550 if(infoA->lpszIssuerInfo)
1551 CertNameToStrA(context->dwCertEncodingType,
1552 &context->pCertInfo->Issuer, CERT_SIMPLE_NAME_STR,
1553 infoA->lpszIssuerInfo, len);
1557 * Contrary to MSDN, these do not appear to be set.
1558 * lpszProtocolName
1559 * lpszSignatureAlgName
1560 * lpszEncryptionAlgName
1561 * dwKeySize
1563 CertFreeCertificateContext(context);
1564 return ERROR_SUCCESS;
1569 return INET_QueryOption(option, buffer, size, unicode);
1572 static DWORD HTTPREQ_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
1574 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1576 switch(option) {
1577 case INTERNET_OPTION_SEND_TIMEOUT:
1578 case INTERNET_OPTION_RECEIVE_TIMEOUT:
1579 TRACE("INTERNET_OPTION_SEND/RECEIVE_TIMEOUT\n");
1581 if (size != sizeof(DWORD))
1582 return ERROR_INVALID_PARAMETER;
1584 return NETCON_set_timeout(&req->netConnection, option == INTERNET_OPTION_SEND_TIMEOUT,
1585 *(DWORD*)buffer);
1587 case INTERNET_OPTION_USERNAME:
1588 HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszUserName);
1589 if (!(req->lpHttpSession->lpszUserName = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1590 return ERROR_SUCCESS;
1592 case INTERNET_OPTION_PASSWORD:
1593 HeapFree(GetProcessHeap(), 0, req->lpHttpSession->lpszPassword);
1594 if (!(req->lpHttpSession->lpszPassword = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
1595 return ERROR_SUCCESS;
1598 return ERROR_INTERNET_INVALID_OPTION;
1601 static void HTTP_ReceiveRequestData(WININETHTTPREQW *req, BOOL first_notif)
1603 INTERNET_ASYNC_RESULT iar;
1604 BYTE buffer[4096];
1605 int available;
1606 BOOL res;
1608 TRACE("%p\n", req);
1610 res = NETCON_recv(&req->netConnection, buffer,
1611 min(sizeof(buffer), req->dwContentLength - req->dwContentRead),
1612 MSG_PEEK, &available);
1614 if(res) {
1615 iar.dwResult = (DWORD_PTR)req->hdr.hInternet;
1616 iar.dwError = first_notif ? 0 : available;
1617 }else {
1618 iar.dwResult = 0;
1619 iar.dwError = INTERNET_GetLastError();
1622 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1623 sizeof(INTERNET_ASYNC_RESULT));
1626 static DWORD HTTP_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1628 int bytes_read;
1630 if(!NETCON_recv(&req->netConnection, buffer, min(size, req->dwContentLength - req->dwContentRead),
1631 sync ? MSG_WAITALL : 0, &bytes_read)) {
1632 if(req->dwContentLength != -1 && req->dwContentRead != req->dwContentLength)
1633 ERR("not all data received %d/%d\n", req->dwContentRead, req->dwContentLength);
1635 /* always return success, even if the network layer returns an error */
1636 *read = 0;
1637 HTTP_FinishedReading(req);
1638 return ERROR_SUCCESS;
1641 req->dwContentRead += bytes_read;
1642 *read = bytes_read;
1644 if(req->lpszCacheFile) {
1645 BOOL res;
1646 DWORD dwBytesWritten;
1648 res = WriteFile(req->hCacheFile, buffer, bytes_read, &dwBytesWritten, NULL);
1649 if(!res)
1650 WARN("WriteFile failed: %u\n", GetLastError());
1653 if(!bytes_read && (req->dwContentRead == req->dwContentLength))
1654 HTTP_FinishedReading(req);
1656 return ERROR_SUCCESS;
1659 static DWORD get_chunk_size(const char *buffer)
1661 const char *p;
1662 DWORD size = 0;
1664 for (p = buffer; *p; p++)
1666 if (*p >= '0' && *p <= '9') size = size * 16 + *p - '0';
1667 else if (*p >= 'a' && *p <= 'f') size = size * 16 + *p - 'a' + 10;
1668 else if (*p >= 'A' && *p <= 'F') size = size * 16 + *p - 'A' + 10;
1669 else if (*p == ';') break;
1671 return size;
1674 static DWORD HTTP_ReadChunked(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1676 char reply[MAX_REPLY_LEN], *p = buffer;
1677 DWORD buflen, to_read, to_write = size;
1678 int bytes_read;
1680 *read = 0;
1681 for (;;)
1683 if (*read == size) break;
1685 if (req->dwContentLength == ~0u) /* new chunk */
1687 buflen = sizeof(reply);
1688 if (!NETCON_getNextLine(&req->netConnection, reply, &buflen)) break;
1690 if (!(req->dwContentLength = get_chunk_size(reply)))
1692 /* zero sized chunk marks end of transfer; read any trailing headers and return */
1693 HTTP_GetResponseHeaders(req, FALSE);
1694 break;
1697 to_read = min(to_write, req->dwContentLength - req->dwContentRead);
1699 if (!NETCON_recv(&req->netConnection, p, to_read, sync ? MSG_WAITALL : 0, &bytes_read))
1701 if (bytes_read != to_read)
1702 ERR("Not all data received %d/%d\n", bytes_read, to_read);
1704 /* always return success, even if the network layer returns an error */
1705 *read = 0;
1706 break;
1708 if (!bytes_read) break;
1710 req->dwContentRead += bytes_read;
1711 to_write -= bytes_read;
1712 *read += bytes_read;
1714 if (req->lpszCacheFile)
1716 DWORD dwBytesWritten;
1718 if (!WriteFile(req->hCacheFile, p, bytes_read, &dwBytesWritten, NULL))
1719 WARN("WriteFile failed: %u\n", GetLastError());
1721 p += bytes_read;
1723 if (req->dwContentRead == req->dwContentLength) /* chunk complete */
1725 req->dwContentRead = 0;
1726 req->dwContentLength = ~0u;
1728 buflen = sizeof(reply);
1729 if (!NETCON_getNextLine(&req->netConnection, reply, &buflen))
1731 ERR("Malformed chunk\n");
1732 *read = 0;
1733 break;
1737 if (!*read) HTTP_FinishedReading(req);
1738 return ERROR_SUCCESS;
1741 static DWORD HTTPREQ_Read(WININETHTTPREQW *req, void *buffer, DWORD size, DWORD *read, BOOL sync)
1743 WCHAR encoding[20];
1744 DWORD buflen = sizeof(encoding);
1745 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
1747 if (HTTP_HttpQueryInfoW(req, HTTP_QUERY_TRANSFER_ENCODING, encoding, &buflen, NULL) &&
1748 !strcmpiW(encoding, szChunked))
1750 return HTTP_ReadChunked(req, buffer, size, read, sync);
1752 else
1753 return HTTP_Read(req, buffer, size, read, sync);
1756 static DWORD HTTPREQ_ReadFile(WININETHANDLEHEADER *hdr, void *buffer, DWORD size, DWORD *read)
1758 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1759 return HTTPREQ_Read(req, buffer, size, read, TRUE);
1762 static void HTTPREQ_AsyncReadFileExAProc(WORKREQUEST *workRequest)
1764 struct WORKREQ_INTERNETREADFILEEXA const *data = &workRequest->u.InternetReadFileExA;
1765 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1766 INTERNET_ASYNC_RESULT iar;
1767 DWORD res;
1769 TRACE("INTERNETREADFILEEXA %p\n", workRequest->hdr);
1771 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1772 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1774 iar.dwResult = res == ERROR_SUCCESS;
1775 iar.dwError = res;
1777 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1778 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1779 sizeof(INTERNET_ASYNC_RESULT));
1782 static DWORD HTTPREQ_ReadFileExA(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSA *buffers,
1783 DWORD flags, DWORD_PTR context)
1786 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1787 DWORD res;
1789 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1790 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1792 if (buffers->dwStructSize != sizeof(*buffers))
1793 return ERROR_INVALID_PARAMETER;
1795 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1797 if (hdr->dwFlags & INTERNET_FLAG_ASYNC) {
1798 DWORD available = 0;
1800 NETCON_query_data_available(&req->netConnection, &available);
1801 if (!available)
1803 WORKREQUEST workRequest;
1805 workRequest.asyncproc = HTTPREQ_AsyncReadFileExAProc;
1806 workRequest.hdr = WININET_AddRef(&req->hdr);
1807 workRequest.u.InternetReadFileExA.lpBuffersOut = buffers;
1809 INTERNET_AsyncCall(&workRequest);
1811 return ERROR_IO_PENDING;
1815 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1816 !(flags & IRF_NO_WAIT));
1818 if (res == ERROR_SUCCESS) {
1819 DWORD size = buffers->dwBufferLength;
1820 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1821 &size, sizeof(size));
1824 return res;
1827 static void HTTPREQ_AsyncReadFileExWProc(WORKREQUEST *workRequest)
1829 struct WORKREQ_INTERNETREADFILEEXW const *data = &workRequest->u.InternetReadFileExW;
1830 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1831 INTERNET_ASYNC_RESULT iar;
1832 DWORD res;
1834 TRACE("INTERNETREADFILEEXW %p\n", workRequest->hdr);
1836 res = HTTPREQ_Read(req, data->lpBuffersOut->lpvBuffer,
1837 data->lpBuffersOut->dwBufferLength, &data->lpBuffersOut->dwBufferLength, TRUE);
1839 iar.dwResult = res == ERROR_SUCCESS;
1840 iar.dwError = res;
1842 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext,
1843 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1844 sizeof(INTERNET_ASYNC_RESULT));
1847 static DWORD HTTPREQ_ReadFileExW(WININETHANDLEHEADER *hdr, INTERNET_BUFFERSW *buffers,
1848 DWORD flags, DWORD_PTR context)
1851 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1852 DWORD res;
1854 if (flags & ~(IRF_ASYNC|IRF_NO_WAIT))
1855 FIXME("these dwFlags aren't implemented: 0x%x\n", flags & ~(IRF_ASYNC|IRF_NO_WAIT));
1857 if (buffers->dwStructSize != sizeof(*buffers))
1858 return ERROR_INVALID_PARAMETER;
1860 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1862 if (hdr->dwFlags & INTERNET_FLAG_ASYNC) {
1863 DWORD available = 0;
1865 NETCON_query_data_available(&req->netConnection, &available);
1866 if (!available)
1868 WORKREQUEST workRequest;
1870 workRequest.asyncproc = HTTPREQ_AsyncReadFileExWProc;
1871 workRequest.hdr = WININET_AddRef(&req->hdr);
1872 workRequest.u.InternetReadFileExW.lpBuffersOut = buffers;
1874 INTERNET_AsyncCall(&workRequest);
1876 return ERROR_IO_PENDING;
1880 res = HTTPREQ_Read(req, buffers->lpvBuffer, buffers->dwBufferLength, &buffers->dwBufferLength,
1881 !(flags & IRF_NO_WAIT));
1883 if (res == ERROR_SUCCESS) {
1884 DWORD size = buffers->dwBufferLength;
1885 INTERNET_SendCallback(&req->hdr, req->hdr.dwContext, INTERNET_STATUS_RESPONSE_RECEIVED,
1886 &size, sizeof(size));
1889 return res;
1892 static BOOL HTTPREQ_WriteFile(WININETHANDLEHEADER *hdr, const void *buffer, DWORD size, DWORD *written)
1894 BOOL ret;
1895 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW)hdr;
1897 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1899 *written = 0;
1900 if ((ret = NETCON_send(&lpwhr->netConnection, buffer, size, 0, (LPINT)written)))
1901 lpwhr->dwBytesWritten += *written;
1903 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REQUEST_SENT, written, sizeof(DWORD));
1904 return ret;
1907 static void HTTPREQ_AsyncQueryDataAvailableProc(WORKREQUEST *workRequest)
1909 WININETHTTPREQW *req = (WININETHTTPREQW*)workRequest->hdr;
1911 HTTP_ReceiveRequestData(req, FALSE);
1914 static DWORD HTTPREQ_QueryDataAvailable(WININETHANDLEHEADER *hdr, DWORD *available, DWORD flags, DWORD_PTR ctx)
1916 WININETHTTPREQW *req = (WININETHTTPREQW*)hdr;
1917 BYTE buffer[4048];
1918 BOOL async;
1920 TRACE("(%p %p %x %lx)\n", req, available, flags, ctx);
1922 if(!NETCON_query_data_available(&req->netConnection, available) || *available)
1923 return ERROR_SUCCESS;
1925 /* Even if we are in async mode, we need to determine whether
1926 * there is actually more data available. We do this by trying
1927 * to peek only a single byte in async mode. */
1928 async = (req->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC) != 0;
1930 if (NETCON_recv(&req->netConnection, buffer,
1931 min(async ? 1 : sizeof(buffer), req->dwContentLength - req->dwContentRead),
1932 MSG_PEEK, (int *)available) && async && *available)
1934 WORKREQUEST workRequest;
1936 *available = 0;
1937 workRequest.asyncproc = HTTPREQ_AsyncQueryDataAvailableProc;
1938 workRequest.hdr = WININET_AddRef( &req->hdr );
1940 INTERNET_AsyncCall(&workRequest);
1942 return ERROR_IO_PENDING;
1945 return ERROR_SUCCESS;
1948 static const HANDLEHEADERVtbl HTTPREQVtbl = {
1949 HTTPREQ_Destroy,
1950 HTTPREQ_CloseConnection,
1951 HTTPREQ_QueryOption,
1952 HTTPREQ_SetOption,
1953 HTTPREQ_ReadFile,
1954 HTTPREQ_ReadFileExA,
1955 HTTPREQ_ReadFileExW,
1956 HTTPREQ_WriteFile,
1957 HTTPREQ_QueryDataAvailable,
1958 NULL
1961 /***********************************************************************
1962 * HTTP_HttpOpenRequestW (internal)
1964 * Open a HTTP request handle
1966 * RETURNS
1967 * HINTERNET a HTTP request handle on success
1968 * NULL on failure
1971 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1972 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1973 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1974 DWORD dwFlags, DWORD_PTR dwContext)
1976 LPWININETAPPINFOW hIC = NULL;
1977 LPWININETHTTPREQW lpwhr;
1978 LPWSTR lpszHostName = NULL;
1979 HINTERNET handle = NULL;
1980 static const WCHAR szHostForm[] = {'%','s',':','%','u',0};
1981 DWORD len;
1983 TRACE("-->\n");
1985 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1986 hIC = lpwhs->lpAppInfo;
1988 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1989 if (NULL == lpwhr)
1991 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1992 goto lend;
1994 lpwhr->hdr.htype = WH_HHTTPREQ;
1995 lpwhr->hdr.vtbl = &HTTPREQVtbl;
1996 lpwhr->hdr.dwFlags = dwFlags;
1997 lpwhr->hdr.dwContext = dwContext;
1998 lpwhr->hdr.refs = 1;
1999 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
2000 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
2002 WININET_AddRef( &lpwhs->hdr );
2003 lpwhr->lpHttpSession = lpwhs;
2004 list_add_head( &lpwhs->hdr.children, &lpwhr->hdr.entry );
2006 lpszHostName = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) *
2007 (strlenW(lpwhs->lpszHostName) + 7 /* length of ":65535" + 1 */));
2008 if (NULL == lpszHostName)
2010 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2011 goto lend;
2014 handle = WININET_AllocHandle( &lpwhr->hdr );
2015 if (NULL == handle)
2017 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2018 goto lend;
2021 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
2023 InternetCloseHandle( handle );
2024 handle = NULL;
2025 goto lend;
2028 if (lpszObjectName && *lpszObjectName) {
2029 HRESULT rc;
2031 len = 0;
2032 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
2033 if (rc != E_POINTER)
2034 len = strlenW(lpszObjectName)+1;
2035 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2036 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
2037 URL_ESCAPE_SPACES_ONLY);
2038 if (rc != S_OK)
2040 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
2041 strcpyW(lpwhr->lpszPath,lpszObjectName);
2045 if (lpszReferrer && *lpszReferrer)
2046 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2048 if (lpszAcceptTypes)
2050 int i;
2051 for (i = 0; lpszAcceptTypes[i]; i++)
2053 if (!*lpszAcceptTypes[i]) continue;
2054 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
2055 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
2056 HTTP_ADDHDR_FLAG_REQ |
2057 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
2061 lpwhr->lpszVerb = WININET_strdupW(lpszVerb && *lpszVerb ? lpszVerb : szGET);
2063 if (lpszVersion)
2064 lpwhr->lpszVersion = WININET_strdupW(lpszVersion);
2065 else
2066 lpwhr->lpszVersion = WININET_strdupW(g_szHttp1_1);
2068 if (lpwhs->nHostPort != INTERNET_INVALID_PORT_NUMBER &&
2069 lpwhs->nHostPort != INTERNET_DEFAULT_HTTP_PORT &&
2070 lpwhs->nHostPort != INTERNET_DEFAULT_HTTPS_PORT)
2072 sprintfW(lpszHostName, szHostForm, lpwhs->lpszHostName, lpwhs->nHostPort);
2073 HTTP_ProcessHeader(lpwhr, szHost, lpszHostName,
2074 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2076 else
2077 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName,
2078 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REQ);
2080 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
2081 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
2082 INTERNET_DEFAULT_HTTPS_PORT :
2083 INTERNET_DEFAULT_HTTP_PORT);
2085 if (lpwhs->nHostPort == INTERNET_INVALID_PORT_NUMBER)
2086 lpwhs->nHostPort = (dwFlags & INTERNET_FLAG_SECURE ?
2087 INTERNET_DEFAULT_HTTPS_PORT :
2088 INTERNET_DEFAULT_HTTP_PORT);
2090 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2091 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
2093 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
2094 INTERNET_STATUS_HANDLE_CREATED, &handle,
2095 sizeof(handle));
2097 lend:
2098 HeapFree(GetProcessHeap(), 0, lpszHostName);
2099 if( lpwhr )
2100 WININET_Release( &lpwhr->hdr );
2102 TRACE("<-- %p (%p)\n", handle, lpwhr);
2103 return handle;
2106 /* read any content returned by the server so that the connection can be
2107 * reused */
2108 static void HTTP_DrainContent(WININETHTTPREQW *req)
2110 DWORD bytes_read;
2112 if (!NETCON_connected(&req->netConnection)) return;
2114 if (req->dwContentLength == -1)
2115 NETCON_close(&req->netConnection);
2119 char buffer[2048];
2120 if (HTTPREQ_Read(req, buffer, sizeof(buffer), &bytes_read, TRUE) != ERROR_SUCCESS)
2121 return;
2122 } while (bytes_read);
2125 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
2126 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
2127 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
2128 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
2129 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2130 static const WCHAR szAge[] = { 'A','g','e',0 };
2131 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
2132 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
2133 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2134 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
2135 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
2136 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
2137 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
2138 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
2139 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
2140 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
2141 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
2142 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 };
2143 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
2144 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
2145 static const WCHAR szDate[] = { 'D','a','t','e',0 };
2146 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
2147 static const WCHAR szETag[] = { 'E','T','a','g',0 };
2148 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
2149 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2150 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
2151 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2152 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
2153 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
2154 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
2155 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
2156 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
2157 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
2158 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
2159 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
2160 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2161 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
2162 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
2163 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
2164 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
2165 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
2166 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
2167 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
2168 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
2169 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 };
2170 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
2171 static const WCHAR szURI[] = { 'U','R','I',0 };
2172 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
2173 static const WCHAR szVary[] = { 'V','a','r','y',0 };
2174 static const WCHAR szVia[] = { 'V','i','a',0 };
2175 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
2176 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
2178 static const LPCWSTR header_lookup[] = {
2179 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
2180 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
2181 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
2182 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
2183 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
2184 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
2185 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
2186 szAllow, /* HTTP_QUERY_ALLOW = 7 */
2187 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
2188 szDate, /* HTTP_QUERY_DATE = 9 */
2189 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
2190 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
2191 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
2192 szURI, /* HTTP_QUERY_URI = 13 */
2193 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
2194 NULL, /* HTTP_QUERY_COST = 15 */
2195 NULL, /* HTTP_QUERY_LINK = 16 */
2196 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
2197 NULL, /* HTTP_QUERY_VERSION = 18 */
2198 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
2199 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
2200 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
2201 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
2202 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
2203 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
2204 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
2205 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
2206 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
2207 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
2208 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
2209 NULL, /* HTTP_QUERY_FORWARDED = 30 */
2210 NULL, /* HTTP_QUERY_FROM = 31 */
2211 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
2212 szLocation, /* HTTP_QUERY_LOCATION = 33 */
2213 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
2214 szReferer, /* HTTP_QUERY_REFERER = 35 */
2215 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
2216 szServer, /* HTTP_QUERY_SERVER = 37 */
2217 NULL, /* HTTP_TITLE = 38 */
2218 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
2219 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
2220 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
2221 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
2222 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
2223 szCookie, /* HTTP_QUERY_COOKIE = 44 */
2224 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
2225 NULL, /* HTTP_QUERY_REFRESH = 46 */
2226 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
2227 szAge, /* HTTP_QUERY_AGE = 48 */
2228 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
2229 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
2230 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
2231 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
2232 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
2233 szETag, /* HTTP_QUERY_ETAG = 54 */
2234 szHost, /* HTTP_QUERY_HOST = 55 */
2235 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
2236 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
2237 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
2238 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
2239 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
2240 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
2241 szRange, /* HTTP_QUERY_RANGE = 62 */
2242 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
2243 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
2244 szVary, /* HTTP_QUERY_VARY = 65 */
2245 szVia, /* HTTP_QUERY_VIA = 66 */
2246 szWarning, /* HTTP_QUERY_WARNING = 67 */
2247 szExpect, /* HTTP_QUERY_EXPECT = 68 */
2248 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
2249 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
2252 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
2254 /***********************************************************************
2255 * HTTP_HttpQueryInfoW (internal)
2257 static BOOL HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
2258 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2260 LPHTTPHEADERW lphttpHdr = NULL;
2261 BOOL bSuccess = FALSE;
2262 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
2263 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
2264 DWORD level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
2265 INT index = -1;
2267 /* Find requested header structure */
2268 switch (level)
2270 case HTTP_QUERY_CUSTOM:
2271 if (!lpBuffer) return FALSE;
2272 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
2273 break;
2275 case HTTP_QUERY_RAW_HEADERS_CRLF:
2277 LPWSTR headers;
2278 DWORD len = 0;
2279 BOOL ret = FALSE;
2281 if (request_only)
2282 headers = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
2283 else
2284 headers = lpwhr->lpszRawHeaders;
2286 if (headers)
2287 len = strlenW(headers) * sizeof(WCHAR);
2289 if (len + sizeof(WCHAR) > *lpdwBufferLength)
2291 len += sizeof(WCHAR);
2292 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2293 ret = FALSE;
2295 else if (lpBuffer)
2297 if (headers)
2298 memcpy(lpBuffer, headers, len + sizeof(WCHAR));
2299 else
2301 len = strlenW(szCrLf) * sizeof(WCHAR);
2302 memcpy(lpBuffer, szCrLf, sizeof(szCrLf));
2304 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len / sizeof(WCHAR)));
2305 ret = TRUE;
2307 *lpdwBufferLength = len;
2309 if (request_only)
2310 HeapFree(GetProcessHeap(), 0, headers);
2311 return ret;
2313 case HTTP_QUERY_RAW_HEADERS:
2315 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
2316 DWORD i, size = 0;
2317 LPWSTR pszString = lpBuffer;
2319 for (i = 0; ppszRawHeaderLines[i]; i++)
2320 size += strlenW(ppszRawHeaderLines[i]) + 1;
2322 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
2324 HTTP_FreeTokens(ppszRawHeaderLines);
2325 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
2326 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2327 return FALSE;
2329 if (pszString)
2331 for (i = 0; ppszRawHeaderLines[i]; i++)
2333 DWORD len = strlenW(ppszRawHeaderLines[i]);
2334 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
2335 pszString += len+1;
2337 *pszString = '\0';
2338 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, size));
2340 *lpdwBufferLength = size * sizeof(WCHAR);
2341 HTTP_FreeTokens(ppszRawHeaderLines);
2343 return TRUE;
2345 case HTTP_QUERY_STATUS_TEXT:
2346 if (lpwhr->lpszStatusText)
2348 DWORD len = strlenW(lpwhr->lpszStatusText);
2349 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2351 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2352 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2353 return FALSE;
2355 if (lpBuffer)
2357 memcpy(lpBuffer, lpwhr->lpszStatusText, (len + 1) * sizeof(WCHAR));
2358 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2360 *lpdwBufferLength = len * sizeof(WCHAR);
2361 return TRUE;
2363 break;
2364 case HTTP_QUERY_VERSION:
2365 if (lpwhr->lpszVersion)
2367 DWORD len = strlenW(lpwhr->lpszVersion);
2368 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
2370 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
2371 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2372 return FALSE;
2374 if (lpBuffer)
2376 memcpy(lpBuffer, lpwhr->lpszVersion, (len + 1) * sizeof(WCHAR));
2377 TRACE("returning data: %s\n", debugstr_wn(lpBuffer, len));
2379 *lpdwBufferLength = len * sizeof(WCHAR);
2380 return TRUE;
2382 break;
2383 default:
2384 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
2386 if (level < LAST_TABLE_HEADER && header_lookup[level])
2387 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
2388 requested_index,request_only);
2391 if (index >= 0)
2392 lphttpHdr = &lpwhr->pCustHeaders[index];
2394 /* Ensure header satisfies requested attributes */
2395 if (!lphttpHdr ||
2396 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
2397 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
2399 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
2400 return bSuccess;
2403 if (lpdwIndex && level != HTTP_QUERY_STATUS_CODE) (*lpdwIndex)++;
2405 /* coalesce value to requested type */
2406 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER && lpBuffer)
2408 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
2409 TRACE(" returning number: %d\n", *(int *)lpBuffer);
2410 bSuccess = TRUE;
2412 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME && lpBuffer)
2414 time_t tmpTime;
2415 struct tm tmpTM;
2416 SYSTEMTIME *STHook;
2418 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
2420 tmpTM = *gmtime(&tmpTime);
2421 STHook = (SYSTEMTIME *)lpBuffer;
2422 STHook->wDay = tmpTM.tm_mday;
2423 STHook->wHour = tmpTM.tm_hour;
2424 STHook->wMilliseconds = 0;
2425 STHook->wMinute = tmpTM.tm_min;
2426 STHook->wDayOfWeek = tmpTM.tm_wday;
2427 STHook->wMonth = tmpTM.tm_mon + 1;
2428 STHook->wSecond = tmpTM.tm_sec;
2429 STHook->wYear = tmpTM.tm_year;
2430 bSuccess = TRUE;
2432 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
2433 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
2434 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
2436 else if (lphttpHdr->lpszValue)
2438 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
2440 if (len > *lpdwBufferLength)
2442 *lpdwBufferLength = len;
2443 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
2444 return bSuccess;
2446 if (lpBuffer)
2448 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
2449 TRACE(" returning string: %s\n", debugstr_w(lpBuffer));
2451 *lpdwBufferLength = len - sizeof(WCHAR);
2452 bSuccess = TRUE;
2454 return bSuccess;
2457 /***********************************************************************
2458 * HttpQueryInfoW (WININET.@)
2460 * Queries for information about an HTTP request
2462 * RETURNS
2463 * TRUE on success
2464 * FALSE on failure
2467 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2468 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2470 BOOL bSuccess = FALSE;
2471 LPWININETHTTPREQW lpwhr;
2473 if (TRACE_ON(wininet)) {
2474 #define FE(x) { x, #x }
2475 static const wininet_flag_info query_flags[] = {
2476 FE(HTTP_QUERY_MIME_VERSION),
2477 FE(HTTP_QUERY_CONTENT_TYPE),
2478 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
2479 FE(HTTP_QUERY_CONTENT_ID),
2480 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
2481 FE(HTTP_QUERY_CONTENT_LENGTH),
2482 FE(HTTP_QUERY_CONTENT_LANGUAGE),
2483 FE(HTTP_QUERY_ALLOW),
2484 FE(HTTP_QUERY_PUBLIC),
2485 FE(HTTP_QUERY_DATE),
2486 FE(HTTP_QUERY_EXPIRES),
2487 FE(HTTP_QUERY_LAST_MODIFIED),
2488 FE(HTTP_QUERY_MESSAGE_ID),
2489 FE(HTTP_QUERY_URI),
2490 FE(HTTP_QUERY_DERIVED_FROM),
2491 FE(HTTP_QUERY_COST),
2492 FE(HTTP_QUERY_LINK),
2493 FE(HTTP_QUERY_PRAGMA),
2494 FE(HTTP_QUERY_VERSION),
2495 FE(HTTP_QUERY_STATUS_CODE),
2496 FE(HTTP_QUERY_STATUS_TEXT),
2497 FE(HTTP_QUERY_RAW_HEADERS),
2498 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
2499 FE(HTTP_QUERY_CONNECTION),
2500 FE(HTTP_QUERY_ACCEPT),
2501 FE(HTTP_QUERY_ACCEPT_CHARSET),
2502 FE(HTTP_QUERY_ACCEPT_ENCODING),
2503 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
2504 FE(HTTP_QUERY_AUTHORIZATION),
2505 FE(HTTP_QUERY_CONTENT_ENCODING),
2506 FE(HTTP_QUERY_FORWARDED),
2507 FE(HTTP_QUERY_FROM),
2508 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
2509 FE(HTTP_QUERY_LOCATION),
2510 FE(HTTP_QUERY_ORIG_URI),
2511 FE(HTTP_QUERY_REFERER),
2512 FE(HTTP_QUERY_RETRY_AFTER),
2513 FE(HTTP_QUERY_SERVER),
2514 FE(HTTP_QUERY_TITLE),
2515 FE(HTTP_QUERY_USER_AGENT),
2516 FE(HTTP_QUERY_WWW_AUTHENTICATE),
2517 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
2518 FE(HTTP_QUERY_ACCEPT_RANGES),
2519 FE(HTTP_QUERY_SET_COOKIE),
2520 FE(HTTP_QUERY_COOKIE),
2521 FE(HTTP_QUERY_REQUEST_METHOD),
2522 FE(HTTP_QUERY_REFRESH),
2523 FE(HTTP_QUERY_CONTENT_DISPOSITION),
2524 FE(HTTP_QUERY_AGE),
2525 FE(HTTP_QUERY_CACHE_CONTROL),
2526 FE(HTTP_QUERY_CONTENT_BASE),
2527 FE(HTTP_QUERY_CONTENT_LOCATION),
2528 FE(HTTP_QUERY_CONTENT_MD5),
2529 FE(HTTP_QUERY_CONTENT_RANGE),
2530 FE(HTTP_QUERY_ETAG),
2531 FE(HTTP_QUERY_HOST),
2532 FE(HTTP_QUERY_IF_MATCH),
2533 FE(HTTP_QUERY_IF_NONE_MATCH),
2534 FE(HTTP_QUERY_IF_RANGE),
2535 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
2536 FE(HTTP_QUERY_MAX_FORWARDS),
2537 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
2538 FE(HTTP_QUERY_RANGE),
2539 FE(HTTP_QUERY_TRANSFER_ENCODING),
2540 FE(HTTP_QUERY_UPGRADE),
2541 FE(HTTP_QUERY_VARY),
2542 FE(HTTP_QUERY_VIA),
2543 FE(HTTP_QUERY_WARNING),
2544 FE(HTTP_QUERY_CUSTOM)
2546 static const wininet_flag_info modifier_flags[] = {
2547 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
2548 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
2549 FE(HTTP_QUERY_FLAG_NUMBER),
2550 FE(HTTP_QUERY_FLAG_COALESCE)
2552 #undef FE
2553 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
2554 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
2555 DWORD i;
2557 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
2558 TRACE(" Attribute:");
2559 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
2560 if (query_flags[i].val == info) {
2561 TRACE(" %s", query_flags[i].name);
2562 break;
2565 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
2566 TRACE(" Unknown (%08x)", info);
2569 TRACE(" Modifier:");
2570 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
2571 if (modifier_flags[i].val & info_mod) {
2572 TRACE(" %s", modifier_flags[i].name);
2573 info_mod &= ~ modifier_flags[i].val;
2577 if (info_mod) {
2578 TRACE(" Unknown (%08x)", info_mod);
2580 TRACE("\n");
2583 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2584 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2586 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2587 goto lend;
2590 if (lpBuffer == NULL)
2591 *lpdwBufferLength = 0;
2592 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
2593 lpBuffer, lpdwBufferLength, lpdwIndex);
2595 lend:
2596 if( lpwhr )
2597 WININET_Release( &lpwhr->hdr );
2599 TRACE("%d <--\n", bSuccess);
2600 return bSuccess;
2603 /***********************************************************************
2604 * HttpQueryInfoA (WININET.@)
2606 * Queries for information about an HTTP request
2608 * RETURNS
2609 * TRUE on success
2610 * FALSE on failure
2613 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
2614 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
2616 BOOL result;
2617 DWORD len;
2618 WCHAR* bufferW;
2620 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
2621 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
2623 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
2624 lpdwBufferLength, lpdwIndex );
2627 if (lpBuffer)
2629 DWORD alloclen;
2630 len = (*lpdwBufferLength)*sizeof(WCHAR);
2631 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2633 alloclen = MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, NULL, 0 ) * sizeof(WCHAR);
2634 if (alloclen < len)
2635 alloclen = len;
2637 else
2638 alloclen = len;
2639 bufferW = HeapAlloc( GetProcessHeap(), 0, alloclen );
2640 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
2641 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2642 MultiByteToWideChar( CP_ACP, 0, lpBuffer, -1, bufferW, alloclen / sizeof(WCHAR) );
2643 } else
2645 bufferW = NULL;
2646 len = 0;
2649 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2650 &len, lpdwIndex );
2651 if( result )
2653 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
2654 lpBuffer, *lpdwBufferLength, NULL, NULL );
2655 *lpdwBufferLength = len - 1;
2657 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
2659 else
2660 /* since the strings being returned from HttpQueryInfoW should be
2661 * only ASCII characters, it is reasonable to assume that all of
2662 * the Unicode characters can be reduced to a single byte */
2663 *lpdwBufferLength = len / sizeof(WCHAR);
2665 HeapFree(GetProcessHeap(), 0, bufferW );
2667 return result;
2670 /***********************************************************************
2671 * HttpSendRequestExA (WININET.@)
2673 * Sends the specified request to the HTTP server and allows chunked
2674 * transfers.
2676 * RETURNS
2677 * Success: TRUE
2678 * Failure: FALSE, call GetLastError() for more information.
2680 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2681 LPINTERNET_BUFFERSA lpBuffersIn,
2682 LPINTERNET_BUFFERSA lpBuffersOut,
2683 DWORD dwFlags, DWORD_PTR dwContext)
2685 INTERNET_BUFFERSW BuffersInW;
2686 BOOL rc = FALSE;
2687 DWORD headerlen;
2688 LPWSTR header = NULL;
2690 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2691 lpBuffersOut, dwFlags, dwContext);
2693 if (lpBuffersIn)
2695 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2696 if (lpBuffersIn->lpcszHeader)
2698 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2699 lpBuffersIn->dwHeadersLength,0,0);
2700 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2701 if (!(BuffersInW.lpcszHeader = header))
2703 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2704 return FALSE;
2706 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2707 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2708 header, headerlen);
2710 else
2711 BuffersInW.lpcszHeader = NULL;
2712 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2713 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2714 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2715 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2716 BuffersInW.Next = NULL;
2719 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2721 HeapFree(GetProcessHeap(),0,header);
2723 return rc;
2726 /***********************************************************************
2727 * HttpSendRequestExW (WININET.@)
2729 * Sends the specified request to the HTTP server and allows chunked
2730 * transfers
2732 * RETURNS
2733 * Success: TRUE
2734 * Failure: FALSE, call GetLastError() for more information.
2736 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2737 LPINTERNET_BUFFERSW lpBuffersIn,
2738 LPINTERNET_BUFFERSW lpBuffersOut,
2739 DWORD dwFlags, DWORD_PTR dwContext)
2741 BOOL ret = FALSE;
2742 LPWININETHTTPREQW lpwhr;
2743 LPWININETHTTPSESSIONW lpwhs;
2744 LPWININETAPPINFOW hIC;
2746 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest, lpBuffersIn,
2747 lpBuffersOut, dwFlags, dwContext);
2749 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2751 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2753 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2754 goto lend;
2757 lpwhs = lpwhr->lpHttpSession;
2758 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
2759 hIC = lpwhs->lpAppInfo;
2760 assert(hIC->hdr.htype == WH_HINIT);
2762 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2764 WORKREQUEST workRequest;
2765 struct WORKREQ_HTTPSENDREQUESTW *req;
2767 workRequest.asyncproc = AsyncHttpSendRequestProc;
2768 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2769 req = &workRequest.u.HttpSendRequestW;
2770 if (lpBuffersIn)
2772 if (lpBuffersIn->lpcszHeader)
2773 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2774 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2775 else
2776 req->lpszHeader = NULL;
2777 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2778 req->lpOptional = lpBuffersIn->lpvBuffer;
2779 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2780 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2782 else
2784 req->lpszHeader = NULL;
2785 req->dwHeaderLength = 0;
2786 req->lpOptional = NULL;
2787 req->dwOptionalLength = 0;
2788 req->dwContentLength = 0;
2791 req->bEndRequest = FALSE;
2793 INTERNET_AsyncCall(&workRequest);
2795 * This is from windows.
2797 INTERNET_SetLastError(ERROR_IO_PENDING);
2799 else
2801 if (lpBuffersIn)
2802 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2803 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2804 lpBuffersIn->dwBufferTotal, FALSE);
2805 else
2806 ret = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, FALSE);
2809 lend:
2810 if ( lpwhr )
2811 WININET_Release( &lpwhr->hdr );
2813 TRACE("<---\n");
2814 return ret;
2817 /***********************************************************************
2818 * HttpSendRequestW (WININET.@)
2820 * Sends the specified request to the HTTP server
2822 * RETURNS
2823 * TRUE on success
2824 * FALSE on failure
2827 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
2828 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2830 LPWININETHTTPREQW lpwhr;
2831 LPWININETHTTPSESSIONW lpwhs = NULL;
2832 LPWININETAPPINFOW hIC = NULL;
2833 BOOL r;
2835 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest,
2836 debugstr_wn(lpszHeaders, dwHeaderLength), dwHeaderLength, lpOptional, dwOptionalLength);
2838 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2839 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2841 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2842 r = FALSE;
2843 goto lend;
2846 lpwhs = lpwhr->lpHttpSession;
2847 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2849 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2850 r = FALSE;
2851 goto lend;
2854 hIC = lpwhs->lpAppInfo;
2855 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2857 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2858 r = FALSE;
2859 goto lend;
2862 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2864 WORKREQUEST workRequest;
2865 struct WORKREQ_HTTPSENDREQUESTW *req;
2867 workRequest.asyncproc = AsyncHttpSendRequestProc;
2868 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2869 req = &workRequest.u.HttpSendRequestW;
2870 if (lpszHeaders)
2872 DWORD size;
2874 if (dwHeaderLength == ~0u) size = (strlenW(lpszHeaders) + 1) * sizeof(WCHAR);
2875 else size = dwHeaderLength * sizeof(WCHAR);
2877 req->lpszHeader = HeapAlloc(GetProcessHeap(), 0, size);
2878 memcpy(req->lpszHeader, lpszHeaders, size);
2880 else
2881 req->lpszHeader = 0;
2882 req->dwHeaderLength = dwHeaderLength;
2883 req->lpOptional = lpOptional;
2884 req->dwOptionalLength = dwOptionalLength;
2885 req->dwContentLength = dwOptionalLength;
2886 req->bEndRequest = TRUE;
2888 INTERNET_AsyncCall(&workRequest);
2890 * This is from windows.
2892 INTERNET_SetLastError(ERROR_IO_PENDING);
2893 r = FALSE;
2895 else
2897 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
2898 dwHeaderLength, lpOptional, dwOptionalLength,
2899 dwOptionalLength, TRUE);
2901 lend:
2902 if( lpwhr )
2903 WININET_Release( &lpwhr->hdr );
2904 return r;
2907 /***********************************************************************
2908 * HttpSendRequestA (WININET.@)
2910 * Sends the specified request to the HTTP server
2912 * RETURNS
2913 * TRUE on success
2914 * FALSE on failure
2917 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
2918 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2920 BOOL result;
2921 LPWSTR szHeaders=NULL;
2922 DWORD nLen=dwHeaderLength;
2923 if(lpszHeaders!=NULL)
2925 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2926 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2927 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
2929 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
2930 HeapFree(GetProcessHeap(),0,szHeaders);
2931 return result;
2934 static BOOL HTTP_GetRequestURL(WININETHTTPREQW *req, LPWSTR buf)
2936 LPHTTPHEADERW host_header;
2938 static const WCHAR formatW[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
2940 host_header = HTTP_GetHeader(req, szHost);
2941 if(!host_header)
2942 return FALSE;
2944 sprintfW(buf, formatW, host_header->lpszValue, req->lpszPath); /* FIXME */
2945 return TRUE;
2948 /***********************************************************************
2949 * HTTP_GetRedirectURL (internal)
2951 static LPWSTR HTTP_GetRedirectURL(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
2953 static WCHAR szHttp[] = {'h','t','t','p',0};
2954 static WCHAR szHttps[] = {'h','t','t','p','s',0};
2955 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2956 URL_COMPONENTSW urlComponents;
2957 DWORD url_length = 0;
2958 LPWSTR orig_url;
2959 LPWSTR combined_url;
2961 if (lpszUrl[0]=='/') return WININET_strdupW( lpszUrl );
2963 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2964 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
2965 urlComponents.dwSchemeLength = 0;
2966 urlComponents.lpszHostName = lpwhs->lpszHostName;
2967 urlComponents.dwHostNameLength = 0;
2968 urlComponents.nPort = lpwhs->nHostPort;
2969 urlComponents.lpszUserName = lpwhs->lpszUserName;
2970 urlComponents.dwUserNameLength = 0;
2971 urlComponents.lpszPassword = NULL;
2972 urlComponents.dwPasswordLength = 0;
2973 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2974 urlComponents.dwUrlPathLength = 0;
2975 urlComponents.lpszExtraInfo = NULL;
2976 urlComponents.dwExtraInfoLength = 0;
2978 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2979 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2980 return NULL;
2982 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
2984 /* convert from bytes to characters */
2985 url_length = url_length / sizeof(WCHAR) - 1;
2986 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2988 HeapFree(GetProcessHeap(), 0, orig_url);
2989 return NULL;
2992 url_length = 0;
2993 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2994 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2996 HeapFree(GetProcessHeap(), 0, orig_url);
2997 return NULL;
2999 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
3001 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
3003 HeapFree(GetProcessHeap(), 0, orig_url);
3004 HeapFree(GetProcessHeap(), 0, combined_url);
3005 return NULL;
3007 HeapFree(GetProcessHeap(), 0, orig_url);
3008 return combined_url;
3012 /***********************************************************************
3013 * HTTP_HandleRedirect (internal)
3015 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
3017 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3018 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
3019 BOOL using_proxy = hIC->lpszProxy && hIC->lpszProxy[0];
3020 WCHAR path[INTERNET_MAX_URL_LENGTH];
3021 int index;
3023 if(lpszUrl[0]=='/')
3025 /* if it's an absolute path, keep the same session info */
3026 lstrcpynW(path, lpszUrl, INTERNET_MAX_URL_LENGTH);
3028 else
3030 URL_COMPONENTSW urlComponents;
3031 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
3032 static WCHAR szHttp[] = {'h','t','t','p',0};
3033 static WCHAR szHttps[] = {'h','t','t','p','s',0};
3035 userName[0] = 0;
3036 hostName[0] = 0;
3037 protocol[0] = 0;
3039 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
3040 urlComponents.lpszScheme = protocol;
3041 urlComponents.dwSchemeLength = 32;
3042 urlComponents.lpszHostName = hostName;
3043 urlComponents.dwHostNameLength = MAXHOSTNAME;
3044 urlComponents.lpszUserName = userName;
3045 urlComponents.dwUserNameLength = 1024;
3046 urlComponents.lpszPassword = NULL;
3047 urlComponents.dwPasswordLength = 0;
3048 urlComponents.lpszUrlPath = path;
3049 urlComponents.dwUrlPathLength = 2048;
3050 urlComponents.lpszExtraInfo = NULL;
3051 urlComponents.dwExtraInfoLength = 0;
3052 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
3053 return FALSE;
3055 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
3056 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3058 TRACE("redirect from secure page to non-secure page\n");
3059 /* FIXME: warn about from secure redirect to non-secure page */
3060 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
3062 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
3063 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
3065 TRACE("redirect from non-secure page to secure page\n");
3066 /* FIXME: notify about redirect to secure page */
3067 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
3070 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
3072 if (lstrlenW(protocol)>4) /*https*/
3073 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
3074 else /*http*/
3075 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
3078 #if 0
3080 * This upsets redirects to binary files on sourceforge.net
3081 * and gives an html page instead of the target file
3082 * Examination of the HTTP request sent by native wininet.dll
3083 * reveals that it doesn't send a referrer in that case.
3084 * Maybe there's a flag that enables this, or maybe a referrer
3085 * shouldn't be added in case of a redirect.
3088 /* consider the current host as the referrer */
3089 if (lpwhs->lpszServerName && *lpwhs->lpszServerName)
3090 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
3091 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
3092 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
3093 #endif
3095 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3096 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
3097 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
3099 int len;
3100 static const WCHAR fmt[] = {'%','s',':','%','i',0};
3101 len = lstrlenW(hostName);
3102 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
3103 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
3104 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
3106 else
3107 lpwhs->lpszHostName = WININET_strdupW(hostName);
3109 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
3111 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3112 lpwhs->lpszUserName = NULL;
3113 if (userName[0])
3114 lpwhs->lpszUserName = WININET_strdupW(userName);
3116 if (!using_proxy)
3118 if (strcmpiW(lpwhs->lpszServerName, hostName) || lpwhs->nServerPort != urlComponents.nPort)
3120 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3121 lpwhs->lpszServerName = WININET_strdupW(hostName);
3122 lpwhs->nServerPort = urlComponents.nPort;
3124 NETCON_close(&lpwhr->netConnection);
3125 if (!HTTP_ResolveName(lpwhr)) return FALSE;
3126 if (!NETCON_init(&lpwhr->netConnection, lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)) return FALSE;
3129 else
3130 TRACE("Redirect through proxy\n");
3133 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
3134 lpwhr->lpszPath=NULL;
3135 if (*path)
3137 DWORD needed = 0;
3138 HRESULT rc;
3140 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
3141 if (rc != E_POINTER)
3142 needed = strlenW(path)+1;
3143 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
3144 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
3145 URL_ESCAPE_SPACES_ONLY);
3146 if (rc != S_OK)
3148 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
3149 strcpyW(lpwhr->lpszPath,path);
3153 /* Remove custom content-type/length headers on redirects. */
3154 index = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Type, 0, TRUE);
3155 if (0 <= index)
3156 HTTP_DeleteCustomHeader(lpwhr, index);
3157 index = HTTP_GetCustomHeaderIndex(lpwhr, szContent_Length, 0, TRUE);
3158 if (0 <= index)
3159 HTTP_DeleteCustomHeader(lpwhr, index);
3161 return TRUE;
3164 /***********************************************************************
3165 * HTTP_build_req (internal)
3167 * concatenate all the strings in the request together
3169 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
3171 LPCWSTR *t;
3172 LPWSTR str;
3174 for( t = list; *t ; t++ )
3175 len += strlenW( *t );
3176 len++;
3178 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
3179 *str = 0;
3181 for( t = list; *t ; t++ )
3182 strcatW( str, *t );
3184 return str;
3187 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
3189 LPWSTR lpszPath;
3190 LPWSTR requestString;
3191 INT len;
3192 INT cnt;
3193 INT responseLen;
3194 char *ascii_req;
3195 BOOL ret;
3196 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
3197 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
3198 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
3200 TRACE("\n");
3202 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
3203 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
3204 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, g_szHttp1_1 );
3205 HeapFree( GetProcessHeap(), 0, lpszPath );
3207 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3208 NULL, 0, NULL, NULL );
3209 len--; /* the nul terminator isn't needed */
3210 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
3211 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3212 ascii_req, len, NULL, NULL );
3213 HeapFree( GetProcessHeap(), 0, requestString );
3215 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
3217 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
3218 HeapFree( GetProcessHeap(), 0, ascii_req );
3219 if (!ret || cnt < 0)
3220 return FALSE;
3222 responseLen = HTTP_GetResponseHeaders( lpwhr, TRUE );
3223 if (!responseLen)
3224 return FALSE;
3226 return TRUE;
3229 static void HTTP_InsertCookies(LPWININETHTTPREQW lpwhr)
3231 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s','%','s',0};
3232 LPWSTR lpszCookies, lpszUrl = NULL;
3233 DWORD nCookieSize, size;
3234 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3236 size = (strlenW(Host->lpszValue) + strlenW(szUrlForm) + strlenW(lpwhr->lpszPath)) * sizeof(WCHAR);
3237 if (!(lpszUrl = HeapAlloc(GetProcessHeap(), 0, size))) return;
3238 sprintfW( lpszUrl, szUrlForm, Host->lpszValue, lpwhr->lpszPath);
3240 if (InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
3242 int cnt = 0;
3243 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
3245 size = sizeof(szCookie) + nCookieSize * sizeof(WCHAR) + sizeof(szCrLf);
3246 if ((lpszCookies = HeapAlloc(GetProcessHeap(), 0, size)))
3248 cnt += sprintfW(lpszCookies, szCookie);
3249 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
3250 strcatW(lpszCookies, szCrLf);
3252 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies), HTTP_ADDREQ_FLAG_REPLACE);
3253 HeapFree(GetProcessHeap(), 0, lpszCookies);
3256 HeapFree(GetProcessHeap(), 0, lpszUrl);
3259 /***********************************************************************
3260 * HTTP_HttpSendRequestW (internal)
3262 * Sends the specified request to the HTTP server
3264 * RETURNS
3265 * TRUE on success
3266 * FALSE on failure
3269 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
3270 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
3271 DWORD dwContentLength, BOOL bEndRequest)
3273 INT cnt;
3274 BOOL bSuccess = FALSE;
3275 LPWSTR requestString = NULL;
3276 INT responseLen;
3277 BOOL loop_next;
3278 INTERNET_ASYNC_RESULT iar;
3279 static const WCHAR szPost[] = { 'P','O','S','T',0 };
3280 static const WCHAR szContentLength[] =
3281 { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0 };
3282 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \r\n */ + 20 /* int */ ];
3284 TRACE("--> %p\n", lpwhr);
3286 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
3288 /* if the verb is NULL default to GET */
3289 if (!lpwhr->lpszVerb)
3290 lpwhr->lpszVerb = WININET_strdupW(szGET);
3292 if (dwContentLength || strcmpW(lpwhr->lpszVerb, szGET))
3294 sprintfW(contentLengthStr, szContentLength, dwContentLength);
3295 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_REPLACE);
3296 lpwhr->dwBytesToWrite = dwContentLength;
3298 if (lpwhr->lpHttpSession->lpAppInfo->lpszAgent)
3300 WCHAR *agent_header;
3301 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
3302 int len;
3304 len = strlenW(lpwhr->lpHttpSession->lpAppInfo->lpszAgent) + strlenW(user_agent);
3305 agent_header = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
3306 sprintfW(agent_header, user_agent, lpwhr->lpHttpSession->lpAppInfo->lpszAgent);
3308 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3309 HeapFree(GetProcessHeap(), 0, agent_header);
3311 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_PRAGMA_NOCACHE)
3313 static const WCHAR pragma_nocache[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
3314 HTTP_HttpAddRequestHeadersW(lpwhr, pragma_nocache, strlenW(pragma_nocache), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3316 if ((lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_CACHE_WRITE) && !strcmpW(lpwhr->lpszVerb, szPost))
3318 static const WCHAR cache_control[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
3319 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
3320 HTTP_HttpAddRequestHeadersW(lpwhr, cache_control, strlenW(cache_control), HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3325 DWORD len;
3326 char *ascii_req;
3328 loop_next = FALSE;
3330 /* like native, just in case the caller forgot to call InternetReadFile
3331 * for all the data */
3332 HTTP_DrainContent(lpwhr);
3333 lpwhr->dwContentRead = 0;
3335 if (TRACE_ON(wininet))
3337 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
3338 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
3341 HTTP_FixURL(lpwhr);
3342 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION)
3344 HTTP_ProcessHeader(lpwhr, szConnection, szKeepAlive, HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
3346 HTTP_InsertAuthorization(lpwhr, lpwhr->pAuthInfo, szAuthorization);
3347 HTTP_InsertAuthorization(lpwhr, lpwhr->pProxyAuthInfo, szProxy_Authorization);
3349 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES))
3350 HTTP_InsertCookies(lpwhr);
3352 /* add the headers the caller supplied */
3353 if( lpszHeaders && dwHeaderLength )
3355 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
3356 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
3359 if (lpwhr->lpHttpSession->lpAppInfo->lpszProxy && lpwhr->lpHttpSession->lpAppInfo->lpszProxy[0])
3361 WCHAR *url = HTTP_BuildProxyRequestUrl(lpwhr);
3362 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, url, lpwhr->lpszVersion);
3363 HeapFree(GetProcessHeap(), 0, url);
3365 else
3366 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, lpwhr->lpszVersion);
3369 TRACE("Request header -> %s\n", debugstr_w(requestString) );
3371 /* Send the request and store the results */
3372 if (!HTTP_OpenConnection(lpwhr))
3373 goto lend;
3375 /* send the request as ASCII, tack on the optional data */
3376 if( !lpOptional )
3377 dwOptionalLength = 0;
3378 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3379 NULL, 0, NULL, NULL );
3380 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
3381 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
3382 ascii_req, len, NULL, NULL );
3383 if( lpOptional )
3384 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
3385 len = (len + dwOptionalLength - 1);
3386 ascii_req[len] = 0;
3387 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
3389 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3390 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
3392 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
3393 HeapFree( GetProcessHeap(), 0, ascii_req );
3395 lpwhr->dwBytesWritten = dwOptionalLength;
3397 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3398 INTERNET_STATUS_REQUEST_SENT,
3399 &len, sizeof(DWORD));
3401 if (bEndRequest)
3403 DWORD dwBufferSize;
3404 DWORD dwStatusCode;
3405 WCHAR encoding[20];
3406 static const WCHAR szChunked[] = {'c','h','u','n','k','e','d',0};
3408 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3409 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
3411 if (cnt < 0)
3412 goto lend;
3414 responseLen = HTTP_GetResponseHeaders(lpwhr, TRUE);
3415 if (responseLen)
3416 bSuccess = TRUE;
3418 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3419 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
3420 sizeof(DWORD));
3422 HTTP_ProcessCookies(lpwhr);
3424 dwBufferSize = sizeof(lpwhr->dwContentLength);
3425 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
3426 &lpwhr->dwContentLength,&dwBufferSize,NULL))
3427 lpwhr->dwContentLength = -1;
3429 if (lpwhr->dwContentLength == 0)
3430 HTTP_FinishedReading(lpwhr);
3432 /* Correct the case where both a Content-Length and Transfer-encoding = chunked are set */
3434 dwBufferSize = sizeof(encoding);
3435 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_TRANSFER_ENCODING, encoding, &dwBufferSize, NULL) &&
3436 !strcmpiW(encoding, szChunked))
3438 lpwhr->dwContentLength = -1;
3441 dwBufferSize = sizeof(dwStatusCode);
3442 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,
3443 &dwStatusCode,&dwBufferSize,NULL))
3444 dwStatusCode = 0;
3446 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
3448 WCHAR *new_url, szNewLocation[INTERNET_MAX_URL_LENGTH];
3449 dwBufferSize=sizeof(szNewLocation);
3450 if ((dwStatusCode==HTTP_STATUS_REDIRECT || dwStatusCode==HTTP_STATUS_MOVED) &&
3451 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
3453 /* redirects are always GETs */
3454 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
3455 lpwhr->lpszVerb = WININET_strdupW(szGET);
3457 HTTP_DrainContent(lpwhr);
3458 if ((new_url = HTTP_GetRedirectURL( lpwhr, szNewLocation )))
3460 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext, INTERNET_STATUS_REDIRECT,
3461 new_url, (strlenW(new_url) + 1) * sizeof(WCHAR));
3462 bSuccess = HTTP_HandleRedirect(lpwhr, new_url);
3463 if (bSuccess)
3465 HeapFree(GetProcessHeap(), 0, requestString);
3466 loop_next = TRUE;
3468 HeapFree( GetProcessHeap(), 0, new_url );
3472 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
3474 WCHAR szAuthValue[2048];
3475 dwBufferSize=2048;
3476 if (dwStatusCode == HTTP_STATUS_DENIED)
3478 DWORD dwIndex = 0;
3479 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3481 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3482 &lpwhr->pAuthInfo,
3483 lpwhr->lpHttpSession->lpszUserName,
3484 lpwhr->lpHttpSession->lpszPassword))
3486 loop_next = TRUE;
3487 break;
3491 if (dwStatusCode == HTTP_STATUS_PROXY_AUTH_REQ)
3493 DWORD dwIndex = 0;
3494 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_PROXY_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
3496 if (HTTP_DoAuthorization(lpwhr, szAuthValue,
3497 &lpwhr->pProxyAuthInfo,
3498 lpwhr->lpHttpSession->lpAppInfo->lpszProxyUsername,
3499 lpwhr->lpHttpSession->lpAppInfo->lpszProxyPassword))
3501 loop_next = TRUE;
3502 break;
3508 else
3509 bSuccess = TRUE;
3511 while (loop_next);
3513 /* FIXME: Better check, when we have to create the cache file */
3514 if(bSuccess && (lpwhr->hdr.dwFlags & INTERNET_FLAG_NEED_FILE)) {
3515 WCHAR url[INTERNET_MAX_URL_LENGTH];
3516 WCHAR cacheFileName[MAX_PATH+1];
3517 BOOL b;
3519 b = HTTP_GetRequestURL(lpwhr, url);
3520 if(!b) {
3521 WARN("Could not get URL\n");
3522 goto lend;
3525 b = CreateUrlCacheEntryW(url, lpwhr->dwContentLength > 0 ? lpwhr->dwContentLength : 0, NULL, cacheFileName, 0);
3526 if(b) {
3527 lpwhr->lpszCacheFile = WININET_strdupW(cacheFileName);
3528 lpwhr->hCacheFile = CreateFileW(lpwhr->lpszCacheFile, GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,
3529 NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
3530 if(lpwhr->hCacheFile == INVALID_HANDLE_VALUE) {
3531 WARN("Could not create file: %u\n", GetLastError());
3532 lpwhr->hCacheFile = NULL;
3534 }else {
3535 WARN("Could not create cache entry: %08x\n", GetLastError());
3539 lend:
3541 HeapFree(GetProcessHeap(), 0, requestString);
3543 /* TODO: send notification for P3P header */
3545 if (lpwhr->lpHttpSession->lpAppInfo->hdr.dwFlags & INTERNET_FLAG_ASYNC)
3547 if (bSuccess)
3549 if (lpwhr->dwBytesWritten == lpwhr->dwBytesToWrite) HTTP_ReceiveRequestData(lpwhr, TRUE);
3550 else
3552 iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
3553 iar.dwError = 0;
3555 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3556 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3557 sizeof(INTERNET_ASYNC_RESULT));
3560 else
3562 iar.dwResult = (DWORD_PTR)lpwhr->hdr.hInternet;
3563 iar.dwError = INTERNET_GetLastError();
3565 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3566 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
3567 sizeof(INTERNET_ASYNC_RESULT));
3571 TRACE("<--\n");
3572 if (bSuccess) INTERNET_SetLastError(ERROR_SUCCESS);
3573 return bSuccess;
3576 /***********************************************************************
3577 * HTTPSESSION_Destroy (internal)
3579 * Deallocate session handle
3582 static void HTTPSESSION_Destroy(WININETHANDLEHEADER *hdr)
3584 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3586 TRACE("%p\n", lpwhs);
3588 WININET_Release(&lpwhs->lpAppInfo->hdr);
3590 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3591 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3592 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3593 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3594 HeapFree(GetProcessHeap(), 0, lpwhs);
3597 static DWORD HTTPSESSION_QueryOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD *size, BOOL unicode)
3599 switch(option) {
3600 case INTERNET_OPTION_HANDLE_TYPE:
3601 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
3603 if (*size < sizeof(ULONG))
3604 return ERROR_INSUFFICIENT_BUFFER;
3606 *size = sizeof(DWORD);
3607 *(DWORD*)buffer = INTERNET_HANDLE_TYPE_CONNECT_HTTP;
3608 return ERROR_SUCCESS;
3611 return INET_QueryOption(option, buffer, size, unicode);
3614 static DWORD HTTPSESSION_SetOption(WININETHANDLEHEADER *hdr, DWORD option, void *buffer, DWORD size)
3616 WININETHTTPSESSIONW *ses = (WININETHTTPSESSIONW*)hdr;
3618 switch(option) {
3619 case INTERNET_OPTION_USERNAME:
3621 HeapFree(GetProcessHeap(), 0, ses->lpszUserName);
3622 if (!(ses->lpszUserName = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
3623 return ERROR_SUCCESS;
3625 case INTERNET_OPTION_PASSWORD:
3627 HeapFree(GetProcessHeap(), 0, ses->lpszPassword);
3628 if (!(ses->lpszPassword = WININET_strdupW(buffer))) return ERROR_OUTOFMEMORY;
3629 return ERROR_SUCCESS;
3631 default: break;
3634 return ERROR_INTERNET_INVALID_OPTION;
3637 static const HANDLEHEADERVtbl HTTPSESSIONVtbl = {
3638 HTTPSESSION_Destroy,
3639 NULL,
3640 HTTPSESSION_QueryOption,
3641 HTTPSESSION_SetOption,
3642 NULL,
3643 NULL,
3644 NULL,
3645 NULL,
3646 NULL
3650 /***********************************************************************
3651 * HTTP_Connect (internal)
3653 * Create http session handle
3655 * RETURNS
3656 * HINTERNET a session handle on success
3657 * NULL on failure
3660 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
3661 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
3662 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
3663 DWORD dwInternalFlags)
3665 LPWININETHTTPSESSIONW lpwhs = NULL;
3666 HINTERNET handle = NULL;
3668 TRACE("-->\n");
3670 if (!lpszServerName || !lpszServerName[0])
3672 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3673 goto lerror;
3676 assert( hIC->hdr.htype == WH_HINIT );
3678 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
3679 if (NULL == lpwhs)
3681 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3682 goto lerror;
3686 * According to my tests. The name is not resolved until a request is sent
3689 lpwhs->hdr.htype = WH_HHTTPSESSION;
3690 lpwhs->hdr.vtbl = &HTTPSESSIONVtbl;
3691 lpwhs->hdr.dwFlags = dwFlags;
3692 lpwhs->hdr.dwContext = dwContext;
3693 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
3694 lpwhs->hdr.refs = 1;
3695 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
3697 WININET_AddRef( &hIC->hdr );
3698 lpwhs->lpAppInfo = hIC;
3699 list_add_head( &hIC->hdr.children, &lpwhs->hdr.entry );
3701 handle = WININET_AllocHandle( &lpwhs->hdr );
3702 if (NULL == handle)
3704 ERR("Failed to alloc handle\n");
3705 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3706 goto lerror;
3709 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
3710 if(strchrW(hIC->lpszProxy, ' '))
3711 FIXME("Several proxies not implemented.\n");
3712 if(hIC->lpszProxyBypass)
3713 FIXME("Proxy bypass is ignored.\n");
3715 if (lpszServerName && lpszServerName[0])
3717 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
3718 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
3720 if (lpszUserName && lpszUserName[0])
3721 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
3722 if (lpszPassword && lpszPassword[0])
3723 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
3724 lpwhs->nServerPort = nServerPort;
3725 lpwhs->nHostPort = nServerPort;
3727 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
3728 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
3730 INTERNET_SendCallback(&hIC->hdr, dwContext,
3731 INTERNET_STATUS_HANDLE_CREATED, &handle,
3732 sizeof(handle));
3735 lerror:
3736 if( lpwhs )
3737 WININET_Release( &lpwhs->hdr );
3740 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
3741 * windows
3744 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
3745 return handle;
3749 /***********************************************************************
3750 * HTTP_OpenConnection (internal)
3752 * Connect to a web server
3754 * RETURNS
3756 * TRUE on success
3757 * FALSE on failure
3759 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
3761 BOOL bSuccess = FALSE;
3762 LPWININETHTTPSESSIONW lpwhs;
3763 LPWININETAPPINFOW hIC = NULL;
3764 char szaddr[32];
3766 TRACE("-->\n");
3769 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
3771 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
3772 goto lend;
3775 if (NETCON_connected(&lpwhr->netConnection))
3777 bSuccess = TRUE;
3778 goto lend;
3780 if (!HTTP_ResolveName(lpwhr)) goto lend;
3782 lpwhs = lpwhr->lpHttpSession;
3784 hIC = lpwhs->lpAppInfo;
3785 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
3786 szaddr, sizeof(szaddr));
3787 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3788 INTERNET_STATUS_CONNECTING_TO_SERVER,
3789 szaddr,
3790 strlen(szaddr)+1);
3792 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
3793 SOCK_STREAM, 0))
3795 WARN("Socket creation failed: %u\n", INTERNET_GetLastError());
3796 goto lend;
3799 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
3800 sizeof(lpwhs->socketAddress)))
3801 goto lend;
3803 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
3805 /* Note: we differ from Microsoft's WinINet here. they seem to have
3806 * a bug that causes no status callbacks to be sent when starting
3807 * a tunnel to a proxy server using the CONNECT verb. i believe our
3808 * behaviour to be more correct and to not cause any incompatibilities
3809 * because using a secure connection through a proxy server is a rare
3810 * case that would be hard for anyone to depend on */
3811 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
3812 goto lend;
3814 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
3816 WARN("Couldn't connect securely to host\n");
3817 goto lend;
3821 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3822 INTERNET_STATUS_CONNECTED_TO_SERVER,
3823 szaddr, strlen(szaddr)+1);
3825 bSuccess = TRUE;
3827 lend:
3828 TRACE("%d <--\n", bSuccess);
3829 return bSuccess;
3833 /***********************************************************************
3834 * HTTP_clear_response_headers (internal)
3836 * clear out any old response headers
3838 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
3840 DWORD i;
3842 for( i=0; i<lpwhr->nCustHeaders; i++)
3844 if( !lpwhr->pCustHeaders[i].lpszField )
3845 continue;
3846 if( !lpwhr->pCustHeaders[i].lpszValue )
3847 continue;
3848 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
3849 continue;
3850 HTTP_DeleteCustomHeader( lpwhr, i );
3851 i--;
3855 /***********************************************************************
3856 * HTTP_GetResponseHeaders (internal)
3858 * Read server response
3860 * RETURNS
3862 * TRUE on success
3863 * FALSE on error
3865 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr, BOOL clear)
3867 INT cbreaks = 0;
3868 WCHAR buffer[MAX_REPLY_LEN];
3869 DWORD buflen = MAX_REPLY_LEN;
3870 BOOL bSuccess = FALSE;
3871 INT rc = 0;
3872 static const WCHAR szHundred[] = {'1','0','0',0};
3873 char bufferA[MAX_REPLY_LEN];
3874 LPWSTR status_code, status_text;
3875 DWORD cchMaxRawHeaders = 1024;
3876 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3877 DWORD cchRawHeaders = 0;
3879 TRACE("-->\n");
3881 /* clear old response headers (eg. from a redirect response) */
3882 if (clear) HTTP_clear_response_headers( lpwhr );
3884 if (!NETCON_connected(&lpwhr->netConnection))
3885 goto lend;
3887 do {
3889 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
3891 buflen = MAX_REPLY_LEN;
3892 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3893 goto lend;
3894 rc += buflen;
3895 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3897 /* split the version from the status code */
3898 status_code = strchrW( buffer, ' ' );
3899 if( !status_code )
3900 goto lend;
3901 *status_code++=0;
3903 /* split the status code from the status text */
3904 status_text = strchrW( status_code, ' ' );
3905 if( !status_text )
3906 goto lend;
3907 *status_text++=0;
3909 TRACE("version [%s] status code [%s] status text [%s]\n",
3910 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3912 } while (!strcmpW(status_code, szHundred)); /* ignore "100 Continue" responses */
3914 /* Add status code */
3915 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3916 HTTP_ADDHDR_FLAG_REPLACE);
3918 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3919 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3921 lpwhr->lpszVersion= WININET_strdupW(buffer);
3922 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3924 /* Restore the spaces */
3925 *(status_code-1) = ' ';
3926 *(status_text-1) = ' ';
3928 /* regenerate raw headers */
3929 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3931 cchMaxRawHeaders *= 2;
3932 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3934 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3935 cchRawHeaders += (buflen-1);
3936 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3937 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3938 lpszRawHeaders[cchRawHeaders] = '\0';
3940 /* Parse each response line */
3943 buflen = MAX_REPLY_LEN;
3944 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3946 LPWSTR * pFieldAndValue;
3948 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
3950 if (!bufferA[0]) break;
3951 if (!strchr(bufferA, ':'))
3953 WARN("invalid header\n");
3954 continue;
3956 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3958 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3960 cchMaxRawHeaders *= 2;
3961 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3963 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3964 cchRawHeaders += (buflen-1);
3965 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3966 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3967 lpszRawHeaders[cchRawHeaders] = '\0';
3969 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3970 if (!pFieldAndValue)
3971 break;
3973 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
3974 HTTP_ADDREQ_FLAG_ADD );
3976 HTTP_FreeTokens(pFieldAndValue);
3978 else
3980 cbreaks++;
3981 if (cbreaks >= 2)
3982 break;
3984 }while(1);
3986 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3987 lpwhr->lpszRawHeaders = lpszRawHeaders;
3988 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
3989 bSuccess = TRUE;
3991 lend:
3993 TRACE("<--\n");
3994 if (bSuccess)
3995 return rc;
3996 else
3998 HeapFree(GetProcessHeap(), 0, lpszRawHeaders);
3999 return 0;
4004 static void strip_spaces(LPWSTR start)
4006 LPWSTR str = start;
4007 LPWSTR end;
4009 while (*str == ' ' && *str != '\0')
4010 str++;
4012 if (str != start)
4013 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
4015 end = start + strlenW(start) - 1;
4016 while (end >= start && *end == ' ')
4018 *end = '\0';
4019 end--;
4024 /***********************************************************************
4025 * HTTP_InterpretHttpHeader (internal)
4027 * Parse server response
4029 * RETURNS
4031 * Pointer to array of field, value, NULL on success.
4032 * NULL on error.
4034 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
4036 LPWSTR * pTokenPair;
4037 LPWSTR pszColon;
4038 INT len;
4040 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
4042 pszColon = strchrW(buffer, ':');
4043 /* must have two tokens */
4044 if (!pszColon)
4046 HTTP_FreeTokens(pTokenPair);
4047 if (buffer[0])
4048 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
4049 return NULL;
4052 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
4053 if (!pTokenPair[0])
4055 HTTP_FreeTokens(pTokenPair);
4056 return NULL;
4058 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
4059 pTokenPair[0][pszColon - buffer] = '\0';
4061 /* skip colon */
4062 pszColon++;
4063 len = strlenW(pszColon);
4064 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
4065 if (!pTokenPair[1])
4067 HTTP_FreeTokens(pTokenPair);
4068 return NULL;
4070 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
4072 strip_spaces(pTokenPair[0]);
4073 strip_spaces(pTokenPair[1]);
4075 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
4076 return pTokenPair;
4079 /***********************************************************************
4080 * HTTP_ProcessHeader (internal)
4082 * Stuff header into header tables according to <dwModifier>
4086 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4088 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
4090 LPHTTPHEADERW lphttpHdr = NULL;
4091 BOOL bSuccess = FALSE;
4092 INT index = -1;
4093 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
4095 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
4097 /* REPLACE wins out over ADD */
4098 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
4099 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
4101 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
4102 index = -1;
4103 else
4104 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
4106 if (index >= 0)
4108 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
4110 return FALSE;
4112 lphttpHdr = &lpwhr->pCustHeaders[index];
4114 else if (value)
4116 HTTPHEADERW hdr;
4118 hdr.lpszField = (LPWSTR)field;
4119 hdr.lpszValue = (LPWSTR)value;
4120 hdr.wFlags = hdr.wCount = 0;
4122 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4123 hdr.wFlags |= HDR_ISREQUEST;
4125 return HTTP_InsertCustomHeader(lpwhr, &hdr);
4127 /* no value to delete */
4128 else return TRUE;
4130 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4131 lphttpHdr->wFlags |= HDR_ISREQUEST;
4132 else
4133 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
4135 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
4137 HTTP_DeleteCustomHeader( lpwhr, index );
4139 if (value)
4141 HTTPHEADERW hdr;
4143 hdr.lpszField = (LPWSTR)field;
4144 hdr.lpszValue = (LPWSTR)value;
4145 hdr.wFlags = hdr.wCount = 0;
4147 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
4148 hdr.wFlags |= HDR_ISREQUEST;
4150 return HTTP_InsertCustomHeader(lpwhr, &hdr);
4153 return TRUE;
4155 else if (dwModifier & COALESCEFLAGS)
4157 LPWSTR lpsztmp;
4158 WCHAR ch = 0;
4159 INT len = 0;
4160 INT origlen = strlenW(lphttpHdr->lpszValue);
4161 INT valuelen = strlenW(value);
4163 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
4165 ch = ',';
4166 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
4168 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
4170 ch = ';';
4171 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
4174 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
4176 lpsztmp = HeapReAlloc(GetProcessHeap(), 0, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
4177 if (lpsztmp)
4179 lphttpHdr->lpszValue = lpsztmp;
4180 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
4181 if (ch > 0)
4183 lphttpHdr->lpszValue[origlen] = ch;
4184 origlen++;
4185 lphttpHdr->lpszValue[origlen] = ' ';
4186 origlen++;
4189 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
4190 lphttpHdr->lpszValue[len] = '\0';
4191 bSuccess = TRUE;
4193 else
4195 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
4196 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4199 TRACE("<-- %d\n",bSuccess);
4200 return bSuccess;
4204 /***********************************************************************
4205 * HTTP_FinishedReading (internal)
4207 * Called when all content from server has been read by client.
4210 static BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
4212 WCHAR szVersion[10];
4213 WCHAR szConnectionResponse[20];
4214 DWORD dwBufferSize = sizeof(szVersion);
4215 BOOL keepalive = FALSE;
4217 TRACE("\n");
4219 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
4220 * the connection is keep-alive by default */
4221 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_VERSION, szVersion,
4222 &dwBufferSize, NULL) &&
4223 !strcmpiW(szVersion, g_szHttp1_1))
4225 keepalive = TRUE;
4228 dwBufferSize = sizeof(szConnectionResponse);
4229 if (HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_PROXY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL) ||
4230 HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse, &dwBufferSize, NULL))
4232 keepalive = !strcmpiW(szConnectionResponse, szKeepAlive);
4235 if (!keepalive)
4237 HTTPREQ_CloseConnection(&lpwhr->hdr);
4240 /* FIXME: store data in the URL cache here */
4242 return TRUE;
4246 /***********************************************************************
4247 * HTTP_GetCustomHeaderIndex (internal)
4249 * Return index of custom header from header array
4252 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
4253 int requested_index, BOOL request_only)
4255 DWORD index;
4257 TRACE("%s\n", debugstr_w(lpszField));
4259 for (index = 0; index < lpwhr->nCustHeaders; index++)
4261 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
4262 continue;
4264 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4265 continue;
4267 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
4268 continue;
4270 if (requested_index == 0)
4271 break;
4272 requested_index --;
4275 if (index >= lpwhr->nCustHeaders)
4276 index = -1;
4278 TRACE("Return: %d\n", index);
4279 return index;
4283 /***********************************************************************
4284 * HTTP_InsertCustomHeader (internal)
4286 * Insert header into array
4289 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
4291 INT count;
4292 LPHTTPHEADERW lph = NULL;
4293 BOOL r = FALSE;
4295 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
4296 count = lpwhr->nCustHeaders + 1;
4297 if (count > 1)
4298 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
4299 else
4300 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
4302 if (NULL != lph)
4304 lpwhr->pCustHeaders = lph;
4305 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
4306 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
4307 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
4308 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
4309 lpwhr->nCustHeaders++;
4310 r = TRUE;
4312 else
4314 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
4317 return r;
4321 /***********************************************************************
4322 * HTTP_DeleteCustomHeader (internal)
4324 * Delete header from array
4325 * If this function is called, the indexs may change.
4327 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
4329 if( lpwhr->nCustHeaders <= 0 )
4330 return FALSE;
4331 if( index >= lpwhr->nCustHeaders )
4332 return FALSE;
4333 lpwhr->nCustHeaders--;
4335 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[index].lpszField);
4336 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[index].lpszValue);
4338 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
4339 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
4340 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
4342 return TRUE;
4346 /***********************************************************************
4347 * HTTP_VerifyValidHeader (internal)
4349 * Verify the given header is not invalid for the given http request
4352 static BOOL HTTP_VerifyValidHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field)
4354 /* Accept-Encoding is stripped from HTTP/1.0 requests. It is invalid */
4355 if (!strcmpW(lpwhr->lpszVersion, g_szHttp1_0) && !strcmpiW(field, szAccept_Encoding))
4356 return FALSE;
4358 return TRUE;
4361 /***********************************************************************
4362 * IsHostInProxyBypassList (@)
4364 * Undocumented
4367 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
4369 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
4370 return FALSE;