Authors: Mike McCormack <mike@codeweavers.com>, Robert Shearman <rob@codeweavers...
[wine/multimedia.git] / dlls / wininet / http.c
blob0a0a51a4c9a93dd77246e54753b63eff1cc08a36
1 /*
2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
9 * Ulrich Czekalla
10 * Aric Stewart
11 * David Hammerton
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "config.h"
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_SOCKET_H
32 # include <sys/socket.h>
33 #endif
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <errno.h>
41 #include <string.h>
42 #include <time.h>
44 #include "windef.h"
45 #include "winbase.h"
46 #include "wininet.h"
47 #include "winreg.h"
48 #include "winerror.h"
49 #define NO_SHLWAPI_STREAM
50 #include "shlwapi.h"
52 #include "internet.h"
53 #include "wine/debug.h"
54 #include "wine/unicode.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
58 static const WCHAR g_szHttp[] = {' ','H','T','T','P','/','1','.','0',0 };
59 static const WCHAR g_szHost[] = {'\r','\n','H','o','s','t',':',' ',0 };
60 static const WCHAR g_szReferer[] = {'R','e','f','e','r','e','r',0};
61 static const WCHAR g_szAccept[] = {'A','c','c','e','p','t',0};
62 static const WCHAR g_szUserAgent[] = {'U','s','e','r','-','A','g','e','n','t',0};
65 #define HTTPHEADER g_szHttp
66 #define HTTPHOSTHEADER g_szHost
67 #define MAXHOSTNAME 100
68 #define MAX_FIELD_VALUE_LEN 256
69 #define MAX_FIELD_LEN 256
71 #define HTTP_REFERER g_szReferer
72 #define HTTP_ACCEPT g_szAccept
73 #define HTTP_USERAGENT g_szUserAgent
75 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
76 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
77 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
78 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
79 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
80 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
81 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
84 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr);
85 int HTTP_WriteDataToStream(LPWININETHTTPREQW lpwhr,
86 void *Buffer, int BytesToWrite);
87 int HTTP_ReadDataFromStream(LPWININETHTTPREQW lpwhr,
88 void *Buffer, int BytesToRead);
89 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr);
90 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier);
91 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR lpsztmp );
92 void HTTP_CloseConnection(LPWININETHTTPREQW lpwhr);
93 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen);
94 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField);
95 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr);
96 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField);
97 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, INT index);
99 /***********************************************************************
100 * HTTP_HttpAddRequestHeadersW (internal)
102 static BOOL WINAPI HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr,
103 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
105 LPWSTR lpszStart;
106 LPWSTR lpszEnd;
107 LPWSTR buffer;
108 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
109 BOOL bSuccess = FALSE;
111 TRACE("copying header: %s\n", debugstr_w(lpszHeader));
112 buffer = WININET_strdupW(lpszHeader);
113 lpszStart = buffer;
117 lpszEnd = lpszStart;
119 while (*lpszEnd != '\0')
121 if (*lpszEnd == '\r' && *(lpszEnd + 1) == '\n')
122 break;
123 lpszEnd++;
126 if (*lpszStart == '\0')
127 break;
129 if (*lpszEnd == '\r')
131 *lpszEnd = '\0';
132 lpszEnd += 2; /* Jump over \r\n */
134 TRACE("interpreting header %s\n", debugstr_w(lpszStart));
135 if (HTTP_InterpretHttpHeader(lpszStart, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
136 bSuccess = HTTP_ProcessHeader(lpwhr, field, value, dwModifier | HTTP_ADDHDR_FLAG_REQ);
138 lpszStart = lpszEnd;
140 } while (bSuccess);
142 HeapFree(GetProcessHeap(), 0, buffer);
144 return bSuccess;
147 /***********************************************************************
148 * HttpAddRequestHeadersW (WININET.@)
150 * Adds one or more HTTP header to the request handler
152 * RETURNS
153 * TRUE on success
154 * FALSE on failure
157 BOOL WINAPI HttpAddRequestHeadersW(HINTERNET hHttpRequest,
158 LPCWSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
160 BOOL bSuccess = FALSE;
161 LPWININETHTTPREQW lpwhr;
163 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_w(lpszHeader), dwHeaderLength,
164 dwModifier);
166 if (!lpszHeader)
167 return TRUE;
169 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
170 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
172 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
173 return bSuccess;
175 bSuccess = HTTP_HttpAddRequestHeadersW( lpwhr, lpszHeader, dwHeaderLength, dwModifier );
177 return bSuccess;
180 /***********************************************************************
181 * HttpAddRequestHeadersA (WININET.@)
183 * Adds one or more HTTP header to the request handler
185 * RETURNS
186 * TRUE on success
187 * FALSE on failure
190 BOOL WINAPI HttpAddRequestHeadersA(HINTERNET hHttpRequest,
191 LPCSTR lpszHeader, DWORD dwHeaderLength, DWORD dwModifier)
193 DWORD len;
194 LPWSTR hdr;
195 BOOL r;
197 TRACE("%p, %s, %li, %li\n", hHttpRequest, debugstr_a(lpszHeader), dwHeaderLength,
198 dwModifier);
200 len = MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, NULL, 0 );
201 hdr = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
202 MultiByteToWideChar( CP_ACP, 0, lpszHeader, dwHeaderLength, hdr, len );
203 if( dwHeaderLength != -1 )
204 dwHeaderLength = len;
206 r = HttpAddRequestHeadersW( hHttpRequest, hdr, dwHeaderLength, dwModifier );
208 HeapFree( GetProcessHeap(), 0, hdr );
210 return r;
213 /***********************************************************************
214 * HttpEndRequestA (WININET.@)
216 * Ends an HTTP request that was started by HttpSendRequestEx
218 * RETURNS
219 * TRUE if successful
220 * FALSE on failure
223 BOOL WINAPI HttpEndRequestA(HINTERNET hRequest, LPINTERNET_BUFFERSA lpBuffersOut,
224 DWORD dwFlags, DWORD dwContext)
226 FIXME("stub\n");
227 return FALSE;
230 /***********************************************************************
231 * HttpEndRequestW (WININET.@)
233 * Ends an HTTP request that was started by HttpSendRequestEx
235 * RETURNS
236 * TRUE if successful
237 * FALSE on failure
240 BOOL WINAPI HttpEndRequestW(HINTERNET hRequest, LPINTERNET_BUFFERSW lpBuffersOut,
241 DWORD dwFlags, DWORD dwContext)
243 FIXME("stub\n");
244 return FALSE;
247 /***********************************************************************
248 * HttpOpenRequestW (WININET.@)
250 * Open a HTTP request handle
252 * RETURNS
253 * HINTERNET a HTTP request handle on success
254 * NULL on failure
257 HINTERNET WINAPI HttpOpenRequestW(HINTERNET hHttpSession,
258 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
259 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
260 DWORD dwFlags, DWORD dwContext)
262 LPWININETHTTPSESSIONW lpwhs;
263 LPWININETAPPINFOW hIC = NULL;
264 HINTERNET handle = NULL;
266 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
267 debugstr_w(lpszVerb), debugstr_w(lpszObjectName),
268 debugstr_w(lpszVersion), debugstr_w(lpszReferrer), lpszAcceptTypes,
269 dwFlags, dwContext);
270 if(lpszAcceptTypes!=NULL)
272 int i;
273 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
274 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes[i]));
277 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
278 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
280 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
281 return NULL;
283 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
286 * My tests seem to show that the windows version does not
287 * become asynchronous until after this point. And anyhow
288 * if this call was asynchronous then how would you get the
289 * necessary HINTERNET pointer returned by this function.
291 * I am leaving this here just in case I am wrong
293 * if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
295 if (0)
297 WORKREQUEST workRequest;
298 struct WORKREQ_HTTPOPENREQUESTW *req;
300 workRequest.asyncall = HTTPOPENREQUESTW;
301 workRequest.handle = hHttpSession;
302 req = &workRequest.u.HttpOpenRequestW;
303 req->lpszVerb = WININET_strdupW(lpszVerb);
304 req->lpszObjectName = WININET_strdupW(lpszObjectName);
305 if (lpszVersion)
306 req->lpszVersion = WININET_strdupW(lpszVersion);
307 else
308 req->lpszVersion = 0;
309 if (lpszReferrer)
310 req->lpszReferrer = WININET_strdupW(lpszReferrer);
311 else
312 req->lpszReferrer = 0;
313 req->lpszAcceptTypes = lpszAcceptTypes;
314 req->dwFlags = dwFlags;
315 req->dwContext = dwContext;
317 INTERNET_AsyncCall(&workRequest);
319 else
321 handle = HTTP_HttpOpenRequestW(hHttpSession, lpszVerb, lpszObjectName,
322 lpszVersion, lpszReferrer, lpszAcceptTypes,
323 dwFlags, dwContext);
325 TRACE("returning %p\n", handle);
326 return handle;
330 /***********************************************************************
331 * HttpOpenRequestA (WININET.@)
333 * Open a HTTP request handle
335 * RETURNS
336 * HINTERNET a HTTP request handle on success
337 * NULL on failure
340 HINTERNET WINAPI HttpOpenRequestA(HINTERNET hHttpSession,
341 LPCSTR lpszVerb, LPCSTR lpszObjectName, LPCSTR lpszVersion,
342 LPCSTR lpszReferrer , LPCSTR *lpszAcceptTypes,
343 DWORD dwFlags, DWORD dwContext)
345 LPWSTR szVerb = NULL, szObjectName = NULL;
346 LPWSTR szVersion = NULL, szReferrer = NULL, *szAcceptTypes = NULL;
347 INT len;
348 INT acceptTypesCount;
349 HINTERNET rc = FALSE;
350 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession,
351 debugstr_a(lpszVerb), debugstr_a(lpszObjectName),
352 debugstr_a(lpszVersion), debugstr_a(lpszReferrer), lpszAcceptTypes,
353 dwFlags, dwContext);
355 if (lpszVerb)
357 len = MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, NULL, 0 );
358 szVerb = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
359 if ( !szVerb )
360 goto end;
361 MultiByteToWideChar(CP_ACP, 0, lpszVerb, -1, szVerb, len);
364 if (lpszObjectName)
366 len = MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, NULL, 0 );
367 szObjectName = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR) );
368 if ( !szObjectName )
369 goto end;
370 MultiByteToWideChar(CP_ACP, 0, lpszObjectName, -1, szObjectName, len );
373 if (lpszVersion)
375 len = MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, NULL, 0 );
376 szVersion = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
377 if ( !szVersion )
378 goto end;
379 MultiByteToWideChar(CP_ACP, 0, lpszVersion, -1, szVersion, len );
382 if (lpszReferrer)
384 len = MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, NULL, 0 );
385 szReferrer = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
386 if ( !szReferrer )
387 goto end;
388 MultiByteToWideChar(CP_ACP, 0, lpszReferrer, -1, szReferrer, len );
391 acceptTypesCount = 0;
392 if (lpszAcceptTypes)
394 /* find out how many there are */
395 while (lpszAcceptTypes[acceptTypesCount])
396 acceptTypesCount++;
397 szAcceptTypes = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR *) * (acceptTypesCount+1));
398 acceptTypesCount = 0;
399 while (lpszAcceptTypes[acceptTypesCount])
401 len = MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
402 -1, NULL, 0 );
403 szAcceptTypes[acceptTypesCount] = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
404 if (!szAcceptTypes[acceptTypesCount] )
405 goto end;
406 MultiByteToWideChar(CP_ACP, 0, lpszAcceptTypes[acceptTypesCount],
407 -1, szAcceptTypes[acceptTypesCount], len );
408 acceptTypesCount++;
410 szAcceptTypes[acceptTypesCount] = NULL;
412 else szAcceptTypes = 0;
414 rc = HttpOpenRequestW(hHttpSession, szVerb, szObjectName,
415 szVersion, szReferrer,
416 (LPCWSTR*)szAcceptTypes, dwFlags, dwContext);
418 end:
419 if (szAcceptTypes)
421 acceptTypesCount = 0;
422 while (szAcceptTypes[acceptTypesCount])
424 HeapFree(GetProcessHeap(), 0, szAcceptTypes[acceptTypesCount]);
425 acceptTypesCount++;
427 HeapFree(GetProcessHeap(), 0, szAcceptTypes);
429 if (szReferrer) HeapFree(GetProcessHeap(), 0, szReferrer);
430 if (szVersion) HeapFree(GetProcessHeap(), 0, szVersion);
431 if (szObjectName) HeapFree(GetProcessHeap(), 0, szObjectName);
432 if (szVerb) HeapFree(GetProcessHeap(), 0, szVerb);
434 return rc;
437 /***********************************************************************
438 * HTTP_Base64
440 static UINT HTTP_Base64( LPCWSTR bin, LPWSTR base64 )
442 UINT n = 0, x;
443 static LPSTR HTTP_Base64Enc =
444 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
446 while( bin[0] )
448 /* first 6 bits, all from bin[0] */
449 base64[n++] = HTTP_Base64Enc[(bin[0] & 0xfc) >> 2];
450 x = (bin[0] & 3) << 4;
452 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
453 if( !bin[1] )
455 base64[n++] = HTTP_Base64Enc[x];
456 base64[n++] = '=';
457 base64[n++] = '=';
458 break;
460 base64[n++] = HTTP_Base64Enc[ x | ( (bin[1]&0xf0) >> 4 ) ];
461 x = ( bin[1] & 0x0f ) << 2;
463 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
464 if( !bin[2] )
466 base64[n++] = HTTP_Base64Enc[x];
467 base64[n++] = '=';
468 break;
470 base64[n++] = HTTP_Base64Enc[ x | ( (bin[2]&0xc0 ) >> 6 ) ];
472 /* last 6 bits, all from bin [2] */
473 base64[n++] = HTTP_Base64Enc[ bin[2] & 0x3f ];
474 bin += 3;
476 base64[n] = 0;
477 return n;
480 /***********************************************************************
481 * HTTP_EncodeBasicAuth
483 * Encode the basic authentication string for HTTP 1.1
485 static LPWSTR HTTP_EncodeBasicAuth( LPCWSTR username, LPCWSTR password)
487 UINT len;
488 LPWSTR in, out;
489 static const WCHAR szBasic[] = {'B','a','s','i','c',' ',0};
490 static const WCHAR szColon[] = {':',0};
492 len = lstrlenW( username ) + 1 + lstrlenW ( password ) + 1;
493 in = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
494 if( !in )
495 return NULL;
497 len = lstrlenW(szBasic) +
498 (lstrlenW( username ) + 1 + lstrlenW ( password ))*2 + 1 + 1;
499 out = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
500 if( out )
502 lstrcpyW( in, username );
503 lstrcatW( in, szColon );
504 lstrcatW( in, password );
505 lstrcpyW( out, szBasic );
506 HTTP_Base64( in, &out[strlenW(out)] );
508 HeapFree( GetProcessHeap(), 0, in );
510 return out;
513 /***********************************************************************
514 * HTTP_InsertProxyAuthorization
516 * Insert the basic authorization field in the request header
518 BOOL HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr,
519 LPCWSTR username, LPCWSTR password )
521 HTTPHEADERW hdr;
522 INT index;
523 static const WCHAR szProxyAuthorization[] = {
524 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
526 hdr.lpszValue = HTTP_EncodeBasicAuth( username, password );
527 hdr.lpszField = (WCHAR *)szProxyAuthorization;
528 hdr.wFlags = HDR_ISREQUEST;
529 hdr.wCount = 0;
530 if( !hdr.lpszValue )
531 return FALSE;
533 TRACE("Inserting %s = %s\n",
534 debugstr_w( hdr.lpszField ), debugstr_w( hdr.lpszValue ) );
536 /* remove the old proxy authorization header */
537 index = HTTP_GetCustomHeaderIndex( lpwhr, hdr.lpszField );
538 if( index >=0 )
539 HTTP_DeleteCustomHeader( lpwhr, index );
541 HTTP_InsertCustomHeader(lpwhr, &hdr);
542 HeapFree( GetProcessHeap(), 0, hdr.lpszValue );
544 return TRUE;
547 /***********************************************************************
548 * HTTP_DealWithProxy
550 static BOOL HTTP_DealWithProxy( LPWININETAPPINFOW hIC,
551 LPWININETHTTPSESSIONW lpwhs, LPWININETHTTPREQW lpwhr)
553 WCHAR buf[MAXHOSTNAME];
554 WCHAR proxy[MAXHOSTNAME + 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
555 WCHAR* url;
556 static const WCHAR szNul[] = { 0 };
557 URL_COMPONENTSW UrlComponents;
558 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/',0 }, szSlash[] = { '/',0 } ;
559 static const WCHAR szFormat1[] = { 'h','t','t','p',':','/','/','%','s',0 };
560 static const WCHAR szFormat2[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
561 int len;
563 memset( &UrlComponents, 0, sizeof UrlComponents );
564 UrlComponents.dwStructSize = sizeof UrlComponents;
565 UrlComponents.lpszHostName = buf;
566 UrlComponents.dwHostNameLength = MAXHOSTNAME;
568 if( CSTR_EQUAL != CompareStringW(LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
569 buf,strlenW(szHttp),szHttp,strlenW(szHttp)) )
570 sprintfW(proxy, szFormat1, hIC->lpszProxy);
571 else
572 strcpyW(proxy,buf);
573 if( !InternetCrackUrlW(proxy, 0, 0, &UrlComponents) )
574 return FALSE;
575 if( UrlComponents.dwHostNameLength == 0 )
576 return FALSE;
578 if( !lpwhr->lpszPath )
579 lpwhr->lpszPath = (LPWSTR)szNul;
580 TRACE("server='%s' path='%s'\n",
581 debugstr_w(lpwhs->lpszServerName), debugstr_w(lpwhr->lpszPath));
582 /* for constant 15 see above */
583 len = strlenW(lpwhs->lpszServerName) + strlenW(lpwhr->lpszPath) + 15;
584 url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
586 if(UrlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
587 UrlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
589 sprintfW(url, szFormat2, lpwhs->lpszServerName, lpwhs->nServerPort);
591 if( lpwhr->lpszPath[0] != '/' )
592 strcatW( url, szSlash );
593 strcatW(url, lpwhr->lpszPath);
594 if(lpwhr->lpszPath != szNul)
595 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
596 lpwhr->lpszPath = url;
597 /* FIXME: Do I have to free lpwhs->lpszServerName here ? */
598 lpwhs->lpszServerName = WININET_strdupW(UrlComponents.lpszHostName);
599 lpwhs->nServerPort = UrlComponents.nPort;
601 return TRUE;
604 /***********************************************************************
605 * HTTP_HttpOpenRequestW (internal)
607 * Open a HTTP request handle
609 * RETURNS
610 * HINTERNET a HTTP request handle on success
611 * NULL on failure
614 HINTERNET WINAPI HTTP_HttpOpenRequestW(HINTERNET hHttpSession,
615 LPCWSTR lpszVerb, LPCWSTR lpszObjectName, LPCWSTR lpszVersion,
616 LPCWSTR lpszReferrer , LPCWSTR *lpszAcceptTypes,
617 DWORD dwFlags, DWORD dwContext)
619 LPWININETHTTPSESSIONW lpwhs;
620 LPWININETAPPINFOW hIC = NULL;
621 LPWININETHTTPREQW lpwhr;
622 LPWSTR lpszCookies;
623 LPWSTR lpszUrl = NULL;
624 DWORD nCookieSize;
625 HINTERNET handle;
626 static const WCHAR szUrlForm[] = {'h','t','t','p',':','/','/','%','s',0};
627 DWORD len;
629 TRACE("--> \n");
631 lpwhs = (LPWININETHTTPSESSIONW) WININET_GetObject( hHttpSession );
632 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
634 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
635 return NULL;
638 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
640 lpwhr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPREQW));
641 if (NULL == lpwhr)
643 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
644 return NULL;
646 handle = WININET_AllocHandle( &lpwhr->hdr );
647 if (NULL == handle)
649 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
650 HeapFree( GetProcessHeap(), 0, lpwhr );
651 return NULL;
654 lpwhr->hdr.htype = WH_HHTTPREQ;
655 lpwhr->hdr.lpwhparent = &lpwhs->hdr;
656 lpwhr->hdr.dwFlags = dwFlags;
657 lpwhr->hdr.dwContext = dwContext;
658 NETCON_init(&lpwhr->netConnection, dwFlags & INTERNET_FLAG_SECURE);
660 if (NULL != lpszObjectName && strlenW(lpszObjectName)) {
661 HRESULT rc;
663 len = 0;
664 rc = UrlEscapeW(lpszObjectName, NULL, &len, URL_ESCAPE_SPACES_ONLY);
665 if (rc != E_POINTER)
666 len = strlenW(lpszObjectName)+1;
667 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
668 rc = UrlEscapeW(lpszObjectName, lpwhr->lpszPath, &len,
669 URL_ESCAPE_SPACES_ONLY);
670 if (rc)
672 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName),rc);
673 strcpyW(lpwhr->lpszPath,lpszObjectName);
677 if (NULL != lpszReferrer && strlenW(lpszReferrer))
678 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpszReferrer, HTTP_ADDHDR_FLAG_COALESCE);
680 if(lpszAcceptTypes!=NULL)
682 int i;
683 for(i=0;lpszAcceptTypes[i]!=NULL;i++)
684 HTTP_ProcessHeader(lpwhr, HTTP_ACCEPT, lpszAcceptTypes[i], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_REQ|HTTP_ADDHDR_FLAG_ADD_IF_NEW);
687 if (NULL == lpszVerb)
689 static const WCHAR szGet[] = {'G','E','T',0};
690 lpwhr->lpszVerb = WININET_strdupW(szGet);
692 else if (strlenW(lpszVerb))
693 lpwhr->lpszVerb = WININET_strdupW(lpszVerb);
695 if (NULL != lpszReferrer && strlenW(lpszReferrer))
697 WCHAR buf[MAXHOSTNAME];
698 URL_COMPONENTSW UrlComponents;
700 memset( &UrlComponents, 0, sizeof UrlComponents );
701 UrlComponents.dwStructSize = sizeof UrlComponents;
702 UrlComponents.lpszHostName = buf;
703 UrlComponents.dwHostNameLength = MAXHOSTNAME;
705 InternetCrackUrlW(lpszReferrer, 0, 0, &UrlComponents);
706 if (strlenW(UrlComponents.lpszHostName))
707 lpwhr->lpszHostName = WININET_strdupW(UrlComponents.lpszHostName);
708 } else {
709 lpwhr->lpszHostName = WININET_strdupW(lpwhs->lpszServerName);
711 if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
712 HTTP_DealWithProxy( hIC, lpwhs, lpwhr );
714 if (hIC->lpszAgent)
716 WCHAR *agent_header;
717 static const WCHAR user_agent[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
719 len = strlenW(hIC->lpszAgent) + strlenW(user_agent);
720 agent_header = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
721 sprintfW(agent_header, user_agent, hIC->lpszAgent );
723 HTTP_HttpAddRequestHeadersW(lpwhr, agent_header, strlenW(agent_header),
724 HTTP_ADDREQ_FLAG_ADD);
725 HeapFree(GetProcessHeap(), 0, agent_header);
728 len = strlenW(lpwhr->lpszHostName) + strlenW(szUrlForm);
729 lpszUrl = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
730 sprintfW( lpszUrl, szUrlForm, lpwhr->lpszHostName );
732 if (InternetGetCookieW(lpszUrl, NULL, NULL, &nCookieSize))
734 int cnt = 0;
735 static const WCHAR szCookie[] = {'C','o','o','k','i','e',':',' ',0};
736 static const WCHAR szcrlf[] = {'\r','\n',0};
738 lpszCookies = HeapAlloc(GetProcessHeap(), 0, (nCookieSize + 1 + 8)*sizeof(WCHAR));
740 cnt += sprintfW(lpszCookies, szCookie);
741 InternetGetCookieW(lpszUrl, NULL, lpszCookies + cnt, &nCookieSize);
742 strcatW(lpszCookies, szcrlf);
744 HTTP_HttpAddRequestHeadersW(lpwhr, lpszCookies, strlenW(lpszCookies),
745 HTTP_ADDREQ_FLAG_ADD);
746 HeapFree(GetProcessHeap(), 0, lpszCookies);
748 HeapFree(GetProcessHeap(), 0, lpszUrl);
752 if (hIC->lpfnStatusCB)
754 INTERNET_ASYNC_RESULT iar;
756 iar.dwResult = (DWORD)handle;
757 iar.dwError = ERROR_SUCCESS;
759 SendAsyncCallback(hIC, hHttpSession, dwContext,
760 INTERNET_STATUS_HANDLE_CREATED, &iar,
761 sizeof(INTERNET_ASYNC_RESULT));
765 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
769 * According to my tests. The name is not resolved until a request is Opened
771 SendAsyncCallback(hIC, hHttpSession, dwContext,
772 INTERNET_STATUS_RESOLVING_NAME,
773 lpwhs->lpszServerName,
774 strlenW(lpwhs->lpszServerName)+1);
775 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
776 &lpwhs->phostent, &lpwhs->socketAddress))
778 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
779 return NULL;
782 SendAsyncCallback(hIC, hHttpSession, lpwhr->hdr.dwContext,
783 INTERNET_STATUS_NAME_RESOLVED,
784 &(lpwhs->socketAddress),
785 sizeof(struct sockaddr_in));
787 TRACE("<-- %p\n", handle);
788 return handle;
791 /***********************************************************************
792 * HTTP_build_resp_header (internal)
794 * This function reconstructs the response header. It takes an array
795 * of strings in the response buffer, and the count of those strings.
796 * A null pointer in the array represents a seperator.
798 * RETURNS:
799 * a buffer allocated and initialized with the reconstructed response
800 * *pSize is set to the number of wide characters in the returned buffer
802 static LPWSTR HTTP_build_resp_header(
803 LPCWSTR *str, DWORD count, BOOL bUseCrlf, DWORD *pSize )
805 static const WCHAR szcrlf[] = { '\r','\n',0 };
806 DWORD len, i;
807 LPWSTR ret;
809 /* calculate the length of the response buffer */
810 len = 0;
811 for( i=0; i<count; i++ )
813 if( str[i] )
814 len += strlenW( str[i] );
815 else if( bUseCrlf )
816 len += 2;
817 else
818 len ++;
820 len++;
822 /* fill the buffer in */
823 ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
824 len = 0;
825 for( i=0; i<count; i++ )
827 if( str[i] )
829 strcpyW( &ret[len], str[i] );
830 len += strlenW( str[i] );
832 else if( bUseCrlf )
834 strcpyW( &ret[len], szcrlf );
835 len += 2;
837 else
838 ret[len++] = 0;
840 ret[len++] = 0;
841 *pSize = len;
842 return ret;
845 /***********************************************************************
846 * HTTP_query_raw_headers (internal)
848 * Reconstruct the raw HTTP header and copy it into the buffer provided
850 static BOOL HTTP_query_raw_headers( LPWININETHTTPREQW lpwhr, BOOL bUseCrlf,
851 LPVOID lpBuffer, LPDWORD lpdwBufferLength )
853 static const WCHAR szColonSpace[] = { ':',' ',0 };
854 static const WCHAR szSpace[] = { ' ',0 };
855 BOOL bSuccess = FALSE;
856 LPCWSTR *str;
857 DWORD i, n, size = 0;
858 LPWSTR hdr;
860 n = 7 + ( HTTP_QUERY_MAX + 1 + lpwhr->nCustHeaders )*4 ;
861 str = HeapAlloc( GetProcessHeap(), 0, sizeof(LPCWSTR)*n );
862 n = 0;
864 /* reconstruct the status line */
865 str[n++] = lpwhr->StdHeaders[HTTP_QUERY_VERSION].lpszValue;
866 str[n++] = szSpace;
867 str[n++] = lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE].lpszValue;
868 str[n++] = szSpace;
869 str[n++] = lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT].lpszValue;
870 str[n++] = NULL;
872 /* Append standard request heades */
873 for (i = 0; i <= HTTP_QUERY_MAX; i++)
875 if( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
876 continue;
877 if( !lpwhr->StdHeaders[i].lpszField )
878 continue;
879 if( !lpwhr->StdHeaders[i].lpszValue )
880 continue;
881 /* ignore the stuff that's in the status line */
882 if( ( i == HTTP_QUERY_VERSION ) ||
883 ( i == HTTP_QUERY_STATUS_CODE ) ||
884 ( i == HTTP_QUERY_STATUS_TEXT ) )
885 continue;
886 str[n++] = lpwhr->StdHeaders[i].lpszField;
887 str[n++] = szColonSpace;
888 str[n++] = lpwhr->StdHeaders[i].lpszValue,
889 str[n++] = NULL;
892 /* Append custom request heades */
893 for (i = 0; i < lpwhr->nCustHeaders; i++)
895 if( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
896 continue;
897 if( !lpwhr->pCustHeaders[i].lpszField )
898 continue;
899 if( !lpwhr->pCustHeaders[i].lpszValue )
900 continue;
901 str[n++] = lpwhr->pCustHeaders[i].lpszField;
902 str[n++] = szColonSpace;
903 str[n++] = lpwhr->pCustHeaders[i].lpszValue;
904 str[n++] = NULL;
906 str[n++] = NULL;
908 /* concatenate all the strings together */
909 hdr = HTTP_build_resp_header( str, n, bUseCrlf, &size );
910 HeapFree( GetProcessHeap(), 0, str );
912 /* check that the target buffer is big enough */
913 if ( size > (*lpdwBufferLength/sizeof(WCHAR)) )
914 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
915 else
917 memcpy( lpBuffer, hdr, size*sizeof(WCHAR) );
918 bSuccess = TRUE;
920 HeapFree( GetProcessHeap(), 0, hdr );
921 *lpdwBufferLength = size*sizeof(WCHAR);
923 return bSuccess;
926 /***********************************************************************
927 * HTTP_HttpQueryInfoW (internal)
929 BOOL WINAPI HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr, DWORD dwInfoLevel,
930 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
932 LPHTTPHEADERW lphttpHdr = NULL;
933 BOOL bSuccess = FALSE;
935 /* Find requested header structure */
936 if ((dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK) == HTTP_QUERY_CUSTOM)
938 INT index = HTTP_GetCustomHeaderIndex(lpwhr, (LPWSTR)lpBuffer);
940 if (index < 0)
941 return bSuccess;
943 lphttpHdr = &lpwhr->pCustHeaders[index];
945 else
947 INT index = dwInfoLevel & ~HTTP_QUERY_MODIFIER_FLAGS_MASK;
949 if (index == HTTP_QUERY_RAW_HEADERS_CRLF)
950 return HTTP_query_raw_headers(lpwhr, TRUE, lpBuffer, lpdwBufferLength );
951 if (index == HTTP_QUERY_RAW_HEADERS)
952 return HTTP_query_raw_headers(lpwhr, FALSE, lpBuffer, lpdwBufferLength );
953 else if (index >= 0 && index <= HTTP_QUERY_MAX && lpwhr->StdHeaders[index].lpszValue)
955 lphttpHdr = &lpwhr->StdHeaders[index];
957 else
959 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
960 return bSuccess;
964 /* Ensure header satisifies requested attributes */
965 if ((dwInfoLevel & HTTP_QUERY_FLAG_REQUEST_HEADERS) &&
966 (~lphttpHdr->wFlags & HDR_ISREQUEST))
968 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
969 return bSuccess;
972 /* coalesce value to reuqested type */
973 if (dwInfoLevel & HTTP_QUERY_FLAG_NUMBER)
975 *(int *)lpBuffer = atoiW(lphttpHdr->lpszValue);
976 bSuccess = TRUE;
978 TRACE(" returning number : %d\n", *(int *)lpBuffer);
980 else if (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME)
982 time_t tmpTime;
983 struct tm tmpTM;
984 SYSTEMTIME *STHook;
986 tmpTime = ConvertTimeString(lphttpHdr->lpszValue);
988 tmpTM = *gmtime(&tmpTime);
989 STHook = (SYSTEMTIME *) lpBuffer;
990 if(STHook==NULL)
991 return bSuccess;
993 STHook->wDay = tmpTM.tm_mday;
994 STHook->wHour = tmpTM.tm_hour;
995 STHook->wMilliseconds = 0;
996 STHook->wMinute = tmpTM.tm_min;
997 STHook->wDayOfWeek = tmpTM.tm_wday;
998 STHook->wMonth = tmpTM.tm_mon + 1;
999 STHook->wSecond = tmpTM.tm_sec;
1000 STHook->wYear = tmpTM.tm_year;
1002 bSuccess = TRUE;
1004 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1005 STHook->wYear, STHook->wMonth, STHook->wDay, STHook->wDayOfWeek,
1006 STHook->wHour, STHook->wMinute, STHook->wSecond, STHook->wMilliseconds);
1008 else if (dwInfoLevel & HTTP_QUERY_FLAG_COALESCE)
1010 if (*lpdwIndex >= lphttpHdr->wCount)
1012 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND);
1014 else
1016 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1017 (*lpdwIndex)++;
1020 else
1022 INT len = (strlenW(lphttpHdr->lpszValue) + 1) * sizeof(WCHAR);
1024 if (len > *lpdwBufferLength)
1026 *lpdwBufferLength = len;
1027 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER);
1028 return bSuccess;
1031 memcpy(lpBuffer, lphttpHdr->lpszValue, len);
1032 *lpdwBufferLength = len - sizeof(WCHAR);
1033 bSuccess = TRUE;
1035 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer));
1037 return bSuccess;
1040 /***********************************************************************
1041 * HttpQueryInfoW (WININET.@)
1043 * Queries for information about an HTTP request
1045 * RETURNS
1046 * TRUE on success
1047 * FALSE on failure
1050 BOOL WINAPI HttpQueryInfoW(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1051 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1053 BOOL bSuccess = FALSE;
1054 LPWININETHTTPREQW lpwhr;
1056 if (TRACE_ON(wininet)) {
1057 #define FE(x) { x, #x }
1058 static const wininet_flag_info query_flags[] = {
1059 FE(HTTP_QUERY_MIME_VERSION),
1060 FE(HTTP_QUERY_CONTENT_TYPE),
1061 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING),
1062 FE(HTTP_QUERY_CONTENT_ID),
1063 FE(HTTP_QUERY_CONTENT_DESCRIPTION),
1064 FE(HTTP_QUERY_CONTENT_LENGTH),
1065 FE(HTTP_QUERY_CONTENT_LANGUAGE),
1066 FE(HTTP_QUERY_ALLOW),
1067 FE(HTTP_QUERY_PUBLIC),
1068 FE(HTTP_QUERY_DATE),
1069 FE(HTTP_QUERY_EXPIRES),
1070 FE(HTTP_QUERY_LAST_MODIFIED),
1071 FE(HTTP_QUERY_MESSAGE_ID),
1072 FE(HTTP_QUERY_URI),
1073 FE(HTTP_QUERY_DERIVED_FROM),
1074 FE(HTTP_QUERY_COST),
1075 FE(HTTP_QUERY_LINK),
1076 FE(HTTP_QUERY_PRAGMA),
1077 FE(HTTP_QUERY_VERSION),
1078 FE(HTTP_QUERY_STATUS_CODE),
1079 FE(HTTP_QUERY_STATUS_TEXT),
1080 FE(HTTP_QUERY_RAW_HEADERS),
1081 FE(HTTP_QUERY_RAW_HEADERS_CRLF),
1082 FE(HTTP_QUERY_CONNECTION),
1083 FE(HTTP_QUERY_ACCEPT),
1084 FE(HTTP_QUERY_ACCEPT_CHARSET),
1085 FE(HTTP_QUERY_ACCEPT_ENCODING),
1086 FE(HTTP_QUERY_ACCEPT_LANGUAGE),
1087 FE(HTTP_QUERY_AUTHORIZATION),
1088 FE(HTTP_QUERY_CONTENT_ENCODING),
1089 FE(HTTP_QUERY_FORWARDED),
1090 FE(HTTP_QUERY_FROM),
1091 FE(HTTP_QUERY_IF_MODIFIED_SINCE),
1092 FE(HTTP_QUERY_LOCATION),
1093 FE(HTTP_QUERY_ORIG_URI),
1094 FE(HTTP_QUERY_REFERER),
1095 FE(HTTP_QUERY_RETRY_AFTER),
1096 FE(HTTP_QUERY_SERVER),
1097 FE(HTTP_QUERY_TITLE),
1098 FE(HTTP_QUERY_USER_AGENT),
1099 FE(HTTP_QUERY_WWW_AUTHENTICATE),
1100 FE(HTTP_QUERY_PROXY_AUTHENTICATE),
1101 FE(HTTP_QUERY_ACCEPT_RANGES),
1102 FE(HTTP_QUERY_SET_COOKIE),
1103 FE(HTTP_QUERY_COOKIE),
1104 FE(HTTP_QUERY_REQUEST_METHOD),
1105 FE(HTTP_QUERY_REFRESH),
1106 FE(HTTP_QUERY_CONTENT_DISPOSITION),
1107 FE(HTTP_QUERY_AGE),
1108 FE(HTTP_QUERY_CACHE_CONTROL),
1109 FE(HTTP_QUERY_CONTENT_BASE),
1110 FE(HTTP_QUERY_CONTENT_LOCATION),
1111 FE(HTTP_QUERY_CONTENT_MD5),
1112 FE(HTTP_QUERY_CONTENT_RANGE),
1113 FE(HTTP_QUERY_ETAG),
1114 FE(HTTP_QUERY_HOST),
1115 FE(HTTP_QUERY_IF_MATCH),
1116 FE(HTTP_QUERY_IF_NONE_MATCH),
1117 FE(HTTP_QUERY_IF_RANGE),
1118 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE),
1119 FE(HTTP_QUERY_MAX_FORWARDS),
1120 FE(HTTP_QUERY_PROXY_AUTHORIZATION),
1121 FE(HTTP_QUERY_RANGE),
1122 FE(HTTP_QUERY_TRANSFER_ENCODING),
1123 FE(HTTP_QUERY_UPGRADE),
1124 FE(HTTP_QUERY_VARY),
1125 FE(HTTP_QUERY_VIA),
1126 FE(HTTP_QUERY_WARNING),
1127 FE(HTTP_QUERY_CUSTOM)
1129 static const wininet_flag_info modifier_flags[] = {
1130 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS),
1131 FE(HTTP_QUERY_FLAG_SYSTEMTIME),
1132 FE(HTTP_QUERY_FLAG_NUMBER),
1133 FE(HTTP_QUERY_FLAG_COALESCE)
1135 #undef FE
1136 DWORD info_mod = dwInfoLevel & HTTP_QUERY_MODIFIER_FLAGS_MASK;
1137 DWORD info = dwInfoLevel & HTTP_QUERY_HEADER_MASK;
1138 int i;
1140 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest, dwInfoLevel, dwInfoLevel);
1141 TRACE(" Attribute:");
1142 for (i = 0; i < (sizeof(query_flags) / sizeof(query_flags[0])); i++) {
1143 if (query_flags[i].val == info) {
1144 DPRINTF(" %s", query_flags[i].name);
1145 break;
1148 if (i == (sizeof(query_flags) / sizeof(query_flags[0]))) {
1149 DPRINTF(" Unknown (%08lx)", info);
1152 DPRINTF(" Modifier:");
1153 for (i = 0; i < (sizeof(modifier_flags) / sizeof(modifier_flags[0])); i++) {
1154 if (modifier_flags[i].val & info_mod) {
1155 DPRINTF(" %s", modifier_flags[i].name);
1156 info_mod &= ~ modifier_flags[i].val;
1160 if (info_mod) {
1161 DPRINTF(" Unknown (%08lx)", info_mod);
1163 DPRINTF("\n");
1166 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1167 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1169 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1170 return FALSE;
1173 bSuccess = HTTP_HttpQueryInfoW( lpwhr, dwInfoLevel,
1174 lpBuffer, lpdwBufferLength, lpdwIndex);
1176 TRACE("%d <--\n", bSuccess);
1177 return bSuccess;
1180 /***********************************************************************
1181 * HttpQueryInfoA (WININET.@)
1183 * Queries for information about an HTTP request
1185 * RETURNS
1186 * TRUE on success
1187 * FALSE on failure
1190 BOOL WINAPI HttpQueryInfoA(HINTERNET hHttpRequest, DWORD dwInfoLevel,
1191 LPVOID lpBuffer, LPDWORD lpdwBufferLength, LPDWORD lpdwIndex)
1193 BOOL result;
1194 DWORD len;
1195 WCHAR* bufferW;
1197 if((dwInfoLevel & HTTP_QUERY_FLAG_NUMBER) ||
1198 (dwInfoLevel & HTTP_QUERY_FLAG_SYSTEMTIME))
1200 return HttpQueryInfoW( hHttpRequest, dwInfoLevel, lpBuffer,
1201 lpdwBufferLength, lpdwIndex );
1204 len = (*lpdwBufferLength)*sizeof(WCHAR);
1205 bufferW = HeapAlloc( GetProcessHeap(), 0, len );
1206 result = HttpQueryInfoW( hHttpRequest, dwInfoLevel, bufferW,
1207 &len, lpdwIndex );
1208 if( result )
1210 len = WideCharToMultiByte( CP_ACP,0, bufferW, len / sizeof(WCHAR),
1211 lpBuffer, *lpdwBufferLength, NULL, NULL );
1212 *lpdwBufferLength = len * sizeof(WCHAR);
1214 HeapFree(GetProcessHeap(), 0, bufferW );
1216 return result;
1219 /***********************************************************************
1220 * HttpSendRequestExA (WININET.@)
1222 * Sends the specified request to the HTTP server and allows chunked
1223 * transfers
1225 BOOL WINAPI HttpSendRequestExA(HINTERNET hRequest,
1226 LPINTERNET_BUFFERSA lpBuffersIn,
1227 LPINTERNET_BUFFERSA lpBuffersOut,
1228 DWORD dwFlags, DWORD dwContext)
1230 FIXME("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest, lpBuffersIn,
1231 lpBuffersOut, dwFlags, dwContext);
1232 return FALSE;
1235 /***********************************************************************
1236 * HttpSendRequestW (WININET.@)
1238 * Sends the specified request to the HTTP server
1240 * RETURNS
1241 * TRUE on success
1242 * FALSE on failure
1245 BOOL WINAPI HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1246 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1248 LPWININETHTTPREQW lpwhr;
1249 LPWININETHTTPSESSIONW lpwhs = NULL;
1250 LPWININETAPPINFOW hIC = NULL;
1252 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest,
1253 lpszHeaders, debugstr_w(lpszHeaders), dwHeaderLength, lpOptional, dwOptionalLength);
1255 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1256 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1258 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1259 return FALSE;
1262 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1263 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1265 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1266 return FALSE;
1269 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1270 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1272 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1273 return FALSE;
1276 if (hIC->hdr.dwFlags & INTERNET_FLAG_ASYNC)
1278 WORKREQUEST workRequest;
1279 struct WORKREQ_HTTPSENDREQUESTW *req;
1281 workRequest.asyncall = HTTPSENDREQUESTW;
1282 workRequest.handle = hHttpRequest;
1283 req = &workRequest.u.HttpSendRequestW;
1284 if (lpszHeaders)
1285 req->lpszHeader = WININET_strdupW(lpszHeaders);
1286 else
1287 req->lpszHeader = 0;
1288 req->dwHeaderLength = dwHeaderLength;
1289 req->lpOptional = lpOptional;
1290 req->dwOptionalLength = dwOptionalLength;
1292 INTERNET_AsyncCall(&workRequest);
1294 * This is from windows.
1296 SetLastError(ERROR_IO_PENDING);
1297 return 0;
1299 else
1301 return HTTP_HttpSendRequestW(hHttpRequest, lpszHeaders,
1302 dwHeaderLength, lpOptional, dwOptionalLength);
1306 /***********************************************************************
1307 * HttpSendRequestA (WININET.@)
1309 * Sends the specified request to the HTTP server
1311 * RETURNS
1312 * TRUE on success
1313 * FALSE on failure
1316 BOOL WINAPI HttpSendRequestA(HINTERNET hHttpRequest, LPCSTR lpszHeaders,
1317 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1319 BOOL result;
1320 LPWSTR szHeaders=NULL;
1321 DWORD nLen=dwHeaderLength;
1322 if(lpszHeaders!=NULL)
1324 nLen=MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,NULL,0);
1325 szHeaders=HeapAlloc(GetProcessHeap(),0,nLen*sizeof(WCHAR));
1326 MultiByteToWideChar(CP_ACP,0,lpszHeaders,dwHeaderLength,szHeaders,nLen);
1328 result=HttpSendRequestW(hHttpRequest, szHeaders, nLen, lpOptional, dwOptionalLength);
1329 if(szHeaders!=NULL)
1330 HeapFree(GetProcessHeap(),0,szHeaders);
1331 return result;
1334 /***********************************************************************
1335 * HTTP_HandleRedirect (internal)
1337 static BOOL HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr, LPCWSTR lpszUrl, LPCWSTR lpszHeaders,
1338 DWORD dwHeaderLength, LPVOID lpOptional, DWORD dwOptionalLength)
1340 LPWININETHTTPSESSIONW lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1341 LPWININETAPPINFOW hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1342 WCHAR path[2048];
1343 HINTERNET handle;
1345 if(lpszUrl[0]=='/')
1347 /* if it's an absolute path, keep the same session info */
1348 strcpyW(path,lpszUrl);
1350 else if (NULL != hIC->lpszProxy && hIC->lpszProxy[0] != 0)
1352 TRACE("Redirect through proxy\n");
1353 strcpyW(path,lpszUrl);
1355 else
1357 URL_COMPONENTSW urlComponents;
1358 WCHAR protocol[32], hostName[MAXHOSTNAME], userName[1024];
1359 WCHAR password[1024], extra[1024];
1360 urlComponents.dwStructSize = sizeof(URL_COMPONENTSW);
1361 urlComponents.lpszScheme = protocol;
1362 urlComponents.dwSchemeLength = 32;
1363 urlComponents.lpszHostName = hostName;
1364 urlComponents.dwHostNameLength = MAXHOSTNAME;
1365 urlComponents.lpszUserName = userName;
1366 urlComponents.dwUserNameLength = 1024;
1367 urlComponents.lpszPassword = password;
1368 urlComponents.dwPasswordLength = 1024;
1369 urlComponents.lpszUrlPath = path;
1370 urlComponents.dwUrlPathLength = 2048;
1371 urlComponents.lpszExtraInfo = extra;
1372 urlComponents.dwExtraInfoLength = 1024;
1373 if(!InternetCrackUrlW(lpszUrl, strlenW(lpszUrl), 0, &urlComponents))
1374 return FALSE;
1376 if (urlComponents.nPort == INTERNET_INVALID_PORT_NUMBER)
1377 urlComponents.nPort = INTERNET_DEFAULT_HTTP_PORT;
1379 #if 0
1381 * This upsets redirects to binary files on sourceforge.net
1382 * and gives an html page instead of the target file
1383 * Examination of the HTTP request sent by native wininet.dll
1384 * reveals that it doesn't send a referrer in that case.
1385 * Maybe there's a flag that enables this, or maybe a referrer
1386 * shouldn't be added in case of a redirect.
1389 /* consider the current host as the referrer */
1390 if (NULL != lpwhs->lpszServerName && strlenW(lpwhs->lpszServerName))
1391 HTTP_ProcessHeader(lpwhr, HTTP_REFERER, lpwhs->lpszServerName,
1392 HTTP_ADDHDR_FLAG_REQ|HTTP_ADDREQ_FLAG_REPLACE|
1393 HTTP_ADDHDR_FLAG_ADD_IF_NEW);
1394 #endif
1396 if (NULL != lpwhs->lpszServerName)
1397 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
1398 lpwhs->lpszServerName = WININET_strdupW(hostName);
1399 if (NULL != lpwhs->lpszUserName)
1400 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
1401 lpwhs->lpszUserName = WININET_strdupW(userName);
1402 lpwhs->nServerPort = urlComponents.nPort;
1404 if (NULL != lpwhr->lpszHostName)
1405 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
1406 lpwhr->lpszHostName=WININET_strdupW(hostName);
1408 SendAsyncCallback(hIC, lpwhs, lpwhr->hdr.dwContext,
1409 INTERNET_STATUS_RESOLVING_NAME,
1410 lpwhs->lpszServerName,
1411 strlenW(lpwhs->lpszServerName)+1);
1413 if (!GetAddress(lpwhs->lpszServerName, lpwhs->nServerPort,
1414 &lpwhs->phostent, &lpwhs->socketAddress))
1416 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED);
1417 return FALSE;
1420 SendAsyncCallback(hIC, lpwhs, lpwhr->hdr.dwContext,
1421 INTERNET_STATUS_NAME_RESOLVED,
1422 &(lpwhs->socketAddress),
1423 sizeof(struct sockaddr_in));
1427 if(lpwhr->lpszPath)
1428 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
1429 lpwhr->lpszPath=NULL;
1430 if (strlenW(path))
1432 DWORD needed = 0;
1433 HRESULT rc;
1435 rc = UrlEscapeW(path, NULL, &needed, URL_ESCAPE_SPACES_ONLY);
1436 if (rc != E_POINTER)
1437 needed = strlenW(path)+1;
1438 lpwhr->lpszPath = HeapAlloc(GetProcessHeap(), 0, needed*sizeof(WCHAR));
1439 rc = UrlEscapeW(path, lpwhr->lpszPath, &needed,
1440 URL_ESCAPE_SPACES_ONLY);
1441 if (rc)
1443 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path),rc);
1444 strcpyW(lpwhr->lpszPath,path);
1448 handle = WININET_FindHandle( &lpwhr->hdr );
1449 return HttpSendRequestW(handle, lpszHeaders, dwHeaderLength, lpOptional, dwOptionalLength);
1452 /***********************************************************************
1453 * HTTP_build_req (internal)
1455 * concatenate all the strings in the request together
1457 static LPWSTR HTTP_build_req( LPCWSTR *list, int len )
1459 LPCWSTR *t;
1460 LPWSTR str;
1462 for( t = list; *t ; t++ )
1463 len += strlenW( *t );
1464 len++;
1466 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1467 *str = 0;
1469 for( t = list; *t ; t++ )
1470 strcatW( str, *t );
1472 return str;
1475 /***********************************************************************
1476 * HTTP_HttpSendRequestW (internal)
1478 * Sends the specified request to the HTTP server
1480 * RETURNS
1481 * TRUE on success
1482 * FALSE on failure
1485 BOOL WINAPI HTTP_HttpSendRequestW(HINTERNET hHttpRequest, LPCWSTR lpszHeaders,
1486 DWORD dwHeaderLength, LPVOID lpOptional ,DWORD dwOptionalLength)
1488 INT cnt;
1489 INT i;
1490 BOOL bSuccess = FALSE;
1491 LPWSTR requestString = NULL;
1492 INT responseLen;
1493 LPWININETHTTPREQW lpwhr;
1494 LPWININETHTTPSESSIONW lpwhs = NULL;
1495 LPWININETAPPINFOW hIC = NULL;
1496 BOOL loop_next = FALSE;
1497 int CustHeaderIndex;
1499 TRACE("--> 0x%08lx\n", (ULONG)hHttpRequest);
1501 /* Verify our tree of internet handles */
1502 lpwhr = (LPWININETHTTPREQW) WININET_GetObject( hHttpRequest );
1503 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1505 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1506 return FALSE;
1509 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
1510 if (NULL == lpwhs || lpwhs->hdr.htype != WH_HHTTPSESSION)
1512 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1513 return FALSE;
1516 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1517 if (NULL == hIC || hIC->hdr.htype != WH_HINIT)
1519 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE);
1520 return FALSE;
1523 /* Clear any error information */
1524 INTERNET_SetLastError(0);
1527 /* We must have a verb */
1528 if (NULL == lpwhr->lpszVerb)
1530 goto lend;
1533 /* if we are using optional stuff, we must add the fixed header of that option length */
1534 if (lpOptional && dwOptionalLength)
1536 static const WCHAR szContentLength[] = {
1537 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
1538 WCHAR contentLengthStr[sizeof szContentLength/2 /* includes \n\r */ + 20 /* int */ ];
1539 sprintfW(contentLengthStr, szContentLength, dwOptionalLength);
1540 HTTP_HttpAddRequestHeadersW(lpwhr, contentLengthStr, -1L, HTTP_ADDREQ_FLAG_ADD);
1545 static const WCHAR szSlash[] = { '/',0 };
1546 static const WCHAR szSpace[] = { ' ',0 };
1547 static const WCHAR szHttp[] = { 'h','t','t','p',':','/','/', 0 };
1548 static const WCHAR szcrlf[] = {'\r','\n', 0};
1549 static const WCHAR sztwocrlf[] = {'\r','\n','\r','\n', 0};
1550 static const WCHAR szSetCookie[] = {'S','e','t','-','C','o','o','k','i','e',0 };
1551 static const WCHAR szColon[] = { ':',' ',0 };
1552 LPCWSTR *req;
1553 LPWSTR p;
1554 int len, n;
1555 char *ascii_req;
1558 TRACE("Going to url %s %s\n", debugstr_w(lpwhr->lpszHostName), debugstr_w(lpwhr->lpszPath));
1559 loop_next = FALSE;
1561 /* If we don't have a path we set it to root */
1562 if (NULL == lpwhr->lpszPath)
1563 lpwhr->lpszPath = WININET_strdupW(szSlash);
1565 if(CSTR_EQUAL != CompareStringW( LOCALE_SYSTEM_DEFAULT, NORM_IGNORECASE,
1566 lpwhr->lpszPath, strlenW(szHttp), szHttp, strlenW(szHttp) )
1567 && lpwhr->lpszPath[0] != '/') /* not an absolute path ?? --> fix it !! */
1569 WCHAR *fixurl = HeapAlloc(GetProcessHeap(), 0,
1570 (strlenW(lpwhr->lpszPath) + 2)*sizeof(WCHAR));
1571 *fixurl = '/';
1572 strcpyW(fixurl + 1, lpwhr->lpszPath);
1573 HeapFree( GetProcessHeap(), 0, lpwhr->lpszPath );
1574 lpwhr->lpszPath = fixurl;
1577 /* allocate space for an array of all the string pointers to be added */
1578 len = (2 + HTTP_QUERY_MAX + lpwhr->nCustHeaders)*4 + 3;
1579 req = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len*sizeof(LPCWSTR) );
1581 /* add the verb, path and HTTP/1.0 */
1582 n = 0;
1583 req[n++] = lpwhr->lpszVerb;
1584 req[n++] = szSpace;
1585 req[n++] = lpwhr->lpszPath;
1586 req[n++] = HTTPHEADER;
1588 /* Append standard request headers */
1589 for (i = 0; i <= HTTP_QUERY_MAX; i++)
1591 if (lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST)
1593 req[n++] = szcrlf;
1594 req[n++] = lpwhr->StdHeaders[i].lpszField;
1595 req[n++] = szColon;
1596 req[n++] = lpwhr->StdHeaders[i].lpszValue;
1598 TRACE("Adding header %s (%s)\n",
1599 debugstr_w(lpwhr->StdHeaders[i].lpszField),
1600 debugstr_w(lpwhr->StdHeaders[i].lpszValue));
1604 /* Append custom request heades */
1605 for (i = 0; i < lpwhr->nCustHeaders; i++)
1607 if (lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST)
1609 req[n++] = szcrlf;
1610 req[n++] = lpwhr->pCustHeaders[i].lpszField;
1611 req[n++] = szColon;
1612 req[n++] = lpwhr->pCustHeaders[i].lpszValue;
1614 TRACE("Adding custom header %s (%s)\n",
1615 debugstr_w(lpwhr->pCustHeaders[i].lpszField),
1616 debugstr_w(lpwhr->pCustHeaders[i].lpszValue));
1620 if (lpwhr->lpszHostName)
1622 req[n++] = HTTPHOSTHEADER;
1623 req[n++] = lpwhr->lpszHostName;
1626 if( n > len )
1627 ERR("oops. buffer overrun\n");
1629 requestString = HTTP_build_req( req, 4 );
1630 HeapFree( GetProcessHeap(), 0, req );
1633 * Set (header) termination string for request
1634 * Make sure there's exactly two new lines at the end of the request
1636 p = &requestString[strlenW(requestString)-1];
1637 while ( (*p == '\n') || (*p == '\r') )
1638 p--;
1639 strcpyW( p+1, sztwocrlf );
1641 TRACE("Request header -> %s\n", debugstr_w(requestString) );
1643 /* Send the request and store the results */
1644 if (!HTTP_OpenConnection(lpwhr))
1645 goto lend;
1647 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1648 INTERNET_STATUS_SENDING_REQUEST, NULL, 0);
1650 /* send the request as ASCII, tack on the optional data */
1651 if( !lpOptional )
1652 dwOptionalLength = 0;
1653 len = WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1654 NULL, 0, NULL, NULL );
1655 ascii_req = HeapAlloc( GetProcessHeap(), 0, len + dwOptionalLength );
1656 WideCharToMultiByte( CP_ACP, 0, requestString, -1,
1657 ascii_req, len, NULL, NULL );
1658 if( lpOptional )
1659 memcpy( &ascii_req[len], lpOptional, dwOptionalLength );
1660 len += dwOptionalLength;
1661 NETCON_send(&lpwhr->netConnection, ascii_req, len, 0, &cnt);
1662 HeapFree( GetProcessHeap(), 0, ascii_req );
1664 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1665 INTERNET_STATUS_REQUEST_SENT,
1666 &len,sizeof(DWORD));
1668 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1669 INTERNET_STATUS_RECEIVING_RESPONSE, NULL, 0);
1671 if (cnt < 0)
1672 goto lend;
1674 responseLen = HTTP_GetResponseHeaders(lpwhr);
1675 if (responseLen)
1676 bSuccess = TRUE;
1678 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1679 INTERNET_STATUS_RESPONSE_RECEIVED, &responseLen,
1680 sizeof(DWORD));
1682 /* process headers here. Is this right? */
1683 CustHeaderIndex = HTTP_GetCustomHeaderIndex(lpwhr, szSetCookie);
1684 if (CustHeaderIndex >= 0)
1686 LPHTTPHEADERW setCookieHeader;
1687 int nPosStart = 0, nPosEnd = 0, len;
1688 static const WCHAR szFmt[] = { 'h','t','t','p',':','/','/','%','s','/',0};
1690 setCookieHeader = &lpwhr->pCustHeaders[CustHeaderIndex];
1692 while (setCookieHeader->lpszValue[nPosEnd] != '\0')
1694 LPWSTR buf_cookie, cookie_name, cookie_data;
1695 LPWSTR buf_url;
1696 LPWSTR domain = NULL;
1697 int nEqualPos = 0;
1698 while (setCookieHeader->lpszValue[nPosEnd] != ';' && setCookieHeader->lpszValue[nPosEnd] != ',' &&
1699 setCookieHeader->lpszValue[nPosEnd] != '\0')
1701 nPosEnd++;
1703 if (setCookieHeader->lpszValue[nPosEnd] == ';')
1705 /* fixme: not case sensitive, strcasestr is gnu only */
1706 int nDomainPosEnd = 0;
1707 int nDomainPosStart = 0, nDomainLength = 0;
1708 static const WCHAR szDomain[] = {'d','o','m','a','i','n','=',0};
1709 LPWSTR lpszDomain = strstrW(&setCookieHeader->lpszValue[nPosEnd], szDomain);
1710 if (lpszDomain)
1711 { /* they have specified their own domain, lets use it */
1712 while (lpszDomain[nDomainPosEnd] != ';' && lpszDomain[nDomainPosEnd] != ',' &&
1713 lpszDomain[nDomainPosEnd] != '\0')
1715 nDomainPosEnd++;
1717 nDomainPosStart = strlenW(szDomain);
1718 nDomainLength = (nDomainPosEnd - nDomainPosStart) + 1;
1719 domain = HeapAlloc(GetProcessHeap(), 0, (nDomainLength + 1)*sizeof(WCHAR));
1720 strncpyW(domain, &lpszDomain[nDomainPosStart], nDomainLength);
1721 domain[nDomainLength] = '\0';
1724 if (setCookieHeader->lpszValue[nPosEnd] == '\0') break;
1725 buf_cookie = HeapAlloc(GetProcessHeap(), 0, ((nPosEnd - nPosStart) + 1)*sizeof(WCHAR));
1726 strncpyW(buf_cookie, &setCookieHeader->lpszValue[nPosStart], (nPosEnd - nPosStart));
1727 buf_cookie[(nPosEnd - nPosStart)] = '\0';
1728 TRACE("%s\n", debugstr_w(buf_cookie));
1729 while (buf_cookie[nEqualPos] != '=' && buf_cookie[nEqualPos] != '\0')
1731 nEqualPos++;
1733 if (buf_cookie[nEqualPos] == '\0' || buf_cookie[nEqualPos + 1] == '\0')
1735 HeapFree(GetProcessHeap(), 0, buf_cookie);
1736 break;
1739 cookie_name = HeapAlloc(GetProcessHeap(), 0, (nEqualPos + 1)*sizeof(WCHAR));
1740 strncpyW(cookie_name, buf_cookie, nEqualPos);
1741 cookie_name[nEqualPos] = '\0';
1742 cookie_data = &buf_cookie[nEqualPos + 1];
1745 len = strlenW((domain ? domain : lpwhr->lpszHostName)) + strlenW(lpwhr->lpszPath) + 9;
1746 buf_url = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
1747 sprintfW(buf_url, szFmt, (domain ? domain : lpwhr->lpszHostName)); /* FIXME PATH!!! */
1748 InternetSetCookieW(buf_url, cookie_name, cookie_data);
1750 HeapFree(GetProcessHeap(), 0, buf_url);
1751 HeapFree(GetProcessHeap(), 0, buf_cookie);
1752 HeapFree(GetProcessHeap(), 0, cookie_name);
1753 if (domain) HeapFree(GetProcessHeap(), 0, domain);
1754 nPosStart = nPosEnd;
1758 while (loop_next);
1760 lend:
1762 if (requestString)
1763 HeapFree(GetProcessHeap(), 0, requestString);
1765 /* TODO: send notification for P3P header */
1767 if(!(hIC->hdr.dwFlags & INTERNET_FLAG_NO_AUTO_REDIRECT) && bSuccess)
1769 DWORD dwCode,dwCodeLength=sizeof(DWORD),dwIndex=0;
1770 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_FLAG_NUMBER|HTTP_QUERY_STATUS_CODE,&dwCode,&dwCodeLength,&dwIndex) &&
1771 (dwCode==302 || dwCode==301))
1773 WCHAR szNewLocation[2048];
1774 DWORD dwBufferSize=2048;
1775 dwIndex=0;
1776 if(HTTP_HttpQueryInfoW(lpwhr,HTTP_QUERY_LOCATION,szNewLocation,&dwBufferSize,&dwIndex))
1778 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1779 INTERNET_STATUS_REDIRECT, szNewLocation,
1780 dwBufferSize);
1781 return HTTP_HandleRedirect(lpwhr, szNewLocation, lpszHeaders,
1782 dwHeaderLength, lpOptional, dwOptionalLength);
1787 if (hIC->lpfnStatusCB)
1789 INTERNET_ASYNC_RESULT iar;
1791 iar.dwResult = (DWORD)bSuccess;
1792 iar.dwError = bSuccess ? ERROR_SUCCESS : INTERNET_GetLastError();
1794 SendAsyncCallback(hIC, hHttpRequest, lpwhr->hdr.dwContext,
1795 INTERNET_STATUS_REQUEST_COMPLETE, &iar,
1796 sizeof(INTERNET_ASYNC_RESULT));
1799 TRACE("<--\n");
1800 return bSuccess;
1804 /***********************************************************************
1805 * HTTP_Connect (internal)
1807 * Create http session handle
1809 * RETURNS
1810 * HINTERNET a session handle on success
1811 * NULL on failure
1814 HINTERNET HTTP_Connect(HINTERNET hInternet, LPCWSTR lpszServerName,
1815 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
1816 LPCWSTR lpszPassword, DWORD dwFlags, DWORD dwContext,
1817 DWORD dwInternalFlags)
1819 BOOL bSuccess = FALSE;
1820 LPWININETAPPINFOW hIC = NULL;
1821 LPWININETHTTPSESSIONW lpwhs = NULL;
1822 HINTERNET handle = NULL;
1824 TRACE("-->\n");
1826 hIC = (LPWININETAPPINFOW) WININET_GetObject( hInternet );
1827 if( (hIC == NULL) || (hIC->hdr.htype != WH_HINIT) )
1828 goto lerror;
1830 hIC->hdr.dwContext = dwContext;
1832 lpwhs = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WININETHTTPSESSIONW));
1833 if (NULL == lpwhs)
1835 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1836 goto lerror;
1839 handle = WININET_AllocHandle( &lpwhs->hdr );
1840 if (NULL == handle)
1842 ERR("Failed to alloc handle\n");
1843 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
1844 goto lerror;
1848 * According to my tests. The name is not resolved until a request is sent
1851 if (nServerPort == INTERNET_INVALID_PORT_NUMBER)
1852 nServerPort = INTERNET_DEFAULT_HTTP_PORT;
1854 lpwhs->hdr.htype = WH_HHTTPSESSION;
1855 lpwhs->hdr.lpwhparent = &hIC->hdr;
1856 lpwhs->hdr.dwFlags = dwFlags;
1857 lpwhs->hdr.dwContext = dwContext;
1858 lpwhs->hdr.dwInternalFlags = dwInternalFlags;
1859 if(hIC->lpszProxy && hIC->dwAccessType == INTERNET_OPEN_TYPE_PROXY) {
1860 if(strchrW(hIC->lpszProxy, ' '))
1861 FIXME("Several proxies not implemented.\n");
1862 if(hIC->lpszProxyBypass)
1863 FIXME("Proxy bypass is ignored.\n");
1865 if (NULL != lpszServerName)
1866 lpwhs->lpszServerName = WININET_strdupW(lpszServerName);
1867 if (NULL != lpszUserName)
1868 lpwhs->lpszUserName = WININET_strdupW(lpszUserName);
1869 lpwhs->nServerPort = nServerPort;
1871 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
1872 if (hIC->lpfnStatusCB && !(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
1874 INTERNET_ASYNC_RESULT iar;
1876 iar.dwResult = (DWORD)handle;
1877 iar.dwError = ERROR_SUCCESS;
1879 SendAsyncCallback(hIC, hInternet, dwContext,
1880 INTERNET_STATUS_HANDLE_CREATED, &iar,
1881 sizeof(INTERNET_ASYNC_RESULT));
1884 bSuccess = TRUE;
1886 lerror:
1887 if (!bSuccess && lpwhs)
1889 HeapFree(GetProcessHeap(), 0, lpwhs);
1890 WININET_FreeHandle( handle );
1891 lpwhs = NULL;
1895 * a INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
1896 * windows
1899 TRACE("%p --> %p\n", hInternet, handle);
1900 return handle;
1904 /***********************************************************************
1905 * HTTP_OpenConnection (internal)
1907 * Connect to a web server
1909 * RETURNS
1911 * TRUE on success
1912 * FALSE on failure
1914 BOOL HTTP_OpenConnection(LPWININETHTTPREQW lpwhr)
1916 BOOL bSuccess = FALSE;
1917 LPWININETHTTPSESSIONW lpwhs;
1918 LPWININETAPPINFOW hIC = NULL;
1920 TRACE("-->\n");
1923 if (NULL == lpwhr || lpwhr->hdr.htype != WH_HHTTPREQ)
1925 INTERNET_SetLastError(ERROR_INVALID_PARAMETER);
1926 goto lend;
1929 lpwhs = (LPWININETHTTPSESSIONW)lpwhr->hdr.lpwhparent;
1931 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
1932 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
1933 INTERNET_STATUS_CONNECTING_TO_SERVER,
1934 &(lpwhs->socketAddress),
1935 sizeof(struct sockaddr_in));
1937 if (!NETCON_create(&lpwhr->netConnection, lpwhs->phostent->h_addrtype,
1938 SOCK_STREAM, 0))
1940 WARN("Socket creation failed\n");
1941 goto lend;
1944 if (!NETCON_connect(&lpwhr->netConnection, (struct sockaddr *)&lpwhs->socketAddress,
1945 sizeof(lpwhs->socketAddress)))
1947 WARN("Unable to connect to host (%s)\n", strerror(errno));
1948 goto lend;
1951 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
1952 INTERNET_STATUS_CONNECTED_TO_SERVER,
1953 &(lpwhs->socketAddress),
1954 sizeof(struct sockaddr_in));
1956 bSuccess = TRUE;
1958 lend:
1959 TRACE("%d <--\n", bSuccess);
1960 return bSuccess;
1964 /***********************************************************************
1965 * HTTP_clear_response_headers (internal)
1967 * clear out any old response headers
1969 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr )
1971 DWORD i;
1973 for( i=0; i<=HTTP_QUERY_MAX; i++ )
1975 if( !lpwhr->StdHeaders[i].lpszField )
1976 continue;
1977 if( !lpwhr->StdHeaders[i].lpszValue )
1978 continue;
1979 if ( lpwhr->StdHeaders[i].wFlags & HDR_ISREQUEST )
1980 continue;
1981 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[i], NULL );
1983 for( i=0; i<lpwhr->nCustHeaders; i++)
1985 if( !lpwhr->pCustHeaders[i].lpszField )
1986 continue;
1987 if( !lpwhr->pCustHeaders[i].lpszValue )
1988 continue;
1989 if ( lpwhr->pCustHeaders[i].wFlags & HDR_ISREQUEST )
1990 continue;
1991 HTTP_ReplaceHeaderValue( &lpwhr->pCustHeaders[i], NULL );
1995 /***********************************************************************
1996 * HTTP_GetResponseHeaders (internal)
1998 * Read server response
2000 * RETURNS
2002 * TRUE on success
2003 * FALSE on error
2005 BOOL HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr)
2007 INT cbreaks = 0;
2008 WCHAR buffer[MAX_REPLY_LEN];
2009 DWORD buflen = MAX_REPLY_LEN;
2010 BOOL bSuccess = FALSE;
2011 INT rc = 0;
2012 WCHAR value[MAX_FIELD_VALUE_LEN], field[MAX_FIELD_LEN];
2013 static const WCHAR szHttp[] = { 'H','T','T','P',0 };
2014 char bufferA[MAX_REPLY_LEN];
2015 LPWSTR status_code, status_text;
2017 TRACE("-->\n");
2019 /* clear old response headers (eg. from a redirect response) */
2020 HTTP_clear_response_headers( lpwhr );
2022 if (!NETCON_connected(&lpwhr->netConnection))
2023 goto lend;
2026 * HACK peek at the buffer
2028 NETCON_recv(&lpwhr->netConnection, buffer, buflen, MSG_PEEK, &rc);
2031 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2033 buflen = MAX_REPLY_LEN;
2034 memset(buffer, 0, MAX_REPLY_LEN);
2035 if (!NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2036 goto lend;
2037 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2039 if (strncmpW(buffer, szHttp, 4) != 0)
2040 goto lend;
2042 /* split the version from the status code */
2043 status_code = strchrW( buffer, ' ' );
2044 if( !status_code )
2045 goto lend;
2046 *status_code++=0;
2048 /* split the status code from the status text */
2049 status_text = strchrW( status_code, ' ' );
2050 if( !status_text )
2051 goto lend;
2052 *status_text++=0;
2054 TRACE("version [%s] status code [%s] status text [%s]\n",
2055 debugstr_w(buffer), debugstr_w(status_code), debugstr_w(status_text) );
2056 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_VERSION], buffer );
2057 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_CODE], status_code );
2058 HTTP_ReplaceHeaderValue( &lpwhr->StdHeaders[HTTP_QUERY_STATUS_TEXT], status_text );
2060 /* Parse each response line */
2063 buflen = MAX_REPLY_LEN;
2064 if (NETCON_getNextLine(&lpwhr->netConnection, bufferA, &buflen))
2066 TRACE("got line %s, now interpretting\n", debugstr_a(bufferA));
2067 MultiByteToWideChar( CP_ACP, 0, bufferA, buflen, buffer, MAX_REPLY_LEN );
2068 if (!HTTP_InterpretHttpHeader(buffer, field, MAX_FIELD_LEN, value, MAX_FIELD_VALUE_LEN))
2069 break;
2071 HTTP_ProcessHeader(lpwhr, field, value, (HTTP_ADDREQ_FLAG_ADD | HTTP_ADDREQ_FLAG_REPLACE));
2073 else
2075 cbreaks++;
2076 if (cbreaks >= 2)
2077 break;
2079 }while(1);
2081 bSuccess = TRUE;
2083 lend:
2085 TRACE("<--\n");
2086 if (bSuccess)
2087 return rc;
2088 else
2089 return FALSE;
2093 /***********************************************************************
2094 * HTTP_InterpretHttpHeader (internal)
2096 * Parse server response
2098 * RETURNS
2100 * TRUE on success
2101 * FALSE on error
2103 INT stripSpaces(LPCWSTR lpszSrc, LPWSTR lpszStart, INT *len)
2105 LPCWSTR lpsztmp;
2106 INT srclen;
2108 srclen = 0;
2110 while (*lpszSrc == ' ' && *lpszSrc != '\0')
2111 lpszSrc++;
2113 lpsztmp = lpszSrc;
2114 while(*lpsztmp != '\0')
2116 if (*lpsztmp != ' ')
2117 srclen = lpsztmp - lpszSrc + 1;
2119 lpsztmp++;
2122 *len = min(*len, srclen);
2123 strncpyW(lpszStart, lpszSrc, *len);
2124 lpszStart[*len] = '\0';
2126 return *len;
2130 BOOL HTTP_InterpretHttpHeader(LPWSTR buffer, LPWSTR field, INT fieldlen, LPWSTR value, INT valuelen)
2132 WCHAR *pd;
2133 BOOL bSuccess = FALSE;
2135 TRACE("\n");
2137 *field = '\0';
2138 *value = '\0';
2140 pd = strchrW(buffer, ':');
2141 if (pd)
2143 *pd = '\0';
2144 if (stripSpaces(buffer, field, &fieldlen) > 0)
2146 if (stripSpaces(pd+1, value, &valuelen) > 0)
2147 bSuccess = TRUE;
2151 TRACE("%d: field(%s) Value(%s)\n", bSuccess, debugstr_w(field), debugstr_w(value));
2152 return bSuccess;
2156 /***********************************************************************
2157 * HTTP_GetStdHeaderIndex (internal)
2159 * Lookup field index in standard http header array
2161 * FIXME: This should be stuffed into a hash table
2163 INT HTTP_GetStdHeaderIndex(LPCWSTR lpszField)
2165 INT index = -1;
2166 static const WCHAR szContentLength[] = {
2167 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
2168 static const WCHAR szContentType[] = {
2169 'C','o','n','t','e','n','t','-','T','y','p','e',0};
2170 static const WCHAR szLastModified[] = {
2171 'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
2172 static const WCHAR szLocation[] = {'L','o','c','a','t','i','o','n',0};
2173 static const WCHAR szAccept[] = {'A','c','c','e','p','t',0};
2174 static const WCHAR szReferer[] = { 'R','e','f','e','r','e','r',0};
2175 static const WCHAR szContentTrans[] = { 'C','o','n','t','e','n','t','-',
2176 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
2177 static const WCHAR szDate[] = { 'D','a','t','e',0};
2178 static const WCHAR szServer[] = { 'S','e','r','v','e','r',0};
2179 static const WCHAR szConnection[] = { 'C','o','n','n','e','c','t','i','o','n',0};
2180 static const WCHAR szETag[] = { 'E','T','a','g',0};
2181 static const WCHAR szAcceptRanges[] = {
2182 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
2183 static const WCHAR szExpires[] = { 'E','x','p','i','r','e','s',0 };
2184 static const WCHAR szMimeVersion[] = {
2185 'M','i','m','e','-','V','e','r','s','i','o','n', 0};
2186 static const WCHAR szPragma[] = { 'P','r','a','g','m','a', 0};
2187 static const WCHAR szCacheControl[] = {
2188 'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
2189 static const WCHAR szUserAgent[] = { 'U','s','e','r','-','A','g','e','n','t',0};
2190 static const WCHAR szProxyAuth[] = {
2191 'P','r','o','x','y','-',
2192 'A','u','t','h','e','n','t','i','c','a','t','e', 0};
2193 static const WCHAR szContentEncoding[] = {
2194 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
2195 static const WCHAR szCookie[] = {'C','o','o','k','i','e',0};
2196 static const WCHAR szVary[] = {'V','a','r','y',0};
2197 static const WCHAR szVia[] = {'V','i','a',0};
2199 if (!strcmpiW(lpszField, szContentLength))
2200 index = HTTP_QUERY_CONTENT_LENGTH;
2201 else if (!strcmpiW(lpszField,szContentType))
2202 index = HTTP_QUERY_CONTENT_TYPE;
2203 else if (!strcmpiW(lpszField,szLastModified))
2204 index = HTTP_QUERY_LAST_MODIFIED;
2205 else if (!strcmpiW(lpszField,szLocation))
2206 index = HTTP_QUERY_LOCATION;
2207 else if (!strcmpiW(lpszField,szAccept))
2208 index = HTTP_QUERY_ACCEPT;
2209 else if (!strcmpiW(lpszField,szReferer))
2210 index = HTTP_QUERY_REFERER;
2211 else if (!strcmpiW(lpszField,szContentTrans))
2212 index = HTTP_QUERY_CONTENT_TRANSFER_ENCODING;
2213 else if (!strcmpiW(lpszField,szDate))
2214 index = HTTP_QUERY_DATE;
2215 else if (!strcmpiW(lpszField,szServer))
2216 index = HTTP_QUERY_SERVER;
2217 else if (!strcmpiW(lpszField,szConnection))
2218 index = HTTP_QUERY_CONNECTION;
2219 else if (!strcmpiW(lpszField,szETag))
2220 index = HTTP_QUERY_ETAG;
2221 else if (!strcmpiW(lpszField,szAcceptRanges))
2222 index = HTTP_QUERY_ACCEPT_RANGES;
2223 else if (!strcmpiW(lpszField,szExpires))
2224 index = HTTP_QUERY_EXPIRES;
2225 else if (!strcmpiW(lpszField,szMimeVersion))
2226 index = HTTP_QUERY_MIME_VERSION;
2227 else if (!strcmpiW(lpszField,szPragma))
2228 index = HTTP_QUERY_PRAGMA;
2229 else if (!strcmpiW(lpszField,szCacheControl))
2230 index = HTTP_QUERY_CACHE_CONTROL;
2231 else if (!strcmpiW(lpszField,szUserAgent))
2232 index = HTTP_QUERY_USER_AGENT;
2233 else if (!strcmpiW(lpszField,szProxyAuth))
2234 index = HTTP_QUERY_PROXY_AUTHENTICATE;
2235 else if (!strcmpiW(lpszField,szContentEncoding))
2236 index = HTTP_QUERY_CONTENT_ENCODING;
2237 else if (!strcmpiW(lpszField,szCookie))
2238 index = HTTP_QUERY_COOKIE;
2239 else if (!strcmpiW(lpszField,szVary))
2240 index = HTTP_QUERY_VARY;
2241 else if (!strcmpiW(lpszField,szVia))
2242 index = HTTP_QUERY_VIA;
2243 else
2245 TRACE("Couldn't find %s in standard header table\n", debugstr_w(lpszField));
2248 return index;
2251 /***********************************************************************
2252 * HTTP_ReplaceHeaderValue (internal)
2254 BOOL HTTP_ReplaceHeaderValue( LPHTTPHEADERW lphttpHdr, LPCWSTR value )
2256 INT len = 0;
2258 if( lphttpHdr->lpszValue )
2259 HeapFree( GetProcessHeap(), 0, lphttpHdr->lpszValue );
2260 lphttpHdr->lpszValue = NULL;
2262 if( value )
2263 len = strlenW(value);
2264 if (len)
2266 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0,
2267 (len+1)*sizeof(WCHAR));
2268 strcpyW(lphttpHdr->lpszValue, value);
2270 return TRUE;
2273 /***********************************************************************
2274 * HTTP_ProcessHeader (internal)
2276 * Stuff header into header tables according to <dwModifier>
2280 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2282 BOOL HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr, LPCWSTR field, LPCWSTR value, DWORD dwModifier)
2284 LPHTTPHEADERW lphttpHdr = NULL;
2285 BOOL bSuccess = FALSE;
2286 INT index;
2288 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field), debugstr_w(value), (unsigned int)dwModifier);
2290 /* Adjust modifier flags */
2291 if (dwModifier & COALESCEFLASG)
2292 dwModifier |= HTTP_ADDHDR_FLAG_ADD;
2294 /* Try to get index into standard header array */
2295 index = HTTP_GetStdHeaderIndex(field);
2296 if (index >= 0)
2298 lphttpHdr = &lpwhr->StdHeaders[index];
2300 else /* Find or create new custom header */
2302 index = HTTP_GetCustomHeaderIndex(lpwhr, field);
2303 if (index >= 0)
2305 if (dwModifier & HTTP_ADDHDR_FLAG_ADD_IF_NEW)
2307 return FALSE;
2309 lphttpHdr = &lpwhr->pCustHeaders[index];
2311 else
2313 HTTPHEADERW hdr;
2315 hdr.lpszField = (LPWSTR)field;
2316 hdr.lpszValue = (LPWSTR)value;
2317 hdr.wFlags = hdr.wCount = 0;
2319 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2320 hdr.wFlags |= HDR_ISREQUEST;
2322 return HTTP_InsertCustomHeader(lpwhr, &hdr);
2326 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2327 lphttpHdr->wFlags |= HDR_ISREQUEST;
2328 else
2329 lphttpHdr->wFlags &= ~HDR_ISREQUEST;
2331 if (!lphttpHdr->lpszValue && (dwModifier & (HTTP_ADDHDR_FLAG_ADD|HTTP_ADDHDR_FLAG_ADD_IF_NEW)))
2333 INT slen;
2335 if (!lpwhr->StdHeaders[index].lpszField)
2337 lphttpHdr->lpszField = WININET_strdupW(field);
2339 if (dwModifier & HTTP_ADDHDR_FLAG_REQ)
2340 lphttpHdr->wFlags |= HDR_ISREQUEST;
2343 slen = strlenW(value) + 1;
2344 lphttpHdr->lpszValue = HeapAlloc(GetProcessHeap(), 0, slen*sizeof(WCHAR));
2345 if (lphttpHdr->lpszValue)
2347 strcpyW(lphttpHdr->lpszValue, value);
2348 bSuccess = TRUE;
2350 else
2352 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2355 else if (lphttpHdr->lpszValue)
2357 if (dwModifier & HTTP_ADDHDR_FLAG_REPLACE)
2358 bSuccess = HTTP_ReplaceHeaderValue( lphttpHdr, value );
2359 else if (dwModifier & COALESCEFLASG)
2361 LPWSTR lpsztmp;
2362 WCHAR ch = 0;
2363 INT len = 0;
2364 INT origlen = strlenW(lphttpHdr->lpszValue);
2365 INT valuelen = strlenW(value);
2367 if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA)
2369 ch = ',';
2370 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2372 else if (dwModifier & HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2374 ch = ';';
2375 lphttpHdr->wFlags |= HDR_COMMADELIMITED;
2378 len = origlen + valuelen + ((ch > 0) ? 1 : 0);
2380 lpsztmp = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lphttpHdr->lpszValue, (len+1)*sizeof(WCHAR));
2381 if (lpsztmp)
2383 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2384 if (ch > 0)
2386 lphttpHdr->lpszValue[origlen] = ch;
2387 origlen++;
2390 memcpy(&lphttpHdr->lpszValue[origlen], value, valuelen*sizeof(WCHAR));
2391 lphttpHdr->lpszValue[len] = '\0';
2392 bSuccess = TRUE;
2394 else
2396 WARN("HeapReAlloc (%d bytes) failed\n",len+1);
2397 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2401 TRACE("<-- %d\n",bSuccess);
2402 return bSuccess;
2406 /***********************************************************************
2407 * HTTP_CloseConnection (internal)
2409 * Close socket connection
2412 VOID HTTP_CloseConnection(LPWININETHTTPREQW lpwhr)
2416 LPWININETHTTPSESSIONW lpwhs = NULL;
2417 LPWININETAPPINFOW hIC = NULL;
2418 HINTERNET handle;
2420 TRACE("%p\n",lpwhr);
2422 handle = WININET_FindHandle( &lpwhr->hdr );
2423 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2424 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2426 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
2427 INTERNET_STATUS_CLOSING_CONNECTION, 0, 0);
2429 if (NETCON_connected(&lpwhr->netConnection))
2431 NETCON_close(&lpwhr->netConnection);
2434 SendAsyncCallback(hIC, lpwhr, lpwhr->hdr.dwContext,
2435 INTERNET_STATUS_CONNECTION_CLOSED, 0, 0);
2439 /***********************************************************************
2440 * HTTP_CloseHTTPRequestHandle (internal)
2442 * Deallocate request handle
2445 void HTTP_CloseHTTPRequestHandle(LPWININETHTTPREQW lpwhr)
2447 int i;
2448 LPWININETHTTPSESSIONW lpwhs = NULL;
2449 LPWININETAPPINFOW hIC = NULL;
2450 HINTERNET handle;
2452 TRACE("\n");
2454 if (NETCON_connected(&lpwhr->netConnection))
2455 HTTP_CloseConnection(lpwhr);
2457 handle = WININET_FindHandle( &lpwhr->hdr );
2458 lpwhs = (LPWININETHTTPSESSIONW) lpwhr->hdr.lpwhparent;
2459 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2461 SendAsyncCallback(hIC, handle, lpwhr->hdr.dwContext,
2462 INTERNET_STATUS_HANDLE_CLOSING, lpwhr,
2463 sizeof(HINTERNET));
2465 if (lpwhr->lpszPath)
2466 HeapFree(GetProcessHeap(), 0, lpwhr->lpszPath);
2467 if (lpwhr->lpszVerb)
2468 HeapFree(GetProcessHeap(), 0, lpwhr->lpszVerb);
2469 if (lpwhr->lpszHostName)
2470 HeapFree(GetProcessHeap(), 0, lpwhr->lpszHostName);
2472 for (i = 0; i <= HTTP_QUERY_MAX; i++)
2474 if (lpwhr->StdHeaders[i].lpszField)
2475 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszField);
2476 if (lpwhr->StdHeaders[i].lpszValue)
2477 HeapFree(GetProcessHeap(), 0, lpwhr->StdHeaders[i].lpszValue);
2480 for (i = 0; i < lpwhr->nCustHeaders; i++)
2482 if (lpwhr->pCustHeaders[i].lpszField)
2483 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszField);
2484 if (lpwhr->pCustHeaders[i].lpszValue)
2485 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders[i].lpszValue);
2488 HeapFree(GetProcessHeap(), 0, lpwhr->pCustHeaders);
2489 HeapFree(GetProcessHeap(), 0, lpwhr);
2491 /* If this handle was opened with InternetOpenUrl, we need to close the parent to prevent
2492 a memory leek
2494 if(lpwhs->hdr.dwInternalFlags & INET_OPENURL)
2496 handle = WININET_FindHandle( &lpwhs->hdr );
2497 InternetCloseHandle(handle);
2502 /***********************************************************************
2503 * HTTP_CloseHTTPSessionHandle (internal)
2505 * Deallocate session handle
2508 void HTTP_CloseHTTPSessionHandle(LPWININETHTTPSESSIONW lpwhs)
2510 LPWININETAPPINFOW hIC = NULL;
2511 HINTERNET handle;
2513 TRACE("%p\n", lpwhs);
2515 hIC = (LPWININETAPPINFOW) lpwhs->hdr.lpwhparent;
2517 /* Don't send a handle closing callback if this handle was created with InternetOpenUrl */
2518 if(!(lpwhs->hdr.dwInternalFlags & INET_OPENURL))
2520 handle = WININET_FindHandle( &lpwhs->hdr );
2521 SendAsyncCallback(hIC, handle, lpwhs->hdr.dwContext,
2522 INTERNET_STATUS_HANDLE_CLOSING, lpwhs,
2523 sizeof(HINTERNET));
2526 if (lpwhs->lpszServerName)
2527 HeapFree(GetProcessHeap(), 0, lpwhs->lpszServerName);
2528 if (lpwhs->lpszUserName)
2529 HeapFree(GetProcessHeap(), 0, lpwhs->lpszUserName);
2530 HeapFree(GetProcessHeap(), 0, lpwhs);
2534 /***********************************************************************
2535 * HTTP_GetCustomHeaderIndex (internal)
2537 * Return index of custom header from header array
2540 INT HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr, LPCWSTR lpszField)
2542 INT index;
2544 TRACE("%s\n", debugstr_w(lpszField));
2546 for (index = 0; index < lpwhr->nCustHeaders; index++)
2548 if (!strcmpiW(lpwhr->pCustHeaders[index].lpszField, lpszField))
2549 break;
2553 if (index >= lpwhr->nCustHeaders)
2554 index = -1;
2556 TRACE("Return: %d\n", index);
2557 return index;
2561 /***********************************************************************
2562 * HTTP_InsertCustomHeader (internal)
2564 * Insert header into array
2567 BOOL HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr, LPHTTPHEADERW lpHdr)
2569 INT count;
2570 LPHTTPHEADERW lph = NULL;
2571 BOOL r = FALSE;
2573 TRACE("--> %s: %s\n", debugstr_w(lpHdr->lpszField), debugstr_w(lpHdr->lpszValue));
2574 count = lpwhr->nCustHeaders + 1;
2575 if (count > 1)
2576 lph = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lpwhr->pCustHeaders, sizeof(HTTPHEADERW) * count);
2577 else
2578 lph = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(HTTPHEADERW) * count);
2580 if (NULL != lph)
2582 lpwhr->pCustHeaders = lph;
2583 lpwhr->pCustHeaders[count-1].lpszField = WININET_strdupW(lpHdr->lpszField);
2584 lpwhr->pCustHeaders[count-1].lpszValue = WININET_strdupW(lpHdr->lpszValue);
2585 lpwhr->pCustHeaders[count-1].wFlags = lpHdr->wFlags;
2586 lpwhr->pCustHeaders[count-1].wCount= lpHdr->wCount;
2587 lpwhr->nCustHeaders++;
2588 r = TRUE;
2590 else
2592 INTERNET_SetLastError(ERROR_OUTOFMEMORY);
2595 return r;
2599 /***********************************************************************
2600 * HTTP_DeleteCustomHeader (internal)
2602 * Delete header from array
2603 * If this function is called, the indexs may change.
2605 BOOL HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr, INT index)
2607 if( lpwhr->nCustHeaders <= 0 )
2608 return FALSE;
2609 if( lpwhr->nCustHeaders >= index )
2610 return FALSE;
2611 lpwhr->nCustHeaders--;
2613 memmove( &lpwhr->pCustHeaders[index], &lpwhr->pCustHeaders[index+1],
2614 (lpwhr->nCustHeaders - index)* sizeof(HTTPHEADERW) );
2615 memset( &lpwhr->pCustHeaders[lpwhr->nCustHeaders], 0, sizeof(HTTPHEADERW) );
2617 return TRUE;
2620 /***********************************************************************
2621 * IsHostInProxyBypassList (@)
2623 * Undocumented
2626 BOOL WINAPI IsHostInProxyBypassList(DWORD flags, LPCSTR szHost, DWORD length)
2628 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags,szHost,length);
2629 return FALSE;