wininet: HTTP headers reworking.
[wine/multimedia.git] / dlls / wininet / http.c
blobaa0b83feb8846027da65659185dc6e47cdcab212
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 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <time.h>
42 #include <assert.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wininet.h"
47 #include "winreg.h"
48 #include "winerror.h"
49 #define NO_SHLWAPI_STREAM
50 #define NO_SHLWAPI_REG
51 #define NO_SHLWAPI_STRFCNS
52 #define NO_SHLWAPI_GDI
53 #include "shlwapi.h"
55 #include "internet.h"
56 #include "wine/debug.h"
57 #include "wine/unicode.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
61 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
62 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
63 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
64 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
65 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
66 static const WCHAR szHost[] = { 'H','o','s','t',0 };
67 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
68 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
70 #define MAXHOSTNAME 100
71 #define MAX_FIELD_VALUE_LEN 256
72 #define MAX_FIELD_LEN 256
74 #define HTTP_REFERER g_szReferer
75 #define HTTP_ACCEPT g_szAccept
76 #define HTTP_USERAGENT g_szUserAgent
78 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
79 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
80 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
81 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
82 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
83 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
84 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
87 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
88 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
89 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
90 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
91 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
92 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
93 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
94 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
95 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
96 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
97 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
98 LPCWSTR username, LPCWSTR password );
99 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
100 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
101 lpdwIndex);
102 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl,
103 LPCWSTR lpszHeaders, DWORD dwHeaderLength, LPVOID lpOptional, DWORD
104 dwOptionalLength, DWORD dwContentLength);
107 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
109 int HeaderIndex = 0;
110 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
111 if (HeaderIndex == -1)
112 return NULL;
113 else
114 return &req->pCustHeaders[HeaderIndex];
117 /***********************************************************************
118 * HTTP_Tokenize (internal)
120 * Tokenize a string, allocating memory for the tokens.
122 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
124 LPWSTR * token_array;
125 int tokens = 0;
126 int i;
127 LPCWSTR next_token;
129 /* empty string has no tokens */
130 if (*string)
131 tokens++;
132 /* count tokens */
133 for (i = 0; string[i]; i++)
134 if (!strncmpW(string+i, token_string, strlenW(token_string)))
136 DWORD j;
137 tokens++;
138 /* we want to skip over separators, but not the null terminator */
139 for (j = 0; j < strlenW(token_string) - 1; j++)
140 if (!string[i+j])
141 break;
142 i += j;
145 /* add 1 for terminating NULL */
146 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
147 token_array[tokens] = NULL;
148 if (!tokens)
149 return token_array;
150 for (i = 0; i < tokens; i++)
152 int len;
153 next_token = strstrW(string, token_string);
154 if (!next_token) next_token = string+strlenW(string);
155 len = next_token - string;
156 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
157 memcpy(token_array[i], string, len*sizeof(WCHAR));
158 token_array[i][len] = '\0';
159 string = next_token+strlenW(token_string);
161 return token_array;
164 /***********************************************************************
165 * HTTP_FreeTokens (internal)
167 * Frees memory returned from HTTP_Tokenize.
169 static void HTTP_FreeTokens(LPWSTR * token_array)
171 int i;
172 for (i = 0; token_array[i]; i++)
173 HeapFree(GetProcessHeap(), 0, token_array[i]);
174 HeapFree(GetProcessHeap(), 0, token_array);
177 /* **********************************************************************
179 * Helper functions for the HttpSendRequest(Ex) functions
182 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
184 /* if the verb is NULL default to GET */
185 if (NULL == lpwhr->lpszVerb)
187 static const WCHAR szGET[] = { 'G','E','T', 0 };
188 lpwhr->lpszVerb = WININET_strdupW(szGET);
192 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
194 static const WCHAR szSlash[] = { '/',0 };
195 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
197 /* If we don't have a path we set it to root */
198 if (NULL == lpwhr->lpszPath)
199 lpwhr->lpszPath = WININET_strdupW(szSlash);
200 else /* remove \r and \n*/
202 int nLen = strlenW(lpwhr->lpszPath);
203 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
205 nLen--;
206 lpwhr->lpszPath[nLen]='\0';
208 /* Replace '\' with '/' */
209 while (nLen>0) {
210 nLen--;
211 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
215 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
216 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
217 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
219 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
220 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
221 *fixurl = '/';
222 strcpyW(fixurl + 1, lpwhr->lpszPath);
223 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
224 lpwhr->lpszPath = fixurl;
228 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
230 LPWSTR requestString;
231 DWORD len, n;
232 LPCWSTR *req;
233 INT i;
234 LPWSTR p;
236 static const WCHAR szSpace[] = { ' ',0 };
237 static const WCHAR szcrlf[] = {'\r','\n', 0};
238 static const WCHAR szColon[] = { ':',' ',0 };
239 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
241 /* allocate space for an array of all the string pointers to be added */
242 len = (lpwhr->nCustHeaders)*4 + 9;
243 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
245 /* add the verb, path and HTTP version string */
246 n = 0;
247 req[n++] = verb;
248 req[n++] = szSpace;
249 req[n++] = path;
250 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
252 /* Append custom request heades */
253 for (i = 0; i < lpwhr->nCustHeaders; i++)
255 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
257 req[n++] = szcrlf;
258 req[n++] = lpwhr->pCustHeaders[i].lpszField;
259 req[n++] = szColon;
260 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
262 TRACE("Adding custom header %s (%s)\n",
263 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
264 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
268 if( n >= len )
269 ERR("oops. buffer overrun\n");
271 req[n] = NULL;
272 requestString = HTTP_build_req( req, 4 );
273 HeapFree( GetProcessHeap(), 0, req );
276 * Set (header) termination string for request
277 * Make sure there's exactly two new lines at the end of the request
279 p = &requestString[strlenW(requestString)-1];
280 while ( (*p == '\n') || (*p == '\r') )
281 p--;
282 strcpyW( p+1, sztwocrlf );
284 return requestString;
287 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
289 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
290 int HeaderIndex;
291 LPHTTPHEADERW setCookieHeader;
293 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
294 if (HeaderIndex == -1)
295 return;
296 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
298 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
300 int nPosStart = 0, nPosEnd = 0, len;
301 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
303 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
305 LPWSTR buf_cookie, cookie_name, cookie_data;
306 LPWSTR buf_url;
307 LPWSTR domain = NULL;
308 LPHTTPHEADERW Host;
310 int nEqualPos = 0;
311 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
312 setCookieHeader->lpszValue[nPosEnd] != '\0')
314 nPosEnd++;
316 if (setCookieHeader->lpszValue[nPosEnd] == ';')
318 /* fixme: not case sensitive, strcasestr is gnu only */
319 int nDomainPosEnd = 0;
320 int nDomainPosStart = 0, nDomainLength = 0;
321 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
322 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
323 if (lpszDomain)
324 { /* they have specified their own domain, lets use it */
325 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
326 lpszDomain[nDomainPosEnd] != '\0')
328 nDomainPosEnd++;
330 nDomainPosStart = strlenW(szDomain);
331 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
332 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
333 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
336 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
337 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
338 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
339 TRACE("%s\n", debugstr_w(buf_cookie));
340 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
342 nEqualPos++;
344 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
346 HeapFree(GetProcessHeap(), 0, buf_cookie);
347 break;
350 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
351 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
352 cookie_data = &buf_cookie[nEqualPos + 1];
354 Host = HTTP_GetHeader(lpwhr,szHost);
355 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
356 strlenW(lpwhr->lpszPath) + 9;
357 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
358 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
359 InternetSetCookieW(buf_url, cookie_name, cookie_data);
361 HeapFree(GetProcessHeap(), 0, buf_url);
362 HeapFree(GetProcessHeap(), 0, buf_cookie);
363 HeapFree(GetProcessHeap(), 0, cookie_name);
364 HeapFree(GetProcessHeap(), 0, domain);
365 nPosStart = nPosEnd;
370 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
372 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
373 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW)lpwhs->hdr.lpwhparent;
375 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
376 assert(hIC->hdr.htype == WH_HINIT);
378 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
379 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
380 hIC->lpszProxyPassword);
383 /***********************************************************************
384 * HTTP_HttpAddRequestHeadersW (internal)
386 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
387 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
389 LPWSTR lpszStart;
390 LPWSTR lpszEnd;
391 LPWSTR buffer;
392 BOOL bSuccess = FALSE;
393 DWORD len;
395 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
397 if( dwHeaderLength == ~0U )
398 len = strlenW(lpszHeader);
399 else
400 len = dwHeaderLength;
401 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
402 lstrcpynW( buffer, lpszHeader, len + 1);
404 lpszStart = buffer;
408 LPWSTR * pFieldAndValue;
410 lpszEnd = lpszStart;
412 while (*lpszEnd != '\0')
414 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
415 break;
416 lpszEnd++;
419 if (*lpszStart == '\0')
420 break;
422 if (*lpszEnd == '\r')
424 *lpszEnd = '\0';
425 lpszEnd += 2; /* Jump over \r\n */
427 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
428 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
429 if (pFieldAndValue)
431 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
432 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
433 HTTP_FreeTokens(pFieldAndValue);
436 lpszStart = lpszEnd;
437 } while (bSuccess);
439 HeapFree(GetProcessHeap(), 0, buffer);
441 return bSuccess;
444 /***********************************************************************
445 * HttpAddRequestHeadersW (WININET.@)
447 * Adds one or more HTTP header to the request handler
449 * RETURNS
450 * TRUE on success
451 * FALSE on failure
454 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
455 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
457 BOOL bSuccess = FALSE;
458 LPWININETHTTPREQW lpwhr;
460 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
461 dwModifier);
463 if (!lpszHeader)
464 return TRUE;
466 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
467 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
469 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
470 goto lend;
472 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
473 lend:
474 if( lpwhr )
475 WININET_Release( &lpwhr->hdr );
477 return bSuccess;
480 /***********************************************************************
481 * HttpAddRequestHeadersA (WININET.@)
483 * Adds one or more HTTP header to the request handler
485 * RETURNS
486 * TRUE on success
487 * FALSE on failure
490 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
491 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
493 DWORD len;
494 LPWSTR hdr;
495 BOOL r;
497 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
498 dwModifier);
500 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
501 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
502 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
503 if( dwHeaderLength != ~0U )
504 dwHeaderLength = len;
506 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
508 HeapFree( GetProcessHeap(), 0, hdr );
510 return r;
513 /***********************************************************************
514 * HttpEndRequestA (WININET.@)
516 * Ends an HTTP request that was started by HttpSendRequestEx
518 * RETURNS
519 * TRUE if successful
520 * FALSE on failure
523 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
524 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
526 LPINTERNET_BUFFERSA ptr;
527 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
528 BOOL rc = FALSE;
530 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
531 dwContext);
533 ptr = lpBuffersOut;
534 if (ptr)
535 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
536 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
537 else
538 lpBuffersOutW = NULL;
540 ptrW = lpBuffersOutW;
541 while (ptr)
543 if (ptr->lpvBuffer && ptr->dwBufferLength)
544 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
545 ptrW->dwBufferLength = ptr->dwBufferLength;
546 ptrW->dwBufferTotal= ptr->dwBufferTotal;
548 if (ptr->Next)
549 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
550 sizeof(INTERNET_BUFFERSW));
552 ptr = ptr->Next;
553 ptrW = ptrW->Next;
556 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
558 if (lpBuffersOutW)
560 ptrW = lpBuffersOutW;
561 while (ptrW)
563 LPINTERNET_BUFFERSW ptrW2;
565 FIXME("Do we need to translate info out of these buffer?\n");
567 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
568 ptrW2 = ptrW->Next;
569 HeapFree(GetProcessHeap(),0,ptrW);
570 ptrW = ptrW2;
574 return rc;
577 /***********************************************************************
578 * HttpEndRequestW (WININET.@)
580 * Ends an HTTP request that was started by HttpSendRequestEx
582 * RETURNS
583 * TRUE if successful
584 * FALSE on failure
587 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
588 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
590 BOOL rc = FALSE;
591 LPWININETHTTPREQW lpwhr;
592 INT responseLen;
594 TRACE("-->\n");
595 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
597 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
599 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
600 return FALSE;
603 lpwhr->hdr.dwFlags |= dwFlags;
604 lpwhr->hdr.dwContext = dwContext;
606 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
607 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
609 responseLen = HTTP_GetResponseHeaders(lpwhr);
610 if (responseLen)
611 rc = TRUE;
613 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
614 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
616 /* process headers here. Is this right? */
617 HTTP_ProcessHeaders(lpwhr);
619 /* We appear to do nothing with the buffer.. is that correct? */
621 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
623 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
624 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
625 (dwCode==302 || dwCode==301))
627 WCHAR szNewLocation[2048];
628 DWORD dwBufferSize=2048;
629 dwIndex=0;
630 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
632 static const WCHAR szGET[] = { 'G','E','T', 0 };
633 /* redirects are always GETs */
634 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
635 lpwhr->lpszVerb = WININET_strdupW(szGET);
636 return HTTP_HandleRedirect(lpwhr, szNewLocation, NULL, 0, NULL, 0, 0);
641 TRACE("%i <--\n",rc);
642 return rc;
645 /***********************************************************************
646 * HttpOpenRequestW (WININET.@)
648 * Open a HTTP request handle
650 * RETURNS
651 * HINTERNET a HTTP request handle on success
652 * NULL on failure
655 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
656 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
657 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
658 DWORD dwFlags, DWORD dwContext)
660 LPWININETHTTPSESSIONW lpwhs;
661 HINTERNET handle = NULL;
663 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
664 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
665 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
666 dwFlags, dwContext);
667 if(lpszAcceptTypes!=NULL)
669 int i;
670 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
671 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
674 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
675 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
677 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
678 goto lend;
682 * My tests seem to show that the windows version does not
683 * become asynchronous until after this point. And anyhow
684 * if this call was asynchronous then how would you get the
685 * necessary HINTERNET pointer returned by this function.
688 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
689 lpszVersion, lpszReferrer, lpszAcceptTypes,
690 dwFlags, dwContext);
691 lend:
692 if( lpwhs )
693 WININET_Release( &lpwhs->hdr );
694 TRACE("returning %p\n", handle);
695 return handle;
699 /***********************************************************************
700 * HttpOpenRequestA (WININET.@)
702 * Open a HTTP request handle
704 * RETURNS
705 * HINTERNET a HTTP request handle on success
706 * NULL on failure
709 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
710 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
711 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
712 DWORD dwFlags, DWORD dwContext)
714 LPWSTR szVerb = NULL, szObjectName = NULL;
715 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
716 INT len;
717 INT acceptTypesCount;
718 HINTERNET rc = FALSE;
719 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
720 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
721 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
722 dwFlags, dwContext);
724 if (lpszVerb)
726 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
727 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
728 if ( !szVerb )
729 goto end;
730 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
733 if (lpszObjectName)
735 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
736 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
737 if ( !szObjectName )
738 goto end;
739 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
742 if (lpszVersion)
744 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
745 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
746 if ( !szVersion )
747 goto end;
748 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
751 if (lpszReferrer)
753 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
754 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
755 if ( !szReferrer )
756 goto end;
757 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
760 acceptTypesCount = 0;
761 if (lpszAcceptTypes)
763 /* find out how many there are */
764 while (lpszAcceptTypes[acceptTypesCount])
765 acceptTypesCount++;
766 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
767 acceptTypesCount = 0;
768 while (lpszAcceptTypes[acceptTypesCount])
770 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
771 -1, NULL, 0 );
772 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
773 if (!szAcceptTypes[acceptTypesCount] )
774 goto end;
775 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
776 -1, szAcceptTypes[acceptTypesCount], len );
777 acceptTypesCount++;
779 szAcceptTypes[acceptTypesCount] = NULL;
781 else szAcceptTypes = 0;
783 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
784 szVersion, szReferrer,
785 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
787 end:
788 if (szAcceptTypes)
790 acceptTypesCount = 0;
791 while (szAcceptTypes[acceptTypesCount])
793 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
794 acceptTypesCount++;
796 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
798 HeapFree(GetProcessHeap(), 0, szReferrer);
799 HeapFree(GetProcessHeap(), 0, szVersion);
800 HeapFree(GetProcessHeap(), 0, szObjectName);
801 HeapFree(GetProcessHeap(), 0, szVerb);
803 return rc;
806 /***********************************************************************
807 * HTTP_Base64
809 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
811 UINT n = 0, x;
812 static LPCSTR HTTP_Base64Enc =
813 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
815 while( bin[0] )
817 /* first 6 bits, all from bin[0] */
818 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
819 x = (bin[0] & 3) << 4;
821 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
822 if( !bin[1] )
824 base64[n++] = HTTP_Base64Enc[x];
825 base64[n++] = '=';
826 base64[n++] = '=';
827 break;
829 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
830 x = ( bin[1] & 0x0f ) << 2;
832 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
833 if( !bin[2] )
835 base64[n++] = HTTP_Base64Enc[x];
836 base64[n++] = '=';
837 break;
839 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
841 /* last 6 bits, all from bin [2] */
842 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
843 bin += 3;
845 base64[n] = 0;
846 return n;
849 /***********************************************************************
850 * HTTP_EncodeBasicAuth
852 * Encode the basic authentication string for HTTP 1.1
854 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
856 UINT len;
857 LPWSTR in, out;
858 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
859 static const WCHAR szColon[] = {':',0};
861 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
862 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
863 if( !in )
864 return NULL;
866 len = lstrlenW(szBasic) +
867 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
868 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
869 if( out )
871 lstrcpyW( in, username );
872 lstrcatW( in, szColon );
873 lstrcatW( in, password );
874 lstrcpyW( out, szBasic );
875 HTTP_Base64( in, &out[strlenW(out)] );
877 HeapFree( GetProcessHeap(), 0, in );
879 return out;
882 /***********************************************************************
883 * HTTP_InsertProxyAuthorization
885 * Insert the basic authorization field in the request header
887 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
888 LPCWSTR username, LPCWSTR password )
890 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
891 BOOL ret = TRUE;
893 if (!authorization)
894 return FALSE;
896 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
898 HTTP_ProcessHeader(lpwhr, szProxy_Authorization, authorization,
899 HTTP_ADDHDR_FLAG_REPLACE);
901 HeapFree( GetProcessHeap(), 0, authorization );
903 return ret;
906 /***********************************************************************
907 * HTTP_DealWithProxy
909 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
910 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
912 WCHAR buf[MAXHOSTNAME];
913 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
914 WCHAR* url;
915 static const WCHAR szNul[] = { 0 };
916 URL_COMPONENTSW UrlComponents;
917 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
918 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
919 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
920 int len;
922 memset( &UrlComponents, 0, sizeof UrlComponents );
923 UrlComponents.dwStructSize = sizeof UrlComponents;
924 UrlComponents.lpszHostName = buf;
925 UrlComponents.dwHostNameLength = MAXHOSTNAME;
927 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
928 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
929 sprintfW(proxy, szFormat1, hIC->lpszProxy);
930 else
931 strcpyW(proxy, hIC->lpszProxy);
932 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
933 return FALSE;
934 if( UrlComponents.dwHostNameLength == 0 )
935 return FALSE;
937 if( !lpwhr->lpszPath )
938 lpwhr->lpszPath = (LPWSTR)szNul;
939 TRACE("server='%s' path='%s'\n",
940 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
941 /* for constant 15 see above */
942 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
943 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
945 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
946 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
948 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
950 if( lpwhr->lpszPath[0] != '/' )
951 strcatW( url, szSlash );
952 strcatW(url, lpwhr->lpszPath);
953 if(lpwhr->lpszPath != szNul)
954 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
955 lpwhr->lpszPath = url;
957 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
958 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
959 lpwhs->nServerPort = UrlComponents.nPort;
961 return TRUE;
964 /***********************************************************************
965 * HTTP_HttpOpenRequestW (internal)
967 * Open a HTTP request handle
969 * RETURNS
970 * HINTERNET a HTTP request handle on success
971 * NULL on failure
974 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
975 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
976 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
977 DWORD dwFlags, DWORD dwContext)
979 LPWININETAPPINFOW hIC = NULL;
980 LPWININETHTTPREQW lpwhr;
981 LPWSTR lpszCookies;
982 LPWSTR lpszUrl = NULL;
983 DWORD nCookieSize;
984 HINTERNET handle = NULL;
985 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
986 DWORD len;
987 LPHTTPHEADERW Host;
989 TRACE("-->\n");
991 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
992 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
994 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
995 if (NULL == lpwhr)
997 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
998 goto lend;
1000 lpwhr->hdr.htype = WH_HHTTPREQ;
1001 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
1002 lpwhr->hdr.dwFlags = dwFlags;
1003 lpwhr->hdr.dwContext = dwContext;
1004 lpwhr->hdr.dwRefCount = 1;
1005 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1006 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1008 handle = WININET_AllocHandle( &lpwhr->hdr );
1009 if (NULL == handle)
1011 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1012 goto lend;
1015 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
1017 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1018 HRESULT rc;
1020 len = 0;
1021 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1022 if (rc != E_POINTER)
1023 len = strlenW(lpszObjectName)+1;
1024 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1025 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1026 URL_ESCAPE_SPACES_ONLY);
1027 if (rc)
1029 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
1030 strcpyW(lpwhr->lpszPath,lpszObjectName);
1034 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1035 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1037 if(lpszAcceptTypes!=NULL)
1039 int i;
1040 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1041 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1044 if (NULL == lpszVerb)
1046 static const WCHAR szGet[] = {'G','E','T',0};
1047 lpwhr->lpszVerb = WININET_strdupW(szGet);
1049 else if (strlenW(lpszVerb))
1050 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1052 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1054 WCHAR buf[MAXHOSTNAME];
1055 URL_COMPONENTSW UrlComponents;
1057 memset( &UrlComponents, 0, sizeof UrlComponents );
1058 UrlComponents.dwStructSize = sizeof UrlComponents;
1059 UrlComponents.lpszHostName = buf;
1060 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1062 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1063 if (strlenW(UrlComponents.lpszHostName))
1064 HTTP_ProcessHeader(lpwhr, szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1066 else
1067 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1069 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1070 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1071 INTERNET_DEFAULT_HTTPS_PORT :
1072 INTERNET_DEFAULT_HTTP_PORT);
1073 lpwhs->nHostPort = lpwhs->nServerPort;
1075 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1076 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1078 if (hIC->lpszAgent)
1080 WCHAR *agent_header;
1081 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1083 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1084 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1085 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1087 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1088 HTTP_ADDREQ_FLAG_ADD);
1089 HeapFree(GetProcessHeap(), 0, agent_header);
1092 Host = HTTP_GetHeader(lpwhr,szHost);
1094 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1095 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1096 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1098 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1099 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1101 int cnt = 0;
1102 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1103 static const WCHAR szcrlf[] = {'\r','\n',0};
1105 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1107 cnt += sprintfW(lpszCookies, szCookie);
1108 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1109 strcatW(lpszCookies, szcrlf);
1111 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1112 HTTP_ADDREQ_FLAG_ADD);
1113 HeapFree(GetProcessHeap(), 0, lpszCookies);
1115 HeapFree(GetProcessHeap(), 0, lpszUrl);
1118 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1119 INTERNET_STATUS_HANDLE_CREATED, &handle,
1120 sizeof(handle));
1123 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1127 * According to my tests. The name is not resolved until a request is Opened
1129 INTERNET_SendCallback(&lpwhr->hdr, dwContext,
1130 INTERNET_STATUS_RESOLVING_NAME,
1131 lpwhs->lpszServerName,
1132 strlenW(lpwhs->lpszServerName)+1);
1134 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1135 &lpwhs->socketAddress))
1137 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1138 InternetCloseHandle( handle );
1139 handle = NULL;
1140 goto lend;
1143 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1144 INTERNET_STATUS_NAME_RESOLVED,
1145 &(lpwhs->socketAddress),
1146 sizeof(struct sockaddr_in));
1148 lend:
1149 if( lpwhr )
1150 WININET_Release( &lpwhr->hdr );
1152 TRACE("<-- %p (%p)\n", handle, lpwhr);
1153 return handle;
1156 typedef struct std_hdr_data
1158 const WCHAR* hdrStr;
1159 INT hdrIndex;
1160 } std_hdr_data;
1162 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1163 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1164 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1165 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1166 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1167 static const WCHAR szAge[] = { 'A','g','e',0 };
1168 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1169 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1170 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1171 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1172 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1173 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1174 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1175 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1176 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1177 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1178 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1179 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1180 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 };
1181 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1182 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1183 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1184 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1185 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1186 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1187 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1188 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1189 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1190 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1191 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1192 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1193 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1194 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1195 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1196 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1197 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1198 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1199 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1200 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1201 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1202 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1203 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1204 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1205 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1206 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1207 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 };
1208 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1209 static const WCHAR szURI[] = { 'U','R','I',0 };
1210 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1211 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1212 static const WCHAR szVia[] = { 'V','i','a',0 };
1213 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1214 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1216 static const std_hdr_data SORTED_STANDARD_HEADERS[] = {
1217 {szAccept, HTTP_QUERY_ACCEPT,},
1218 {szAccept_Charset, HTTP_QUERY_ACCEPT_CHARSET,},
1219 {szAccept_Encoding, HTTP_QUERY_ACCEPT_ENCODING,},
1220 {szAccept_Language, HTTP_QUERY_ACCEPT_LANGUAGE,},
1221 {szAccept_Ranges, HTTP_QUERY_ACCEPT_RANGES,},
1222 {szAge, HTTP_QUERY_AGE,},
1223 {szAllow, HTTP_QUERY_ALLOW,},
1224 {szAuthorization, HTTP_QUERY_AUTHORIZATION,},
1225 {szCache_Control, HTTP_QUERY_CACHE_CONTROL,},
1226 {szConnection, HTTP_QUERY_CONNECTION,},
1227 {szContent_Base, HTTP_QUERY_CONTENT_BASE,},
1228 {szContent_Encoding, HTTP_QUERY_CONTENT_ENCODING,},
1229 {szContent_ID, HTTP_QUERY_CONTENT_ID,},
1230 {szContent_Language, HTTP_QUERY_CONTENT_LANGUAGE,},
1231 {szContent_Length, HTTP_QUERY_CONTENT_LENGTH,},
1232 {szContent_Location, HTTP_QUERY_CONTENT_LOCATION,},
1233 {szContent_MD5, HTTP_QUERY_CONTENT_MD5,},
1234 {szContent_Range, HTTP_QUERY_CONTENT_RANGE,},
1235 {szContent_Transfer_Encoding,HTTP_QUERY_CONTENT_TRANSFER_ENCODING,},
1236 {szContent_Type, HTTP_QUERY_CONTENT_TYPE,},
1237 {szCookie, HTTP_QUERY_COOKIE,},
1238 {szDate, HTTP_QUERY_DATE,},
1239 {szETag, HTTP_QUERY_ETAG,},
1240 {szExpect, HTTP_QUERY_EXPECT,},
1241 {szExpires, HTTP_QUERY_EXPIRES,},
1242 {szFrom, HTTP_QUERY_DERIVED_FROM,},
1243 {szHost, HTTP_QUERY_HOST,},
1244 {szIf_Match, HTTP_QUERY_IF_MATCH,},
1245 {szIf_Modified_Since, HTTP_QUERY_IF_MODIFIED_SINCE,},
1246 {szIf_None_Match, HTTP_QUERY_IF_NONE_MATCH,},
1247 {szIf_Range, HTTP_QUERY_IF_RANGE,},
1248 {szIf_Unmodified_Since, HTTP_QUERY_IF_UNMODIFIED_SINCE,},
1249 {szLast_Modified, HTTP_QUERY_LAST_MODIFIED,},
1250 {szLocation, HTTP_QUERY_LOCATION,},
1251 {szMax_Forwards, HTTP_QUERY_MAX_FORWARDS,},
1252 {szMime_Version, HTTP_QUERY_MIME_VERSION,},
1253 {szPragma, HTTP_QUERY_PRAGMA,},
1254 {szProxy_Authenticate, HTTP_QUERY_PROXY_AUTHENTICATE,},
1255 {szProxy_Authorization, HTTP_QUERY_PROXY_AUTHORIZATION,},
1256 {szProxy_Connection, HTTP_QUERY_PROXY_CONNECTION,},
1257 {szPublic, HTTP_QUERY_PUBLIC,},
1258 {szRange, HTTP_QUERY_RANGE,},
1259 {szReferer, HTTP_QUERY_REFERER,},
1260 {szRetry_After, HTTP_QUERY_RETRY_AFTER,},
1261 {szServer, HTTP_QUERY_SERVER,},
1262 {szSet_Cookie, HTTP_QUERY_SET_COOKIE,},
1263 {szStatus, HTTP_QUERY_STATUS_CODE,},
1264 {szTransfer_Encoding, HTTP_QUERY_TRANSFER_ENCODING,},
1265 {szUnless_Modified_Since, HTTP_QUERY_UNLESS_MODIFIED_SINCE,},
1266 {szUpgrade, HTTP_QUERY_UPGRADE,},
1267 {szURI, HTTP_QUERY_URI,},
1268 {szUser_Agent, HTTP_QUERY_USER_AGENT,},
1269 {szVary, HTTP_QUERY_VARY,},
1270 {szVia, HTTP_QUERY_VIA,},
1271 {szWarning, HTTP_QUERY_WARNING,},
1272 {szWWW_Authenticate, HTTP_QUERY_WWW_AUTHENTICATE,},
1275 /***********************************************************************
1276 * HTTP_HttpQueryInfoW (internal)
1278 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1279 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1281 LPHTTPHEADERW lphttpHdr = NULL;
1282 BOOL bSuccess = FALSE;
1283 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1286 /* Find requested header structure */
1287 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
1289 INT requested_index = (lpdwIndex)?(*lpdwIndex):0;
1290 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer,
1291 requested_index,request_only);
1293 if (index < 0)
1294 return bSuccess;
1295 else
1296 lphttpHdr = &lpwhr->pCustHeaders[index];
1298 if (lpdwIndex)
1299 (*lpdwIndex)++;
1301 else
1303 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
1305 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
1307 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1308 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1310 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1311 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1312 return FALSE;
1314 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1315 *lpdwBufferLength = len * sizeof(WCHAR);
1317 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1319 return TRUE;
1321 else if (index == HTTP_QUERY_RAW_HEADERS)
1323 static const WCHAR szCrLf[] = {'\r','\n',0};
1324 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1325 DWORD i, size = 0;
1326 LPWSTR pszString = (WCHAR*)lpBuffer;
1328 for (i = 0; ppszRawHeaderLines[i]; i++)
1329 size += strlenW(ppszRawHeaderLines[i]) + 1;
1331 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1333 HTTP_FreeTokens(ppszRawHeaderLines);
1334 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1335 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1336 return FALSE;
1339 for (i = 0; ppszRawHeaderLines[i]; i++)
1341 DWORD len = strlenW(ppszRawHeaderLines[i]);
1342 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1343 pszString += len+1;
1345 *pszString = '\0';
1347 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1349 *lpdwBufferLength = size * sizeof(WCHAR);
1350 HTTP_FreeTokens(ppszRawHeaderLines);
1352 return TRUE;
1354 else if (index == HTTP_QUERY_STATUS_TEXT)
1356 DWORD len = strlenW(lpwhr->lpszStatusText);
1357 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1359 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1360 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1361 return FALSE;
1363 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1364 *lpdwBufferLength = len * sizeof(WCHAR);
1366 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1368 return TRUE;
1370 else if (index == HTTP_QUERY_VERSION)
1372 DWORD len = strlenW(lpwhr->lpszVersion);
1373 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1375 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1376 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1377 return FALSE;
1379 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1380 *lpdwBufferLength = len * sizeof(WCHAR);
1382 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1384 return TRUE;
1386 else if (index >= 0 && index <= HTTP_QUERY_MAX )
1388 int i;
1389 for (i = 0; i < sizeof(SORTED_STANDARD_HEADERS)/sizeof(std_hdr_data) ; i++)
1391 if (SORTED_STANDARD_HEADERS[i].hdrIndex == index)
1393 INT requested_index = (lpdwIndex)?(*lpdwIndex):0;
1394 INT index = HTTP_GetCustomHeaderIndex(lpwhr,
1395 (LPWSTR)SORTED_STANDARD_HEADERS[i].hdrStr,
1396 requested_index,request_only);
1398 if (index < 0)
1399 return bSuccess;
1400 else
1401 lphttpHdr = &lpwhr->pCustHeaders[index];
1403 if (lpdwIndex)
1404 (*lpdwIndex)++;
1406 break;
1410 if (!lphttpHdr)
1412 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1413 return bSuccess;
1416 else
1418 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1419 return bSuccess;
1423 /* Ensure header satisifies requested attributes */
1424 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1425 (~lphttpHdr->wFlags & HDR_ISREQUEST))
1427 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1428 return bSuccess;
1431 /* coalesce value to reuqested type */
1432 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1434 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1435 bSuccess = TRUE;
1437 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1439 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1441 time_t tmpTime;
1442 struct tm tmpTM;
1443 SYSTEMTIME *STHook;
1445 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1447 tmpTM = *gmtime(&tmpTime);
1448 STHook = (SYSTEMTIME *) lpBuffer;
1449 if(STHook==NULL)
1450 return bSuccess;
1452 STHook->wDay = tmpTM.tm_mday;
1453 STHook->wHour = tmpTM.tm_hour;
1454 STHook->wMilliseconds = 0;
1455 STHook->wMinute = tmpTM.tm_min;
1456 STHook->wDayOfWeek = tmpTM.tm_wday;
1457 STHook->wMonth = tmpTM.tm_mon + 1;
1458 STHook->wSecond = tmpTM.tm_sec;
1459 STHook->wYear = tmpTM.tm_year;
1461 bSuccess = TRUE;
1463 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1464 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1465 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1467 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1469 if (*lpdwIndex >= lphttpHdr->wCount)
1471 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1473 else
1475 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1476 (*lpdwIndex)++;
1479 else if (lphttpHdr->lpszValue)
1481 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1483 if (len > *lpdwBufferLength)
1485 *lpdwBufferLength = len;
1486 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1487 return bSuccess;
1490 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1491 *lpdwBufferLength = len - sizeof(WCHAR);
1492 bSuccess = TRUE;
1494 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1496 return bSuccess;
1499 /***********************************************************************
1500 * HttpQueryInfoW (WININET.@)
1502 * Queries for information about an HTTP request
1504 * RETURNS
1505 * TRUE on success
1506 * FALSE on failure
1509 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1510 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1512 BOOL bSuccess = FALSE;
1513 LPWININETHTTPREQW lpwhr;
1515 if (TRACE_ON(wininet)) {
1516 #define FE(x) { x, #x }
1517 static const wininet_flag_info query_flags[] = {
1518 FE(HTTP_QUERY_MIME_VERSION),
1519 FE(HTTP_QUERY_CONTENT_TYPE),
1520 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1521 FE(HTTP_QUERY_CONTENT_ID),
1522 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1523 FE(HTTP_QUERY_CONTENT_LENGTH),
1524 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1525 FE(HTTP_QUERY_ALLOW),
1526 FE(HTTP_QUERY_PUBLIC),
1527 FE(HTTP_QUERY_DATE),
1528 FE(HTTP_QUERY_EXPIRES),
1529 FE(HTTP_QUERY_LAST_MODIFIED),
1530 FE(HTTP_QUERY_MESSAGE_ID),
1531 FE(HTTP_QUERY_URI),
1532 FE(HTTP_QUERY_DERIVED_FROM),
1533 FE(HTTP_QUERY_COST),
1534 FE(HTTP_QUERY_LINK),
1535 FE(HTTP_QUERY_PRAGMA),
1536 FE(HTTP_QUERY_VERSION),
1537 FE(HTTP_QUERY_STATUS_CODE),
1538 FE(HTTP_QUERY_STATUS_TEXT),
1539 FE(HTTP_QUERY_RAW_HEADERS),
1540 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1541 FE(HTTP_QUERY_CONNECTION),
1542 FE(HTTP_QUERY_ACCEPT),
1543 FE(HTTP_QUERY_ACCEPT_CHARSET),
1544 FE(HTTP_QUERY_ACCEPT_ENCODING),
1545 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1546 FE(HTTP_QUERY_AUTHORIZATION),
1547 FE(HTTP_QUERY_CONTENT_ENCODING),
1548 FE(HTTP_QUERY_FORWARDED),
1549 FE(HTTP_QUERY_FROM),
1550 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1551 FE(HTTP_QUERY_LOCATION),
1552 FE(HTTP_QUERY_ORIG_URI),
1553 FE(HTTP_QUERY_REFERER),
1554 FE(HTTP_QUERY_RETRY_AFTER),
1555 FE(HTTP_QUERY_SERVER),
1556 FE(HTTP_QUERY_TITLE),
1557 FE(HTTP_QUERY_USER_AGENT),
1558 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1559 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1560 FE(HTTP_QUERY_ACCEPT_RANGES),
1561 FE(HTTP_QUERY_SET_COOKIE),
1562 FE(HTTP_QUERY_COOKIE),
1563 FE(HTTP_QUERY_REQUEST_METHOD),
1564 FE(HTTP_QUERY_REFRESH),
1565 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1566 FE(HTTP_QUERY_AGE),
1567 FE(HTTP_QUERY_CACHE_CONTROL),
1568 FE(HTTP_QUERY_CONTENT_BASE),
1569 FE(HTTP_QUERY_CONTENT_LOCATION),
1570 FE(HTTP_QUERY_CONTENT_MD5),
1571 FE(HTTP_QUERY_CONTENT_RANGE),
1572 FE(HTTP_QUERY_ETAG),
1573 FE(HTTP_QUERY_HOST),
1574 FE(HTTP_QUERY_IF_MATCH),
1575 FE(HTTP_QUERY_IF_NONE_MATCH),
1576 FE(HTTP_QUERY_IF_RANGE),
1577 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1578 FE(HTTP_QUERY_MAX_FORWARDS),
1579 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1580 FE(HTTP_QUERY_RANGE),
1581 FE(HTTP_QUERY_TRANSFER_ENCODING),
1582 FE(HTTP_QUERY_UPGRADE),
1583 FE(HTTP_QUERY_VARY),
1584 FE(HTTP_QUERY_VIA),
1585 FE(HTTP_QUERY_WARNING),
1586 FE(HTTP_QUERY_CUSTOM)
1588 static const wininet_flag_info modifier_flags[] = {
1589 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1590 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1591 FE(HTTP_QUERY_FLAG_NUMBER),
1592 FE(HTTP_QUERY_FLAG_COALESCE)
1594 #undef FE
1595 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1596 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1597 DWORD i;
1599 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1600 TRACE(" Attribute:");
1601 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1602 if (query_flags[i].val == info) {
1603 TRACE(" %s", query_flags[i].name);
1604 break;
1607 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1608 TRACE(" Unknown (%08lx)", info);
1611 TRACE(" Modifier:");
1612 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1613 if (modifier_flags[i].val & info_mod) {
1614 TRACE(" %s", modifier_flags[i].name);
1615 info_mod &= ~ modifier_flags[i].val;
1619 if (info_mod) {
1620 TRACE(" Unknown (%08lx)", info_mod);
1622 TRACE("\n");
1625 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1626 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1628 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1629 goto lend;
1632 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1633 lpBuffer, lpdwBufferLength, lpdwIndex);
1635 lend:
1636 if( lpwhr )
1637 WININET_Release( &lpwhr->hdr );
1639 TRACE("%d <--\n", bSuccess);
1640 return bSuccess;
1643 /***********************************************************************
1644 * HttpQueryInfoA (WININET.@)
1646 * Queries for information about an HTTP request
1648 * RETURNS
1649 * TRUE on success
1650 * FALSE on failure
1653 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1654 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1656 BOOL result;
1657 DWORD len;
1658 WCHAR* bufferW;
1660 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1661 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1663 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1664 lpdwBufferLength, lpdwIndex );
1667 len = (*lpdwBufferLength)*sizeof(WCHAR);
1668 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1669 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1670 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
1671 MultiByteToWideChar(CP_ACP,0,lpBuffer,-1,bufferW,len);
1672 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1673 &len, lpdwIndex );
1674 if( result )
1676 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1677 lpBuffer, *lpdwBufferLength, NULL, NULL );
1678 *lpdwBufferLength = len - 1;
1680 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1682 else
1683 /* since the strings being returned from HttpQueryInfoW should be
1684 * only ASCII characters, it is reasonable to assume that all of
1685 * the Unicode characters can be reduced to a single byte */
1686 *lpdwBufferLength = len / sizeof(WCHAR);
1688 HeapFree(GetProcessHeap(), 0, bufferW );
1690 return result;
1693 /***********************************************************************
1694 * HttpSendRequestExA (WININET.@)
1696 * Sends the specified request to the HTTP server and allows chunked
1697 * transfers
1699 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1700 LPINTERNET_BUFFERSA lpBuffersIn,
1701 LPINTERNET_BUFFERSA lpBuffersOut,
1702 DWORD dwFlags, DWORD dwContext)
1704 INTERNET_BUFFERSW BuffersInW;
1705 BOOL rc = FALSE;
1706 DWORD headerlen;
1708 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1709 lpBuffersOut, dwFlags, dwContext);
1711 if (lpBuffersIn)
1713 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1714 if (lpBuffersIn->lpcszHeader)
1716 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
1717 lpBuffersIn->dwHeadersLength,0,0);
1718 BuffersInW.lpcszHeader = HeapAlloc(GetProcessHeap(),0,headerlen*
1719 sizeof(WCHAR));
1720 if (!BuffersInW.lpcszHeader)
1722 SetLastError(ERROR_OUTOFMEMORY);
1723 return FALSE;
1725 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1726 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1727 (LPWSTR)BuffersInW.lpcszHeader, headerlen);
1729 else
1730 BuffersInW.lpcszHeader = NULL;
1731 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
1732 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
1733 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
1734 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
1735 BuffersInW.Next = NULL;
1738 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
1740 if (lpBuffersIn)
1741 HeapFree(GetProcessHeap(),0,(LPVOID)BuffersInW.lpcszHeader);
1743 return rc;
1746 /***********************************************************************
1747 * HttpSendRequestExW (WININET.@)
1749 * Sends the specified request to the HTTP server and allows chunked
1750 * transfers
1752 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1753 LPINTERNET_BUFFERSW lpBuffersIn,
1754 LPINTERNET_BUFFERSW lpBuffersOut,
1755 DWORD dwFlags, DWORD dwContext)
1757 BOOL ret;
1758 LPWININETHTTPREQW lpwhr;
1759 LPWININETHTTPSESSIONW lpwhs;
1760 LPWININETAPPINFOW hIC;
1762 TRACE("(%p, %p, %p, %08lx, %08lx)\n", hRequest, lpBuffersIn,
1763 lpBuffersOut, dwFlags, dwContext);
1765 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1767 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1769 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1770 return FALSE;
1773 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1774 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
1775 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1776 assert(hIC->hdr.htype == WH_HINIT);
1778 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1780 WORKREQUEST workRequest;
1781 struct WORKREQ_HTTPSENDREQUESTW *req;
1783 workRequest.asyncall = HTTPSENDREQUESTW;
1784 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1785 req = &workRequest.u.HttpSendRequestW;
1786 if (lpBuffersIn->lpcszHeader)
1787 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1788 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
1789 else
1790 req->lpszHeader = NULL;
1791 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
1792 req->lpOptional = lpBuffersIn->lpvBuffer;
1793 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
1794 req->dwContentLength = lpBuffersIn->dwBufferTotal;
1795 req->bEndRequest = FALSE;
1797 INTERNET_AsyncCall(&workRequest);
1799 * This is from windows.
1801 SetLastError(ERROR_IO_PENDING);
1802 ret = FALSE;
1804 else
1806 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1807 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
1808 lpBuffersIn->dwBufferTotal, FALSE);
1811 WININET_Release(&lpwhr->hdr);
1812 TRACE("<---\n");
1813 return ret;
1816 /***********************************************************************
1817 * HttpSendRequestW (WININET.@)
1819 * Sends the specified request to the HTTP server
1821 * RETURNS
1822 * TRUE on success
1823 * FALSE on failure
1826 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1827 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1829 LPWININETHTTPREQW lpwhr;
1830 LPWININETHTTPSESSIONW lpwhs = NULL;
1831 LPWININETAPPINFOW hIC = NULL;
1832 BOOL r;
1834 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1835 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1837 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1838 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1840 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1841 r = FALSE;
1842 goto lend;
1845 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1846 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1848 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1849 r = FALSE;
1850 goto lend;
1853 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1854 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1856 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1857 r = FALSE;
1858 goto lend;
1861 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1863 WORKREQUEST workRequest;
1864 struct WORKREQ_HTTPSENDREQUESTW *req;
1866 workRequest.asyncall = HTTPSENDREQUESTW;
1867 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1868 req = &workRequest.u.HttpSendRequestW;
1869 if (lpszHeaders)
1870 req->lpszHeader = WININET_strdupW(lpszHeaders);
1871 else
1872 req->lpszHeader = 0;
1873 req->dwHeaderLength = dwHeaderLength;
1874 req->lpOptional = lpOptional;
1875 req->dwOptionalLength = dwOptionalLength;
1876 req->dwContentLength = dwOptionalLength;
1877 req->bEndRequest = TRUE;
1879 INTERNET_AsyncCall(&workRequest);
1881 * This is from windows.
1883 SetLastError(ERROR_IO_PENDING);
1884 r = FALSE;
1886 else
1888 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1889 dwHeaderLength, lpOptional, dwOptionalLength,
1890 dwOptionalLength, TRUE);
1892 lend:
1893 if( lpwhr )
1894 WININET_Release( &lpwhr->hdr );
1895 return r;
1898 /***********************************************************************
1899 * HttpSendRequestA (WININET.@)
1901 * Sends the specified request to the HTTP server
1903 * RETURNS
1904 * TRUE on success
1905 * FALSE on failure
1908 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1909 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1911 BOOL result;
1912 LPWSTR szHeaders=NULL;
1913 DWORD nLen=dwHeaderLength;
1914 if(lpszHeaders!=NULL)
1916 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1917 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1918 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1920 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1921 HeapFree(GetProcessHeap(),0,szHeaders);
1922 return result;
1925 /***********************************************************************
1926 * HTTP_HandleRedirect (internal)
1928 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1929 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
1930 DWORD dwContentLength)
1932 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1933 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1934 WCHAR path[2048];
1936 if(lpszUrl[0]=='/')
1938 /* if it's an absolute path, keep the same session info */
1939 lstrcpynW(path, lpszUrl, 2048);
1941 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1943 TRACE("Redirect through proxy\n");
1944 lstrcpynW(path, lpszUrl, 2048);
1946 else
1948 URL_COMPONENTSW urlComponents;
1949 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1950 static const WCHAR szHttp[] = {'h','t','t','p',0};
1951 static const WCHAR szHttps[] = {'h','t','t','p','s',0};
1952 userName[0] = 0;
1953 hostName[0] = 0;
1954 protocol[0] = 0;
1956 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1957 urlComponents.lpszScheme = protocol;
1958 urlComponents.dwSchemeLength = 32;
1959 urlComponents.lpszHostName = hostName;
1960 urlComponents.dwHostNameLength = MAXHOSTNAME;
1961 urlComponents.lpszUserName = userName;
1962 urlComponents.dwUserNameLength = 1024;
1963 urlComponents.lpszPassword = NULL;
1964 urlComponents.dwPasswordLength = 0;
1965 urlComponents.lpszUrlPath = path;
1966 urlComponents.dwUrlPathLength = 2048;
1967 urlComponents.lpszExtraInfo = NULL;
1968 urlComponents.dwExtraInfoLength = 0;
1969 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1970 return FALSE;
1972 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
1973 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
1975 TRACE("redirect from secure page to non-secure page\n");
1976 /* FIXME: warn about from secure redirect to non-secure page */
1977 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
1979 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
1980 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
1982 TRACE("redirect from non-secure page to secure page\n");
1983 /* FIXME: notify about redirect to secure page */
1984 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
1987 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1989 if (lstrlenW(protocol)>4) /*https*/
1990 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
1991 else /*http*/
1992 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1995 #if 0
1997 * This upsets redirects to binary files on sourceforge.net
1998 * and gives an html page instead of the target file
1999 * Examination of the HTTP request sent by native wininet.dll
2000 * reveals that it doesn't send a referrer in that case.
2001 * Maybe there's a flag that enables this, or maybe a referrer
2002 * shouldn't be added in case of a redirect.
2005 /* consider the current host as the referrer */
2006 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2007 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2008 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2009 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2010 #endif
2012 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2013 lpwhs->lpszServerName = WININET_strdupW(hostName);
2014 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2015 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2016 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2018 int len;
2019 static WCHAR fmt[] = {'%','s',':','%','i',0};
2020 len = lstrlenW(hostName);
2021 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2022 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2023 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2025 else
2026 lpwhs->lpszHostName = WININET_strdupW(hostName);
2028 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2031 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2032 lpwhs->lpszUserName = WININET_strdupW(userName);
2033 lpwhs->nServerPort = urlComponents.nPort;
2035 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2036 INTERNET_STATUS_RESOLVING_NAME,
2037 lpwhs->lpszServerName,
2038 strlenW(lpwhs->lpszServerName)+1);
2040 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
2041 &lpwhs->socketAddress))
2043 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2044 return FALSE;
2047 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2048 INTERNET_STATUS_NAME_RESOLVED,
2049 &(lpwhs->socketAddress),
2050 sizeof(struct sockaddr_in));
2052 NETCON_close(&lpwhr->netConnection);
2053 NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE);
2056 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2057 lpwhr->lpszPath=NULL;
2058 if (strlenW(path))
2060 DWORD needed = 0;
2061 HRESULT rc;
2063 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2064 if (rc != E_POINTER)
2065 needed = strlenW(path)+1;
2066 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2067 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2068 URL_ESCAPE_SPACES_ONLY);
2069 if (rc)
2071 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
2072 strcpyW(lpwhr->lpszPath,path);
2076 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional,
2077 dwOptionalLength, dwContentLength, TRUE);
2080 /***********************************************************************
2081 * HTTP_build_req (internal)
2083 * concatenate all the strings in the request together
2085 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2087 LPCWSTR *t;
2088 LPWSTR str;
2090 for( t = list; *t ; t++ )
2091 len += strlenW( *t );
2092 len++;
2094 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2095 *str = 0;
2097 for( t = list; *t ; t++ )
2098 strcatW( str, *t );
2100 return str;
2103 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2105 LPWSTR lpszPath;
2106 LPWSTR requestString;
2107 INT len;
2108 INT cnt;
2109 INT responseLen;
2110 char *ascii_req;
2111 BOOL ret;
2112 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2113 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2114 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2116 TRACE("\n");
2118 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2119 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2120 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2121 HeapFree( GetProcessHeap(), 0, lpszPath );
2123 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2124 NULL, 0, NULL, NULL );
2125 len--; /* the nul terminator isn't needed */
2126 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2127 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2128 ascii_req, len, NULL, NULL );
2129 HeapFree( GetProcessHeap(), 0, requestString );
2131 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2133 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2134 HeapFree( GetProcessHeap(), 0, ascii_req );
2135 if (!ret || cnt < 0)
2136 return FALSE;
2138 responseLen = HTTP_GetResponseHeaders( lpwhr );
2139 if (!responseLen)
2140 return FALSE;
2142 return TRUE;
2145 /***********************************************************************
2146 * HTTP_HttpSendRequestW (internal)
2148 * Sends the specified request to the HTTP server
2150 * RETURNS
2151 * TRUE on success
2152 * FALSE on failure
2155 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2156 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2157 DWORD dwContentLength, BOOL bEndRequest)
2159 INT cnt;
2160 BOOL bSuccess = FALSE;
2161 LPWSTR requestString = NULL;
2162 INT responseLen;
2163 BOOL loop_next = FALSE;
2164 INTERNET_ASYNC_RESULT iar;
2165 LPHTTPHEADERW Host;
2167 TRACE("--> %p\n", lpwhr);
2169 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2171 /* Clear any error information */
2172 INTERNET_SetLastError(0);
2174 HTTP_FixVerb(lpwhr);
2176 /* if we are using optional stuff, we must add the fixed header of that option length */
2177 if (dwContentLength > 0)
2179 static const WCHAR szContentLength[] = {
2180 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2181 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2182 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2183 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
2186 Host = HTTP_GetHeader(lpwhr,szHost);
2189 DWORD len;
2190 char *ascii_req;
2192 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2193 loop_next = FALSE;
2195 HTTP_FixURL(lpwhr);
2197 /* add the headers the caller supplied */
2198 if( lpszHeaders && dwHeaderLength )
2200 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2201 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2204 /* if there's a proxy username and password, add it to the headers */
2205 HTTP_AddProxyInfo(lpwhr);
2207 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2209 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2211 /* Send the request and store the results */
2212 if (!HTTP_OpenConnection(lpwhr))
2213 goto lend;
2215 /* send the request as ASCII, tack on the optional data */
2216 if( !lpOptional )
2217 dwOptionalLength = 0;
2218 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2219 NULL, 0, NULL, NULL );
2220 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2221 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2222 ascii_req, len, NULL, NULL );
2223 if( lpOptional )
2224 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2225 len = (len + dwOptionalLength - 1);
2226 ascii_req[len] = 0;
2227 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2229 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2230 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2232 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2233 HeapFree( GetProcessHeap(), 0, ascii_req );
2235 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2236 INTERNET_STATUS_REQUEST_SENT,
2237 &len, sizeof(DWORD));
2239 if (bEndRequest)
2241 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2242 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2244 if (cnt < 0)
2245 goto lend;
2247 responseLen = HTTP_GetResponseHeaders(lpwhr);
2248 if (responseLen)
2249 bSuccess = TRUE;
2251 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2252 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2253 sizeof(DWORD));
2255 HTTP_ProcessHeaders(lpwhr);
2257 else
2258 bSuccess = TRUE;
2260 while (loop_next);
2262 lend:
2264 HeapFree(GetProcessHeap(), 0, requestString);
2266 /* TODO: send notification for P3P header */
2268 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess && bEndRequest)
2270 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2271 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2272 (dwCode==302 || dwCode==301))
2274 WCHAR szNewLocation[2048];
2275 DWORD dwBufferSize=2048;
2276 dwIndex=0;
2277 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2279 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2280 INTERNET_STATUS_REDIRECT, szNewLocation,
2281 dwBufferSize);
2282 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
2283 dwHeaderLength, lpOptional, dwOptionalLength,
2284 dwContentLength);
2290 iar.dwResult = (DWORD)bSuccess;
2291 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2293 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2294 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2295 sizeof(INTERNET_ASYNC_RESULT));
2297 TRACE("<--\n");
2298 return bSuccess;
2302 /***********************************************************************
2303 * HTTP_Connect (internal)
2305 * Create http session handle
2307 * RETURNS
2308 * HINTERNET a session handle on success
2309 * NULL on failure
2312 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2313 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2314 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2315 DWORD dwInternalFlags)
2317 BOOL bSuccess = FALSE;
2318 LPWININETHTTPSESSIONW lpwhs = NULL;
2319 HINTERNET handle = NULL;
2321 TRACE("-->\n");
2323 assert( hIC->hdr.htype == WH_HINIT );
2325 hIC->hdr.dwContext = dwContext;
2327 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2328 if (NULL == lpwhs)
2330 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2331 goto lerror;
2335 * According to my tests. The name is not resolved until a request is sent
2338 lpwhs->hdr.htype = WH_HHTTPSESSION;
2339 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
2340 lpwhs->hdr.dwFlags = dwFlags;
2341 lpwhs->hdr.dwContext = dwContext;
2342 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
2343 lpwhs->hdr.dwRefCount = 1;
2344 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2345 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2347 handle = WININET_AllocHandle( &lpwhs->hdr );
2348 if (NULL == handle)
2350 ERR("Failed to alloc handle\n");
2351 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2352 goto lerror;
2355 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2356 if(strchrW(hIC->lpszProxy, ' '))
2357 FIXME("Several proxies not implemented.\n");
2358 if(hIC->lpszProxyBypass)
2359 FIXME("Proxy bypass is ignored.\n");
2361 if (NULL != lpszServerName)
2363 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2364 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2366 if (NULL != lpszUserName)
2367 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2368 lpwhs->nServerPort = nServerPort;
2369 lpwhs->nHostPort = nServerPort;
2371 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2372 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2374 INTERNET_SendCallback(&hIC->hdr, dwContext,
2375 INTERNET_STATUS_HANDLE_CREATED, &handle,
2376 sizeof(handle));
2379 bSuccess = TRUE;
2381 lerror:
2382 if( lpwhs )
2383 WININET_Release( &lpwhs->hdr );
2386 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2387 * windows
2390 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2391 return handle;
2395 /***********************************************************************
2396 * HTTP_OpenConnection (internal)
2398 * Connect to a web server
2400 * RETURNS
2402 * TRUE on success
2403 * FALSE on failure
2405 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2407 BOOL bSuccess = FALSE;
2408 LPWININETHTTPSESSIONW lpwhs;
2409 LPWININETAPPINFOW hIC = NULL;
2411 TRACE("-->\n");
2414 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2416 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2417 goto lend;
2420 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2422 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2423 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2424 INTERNET_STATUS_CONNECTING_TO_SERVER,
2425 &(lpwhs->socketAddress),
2426 sizeof(struct sockaddr_in));
2428 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2429 SOCK_STREAM, 0))
2431 WARN("Socket creation failed\n");
2432 goto lend;
2435 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2436 sizeof(lpwhs->socketAddress)))
2437 goto lend;
2439 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2441 /* Note: we differ from Microsoft's WinINet here. they seem to have
2442 * a bug that causes no status callbacks to be sent when starting
2443 * a tunnel to a proxy server using the CONNECT verb. i believe our
2444 * behaviour to be more correct and to not cause any incompatibilities
2445 * because using a secure connection through a proxy server is a rare
2446 * case that would be hard for anyone to depend on */
2447 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2448 goto lend;
2450 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2452 WARN("Couldn't connect securely to host\n");
2453 goto lend;
2457 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2458 INTERNET_STATUS_CONNECTED_TO_SERVER,
2459 &(lpwhs->socketAddress),
2460 sizeof(struct sockaddr_in));
2462 bSuccess = TRUE;
2464 lend:
2465 TRACE("%d <--\n", bSuccess);
2466 return bSuccess;
2470 /***********************************************************************
2471 * HTTP_clear_response_headers (internal)
2473 * clear out any old response headers
2475 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2477 DWORD i;
2479 for( i=0; i<lpwhr->nCustHeaders; i++)
2481 if( !lpwhr->pCustHeaders[i].lpszField )
2482 continue;
2483 if( !lpwhr->pCustHeaders[i].lpszValue )
2484 continue;
2485 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2486 continue;
2487 HTTP_DeleteCustomHeader( lpwhr, i );
2488 i--;
2492 /***********************************************************************
2493 * HTTP_GetResponseHeaders (internal)
2495 * Read server response
2497 * RETURNS
2499 * TRUE on success
2500 * FALSE on error
2502 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2504 INT cbreaks = 0;
2505 WCHAR buffer[MAX_REPLY_LEN];
2506 DWORD buflen = MAX_REPLY_LEN;
2507 BOOL bSuccess = FALSE;
2508 INT rc = 0;
2509 static const WCHAR szCrLf[] = {'\r','\n',0};
2510 char bufferA[MAX_REPLY_LEN];
2511 LPWSTR status_code, status_text;
2512 DWORD cchMaxRawHeaders = 1024;
2513 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2514 DWORD cchRawHeaders = 0;
2516 TRACE("-->\n");
2518 /* clear old response headers (eg. from a redirect response) */
2519 HTTP_clear_response_headers( lpwhr );
2521 if (!NETCON_connected(&lpwhr->netConnection))
2522 goto lend;
2525 * HACK peek at the buffer
2527 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2530 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2532 buflen = MAX_REPLY_LEN;
2533 memset(buffer, 0, MAX_REPLY_LEN);
2534 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2535 goto lend;
2536 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2538 /* regenerate raw headers */
2539 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2541 cchMaxRawHeaders *= 2;
2542 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2544 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2545 cchRawHeaders += (buflen-1);
2546 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2547 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2548 lpszRawHeaders[cchRawHeaders] = '\0';
2550 /* split the version from the status code */
2551 status_code = strchrW( buffer, ' ' );
2552 if( !status_code )
2553 goto lend;
2554 *status_code++=0;
2556 /* split the status code from the status text */
2557 status_text = strchrW( status_code, ' ' );
2558 if( !status_text )
2559 goto lend;
2560 *status_text++=0;
2562 TRACE("version [%s] status code [%s] status text [%s]\n",
2563 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2565 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
2566 HTTP_ADDHDR_FLAG_REPLACE);
2568 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
2569 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
2571 lpwhr->lpszVersion= WININET_strdupW(buffer);
2572 lpwhr->lpszStatusText = WININET_strdupW(status_text);
2574 /* Parse each response line */
2577 buflen = MAX_REPLY_LEN;
2578 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2580 LPWSTR * pFieldAndValue;
2582 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2583 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2585 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2587 cchMaxRawHeaders *= 2;
2588 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2590 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2591 cchRawHeaders += (buflen-1);
2592 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2593 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2594 lpszRawHeaders[cchRawHeaders] = '\0';
2596 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2597 if (!pFieldAndValue)
2598 break;
2600 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2601 HTTP_ADDREQ_FLAG_ADD );
2603 HTTP_FreeTokens(pFieldAndValue);
2605 else
2607 cbreaks++;
2608 if (cbreaks >= 2)
2609 break;
2611 }while(1);
2613 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2614 lpwhr->lpszRawHeaders = lpszRawHeaders;
2615 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2616 bSuccess = TRUE;
2618 lend:
2620 TRACE("<--\n");
2621 if (bSuccess)
2622 return rc;
2623 else
2624 return 0;
2628 static void strip_spaces(LPWSTR start)
2630 LPWSTR str = start;
2631 LPWSTR end;
2633 while (*str == ' ' && *str != '\0')
2634 str++;
2636 if (str != start)
2637 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2639 end = start + strlenW(start) - 1;
2640 while (end >= start && *end == ' ')
2642 *end = '\0';
2643 end--;
2648 /***********************************************************************
2649 * HTTP_InterpretHttpHeader (internal)
2651 * Parse server response
2653 * RETURNS
2655 * Pointer to array of field, value, NULL on success.
2656 * NULL on error.
2658 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2660 LPWSTR * pTokenPair;
2661 LPWSTR pszColon;
2662 INT len;
2664 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2666 pszColon = strchrW(buffer, ':');
2667 /* must have two tokens */
2668 if (!pszColon)
2670 HTTP_FreeTokens(pTokenPair);
2671 if (buffer[0])
2672 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2673 return NULL;
2676 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2677 if (!pTokenPair[0])
2679 HTTP_FreeTokens(pTokenPair);
2680 return NULL;
2682 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2683 pTokenPair[0][pszColon - buffer] = '\0';
2685 /* skip colon */
2686 pszColon++;
2687 len = strlenW(pszColon);
2688 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2689 if (!pTokenPair[1])
2691 HTTP_FreeTokens(pTokenPair);
2692 return NULL;
2694 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2696 strip_spaces(pTokenPair[0]);
2697 strip_spaces(pTokenPair[1]);
2699 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2700 return pTokenPair;
2703 /***********************************************************************
2704 * HTTP_ProcessHeader (internal)
2706 * Stuff header into header tables according to <dwModifier>
2710 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2712 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2714 LPHTTPHEADERW lphttpHdr = NULL;
2715 BOOL bSuccess = FALSE;
2716 INT index = -1;
2717 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2718 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
2720 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2722 /* Don't let applications add Connection header to request */
2723 if (strcmpW(szConnection,field)==0 && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2725 return FALSE;
2728 /* REPLACE wins out over ADD */
2729 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2730 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
2732 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
2733 index = -1;
2734 else
2735 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
2737 if (index >= 0)
2739 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2741 return FALSE;
2743 lphttpHdr = &lpwhr->pCustHeaders[index];
2745 else if (value)
2747 HTTPHEADERW hdr;
2749 hdr.lpszField = (LPWSTR)field;
2750 hdr.lpszValue = (LPWSTR)value;
2751 hdr.wFlags = hdr.wCount = 0;
2753 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2754 hdr.wFlags |= HDR_ISREQUEST;
2756 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2759 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2760 lphttpHdr->wFlags |= HDR_ISREQUEST;
2761 else
2762 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2764 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2766 HTTP_DeleteCustomHeader( lpwhr, index );
2768 if (value)
2770 HTTPHEADERW hdr;
2772 hdr.lpszField = (LPWSTR)field;
2773 hdr.lpszValue = (LPWSTR)value;
2774 hdr.wFlags = hdr.wCount = 0;
2776 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2777 hdr.wFlags |= HDR_ISREQUEST;
2779 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2782 return TRUE;
2784 else if (dwModifier & COALESCEFLASG)
2786 LPWSTR lpsztmp;
2787 WCHAR ch = 0;
2788 INT len = 0;
2789 INT origlen = strlenW(lphttpHdr->lpszValue);
2790 INT valuelen = strlenW(value);
2792 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2794 ch = ',';
2795 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2797 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2799 ch = ';';
2800 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2803 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
2805 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2806 if (lpsztmp)
2808 lphttpHdr->lpszValue = lpsztmp;
2809 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2810 if (ch > 0)
2812 lphttpHdr->lpszValue[origlen] = ch;
2813 origlen++;
2814 lphttpHdr->lpszValue[origlen] = ' ';
2815 origlen++;
2818 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2819 lphttpHdr->lpszValue[len] = '\0';
2820 bSuccess = TRUE;
2822 else
2824 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2825 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2828 TRACE("<-- %d\n",bSuccess);
2829 return bSuccess;
2833 /***********************************************************************
2834 * HTTP_CloseConnection (internal)
2836 * Close socket connection
2839 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2841 LPWININETHTTPSESSIONW lpwhs = NULL;
2842 LPWININETAPPINFOW hIC = NULL;
2844 TRACE("%p\n",lpwhr);
2846 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2847 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2849 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2850 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2852 if (NETCON_connected(&lpwhr->netConnection))
2854 NETCON_close(&lpwhr->netConnection);
2857 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2858 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2862 /***********************************************************************
2863 * HTTP_CloseHTTPRequestHandle (internal)
2865 * Deallocate request handle
2868 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2870 DWORD i;
2871 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2873 TRACE("\n");
2875 if (NETCON_connected(&lpwhr->netConnection))
2876 HTTP_CloseConnection(lpwhr);
2878 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2879 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2880 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2881 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
2882 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
2884 for (i = 0; i < lpwhr->nCustHeaders; i++)
2886 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2887 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2890 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2891 HeapFree(GetProcessHeap(), 0, lpwhr);
2895 /***********************************************************************
2896 * HTTP_CloseHTTPSessionHandle (internal)
2898 * Deallocate session handle
2901 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
2903 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
2905 TRACE("%p\n", lpwhs);
2907 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2908 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2909 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2910 HeapFree(GetProcessHeap(), 0, lpwhs);
2914 /***********************************************************************
2915 * HTTP_GetCustomHeaderIndex (internal)
2917 * Return index of custom header from header array
2920 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,int requested_index, BOOL request_only)
2922 DWORD index;
2924 TRACE("%s\n", debugstr_w(lpszField));
2926 for (index = 0; index < lpwhr->nCustHeaders; index++)
2928 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2930 if ((request_only &&
2931 !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))||
2932 (!request_only &&
2933 (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST)))
2934 continue;
2936 if (requested_index == 0)
2937 break;
2938 else
2939 requested_index --;
2943 if (index >= lpwhr->nCustHeaders)
2944 index = -1;
2946 TRACE("Return: %ld\n", index);
2947 return index;
2951 /***********************************************************************
2952 * HTTP_InsertCustomHeader (internal)
2954 * Insert header into array
2957 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2959 INT count;
2960 LPHTTPHEADERW lph = NULL;
2961 BOOL r = FALSE;
2963 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2964 count = lpwhr->nCustHeaders + 1;
2965 if (count > 1)
2966 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2967 else
2968 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2970 if (NULL != lph)
2972 lpwhr->pCustHeaders = lph;
2973 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2974 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2975 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2976 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2977 lpwhr->nCustHeaders++;
2978 r = TRUE;
2980 else
2982 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2985 return r;
2989 /***********************************************************************
2990 * HTTP_DeleteCustomHeader (internal)
2992 * Delete header from array
2993 * If this function is called, the indexs may change.
2995 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
2997 if( lpwhr->nCustHeaders <= 0 )
2998 return FALSE;
2999 if( index >= lpwhr->nCustHeaders )
3000 return FALSE;
3001 lpwhr->nCustHeaders--;
3003 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3004 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3005 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3007 return TRUE;
3010 /***********************************************************************
3011 * IsHostInProxyBypassList (@)
3013 * Undocumented
3016 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3018 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);
3019 return FALSE;