ntdll: Fix DTR and CTS controls in serial code.
[wine/testsucceed.git] / dlls / wininet / http.c
blob813d99e37a90ef6c92f9bfec060a80cc02a43590
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
10 * Ulrich Czekalla
11 * David Hammerton
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "config.h"
29 #include "wine/port.h"
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #include <time.h>
45 #include <assert.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wininet.h"
50 #include "winreg.h"
51 #include "winerror.h"
52 #define NO_SHLWAPI_STREAM
53 #define NO_SHLWAPI_REG
54 #define NO_SHLWAPI_STRFCNS
55 #define NO_SHLWAPI_GDI
56 #include "shlwapi.h"
58 #include "internet.h"
59 #include "wine/debug.h"
60 #include "wine/unicode.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
64 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
65 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
66 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
67 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
68 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
69 static const WCHAR szHost[] = { 'H','o','s','t',0 };
70 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
71 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
73 #define MAXHOSTNAME 100
74 #define MAX_FIELD_VALUE_LEN 256
75 #define MAX_FIELD_LEN 256
77 #define HTTP_REFERER g_szReferer
78 #define HTTP_ACCEPT g_szAccept
79 #define HTTP_USERAGENT g_szUserAgent
81 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
82 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
83 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
84 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
85 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
86 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
87 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
90 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
91 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
92 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
93 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
94 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
95 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
96 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
97 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
98 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
99 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
100 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
101 LPCWSTR username, LPCWSTR password );
102 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
103 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
104 lpdwIndex);
105 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl);
108 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
110 int HeaderIndex = 0;
111 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
112 if (HeaderIndex == -1)
113 return NULL;
114 else
115 return &req->pCustHeaders[HeaderIndex];
118 /***********************************************************************
119 * HTTP_Tokenize (internal)
121 * Tokenize a string, allocating memory for the tokens.
123 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
125 LPWSTR * token_array;
126 int tokens = 0;
127 int i;
128 LPCWSTR next_token;
130 /* empty string has no tokens */
131 if (*string)
132 tokens++;
133 /* count tokens */
134 for (i = 0; string[i]; i++)
135 if (!strncmpW(string+i, token_string, strlenW(token_string)))
137 DWORD j;
138 tokens++;
139 /* we want to skip over separators, but not the null terminator */
140 for (j = 0; j < strlenW(token_string) - 1; j++)
141 if (!string[i+j])
142 break;
143 i += j;
146 /* add 1 for terminating NULL */
147 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
148 token_array[tokens] = NULL;
149 if (!tokens)
150 return token_array;
151 for (i = 0; i < tokens; i++)
153 int len;
154 next_token = strstrW(string, token_string);
155 if (!next_token) next_token = string+strlenW(string);
156 len = next_token - string;
157 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
158 memcpy(token_array[i], string, len*sizeof(WCHAR));
159 token_array[i][len] = '\0';
160 string = next_token+strlenW(token_string);
162 return token_array;
165 /***********************************************************************
166 * HTTP_FreeTokens (internal)
168 * Frees memory returned from HTTP_Tokenize.
170 static void HTTP_FreeTokens(LPWSTR * token_array)
172 int i;
173 for (i = 0; token_array[i]; i++)
174 HeapFree(GetProcessHeap(), 0, token_array[i]);
175 HeapFree(GetProcessHeap(), 0, token_array);
178 /* **********************************************************************
180 * Helper functions for the HttpSendRequest(Ex) functions
183 static void AsyncHttpSendRequestProc(WORKREQUEST *workRequest)
185 struct WORKREQ_HTTPSENDREQUESTW const *req = &workRequest->u.HttpSendRequestW;
186 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) workRequest->hdr;
188 TRACE("%p\n", lpwhr);
190 HTTP_HttpSendRequestW(lpwhr, req->lpszHeader,
191 req->dwHeaderLength, req->lpOptional, req->dwOptionalLength,
192 req->dwContentLength, req->bEndRequest);
194 HeapFree(GetProcessHeap(), 0, req->lpszHeader);
197 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
199 /* if the verb is NULL default to GET */
200 if (NULL == lpwhr->lpszVerb)
202 static const WCHAR szGET[] = { 'G','E','T', 0 };
203 lpwhr->lpszVerb = WININET_strdupW(szGET);
207 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
209 static const WCHAR szSlash[] = { '/',0 };
210 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
212 /* If we don't have a path we set it to root */
213 if (NULL == lpwhr->lpszPath)
214 lpwhr->lpszPath = WININET_strdupW(szSlash);
215 else /* remove \r and \n*/
217 int nLen = strlenW(lpwhr->lpszPath);
218 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
220 nLen--;
221 lpwhr->lpszPath[nLen]='\0';
223 /* Replace '\' with '/' */
224 while (nLen>0) {
225 nLen--;
226 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
230 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
231 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
232 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
234 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
235 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
236 *fixurl = '/';
237 strcpyW(fixurl + 1, lpwhr->lpszPath);
238 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
239 lpwhr->lpszPath = fixurl;
243 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
245 LPWSTR requestString;
246 DWORD len, n;
247 LPCWSTR *req;
248 INT i;
249 LPWSTR p;
251 static const WCHAR szSpace[] = { ' ',0 };
252 static const WCHAR szcrlf[] = {'\r','\n', 0};
253 static const WCHAR szColon[] = { ':',' ',0 };
254 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
256 /* allocate space for an array of all the string pointers to be added */
257 len = (lpwhr->nCustHeaders)*4 + 9;
258 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
260 /* add the verb, path and HTTP version string */
261 n = 0;
262 req[n++] = verb;
263 req[n++] = szSpace;
264 req[n++] = path;
265 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
267 /* Append custom request heades */
268 for (i = 0; i < lpwhr->nCustHeaders; i++)
270 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
272 req[n++] = szcrlf;
273 req[n++] = lpwhr->pCustHeaders[i].lpszField;
274 req[n++] = szColon;
275 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
277 TRACE("Adding custom header %s (%s)\n",
278 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
279 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
283 if( n >= len )
284 ERR("oops. buffer overrun\n");
286 req[n] = NULL;
287 requestString = HTTP_build_req( req, 4 );
288 HeapFree( GetProcessHeap(), 0, req );
291 * Set (header) termination string for request
292 * Make sure there's exactly two new lines at the end of the request
294 p = &requestString[strlenW(requestString)-1];
295 while ( (*p == '\n') || (*p == '\r') )
296 p--;
297 strcpyW( p+1, sztwocrlf );
299 return requestString;
302 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
304 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
305 int HeaderIndex;
306 LPHTTPHEADERW setCookieHeader;
308 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
309 if (HeaderIndex == -1)
310 return;
311 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
313 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
315 int nPosStart = 0, nPosEnd = 0, len;
316 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
318 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
320 LPWSTR buf_cookie, cookie_name, cookie_data;
321 LPWSTR buf_url;
322 LPWSTR domain = NULL;
323 LPHTTPHEADERW Host;
325 int nEqualPos = 0;
326 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
327 setCookieHeader->lpszValue[nPosEnd] != '\0')
329 nPosEnd++;
331 if (setCookieHeader->lpszValue[nPosEnd] == ';')
333 /* fixme: not case sensitive, strcasestr is gnu only */
334 int nDomainPosEnd = 0;
335 int nDomainPosStart = 0, nDomainLength = 0;
336 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
337 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
338 if (lpszDomain)
339 { /* they have specified their own domain, lets use it */
340 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
341 lpszDomain[nDomainPosEnd] != '\0')
343 nDomainPosEnd++;
345 nDomainPosStart = strlenW(szDomain);
346 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
347 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
348 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
351 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
352 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
353 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
354 TRACE("%s\n", debugstr_w(buf_cookie));
355 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
357 nEqualPos++;
359 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
361 HeapFree(GetProcessHeap(), 0, buf_cookie);
362 break;
365 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
366 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
367 cookie_data = &buf_cookie[nEqualPos + 1];
369 Host = HTTP_GetHeader(lpwhr,szHost);
370 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
371 strlenW(lpwhr->lpszPath) + 9;
372 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
373 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
374 InternetSetCookieW(buf_url, cookie_name, cookie_data);
376 HeapFree(GetProcessHeap(), 0, buf_url);
377 HeapFree(GetProcessHeap(), 0, buf_cookie);
378 HeapFree(GetProcessHeap(), 0, cookie_name);
379 HeapFree(GetProcessHeap(), 0, domain);
380 nPosStart = nPosEnd;
385 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
387 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
388 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
390 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
391 assert(hIC->hdr.htype == WH_HINIT);
393 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
394 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
395 hIC->lpszProxyPassword);
398 /***********************************************************************
399 * HTTP_HttpAddRequestHeadersW (internal)
401 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
402 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
404 LPWSTR lpszStart;
405 LPWSTR lpszEnd;
406 LPWSTR buffer;
407 BOOL bSuccess = FALSE;
408 DWORD len;
410 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
412 if( dwHeaderLength == ~0U )
413 len = strlenW(lpszHeader);
414 else
415 len = dwHeaderLength;
416 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
417 lstrcpynW( buffer, lpszHeader, len + 1);
419 lpszStart = buffer;
423 LPWSTR * pFieldAndValue;
425 lpszEnd = lpszStart;
427 while (*lpszEnd != '\0')
429 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
430 break;
431 lpszEnd++;
434 if (*lpszStart == '\0')
435 break;
437 if (*lpszEnd == '\r')
439 *lpszEnd = '\0';
440 lpszEnd += 2; /* Jump over \r\n */
442 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
443 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
444 if (pFieldAndValue)
446 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
447 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
448 HTTP_FreeTokens(pFieldAndValue);
451 lpszStart = lpszEnd;
452 } while (bSuccess);
454 HeapFree(GetProcessHeap(), 0, buffer);
456 return bSuccess;
459 /***********************************************************************
460 * HttpAddRequestHeadersW (WININET.@)
462 * Adds one or more HTTP header to the request handler
464 * RETURNS
465 * TRUE on success
466 * FALSE on failure
469 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
470 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
472 BOOL bSuccess = FALSE;
473 LPWININETHTTPREQW lpwhr;
475 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
476 dwModifier);
478 if (!lpszHeader)
479 return TRUE;
481 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
482 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
484 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
485 goto lend;
487 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
488 lend:
489 if( lpwhr )
490 WININET_Release( &lpwhr->hdr );
492 return bSuccess;
495 /***********************************************************************
496 * HttpAddRequestHeadersA (WININET.@)
498 * Adds one or more HTTP header to the request handler
500 * RETURNS
501 * TRUE on success
502 * FALSE on failure
505 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
506 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
508 DWORD len;
509 LPWSTR hdr;
510 BOOL r;
512 TRACE("%p, %s, %i, %i\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
513 dwModifier);
515 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
516 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
517 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
518 if( dwHeaderLength != ~0U )
519 dwHeaderLength = len;
521 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
523 HeapFree( GetProcessHeap(), 0, hdr );
525 return r;
528 /***********************************************************************
529 * HttpEndRequestA (WININET.@)
531 * Ends an HTTP request that was started by HttpSendRequestEx
533 * RETURNS
534 * TRUE if successful
535 * FALSE on failure
538 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
539 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
541 LPINTERNET_BUFFERSA ptr;
542 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
543 BOOL rc = FALSE;
545 TRACE("(%p, %p, %08x, %08x): stub\n", hRequest, lpBuffersOut, dwFlags,
546 dwContext);
548 ptr = lpBuffersOut;
549 if (ptr)
550 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
551 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
552 else
553 lpBuffersOutW = NULL;
555 ptrW = lpBuffersOutW;
556 while (ptr)
558 if (ptr->lpvBuffer && ptr->dwBufferLength)
559 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
560 ptrW->dwBufferLength = ptr->dwBufferLength;
561 ptrW->dwBufferTotal= ptr->dwBufferTotal;
563 if (ptr->Next)
564 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
565 sizeof(INTERNET_BUFFERSW));
567 ptr = ptr->Next;
568 ptrW = ptrW->Next;
571 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
573 if (lpBuffersOutW)
575 ptrW = lpBuffersOutW;
576 while (ptrW)
578 LPINTERNET_BUFFERSW ptrW2;
580 FIXME("Do we need to translate info out of these buffer?\n");
582 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
583 ptrW2 = ptrW->Next;
584 HeapFree(GetProcessHeap(),0,ptrW);
585 ptrW = ptrW2;
589 return rc;
592 /***********************************************************************
593 * HttpEndRequestW (WININET.@)
595 * Ends an HTTP request that was started by HttpSendRequestEx
597 * RETURNS
598 * TRUE if successful
599 * FALSE on failure
602 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
603 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
605 BOOL rc = FALSE;
606 LPWININETHTTPREQW lpwhr;
607 INT responseLen;
609 TRACE("-->\n");
610 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
612 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
614 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
615 return FALSE;
618 lpwhr->hdr.dwFlags |= dwFlags;
619 lpwhr->hdr.dwContext = dwContext;
621 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
622 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
624 responseLen = HTTP_GetResponseHeaders(lpwhr);
625 if (responseLen)
626 rc = TRUE;
628 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
629 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
631 /* process headers here. Is this right? */
632 HTTP_ProcessHeaders(lpwhr);
634 /* We appear to do nothing with the buffer.. is that correct? */
636 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
638 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
639 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
640 (dwCode==302 || dwCode==301))
642 WCHAR szNewLocation[2048];
643 DWORD dwBufferSize=2048;
644 dwIndex=0;
645 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
647 static const WCHAR szGET[] = { 'G','E','T', 0 };
648 /* redirects are always GETs */
649 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
650 lpwhr->lpszVerb = WININET_strdupW(szGET);
651 rc = HTTP_HandleRedirect(lpwhr, szNewLocation);
652 if (rc)
653 rc = HTTP_HttpSendRequestW(lpwhr, NULL, 0, NULL, 0, 0, TRUE);
658 TRACE("%i <--\n",rc);
659 return rc;
662 /***********************************************************************
663 * HttpOpenRequestW (WININET.@)
665 * Open a HTTP request handle
667 * RETURNS
668 * HINTERNET a HTTP request handle on success
669 * NULL on failure
672 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
673 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
674 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
675 DWORD dwFlags, DWORD dwContext)
677 LPWININETHTTPSESSIONW lpwhs;
678 HINTERNET handle = NULL;
680 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession,
681 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
682 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
683 dwFlags, dwContext);
684 if(lpszAcceptTypes!=NULL)
686 int i;
687 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
688 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
691 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
692 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
694 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
695 goto lend;
699 * My tests seem to show that the windows version does not
700 * become asynchronous until after this point. And anyhow
701 * if this call was asynchronous then how would you get the
702 * necessary HINTERNET pointer returned by this function.
705 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
706 lpszVersion, lpszReferrer, lpszAcceptTypes,
707 dwFlags, dwContext);
708 lend:
709 if( lpwhs )
710 WININET_Release( &lpwhs->hdr );
711 TRACE("returning %p\n", handle);
712 return handle;
716 /***********************************************************************
717 * HttpOpenRequestA (WININET.@)
719 * Open a HTTP request handle
721 * RETURNS
722 * HINTERNET a HTTP request handle on success
723 * NULL on failure
726 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
727 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
728 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
729 DWORD dwFlags, DWORD dwContext)
731 LPWSTR szVerb = NULL, szObjectName = NULL;
732 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
733 INT len;
734 INT acceptTypesCount;
735 HINTERNET rc = FALSE;
736 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08x)\n", hHttpSession,
737 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
738 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
739 dwFlags, dwContext);
741 if (lpszVerb)
743 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
744 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
745 if ( !szVerb )
746 goto end;
747 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
750 if (lpszObjectName)
752 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
753 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
754 if ( !szObjectName )
755 goto end;
756 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
759 if (lpszVersion)
761 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
762 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
763 if ( !szVersion )
764 goto end;
765 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
768 if (lpszReferrer)
770 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
771 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
772 if ( !szReferrer )
773 goto end;
774 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
777 acceptTypesCount = 0;
778 if (lpszAcceptTypes)
780 /* find out how many there are */
781 while (lpszAcceptTypes[acceptTypesCount])
782 acceptTypesCount++;
783 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
784 acceptTypesCount = 0;
785 while (lpszAcceptTypes[acceptTypesCount])
787 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
788 -1, NULL, 0 );
789 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
790 if (!szAcceptTypes[acceptTypesCount] )
791 goto end;
792 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
793 -1, szAcceptTypes[acceptTypesCount], len );
794 acceptTypesCount++;
796 szAcceptTypes[acceptTypesCount] = NULL;
798 else szAcceptTypes = 0;
800 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
801 szVersion, szReferrer,
802 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
804 end:
805 if (szAcceptTypes)
807 acceptTypesCount = 0;
808 while (szAcceptTypes[acceptTypesCount])
810 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
811 acceptTypesCount++;
813 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
815 HeapFree(GetProcessHeap(), 0, szReferrer);
816 HeapFree(GetProcessHeap(), 0, szVersion);
817 HeapFree(GetProcessHeap(), 0, szObjectName);
818 HeapFree(GetProcessHeap(), 0, szVerb);
820 return rc;
823 /***********************************************************************
824 * HTTP_Base64
826 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
828 UINT n = 0, x;
829 static LPCSTR HTTP_Base64Enc =
830 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
832 while( bin[0] )
834 /* first 6 bits, all from bin[0] */
835 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
836 x = (bin[0] & 3) << 4;
838 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
839 if( !bin[1] )
841 base64[n++] = HTTP_Base64Enc[x];
842 base64[n++] = '=';
843 base64[n++] = '=';
844 break;
846 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
847 x = ( bin[1] & 0x0f ) << 2;
849 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
850 if( !bin[2] )
852 base64[n++] = HTTP_Base64Enc[x];
853 base64[n++] = '=';
854 break;
856 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
858 /* last 6 bits, all from bin [2] */
859 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
860 bin += 3;
862 base64[n] = 0;
863 return n;
866 /***********************************************************************
867 * HTTP_EncodeBasicAuth
869 * Encode the basic authentication string for HTTP 1.1
871 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
873 UINT len;
874 LPWSTR in, out;
875 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
876 static const WCHAR szColon[] = {':',0};
878 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
879 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
880 if( !in )
881 return NULL;
883 len = lstrlenW(szBasic) +
884 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
885 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
886 if( out )
888 lstrcpyW( in, username );
889 lstrcatW( in, szColon );
890 lstrcatW( in, password );
891 lstrcpyW( out, szBasic );
892 HTTP_Base64( in, &out[strlenW(out)] );
894 HeapFree( GetProcessHeap(), 0, in );
896 return out;
899 /***********************************************************************
900 * HTTP_InsertProxyAuthorization
902 * Insert the basic authorization field in the request header
904 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
905 LPCWSTR username, LPCWSTR password )
907 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
908 BOOL ret = TRUE;
910 if (!authorization)
911 return FALSE;
913 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
915 HTTP_ProcessHeader(lpwhr, szProxy_Authorization, authorization,
916 HTTP_ADDHDR_FLAG_REPLACE);
918 HeapFree( GetProcessHeap(), 0, authorization );
920 return ret;
923 /***********************************************************************
924 * HTTP_DealWithProxy
926 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
927 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
929 WCHAR buf[MAXHOSTNAME];
930 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
931 WCHAR* url;
932 static WCHAR szNul[] = { 0 };
933 URL_COMPONENTSW UrlComponents;
934 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
935 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
936 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
937 int len;
939 memset( &UrlComponents, 0, sizeof UrlComponents );
940 UrlComponents.dwStructSize = sizeof UrlComponents;
941 UrlComponents.lpszHostName = buf;
942 UrlComponents.dwHostNameLength = MAXHOSTNAME;
944 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
945 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
946 sprintfW(proxy, szFormat1, hIC->lpszProxy);
947 else
948 strcpyW(proxy, hIC->lpszProxy);
949 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
950 return FALSE;
951 if( UrlComponents.dwHostNameLength == 0 )
952 return FALSE;
954 if( !lpwhr->lpszPath )
955 lpwhr->lpszPath = szNul;
956 TRACE("server='%s' path='%s'\n",
957 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
958 /* for constant 15 see above */
959 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
960 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
962 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
963 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
965 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
967 if( lpwhr->lpszPath[0] != '/' )
968 strcatW( url, szSlash );
969 strcatW(url, lpwhr->lpszPath);
970 if(lpwhr->lpszPath != szNul)
971 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
972 lpwhr->lpszPath = url;
974 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
975 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
976 lpwhs->nServerPort = UrlComponents.nPort;
978 return TRUE;
981 static BOOL HTTP_ResolveName(LPWININETHTTPREQW lpwhr)
983 char szaddr[32];
984 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
986 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
987 INTERNET_STATUS_RESOLVING_NAME,
988 lpwhs->lpszServerName,
989 strlenW(lpwhs->lpszServerName)+1);
991 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
992 &lpwhs->socketAddress))
994 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
995 return FALSE;
998 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
999 szaddr, sizeof(szaddr));
1000 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1001 INTERNET_STATUS_NAME_RESOLVED,
1002 szaddr, strlen(szaddr)+1);
1003 return TRUE;
1006 /***********************************************************************
1007 * HTTP_HttpOpenRequestW (internal)
1009 * Open a HTTP request handle
1011 * RETURNS
1012 * HINTERNET a HTTP request handle on success
1013 * NULL on failure
1016 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
1017 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
1018 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
1019 DWORD dwFlags, DWORD dwContext)
1021 LPWININETAPPINFOW hIC = NULL;
1022 LPWININETHTTPREQW lpwhr;
1023 LPWSTR lpszCookies;
1024 LPWSTR lpszUrl = NULL;
1025 DWORD nCookieSize;
1026 HINTERNET handle = NULL;
1027 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
1028 DWORD len;
1029 LPHTTPHEADERW Host;
1031 TRACE("-->\n");
1033 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
1034 hIC = lpwhs->lpAppInfo;
1036 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1037 if (NULL == lpwhr)
1039 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1040 goto lend;
1042 lpwhr->hdr.htype = WH_HHTTPREQ;
1043 lpwhr->hdr.dwFlags = dwFlags;
1044 lpwhr->hdr.dwContext = dwContext;
1045 lpwhr->hdr.dwRefCount = 1;
1046 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1047 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1048 lpwhr->hdr.dwInternalFlags = lpwhs->hdr.dwInternalFlags & INET_CALLBACKW;
1050 WININET_AddRef( &lpwhs->hdr );
1051 lpwhr->lpHttpSession = lpwhs;
1053 handle = WININET_AllocHandle( &lpwhr->hdr );
1054 if (NULL == handle)
1056 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1057 goto lend;
1060 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1062 InternetCloseHandle( handle );
1063 handle = NULL;
1064 goto lend;
1067 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1068 HRESULT rc;
1070 len = 0;
1071 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1072 if (rc != E_POINTER)
1073 len = strlenW(lpszObjectName)+1;
1074 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1075 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1076 URL_ESCAPE_SPACES_ONLY);
1077 if (rc)
1079 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName),rc);
1080 strcpyW(lpwhr->lpszPath,lpszObjectName);
1084 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1085 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1087 if(lpszAcceptTypes!=NULL)
1089 int i;
1090 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1091 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1094 if (NULL == lpszVerb)
1096 static const WCHAR szGet[] = {'G','E','T',0};
1097 lpwhr->lpszVerb = WININET_strdupW(szGet);
1099 else if (strlenW(lpszVerb))
1100 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1102 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1104 WCHAR buf[MAXHOSTNAME];
1105 URL_COMPONENTSW UrlComponents;
1107 memset( &UrlComponents, 0, sizeof UrlComponents );
1108 UrlComponents.dwStructSize = sizeof UrlComponents;
1109 UrlComponents.lpszHostName = buf;
1110 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1112 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1113 if (strlenW(UrlComponents.lpszHostName))
1114 HTTP_ProcessHeader(lpwhr, szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1116 else
1117 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1119 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1120 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1121 INTERNET_DEFAULT_HTTPS_PORT :
1122 INTERNET_DEFAULT_HTTP_PORT);
1123 lpwhs->nHostPort = lpwhs->nServerPort;
1125 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1126 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1128 if (hIC->lpszAgent)
1130 WCHAR *agent_header;
1131 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1133 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1134 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1135 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1137 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1138 HTTP_ADDREQ_FLAG_ADD);
1139 HeapFree(GetProcessHeap(), 0, agent_header);
1142 Host = HTTP_GetHeader(lpwhr,szHost);
1144 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1145 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1146 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1148 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1149 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1151 int cnt = 0;
1152 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1153 static const WCHAR szcrlf[] = {'\r','\n',0};
1155 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1157 cnt += sprintfW(lpszCookies, szCookie);
1158 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1159 strcatW(lpszCookies, szcrlf);
1161 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1162 HTTP_ADDREQ_FLAG_ADD);
1163 HeapFree(GetProcessHeap(), 0, lpszCookies);
1165 HeapFree(GetProcessHeap(), 0, lpszUrl);
1168 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1169 INTERNET_STATUS_HANDLE_CREATED, &handle,
1170 sizeof(handle));
1173 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1176 if (!HTTP_ResolveName(lpwhr))
1178 InternetCloseHandle( handle );
1179 handle = NULL;
1182 lend:
1183 if( lpwhr )
1184 WININET_Release( &lpwhr->hdr );
1186 TRACE("<-- %p (%p)\n", handle, lpwhr);
1187 return handle;
1190 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1191 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1192 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1193 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1194 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1195 static const WCHAR szAge[] = { 'A','g','e',0 };
1196 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1197 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1198 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1199 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1200 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1201 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1202 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1203 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1204 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1205 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1206 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1207 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1208 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 };
1209 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1210 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1211 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1212 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1213 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1214 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1215 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1216 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1217 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1218 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1219 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1220 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1221 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1222 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1223 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1224 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1225 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1226 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1227 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1228 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1229 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1230 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1231 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1232 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1233 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1234 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1235 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 };
1236 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1237 static const WCHAR szURI[] = { 'U','R','I',0 };
1238 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1239 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1240 static const WCHAR szVia[] = { 'V','i','a',0 };
1241 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1242 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1244 static const LPCWSTR header_lookup[] = {
1245 szMime_Version, /* HTTP_QUERY_MIME_VERSION = 0 */
1246 szContent_Type, /* HTTP_QUERY_CONTENT_TYPE = 1 */
1247 szContent_Transfer_Encoding,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
1248 szContent_ID, /* HTTP_QUERY_CONTENT_ID = 3 */
1249 NULL, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
1250 szContent_Length, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
1251 szContent_Language, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
1252 szAllow, /* HTTP_QUERY_ALLOW = 7 */
1253 szPublic, /* HTTP_QUERY_PUBLIC = 8 */
1254 szDate, /* HTTP_QUERY_DATE = 9 */
1255 szExpires, /* HTTP_QUERY_EXPIRES = 10 */
1256 szLast_Modified, /* HTTP_QUERY_LAST_MODIFIED = 11 */
1257 NULL, /* HTTP_QUERY_MESSAGE_ID = 12 */
1258 szURI, /* HTTP_QUERY_URI = 13 */
1259 szFrom, /* HTTP_QUERY_DERIVED_FROM = 14 */
1260 NULL, /* HTTP_QUERY_COST = 15 */
1261 NULL, /* HTTP_QUERY_LINK = 16 */
1262 szPragma, /* HTTP_QUERY_PRAGMA = 17 */
1263 NULL, /* HTTP_QUERY_VERSION = 18 */
1264 szStatus, /* HTTP_QUERY_STATUS_CODE = 19 */
1265 NULL, /* HTTP_QUERY_STATUS_TEXT = 20 */
1266 NULL, /* HTTP_QUERY_RAW_HEADERS = 21 */
1267 NULL, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
1268 szConnection, /* HTTP_QUERY_CONNECTION = 23 */
1269 szAccept, /* HTTP_QUERY_ACCEPT = 24 */
1270 szAccept_Charset, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
1271 szAccept_Encoding, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
1272 szAccept_Language, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
1273 szAuthorization, /* HTTP_QUERY_AUTHORIZATION = 28 */
1274 szContent_Encoding, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
1275 NULL, /* HTTP_QUERY_FORWARDED = 30 */
1276 NULL, /* HTTP_QUERY_FROM = 31 */
1277 szIf_Modified_Since, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
1278 szLocation, /* HTTP_QUERY_LOCATION = 33 */
1279 NULL, /* HTTP_QUERY_ORIG_URI = 34 */
1280 szReferer, /* HTTP_QUERY_REFERER = 35 */
1281 szRetry_After, /* HTTP_QUERY_RETRY_AFTER = 36 */
1282 szServer, /* HTTP_QUERY_SERVER = 37 */
1283 NULL, /* HTTP_TITLE = 38 */
1284 szUser_Agent, /* HTTP_QUERY_USER_AGENT = 39 */
1285 szWWW_Authenticate, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
1286 szProxy_Authenticate, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
1287 szAccept_Ranges, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
1288 szSet_Cookie, /* HTTP_QUERY_SET_COOKIE = 43 */
1289 szCookie, /* HTTP_QUERY_COOKIE = 44 */
1290 NULL, /* HTTP_QUERY_REQUEST_METHOD = 45 */
1291 NULL, /* HTTP_QUERY_REFRESH = 46 */
1292 NULL, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
1293 szAge, /* HTTP_QUERY_AGE = 48 */
1294 szCache_Control, /* HTTP_QUERY_CACHE_CONTROL = 49 */
1295 szContent_Base, /* HTTP_QUERY_CONTENT_BASE = 50 */
1296 szContent_Location, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
1297 szContent_MD5, /* HTTP_QUERY_CONTENT_MD5 = 52 */
1298 szContent_Range, /* HTTP_QUERY_CONTENT_RANGE = 53 */
1299 szETag, /* HTTP_QUERY_ETAG = 54 */
1300 szHost, /* HTTP_QUERY_HOST = 55 */
1301 szIf_Match, /* HTTP_QUERY_IF_MATCH = 56 */
1302 szIf_None_Match, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
1303 szIf_Range, /* HTTP_QUERY_IF_RANGE = 58 */
1304 szIf_Unmodified_Since, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
1305 szMax_Forwards, /* HTTP_QUERY_MAX_FORWARDS = 60 */
1306 szProxy_Authorization, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
1307 szRange, /* HTTP_QUERY_RANGE = 62 */
1308 szTransfer_Encoding, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
1309 szUpgrade, /* HTTP_QUERY_UPGRADE = 64 */
1310 szVary, /* HTTP_QUERY_VARY = 65 */
1311 szVia, /* HTTP_QUERY_VIA = 66 */
1312 szWarning, /* HTTP_QUERY_WARNING = 67 */
1313 szExpect, /* HTTP_QUERY_EXPECT = 68 */
1314 szProxy_Connection, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
1315 szUnless_Modified_Since, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
1318 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
1320 /***********************************************************************
1321 * HTTP_HttpQueryInfoW (internal)
1323 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1324 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1326 LPHTTPHEADERW lphttpHdr = NULL;
1327 BOOL bSuccess = FALSE;
1328 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1329 INT requested_index = lpdwIndex ? *lpdwIndex : 0;
1330 INT level = (dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK);
1331 INT index = -1;
1333 /* Find requested header structure */
1334 switch (level)
1336 case HTTP_QUERY_CUSTOM:
1337 index = HTTP_GetCustomHeaderIndex(lpwhr, lpBuffer, requested_index, request_only);
1338 break;
1340 case HTTP_QUERY_RAW_HEADERS_CRLF:
1342 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1343 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1345 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1346 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1347 return FALSE;
1349 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1350 *lpdwBufferLength = len * sizeof(WCHAR);
1352 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1354 return TRUE;
1356 case HTTP_QUERY_RAW_HEADERS:
1358 static const WCHAR szCrLf[] = {'\r','\n',0};
1359 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1360 DWORD i, size = 0;
1361 LPWSTR pszString = (WCHAR*)lpBuffer;
1363 for (i = 0; ppszRawHeaderLines[i]; i++)
1364 size += strlenW(ppszRawHeaderLines[i]) + 1;
1366 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1368 HTTP_FreeTokens(ppszRawHeaderLines);
1369 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1370 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1371 return FALSE;
1374 for (i = 0; ppszRawHeaderLines[i]; i++)
1376 DWORD len = strlenW(ppszRawHeaderLines[i]);
1377 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1378 pszString += len+1;
1380 *pszString = '\0';
1382 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1384 *lpdwBufferLength = size * sizeof(WCHAR);
1385 HTTP_FreeTokens(ppszRawHeaderLines);
1387 return TRUE;
1389 case HTTP_QUERY_STATUS_TEXT:
1390 if (lpwhr->lpszStatusText)
1392 DWORD len = strlenW(lpwhr->lpszStatusText);
1393 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1395 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1396 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1397 return FALSE;
1399 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1400 *lpdwBufferLength = len * sizeof(WCHAR);
1402 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1404 return TRUE;
1406 break;
1407 case HTTP_QUERY_VERSION:
1408 if (lpwhr->lpszVersion)
1410 DWORD len = strlenW(lpwhr->lpszVersion);
1411 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1413 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1414 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1415 return FALSE;
1417 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1418 *lpdwBufferLength = len * sizeof(WCHAR);
1420 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1422 return TRUE;
1424 break;
1425 default:
1426 assert (LAST_TABLE_HEADER == (HTTP_QUERY_UNLESS_MODIFIED_SINCE + 1));
1428 if (level >= 0 && level < LAST_TABLE_HEADER && header_lookup[level])
1429 index = HTTP_GetCustomHeaderIndex(lpwhr, header_lookup[level],
1430 requested_index,request_only);
1433 if (index >= 0)
1434 lphttpHdr = &lpwhr->pCustHeaders[index];
1436 /* Ensure header satisifies requested attributes */
1437 if (!lphttpHdr ||
1438 ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1439 (~lphttpHdr->wFlags & HDR_ISREQUEST)))
1441 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1442 return bSuccess;
1445 if (lpdwIndex)
1446 (*lpdwIndex)++;
1448 /* coalesce value to reuqested type */
1449 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1451 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1452 bSuccess = TRUE;
1454 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1456 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1458 time_t tmpTime;
1459 struct tm tmpTM;
1460 SYSTEMTIME *STHook;
1462 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1464 tmpTM = *gmtime(&tmpTime);
1465 STHook = (SYSTEMTIME *) lpBuffer;
1466 if(STHook==NULL)
1467 return bSuccess;
1469 STHook->wDay = tmpTM.tm_mday;
1470 STHook->wHour = tmpTM.tm_hour;
1471 STHook->wMilliseconds = 0;
1472 STHook->wMinute = tmpTM.tm_min;
1473 STHook->wDayOfWeek = tmpTM.tm_wday;
1474 STHook->wMonth = tmpTM.tm_mon + 1;
1475 STHook->wSecond = tmpTM.tm_sec;
1476 STHook->wYear = tmpTM.tm_year;
1478 bSuccess = TRUE;
1480 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1481 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1482 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1484 else if (lphttpHdr->lpszValue)
1486 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1488 if (len > *lpdwBufferLength)
1490 *lpdwBufferLength = len;
1491 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1492 return bSuccess;
1495 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1496 *lpdwBufferLength = len - sizeof(WCHAR);
1497 bSuccess = TRUE;
1499 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1501 return bSuccess;
1504 /***********************************************************************
1505 * HttpQueryInfoW (WININET.@)
1507 * Queries for information about an HTTP request
1509 * RETURNS
1510 * TRUE on success
1511 * FALSE on failure
1514 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1515 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1517 BOOL bSuccess = FALSE;
1518 LPWININETHTTPREQW lpwhr;
1520 if (TRACE_ON(wininet)) {
1521 #define FE(x) { x, #x }
1522 static const wininet_flag_info query_flags[] = {
1523 FE(HTTP_QUERY_MIME_VERSION),
1524 FE(HTTP_QUERY_CONTENT_TYPE),
1525 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1526 FE(HTTP_QUERY_CONTENT_ID),
1527 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1528 FE(HTTP_QUERY_CONTENT_LENGTH),
1529 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1530 FE(HTTP_QUERY_ALLOW),
1531 FE(HTTP_QUERY_PUBLIC),
1532 FE(HTTP_QUERY_DATE),
1533 FE(HTTP_QUERY_EXPIRES),
1534 FE(HTTP_QUERY_LAST_MODIFIED),
1535 FE(HTTP_QUERY_MESSAGE_ID),
1536 FE(HTTP_QUERY_URI),
1537 FE(HTTP_QUERY_DERIVED_FROM),
1538 FE(HTTP_QUERY_COST),
1539 FE(HTTP_QUERY_LINK),
1540 FE(HTTP_QUERY_PRAGMA),
1541 FE(HTTP_QUERY_VERSION),
1542 FE(HTTP_QUERY_STATUS_CODE),
1543 FE(HTTP_QUERY_STATUS_TEXT),
1544 FE(HTTP_QUERY_RAW_HEADERS),
1545 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1546 FE(HTTP_QUERY_CONNECTION),
1547 FE(HTTP_QUERY_ACCEPT),
1548 FE(HTTP_QUERY_ACCEPT_CHARSET),
1549 FE(HTTP_QUERY_ACCEPT_ENCODING),
1550 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1551 FE(HTTP_QUERY_AUTHORIZATION),
1552 FE(HTTP_QUERY_CONTENT_ENCODING),
1553 FE(HTTP_QUERY_FORWARDED),
1554 FE(HTTP_QUERY_FROM),
1555 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1556 FE(HTTP_QUERY_LOCATION),
1557 FE(HTTP_QUERY_ORIG_URI),
1558 FE(HTTP_QUERY_REFERER),
1559 FE(HTTP_QUERY_RETRY_AFTER),
1560 FE(HTTP_QUERY_SERVER),
1561 FE(HTTP_QUERY_TITLE),
1562 FE(HTTP_QUERY_USER_AGENT),
1563 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1564 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1565 FE(HTTP_QUERY_ACCEPT_RANGES),
1566 FE(HTTP_QUERY_SET_COOKIE),
1567 FE(HTTP_QUERY_COOKIE),
1568 FE(HTTP_QUERY_REQUEST_METHOD),
1569 FE(HTTP_QUERY_REFRESH),
1570 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1571 FE(HTTP_QUERY_AGE),
1572 FE(HTTP_QUERY_CACHE_CONTROL),
1573 FE(HTTP_QUERY_CONTENT_BASE),
1574 FE(HTTP_QUERY_CONTENT_LOCATION),
1575 FE(HTTP_QUERY_CONTENT_MD5),
1576 FE(HTTP_QUERY_CONTENT_RANGE),
1577 FE(HTTP_QUERY_ETAG),
1578 FE(HTTP_QUERY_HOST),
1579 FE(HTTP_QUERY_IF_MATCH),
1580 FE(HTTP_QUERY_IF_NONE_MATCH),
1581 FE(HTTP_QUERY_IF_RANGE),
1582 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1583 FE(HTTP_QUERY_MAX_FORWARDS),
1584 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1585 FE(HTTP_QUERY_RANGE),
1586 FE(HTTP_QUERY_TRANSFER_ENCODING),
1587 FE(HTTP_QUERY_UPGRADE),
1588 FE(HTTP_QUERY_VARY),
1589 FE(HTTP_QUERY_VIA),
1590 FE(HTTP_QUERY_WARNING),
1591 FE(HTTP_QUERY_CUSTOM)
1593 static const wininet_flag_info modifier_flags[] = {
1594 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1595 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1596 FE(HTTP_QUERY_FLAG_NUMBER),
1597 FE(HTTP_QUERY_FLAG_COALESCE)
1599 #undef FE
1600 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1601 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1602 DWORD i;
1604 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1605 TRACE(" Attribute:");
1606 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1607 if (query_flags[i].val == info) {
1608 TRACE(" %s", query_flags[i].name);
1609 break;
1612 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1613 TRACE(" Unknown (%08x)", info);
1616 TRACE(" Modifier:");
1617 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1618 if (modifier_flags[i].val & info_mod) {
1619 TRACE(" %s", modifier_flags[i].name);
1620 info_mod &= ~ modifier_flags[i].val;
1624 if (info_mod) {
1625 TRACE(" Unknown (%08x)", info_mod);
1627 TRACE("\n");
1630 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1631 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1633 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1634 goto lend;
1637 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1638 lpBuffer, lpdwBufferLength, lpdwIndex);
1640 lend:
1641 if( lpwhr )
1642 WININET_Release( &lpwhr->hdr );
1644 TRACE("%d <--\n", bSuccess);
1645 return bSuccess;
1648 /***********************************************************************
1649 * HttpQueryInfoA (WININET.@)
1651 * Queries for information about an HTTP request
1653 * RETURNS
1654 * TRUE on success
1655 * FALSE on failure
1658 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1659 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1661 BOOL result;
1662 DWORD len;
1663 WCHAR* bufferW;
1665 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1666 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1668 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1669 lpdwBufferLength, lpdwIndex );
1672 len = (*lpdwBufferLength)*sizeof(WCHAR);
1673 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1674 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1675 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
1676 MultiByteToWideChar(CP_ACP,0,lpBuffer,-1,bufferW,len);
1677 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1678 &len, lpdwIndex );
1679 if( result )
1681 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1682 lpBuffer, *lpdwBufferLength, NULL, NULL );
1683 *lpdwBufferLength = len - 1;
1685 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1687 else
1688 /* since the strings being returned from HttpQueryInfoW should be
1689 * only ASCII characters, it is reasonable to assume that all of
1690 * the Unicode characters can be reduced to a single byte */
1691 *lpdwBufferLength = len / sizeof(WCHAR);
1693 HeapFree(GetProcessHeap(), 0, bufferW );
1695 return result;
1698 /***********************************************************************
1699 * HttpSendRequestExA (WININET.@)
1701 * Sends the specified request to the HTTP server and allows chunked
1702 * transfers.
1704 * RETURNS
1705 * Success: TRUE
1706 * Failure: FALSE, call GetLastError() for more information.
1708 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1709 LPINTERNET_BUFFERSA lpBuffersIn,
1710 LPINTERNET_BUFFERSA lpBuffersOut,
1711 DWORD dwFlags, DWORD dwContext)
1713 INTERNET_BUFFERSW BuffersInW;
1714 BOOL rc = FALSE;
1715 DWORD headerlen;
1716 LPWSTR header = NULL;
1718 TRACE("(%p, %p, %p, %08x, %08x): stub\n", hRequest, lpBuffersIn,
1719 lpBuffersOut, dwFlags, dwContext);
1721 if (lpBuffersIn)
1723 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1724 if (lpBuffersIn->lpcszHeader)
1726 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
1727 lpBuffersIn->dwHeadersLength,0,0);
1728 header = HeapAlloc(GetProcessHeap(),0,headerlen*sizeof(WCHAR));
1729 if (!(BuffersInW.lpcszHeader = header))
1731 SetLastError(ERROR_OUTOFMEMORY);
1732 return FALSE;
1734 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1735 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1736 header, headerlen);
1738 else
1739 BuffersInW.lpcszHeader = NULL;
1740 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
1741 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
1742 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
1743 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
1744 BuffersInW.Next = NULL;
1747 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
1749 HeapFree(GetProcessHeap(),0,header);
1751 return rc;
1754 /***********************************************************************
1755 * HttpSendRequestExW (WININET.@)
1757 * Sends the specified request to the HTTP server and allows chunked
1758 * transfers
1760 * RETURNS
1761 * Success: TRUE
1762 * Failure: FALSE, call GetLastError() for more information.
1764 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1765 LPINTERNET_BUFFERSW lpBuffersIn,
1766 LPINTERNET_BUFFERSW lpBuffersOut,
1767 DWORD dwFlags, DWORD dwContext)
1769 BOOL ret;
1770 LPWININETHTTPREQW lpwhr;
1771 LPWININETHTTPSESSIONW lpwhs;
1772 LPWININETAPPINFOW hIC;
1774 TRACE("(%p, %p, %p, %08x, %08x)\n", hRequest, lpBuffersIn,
1775 lpBuffersOut, dwFlags, dwContext);
1777 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1779 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1781 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1782 return FALSE;
1785 lpwhs = lpwhr->lpHttpSession;
1786 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
1787 hIC = lpwhs->lpAppInfo;
1788 assert(hIC->hdr.htype == WH_HINIT);
1790 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1792 WORKREQUEST workRequest;
1793 struct WORKREQ_HTTPSENDREQUESTW *req;
1795 workRequest.asyncproc = AsyncHttpSendRequestProc;
1796 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1797 req = &workRequest.u.HttpSendRequestW;
1798 if (lpBuffersIn)
1800 if (lpBuffersIn->lpcszHeader)
1801 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1802 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
1803 else
1804 req->lpszHeader = NULL;
1805 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
1806 req->lpOptional = lpBuffersIn->lpvBuffer;
1807 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
1808 req->dwContentLength = lpBuffersIn->dwBufferTotal;
1810 else
1812 req->lpszHeader = NULL;
1813 req->dwHeaderLength = 0;
1814 req->lpOptional = NULL;
1815 req->dwOptionalLength = 0;
1816 req->dwContentLength = 0;
1819 req->bEndRequest = FALSE;
1821 INTERNET_AsyncCall(&workRequest);
1823 * This is from windows.
1825 SetLastError(ERROR_IO_PENDING);
1826 ret = FALSE;
1828 else
1830 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1831 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
1832 lpBuffersIn->dwBufferTotal, FALSE);
1835 WININET_Release(&lpwhr->hdr);
1836 TRACE("<---\n");
1837 return ret;
1840 /***********************************************************************
1841 * HttpSendRequestW (WININET.@)
1843 * Sends the specified request to the HTTP server
1845 * RETURNS
1846 * TRUE on success
1847 * FALSE on failure
1850 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1851 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1853 LPWININETHTTPREQW lpwhr;
1854 LPWININETHTTPSESSIONW lpwhs = NULL;
1855 LPWININETAPPINFOW hIC = NULL;
1856 BOOL r;
1858 TRACE("%p, %p (%s), %i, %p, %i)\n", hHttpRequest,
1859 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1861 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1862 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1864 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1865 r = FALSE;
1866 goto lend;
1869 lpwhs = lpwhr->lpHttpSession;
1870 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1872 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1873 r = FALSE;
1874 goto lend;
1877 hIC = lpwhs->lpAppInfo;
1878 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1880 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1881 r = FALSE;
1882 goto lend;
1885 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1887 WORKREQUEST workRequest;
1888 struct WORKREQ_HTTPSENDREQUESTW *req;
1890 workRequest.asyncproc = AsyncHttpSendRequestProc;
1891 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1892 req = &workRequest.u.HttpSendRequestW;
1893 if (lpszHeaders)
1894 req->lpszHeader = WININET_strdupW(lpszHeaders);
1895 else
1896 req->lpszHeader = 0;
1897 req->dwHeaderLength = dwHeaderLength;
1898 req->lpOptional = lpOptional;
1899 req->dwOptionalLength = dwOptionalLength;
1900 req->dwContentLength = dwOptionalLength;
1901 req->bEndRequest = TRUE;
1903 INTERNET_AsyncCall(&workRequest);
1905 * This is from windows.
1907 SetLastError(ERROR_IO_PENDING);
1908 r = FALSE;
1910 else
1912 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1913 dwHeaderLength, lpOptional, dwOptionalLength,
1914 dwOptionalLength, TRUE);
1916 lend:
1917 if( lpwhr )
1918 WININET_Release( &lpwhr->hdr );
1919 return r;
1922 /***********************************************************************
1923 * HttpSendRequestA (WININET.@)
1925 * Sends the specified request to the HTTP server
1927 * RETURNS
1928 * TRUE on success
1929 * FALSE on failure
1932 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1933 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1935 BOOL result;
1936 LPWSTR szHeaders=NULL;
1937 DWORD nLen=dwHeaderLength;
1938 if(lpszHeaders!=NULL)
1940 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1941 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1942 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1944 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1945 HeapFree(GetProcessHeap(),0,szHeaders);
1946 return result;
1949 /***********************************************************************
1950 * HTTP_HandleRedirect (internal)
1952 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl)
1954 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
1955 LPWININETAPPINFOW hIC = lpwhs->lpAppInfo;
1956 WCHAR path[2048];
1958 if(lpszUrl[0]=='/')
1960 /* if it's an absolute path, keep the same session info */
1961 lstrcpynW(path, lpszUrl, 2048);
1963 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1965 TRACE("Redirect through proxy\n");
1966 lstrcpynW(path, lpszUrl, 2048);
1968 else
1970 URL_COMPONENTSW urlComponents;
1971 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1972 static WCHAR szHttp[] = {'h','t','t','p',0};
1973 static WCHAR szHttps[] = {'h','t','t','p','s',0};
1974 DWORD url_length = 0;
1975 LPWSTR orig_url;
1976 LPWSTR combined_url;
1978 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1979 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? szHttps : szHttp;
1980 urlComponents.dwSchemeLength = 0;
1981 urlComponents.lpszHostName = lpwhs->lpszHostName;
1982 urlComponents.dwHostNameLength = 0;
1983 urlComponents.nPort = lpwhs->nHostPort;
1984 urlComponents.lpszUserName = lpwhs->lpszUserName;
1985 urlComponents.dwUserNameLength = 0;
1986 urlComponents.lpszPassword = NULL;
1987 urlComponents.dwPasswordLength = 0;
1988 urlComponents.lpszUrlPath = lpwhr->lpszPath;
1989 urlComponents.dwUrlPathLength = 0;
1990 urlComponents.lpszExtraInfo = NULL;
1991 urlComponents.dwExtraInfoLength = 0;
1993 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
1994 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
1995 return FALSE;
1997 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length);
1999 /* convert from bytes to characters */
2000 url_length = url_length / sizeof(WCHAR) - 1;
2001 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2003 HeapFree(GetProcessHeap(), 0, orig_url);
2004 return FALSE;
2007 url_length = 0;
2008 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2009 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2011 HeapFree(GetProcessHeap(), 0, orig_url);
2012 return FALSE;
2014 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2016 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2018 HeapFree(GetProcessHeap(), 0, orig_url);
2019 HeapFree(GetProcessHeap(), 0, combined_url);
2020 return FALSE;
2022 HeapFree(GetProcessHeap(), 0, orig_url);
2024 userName[0] = 0;
2025 hostName[0] = 0;
2026 protocol[0] = 0;
2028 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2029 urlComponents.lpszScheme = protocol;
2030 urlComponents.dwSchemeLength = 32;
2031 urlComponents.lpszHostName = hostName;
2032 urlComponents.dwHostNameLength = MAXHOSTNAME;
2033 urlComponents.lpszUserName = userName;
2034 urlComponents.dwUserNameLength = 1024;
2035 urlComponents.lpszPassword = NULL;
2036 urlComponents.dwPasswordLength = 0;
2037 urlComponents.lpszUrlPath = path;
2038 urlComponents.dwUrlPathLength = 2048;
2039 urlComponents.lpszExtraInfo = NULL;
2040 urlComponents.dwExtraInfoLength = 0;
2041 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2043 HeapFree(GetProcessHeap(), 0, combined_url);
2044 return FALSE;
2046 HeapFree(GetProcessHeap(), 0, combined_url);
2048 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2049 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2051 TRACE("redirect from secure page to non-secure page\n");
2052 /* FIXME: warn about from secure redirect to non-secure page */
2053 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2055 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2056 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2058 TRACE("redirect from non-secure page to secure page\n");
2059 /* FIXME: notify about redirect to secure page */
2060 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2063 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2065 if (lstrlenW(protocol)>4) /*https*/
2066 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2067 else /*http*/
2068 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2071 #if 0
2073 * This upsets redirects to binary files on sourceforge.net
2074 * and gives an html page instead of the target file
2075 * Examination of the HTTP request sent by native wininet.dll
2076 * reveals that it doesn't send a referrer in that case.
2077 * Maybe there's a flag that enables this, or maybe a referrer
2078 * shouldn't be added in case of a redirect.
2081 /* consider the current host as the referrer */
2082 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2083 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2084 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2085 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2086 #endif
2088 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2089 lpwhs->lpszServerName = WININET_strdupW(hostName);
2090 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2091 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2092 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2094 int len;
2095 static const WCHAR fmt[] = {'%','s',':','%','i',0};
2096 len = lstrlenW(hostName);
2097 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2098 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2099 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2101 else
2102 lpwhs->lpszHostName = WININET_strdupW(hostName);
2104 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2107 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2108 lpwhs->lpszUserName = NULL;
2109 if (userName[0])
2110 lpwhs->lpszUserName = WININET_strdupW(userName);
2111 lpwhs->nServerPort = urlComponents.nPort;
2113 if (!HTTP_ResolveName(lpwhr))
2114 return FALSE;
2116 NETCON_close(&lpwhr->netConnection);
2118 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2119 return FALSE;
2122 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2123 lpwhr->lpszPath=NULL;
2124 if (strlenW(path))
2126 DWORD needed = 0;
2127 HRESULT rc;
2129 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2130 if (rc != E_POINTER)
2131 needed = strlenW(path)+1;
2132 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2133 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2134 URL_ESCAPE_SPACES_ONLY);
2135 if (rc)
2137 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path),rc);
2138 strcpyW(lpwhr->lpszPath,path);
2142 return TRUE;
2145 /***********************************************************************
2146 * HTTP_build_req (internal)
2148 * concatenate all the strings in the request together
2150 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2152 LPCWSTR *t;
2153 LPWSTR str;
2155 for( t = list; *t ; t++ )
2156 len += strlenW( *t );
2157 len++;
2159 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2160 *str = 0;
2162 for( t = list; *t ; t++ )
2163 strcatW( str, *t );
2165 return str;
2168 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2170 LPWSTR lpszPath;
2171 LPWSTR requestString;
2172 INT len;
2173 INT cnt;
2174 INT responseLen;
2175 char *ascii_req;
2176 BOOL ret;
2177 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2178 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2179 LPWININETHTTPSESSIONW lpwhs = lpwhr->lpHttpSession;
2181 TRACE("\n");
2183 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2184 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2185 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2186 HeapFree( GetProcessHeap(), 0, lpszPath );
2188 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2189 NULL, 0, NULL, NULL );
2190 len--; /* the nul terminator isn't needed */
2191 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2192 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2193 ascii_req, len, NULL, NULL );
2194 HeapFree( GetProcessHeap(), 0, requestString );
2196 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2198 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2199 HeapFree( GetProcessHeap(), 0, ascii_req );
2200 if (!ret || cnt < 0)
2201 return FALSE;
2203 responseLen = HTTP_GetResponseHeaders( lpwhr );
2204 if (!responseLen)
2205 return FALSE;
2207 return TRUE;
2210 /***********************************************************************
2211 * HTTP_HttpSendRequestW (internal)
2213 * Sends the specified request to the HTTP server
2215 * RETURNS
2216 * TRUE on success
2217 * FALSE on failure
2220 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2221 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2222 DWORD dwContentLength, BOOL bEndRequest)
2224 INT cnt;
2225 BOOL bSuccess = FALSE;
2226 LPWSTR requestString = NULL;
2227 INT responseLen;
2228 BOOL loop_next = FALSE;
2229 INTERNET_ASYNC_RESULT iar;
2231 TRACE("--> %p\n", lpwhr);
2233 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2235 /* Clear any error information */
2236 INTERNET_SetLastError(0);
2238 HTTP_FixVerb(lpwhr);
2240 /* if we are using optional stuff, we must add the fixed header of that option length */
2241 if (dwContentLength > 0)
2243 static const WCHAR szContentLength[] = {
2244 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2245 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2246 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2247 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L,
2248 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2253 DWORD len;
2254 char *ascii_req;
2256 if (TRACE_ON(wininet))
2258 LPHTTPHEADERW Host = HTTP_GetHeader(lpwhr,szHost);
2259 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2262 HTTP_FixURL(lpwhr);
2264 /* add the headers the caller supplied */
2265 if( lpszHeaders && dwHeaderLength )
2267 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2268 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2271 /* if there's a proxy username and password, add it to the headers */
2272 HTTP_AddProxyInfo(lpwhr);
2274 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2276 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2278 /* Send the request and store the results */
2279 if (!HTTP_OpenConnection(lpwhr))
2280 goto lend;
2282 /* send the request as ASCII, tack on the optional data */
2283 if( !lpOptional )
2284 dwOptionalLength = 0;
2285 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2286 NULL, 0, NULL, NULL );
2287 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2288 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2289 ascii_req, len, NULL, NULL );
2290 if( lpOptional )
2291 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2292 len = (len + dwOptionalLength - 1);
2293 ascii_req[len] = 0;
2294 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2296 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2297 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2299 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2300 HeapFree( GetProcessHeap(), 0, ascii_req );
2302 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2303 INTERNET_STATUS_REQUEST_SENT,
2304 &len, sizeof(DWORD));
2306 if (bEndRequest)
2308 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2309 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2311 if (cnt < 0)
2312 goto lend;
2314 responseLen = HTTP_GetResponseHeaders(lpwhr);
2315 if (responseLen)
2316 bSuccess = TRUE;
2318 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2319 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2320 sizeof(DWORD));
2322 HTTP_ProcessHeaders(lpwhr);
2324 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
2326 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2327 WCHAR szNewLocation[2048];
2328 DWORD dwBufferSize=2048;
2329 if (HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2330 (dwCode==HTTP_STATUS_REDIRECT || dwCode==HTTP_STATUS_MOVED) &&
2331 HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2333 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2334 INTERNET_STATUS_REDIRECT, szNewLocation,
2335 dwBufferSize);
2336 bSuccess = HTTP_HandleRedirect(lpwhr, szNewLocation);
2337 if (bSuccess)
2339 HeapFree(GetProcessHeap(), 0, requestString);
2340 loop_next = TRUE;
2346 else
2347 bSuccess = TRUE;
2349 while (loop_next);
2351 lend:
2353 HeapFree(GetProcessHeap(), 0, requestString);
2355 /* TODO: send notification for P3P header */
2357 iar.dwResult = (DWORD)bSuccess;
2358 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2360 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2361 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2362 sizeof(INTERNET_ASYNC_RESULT));
2364 TRACE("<--\n");
2365 return bSuccess;
2368 /***********************************************************************
2369 * HTTP_Connect (internal)
2371 * Create http session handle
2373 * RETURNS
2374 * HINTERNET a session handle on success
2375 * NULL on failure
2378 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2379 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2380 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2381 DWORD dwInternalFlags)
2383 BOOL bSuccess = FALSE;
2384 LPWININETHTTPSESSIONW lpwhs = NULL;
2385 HINTERNET handle = NULL;
2387 TRACE("-->\n");
2389 assert( hIC->hdr.htype == WH_HINIT );
2391 hIC->hdr.dwContext = dwContext;
2393 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2394 if (NULL == lpwhs)
2396 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2397 goto lerror;
2401 * According to my tests. The name is not resolved until a request is sent
2404 lpwhs->hdr.htype = WH_HHTTPSESSION;
2405 lpwhs->hdr.dwFlags = dwFlags;
2406 lpwhs->hdr.dwContext = dwContext;
2407 lpwhs->hdr.dwInternalFlags = dwInternalFlags | (hIC->hdr.dwInternalFlags & INET_CALLBACKW);
2408 lpwhs->hdr.dwRefCount = 1;
2409 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2410 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2412 WININET_AddRef( &hIC->hdr );
2413 lpwhs->lpAppInfo = hIC;
2415 handle = WININET_AllocHandle( &lpwhs->hdr );
2416 if (NULL == handle)
2418 ERR("Failed to alloc handle\n");
2419 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2420 goto lerror;
2423 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2424 if(strchrW(hIC->lpszProxy, ' '))
2425 FIXME("Several proxies not implemented.\n");
2426 if(hIC->lpszProxyBypass)
2427 FIXME("Proxy bypass is ignored.\n");
2429 if (lpszServerName && lpszServerName[0])
2431 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2432 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2434 if (lpszUserName && lpszUserName[0])
2435 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2436 lpwhs->nServerPort = nServerPort;
2437 lpwhs->nHostPort = nServerPort;
2439 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2440 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2442 INTERNET_SendCallback(&hIC->hdr, dwContext,
2443 INTERNET_STATUS_HANDLE_CREATED, &handle,
2444 sizeof(handle));
2447 bSuccess = TRUE;
2449 lerror:
2450 if( lpwhs )
2451 WININET_Release( &lpwhs->hdr );
2454 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2455 * windows
2458 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2459 return handle;
2463 /***********************************************************************
2464 * HTTP_OpenConnection (internal)
2466 * Connect to a web server
2468 * RETURNS
2470 * TRUE on success
2471 * FALSE on failure
2473 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2475 BOOL bSuccess = FALSE;
2476 LPWININETHTTPSESSIONW lpwhs;
2477 LPWININETAPPINFOW hIC = NULL;
2478 char szaddr[32];
2480 TRACE("-->\n");
2483 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2485 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2486 goto lend;
2489 lpwhs = lpwhr->lpHttpSession;
2491 hIC = lpwhs->lpAppInfo;
2492 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2493 szaddr, sizeof(szaddr));
2494 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2495 INTERNET_STATUS_CONNECTING_TO_SERVER,
2496 szaddr,
2497 strlen(szaddr)+1);
2499 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2500 SOCK_STREAM, 0))
2502 WARN("Socket creation failed\n");
2503 goto lend;
2506 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2507 sizeof(lpwhs->socketAddress)))
2508 goto lend;
2510 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2512 /* Note: we differ from Microsoft's WinINet here. they seem to have
2513 * a bug that causes no status callbacks to be sent when starting
2514 * a tunnel to a proxy server using the CONNECT verb. i believe our
2515 * behaviour to be more correct and to not cause any incompatibilities
2516 * because using a secure connection through a proxy server is a rare
2517 * case that would be hard for anyone to depend on */
2518 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2519 goto lend;
2521 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2523 WARN("Couldn't connect securely to host\n");
2524 goto lend;
2528 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2529 INTERNET_STATUS_CONNECTED_TO_SERVER,
2530 szaddr, strlen(szaddr)+1);
2532 bSuccess = TRUE;
2534 lend:
2535 TRACE("%d <--\n", bSuccess);
2536 return bSuccess;
2540 /***********************************************************************
2541 * HTTP_clear_response_headers (internal)
2543 * clear out any old response headers
2545 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2547 DWORD i;
2549 for( i=0; i<lpwhr->nCustHeaders; i++)
2551 if( !lpwhr->pCustHeaders[i].lpszField )
2552 continue;
2553 if( !lpwhr->pCustHeaders[i].lpszValue )
2554 continue;
2555 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2556 continue;
2557 HTTP_DeleteCustomHeader( lpwhr, i );
2558 i--;
2562 /***********************************************************************
2563 * HTTP_GetResponseHeaders (internal)
2565 * Read server response
2567 * RETURNS
2569 * TRUE on success
2570 * FALSE on error
2572 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2574 INT cbreaks = 0;
2575 WCHAR buffer[MAX_REPLY_LEN];
2576 DWORD buflen = MAX_REPLY_LEN;
2577 BOOL bSuccess = FALSE;
2578 INT rc = 0;
2579 static const WCHAR szCrLf[] = {'\r','\n',0};
2580 char bufferA[MAX_REPLY_LEN];
2581 LPWSTR status_code, status_text;
2582 DWORD cchMaxRawHeaders = 1024;
2583 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2584 DWORD cchRawHeaders = 0;
2586 TRACE("-->\n");
2588 /* clear old response headers (eg. from a redirect response) */
2589 HTTP_clear_response_headers( lpwhr );
2591 if (!NETCON_connected(&lpwhr->netConnection))
2592 goto lend;
2595 * HACK peek at the buffer
2597 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2600 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2602 buflen = MAX_REPLY_LEN;
2603 memset(buffer, 0, MAX_REPLY_LEN);
2604 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2605 goto lend;
2606 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2608 /* regenerate raw headers */
2609 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2611 cchMaxRawHeaders *= 2;
2612 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2614 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2615 cchRawHeaders += (buflen-1);
2616 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2617 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2618 lpszRawHeaders[cchRawHeaders] = '\0';
2620 /* split the version from the status code */
2621 status_code = strchrW( buffer, ' ' );
2622 if( !status_code )
2623 goto lend;
2624 *status_code++=0;
2626 /* split the status code from the status text */
2627 status_text = strchrW( status_code, ' ' );
2628 if( !status_text )
2629 goto lend;
2630 *status_text++=0;
2632 TRACE("version [%s] status code [%s] status text [%s]\n",
2633 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2635 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
2636 HTTP_ADDHDR_FLAG_REPLACE);
2638 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
2639 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
2641 lpwhr->lpszVersion= WININET_strdupW(buffer);
2642 lpwhr->lpszStatusText = WININET_strdupW(status_text);
2644 /* Parse each response line */
2647 buflen = MAX_REPLY_LEN;
2648 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2650 LPWSTR * pFieldAndValue;
2652 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2653 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2655 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2657 cchMaxRawHeaders *= 2;
2658 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2660 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2661 cchRawHeaders += (buflen-1);
2662 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2663 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2664 lpszRawHeaders[cchRawHeaders] = '\0';
2666 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2667 if (!pFieldAndValue)
2668 break;
2670 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2671 HTTP_ADDREQ_FLAG_ADD );
2673 HTTP_FreeTokens(pFieldAndValue);
2675 else
2677 cbreaks++;
2678 if (cbreaks >= 2)
2679 break;
2681 }while(1);
2683 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2684 lpwhr->lpszRawHeaders = lpszRawHeaders;
2685 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2686 bSuccess = TRUE;
2688 lend:
2690 TRACE("<--\n");
2691 if (bSuccess)
2692 return rc;
2693 else
2694 return 0;
2698 static void strip_spaces(LPWSTR start)
2700 LPWSTR str = start;
2701 LPWSTR end;
2703 while (*str == ' ' && *str != '\0')
2704 str++;
2706 if (str != start)
2707 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2709 end = start + strlenW(start) - 1;
2710 while (end >= start && *end == ' ')
2712 *end = '\0';
2713 end--;
2718 /***********************************************************************
2719 * HTTP_InterpretHttpHeader (internal)
2721 * Parse server response
2723 * RETURNS
2725 * Pointer to array of field, value, NULL on success.
2726 * NULL on error.
2728 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2730 LPWSTR * pTokenPair;
2731 LPWSTR pszColon;
2732 INT len;
2734 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2736 pszColon = strchrW(buffer, ':');
2737 /* must have two tokens */
2738 if (!pszColon)
2740 HTTP_FreeTokens(pTokenPair);
2741 if (buffer[0])
2742 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2743 return NULL;
2746 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2747 if (!pTokenPair[0])
2749 HTTP_FreeTokens(pTokenPair);
2750 return NULL;
2752 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2753 pTokenPair[0][pszColon - buffer] = '\0';
2755 /* skip colon */
2756 pszColon++;
2757 len = strlenW(pszColon);
2758 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2759 if (!pTokenPair[1])
2761 HTTP_FreeTokens(pTokenPair);
2762 return NULL;
2764 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2766 strip_spaces(pTokenPair[0]);
2767 strip_spaces(pTokenPair[1]);
2769 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2770 return pTokenPair;
2773 /***********************************************************************
2774 * HTTP_ProcessHeader (internal)
2776 * Stuff header into header tables according to <dwModifier>
2780 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2782 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2784 LPHTTPHEADERW lphttpHdr = NULL;
2785 BOOL bSuccess = FALSE;
2786 INT index = -1;
2787 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2788 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
2790 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), dwModifier);
2792 /* Don't let applications add Connection header to request */
2793 if (strcmpW(szConnection,field)==0 && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2795 return FALSE;
2798 /* REPLACE wins out over ADD */
2799 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2800 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
2802 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
2803 index = -1;
2804 else
2805 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
2807 if (index >= 0)
2809 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2811 return FALSE;
2813 lphttpHdr = &lpwhr->pCustHeaders[index];
2815 else if (value)
2817 HTTPHEADERW hdr;
2819 hdr.lpszField = (LPWSTR)field;
2820 hdr.lpszValue = (LPWSTR)value;
2821 hdr.wFlags = hdr.wCount = 0;
2823 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2824 hdr.wFlags |= HDR_ISREQUEST;
2826 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2829 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2830 lphttpHdr->wFlags |= HDR_ISREQUEST;
2831 else
2832 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2834 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2836 HTTP_DeleteCustomHeader( lpwhr, index );
2838 if (value)
2840 HTTPHEADERW hdr;
2842 hdr.lpszField = (LPWSTR)field;
2843 hdr.lpszValue = (LPWSTR)value;
2844 hdr.wFlags = hdr.wCount = 0;
2846 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2847 hdr.wFlags |= HDR_ISREQUEST;
2849 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2852 return TRUE;
2854 else if (dwModifier & COALESCEFLASG)
2856 LPWSTR lpsztmp;
2857 WCHAR ch = 0;
2858 INT len = 0;
2859 INT origlen = strlenW(lphttpHdr->lpszValue);
2860 INT valuelen = strlenW(value);
2862 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2864 ch = ',';
2865 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2867 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2869 ch = ';';
2870 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2873 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
2875 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2876 if (lpsztmp)
2878 lphttpHdr->lpszValue = lpsztmp;
2879 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2880 if (ch > 0)
2882 lphttpHdr->lpszValue[origlen] = ch;
2883 origlen++;
2884 lphttpHdr->lpszValue[origlen] = ' ';
2885 origlen++;
2888 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2889 lphttpHdr->lpszValue[len] = '\0';
2890 bSuccess = TRUE;
2892 else
2894 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2895 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2898 TRACE("<-- %d\n",bSuccess);
2899 return bSuccess;
2903 /***********************************************************************
2904 * HTTP_CloseConnection (internal)
2906 * Close socket connection
2909 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2911 LPWININETHTTPSESSIONW lpwhs = NULL;
2912 LPWININETAPPINFOW hIC = NULL;
2914 TRACE("%p\n",lpwhr);
2916 lpwhs = lpwhr->lpHttpSession;
2917 hIC = lpwhs->lpAppInfo;
2919 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2920 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2922 if (NETCON_connected(&lpwhr->netConnection))
2924 NETCON_close(&lpwhr->netConnection);
2927 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2928 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2932 /***********************************************************************
2933 * HTTP_CloseHTTPRequestHandle (internal)
2935 * Deallocate request handle
2938 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2940 DWORD i;
2941 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2943 TRACE("\n");
2945 WININET_Release(&lpwhr->hdr);
2947 if (NETCON_connected(&lpwhr->netConnection))
2948 HTTP_CloseConnection(lpwhr);
2950 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2951 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2952 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2953 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
2954 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
2956 for (i = 0; i < lpwhr->nCustHeaders; i++)
2958 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2959 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2962 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2963 HeapFree(GetProcessHeap(), 0, lpwhr);
2967 /***********************************************************************
2968 * HTTP_CloseHTTPSessionHandle (internal)
2970 * Deallocate session handle
2973 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2975 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2977 TRACE("%p\n", lpwhs);
2979 WININET_Release(&lpwhs->lpAppInfo->hdr);
2981 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2982 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2983 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2984 HeapFree(GetProcessHeap(), 0, lpwhs);
2988 /***********************************************************************
2989 * HTTP_GetCustomHeaderIndex (internal)
2991 * Return index of custom header from header array
2994 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,
2995 int requested_index, BOOL request_only)
2997 DWORD index;
2999 TRACE("%s\n", debugstr_w(lpszField));
3001 for (index = 0; index < lpwhr->nCustHeaders; index++)
3003 if (strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3004 continue;
3006 if (request_only && !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3007 continue;
3009 if (!request_only && (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))
3010 continue;
3012 if (requested_index == 0)
3013 break;
3014 requested_index --;
3017 if (index >= lpwhr->nCustHeaders)
3018 index = -1;
3020 TRACE("Return: %d\n", index);
3021 return index;
3025 /***********************************************************************
3026 * HTTP_InsertCustomHeader (internal)
3028 * Insert header into array
3031 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3033 INT count;
3034 LPHTTPHEADERW lph = NULL;
3035 BOOL r = FALSE;
3037 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3038 count = lpwhr->nCustHeaders + 1;
3039 if (count > 1)
3040 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3041 else
3042 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3044 if (NULL != lph)
3046 lpwhr->pCustHeaders = lph;
3047 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3048 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3049 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3050 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3051 lpwhr->nCustHeaders++;
3052 r = TRUE;
3054 else
3056 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3059 return r;
3063 /***********************************************************************
3064 * HTTP_DeleteCustomHeader (internal)
3066 * Delete header from array
3067 * If this function is called, the indexs may change.
3069 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3071 if( lpwhr->nCustHeaders <= 0 )
3072 return FALSE;
3073 if( index >= lpwhr->nCustHeaders )
3074 return FALSE;
3075 lpwhr->nCustHeaders--;
3077 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3078 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3079 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3081 return TRUE;
3084 /***********************************************************************
3085 * IsHostInProxyBypassList (@)
3087 * Undocumented
3090 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3092 FIXME("STUB: flags=%d host=%s length=%d\n",flags,szHost,length);
3093 return FALSE;