Delete all Trailing spaces in code.
[reactos.git] / reactos / dll / win32 / wininet / http.c
1 /*
2 * Wininet - Http Implementation
3 *
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
8 * Copyright 2005 Aric Stewart for CodeWeavers
9 *
10 * Ulrich Czekalla
11 * David Hammerton
12 *
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.
17 *
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.
22 *
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
26 */
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
34 #endif
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
37 #endif
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #include <time.h>
45 #include <assert.h>
46
47 #include "windef.h"
48 #include "winbase.h"
49 #include "wininet.h"
50 #include "winreg.h"
51 #include "winerror.h"
52 #define NO_SHLWAPI_STREAM
53 #define NO_SHLWAPI_REG
54 #define NO_SHLWAPI_STRFCNS
55 #define NO_SHLWAPI_GDI
56 #include "shlwapi.h"
57
58 #include "internet.h"
59 #include "wine/debug.h"
60 #include "wine/unicode.h"
61
62 #include "inet_ntop.c"
63
64 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
65
66 static const WCHAR g_szHttp1_0[] = {' ','H','T','T','P','/','1','.','0',0 };
67 static const WCHAR g_szHttp1_1[] = {' ','H','T','T','P','/','1','.','1',0 };
68 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
69 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
70 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
71 static const WCHAR szHost[] = { 'H','o','s','t',0 };
72 static const WCHAR szProxy_Authorization[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szStatus[] = { 'S','t','a','t','u','s',0 };
74
75 #define MAXHOSTNAME 100
76 #define MAX_FIELD_VALUE_LEN 256
77 #define MAX_FIELD_LEN 256
78
79 #define HTTP_REFERER g_szReferer
80 #define HTTP_ACCEPT g_szAccept
81 #define HTTP_USERAGENT g_szUserAgent
82
83 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
84 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
85 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
86 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
87 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
88 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
89 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
90
91
92 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr);
93 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr);
94 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
95 static BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
96 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
97 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer);
98 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
99 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField, INT index, BOOL Request);
100 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index);
101 static LPWSTR HTTP_build_req( LPCWSTR *list, int len );
102 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
103 LPCWSTR username, LPCWSTR password );
104 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD
105 dwInfoLevel, LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD
106 lpdwIndex);
107 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl,
108 LPCWSTR lpszHeaders, DWORD dwHeaderLength, LPVOID lpOptional, DWORD
109 dwOptionalLength, DWORD dwContentLength);
110
111
112 LPHTTPHEADERW HTTP_GetHeader(LPWININETHTTPREQW req, LPCWSTR head)
113 {
114 int HeaderIndex = 0;
115 HeaderIndex = HTTP_GetCustomHeaderIndex(req, head, 0, TRUE);
116 if (HeaderIndex == -1)
117 return NULL;
118 else
119 return &req->pCustHeaders[HeaderIndex];
120 }
121
122 /***********************************************************************
123 * HTTP_Tokenize (internal)
124 *
125 * Tokenize a string, allocating memory for the tokens.
126 */
127 static LPWSTR * HTTP_Tokenize(LPCWSTR string, LPCWSTR token_string)
128 {
129 LPWSTR * token_array;
130 int tokens = 0;
131 int i;
132 LPCWSTR next_token;
133
134 /* empty string has no tokens */
135 if (*string)
136 tokens++;
137 /* count tokens */
138 for (i = 0; string[i]; i++)
139 if (!strncmpW(string+i, token_string, strlenW(token_string)))
140 {
141 DWORD j;
142 tokens++;
143 /* we want to skip over separators, but not the null terminator */
144 for (j = 0; j < strlenW(token_string) - 1; j++)
145 if (!string[i+j])
146 break;
147 i += j;
148 }
149
150 /* add 1 for terminating NULL */
151 token_array = HeapAlloc(GetProcessHeap(), 0, (tokens+1) * sizeof(*token_array));
152 token_array[tokens] = NULL;
153 if (!tokens)
154 return token_array;
155 for (i = 0; i < tokens; i++)
156 {
157 int len;
158 next_token = strstrW(string, token_string);
159 if (!next_token) next_token = string+strlenW(string);
160 len = next_token - string;
161 token_array[i] = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
162 memcpy(token_array[i], string, len*sizeof(WCHAR));
163 token_array[i][len] = '\0';
164 string = next_token+strlenW(token_string);
165 }
166 return token_array;
167 }
168
169 /***********************************************************************
170 * HTTP_FreeTokens (internal)
171 *
172 * Frees memory returned from HTTP_Tokenize.
173 */
174 static void HTTP_FreeTokens(LPWSTR * token_array)
175 {
176 int i;
177 for (i = 0; token_array[i]; i++)
178 HeapFree(GetProcessHeap(), 0, token_array[i]);
179 HeapFree(GetProcessHeap(), 0, token_array);
180 }
181
182 /* **********************************************************************
183 *
184 * Helper functions for the HttpSendRequest(Ex) functions
185 *
186 */
187 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr )
188 {
189 /* if the verb is NULL default to GET */
190 if (NULL == lpwhr->lpszVerb)
191 {
192 static const WCHAR szGET[] = { 'G','E','T', 0 };
193 lpwhr->lpszVerb = WININET_strdupW(szGET);
194 }
195 }
196
197 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr)
198 {
199 static const WCHAR szSlash[] = { '/',0 };
200 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
201
202 /* If we don't have a path we set it to root */
203 if (NULL == lpwhr->lpszPath)
204 lpwhr->lpszPath = WININET_strdupW(szSlash);
205 else /* remove \r and \n*/
206 {
207 int nLen = strlenW(lpwhr->lpszPath);
208 while ((nLen >0 ) && ((lpwhr->lpszPath[nLen-1] == '\r')||(lpwhr->lpszPath[nLen-1] == '\n')))
209 {
210 nLen--;
211 lpwhr->lpszPath[nLen]='\0';
212 }
213 /* Replace '\' with '/' */
214 while (nLen>0) {
215 nLen--;
216 if (lpwhr->lpszPath[nLen] == '\\') lpwhr->lpszPath[nLen]='/';
217 }
218 }
219
220 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
221 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
222 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
223 {
224 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
225 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
226 *fixurl = '/';
227 strcpyW(fixurl + 1, lpwhr->lpszPath);
228 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
229 lpwhr->lpszPath = fixurl;
230 }
231 }
232
233 static LPWSTR HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr, LPCWSTR verb, LPCWSTR path, BOOL http1_1 )
234 {
235 LPWSTR requestString;
236 DWORD len, n;
237 LPCWSTR *req;
238 INT i;
239 LPWSTR p;
240
241 static const WCHAR szSpace[] = { ' ',0 };
242 static const WCHAR szcrlf[] = {'\r','\n', 0};
243 static const WCHAR szColon[] = { ':',' ',0 };
244 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
245
246 /* allocate space for an array of all the string pointers to be added */
247 len = (lpwhr->nCustHeaders)*4 + 9;
248 req = HeapAlloc( GetProcessHeap(), 0, len*sizeof(LPCWSTR) );
249
250 /* add the verb, path and HTTP version string */
251 n = 0;
252 req[n++] = verb;
253 req[n++] = szSpace;
254 req[n++] = path;
255 req[n++] = http1_1 ? g_szHttp1_1 : g_szHttp1_0;
256
257 /* Append custom request heades */
258 for (i = 0; i < lpwhr->nCustHeaders; i++)
259 {
260 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
261 {
262 req[n++] = szcrlf;
263 req[n++] = lpwhr->pCustHeaders[i].lpszField;
264 req[n++] = szColon;
265 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
266
267 TRACE("Adding custom header %s (%s)\n",
268 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
269 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
270 }
271 }
272
273 if( n >= len )
274 ERR("oops. buffer overrun\n");
275
276 req[n] = NULL;
277 requestString = HTTP_build_req( req, 4 );
278 HeapFree( GetProcessHeap(), 0, req );
279
280 /*
281 * Set (header) termination string for request
282 * Make sure there's exactly two new lines at the end of the request
283 */
284 p = &requestString[strlenW(requestString)-1];
285 while ( (*p == '\n') || (*p == '\r') )
286 p--;
287 strcpyW( p+1, sztwocrlf );
288
289 return requestString;
290 }
291
292 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr )
293 {
294 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
295 int HeaderIndex;
296 LPHTTPHEADERW setCookieHeader;
297
298 HeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSet_Cookie, 0, FALSE);
299 if (HeaderIndex == -1)
300 return;
301 setCookieHeader = &lpwhr->pCustHeaders[HeaderIndex];
302
303 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) && setCookieHeader->lpszValue)
304 {
305 int nPosStart = 0, nPosEnd = 0, len;
306 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
307
308 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
309 {
310 LPWSTR buf_cookie, cookie_name, cookie_data;
311 LPWSTR buf_url;
312 LPWSTR domain = NULL;
313 LPHTTPHEADERW Host;
314
315 int nEqualPos = 0;
316 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
317 setCookieHeader->lpszValue[nPosEnd] != '\0')
318 {
319 nPosEnd++;
320 }
321 if (setCookieHeader->lpszValue[nPosEnd] == ';')
322 {
323 /* fixme: not case sensitive, strcasestr is gnu only */
324 int nDomainPosEnd = 0;
325 int nDomainPosStart = 0, nDomainLength = 0;
326 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
327 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
328 if (lpszDomain)
329 { /* they have specified their own domain, lets use it */
330 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
331 lpszDomain[nDomainPosEnd] != '\0')
332 {
333 nDomainPosEnd++;
334 }
335 nDomainPosStart = strlenW(szDomain);
336 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
337 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
338 lstrcpynW(domain, &lpszDomain[nDomainPosStart], nDomainLength + 1);
339 }
340 }
341 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
342 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
343 lstrcpynW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart) + 1);
344 TRACE("%s\n", debugstr_w(buf_cookie));
345 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
346 {
347 nEqualPos++;
348 }
349 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
350 {
351 HeapFree(GetProcessHeap(), 0, buf_cookie);
352 break;
353 }
354
355 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
356 lstrcpynW(cookie_name, buf_cookie, nEqualPos + 1);
357 cookie_data = &buf_cookie[nEqualPos + 1];
358
359 Host = HTTP_GetHeader(lpwhr,szHost);
360 len = lstrlenW((domain ? domain : (Host?Host->lpszValue:NULL))) +
361 strlenW(lpwhr->lpszPath) + 9;
362 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
363 sprintfW(buf_url, szFmt, (domain ? domain : (Host?Host->lpszValue:NULL))); /* FIXME PATH!!! */
364 InternetSetCookieW(buf_url, cookie_name, cookie_data);
365
366 HeapFree(GetProcessHeap(), 0, buf_url);
367 HeapFree(GetProcessHeap(), 0, buf_cookie);
368 HeapFree(GetProcessHeap(), 0, cookie_name);
369 HeapFree(GetProcessHeap(), 0, domain);
370 nPosStart = nPosEnd;
371 }
372 }
373 }
374
375 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr )
376 {
377 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
378 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW)lpwhs->hdr.lpwhparent;
379
380 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
381 assert(hIC->hdr.htype == WH_HINIT);
382
383 if (hIC && (hIC->lpszProxyUsername || hIC->lpszProxyPassword ))
384 HTTP_InsertProxyAuthorization(lpwhr, hIC->lpszProxyUsername,
385 hIC->lpszProxyPassword);
386 }
387
388 /***********************************************************************
389 * HTTP_HttpAddRequestHeadersW (internal)
390 */
391 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
392 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
393 {
394 LPWSTR lpszStart;
395 LPWSTR lpszEnd;
396 LPWSTR buffer;
397 BOOL bSuccess = FALSE;
398 DWORD len;
399
400 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
401
402 if( dwHeaderLength == ~0U )
403 len = strlenW(lpszHeader);
404 else
405 len = dwHeaderLength;
406 buffer = HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR)*(len+1) );
407 lstrcpynW( buffer, lpszHeader, len + 1);
408
409 lpszStart = buffer;
410
411 do
412 {
413 LPWSTR * pFieldAndValue;
414
415 lpszEnd = lpszStart;
416
417 while (*lpszEnd != '\0')
418 {
419 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
420 break;
421 lpszEnd++;
422 }
423
424 if (*lpszStart == '\0')
425 break;
426
427 if (*lpszEnd == '\r')
428 {
429 *lpszEnd = '\0';
430 lpszEnd += 2; /* Jump over \r\n */
431 }
432 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
433 pFieldAndValue = HTTP_InterpretHttpHeader(lpszStart);
434 if (pFieldAndValue)
435 {
436 bSuccess = HTTP_ProcessHeader(lpwhr, pFieldAndValue[0],
437 pFieldAndValue[1], dwModifier | HTTP_ADDHDR_FLAG_REQ);
438 HTTP_FreeTokens(pFieldAndValue);
439 }
440
441 lpszStart = lpszEnd;
442 } while (bSuccess);
443
444 HeapFree(GetProcessHeap(), 0, buffer);
445
446 return bSuccess;
447 }
448
449 /***********************************************************************
450 * HttpAddRequestHeadersW (WININET.@)
451 *
452 * Adds one or more HTTP header to the request handler
453 *
454 * RETURNS
455 * TRUE on success
456 * FALSE on failure
457 *
458 */
459 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
460 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
461 {
462 BOOL bSuccess = FALSE;
463 LPWININETHTTPREQW lpwhr;
464
465 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
466 dwModifier);
467
468 if (!lpszHeader)
469 return TRUE;
470
471 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
472 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
473 {
474 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
475 goto lend;
476 }
477 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
478 lend:
479 if( lpwhr )
480 WININET_Release( &lpwhr->hdr );
481
482 return bSuccess;
483 }
484
485 /***********************************************************************
486 * HttpAddRequestHeadersA (WININET.@)
487 *
488 * Adds one or more HTTP header to the request handler
489 *
490 * RETURNS
491 * TRUE on success
492 * FALSE on failure
493 *
494 */
495 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
496 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
497 {
498 DWORD len;
499 LPWSTR hdr;
500 BOOL r;
501
502 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
503 dwModifier);
504
505 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
506 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
507 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
508 if( dwHeaderLength != ~0U )
509 dwHeaderLength = len;
510
511 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
512
513 HeapFree( GetProcessHeap(), 0, hdr );
514
515 return r;
516 }
517
518 /***********************************************************************
519 * HttpEndRequestA (WININET.@)
520 *
521 * Ends an HTTP request that was started by HttpSendRequestEx
522 *
523 * RETURNS
524 * TRUE if successful
525 * FALSE on failure
526 *
527 */
528 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest,
529 LPINTERNET_BUFFERSA lpBuffersOut, DWORD dwFlags, DWORD dwContext)
530 {
531 LPINTERNET_BUFFERSA ptr;
532 LPINTERNET_BUFFERSW lpBuffersOutW,ptrW;
533 BOOL rc = FALSE;
534
535 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersOut, dwFlags,
536 dwContext);
537
538 ptr = lpBuffersOut;
539 if (ptr)
540 lpBuffersOutW = (LPINTERNET_BUFFERSW)HeapAlloc(GetProcessHeap(),
541 HEAP_ZERO_MEMORY, sizeof(INTERNET_BUFFERSW));
542 else
543 lpBuffersOutW = NULL;
544
545 ptrW = lpBuffersOutW;
546 while (ptr)
547 {
548 if (ptr->lpvBuffer && ptr->dwBufferLength)
549 ptrW->lpvBuffer = HeapAlloc(GetProcessHeap(),0,ptr->dwBufferLength);
550 ptrW->dwBufferLength = ptr->dwBufferLength;
551 ptrW->dwBufferTotal= ptr->dwBufferTotal;
552
553 if (ptr->Next)
554 ptrW->Next = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,
555 sizeof(INTERNET_BUFFERSW));
556
557 ptr = ptr->Next;
558 ptrW = ptrW->Next;
559 }
560
561 rc = HttpEndRequestW(hRequest, lpBuffersOutW, dwFlags, dwContext);
562
563 if (lpBuffersOutW)
564 {
565 ptrW = lpBuffersOutW;
566 while (ptrW)
567 {
568 LPINTERNET_BUFFERSW ptrW2;
569
570 FIXME("Do we need to translate info out of these buffer?\n");
571
572 HeapFree(GetProcessHeap(),0,(LPVOID)ptrW->lpvBuffer);
573 ptrW2 = ptrW->Next;
574 HeapFree(GetProcessHeap(),0,ptrW);
575 ptrW = ptrW2;
576 }
577 }
578
579 return rc;
580 }
581
582 /***********************************************************************
583 * HttpEndRequestW (WININET.@)
584 *
585 * Ends an HTTP request that was started by HttpSendRequestEx
586 *
587 * RETURNS
588 * TRUE if successful
589 * FALSE on failure
590 *
591 */
592 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest,
593 LPINTERNET_BUFFERSW lpBuffersOut, DWORD dwFlags, DWORD dwContext)
594 {
595 BOOL rc = FALSE;
596 LPWININETHTTPREQW lpwhr;
597 INT responseLen;
598
599 TRACE("-->\n");
600 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
601
602 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
603 {
604 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
605 return FALSE;
606 }
607
608 lpwhr->hdr.dwFlags |= dwFlags;
609 lpwhr->hdr.dwContext = dwContext;
610
611 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
612 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
613
614 responseLen = HTTP_GetResponseHeaders(lpwhr);
615 if (responseLen)
616 rc = TRUE;
617
618 SendAsyncCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
619 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen, sizeof(DWORD));
620
621 /* process headers here. Is this right? */
622 HTTP_ProcessHeaders(lpwhr);
623
624 /* We appear to do nothing with the buffer.. is that correct? */
625
626 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT))
627 {
628 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
629 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
630 (dwCode==302 || dwCode==301))
631 {
632 WCHAR szNewLocation[2048];
633 DWORD dwBufferSize=2048;
634 dwIndex=0;
635 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
636 {
637 static const WCHAR szGET[] = { 'G','E','T', 0 };
638 /* redirects are always GETs */
639 HeapFree(GetProcessHeap(),0,lpwhr->lpszVerb);
640 lpwhr->lpszVerb = WININET_strdupW(szGET);
641 return HTTP_HandleRedirect(lpwhr, szNewLocation, NULL, 0, NULL, 0, 0);
642 }
643 }
644 }
645
646 TRACE("%i <--\n",rc);
647 return rc;
648 }
649
650 /***********************************************************************
651 * HttpOpenRequestW (WININET.@)
652 *
653 * Open a HTTP request handle
654 *
655 * RETURNS
656 * HINTERNET a HTTP request handle on success
657 * NULL on failure
658 *
659 */
660 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
661 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
662 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
663 DWORD dwFlags, DWORD dwContext)
664 {
665 LPWININETHTTPSESSIONW lpwhs;
666 HINTERNET handle = NULL;
667
668 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
669 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
670 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
671 dwFlags, dwContext);
672 if(lpszAcceptTypes!=NULL)
673 {
674 int i;
675 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
676 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
677 }
678
679 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
680 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
681 {
682 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
683 goto lend;
684 }
685
686 /*
687 * My tests seem to show that the windows version does not
688 * become asynchronous until after this point. And anyhow
689 * if this call was asynchronous then how would you get the
690 * necessary HINTERNET pointer returned by this function.
691 *
692 */
693 handle = HTTP_HttpOpenRequestW(lpwhs, lpszVerb, lpszObjectName,
694 lpszVersion, lpszReferrer, lpszAcceptTypes,
695 dwFlags, dwContext);
696 lend:
697 if( lpwhs )
698 WININET_Release( &lpwhs->hdr );
699 TRACE("returning %p\n", handle);
700 return handle;
701 }
702
703
704 /***********************************************************************
705 * HttpOpenRequestA (WININET.@)
706 *
707 * Open a HTTP request handle
708 *
709 * RETURNS
710 * HINTERNET a HTTP request handle on success
711 * NULL on failure
712 *
713 */
714 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
715 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
716 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
717 DWORD dwFlags, DWORD dwContext)
718 {
719 LPWSTR szVerb = NULL, szObjectName = NULL;
720 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
721 INT len;
722 INT acceptTypesCount;
723 HINTERNET rc = FALSE;
724 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
725 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
726 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
727 dwFlags, dwContext);
728
729 if (lpszVerb)
730 {
731 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
732 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
733 if ( !szVerb )
734 goto end;
735 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
736 }
737
738 if (lpszObjectName)
739 {
740 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
741 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
742 if ( !szObjectName )
743 goto end;
744 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
745 }
746
747 if (lpszVersion)
748 {
749 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
750 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
751 if ( !szVersion )
752 goto end;
753 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
754 }
755
756 if (lpszReferrer)
757 {
758 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
759 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
760 if ( !szReferrer )
761 goto end;
762 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
763 }
764
765 acceptTypesCount = 0;
766 if (lpszAcceptTypes)
767 {
768 /* find out how many there are */
769 while (lpszAcceptTypes[acceptTypesCount])
770 acceptTypesCount++;
771 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
772 acceptTypesCount = 0;
773 while (lpszAcceptTypes[acceptTypesCount])
774 {
775 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
776 -1, NULL, 0 );
777 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
778 if (!szAcceptTypes[acceptTypesCount] )
779 goto end;
780 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
781 -1, szAcceptTypes[acceptTypesCount], len );
782 acceptTypesCount++;
783 }
784 szAcceptTypes[acceptTypesCount] = NULL;
785 }
786 else szAcceptTypes = 0;
787
788 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
789 szVersion, szReferrer,
790 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
791
792 end:
793 if (szAcceptTypes)
794 {
795 acceptTypesCount = 0;
796 while (szAcceptTypes[acceptTypesCount])
797 {
798 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
799 acceptTypesCount++;
800 }
801 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
802 }
803 HeapFree(GetProcessHeap(), 0, szReferrer);
804 HeapFree(GetProcessHeap(), 0, szVersion);
805 HeapFree(GetProcessHeap(), 0, szObjectName);
806 HeapFree(GetProcessHeap(), 0, szVerb);
807
808 return rc;
809 }
810
811 /***********************************************************************
812 * HTTP_Base64
813 */
814 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
815 {
816 UINT n = 0, x;
817 static LPCSTR HTTP_Base64Enc =
818 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
819
820 while( bin[0] )
821 {
822 /* first 6 bits, all from bin[0] */
823 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
824 x = (bin[0] & 3) << 4;
825
826 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
827 if( !bin[1] )
828 {
829 base64[n++] = HTTP_Base64Enc[x];
830 base64[n++] = '=';
831 base64[n++] = '=';
832 break;
833 }
834 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
835 x = ( bin[1] & 0x0f ) << 2;
836
837 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
838 if( !bin[2] )
839 {
840 base64[n++] = HTTP_Base64Enc[x];
841 base64[n++] = '=';
842 break;
843 }
844 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
845
846 /* last 6 bits, all from bin [2] */
847 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
848 bin += 3;
849 }
850 base64[n] = 0;
851 return n;
852 }
853
854 /***********************************************************************
855 * HTTP_EncodeBasicAuth
856 *
857 * Encode the basic authentication string for HTTP 1.1
858 */
859 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
860 {
861 UINT len;
862 LPWSTR in, out;
863 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
864 static const WCHAR szColon[] = {':',0};
865
866 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
867 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
868 if( !in )
869 return NULL;
870
871 len = lstrlenW(szBasic) +
872 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
873 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
874 if( out )
875 {
876 lstrcpyW( in, username );
877 lstrcatW( in, szColon );
878 lstrcatW( in, password );
879 lstrcpyW( out, szBasic );
880 HTTP_Base64( in, &out[strlenW(out)] );
881 }
882 HeapFree( GetProcessHeap(), 0, in );
883
884 return out;
885 }
886
887 /***********************************************************************
888 * HTTP_InsertProxyAuthorization
889 *
890 * Insert the basic authorization field in the request header
891 */
892 static BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
893 LPCWSTR username, LPCWSTR password )
894 {
895 WCHAR *authorization = HTTP_EncodeBasicAuth( username, password );
896 BOOL ret = TRUE;
897
898 if (!authorization)
899 return FALSE;
900
901 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization ) );
902
903 HTTP_ProcessHeader(lpwhr, szProxy_Authorization, authorization,
904 HTTP_ADDHDR_FLAG_REPLACE);
905
906 HeapFree( GetProcessHeap(), 0, authorization );
907
908 return ret;
909 }
910
911 /***********************************************************************
912 * HTTP_DealWithProxy
913 */
914 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
915 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
916 {
917 WCHAR buf[MAXHOSTNAME];
918 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
919 WCHAR* url;
920 static const WCHAR szNul[] = { 0 };
921 URL_COMPONENTSW UrlComponents;
922 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
923 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
924 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
925 int len;
926
927 memset( &UrlComponents, 0, sizeof UrlComponents );
928 UrlComponents.dwStructSize = sizeof UrlComponents;
929 UrlComponents.lpszHostName = buf;
930 UrlComponents.dwHostNameLength = MAXHOSTNAME;
931
932 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
933 hIC->lpszProxy,strlenW(szHttp),szHttp,strlenW(szHttp)) )
934 sprintfW(proxy, szFormat1, hIC->lpszProxy);
935 else
936 strcpyW(proxy, hIC->lpszProxy);
937 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
938 return FALSE;
939 if( UrlComponents.dwHostNameLength == 0 )
940 return FALSE;
941
942 if( !lpwhr->lpszPath )
943 lpwhr->lpszPath = (LPWSTR)szNul;
944 TRACE("server='%s' path='%s'\n",
945 debugstr_w(lpwhs->lpszHostName), debugstr_w(lpwhr->lpszPath));
946 /* for constant 15 see above */
947 len = strlenW(lpwhs->lpszHostName) + strlenW(lpwhr->lpszPath) + 15;
948 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
949
950 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
951 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
952
953 sprintfW(url, szFormat2, lpwhs->lpszHostName, lpwhs->nHostPort);
954
955 if( lpwhr->lpszPath[0] != '/' )
956 strcatW( url, szSlash );
957 strcatW(url, lpwhr->lpszPath);
958 if(lpwhr->lpszPath != szNul)
959 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
960 lpwhr->lpszPath = url;
961
962 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
963 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
964 lpwhs->nServerPort = UrlComponents.nPort;
965
966 return TRUE;
967 }
968
969 /***********************************************************************
970 * HTTP_HttpOpenRequestW (internal)
971 *
972 * Open a HTTP request handle
973 *
974 * RETURNS
975 * HINTERNET a HTTP request handle on success
976 * NULL on failure
977 *
978 */
979 HINTERNET WINAPI HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs,
980 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
981 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
982 DWORD dwFlags, DWORD dwContext)
983 {
984 LPWININETAPPINFOW hIC = NULL;
985 LPWININETHTTPREQW lpwhr;
986 LPWSTR lpszCookies;
987 LPWSTR lpszUrl = NULL;
988 DWORD nCookieSize;
989 HINTERNET handle = NULL;
990 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
991 DWORD len;
992 LPHTTPHEADERW Host;
993 char szaddr[32];
994
995 TRACE("-->\n");
996
997 assert( lpwhs->hdr.htype == WH_HHTTPSESSION );
998 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
999
1000 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
1001 if (NULL == lpwhr)
1002 {
1003 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1004 goto lend;
1005 }
1006 lpwhr->hdr.htype = WH_HHTTPREQ;
1007 lpwhr->hdr.lpwhparent = WININET_AddRef( &lpwhs->hdr );
1008 lpwhr->hdr.dwFlags = dwFlags;
1009 lpwhr->hdr.dwContext = dwContext;
1010 lpwhr->hdr.dwRefCount = 1;
1011 lpwhr->hdr.destroy = HTTP_CloseHTTPRequestHandle;
1012 lpwhr->hdr.lpfnStatusCB = lpwhs->hdr.lpfnStatusCB;
1013
1014 handle = WININET_AllocHandle( &lpwhr->hdr );
1015 if (NULL == handle)
1016 {
1017 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1018 goto lend;
1019 }
1020
1021 if (!NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE))
1022 {
1023 InternetCloseHandle( handle );
1024 handle = NULL;
1025 goto lend;
1026 }
1027
1028 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
1029 HRESULT rc;
1030
1031 len = 0;
1032 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
1033 if (rc != E_POINTER)
1034 len = strlenW(lpszObjectName)+1;
1035 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1036 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
1037 URL_ESCAPE_SPACES_ONLY);
1038 if (rc)
1039 {
1040 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
1041 strcpyW(lpwhr->lpszPath,lpszObjectName);
1042 }
1043 }
1044
1045 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1046 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
1047
1048 if(lpszAcceptTypes!=NULL)
1049 {
1050 int i;
1051 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
1052 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1053 }
1054
1055 if (NULL == lpszVerb)
1056 {
1057 static const WCHAR szGet[] = {'G','E','T',0};
1058 lpwhr->lpszVerb = WININET_strdupW(szGet);
1059 }
1060 else if (strlenW(lpszVerb))
1061 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
1062
1063 if (NULL != lpszReferrer && strlenW(lpszReferrer))
1064 {
1065 WCHAR buf[MAXHOSTNAME];
1066 URL_COMPONENTSW UrlComponents;
1067
1068 memset( &UrlComponents, 0, sizeof UrlComponents );
1069 UrlComponents.dwStructSize = sizeof UrlComponents;
1070 UrlComponents.lpszHostName = buf;
1071 UrlComponents.dwHostNameLength = MAXHOSTNAME;
1072
1073 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
1074 if (strlenW(UrlComponents.lpszHostName))
1075 HTTP_ProcessHeader(lpwhr, szHost, UrlComponents.lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1076 }
1077 else
1078 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
1079
1080 if (lpwhs->nServerPort == INTERNET_INVALID_PORT_NUMBER)
1081 lpwhs->nServerPort = (dwFlags & INTERNET_FLAG_SECURE ?
1082 INTERNET_DEFAULT_HTTPS_PORT :
1083 INTERNET_DEFAULT_HTTP_PORT);
1084 lpwhs->nHostPort = lpwhs->nServerPort;
1085
1086 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1087 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
1088
1089 if (hIC->lpszAgent)
1090 {
1091 WCHAR *agent_header;
1092 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1093
1094 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
1095 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1096 sprintfW(agent_header, user_agent, hIC->lpszAgent );
1097
1098 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
1099 HTTP_ADDREQ_FLAG_ADD);
1100 HeapFree(GetProcessHeap(), 0, agent_header);
1101 }
1102
1103 Host = HTTP_GetHeader(lpwhr,szHost);
1104
1105 len = lstrlenW(Host->lpszValue) + strlenW(szUrlForm);
1106 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1107 sprintfW( lpszUrl, szUrlForm, Host->lpszValue );
1108
1109 if (!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_COOKIES) &&
1110 InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
1111 {
1112 int cnt = 0;
1113 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
1114 static const WCHAR szcrlf[] = {'\r','\n',0};
1115
1116 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
1117
1118 cnt += sprintfW(lpszCookies, szCookie);
1119 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
1120 strcatW(lpszCookies, szcrlf);
1121
1122 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
1123 HTTP_ADDREQ_FLAG_ADD);
1124 HeapFree(GetProcessHeap(), 0, lpszCookies);
1125 }
1126 HeapFree(GetProcessHeap(), 0, lpszUrl);
1127
1128
1129 INTERNET_SendCallback(&lpwhs->hdr, dwContext,
1130 INTERNET_STATUS_HANDLE_CREATED, &handle,
1131 sizeof(handle));
1132
1133 /*
1134 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1135 */
1136
1137 /*
1138 * According to my tests. The name is not resolved until a request is Opened
1139 */
1140 INTERNET_SendCallback(&lpwhr->hdr, dwContext,
1141 INTERNET_STATUS_RESOLVING_NAME,
1142 lpwhs->lpszServerName,
1143 strlenW(lpwhs->lpszServerName)+1);
1144
1145 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1146 &lpwhs->socketAddress))
1147 {
1148 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1149 InternetCloseHandle( handle );
1150 handle = NULL;
1151 goto lend;
1152 }
1153
1154 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
1155 szaddr, sizeof(szaddr));
1156 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
1157 INTERNET_STATUS_NAME_RESOLVED,
1158 szaddr, strlen(szaddr)+1);
1159
1160 lend:
1161 if( lpwhr )
1162 WININET_Release( &lpwhr->hdr );
1163
1164 TRACE("<-- %p (%p)\n", handle, lpwhr);
1165 return handle;
1166 }
1167
1168 typedef struct std_hdr_data
1169 {
1170 const WCHAR* hdrStr;
1171 INT hdrIndex;
1172 } std_hdr_data;
1173
1174 static const WCHAR szAccept[] = { 'A','c','c','e','p','t',0 };
1175 static const WCHAR szAccept_Charset[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1176 static const WCHAR szAccept_Encoding[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1177 static const WCHAR szAccept_Language[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1178 static const WCHAR szAccept_Ranges[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1179 static const WCHAR szAge[] = { 'A','g','e',0 };
1180 static const WCHAR szAllow[] = { 'A','l','l','o','w',0 };
1181 static const WCHAR szAuthorization[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1182 static const WCHAR szCache_Control[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1183 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1184 static const WCHAR szContent_Base[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1185 static const WCHAR szContent_Encoding[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1186 static const WCHAR szContent_ID[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1187 static const WCHAR szContent_Language[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1188 static const WCHAR szContent_Length[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1189 static const WCHAR szContent_Location[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1190 static const WCHAR szContent_MD5[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1191 static const WCHAR szContent_Range[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1192 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 };
1193 static const WCHAR szContent_Type[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1194 static const WCHAR szCookie[] = { 'C','o','o','k','i','e',0 };
1195 static const WCHAR szDate[] = { 'D','a','t','e',0 };
1196 static const WCHAR szFrom[] = { 'F','r','o','m',0 };
1197 static const WCHAR szETag[] = { 'E','T','a','g',0 };
1198 static const WCHAR szExpect[] = { 'E','x','p','e','c','t',0 };
1199 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
1200 static const WCHAR szIf_Match[] = { 'I','f','-','M','a','t','c','h',0 };
1201 static const WCHAR szIf_Modified_Since[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1202 static const WCHAR szIf_None_Match[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1203 static const WCHAR szIf_Range[] = { 'I','f','-','R','a','n','g','e',0 };
1204 static const WCHAR szIf_Unmodified_Since[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1205 static const WCHAR szLast_Modified[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1206 static const WCHAR szLocation[] = { 'L','o','c','a','t','i','o','n',0 };
1207 static const WCHAR szMax_Forwards[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1208 static const WCHAR szMime_Version[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1209 static const WCHAR szPragma[] = { 'P','r','a','g','m','a',0 };
1210 static const WCHAR szProxy_Authenticate[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1211 static const WCHAR szProxy_Connection[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1212 static const WCHAR szPublic[] = { 'P','u','b','l','i','c',0 };
1213 static const WCHAR szRange[] = { 'R','a','n','g','e',0 };
1214 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0 };
1215 static const WCHAR szRetry_After[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1216 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0 };
1217 static const WCHAR szSet_Cookie[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1218 static const WCHAR szTransfer_Encoding[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1219 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 };
1220 static const WCHAR szUpgrade[] = { 'U','p','g','r','a','d','e',0 };
1221 static const WCHAR szURI[] = { 'U','R','I',0 };
1222 static const WCHAR szUser_Agent[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1223 static const WCHAR szVary[] = { 'V','a','r','y',0 };
1224 static const WCHAR szVia[] = { 'V','i','a',0 };
1225 static const WCHAR szWarning[] = { 'W','a','r','n','i','n','g',0 };
1226 static const WCHAR szWWW_Authenticate[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1227
1228 static const std_hdr_data SORTED_STANDARD_HEADERS[] = {
1229 {szAccept, HTTP_QUERY_ACCEPT,},
1230 {szAccept_Charset, HTTP_QUERY_ACCEPT_CHARSET,},
1231 {szAccept_Encoding, HTTP_QUERY_ACCEPT_ENCODING,},
1232 {szAccept_Language, HTTP_QUERY_ACCEPT_LANGUAGE,},
1233 {szAccept_Ranges, HTTP_QUERY_ACCEPT_RANGES,},
1234 {szAge, HTTP_QUERY_AGE,},
1235 {szAllow, HTTP_QUERY_ALLOW,},
1236 {szAuthorization, HTTP_QUERY_AUTHORIZATION,},
1237 {szCache_Control, HTTP_QUERY_CACHE_CONTROL,},
1238 {szConnection, HTTP_QUERY_CONNECTION,},
1239 {szContent_Base, HTTP_QUERY_CONTENT_BASE,},
1240 {szContent_Encoding, HTTP_QUERY_CONTENT_ENCODING,},
1241 {szContent_ID, HTTP_QUERY_CONTENT_ID,},
1242 {szContent_Language, HTTP_QUERY_CONTENT_LANGUAGE,},
1243 {szContent_Length, HTTP_QUERY_CONTENT_LENGTH,},
1244 {szContent_Location, HTTP_QUERY_CONTENT_LOCATION,},
1245 {szContent_MD5, HTTP_QUERY_CONTENT_MD5,},
1246 {szContent_Range, HTTP_QUERY_CONTENT_RANGE,},
1247 {szContent_Transfer_Encoding,HTTP_QUERY_CONTENT_TRANSFER_ENCODING,},
1248 {szContent_Type, HTTP_QUERY_CONTENT_TYPE,},
1249 {szCookie, HTTP_QUERY_COOKIE,},
1250 {szDate, HTTP_QUERY_DATE,},
1251 {szETag, HTTP_QUERY_ETAG,},
1252 {szExpect, HTTP_QUERY_EXPECT,},
1253 {szExpires, HTTP_QUERY_EXPIRES,},
1254 {szFrom, HTTP_QUERY_DERIVED_FROM,},
1255 {szHost, HTTP_QUERY_HOST,},
1256 {szIf_Match, HTTP_QUERY_IF_MATCH,},
1257 {szIf_Modified_Since, HTTP_QUERY_IF_MODIFIED_SINCE,},
1258 {szIf_None_Match, HTTP_QUERY_IF_NONE_MATCH,},
1259 {szIf_Range, HTTP_QUERY_IF_RANGE,},
1260 {szIf_Unmodified_Since, HTTP_QUERY_IF_UNMODIFIED_SINCE,},
1261 {szLast_Modified, HTTP_QUERY_LAST_MODIFIED,},
1262 {szLocation, HTTP_QUERY_LOCATION,},
1263 {szMax_Forwards, HTTP_QUERY_MAX_FORWARDS,},
1264 {szMime_Version, HTTP_QUERY_MIME_VERSION,},
1265 {szPragma, HTTP_QUERY_PRAGMA,},
1266 {szProxy_Authenticate, HTTP_QUERY_PROXY_AUTHENTICATE,},
1267 {szProxy_Authorization, HTTP_QUERY_PROXY_AUTHORIZATION,},
1268 {szProxy_Connection, HTTP_QUERY_PROXY_CONNECTION,},
1269 {szPublic, HTTP_QUERY_PUBLIC,},
1270 {szRange, HTTP_QUERY_RANGE,},
1271 {szReferer, HTTP_QUERY_REFERER,},
1272 {szRetry_After, HTTP_QUERY_RETRY_AFTER,},
1273 {szServer, HTTP_QUERY_SERVER,},
1274 {szSet_Cookie, HTTP_QUERY_SET_COOKIE,},
1275 {szStatus, HTTP_QUERY_STATUS_CODE,},
1276 {szTransfer_Encoding, HTTP_QUERY_TRANSFER_ENCODING,},
1277 {szUnless_Modified_Since, HTTP_QUERY_UNLESS_MODIFIED_SINCE,},
1278 {szUpgrade, HTTP_QUERY_UPGRADE,},
1279 {szURI, HTTP_QUERY_URI,},
1280 {szUser_Agent, HTTP_QUERY_USER_AGENT,},
1281 {szVary, HTTP_QUERY_VARY,},
1282 {szVia, HTTP_QUERY_VIA,},
1283 {szWarning, HTTP_QUERY_WARNING,},
1284 {szWWW_Authenticate, HTTP_QUERY_WWW_AUTHENTICATE,},
1285 };
1286
1287 /***********************************************************************
1288 * HTTP_HttpQueryInfoW (internal)
1289 */
1290 static BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
1291 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1292 {
1293 LPHTTPHEADERW lphttpHdr = NULL;
1294 BOOL bSuccess = FALSE;
1295 BOOL request_only = dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS;
1296
1297
1298 /* Find requested header structure */
1299 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
1300 {
1301 INT requested_index = (lpdwIndex)?(*lpdwIndex):0;
1302 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer,
1303 requested_index,request_only);
1304
1305 if (index < 0)
1306 return bSuccess;
1307 else
1308 lphttpHdr = &lpwhr->pCustHeaders[index];
1309
1310 if (lpdwIndex)
1311 (*lpdwIndex)++;
1312 }
1313 else
1314 {
1315 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
1316
1317 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
1318 {
1319 DWORD len = strlenW(lpwhr->lpszRawHeaders);
1320 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1321 {
1322 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1323 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1324 return FALSE;
1325 }
1326 memcpy(lpBuffer, lpwhr->lpszRawHeaders, (len+1)*sizeof(WCHAR));
1327 *lpdwBufferLength = len * sizeof(WCHAR);
1328
1329 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1330
1331 return TRUE;
1332 }
1333 else if (index == HTTP_QUERY_RAW_HEADERS)
1334 {
1335 static const WCHAR szCrLf[] = {'\r','\n',0};
1336 LPWSTR * ppszRawHeaderLines = HTTP_Tokenize(lpwhr->lpszRawHeaders, szCrLf);
1337 DWORD i, size = 0;
1338 LPWSTR pszString = (WCHAR*)lpBuffer;
1339
1340 for (i = 0; ppszRawHeaderLines[i]; i++)
1341 size += strlenW(ppszRawHeaderLines[i]) + 1;
1342
1343 if (size + 1 > *lpdwBufferLength/sizeof(WCHAR))
1344 {
1345 HTTP_FreeTokens(ppszRawHeaderLines);
1346 *lpdwBufferLength = (size + 1) * sizeof(WCHAR);
1347 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1348 return FALSE;
1349 }
1350
1351 for (i = 0; ppszRawHeaderLines[i]; i++)
1352 {
1353 DWORD len = strlenW(ppszRawHeaderLines[i]);
1354 memcpy(pszString, ppszRawHeaderLines[i], (len+1)*sizeof(WCHAR));
1355 pszString += len+1;
1356 }
1357 *pszString = '\0';
1358
1359 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, size));
1360
1361 *lpdwBufferLength = size * sizeof(WCHAR);
1362 HTTP_FreeTokens(ppszRawHeaderLines);
1363
1364 return TRUE;
1365 }
1366 else if (index == HTTP_QUERY_STATUS_TEXT)
1367 {
1368 DWORD len = strlenW(lpwhr->lpszStatusText);
1369 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1370 {
1371 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1372 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1373 return FALSE;
1374 }
1375 memcpy(lpBuffer, lpwhr->lpszStatusText, (len+1)*sizeof(WCHAR));
1376 *lpdwBufferLength = len * sizeof(WCHAR);
1377
1378 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1379
1380 return TRUE;
1381 }
1382 else if (index == HTTP_QUERY_VERSION)
1383 {
1384 DWORD len = strlenW(lpwhr->lpszVersion);
1385 if (len + 1 > *lpdwBufferLength/sizeof(WCHAR))
1386 {
1387 *lpdwBufferLength = (len + 1) * sizeof(WCHAR);
1388 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1389 return FALSE;
1390 }
1391 memcpy(lpBuffer, lpwhr->lpszVersion, (len+1)*sizeof(WCHAR));
1392 *lpdwBufferLength = len * sizeof(WCHAR);
1393
1394 TRACE("returning data: %s\n", debugstr_wn((WCHAR*)lpBuffer, len));
1395
1396 return TRUE;
1397 }
1398 else if (index >= 0 && index <= HTTP_QUERY_MAX )
1399 {
1400 int i;
1401 for (i = 0; i < sizeof(SORTED_STANDARD_HEADERS)/sizeof(std_hdr_data) ; i++)
1402 {
1403 if (SORTED_STANDARD_HEADERS[i].hdrIndex == index)
1404 {
1405 INT requested_index = (lpdwIndex)?(*lpdwIndex):0;
1406 INT index = HTTP_GetCustomHeaderIndex(lpwhr,
1407 (LPWSTR)SORTED_STANDARD_HEADERS[i].hdrStr,
1408 requested_index,request_only);
1409
1410 if (index < 0)
1411 return bSuccess;
1412 else
1413 lphttpHdr = &lpwhr->pCustHeaders[index];
1414
1415 if (lpdwIndex)
1416 (*lpdwIndex)++;
1417
1418 break;
1419 }
1420 }
1421
1422 if (!lphttpHdr)
1423 {
1424 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1425 return bSuccess;
1426 }
1427 }
1428 else
1429 {
1430 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1431 return bSuccess;
1432 }
1433 }
1434
1435 /* Ensure header satisifies requested attributes */
1436 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
1437 (~lphttpHdr->wFlags & HDR_ISREQUEST))
1438 {
1439 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1440 return bSuccess;
1441 }
1442
1443 /* coalesce value to reuqested type */
1444 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
1445 {
1446 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
1447 bSuccess = TRUE;
1448
1449 TRACE(" returning number : %d\n", *(int *)lpBuffer);
1450 }
1451 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
1452 {
1453 time_t tmpTime;
1454 struct tm tmpTM;
1455 SYSTEMTIME *STHook;
1456
1457 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
1458
1459 tmpTM = *gmtime(&tmpTime);
1460 STHook = (SYSTEMTIME *) lpBuffer;
1461 if(STHook==NULL)
1462 return bSuccess;
1463
1464 STHook->wDay = tmpTM.tm_mday;
1465 STHook->wHour = tmpTM.tm_hour;
1466 STHook->wMilliseconds = 0;
1467 STHook->wMinute = tmpTM.tm_min;
1468 STHook->wDayOfWeek = tmpTM.tm_wday;
1469 STHook->wMonth = tmpTM.tm_mon + 1;
1470 STHook->wSecond = tmpTM.tm_sec;
1471 STHook->wYear = tmpTM.tm_year;
1472
1473 bSuccess = TRUE;
1474
1475 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1476 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1477 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1478 }
1479 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1480 {
1481 if (*lpdwIndex >= lphttpHdr->wCount)
1482 {
1483 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1484 }
1485 else
1486 {
1487 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1488 (*lpdwIndex)++;
1489 }
1490 }
1491 else if (lphttpHdr->lpszValue)
1492 {
1493 DWORD len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1494
1495 if (len > *lpdwBufferLength)
1496 {
1497 *lpdwBufferLength = len;
1498 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1499 return bSuccess;
1500 }
1501
1502 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1503 *lpdwBufferLength = len - sizeof(WCHAR);
1504 bSuccess = TRUE;
1505
1506 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1507 }
1508 return bSuccess;
1509 }
1510
1511 /***********************************************************************
1512 * HttpQueryInfoW (WININET.@)
1513 *
1514 * Queries for information about an HTTP request
1515 *
1516 * RETURNS
1517 * TRUE on success
1518 * FALSE on failure
1519 *
1520 */
1521 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1522 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1523 {
1524 BOOL bSuccess = FALSE;
1525 LPWININETHTTPREQW lpwhr;
1526
1527 if (TRACE_ON(wininet)) {
1528 #define FE(x) { x, #x }
1529 static const wininet_flag_info query_flags[] = {
1530 FE(HTTP_QUERY_MIME_VERSION),
1531 FE(HTTP_QUERY_CONTENT_TYPE),
1532 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1533 FE(HTTP_QUERY_CONTENT_ID),
1534 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1535 FE(HTTP_QUERY_CONTENT_LENGTH),
1536 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1537 FE(HTTP_QUERY_ALLOW),
1538 FE(HTTP_QUERY_PUBLIC),
1539 FE(HTTP_QUERY_DATE),
1540 FE(HTTP_QUERY_EXPIRES),
1541 FE(HTTP_QUERY_LAST_MODIFIED),
1542 FE(HTTP_QUERY_MESSAGE_ID),
1543 FE(HTTP_QUERY_URI),
1544 FE(HTTP_QUERY_DERIVED_FROM),
1545 FE(HTTP_QUERY_COST),
1546 FE(HTTP_QUERY_LINK),
1547 FE(HTTP_QUERY_PRAGMA),
1548 FE(HTTP_QUERY_VERSION),
1549 FE(HTTP_QUERY_STATUS_CODE),
1550 FE(HTTP_QUERY_STATUS_TEXT),
1551 FE(HTTP_QUERY_RAW_HEADERS),
1552 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1553 FE(HTTP_QUERY_CONNECTION),
1554 FE(HTTP_QUERY_ACCEPT),
1555 FE(HTTP_QUERY_ACCEPT_CHARSET),
1556 FE(HTTP_QUERY_ACCEPT_ENCODING),
1557 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1558 FE(HTTP_QUERY_AUTHORIZATION),
1559 FE(HTTP_QUERY_CONTENT_ENCODING),
1560 FE(HTTP_QUERY_FORWARDED),
1561 FE(HTTP_QUERY_FROM),
1562 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1563 FE(HTTP_QUERY_LOCATION),
1564 FE(HTTP_QUERY_ORIG_URI),
1565 FE(HTTP_QUERY_REFERER),
1566 FE(HTTP_QUERY_RETRY_AFTER),
1567 FE(HTTP_QUERY_SERVER),
1568 FE(HTTP_QUERY_TITLE),
1569 FE(HTTP_QUERY_USER_AGENT),
1570 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1571 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1572 FE(HTTP_QUERY_ACCEPT_RANGES),
1573 FE(HTTP_QUERY_SET_COOKIE),
1574 FE(HTTP_QUERY_COOKIE),
1575 FE(HTTP_QUERY_REQUEST_METHOD),
1576 FE(HTTP_QUERY_REFRESH),
1577 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1578 FE(HTTP_QUERY_AGE),
1579 FE(HTTP_QUERY_CACHE_CONTROL),
1580 FE(HTTP_QUERY_CONTENT_BASE),
1581 FE(HTTP_QUERY_CONTENT_LOCATION),
1582 FE(HTTP_QUERY_CONTENT_MD5),
1583 FE(HTTP_QUERY_CONTENT_RANGE),
1584 FE(HTTP_QUERY_ETAG),
1585 FE(HTTP_QUERY_HOST),
1586 FE(HTTP_QUERY_IF_MATCH),
1587 FE(HTTP_QUERY_IF_NONE_MATCH),
1588 FE(HTTP_QUERY_IF_RANGE),
1589 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1590 FE(HTTP_QUERY_MAX_FORWARDS),
1591 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1592 FE(HTTP_QUERY_RANGE),
1593 FE(HTTP_QUERY_TRANSFER_ENCODING),
1594 FE(HTTP_QUERY_UPGRADE),
1595 FE(HTTP_QUERY_VARY),
1596 FE(HTTP_QUERY_VIA),
1597 FE(HTTP_QUERY_WARNING),
1598 FE(HTTP_QUERY_CUSTOM)
1599 };
1600 static const wininet_flag_info modifier_flags[] = {
1601 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1602 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1603 FE(HTTP_QUERY_FLAG_NUMBER),
1604 FE(HTTP_QUERY_FLAG_COALESCE)
1605 };
1606 #undef FE
1607 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1608 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1609 DWORD i;
1610
1611 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1612 TRACE(" Attribute:");
1613 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1614 if (query_flags[i].val == info) {
1615 TRACE(" %s", query_flags[i].name);
1616 break;
1617 }
1618 }
1619 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1620 TRACE(" Unknown (%08lx)", info);
1621 }
1622
1623 TRACE(" Modifier:");
1624 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1625 if (modifier_flags[i].val & info_mod) {
1626 TRACE(" %s", modifier_flags[i].name);
1627 info_mod &= ~ modifier_flags[i].val;
1628 }
1629 }
1630
1631 if (info_mod) {
1632 TRACE(" Unknown (%08lx)", info_mod);
1633 }
1634 TRACE("\n");
1635 }
1636
1637 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1638 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1639 {
1640 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1641 goto lend;
1642 }
1643
1644 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1645 lpBuffer, lpdwBufferLength, lpdwIndex);
1646
1647 lend:
1648 if( lpwhr )
1649 WININET_Release( &lpwhr->hdr );
1650
1651 TRACE("%d <--\n", bSuccess);
1652 return bSuccess;
1653 }
1654
1655 /***********************************************************************
1656 * HttpQueryInfoA (WININET.@)
1657 *
1658 * Queries for information about an HTTP request
1659 *
1660 * RETURNS
1661 * TRUE on success
1662 * FALSE on failure
1663 *
1664 */
1665 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1666 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1667 {
1668 BOOL result;
1669 DWORD len;
1670 WCHAR* bufferW;
1671
1672 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1673 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1674 {
1675 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1676 lpdwBufferLength, lpdwIndex );
1677 }
1678
1679 len = (*lpdwBufferLength)*sizeof(WCHAR);
1680 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1681 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1682 if ((dwInfoLevel & HTTP_QUERY_HEADER_MASK) == HTTP_QUERY_CUSTOM)
1683 MultiByteToWideChar(CP_ACP,0,lpBuffer,-1,bufferW,len);
1684 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1685 &len, lpdwIndex );
1686 if( result )
1687 {
1688 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR) + 1,
1689 lpBuffer, *lpdwBufferLength, NULL, NULL );
1690 *lpdwBufferLength = len - 1;
1691
1692 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer));
1693 }
1694 else
1695 /* since the strings being returned from HttpQueryInfoW should be
1696 * only ASCII characters, it is reasonable to assume that all of
1697 * the Unicode characters can be reduced to a single byte */
1698 *lpdwBufferLength = len / sizeof(WCHAR);
1699
1700 HeapFree(GetProcessHeap(), 0, bufferW );
1701
1702 return result;
1703 }
1704
1705 /***********************************************************************
1706 * HttpSendRequestExA (WININET.@)
1707 *
1708 * Sends the specified request to the HTTP server and allows chunked
1709 * transfers.
1710 *
1711 * RETURNS
1712 * Success: TRUE
1713 * Failure: FALSE, call GetLastError() for more information.
1714 */
1715 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1716 LPINTERNET_BUFFERSA lpBuffersIn,
1717 LPINTERNET_BUFFERSA lpBuffersOut,
1718 DWORD dwFlags, DWORD dwContext)
1719 {
1720 INTERNET_BUFFERSW BuffersInW;
1721 BOOL rc = FALSE;
1722 DWORD headerlen;
1723
1724 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1725 lpBuffersOut, dwFlags, dwContext);
1726
1727 if (lpBuffersIn)
1728 {
1729 BuffersInW.dwStructSize = sizeof(LPINTERNET_BUFFERSW);
1730 if (lpBuffersIn->lpcszHeader)
1731 {
1732 headerlen = MultiByteToWideChar(CP_ACP,0,lpBuffersIn->lpcszHeader,
1733 lpBuffersIn->dwHeadersLength,0,0);
1734 BuffersInW.lpcszHeader = HeapAlloc(GetProcessHeap(),0,headerlen*
1735 sizeof(WCHAR));
1736 if (!BuffersInW.lpcszHeader)
1737 {
1738 SetLastError(ERROR_OUTOFMEMORY);
1739 return FALSE;
1740 }
1741 BuffersInW.dwHeadersLength = MultiByteToWideChar(CP_ACP, 0,
1742 lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1743 (LPWSTR)BuffersInW.lpcszHeader, headerlen);
1744 }
1745 else
1746 BuffersInW.lpcszHeader = NULL;
1747 BuffersInW.dwHeadersTotal = lpBuffersIn->dwHeadersTotal;
1748 BuffersInW.lpvBuffer = lpBuffersIn->lpvBuffer;
1749 BuffersInW.dwBufferLength = lpBuffersIn->dwBufferLength;
1750 BuffersInW.dwBufferTotal = lpBuffersIn->dwBufferTotal;
1751 BuffersInW.Next = NULL;
1752 }
1753
1754 rc = HttpSendRequestExW(hRequest, lpBuffersIn ? &BuffersInW : NULL, NULL, dwFlags, dwContext);
1755
1756 if (lpBuffersIn)
1757 HeapFree(GetProcessHeap(),0,(LPVOID)BuffersInW.lpcszHeader);
1758
1759 return rc;
1760 }
1761
1762 /***********************************************************************
1763 * HttpSendRequestExW (WININET.@)
1764 *
1765 * Sends the specified request to the HTTP server and allows chunked
1766 * transfers
1767 *
1768 * RETURNS
1769 * Success: TRUE
1770 * Failure: FALSE, call GetLastError() for more information.
1771 */
1772 BOOL WINAPI HttpSendRequestExW(HINTERNET hRequest,
1773 LPINTERNET_BUFFERSW lpBuffersIn,
1774 LPINTERNET_BUFFERSW lpBuffersOut,
1775 DWORD dwFlags, DWORD dwContext)
1776 {
1777 BOOL ret;
1778 LPWININETHTTPREQW lpwhr;
1779 LPWININETHTTPSESSIONW lpwhs;
1780 LPWININETAPPINFOW hIC;
1781
1782 TRACE("(%p, %p, %p, %08lx, %08lx)\n", hRequest, lpBuffersIn,
1783 lpBuffersOut, dwFlags, dwContext);
1784
1785 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hRequest );
1786
1787 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1788 {
1789 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1790 return FALSE;
1791 }
1792
1793 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1794 assert(lpwhs->hdr.htype == WH_HHTTPSESSION);
1795 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1796 assert(hIC->hdr.htype == WH_HINIT);
1797
1798 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1799 {
1800 WORKREQUEST workRequest;
1801 struct WORKREQ_HTTPSENDREQUESTW *req;
1802
1803 workRequest.asyncall = HTTPSENDREQUESTW;
1804 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1805 req = &workRequest.u.HttpSendRequestW;
1806 if (lpBuffersIn)
1807 {
1808 if (lpBuffersIn->lpcszHeader)
1809 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1810 req->lpszHeader = WININET_strdupW(lpBuffersIn->lpcszHeader);
1811 else
1812 req->lpszHeader = NULL;
1813 req->dwHeaderLength = lpBuffersIn->dwHeadersLength;
1814 req->lpOptional = lpBuffersIn->lpvBuffer;
1815 req->dwOptionalLength = lpBuffersIn->dwBufferLength;
1816 req->dwContentLength = lpBuffersIn->dwBufferTotal;
1817 }
1818 else
1819 {
1820 req->lpszHeader = NULL;
1821 req->dwHeaderLength = 0;
1822 req->lpOptional = NULL;
1823 req->dwOptionalLength = 0;
1824 req->dwContentLength = 0;
1825 }
1826
1827 req->bEndRequest = FALSE;
1828
1829 INTERNET_AsyncCall(&workRequest);
1830 /*
1831 * This is from windows.
1832 */
1833 SetLastError(ERROR_IO_PENDING);
1834 ret = FALSE;
1835 }
1836 else
1837 {
1838 ret = HTTP_HttpSendRequestW(lpwhr, lpBuffersIn->lpcszHeader, lpBuffersIn->dwHeadersLength,
1839 lpBuffersIn->lpvBuffer, lpBuffersIn->dwBufferLength,
1840 lpBuffersIn->dwBufferTotal, FALSE);
1841 }
1842
1843 WININET_Release(&lpwhr->hdr);
1844 TRACE("<---\n");
1845 return ret;
1846 }
1847
1848 /***********************************************************************
1849 * HttpSendRequestW (WININET.@)
1850 *
1851 * Sends the specified request to the HTTP server
1852 *
1853 * RETURNS
1854 * TRUE on success
1855 * FALSE on failure
1856 *
1857 */
1858 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1859 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1860 {
1861 LPWININETHTTPREQW lpwhr;
1862 LPWININETHTTPSESSIONW lpwhs = NULL;
1863 LPWININETAPPINFOW hIC = NULL;
1864 BOOL r;
1865
1866 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1867 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1868
1869 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1870 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1871 {
1872 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1873 r = FALSE;
1874 goto lend;
1875 }
1876
1877 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1878 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1879 {
1880 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1881 r = FALSE;
1882 goto lend;
1883 }
1884
1885 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1886 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1887 {
1888 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1889 r = FALSE;
1890 goto lend;
1891 }
1892
1893 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1894 {
1895 WORKREQUEST workRequest;
1896 struct WORKREQ_HTTPSENDREQUESTW *req;
1897
1898 workRequest.asyncall = HTTPSENDREQUESTW;
1899 workRequest.hdr = WININET_AddRef( &lpwhr->hdr );
1900 req = &workRequest.u.HttpSendRequestW;
1901 if (lpszHeaders)
1902 req->lpszHeader = WININET_strdupW(lpszHeaders);
1903 else
1904 req->lpszHeader = 0;
1905 req->dwHeaderLength = dwHeaderLength;
1906 req->lpOptional = lpOptional;
1907 req->dwOptionalLength = dwOptionalLength;
1908 req->dwContentLength = dwOptionalLength;
1909 req->bEndRequest = TRUE;
1910
1911 INTERNET_AsyncCall(&workRequest);
1912 /*
1913 * This is from windows.
1914 */
1915 SetLastError(ERROR_IO_PENDING);
1916 r = FALSE;
1917 }
1918 else
1919 {
1920 r = HTTP_HttpSendRequestW(lpwhr, lpszHeaders,
1921 dwHeaderLength, lpOptional, dwOptionalLength,
1922 dwOptionalLength, TRUE);
1923 }
1924 lend:
1925 if( lpwhr )
1926 WININET_Release( &lpwhr->hdr );
1927 return r;
1928 }
1929
1930 /***********************************************************************
1931 * HttpSendRequestA (WININET.@)
1932 *
1933 * Sends the specified request to the HTTP server
1934 *
1935 * RETURNS
1936 * TRUE on success
1937 * FALSE on failure
1938 *
1939 */
1940 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1941 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1942 {
1943 BOOL result;
1944 LPWSTR szHeaders=NULL;
1945 DWORD nLen=dwHeaderLength;
1946 if(lpszHeaders!=NULL)
1947 {
1948 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1949 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1950 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1951 }
1952 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1953 HeapFree(GetProcessHeap(),0,szHeaders);
1954 return result;
1955 }
1956
1957 /***********************************************************************
1958 * HTTP_HandleRedirect (internal)
1959 */
1960 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1961 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
1962 DWORD dwContentLength)
1963 {
1964 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1965 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1966 WCHAR path[2048];
1967 char szaddr[32];
1968
1969 if(lpszUrl[0]=='/')
1970 {
1971 /* if it's an absolute path, keep the same session info */
1972 lstrcpynW(path, lpszUrl, 2048);
1973 }
1974 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1975 {
1976 TRACE("Redirect through proxy\n");
1977 lstrcpynW(path, lpszUrl, 2048);
1978 }
1979 else
1980 {
1981 URL_COMPONENTSW urlComponents;
1982 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1983 static const WCHAR szHttp[] = {'h','t','t','p',0};
1984 static const WCHAR szHttps[] = {'h','t','t','p','s',0};
1985 DWORD url_length = 0;
1986 LPWSTR orig_url;
1987 LPWSTR combined_url;
1988
1989 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1990 urlComponents.lpszScheme = (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE) ? (LPWSTR)szHttps : (LPWSTR)szHttp;
1991 urlComponents.dwSchemeLength = 0;
1992 urlComponents.lpszHostName = lpwhs->lpszHostName;
1993 urlComponents.dwHostNameLength = 0;
1994 urlComponents.nPort = lpwhs->nHostPort;
1995 urlComponents.lpszUserName = lpwhs->lpszUserName;
1996 urlComponents.dwUserNameLength = 0;
1997 urlComponents.lpszPassword = NULL;
1998 urlComponents.dwPasswordLength = 0;
1999 urlComponents.lpszUrlPath = lpwhr->lpszPath;
2000 urlComponents.dwUrlPathLength = 0;
2001 urlComponents.lpszExtraInfo = NULL;
2002 urlComponents.dwExtraInfoLength = 0;
2003
2004 if (!InternetCreateUrlW(&urlComponents, 0, NULL, &url_length) &&
2005 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2006 return FALSE;
2007
2008 url_length++; /* for nul terminating character */
2009 orig_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2010
2011 if (!InternetCreateUrlW(&urlComponents, 0, orig_url, &url_length))
2012 {
2013 HeapFree(GetProcessHeap(), 0, orig_url);
2014 return FALSE;
2015 }
2016
2017 url_length = 0;
2018 if (!InternetCombineUrlW(orig_url, lpszUrl, NULL, &url_length, ICU_ENCODE_SPACES_ONLY) &&
2019 (GetLastError() != ERROR_INSUFFICIENT_BUFFER))
2020 {
2021 HeapFree(GetProcessHeap(), 0, orig_url);
2022 return FALSE;
2023 }
2024 combined_url = HeapAlloc(GetProcessHeap(), 0, url_length * sizeof(WCHAR));
2025
2026 if (!InternetCombineUrlW(orig_url, lpszUrl, combined_url, &url_length, ICU_ENCODE_SPACES_ONLY))
2027 {
2028 HeapFree(GetProcessHeap(), 0, orig_url);
2029 HeapFree(GetProcessHeap(), 0, combined_url);
2030 return FALSE;
2031 }
2032 HeapFree(GetProcessHeap(), 0, orig_url);
2033
2034 userName[0] = 0;
2035 hostName[0] = 0;
2036 protocol[0] = 0;
2037
2038 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
2039 urlComponents.lpszScheme = protocol;
2040 urlComponents.dwSchemeLength = 32;
2041 urlComponents.lpszHostName = hostName;
2042 urlComponents.dwHostNameLength = MAXHOSTNAME;
2043 urlComponents.lpszUserName = userName;
2044 urlComponents.dwUserNameLength = 1024;
2045 urlComponents.lpszPassword = NULL;
2046 urlComponents.dwPasswordLength = 0;
2047 urlComponents.lpszUrlPath = path;
2048 urlComponents.dwUrlPathLength = 2048;
2049 urlComponents.lpszExtraInfo = NULL;
2050 urlComponents.dwExtraInfoLength = 0;
2051 if(!InternetCrackUrlW(combined_url, strlenW(combined_url), 0, &urlComponents))
2052 {
2053 HeapFree(GetProcessHeap(), 0, combined_url);
2054 return FALSE;
2055 }
2056 HeapFree(GetProcessHeap(), 0, combined_url);
2057
2058 if (!strncmpW(szHttp, urlComponents.lpszScheme, strlenW(szHttp)) &&
2059 (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2060 {
2061 TRACE("redirect from secure page to non-secure page\n");
2062 /* FIXME: warn about from secure redirect to non-secure page */
2063 lpwhr->hdr.dwFlags &= ~INTERNET_FLAG_SECURE;
2064 }
2065 if (!strncmpW(szHttps, urlComponents.lpszScheme, strlenW(szHttps)) &&
2066 !(lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2067 {
2068 TRACE("redirect from non-secure page to secure page\n");
2069 /* FIXME: notify about redirect to secure page */
2070 lpwhr->hdr.dwFlags |= INTERNET_FLAG_SECURE;
2071 }
2072
2073 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
2074 {
2075 if (lstrlenW(protocol)>4) /*https*/
2076 urlComponents.nPort = INTERNET_DEFAULT_HTTPS_PORT;
2077 else /*http*/
2078 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
2079 }
2080
2081 #if 0
2082 /*
2083 * This upsets redirects to binary files on sourceforge.net
2084 * and gives an html page instead of the target file
2085 * Examination of the HTTP request sent by native wininet.dll
2086 * reveals that it doesn't send a referrer in that case.
2087 * Maybe there's a flag that enables this, or maybe a referrer
2088 * shouldn't be added in case of a redirect.
2089 */
2090
2091 /* consider the current host as the referrer */
2092 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
2093 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
2094 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
2095 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
2096 #endif
2097
2098 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2099 lpwhs->lpszServerName = WININET_strdupW(hostName);
2100 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
2101 if (urlComponents.nPort != INTERNET_DEFAULT_HTTP_PORT &&
2102 urlComponents.nPort != INTERNET_DEFAULT_HTTPS_PORT)
2103 {
2104 int len;
2105 static WCHAR fmt[] = {'%','s',':','%','i',0};
2106 len = lstrlenW(hostName);
2107 len += 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2108 lpwhs->lpszHostName = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
2109 sprintfW(lpwhs->lpszHostName, fmt, hostName, urlComponents.nPort);
2110 }
2111 else
2112 lpwhs->lpszHostName = WININET_strdupW(hostName);
2113
2114 HTTP_ProcessHeader(lpwhr, szHost, lpwhs->lpszHostName, HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE | HTTP_ADDHDR_FLAG_REQ);
2115
2116
2117 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2118 lpwhs->lpszUserName = NULL;
2119 if (userName[0])
2120 lpwhs->lpszUserName = WININET_strdupW(userName);
2121 lpwhs->nServerPort = urlComponents.nPort;
2122
2123 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2124 INTERNET_STATUS_RESOLVING_NAME,
2125 lpwhs->lpszServerName,
2126 strlenW(lpwhs->lpszServerName)+1);
2127
2128 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
2129 &lpwhs->socketAddress))
2130 {
2131 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
2132 return FALSE;
2133 }
2134
2135 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2136 szaddr, sizeof(szaddr));
2137 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2138 INTERNET_STATUS_NAME_RESOLVED,
2139 szaddr, strlen(szaddr)+1);
2140
2141 NETCON_close(&lpwhr->netConnection);
2142
2143 if (!NETCON_init(&lpwhr->netConnection,lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE))
2144 return FALSE;
2145 }
2146
2147 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2148 lpwhr->lpszPath=NULL;
2149 if (strlenW(path))
2150 {
2151 DWORD needed = 0;
2152 HRESULT rc;
2153
2154 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
2155 if (rc != E_POINTER)
2156 needed = strlenW(path)+1;
2157 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
2158 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
2159 URL_ESCAPE_SPACES_ONLY);
2160 if (rc)
2161 {
2162 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
2163 strcpyW(lpwhr->lpszPath,path);
2164 }
2165 }
2166
2167 return HTTP_HttpSendRequestW(lpwhr, lpszHeaders, dwHeaderLength, lpOptional,
2168 dwOptionalLength, dwContentLength, TRUE);
2169 }
2170
2171 /***********************************************************************
2172 * HTTP_build_req (internal)
2173 *
2174 * concatenate all the strings in the request together
2175 */
2176 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
2177 {
2178 LPCWSTR *t;
2179 LPWSTR str;
2180
2181 for( t = list; *t ; t++ )
2182 len += strlenW( *t );
2183 len++;
2184
2185 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
2186 *str = 0;
2187
2188 for( t = list; *t ; t++ )
2189 strcatW( str, *t );
2190
2191 return str;
2192 }
2193
2194 static BOOL HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr)
2195 {
2196 LPWSTR lpszPath;
2197 LPWSTR requestString;
2198 INT len;
2199 INT cnt;
2200 INT responseLen;
2201 char *ascii_req;
2202 BOOL ret;
2203 static const WCHAR szConnect[] = {'C','O','N','N','E','C','T',0};
2204 static const WCHAR szFormat[] = {'%','s',':','%','d',0};
2205 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2206
2207 TRACE("\n");
2208
2209 lpszPath = HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs->lpszHostName ) + 13)*sizeof(WCHAR) );
2210 sprintfW( lpszPath, szFormat, lpwhs->lpszHostName, lpwhs->nHostPort );
2211 requestString = HTTP_BuildHeaderRequestString( lpwhr, szConnect, lpszPath, FALSE );
2212 HeapFree( GetProcessHeap(), 0, lpszPath );
2213
2214 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2215 NULL, 0, NULL, NULL );
2216 len--; /* the nul terminator isn't needed */
2217 ascii_req = HeapAlloc( GetProcessHeap(), 0, len );
2218 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2219 ascii_req, len, NULL, NULL );
2220 HeapFree( GetProcessHeap(), 0, requestString );
2221
2222 TRACE("full request -> %s\n", debugstr_an( ascii_req, len ) );
2223
2224 ret = NETCON_send( &lpwhr->netConnection, ascii_req, len, 0, &cnt );
2225 HeapFree( GetProcessHeap(), 0, ascii_req );
2226 if (!ret || cnt < 0)
2227 return FALSE;
2228
2229 responseLen = HTTP_GetResponseHeaders( lpwhr );
2230 if (!responseLen)
2231 return FALSE;
2232
2233 return TRUE;
2234 }
2235
2236 /***********************************************************************
2237 * HTTP_HttpSendRequestW (internal)
2238 *
2239 * Sends the specified request to the HTTP server
2240 *
2241 * RETURNS
2242 * TRUE on success
2243 * FALSE on failure
2244 *
2245 */
2246 BOOL WINAPI HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr, LPCWSTR lpszHeaders,
2247 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength,
2248 DWORD dwContentLength, BOOL bEndRequest)
2249 {
2250 INT cnt;
2251 BOOL bSuccess = FALSE;
2252 LPWSTR requestString = NULL;
2253 INT responseLen;
2254 BOOL loop_next = FALSE;
2255 INTERNET_ASYNC_RESULT iar;
2256 LPHTTPHEADERW Host;
2257
2258 TRACE("--> %p\n", lpwhr);
2259
2260 assert(lpwhr->hdr.htype == WH_HHTTPREQ);
2261
2262 /* Clear any error information */
2263 INTERNET_SetLastError(0);
2264
2265 HTTP_FixVerb(lpwhr);
2266
2267 /* if we are using optional stuff, we must add the fixed header of that option length */
2268 if (dwContentLength > 0)
2269 {
2270 static const WCHAR szContentLength[] = {
2271 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2272 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
2273 sprintfW(contentLengthStr, szContentLength, dwContentLength);
2274 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
2275 }
2276
2277 Host = HTTP_GetHeader(lpwhr,szHost);
2278 do
2279 {
2280 DWORD len;
2281 char *ascii_req;
2282
2283 TRACE("Going to url %s %s\n", debugstr_w(Host->lpszValue), debugstr_w(lpwhr->lpszPath));
2284 loop_next = FALSE;
2285
2286 HTTP_FixURL(lpwhr);
2287
2288 /* add the headers the caller supplied */
2289 if( lpszHeaders && dwHeaderLength )
2290 {
2291 HTTP_HttpAddRequestHeadersW(lpwhr, lpszHeaders, dwHeaderLength,
2292 HTTP_ADDREQ_FLAG_ADD | HTTP_ADDHDR_FLAG_REPLACE);
2293 }
2294
2295 /* if there's a proxy username and password, add it to the headers */
2296 HTTP_AddProxyInfo(lpwhr);
2297
2298 requestString = HTTP_BuildHeaderRequestString(lpwhr, lpwhr->lpszVerb, lpwhr->lpszPath, FALSE);
2299
2300 TRACE("Request header -> %s\n", debugstr_w(requestString) );
2301
2302 /* Send the request and store the results */
2303 if (!HTTP_OpenConnection(lpwhr))
2304 goto lend;
2305
2306 /* send the request as ASCII, tack on the optional data */
2307 if( !lpOptional )
2308 dwOptionalLength = 0;
2309 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2310 NULL, 0, NULL, NULL );
2311 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
2312 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
2313 ascii_req, len, NULL, NULL );
2314 if( lpOptional )
2315 memcpy( &ascii_req[len-1], lpOptional, dwOptionalLength );
2316 len = (len + dwOptionalLength - 1);
2317 ascii_req[len] = 0;
2318 TRACE("full request -> %s\n", debugstr_a(ascii_req) );
2319
2320 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2321 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
2322
2323 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
2324 HeapFree( GetProcessHeap(), 0, ascii_req );
2325
2326 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2327 INTERNET_STATUS_REQUEST_SENT,
2328 &len, sizeof(DWORD));
2329
2330 if (bEndRequest)
2331 {
2332 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2333 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
2334
2335 if (cnt < 0)
2336 goto lend;
2337
2338 responseLen = HTTP_GetResponseHeaders(lpwhr);
2339 if (responseLen)
2340 bSuccess = TRUE;
2341
2342 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2343 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
2344 sizeof(DWORD));
2345
2346 HTTP_ProcessHeaders(lpwhr);
2347 }
2348 else
2349 bSuccess = TRUE;
2350 }
2351 while (loop_next);
2352
2353 lend:
2354
2355 HeapFree(GetProcessHeap(), 0, requestString);
2356
2357 /* TODO: send notification for P3P header */
2358
2359 if(!(lpwhr->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess && bEndRequest)
2360 {
2361 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
2362 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
2363 (dwCode==302 || dwCode==301))
2364 {
2365 WCHAR szNewLocation[2048];
2366 DWORD dwBufferSize=2048;
2367 dwIndex=0;
2368 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
2369 {
2370 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2371 INTERNET_STATUS_REDIRECT, szNewLocation,
2372 dwBufferSize);
2373 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
2374 dwHeaderLength, lpOptional, dwOptionalLength,
2375 dwContentLength);
2376 }
2377 }
2378 }
2379
2380
2381 iar.dwResult = (DWORD)bSuccess;
2382 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
2383
2384 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2385 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
2386 sizeof(INTERNET_ASYNC_RESULT));
2387
2388 TRACE("<--\n");
2389 return bSuccess;
2390 }
2391
2392
2393 /***********************************************************************
2394 * HTTP_Connect (internal)
2395 *
2396 * Create http session handle
2397 *
2398 * RETURNS
2399 * HINTERNET a session handle on success
2400 * NULL on failure
2401 *
2402 */
2403 HINTERNET HTTP_Connect(LPWININETAPPINFOW hIC, LPCWSTR lpszServerName,
2404 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
2405 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
2406 DWORD dwInternalFlags)
2407 {
2408 BOOL bSuccess = FALSE;
2409 LPWININETHTTPSESSIONW lpwhs = NULL;
2410 HINTERNET handle = NULL;
2411
2412 TRACE("-->\n");
2413
2414 assert( hIC->hdr.htype == WH_HINIT );
2415
2416 hIC->hdr.dwContext = dwContext;
2417
2418 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
2419 if (NULL == lpwhs)
2420 {
2421 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2422 goto lerror;
2423 }
2424
2425 /*
2426 * According to my tests. The name is not resolved until a request is sent
2427 */
2428
2429 lpwhs->hdr.htype = WH_HHTTPSESSION;
2430 lpwhs->hdr.lpwhparent = WININET_AddRef( &hIC->hdr );
2431 lpwhs->hdr.dwFlags = dwFlags;
2432 lpwhs->hdr.dwContext = dwContext;
2433 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
2434 lpwhs->hdr.dwRefCount = 1;
2435 lpwhs->hdr.destroy = HTTP_CloseHTTPSessionHandle;
2436 lpwhs->hdr.lpfnStatusCB = hIC->hdr.lpfnStatusCB;
2437
2438 handle = WININET_AllocHandle( &lpwhs->hdr );
2439 if (NULL == handle)
2440 {
2441 ERR("Failed to alloc handle\n");
2442 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2443 goto lerror;
2444 }
2445
2446 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
2447 if(strchrW(hIC->lpszProxy, ' '))
2448 FIXME("Several proxies not implemented.\n");
2449 if(hIC->lpszProxyBypass)
2450 FIXME("Proxy bypass is ignored.\n");
2451 }
2452 if (lpszServerName && lpszServerName[0])
2453 {
2454 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
2455 lpwhs->lpszHostName = WININET_strdupW(lpszServerName);
2456 }
2457 if (lpszUserName && lpszUserName[0])
2458 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
2459 lpwhs->nServerPort = nServerPort;
2460 lpwhs->nHostPort = nServerPort;
2461
2462 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2463 if (!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2464 {
2465 INTERNET_SendCallback(&hIC->hdr, dwContext,
2466 INTERNET_STATUS_HANDLE_CREATED, &handle,
2467 sizeof(handle));
2468 }
2469
2470 bSuccess = TRUE;
2471
2472 lerror:
2473 if( lpwhs )
2474 WININET_Release( &lpwhs->hdr );
2475
2476 /*
2477 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2478 * windows
2479 */
2480
2481 TRACE("%p --> %p (%p)\n", hIC, handle, lpwhs);
2482 return handle;
2483 }
2484
2485
2486 /***********************************************************************
2487 * HTTP_OpenConnection (internal)
2488 *
2489 * Connect to a web server
2490 *
2491 * RETURNS
2492 *
2493 * TRUE on success
2494 * FALSE on failure
2495 */
2496 static BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
2497 {
2498 BOOL bSuccess = FALSE;
2499 LPWININETHTTPSESSIONW lpwhs;
2500 LPWININETAPPINFOW hIC = NULL;
2501 char szaddr[32];
2502
2503 TRACE("-->\n");
2504
2505
2506 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
2507 {
2508 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
2509 goto lend;
2510 }
2511
2512 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
2513
2514 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2515 inet_ntop(lpwhs->socketAddress.sin_family, &lpwhs->socketAddress.sin_addr,
2516 szaddr, sizeof(szaddr));
2517 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2518 INTERNET_STATUS_CONNECTING_TO_SERVER,
2519 szaddr,
2520 strlen(szaddr)+1);
2521
2522 if (!NETCON_create(&lpwhr->netConnection, lpwhs->socketAddress.sin_family,
2523 SOCK_STREAM, 0))
2524 {
2525 WARN("Socket creation failed\n");
2526 goto lend;
2527 }
2528
2529 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
2530 sizeof(lpwhs->socketAddress)))
2531 goto lend;
2532
2533 if (lpwhr->hdr.dwFlags & INTERNET_FLAG_SECURE)
2534 {
2535 /* Note: we differ from Microsoft's WinINet here. they seem to have
2536 * a bug that causes no status callbacks to be sent when starting
2537 * a tunnel to a proxy server using the CONNECT verb. i believe our
2538 * behaviour to be more correct and to not cause any incompatibilities
2539 * because using a secure connection through a proxy server is a rare
2540 * case that would be hard for anyone to depend on */
2541 if (hIC->lpszProxy && !HTTP_SecureProxyConnect(lpwhr))
2542 goto lend;
2543
2544 if (!NETCON_secure_connect(&lpwhr->netConnection, lpwhs->lpszHostName))
2545 {
2546 WARN("Couldn't connect securely to host\n");
2547 goto lend;
2548 }
2549 }
2550
2551 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2552 INTERNET_STATUS_CONNECTED_TO_SERVER,
2553 szaddr, strlen(szaddr)+1);
2554
2555 bSuccess = TRUE;
2556
2557 lend:
2558 TRACE("%d <--\n", bSuccess);
2559 return bSuccess;
2560 }
2561
2562
2563 /***********************************************************************
2564 * HTTP_clear_response_headers (internal)
2565 *
2566 * clear out any old response headers
2567 */
2568 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
2569 {
2570 DWORD i;
2571
2572 for( i=0; i<lpwhr->nCustHeaders; i++)
2573 {
2574 if( !lpwhr->pCustHeaders[i].lpszField )
2575 continue;
2576 if( !lpwhr->pCustHeaders[i].lpszValue )
2577 continue;
2578 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
2579 continue;
2580 HTTP_DeleteCustomHeader( lpwhr, i );
2581 i--;
2582 }
2583 }
2584
2585 /***********************************************************************
2586 * HTTP_GetResponseHeaders (internal)
2587 *
2588 * Read server response
2589 *
2590 * RETURNS
2591 *
2592 * TRUE on success
2593 * FALSE on error
2594 */
2595 static INT HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2596 {
2597 INT cbreaks = 0;
2598 WCHAR buffer[MAX_REPLY_LEN];
2599 DWORD buflen = MAX_REPLY_LEN;
2600 BOOL bSuccess = FALSE;
2601 INT rc = 0;
2602 static const WCHAR szCrLf[] = {'\r','\n',0};
2603 char bufferA[MAX_REPLY_LEN];
2604 LPWSTR status_code, status_text;
2605 DWORD cchMaxRawHeaders = 1024;
2606 LPWSTR lpszRawHeaders = HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2607 DWORD cchRawHeaders = 0;
2608
2609 TRACE("-->\n");
2610
2611 /* clear old response headers (eg. from a redirect response) */
2612 HTTP_clear_response_headers( lpwhr );
2613
2614 if (!NETCON_connected(&lpwhr->netConnection))
2615 goto lend;
2616
2617 /*
2618 * HACK peek at the buffer
2619 */
2620 #if 0
2621 /* This is Wine code, we don't support MSG_PEEK yet so we have to do it
2622 a bit different */
2623 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2624 #endif
2625
2626 /*
2627 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2628 */
2629 buflen = MAX_REPLY_LEN;
2630 memset(buffer, 0, MAX_REPLY_LEN);
2631 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2632 goto lend;
2633 #if 1
2634 rc = buflen;
2635 #endif
2636 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2637
2638 /* regenerate raw headers */
2639 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2640 {
2641 cchMaxRawHeaders *= 2;
2642 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2643 }
2644 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2645 cchRawHeaders += (buflen-1);
2646 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2647 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2648 lpszRawHeaders[cchRawHeaders] = '\0';
2649
2650 /* split the version from the status code */
2651 status_code = strchrW( buffer, ' ' );
2652 if( !status_code )
2653 goto lend;
2654 *status_code++=0;
2655
2656 /* split the status code from the status text */
2657 status_text = strchrW( status_code, ' ' );
2658 if( !status_text )
2659 goto lend;
2660 *status_text++=0;
2661
2662 TRACE("version [%s] status code [%s] status text [%s]\n",
2663 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2664
2665 HTTP_ProcessHeader(lpwhr, szStatus, status_code,
2666 HTTP_ADDHDR_FLAG_REPLACE);
2667
2668 HeapFree(GetProcessHeap(),0,lpwhr->lpszVersion);
2669 HeapFree(GetProcessHeap(),0,lpwhr->lpszStatusText);
2670
2671 lpwhr->lpszVersion= WININET_strdupW(buffer);
2672 lpwhr->lpszStatusText = WININET_strdupW(status_text);
2673
2674 /* Parse each response line */
2675 do
2676 {
2677 buflen = MAX_REPLY_LEN;
2678 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2679 {
2680 LPWSTR * pFieldAndValue;
2681
2682 #if 1
2683 rc += buflen;
2684 #endif
2685 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA));
2686 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2687
2688 while (cchRawHeaders + buflen + strlenW(szCrLf) > cchMaxRawHeaders)
2689 {
2690 cchMaxRawHeaders *= 2;
2691 lpszRawHeaders = HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders, (cchMaxRawHeaders+1)*sizeof(WCHAR));
2692 }
2693 memcpy(lpszRawHeaders+cchRawHeaders, buffer, (buflen-1)*sizeof(WCHAR));
2694 cchRawHeaders += (buflen-1);
2695 memcpy(lpszRawHeaders+cchRawHeaders, szCrLf, sizeof(szCrLf));
2696 cchRawHeaders += sizeof(szCrLf)/sizeof(szCrLf[0])-1;
2697 lpszRawHeaders[cchRawHeaders] = '\0';
2698
2699 pFieldAndValue = HTTP_InterpretHttpHeader(buffer);
2700 if (!pFieldAndValue)
2701 break;
2702
2703 HTTP_ProcessHeader(lpwhr, pFieldAndValue[0], pFieldAndValue[1],
2704 HTTP_ADDREQ_FLAG_ADD );
2705
2706 HTTP_FreeTokens(pFieldAndValue);
2707 }
2708 else
2709 {
2710 cbreaks++;
2711 if (cbreaks >= 2)
2712 break;
2713 }
2714 }while(1);
2715
2716 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2717 lpwhr->lpszRawHeaders = lpszRawHeaders;
2718 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders));
2719 bSuccess = TRUE;
2720
2721 lend:
2722
2723 TRACE("<--\n");
2724 if (bSuccess)
2725 return rc;
2726 else
2727 return 0;
2728 }
2729
2730
2731 static void strip_spaces(LPWSTR start)
2732 {
2733 LPWSTR str = start;
2734 LPWSTR end;
2735
2736 while (*str == ' ' && *str != '\0')
2737 str++;
2738
2739 if (str != start)
2740 memmove(start, str, sizeof(WCHAR) * (strlenW(str) + 1));
2741
2742 end = start + strlenW(start) - 1;
2743 while (end >= start && *end == ' ')
2744 {
2745 *end = '\0';
2746 end--;
2747 }
2748 }
2749
2750
2751 /***********************************************************************
2752 * HTTP_InterpretHttpHeader (internal)
2753 *
2754 * Parse server response
2755 *
2756 * RETURNS
2757 *
2758 * Pointer to array of field, value, NULL on success.
2759 * NULL on error.
2760 */
2761 static LPWSTR * HTTP_InterpretHttpHeader(LPCWSTR buffer)
2762 {
2763 LPWSTR * pTokenPair;
2764 LPWSTR pszColon;
2765 INT len;
2766
2767 pTokenPair = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pTokenPair)*3);
2768
2769 pszColon = strchrW(buffer, ':');
2770 /* must have two tokens */
2771 if (!pszColon)
2772 {
2773 HTTP_FreeTokens(pTokenPair);
2774 if (buffer[0])
2775 TRACE("No ':' in line: %s\n", debugstr_w(buffer));
2776 return NULL;
2777 }
2778
2779 pTokenPair[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon - buffer + 1) * sizeof(WCHAR));
2780 if (!pTokenPair[0])
2781 {
2782 HTTP_FreeTokens(pTokenPair);
2783 return NULL;
2784 }
2785 memcpy(pTokenPair[0], buffer, (pszColon - buffer) * sizeof(WCHAR));
2786 pTokenPair[0][pszColon - buffer] = '\0';
2787
2788 /* skip colon */
2789 pszColon++;
2790 len = strlenW(pszColon);
2791 pTokenPair[1] = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
2792 if (!pTokenPair[1])
2793 {
2794 HTTP_FreeTokens(pTokenPair);
2795 return NULL;
2796 }
2797 memcpy(pTokenPair[1], pszColon, (len + 1) * sizeof(WCHAR));
2798
2799 strip_spaces(pTokenPair[0]);
2800 strip_spaces(pTokenPair[1]);
2801
2802 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair[0]), debugstr_w(pTokenPair[1]));
2803 return pTokenPair;
2804 }
2805
2806 /***********************************************************************
2807 * HTTP_ProcessHeader (internal)
2808 *
2809 * Stuff header into header tables according to <dwModifier>
2810 *
2811 */
2812
2813 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2814
2815 static BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2816 {
2817 LPHTTPHEADERW lphttpHdr = NULL;
2818 BOOL bSuccess = FALSE;
2819 INT index = -1;
2820 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2821 BOOL request_only = dwModifier & HTTP_ADDHDR_FLAG_REQ;
2822
2823 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field), debugstr_w(value), dwModifier);
2824
2825 /* Don't let applications add Connection header to request */
2826 if (strcmpW(szConnection,field)==0 && (dwModifier & HTTP_ADDHDR_FLAG_REQ))
2827 {
2828 return FALSE;
2829 }
2830
2831 /* REPLACE wins out over ADD */
2832 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2833 dwModifier &= ~HTTP_ADDHDR_FLAG_ADD;
2834
2835 if (dwModifier & HTTP_ADDHDR_FLAG_ADD)
2836 index = -1;
2837 else
2838 index = HTTP_GetCustomHeaderIndex(lpwhr, field, 0, request_only);
2839
2840 if (index >= 0)
2841 {
2842 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2843 {
2844 return FALSE;
2845 }
2846 lphttpHdr = &lpwhr->pCustHeaders[index];
2847 }
2848 else if (value)
2849 {
2850 HTTPHEADERW hdr;
2851
2852 hdr.lpszField = (LPWSTR)field;
2853 hdr.lpszValue = (LPWSTR)value;
2854 hdr.wFlags = hdr.wCount = 0;
2855
2856 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2857 hdr.wFlags |= HDR_ISREQUEST;
2858
2859 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2860 }
2861
2862 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2863 lphttpHdr->wFlags |= HDR_ISREQUEST;
2864 else
2865 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2866
2867 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2868 {
2869 HTTP_DeleteCustomHeader( lpwhr, index );
2870
2871 if (value)
2872 {
2873 HTTPHEADERW hdr;
2874
2875 hdr.lpszField = (LPWSTR)field;
2876 hdr.lpszValue = (LPWSTR)value;
2877 hdr.wFlags = hdr.wCount = 0;
2878
2879 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2880 hdr.wFlags |= HDR_ISREQUEST;
2881
2882 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2883 }
2884
2885 return TRUE;
2886 }
2887 else if (dwModifier & COALESCEFLASG)
2888 {
2889 LPWSTR lpsztmp;
2890 WCHAR ch = 0;
2891 INT len = 0;
2892 INT origlen = strlenW(lphttpHdr->lpszValue);
2893 INT valuelen = strlenW(value);
2894
2895 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2896 {
2897 ch = ',';
2898 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2899 }
2900 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2901 {
2902 ch = ';';
2903 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2904 }
2905
2906 len = origlen + valuelen + ((ch > 0) ? 2 : 0);
2907
2908 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2909 if (lpsztmp)
2910 {
2911 lphttpHdr->lpszValue = lpsztmp;
2912 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2913 if (ch > 0)
2914 {
2915 lphttpHdr->lpszValue[origlen] = ch;
2916 origlen++;
2917 lphttpHdr->lpszValue[origlen] = ' ';
2918 origlen++;
2919 }
2920
2921 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2922 lphttpHdr->lpszValue[len] = '\0';
2923 bSuccess = TRUE;
2924 }
2925 else
2926 {
2927 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2928 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2929 }
2930 }
2931 TRACE("<-- %d\n",bSuccess);
2932 return bSuccess;
2933 }
2934
2935
2936 /***********************************************************************
2937 * HTTP_CloseConnection (internal)
2938 *
2939 * Close socket connection
2940 *
2941 */
2942 static VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2943 {
2944 LPWININETHTTPSESSIONW lpwhs = NULL;
2945 LPWININETAPPINFOW hIC = NULL;
2946
2947 TRACE("%p\n",lpwhr);
2948
2949 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2950 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2951
2952 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2953 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2954
2955 if (NETCON_connected(&lpwhr->netConnection))
2956 {
2957 NETCON_close(&lpwhr->netConnection);
2958 }
2959
2960 INTERNET_SendCallback(&lpwhr->hdr, lpwhr->hdr.dwContext,
2961 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2962 }
2963
2964
2965 /***********************************************************************
2966 * HTTP_CloseHTTPRequestHandle (internal)
2967 *
2968 * Deallocate request handle
2969 *
2970 */
2971 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr)
2972 {
2973 DWORD i;
2974 LPWININETHTTPREQW lpwhr = (LPWININETHTTPREQW) hdr;
2975
2976 TRACE("\n");
2977
2978 if (NETCON_connected(&lpwhr->netConnection))
2979 HTTP_CloseConnection(lpwhr);
2980
2981 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2982 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2983 HeapFree(GetProcessHeap(), 0, lpwhr->lpszRawHeaders);
2984 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVersion);
2985 HeapFree(GetProcessHeap(), 0, lpwhr->lpszStatusText);
2986
2987 for (i = 0; i < lpwhr->nCustHeaders; i++)
2988 {
2989 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2990 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2991 }
2992
2993 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2994 HeapFree(GetProcessHeap(), 0, lpwhr);
2995 }
2996
2997
2998 /***********************************************************************
2999 * HTTP_CloseHTTPSessionHandle (internal)
3000 *
3001 * Deallocate session handle
3002 *
3003 */
3004 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr)
3005 {
3006 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) hdr;
3007
3008 TRACE("%p\n", lpwhs);
3009
3010 HeapFree(GetProcessHeap(), 0, lpwhs->lpszHostName);
3011 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
3012 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
3013 HeapFree(GetProcessHeap(), 0, lpwhs);
3014 }
3015
3016
3017 /***********************************************************************
3018 * HTTP_GetCustomHeaderIndex (internal)
3019 *
3020 * Return index of custom header from header array
3021 *
3022 */
3023 static INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField,int requested_index, BOOL request_only)
3024 {
3025 DWORD index;
3026
3027 TRACE("%s\n", debugstr_w(lpszField));
3028
3029 for (index = 0; index < lpwhr->nCustHeaders; index++)
3030 {
3031 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
3032 {
3033 if ((request_only &&
3034 !(lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST))||
3035 (!request_only &&
3036 (lpwhr->pCustHeaders[index].wFlags & HDR_ISREQUEST)))
3037 continue;
3038
3039 if (requested_index == 0)
3040 break;
3041 else
3042 requested_index --;
3043 }
3044 }
3045
3046 if (index >= lpwhr->nCustHeaders)
3047 index = -1;
3048
3049 TRACE("Return: %ld\n", index);
3050 return index;
3051 }
3052
3053
3054 /***********************************************************************
3055 * HTTP_InsertCustomHeader (internal)
3056 *
3057 * Insert header into array
3058 *
3059 */
3060 static BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
3061 {
3062 INT count;
3063 LPHTTPHEADERW lph = NULL;
3064 BOOL r = FALSE;
3065
3066 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
3067 count = lpwhr->nCustHeaders + 1;
3068 if (count > 1)
3069 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
3070 else
3071 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
3072
3073 if (NULL != lph)
3074 {
3075 lpwhr->pCustHeaders = lph;
3076 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
3077 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
3078 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
3079 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
3080 lpwhr->nCustHeaders++;
3081 r = TRUE;
3082 }
3083 else
3084 {
3085 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
3086 }
3087
3088 return r;
3089 }
3090
3091
3092 /***********************************************************************
3093 * HTTP_DeleteCustomHeader (internal)
3094 *
3095 * Delete header from array
3096 * If this function is called, the indexs may change.
3097 */
3098 static BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, DWORD index)
3099 {
3100 if( lpwhr->nCustHeaders <= 0 )
3101 return FALSE;
3102 if( index >= lpwhr->nCustHeaders )
3103 return FALSE;
3104 lpwhr->nCustHeaders--;
3105
3106 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
3107 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
3108 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
3109
3110 return TRUE;
3111 }
3112
3113 /***********************************************************************
3114 * IsHostInProxyBypassList (@)
3115 *
3116 * Undocumented
3117 *
3118 */
3119 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
3120 {
3121 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);
3122 return FALSE;
3123 }