Added implementation of security.dll.
[wine/hacks.git] / dlls / wininet / http.c
blobee78f24a0255fa8994ec233c4af8b4e230f2c00a
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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,
106 LPCWSTR lpszHeaders, DWORD dwHeaderLength, LPVOID lpOptional, DWORD
107 dwOptionalLength, DWORD dwContentLength);
110 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
112 int HeaderIndex = 0;
113 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
114 if (HeaderIndex == -1)
115 return NULL;
116 else
117 return &req->pCustHeaders[HeaderIndex];
120 /***********************************************************************
121 * HTTP_Tokenize (internal)
123 * Tokenize a string, allocating memory for the tokens.
125 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
127 LPWSTR * token_array;
128 int tokens = 0;
129 int i;
130 LPCWSTR next_token;
132 /* empty string has no tokens */
133 if (*string)
134 tokens++;
135 /* count tokens */
136 for (i = 0; string[i]; i++)
137 if (!strncmpW(string+i, token_string, strlenW(token_string)))
139 DWORD j;
140 tokens++;
141 /* we want to skip over separators, but not the null terminator */
142 for (j = 0; j < strlenW(token_string) - 1; j++)
143 if (!string[i+j])
144 break;
145 i += j;
148 /* add 1 for terminating NULL */
149 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
150 token_array[tokens] = NULL;
151 if (!tokens)
152 return token_array;
153 for (i = 0; i < tokens; i++)
155 int len;
156 next_token = strstrW(string, token_string);
157 if (!next_token) next_token = string+strlenW(string);
158 len = next_token - string;
159 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
160 memcpy(token_array[i], string, len*sizeof(WCHAR));
161 token_array[i][len] = '\0';
162 string = next_token+strlenW(token_string);
164 return token_array;
167 /***********************************************************************
168 * HTTP_FreeTokens (internal)
170 * Frees memory returned from HTTP_Tokenize.
172 static void HTTP_FreeTokens(LPWSTR * token_array)
174 int i;
175 for (i = 0; token_array[i]; i++)
176 HeapFree(GetProcessHeap(), 0, token_array[i]);
177 HeapFree(GetProcessHeap(), 0, token_array);
180 /* **********************************************************************
182 * Helper functions for the HttpSendRequest(Ex) functions
185 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
187 /* if the verb is NULL default to GET */
188 if (NULL == lpwhr->lpszVerb)
190 static const WCHAR szGET[] = { 'G','E','T', 0 };
191 lpwhr->lpszVerb = WININET_strdupW(szGET);
195 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
197 static const WCHAR szSlash[] = { '/',0 };
198 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
200 /* If we don't have a path we set it to root */
201 if (NULL == lpwhr->lpszPath)
202 lpwhr->lpszPath = WININET_strdupW(szSlash);
203 else /* remove \r and \n*/
205 int nLen = strlenW(lpwhr->lpszPath);
206 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
208 nLen--;
209 lpwhr->lpszPath[nLen]='\0';
211 /* Replace '\' with '/' */
212 while (nLen>0) {
213 nLen--;
214 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
218 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
219 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
220 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
222 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
223 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
224 *fixurl = '/';
225 strcpyW(fixurl + 1, lpwhr->lpszPath);
226 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
227 lpwhr->lpszPath = fixurl;
231 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
233 LPWSTR requestString;
234 DWORD len, n;
235 LPCWSTR *req;
236 INT i;
237 LPWSTR p;
239 static const WCHAR szSpace[] = { ' ',0 };
240 static const WCHAR szcrlf[] = {'\r','\n', 0};
241 static const WCHAR szColon[] = { ':',' ',0 };
242 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
244 /* allocate space for an array of all the string pointers to be added */
245 len = (lpwhr->nCustHeaders)*4 + 9;
246 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
248 /* add the verb, path and HTTP version string */
249 n = 0;
250 req[n++] = verb;
251 req[n++] = szSpace;
252 req[n++] = path;
253 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
255 /* Append custom request heades */
256 for (i = 0; i < lpwhr->nCustHeaders; i++)
258 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
260 req[n++] = szcrlf;
261 req[n++] = lpwhr->pCustHeaders[i].lpszField;
262 req[n++] = szColon;
263 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
265 TRACE("Adding custom header %s (%s)\n",
266 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
267 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
271 if( n >= len )
272 ERR("oops. buffer overrun\n");
274 req[n] = NULL;
275 requestString = HTTP_build_req( req, 4 );
276 HeapFree( GetProcessHeap(), 0, req );
279 * Set (header) termination string for request
280 * Make sure there's exactly two new lines at the end of the request
282 p = &requestString[strlenW(requestString)-1];
283 while ( (*p == '\n') || (*p == '\r') )
284 p--;
285 strcpyW( p+1, sztwocrlf );
287 return requestString;
290 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
292 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
293 int HeaderIndex;
294 LPHTTPHEADERW setCookieHeader;
296 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
297 if (HeaderIndex == -1)
298 return;
299 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
301 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
303 int nPosStart = 0, nPosEnd = 0, len;
304 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
306 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
308 LPWSTR buf_cookie, cookie_name, cookie_data;
309 LPWSTR buf_url;
310 LPWSTR domain = NULL;
311 LPHTTPHEADERW Host;
313 int nEqualPos = 0;
314 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
315 setCookieHeader->lpszValue[nPosEnd] != '\0')
317 nPosEnd++;
319 if (setCookieHeader->lpszValue[nPosEnd] == ';')
321 /* fixme: not case sensitive, strcasestr is gnu only */
322 int nDomainPosEnd = 0;
323 int nDomainPosStart = 0, nDomainLength = 0;
324 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
325 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
326 if (lpszDomain)
327 { /* they have specified their own domain, lets use it */
328 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
329 lpszDomain[nDomainPosEnd] != '\0')
331 nDomainPosEnd++;
333 nDomainPosStart = strlenW(szDomain);
334 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
335 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
336 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
339 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
340 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
341 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
342 TRACE("%s\n", debugstr_w(buf_cookie));
343 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
345 nEqualPos++;
347 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
349 HeapFree(GetProcessHeap(), 0, buf_cookie);
350 break;
353 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
354 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
355 cookie_data = &buf_cookie[nEqualPos + 1];
357 Host = HTTP_GetHeader(lpwhr,szHost);
358 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
359 strlenW(lpwhr->lpszPath) + 9;
360 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
361 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
362 InternetSetCookieW(buf_url, cookie_name, cookie_data);
364 HeapFree(GetProcessHeap(), 0, buf_url);
365 HeapFree(GetProcessHeap(), 0, buf_cookie);
366 HeapFree(GetProcessHeap(), 0, cookie_name);
367 HeapFree(GetProcessHeap(), 0, domain);
368 nPosStart = nPosEnd;
373 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
375 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
376 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW)lpwhs->hdr.lpwhparent;
378 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
379 assert(hIC->hdr.htype == WH_HINIT);
381 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
382 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
383 hIC->lpszProxyPassword);
386 /***********************************************************************
387 * HTTP_HttpAddRequestHeadersW (internal)
389 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
390 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
392 LPWSTR lpszStart;
393 LPWSTR lpszEnd;
394 LPWSTR buffer;
395 BOOL bSuccess = FALSE;
396 DWORD len;
398 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
400 if( dwHeaderLength == ~0U )
401 len = strlenW(lpszHeader);
402 else
403 len = dwHeaderLength;
404 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
405 lstrcpynW( buffer, lpszHeader, len + 1);
407 lpszStart = buffer;
411 LPWSTR * pFieldAndValue;
413 lpszEnd = lpszStart;
415 while (*lpszEnd != '\0')
417 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
418 break;
419 lpszEnd++;
422 if (*lpszStart == '\0')
423 break;
425 if (*lpszEnd == '\r')
427 *lpszEnd = '\0';
428 lpszEnd += 2; /* Jump over \r\n */
430 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
431 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
432 if (pFieldAndValue)
434 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
435 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
436 HTTP_FreeTokens(pFieldAndValue);
439 lpszStart = lpszEnd;
440 } while (bSuccess);
442 HeapFree(GetProcessHeap(), 0, buffer);
444 return bSuccess;
447 /***********************************************************************
448 * HttpAddRequestHeadersW (WININET.@)
450 * Adds one or more HTTP header to the request handler
452 * RETURNS
453 * TRUE on success
454 * FALSE on failure
457 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
458 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
460 BOOL bSuccess = FALSE;
461 LPWININETHTTPREQW lpwhr;
463 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
464 dwModifier);
466 if (!lpszHeader)
467 return TRUE;
469 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
470 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
472 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
473 goto lend;
475 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
476 lend:
477 if( lpwhr )
478 WININET_Release( &lpwhr->hdr );
480 return bSuccess;
483 /***********************************************************************
484 * HttpAddRequestHeadersA (WININET.@)
486 * Adds one or more HTTP header to the request handler
488 * RETURNS
489 * TRUE on success
490 * FALSE on failure
493 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
494 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
496 DWORD len;
497 LPWSTR hdr;
498 BOOL r;
500 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
501 dwModifier);
503 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
504 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
505 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
506 if( dwHeaderLength != ~0U )
507 dwHeaderLength = len;
509 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
511 HeapFree( GetProcessHeap(), 0, hdr );
513 return r;
516 /***********************************************************************
517 * HttpEndRequestA (WININET.@)
519 * Ends an HTTP request that was started by HttpSendRequestEx
521 * RETURNS
522 * TRUE if successful
523 * FALSE on failure
526 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
527 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
529 LPINTERNET_BUFFERSA ptr;
530 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
531 BOOL rc = FALSE;
533 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
534 dwContext);
536 ptr = lpBuffersOut;
537 if (ptr)
538 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
539 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
540 else
541 lpBuffersOutW = NULL;
543 ptrW = lpBuffersOutW;
544 while (ptr)
546 if (ptr->lpvBuffer && ptr->dwBufferLength)
547 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
548 ptrW->dwBufferLength = ptr->dwBufferLength;
549 ptrW->dwBufferTotal= ptr->dwBufferTotal;
551 if (ptr->Next)
552 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
553 sizeof(INTERNET_BUFFERSW));
555 ptr = ptr->Next;
556 ptrW = ptrW->Next;
559 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
561 if (lpBuffersOutW)
563 ptrW = lpBuffersOutW;
564 while (ptrW)
566 LPINTERNET_BUFFERSW ptrW2;
568 FIXME("Do we need to translate info out of these buffer?\n");
570 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
571 ptrW2 = ptrW->Next;
572 HeapFree(GetProcessHeap(),0,ptrW);
573 ptrW = ptrW2;
577 return rc;
580 /***********************************************************************
581 * HttpEndRequestW (WININET.@)
583 * Ends an HTTP request that was started by HttpSendRequestEx
585 * RETURNS
586 * TRUE if successful
587 * FALSE on failure
590 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
591 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
593 BOOL rc = FALSE;
594 LPWININETHTTPREQW lpwhr;
595 INT responseLen;
597 TRACE("-->\n");
598 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
600 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
602 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
603 return FALSE;
606 lpwhr->hdr.dwFlags |= dwFlags;
607 lpwhr->hdr.dwContext = dwContext;
609 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
610 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
612 responseLen = HTTP_GetResponseHeaders(lpwhr);
613 if (responseLen)
614 rc = TRUE;
616 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
617 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
619 /* process headers here. Is this right? */
620 HTTP_ProcessHeaders(lpwhr);
622 /* We appear to do nothing with the buffer.. is that correct? */
624 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
626 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
627 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
628 (dwCode==302 || dwCode==301))
630 WCHAR szNewLocation[2048];
631 DWORD dwBufferSize=2048;
632 dwIndex=0;
633 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
635 static const WCHAR szGET[] = { 'G','E','T', 0 };
636 /* redirects are always GETs */
637 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
638 lpwhr->lpszVerb = WININET_strdupW(szGET);
639 return HTTP_HandleRedirect(lpwhr, szNewLocation, NULL, 0, NULL, 0, 0);
644 TRACE("%i <--\n",rc);
645 return rc;
648 /***********************************************************************
649 * HttpOpenRequestW (WININET.@)
651 * Open a HTTP request handle
653 * RETURNS
654 * HINTERNET a HTTP request handle on success
655 * NULL on failure
658 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
659 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
660 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
661 DWORD dwFlags, DWORD dwContext)
663 LPWININETHTTPSESSIONW lpwhs;
664 HINTERNET handle = NULL;
666 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
667 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
668 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
669 dwFlags, dwContext);
670 if(lpszAcceptTypes!=NULL)
672 int i;
673 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
674 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
677 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
678 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
680 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
681 goto lend;
685 * My tests seem to show that the windows version does not
686 * become asynchronous until after this point. And anyhow
687 * if this call was asynchronous then how would you get the
688 * necessary HINTERNET pointer returned by this function.
691 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
692 lpszVersion, lpszReferrer, lpszAcceptTypes,
693 dwFlags, dwContext);
694 lend:
695 if( lpwhs )
696 WININET_Release( &lpwhs->hdr );
697 TRACE("returning %p\n", handle);
698 return handle;
702 /***********************************************************************
703 * HttpOpenRequestA (WININET.@)
705 * Open a HTTP request handle
707 * RETURNS
708 * HINTERNET a HTTP request handle on success
709 * NULL on failure
712 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
713 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
714 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
715 DWORD dwFlags, DWORD dwContext)
717 LPWSTR szVerb = NULL, szObjectName = NULL;
718 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
719 INT len;
720 INT acceptTypesCount;
721 HINTERNET rc = FALSE;
722 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
723 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
724 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
725 dwFlags, dwContext);
727 if (lpszVerb)
729 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
730 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
731 if ( !szVerb )
732 goto end;
733 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
736 if (lpszObjectName)
738 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
739 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
740 if ( !szObjectName )
741 goto end;
742 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
745 if (lpszVersion)
747 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
748 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
749 if ( !szVersion )
750 goto end;
751 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
754 if (lpszReferrer)
756 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
757 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
758 if ( !szReferrer )
759 goto end;
760 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
763 acceptTypesCount = 0;
764 if (lpszAcceptTypes)
766 /* find out how many there are */
767 while (lpszAcceptTypes[acceptTypesCount])
768 acceptTypesCount++;
769 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
770 acceptTypesCount = 0;
771 while (lpszAcceptTypes[acceptTypesCount])
773 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
774 -1, NULL, 0 );
775 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
776 if (!szAcceptTypes[acceptTypesCount] )
777 goto end;
778 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
779 -1, szAcceptTypes[acceptTypesCount], len );
780 acceptTypesCount++;
782 szAcceptTypes[acceptTypesCount] = NULL;
784 else szAcceptTypes = 0;
786 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
787 szVersion, szReferrer,
788 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
790 end:
791 if (szAcceptTypes)
793 acceptTypesCount = 0;
794 while (szAcceptTypes[acceptTypesCount])
796 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
797 acceptTypesCount++;
799 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
801 HeapFree(GetProcessHeap(), 0, szReferrer);
802 HeapFree(GetProcessHeap(), 0, szVersion);
803 HeapFree(GetProcessHeap(), 0, szObjectName);
804 HeapFree(GetProcessHeap(), 0, szVerb);
806 return rc;
809 /***********************************************************************
810 * HTTP_Base64
812 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
814 UINT n = 0, x;
815 static LPCSTR HTTP_Base64Enc =
816 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
818 while( bin[0] )
820 /* first 6 bits, all from bin[0] */
821 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
822 x = (bin[0] & 3) << 4;
824 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
825 if( !bin[1] )
827 base64[n++] = HTTP_Base64Enc[x];
828 base64[n++] = '=';
829 base64[n++] = '=';
830 break;
832 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
833 x = ( bin[1] & 0x0f ) << 2;
835 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
836 if( !bin[2] )
838 base64[n++] = HTTP_Base64Enc[x];
839 base64[n++] = '=';
840 break;
842 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
844 /* last 6 bits, all from bin [2] */
845 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
846 bin += 3;
848 base64[n] = 0;
849 return n;
852 /***********************************************************************
853 * HTTP_EncodeBasicAuth
855 * Encode the basic authentication string for HTTP 1.1
857 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
859 UINT len;
860 LPWSTR in, out;
861 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
862 static const WCHAR szColon[] = {':',0};
864 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
865 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
866 if( !in )
867 return NULL;
869 len = lstrlenW(szBasic) +
870 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
871 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
872 if( out )
874 lstrcpyW( in, username );
875 lstrcatW( in, szColon );
876 lstrcatW( in, password );
877 lstrcpyW( out, szBasic );
878 HTTP_Base64( in, &out[strlenW(out)] );
880 HeapFree( GetProcessHeap(), 0, in );
882 return out;
885 /***********************************************************************
886 * HTTP_InsertProxyAuthorization
888 * Insert the basic authorization field in the request header
890 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
891 LPCWSTR username, LPCWSTR password )
893 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
894 BOOL ret = TRUE;
896 if (!authorization)
897 return FALSE;
899 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
901 HTTP_ProcessHeader(lpwhr, szProxy_Authorization, authorization,
902 HTTP_ADDHDR_FLAG_REPLACE);
904 HeapFree( GetProcessHeap(), 0, authorization );
906 return ret;
909 /***********************************************************************
910 * HTTP_DealWithProxy
912 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
913 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
915 WCHAR buf[MAXHOSTNAME];
916 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
917 WCHAR* url;
918 static const WCHAR szNul[] = { 0 };
919 URL_COMPONENTSW UrlComponents;
920 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
921 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
922 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
923 int len;
925 memset( &UrlComponents, 0, sizeof UrlComponents );
926 UrlComponents.dwStructSize = sizeof UrlComponents;
927 UrlComponents.lpszHostName = buf;
928 UrlComponents.dwHostNameLength = MAXHOSTNAME;
930 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
931 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
932 sprintfW(proxy, szFormat1, hIC->lpszProxy);
933 else
934 strcpyW(proxy, hIC->lpszProxy);
935 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
936 return FALSE;
937 if( UrlComponents.dwHostNameLength == 0 )
938 return FALSE;
940 if( !lpwhr->lpszPath )
941 lpwhr->lpszPath = (LPWSTR)szNul;
942 TRACE("server='%s' path='%s'\n",
943 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
944 /* for constant 15 see above */
945 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
946 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
948 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
949 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
951 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
953 if( lpwhr->lpszPath[0] != '/' )
954 strcatW( url, szSlash );
955 strcatW(url, lpwhr->lpszPath);
956 if(lpwhr->lpszPath != szNul)
957 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
958 lpwhr->lpszPath = url;
960 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
961 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
962 lpwhs->nServerPort = UrlComponents.nPort;
964 return TRUE;
967 /***********************************************************************
968 * HTTP_HttpOpenRequestW (internal)
970 * Open a HTTP request handle
972 * RETURNS
973 * HINTERNET a HTTP request handle on success
974 * NULL on failure
977 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
978 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
979 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
980 DWORD dwFlags, DWORD dwContext)
982 LPWININETAPPINFOW hIC = NULL;
983 LPWININETHTTPREQW lpwhr;
984 LPWSTR lpszCookies;
985 LPWSTR lpszUrl = NULL;
986 DWORD nCookieSize;
987 HINTERNET handle = NULL;
988 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
989 DWORD len;
990 LPHTTPHEADERW Host;
991 char szaddr[32];
993 TRACE("-->\n");
995 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
996 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
998 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
999 if (NULL == lpwhr)
1001 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1002 goto lend;
1004 lpwhr->hdr.htype = WH_HHTTPREQ;
1005 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
1006 lpwhr->hdr.dwFlags = dwFlags;
1007 lpwhr->hdr.dwContext = dwContext;
1008 lpwhr->hdr.dwRefCount = 1;
1009 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1010 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1012 handle = WININET_AllocHandle( &lpwhr->hdr );
1013 if (NULL == handle)
1015 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1016 goto lend;
1019 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
1021 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1022 HRESULT rc;
1024 len = 0;
1025 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1026 if (rc != E_POINTER)
1027 len = strlenW(lpszObjectName)+1;
1028 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1029 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1030 URL_ESCAPE_SPACES_ONLY);
1031 if (rc)
1033 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
1034 strcpyW(lpwhr->lpszPath,lpszObjectName);
1038 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1039 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1041 if(lpszAcceptTypes!=NULL)
1043 int i;
1044 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1045 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1048 if (NULL == lpszVerb)
1050 static const WCHAR szGet[] = {'G','E','T',0};
1051 lpwhr->lpszVerb = WININET_strdupW(szGet);
1053 else if (strlenW(lpszVerb))
1054 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1056 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1058 WCHAR buf[MAXHOSTNAME];
1059 URL_COMPONENTSW UrlComponents;
1061 memset( &UrlComponents, 0, sizeof UrlComponents );
1062 UrlComponents.dwStructSize = sizeof UrlComponents;
1063 UrlComponents.lpszHostName = buf;
1064 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1066 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1067 if (strlenW(UrlComponents.lpszHostName))
1068 HTTP_ProcessHeader(lpwhr, szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1070 else
1071 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1073 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1074 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1075 INTERNET_DEFAULT_HTTPS_PORT :
1076 INTERNET_DEFAULT_HTTP_PORT);
1077 lpwhs->nHostPort = lpwhs->nServerPort;
1079 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1080 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1082 if (hIC->lpszAgent)
1084 WCHAR *agent_header;
1085 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1087 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1088 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1089 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1091 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1092 HTTP_ADDREQ_FLAG_ADD);
1093 HeapFree(GetProcessHeap(), 0, agent_header);
1096 Host = HTTP_GetHeader(lpwhr,szHost);
1098 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1099 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1100 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1102 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1103 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1105 int cnt = 0;
1106 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1107 static const WCHAR szcrlf[] = {'\r','\n',0};
1109 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1111 cnt += sprintfW(lpszCookies, szCookie);
1112 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1113 strcatW(lpszCookies, szcrlf);
1115 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1116 HTTP_ADDREQ_FLAG_ADD);
1117 HeapFree(GetProcessHeap(), 0, lpszCookies);
1119 HeapFree(GetProcessHeap(), 0, lpszUrl);
1122 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1123 INTERNET_STATUS_HANDLE_CREATED, &handle,
1124 sizeof(handle));
1127 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1131 * According to my tests. The name is not resolved until a request is Opened
1133 INTERNET_SendCallback(&lpwhr->hdr, dwContext,
1134 INTERNET_STATUS_RESOLVING_NAME,
1135 lpwhs->lpszServerName,
1136 strlenW(lpwhs->lpszServerName)+1);
1138 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1139 &lpwhs->socketAddress))
1141 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1142 InternetCloseHandle( handle );
1143 handle = NULL;
1144 goto lend;
1147 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1148 szaddr, sizeof(szaddr));
1149 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1150 INTERNET_STATUS_NAME_RESOLVED,
1151 szaddr, strlen(szaddr)+1);
1153 lend:
1154 if( lpwhr )
1155 WININET_Release( &lpwhr->hdr );
1157 TRACE("<-- %p (%p)\n", handle, lpwhr);
1158 return handle;
1161 typedef struct std_hdr_data
1163 const WCHAR* hdrStr;
1164 INT hdrIndex;
1165 } std_hdr_data;
1167 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1168 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1169 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1170 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1171 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1172 static const WCHAR szAge[] = { 'A','g','e',0 };
1173 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1174 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1175 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1176 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1177 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1178 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1179 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1180 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1181 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1182 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1183 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1184 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1185 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 };
1186 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1187 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1188 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1189 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1190 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1191 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1192 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1193 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1194 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1195 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1196 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1197 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1198 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1199 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1200 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1201 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1202 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1203 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1204 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1205 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1206 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1207 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1208 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1209 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1210 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1211 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1212 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 };
1213 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1214 static const WCHAR szURI[] = { 'U','R','I',0 };
1215 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1216 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1217 static const WCHAR szVia[] = { 'V','i','a',0 };
1218 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1219 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1221 static const std_hdr_data SORTED_STANDARD_HEADERS[] = {
1222 {szAccept, HTTP_QUERY_ACCEPT,},
1223 {szAccept_Charset, HTTP_QUERY_ACCEPT_CHARSET,},
1224 {szAccept_Encoding, HTTP_QUERY_ACCEPT_ENCODING,},
1225 {szAccept_Language, HTTP_QUERY_ACCEPT_LANGUAGE,},
1226 {szAccept_Ranges, HTTP_QUERY_ACCEPT_RANGES,},
1227 {szAge, HTTP_QUERY_AGE,},
1228 {szAllow, HTTP_QUERY_ALLOW,},
1229 {szAuthorization, HTTP_QUERY_AUTHORIZATION,},
1230 {szCache_Control, HTTP_QUERY_CACHE_CONTROL,},
1231 {szConnection, HTTP_QUERY_CONNECTION,},
1232 {szContent_Base, HTTP_QUERY_CONTENT_BASE,},
1233 {szContent_Encoding, HTTP_QUERY_CONTENT_ENCODING,},
1234 {szContent_ID, HTTP_QUERY_CONTENT_ID,},
1235 {szContent_Language, HTTP_QUERY_CONTENT_LANGUAGE,},
1236 {szContent_Length, HTTP_QUERY_CONTENT_LENGTH,},
1237 {szContent_Location, HTTP_QUERY_CONTENT_LOCATION,},
1238 {szContent_MD5, HTTP_QUERY_CONTENT_MD5,},
1239 {szContent_Range, HTTP_QUERY_CONTENT_RANGE,},
1240 {szContent_Transfer_Encoding,HTTP_QUERY_CONTENT_TRANSFER_ENCODING,},
1241 {szContent_Type, HTTP_QUERY_CONTENT_TYPE,},
1242 {szCookie, HTTP_QUERY_COOKIE,},
1243 {szDate, HTTP_QUERY_DATE,},
1244 {szETag, HTTP_QUERY_ETAG,},
1245 {szExpect, HTTP_QUERY_EXPECT,},
1246 {szExpires, HTTP_QUERY_EXPIRES,},
1247 {szFrom, HTTP_QUERY_DERIVED_FROM,},
1248 {szHost, HTTP_QUERY_HOST,},
1249 {szIf_Match, HTTP_QUERY_IF_MATCH,},
1250 {szIf_Modified_Since, HTTP_QUERY_IF_MODIFIED_SINCE,},
1251 {szIf_None_Match, HTTP_QUERY_IF_NONE_MATCH,},
1252 {szIf_Range, HTTP_QUERY_IF_RANGE,},
1253 {szIf_Unmodified_Since, HTTP_QUERY_IF_UNMODIFIED_SINCE,},
1254 {szLast_Modified, HTTP_QUERY_LAST_MODIFIED,},
1255 {szLocation, HTTP_QUERY_LOCATION,},
1256 {szMax_Forwards, HTTP_QUERY_MAX_FORWARDS,},
1257 {szMime_Version, HTTP_QUERY_MIME_VERSION,},
1258 {szPragma, HTTP_QUERY_PRAGMA,},
1259 {szProxy_Authenticate, HTTP_QUERY_PROXY_AUTHENTICATE,},
1260 {szProxy_Authorization, HTTP_QUERY_PROXY_AUTHORIZATION,},
1261 {szProxy_Connection, HTTP_QUERY_PROXY_CONNECTION,},
1262 {szPublic, HTTP_QUERY_PUBLIC,},
1263 {szRange, HTTP_QUERY_RANGE,},
1264 {szReferer, HTTP_QUERY_REFERER,},
1265 {szRetry_After, HTTP_QUERY_RETRY_AFTER,},
1266 {szServer, HTTP_QUERY_SERVER,},
1267 {szSet_Cookie, HTTP_QUERY_SET_COOKIE,},
1268 {szStatus, HTTP_QUERY_STATUS_CODE,},
1269 {szTransfer_Encoding, HTTP_QUERY_TRANSFER_ENCODING,},
1270 {szUnless_Modified_Since, HTTP_QUERY_UNLESS_MODIFIED_SINCE,},
1271 {szUpgrade, HTTP_QUERY_UPGRADE,},
1272 {szURI, HTTP_QUERY_URI,},
1273 {szUser_Agent, HTTP_QUERY_USER_AGENT,},
1274 {szVary, HTTP_QUERY_VARY,},
1275 {szVia, HTTP_QUERY_VIA,},
1276 {szWarning, HTTP_QUERY_WARNING,},
1277 {szWWW_Authenticate, HTTP_QUERY_WWW_AUTHENTICATE,},
1280 /***********************************************************************
1281 * HTTP_HttpQueryInfoW (internal)
1283 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1284 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1286 LPHTTPHEADERW lphttpHdr = NULL;
1287 BOOL bSuccess = FALSE;
1288 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1291 /* Find requested header structure */
1292 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
1294 INT requested_index = (lpdwIndex)?(*lpdwIndex):0;
1295 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer,
1296 requested_index,request_only);
1298 if (index < 0)
1299 return bSuccess;
1300 else
1301 lphttpHdr = &lpwhr->pCustHeaders[index];
1303 if (lpdwIndex)
1304 (*lpdwIndex)++;
1306 else
1308 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
1310 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
1312 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1313 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1315 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1316 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1317 return FALSE;
1319 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1320 *lpdwBufferLength = len * sizeof(WCHAR);
1322 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1324 return TRUE;
1326 else if (index == HTTP_QUERY_RAW_HEADERS)
1328 static const WCHAR szCrLf[] = {'\r','\n',0};
1329 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1330 DWORD i, size = 0;
1331 LPWSTR pszString = (WCHAR*)lpBuffer;
1333 for (i = 0; ppszRawHeaderLines[i]; i++)
1334 size += strlenW(ppszRawHeaderLines[i]) + 1;
1336 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1338 HTTP_FreeTokens(ppszRawHeaderLines);
1339 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1340 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1341 return FALSE;
1344 for (i = 0; ppszRawHeaderLines[i]; i++)
1346 DWORD len = strlenW(ppszRawHeaderLines[i]);
1347 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1348 pszString += len+1;
1350 *pszString = '\0';
1352 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1354 *lpdwBufferLength = size * sizeof(WCHAR);
1355 HTTP_FreeTokens(ppszRawHeaderLines);
1357 return TRUE;
1359 else if (index == HTTP_QUERY_STATUS_TEXT)
1361 DWORD len = strlenW(lpwhr->lpszStatusText);
1362 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1364 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1365 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1366 return FALSE;
1368 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1369 *lpdwBufferLength = len * sizeof(WCHAR);
1371 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1373 return TRUE;
1375 else if (index == HTTP_QUERY_VERSION)
1377 DWORD len = strlenW(lpwhr->lpszVersion);
1378 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1380 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1381 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1382 return FALSE;
1384 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1385 *lpdwBufferLength = len * sizeof(WCHAR);
1387 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1389 return TRUE;
1391 else if (index >= 0 && index <= HTTP_QUERY_MAX )
1393 int i;
1394 for (i = 0; i < sizeof(SORTED_STANDARD_HEADERS)/sizeof(std_hdr_data) ; i++)
1396 if (SORTED_STANDARD_HEADERS[i].hdrIndex == index)
1398 INT requested_index = (lpdwIndex)?(*lpdwIndex):0;
1399 INT index = HTTP_GetCustomHeaderIndex(lpwhr,
1400 (LPWSTR)SORTED_STANDARD_HEADERS[i].hdrStr,
1401 requested_index,request_only);
1403 if (index < 0)
1404 return bSuccess;
1405 else
1406 lphttpHdr = &lpwhr->pCustHeaders[index];
1408 if (lpdwIndex)
1409 (*lpdwIndex)++;
1411 break;
1415 if (!lphttpHdr)
1417 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1418 return bSuccess;
1421 else
1423 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1424 return bSuccess;
1428 /* Ensure header satisifies requested attributes */
1429 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1430 (~lphttpHdr->wFlags & HDR_ISREQUEST))
1432 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1433 return bSuccess;
1436 /* coalesce value to reuqested type */
1437 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1439 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1440 bSuccess = TRUE;
1442 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1444 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1446 time_t tmpTime;
1447 struct tm tmpTM;
1448 SYSTEMTIME *STHook;
1450 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1452 tmpTM = *gmtime(&tmpTime);
1453 STHook = (SYSTEMTIME *) lpBuffer;
1454 if(STHook==NULL)
1455 return bSuccess;
1457 STHook->wDay = tmpTM.tm_mday;
1458 STHook->wHour = tmpTM.tm_hour;
1459 STHook->wMilliseconds = 0;
1460 STHook->wMinute = tmpTM.tm_min;
1461 STHook->wDayOfWeek = tmpTM.tm_wday;
1462 STHook->wMonth = tmpTM.tm_mon + 1;
1463 STHook->wSecond = tmpTM.tm_sec;
1464 STHook->wYear = tmpTM.tm_year;
1466 bSuccess = TRUE;
1468 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1469 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1470 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1472 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1474 if (*lpdwIndex >= lphttpHdr->wCount)
1476 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1478 else
1480 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1481 (*lpdwIndex)++;
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%08lx)--> %ld\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 (%08lx)", 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 (%08lx)", 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 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1705 LPINTERNET_BUFFERSA lpBuffersIn,
1706 LPINTERNET_BUFFERSA lpBuffersOut,
1707 DWORD dwFlags, DWORD dwContext)
1709 INTERNET_BUFFERSW BuffersInW;
1710 BOOL rc = FALSE;
1711 DWORD headerlen;
1713 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1714 lpBuffersOut, dwFlags, dwContext);
1716 if (lpBuffersIn)
1718 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1719 if (lpBuffersIn->lpcszHeader)
1721 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
1722 lpBuffersIn->dwHeadersLength,0,0);
1723 BuffersInW.lpcszHeader = HeapAlloc(GetProcessHeap(),0,headerlen*
1724 sizeof(WCHAR));
1725 if (!BuffersInW.lpcszHeader)
1727 SetLastError(ERROR_OUTOFMEMORY);
1728 return FALSE;
1730 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1731 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1732 (LPWSTR)BuffersInW.lpcszHeader, headerlen);
1734 else
1735 BuffersInW.lpcszHeader = NULL;
1736 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
1737 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
1738 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
1739 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
1740 BuffersInW.Next = NULL;
1743 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
1745 if (lpBuffersIn)
1746 HeapFree(GetProcessHeap(),0,(LPVOID)BuffersInW.lpcszHeader);
1748 return rc;
1751 /***********************************************************************
1752 * HttpSendRequestExW (WININET.@)
1754 * Sends the specified request to the HTTP server and allows chunked
1755 * transfers
1757 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1758 LPINTERNET_BUFFERSW lpBuffersIn,
1759 LPINTERNET_BUFFERSW lpBuffersOut,
1760 DWORD dwFlags, DWORD dwContext)
1762 BOOL ret;
1763 LPWININETHTTPREQW lpwhr;
1764 LPWININETHTTPSESSIONW lpwhs;
1765 LPWININETAPPINFOW hIC;
1767 TRACE("(%p, %p, %p, %08lx, %08lx)\n", hRequest, lpBuffersIn,
1768 lpBuffersOut, dwFlags, dwContext);
1770 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1772 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1774 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1775 return FALSE;
1778 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1779 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
1780 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1781 assert(hIC->hdr.htype == WH_HINIT);
1783 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1785 WORKREQUEST workRequest;
1786 struct WORKREQ_HTTPSENDREQUESTW *req;
1788 workRequest.asyncall = HTTPSENDREQUESTW;
1789 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1790 req = &workRequest.u.HttpSendRequestW;
1791 if (lpBuffersIn->lpcszHeader)
1792 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1793 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
1794 else
1795 req->lpszHeader = NULL;
1796 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
1797 req->lpOptional = lpBuffersIn->lpvBuffer;
1798 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
1799 req->dwContentLength = lpBuffersIn->dwBufferTotal;
1800 req->bEndRequest = FALSE;
1802 INTERNET_AsyncCall(&workRequest);
1804 * This is from windows.
1806 SetLastError(ERROR_IO_PENDING);
1807 ret = FALSE;
1809 else
1811 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1812 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
1813 lpBuffersIn->dwBufferTotal, FALSE);
1816 WININET_Release(&lpwhr->hdr);
1817 TRACE("<---\n");
1818 return ret;
1821 /***********************************************************************
1822 * HttpSendRequestW (WININET.@)
1824 * Sends the specified request to the HTTP server
1826 * RETURNS
1827 * TRUE on success
1828 * FALSE on failure
1831 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1832 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1834 LPWININETHTTPREQW lpwhr;
1835 LPWININETHTTPSESSIONW lpwhs = NULL;
1836 LPWININETAPPINFOW hIC = NULL;
1837 BOOL r;
1839 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1840 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1842 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1843 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1845 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1846 r = FALSE;
1847 goto lend;
1850 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1851 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1853 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1854 r = FALSE;
1855 goto lend;
1858 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1859 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1861 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1862 r = FALSE;
1863 goto lend;
1866 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1868 WORKREQUEST workRequest;
1869 struct WORKREQ_HTTPSENDREQUESTW *req;
1871 workRequest.asyncall = HTTPSENDREQUESTW;
1872 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1873 req = &workRequest.u.HttpSendRequestW;
1874 if (lpszHeaders)
1875 req->lpszHeader = WININET_strdupW(lpszHeaders);
1876 else
1877 req->lpszHeader = 0;
1878 req->dwHeaderLength = dwHeaderLength;
1879 req->lpOptional = lpOptional;
1880 req->dwOptionalLength = dwOptionalLength;
1881 req->dwContentLength = dwOptionalLength;
1882 req->bEndRequest = TRUE;
1884 INTERNET_AsyncCall(&workRequest);
1886 * This is from windows.
1888 SetLastError(ERROR_IO_PENDING);
1889 r = FALSE;
1891 else
1893 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1894 dwHeaderLength, lpOptional, dwOptionalLength,
1895 dwOptionalLength, TRUE);
1897 lend:
1898 if( lpwhr )
1899 WININET_Release( &lpwhr->hdr );
1900 return r;
1903 /***********************************************************************
1904 * HttpSendRequestA (WININET.@)
1906 * Sends the specified request to the HTTP server
1908 * RETURNS
1909 * TRUE on success
1910 * FALSE on failure
1913 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1914 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1916 BOOL result;
1917 LPWSTR szHeaders=NULL;
1918 DWORD nLen=dwHeaderLength;
1919 if(lpszHeaders!=NULL)
1921 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1922 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1923 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1925 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1926 HeapFree(GetProcessHeap(),0,szHeaders);
1927 return result;
1930 /***********************************************************************
1931 * HTTP_HandleRedirect (internal)
1933 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1934 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
1935 DWORD dwContentLength)
1937 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1938 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1939 WCHAR path[2048];
1940 char szaddr[32];
1942 if(lpszUrl[0]=='/')
1944 /* if it's an absolute path, keep the same session info */
1945 lstrcpynW(path, lpszUrl, 2048);
1947 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1949 TRACE("Redirect through proxy\n");
1950 lstrcpynW(path, lpszUrl, 2048);
1952 else
1954 URL_COMPONENTSW urlComponents;
1955 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1956 static const WCHAR szHttp[] = {'h','t','t','p',0};
1957 static const WCHAR szHttps[] = {'h','t','t','p','s',0};
1958 userName[0] = 0;
1959 hostName[0] = 0;
1960 protocol[0] = 0;
1962 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1963 urlComponents.lpszScheme = protocol;
1964 urlComponents.dwSchemeLength = 32;
1965 urlComponents.lpszHostName = hostName;
1966 urlComponents.dwHostNameLength = MAXHOSTNAME;
1967 urlComponents.lpszUserName = userName;
1968 urlComponents.dwUserNameLength = 1024;
1969 urlComponents.lpszPassword = NULL;
1970 urlComponents.dwPasswordLength = 0;
1971 urlComponents.lpszUrlPath = path;
1972 urlComponents.dwUrlPathLength = 2048;
1973 urlComponents.lpszExtraInfo = NULL;
1974 urlComponents.dwExtraInfoLength = 0;
1975 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1976 return FALSE;
1978 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
1979 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
1981 TRACE("redirect from secure page to non-secure page\n");
1982 /* FIXME: warn about from secure redirect to non-secure page */
1983 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
1985 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
1986 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
1988 TRACE("redirect from non-secure page to secure page\n");
1989 /* FIXME: notify about redirect to secure page */
1990 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
1993 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1995 if (lstrlenW(protocol)>4) /*https*/
1996 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1997 else /*http*/
1998 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2001 #if 0
2003 * This upsets redirects to binary files on sourceforge.net
2004 * and gives an html page instead of the target file
2005 * Examination of the HTTP request sent by native wininet.dll
2006 * reveals that it doesn't send a referrer in that case.
2007 * Maybe there's a flag that enables this, or maybe a referrer
2008 * shouldn't be added in case of a redirect.
2011 /* consider the current host as the referrer */
2012 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2013 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2014 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2015 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2016 #endif
2018 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2019 lpwhs->lpszServerName = WININET_strdupW(hostName);
2020 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2021 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2022 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2024 int len;
2025 static WCHAR fmt[] = {'%','s',':','%','i',0};
2026 len = lstrlenW(hostName);
2027 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2028 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2029 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2031 else
2032 lpwhs->lpszHostName = WININET_strdupW(hostName);
2034 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2037 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2038 lpwhs->lpszUserName = WININET_strdupW(userName);
2039 lpwhs->nServerPort = urlComponents.nPort;
2041 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2042 INTERNET_STATUS_RESOLVING_NAME,
2043 lpwhs->lpszServerName,
2044 strlenW(lpwhs->lpszServerName)+1);
2046 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
2047 &lpwhs->socketAddress))
2049 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2050 return FALSE;
2053 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2054 szaddr, sizeof(szaddr));
2055 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2056 INTERNET_STATUS_NAME_RESOLVED,
2057 szaddr, strlen(szaddr)+1);
2059 NETCON_close(&lpwhr->netConnection);
2060 NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE);
2063 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2064 lpwhr->lpszPath=NULL;
2065 if (strlenW(path))
2067 DWORD needed = 0;
2068 HRESULT rc;
2070 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2071 if (rc != E_POINTER)
2072 needed = strlenW(path)+1;
2073 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2074 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2075 URL_ESCAPE_SPACES_ONLY);
2076 if (rc)
2078 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
2079 strcpyW(lpwhr->lpszPath,path);
2083 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional,
2084 dwOptionalLength, dwContentLength, TRUE);
2087 /***********************************************************************
2088 * HTTP_build_req (internal)
2090 * concatenate all the strings in the request together
2092 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2094 LPCWSTR *t;
2095 LPWSTR str;
2097 for( t = list; *t ; t++ )
2098 len += strlenW( *t );
2099 len++;
2101 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2102 *str = 0;
2104 for( t = list; *t ; t++ )
2105 strcatW( str, *t );
2107 return str;
2110 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2112 LPWSTR lpszPath;
2113 LPWSTR requestString;
2114 INT len;
2115 INT cnt;
2116 INT responseLen;
2117 char *ascii_req;
2118 BOOL ret;
2119 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2120 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2121 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2123 TRACE("\n");
2125 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2126 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2127 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2128 HeapFree( GetProcessHeap(), 0, lpszPath );
2130 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2131 NULL, 0, NULL, NULL );
2132 len--; /* the nul terminator isn't needed */
2133 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2134 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2135 ascii_req, len, NULL, NULL );
2136 HeapFree( GetProcessHeap(), 0, requestString );
2138 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2140 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2141 HeapFree( GetProcessHeap(), 0, ascii_req );
2142 if (!ret || cnt < 0)
2143 return FALSE;
2145 responseLen = HTTP_GetResponseHeaders( lpwhr );
2146 if (!responseLen)
2147 return FALSE;
2149 return TRUE;
2152 /***********************************************************************
2153 * HTTP_HttpSendRequestW (internal)
2155 * Sends the specified request to the HTTP server
2157 * RETURNS
2158 * TRUE on success
2159 * FALSE on failure
2162 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2163 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2164 DWORD dwContentLength, BOOL bEndRequest)
2166 INT cnt;
2167 BOOL bSuccess = FALSE;
2168 LPWSTR requestString = NULL;
2169 INT responseLen;
2170 BOOL loop_next = FALSE;
2171 INTERNET_ASYNC_RESULT iar;
2172 LPHTTPHEADERW Host;
2174 TRACE("--> %p\n", lpwhr);
2176 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2178 /* Clear any error information */
2179 INTERNET_SetLastError(0);
2181 HTTP_FixVerb(lpwhr);
2183 /* if we are using optional stuff, we must add the fixed header of that option length */
2184 if (dwContentLength > 0)
2186 static const WCHAR szContentLength[] = {
2187 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2188 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2189 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2190 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
2193 Host = HTTP_GetHeader(lpwhr,szHost);
2196 DWORD len;
2197 char *ascii_req;
2199 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2200 loop_next = FALSE;
2202 HTTP_FixURL(lpwhr);
2204 /* add the headers the caller supplied */
2205 if( lpszHeaders && dwHeaderLength )
2207 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2208 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2211 /* if there's a proxy username and password, add it to the headers */
2212 HTTP_AddProxyInfo(lpwhr);
2214 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2216 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2218 /* Send the request and store the results */
2219 if (!HTTP_OpenConnection(lpwhr))
2220 goto lend;
2222 /* send the request as ASCII, tack on the optional data */
2223 if( !lpOptional )
2224 dwOptionalLength = 0;
2225 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2226 NULL, 0, NULL, NULL );
2227 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2228 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2229 ascii_req, len, NULL, NULL );
2230 if( lpOptional )
2231 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2232 len = (len + dwOptionalLength - 1);
2233 ascii_req[len] = 0;
2234 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2236 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2237 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2239 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2240 HeapFree( GetProcessHeap(), 0, ascii_req );
2242 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2243 INTERNET_STATUS_REQUEST_SENT,
2244 &len, sizeof(DWORD));
2246 if (bEndRequest)
2248 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2249 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2251 if (cnt < 0)
2252 goto lend;
2254 responseLen = HTTP_GetResponseHeaders(lpwhr);
2255 if (responseLen)
2256 bSuccess = TRUE;
2258 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2259 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2260 sizeof(DWORD));
2262 HTTP_ProcessHeaders(lpwhr);
2264 else
2265 bSuccess = TRUE;
2267 while (loop_next);
2269 lend:
2271 HeapFree(GetProcessHeap(), 0, requestString);
2273 /* TODO: send notification for P3P header */
2275 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess && bEndRequest)
2277 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2278 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2279 (dwCode==302 || dwCode==301))
2281 WCHAR szNewLocation[2048];
2282 DWORD dwBufferSize=2048;
2283 dwIndex=0;
2284 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2286 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2287 INTERNET_STATUS_REDIRECT, szNewLocation,
2288 dwBufferSize);
2289 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
2290 dwHeaderLength, lpOptional, dwOptionalLength,
2291 dwContentLength);
2297 iar.dwResult = (DWORD)bSuccess;
2298 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2300 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2301 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2302 sizeof(INTERNET_ASYNC_RESULT));
2304 TRACE("<--\n");
2305 return bSuccess;
2309 /***********************************************************************
2310 * HTTP_Connect (internal)
2312 * Create http session handle
2314 * RETURNS
2315 * HINTERNET a session handle on success
2316 * NULL on failure
2319 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2320 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2321 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2322 DWORD dwInternalFlags)
2324 BOOL bSuccess = FALSE;
2325 LPWININETHTTPSESSIONW lpwhs = NULL;
2326 HINTERNET handle = NULL;
2328 TRACE("-->\n");
2330 assert( hIC->hdr.htype == WH_HINIT );
2332 hIC->hdr.dwContext = dwContext;
2334 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2335 if (NULL == lpwhs)
2337 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2338 goto lerror;
2342 * According to my tests. The name is not resolved until a request is sent
2345 lpwhs->hdr.htype = WH_HHTTPSESSION;
2346 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
2347 lpwhs->hdr.dwFlags = dwFlags;
2348 lpwhs->hdr.dwContext = dwContext;
2349 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
2350 lpwhs->hdr.dwRefCount = 1;
2351 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2352 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2354 handle = WININET_AllocHandle( &lpwhs->hdr );
2355 if (NULL == handle)
2357 ERR("Failed to alloc handle\n");
2358 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2359 goto lerror;
2362 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2363 if(strchrW(hIC->lpszProxy, ' '))
2364 FIXME("Several proxies not implemented.\n");
2365 if(hIC->lpszProxyBypass)
2366 FIXME("Proxy bypass is ignored.\n");
2368 if (NULL != lpszServerName)
2370 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2371 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2373 if (NULL != lpszUserName)
2374 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2375 lpwhs->nServerPort = nServerPort;
2376 lpwhs->nHostPort = nServerPort;
2378 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2379 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2381 INTERNET_SendCallback(&hIC->hdr, dwContext,
2382 INTERNET_STATUS_HANDLE_CREATED, &handle,
2383 sizeof(handle));
2386 bSuccess = TRUE;
2388 lerror:
2389 if( lpwhs )
2390 WININET_Release( &lpwhs->hdr );
2393 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2394 * windows
2397 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2398 return handle;
2402 /***********************************************************************
2403 * HTTP_OpenConnection (internal)
2405 * Connect to a web server
2407 * RETURNS
2409 * TRUE on success
2410 * FALSE on failure
2412 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2414 BOOL bSuccess = FALSE;
2415 LPWININETHTTPSESSIONW lpwhs;
2416 LPWININETAPPINFOW hIC = NULL;
2417 char szaddr[32];
2419 TRACE("-->\n");
2422 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2424 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2425 goto lend;
2428 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2430 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2431 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2432 szaddr, sizeof(szaddr));
2433 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2434 INTERNET_STATUS_CONNECTING_TO_SERVER,
2435 szaddr,
2436 strlen(szaddr)+1);
2438 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2439 SOCK_STREAM, 0))
2441 WARN("Socket creation failed\n");
2442 goto lend;
2445 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2446 sizeof(lpwhs->socketAddress)))
2447 goto lend;
2449 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2451 /* Note: we differ from Microsoft's WinINet here. they seem to have
2452 * a bug that causes no status callbacks to be sent when starting
2453 * a tunnel to a proxy server using the CONNECT verb. i believe our
2454 * behaviour to be more correct and to not cause any incompatibilities
2455 * because using a secure connection through a proxy server is a rare
2456 * case that would be hard for anyone to depend on */
2457 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2458 goto lend;
2460 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2462 WARN("Couldn't connect securely to host\n");
2463 goto lend;
2467 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2468 INTERNET_STATUS_CONNECTED_TO_SERVER,
2469 szaddr, strlen(szaddr)+1);
2471 bSuccess = TRUE;
2473 lend:
2474 TRACE("%d <--\n", bSuccess);
2475 return bSuccess;
2479 /***********************************************************************
2480 * HTTP_clear_response_headers (internal)
2482 * clear out any old response headers
2484 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2486 DWORD i;
2488 for( i=0; i<lpwhr->nCustHeaders; i++)
2490 if( !lpwhr->pCustHeaders[i].lpszField )
2491 continue;
2492 if( !lpwhr->pCustHeaders[i].lpszValue )
2493 continue;
2494 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2495 continue;
2496 HTTP_DeleteCustomHeader( lpwhr, i );
2497 i--;
2501 /***********************************************************************
2502 * HTTP_GetResponseHeaders (internal)
2504 * Read server response
2506 * RETURNS
2508 * TRUE on success
2509 * FALSE on error
2511 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2513 INT cbreaks = 0;
2514 WCHAR buffer[MAX_REPLY_LEN];
2515 DWORD buflen = MAX_REPLY_LEN;
2516 BOOL bSuccess = FALSE;
2517 INT rc = 0;
2518 static const WCHAR szCrLf[] = {'\r','\n',0};
2519 char bufferA[MAX_REPLY_LEN];
2520 LPWSTR status_code, status_text;
2521 DWORD cchMaxRawHeaders = 1024;
2522 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2523 DWORD cchRawHeaders = 0;
2525 TRACE("-->\n");
2527 /* clear old response headers (eg. from a redirect response) */
2528 HTTP_clear_response_headers( lpwhr );
2530 if (!NETCON_connected(&lpwhr->netConnection))
2531 goto lend;
2534 * HACK peek at the buffer
2536 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2539 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2541 buflen = MAX_REPLY_LEN;
2542 memset(buffer, 0, MAX_REPLY_LEN);
2543 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2544 goto lend;
2545 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2547 /* regenerate raw headers */
2548 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2550 cchMaxRawHeaders *= 2;
2551 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2553 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2554 cchRawHeaders += (buflen-1);
2555 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2556 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2557 lpszRawHeaders[cchRawHeaders] = '\0';
2559 /* split the version from the status code */
2560 status_code = strchrW( buffer, ' ' );
2561 if( !status_code )
2562 goto lend;
2563 *status_code++=0;
2565 /* split the status code from the status text */
2566 status_text = strchrW( status_code, ' ' );
2567 if( !status_text )
2568 goto lend;
2569 *status_text++=0;
2571 TRACE("version [%s] status code [%s] status text [%s]\n",
2572 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2574 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
2575 HTTP_ADDHDR_FLAG_REPLACE);
2577 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
2578 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
2580 lpwhr->lpszVersion= WININET_strdupW(buffer);
2581 lpwhr->lpszStatusText = WININET_strdupW(status_text);
2583 /* Parse each response line */
2586 buflen = MAX_REPLY_LEN;
2587 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2589 LPWSTR * pFieldAndValue;
2591 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2592 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2594 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2596 cchMaxRawHeaders *= 2;
2597 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2599 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2600 cchRawHeaders += (buflen-1);
2601 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2602 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2603 lpszRawHeaders[cchRawHeaders] = '\0';
2605 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2606 if (!pFieldAndValue)
2607 break;
2609 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2610 HTTP_ADDREQ_FLAG_ADD );
2612 HTTP_FreeTokens(pFieldAndValue);
2614 else
2616 cbreaks++;
2617 if (cbreaks >= 2)
2618 break;
2620 }while(1);
2622 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2623 lpwhr->lpszRawHeaders = lpszRawHeaders;
2624 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2625 bSuccess = TRUE;
2627 lend:
2629 TRACE("<--\n");
2630 if (bSuccess)
2631 return rc;
2632 else
2633 return 0;
2637 static void strip_spaces(LPWSTR start)
2639 LPWSTR str = start;
2640 LPWSTR end;
2642 while (*str == ' ' && *str != '\0')
2643 str++;
2645 if (str != start)
2646 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2648 end = start + strlenW(start) - 1;
2649 while (end >= start && *end == ' ')
2651 *end = '\0';
2652 end--;
2657 /***********************************************************************
2658 * HTTP_InterpretHttpHeader (internal)
2660 * Parse server response
2662 * RETURNS
2664 * Pointer to array of field, value, NULL on success.
2665 * NULL on error.
2667 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2669 LPWSTR * pTokenPair;
2670 LPWSTR pszColon;
2671 INT len;
2673 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2675 pszColon = strchrW(buffer, ':');
2676 /* must have two tokens */
2677 if (!pszColon)
2679 HTTP_FreeTokens(pTokenPair);
2680 if (buffer[0])
2681 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2682 return NULL;
2685 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2686 if (!pTokenPair[0])
2688 HTTP_FreeTokens(pTokenPair);
2689 return NULL;
2691 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2692 pTokenPair[0][pszColon - buffer] = '\0';
2694 /* skip colon */
2695 pszColon++;
2696 len = strlenW(pszColon);
2697 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2698 if (!pTokenPair[1])
2700 HTTP_FreeTokens(pTokenPair);
2701 return NULL;
2703 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2705 strip_spaces(pTokenPair[0]);
2706 strip_spaces(pTokenPair[1]);
2708 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2709 return pTokenPair;
2712 /***********************************************************************
2713 * HTTP_ProcessHeader (internal)
2715 * Stuff header into header tables according to <dwModifier>
2719 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2721 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2723 LPHTTPHEADERW lphttpHdr = NULL;
2724 BOOL bSuccess = FALSE;
2725 INT index = -1;
2726 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2727 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
2729 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2731 /* Don't let applications add Connection header to request */
2732 if (strcmpW(szConnection,field)==0 && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2734 return FALSE;
2737 /* REPLACE wins out over ADD */
2738 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2739 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
2741 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
2742 index = -1;
2743 else
2744 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
2746 if (index >= 0)
2748 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2750 return FALSE;
2752 lphttpHdr = &lpwhr->pCustHeaders[index];
2754 else if (value)
2756 HTTPHEADERW hdr;
2758 hdr.lpszField = (LPWSTR)field;
2759 hdr.lpszValue = (LPWSTR)value;
2760 hdr.wFlags = hdr.wCount = 0;
2762 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2763 hdr.wFlags |= HDR_ISREQUEST;
2765 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2768 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2769 lphttpHdr->wFlags |= HDR_ISREQUEST;
2770 else
2771 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2773 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2775 HTTP_DeleteCustomHeader( lpwhr, index );
2777 if (value)
2779 HTTPHEADERW hdr;
2781 hdr.lpszField = (LPWSTR)field;
2782 hdr.lpszValue = (LPWSTR)value;
2783 hdr.wFlags = hdr.wCount = 0;
2785 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2786 hdr.wFlags |= HDR_ISREQUEST;
2788 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2791 return TRUE;
2793 else if (dwModifier & COALESCEFLASG)
2795 LPWSTR lpsztmp;
2796 WCHAR ch = 0;
2797 INT len = 0;
2798 INT origlen = strlenW(lphttpHdr->lpszValue);
2799 INT valuelen = strlenW(value);
2801 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2803 ch = ',';
2804 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2806 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2808 ch = ';';
2809 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2812 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
2814 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2815 if (lpsztmp)
2817 lphttpHdr->lpszValue = lpsztmp;
2818 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2819 if (ch > 0)
2821 lphttpHdr->lpszValue[origlen] = ch;
2822 origlen++;
2823 lphttpHdr->lpszValue[origlen] = ' ';
2824 origlen++;
2827 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2828 lphttpHdr->lpszValue[len] = '\0';
2829 bSuccess = TRUE;
2831 else
2833 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2834 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2837 TRACE("<-- %d\n",bSuccess);
2838 return bSuccess;
2842 /***********************************************************************
2843 * HTTP_CloseConnection (internal)
2845 * Close socket connection
2848 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2850 LPWININETHTTPSESSIONW lpwhs = NULL;
2851 LPWININETAPPINFOW hIC = NULL;
2853 TRACE("%p\n",lpwhr);
2855 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2856 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2858 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2859 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2861 if (NETCON_connected(&lpwhr->netConnection))
2863 NETCON_close(&lpwhr->netConnection);
2866 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2867 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2871 /***********************************************************************
2872 * HTTP_CloseHTTPRequestHandle (internal)
2874 * Deallocate request handle
2877 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2879 DWORD i;
2880 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2882 TRACE("\n");
2884 if (NETCON_connected(&lpwhr->netConnection))
2885 HTTP_CloseConnection(lpwhr);
2887 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2888 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2889 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2890 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
2891 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
2893 for (i = 0; i < lpwhr->nCustHeaders; i++)
2895 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2896 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2899 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2900 HeapFree(GetProcessHeap(), 0, lpwhr);
2904 /***********************************************************************
2905 * HTTP_CloseHTTPSessionHandle (internal)
2907 * Deallocate session handle
2910 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2912 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2914 TRACE("%p\n", lpwhs);
2916 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2917 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2918 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2919 HeapFree(GetProcessHeap(), 0, lpwhs);
2923 /***********************************************************************
2924 * HTTP_GetCustomHeaderIndex (internal)
2926 * Return index of custom header from header array
2929 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,int requested_index, BOOL request_only)
2931 DWORD index;
2933 TRACE("%s\n", debugstr_w(lpszField));
2935 for (index = 0; index < lpwhr->nCustHeaders; index++)
2937 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2939 if ((request_only &&
2940 !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))||
2941 (!request_only &&
2942 (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST)))
2943 continue;
2945 if (requested_index == 0)
2946 break;
2947 else
2948 requested_index --;
2952 if (index >= lpwhr->nCustHeaders)
2953 index = -1;
2955 TRACE("Return: %ld\n", index);
2956 return index;
2960 /***********************************************************************
2961 * HTTP_InsertCustomHeader (internal)
2963 * Insert header into array
2966 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2968 INT count;
2969 LPHTTPHEADERW lph = NULL;
2970 BOOL r = FALSE;
2972 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2973 count = lpwhr->nCustHeaders + 1;
2974 if (count > 1)
2975 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2976 else
2977 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2979 if (NULL != lph)
2981 lpwhr->pCustHeaders = lph;
2982 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2983 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2984 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2985 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2986 lpwhr->nCustHeaders++;
2987 r = TRUE;
2989 else
2991 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2994 return r;
2998 /***********************************************************************
2999 * HTTP_DeleteCustomHeader (internal)
3001 * Delete header from array
3002 * If this function is called, the indexs may change.
3004 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3006 if( lpwhr->nCustHeaders <= 0 )
3007 return FALSE;
3008 if( index >= lpwhr->nCustHeaders )
3009 return FALSE;
3010 lpwhr->nCustHeaders--;
3012 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3013 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3014 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3016 return TRUE;
3019 /***********************************************************************
3020 * IsHostInProxyBypassList (@)
3022 * Undocumented
3025 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3027 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);
3028 return FALSE;