Changes in crossover-wine-src-6.1.0 except for configure
[wine/hacks.git] / dlls / wininet / http.c
blobf1db13ffedc37a0b2d6f8961176b2896e936393a
1 /*
2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
8 * Copyright 2005 Aric Stewart for CodeWeavers
9 * Copyright 2006 Robert Shearman for CodeWeavers
11 * Ulrich Czekalla
12 * David Hammerton
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #include "config.h"
30 #include "wine/port.h"
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_SOCKET_H
34 # include <sys/socket.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 # include <arpa/inet.h>
38 #endif
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #ifdef HAVE_UNISTD_H
43 # include <unistd.h>
44 #endif
45 #include <time.h>
46 #include <assert.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wininet.h"
51 #include "winreg.h"
52 #include "winerror.h"
53 #define NO_SHLWAPI_STREAM
54 #define NO_SHLWAPI_REG
55 #define NO_SHLWAPI_STRFCNS
56 #define NO_SHLWAPI_GDI
57 #include "shlwapi.h"
58 #include "sspi.h"
60 #include "internet.h"
61 #include "wine/debug.h"
62 #include "wine/unicode.h"
64 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
66 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
67 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
68 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
69 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
70 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
71 static const WCHAR szHost[] = { 'H','o','s','t',0 };
72 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
74 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
75 static const WCHAR szKeepAlive[] = {'K','e','e','p','-','A','l','i','v','e',0};
77 #define MAXHOSTNAME 100
78 #define MAX_FIELD_VALUE_LEN 256
79 #define MAX_FIELD_LEN 256
81 #define HTTP_REFERER g_szReferer
82 #define HTTP_ACCEPT g_szAccept
83 #define HTTP_USERAGENT g_szUserAgent
85 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
86 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
87 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
88 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
89 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
90 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
91 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
93 struct HttpAuthInfo
95 LPWSTR scheme;
96 CredHandle cred;
97 CtxtHandle ctx;
98 TimeStamp exp;
99 ULONG attr;
100 void *auth_data;
101 unsigned int auth_data_len;
102 BOOL finished; /* finished authenticating */
105 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
106 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
107 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
108 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
109 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
110 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
111 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
112 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
113 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
114 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
115 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
116 LPCWSTR username, LPCWSTR password );
117 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
118 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
119 lpdwIndex);
120 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
121 static UINT HTTP_DecodeBase64(LPCWSTR base64, LPSTR bin);
124 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
126 int HeaderIndex = 0;
127 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
128 if (HeaderIndex == -1)
129 return NULL;
130 else
131 return &req->pCustHeaders[HeaderIndex];
134 /***********************************************************************
135 * HTTP_Tokenize (internal)
137 * Tokenize a string, allocating memory for the tokens.
139 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
141 LPWSTR * token_array;
142 int tokens = 0;
143 int i;
144 LPCWSTR next_token;
146 /* empty string has no tokens */
147 if (*string)
148 tokens++;
149 /* count tokens */
150 for (i = 0; string[i]; i++)
151 if (!strncmpW(string+i, token_string, strlenW(token_string)))
153 DWORD j;
154 tokens++;
155 /* we want to skip over separators, but not the null terminator */
156 for (j = 0; j < strlenW(token_string) - 1; j++)
157 if (!string[i+j])
158 break;
159 i += j;
162 /* add 1 for terminating NULL */
163 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
164 token_array[tokens] = NULL;
165 if (!tokens)
166 return token_array;
167 for (i = 0; i < tokens; i++)
169 int len;
170 next_token = strstrW(string, token_string);
171 if (!next_token) next_token = string+strlenW(string);
172 len = next_token - string;
173 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
174 memcpy(token_array[i], string, len*sizeof(WCHAR));
175 token_array[i][len] = '\0';
176 string = next_token+strlenW(token_string);
178 return token_array;
181 /***********************************************************************
182 * HTTP_FreeTokens (internal)
184 * Frees memory returned from HTTP_Tokenize.
186 static void HTTP_FreeTokens(LPWSTR * token_array)
188 int i;
189 for (i = 0; token_array[i]; i++)
190 HeapFree(GetProcessHeap(), 0, token_array[i]);
191 HeapFree(GetProcessHeap(), 0, token_array);
194 /* **********************************************************************
196 * Helper functions for the HttpSendRequest(Ex) functions
199 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
201 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
202 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
204 TRACE("%p\n", lpwhr);
206 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
207 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
208 req->dwContentLength, req->bEndRequest);
210 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
213 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
215 /* if the verb is NULL default to GET */
216 if (NULL == lpwhr->lpszVerb)
218 static const WCHAR szGET[] = { 'G','E','T', 0 };
219 lpwhr->lpszVerb = WININET_strdupW(szGET);
223 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
225 static const WCHAR szSlash[] = { '/',0 };
226 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
228 /* If we don't have a path we set it to root */
229 if (NULL == lpwhr->lpszPath)
230 lpwhr->lpszPath = WININET_strdupW(szSlash);
231 else /* remove \r and \n*/
233 int nLen = strlenW(lpwhr->lpszPath);
234 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
236 nLen--;
237 lpwhr->lpszPath[nLen]='\0';
239 /* Replace '\' with '/' */
240 while (nLen>0) {
241 nLen--;
242 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
246 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
247 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
248 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
250 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
251 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
252 *fixurl = '/';
253 strcpyW(fixurl + 1, lpwhr->lpszPath);
254 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
255 lpwhr->lpszPath = fixurl;
259 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
261 LPWSTR requestString;
262 DWORD len, n;
263 LPCWSTR *req;
264 INT i;
265 LPWSTR p;
267 static const WCHAR szSpace[] = { ' ',0 };
268 static const WCHAR szcrlf[] = {'\r','\n', 0};
269 static const WCHAR szColon[] = { ':',' ',0 };
270 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
272 /* allocate space for an array of all the string pointers to be added */
273 len = (lpwhr->nCustHeaders)*4 + 9;
274 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
276 /* add the verb, path and HTTP version string */
277 n = 0;
278 req[n++] = verb;
279 req[n++] = szSpace;
280 req[n++] = path;
281 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
283 /* Append custom request heades */
284 for (i = 0; i < lpwhr->nCustHeaders; i++)
286 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
288 req[n++] = szcrlf;
289 req[n++] = lpwhr->pCustHeaders[i].lpszField;
290 req[n++] = szColon;
291 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
293 TRACE("Adding custom header %s (%s)\n",
294 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
295 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
299 if( n >= len )
300 ERR("oops. buffer overrun\n");
302 req[n] = NULL;
303 requestString = HTTP_build_req( req, 4 );
304 HeapFree( GetProcessHeap(), 0, req );
307 * Set (header) termination string for request
308 * Make sure there's exactly two new lines at the end of the request
310 p = &requestString[strlenW(requestString)-1];
311 while ( (*p == '\n') || (*p == '\r') )
312 p--;
313 strcpyW( p+1, sztwocrlf );
315 return requestString;
318 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
320 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
321 int HeaderIndex;
322 LPHTTPHEADERW setCookieHeader;
324 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
325 if (HeaderIndex == -1)
326 return;
327 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
329 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
331 int nPosStart = 0, nPosEnd = 0, len;
332 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
334 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
336 LPWSTR buf_cookie, cookie_name, cookie_data;
337 LPWSTR buf_url;
338 LPWSTR domain = NULL;
339 LPHTTPHEADERW Host;
341 int nEqualPos = 0;
342 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
343 setCookieHeader->lpszValue[nPosEnd] != '\0')
345 nPosEnd++;
347 if (setCookieHeader->lpszValue[nPosEnd] == ';')
349 /* fixme: not case sensitive, strcasestr is gnu only */
350 int nDomainPosEnd = 0;
351 int nDomainPosStart = 0, nDomainLength = 0;
352 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
353 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
354 if (lpszDomain)
355 { /* they have specified their own domain, lets use it */
356 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
357 lpszDomain[nDomainPosEnd] != '\0')
359 nDomainPosEnd++;
361 nDomainPosStart = strlenW(szDomain);
362 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
363 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
364 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
367 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
368 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
369 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
370 TRACE("%s\n", debugstr_w(buf_cookie));
371 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
373 nEqualPos++;
375 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
377 HeapFree(GetProcessHeap(), 0, buf_cookie);
378 break;
381 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
382 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
383 cookie_data = &buf_cookie[nEqualPos + 1];
385 Host = HTTP_GetHeader(lpwhr,szHost);
386 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
387 strlenW(lpwhr->lpszPath) + 9;
388 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
389 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
390 InternetSetCookieW(buf_url, cookie_name, cookie_data);
392 HeapFree(GetProcessHeap(), 0, buf_url);
393 HeapFree(GetProcessHeap(), 0, buf_cookie);
394 HeapFree(GetProcessHeap(), 0, cookie_name);
395 HeapFree(GetProcessHeap(), 0, domain);
396 nPosStart = nPosEnd;
401 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
403 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
404 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
406 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
407 assert(hIC->hdr.htype == WH_HINIT);
409 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
410 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
411 hIC->lpszProxyPassword);
414 static BOOL HTTP_DoAuthorization( LPWININETHTTPREQW lpwhr, LPCWSTR pszAuthValue )
416 static const WCHAR szBasic[] = {'B','a','s','i','c',0};
417 SECURITY_STATUS sec_status;
418 struct HttpAuthInfo *pAuthInfo = lpwhr->pAuthInfo;
419 LPWSTR password = lpwhr->lpHttpSession->lpszPassword;
420 LPWSTR domain_and_username = lpwhr->lpHttpSession->lpszUserName;
422 TRACE("%s\n", debugstr_w(pszAuthValue));
424 if (!domain_and_username) return FALSE;
426 if (!pAuthInfo)
428 TimeStamp exp;
430 pAuthInfo = HeapAlloc(GetProcessHeap(), 0, sizeof(*pAuthInfo));
431 if (!pAuthInfo)
432 return FALSE;
434 pAuthInfo->scheme = WININET_strdupW(pszAuthValue);
435 if (!pAuthInfo->scheme)
437 HeapFree(GetProcessHeap(), 0, pAuthInfo);
438 return FALSE;
440 SecInvalidateHandle(&pAuthInfo->cred);
441 SecInvalidateHandle(&pAuthInfo->ctx);
442 memset(&pAuthInfo->exp, 0, sizeof(pAuthInfo->exp));
443 pAuthInfo->attr = 0;
444 pAuthInfo->auth_data = NULL;
445 pAuthInfo->auth_data_len = 0;
446 pAuthInfo->finished = FALSE;
448 if (strncmpiW(pszAuthValue, szBasic, sizeof(szBasic)/sizeof(szBasic[0])-1))
450 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity;
451 WCHAR *user = strchrW(domain_and_username, '\\');
452 WCHAR *domain = domain_and_username;
454 if (user) user++;
455 else
457 user = domain_and_username;
458 domain = NULL;
460 nt_auth_identity.Flags = SEC_WINNT_AUTH_IDENTITY_UNICODE;
461 nt_auth_identity.User = user;
462 nt_auth_identity.UserLength = strlenW(nt_auth_identity.User);
463 nt_auth_identity.Domain = domain;
464 nt_auth_identity.DomainLength = domain ? user - domain - 1 : 0;
465 nt_auth_identity.Password = password;
466 nt_auth_identity.PasswordLength = strlenW(nt_auth_identity.Password);
468 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
470 sec_status = AcquireCredentialsHandleW(NULL, pAuthInfo->scheme,
471 SECPKG_CRED_OUTBOUND, NULL,
472 &nt_auth_identity, NULL,
473 NULL, &pAuthInfo->cred,
474 &exp);
475 if (sec_status != SEC_E_OK)
477 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
478 debugstr_w(pAuthInfo->scheme), sec_status);
479 HeapFree(GetProcessHeap(), 0, pAuthInfo->scheme);
480 HeapFree(GetProcessHeap(), 0, pAuthInfo);
481 return FALSE;
484 lpwhr->pAuthInfo = pAuthInfo;
486 else if (pAuthInfo->finished)
487 return FALSE;
489 if (strncmpiW(pszAuthValue, pAuthInfo->scheme, strlenW(pAuthInfo->scheme)))
491 ERR("authentication scheme changed from %s to %s\n",
492 debugstr_w(pAuthInfo->scheme), debugstr_w(pszAuthValue));
493 return FALSE;
496 if (!strncmpW(pszAuthValue, szBasic, sizeof(szBasic)/sizeof(szBasic[0])-1))
498 int userlen = WideCharToMultiByte(CP_UTF8, 0, domain_and_username, lstrlenW(domain_and_username), NULL, 0, NULL, NULL);
499 int passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
500 char *auth_data;
502 TRACE("basic authentication\n");
504 /* length includes a nul terminator, which will be re-used for the ':' */
505 auth_data = HeapAlloc(GetProcessHeap(), 0, userlen + 1 + passlen);
506 if (!auth_data)
507 return FALSE;
509 WideCharToMultiByte(CP_UTF8, 0, domain_and_username, -1, auth_data, userlen, NULL, NULL);
510 auth_data[userlen] = ':';
511 WideCharToMultiByte(CP_UTF8, 0, password, -1, &auth_data[userlen+1], passlen, NULL, NULL);
513 pAuthInfo->auth_data = auth_data;
514 pAuthInfo->auth_data_len = userlen + 1 + passlen;
515 pAuthInfo->finished = TRUE;
517 return TRUE;
519 else
521 LPCWSTR pszAuthData;
522 SecBufferDesc out_desc, in_desc;
523 SecBuffer out, in;
524 unsigned char *buffer;
525 ULONG context_req = ISC_REQ_CONNECTION | ISC_REQ_USE_DCE_STYLE |
526 ISC_REQ_MUTUAL_AUTH | ISC_REQ_DELEGATE;
528 in.BufferType = SECBUFFER_TOKEN;
529 in.cbBuffer = 0;
530 in.pvBuffer = NULL;
532 in_desc.ulVersion = 0;
533 in_desc.cBuffers = 1;
534 in_desc.pBuffers = &in;
536 pszAuthData = pszAuthValue + strlenW(pAuthInfo->scheme);
537 if (*pszAuthData == ' ')
539 pszAuthData++;
540 in.cbBuffer = HTTP_DecodeBase64(pszAuthData, NULL);
541 in.pvBuffer = HeapAlloc(GetProcessHeap(), 0, in.cbBuffer);
542 HTTP_DecodeBase64(pszAuthData, in.pvBuffer);
545 buffer = HeapAlloc(GetProcessHeap(), 0, 0x100);
547 out.BufferType = SECBUFFER_TOKEN;
548 out.cbBuffer = 0x100;
549 out.pvBuffer = buffer;
551 out_desc.ulVersion = 0;
552 out_desc.cBuffers = 1;
553 out_desc.pBuffers = &out;
555 sec_status = InitializeSecurityContextW(&pAuthInfo->cred, NULL, NULL,
556 context_req, 0, SECURITY_NETWORK_DREP,
557 in.pvBuffer ? &in_desc : NULL,
558 0, &pAuthInfo->ctx, &out_desc,
559 &pAuthInfo->attr, &pAuthInfo->exp);
560 if (sec_status == SEC_E_OK)
562 pAuthInfo->finished = TRUE;
563 pAuthInfo->auth_data = out.pvBuffer;
564 pAuthInfo->auth_data_len = out.cbBuffer;
565 TRACE("sending last auth packet\n");
567 else if (sec_status == SEC_I_CONTINUE_NEEDED)
569 pAuthInfo->auth_data = out.pvBuffer;
570 pAuthInfo->auth_data_len = out.cbBuffer;
571 TRACE("sending next auth packet\n");
573 else
575 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status);
576 HeapFree(GetProcessHeap(), 0, out.pvBuffer);
577 return FALSE;
581 return TRUE;
584 /***********************************************************************
585 * HTTP_HttpAddRequestHeadersW (internal)
587 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
588 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
590 LPWSTR lpszStart;
591 LPWSTR lpszEnd;
592 LPWSTR buffer;
593 BOOL bSuccess = FALSE;
594 DWORD len;
596 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
598 if( dwHeaderLength == ~0U )
599 len = strlenW(lpszHeader);
600 else
601 len = dwHeaderLength;
602 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
603 lstrcpynW( buffer, lpszHeader, len + 1);
605 lpszStart = buffer;
609 LPWSTR * pFieldAndValue;
611 lpszEnd = lpszStart;
613 while (*lpszEnd != '\0')
615 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
616 break;
617 lpszEnd++;
620 if (*lpszStart == '\0')
621 break;
623 if (*lpszEnd == '\r')
625 *lpszEnd = '\0';
626 lpszEnd += 2; /* Jump over \r\n */
628 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
629 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
630 if (pFieldAndValue)
632 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
633 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
634 HTTP_FreeTokens(pFieldAndValue);
637 lpszStart = lpszEnd;
638 } while (bSuccess);
640 HeapFree(GetProcessHeap(), 0, buffer);
642 return bSuccess;
645 /***********************************************************************
646 * HttpAddRequestHeadersW (WININET.@)
648 * Adds one or more HTTP header to the request handler
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_w(lpszHeader), dwHeaderLength,
662 dwModifier);
664 if (!lpszHeader)
665 return TRUE;
667 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
668 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
670 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
671 goto lend;
673 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
674 lend:
675 if( lpwhr )
676 WININET_Release( &lpwhr->hdr );
678 return bSuccess;
681 /***********************************************************************
682 * HttpAddRequestHeadersA (WININET.@)
684 * Adds one or more HTTP header to the request handler
686 * RETURNS
687 * TRUE on success
688 * FALSE on failure
691 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
692 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
694 DWORD len;
695 LPWSTR hdr;
696 BOOL r;
698 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
699 dwModifier);
701 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
702 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
703 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
704 if( dwHeaderLength != ~0U )
705 dwHeaderLength = len;
707 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
709 HeapFree( GetProcessHeap(), 0, hdr );
711 return r;
714 /* read any content returned by the server so that the connection can be
715 * resued */
716 static void HTTP_DrainContent(LPWININETHTTPREQW lpwhr)
718 DWORD bytes_read;
721 char buffer[2048];
722 if (!INTERNET_ReadFile(&lpwhr->hdr, buffer, sizeof(buffer), &bytes_read,
723 TRUE, FALSE))
724 return;
725 } while (bytes_read);
728 /***********************************************************************
729 * HttpEndRequestA (WININET.@)
731 * Ends an HTTP request that was started by HttpSendRequestEx
733 * RETURNS
734 * TRUE if successful
735 * FALSE on failure
738 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
739 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
741 LPINTERNET_BUFFERSA ptr;
742 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
743 BOOL rc = FALSE;
745 TRACE("(%p, %p, %08x, %08x): stub\n", hRequest, lpBuffersOut, dwFlags,
746 dwContext);
748 ptr = lpBuffersOut;
749 if (ptr)
750 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
751 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
752 else
753 lpBuffersOutW = NULL;
755 ptrW = lpBuffersOutW;
756 while (ptr)
758 if (ptr->lpvBuffer && ptr->dwBufferLength)
759 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
760 ptrW->dwBufferLength = ptr->dwBufferLength;
761 ptrW->dwBufferTotal= ptr->dwBufferTotal;
763 if (ptr->Next)
764 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
765 sizeof(INTERNET_BUFFERSW));
767 ptr = ptr->Next;
768 ptrW = ptrW->Next;
771 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
773 if (lpBuffersOutW)
775 ptrW = lpBuffersOutW;
776 while (ptrW)
778 LPINTERNET_BUFFERSW ptrW2;
780 FIXME("Do we need to translate info out of these buffer?\n");
782 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
783 ptrW2 = ptrW->Next;
784 HeapFree(GetProcessHeap(),0,ptrW);
785 ptrW = ptrW2;
789 return rc;
792 /***********************************************************************
793 * HttpEndRequestW (WININET.@)
795 * Ends an HTTP request that was started by HttpSendRequestEx
797 * RETURNS
798 * TRUE if successful
799 * FALSE on failure
802 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
803 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
805 BOOL rc = FALSE;
806 LPWININETHTTPREQW lpwhr;
807 INT responseLen;
808 DWORD dwBufferSize;
810 TRACE("-->\n");
811 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
813 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
815 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
816 return FALSE;
819 lpwhr->hdr.dwFlags |= dwFlags;
820 lpwhr->hdr.dwContext = dwContext;
822 /* We appear to do nothing with lpBuffersOut.. is that correct? */
824 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
825 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
827 responseLen = HTTP_GetResponseHeaders(lpwhr);
828 if (responseLen)
829 rc = TRUE;
831 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
832 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
834 /* process headers here. Is this right? */
835 HTTP_ProcessHeaders(lpwhr);
837 dwBufferSize = sizeof(lpwhr->dwContentLength);
838 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
839 &lpwhr->dwContentLength,&dwBufferSize,NULL))
840 lpwhr->dwContentLength = -1;
842 if (lpwhr->dwContentLength == 0)
843 HTTP_FinishedReading(lpwhr);
845 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
847 DWORD dwCode,dwCodeLength=sizeof(DWORD);
848 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
849 (dwCode==302 || dwCode==301))
851 WCHAR szNewLocation[2048];
852 dwBufferSize=sizeof(szNewLocation);
853 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
855 static const WCHAR szGET[] = { 'G','E','T', 0 };
856 /* redirects are always GETs */
857 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
858 lpwhr->lpszVerb = WININET_strdupW(szGET);
859 HTTP_DrainContent(lpwhr);
860 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
861 if (rc)
862 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
867 TRACE("%i <--\n",rc);
868 return rc;
871 /***********************************************************************
872 * HttpOpenRequestW (WININET.@)
874 * Open a HTTP request handle
876 * RETURNS
877 * HINTERNET a HTTP request handle on success
878 * NULL on failure
881 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
882 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
883 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
884 DWORD dwFlags, DWORD dwContext)
886 LPWININETHTTPSESSIONW lpwhs;
887 HINTERNET handle = NULL;
889 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession,
890 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
891 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
892 dwFlags, dwContext);
893 if(lpszAcceptTypes!=NULL)
895 int i;
896 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
897 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
900 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
901 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
903 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
904 goto lend;
908 * My tests seem to show that the windows version does not
909 * become asynchronous until after this point. And anyhow
910 * if this call was asynchronous then how would you get the
911 * necessary HINTERNET pointer returned by this function.
914 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
915 lpszVersion, lpszReferrer, lpszAcceptTypes,
916 dwFlags, dwContext);
917 lend:
918 if( lpwhs )
919 WININET_Release( &lpwhs->hdr );
920 TRACE("returning %p\n", handle);
921 return handle;
925 /***********************************************************************
926 * HttpOpenRequestA (WININET.@)
928 * Open a HTTP request handle
930 * RETURNS
931 * HINTERNET a HTTP request handle on success
932 * NULL on failure
935 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
936 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
937 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
938 DWORD dwFlags, DWORD dwContext)
940 LPWSTR szVerb = NULL, szObjectName = NULL;
941 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
942 INT len;
943 INT acceptTypesCount;
944 HINTERNET rc = FALSE;
945 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession,
946 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
947 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
948 dwFlags, dwContext);
950 if (lpszVerb)
952 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
953 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
954 if ( !szVerb )
955 goto end;
956 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
959 if (lpszObjectName)
961 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
962 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
963 if ( !szObjectName )
964 goto end;
965 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
968 if (lpszVersion)
970 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
971 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
972 if ( !szVersion )
973 goto end;
974 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
977 if (lpszReferrer)
979 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
980 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
981 if ( !szReferrer )
982 goto end;
983 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
986 acceptTypesCount = 0;
987 if (lpszAcceptTypes)
989 /* find out how many there are */
990 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
991 acceptTypesCount++;
992 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
993 acceptTypesCount = 0;
994 while (lpszAcceptTypes[acceptTypesCount] && *lpszAcceptTypes[acceptTypesCount])
996 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
997 -1, NULL, 0 );
998 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
999 if (!szAcceptTypes[acceptTypesCount] )
1000 goto end;
1001 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
1002 -1, szAcceptTypes[acceptTypesCount], len );
1003 acceptTypesCount++;
1005 szAcceptTypes[acceptTypesCount] = NULL;
1007 else szAcceptTypes = 0;
1009 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
1010 szVersion, szReferrer,
1011 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
1013 end:
1014 if (szAcceptTypes)
1016 acceptTypesCount = 0;
1017 while (szAcceptTypes[acceptTypesCount])
1019 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
1020 acceptTypesCount++;
1022 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
1024 HeapFree(GetProcessHeap(), 0, szReferrer);
1025 HeapFree(GetProcessHeap(), 0, szVersion);
1026 HeapFree(GetProcessHeap(), 0, szObjectName);
1027 HeapFree(GetProcessHeap(), 0, szVerb);
1029 return rc;
1032 /***********************************************************************
1033 * HTTP_EncodeBase64
1035 static UINT HTTP_EncodeBase64( LPCSTR bin, unsigned int len, LPWSTR base64 )
1037 UINT n = 0, x;
1038 static const CHAR HTTP_Base64Enc[] =
1039 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1041 while( len > 0 )
1043 /* first 6 bits, all from bin[0] */
1044 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
1045 x = (bin[0] & 3) << 4;
1047 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1048 if( len == 1 )
1050 base64[n++] = HTTP_Base64Enc[x];
1051 base64[n++] = '=';
1052 base64[n++] = '=';
1053 break;
1055 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
1056 x = ( bin[1] & 0x0f ) << 2;
1058 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1059 if( len == 2 )
1061 base64[n++] = HTTP_Base64Enc[x];
1062 base64[n++] = '=';
1063 break;
1065 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
1067 /* last 6 bits, all from bin [2] */
1068 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
1069 bin += 3;
1070 len -= 3;
1072 base64[n] = 0;
1073 return n;
1076 /***********************************************************************
1077 * HTTP_DecodeBase64
1079 static UINT HTTP_DecodeBase64( LPCWSTR base64, LPSTR bin )
1081 static char HTTP_Base64Dec[256];
1082 unsigned int n = 0;
1084 /* compare against last character to be set to avoid a race */
1085 if (HTTP_Base64Dec['/'] != 63)
1087 static char HTTP_Base64Enc[] =
1088 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1089 int i;
1090 memset(HTTP_Base64Dec, -1, sizeof(HTTP_Base64Dec));
1091 for (i = 0; i < strlen(HTTP_Base64Enc); i++)
1092 HTTP_Base64Dec[(unsigned char)HTTP_Base64Enc[i]] = i;
1095 while(*base64)
1097 char in[4];
1099 if (base64[0] > sizeof(HTTP_Base64Dec) ||
1100 ((in[0] = HTTP_Base64Dec[base64[0]]) == -1) ||
1101 base64[1] > sizeof(HTTP_Base64Dec) ||
1102 ((in[1] = HTTP_Base64Dec[base64[1]]) == -1))
1104 WARN("invalid base64: %s\n", debugstr_w(base64));
1105 return 0;
1107 if (bin)
1108 bin[n] = (unsigned char) (in[0] << 2 | in[1] >> 4);
1109 n++;
1111 if ((base64[2] == '=') && (base64[3] == '='))
1112 break;
1113 if (base64[2] > sizeof(HTTP_Base64Dec) ||
1114 ((in[2] = HTTP_Base64Dec[base64[2]]) == -1))
1116 WARN("invalid base64: %s\n", debugstr_w(&base64[2]));
1117 return 0;
1119 if (bin)
1120 bin[n] = (unsigned char) (in[1] << 4 | in[2] >> 2);
1121 n++;
1123 if (base64[3] == '=')
1124 break;
1125 if (base64[3] > sizeof(HTTP_Base64Dec) ||
1126 ((in[3] = HTTP_Base64Dec[base64[3]]) == -1))
1128 WARN("invalid base64: %s\n", debugstr_w(&base64[3]));
1129 return 0;
1131 if (bin)
1132 bin[n] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
1133 n++;
1135 base64 += 4;
1138 return n;
1141 /***********************************************************************
1142 * HTTP_EncodeBasicAuth
1144 * Encode the basic authentication string for HTTP 1.1
1146 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
1148 UINT len;
1149 char *in;
1150 LPWSTR out;
1151 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
1152 int userlen = WideCharToMultiByte(CP_UTF8, 0, username, lstrlenW(username), NULL, 0, NULL, NULL);
1153 int passlen = WideCharToMultiByte(CP_UTF8, 0, password, lstrlenW(password), NULL, 0, NULL, NULL);
1155 in = HeapAlloc( GetProcessHeap(), 0, userlen + 1 + passlen );
1156 if( !in )
1157 return NULL;
1159 len = lstrlenW(szBasic) +
1160 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
1161 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1162 if( out )
1164 WideCharToMultiByte(CP_UTF8, 0, username, -1, in, userlen, NULL, NULL);
1165 in[userlen] = ':';
1166 WideCharToMultiByte(CP_UTF8, 0, password, -1, &in[userlen+1], passlen, NULL, NULL);
1167 lstrcpyW( out, szBasic );
1168 HTTP_EncodeBase64( in, userlen + 1 + passlen, &out[strlenW(out)] );
1170 HeapFree( GetProcessHeap(), 0, in );
1172 return out;
1175 /***********************************************************************
1176 * HTTP_InsertProxyAuthorization
1178 * Insert the basic authorization field in the request header
1180 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
1181 LPCWSTR username, LPCWSTR password )
1183 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
1184 BOOL ret = TRUE;
1186 if (!authorization)
1187 return FALSE;
1189 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
1191 HTTP_ProcessHeader(lpwhr, szProxy_Authorization, authorization,
1192 HTTP_ADDHDR_FLAG_REPLACE);
1194 HeapFree( GetProcessHeap(), 0, authorization );
1196 return ret;
1199 /***********************************************************************
1200 * HTTP_InsertAuthorization
1202 * Insert or delete the authorization field in the request header.
1204 static BOOL HTTP_InsertAuthorization( LPWININETHTTPREQW lpwhr )
1206 WCHAR *authorization = NULL;
1208 if (lpwhr->pAuthInfo && lpwhr->pAuthInfo->auth_data_len)
1210 static const WCHAR wszSpace[] = {' ',0};
1211 unsigned int len;
1213 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1214 len = strlenW(lpwhr->pAuthInfo->scheme)+1+((lpwhr->pAuthInfo->auth_data_len+2)*4)/3;
1215 authorization = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
1216 if (!authorization)
1217 return FALSE;
1219 strcpyW(authorization, lpwhr->pAuthInfo->scheme);
1220 strcatW(authorization, wszSpace);
1221 HTTP_EncodeBase64(lpwhr->pAuthInfo->auth_data,
1222 lpwhr->pAuthInfo->auth_data_len,
1223 authorization+strlenW(authorization));
1225 /* clear the data as it isn't valid now that it has been sent to the
1226 * server */
1227 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
1228 lpwhr->pAuthInfo->auth_data = NULL;
1229 lpwhr->pAuthInfo->auth_data_len = 0;
1232 TRACE("Inserting authorization: %s\n", debugstr_w(authorization));
1234 HTTP_ProcessHeader(lpwhr, szAuthorization, authorization,
1235 HTTP_ADDHDR_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1237 HeapFree(GetProcessHeap(), 0, authorization);
1239 return TRUE;
1242 /***********************************************************************
1243 * HTTP_DealWithProxy
1245 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
1246 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
1248 WCHAR buf[MAXHOSTNAME];
1249 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
1250 WCHAR* url;
1251 static WCHAR szNul[] = { 0 };
1252 URL_COMPONENTSW UrlComponents;
1253 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
1254 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
1255 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
1256 int len;
1258 memset( &UrlComponents, 0, sizeof UrlComponents );
1259 UrlComponents.dwStructSize = sizeof UrlComponents;
1260 UrlComponents.lpszHostName = buf;
1261 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1263 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1264 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
1265 sprintfW(proxy, szFormat1, hIC->lpszProxy);
1266 else
1267 strcpyW(proxy, hIC->lpszProxy);
1268 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
1269 return FALSE;
1270 if( UrlComponents.dwHostNameLength == 0 )
1271 return FALSE;
1273 if( !lpwhr->lpszPath )
1274 lpwhr->lpszPath = szNul;
1275 TRACE("server=%s path=%s\n",
1276 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
1277 /* for constant 15 see above */
1278 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
1279 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1281 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1282 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1284 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
1286 if( lpwhr->lpszPath[0] != '/' )
1287 strcatW( url, szSlash );
1288 strcatW(url, lpwhr->lpszPath);
1289 if(lpwhr->lpszPath != szNul)
1290 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1291 lpwhr->lpszPath = url;
1293 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1294 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
1295 lpwhs->nServerPort = UrlComponents.nPort;
1297 return TRUE;
1300 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
1302 char szaddr[32];
1303 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1305 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1306 INTERNET_STATUS_RESOLVING_NAME,
1307 lpwhs->lpszServerName,
1308 strlenW(lpwhs->lpszServerName)+1);
1310 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1311 &lpwhs->socketAddress))
1313 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1314 return FALSE;
1317 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1318 szaddr, sizeof(szaddr));
1319 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1320 INTERNET_STATUS_NAME_RESOLVED,
1321 szaddr, strlen(szaddr)+1);
1322 return TRUE;
1325 /***********************************************************************
1326 * HTTP_HttpOpenRequestW (internal)
1328 * Open a HTTP request handle
1330 * RETURNS
1331 * HINTERNET a HTTP request handle on success
1332 * NULL on failure
1335 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1336 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1337 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1338 DWORD dwFlags, DWORD dwContext)
1340 LPWININETAPPINFOW hIC = NULL;
1341 LPWININETHTTPREQW lpwhr;
1342 LPWSTR lpszCookies;
1343 LPWSTR lpszUrl = NULL;
1344 DWORD nCookieSize;
1345 HINTERNET handle = NULL;
1346 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
1347 DWORD len;
1348 LPHTTPHEADERW Host;
1350 TRACE("-->\n");
1352 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1353 hIC = lpwhs->lpAppInfo;
1355 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1356 if (NULL == lpwhr)
1358 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1359 goto lend;
1361 lpwhr->hdr.htype = WH_HHTTPREQ;
1362 lpwhr->hdr.dwFlags = dwFlags;
1363 lpwhr->hdr.dwContext = dwContext;
1364 lpwhr->hdr.dwRefCount = 1;
1365 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1366 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1367 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
1369 WININET_AddRef( &lpwhs->hdr );
1370 lpwhr->lpHttpSession = lpwhs;
1372 handle = WININET_AllocHandle( &lpwhr->hdr );
1373 if (NULL == handle)
1375 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1376 goto lend;
1379 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1381 InternetCloseHandle( handle );
1382 handle = NULL;
1383 goto lend;
1386 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1387 HRESULT rc;
1389 len = 0;
1390 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1391 if (rc != E_POINTER)
1392 len = strlenW(lpszObjectName)+1;
1393 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1394 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1395 URL_ESCAPE_SPACES_ONLY);
1396 if (rc)
1398 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
1399 strcpyW(lpwhr->lpszPath,lpszObjectName);
1403 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1404 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1406 if (lpszAcceptTypes)
1408 int i;
1409 for (i = 0; lpszAcceptTypes[i]; i++)
1411 if (!*lpszAcceptTypes[i]) continue;
1412 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i],
1413 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA |
1414 HTTP_ADDHDR_FLAG_REQ |
1415 (i == 0 ? HTTP_ADDHDR_FLAG_REPLACE : 0));
1419 if (NULL == lpszVerb)
1421 static const WCHAR szGet[] = {'G','E','T',0};
1422 lpwhr->lpszVerb = WININET_strdupW(szGet);
1424 else if (strlenW(lpszVerb))
1425 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1427 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1429 WCHAR buf[MAXHOSTNAME];
1430 URL_COMPONENTSW UrlComponents;
1432 memset( &UrlComponents, 0, sizeof UrlComponents );
1433 UrlComponents.dwStructSize = sizeof UrlComponents;
1434 UrlComponents.lpszHostName = buf;
1435 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1437 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1438 if (strlenW(UrlComponents.lpszHostName))
1439 HTTP_ProcessHeader(lpwhr, szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1441 else
1442 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1444 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1445 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1446 INTERNET_DEFAULT_HTTPS_PORT :
1447 INTERNET_DEFAULT_HTTP_PORT);
1448 lpwhs->nHostPort = lpwhs->nServerPort;
1450 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1451 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1453 if (hIC->lpszAgent)
1455 WCHAR *agent_header;
1456 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1458 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1459 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1460 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1462 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1463 HTTP_ADDREQ_FLAG_ADD);
1464 HeapFree(GetProcessHeap(), 0, agent_header);
1467 Host = HTTP_GetHeader(lpwhr,szHost);
1469 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1470 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1471 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1473 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1474 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1476 int cnt = 0;
1477 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1478 static const WCHAR szcrlf[] = {'\r','\n',0};
1480 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1482 cnt += sprintfW(lpszCookies, szCookie);
1483 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1484 strcatW(lpszCookies, szcrlf);
1486 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1487 HTTP_ADDREQ_FLAG_ADD);
1488 HeapFree(GetProcessHeap(), 0, lpszCookies);
1490 HeapFree(GetProcessHeap(), 0, lpszUrl);
1493 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1494 INTERNET_STATUS_HANDLE_CREATED, &handle,
1495 sizeof(handle));
1498 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1501 if (!HTTP_ResolveName(lpwhr))
1503 InternetCloseHandle( handle );
1504 handle = NULL;
1507 lend:
1508 if( lpwhr )
1509 WININET_Release( &lpwhr->hdr );
1511 TRACE("<-- %p (%p)\n", handle, lpwhr);
1512 return handle;
1515 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1516 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1517 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1518 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1519 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1520 static const WCHAR szAge[] = { 'A','g','e',0 };
1521 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1522 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1523 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1524 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1525 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1526 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1527 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1528 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1529 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1530 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1531 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1532 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 };
1533 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1534 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1535 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1536 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1537 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1538 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1539 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1540 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1541 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1542 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1543 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1544 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1545 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1546 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1547 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1548 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1549 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1550 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1551 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1552 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1553 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1554 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1555 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1556 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1557 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1558 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1559 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 };
1560 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1561 static const WCHAR szURI[] = { 'U','R','I',0 };
1562 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1563 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1564 static const WCHAR szVia[] = { 'V','i','a',0 };
1565 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1566 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1568 static const LPCWSTR header_lookup[] = {
1569 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
1570 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1571 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1572 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
1573 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1574 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1575 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1576 szAllow, /* HTTP_QUERY_ALLOW = 7 */
1577 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
1578 szDate, /* HTTP_QUERY_DATE = 9 */
1579 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
1580 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
1581 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
1582 szURI, /* HTTP_QUERY_URI = 13 */
1583 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
1584 NULL, /* HTTP_QUERY_COST = 15 */
1585 NULL, /* HTTP_QUERY_LINK = 16 */
1586 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
1587 NULL, /* HTTP_QUERY_VERSION = 18 */
1588 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
1589 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
1590 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
1591 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
1592 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
1593 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
1594 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
1595 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
1596 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
1597 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
1598 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
1599 NULL, /* HTTP_QUERY_FORWARDED = 30 */
1600 NULL, /* HTTP_QUERY_FROM = 31 */
1601 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
1602 szLocation, /* HTTP_QUERY_LOCATION = 33 */
1603 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
1604 szReferer, /* HTTP_QUERY_REFERER = 35 */
1605 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
1606 szServer, /* HTTP_QUERY_SERVER = 37 */
1607 NULL, /* HTTP_TITLE = 38 */
1608 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
1609 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
1610 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
1611 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
1612 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
1613 szCookie, /* HTTP_QUERY_COOKIE = 44 */
1614 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
1615 NULL, /* HTTP_QUERY_REFRESH = 46 */
1616 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
1617 szAge, /* HTTP_QUERY_AGE = 48 */
1618 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
1619 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
1620 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
1621 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
1622 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
1623 szETag, /* HTTP_QUERY_ETAG = 54 */
1624 szHost, /* HTTP_QUERY_HOST = 55 */
1625 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
1626 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
1627 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
1628 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
1629 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
1630 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
1631 szRange, /* HTTP_QUERY_RANGE = 62 */
1632 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
1633 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
1634 szVary, /* HTTP_QUERY_VARY = 65 */
1635 szVia, /* HTTP_QUERY_VIA = 66 */
1636 szWarning, /* HTTP_QUERY_WARNING = 67 */
1637 szExpect, /* HTTP_QUERY_EXPECT = 68 */
1638 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
1639 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
1642 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
1644 /***********************************************************************
1645 * HTTP_HttpQueryInfoW (internal)
1647 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1648 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1650 LPHTTPHEADERW lphttpHdr = NULL;
1651 BOOL bSuccess = FALSE;
1652 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1653 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
1654 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
1655 INT index = -1;
1657 /* Find requested header structure */
1658 switch (level)
1660 case HTTP_QUERY_CUSTOM:
1661 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
1662 break;
1664 case HTTP_QUERY_RAW_HEADERS_CRLF:
1666 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1667 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1669 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1670 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1671 return FALSE;
1673 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1674 *lpdwBufferLength = len * sizeof(WCHAR);
1676 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1678 return TRUE;
1680 case HTTP_QUERY_RAW_HEADERS:
1682 static const WCHAR szCrLf[] = {'\r','\n',0};
1683 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1684 DWORD i, size = 0;
1685 LPWSTR pszString = (WCHAR*)lpBuffer;
1687 for (i = 0; ppszRawHeaderLines[i]; i++)
1688 size += strlenW(ppszRawHeaderLines[i]) + 1;
1690 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1692 HTTP_FreeTokens(ppszRawHeaderLines);
1693 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1694 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1695 return FALSE;
1698 for (i = 0; ppszRawHeaderLines[i]; i++)
1700 DWORD len = strlenW(ppszRawHeaderLines[i]);
1701 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1702 pszString += len+1;
1704 *pszString = '\0';
1706 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1708 *lpdwBufferLength = size * sizeof(WCHAR);
1709 HTTP_FreeTokens(ppszRawHeaderLines);
1711 return TRUE;
1713 case HTTP_QUERY_STATUS_TEXT:
1714 if (lpwhr->lpszStatusText)
1716 DWORD len = strlenW(lpwhr->lpszStatusText);
1717 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1719 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1720 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1721 return FALSE;
1723 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1724 *lpdwBufferLength = len * sizeof(WCHAR);
1726 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1728 return TRUE;
1730 break;
1731 case HTTP_QUERY_VERSION:
1732 if (lpwhr->lpszVersion)
1734 DWORD len = strlenW(lpwhr->lpszVersion);
1735 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1737 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1738 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1739 return FALSE;
1741 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1742 *lpdwBufferLength = len * sizeof(WCHAR);
1744 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1746 return TRUE;
1748 break;
1749 default:
1750 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
1752 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
1753 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
1754 requested_index,request_only);
1757 if (index >= 0)
1758 lphttpHdr = &lpwhr->pCustHeaders[index];
1760 /* Ensure header satisifies requested attributes */
1761 if (!lphttpHdr ||
1762 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1763 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
1765 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1766 return bSuccess;
1769 if (lpdwIndex)
1770 (*lpdwIndex)++;
1772 /* coalesce value to reuqested type */
1773 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1775 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1776 bSuccess = TRUE;
1778 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1780 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1782 time_t tmpTime;
1783 struct tm tmpTM;
1784 SYSTEMTIME *STHook;
1786 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1788 tmpTM = *gmtime(&tmpTime);
1789 STHook = (SYSTEMTIME *) lpBuffer;
1790 if(STHook==NULL)
1791 return bSuccess;
1793 STHook->wDay = tmpTM.tm_mday;
1794 STHook->wHour = tmpTM.tm_hour;
1795 STHook->wMilliseconds = 0;
1796 STHook->wMinute = tmpTM.tm_min;
1797 STHook->wDayOfWeek = tmpTM.tm_wday;
1798 STHook->wMonth = tmpTM.tm_mon + 1;
1799 STHook->wSecond = tmpTM.tm_sec;
1800 STHook->wYear = tmpTM.tm_year;
1802 bSuccess = TRUE;
1804 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1805 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1806 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1808 else if (lphttpHdr->lpszValue)
1810 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1812 if (len > *lpdwBufferLength)
1814 *lpdwBufferLength = len;
1815 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1816 return bSuccess;
1819 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1820 *lpdwBufferLength = len - sizeof(WCHAR);
1821 bSuccess = TRUE;
1823 TRACE(" returning string : %s\n", debugstr_w(lpBuffer));
1825 return bSuccess;
1828 /***********************************************************************
1829 * HttpQueryInfoW (WININET.@)
1831 * Queries for information about an HTTP request
1833 * RETURNS
1834 * TRUE on success
1835 * FALSE on failure
1838 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1839 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1841 BOOL bSuccess = FALSE;
1842 LPWININETHTTPREQW lpwhr;
1844 if (TRACE_ON(wininet)) {
1845 #define FE(x) { x, #x }
1846 static const wininet_flag_info query_flags[] = {
1847 FE(HTTP_QUERY_MIME_VERSION),
1848 FE(HTTP_QUERY_CONTENT_TYPE),
1849 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1850 FE(HTTP_QUERY_CONTENT_ID),
1851 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1852 FE(HTTP_QUERY_CONTENT_LENGTH),
1853 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1854 FE(HTTP_QUERY_ALLOW),
1855 FE(HTTP_QUERY_PUBLIC),
1856 FE(HTTP_QUERY_DATE),
1857 FE(HTTP_QUERY_EXPIRES),
1858 FE(HTTP_QUERY_LAST_MODIFIED),
1859 FE(HTTP_QUERY_MESSAGE_ID),
1860 FE(HTTP_QUERY_URI),
1861 FE(HTTP_QUERY_DERIVED_FROM),
1862 FE(HTTP_QUERY_COST),
1863 FE(HTTP_QUERY_LINK),
1864 FE(HTTP_QUERY_PRAGMA),
1865 FE(HTTP_QUERY_VERSION),
1866 FE(HTTP_QUERY_STATUS_CODE),
1867 FE(HTTP_QUERY_STATUS_TEXT),
1868 FE(HTTP_QUERY_RAW_HEADERS),
1869 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1870 FE(HTTP_QUERY_CONNECTION),
1871 FE(HTTP_QUERY_ACCEPT),
1872 FE(HTTP_QUERY_ACCEPT_CHARSET),
1873 FE(HTTP_QUERY_ACCEPT_ENCODING),
1874 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1875 FE(HTTP_QUERY_AUTHORIZATION),
1876 FE(HTTP_QUERY_CONTENT_ENCODING),
1877 FE(HTTP_QUERY_FORWARDED),
1878 FE(HTTP_QUERY_FROM),
1879 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1880 FE(HTTP_QUERY_LOCATION),
1881 FE(HTTP_QUERY_ORIG_URI),
1882 FE(HTTP_QUERY_REFERER),
1883 FE(HTTP_QUERY_RETRY_AFTER),
1884 FE(HTTP_QUERY_SERVER),
1885 FE(HTTP_QUERY_TITLE),
1886 FE(HTTP_QUERY_USER_AGENT),
1887 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1888 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1889 FE(HTTP_QUERY_ACCEPT_RANGES),
1890 FE(HTTP_QUERY_SET_COOKIE),
1891 FE(HTTP_QUERY_COOKIE),
1892 FE(HTTP_QUERY_REQUEST_METHOD),
1893 FE(HTTP_QUERY_REFRESH),
1894 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1895 FE(HTTP_QUERY_AGE),
1896 FE(HTTP_QUERY_CACHE_CONTROL),
1897 FE(HTTP_QUERY_CONTENT_BASE),
1898 FE(HTTP_QUERY_CONTENT_LOCATION),
1899 FE(HTTP_QUERY_CONTENT_MD5),
1900 FE(HTTP_QUERY_CONTENT_RANGE),
1901 FE(HTTP_QUERY_ETAG),
1902 FE(HTTP_QUERY_HOST),
1903 FE(HTTP_QUERY_IF_MATCH),
1904 FE(HTTP_QUERY_IF_NONE_MATCH),
1905 FE(HTTP_QUERY_IF_RANGE),
1906 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1907 FE(HTTP_QUERY_MAX_FORWARDS),
1908 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1909 FE(HTTP_QUERY_RANGE),
1910 FE(HTTP_QUERY_TRANSFER_ENCODING),
1911 FE(HTTP_QUERY_UPGRADE),
1912 FE(HTTP_QUERY_VARY),
1913 FE(HTTP_QUERY_VIA),
1914 FE(HTTP_QUERY_WARNING),
1915 FE(HTTP_QUERY_CUSTOM)
1917 static const wininet_flag_info modifier_flags[] = {
1918 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1919 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1920 FE(HTTP_QUERY_FLAG_NUMBER),
1921 FE(HTTP_QUERY_FLAG_COALESCE)
1923 #undef FE
1924 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1925 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1926 DWORD i;
1928 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1929 TRACE(" Attribute:");
1930 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1931 if (query_flags[i].val == info) {
1932 TRACE(" %s", query_flags[i].name);
1933 break;
1936 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1937 TRACE(" Unknown (%08x)", info);
1940 TRACE(" Modifier:");
1941 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1942 if (modifier_flags[i].val & info_mod) {
1943 TRACE(" %s", modifier_flags[i].name);
1944 info_mod &= ~ modifier_flags[i].val;
1948 if (info_mod) {
1949 TRACE(" Unknown (%08x)", info_mod);
1951 TRACE("\n");
1954 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1955 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1957 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1958 goto lend;
1961 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1962 lpBuffer, lpdwBufferLength, lpdwIndex);
1964 lend:
1965 if( lpwhr )
1966 WININET_Release( &lpwhr->hdr );
1968 TRACE("%d <--\n", bSuccess);
1969 return bSuccess;
1972 /***********************************************************************
1973 * HttpQueryInfoA (WININET.@)
1975 * Queries for information about an HTTP request
1977 * RETURNS
1978 * TRUE on success
1979 * FALSE on failure
1982 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1983 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1985 BOOL result;
1986 DWORD len;
1987 WCHAR* bufferW;
1989 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1990 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1992 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1993 lpdwBufferLength, lpdwIndex );
1996 len = (*lpdwBufferLength)*sizeof(WCHAR);
1997 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1998 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1999 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
2000 MultiByteToWideChar(CP_ACP,0,lpBuffer,-1,bufferW,len);
2001 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
2002 &len, lpdwIndex );
2003 if( result )
2005 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
2006 lpBuffer, *lpdwBufferLength, NULL, NULL );
2007 *lpdwBufferLength = len - 1;
2009 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
2011 else
2012 /* since the strings being returned from HttpQueryInfoW should be
2013 * only ASCII characters, it is reasonable to assume that all of
2014 * the Unicode characters can be reduced to a single byte */
2015 *lpdwBufferLength = len / sizeof(WCHAR);
2017 HeapFree(GetProcessHeap(), 0, bufferW );
2019 return result;
2022 /***********************************************************************
2023 * HttpSendRequestExA (WININET.@)
2025 * Sends the specified request to the HTTP server and allows chunked
2026 * transfers.
2028 * RETURNS
2029 * Success: TRUE
2030 * Failure: FALSE, call GetLastError() for more information.
2032 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
2033 LPINTERNET_BUFFERSA lpBuffersIn,
2034 LPINTERNET_BUFFERSA lpBuffersOut,
2035 DWORD dwFlags, DWORD dwContext)
2037 INTERNET_BUFFERSW BuffersInW;
2038 BOOL rc = FALSE;
2039 DWORD headerlen;
2040 LPWSTR header = NULL;
2042 TRACE("(%p, %p, %p, %08x, %08x): stub\n", hRequest, lpBuffersIn,
2043 lpBuffersOut, dwFlags, dwContext);
2045 if (lpBuffersIn)
2047 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
2048 if (lpBuffersIn->lpcszHeader)
2050 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
2051 lpBuffersIn->dwHeadersLength,0,0);
2052 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
2053 if (!(BuffersInW.lpcszHeader = header))
2055 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2056 return FALSE;
2058 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
2059 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2060 header, headerlen);
2062 else
2063 BuffersInW.lpcszHeader = NULL;
2064 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
2065 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
2066 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
2067 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
2068 BuffersInW.Next = NULL;
2071 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
2073 HeapFree(GetProcessHeap(),0,header);
2075 return rc;
2078 /***********************************************************************
2079 * HttpSendRequestExW (WININET.@)
2081 * Sends the specified request to the HTTP server and allows chunked
2082 * transfers
2084 * RETURNS
2085 * Success: TRUE
2086 * Failure: FALSE, call GetLastError() for more information.
2088 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
2089 LPINTERNET_BUFFERSW lpBuffersIn,
2090 LPINTERNET_BUFFERSW lpBuffersOut,
2091 DWORD dwFlags, DWORD dwContext)
2093 BOOL ret;
2094 LPWININETHTTPREQW lpwhr;
2095 LPWININETHTTPSESSIONW lpwhs;
2096 LPWININETAPPINFOW hIC;
2098 TRACE("(%p, %p, %p, %08x, %08x)\n", hRequest, lpBuffersIn,
2099 lpBuffersOut, dwFlags, dwContext);
2101 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
2103 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2105 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2106 return FALSE;
2109 lpwhs = lpwhr->lpHttpSession;
2110 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
2111 hIC = lpwhs->lpAppInfo;
2112 assert(hIC->hdr.htype == WH_HINIT);
2114 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2116 WORKREQUEST workRequest;
2117 struct WORKREQ_HTTPSENDREQUESTW *req;
2119 workRequest.asyncproc = AsyncHttpSendRequestProc;
2120 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2121 req = &workRequest.u.HttpSendRequestW;
2122 if (lpBuffersIn)
2124 if (lpBuffersIn->lpcszHeader)
2125 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
2126 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
2127 else
2128 req->lpszHeader = NULL;
2129 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
2130 req->lpOptional = lpBuffersIn->lpvBuffer;
2131 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
2132 req->dwContentLength = lpBuffersIn->dwBufferTotal;
2134 else
2136 req->lpszHeader = NULL;
2137 req->dwHeaderLength = 0;
2138 req->lpOptional = NULL;
2139 req->dwOptionalLength = 0;
2140 req->dwContentLength = 0;
2143 req->bEndRequest = FALSE;
2145 INTERNET_AsyncCall(&workRequest);
2147 * This is from windows.
2149 INTERNET_SetLastError(ERROR_IO_PENDING);
2150 ret = FALSE;
2152 else
2154 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
2155 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
2156 lpBuffersIn->dwBufferTotal, FALSE);
2159 WININET_Release(&lpwhr->hdr);
2160 TRACE("<---\n");
2161 return ret;
2164 /***********************************************************************
2165 * HttpSendRequestW (WININET.@)
2167 * Sends the specified request to the HTTP server
2169 * RETURNS
2170 * TRUE on success
2171 * FALSE on failure
2174 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
2175 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2177 LPWININETHTTPREQW lpwhr;
2178 LPWININETHTTPSESSIONW lpwhs = NULL;
2179 LPWININETAPPINFOW hIC = NULL;
2180 BOOL r;
2182 TRACE("%p, %p (%s), %i, %p, %i)\n", hHttpRequest,
2183 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
2185 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
2186 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2188 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2189 r = FALSE;
2190 goto lend;
2193 lpwhs = lpwhr->lpHttpSession;
2194 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
2196 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2197 r = FALSE;
2198 goto lend;
2201 hIC = lpwhs->lpAppInfo;
2202 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
2204 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
2205 r = FALSE;
2206 goto lend;
2209 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
2211 WORKREQUEST workRequest;
2212 struct WORKREQ_HTTPSENDREQUESTW *req;
2214 workRequest.asyncproc = AsyncHttpSendRequestProc;
2215 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
2216 req = &workRequest.u.HttpSendRequestW;
2217 if (lpszHeaders)
2218 req->lpszHeader = WININET_strdupW(lpszHeaders);
2219 else
2220 req->lpszHeader = 0;
2221 req->dwHeaderLength = dwHeaderLength;
2222 req->lpOptional = lpOptional;
2223 req->dwOptionalLength = dwOptionalLength;
2224 req->dwContentLength = dwOptionalLength;
2225 req->bEndRequest = TRUE;
2227 INTERNET_AsyncCall(&workRequest);
2229 * This is from windows.
2231 INTERNET_SetLastError(ERROR_IO_PENDING);
2232 r = FALSE;
2234 else
2236 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
2237 dwHeaderLength, lpOptional, dwOptionalLength,
2238 dwOptionalLength, TRUE);
2240 lend:
2241 if( lpwhr )
2242 WININET_Release( &lpwhr->hdr );
2243 return r;
2246 /***********************************************************************
2247 * HttpSendRequestA (WININET.@)
2249 * Sends the specified request to the HTTP server
2251 * RETURNS
2252 * TRUE on success
2253 * FALSE on failure
2256 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
2257 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
2259 BOOL result;
2260 LPWSTR szHeaders=NULL;
2261 DWORD nLen=dwHeaderLength;
2262 if(lpszHeaders!=NULL)
2264 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
2265 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
2266 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
2268 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
2269 HeapFree(GetProcessHeap(),0,szHeaders);
2270 return result;
2273 /***********************************************************************
2274 * HTTP_HandleRedirect (internal)
2276 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
2278 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2279 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
2280 WCHAR path[2048];
2282 if(lpszUrl[0]=='/')
2284 /* if it's an absolute path, keep the same session info */
2285 lstrcpynW(path, lpszUrl, 2048);
2287 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
2289 TRACE("Redirect through proxy\n");
2290 lstrcpynW(path, lpszUrl, 2048);
2292 else
2294 URL_COMPONENTSW urlComponents;
2295 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
2296 static WCHAR szHttp[] = {'h','t','t','p',0};
2297 static WCHAR szHttps[] = {'h','t','t','p','s',0};
2298 DWORD url_length = 0;
2299 LPWSTR orig_url;
2300 LPWSTR combined_url;
2302 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2303 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
2304 urlComponents.dwSchemeLength = 0;
2305 urlComponents.lpszHostName = lpwhs->lpszHostName;
2306 urlComponents.dwHostNameLength = 0;
2307 urlComponents.nPort = lpwhs->nHostPort;
2308 urlComponents.lpszUserName = lpwhs->lpszUserName;
2309 urlComponents.dwUserNameLength = 0;
2310 urlComponents.lpszPassword = NULL;
2311 urlComponents.dwPasswordLength = 0;
2312 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2313 urlComponents.dwUrlPathLength = 0;
2314 urlComponents.lpszExtraInfo = NULL;
2315 urlComponents.dwExtraInfoLength = 0;
2317 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2318 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2319 return FALSE;
2321 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
2323 /* convert from bytes to characters */
2324 url_length = url_length / sizeof(WCHAR) - 1;
2325 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2327 HeapFree(GetProcessHeap(), 0, orig_url);
2328 return FALSE;
2331 url_length = 0;
2332 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2333 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2335 HeapFree(GetProcessHeap(), 0, orig_url);
2336 return FALSE;
2338 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2340 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2342 HeapFree(GetProcessHeap(), 0, orig_url);
2343 HeapFree(GetProcessHeap(), 0, combined_url);
2344 return FALSE;
2346 HeapFree(GetProcessHeap(), 0, orig_url);
2348 userName[0] = 0;
2349 hostName[0] = 0;
2350 protocol[0] = 0;
2352 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2353 urlComponents.lpszScheme = protocol;
2354 urlComponents.dwSchemeLength = 32;
2355 urlComponents.lpszHostName = hostName;
2356 urlComponents.dwHostNameLength = MAXHOSTNAME;
2357 urlComponents.lpszUserName = userName;
2358 urlComponents.dwUserNameLength = 1024;
2359 urlComponents.lpszPassword = NULL;
2360 urlComponents.dwPasswordLength = 0;
2361 urlComponents.lpszUrlPath = path;
2362 urlComponents.dwUrlPathLength = 2048;
2363 urlComponents.lpszExtraInfo = NULL;
2364 urlComponents.dwExtraInfoLength = 0;
2365 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2367 HeapFree(GetProcessHeap(), 0, combined_url);
2368 return FALSE;
2370 HeapFree(GetProcessHeap(), 0, combined_url);
2372 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2373 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2375 TRACE("redirect from secure page to non-secure page\n");
2376 /* FIXME: warn about from secure redirect to non-secure page */
2377 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2379 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2380 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2382 TRACE("redirect from non-secure page to secure page\n");
2383 /* FIXME: notify about redirect to secure page */
2384 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2387 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2389 if (lstrlenW(protocol)>4) /*https*/
2390 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2391 else /*http*/
2392 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2395 #if 0
2397 * This upsets redirects to binary files on sourceforge.net
2398 * and gives an html page instead of the target file
2399 * Examination of the HTTP request sent by native wininet.dll
2400 * reveals that it doesn't send a referrer in that case.
2401 * Maybe there's a flag that enables this, or maybe a referrer
2402 * shouldn't be added in case of a redirect.
2405 /* consider the current host as the referrer */
2406 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2407 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2408 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2409 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2410 #endif
2412 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2413 lpwhs->lpszServerName = WININET_strdupW(hostName);
2414 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2415 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2416 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2418 int len;
2419 static const WCHAR fmt[] = {'%','s',':','%','i',0};
2420 len = lstrlenW(hostName);
2421 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2422 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2423 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2425 else
2426 lpwhs->lpszHostName = WININET_strdupW(hostName);
2428 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2431 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2432 lpwhs->lpszUserName = NULL;
2433 if (userName[0])
2434 lpwhs->lpszUserName = WININET_strdupW(userName);
2435 lpwhs->nServerPort = urlComponents.nPort;
2437 if (!HTTP_ResolveName(lpwhr))
2438 return FALSE;
2440 NETCON_close(&lpwhr->netConnection);
2442 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2443 return FALSE;
2446 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2447 lpwhr->lpszPath=NULL;
2448 if (strlenW(path))
2450 DWORD needed = 0;
2451 HRESULT rc;
2453 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2454 if (rc != E_POINTER)
2455 needed = strlenW(path)+1;
2456 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2457 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2458 URL_ESCAPE_SPACES_ONLY);
2459 if (rc)
2461 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
2462 strcpyW(lpwhr->lpszPath,path);
2466 return TRUE;
2469 /***********************************************************************
2470 * HTTP_build_req (internal)
2472 * concatenate all the strings in the request together
2474 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2476 LPCWSTR *t;
2477 LPWSTR str;
2479 for( t = list; *t ; t++ )
2480 len += strlenW( *t );
2481 len++;
2483 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2484 *str = 0;
2486 for( t = list; *t ; t++ )
2487 strcatW( str, *t );
2489 return str;
2492 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2494 LPWSTR lpszPath;
2495 LPWSTR requestString;
2496 INT len;
2497 INT cnt;
2498 INT responseLen;
2499 char *ascii_req;
2500 BOOL ret;
2501 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2502 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2503 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2505 TRACE("\n");
2507 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2508 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2509 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2510 HeapFree( GetProcessHeap(), 0, lpszPath );
2512 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2513 NULL, 0, NULL, NULL );
2514 len--; /* the nul terminator isn't needed */
2515 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2516 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2517 ascii_req, len, NULL, NULL );
2518 HeapFree( GetProcessHeap(), 0, requestString );
2520 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2522 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2523 HeapFree( GetProcessHeap(), 0, ascii_req );
2524 if (!ret || cnt < 0)
2525 return FALSE;
2527 responseLen = HTTP_GetResponseHeaders( lpwhr );
2528 if (!responseLen)
2529 return FALSE;
2531 return TRUE;
2534 /***********************************************************************
2535 * HTTP_HttpSendRequestW (internal)
2537 * Sends the specified request to the HTTP server
2539 * RETURNS
2540 * TRUE on success
2541 * FALSE on failure
2544 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2545 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2546 DWORD dwContentLength, BOOL bEndRequest)
2548 INT cnt;
2549 BOOL bSuccess = FALSE;
2550 LPWSTR requestString = NULL;
2551 INT responseLen;
2552 BOOL loop_next;
2553 INTERNET_ASYNC_RESULT iar;
2554 static const WCHAR szClose[] = { 'C','l','o','s','e',0 };
2556 TRACE("--> %p\n", lpwhr);
2558 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2560 /* Clear any error information */
2561 INTERNET_SetLastError(0);
2563 HTTP_FixVerb(lpwhr);
2565 /* if we are using optional stuff, we must add the fixed header of that option length */
2566 if (dwContentLength > 0)
2568 static const WCHAR szContentLength[] = {
2569 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2570 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2571 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2572 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L,
2573 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2578 DWORD len;
2579 char *ascii_req;
2581 loop_next = FALSE;
2583 /* like native, just in case the caller forgot to call InternetReadFile
2584 * for all the data */
2585 HTTP_DrainContent(lpwhr);
2586 lpwhr->dwContentRead = 0;
2588 if (TRACE_ON(wininet))
2590 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
2591 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2594 HTTP_FixURL(lpwhr);
2596 /* add the headers the caller supplied */
2597 if( lpszHeaders && dwHeaderLength )
2599 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2600 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2603 HTTP_ProcessHeader(lpwhr, szConnection,
2604 lpwhr->hdr.dwFlags & INTERNET_FLAG_KEEP_CONNECTION ? szKeepAlive : szClose,
2605 HTTP_ADDHDR_FLAG_REQ | HTTP_ADDHDR_FLAG_REPLACE);
2607 HTTP_InsertAuthorization(lpwhr);
2608 /* if there's a proxy username and password, add it to the headers */
2609 HTTP_AddProxyInfo(lpwhr);
2611 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2613 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2615 /* Send the request and store the results */
2616 if (!HTTP_OpenConnection(lpwhr))
2617 goto lend;
2619 /* send the request as ASCII, tack on the optional data */
2620 if( !lpOptional )
2621 dwOptionalLength = 0;
2622 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2623 NULL, 0, NULL, NULL );
2624 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2625 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2626 ascii_req, len, NULL, NULL );
2627 if( lpOptional )
2628 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2629 len = (len + dwOptionalLength - 1);
2630 ascii_req[len] = 0;
2631 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2633 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2634 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2636 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2637 HeapFree( GetProcessHeap(), 0, ascii_req );
2639 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2640 INTERNET_STATUS_REQUEST_SENT,
2641 &len, sizeof(DWORD));
2643 if (bEndRequest)
2645 DWORD dwBufferSize;
2647 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2648 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2650 if (cnt < 0)
2651 goto lend;
2653 responseLen = HTTP_GetResponseHeaders(lpwhr);
2654 if (responseLen)
2655 bSuccess = TRUE;
2657 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2658 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2659 sizeof(DWORD));
2661 HTTP_ProcessHeaders(lpwhr);
2663 dwBufferSize = sizeof(lpwhr->dwContentLength);
2664 if (!HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_CONTENT_LENGTH,
2665 &lpwhr->dwContentLength,&dwBufferSize,NULL))
2666 lpwhr->dwContentLength = -1;
2668 if (lpwhr->dwContentLength == 0)
2669 HTTP_FinishedReading(lpwhr);
2671 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2673 DWORD dwCode,dwCodeLength=sizeof(DWORD);
2674 WCHAR szNewLocation[2048];
2675 dwBufferSize=sizeof(szNewLocation);
2676 if (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
2677 (dwCode==HTTP_STATUS_REDIRECT || dwCode==HTTP_STATUS_MOVED) &&
2678 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,NULL))
2680 HTTP_DrainContent(lpwhr);
2681 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2682 INTERNET_STATUS_REDIRECT, szNewLocation,
2683 dwBufferSize);
2684 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
2685 if (bSuccess)
2687 HeapFree(GetProcessHeap(), 0, requestString);
2688 loop_next = TRUE;
2692 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTH) && bSuccess)
2694 DWORD dwCode,dwCodeLength=sizeof(DWORD);
2695 WCHAR szAuthValue[2048];
2696 dwBufferSize=2048;
2697 if (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,NULL) &&
2698 (dwCode == HTTP_STATUS_DENIED))
2700 DWORD dwIndex = 0;
2701 while (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_WWW_AUTHENTICATE,szAuthValue,&dwBufferSize,&dwIndex))
2703 if (HTTP_DoAuthorization(lpwhr, szAuthValue))
2705 loop_next = TRUE;
2706 break;
2712 else
2713 bSuccess = TRUE;
2715 while (loop_next);
2717 lend:
2719 HeapFree(GetProcessHeap(), 0, requestString);
2721 /* TODO: send notification for P3P header */
2723 iar.dwResult = (DWORD)bSuccess;
2724 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2726 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2727 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2728 sizeof(INTERNET_ASYNC_RESULT));
2730 TRACE("<--\n");
2731 return bSuccess;
2734 /***********************************************************************
2735 * HTTP_Connect (internal)
2737 * Create http session handle
2739 * RETURNS
2740 * HINTERNET a session handle on success
2741 * NULL on failure
2744 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2745 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2746 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2747 DWORD dwInternalFlags)
2749 BOOL bSuccess = FALSE;
2750 LPWININETHTTPSESSIONW lpwhs = NULL;
2751 HINTERNET handle = NULL;
2753 TRACE("-->\n");
2755 assert( hIC->hdr.htype == WH_HINIT );
2757 hIC->hdr.dwContext = dwContext;
2759 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2760 if (NULL == lpwhs)
2762 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2763 goto lerror;
2767 * According to my tests. The name is not resolved until a request is sent
2770 lpwhs->hdr.htype = WH_HHTTPSESSION;
2771 lpwhs->hdr.dwFlags = dwFlags;
2772 lpwhs->hdr.dwContext = dwContext;
2773 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
2774 lpwhs->hdr.dwRefCount = 1;
2775 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2776 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2778 WININET_AddRef( &hIC->hdr );
2779 lpwhs->lpAppInfo = hIC;
2781 handle = WININET_AllocHandle( &lpwhs->hdr );
2782 if (NULL == handle)
2784 ERR("Failed to alloc handle\n");
2785 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2786 goto lerror;
2789 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2790 if(strchrW(hIC->lpszProxy, ' '))
2791 FIXME("Several proxies not implemented.\n");
2792 if(hIC->lpszProxyBypass)
2793 FIXME("Proxy bypass is ignored.\n");
2795 if (lpszServerName && lpszServerName[0])
2797 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2798 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2800 if (lpszUserName && lpszUserName[0])
2801 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2802 if (lpszPassword && lpszPassword[0])
2803 lpwhs->lpszPassword = WININET_strdupW(lpszPassword);
2804 lpwhs->nServerPort = nServerPort;
2805 lpwhs->nHostPort = nServerPort;
2807 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2808 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2810 INTERNET_SendCallback(&hIC->hdr, dwContext,
2811 INTERNET_STATUS_HANDLE_CREATED, &handle,
2812 sizeof(handle));
2815 bSuccess = TRUE;
2817 lerror:
2818 if( lpwhs )
2819 WININET_Release( &lpwhs->hdr );
2822 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2823 * windows
2826 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2827 return handle;
2831 /***********************************************************************
2832 * HTTP_OpenConnection (internal)
2834 * Connect to a web server
2836 * RETURNS
2838 * TRUE on success
2839 * FALSE on failure
2841 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2843 BOOL bSuccess = FALSE;
2844 LPWININETHTTPSESSIONW lpwhs;
2845 LPWININETAPPINFOW hIC = NULL;
2846 char szaddr[32];
2848 TRACE("-->\n");
2851 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2853 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2854 goto lend;
2857 if (NETCON_connected(&lpwhr->netConnection))
2859 bSuccess = TRUE;
2860 goto lend;
2863 lpwhs = lpwhr->lpHttpSession;
2865 hIC = lpwhs->lpAppInfo;
2866 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2867 szaddr, sizeof(szaddr));
2868 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2869 INTERNET_STATUS_CONNECTING_TO_SERVER,
2870 szaddr,
2871 strlen(szaddr)+1);
2873 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2874 SOCK_STREAM, 0))
2876 WARN("Socket creation failed\n");
2877 goto lend;
2880 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2881 sizeof(lpwhs->socketAddress)))
2882 goto lend;
2884 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2886 /* Note: we differ from Microsoft's WinINet here. they seem to have
2887 * a bug that causes no status callbacks to be sent when starting
2888 * a tunnel to a proxy server using the CONNECT verb. i believe our
2889 * behaviour to be more correct and to not cause any incompatibilities
2890 * because using a secure connection through a proxy server is a rare
2891 * case that would be hard for anyone to depend on */
2892 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2893 goto lend;
2895 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2897 WARN("Couldn't connect securely to host\n");
2898 goto lend;
2902 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2903 INTERNET_STATUS_CONNECTED_TO_SERVER,
2904 szaddr, strlen(szaddr)+1);
2906 bSuccess = TRUE;
2908 lend:
2909 TRACE("%d <--\n", bSuccess);
2910 return bSuccess;
2914 /***********************************************************************
2915 * HTTP_clear_response_headers (internal)
2917 * clear out any old response headers
2919 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2921 DWORD i;
2923 for( i=0; i<lpwhr->nCustHeaders; i++)
2925 if( !lpwhr->pCustHeaders[i].lpszField )
2926 continue;
2927 if( !lpwhr->pCustHeaders[i].lpszValue )
2928 continue;
2929 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2930 continue;
2931 HTTP_DeleteCustomHeader( lpwhr, i );
2932 i--;
2936 /***********************************************************************
2937 * HTTP_GetResponseHeaders (internal)
2939 * Read server response
2941 * RETURNS
2943 * TRUE on success
2944 * FALSE on error
2946 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2948 INT cbreaks = 0;
2949 WCHAR buffer[MAX_REPLY_LEN];
2950 DWORD buflen = MAX_REPLY_LEN;
2951 BOOL bSuccess = FALSE;
2952 INT rc = 0;
2953 static const WCHAR szCrLf[] = {'\r','\n',0};
2954 char bufferA[MAX_REPLY_LEN];
2955 LPWSTR status_code, status_text;
2956 DWORD cchMaxRawHeaders = 1024;
2957 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2958 DWORD cchRawHeaders = 0;
2960 TRACE("-->\n");
2962 /* clear old response headers (eg. from a redirect response) */
2963 HTTP_clear_response_headers( lpwhr );
2965 if (!NETCON_connected(&lpwhr->netConnection))
2966 goto lend;
2969 * HACK peek at the buffer
2971 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2974 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2976 buflen = MAX_REPLY_LEN;
2977 memset(buffer, 0, MAX_REPLY_LEN);
2978 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2979 goto lend;
2980 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2982 /* regenerate raw headers */
2983 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2985 cchMaxRawHeaders *= 2;
2986 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2988 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2989 cchRawHeaders += (buflen-1);
2990 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2991 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2992 lpszRawHeaders[cchRawHeaders] = '\0';
2994 /* split the version from the status code */
2995 status_code = strchrW( buffer, ' ' );
2996 if( !status_code )
2997 goto lend;
2998 *status_code++=0;
3000 /* split the status code from the status text */
3001 status_text = strchrW( status_code, ' ' );
3002 if( !status_text )
3003 goto lend;
3004 *status_text++=0;
3006 TRACE("version [%s] status code [%s] status text [%s]\n",
3007 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
3009 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
3010 HTTP_ADDHDR_FLAG_REPLACE);
3012 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
3013 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
3015 lpwhr->lpszVersion= WININET_strdupW(buffer);
3016 lpwhr->lpszStatusText = WININET_strdupW(status_text);
3018 /* Parse each response line */
3021 buflen = MAX_REPLY_LEN;
3022 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
3024 LPWSTR * pFieldAndValue;
3026 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
3027 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
3029 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
3031 cchMaxRawHeaders *= 2;
3032 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
3034 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
3035 cchRawHeaders += (buflen-1);
3036 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
3037 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
3038 lpszRawHeaders[cchRawHeaders] = '\0';
3040 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
3041 if (!pFieldAndValue)
3042 break;
3044 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
3045 HTTP_ADDREQ_FLAG_ADD );
3047 HTTP_FreeTokens(pFieldAndValue);
3049 else
3051 cbreaks++;
3052 if (cbreaks >= 2)
3053 break;
3055 }while(1);
3057 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3058 lpwhr->lpszRawHeaders = lpszRawHeaders;
3059 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
3060 bSuccess = TRUE;
3062 lend:
3064 TRACE("<--\n");
3065 if (bSuccess)
3066 return rc;
3067 else
3068 return 0;
3072 static void strip_spaces(LPWSTR start)
3074 LPWSTR str = start;
3075 LPWSTR end;
3077 while (*str == ' ' && *str != '\0')
3078 str++;
3080 if (str != start)
3081 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
3083 end = start + strlenW(start) - 1;
3084 while (end >= start && *end == ' ')
3086 *end = '\0';
3087 end--;
3092 /***********************************************************************
3093 * HTTP_InterpretHttpHeader (internal)
3095 * Parse server response
3097 * RETURNS
3099 * Pointer to array of field, value, NULL on success.
3100 * NULL on error.
3102 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
3104 LPWSTR * pTokenPair;
3105 LPWSTR pszColon;
3106 INT len;
3108 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
3110 pszColon = strchrW(buffer, ':');
3111 /* must have two tokens */
3112 if (!pszColon)
3114 HTTP_FreeTokens(pTokenPair);
3115 if (buffer[0])
3116 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
3117 return NULL;
3120 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
3121 if (!pTokenPair[0])
3123 HTTP_FreeTokens(pTokenPair);
3124 return NULL;
3126 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
3127 pTokenPair[0][pszColon - buffer] = '\0';
3129 /* skip colon */
3130 pszColon++;
3131 len = strlenW(pszColon);
3132 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
3133 if (!pTokenPair[1])
3135 HTTP_FreeTokens(pTokenPair);
3136 return NULL;
3138 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
3140 strip_spaces(pTokenPair[0]);
3141 strip_spaces(pTokenPair[1]);
3143 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
3144 return pTokenPair;
3147 /***********************************************************************
3148 * HTTP_ProcessHeader (internal)
3150 * Stuff header into header tables according to <dwModifier>
3154 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3156 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
3158 LPHTTPHEADERW lphttpHdr = NULL;
3159 BOOL bSuccess = FALSE;
3160 INT index = -1;
3161 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
3163 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
3165 /* REPLACE wins out over ADD */
3166 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3167 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
3169 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
3170 index = -1;
3171 else
3172 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
3174 if (index >= 0)
3176 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
3178 return FALSE;
3180 lphttpHdr = &lpwhr->pCustHeaders[index];
3182 else if (value)
3184 HTTPHEADERW hdr;
3186 hdr.lpszField = (LPWSTR)field;
3187 hdr.lpszValue = (LPWSTR)value;
3188 hdr.wFlags = hdr.wCount = 0;
3190 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3191 hdr.wFlags |= HDR_ISREQUEST;
3193 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3195 /* no value to delete */
3196 else return TRUE;
3198 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3199 lphttpHdr->wFlags |= HDR_ISREQUEST;
3200 else
3201 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
3203 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
3205 HTTP_DeleteCustomHeader( lpwhr, index );
3207 if (value)
3209 HTTPHEADERW hdr;
3211 hdr.lpszField = (LPWSTR)field;
3212 hdr.lpszValue = (LPWSTR)value;
3213 hdr.wFlags = hdr.wCount = 0;
3215 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
3216 hdr.wFlags |= HDR_ISREQUEST;
3218 return HTTP_InsertCustomHeader(lpwhr, &hdr);
3221 return TRUE;
3223 else if (dwModifier & COALESCEFLASG)
3225 LPWSTR lpsztmp;
3226 WCHAR ch = 0;
3227 INT len = 0;
3228 INT origlen = strlenW(lphttpHdr->lpszValue);
3229 INT valuelen = strlenW(value);
3231 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
3233 ch = ',';
3234 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3236 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
3238 ch = ';';
3239 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
3242 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
3244 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
3245 if (lpsztmp)
3247 lphttpHdr->lpszValue = lpsztmp;
3248 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
3249 if (ch > 0)
3251 lphttpHdr->lpszValue[origlen] = ch;
3252 origlen++;
3253 lphttpHdr->lpszValue[origlen] = ' ';
3254 origlen++;
3257 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
3258 lphttpHdr->lpszValue[len] = '\0';
3259 bSuccess = TRUE;
3261 else
3263 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
3264 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3267 TRACE("<-- %d\n",bSuccess);
3268 return bSuccess;
3272 /***********************************************************************
3273 * HTTP_CloseConnection (internal)
3275 * Close socket connection
3278 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
3280 LPWININETHTTPSESSIONW lpwhs = NULL;
3281 LPWININETAPPINFOW hIC = NULL;
3283 TRACE("%p\n",lpwhr);
3285 if (!NETCON_connected(&lpwhr->netConnection))
3286 return;
3288 if (lpwhr->pAuthInfo)
3290 DeleteSecurityContext(&lpwhr->pAuthInfo->ctx);
3291 FreeCredentialsHandle(&lpwhr->pAuthInfo->cred);
3293 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->auth_data);
3294 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo->scheme);
3295 HeapFree(GetProcessHeap(), 0, lpwhr->pAuthInfo);
3296 lpwhr->pAuthInfo = NULL;
3299 lpwhs = lpwhr->lpHttpSession;
3300 hIC = lpwhs->lpAppInfo;
3302 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3303 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
3305 NETCON_close(&lpwhr->netConnection);
3307 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
3308 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
3312 /***********************************************************************
3313 * HTTP_FinishedReading (internal)
3315 * Called when all content from server has been read by client.
3318 BOOL HTTP_FinishedReading(LPWININETHTTPREQW lpwhr)
3320 WCHAR szConnectionResponse[20];
3321 DWORD dwBufferSize = sizeof(szConnectionResponse);
3323 TRACE("\n");
3325 if (!HTTP_HttpQueryInfoW(lpwhr, HTTP_QUERY_CONNECTION, szConnectionResponse,
3326 &dwBufferSize, NULL) ||
3327 strcmpiW(szConnectionResponse, szKeepAlive))
3329 HTTP_CloseConnection(lpwhr);
3332 /* FIXME: store data in the URL cache here */
3334 return TRUE;
3337 /***********************************************************************
3338 * HTTP_CloseHTTPRequestHandle (internal)
3340 * Deallocate request handle
3343 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
3345 DWORD i;
3346 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
3348 TRACE("\n");
3350 WININET_Release(&lpwhr->lpHttpSession->hdr);
3352 HTTP_CloseConnection(lpwhr);
3354 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
3355 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
3356 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
3357 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
3358 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
3360 for (i = 0; i < lpwhr->nCustHeaders; i++)
3362 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
3363 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
3366 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
3367 HeapFree(GetProcessHeap(), 0, lpwhr);
3371 /***********************************************************************
3372 * HTTP_CloseHTTPSessionHandle (internal)
3374 * Deallocate session handle
3377 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
3379 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3381 TRACE("%p\n", lpwhs);
3383 WININET_Release(&lpwhs->lpAppInfo->hdr);
3385 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3386 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3387 HeapFree(GetProcessHeap(), 0, lpwhs->lpszPassword);
3388 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3389 HeapFree(GetProcessHeap(), 0, lpwhs);
3393 /***********************************************************************
3394 * HTTP_GetCustomHeaderIndex (internal)
3396 * Return index of custom header from header array
3399 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
3400 int requested_index, BOOL request_only)
3402 DWORD index;
3404 TRACE("%s\n", debugstr_w(lpszField));
3406 for (index = 0; index < lpwhr->nCustHeaders; index++)
3408 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3409 continue;
3411 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3412 continue;
3414 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3415 continue;
3417 if (requested_index == 0)
3418 break;
3419 requested_index --;
3422 if (index >= lpwhr->nCustHeaders)
3423 index = -1;
3425 TRACE("Return: %d\n", index);
3426 return index;
3430 /***********************************************************************
3431 * HTTP_InsertCustomHeader (internal)
3433 * Insert header into array
3436 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3438 INT count;
3439 LPHTTPHEADERW lph = NULL;
3440 BOOL r = FALSE;
3442 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3443 count = lpwhr->nCustHeaders + 1;
3444 if (count > 1)
3445 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3446 else
3447 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3449 if (NULL != lph)
3451 lpwhr->pCustHeaders = lph;
3452 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3453 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3454 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3455 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3456 lpwhr->nCustHeaders++;
3457 r = TRUE;
3459 else
3461 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3464 return r;
3468 /***********************************************************************
3469 * HTTP_DeleteCustomHeader (internal)
3471 * Delete header from array
3472 * If this function is called, the indexs may change.
3474 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3476 if( lpwhr->nCustHeaders <= 0 )
3477 return FALSE;
3478 if( index >= lpwhr->nCustHeaders )
3479 return FALSE;
3480 lpwhr->nCustHeaders--;
3482 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3483 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3484 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3486 return TRUE;
3489 /***********************************************************************
3490 * IsHostInProxyBypassList (@)
3492 * Undocumented
3495 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3497 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
3498 return FALSE;