2 * Wininet - Http Implementation
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
6 * Copyright 2002 TransGaming Technologies Inc.
7 * Copyright 2004 Mike McCormack for CodeWeavers
8 * Copyright 2005 Aric Stewart for CodeWeavers
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
29 #include "wine/port.h"
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_SOCKET_H
33 # include <sys/socket.h>
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
52 #define NO_SHLWAPI_STREAM
53 #define NO_SHLWAPI_REG
54 #define NO_SHLWAPI_STRFCNS
55 #define NO_SHLWAPI_GDI
59 #include "wine/debug.h"
60 #include "wine/unicode.h"
62 #include "inet_ntop.c"
64 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
66 static const WCHAR g_szHttp1_0
[] = {' ','H','T','T','P','/','1','.','0',0 };
67 static const WCHAR g_szHttp1_1
[] = {' ','H','T','T','P','/','1','.','1',0 };
68 static const WCHAR g_szReferer
[] = {'R','e','f','e','r','e','r',0};
69 static const WCHAR g_szAccept
[] = {'A','c','c','e','p','t',0};
70 static const WCHAR g_szUserAgent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
71 static const WCHAR szHost
[] = { 'H','o','s','t',0 };
72 static const WCHAR szProxy_Authorization
[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
73 static const WCHAR szStatus
[] = { 'S','t','a','t','u','s',0 };
75 #define MAXHOSTNAME 100
76 #define MAX_FIELD_VALUE_LEN 256
77 #define MAX_FIELD_LEN 256
79 #define HTTP_REFERER g_szReferer
80 #define HTTP_ACCEPT g_szAccept
81 #define HTTP_USERAGENT g_szUserAgent
83 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
84 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
85 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
86 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
87 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
88 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
89 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
92 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr
);
93 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr
);
94 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
);
95 static BOOL
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
);
96 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
97 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
98 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
);
99 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
, INT index
, BOOL Request
);
100 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
);
101 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
102 static BOOL
HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr
,
103 LPCWSTR username
, LPCWSTR password
);
104 static BOOL WINAPI
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD
105 dwInfoLevel
, LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD
107 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
,
108 LPCWSTR lpszHeaders
, DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD
109 dwOptionalLength
, DWORD dwContentLength
);
112 LPHTTPHEADERW
HTTP_GetHeader(LPWININETHTTPREQW req
, LPCWSTR head
)
115 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
116 if (HeaderIndex
== -1)
119 return &req
->pCustHeaders
[HeaderIndex
];
122 /***********************************************************************
123 * HTTP_Tokenize (internal)
125 * Tokenize a string, allocating memory for the tokens.
127 static LPWSTR
* HTTP_Tokenize(LPCWSTR string
, LPCWSTR token_string
)
129 LPWSTR
* token_array
;
134 /* empty string has no tokens */
138 for (i
= 0; string
[i
]; i
++)
139 if (!strncmpW(string
+i
, token_string
, strlenW(token_string
)))
143 /* we want to skip over separators, but not the null terminator */
144 for (j
= 0; j
< strlenW(token_string
) - 1; j
++)
150 /* add 1 for terminating NULL */
151 token_array
= HeapAlloc(GetProcessHeap(), 0, (tokens
+1) * sizeof(*token_array
));
152 token_array
[tokens
] = NULL
;
155 for (i
= 0; i
< tokens
; i
++)
158 next_token
= strstrW(string
, token_string
);
159 if (!next_token
) next_token
= string
+strlenW(string
);
160 len
= next_token
- string
;
161 token_array
[i
] = HeapAlloc(GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
));
162 memcpy(token_array
[i
], string
, len
*sizeof(WCHAR
));
163 token_array
[i
][len
] = '\0';
164 string
= next_token
+strlenW(token_string
);
169 /***********************************************************************
170 * HTTP_FreeTokens (internal)
172 * Frees memory returned from HTTP_Tokenize.
174 static void HTTP_FreeTokens(LPWSTR
* token_array
)
177 for (i
= 0; token_array
[i
]; i
++)
178 HeapFree(GetProcessHeap(), 0, token_array
[i
]);
179 HeapFree(GetProcessHeap(), 0, token_array
);
182 /* **********************************************************************
184 * Helper functions for the HttpSendRequest(Ex) functions
187 static void HTTP_FixVerb( LPWININETHTTPREQW lpwhr
)
189 /* if the verb is NULL default to GET */
190 if (NULL
== lpwhr
->lpszVerb
)
192 static const WCHAR szGET
[] = { 'G','E','T', 0 };
193 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
197 static void HTTP_FixURL( LPWININETHTTPREQW lpwhr
)
199 static const WCHAR szSlash
[] = { '/',0 };
200 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
202 /* If we don't have a path we set it to root */
203 if (NULL
== lpwhr
->lpszPath
)
204 lpwhr
->lpszPath
= WININET_strdupW(szSlash
);
205 else /* remove \r and \n*/
207 int nLen
= strlenW(lpwhr
->lpszPath
);
208 while ((nLen
>0 ) && ((lpwhr
->lpszPath
[nLen
-1] == '\r')||(lpwhr
->lpszPath
[nLen
-1] == '\n')))
211 lpwhr
->lpszPath
[nLen
]='\0';
213 /* Replace '\' with '/' */
216 if (lpwhr
->lpszPath
[nLen
] == '\\') lpwhr
->lpszPath
[nLen
]='/';
220 if(CSTR_EQUAL
!= CompareStringW( LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
221 lpwhr
->lpszPath
, strlenW(szHttp
), szHttp
, strlenW(szHttp
) )
222 && lpwhr
->lpszPath
[0] != '/') /* not an absolute path ?? --> fix it !! */
224 WCHAR
*fixurl
= HeapAlloc(GetProcessHeap(), 0,
225 (strlenW(lpwhr
->lpszPath
) + 2)*sizeof(WCHAR
));
227 strcpyW(fixurl
+ 1, lpwhr
->lpszPath
);
228 HeapFree( GetProcessHeap(), 0, lpwhr
->lpszPath
);
229 lpwhr
->lpszPath
= fixurl
;
233 static LPWSTR
HTTP_BuildHeaderRequestString( LPWININETHTTPREQW lpwhr
, LPCWSTR verb
, LPCWSTR path
, BOOL http1_1
)
235 LPWSTR requestString
;
241 static const WCHAR szSpace
[] = { ' ',0 };
242 static const WCHAR szcrlf
[] = {'\r','\n', 0};
243 static const WCHAR szColon
[] = { ':',' ',0 };
244 static const WCHAR sztwocrlf
[] = {'\r','\n','\r','\n', 0};
246 /* allocate space for an array of all the string pointers to be added */
247 len
= (lpwhr
->nCustHeaders
)*4 + 9;
248 req
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(LPCWSTR
) );
250 /* add the verb, path and HTTP version string */
255 req
[n
++] = http1_1
? g_szHttp1_1
: g_szHttp1_0
;
257 /* Append custom request heades */
258 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
260 if (lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
263 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszField
;
265 req
[n
++] = lpwhr
->pCustHeaders
[i
].lpszValue
;
267 TRACE("Adding custom header %s (%s)\n",
268 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszField
),
269 debugstr_w(lpwhr
->pCustHeaders
[i
].lpszValue
));
274 ERR("oops. buffer overrun\n");
277 requestString
= HTTP_build_req( req
, 4 );
278 HeapFree( GetProcessHeap(), 0, req
);
281 * Set (header) termination string for request
282 * Make sure there's exactly two new lines at the end of the request
284 p
= &requestString
[strlenW(requestString
)-1];
285 while ( (*p
== '\n') || (*p
== '\r') )
287 strcpyW( p
+1, sztwocrlf
);
289 return requestString
;
292 static void HTTP_ProcessHeaders( LPWININETHTTPREQW lpwhr
)
294 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
296 LPHTTPHEADERW setCookieHeader
;
298 HeaderIndex
= HTTP_GetCustomHeaderIndex(lpwhr
, szSet_Cookie
, 0, FALSE
);
299 if (HeaderIndex
== -1)
301 setCookieHeader
= &lpwhr
->pCustHeaders
[HeaderIndex
];
303 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
) && setCookieHeader
->lpszValue
)
305 int nPosStart
= 0, nPosEnd
= 0, len
;
306 static const WCHAR szFmt
[] = { 'h','t','t','p',':','/','/','%','s','/',0};
308 while (setCookieHeader
->lpszValue
[nPosEnd
] != '\0')
310 LPWSTR buf_cookie
, cookie_name
, cookie_data
;
312 LPWSTR domain
= NULL
;
316 while (setCookieHeader
->lpszValue
[nPosEnd
] != ';' && setCookieHeader
->lpszValue
[nPosEnd
] != ',' &&
317 setCookieHeader
->lpszValue
[nPosEnd
] != '\0')
321 if (setCookieHeader
->lpszValue
[nPosEnd
] == ';')
323 /* fixme: not case sensitive, strcasestr is gnu only */
324 int nDomainPosEnd
= 0;
325 int nDomainPosStart
= 0, nDomainLength
= 0;
326 static const WCHAR szDomain
[] = {'d','o','m','a','i','n','=',0};
327 LPWSTR lpszDomain
= strstrW(&setCookieHeader
->lpszValue
[nPosEnd
], szDomain
);
329 { /* they have specified their own domain, lets use it */
330 while (lpszDomain
[nDomainPosEnd
] != ';' && lpszDomain
[nDomainPosEnd
] != ',' &&
331 lpszDomain
[nDomainPosEnd
] != '\0')
335 nDomainPosStart
= strlenW(szDomain
);
336 nDomainLength
= (nDomainPosEnd
- nDomainPosStart
) + 1;
337 domain
= HeapAlloc(GetProcessHeap(), 0, (nDomainLength
+ 1)*sizeof(WCHAR
));
338 lstrcpynW(domain
, &lpszDomain
[nDomainPosStart
], nDomainLength
+ 1);
341 if (setCookieHeader
->lpszValue
[nPosEnd
] == '\0') break;
342 buf_cookie
= HeapAlloc(GetProcessHeap(), 0, ((nPosEnd
- nPosStart
) + 1)*sizeof(WCHAR
));
343 lstrcpynW(buf_cookie
, &setCookieHeader
->lpszValue
[nPosStart
], (nPosEnd
- nPosStart
) + 1);
344 TRACE("%s\n", debugstr_w(buf_cookie
));
345 while (buf_cookie
[nEqualPos
] != '=' && buf_cookie
[nEqualPos
] != '\0')
349 if (buf_cookie
[nEqualPos
] == '\0' || buf_cookie
[nEqualPos
+ 1] == '\0')
351 HeapFree(GetProcessHeap(), 0, buf_cookie
);
355 cookie_name
= HeapAlloc(GetProcessHeap(), 0, (nEqualPos
+ 1)*sizeof(WCHAR
));
356 lstrcpynW(cookie_name
, buf_cookie
, nEqualPos
+ 1);
357 cookie_data
= &buf_cookie
[nEqualPos
+ 1];
359 Host
= HTTP_GetHeader(lpwhr
,szHost
);
360 len
= lstrlenW((domain
? domain
: (Host
?Host
->lpszValue
:NULL
))) +
361 strlenW(lpwhr
->lpszPath
) + 9;
362 buf_url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
363 sprintfW(buf_url
, szFmt
, (domain
? domain
: (Host
?Host
->lpszValue
:NULL
))); /* FIXME PATH!!! */
364 InternetSetCookieW(buf_url
, cookie_name
, cookie_data
);
366 HeapFree(GetProcessHeap(), 0, buf_url
);
367 HeapFree(GetProcessHeap(), 0, buf_cookie
);
368 HeapFree(GetProcessHeap(), 0, cookie_name
);
369 HeapFree(GetProcessHeap(), 0, domain
);
375 static void HTTP_AddProxyInfo( LPWININETHTTPREQW lpwhr
)
377 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
)lpwhr
->hdr
.lpwhparent
;
378 LPWININETAPPINFOW hIC
= (LPWININETAPPINFOW
)lpwhs
->hdr
.lpwhparent
;
380 assert(lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
381 assert(hIC
->hdr
.htype
== WH_HINIT
);
383 if (hIC
&& (hIC
->lpszProxyUsername
|| hIC
->lpszProxyPassword
))
384 HTTP_InsertProxyAuthorization(lpwhr
, hIC
->lpszProxyUsername
,
385 hIC
->lpszProxyPassword
);
388 /***********************************************************************
389 * HTTP_HttpAddRequestHeadersW (internal)
391 static BOOL WINAPI
HTTP_HttpAddRequestHeadersW(LPWININETHTTPREQW lpwhr
,
392 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
397 BOOL bSuccess
= FALSE
;
400 TRACE("copying header: %s\n", debugstr_w(lpszHeader
));
402 if( dwHeaderLength
== ~0U )
403 len
= strlenW(lpszHeader
);
405 len
= dwHeaderLength
;
406 buffer
= HeapAlloc( GetProcessHeap(), 0, sizeof(WCHAR
)*(len
+1) );
407 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
413 LPWSTR
* pFieldAndValue
;
417 while (*lpszEnd
!= '\0')
419 if (*lpszEnd
== '\r' && *(lpszEnd
+ 1) == '\n')
424 if (*lpszStart
== '\0')
427 if (*lpszEnd
== '\r')
430 lpszEnd
+= 2; /* Jump over \r\n */
432 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
433 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
436 bSuccess
= HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0],
437 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
438 HTTP_FreeTokens(pFieldAndValue
);
444 HeapFree(GetProcessHeap(), 0, buffer
);
449 /***********************************************************************
450 * HttpAddRequestHeadersW (WININET.@)
452 * Adds one or more HTTP header to the request handler
459 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
460 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
462 BOOL bSuccess
= FALSE
;
463 LPWININETHTTPREQW lpwhr
;
465 TRACE("%p, %s, %li, %li\n", hHttpRequest
, debugstr_w(lpszHeader
), dwHeaderLength
,
471 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
472 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
474 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
477 bSuccess
= HTTP_HttpAddRequestHeadersW( lpwhr
, lpszHeader
, dwHeaderLength
, dwModifier
);
480 WININET_Release( &lpwhr
->hdr
);
485 /***********************************************************************
486 * HttpAddRequestHeadersA (WININET.@)
488 * Adds one or more HTTP header to the request handler
495 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
496 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
502 TRACE("%p, %s, %li, %li\n", hHttpRequest
, debugstr_a(lpszHeader
), dwHeaderLength
,
505 len
= MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, NULL
, 0 );
506 hdr
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
507 MultiByteToWideChar( CP_ACP
, 0, lpszHeader
, dwHeaderLength
, hdr
, len
);
508 if( dwHeaderLength
!= ~0U )
509 dwHeaderLength
= len
;
511 r
= HttpAddRequestHeadersW( hHttpRequest
, hdr
, dwHeaderLength
, dwModifier
);
513 HeapFree( GetProcessHeap(), 0, hdr
);
518 /***********************************************************************
519 * HttpEndRequestA (WININET.@)
521 * Ends an HTTP request that was started by HttpSendRequestEx
528 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
529 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD dwContext
)
531 LPINTERNET_BUFFERSA ptr
;
532 LPINTERNET_BUFFERSW lpBuffersOutW
,ptrW
;
535 TRACE("(%p, %p, %08lx, %08lx): stub\n", hRequest
, lpBuffersOut
, dwFlags
,
540 lpBuffersOutW
= (LPINTERNET_BUFFERSW
)HeapAlloc(GetProcessHeap(),
541 HEAP_ZERO_MEMORY
, sizeof(INTERNET_BUFFERSW
));
543 lpBuffersOutW
= NULL
;
545 ptrW
= lpBuffersOutW
;
548 if (ptr
->lpvBuffer
&& ptr
->dwBufferLength
)
549 ptrW
->lpvBuffer
= HeapAlloc(GetProcessHeap(),0,ptr
->dwBufferLength
);
550 ptrW
->dwBufferLength
= ptr
->dwBufferLength
;
551 ptrW
->dwBufferTotal
= ptr
->dwBufferTotal
;
554 ptrW
->Next
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,
555 sizeof(INTERNET_BUFFERSW
));
561 rc
= HttpEndRequestW(hRequest
, lpBuffersOutW
, dwFlags
, dwContext
);
565 ptrW
= lpBuffersOutW
;
568 LPINTERNET_BUFFERSW ptrW2
;
570 FIXME("Do we need to translate info out of these buffer?\n");
572 HeapFree(GetProcessHeap(),0,(LPVOID
)ptrW
->lpvBuffer
);
574 HeapFree(GetProcessHeap(),0,ptrW
);
582 /***********************************************************************
583 * HttpEndRequestW (WININET.@)
585 * Ends an HTTP request that was started by HttpSendRequestEx
592 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
593 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD dwContext
)
596 LPWININETHTTPREQW lpwhr
;
600 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
602 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
604 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
608 lpwhr
->hdr
.dwFlags
|= dwFlags
;
609 lpwhr
->hdr
.dwContext
= dwContext
;
611 SendAsyncCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
612 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
614 responseLen
= HTTP_GetResponseHeaders(lpwhr
);
618 SendAsyncCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
619 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
621 /* process headers here. Is this right? */
622 HTTP_ProcessHeaders(lpwhr
);
624 /* We appear to do nothing with the buffer.. is that correct? */
626 if(!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
628 DWORD dwCode
,dwCodeLength
=sizeof(DWORD
),dwIndex
=0;
629 if(HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
,&dwCode
,&dwCodeLength
,&dwIndex
) &&
630 (dwCode
==302 || dwCode
==301))
632 WCHAR szNewLocation
[2048];
633 DWORD dwBufferSize
=2048;
635 if(HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,&dwIndex
))
637 static const WCHAR szGET
[] = { 'G','E','T', 0 };
638 /* redirects are always GETs */
639 HeapFree(GetProcessHeap(),0,lpwhr
->lpszVerb
);
640 lpwhr
->lpszVerb
= WININET_strdupW(szGET
);
641 return HTTP_HandleRedirect(lpwhr
, szNewLocation
, NULL
, 0, NULL
, 0, 0);
646 TRACE("%i <--\n",rc
);
650 /***********************************************************************
651 * HttpOpenRequestW (WININET.@)
653 * Open a HTTP request handle
656 * HINTERNET a HTTP request handle on success
660 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
661 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
662 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
663 DWORD dwFlags
, DWORD dwContext
)
665 LPWININETHTTPSESSIONW lpwhs
;
666 HINTERNET handle
= NULL
;
668 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession
,
669 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
670 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
672 if(lpszAcceptTypes
!=NULL
)
675 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
676 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
679 lpwhs
= (LPWININETHTTPSESSIONW
) WININET_GetObject( hHttpSession
);
680 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
682 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
687 * My tests seem to show that the windows version does not
688 * become asynchronous until after this point. And anyhow
689 * if this call was asynchronous then how would you get the
690 * necessary HINTERNET pointer returned by this function.
693 handle
= HTTP_HttpOpenRequestW(lpwhs
, lpszVerb
, lpszObjectName
,
694 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
698 WININET_Release( &lpwhs
->hdr
);
699 TRACE("returning %p\n", handle
);
704 /***********************************************************************
705 * HttpOpenRequestA (WININET.@)
707 * Open a HTTP request handle
710 * HINTERNET a HTTP request handle on success
714 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
715 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
716 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
717 DWORD dwFlags
, DWORD dwContext
)
719 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
720 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
722 INT acceptTypesCount
;
723 HINTERNET rc
= FALSE
;
724 TRACE("(%p, %s, %s, %s, %s, %p, %08lx, %08lx)\n", hHttpSession
,
725 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
726 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
731 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, NULL
, 0 );
732 szVerb
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
735 MultiByteToWideChar(CP_ACP
, 0, lpszVerb
, -1, szVerb
, len
);
740 len
= MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, NULL
, 0 );
741 szObjectName
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
744 MultiByteToWideChar(CP_ACP
, 0, lpszObjectName
, -1, szObjectName
, len
);
749 len
= MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, NULL
, 0 );
750 szVersion
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
753 MultiByteToWideChar(CP_ACP
, 0, lpszVersion
, -1, szVersion
, len
);
758 len
= MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, NULL
, 0 );
759 szReferrer
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
762 MultiByteToWideChar(CP_ACP
, 0, lpszReferrer
, -1, szReferrer
, len
);
765 acceptTypesCount
= 0;
768 /* find out how many there are */
769 while (lpszAcceptTypes
[acceptTypesCount
])
771 szAcceptTypes
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
*) * (acceptTypesCount
+1));
772 acceptTypesCount
= 0;
773 while (lpszAcceptTypes
[acceptTypesCount
])
775 len
= MultiByteToWideChar(CP_ACP
, 0, lpszAcceptTypes
[acceptTypesCount
],
777 szAcceptTypes
[acceptTypesCount
] = HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
778 if (!szAcceptTypes
[acceptTypesCount
] )
780 MultiByteToWideChar(CP_ACP
, 0, lpszAcceptTypes
[acceptTypesCount
],
781 -1, szAcceptTypes
[acceptTypesCount
], len
);
784 szAcceptTypes
[acceptTypesCount
] = NULL
;
786 else szAcceptTypes
= 0;
788 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
,
789 szVersion
, szReferrer
,
790 (LPCWSTR
*)szAcceptTypes
, dwFlags
, dwContext
);
795 acceptTypesCount
= 0;
796 while (szAcceptTypes
[acceptTypesCount
])
798 HeapFree(GetProcessHeap(), 0, szAcceptTypes
[acceptTypesCount
]);
801 HeapFree(GetProcessHeap(), 0, szAcceptTypes
);
803 HeapFree(GetProcessHeap(), 0, szReferrer
);
804 HeapFree(GetProcessHeap(), 0, szVersion
);
805 HeapFree(GetProcessHeap(), 0, szObjectName
);
806 HeapFree(GetProcessHeap(), 0, szVerb
);
811 /***********************************************************************
814 static UINT
HTTP_Base64( LPCWSTR bin
, LPWSTR base64
)
817 static LPCSTR HTTP_Base64Enc
=
818 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
822 /* first 6 bits, all from bin[0] */
823 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
824 x
= (bin
[0] & 3) << 4;
826 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
829 base64
[n
++] = HTTP_Base64Enc
[x
];
834 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
835 x
= ( bin
[1] & 0x0f ) << 2;
837 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
840 base64
[n
++] = HTTP_Base64Enc
[x
];
844 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
846 /* last 6 bits, all from bin [2] */
847 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
854 /***********************************************************************
855 * HTTP_EncodeBasicAuth
857 * Encode the basic authentication string for HTTP 1.1
859 static LPWSTR
HTTP_EncodeBasicAuth( LPCWSTR username
, LPCWSTR password
)
863 static const WCHAR szBasic
[] = {'B','a','s','i','c',' ',0};
864 static const WCHAR szColon
[] = {':',0};
866 len
= lstrlenW( username
) + 1 + lstrlenW ( password
) + 1;
867 in
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
871 len
= lstrlenW(szBasic
) +
872 (lstrlenW( username
) + 1 + lstrlenW ( password
))*2 + 1 + 1;
873 out
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
876 lstrcpyW( in
, username
);
877 lstrcatW( in
, szColon
);
878 lstrcatW( in
, password
);
879 lstrcpyW( out
, szBasic
);
880 HTTP_Base64( in
, &out
[strlenW(out
)] );
882 HeapFree( GetProcessHeap(), 0, in
);
887 /***********************************************************************
888 * HTTP_InsertProxyAuthorization
890 * Insert the basic authorization field in the request header
892 static BOOL
HTTP_InsertProxyAuthorization( LPWININETHTTPREQW lpwhr
,
893 LPCWSTR username
, LPCWSTR password
)
895 WCHAR
*authorization
= HTTP_EncodeBasicAuth( username
, password
);
901 TRACE( "Inserting authorization: %s\n", debugstr_w( authorization
) );
903 HTTP_ProcessHeader(lpwhr
, szProxy_Authorization
, authorization
,
904 HTTP_ADDHDR_FLAG_REPLACE
);
906 HeapFree( GetProcessHeap(), 0, authorization
);
911 /***********************************************************************
914 static BOOL
HTTP_DealWithProxy( LPWININETAPPINFOW hIC
,
915 LPWININETHTTPSESSIONW lpwhs
, LPWININETHTTPREQW lpwhr
)
917 WCHAR buf
[MAXHOSTNAME
];
918 WCHAR proxy
[MAXHOSTNAME
+ 15]; /* 15 == "http://" + sizeof(port#) + ":/\0" */
920 static const WCHAR szNul
[] = { 0 };
921 URL_COMPONENTSW UrlComponents
;
922 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 }, szSlash
[] = { '/',0 } ;
923 static const WCHAR szFormat1
[] = { 'h','t','t','p',':','/','/','%','s',0 };
924 static const WCHAR szFormat2
[] = { 'h','t','t','p',':','/','/','%','s',':','%','d',0 };
927 memset( &UrlComponents
, 0, sizeof UrlComponents
);
928 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
929 UrlComponents
.lpszHostName
= buf
;
930 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
932 if( CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
933 hIC
->lpszProxy
,strlenW(szHttp
),szHttp
,strlenW(szHttp
)) )
934 sprintfW(proxy
, szFormat1
, hIC
->lpszProxy
);
936 strcpyW(proxy
, hIC
->lpszProxy
);
937 if( !InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) )
939 if( UrlComponents
.dwHostNameLength
== 0 )
942 if( !lpwhr
->lpszPath
)
943 lpwhr
->lpszPath
= (LPWSTR
)szNul
;
944 TRACE("server='%s' path='%s'\n",
945 debugstr_w(lpwhs
->lpszHostName
), debugstr_w(lpwhr
->lpszPath
));
946 /* for constant 15 see above */
947 len
= strlenW(lpwhs
->lpszHostName
) + strlenW(lpwhr
->lpszPath
) + 15;
948 url
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
950 if(UrlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
951 UrlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
953 sprintfW(url
, szFormat2
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
955 if( lpwhr
->lpszPath
[0] != '/' )
956 strcatW( url
, szSlash
);
957 strcatW(url
, lpwhr
->lpszPath
);
958 if(lpwhr
->lpszPath
!= szNul
)
959 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
960 lpwhr
->lpszPath
= url
;
962 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
963 lpwhs
->lpszServerName
= WININET_strdupW(UrlComponents
.lpszHostName
);
964 lpwhs
->nServerPort
= UrlComponents
.nPort
;
969 /***********************************************************************
970 * HTTP_HttpOpenRequestW (internal)
972 * Open a HTTP request handle
975 * HINTERNET a HTTP request handle on success
979 HINTERNET WINAPI
HTTP_HttpOpenRequestW(LPWININETHTTPSESSIONW lpwhs
,
980 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
981 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
982 DWORD dwFlags
, DWORD dwContext
)
984 LPWININETAPPINFOW hIC
= NULL
;
985 LPWININETHTTPREQW lpwhr
;
987 LPWSTR lpszUrl
= NULL
;
989 HINTERNET handle
= NULL
;
990 static const WCHAR szUrlForm
[] = {'h','t','t','p',':','/','/','%','s',0};
997 assert( lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
998 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
1000 lpwhr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPREQW
));
1003 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1006 lpwhr
->hdr
.htype
= WH_HHTTPREQ
;
1007 lpwhr
->hdr
.lpwhparent
= WININET_AddRef( &lpwhs
->hdr
);
1008 lpwhr
->hdr
.dwFlags
= dwFlags
;
1009 lpwhr
->hdr
.dwContext
= dwContext
;
1010 lpwhr
->hdr
.dwRefCount
= 1;
1011 lpwhr
->hdr
.destroy
= HTTP_CloseHTTPRequestHandle
;
1012 lpwhr
->hdr
.lpfnStatusCB
= lpwhs
->hdr
.lpfnStatusCB
;
1014 handle
= WININET_AllocHandle( &lpwhr
->hdr
);
1017 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
1021 if (!NETCON_init(&lpwhr
->netConnection
, dwFlags
& INTERNET_FLAG_SECURE
))
1023 InternetCloseHandle( handle
);
1028 if (NULL
!= lpszObjectName
&& strlenW(lpszObjectName
)) {
1032 rc
= UrlEscapeW(lpszObjectName
, NULL
, &len
, URL_ESCAPE_SPACES_ONLY
);
1033 if (rc
!= E_POINTER
)
1034 len
= strlenW(lpszObjectName
)+1;
1035 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
1036 rc
= UrlEscapeW(lpszObjectName
, lpwhr
->lpszPath
, &len
,
1037 URL_ESCAPE_SPACES_ONLY
);
1040 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(lpszObjectName
),rc
);
1041 strcpyW(lpwhr
->lpszPath
,lpszObjectName
);
1045 if (NULL
!= lpszReferrer
&& strlenW(lpszReferrer
))
1046 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDHDR_FLAG_COALESCE
);
1048 if(lpszAcceptTypes
!=NULL
)
1051 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
1052 HTTP_ProcessHeader(lpwhr
, HTTP_ACCEPT
, lpszAcceptTypes
[i
], HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
1055 if (NULL
== lpszVerb
)
1057 static const WCHAR szGet
[] = {'G','E','T',0};
1058 lpwhr
->lpszVerb
= WININET_strdupW(szGet
);
1060 else if (strlenW(lpszVerb
))
1061 lpwhr
->lpszVerb
= WININET_strdupW(lpszVerb
);
1063 if (NULL
!= lpszReferrer
&& strlenW(lpszReferrer
))
1065 WCHAR buf
[MAXHOSTNAME
];
1066 URL_COMPONENTSW UrlComponents
;
1068 memset( &UrlComponents
, 0, sizeof UrlComponents
);
1069 UrlComponents
.dwStructSize
= sizeof UrlComponents
;
1070 UrlComponents
.lpszHostName
= buf
;
1071 UrlComponents
.dwHostNameLength
= MAXHOSTNAME
;
1073 InternetCrackUrlW(lpszReferrer
, 0, 0, &UrlComponents
);
1074 if (strlenW(UrlComponents
.lpszHostName
))
1075 HTTP_ProcessHeader(lpwhr
, szHost
, UrlComponents
.lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
1078 HTTP_ProcessHeader(lpwhr
, szHost
, lpwhs
->lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
1080 if (lpwhs
->nServerPort
== INTERNET_INVALID_PORT_NUMBER
)
1081 lpwhs
->nServerPort
= (dwFlags
& INTERNET_FLAG_SECURE
?
1082 INTERNET_DEFAULT_HTTPS_PORT
:
1083 INTERNET_DEFAULT_HTTP_PORT
);
1084 lpwhs
->nHostPort
= lpwhs
->nServerPort
;
1086 if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
1087 HTTP_DealWithProxy( hIC
, lpwhs
, lpwhr
);
1091 WCHAR
*agent_header
;
1092 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0 };
1094 len
= strlenW(hIC
->lpszAgent
) + strlenW(user_agent
);
1095 agent_header
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1096 sprintfW(agent_header
, user_agent
, hIC
->lpszAgent
);
1098 HTTP_HttpAddRequestHeadersW(lpwhr
, agent_header
, strlenW(agent_header
),
1099 HTTP_ADDREQ_FLAG_ADD
);
1100 HeapFree(GetProcessHeap(), 0, agent_header
);
1103 Host
= HTTP_GetHeader(lpwhr
,szHost
);
1105 len
= lstrlenW(Host
->lpszValue
) + strlenW(szUrlForm
);
1106 lpszUrl
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
1107 sprintfW( lpszUrl
, szUrlForm
, Host
->lpszValue
);
1109 if (!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
) &&
1110 InternetGetCookieW(lpszUrl
, NULL
, NULL
, &nCookieSize
))
1113 static const WCHAR szCookie
[] = {'C','o','o','k','i','e',':',' ',0};
1114 static const WCHAR szcrlf
[] = {'\r','\n',0};
1116 lpszCookies
= HeapAlloc(GetProcessHeap(), 0, (nCookieSize
+ 1 + 8)*sizeof(WCHAR
));
1118 cnt
+= sprintfW(lpszCookies
, szCookie
);
1119 InternetGetCookieW(lpszUrl
, NULL
, lpszCookies
+ cnt
, &nCookieSize
);
1120 strcatW(lpszCookies
, szcrlf
);
1122 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszCookies
, strlenW(lpszCookies
),
1123 HTTP_ADDREQ_FLAG_ADD
);
1124 HeapFree(GetProcessHeap(), 0, lpszCookies
);
1126 HeapFree(GetProcessHeap(), 0, lpszUrl
);
1129 INTERNET_SendCallback(&lpwhs
->hdr
, dwContext
,
1130 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
1134 * A STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on windows
1138 * According to my tests. The name is not resolved until a request is Opened
1140 INTERNET_SendCallback(&lpwhr
->hdr
, dwContext
,
1141 INTERNET_STATUS_RESOLVING_NAME
,
1142 lpwhs
->lpszServerName
,
1143 strlenW(lpwhs
->lpszServerName
)+1);
1145 if (!GetAddress(lpwhs
->lpszServerName
, lpwhs
->nServerPort
,
1146 &lpwhs
->socketAddress
))
1148 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
1149 InternetCloseHandle( handle
);
1154 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
1155 szaddr
, sizeof(szaddr
));
1156 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
1157 INTERNET_STATUS_NAME_RESOLVED
,
1158 szaddr
, strlen(szaddr
)+1);
1162 WININET_Release( &lpwhr
->hdr
);
1164 TRACE("<-- %p (%p)\n", handle
, lpwhr
);
1168 typedef struct std_hdr_data
1170 const WCHAR
* hdrStr
;
1174 static const WCHAR szAccept
[] = { 'A','c','c','e','p','t',0 };
1175 static const WCHAR szAccept_Charset
[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
1176 static const WCHAR szAccept_Encoding
[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
1177 static const WCHAR szAccept_Language
[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
1178 static const WCHAR szAccept_Ranges
[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
1179 static const WCHAR szAge
[] = { 'A','g','e',0 };
1180 static const WCHAR szAllow
[] = { 'A','l','l','o','w',0 };
1181 static const WCHAR szAuthorization
[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
1182 static const WCHAR szCache_Control
[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
1183 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
1184 static const WCHAR szContent_Base
[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
1185 static const WCHAR szContent_Encoding
[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
1186 static const WCHAR szContent_ID
[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
1187 static const WCHAR szContent_Language
[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
1188 static const WCHAR szContent_Length
[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
1189 static const WCHAR szContent_Location
[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
1190 static const WCHAR szContent_MD5
[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
1191 static const WCHAR szContent_Range
[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
1192 static const WCHAR szContent_Transfer_Encoding
[] = { 'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1193 static const WCHAR szContent_Type
[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
1194 static const WCHAR szCookie
[] = { 'C','o','o','k','i','e',0 };
1195 static const WCHAR szDate
[] = { 'D','a','t','e',0 };
1196 static const WCHAR szFrom
[] = { 'F','r','o','m',0 };
1197 static const WCHAR szETag
[] = { 'E','T','a','g',0 };
1198 static const WCHAR szExpect
[] = { 'E','x','p','e','c','t',0 };
1199 static const WCHAR szExpires
[] = { 'E','x','p','i','r','e','s',0 };
1200 static const WCHAR szIf_Match
[] = { 'I','f','-','M','a','t','c','h',0 };
1201 static const WCHAR szIf_Modified_Since
[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1202 static const WCHAR szIf_None_Match
[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
1203 static const WCHAR szIf_Range
[] = { 'I','f','-','R','a','n','g','e',0 };
1204 static const WCHAR szIf_Unmodified_Since
[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1205 static const WCHAR szLast_Modified
[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
1206 static const WCHAR szLocation
[] = { 'L','o','c','a','t','i','o','n',0 };
1207 static const WCHAR szMax_Forwards
[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
1208 static const WCHAR szMime_Version
[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
1209 static const WCHAR szPragma
[] = { 'P','r','a','g','m','a',0 };
1210 static const WCHAR szProxy_Authenticate
[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1211 static const WCHAR szProxy_Connection
[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
1212 static const WCHAR szPublic
[] = { 'P','u','b','l','i','c',0 };
1213 static const WCHAR szRange
[] = { 'R','a','n','g','e',0 };
1214 static const WCHAR szReferer
[] = { 'R','e','f','e','r','e','r',0 };
1215 static const WCHAR szRetry_After
[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
1216 static const WCHAR szServer
[] = { 'S','e','r','v','e','r',0 };
1217 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
1218 static const WCHAR szTransfer_Encoding
[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
1219 static const WCHAR szUnless_Modified_Since
[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
1220 static const WCHAR szUpgrade
[] = { 'U','p','g','r','a','d','e',0 };
1221 static const WCHAR szURI
[] = { 'U','R','I',0 };
1222 static const WCHAR szUser_Agent
[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
1223 static const WCHAR szVary
[] = { 'V','a','r','y',0 };
1224 static const WCHAR szVia
[] = { 'V','i','a',0 };
1225 static const WCHAR szWarning
[] = { 'W','a','r','n','i','n','g',0 };
1226 static const WCHAR szWWW_Authenticate
[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
1228 static const std_hdr_data SORTED_STANDARD_HEADERS
[] = {
1229 {szAccept
, HTTP_QUERY_ACCEPT
,},
1230 {szAccept_Charset
, HTTP_QUERY_ACCEPT_CHARSET
,},
1231 {szAccept_Encoding
, HTTP_QUERY_ACCEPT_ENCODING
,},
1232 {szAccept_Language
, HTTP_QUERY_ACCEPT_LANGUAGE
,},
1233 {szAccept_Ranges
, HTTP_QUERY_ACCEPT_RANGES
,},
1234 {szAge
, HTTP_QUERY_AGE
,},
1235 {szAllow
, HTTP_QUERY_ALLOW
,},
1236 {szAuthorization
, HTTP_QUERY_AUTHORIZATION
,},
1237 {szCache_Control
, HTTP_QUERY_CACHE_CONTROL
,},
1238 {szConnection
, HTTP_QUERY_CONNECTION
,},
1239 {szContent_Base
, HTTP_QUERY_CONTENT_BASE
,},
1240 {szContent_Encoding
, HTTP_QUERY_CONTENT_ENCODING
,},
1241 {szContent_ID
, HTTP_QUERY_CONTENT_ID
,},
1242 {szContent_Language
, HTTP_QUERY_CONTENT_LANGUAGE
,},
1243 {szContent_Length
, HTTP_QUERY_CONTENT_LENGTH
,},
1244 {szContent_Location
, HTTP_QUERY_CONTENT_LOCATION
,},
1245 {szContent_MD5
, HTTP_QUERY_CONTENT_MD5
,},
1246 {szContent_Range
, HTTP_QUERY_CONTENT_RANGE
,},
1247 {szContent_Transfer_Encoding
,HTTP_QUERY_CONTENT_TRANSFER_ENCODING
,},
1248 {szContent_Type
, HTTP_QUERY_CONTENT_TYPE
,},
1249 {szCookie
, HTTP_QUERY_COOKIE
,},
1250 {szDate
, HTTP_QUERY_DATE
,},
1251 {szETag
, HTTP_QUERY_ETAG
,},
1252 {szExpect
, HTTP_QUERY_EXPECT
,},
1253 {szExpires
, HTTP_QUERY_EXPIRES
,},
1254 {szFrom
, HTTP_QUERY_DERIVED_FROM
,},
1255 {szHost
, HTTP_QUERY_HOST
,},
1256 {szIf_Match
, HTTP_QUERY_IF_MATCH
,},
1257 {szIf_Modified_Since
, HTTP_QUERY_IF_MODIFIED_SINCE
,},
1258 {szIf_None_Match
, HTTP_QUERY_IF_NONE_MATCH
,},
1259 {szIf_Range
, HTTP_QUERY_IF_RANGE
,},
1260 {szIf_Unmodified_Since
, HTTP_QUERY_IF_UNMODIFIED_SINCE
,},
1261 {szLast_Modified
, HTTP_QUERY_LAST_MODIFIED
,},
1262 {szLocation
, HTTP_QUERY_LOCATION
,},
1263 {szMax_Forwards
, HTTP_QUERY_MAX_FORWARDS
,},
1264 {szMime_Version
, HTTP_QUERY_MIME_VERSION
,},
1265 {szPragma
, HTTP_QUERY_PRAGMA
,},
1266 {szProxy_Authenticate
, HTTP_QUERY_PROXY_AUTHENTICATE
,},
1267 {szProxy_Authorization
, HTTP_QUERY_PROXY_AUTHORIZATION
,},
1268 {szProxy_Connection
, HTTP_QUERY_PROXY_CONNECTION
,},
1269 {szPublic
, HTTP_QUERY_PUBLIC
,},
1270 {szRange
, HTTP_QUERY_RANGE
,},
1271 {szReferer
, HTTP_QUERY_REFERER
,},
1272 {szRetry_After
, HTTP_QUERY_RETRY_AFTER
,},
1273 {szServer
, HTTP_QUERY_SERVER
,},
1274 {szSet_Cookie
, HTTP_QUERY_SET_COOKIE
,},
1275 {szStatus
, HTTP_QUERY_STATUS_CODE
,},
1276 {szTransfer_Encoding
, HTTP_QUERY_TRANSFER_ENCODING
,},
1277 {szUnless_Modified_Since
, HTTP_QUERY_UNLESS_MODIFIED_SINCE
,},
1278 {szUpgrade
, HTTP_QUERY_UPGRADE
,},
1279 {szURI
, HTTP_QUERY_URI
,},
1280 {szUser_Agent
, HTTP_QUERY_USER_AGENT
,},
1281 {szVary
, HTTP_QUERY_VARY
,},
1282 {szVia
, HTTP_QUERY_VIA
,},
1283 {szWarning
, HTTP_QUERY_WARNING
,},
1284 {szWWW_Authenticate
, HTTP_QUERY_WWW_AUTHENTICATE
,},
1287 /***********************************************************************
1288 * HTTP_HttpQueryInfoW (internal)
1290 static BOOL WINAPI
HTTP_HttpQueryInfoW( LPWININETHTTPREQW lpwhr
, DWORD dwInfoLevel
,
1291 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1293 LPHTTPHEADERW lphttpHdr
= NULL
;
1294 BOOL bSuccess
= FALSE
;
1295 BOOL request_only
= dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
;
1298 /* Find requested header structure */
1299 if ((dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
) == HTTP_QUERY_CUSTOM
)
1301 INT requested_index
= (lpdwIndex
)?(*lpdwIndex
):0;
1302 INT index
= HTTP_GetCustomHeaderIndex(lpwhr
, (LPWSTR
)lpBuffer
,
1303 requested_index
,request_only
);
1308 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
1315 INT index
= dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
;
1317 if (index
== HTTP_QUERY_RAW_HEADERS_CRLF
)
1319 DWORD len
= strlenW(lpwhr
->lpszRawHeaders
);
1320 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1322 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
1323 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1326 memcpy(lpBuffer
, lpwhr
->lpszRawHeaders
, (len
+1)*sizeof(WCHAR
));
1327 *lpdwBufferLength
= len
* sizeof(WCHAR
);
1329 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, len
));
1333 else if (index
== HTTP_QUERY_RAW_HEADERS
)
1335 static const WCHAR szCrLf
[] = {'\r','\n',0};
1336 LPWSTR
* ppszRawHeaderLines
= HTTP_Tokenize(lpwhr
->lpszRawHeaders
, szCrLf
);
1338 LPWSTR pszString
= (WCHAR
*)lpBuffer
;
1340 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
1341 size
+= strlenW(ppszRawHeaderLines
[i
]) + 1;
1343 if (size
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1345 HTTP_FreeTokens(ppszRawHeaderLines
);
1346 *lpdwBufferLength
= (size
+ 1) * sizeof(WCHAR
);
1347 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1351 for (i
= 0; ppszRawHeaderLines
[i
]; i
++)
1353 DWORD len
= strlenW(ppszRawHeaderLines
[i
]);
1354 memcpy(pszString
, ppszRawHeaderLines
[i
], (len
+1)*sizeof(WCHAR
));
1359 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, size
));
1361 *lpdwBufferLength
= size
* sizeof(WCHAR
);
1362 HTTP_FreeTokens(ppszRawHeaderLines
);
1366 else if (index
== HTTP_QUERY_STATUS_TEXT
)
1368 DWORD len
= strlenW(lpwhr
->lpszStatusText
);
1369 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1371 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
1372 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1375 memcpy(lpBuffer
, lpwhr
->lpszStatusText
, (len
+1)*sizeof(WCHAR
));
1376 *lpdwBufferLength
= len
* sizeof(WCHAR
);
1378 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, len
));
1382 else if (index
== HTTP_QUERY_VERSION
)
1384 DWORD len
= strlenW(lpwhr
->lpszVersion
);
1385 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
1387 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
1388 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1391 memcpy(lpBuffer
, lpwhr
->lpszVersion
, (len
+1)*sizeof(WCHAR
));
1392 *lpdwBufferLength
= len
* sizeof(WCHAR
);
1394 TRACE("returning data: %s\n", debugstr_wn((WCHAR
*)lpBuffer
, len
));
1398 else if (index
>= 0 && index
<= HTTP_QUERY_MAX
)
1401 for (i
= 0; i
< sizeof(SORTED_STANDARD_HEADERS
)/sizeof(std_hdr_data
) ; i
++)
1403 if (SORTED_STANDARD_HEADERS
[i
].hdrIndex
== index
)
1405 INT requested_index
= (lpdwIndex
)?(*lpdwIndex
):0;
1406 INT index
= HTTP_GetCustomHeaderIndex(lpwhr
,
1407 (LPWSTR
)SORTED_STANDARD_HEADERS
[i
].hdrStr
,
1408 requested_index
,request_only
);
1413 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
1424 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
1430 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
1435 /* Ensure header satisifies requested attributes */
1436 if ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
1437 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
))
1439 SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
1443 /* coalesce value to reuqested type */
1444 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
)
1446 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
1449 TRACE(" returning number : %d\n", *(int *)lpBuffer
);
1451 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
)
1457 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
1459 tmpTM
= *gmtime(&tmpTime
);
1460 STHook
= (SYSTEMTIME
*) lpBuffer
;
1464 STHook
->wDay
= tmpTM
.tm_mday
;
1465 STHook
->wHour
= tmpTM
.tm_hour
;
1466 STHook
->wMilliseconds
= 0;
1467 STHook
->wMinute
= tmpTM
.tm_min
;
1468 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
1469 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
1470 STHook
->wSecond
= tmpTM
.tm_sec
;
1471 STHook
->wYear
= tmpTM
.tm_year
;
1475 TRACE(" returning time : %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
1476 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
1477 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
1479 else if (dwInfoLevel
& HTTP_QUERY_FLAG_COALESCE
)
1481 if (*lpdwIndex
>= lphttpHdr
->wCount
)
1483 INTERNET_SetLastError(ERROR_HTTP_HEADER_NOT_FOUND
);
1487 /* Copy strncpyW(lpBuffer, lphttpHdr[*lpdwIndex], len); */
1491 else if (lphttpHdr
->lpszValue
)
1493 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
1495 if (len
> *lpdwBufferLength
)
1497 *lpdwBufferLength
= len
;
1498 INTERNET_SetLastError(ERROR_INSUFFICIENT_BUFFER
);
1502 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
1503 *lpdwBufferLength
= len
- sizeof(WCHAR
);
1506 TRACE(" returning string : '%s'\n", debugstr_w(lpBuffer
));
1511 /***********************************************************************
1512 * HttpQueryInfoW (WININET.@)
1514 * Queries for information about an HTTP request
1521 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
1522 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1524 BOOL bSuccess
= FALSE
;
1525 LPWININETHTTPREQW lpwhr
;
1527 if (TRACE_ON(wininet
)) {
1528 #define FE(x) { x, #x }
1529 static const wininet_flag_info query_flags
[] = {
1530 FE(HTTP_QUERY_MIME_VERSION
),
1531 FE(HTTP_QUERY_CONTENT_TYPE
),
1532 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
1533 FE(HTTP_QUERY_CONTENT_ID
),
1534 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
1535 FE(HTTP_QUERY_CONTENT_LENGTH
),
1536 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
1537 FE(HTTP_QUERY_ALLOW
),
1538 FE(HTTP_QUERY_PUBLIC
),
1539 FE(HTTP_QUERY_DATE
),
1540 FE(HTTP_QUERY_EXPIRES
),
1541 FE(HTTP_QUERY_LAST_MODIFIED
),
1542 FE(HTTP_QUERY_MESSAGE_ID
),
1544 FE(HTTP_QUERY_DERIVED_FROM
),
1545 FE(HTTP_QUERY_COST
),
1546 FE(HTTP_QUERY_LINK
),
1547 FE(HTTP_QUERY_PRAGMA
),
1548 FE(HTTP_QUERY_VERSION
),
1549 FE(HTTP_QUERY_STATUS_CODE
),
1550 FE(HTTP_QUERY_STATUS_TEXT
),
1551 FE(HTTP_QUERY_RAW_HEADERS
),
1552 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
1553 FE(HTTP_QUERY_CONNECTION
),
1554 FE(HTTP_QUERY_ACCEPT
),
1555 FE(HTTP_QUERY_ACCEPT_CHARSET
),
1556 FE(HTTP_QUERY_ACCEPT_ENCODING
),
1557 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
1558 FE(HTTP_QUERY_AUTHORIZATION
),
1559 FE(HTTP_QUERY_CONTENT_ENCODING
),
1560 FE(HTTP_QUERY_FORWARDED
),
1561 FE(HTTP_QUERY_FROM
),
1562 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
1563 FE(HTTP_QUERY_LOCATION
),
1564 FE(HTTP_QUERY_ORIG_URI
),
1565 FE(HTTP_QUERY_REFERER
),
1566 FE(HTTP_QUERY_RETRY_AFTER
),
1567 FE(HTTP_QUERY_SERVER
),
1568 FE(HTTP_QUERY_TITLE
),
1569 FE(HTTP_QUERY_USER_AGENT
),
1570 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
1571 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
1572 FE(HTTP_QUERY_ACCEPT_RANGES
),
1573 FE(HTTP_QUERY_SET_COOKIE
),
1574 FE(HTTP_QUERY_COOKIE
),
1575 FE(HTTP_QUERY_REQUEST_METHOD
),
1576 FE(HTTP_QUERY_REFRESH
),
1577 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
1579 FE(HTTP_QUERY_CACHE_CONTROL
),
1580 FE(HTTP_QUERY_CONTENT_BASE
),
1581 FE(HTTP_QUERY_CONTENT_LOCATION
),
1582 FE(HTTP_QUERY_CONTENT_MD5
),
1583 FE(HTTP_QUERY_CONTENT_RANGE
),
1584 FE(HTTP_QUERY_ETAG
),
1585 FE(HTTP_QUERY_HOST
),
1586 FE(HTTP_QUERY_IF_MATCH
),
1587 FE(HTTP_QUERY_IF_NONE_MATCH
),
1588 FE(HTTP_QUERY_IF_RANGE
),
1589 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
1590 FE(HTTP_QUERY_MAX_FORWARDS
),
1591 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
1592 FE(HTTP_QUERY_RANGE
),
1593 FE(HTTP_QUERY_TRANSFER_ENCODING
),
1594 FE(HTTP_QUERY_UPGRADE
),
1595 FE(HTTP_QUERY_VARY
),
1597 FE(HTTP_QUERY_WARNING
),
1598 FE(HTTP_QUERY_CUSTOM
)
1600 static const wininet_flag_info modifier_flags
[] = {
1601 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
1602 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
1603 FE(HTTP_QUERY_FLAG_NUMBER
),
1604 FE(HTTP_QUERY_FLAG_COALESCE
)
1607 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
1608 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
1611 TRACE("(%p, 0x%08lx)--> %ld\n", hHttpRequest
, dwInfoLevel
, dwInfoLevel
);
1612 TRACE(" Attribute:");
1613 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
1614 if (query_flags
[i
].val
== info
) {
1615 TRACE(" %s", query_flags
[i
].name
);
1619 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
1620 TRACE(" Unknown (%08lx)", info
);
1623 TRACE(" Modifier:");
1624 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
1625 if (modifier_flags
[i
].val
& info_mod
) {
1626 TRACE(" %s", modifier_flags
[i
].name
);
1627 info_mod
&= ~ modifier_flags
[i
].val
;
1632 TRACE(" Unknown (%08lx)", info_mod
);
1637 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
1638 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1640 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1644 bSuccess
= HTTP_HttpQueryInfoW( lpwhr
, dwInfoLevel
,
1645 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
1649 WININET_Release( &lpwhr
->hdr
);
1651 TRACE("%d <--\n", bSuccess
);
1655 /***********************************************************************
1656 * HttpQueryInfoA (WININET.@)
1658 * Queries for information about an HTTP request
1665 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
1666 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
1672 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
1673 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
1675 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
1676 lpdwBufferLength
, lpdwIndex
);
1679 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
1680 bufferW
= HeapAlloc( GetProcessHeap(), 0, len
);
1681 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
1682 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
1683 MultiByteToWideChar(CP_ACP
,0,lpBuffer
,-1,bufferW
,len
);
1684 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
1688 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
1689 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
1690 *lpdwBufferLength
= len
- 1;
1692 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
1695 /* since the strings being returned from HttpQueryInfoW should be
1696 * only ASCII characters, it is reasonable to assume that all of
1697 * the Unicode characters can be reduced to a single byte */
1698 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
1700 HeapFree(GetProcessHeap(), 0, bufferW
);
1705 /***********************************************************************
1706 * HttpSendRequestExA (WININET.@)
1708 * Sends the specified request to the HTTP server and allows chunked
1713 * Failure: FALSE, call GetLastError() for more information.
1715 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
1716 LPINTERNET_BUFFERSA lpBuffersIn
,
1717 LPINTERNET_BUFFERSA lpBuffersOut
,
1718 DWORD dwFlags
, DWORD dwContext
)
1720 INTERNET_BUFFERSW BuffersInW
;
1724 TRACE("(%p, %p, %p, %08lx, %08lx): stub\n", hRequest
, lpBuffersIn
,
1725 lpBuffersOut
, dwFlags
, dwContext
);
1729 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
1730 if (lpBuffersIn
->lpcszHeader
)
1732 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
1733 lpBuffersIn
->dwHeadersLength
,0,0);
1734 BuffersInW
.lpcszHeader
= HeapAlloc(GetProcessHeap(),0,headerlen
*
1736 if (!BuffersInW
.lpcszHeader
)
1738 SetLastError(ERROR_OUTOFMEMORY
);
1741 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
1742 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
1743 (LPWSTR
)BuffersInW
.lpcszHeader
, headerlen
);
1746 BuffersInW
.lpcszHeader
= NULL
;
1747 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
1748 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
1749 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
1750 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
1751 BuffersInW
.Next
= NULL
;
1754 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
1757 HeapFree(GetProcessHeap(),0,(LPVOID
)BuffersInW
.lpcszHeader
);
1762 /***********************************************************************
1763 * HttpSendRequestExW (WININET.@)
1765 * Sends the specified request to the HTTP server and allows chunked
1770 * Failure: FALSE, call GetLastError() for more information.
1772 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
1773 LPINTERNET_BUFFERSW lpBuffersIn
,
1774 LPINTERNET_BUFFERSW lpBuffersOut
,
1775 DWORD dwFlags
, DWORD dwContext
)
1778 LPWININETHTTPREQW lpwhr
;
1779 LPWININETHTTPSESSIONW lpwhs
;
1780 LPWININETAPPINFOW hIC
;
1782 TRACE("(%p, %p, %p, %08lx, %08lx)\n", hRequest
, lpBuffersIn
,
1783 lpBuffersOut
, dwFlags
, dwContext
);
1785 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hRequest
);
1787 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1789 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1793 lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
1794 assert(lpwhs
->hdr
.htype
== WH_HHTTPSESSION
);
1795 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
1796 assert(hIC
->hdr
.htype
== WH_HINIT
);
1798 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1800 WORKREQUEST workRequest
;
1801 struct WORKREQ_HTTPSENDREQUESTW
*req
;
1803 workRequest
.asyncall
= HTTPSENDREQUESTW
;
1804 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
1805 req
= &workRequest
.u
.HttpSendRequestW
;
1808 if (lpBuffersIn
->lpcszHeader
)
1809 /* FIXME: this should use dwHeadersLength or may not be necessary at all */
1810 req
->lpszHeader
= WININET_strdupW(lpBuffersIn
->lpcszHeader
);
1812 req
->lpszHeader
= NULL
;
1813 req
->dwHeaderLength
= lpBuffersIn
->dwHeadersLength
;
1814 req
->lpOptional
= lpBuffersIn
->lpvBuffer
;
1815 req
->dwOptionalLength
= lpBuffersIn
->dwBufferLength
;
1816 req
->dwContentLength
= lpBuffersIn
->dwBufferTotal
;
1820 req
->lpszHeader
= NULL
;
1821 req
->dwHeaderLength
= 0;
1822 req
->lpOptional
= NULL
;
1823 req
->dwOptionalLength
= 0;
1824 req
->dwContentLength
= 0;
1827 req
->bEndRequest
= FALSE
;
1829 INTERNET_AsyncCall(&workRequest
);
1831 * This is from windows.
1833 SetLastError(ERROR_IO_PENDING
);
1838 ret
= HTTP_HttpSendRequestW(lpwhr
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
1839 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
1840 lpBuffersIn
->dwBufferTotal
, FALSE
);
1843 WININET_Release(&lpwhr
->hdr
);
1848 /***********************************************************************
1849 * HttpSendRequestW (WININET.@)
1851 * Sends the specified request to the HTTP server
1858 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
1859 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
1861 LPWININETHTTPREQW lpwhr
;
1862 LPWININETHTTPSESSIONW lpwhs
= NULL
;
1863 LPWININETAPPINFOW hIC
= NULL
;
1866 TRACE("%p, %p (%s), %li, %p, %li)\n", hHttpRequest
,
1867 lpszHeaders
, debugstr_w(lpszHeaders
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
1869 lpwhr
= (LPWININETHTTPREQW
) WININET_GetObject( hHttpRequest
);
1870 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
1872 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1877 lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
1878 if (NULL
== lpwhs
|| lpwhs
->hdr
.htype
!= WH_HHTTPSESSION
)
1880 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1885 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
1886 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
1888 INTERNET_SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
1893 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
1895 WORKREQUEST workRequest
;
1896 struct WORKREQ_HTTPSENDREQUESTW
*req
;
1898 workRequest
.asyncall
= HTTPSENDREQUESTW
;
1899 workRequest
.hdr
= WININET_AddRef( &lpwhr
->hdr
);
1900 req
= &workRequest
.u
.HttpSendRequestW
;
1902 req
->lpszHeader
= WININET_strdupW(lpszHeaders
);
1904 req
->lpszHeader
= 0;
1905 req
->dwHeaderLength
= dwHeaderLength
;
1906 req
->lpOptional
= lpOptional
;
1907 req
->dwOptionalLength
= dwOptionalLength
;
1908 req
->dwContentLength
= dwOptionalLength
;
1909 req
->bEndRequest
= TRUE
;
1911 INTERNET_AsyncCall(&workRequest
);
1913 * This is from windows.
1915 SetLastError(ERROR_IO_PENDING
);
1920 r
= HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
,
1921 dwHeaderLength
, lpOptional
, dwOptionalLength
,
1922 dwOptionalLength
, TRUE
);
1926 WININET_Release( &lpwhr
->hdr
);
1930 /***********************************************************************
1931 * HttpSendRequestA (WININET.@)
1933 * Sends the specified request to the HTTP server
1940 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
1941 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
1944 LPWSTR szHeaders
=NULL
;
1945 DWORD nLen
=dwHeaderLength
;
1946 if(lpszHeaders
!=NULL
)
1948 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
1949 szHeaders
=HeapAlloc(GetProcessHeap(),0,nLen
*sizeof(WCHAR
));
1950 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
1952 result
=HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
1953 HeapFree(GetProcessHeap(),0,szHeaders
);
1957 /***********************************************************************
1958 * HTTP_HandleRedirect (internal)
1960 static BOOL
HTTP_HandleRedirect(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszUrl
, LPCWSTR lpszHeaders
,
1961 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
1962 DWORD dwContentLength
)
1964 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
1965 LPWININETAPPINFOW hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
1971 /* if it's an absolute path, keep the same session info */
1972 lstrcpynW(path
, lpszUrl
, 2048);
1974 else if (NULL
!= hIC
->lpszProxy
&& hIC
->lpszProxy
[0] != 0)
1976 TRACE("Redirect through proxy\n");
1977 lstrcpynW(path
, lpszUrl
, 2048);
1981 URL_COMPONENTSW urlComponents
;
1982 WCHAR protocol
[32], hostName
[MAXHOSTNAME
], userName
[1024];
1983 static const WCHAR szHttp
[] = {'h','t','t','p',0};
1984 static const WCHAR szHttps
[] = {'h','t','t','p','s',0};
1985 DWORD url_length
= 0;
1987 LPWSTR combined_url
;
1989 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
1990 urlComponents
.lpszScheme
= (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? (LPWSTR
)szHttps
: (LPWSTR
)szHttp
;
1991 urlComponents
.dwSchemeLength
= 0;
1992 urlComponents
.lpszHostName
= lpwhs
->lpszHostName
;
1993 urlComponents
.dwHostNameLength
= 0;
1994 urlComponents
.nPort
= lpwhs
->nHostPort
;
1995 urlComponents
.lpszUserName
= lpwhs
->lpszUserName
;
1996 urlComponents
.dwUserNameLength
= 0;
1997 urlComponents
.lpszPassword
= NULL
;
1998 urlComponents
.dwPasswordLength
= 0;
1999 urlComponents
.lpszUrlPath
= lpwhr
->lpszPath
;
2000 urlComponents
.dwUrlPathLength
= 0;
2001 urlComponents
.lpszExtraInfo
= NULL
;
2002 urlComponents
.dwExtraInfoLength
= 0;
2004 if (!InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
) &&
2005 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
2008 url_length
++; /* for nul terminating character */
2009 orig_url
= HeapAlloc(GetProcessHeap(), 0, url_length
* sizeof(WCHAR
));
2011 if (!InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
))
2013 HeapFree(GetProcessHeap(), 0, orig_url
);
2018 if (!InternetCombineUrlW(orig_url
, lpszUrl
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
) &&
2019 (GetLastError() != ERROR_INSUFFICIENT_BUFFER
))
2021 HeapFree(GetProcessHeap(), 0, orig_url
);
2024 combined_url
= HeapAlloc(GetProcessHeap(), 0, url_length
* sizeof(WCHAR
));
2026 if (!InternetCombineUrlW(orig_url
, lpszUrl
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
))
2028 HeapFree(GetProcessHeap(), 0, orig_url
);
2029 HeapFree(GetProcessHeap(), 0, combined_url
);
2032 HeapFree(GetProcessHeap(), 0, orig_url
);
2038 urlComponents
.dwStructSize
= sizeof(URL_COMPONENTSW
);
2039 urlComponents
.lpszScheme
= protocol
;
2040 urlComponents
.dwSchemeLength
= 32;
2041 urlComponents
.lpszHostName
= hostName
;
2042 urlComponents
.dwHostNameLength
= MAXHOSTNAME
;
2043 urlComponents
.lpszUserName
= userName
;
2044 urlComponents
.dwUserNameLength
= 1024;
2045 urlComponents
.lpszPassword
= NULL
;
2046 urlComponents
.dwPasswordLength
= 0;
2047 urlComponents
.lpszUrlPath
= path
;
2048 urlComponents
.dwUrlPathLength
= 2048;
2049 urlComponents
.lpszExtraInfo
= NULL
;
2050 urlComponents
.dwExtraInfoLength
= 0;
2051 if(!InternetCrackUrlW(combined_url
, strlenW(combined_url
), 0, &urlComponents
))
2053 HeapFree(GetProcessHeap(), 0, combined_url
);
2056 HeapFree(GetProcessHeap(), 0, combined_url
);
2058 if (!strncmpW(szHttp
, urlComponents
.lpszScheme
, strlenW(szHttp
)) &&
2059 (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
2061 TRACE("redirect from secure page to non-secure page\n");
2062 /* FIXME: warn about from secure redirect to non-secure page */
2063 lpwhr
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
2065 if (!strncmpW(szHttps
, urlComponents
.lpszScheme
, strlenW(szHttps
)) &&
2066 !(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
2068 TRACE("redirect from non-secure page to secure page\n");
2069 /* FIXME: notify about redirect to secure page */
2070 lpwhr
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
2073 if (urlComponents
.nPort
== INTERNET_INVALID_PORT_NUMBER
)
2075 if (lstrlenW(protocol
)>4) /*https*/
2076 urlComponents
.nPort
= INTERNET_DEFAULT_HTTPS_PORT
;
2078 urlComponents
.nPort
= INTERNET_DEFAULT_HTTP_PORT
;
2083 * This upsets redirects to binary files on sourceforge.net
2084 * and gives an html page instead of the target file
2085 * Examination of the HTTP request sent by native wininet.dll
2086 * reveals that it doesn't send a referrer in that case.
2087 * Maybe there's a flag that enables this, or maybe a referrer
2088 * shouldn't be added in case of a redirect.
2091 /* consider the current host as the referrer */
2092 if (NULL
!= lpwhs
->lpszServerName
&& strlenW(lpwhs
->lpszServerName
))
2093 HTTP_ProcessHeader(lpwhr
, HTTP_REFERER
, lpwhs
->lpszServerName
,
2094 HTTP_ADDHDR_FLAG_REQ
|HTTP_ADDREQ_FLAG_REPLACE
|
2095 HTTP_ADDHDR_FLAG_ADD_IF_NEW
);
2098 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
2099 lpwhs
->lpszServerName
= WININET_strdupW(hostName
);
2100 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
2101 if (urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
&&
2102 urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
)
2105 static WCHAR fmt
[] = {'%','s',':','%','i',0};
2106 len
= lstrlenW(hostName
);
2107 len
+= 7; /* 5 for strlen("65535") + 1 for ":" + 1 for '\0' */
2108 lpwhs
->lpszHostName
= HeapAlloc(GetProcessHeap(), 0, len
*sizeof(WCHAR
));
2109 sprintfW(lpwhs
->lpszHostName
, fmt
, hostName
, urlComponents
.nPort
);
2112 lpwhs
->lpszHostName
= WININET_strdupW(hostName
);
2114 HTTP_ProcessHeader(lpwhr
, szHost
, lpwhs
->lpszHostName
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
2117 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
2118 lpwhs
->lpszUserName
= NULL
;
2120 lpwhs
->lpszUserName
= WININET_strdupW(userName
);
2121 lpwhs
->nServerPort
= urlComponents
.nPort
;
2123 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2124 INTERNET_STATUS_RESOLVING_NAME
,
2125 lpwhs
->lpszServerName
,
2126 strlenW(lpwhs
->lpszServerName
)+1);
2128 if (!GetAddress(lpwhs
->lpszServerName
, lpwhs
->nServerPort
,
2129 &lpwhs
->socketAddress
))
2131 INTERNET_SetLastError(ERROR_INTERNET_NAME_NOT_RESOLVED
);
2135 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
2136 szaddr
, sizeof(szaddr
));
2137 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2138 INTERNET_STATUS_NAME_RESOLVED
,
2139 szaddr
, strlen(szaddr
)+1);
2141 NETCON_close(&lpwhr
->netConnection
);
2143 if (!NETCON_init(&lpwhr
->netConnection
,lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
2147 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
2148 lpwhr
->lpszPath
=NULL
;
2154 rc
= UrlEscapeW(path
, NULL
, &needed
, URL_ESCAPE_SPACES_ONLY
);
2155 if (rc
!= E_POINTER
)
2156 needed
= strlenW(path
)+1;
2157 lpwhr
->lpszPath
= HeapAlloc(GetProcessHeap(), 0, needed
*sizeof(WCHAR
));
2158 rc
= UrlEscapeW(path
, lpwhr
->lpszPath
, &needed
,
2159 URL_ESCAPE_SPACES_ONLY
);
2162 ERR("Unable to escape string!(%s) (%ld)\n",debugstr_w(path
),rc
);
2163 strcpyW(lpwhr
->lpszPath
,path
);
2167 return HTTP_HttpSendRequestW(lpwhr
, lpszHeaders
, dwHeaderLength
, lpOptional
,
2168 dwOptionalLength
, dwContentLength
, TRUE
);
2171 /***********************************************************************
2172 * HTTP_build_req (internal)
2174 * concatenate all the strings in the request together
2176 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
2181 for( t
= list
; *t
; t
++ )
2182 len
+= strlenW( *t
);
2185 str
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
2188 for( t
= list
; *t
; t
++ )
2194 static BOOL
HTTP_SecureProxyConnect(LPWININETHTTPREQW lpwhr
)
2197 LPWSTR requestString
;
2203 static const WCHAR szConnect
[] = {'C','O','N','N','E','C','T',0};
2204 static const WCHAR szFormat
[] = {'%','s',':','%','d',0};
2205 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
)lpwhr
->hdr
.lpwhparent
;
2209 lpszPath
= HeapAlloc( GetProcessHeap(), 0, (lstrlenW( lpwhs
->lpszHostName
) + 13)*sizeof(WCHAR
) );
2210 sprintfW( lpszPath
, szFormat
, lpwhs
->lpszHostName
, lpwhs
->nHostPort
);
2211 requestString
= HTTP_BuildHeaderRequestString( lpwhr
, szConnect
, lpszPath
, FALSE
);
2212 HeapFree( GetProcessHeap(), 0, lpszPath
);
2214 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2215 NULL
, 0, NULL
, NULL
);
2216 len
--; /* the nul terminator isn't needed */
2217 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
);
2218 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2219 ascii_req
, len
, NULL
, NULL
);
2220 HeapFree( GetProcessHeap(), 0, requestString
);
2222 TRACE("full request -> %s\n", debugstr_an( ascii_req
, len
) );
2224 ret
= NETCON_send( &lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
2225 HeapFree( GetProcessHeap(), 0, ascii_req
);
2226 if (!ret
|| cnt
< 0)
2229 responseLen
= HTTP_GetResponseHeaders( lpwhr
);
2236 /***********************************************************************
2237 * HTTP_HttpSendRequestW (internal)
2239 * Sends the specified request to the HTTP server
2246 BOOL WINAPI
HTTP_HttpSendRequestW(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszHeaders
,
2247 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
2248 DWORD dwContentLength
, BOOL bEndRequest
)
2251 BOOL bSuccess
= FALSE
;
2252 LPWSTR requestString
= NULL
;
2254 BOOL loop_next
= FALSE
;
2255 INTERNET_ASYNC_RESULT iar
;
2258 TRACE("--> %p\n", lpwhr
);
2260 assert(lpwhr
->hdr
.htype
== WH_HHTTPREQ
);
2262 /* Clear any error information */
2263 INTERNET_SetLastError(0);
2265 HTTP_FixVerb(lpwhr
);
2267 /* if we are using optional stuff, we must add the fixed header of that option length */
2268 if (dwContentLength
> 0)
2270 static const WCHAR szContentLength
[] = {
2271 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','l','i','\r','\n',0};
2272 WCHAR contentLengthStr
[sizeof szContentLength
/2 /* includes \n\r */ + 20 /* int */ ];
2273 sprintfW(contentLengthStr
, szContentLength
, dwContentLength
);
2274 HTTP_HttpAddRequestHeadersW(lpwhr
, contentLengthStr
, -1L, HTTP_ADDREQ_FLAG_ADD
);
2277 Host
= HTTP_GetHeader(lpwhr
,szHost
);
2283 TRACE("Going to url %s %s\n", debugstr_w(Host
->lpszValue
), debugstr_w(lpwhr
->lpszPath
));
2288 /* add the headers the caller supplied */
2289 if( lpszHeaders
&& dwHeaderLength
)
2291 HTTP_HttpAddRequestHeadersW(lpwhr
, lpszHeaders
, dwHeaderLength
,
2292 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
2295 /* if there's a proxy username and password, add it to the headers */
2296 HTTP_AddProxyInfo(lpwhr
);
2298 requestString
= HTTP_BuildHeaderRequestString(lpwhr
, lpwhr
->lpszVerb
, lpwhr
->lpszPath
, FALSE
);
2300 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
2302 /* Send the request and store the results */
2303 if (!HTTP_OpenConnection(lpwhr
))
2306 /* send the request as ASCII, tack on the optional data */
2308 dwOptionalLength
= 0;
2309 len
= WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2310 NULL
, 0, NULL
, NULL
);
2311 ascii_req
= HeapAlloc( GetProcessHeap(), 0, len
+ dwOptionalLength
);
2312 WideCharToMultiByte( CP_ACP
, 0, requestString
, -1,
2313 ascii_req
, len
, NULL
, NULL
);
2315 memcpy( &ascii_req
[len
-1], lpOptional
, dwOptionalLength
);
2316 len
= (len
+ dwOptionalLength
- 1);
2318 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
2320 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2321 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
2323 NETCON_send(&lpwhr
->netConnection
, ascii_req
, len
, 0, &cnt
);
2324 HeapFree( GetProcessHeap(), 0, ascii_req
);
2326 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2327 INTERNET_STATUS_REQUEST_SENT
,
2328 &len
, sizeof(DWORD
));
2332 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2333 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
2338 responseLen
= HTTP_GetResponseHeaders(lpwhr
);
2342 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2343 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
2346 HTTP_ProcessHeaders(lpwhr
);
2355 HeapFree(GetProcessHeap(), 0, requestString
);
2357 /* TODO: send notification for P3P header */
2359 if(!(lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && bSuccess
&& bEndRequest
)
2361 DWORD dwCode
,dwCodeLength
=sizeof(DWORD
),dwIndex
=0;
2362 if(HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_STATUS_CODE
,&dwCode
,&dwCodeLength
,&dwIndex
) &&
2363 (dwCode
==302 || dwCode
==301))
2365 WCHAR szNewLocation
[2048];
2366 DWORD dwBufferSize
=2048;
2368 if(HTTP_HttpQueryInfoW(lpwhr
,HTTP_QUERY_LOCATION
,szNewLocation
,&dwBufferSize
,&dwIndex
))
2370 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2371 INTERNET_STATUS_REDIRECT
, szNewLocation
,
2373 return HTTP_HandleRedirect(lpwhr
, szNewLocation
, lpszHeaders
,
2374 dwHeaderLength
, lpOptional
, dwOptionalLength
,
2381 iar
.dwResult
= (DWORD
)bSuccess
;
2382 iar
.dwError
= bSuccess
? ERROR_SUCCESS
: INTERNET_GetLastError();
2384 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2385 INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
2386 sizeof(INTERNET_ASYNC_RESULT
));
2393 /***********************************************************************
2394 * HTTP_Connect (internal)
2396 * Create http session handle
2399 * HINTERNET a session handle on success
2403 HINTERNET
HTTP_Connect(LPWININETAPPINFOW hIC
, LPCWSTR lpszServerName
,
2404 INTERNET_PORT nServerPort
, LPCWSTR lpszUserName
,
2405 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD dwContext
,
2406 DWORD dwInternalFlags
)
2408 BOOL bSuccess
= FALSE
;
2409 LPWININETHTTPSESSIONW lpwhs
= NULL
;
2410 HINTERNET handle
= NULL
;
2414 assert( hIC
->hdr
.htype
== WH_HINIT
);
2416 hIC
->hdr
.dwContext
= dwContext
;
2418 lpwhs
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WININETHTTPSESSIONW
));
2421 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2426 * According to my tests. The name is not resolved until a request is sent
2429 lpwhs
->hdr
.htype
= WH_HHTTPSESSION
;
2430 lpwhs
->hdr
.lpwhparent
= WININET_AddRef( &hIC
->hdr
);
2431 lpwhs
->hdr
.dwFlags
= dwFlags
;
2432 lpwhs
->hdr
.dwContext
= dwContext
;
2433 lpwhs
->hdr
.dwInternalFlags
= dwInternalFlags
;
2434 lpwhs
->hdr
.dwRefCount
= 1;
2435 lpwhs
->hdr
.destroy
= HTTP_CloseHTTPSessionHandle
;
2436 lpwhs
->hdr
.lpfnStatusCB
= hIC
->hdr
.lpfnStatusCB
;
2438 handle
= WININET_AllocHandle( &lpwhs
->hdr
);
2441 ERR("Failed to alloc handle\n");
2442 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2446 if(hIC
->lpszProxy
&& hIC
->dwAccessType
== INTERNET_OPEN_TYPE_PROXY
) {
2447 if(strchrW(hIC
->lpszProxy
, ' '))
2448 FIXME("Several proxies not implemented.\n");
2449 if(hIC
->lpszProxyBypass
)
2450 FIXME("Proxy bypass is ignored.\n");
2452 if (lpszServerName
&& lpszServerName
[0])
2454 lpwhs
->lpszServerName
= WININET_strdupW(lpszServerName
);
2455 lpwhs
->lpszHostName
= WININET_strdupW(lpszServerName
);
2457 if (lpszUserName
&& lpszUserName
[0])
2458 lpwhs
->lpszUserName
= WININET_strdupW(lpszUserName
);
2459 lpwhs
->nServerPort
= nServerPort
;
2460 lpwhs
->nHostPort
= nServerPort
;
2462 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
2463 if (!(lpwhs
->hdr
.dwInternalFlags
& INET_OPENURL
))
2465 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
2466 INTERNET_STATUS_HANDLE_CREATED
, &handle
,
2474 WININET_Release( &lpwhs
->hdr
);
2477 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
2481 TRACE("%p --> %p (%p)\n", hIC
, handle
, lpwhs
);
2486 /***********************************************************************
2487 * HTTP_OpenConnection (internal)
2489 * Connect to a web server
2496 static BOOL
HTTP_OpenConnection(LPWININETHTTPREQW lpwhr
)
2498 BOOL bSuccess
= FALSE
;
2499 LPWININETHTTPSESSIONW lpwhs
;
2500 LPWININETAPPINFOW hIC
= NULL
;
2506 if (NULL
== lpwhr
|| lpwhr
->hdr
.htype
!= WH_HHTTPREQ
)
2508 INTERNET_SetLastError(ERROR_INVALID_PARAMETER
);
2512 lpwhs
= (LPWININETHTTPSESSIONW
)lpwhr
->hdr
.lpwhparent
;
2514 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
2515 inet_ntop(lpwhs
->socketAddress
.sin_family
, &lpwhs
->socketAddress
.sin_addr
,
2516 szaddr
, sizeof(szaddr
));
2517 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2518 INTERNET_STATUS_CONNECTING_TO_SERVER
,
2522 if (!NETCON_create(&lpwhr
->netConnection
, lpwhs
->socketAddress
.sin_family
,
2525 WARN("Socket creation failed\n");
2529 if (!NETCON_connect(&lpwhr
->netConnection
, (struct sockaddr
*)&lpwhs
->socketAddress
,
2530 sizeof(lpwhs
->socketAddress
)))
2533 if (lpwhr
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)
2535 /* Note: we differ from Microsoft's WinINet here. they seem to have
2536 * a bug that causes no status callbacks to be sent when starting
2537 * a tunnel to a proxy server using the CONNECT verb. i believe our
2538 * behaviour to be more correct and to not cause any incompatibilities
2539 * because using a secure connection through a proxy server is a rare
2540 * case that would be hard for anyone to depend on */
2541 if (hIC
->lpszProxy
&& !HTTP_SecureProxyConnect(lpwhr
))
2544 if (!NETCON_secure_connect(&lpwhr
->netConnection
, lpwhs
->lpszHostName
))
2546 WARN("Couldn't connect securely to host\n");
2551 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2552 INTERNET_STATUS_CONNECTED_TO_SERVER
,
2553 szaddr
, strlen(szaddr
)+1);
2558 TRACE("%d <--\n", bSuccess
);
2563 /***********************************************************************
2564 * HTTP_clear_response_headers (internal)
2566 * clear out any old response headers
2568 static void HTTP_clear_response_headers( LPWININETHTTPREQW lpwhr
)
2572 for( i
=0; i
<lpwhr
->nCustHeaders
; i
++)
2574 if( !lpwhr
->pCustHeaders
[i
].lpszField
)
2576 if( !lpwhr
->pCustHeaders
[i
].lpszValue
)
2578 if ( lpwhr
->pCustHeaders
[i
].wFlags
& HDR_ISREQUEST
)
2580 HTTP_DeleteCustomHeader( lpwhr
, i
);
2585 /***********************************************************************
2586 * HTTP_GetResponseHeaders (internal)
2588 * Read server response
2595 static INT
HTTP_GetResponseHeaders(LPWININETHTTPREQW lpwhr
)
2598 WCHAR buffer
[MAX_REPLY_LEN
];
2599 DWORD buflen
= MAX_REPLY_LEN
;
2600 BOOL bSuccess
= FALSE
;
2602 static const WCHAR szCrLf
[] = {'\r','\n',0};
2603 char bufferA
[MAX_REPLY_LEN
];
2604 LPWSTR status_code
, status_text
;
2605 DWORD cchMaxRawHeaders
= 1024;
2606 LPWSTR lpszRawHeaders
= HeapAlloc(GetProcessHeap(), 0, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
2607 DWORD cchRawHeaders
= 0;
2611 /* clear old response headers (eg. from a redirect response) */
2612 HTTP_clear_response_headers( lpwhr
);
2614 if (!NETCON_connected(&lpwhr
->netConnection
))
2618 * HACK peek at the buffer
2621 /* This is Wine code, we don't support MSG_PEEK yet so we have to do it
2623 NETCON_recv(&lpwhr
->netConnection
, buffer
, buflen
, MSG_PEEK
, &rc
);
2627 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
2629 buflen
= MAX_REPLY_LEN
;
2630 memset(buffer
, 0, MAX_REPLY_LEN
);
2631 if (!NETCON_getNextLine(&lpwhr
->netConnection
, bufferA
, &buflen
))
2636 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
2638 /* regenerate raw headers */
2639 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
2641 cchMaxRawHeaders
*= 2;
2642 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
2644 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
2645 cchRawHeaders
+= (buflen
-1);
2646 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
2647 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
2648 lpszRawHeaders
[cchRawHeaders
] = '\0';
2650 /* split the version from the status code */
2651 status_code
= strchrW( buffer
, ' ' );
2656 /* split the status code from the status text */
2657 status_text
= strchrW( status_code
, ' ' );
2662 TRACE("version [%s] status code [%s] status text [%s]\n",
2663 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
2665 HTTP_ProcessHeader(lpwhr
, szStatus
, status_code
,
2666 HTTP_ADDHDR_FLAG_REPLACE
);
2668 HeapFree(GetProcessHeap(),0,lpwhr
->lpszVersion
);
2669 HeapFree(GetProcessHeap(),0,lpwhr
->lpszStatusText
);
2671 lpwhr
->lpszVersion
= WININET_strdupW(buffer
);
2672 lpwhr
->lpszStatusText
= WININET_strdupW(status_text
);
2674 /* Parse each response line */
2677 buflen
= MAX_REPLY_LEN
;
2678 if (NETCON_getNextLine(&lpwhr
->netConnection
, bufferA
, &buflen
))
2680 LPWSTR
* pFieldAndValue
;
2685 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
2686 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
2688 while (cchRawHeaders
+ buflen
+ strlenW(szCrLf
) > cchMaxRawHeaders
)
2690 cchMaxRawHeaders
*= 2;
2691 lpszRawHeaders
= HeapReAlloc(GetProcessHeap(), 0, lpszRawHeaders
, (cchMaxRawHeaders
+1)*sizeof(WCHAR
));
2693 memcpy(lpszRawHeaders
+cchRawHeaders
, buffer
, (buflen
-1)*sizeof(WCHAR
));
2694 cchRawHeaders
+= (buflen
-1);
2695 memcpy(lpszRawHeaders
+cchRawHeaders
, szCrLf
, sizeof(szCrLf
));
2696 cchRawHeaders
+= sizeof(szCrLf
)/sizeof(szCrLf
[0])-1;
2697 lpszRawHeaders
[cchRawHeaders
] = '\0';
2699 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
2700 if (!pFieldAndValue
)
2703 HTTP_ProcessHeader(lpwhr
, pFieldAndValue
[0], pFieldAndValue
[1],
2704 HTTP_ADDREQ_FLAG_ADD
);
2706 HTTP_FreeTokens(pFieldAndValue
);
2716 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
2717 lpwhr
->lpszRawHeaders
= lpszRawHeaders
;
2718 TRACE("raw headers: %s\n", debugstr_w(lpszRawHeaders
));
2731 static void strip_spaces(LPWSTR start
)
2736 while (*str
== ' ' && *str
!= '\0')
2740 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
2742 end
= start
+ strlenW(start
) - 1;
2743 while (end
>= start
&& *end
== ' ')
2751 /***********************************************************************
2752 * HTTP_InterpretHttpHeader (internal)
2754 * Parse server response
2758 * Pointer to array of field, value, NULL on success.
2761 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
2763 LPWSTR
* pTokenPair
;
2767 pTokenPair
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*pTokenPair
)*3);
2769 pszColon
= strchrW(buffer
, ':');
2770 /* must have two tokens */
2773 HTTP_FreeTokens(pTokenPair
);
2775 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
2779 pTokenPair
[0] = HeapAlloc(GetProcessHeap(), 0, (pszColon
- buffer
+ 1) * sizeof(WCHAR
));
2782 HTTP_FreeTokens(pTokenPair
);
2785 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
2786 pTokenPair
[0][pszColon
- buffer
] = '\0';
2790 len
= strlenW(pszColon
);
2791 pTokenPair
[1] = HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
2794 HTTP_FreeTokens(pTokenPair
);
2797 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
2799 strip_spaces(pTokenPair
[0]);
2800 strip_spaces(pTokenPair
[1]);
2802 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
2806 /***********************************************************************
2807 * HTTP_ProcessHeader (internal)
2809 * Stuff header into header tables according to <dwModifier>
2813 #define COALESCEFLASG (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
2815 static BOOL
HTTP_ProcessHeader(LPWININETHTTPREQW lpwhr
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
2817 LPHTTPHEADERW lphttpHdr
= NULL
;
2818 BOOL bSuccess
= FALSE
;
2820 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
2821 BOOL request_only
= dwModifier
& HTTP_ADDHDR_FLAG_REQ
;
2823 TRACE("--> %s: %s - 0x%08lx\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
2825 /* Don't let applications add Connection header to request */
2826 if (strcmpW(szConnection
,field
)==0 && (dwModifier
& HTTP_ADDHDR_FLAG_REQ
))
2831 /* REPLACE wins out over ADD */
2832 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
2833 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
2835 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
2838 index
= HTTP_GetCustomHeaderIndex(lpwhr
, field
, 0, request_only
);
2842 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
2846 lphttpHdr
= &lpwhr
->pCustHeaders
[index
];
2852 hdr
.lpszField
= (LPWSTR
)field
;
2853 hdr
.lpszValue
= (LPWSTR
)value
;
2854 hdr
.wFlags
= hdr
.wCount
= 0;
2856 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
2857 hdr
.wFlags
|= HDR_ISREQUEST
;
2859 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
2862 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
2863 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
2865 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
2867 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
2869 HTTP_DeleteCustomHeader( lpwhr
, index
);
2875 hdr
.lpszField
= (LPWSTR
)field
;
2876 hdr
.lpszValue
= (LPWSTR
)value
;
2877 hdr
.wFlags
= hdr
.wCount
= 0;
2879 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
2880 hdr
.wFlags
|= HDR_ISREQUEST
;
2882 return HTTP_InsertCustomHeader(lpwhr
, &hdr
);
2887 else if (dwModifier
& COALESCEFLASG
)
2892 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
2893 INT valuelen
= strlenW(value
);
2895 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
2898 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
2900 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
2903 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
2906 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
2908 lpsztmp
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
2911 lphttpHdr
->lpszValue
= lpsztmp
;
2912 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
2915 lphttpHdr
->lpszValue
[origlen
] = ch
;
2917 lphttpHdr
->lpszValue
[origlen
] = ' ';
2921 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
2922 lphttpHdr
->lpszValue
[len
] = '\0';
2927 WARN("HeapReAlloc (%d bytes) failed\n",len
+1);
2928 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
2931 TRACE("<-- %d\n",bSuccess
);
2936 /***********************************************************************
2937 * HTTP_CloseConnection (internal)
2939 * Close socket connection
2942 static VOID
HTTP_CloseConnection(LPWININETHTTPREQW lpwhr
)
2944 LPWININETHTTPSESSIONW lpwhs
= NULL
;
2945 LPWININETAPPINFOW hIC
= NULL
;
2947 TRACE("%p\n",lpwhr
);
2949 lpwhs
= (LPWININETHTTPSESSIONW
) lpwhr
->hdr
.lpwhparent
;
2950 hIC
= (LPWININETAPPINFOW
) lpwhs
->hdr
.lpwhparent
;
2952 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2953 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
2955 if (NETCON_connected(&lpwhr
->netConnection
))
2957 NETCON_close(&lpwhr
->netConnection
);
2960 INTERNET_SendCallback(&lpwhr
->hdr
, lpwhr
->hdr
.dwContext
,
2961 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
2965 /***********************************************************************
2966 * HTTP_CloseHTTPRequestHandle (internal)
2968 * Deallocate request handle
2971 static void HTTP_CloseHTTPRequestHandle(LPWININETHANDLEHEADER hdr
)
2974 LPWININETHTTPREQW lpwhr
= (LPWININETHTTPREQW
) hdr
;
2978 if (NETCON_connected(&lpwhr
->netConnection
))
2979 HTTP_CloseConnection(lpwhr
);
2981 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszPath
);
2982 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVerb
);
2983 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszRawHeaders
);
2984 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszVersion
);
2985 HeapFree(GetProcessHeap(), 0, lpwhr
->lpszStatusText
);
2987 for (i
= 0; i
< lpwhr
->nCustHeaders
; i
++)
2989 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszField
);
2990 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
[i
].lpszValue
);
2993 HeapFree(GetProcessHeap(), 0, lpwhr
->pCustHeaders
);
2994 HeapFree(GetProcessHeap(), 0, lpwhr
);
2998 /***********************************************************************
2999 * HTTP_CloseHTTPSessionHandle (internal)
3001 * Deallocate session handle
3004 static void HTTP_CloseHTTPSessionHandle(LPWININETHANDLEHEADER hdr
)
3006 LPWININETHTTPSESSIONW lpwhs
= (LPWININETHTTPSESSIONW
) hdr
;
3008 TRACE("%p\n", lpwhs
);
3010 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszHostName
);
3011 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszServerName
);
3012 HeapFree(GetProcessHeap(), 0, lpwhs
->lpszUserName
);
3013 HeapFree(GetProcessHeap(), 0, lpwhs
);
3017 /***********************************************************************
3018 * HTTP_GetCustomHeaderIndex (internal)
3020 * Return index of custom header from header array
3023 static INT
HTTP_GetCustomHeaderIndex(LPWININETHTTPREQW lpwhr
, LPCWSTR lpszField
,int requested_index
, BOOL request_only
)
3027 TRACE("%s\n", debugstr_w(lpszField
));
3029 for (index
= 0; index
< lpwhr
->nCustHeaders
; index
++)
3031 if (!strcmpiW(lpwhr
->pCustHeaders
[index
].lpszField
, lpszField
))
3033 if ((request_only
&&
3034 !(lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
))||
3036 (lpwhr
->pCustHeaders
[index
].wFlags
& HDR_ISREQUEST
)))
3039 if (requested_index
== 0)
3046 if (index
>= lpwhr
->nCustHeaders
)
3049 TRACE("Return: %ld\n", index
);
3054 /***********************************************************************
3055 * HTTP_InsertCustomHeader (internal)
3057 * Insert header into array
3060 static BOOL
HTTP_InsertCustomHeader(LPWININETHTTPREQW lpwhr
, LPHTTPHEADERW lpHdr
)
3063 LPHTTPHEADERW lph
= NULL
;
3066 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
3067 count
= lpwhr
->nCustHeaders
+ 1;
3069 lph
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, lpwhr
->pCustHeaders
, sizeof(HTTPHEADERW
) * count
);
3071 lph
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(HTTPHEADERW
) * count
);
3075 lpwhr
->pCustHeaders
= lph
;
3076 lpwhr
->pCustHeaders
[count
-1].lpszField
= WININET_strdupW(lpHdr
->lpszField
);
3077 lpwhr
->pCustHeaders
[count
-1].lpszValue
= WININET_strdupW(lpHdr
->lpszValue
);
3078 lpwhr
->pCustHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
3079 lpwhr
->pCustHeaders
[count
-1].wCount
= lpHdr
->wCount
;
3080 lpwhr
->nCustHeaders
++;
3085 INTERNET_SetLastError(ERROR_OUTOFMEMORY
);
3092 /***********************************************************************
3093 * HTTP_DeleteCustomHeader (internal)
3095 * Delete header from array
3096 * If this function is called, the indexs may change.
3098 static BOOL
HTTP_DeleteCustomHeader(LPWININETHTTPREQW lpwhr
, DWORD index
)
3100 if( lpwhr
->nCustHeaders
<= 0 )
3102 if( index
>= lpwhr
->nCustHeaders
)
3104 lpwhr
->nCustHeaders
--;
3106 memmove( &lpwhr
->pCustHeaders
[index
], &lpwhr
->pCustHeaders
[index
+1],
3107 (lpwhr
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
3108 memset( &lpwhr
->pCustHeaders
[lpwhr
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
3113 /***********************************************************************
3114 * IsHostInProxyBypassList (@)
3119 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
3121 FIXME("STUB: flags=%ld host=%s length=%ld\n",flags
,szHost
,length
);