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