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
9 * Copyright 2006 Robert Shearman for CodeWeavers
10 * Copyright 2011 Jacek Caban for CodeWeavers
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
47 #define NO_SHLWAPI_STREAM
48 #define NO_SHLWAPI_REG
49 #define NO_SHLWAPI_GDI
58 #include "wine/debug.h"
59 #include "wine/exception.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
63 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
64 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
65 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
66 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
67 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
68 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
70 #define COLLECT_TIME 60000
81 unsigned int auth_data_len
;
82 BOOL finished
; /* finished authenticating */
86 typedef struct _basicAuthorizationData
93 UINT authorizationLen
;
94 } basicAuthorizationData
;
96 typedef struct _authorizationData
110 static struct list basicAuthorizationCache
= LIST_INIT(basicAuthorizationCache
);
111 static struct list authorizationCache
= LIST_INIT(authorizationCache
);
113 static CRITICAL_SECTION authcache_cs
;
114 static CRITICAL_SECTION_DEBUG critsect_debug
=
117 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
118 0, 0, { (DWORD_PTR
)(__FILE__
": authcache_cs") }
120 static CRITICAL_SECTION authcache_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
122 static DWORD
HTTP_GetResponseHeaders(http_request_t
*req
, INT
*len
);
123 static DWORD
HTTP_ProcessHeader(http_request_t
*req
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
124 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
125 static DWORD
HTTP_InsertCustomHeader(http_request_t
*req
, LPHTTPHEADERW lpHdr
);
126 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*req
, LPCWSTR lpszField
, INT index
, BOOL Request
);
127 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*req
, DWORD index
);
128 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
129 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*, DWORD
, LPVOID
, LPDWORD
, LPDWORD
);
130 static UINT
HTTP_DecodeBase64(LPCWSTR base64
, LPSTR bin
);
131 static DWORD
drain_content(http_request_t
*,BOOL
);
133 static CRITICAL_SECTION connection_pool_cs
;
134 static CRITICAL_SECTION_DEBUG connection_pool_debug
=
136 0, 0, &connection_pool_cs
,
137 { &connection_pool_debug
.ProcessLocksList
, &connection_pool_debug
.ProcessLocksList
},
138 0, 0, { (DWORD_PTR
)(__FILE__
": connection_pool_cs") }
140 static CRITICAL_SECTION connection_pool_cs
= { &connection_pool_debug
, -1, 0, 0, 0, 0 };
142 static struct list connection_pool
= LIST_INIT(connection_pool
);
143 static BOOL collector_running
;
145 void server_addref(server_t
*server
)
147 InterlockedIncrement(&server
->ref
);
150 void server_release(server_t
*server
)
152 if(InterlockedDecrement(&server
->ref
))
155 list_remove(&server
->entry
);
157 if(server
->cert_chain
)
158 CertFreeCertificateChain(server
->cert_chain
);
159 heap_free(server
->name
);
160 heap_free(server
->scheme_host_port
);
164 static BOOL
process_host_port(server_t
*server
)
167 size_t name_len
, len
;
170 name_len
= lstrlenW(server
->name
);
171 len
= name_len
+ 10 /* strlen("://:<port>") */ + ARRAY_SIZE(L
"https");
172 buf
= heap_alloc( len
* sizeof(WCHAR
) );
176 swprintf(buf
, len
, L
"%s://%s:%u", server
->is_https
? L
"https" : L
"http", server
->name
, server
->port
);
177 server
->scheme_host_port
= buf
;
179 server
->host_port
= server
->scheme_host_port
+ 7 /* strlen("http://") */;
183 default_port
= server
->port
== (server
->is_https
? INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
);
184 server
->canon_host_port
= default_port
? server
->name
: server
->host_port
;
188 server_t
*get_server(substr_t name
, INTERNET_PORT port
, BOOL is_https
, BOOL do_create
)
190 server_t
*iter
, *server
= NULL
;
192 EnterCriticalSection(&connection_pool_cs
);
194 LIST_FOR_EACH_ENTRY(iter
, &connection_pool
, server_t
, entry
) {
195 if(iter
->port
== port
&& name
.len
== lstrlenW(iter
->name
) && !wcsnicmp(iter
->name
, name
.str
, name
.len
)
196 && iter
->is_https
== is_https
) {
198 server_addref(server
);
203 if(!server
&& do_create
) {
204 server
= heap_alloc_zero(sizeof(*server
));
206 server
->ref
= 2; /* list reference and return */
208 server
->is_https
= is_https
;
209 list_init(&server
->conn_pool
);
210 server
->name
= heap_strndupW(name
.str
, name
.len
);
211 if(server
->name
&& process_host_port(server
)) {
212 list_add_head(&connection_pool
, &server
->entry
);
220 LeaveCriticalSection(&connection_pool_cs
);
225 BOOL
collect_connections(collect_type_t collect_type
)
227 netconn_t
*netconn
, *netconn_safe
;
228 server_t
*server
, *server_safe
;
229 BOOL remaining
= FALSE
;
232 now
= GetTickCount64();
234 LIST_FOR_EACH_ENTRY_SAFE(server
, server_safe
, &connection_pool
, server_t
, entry
) {
235 LIST_FOR_EACH_ENTRY_SAFE(netconn
, netconn_safe
, &server
->conn_pool
, netconn_t
, pool_entry
) {
236 if(collect_type
> COLLECT_TIMEOUT
|| netconn
->keep_until
< now
) {
237 TRACE("freeing %p\n", netconn
);
238 list_remove(&netconn
->pool_entry
);
239 free_netconn(netconn
);
245 if(collect_type
== COLLECT_CLEANUP
) {
246 list_remove(&server
->entry
);
247 list_init(&server
->entry
);
248 server_release(server
);
255 static DWORD WINAPI
collect_connections_proc(void *arg
)
257 BOOL remaining_conns
;
260 /* FIXME: Use more sophisticated method */
263 EnterCriticalSection(&connection_pool_cs
);
265 remaining_conns
= collect_connections(COLLECT_TIMEOUT
);
267 collector_running
= FALSE
;
269 LeaveCriticalSection(&connection_pool_cs
);
270 }while(remaining_conns
);
272 FreeLibraryAndExitThread(WININET_hModule
, 0);
275 /***********************************************************************
276 * HTTP_GetHeader (internal)
278 * Headers section must be held
280 static LPHTTPHEADERW
HTTP_GetHeader(http_request_t
*req
, LPCWSTR head
)
283 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
284 if (HeaderIndex
== -1)
287 return &req
->custHeaders
[HeaderIndex
];
290 static WCHAR
*get_host_header( http_request_t
*req
)
295 EnterCriticalSection( &req
->headers_section
);
296 if ((header
= HTTP_GetHeader( req
, L
"Host" ))) ret
= heap_strdupW( header
->lpszValue
);
297 else ret
= heap_strdupW( req
->server
->canon_host_port
);
298 LeaveCriticalSection( &req
->headers_section
);
302 struct data_stream_vtbl_t
{
303 BOOL (*end_of_data
)(data_stream_t
*,http_request_t
*);
304 DWORD (*read
)(data_stream_t
*,http_request_t
*,BYTE
*,DWORD
,DWORD
*,BOOL
);
305 DWORD (*drain_content
)(data_stream_t
*,http_request_t
*,BOOL
);
306 void (*destroy
)(data_stream_t
*);
310 data_stream_t data_stream
;
312 BYTE buf
[READ_BUFFER_SIZE
];
318 CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
,
319 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
,
320 CHUNKED_STREAM_STATE_READING_CHUNK
,
321 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
,
322 CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
,
323 CHUNKED_STREAM_STATE_END_OF_STREAM
,
324 CHUNKED_STREAM_STATE_ERROR
328 static inline void destroy_data_stream(data_stream_t
*stream
)
330 stream
->vtbl
->destroy(stream
);
333 static void reset_data_stream(http_request_t
*req
)
335 destroy_data_stream(req
->data_stream
);
336 req
->data_stream
= &req
->netconn_stream
.data_stream
;
337 req
->read_pos
= req
->read_size
= req
->netconn_stream
.content_read
= 0;
338 req
->read_gzip
= FALSE
;
341 static void remove_header( http_request_t
*request
, const WCHAR
*str
, BOOL from_request
)
344 EnterCriticalSection( &request
->headers_section
);
345 index
= HTTP_GetCustomHeaderIndex( request
, str
, 0, from_request
);
346 if (index
!= -1) HTTP_DeleteCustomHeader( request
, index
);
347 LeaveCriticalSection( &request
->headers_section
);
351 data_stream_t stream
;
352 data_stream_t
*parent_stream
;
354 BYTE buf
[READ_BUFFER_SIZE
];
360 static BOOL
gzip_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
362 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
363 return gzip_stream
->end_of_data
364 || (!gzip_stream
->buf_size
&& gzip_stream
->parent_stream
->vtbl
->end_of_data(gzip_stream
->parent_stream
, req
));
367 static DWORD
gzip_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
368 DWORD
*read
, BOOL allow_blocking
)
370 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
371 z_stream
*zstream
= &gzip_stream
->zstream
;
372 DWORD current_read
, ret_read
= 0;
374 DWORD res
= ERROR_SUCCESS
;
376 TRACE("(%d %x)\n", size
, allow_blocking
);
378 while(size
&& !gzip_stream
->end_of_data
) {
379 if(!gzip_stream
->buf_size
) {
380 if(gzip_stream
->buf_pos
) {
381 if(gzip_stream
->buf_size
)
382 memmove(gzip_stream
->buf
, gzip_stream
->buf
+gzip_stream
->buf_pos
, gzip_stream
->buf_size
);
383 gzip_stream
->buf_pos
= 0;
385 res
= gzip_stream
->parent_stream
->vtbl
->read(gzip_stream
->parent_stream
, req
, gzip_stream
->buf
+gzip_stream
->buf_size
,
386 sizeof(gzip_stream
->buf
)-gzip_stream
->buf_size
, ¤t_read
, allow_blocking
);
387 if(res
!= ERROR_SUCCESS
)
390 gzip_stream
->buf_size
+= current_read
;
392 WARN("unexpected end of data\n");
393 gzip_stream
->end_of_data
= TRUE
;
398 zstream
->next_in
= gzip_stream
->buf
+gzip_stream
->buf_pos
;
399 zstream
->avail_in
= gzip_stream
->buf_size
;
400 zstream
->next_out
= buf
+ret_read
;
401 zstream
->avail_out
= size
;
402 zres
= inflate(&gzip_stream
->zstream
, 0);
403 current_read
= size
- zstream
->avail_out
;
404 size
-= current_read
;
405 ret_read
+= current_read
;
406 gzip_stream
->buf_size
-= zstream
->next_in
- (gzip_stream
->buf
+gzip_stream
->buf_pos
);
407 gzip_stream
->buf_pos
= zstream
->next_in
-gzip_stream
->buf
;
408 if(zres
== Z_STREAM_END
) {
409 TRACE("end of data\n");
410 gzip_stream
->end_of_data
= TRUE
;
412 }else if(zres
!= Z_OK
) {
413 WARN("inflate failed %d: %s\n", zres
, debugstr_a(zstream
->msg
));
415 res
= ERROR_INTERNET_DECODING_FAILED
;
420 allow_blocking
= FALSE
;
423 TRACE("read %u bytes\n", ret_read
);
430 static DWORD
gzip_drain_content(data_stream_t
*stream
, http_request_t
*req
, BOOL allow_blocking
)
432 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
433 return gzip_stream
->parent_stream
->vtbl
->drain_content(gzip_stream
->parent_stream
, req
, allow_blocking
);
436 static void gzip_destroy(data_stream_t
*stream
)
438 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
440 destroy_data_stream(gzip_stream
->parent_stream
);
442 if(!gzip_stream
->end_of_data
)
443 inflateEnd(&gzip_stream
->zstream
);
444 heap_free(gzip_stream
);
447 static const data_stream_vtbl_t gzip_stream_vtbl
= {
454 static voidpf
wininet_zalloc(voidpf opaque
, uInt items
, uInt size
)
456 return heap_alloc(items
*size
);
459 static void wininet_zfree(voidpf opaque
, voidpf address
)
464 static DWORD
init_gzip_stream(http_request_t
*req
, BOOL is_gzip
)
466 gzip_stream_t
*gzip_stream
;
469 gzip_stream
= heap_alloc_zero(sizeof(gzip_stream_t
));
471 return ERROR_OUTOFMEMORY
;
473 gzip_stream
->stream
.vtbl
= &gzip_stream_vtbl
;
474 gzip_stream
->zstream
.zalloc
= wininet_zalloc
;
475 gzip_stream
->zstream
.zfree
= wininet_zfree
;
477 zres
= inflateInit2(&gzip_stream
->zstream
, is_gzip
? 0x1f : -15);
479 ERR("inflateInit failed: %d\n", zres
);
480 heap_free(gzip_stream
);
481 return ERROR_OUTOFMEMORY
;
484 remove_header(req
, L
"Content-Length", FALSE
);
487 memcpy(gzip_stream
->buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
488 gzip_stream
->buf_size
= req
->read_size
;
489 req
->read_pos
= req
->read_size
= 0;
492 req
->read_gzip
= TRUE
;
493 gzip_stream
->parent_stream
= req
->data_stream
;
494 req
->data_stream
= &gzip_stream
->stream
;
495 return ERROR_SUCCESS
;
498 /***********************************************************************
499 * HTTP_FreeTokens (internal)
501 * Frees table of pointers.
503 static void HTTP_FreeTokens(LPWSTR
* token_array
)
506 for (i
= 0; token_array
[i
]; i
++) heap_free(token_array
[i
]);
507 heap_free(token_array
);
510 static void HTTP_FixURL(http_request_t
*request
)
512 /* If we don't have a path we set it to root */
513 if (NULL
== request
->path
)
514 request
->path
= heap_strdupW(L
"/");
515 else /* remove \r and \n*/
517 int nLen
= lstrlenW(request
->path
);
518 while ((nLen
>0 ) && ((request
->path
[nLen
-1] == '\r')||(request
->path
[nLen
-1] == '\n')))
521 request
->path
[nLen
]='\0';
523 /* Replace '\' with '/' */
526 if (request
->path
[nLen
] == '\\') request
->path
[nLen
]='/';
530 if(CSTR_EQUAL
!= CompareStringW( LOCALE_INVARIANT
, NORM_IGNORECASE
,
531 request
->path
, lstrlenW(request
->path
), L
"http://", lstrlenW(L
"http://") )
532 && request
->path
[0] != '/') /* not an absolute path ?? --> fix it !! */
534 WCHAR
*fixurl
= heap_alloc((lstrlenW(request
->path
) + 2)*sizeof(WCHAR
));
536 lstrcpyW(fixurl
+ 1, request
->path
);
537 heap_free( request
->path
);
538 request
->path
= fixurl
;
542 static WCHAR
* build_request_header(http_request_t
*request
, const WCHAR
*verb
,
543 const WCHAR
*path
, const WCHAR
*version
, BOOL use_cr
)
545 LPWSTR requestString
;
550 EnterCriticalSection( &request
->headers_section
);
552 /* allocate space for an array of all the string pointers to be added */
553 len
= request
->nCustHeaders
* 5 + 10;
554 if (!(req
= heap_alloc( len
* sizeof(const WCHAR
*) )))
556 LeaveCriticalSection( &request
->headers_section
);
560 /* add the verb, path and HTTP version string */
571 /* Append custom request headers */
572 for (i
= 0; i
< request
->nCustHeaders
; i
++)
574 if (request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
576 req
[n
++] = request
->custHeaders
[i
].lpszField
;
578 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
583 TRACE("Adding custom header %s (%s)\n",
584 debugstr_w(request
->custHeaders
[i
].lpszField
),
585 debugstr_w(request
->custHeaders
[i
].lpszValue
));
593 requestString
= HTTP_build_req( req
, 4 );
595 LeaveCriticalSection( &request
->headers_section
);
596 return requestString
;
599 static WCHAR
* build_response_header(http_request_t
*request
, BOOL use_cr
)
605 EnterCriticalSection( &request
->headers_section
);
607 if (!(req
= heap_alloc( (request
->nCustHeaders
* 5 + 8) * sizeof(WCHAR
*) )))
609 LeaveCriticalSection( &request
->headers_section
);
613 if (request
->status_code
)
615 req
[n
++] = request
->version
;
616 swprintf(buf
, ARRAY_SIZE(buf
), L
" %u ", request
->status_code
);
618 req
[n
++] = request
->statusText
;
624 for(i
= 0; i
< request
->nCustHeaders
; i
++)
626 if(!(request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
627 && wcscmp(request
->custHeaders
[i
].lpszField
, L
"Status"))
629 req
[n
++] = request
->custHeaders
[i
].lpszField
;
631 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
636 TRACE("Adding custom header %s (%s)\n",
637 debugstr_w(request
->custHeaders
[i
].lpszField
),
638 debugstr_w(request
->custHeaders
[i
].lpszValue
));
646 ret
= HTTP_build_req(req
, 0);
648 LeaveCriticalSection( &request
->headers_section
);
652 static void HTTP_ProcessCookies( http_request_t
*request
)
656 LPHTTPHEADERW setCookieHeader
;
658 if(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
)
661 EnterCriticalSection( &request
->headers_section
);
663 while((HeaderIndex
= HTTP_GetCustomHeaderIndex(request
, L
"Set-Cookie", numCookies
++, FALSE
)) != -1)
668 setCookieHeader
= &request
->custHeaders
[HeaderIndex
];
670 if (!setCookieHeader
->lpszValue
)
673 data
= wcschr(setCookieHeader
->lpszValue
, '=');
677 name
= substr(setCookieHeader
->lpszValue
, data
- setCookieHeader
->lpszValue
);
679 set_cookie(substrz(request
->server
->name
), substrz(request
->path
), name
, substrz(data
), INTERNET_COOKIE_HTTPONLY
);
682 LeaveCriticalSection( &request
->headers_section
);
685 static void strip_spaces(LPWSTR start
)
694 memmove(start
, str
, sizeof(WCHAR
) * (lstrlenW(str
) + 1));
696 end
= start
+ lstrlenW(start
) - 1;
697 while (end
>= start
&& *end
== ' ')
704 static inline BOOL
is_basic_auth_value( LPCWSTR pszAuthValue
, LPWSTR
*pszRealm
)
706 static const WCHAR szBasic
[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
707 static const WCHAR szRealm
[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
709 is_basic
= !wcsnicmp(pszAuthValue
, szBasic
, ARRAY_SIZE(szBasic
)) &&
710 ((pszAuthValue
[ARRAY_SIZE(szBasic
)] == ' ') || !pszAuthValue
[ARRAY_SIZE(szBasic
)]);
711 if (is_basic
&& pszRealm
)
714 LPCWSTR ptr
= &pszAuthValue
[ARRAY_SIZE(szBasic
)];
718 token
= wcschr(ptr
,'=');
722 while (*realm
== ' ')
724 if(!wcsnicmp(realm
, szRealm
, ARRAY_SIZE(szRealm
)) &&
725 (realm
[ARRAY_SIZE(szRealm
)] == ' ' || realm
[ARRAY_SIZE(szRealm
)] == '='))
728 while (*token
== ' ')
732 *pszRealm
= heap_strdupW(token
);
733 strip_spaces(*pszRealm
);
740 static void destroy_authinfo( struct HttpAuthInfo
*authinfo
)
742 if (!authinfo
) return;
744 if (SecIsValidHandle(&authinfo
->ctx
))
745 DeleteSecurityContext(&authinfo
->ctx
);
746 if (SecIsValidHandle(&authinfo
->cred
))
747 FreeCredentialsHandle(&authinfo
->cred
);
749 heap_free(authinfo
->auth_data
);
750 heap_free(authinfo
->scheme
);
754 static UINT
retrieve_cached_basic_authorization(http_request_t
*req
, const WCHAR
*host
, const WCHAR
*realm
, char **auth_data
)
756 basicAuthorizationData
*ad
;
759 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host
),debugstr_w(realm
));
761 EnterCriticalSection(&authcache_cs
);
762 LIST_FOR_EACH_ENTRY(ad
, &basicAuthorizationCache
, basicAuthorizationData
, entry
)
764 if (!wcsicmp(host
, ad
->host
) && (!realm
|| !wcscmp(realm
, ad
->realm
)))
769 TRACE("Authorization found in cache\n");
770 *auth_data
= heap_alloc(ad
->authorizationLen
);
771 memcpy(*auth_data
,ad
->authorization
,ad
->authorizationLen
);
772 rc
= ad
->authorizationLen
;
774 /* update session username and password to reflect current credentials */
775 colon
= strchr(ad
->authorization
, ':');
776 length
= colon
- ad
->authorization
;
778 heap_free(req
->session
->userName
);
779 heap_free(req
->session
->password
);
781 req
->session
->userName
= heap_strndupAtoW(ad
->authorization
, length
, &length
);
783 req
->session
->password
= heap_strndupAtoW(&ad
->authorization
[length
], ad
->authorizationLen
- length
, &length
);
787 LeaveCriticalSection(&authcache_cs
);
791 static void cache_basic_authorization(LPWSTR host
, LPWSTR realm
, LPSTR auth_data
, UINT auth_data_len
)
794 basicAuthorizationData
* ad
= NULL
;
796 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host
),debugstr_w(realm
),debugstr_an(auth_data
,auth_data_len
));
798 EnterCriticalSection(&authcache_cs
);
799 LIST_FOR_EACH(cursor
, &basicAuthorizationCache
)
801 basicAuthorizationData
*check
= LIST_ENTRY(cursor
,basicAuthorizationData
,entry
);
802 if (!wcsicmp(host
,check
->host
) && !wcscmp(realm
,check
->realm
))
811 TRACE("Found match in cache, replacing\n");
812 heap_free(ad
->authorization
);
813 ad
->authorization
= heap_alloc(auth_data_len
);
814 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
815 ad
->authorizationLen
= auth_data_len
;
819 ad
= heap_alloc(sizeof(basicAuthorizationData
));
820 ad
->host
= heap_strdupW(host
);
821 ad
->realm
= heap_strdupW(realm
);
822 ad
->authorization
= heap_alloc(auth_data_len
);
823 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
824 ad
->authorizationLen
= auth_data_len
;
825 list_add_head(&basicAuthorizationCache
,&ad
->entry
);
826 TRACE("authorization cached\n");
828 LeaveCriticalSection(&authcache_cs
);
831 static BOOL
retrieve_cached_authorization(LPWSTR host
, LPWSTR scheme
,
832 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
834 authorizationData
*ad
;
836 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
838 EnterCriticalSection(&authcache_cs
);
839 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
) {
840 if(!wcsicmp(host
, ad
->host
) && !wcsicmp(scheme
, ad
->scheme
)) {
841 TRACE("Authorization found in cache\n");
843 nt_auth_identity
->User
= heap_strdupW(ad
->user
);
844 nt_auth_identity
->Password
= heap_strdupW(ad
->password
);
845 nt_auth_identity
->Domain
= heap_alloc(sizeof(WCHAR
)*ad
->domain_len
);
846 if(!nt_auth_identity
->User
|| !nt_auth_identity
->Password
||
847 (!nt_auth_identity
->Domain
&& ad
->domain_len
)) {
848 heap_free(nt_auth_identity
->User
);
849 heap_free(nt_auth_identity
->Password
);
850 heap_free(nt_auth_identity
->Domain
);
854 nt_auth_identity
->Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
855 nt_auth_identity
->UserLength
= ad
->user_len
;
856 nt_auth_identity
->PasswordLength
= ad
->password_len
;
857 memcpy(nt_auth_identity
->Domain
, ad
->domain
, sizeof(WCHAR
)*ad
->domain_len
);
858 nt_auth_identity
->DomainLength
= ad
->domain_len
;
859 LeaveCriticalSection(&authcache_cs
);
863 LeaveCriticalSection(&authcache_cs
);
868 static void cache_authorization(LPWSTR host
, LPWSTR scheme
,
869 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
871 authorizationData
*ad
;
874 TRACE("Caching authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
876 EnterCriticalSection(&authcache_cs
);
877 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
)
878 if(!wcsicmp(host
, ad
->host
) && !wcsicmp(scheme
, ad
->scheme
)) {
885 heap_free(ad
->password
);
886 heap_free(ad
->domain
);
888 ad
= heap_alloc(sizeof(authorizationData
));
890 LeaveCriticalSection(&authcache_cs
);
894 ad
->host
= heap_strdupW(host
);
895 ad
->scheme
= heap_strdupW(scheme
);
896 list_add_head(&authorizationCache
, &ad
->entry
);
899 ad
->user
= heap_strndupW(nt_auth_identity
->User
, nt_auth_identity
->UserLength
);
900 ad
->password
= heap_strndupW(nt_auth_identity
->Password
, nt_auth_identity
->PasswordLength
);
901 ad
->domain
= heap_strndupW(nt_auth_identity
->Domain
, nt_auth_identity
->DomainLength
);
902 ad
->user_len
= nt_auth_identity
->UserLength
;
903 ad
->password_len
= nt_auth_identity
->PasswordLength
;
904 ad
->domain_len
= nt_auth_identity
->DomainLength
;
906 if(!ad
->host
|| !ad
->scheme
|| !ad
->user
|| !ad
->password
907 || (nt_auth_identity
->Domain
&& !ad
->domain
)) {
909 heap_free(ad
->scheme
);
911 heap_free(ad
->password
);
912 heap_free(ad
->domain
);
913 list_remove(&ad
->entry
);
917 LeaveCriticalSection(&authcache_cs
);
920 void free_authorization_cache(void)
922 authorizationData
*ad
, *sa_safe
;
923 basicAuthorizationData
*basic
, *basic_safe
;
925 EnterCriticalSection(&authcache_cs
);
927 LIST_FOR_EACH_ENTRY_SAFE(basic
, basic_safe
, &basicAuthorizationCache
, basicAuthorizationData
, entry
)
929 heap_free(basic
->host
);
930 heap_free(basic
->realm
);
931 heap_free(basic
->authorization
);
933 list_remove(&basic
->entry
);
937 LIST_FOR_EACH_ENTRY_SAFE(ad
, sa_safe
, &authorizationCache
, authorizationData
, entry
)
940 heap_free(ad
->scheme
);
942 heap_free(ad
->password
);
943 heap_free(ad
->domain
);
944 list_remove(&ad
->entry
);
948 LeaveCriticalSection(&authcache_cs
);
951 static BOOL
HTTP_DoAuthorization( http_request_t
*request
, LPCWSTR pszAuthValue
,
952 struct HttpAuthInfo
**ppAuthInfo
,
953 LPWSTR domain_and_username
, LPWSTR password
,
956 SECURITY_STATUS sec_status
;
957 struct HttpAuthInfo
*pAuthInfo
= *ppAuthInfo
;
959 LPWSTR szRealm
= NULL
;
961 TRACE("%s\n", debugstr_w(pszAuthValue
));
968 pAuthInfo
= heap_alloc(sizeof(*pAuthInfo
));
972 SecInvalidateHandle(&pAuthInfo
->cred
);
973 SecInvalidateHandle(&pAuthInfo
->ctx
);
974 memset(&pAuthInfo
->exp
, 0, sizeof(pAuthInfo
->exp
));
976 pAuthInfo
->auth_data
= NULL
;
977 pAuthInfo
->auth_data_len
= 0;
978 pAuthInfo
->finished
= FALSE
;
980 if (is_basic_auth_value(pszAuthValue
,NULL
))
982 pAuthInfo
->scheme
= heap_strdupW(L
"Basic");
983 if (!pAuthInfo
->scheme
)
985 heap_free(pAuthInfo
);
992 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity
;
994 pAuthInfo
->scheme
= heap_strdupW(pszAuthValue
);
995 if (!pAuthInfo
->scheme
)
997 heap_free(pAuthInfo
);
1001 if (domain_and_username
)
1003 WCHAR
*user
= wcschr(domain_and_username
, '\\');
1004 WCHAR
*domain
= domain_and_username
;
1006 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1008 pAuthData
= &nt_auth_identity
;
1013 user
= domain_and_username
;
1017 nt_auth_identity
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
1018 nt_auth_identity
.User
= user
;
1019 nt_auth_identity
.UserLength
= lstrlenW(nt_auth_identity
.User
);
1020 nt_auth_identity
.Domain
= domain
;
1021 nt_auth_identity
.DomainLength
= domain
? user
- domain
- 1 : 0;
1022 nt_auth_identity
.Password
= password
;
1023 nt_auth_identity
.PasswordLength
= lstrlenW(nt_auth_identity
.Password
);
1025 cache_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
);
1027 else if(retrieve_cached_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
))
1028 pAuthData
= &nt_auth_identity
;
1030 /* use default credentials */
1033 sec_status
= AcquireCredentialsHandleW(NULL
, pAuthInfo
->scheme
,
1034 SECPKG_CRED_OUTBOUND
, NULL
,
1036 NULL
, &pAuthInfo
->cred
,
1039 if(pAuthData
&& !domain_and_username
) {
1040 heap_free(nt_auth_identity
.User
);
1041 heap_free(nt_auth_identity
.Domain
);
1042 heap_free(nt_auth_identity
.Password
);
1045 if (sec_status
== SEC_E_OK
)
1047 PSecPkgInfoW sec_pkg_info
;
1048 sec_status
= QuerySecurityPackageInfoW(pAuthInfo
->scheme
, &sec_pkg_info
);
1049 if (sec_status
== SEC_E_OK
)
1051 pAuthInfo
->max_token
= sec_pkg_info
->cbMaxToken
;
1052 FreeContextBuffer(sec_pkg_info
);
1055 if (sec_status
!= SEC_E_OK
)
1057 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1058 debugstr_w(pAuthInfo
->scheme
), sec_status
);
1059 heap_free(pAuthInfo
->scheme
);
1060 heap_free(pAuthInfo
);
1064 *ppAuthInfo
= pAuthInfo
;
1066 else if (pAuthInfo
->finished
)
1069 if ((lstrlenW(pszAuthValue
) < lstrlenW(pAuthInfo
->scheme
)) ||
1070 wcsnicmp(pszAuthValue
, pAuthInfo
->scheme
, lstrlenW(pAuthInfo
->scheme
)))
1072 ERR("authentication scheme changed from %s to %s\n",
1073 debugstr_w(pAuthInfo
->scheme
), debugstr_w(pszAuthValue
));
1077 if (is_basic_auth_value(pszAuthValue
,&szRealm
))
1081 char *auth_data
= NULL
;
1082 UINT auth_data_len
= 0;
1084 TRACE("basic authentication realm %s\n",debugstr_w(szRealm
));
1086 if (!domain_and_username
)
1088 if (host
&& szRealm
)
1089 auth_data_len
= retrieve_cached_basic_authorization(request
, host
, szRealm
,&auth_data
);
1090 if (auth_data_len
== 0)
1098 userlen
= WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, lstrlenW(domain_and_username
), NULL
, 0, NULL
, NULL
);
1099 passlen
= WideCharToMultiByte(CP_UTF8
, 0, password
, lstrlenW(password
), NULL
, 0, NULL
, NULL
);
1101 /* length includes a nul terminator, which will be re-used for the ':' */
1102 auth_data
= heap_alloc(userlen
+ 1 + passlen
);
1109 WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, -1, auth_data
, userlen
, NULL
, NULL
);
1110 auth_data
[userlen
] = ':';
1111 WideCharToMultiByte(CP_UTF8
, 0, password
, -1, &auth_data
[userlen
+1], passlen
, NULL
, NULL
);
1112 auth_data_len
= userlen
+ 1 + passlen
;
1113 if (host
&& szRealm
)
1114 cache_basic_authorization(host
, szRealm
, auth_data
, auth_data_len
);
1117 pAuthInfo
->auth_data
= auth_data
;
1118 pAuthInfo
->auth_data_len
= auth_data_len
;
1119 pAuthInfo
->finished
= TRUE
;
1125 LPCWSTR pszAuthData
;
1126 SecBufferDesc out_desc
, in_desc
;
1128 unsigned char *buffer
;
1129 ULONG context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
1130 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
1132 in
.BufferType
= SECBUFFER_TOKEN
;
1136 in_desc
.ulVersion
= 0;
1137 in_desc
.cBuffers
= 1;
1138 in_desc
.pBuffers
= &in
;
1140 pszAuthData
= pszAuthValue
+ lstrlenW(pAuthInfo
->scheme
);
1141 if (*pszAuthData
== ' ')
1144 in
.cbBuffer
= HTTP_DecodeBase64(pszAuthData
, NULL
);
1145 in
.pvBuffer
= heap_alloc(in
.cbBuffer
);
1146 HTTP_DecodeBase64(pszAuthData
, in
.pvBuffer
);
1149 buffer
= heap_alloc(pAuthInfo
->max_token
);
1151 out
.BufferType
= SECBUFFER_TOKEN
;
1152 out
.cbBuffer
= pAuthInfo
->max_token
;
1153 out
.pvBuffer
= buffer
;
1155 out_desc
.ulVersion
= 0;
1156 out_desc
.cBuffers
= 1;
1157 out_desc
.pBuffers
= &out
;
1159 sec_status
= InitializeSecurityContextW(first
? &pAuthInfo
->cred
: NULL
,
1160 first
? NULL
: &pAuthInfo
->ctx
,
1161 first
? request
->server
->name
: NULL
,
1162 context_req
, 0, SECURITY_NETWORK_DREP
,
1163 in
.pvBuffer
? &in_desc
: NULL
,
1164 0, &pAuthInfo
->ctx
, &out_desc
,
1165 &pAuthInfo
->attr
, &pAuthInfo
->exp
);
1166 if (sec_status
== SEC_E_OK
)
1168 pAuthInfo
->finished
= TRUE
;
1169 pAuthInfo
->auth_data
= out
.pvBuffer
;
1170 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1171 TRACE("sending last auth packet\n");
1173 else if (sec_status
== SEC_I_CONTINUE_NEEDED
)
1175 pAuthInfo
->auth_data
= out
.pvBuffer
;
1176 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1177 TRACE("sending next auth packet\n");
1181 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status
);
1182 heap_free(out
.pvBuffer
);
1183 destroy_authinfo(pAuthInfo
);
1192 /***********************************************************************
1193 * HTTP_HttpAddRequestHeadersW (internal)
1195 static DWORD
HTTP_HttpAddRequestHeadersW(http_request_t
*request
,
1196 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1201 DWORD len
, res
= ERROR_HTTP_INVALID_HEADER
;
1203 TRACE("copying header: %s\n", debugstr_wn(lpszHeader
, dwHeaderLength
));
1205 if( dwHeaderLength
== ~0U )
1206 len
= lstrlenW(lpszHeader
);
1208 len
= dwHeaderLength
;
1209 buffer
= heap_alloc(sizeof(WCHAR
)*(len
+1));
1210 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
1216 LPWSTR
* pFieldAndValue
;
1218 lpszEnd
= lpszStart
;
1220 while (*lpszEnd
!= '\0')
1222 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1227 if (*lpszStart
== '\0')
1230 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1233 lpszEnd
++; /* Jump over newline */
1235 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
1236 if (*lpszStart
== '\0')
1238 /* Skip 0-length headers */
1239 lpszStart
= lpszEnd
;
1240 res
= ERROR_SUCCESS
;
1243 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
1246 res
= HTTP_ProcessHeader(request
, pFieldAndValue
[0],
1247 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
1248 HTTP_FreeTokens(pFieldAndValue
);
1251 lpszStart
= lpszEnd
;
1252 } while (res
== ERROR_SUCCESS
);
1258 /***********************************************************************
1259 * HttpAddRequestHeadersW (WININET.@)
1261 * Adds one or more HTTP header to the request handler
1264 * On Windows if dwHeaderLength includes the trailing '\0', then
1265 * HttpAddRequestHeadersW() adds it too. However this results in an
1266 * invalid HTTP header which is rejected by some servers so we probably
1267 * don't need to match Windows on that point.
1274 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
1275 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1277 http_request_t
*request
;
1278 DWORD res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
1280 TRACE("%p, %s, %u, %08x\n", hHttpRequest
, debugstr_wn(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1285 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
1286 if (request
&& request
->hdr
.htype
== WH_HHTTPREQ
)
1287 res
= HTTP_HttpAddRequestHeadersW( request
, lpszHeader
, dwHeaderLength
, dwModifier
);
1289 WININET_Release( &request
->hdr
);
1291 if(res
!= ERROR_SUCCESS
)
1293 return res
== ERROR_SUCCESS
;
1296 /***********************************************************************
1297 * HttpAddRequestHeadersA (WININET.@)
1299 * Adds one or more HTTP header to the request handler
1306 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
1307 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1309 WCHAR
*headers
= NULL
;
1312 TRACE("%p, %s, %u, %08x\n", hHttpRequest
, debugstr_an(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1315 headers
= heap_strndupAtoW(lpszHeader
, dwHeaderLength
, &dwHeaderLength
);
1317 r
= HttpAddRequestHeadersW(hHttpRequest
, headers
, dwHeaderLength
, dwModifier
);
1323 static void free_accept_types( WCHAR
**accept_types
)
1325 WCHAR
*ptr
, **types
= accept_types
;
1328 while ((ptr
= *types
))
1333 heap_free( accept_types
);
1336 static WCHAR
**convert_accept_types( const char **accept_types
)
1339 const char **types
= accept_types
;
1341 BOOL invalid_pointer
= FALSE
;
1343 if (!types
) return NULL
;
1349 /* find out how many there are */
1350 if (*types
&& **types
)
1352 TRACE("accept type: %s\n", debugstr_a(*types
));
1358 WARN("invalid accept type pointer\n");
1359 invalid_pointer
= TRUE
;
1364 if (invalid_pointer
) return NULL
;
1365 if (!(typesW
= heap_alloc( sizeof(WCHAR
*) * (count
+ 1) ))) return NULL
;
1367 types
= accept_types
;
1370 if (*types
&& **types
) typesW
[count
++] = heap_strdupAtoW( *types
);
1373 typesW
[count
] = NULL
;
1377 /***********************************************************************
1378 * HttpOpenRequestA (WININET.@)
1380 * Open a HTTP request handle
1383 * HINTERNET a HTTP request handle on success
1387 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
1388 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
1389 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
1390 DWORD dwFlags
, DWORD_PTR dwContext
)
1392 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
1393 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
1394 HINTERNET rc
= NULL
;
1396 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
1397 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
1398 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
1399 dwFlags
, dwContext
);
1403 szVerb
= heap_strdupAtoW(lpszVerb
);
1410 szObjectName
= heap_strdupAtoW(lpszObjectName
);
1411 if ( !szObjectName
)
1417 szVersion
= heap_strdupAtoW(lpszVersion
);
1424 szReferrer
= heap_strdupAtoW(lpszReferrer
);
1429 szAcceptTypes
= convert_accept_types( lpszAcceptTypes
);
1430 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
, szVersion
, szReferrer
,
1431 (const WCHAR
**)szAcceptTypes
, dwFlags
, dwContext
);
1434 free_accept_types(szAcceptTypes
);
1435 heap_free(szReferrer
);
1436 heap_free(szVersion
);
1437 heap_free(szObjectName
);
1442 /***********************************************************************
1445 static UINT
HTTP_EncodeBase64( LPCSTR bin
, unsigned int len
, LPWSTR base64
)
1448 static const CHAR HTTP_Base64Enc
[] =
1449 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1453 /* first 6 bits, all from bin[0] */
1454 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
1455 x
= (bin
[0] & 3) << 4;
1457 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1460 base64
[n
++] = HTTP_Base64Enc
[x
];
1465 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
1466 x
= ( bin
[1] & 0x0f ) << 2;
1468 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1471 base64
[n
++] = HTTP_Base64Enc
[x
];
1475 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
1477 /* last 6 bits, all from bin [2] */
1478 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
1486 static const signed char HTTP_Base64Dec
[] =
1488 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1489 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1490 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1491 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1492 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1493 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1494 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1495 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1498 /***********************************************************************
1501 static UINT
HTTP_DecodeBase64( LPCWSTR base64
, LPSTR bin
)
1509 if (base64
[0] >= ARRAY_SIZE(HTTP_Base64Dec
) ||
1510 ((in
[0] = HTTP_Base64Dec
[base64
[0]]) == -1) ||
1511 base64
[1] >= ARRAY_SIZE(HTTP_Base64Dec
) ||
1512 ((in
[1] = HTTP_Base64Dec
[base64
[1]]) == -1))
1514 WARN("invalid base64: %s\n", debugstr_w(base64
));
1518 bin
[n
] = (unsigned char) (in
[0] << 2 | in
[1] >> 4);
1521 if ((base64
[2] == '=') && (base64
[3] == '='))
1523 if (base64
[2] > ARRAY_SIZE(HTTP_Base64Dec
) ||
1524 ((in
[2] = HTTP_Base64Dec
[base64
[2]]) == -1))
1526 WARN("invalid base64: %s\n", debugstr_w(&base64
[2]));
1530 bin
[n
] = (unsigned char) (in
[1] << 4 | in
[2] >> 2);
1533 if (base64
[3] == '=')
1535 if (base64
[3] > ARRAY_SIZE(HTTP_Base64Dec
) ||
1536 ((in
[3] = HTTP_Base64Dec
[base64
[3]]) == -1))
1538 WARN("invalid base64: %s\n", debugstr_w(&base64
[3]));
1542 bin
[n
] = (unsigned char) (((in
[2] << 6) & 0xc0) | in
[3]);
1551 static WCHAR
*encode_auth_data( const WCHAR
*scheme
, const char *data
, UINT data_len
)
1554 UINT len
, scheme_len
= lstrlenW( scheme
);
1556 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1557 len
= scheme_len
+ 1 + ((data_len
+ 2) * 4) / 3;
1558 if (!(ret
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1559 memcpy( ret
, scheme
, scheme_len
* sizeof(WCHAR
) );
1560 ret
[scheme_len
] = ' ';
1561 HTTP_EncodeBase64( data
, data_len
, ret
+ scheme_len
+ 1 );
1566 /***********************************************************************
1567 * HTTP_InsertAuthorization
1569 * Insert or delete the authorization field in the request header.
1571 static BOOL
HTTP_InsertAuthorization( http_request_t
*request
, struct HttpAuthInfo
*pAuthInfo
, LPCWSTR header
)
1573 WCHAR
*host
, *authorization
= NULL
;
1577 if (pAuthInfo
->auth_data_len
)
1579 if (!(authorization
= encode_auth_data(pAuthInfo
->scheme
, pAuthInfo
->auth_data
, pAuthInfo
->auth_data_len
)))
1582 /* clear the data as it isn't valid now that it has been sent to the
1583 * server, unless it's Basic authentication which doesn't do
1584 * connection tracking */
1585 if (wcsicmp(pAuthInfo
->scheme
, L
"Basic"))
1587 heap_free(pAuthInfo
->auth_data
);
1588 pAuthInfo
->auth_data
= NULL
;
1589 pAuthInfo
->auth_data_len
= 0;
1593 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1595 HTTP_ProcessHeader(request
, header
, authorization
,
1596 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
1597 heap_free(authorization
);
1604 /* Don't use cached credentials when a username or Authorization was specified */
1605 if ((request
->session
->userName
&& request
->session
->userName
[0]) || wcscmp(header
, L
"Authorization"))
1608 if (!(host
= get_host_header(request
)))
1611 if ((data_len
= retrieve_cached_basic_authorization(request
, host
, NULL
, &data
)))
1613 TRACE("Found cached basic authorization for %s\n", debugstr_w(host
));
1615 if (!(authorization
= encode_auth_data(L
"Basic", data
, data_len
)))
1622 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1624 HTTP_ProcessHeader(request
, header
, authorization
,
1625 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
1627 heap_free(authorization
);
1634 static WCHAR
*build_proxy_path_url(http_request_t
*req
)
1639 len
= lstrlenW(req
->server
->scheme_host_port
);
1640 size
= len
+ lstrlenW(req
->path
) + 1;
1641 if(*req
->path
!= '/')
1643 url
= heap_alloc(size
* sizeof(WCHAR
));
1647 memcpy(url
, req
->server
->scheme_host_port
, len
*sizeof(WCHAR
));
1648 if(*req
->path
!= '/')
1651 lstrcpyW(url
+len
, req
->path
);
1653 TRACE("url=%s\n", debugstr_w(url
));
1657 static BOOL
HTTP_DomainMatches(LPCWSTR server
, substr_t domain
)
1659 const WCHAR
*dot
, *ptr
;
1662 if(domain
.len
== ARRAY_SIZE(L
"<local>")-1 && !wcsnicmp(domain
.str
, L
"<local>", domain
.len
) && !wcschr(server
, '.' ))
1665 if(domain
.len
&& *domain
.str
!= '*')
1666 return domain
.len
== lstrlenW(server
) && !wcsnicmp(server
, domain
.str
, domain
.len
);
1668 if(domain
.len
< 2 || domain
.str
[1] != '.')
1671 /* For a hostname to match a wildcard, the last domain must match
1672 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1673 * hostname is www.foo.a.b, it matches, but a.b does not.
1675 dot
= wcschr(server
, '.');
1679 len
= lstrlenW(dot
+ 1);
1680 if(len
< domain
.len
- 2)
1683 /* The server's domain is longer than the wildcard, so it
1684 * could be a subdomain. Compare the last portion of the
1687 ptr
= dot
+ 1 + len
- domain
.len
+ 2;
1688 if(!wcsnicmp(ptr
, domain
.str
+2, domain
.len
-2))
1689 /* This is only a match if the preceding character is
1690 * a '.', i.e. that it is a matching domain. E.g.
1691 * if domain is '*.b.c' and server is 'www.ab.c' they
1694 return *(ptr
- 1) == '.';
1696 return len
== domain
.len
-2 && !wcsnicmp(dot
+ 1, domain
.str
+ 2, len
);
1699 static BOOL
HTTP_ShouldBypassProxy(appinfo_t
*lpwai
, LPCWSTR server
)
1704 if (!lpwai
->proxyBypass
) return FALSE
;
1705 ptr
= lpwai
->proxyBypass
;
1709 ptr
= wcschr( ptr
, ';' );
1711 ptr
= wcschr( tmp
, ' ' );
1713 ptr
= tmp
+ lstrlenW(tmp
);
1714 ret
= HTTP_DomainMatches( server
, substr(tmp
, ptr
-tmp
) );
1722 /***********************************************************************
1723 * HTTP_DealWithProxy
1725 static BOOL
HTTP_DealWithProxy(appinfo_t
*hIC
, http_session_t
*session
, http_request_t
*request
)
1727 static WCHAR szNul
[] = L
"";
1728 URL_COMPONENTSW UrlComponents
= { sizeof(UrlComponents
) };
1729 server_t
*new_server
= NULL
;
1732 proxy
= INTERNET_FindProxyForProtocol(hIC
->proxy
, L
"http");
1735 if(CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
1736 proxy
, lstrlenW(L
"http://"), L
"http://", lstrlenW(L
"http://"))) {
1737 WCHAR
*proxy_url
= heap_alloc(lstrlenW(proxy
)*sizeof(WCHAR
) + sizeof(L
"http://"));
1742 lstrcpyW(proxy_url
, L
"http://");
1743 lstrcatW(proxy_url
, proxy
);
1748 UrlComponents
.dwHostNameLength
= 1;
1749 if(InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) && UrlComponents
.dwHostNameLength
) {
1750 if( !request
->path
)
1751 request
->path
= szNul
;
1753 new_server
= get_server(substr(UrlComponents
.lpszHostName
, UrlComponents
.dwHostNameLength
),
1754 UrlComponents
.nPort
, UrlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
, TRUE
);
1760 request
->proxy
= new_server
;
1762 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server
->name
), new_server
->port
);
1766 static DWORD
HTTP_ResolveName(http_request_t
*request
)
1768 server_t
*server
= request
->proxy
? request
->proxy
: request
->server
;
1771 if(server
->addr_len
)
1772 return ERROR_SUCCESS
;
1774 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1775 INTERNET_STATUS_RESOLVING_NAME
,
1777 (lstrlenW(server
->name
)+1) * sizeof(WCHAR
));
1779 addr_len
= sizeof(server
->addr
);
1780 if (!GetAddress(server
->name
, server
->port
, (SOCKADDR
*)&server
->addr
, &addr_len
, server
->addr_str
))
1781 return ERROR_INTERNET_NAME_NOT_RESOLVED
;
1783 server
->addr_len
= addr_len
;
1784 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1785 INTERNET_STATUS_NAME_RESOLVED
,
1786 server
->addr_str
, strlen(server
->addr_str
)+1);
1788 TRACE("resolved %s to %s\n", debugstr_w(server
->name
), server
->addr_str
);
1789 return ERROR_SUCCESS
;
1792 static WCHAR
*compose_request_url(http_request_t
*req
)
1794 const WCHAR
*host
, *scheme
;
1798 host
= req
->server
->canon_host_port
;
1800 if (req
->server
->is_https
)
1801 scheme
= L
"https://";
1803 scheme
= L
"http://";
1805 len
= lstrlenW(scheme
) + lstrlenW(host
) + (req
->path
[0] != '/' ? 1 : 0) + lstrlenW(req
->path
);
1806 ptr
= buf
= heap_alloc((len
+1) * sizeof(WCHAR
));
1808 lstrcpyW(ptr
, scheme
);
1809 ptr
+= lstrlenW(ptr
);
1811 lstrcpyW(ptr
, host
);
1812 ptr
+= lstrlenW(ptr
);
1814 if(req
->path
[0] != '/')
1817 lstrcpyW(ptr
, req
->path
);
1818 ptr
+= lstrlenW(ptr
);
1826 /***********************************************************************
1827 * HTTPREQ_Destroy (internal)
1829 * Deallocate request handle
1832 static void HTTPREQ_Destroy(object_header_t
*hdr
)
1834 http_request_t
*request
= (http_request_t
*) hdr
;
1839 if(request
->hCacheFile
)
1840 CloseHandle(request
->hCacheFile
);
1841 if(request
->req_file
)
1842 req_file_release(request
->req_file
);
1844 request
->headers_section
.DebugInfo
->Spare
[0] = 0;
1845 DeleteCriticalSection( &request
->headers_section
);
1846 request
->read_section
.DebugInfo
->Spare
[0] = 0;
1847 DeleteCriticalSection( &request
->read_section
);
1848 WININET_Release(&request
->session
->hdr
);
1850 destroy_authinfo(request
->authInfo
);
1851 destroy_authinfo(request
->proxyAuthInfo
);
1854 server_release(request
->server
);
1856 server_release(request
->proxy
);
1858 heap_free(request
->path
);
1859 heap_free(request
->verb
);
1860 heap_free(request
->version
);
1861 heap_free(request
->statusText
);
1863 for (i
= 0; i
< request
->nCustHeaders
; i
++)
1865 heap_free(request
->custHeaders
[i
].lpszField
);
1866 heap_free(request
->custHeaders
[i
].lpszValue
);
1868 destroy_data_stream(request
->data_stream
);
1869 heap_free(request
->custHeaders
);
1872 static void http_release_netconn(http_request_t
*req
, BOOL reuse
)
1874 TRACE("%p %p %x\n",req
, req
->netconn
, reuse
);
1876 if(!is_valid_netconn(req
->netconn
))
1879 if(reuse
&& req
->netconn
->keep_alive
) {
1882 EnterCriticalSection(&connection_pool_cs
);
1884 list_add_head(&req
->netconn
->server
->conn_pool
, &req
->netconn
->pool_entry
);
1885 req
->netconn
->keep_until
= GetTickCount64() + COLLECT_TIME
;
1886 req
->netconn
= NULL
;
1888 run_collector
= !collector_running
;
1889 collector_running
= TRUE
;
1891 LeaveCriticalSection(&connection_pool_cs
);
1894 HANDLE thread
= NULL
;
1897 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (const WCHAR
*)WININET_hModule
, &module
);
1899 thread
= CreateThread(NULL
, 0, collect_connections_proc
, NULL
, 0, NULL
);
1901 EnterCriticalSection(&connection_pool_cs
);
1902 collector_running
= FALSE
;
1903 LeaveCriticalSection(&connection_pool_cs
);
1906 FreeLibrary(module
);
1909 CloseHandle(thread
);
1914 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1915 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
1917 close_netconn(req
->netconn
);
1919 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1920 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
1923 static BOOL
HTTP_KeepAlive(http_request_t
*request
)
1925 WCHAR szVersion
[10];
1926 WCHAR szConnectionResponse
[20];
1927 DWORD dwBufferSize
= sizeof(szVersion
);
1928 BOOL keepalive
= FALSE
;
1930 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
1931 * the connection is keep-alive by default */
1932 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_VERSION
, szVersion
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
1933 && !wcsicmp(szVersion
, L
"HTTP/1.1"))
1938 dwBufferSize
= sizeof(szConnectionResponse
);
1939 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_PROXY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
1940 || HTTP_HttpQueryInfoW(request
, HTTP_QUERY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
)
1942 keepalive
= !wcsicmp(szConnectionResponse
, L
"Keep-Alive");
1948 static void HTTPREQ_CloseConnection(object_header_t
*hdr
)
1950 http_request_t
*req
= (http_request_t
*)hdr
;
1952 http_release_netconn(req
, drain_content(req
, FALSE
) == ERROR_SUCCESS
);
1955 static DWORD
str_to_buffer(const WCHAR
*str
, void *buffer
, DWORD
*size
, BOOL unicode
)
1960 WCHAR
*buf
= buffer
;
1962 if (str
) len
= lstrlenW(str
);
1964 if (*size
< (len
+ 1) * sizeof(WCHAR
))
1966 *size
= (len
+ 1) * sizeof(WCHAR
);
1967 return ERROR_INSUFFICIENT_BUFFER
;
1969 if (str
) lstrcpyW(buf
, str
);
1973 return ERROR_SUCCESS
;
1979 if (str
) len
= WideCharToMultiByte(CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
1984 return ERROR_INSUFFICIENT_BUFFER
;
1986 if (str
) WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, *size
, NULL
, NULL
);
1990 return ERROR_SUCCESS
;
1994 static DWORD
get_security_cert_struct(http_request_t
*req
, INTERNET_CERTIFICATE_INFOA
*info
)
1996 PCCERT_CONTEXT context
;
1999 context
= (PCCERT_CONTEXT
)NETCON_GetCert(req
->netconn
);
2001 return ERROR_NOT_SUPPORTED
;
2003 memset(info
, 0, sizeof(*info
));
2004 info
->ftExpiry
= context
->pCertInfo
->NotAfter
;
2005 info
->ftStart
= context
->pCertInfo
->NotBefore
;
2006 len
= CertNameToStrA(context
->dwCertEncodingType
,
2007 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2008 info
->lpszSubjectInfo
= LocalAlloc(0, len
);
2009 if(info
->lpszSubjectInfo
)
2010 CertNameToStrA(context
->dwCertEncodingType
,
2011 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2012 info
->lpszSubjectInfo
, len
);
2013 len
= CertNameToStrA(context
->dwCertEncodingType
,
2014 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2015 info
->lpszIssuerInfo
= LocalAlloc(0, len
);
2016 if(info
->lpszIssuerInfo
)
2017 CertNameToStrA(context
->dwCertEncodingType
,
2018 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2019 info
->lpszIssuerInfo
, len
);
2020 info
->dwKeySize
= NETCON_GetCipherStrength(req
->netconn
);
2022 CertFreeCertificateContext(context
);
2023 return ERROR_SUCCESS
;
2026 static DWORD
HTTPREQ_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2028 http_request_t
*req
= (http_request_t
*)hdr
;
2031 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO
:
2033 INTERNET_DIAGNOSTIC_SOCKET_INFO
*info
= buffer
;
2035 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2037 if (*size
< sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
))
2038 return ERROR_INSUFFICIENT_BUFFER
;
2039 *size
= sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
);
2040 /* FIXME: can't get a SOCKET from our connection since we don't use
2044 /* FIXME: get source port from req->netConnection */
2045 info
->SourcePort
= 0;
2046 info
->DestPort
= req
->server
->port
;
2048 if (HTTP_KeepAlive(req
))
2049 info
->Flags
|= IDSI_FLAG_KEEP_ALIVE
;
2051 info
->Flags
|= IDSI_FLAG_PROXY
;
2052 if (is_valid_netconn(req
->netconn
) && req
->netconn
->secure
)
2053 info
->Flags
|= IDSI_FLAG_SECURE
;
2055 return ERROR_SUCCESS
;
2059 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2061 case INTERNET_OPTION_SECURITY_FLAGS
:
2065 if (*size
< sizeof(ULONG
))
2066 return ERROR_INSUFFICIENT_BUFFER
;
2068 *size
= sizeof(DWORD
);
2069 flags
= is_valid_netconn(req
->netconn
) ? req
->netconn
->security_flags
: req
->security_flags
| req
->server
->security_flags
;
2070 *(DWORD
*)buffer
= flags
;
2072 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags
);
2073 return ERROR_SUCCESS
;
2076 case INTERNET_OPTION_HANDLE_TYPE
:
2077 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2079 if (*size
< sizeof(ULONG
))
2080 return ERROR_INSUFFICIENT_BUFFER
;
2082 *size
= sizeof(DWORD
);
2083 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_HTTP_REQUEST
;
2084 return ERROR_SUCCESS
;
2086 case INTERNET_OPTION_URL
: {
2090 TRACE("INTERNET_OPTION_URL\n");
2092 url
= compose_request_url(req
);
2094 return ERROR_OUTOFMEMORY
;
2096 res
= str_to_buffer(url
, buffer
, size
, unicode
);
2100 case INTERNET_OPTION_USER_AGENT
:
2101 return str_to_buffer(req
->session
->appInfo
->agent
, buffer
, size
, unicode
);
2102 case INTERNET_OPTION_USERNAME
:
2103 return str_to_buffer(req
->session
->userName
, buffer
, size
, unicode
);
2104 case INTERNET_OPTION_PASSWORD
:
2105 return str_to_buffer(req
->session
->password
, buffer
, size
, unicode
);
2106 case INTERNET_OPTION_PROXY_USERNAME
:
2107 return str_to_buffer(req
->session
->appInfo
->proxyUsername
, buffer
, size
, unicode
);
2108 case INTERNET_OPTION_PROXY_PASSWORD
:
2109 return str_to_buffer(req
->session
->appInfo
->proxyPassword
, buffer
, size
, unicode
);
2111 case INTERNET_OPTION_CACHE_TIMESTAMPS
: {
2112 INTERNET_CACHE_ENTRY_INFOW
*info
;
2113 INTERNET_CACHE_TIMESTAMPS
*ts
= buffer
;
2114 DWORD nbytes
, error
;
2117 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2120 return ERROR_FILE_NOT_FOUND
;
2122 if (*size
< sizeof(*ts
))
2124 *size
= sizeof(*ts
);
2125 return ERROR_INSUFFICIENT_BUFFER
;
2129 ret
= GetUrlCacheEntryInfoW(req
->req_file
->url
, NULL
, &nbytes
);
2130 error
= GetLastError();
2131 if (!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
)
2133 if (!(info
= heap_alloc(nbytes
)))
2134 return ERROR_OUTOFMEMORY
;
2136 GetUrlCacheEntryInfoW(req
->req_file
->url
, info
, &nbytes
);
2138 ts
->ftExpires
= info
->ExpireTime
;
2139 ts
->ftLastModified
= info
->LastModifiedTime
;
2142 *size
= sizeof(*ts
);
2143 return ERROR_SUCCESS
;
2148 case INTERNET_OPTION_DATAFILE_NAME
: {
2151 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2153 if(!req
->req_file
) {
2155 return ERROR_INTERNET_ITEM_NOT_FOUND
;
2159 req_size
= (lstrlenW(req
->req_file
->file_name
)+1) * sizeof(WCHAR
);
2160 if(*size
< req_size
)
2161 return ERROR_INSUFFICIENT_BUFFER
;
2164 memcpy(buffer
, req
->req_file
->file_name
, *size
);
2165 return ERROR_SUCCESS
;
2167 req_size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
, -1, NULL
, 0, NULL
, NULL
);
2168 if (req_size
> *size
)
2169 return ERROR_INSUFFICIENT_BUFFER
;
2171 *size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
,
2172 -1, buffer
, *size
, NULL
, NULL
);
2173 return ERROR_SUCCESS
;
2177 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
: {
2179 return ERROR_INTERNET_INVALID_OPERATION
;
2181 if(*size
< sizeof(INTERNET_CERTIFICATE_INFOA
)) {
2182 *size
= sizeof(INTERNET_CERTIFICATE_INFOA
);
2183 return ERROR_INSUFFICIENT_BUFFER
;
2186 return get_security_cert_struct(req
, (INTERNET_CERTIFICATE_INFOA
*)buffer
);
2188 case INTERNET_OPTION_SECURITY_CERTIFICATE
: {
2194 char expiration
[64];
2197 char encryption
[64];
2201 char start_date
[32];
2202 char start_time
[32];
2203 char expiry_date
[32];
2204 char expiry_time
[32];
2205 SYSTEMTIME start
, expiry
;
2206 INTERNET_CERTIFICATE_INFOA info
;
2209 return ERROR_INVALID_PARAMETER
;
2213 return ERROR_INTERNET_INVALID_OPERATION
;
2218 return ERROR_INSUFFICIENT_BUFFER
;
2221 if((err
= get_security_cert_struct(req
, &info
)))
2224 LoadStringA(WININET_hModule
, IDS_CERT_SUBJECT
, subject
, sizeof(subject
));
2225 LoadStringA(WININET_hModule
, IDS_CERT_ISSUER
, issuer
, sizeof(issuer
));
2226 LoadStringA(WININET_hModule
, IDS_CERT_EFFECTIVE
, effective
, sizeof(effective
));
2227 LoadStringA(WININET_hModule
, IDS_CERT_EXPIRATION
, expiration
, sizeof(expiration
));
2228 LoadStringA(WININET_hModule
, IDS_CERT_PROTOCOL
, protocol
, sizeof(protocol
));
2229 LoadStringA(WININET_hModule
, IDS_CERT_SIGNATURE
, signature
, sizeof(signature
));
2230 LoadStringA(WININET_hModule
, IDS_CERT_ENCRYPTION
, encryption
, sizeof(encryption
));
2231 LoadStringA(WININET_hModule
, IDS_CERT_PRIVACY
, privacy
, sizeof(privacy
));
2232 LoadStringA(WININET_hModule
, info
.dwKeySize
>= 128 ? IDS_CERT_HIGH
: IDS_CERT_LOW
,
2233 strength
, sizeof(strength
));
2234 LoadStringA(WININET_hModule
, IDS_CERT_BITS
, bits
, sizeof(bits
));
2236 FileTimeToSystemTime(&info
.ftStart
, &start
);
2237 FileTimeToSystemTime(&info
.ftExpiry
, &expiry
);
2238 GetDateFormatA(LOCALE_USER_DEFAULT
, 0, &start
, NULL
, start_date
, sizeof(start_date
));
2239 GetTimeFormatA(LOCALE_USER_DEFAULT
, 0, &start
, NULL
, start_time
, sizeof(start_time
));
2240 GetDateFormatA(LOCALE_USER_DEFAULT
, 0, &expiry
, NULL
, expiry_date
, sizeof(expiry_date
));
2241 GetTimeFormatA(LOCALE_USER_DEFAULT
, 0, &expiry
, NULL
, expiry_time
, sizeof(expiry_time
));
2243 needed
= _scprintf("%s:\r\n%s\r\n"
2251 subject
, info
.lpszSubjectInfo
,
2252 issuer
, info
.lpszIssuerInfo
,
2253 effective
, start_date
, start_time
,
2254 expiration
, expiry_date
, expiry_time
,
2255 protocol
, signature
, encryption
,
2256 privacy
, strength
, info
.dwKeySize
, bits
);
2258 if(needed
< *size
) {
2259 err
= ERROR_SUCCESS
;
2260 *size
= snprintf(buffer
, *size
,
2269 subject
, info
.lpszSubjectInfo
,
2270 issuer
, info
.lpszIssuerInfo
,
2271 effective
, start_date
, start_time
,
2272 expiration
, expiry_date
, expiry_time
,
2273 protocol
, signature
, encryption
,
2274 privacy
, strength
, info
.dwKeySize
, bits
);
2276 err
= ERROR_INSUFFICIENT_BUFFER
;
2280 LocalFree(info
.lpszSubjectInfo
);
2281 LocalFree(info
.lpszIssuerInfo
);
2282 LocalFree(info
.lpszProtocolName
);
2283 LocalFree(info
.lpszSignatureAlgName
);
2284 LocalFree(info
.lpszEncryptionAlgName
);
2287 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2288 if (*size
< sizeof(DWORD
))
2289 return ERROR_INSUFFICIENT_BUFFER
;
2291 *size
= sizeof(DWORD
);
2292 *(DWORD
*)buffer
= req
->connect_timeout
;
2293 return ERROR_SUCCESS
;
2294 case INTERNET_OPTION_REQUEST_FLAGS
: {
2297 if (*size
< sizeof(DWORD
))
2298 return ERROR_INSUFFICIENT_BUFFER
;
2300 /* FIXME: Add support for:
2301 * INTERNET_REQFLAG_FROM_CACHE
2302 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2306 flags
|= INTERNET_REQFLAG_VIA_PROXY
;
2307 if(!req
->status_code
)
2308 flags
|= INTERNET_REQFLAG_NO_HEADERS
;
2310 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags
);
2312 *size
= sizeof(DWORD
);
2313 *(DWORD
*)buffer
= flags
;
2314 return ERROR_SUCCESS
;
2316 case INTERNET_OPTION_ERROR_MASK
:
2317 TRACE("INTERNET_OPTION_ERROR_MASK\n");
2319 if (*size
< sizeof(ULONG
))
2320 return ERROR_INSUFFICIENT_BUFFER
;
2322 *(ULONG
*)buffer
= hdr
->ErrorMask
;
2323 *size
= sizeof(ULONG
);
2324 return ERROR_SUCCESS
;
2327 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2330 static DWORD
HTTPREQ_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
2332 http_request_t
*req
= (http_request_t
*)hdr
;
2335 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2336 TRACE("Undocumented option 99\n");
2338 if (!buffer
|| size
!= sizeof(DWORD
))
2339 return ERROR_INVALID_PARAMETER
;
2340 if(*(DWORD
*)buffer
& ~SECURITY_SET_MASK
)
2341 return ERROR_INTERNET_OPTION_NOT_SETTABLE
;
2344 case INTERNET_OPTION_SECURITY_FLAGS
:
2348 if (!buffer
|| size
!= sizeof(DWORD
))
2349 return ERROR_INVALID_PARAMETER
;
2350 flags
= *(DWORD
*)buffer
;
2351 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags
);
2352 flags
&= SECURITY_SET_MASK
;
2353 req
->security_flags
|= flags
;
2354 if(is_valid_netconn(req
->netconn
))
2355 req
->netconn
->security_flags
|= flags
;
2356 return ERROR_SUCCESS
;
2358 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2359 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2360 req
->connect_timeout
= *(DWORD
*)buffer
;
2361 return ERROR_SUCCESS
;
2363 case INTERNET_OPTION_SEND_TIMEOUT
:
2364 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2365 req
->send_timeout
= *(DWORD
*)buffer
;
2366 return ERROR_SUCCESS
;
2368 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
2369 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2370 req
->receive_timeout
= *(DWORD
*)buffer
;
2371 return ERROR_SUCCESS
;
2373 case INTERNET_OPTION_USERNAME
:
2374 heap_free(req
->session
->userName
);
2375 if (!(req
->session
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2376 return ERROR_SUCCESS
;
2378 case INTERNET_OPTION_PASSWORD
:
2379 heap_free(req
->session
->password
);
2380 if (!(req
->session
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2381 return ERROR_SUCCESS
;
2383 case INTERNET_OPTION_PROXY_USERNAME
:
2384 heap_free(req
->session
->appInfo
->proxyUsername
);
2385 if (!(req
->session
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2386 return ERROR_SUCCESS
;
2388 case INTERNET_OPTION_PROXY_PASSWORD
:
2389 heap_free(req
->session
->appInfo
->proxyPassword
);
2390 if (!(req
->session
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2391 return ERROR_SUCCESS
;
2395 return INET_SetOption(hdr
, option
, buffer
, size
);
2398 static void commit_cache_entry(http_request_t
*req
)
2406 CloseHandle(req
->hCacheFile
);
2407 req
->hCacheFile
= NULL
;
2409 header
= build_response_header(req
, TRUE
);
2410 header_len
= (header
? lstrlenW(header
) : 0);
2411 res
= CommitUrlCacheEntryW(req
->req_file
->url
, req
->req_file
->file_name
, req
->expires
,
2412 req
->last_modified
, NORMAL_CACHE_ENTRY
,
2413 header
, header_len
, NULL
, 0);
2415 req
->req_file
->is_committed
= TRUE
;
2417 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2421 static void create_cache_entry(http_request_t
*req
)
2423 WCHAR file_name
[MAX_PATH
+1];
2427 /* FIXME: We should free previous cache file earlier */
2429 req_file_release(req
->req_file
);
2430 req
->req_file
= NULL
;
2432 if(req
->hCacheFile
) {
2433 CloseHandle(req
->hCacheFile
);
2434 req
->hCacheFile
= NULL
;
2437 if(req
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
)
2443 EnterCriticalSection( &req
->headers_section
);
2445 header_idx
= HTTP_GetCustomHeaderIndex(req
, L
"Cache-Control", 0, FALSE
);
2446 if(header_idx
!= -1) {
2449 for(ptr
=req
->custHeaders
[header_idx
].lpszValue
; *ptr
; ) {
2452 while(*ptr
==' ' || *ptr
=='\t')
2455 end
= wcschr(ptr
, ',');
2457 end
= ptr
+ lstrlenW(ptr
);
2459 if(!wcsnicmp(ptr
, L
"no-cache", ARRAY_SIZE(L
"no-cache")-1)
2460 || !wcsnicmp(ptr
, L
"no-store", ARRAY_SIZE(L
"no-store")-1)) {
2471 LeaveCriticalSection( &req
->headers_section
);
2475 if(!(req
->hdr
.dwFlags
& INTERNET_FLAG_NEED_FILE
))
2478 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2481 url
= compose_request_url(req
);
2483 WARN("Could not get URL\n");
2487 b
= CreateUrlCacheEntryW(url
, req
->contentLength
== ~0 ? 0 : req
->contentLength
, NULL
, file_name
, 0);
2489 WARN("Could not create cache entry: %08x\n", GetLastError());
2493 create_req_file(file_name
, &req
->req_file
);
2494 req
->req_file
->url
= url
;
2496 req
->hCacheFile
= CreateFileW(file_name
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2497 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2498 if(req
->hCacheFile
== INVALID_HANDLE_VALUE
) {
2499 WARN("Could not create file: %u\n", GetLastError());
2500 req
->hCacheFile
= NULL
;
2504 if(req
->read_size
) {
2507 b
= WriteFile(req
->hCacheFile
, req
->read_buf
+req
->read_pos
, req
->read_size
, &written
, NULL
);
2509 FIXME("WriteFile failed: %u\n", GetLastError());
2511 if(req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2512 commit_cache_entry(req
);
2516 /* read some more data into the read buffer (the read section must be held) */
2517 static DWORD
read_more_data( http_request_t
*req
, int maxlen
)
2524 /* move existing data to the start of the buffer */
2526 memmove( req
->read_buf
, req
->read_buf
+ req
->read_pos
, req
->read_size
);
2530 if (maxlen
== -1) maxlen
= sizeof(req
->read_buf
);
2532 res
= NETCON_recv( req
->netconn
, req
->read_buf
+ req
->read_size
,
2533 maxlen
- req
->read_size
, TRUE
, &len
);
2534 if(res
== ERROR_SUCCESS
)
2535 req
->read_size
+= len
;
2540 /* remove some amount of data from the read buffer (the read section must be held) */
2541 static void remove_data( http_request_t
*req
, int count
)
2543 if (!(req
->read_size
-= count
)) req
->read_pos
= 0;
2544 else req
->read_pos
+= count
;
2547 static DWORD
read_line( http_request_t
*req
, LPSTR buffer
, DWORD
*len
)
2549 int count
, bytes_read
, pos
= 0;
2552 EnterCriticalSection( &req
->read_section
);
2555 BYTE
*eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
2559 count
= eol
- (req
->read_buf
+ req
->read_pos
);
2560 bytes_read
= count
+ 1;
2562 else count
= bytes_read
= req
->read_size
;
2564 count
= min( count
, *len
- pos
);
2565 memcpy( buffer
+ pos
, req
->read_buf
+ req
->read_pos
, count
);
2567 remove_data( req
, bytes_read
);
2570 if ((res
= read_more_data( req
, -1 )))
2572 WARN( "read failed %u\n", res
);
2573 LeaveCriticalSection( &req
->read_section
);
2576 if (!req
->read_size
)
2579 TRACE( "returning empty string\n" );
2580 LeaveCriticalSection( &req
->read_section
);
2581 return ERROR_SUCCESS
;
2584 LeaveCriticalSection( &req
->read_section
);
2588 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
2591 buffer
[*len
- 1] = 0;
2592 TRACE( "returning %s\n", debugstr_a(buffer
));
2593 return ERROR_SUCCESS
;
2596 /* check if we have reached the end of the data to read (the read section must be held) */
2597 static BOOL
end_of_read_data( http_request_t
*req
)
2599 return !req
->read_size
&& req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
);
2602 static DWORD
read_http_stream(http_request_t
*req
, BYTE
*buf
, DWORD size
, DWORD
*read
, BOOL allow_blocking
)
2606 res
= req
->data_stream
->vtbl
->read(req
->data_stream
, req
, buf
, size
, read
, allow_blocking
);
2607 if(res
!= ERROR_SUCCESS
)
2609 assert(*read
<= size
);
2611 if(req
->hCacheFile
) {
2616 bres
= WriteFile(req
->hCacheFile
, buf
, *read
, &written
, NULL
);
2618 FIXME("WriteFile failed: %u\n", GetLastError());
2621 if((res
== ERROR_SUCCESS
&& !*read
) || req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2622 commit_cache_entry(req
);
2628 /* fetch some more data into the read buffer (the read section must be held) */
2629 static DWORD
refill_read_buffer(http_request_t
*req
, BOOL allow_blocking
, DWORD
*read_bytes
)
2633 if(req
->read_size
== sizeof(req
->read_buf
))
2634 return ERROR_SUCCESS
;
2638 memmove(req
->read_buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
2642 res
= read_http_stream(req
, req
->read_buf
+req
->read_size
, sizeof(req
->read_buf
) - req
->read_size
,
2643 &read
, allow_blocking
);
2644 if(res
!= ERROR_SUCCESS
)
2647 req
->read_size
+= read
;
2649 TRACE("read %u bytes, read_size %u\n", read
, req
->read_size
);
2655 static BOOL
netconn_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2657 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2658 return netconn_stream
->content_read
== netconn_stream
->content_length
|| !is_valid_netconn(req
->netconn
);
2661 static DWORD
netconn_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2662 DWORD
*read
, BOOL allow_blocking
)
2664 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2665 DWORD res
= ERROR_SUCCESS
;
2668 size
= min(size
, netconn_stream
->content_length
-netconn_stream
->content_read
);
2670 if(size
&& is_valid_netconn(req
->netconn
)) {
2671 res
= NETCON_recv(req
->netconn
, buf
, size
, allow_blocking
, &ret
);
2672 if(res
== ERROR_SUCCESS
) {
2674 netconn_stream
->content_length
= netconn_stream
->content_read
;
2675 netconn_stream
->content_read
+= ret
;
2679 TRACE("res %u read %u bytes\n", res
, ret
);
2684 static DWORD
netconn_drain_content(data_stream_t
*stream
, http_request_t
*req
, BOOL allow_blocking
)
2686 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2691 if(netconn_stream
->content_length
== ~0)
2694 while(netconn_stream
->content_read
< netconn_stream
->content_length
) {
2695 size
= min(sizeof(buf
), netconn_stream
->content_length
-netconn_stream
->content_read
);
2696 res
= NETCON_recv(req
->netconn
, buf
, size
, allow_blocking
, &len
);
2700 return WSAECONNABORTED
;
2702 netconn_stream
->content_read
+= len
;
2705 return ERROR_SUCCESS
;
2708 static void netconn_destroy(data_stream_t
*stream
)
2712 static const data_stream_vtbl_t netconn_stream_vtbl
= {
2713 netconn_end_of_data
,
2715 netconn_drain_content
,
2719 static char next_chunked_data_char(chunked_stream_t
*stream
)
2721 assert(stream
->buf_size
);
2724 return stream
->buf
[stream
->buf_pos
++];
2727 static BOOL
chunked_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2729 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2730 switch(chunked_stream
->state
) {
2731 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
:
2732 case CHUNKED_STREAM_STATE_END_OF_STREAM
:
2733 case CHUNKED_STREAM_STATE_ERROR
:
2740 static DWORD
chunked_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2741 DWORD
*read
, BOOL allow_blocking
)
2743 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2744 DWORD ret_read
= 0, res
= ERROR_SUCCESS
;
2745 BOOL continue_read
= TRUE
;
2750 TRACE("state %d\n", chunked_stream
->state
);
2752 /* Ensure that we have data in the buffer for states that need it. */
2753 if(!chunked_stream
->buf_size
) {
2754 BOOL blocking_read
= allow_blocking
;
2756 switch(chunked_stream
->state
) {
2757 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
:
2758 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
:
2759 /* never allow blocking after 0 chunk size */
2760 if(!chunked_stream
->chunk_size
)
2761 blocking_read
= FALSE
;
2763 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
:
2764 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
:
2765 chunked_stream
->buf_pos
= 0;
2766 res
= NETCON_recv(req
->netconn
, chunked_stream
->buf
, sizeof(chunked_stream
->buf
), blocking_read
, &read_bytes
);
2767 if(res
== ERROR_SUCCESS
&& read_bytes
) {
2768 chunked_stream
->buf_size
+= read_bytes
;
2769 }else if(res
== WSAEWOULDBLOCK
) {
2770 if(ret_read
|| allow_blocking
)
2771 res
= ERROR_SUCCESS
;
2772 continue_read
= FALSE
;
2775 chunked_stream
->state
= CHUNKED_STREAM_STATE_ERROR
;
2783 switch(chunked_stream
->state
) {
2784 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
:
2785 ch
= next_chunked_data_char(chunked_stream
);
2787 if(ch
>= '0' && ch
<= '9') {
2788 chunked_stream
->chunk_size
= chunked_stream
->chunk_size
* 16 + ch
- '0';
2789 }else if(ch
>= 'a' && ch
<= 'f') {
2790 chunked_stream
->chunk_size
= chunked_stream
->chunk_size
* 16 + ch
- 'a' + 10;
2791 }else if (ch
>= 'A' && ch
<= 'F') {
2792 chunked_stream
->chunk_size
= chunked_stream
->chunk_size
* 16 + ch
- 'A' + 10;
2793 }else if (ch
== ';' || ch
== '\r' || ch
== '\n') {
2794 TRACE("reading %u byte chunk\n", chunked_stream
->chunk_size
);
2795 chunked_stream
->buf_size
++;
2796 chunked_stream
->buf_pos
--;
2797 if(req
->contentLength
== ~0) req
->contentLength
= chunked_stream
->chunk_size
;
2798 else req
->contentLength
+= chunked_stream
->chunk_size
;
2799 chunked_stream
->state
= CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
;
2803 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
:
2804 ch
= next_chunked_data_char(chunked_stream
);
2806 chunked_stream
->state
= chunked_stream
->chunk_size
2807 ? CHUNKED_STREAM_STATE_READING_CHUNK
2808 : CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
;
2810 WARN("unexpected char '%c'\n", ch
);
2813 case CHUNKED_STREAM_STATE_READING_CHUNK
:
2814 assert(chunked_stream
->chunk_size
);
2816 continue_read
= FALSE
;
2819 read_bytes
= min(size
, chunked_stream
->chunk_size
);
2821 if(chunked_stream
->buf_size
) {
2822 if(read_bytes
> chunked_stream
->buf_size
)
2823 read_bytes
= chunked_stream
->buf_size
;
2825 memcpy(buf
+ret_read
, chunked_stream
->buf
+chunked_stream
->buf_pos
, read_bytes
);
2826 chunked_stream
->buf_pos
+= read_bytes
;
2827 chunked_stream
->buf_size
-= read_bytes
;
2829 res
= NETCON_recv(req
->netconn
, (char*)buf
+ret_read
, read_bytes
,
2830 allow_blocking
, (int*)&read_bytes
);
2831 if(res
!= ERROR_SUCCESS
) {
2832 continue_read
= FALSE
;
2837 chunked_stream
->state
= CHUNKED_STREAM_STATE_ERROR
;
2842 chunked_stream
->chunk_size
-= read_bytes
;
2844 ret_read
+= read_bytes
;
2845 if(!chunked_stream
->chunk_size
)
2846 chunked_stream
->state
= CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
;
2847 allow_blocking
= FALSE
;
2850 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
:
2851 ch
= next_chunked_data_char(chunked_stream
);
2853 chunked_stream
->state
= CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
;
2855 WARN("unexpected char '%c'\n", ch
);
2858 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
:
2859 ch
= next_chunked_data_char(chunked_stream
);
2861 chunked_stream
->state
= CHUNKED_STREAM_STATE_END_OF_STREAM
;
2863 WARN("unexpected char '%c'\n", ch
);
2866 case CHUNKED_STREAM_STATE_END_OF_STREAM
:
2867 case CHUNKED_STREAM_STATE_ERROR
:
2868 continue_read
= FALSE
;
2871 } while(continue_read
);
2874 res
= ERROR_SUCCESS
;
2875 if(res
!= ERROR_SUCCESS
)
2878 TRACE("read %d bytes\n", ret_read
);
2880 return ERROR_SUCCESS
;
2883 static DWORD
chunked_drain_content(data_stream_t
*stream
, http_request_t
*req
, BOOL allow_blocking
)
2885 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2889 while(chunked_stream
->state
!= CHUNKED_STREAM_STATE_END_OF_STREAM
2890 && chunked_stream
->state
!= CHUNKED_STREAM_STATE_ERROR
) {
2891 res
= chunked_read(stream
, req
, buf
, sizeof(buf
), &size
, allow_blocking
);
2892 if(res
!= ERROR_SUCCESS
)
2896 if(chunked_stream
->state
!= CHUNKED_STREAM_STATE_END_OF_STREAM
)
2897 return ERROR_NO_DATA
;
2898 return ERROR_SUCCESS
;
2901 static void chunked_destroy(data_stream_t
*stream
)
2903 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2904 heap_free(chunked_stream
);
2907 static const data_stream_vtbl_t chunked_stream_vtbl
= {
2908 chunked_end_of_data
,
2910 chunked_drain_content
,
2914 /* set the request content length based on the headers */
2915 static DWORD
set_content_length(http_request_t
*request
)
2917 WCHAR contentLength
[32];
2921 if(request
->status_code
== HTTP_STATUS_NO_CONTENT
|| !wcscmp(request
->verb
, L
"HEAD")) {
2922 request
->contentLength
= request
->netconn_stream
.content_length
= 0;
2923 return ERROR_SUCCESS
;
2926 size
= sizeof(contentLength
);
2927 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_CONTENT_LENGTH
,
2928 contentLength
, &size
, NULL
) != ERROR_SUCCESS
||
2929 !StrToInt64ExW(contentLength
, STIF_DEFAULT
, (LONGLONG
*)&request
->contentLength
)) {
2930 request
->contentLength
= ~0;
2933 request
->netconn_stream
.content_length
= request
->contentLength
;
2934 request
->netconn_stream
.content_read
= request
->read_size
;
2936 size
= sizeof(encoding
);
2937 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_TRANSFER_ENCODING
, encoding
, &size
, NULL
) == ERROR_SUCCESS
&&
2938 !wcsicmp(encoding
, L
"chunked"))
2940 chunked_stream_t
*chunked_stream
;
2942 chunked_stream
= heap_alloc(sizeof(*chunked_stream
));
2944 return ERROR_OUTOFMEMORY
;
2946 chunked_stream
->data_stream
.vtbl
= &chunked_stream_vtbl
;
2947 chunked_stream
->buf_size
= chunked_stream
->buf_pos
= 0;
2948 chunked_stream
->chunk_size
= 0;
2949 chunked_stream
->state
= CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
;
2951 if(request
->read_size
) {
2952 memcpy(chunked_stream
->buf
, request
->read_buf
+request
->read_pos
, request
->read_size
);
2953 chunked_stream
->buf_size
= request
->read_size
;
2954 request
->read_size
= request
->read_pos
= 0;
2957 request
->data_stream
= &chunked_stream
->data_stream
;
2958 request
->contentLength
= ~0;
2961 if(request
->hdr
.decoding
) {
2964 EnterCriticalSection( &request
->headers_section
);
2966 encoding_idx
= HTTP_GetCustomHeaderIndex(request
, L
"Content-Encoding", 0, FALSE
);
2967 if(encoding_idx
!= -1) {
2968 if(!wcsicmp(request
->custHeaders
[encoding_idx
].lpszValue
, L
"gzip")) {
2969 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2970 LeaveCriticalSection( &request
->headers_section
);
2971 return init_gzip_stream(request
, TRUE
);
2973 if(!wcsicmp(request
->custHeaders
[encoding_idx
].lpszValue
, L
"deflate")) {
2974 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2975 LeaveCriticalSection( &request
->headers_section
);
2976 return init_gzip_stream(request
, FALSE
);
2980 LeaveCriticalSection( &request
->headers_section
);
2983 return ERROR_SUCCESS
;
2986 static void send_request_complete(http_request_t
*req
, DWORD_PTR result
, DWORD error
)
2988 INTERNET_ASYNC_RESULT iar
;
2990 iar
.dwResult
= result
;
2991 iar
.dwError
= error
;
2993 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
2994 sizeof(INTERNET_ASYNC_RESULT
));
2997 static void HTTP_ReceiveRequestData(http_request_t
*req
)
2999 DWORD res
, read
= 0;
3003 EnterCriticalSection( &req
->read_section
);
3005 res
= refill_read_buffer(req
, FALSE
, &read
);
3006 if(res
== ERROR_SUCCESS
)
3007 read
+= req
->read_size
;
3009 LeaveCriticalSection( &req
->read_section
);
3011 if(res
!= WSAEWOULDBLOCK
&& (res
!= ERROR_SUCCESS
|| !read
)) {
3012 WARN("res %u read %u, closing connection\n", res
, read
);
3013 http_release_netconn(req
, FALSE
);
3016 if(res
!= ERROR_SUCCESS
&& res
!= WSAEWOULDBLOCK
) {
3017 send_request_complete(req
, 0, res
);
3021 send_request_complete(req
, req
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)req
->hdr
.hInternet
: 1, 0);
3024 /* read data from the http connection (the read section must be held) */
3025 static DWORD
HTTPREQ_Read(http_request_t
*req
, void *buffer
, DWORD size
, DWORD
*read
, BOOL allow_blocking
)
3027 DWORD current_read
= 0, ret_read
= 0;
3028 DWORD res
= ERROR_SUCCESS
;
3030 EnterCriticalSection( &req
->read_section
);
3032 if(req
->read_size
) {
3033 ret_read
= min(size
, req
->read_size
);
3034 memcpy(buffer
, req
->read_buf
+req
->read_pos
, ret_read
);
3035 req
->read_size
-= ret_read
;
3036 req
->read_pos
+= ret_read
;
3037 allow_blocking
= FALSE
;
3040 if(ret_read
< size
) {
3041 res
= read_http_stream(req
, (BYTE
*)buffer
+ret_read
, size
-ret_read
, ¤t_read
, allow_blocking
);
3042 if(res
== ERROR_SUCCESS
)
3043 ret_read
+= current_read
;
3044 else if(res
== WSAEWOULDBLOCK
&& ret_read
)
3045 res
= ERROR_SUCCESS
;
3048 LeaveCriticalSection( &req
->read_section
);
3051 TRACE( "retrieved %u bytes (res %u)\n", ret_read
, res
);
3053 if(res
!= WSAEWOULDBLOCK
) {
3054 if(res
!= ERROR_SUCCESS
)
3055 http_release_netconn(req
, FALSE
);
3056 else if(!ret_read
&& drain_content(req
, FALSE
) == ERROR_SUCCESS
)
3057 http_release_netconn(req
, TRUE
);
3063 static DWORD
drain_content(http_request_t
*req
, BOOL blocking
)
3067 TRACE("%p\n", req
->netconn
);
3069 if(!is_valid_netconn(req
->netconn
))
3070 return ERROR_NO_DATA
;
3072 if(!wcscmp(req
->verb
, L
"HEAD"))
3073 return ERROR_SUCCESS
;
3075 EnterCriticalSection( &req
->read_section
);
3076 res
= req
->data_stream
->vtbl
->drain_content(req
->data_stream
, req
, blocking
);
3077 LeaveCriticalSection( &req
->read_section
);
3089 static void async_read_file_proc(task_header_t
*hdr
)
3091 read_file_task_t
*task
= (read_file_task_t
*)hdr
;
3092 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
3093 DWORD res
= ERROR_SUCCESS
, read
= task
->read_pos
, complete_arg
= 0;
3095 TRACE("req %p buf %p size %u read_pos %u ret_read %p\n", req
, task
->buf
, task
->size
, task
->read_pos
, task
->ret_read
);
3099 while (read
< task
->size
) {
3100 res
= HTTPREQ_Read(req
, (char*)task
->buf
+ read
, task
->size
- read
, &read_bytes
, TRUE
);
3101 if (res
!= ERROR_SUCCESS
|| !read_bytes
)
3106 EnterCriticalSection(&req
->read_section
);
3107 res
= refill_read_buffer(req
, TRUE
, &read
);
3108 LeaveCriticalSection(&req
->read_section
);
3111 complete_arg
= read
; /* QueryDataAvailable reports read bytes in request complete notification */
3112 if(res
!= ERROR_SUCCESS
|| !read
)
3113 http_release_netconn(req
, drain_content(req
, FALSE
) == ERROR_SUCCESS
);
3116 TRACE("res %u read %u\n", res
, read
);
3119 *task
->ret_read
= read
;
3121 /* FIXME: We should report bytes transferred before decoding content. */
3122 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
, &read
, sizeof(read
));
3124 if(res
!= ERROR_SUCCESS
)
3126 send_request_complete(req
, res
== ERROR_SUCCESS
, complete_arg
);
3129 static DWORD
async_read(http_request_t
*req
, void *buf
, DWORD size
, DWORD read_pos
, DWORD
*ret_read
)
3131 read_file_task_t
*task
;
3133 task
= alloc_async_task(&req
->hdr
, async_read_file_proc
, sizeof(*task
));
3135 return ERROR_OUTOFMEMORY
;
3139 task
->read_pos
= read_pos
;
3140 task
->ret_read
= ret_read
;
3142 INTERNET_AsyncCall(&task
->hdr
);
3143 return ERROR_IO_PENDING
;
3146 static DWORD
HTTPREQ_ReadFile(object_header_t
*hdr
, void *buf
, DWORD size
, DWORD
*ret_read
,
3147 DWORD flags
, DWORD_PTR context
)
3149 http_request_t
*req
= (http_request_t
*)hdr
;
3150 DWORD res
= ERROR_SUCCESS
, read
= 0, cread
, error
= ERROR_SUCCESS
;
3151 BOOL allow_blocking
, notify_received
= FALSE
;
3153 TRACE("(%p %p %u %x)\n", req
, buf
, size
, flags
);
3155 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
3156 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
3158 allow_blocking
= !(req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
);
3160 if(allow_blocking
|| TryEnterCriticalSection(&req
->read_section
)) {
3162 EnterCriticalSection(&req
->read_section
);
3163 if(hdr
->dwError
== ERROR_SUCCESS
)
3164 hdr
->dwError
= INTERNET_HANDLE_IN_USE
;
3165 else if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3166 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3168 if(req
->read_size
) {
3169 read
= min(size
, req
->read_size
);
3170 memcpy(buf
, req
->read_buf
+ req
->read_pos
, read
);
3171 req
->read_size
-= read
;
3172 req
->read_pos
+= read
;
3175 if(read
< size
&& (!read
|| !(flags
& IRF_NO_WAIT
)) && !end_of_read_data(req
)) {
3176 LeaveCriticalSection(&req
->read_section
);
3177 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3178 EnterCriticalSection( &req
->read_section
);
3179 notify_received
= TRUE
;
3181 while(read
< size
) {
3182 res
= HTTPREQ_Read(req
, (char*)buf
+read
, size
-read
, &cread
, allow_blocking
);
3184 if (res
!= ERROR_SUCCESS
|| !cread
)
3189 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3190 hdr
->dwError
= ERROR_SUCCESS
;
3192 error
= hdr
->dwError
;
3194 LeaveCriticalSection( &req
->read_section
);
3196 res
= WSAEWOULDBLOCK
;
3199 if(res
== WSAEWOULDBLOCK
) {
3200 if(!(flags
& IRF_NO_WAIT
))
3201 return async_read(req
, buf
, size
, read
, ret_read
);
3203 return async_read(req
, NULL
, 0, 0, NULL
);
3204 res
= ERROR_SUCCESS
;
3208 if (res
!= ERROR_SUCCESS
)
3212 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3213 &read
, sizeof(read
));
3217 static DWORD
HTTPREQ_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
3220 http_request_t
*request
= (http_request_t
*)hdr
;
3222 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
3225 res
= NETCON_send(request
->netconn
, buffer
, size
, 0, (LPINT
)written
);
3226 if (res
== ERROR_SUCCESS
)
3227 request
->bytesWritten
+= *written
;
3229 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_SENT
, written
, sizeof(DWORD
));
3233 static DWORD
HTTPREQ_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
3235 http_request_t
*req
= (http_request_t
*)hdr
;
3236 DWORD res
= ERROR_SUCCESS
, avail
= 0, error
= ERROR_SUCCESS
;
3237 BOOL allow_blocking
, notify_received
= FALSE
;
3239 TRACE("(%p %p %x %lx)\n", req
, available
, flags
, ctx
);
3241 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
3242 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
3245 allow_blocking
= !(req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
);
3247 if(allow_blocking
|| TryEnterCriticalSection(&req
->read_section
)) {
3249 EnterCriticalSection(&req
->read_section
);
3250 if(hdr
->dwError
== ERROR_SUCCESS
)
3251 hdr
->dwError
= INTERNET_HANDLE_IN_USE
;
3252 else if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3253 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3255 avail
= req
->read_size
;
3257 if(!avail
&& !end_of_read_data(req
)) {
3258 LeaveCriticalSection(&req
->read_section
);
3259 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3260 EnterCriticalSection( &req
->read_section
);
3261 notify_received
= TRUE
;
3263 res
= refill_read_buffer(req
, allow_blocking
, &avail
);
3266 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3267 hdr
->dwError
= ERROR_SUCCESS
;
3269 error
= hdr
->dwError
;
3271 LeaveCriticalSection( &req
->read_section
);
3273 res
= WSAEWOULDBLOCK
;
3276 if(res
== WSAEWOULDBLOCK
)
3277 return async_read(req
, NULL
, 0, 0, available
);
3279 if (res
!= ERROR_SUCCESS
)
3284 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3285 &avail
, sizeof(avail
));
3289 static DWORD
HTTPREQ_LockRequestFile(object_header_t
*hdr
, req_file_t
**ret
)
3291 http_request_t
*req
= (http_request_t
*)hdr
;
3293 TRACE("(%p)\n", req
);
3295 if(!req
->req_file
) {
3296 WARN("No cache file name available\n");
3297 return ERROR_FILE_NOT_FOUND
;
3300 *ret
= req_file_addref(req
->req_file
);
3301 return ERROR_SUCCESS
;
3304 static const object_vtbl_t HTTPREQVtbl
= {
3306 HTTPREQ_CloseConnection
,
3307 HTTPREQ_QueryOption
,
3311 HTTPREQ_QueryDataAvailable
,
3313 HTTPREQ_LockRequestFile
3316 /***********************************************************************
3317 * HTTP_HttpOpenRequestW (internal)
3319 * Open a HTTP request handle
3322 * HINTERNET a HTTP request handle on success
3326 static DWORD
HTTP_HttpOpenRequestW(http_session_t
*session
,
3327 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3328 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3329 DWORD dwFlags
, DWORD_PTR dwContext
, HINTERNET
*ret
)
3331 appinfo_t
*hIC
= session
->appInfo
;
3332 http_request_t
*request
;
3337 request
= alloc_object(&session
->hdr
, &HTTPREQVtbl
, sizeof(http_request_t
));
3339 return ERROR_OUTOFMEMORY
;
3341 request
->hdr
.htype
= WH_HHTTPREQ
;
3342 request
->hdr
.dwFlags
= dwFlags
;
3343 request
->hdr
.dwContext
= dwContext
;
3344 request
->hdr
.decoding
= session
->hdr
.decoding
;
3345 request
->contentLength
= ~0;
3347 request
->netconn_stream
.data_stream
.vtbl
= &netconn_stream_vtbl
;
3348 request
->data_stream
= &request
->netconn_stream
.data_stream
;
3349 request
->connect_timeout
= session
->connect_timeout
;
3350 request
->send_timeout
= session
->send_timeout
;
3351 request
->receive_timeout
= session
->receive_timeout
;
3353 InitializeCriticalSection( &request
->headers_section
);
3354 request
->headers_section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": http_request_t.headers_section");
3356 InitializeCriticalSection( &request
->read_section
);
3357 request
->read_section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": http_request_t.read_section");
3359 WININET_AddRef( &session
->hdr
);
3360 request
->session
= session
;
3361 list_add_head( &session
->hdr
.children
, &request
->hdr
.entry
);
3363 port
= session
->hostPort
;
3364 if (port
== INTERNET_INVALID_PORT_NUMBER
)
3365 port
= (session
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ?
3366 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
3368 request
->server
= get_server(substrz(session
->hostName
), port
, (dwFlags
& INTERNET_FLAG_SECURE
) != 0, TRUE
);
3369 if(!request
->server
) {
3370 WININET_Release(&request
->hdr
);
3371 return ERROR_OUTOFMEMORY
;
3374 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_CN_INVALID
)
3375 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
;
3376 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
)
3377 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
;
3379 if (lpszObjectName
&& *lpszObjectName
) {
3384 rc
= UrlCanonicalizeW(lpszObjectName
, &dummy
, &len
, URL_ESCAPE_SPACES_ONLY
);
3385 if (rc
!= E_POINTER
)
3386 len
= lstrlenW(lpszObjectName
)+1;
3387 request
->path
= heap_alloc(len
*sizeof(WCHAR
));
3388 rc
= UrlCanonicalizeW(lpszObjectName
, request
->path
, &len
,
3389 URL_ESCAPE_SPACES_ONLY
);
3392 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName
),rc
);
3393 lstrcpyW(request
->path
,lpszObjectName
);
3396 request
->path
= heap_strdupW(L
"/");
3399 if (lpszReferrer
&& *lpszReferrer
)
3400 HTTP_ProcessHeader(request
, L
"Referer", lpszReferrer
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
3402 if (lpszAcceptTypes
)
3405 for (i
= 0; lpszAcceptTypes
[i
]; i
++)
3407 if (!*lpszAcceptTypes
[i
]) continue;
3408 HTTP_ProcessHeader(request
, L
"Accept", lpszAcceptTypes
[i
],
3409 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|
3410 HTTP_ADDHDR_FLAG_REQ
|
3411 (i
== 0 ? (HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
) : 0));
3415 request
->verb
= heap_strdupW(lpszVerb
&& *lpszVerb
? lpszVerb
: L
"GET");
3416 request
->version
= heap_strdupW(lpszVersion
&& *lpszVersion
? lpszVersion
: L
"HTTP/1.1");
3418 if (hIC
->proxy
&& hIC
->proxy
[0] && !HTTP_ShouldBypassProxy(hIC
, session
->hostName
))
3419 HTTP_DealWithProxy( hIC
, session
, request
);
3421 INTERNET_SendCallback(&session
->hdr
, dwContext
,
3422 INTERNET_STATUS_HANDLE_CREATED
, &request
->hdr
.hInternet
,
3425 TRACE("<-- (%p)\n", request
);
3427 *ret
= request
->hdr
.hInternet
;
3428 return ERROR_SUCCESS
;
3431 /***********************************************************************
3432 * HttpOpenRequestW (WININET.@)
3434 * Open a HTTP request handle
3437 * HINTERNET a HTTP request handle on success
3441 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
3442 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3443 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3444 DWORD dwFlags
, DWORD_PTR dwContext
)
3446 http_session_t
*session
;
3447 HINTERNET handle
= NULL
;
3450 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
3451 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
3452 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
3453 dwFlags
, dwContext
);
3454 if(lpszAcceptTypes
!=NULL
)
3457 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
3458 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
3461 session
= (http_session_t
*) get_handle_object( hHttpSession
);
3462 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
3464 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3469 * My tests seem to show that the windows version does not
3470 * become asynchronous until after this point. And anyhow
3471 * if this call was asynchronous then how would you get the
3472 * necessary HINTERNET pointer returned by this function.
3475 res
= HTTP_HttpOpenRequestW(session
, lpszVerb
, lpszObjectName
,
3476 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
3477 dwFlags
, dwContext
, &handle
);
3480 WININET_Release( &session
->hdr
);
3481 TRACE("returning %p\n", handle
);
3482 if(res
!= ERROR_SUCCESS
)
3487 static const LPCWSTR header_lookup
[] = {
3488 L
"Mime-Version", /* HTTP_QUERY_MIME_VERSION = 0 */
3489 L
"Content-Type", /* HTTP_QUERY_CONTENT_TYPE = 1 */
3490 L
"Content-Transfer-Encoding", /* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3491 L
"Content-ID", /* HTTP_QUERY_CONTENT_ID = 3 */
3492 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3493 L
"Content-Length", /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3494 L
"Content-Language", /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3495 L
"Allow", /* HTTP_QUERY_ALLOW = 7 */
3496 L
"Public", /* HTTP_QUERY_PUBLIC = 8 */
3497 L
"Date", /* HTTP_QUERY_DATE = 9 */
3498 L
"Expires", /* HTTP_QUERY_EXPIRES = 10 */
3499 L
"Last-Modified", /* HTTP_QUERY_LAST_MODIFIED = 11 */
3500 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
3501 L
"URI", /* HTTP_QUERY_URI = 13 */
3502 L
"From", /* HTTP_QUERY_DERIVED_FROM = 14 */
3503 NULL
, /* HTTP_QUERY_COST = 15 */
3504 NULL
, /* HTTP_QUERY_LINK = 16 */
3505 L
"Pragma", /* HTTP_QUERY_PRAGMA = 17 */
3506 NULL
, /* HTTP_QUERY_VERSION = 18 */
3507 L
"Status", /* HTTP_QUERY_STATUS_CODE = 19 */
3508 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
3509 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
3510 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3511 L
"Connection", /* HTTP_QUERY_CONNECTION = 23 */
3512 L
"Accept", /* HTTP_QUERY_ACCEPT = 24 */
3513 L
"Accept-Charset", /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3514 L
"Accept-Encoding", /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3515 L
"Accept-Language", /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3516 L
"Authorization", /* HTTP_QUERY_AUTHORIZATION = 28 */
3517 L
"Content-Encoding", /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3518 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
3519 NULL
, /* HTTP_QUERY_FROM = 31 */
3520 L
"If-Modified-Since", /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3521 L
"Location", /* HTTP_QUERY_LOCATION = 33 */
3522 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
3523 L
"Referer", /* HTTP_QUERY_REFERER = 35 */
3524 L
"Retry-After", /* HTTP_QUERY_RETRY_AFTER = 36 */
3525 L
"Server", /* HTTP_QUERY_SERVER = 37 */
3526 NULL
, /* HTTP_TITLE = 38 */
3527 L
"User-Agent", /* HTTP_QUERY_USER_AGENT = 39 */
3528 L
"WWW-Authenticate", /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3529 L
"Proxy-Authenticate", /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3530 L
"Accept-Ranges", /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3531 L
"Set-Cookie", /* HTTP_QUERY_SET_COOKIE = 43 */
3532 L
"Cookie", /* HTTP_QUERY_COOKIE = 44 */
3533 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3534 NULL
, /* HTTP_QUERY_REFRESH = 46 */
3535 L
"Content-Disposition", /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3536 L
"Age", /* HTTP_QUERY_AGE = 48 */
3537 L
"Cache-Control", /* HTTP_QUERY_CACHE_CONTROL = 49 */
3538 L
"Content-Base", /* HTTP_QUERY_CONTENT_BASE = 50 */
3539 L
"Content-Location", /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3540 L
"Content-MD5", /* HTTP_QUERY_CONTENT_MD5 = 52 */
3541 L
"Content-Range", /* HTTP_QUERY_CONTENT_RANGE = 53 */
3542 L
"ETag", /* HTTP_QUERY_ETAG = 54 */
3543 L
"Host", /* HTTP_QUERY_HOST = 55 */
3544 L
"If-Match", /* HTTP_QUERY_IF_MATCH = 56 */
3545 L
"If-None-Match", /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3546 L
"If-Range", /* HTTP_QUERY_IF_RANGE = 58 */
3547 L
"If-Unmodified-Since", /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3548 L
"Max-Forwards", /* HTTP_QUERY_MAX_FORWARDS = 60 */
3549 L
"Proxy-Authorization", /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3550 L
"Range", /* HTTP_QUERY_RANGE = 62 */
3551 L
"Transfer-Encoding", /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3552 L
"Upgrade", /* HTTP_QUERY_UPGRADE = 64 */
3553 L
"Vary", /* HTTP_QUERY_VARY = 65 */
3554 L
"Via", /* HTTP_QUERY_VIA = 66 */
3555 L
"Warning", /* HTTP_QUERY_WARNING = 67 */
3556 L
"Expect", /* HTTP_QUERY_EXPECT = 68 */
3557 L
"Proxy-Connection", /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3558 L
"Unless-Modified-Since", /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3561 /***********************************************************************
3562 * HTTP_HttpQueryInfoW (internal)
3564 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*request
, DWORD dwInfoLevel
,
3565 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3567 LPHTTPHEADERW lphttpHdr
= NULL
;
3568 BOOL request_only
= !!(dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
);
3569 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
3570 DWORD level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
3573 EnterCriticalSection( &request
->headers_section
);
3575 /* Find requested header structure */
3578 case HTTP_QUERY_CUSTOM
:
3581 LeaveCriticalSection( &request
->headers_section
);
3582 return ERROR_INVALID_PARAMETER
;
3584 index
= HTTP_GetCustomHeaderIndex(request
, lpBuffer
, requested_index
, request_only
);
3586 case HTTP_QUERY_RAW_HEADERS_CRLF
:
3590 DWORD res
= ERROR_INVALID_PARAMETER
;
3593 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
3595 headers
= build_response_header(request
, TRUE
);
3598 LeaveCriticalSection( &request
->headers_section
);
3599 return ERROR_OUTOFMEMORY
;
3602 len
= lstrlenW(headers
) * sizeof(WCHAR
);
3603 if (len
+ sizeof(WCHAR
) > *lpdwBufferLength
)
3605 len
+= sizeof(WCHAR
);
3606 res
= ERROR_INSUFFICIENT_BUFFER
;
3610 memcpy(lpBuffer
, headers
, len
+ sizeof(WCHAR
));
3611 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
/ sizeof(WCHAR
)));
3612 res
= ERROR_SUCCESS
;
3614 *lpdwBufferLength
= len
;
3617 LeaveCriticalSection( &request
->headers_section
);
3620 case HTTP_QUERY_RAW_HEADERS
:
3626 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, FALSE
);
3628 headers
= build_response_header(request
, FALSE
);
3632 LeaveCriticalSection( &request
->headers_section
);
3633 return ERROR_OUTOFMEMORY
;
3636 len
= lstrlenW(headers
) * sizeof(WCHAR
);
3637 if (len
> *lpdwBufferLength
)
3639 *lpdwBufferLength
= len
;
3641 LeaveCriticalSection( &request
->headers_section
);
3642 return ERROR_INSUFFICIENT_BUFFER
;
3649 TRACE("returning data: %s\n", debugstr_wn(headers
, len
/ sizeof(WCHAR
)));
3651 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
3653 if (headers
[i
] == '\n')
3656 memcpy(lpBuffer
, headers
, len
);
3658 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3661 LeaveCriticalSection( &request
->headers_section
);
3662 return ERROR_SUCCESS
;
3664 case HTTP_QUERY_STATUS_TEXT
:
3665 if (request
->statusText
)
3667 DWORD len
= lstrlenW(request
->statusText
);
3668 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3670 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3671 LeaveCriticalSection( &request
->headers_section
);
3672 return ERROR_INSUFFICIENT_BUFFER
;
3676 memcpy(lpBuffer
, request
->statusText
, (len
+ 1) * sizeof(WCHAR
));
3677 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3679 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3680 LeaveCriticalSection( &request
->headers_section
);
3681 return ERROR_SUCCESS
;
3684 case HTTP_QUERY_VERSION
:
3685 if (request
->version
)
3687 DWORD len
= lstrlenW(request
->version
);
3688 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3690 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3691 LeaveCriticalSection( &request
->headers_section
);
3692 return ERROR_INSUFFICIENT_BUFFER
;
3696 memcpy(lpBuffer
, request
->version
, (len
+ 1) * sizeof(WCHAR
));
3697 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3699 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3700 LeaveCriticalSection( &request
->headers_section
);
3701 return ERROR_SUCCESS
;
3704 case HTTP_QUERY_CONTENT_ENCODING
:
3705 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[request
->read_gzip
? HTTP_QUERY_CONTENT_TYPE
: level
],
3706 requested_index
,request_only
);
3708 case HTTP_QUERY_STATUS_CODE
: {
3709 DWORD res
= ERROR_SUCCESS
;
3713 LeaveCriticalSection( &request
->headers_section
);
3714 return ERROR_HTTP_INVALID_QUERY_REQUEST
;
3720 if(dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) {
3721 if(*lpdwBufferLength
>= sizeof(DWORD
))
3722 *(DWORD
*)lpBuffer
= request
->status_code
;
3724 res
= ERROR_INSUFFICIENT_BUFFER
;
3725 *lpdwBufferLength
= sizeof(DWORD
);
3730 size
= swprintf(buf
, ARRAY_SIZE(buf
), L
"%u", request
->status_code
) * sizeof(WCHAR
);
3732 if(size
<= *lpdwBufferLength
) {
3733 memcpy(lpBuffer
, buf
, size
+sizeof(WCHAR
));
3735 size
+= sizeof(WCHAR
);
3736 res
= ERROR_INSUFFICIENT_BUFFER
;
3739 *lpdwBufferLength
= size
;
3741 LeaveCriticalSection( &request
->headers_section
);
3745 assert (ARRAY_SIZE(header_lookup
) == (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
3747 if (level
< ARRAY_SIZE(header_lookup
) && header_lookup
[level
])
3748 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[level
],
3749 requested_index
,request_only
);
3753 lphttpHdr
= &request
->custHeaders
[index
];
3755 /* Ensure header satisfies requested attributes */
3757 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
3758 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
3760 LeaveCriticalSection( &request
->headers_section
);
3761 return ERROR_HTTP_HEADER_NOT_FOUND
;
3764 /* coalesce value to requested type */
3765 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
&& lpBuffer
)
3767 unsigned long value
;
3769 if (*lpdwBufferLength
!= sizeof(DWORD
))
3771 LeaveCriticalSection( &request
->headers_section
);
3772 return ERROR_HTTP_INVALID_HEADER
;
3776 value
= wcstoul( lphttpHdr
->lpszValue
, NULL
, 10 );
3777 if (value
> UINT_MAX
|| (value
== ULONG_MAX
&& errno
== ERANGE
))
3779 LeaveCriticalSection( &request
->headers_section
);
3780 return ERROR_HTTP_INVALID_HEADER
;
3783 *(DWORD
*)lpBuffer
= value
;
3784 TRACE(" returning number: %u\n", *(DWORD
*)lpBuffer
);
3786 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
&& lpBuffer
)
3792 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
3794 tmpTM
= *gmtime(&tmpTime
);
3795 STHook
= (SYSTEMTIME
*)lpBuffer
;
3796 STHook
->wDay
= tmpTM
.tm_mday
;
3797 STHook
->wHour
= tmpTM
.tm_hour
;
3798 STHook
->wMilliseconds
= 0;
3799 STHook
->wMinute
= tmpTM
.tm_min
;
3800 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
3801 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
3802 STHook
->wSecond
= tmpTM
.tm_sec
;
3803 STHook
->wYear
= 1900+tmpTM
.tm_year
;
3805 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3806 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
3807 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
3809 else if (lphttpHdr
->lpszValue
)
3811 DWORD len
= (lstrlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
3813 if (len
> *lpdwBufferLength
)
3815 *lpdwBufferLength
= len
;
3816 LeaveCriticalSection( &request
->headers_section
);
3817 return ERROR_INSUFFICIENT_BUFFER
;
3821 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
3822 TRACE("! returning string: %s\n", debugstr_w(lpBuffer
));
3824 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3826 if (lpdwIndex
) (*lpdwIndex
)++;
3828 LeaveCriticalSection( &request
->headers_section
);
3829 return ERROR_SUCCESS
;
3832 /***********************************************************************
3833 * HttpQueryInfoW (WININET.@)
3835 * Queries for information about an HTTP request
3842 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3843 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3845 http_request_t
*request
;
3848 if (TRACE_ON(wininet
)) {
3849 #define FE(x) { x, #x }
3850 static const wininet_flag_info query_flags
[] = {
3851 FE(HTTP_QUERY_MIME_VERSION
),
3852 FE(HTTP_QUERY_CONTENT_TYPE
),
3853 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
3854 FE(HTTP_QUERY_CONTENT_ID
),
3855 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
3856 FE(HTTP_QUERY_CONTENT_LENGTH
),
3857 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
3858 FE(HTTP_QUERY_ALLOW
),
3859 FE(HTTP_QUERY_PUBLIC
),
3860 FE(HTTP_QUERY_DATE
),
3861 FE(HTTP_QUERY_EXPIRES
),
3862 FE(HTTP_QUERY_LAST_MODIFIED
),
3863 FE(HTTP_QUERY_MESSAGE_ID
),
3865 FE(HTTP_QUERY_DERIVED_FROM
),
3866 FE(HTTP_QUERY_COST
),
3867 FE(HTTP_QUERY_LINK
),
3868 FE(HTTP_QUERY_PRAGMA
),
3869 FE(HTTP_QUERY_VERSION
),
3870 FE(HTTP_QUERY_STATUS_CODE
),
3871 FE(HTTP_QUERY_STATUS_TEXT
),
3872 FE(HTTP_QUERY_RAW_HEADERS
),
3873 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
3874 FE(HTTP_QUERY_CONNECTION
),
3875 FE(HTTP_QUERY_ACCEPT
),
3876 FE(HTTP_QUERY_ACCEPT_CHARSET
),
3877 FE(HTTP_QUERY_ACCEPT_ENCODING
),
3878 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
3879 FE(HTTP_QUERY_AUTHORIZATION
),
3880 FE(HTTP_QUERY_CONTENT_ENCODING
),
3881 FE(HTTP_QUERY_FORWARDED
),
3882 FE(HTTP_QUERY_FROM
),
3883 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
3884 FE(HTTP_QUERY_LOCATION
),
3885 FE(HTTP_QUERY_ORIG_URI
),
3886 FE(HTTP_QUERY_REFERER
),
3887 FE(HTTP_QUERY_RETRY_AFTER
),
3888 FE(HTTP_QUERY_SERVER
),
3889 FE(HTTP_QUERY_TITLE
),
3890 FE(HTTP_QUERY_USER_AGENT
),
3891 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
3892 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
3893 FE(HTTP_QUERY_ACCEPT_RANGES
),
3894 FE(HTTP_QUERY_SET_COOKIE
),
3895 FE(HTTP_QUERY_COOKIE
),
3896 FE(HTTP_QUERY_REQUEST_METHOD
),
3897 FE(HTTP_QUERY_REFRESH
),
3898 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
3900 FE(HTTP_QUERY_CACHE_CONTROL
),
3901 FE(HTTP_QUERY_CONTENT_BASE
),
3902 FE(HTTP_QUERY_CONTENT_LOCATION
),
3903 FE(HTTP_QUERY_CONTENT_MD5
),
3904 FE(HTTP_QUERY_CONTENT_RANGE
),
3905 FE(HTTP_QUERY_ETAG
),
3906 FE(HTTP_QUERY_HOST
),
3907 FE(HTTP_QUERY_IF_MATCH
),
3908 FE(HTTP_QUERY_IF_NONE_MATCH
),
3909 FE(HTTP_QUERY_IF_RANGE
),
3910 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
3911 FE(HTTP_QUERY_MAX_FORWARDS
),
3912 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
3913 FE(HTTP_QUERY_RANGE
),
3914 FE(HTTP_QUERY_TRANSFER_ENCODING
),
3915 FE(HTTP_QUERY_UPGRADE
),
3916 FE(HTTP_QUERY_VARY
),
3918 FE(HTTP_QUERY_WARNING
),
3919 FE(HTTP_QUERY_CUSTOM
)
3921 static const wininet_flag_info modifier_flags
[] = {
3922 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
3923 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
3924 FE(HTTP_QUERY_FLAG_NUMBER
),
3925 FE(HTTP_QUERY_FLAG_COALESCE
)
3928 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
3929 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
3932 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, info
);
3933 TRACE(" Attribute:");
3934 for (i
= 0; i
< ARRAY_SIZE(query_flags
); i
++) {
3935 if (query_flags
[i
].val
== info
) {
3936 TRACE(" %s", query_flags
[i
].name
);
3940 if (i
== ARRAY_SIZE(query_flags
)) {
3941 TRACE(" Unknown (%08x)", info
);
3944 TRACE(" Modifier:");
3945 for (i
= 0; i
< ARRAY_SIZE(modifier_flags
); i
++) {
3946 if (modifier_flags
[i
].val
& info_mod
) {
3947 TRACE(" %s", modifier_flags
[i
].name
);
3948 info_mod
&= ~ modifier_flags
[i
].val
;
3953 TRACE(" Unknown (%08x)", info_mod
);
3958 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
3959 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
3961 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3965 if (lpBuffer
== NULL
)
3966 *lpdwBufferLength
= 0;
3967 res
= HTTP_HttpQueryInfoW( request
, dwInfoLevel
,
3968 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
3972 WININET_Release( &request
->hdr
);
3974 TRACE("%u <--\n", res
);
3977 return res
== ERROR_SUCCESS
;
3980 /***********************************************************************
3981 * HttpQueryInfoA (WININET.@)
3983 * Queries for information about an HTTP request
3990 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3991 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3997 TRACE("%p %x\n", hHttpRequest
, dwInfoLevel
);
3999 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
4000 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
4002 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
4003 lpdwBufferLength
, lpdwIndex
);
4009 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
4010 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
4012 alloclen
= MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, NULL
, 0 ) * sizeof(WCHAR
);
4018 bufferW
= heap_alloc(alloclen
);
4019 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
4020 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
4021 MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, bufferW
, alloclen
/ sizeof(WCHAR
) );
4028 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
4032 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
4033 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
4034 *lpdwBufferLength
= len
- 1;
4036 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
4039 /* since the strings being returned from HttpQueryInfoW should be
4040 * only ASCII characters, it is reasonable to assume that all of
4041 * the Unicode characters can be reduced to a single byte */
4042 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
4044 heap_free( bufferW
);
4048 static WCHAR
*get_redirect_url(http_request_t
*request
)
4050 static WCHAR szHttp
[] = L
"http";
4051 static WCHAR szHttps
[] = L
"https";
4052 http_session_t
*session
= request
->session
;
4053 URL_COMPONENTSW urlComponents
= { sizeof(urlComponents
) };
4054 WCHAR
*orig_url
= NULL
, *redirect_url
= NULL
, *combined_url
= NULL
;
4055 DWORD url_length
= 0, res
;
4059 res
= HTTP_HttpQueryInfoW(request
, HTTP_QUERY_LOCATION
, redirect_url
, &url_length
, NULL
);
4060 if(res
== ERROR_INSUFFICIENT_BUFFER
) {
4061 redirect_url
= heap_alloc(url_length
);
4062 res
= HTTP_HttpQueryInfoW(request
, HTTP_QUERY_LOCATION
, redirect_url
, &url_length
, NULL
);
4064 if(res
!= ERROR_SUCCESS
) {
4065 heap_free(redirect_url
);
4069 urlComponents
.dwSchemeLength
= 1;
4070 b
= InternetCrackUrlW(redirect_url
, url_length
/ sizeof(WCHAR
), 0, &urlComponents
);
4071 if(b
&& urlComponents
.dwSchemeLength
&&
4072 urlComponents
.nScheme
!= INTERNET_SCHEME_HTTP
&& urlComponents
.nScheme
!= INTERNET_SCHEME_HTTPS
) {
4073 TRACE("redirect to non-http URL\n");
4077 urlComponents
.lpszScheme
= (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
4078 urlComponents
.dwSchemeLength
= 0;
4079 urlComponents
.lpszHostName
= request
->server
->name
;
4080 urlComponents
.nPort
= request
->server
->port
;
4081 urlComponents
.lpszUserName
= session
->userName
;
4082 urlComponents
.lpszUrlPath
= request
->path
;
4084 b
= InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
);
4085 if(!b
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
4086 orig_url
= heap_alloc(url_length
);
4088 /* convert from bytes to characters */
4089 url_length
= url_length
/ sizeof(WCHAR
) - 1;
4090 b
= InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
);
4095 b
= InternetCombineUrlW(orig_url
, redirect_url
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
);
4096 if(!b
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
4097 combined_url
= heap_alloc(url_length
* sizeof(WCHAR
));
4098 b
= InternetCombineUrlW(orig_url
, redirect_url
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
);
4100 heap_free(combined_url
);
4101 combined_url
= NULL
;
4106 heap_free(orig_url
);
4107 heap_free(redirect_url
);
4108 return combined_url
;
4112 /***********************************************************************
4113 * HTTP_HandleRedirect (internal)
4115 static DWORD
HTTP_HandleRedirect(http_request_t
*request
, WCHAR
*url
)
4117 URL_COMPONENTSW urlComponents
= { sizeof(urlComponents
) };
4118 http_session_t
*session
= request
->session
;
4119 size_t url_len
= lstrlenW(url
);
4123 /* if it's an absolute path, keep the same session info */
4124 urlComponents
.lpszUrlPath
= url
;
4125 urlComponents
.dwUrlPathLength
= url_len
;
4129 urlComponents
.dwHostNameLength
= 1;
4130 urlComponents
.dwUserNameLength
= 1;
4131 urlComponents
.dwUrlPathLength
= 1;
4132 if(!InternetCrackUrlW(url
, url_len
, 0, &urlComponents
))
4133 return INTERNET_GetLastError();
4135 if(!urlComponents
.dwHostNameLength
)
4136 return ERROR_INTERNET_INVALID_URL
;
4139 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
4140 url
, (url_len
+ 1) * sizeof(WCHAR
));
4142 if(urlComponents
.dwHostNameLength
) {
4143 BOOL custom_port
= FALSE
;
4146 if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
) {
4147 if(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) {
4148 TRACE("redirect from secure page to non-secure page\n");
4149 /* FIXME: warn about from secure redirect to non-secure page */
4150 request
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
4153 custom_port
= urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
;
4154 }else if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) {
4155 if(!(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)) {
4156 TRACE("redirect from non-secure page to secure page\n");
4157 /* FIXME: notify about redirect to secure page */
4158 request
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
4161 custom_port
= urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
;
4164 heap_free(session
->hostName
);
4166 session
->hostName
= heap_strndupW(urlComponents
.lpszHostName
, urlComponents
.dwHostNameLength
);
4167 session
->hostPort
= urlComponents
.nPort
;
4169 heap_free(session
->userName
);
4170 session
->userName
= NULL
;
4171 if (urlComponents
.dwUserNameLength
)
4172 session
->userName
= heap_strndupW(urlComponents
.lpszUserName
, urlComponents
.dwUserNameLength
);
4174 reset_data_stream(request
);
4176 host
= substr(urlComponents
.lpszHostName
, urlComponents
.dwHostNameLength
);
4178 if(host
.len
!= lstrlenW(request
->server
->name
) || wcsnicmp(request
->server
->name
, host
.str
, host
.len
)
4179 || request
->server
->port
!= urlComponents
.nPort
) {
4180 server_t
*new_server
;
4182 new_server
= get_server(host
, urlComponents
.nPort
, urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
, TRUE
);
4183 server_release(request
->server
);
4184 request
->server
= new_server
;
4188 HTTP_ProcessHeader(request
, L
"Host", request
->server
->host_port
,
4189 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
4191 HTTP_ProcessHeader(request
, L
"Host", request
->server
->name
,
4192 HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
4195 heap_free(request
->path
);
4196 request
->path
= NULL
;
4197 if(urlComponents
.dwUrlPathLength
)
4201 WCHAR dummy
[] = L
"";
4204 path
= heap_strndupW(urlComponents
.lpszUrlPath
, urlComponents
.dwUrlPathLength
);
4205 rc
= UrlEscapeW(path
, dummy
, &needed
, URL_ESCAPE_SPACES_ONLY
);
4206 if (rc
!= E_POINTER
)
4207 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
4208 request
->path
= heap_alloc(needed
*sizeof(WCHAR
));
4209 rc
= UrlEscapeW(path
, request
->path
, &needed
,
4210 URL_ESCAPE_SPACES_ONLY
);
4213 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
4214 lstrcpyW(request
->path
, path
);
4219 /* Remove custom content-type/length headers on redirects. */
4220 remove_header(request
, L
"Content-Type", TRUE
);
4221 remove_header(request
, L
"Content-Length", TRUE
);
4223 return ERROR_SUCCESS
;
4226 /***********************************************************************
4227 * HTTP_build_req (internal)
4229 * concatenate all the strings in the request together
4231 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
4236 for( t
= list
; *t
; t
++ )
4237 len
+= lstrlenW( *t
);
4240 str
= heap_alloc(len
*sizeof(WCHAR
));
4243 for( t
= list
; *t
; t
++ )
4244 lstrcatW( str
, *t
);
4249 static void HTTP_InsertCookies(http_request_t
*request
)
4254 res
= get_cookie_header(request
->server
->name
, request
->path
, &cookies
);
4255 if(res
!= ERROR_SUCCESS
|| !cookies
)
4258 HTTP_HttpAddRequestHeadersW(request
, cookies
, lstrlenW(cookies
),
4259 HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
4263 static WORD
HTTP_ParseWkday(LPCWSTR day
)
4265 static const WCHAR days
[7][4] = {L
"sun",
4273 for (i
= 0; i
< ARRAY_SIZE(days
); i
++)
4274 if (!wcsicmp(day
, days
[i
]))
4281 static WORD
HTTP_ParseMonth(LPCWSTR month
)
4283 if (!wcsicmp(month
, L
"jan")) return 1;
4284 if (!wcsicmp(month
, L
"feb")) return 2;
4285 if (!wcsicmp(month
, L
"mar")) return 3;
4286 if (!wcsicmp(month
, L
"apr")) return 4;
4287 if (!wcsicmp(month
, L
"may")) return 5;
4288 if (!wcsicmp(month
, L
"jun")) return 6;
4289 if (!wcsicmp(month
, L
"jul")) return 7;
4290 if (!wcsicmp(month
, L
"aug")) return 8;
4291 if (!wcsicmp(month
, L
"sep")) return 9;
4292 if (!wcsicmp(month
, L
"oct")) return 10;
4293 if (!wcsicmp(month
, L
"nov")) return 11;
4294 if (!wcsicmp(month
, L
"dec")) return 12;
4299 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4300 * optionally preceded by whitespace.
4301 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4302 * st, and sets *str to the first character after the time format.
4304 static BOOL
HTTP_ParseTime(SYSTEMTIME
*st
, LPCWSTR
*str
)
4310 while (iswspace(*ptr
))
4313 num
= wcstoul(ptr
, &nextPtr
, 10);
4314 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4316 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4321 ERR("unexpected hour in time format %s\n", debugstr_w(ptr
));
4325 st
->wHour
= (WORD
)num
;
4326 num
= wcstoul(ptr
, &nextPtr
, 10);
4327 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4329 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4334 ERR("unexpected minute in time format %s\n", debugstr_w(ptr
));
4338 st
->wMinute
= (WORD
)num
;
4339 num
= wcstoul(ptr
, &nextPtr
, 10);
4340 if (!nextPtr
|| nextPtr
<= ptr
)
4342 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4347 ERR("unexpected second in time format %s\n", debugstr_w(ptr
));
4351 st
->wSecond
= (WORD
)num
;
4355 static BOOL
HTTP_ParseDateAsAsctime(LPCWSTR value
, FILETIME
*ft
)
4357 WCHAR day
[4], *dayPtr
, month
[4], *monthPtr
, *nextPtr
;
4359 SYSTEMTIME st
= { 0 };
4362 for (ptr
= value
, dayPtr
= day
; *ptr
&& !iswspace(*ptr
) &&
4363 dayPtr
- day
< ARRAY_SIZE(day
) - 1; ptr
++, dayPtr
++)
4366 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4367 if (st
.wDayOfWeek
>= 7)
4369 ERR("unexpected weekday %s\n", debugstr_w(day
));
4373 while (iswspace(*ptr
))
4376 for (monthPtr
= month
; !iswspace(*ptr
) && monthPtr
- month
< ARRAY_SIZE(month
) - 1;
4380 st
.wMonth
= HTTP_ParseMonth(month
);
4381 if (!st
.wMonth
|| st
.wMonth
> 12)
4383 ERR("unexpected month %s\n", debugstr_w(month
));
4387 while (iswspace(*ptr
))
4390 num
= wcstoul(ptr
, &nextPtr
, 10);
4391 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4393 ERR("unexpected day %s\n", debugstr_w(ptr
));
4397 st
.wDay
= (WORD
)num
;
4399 while (iswspace(*ptr
))
4402 if (!HTTP_ParseTime(&st
, &ptr
))
4405 while (iswspace(*ptr
))
4408 num
= wcstoul(ptr
, &nextPtr
, 10);
4409 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4411 ERR("unexpected year %s\n", debugstr_w(ptr
));
4415 st
.wYear
= (WORD
)num
;
4417 while (iswspace(*ptr
))
4420 /* asctime() doesn't report a timezone, but some web servers do, so accept
4421 * with or without GMT.
4423 if (*ptr
&& wcscmp(ptr
, L
"GMT"))
4425 ERR("unexpected timezone %s\n", debugstr_w(ptr
));
4428 return SystemTimeToFileTime(&st
, ft
);
4431 static BOOL
HTTP_ParseRfc1123Date(LPCWSTR value
, FILETIME
*ft
)
4433 WCHAR
*nextPtr
, day
[4], month
[4], *monthPtr
;
4436 SYSTEMTIME st
= { 0 };
4438 ptr
= wcschr(value
, ',');
4441 if (ptr
- value
!= 3)
4443 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4446 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4448 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4449 if (st
.wDayOfWeek
> 6)
4451 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4456 while (iswspace(*ptr
))
4459 num
= wcstoul(ptr
, &nextPtr
, 10);
4460 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4462 WARN("unexpected day %s\n", debugstr_w(value
));
4466 st
.wDay
= (WORD
)num
;
4468 while (iswspace(*ptr
))
4471 for (monthPtr
= month
; !iswspace(*ptr
) && monthPtr
- month
< ARRAY_SIZE(month
) - 1;
4475 st
.wMonth
= HTTP_ParseMonth(month
);
4476 if (!st
.wMonth
|| st
.wMonth
> 12)
4478 WARN("unexpected month %s\n", debugstr_w(month
));
4482 while (iswspace(*ptr
))
4485 num
= wcstoul(ptr
, &nextPtr
, 10);
4486 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4488 ERR("unexpected year %s\n", debugstr_w(value
));
4492 st
.wYear
= (WORD
)num
;
4494 if (!HTTP_ParseTime(&st
, &ptr
))
4497 while (iswspace(*ptr
))
4500 if (wcscmp(ptr
, L
"GMT"))
4502 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4505 return SystemTimeToFileTime(&st
, ft
);
4508 static WORD
HTTP_ParseWeekday(LPCWSTR day
)
4510 static const WCHAR days
[7][10] = {L
"sunday",
4518 for (i
= 0; i
< ARRAY_SIZE(days
); i
++)
4519 if (!wcsicmp(day
, days
[i
]))
4526 static BOOL
HTTP_ParseRfc850Date(LPCWSTR value
, FILETIME
*ft
)
4528 WCHAR
*nextPtr
, day
[10], month
[4], *monthPtr
;
4531 SYSTEMTIME st
= { 0 };
4533 ptr
= wcschr(value
, ',');
4536 if (ptr
- value
== 3)
4538 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4540 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4541 if (st
.wDayOfWeek
> 6)
4543 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4547 else if (ptr
- value
< ARRAY_SIZE(day
))
4549 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4550 day
[ptr
- value
+ 1] = 0;
4551 st
.wDayOfWeek
= HTTP_ParseWeekday(day
);
4552 if (st
.wDayOfWeek
> 6)
4554 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4560 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4565 while (iswspace(*ptr
))
4568 num
= wcstoul(ptr
, &nextPtr
, 10);
4569 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4571 ERR("unexpected day %s\n", debugstr_w(value
));
4575 st
.wDay
= (WORD
)num
;
4579 ERR("unexpected month format %s\n", debugstr_w(ptr
));
4584 for (monthPtr
= month
; *ptr
!= '-' && monthPtr
- month
< ARRAY_SIZE(month
) - 1;
4588 st
.wMonth
= HTTP_ParseMonth(month
);
4589 if (!st
.wMonth
|| st
.wMonth
> 12)
4591 ERR("unexpected month %s\n", debugstr_w(month
));
4597 ERR("unexpected year format %s\n", debugstr_w(ptr
));
4602 num
= wcstoul(ptr
, &nextPtr
, 10);
4603 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4605 ERR("unexpected year %s\n", debugstr_w(value
));
4609 st
.wYear
= (WORD
)num
;
4611 if (!HTTP_ParseTime(&st
, &ptr
))
4614 while (iswspace(*ptr
))
4617 if (wcscmp(ptr
, L
"GMT"))
4619 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4622 return SystemTimeToFileTime(&st
, ft
);
4625 static BOOL
HTTP_ParseDate(LPCWSTR value
, FILETIME
*ft
)
4629 if (!wcscmp(value
, L
"0"))
4631 ft
->dwLowDateTime
= ft
->dwHighDateTime
= 0;
4634 else if (wcschr(value
, ','))
4636 ret
= HTTP_ParseRfc1123Date(value
, ft
);
4639 ret
= HTTP_ParseRfc850Date(value
, ft
);
4641 ERR("unexpected date format %s\n", debugstr_w(value
));
4646 ret
= HTTP_ParseDateAsAsctime(value
, ft
);
4648 ERR("unexpected date format %s\n", debugstr_w(value
));
4653 static void HTTP_ProcessExpires(http_request_t
*request
)
4655 BOOL expirationFound
= FALSE
;
4658 EnterCriticalSection( &request
->headers_section
);
4660 /* Look for a Cache-Control header with a max-age directive, as it takes
4661 * precedence over the Expires header.
4663 headerIndex
= HTTP_GetCustomHeaderIndex(request
, L
"Cache-Control", 0, FALSE
);
4664 if (headerIndex
!= -1)
4666 LPHTTPHEADERW ccHeader
= &request
->custHeaders
[headerIndex
];
4669 for (ptr
= ccHeader
->lpszValue
; ptr
&& *ptr
; )
4671 LPWSTR comma
= wcschr(ptr
, ','), end
, equal
;
4676 end
= ptr
+ lstrlenW(ptr
);
4677 for (equal
= end
- 1; equal
> ptr
&& *equal
!= '='; equal
--)
4681 if (!wcsnicmp(ptr
, L
"max-age", equal
- ptr
- 1))
4686 age
= wcstoul(equal
+ 1, &nextPtr
, 10);
4687 if (nextPtr
> equal
+ 1)
4691 NtQuerySystemTime( &ft
);
4692 /* Age is in seconds, FILETIME resolution is in
4693 * 100 nanosecond intervals.
4695 ft
.QuadPart
+= age
* (ULONGLONG
)1000000;
4696 request
->expires
.dwLowDateTime
= ft
.u
.LowPart
;
4697 request
->expires
.dwHighDateTime
= ft
.u
.HighPart
;
4698 expirationFound
= TRUE
;
4705 while (iswspace(*ptr
))
4712 if (!expirationFound
)
4714 headerIndex
= HTTP_GetCustomHeaderIndex(request
, L
"Expires", 0, FALSE
);
4715 if (headerIndex
!= -1)
4717 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4720 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4722 expirationFound
= TRUE
;
4723 request
->expires
= ft
;
4727 if (!expirationFound
)
4731 /* With no known age, default to 10 minutes until expiration. */
4732 NtQuerySystemTime( &t
);
4733 t
.QuadPart
+= 10 * 60 * (ULONGLONG
)10000000;
4734 request
->expires
.dwLowDateTime
= t
.u
.LowPart
;
4735 request
->expires
.dwHighDateTime
= t
.u
.HighPart
;
4738 LeaveCriticalSection( &request
->headers_section
);
4741 static void HTTP_ProcessLastModified(http_request_t
*request
)
4745 EnterCriticalSection( &request
->headers_section
);
4747 headerIndex
= HTTP_GetCustomHeaderIndex(request
, L
"Last-Modified", 0, FALSE
);
4748 if (headerIndex
!= -1)
4750 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4753 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4754 request
->last_modified
= ft
;
4757 LeaveCriticalSection( &request
->headers_section
);
4760 static void http_process_keep_alive(http_request_t
*req
)
4764 EnterCriticalSection( &req
->headers_section
);
4766 if ((index
= HTTP_GetCustomHeaderIndex(req
, L
"Connection", 0, FALSE
)) != -1)
4767 req
->netconn
->keep_alive
= !wcsicmp(req
->custHeaders
[index
].lpszValue
, L
"Keep-Alive");
4768 else if ((index
= HTTP_GetCustomHeaderIndex(req
, L
"Proxy-Connection", 0, FALSE
)) != -1)
4769 req
->netconn
->keep_alive
= !wcsicmp(req
->custHeaders
[index
].lpszValue
, L
"Keep-Alive");
4771 req
->netconn
->keep_alive
= !wcsicmp(req
->version
, L
"HTTP/1.1");
4773 LeaveCriticalSection( &req
->headers_section
);
4776 static DWORD
open_http_connection(http_request_t
*request
, BOOL
*reusing
)
4778 netconn_t
*netconn
= NULL
;
4781 if (request
->netconn
)
4783 if (NETCON_is_alive(request
->netconn
) && drain_content(request
, TRUE
) == ERROR_SUCCESS
)
4785 reset_data_stream(request
);
4787 return ERROR_SUCCESS
;
4790 TRACE("freeing netconn\n");
4791 free_netconn(request
->netconn
);
4792 request
->netconn
= NULL
;
4795 reset_data_stream(request
);
4797 res
= HTTP_ResolveName(request
);
4798 if(res
!= ERROR_SUCCESS
)
4801 EnterCriticalSection(&connection_pool_cs
);
4803 while(!list_empty(&request
->server
->conn_pool
)) {
4804 netconn
= LIST_ENTRY(list_head(&request
->server
->conn_pool
), netconn_t
, pool_entry
);
4805 list_remove(&netconn
->pool_entry
);
4807 if(is_valid_netconn(netconn
) && NETCON_is_alive(netconn
))
4810 TRACE("connection %p closed during idle\n", netconn
);
4811 free_netconn(netconn
);
4815 LeaveCriticalSection(&connection_pool_cs
);
4818 TRACE("<-- reusing %p netconn\n", netconn
);
4819 request
->netconn
= netconn
;
4821 return ERROR_SUCCESS
;
4824 TRACE("connecting to %s, proxy %s\n", debugstr_w(request
->server
->name
),
4825 request
->proxy
? debugstr_w(request
->proxy
->name
) : "(null)");
4827 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4828 INTERNET_STATUS_CONNECTING_TO_SERVER
,
4829 request
->server
->addr_str
,
4830 strlen(request
->server
->addr_str
)+1);
4832 res
= create_netconn(request
->proxy
? request
->proxy
: request
->server
, request
->security_flags
,
4833 (request
->hdr
.ErrorMask
& INTERNET_ERROR_MASK_COMBINED_SEC_CERT
) != 0,
4834 request
->connect_timeout
, &netconn
);
4835 if(res
!= ERROR_SUCCESS
) {
4836 ERR("create_netconn failed: %u\n", res
);
4840 request
->netconn
= netconn
;
4842 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4843 INTERNET_STATUS_CONNECTED_TO_SERVER
,
4844 request
->server
->addr_str
, strlen(request
->server
->addr_str
)+1);
4847 TRACE("Created connection to %s: %p\n", debugstr_w(request
->server
->name
), netconn
);
4848 return ERROR_SUCCESS
;
4851 static char *build_ascii_request( const WCHAR
*str
, void *data
, DWORD data_len
, DWORD
*out_len
)
4853 int len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
4856 if (!(ret
= heap_alloc( len
+ data_len
))) return NULL
;
4857 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
4858 if (data_len
) memcpy( ret
+ len
- 1, data
, data_len
);
4859 *out_len
= len
+ data_len
- 1;
4864 static void set_content_length_header( http_request_t
*request
, DWORD len
, DWORD flags
)
4866 WCHAR buf
[ARRAY_SIZE(L
"Content-Length: %u\r\n") + 10];
4868 swprintf( buf
, ARRAY_SIZE(buf
), L
"Content-Length: %u\r\n", len
);
4869 HTTP_HttpAddRequestHeadersW( request
, buf
, ~0u, flags
);
4872 /***********************************************************************
4873 * HTTP_HttpSendRequestW (internal)
4875 * Sends the specified request to the HTTP server
4878 * ERROR_SUCCESS on success
4879 * win32 error code on failure
4882 static DWORD
HTTP_HttpSendRequestW(http_request_t
*request
, LPCWSTR lpszHeaders
,
4883 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
4884 DWORD dwContentLength
, BOOL bEndRequest
)
4886 BOOL redirected
= FALSE
, secure_proxy_connect
= FALSE
, loop_next
;
4887 WCHAR
*request_header
= NULL
;
4888 INT responseLen
, cnt
;
4891 TRACE("--> %p\n", request
);
4893 assert(request
->hdr
.htype
== WH_HHTTPREQ
);
4895 /* if the verb is NULL default to GET */
4897 request
->verb
= heap_strdupW(L
"GET");
4899 HTTP_ProcessHeader(request
, L
"Host", request
->server
->canon_host_port
,
4900 HTTP_ADDREQ_FLAG_ADD_IF_NEW
| HTTP_ADDHDR_FLAG_REQ
);
4902 if (dwContentLength
|| wcscmp(request
->verb
, L
"GET"))
4904 set_content_length_header(request
, dwContentLength
, HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4905 request
->bytesToWrite
= dwContentLength
;
4907 if (request
->session
->appInfo
->agent
)
4909 WCHAR
*agent_header
;
4912 len
= lstrlenW(request
->session
->appInfo
->agent
) + lstrlenW(L
"User-Agent: %s\r\n");
4913 agent_header
= heap_alloc(len
* sizeof(WCHAR
));
4914 swprintf(agent_header
, len
, L
"User-Agent: %s\r\n", request
->session
->appInfo
->agent
);
4916 HTTP_HttpAddRequestHeadersW(request
, agent_header
, lstrlenW(agent_header
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4917 heap_free(agent_header
);
4919 if (request
->hdr
.dwFlags
& INTERNET_FLAG_PRAGMA_NOCACHE
)
4921 HTTP_HttpAddRequestHeadersW(request
, L
"Pragma: no-cache\r\n",
4922 lstrlenW(L
"Pragma: no-cache\r\n"), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4924 if ((request
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
) && wcscmp(request
->verb
, L
"GET"))
4926 HTTP_HttpAddRequestHeadersW(request
, L
"Cache-Control: no-cache\r\n",
4927 lstrlenW(L
"Cache-Control: no-cache\r\n"), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4930 /* add the headers the caller supplied */
4931 if( lpszHeaders
&& dwHeaderLength
)
4932 HTTP_HttpAddRequestHeadersW(request
, lpszHeaders
, dwHeaderLength
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
4936 DWORD len
, data_len
= dwOptionalLength
;
4937 BOOL reusing_connection
;
4943 request
->contentLength
= ~0;
4944 request
->bytesToWrite
= 0;
4947 if (TRACE_ON(wininet
))
4951 EnterCriticalSection( &request
->headers_section
);
4952 host
= HTTP_GetHeader(request
, L
"Host");
4953 TRACE("Going to url %s %s\n", debugstr_w(host
->lpszValue
), debugstr_w(request
->path
));
4954 LeaveCriticalSection( &request
->headers_section
);
4957 HTTP_FixURL(request
);
4958 if (request
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
)
4960 HTTP_ProcessHeader(request
, L
"Connection", L
"Keep-Alive",
4961 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
4963 HTTP_InsertAuthorization(request
, request
->authInfo
, L
"Authorization");
4964 HTTP_InsertAuthorization(request
, request
->proxyAuthInfo
, L
"Proxy-Authorization");
4966 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
))
4967 HTTP_InsertCookies(request
);
4969 res
= open_http_connection(request
, &reusing_connection
);
4970 if (res
!= ERROR_SUCCESS
)
4973 if (!reusing_connection
&& (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
4975 if (request
->proxy
) secure_proxy_connect
= TRUE
;
4978 res
= NETCON_secure_connect(request
->netconn
, request
->server
);
4979 if (res
!= ERROR_SUCCESS
)
4981 WARN("failed to upgrade to secure connection\n");
4982 http_release_netconn(request
, FALSE
);
4987 if (secure_proxy_connect
)
4989 const WCHAR
*target
= request
->server
->host_port
;
4991 if (HTTP_GetCustomHeaderIndex(request
, L
"Content-Length", 0, TRUE
) >= 0)
4992 set_content_length_header(request
, 0, HTTP_ADDREQ_FLAG_REPLACE
);
4994 request_header
= build_request_header(request
, L
"CONNECT", target
, L
"HTTP/1.1", TRUE
);
4996 else if (request
->proxy
&& !(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
4998 WCHAR
*url
= build_proxy_path_url(request
);
4999 request_header
= build_request_header(request
, request
->verb
, url
, request
->version
, TRUE
);
5004 if (request
->proxy
&& HTTP_GetCustomHeaderIndex(request
, L
"Content-Length", 0, TRUE
) >= 0)
5005 set_content_length_header(request
, dwContentLength
, HTTP_ADDREQ_FLAG_REPLACE
);
5007 request_header
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
5010 TRACE("Request header -> %s\n", debugstr_w(request_header
) );
5012 /* send the request as ASCII, tack on the optional data */
5013 if (!lpOptional
|| redirected
|| secure_proxy_connect
)
5016 ascii_req
= build_ascii_request(request_header
, lpOptional
, data_len
, &len
);
5017 heap_free(request_header
);
5018 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
5020 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5021 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
5023 NETCON_set_timeout( request
->netconn
, TRUE
, request
->send_timeout
);
5024 res
= NETCON_send(request
->netconn
, ascii_req
, len
, 0, &cnt
);
5025 heap_free( ascii_req
);
5026 if(res
!= ERROR_SUCCESS
) {
5027 TRACE("send failed: %u\n", res
);
5028 if(!reusing_connection
)
5030 http_release_netconn(request
, FALSE
);
5035 request
->bytesWritten
= data_len
;
5037 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5038 INTERNET_STATUS_REQUEST_SENT
,
5039 &len
, sizeof(DWORD
));
5045 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5046 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
5048 if (HTTP_GetResponseHeaders(request
, &responseLen
))
5050 http_release_netconn(request
, FALSE
);
5051 res
= ERROR_INTERNET_CONNECTION_ABORTED
;
5054 /* FIXME: We should know that connection is closed before sending
5055 * headers. Otherwise wrong callbacks are executed */
5056 if(!responseLen
&& reusing_connection
) {
5057 TRACE("Connection closed by server, reconnecting\n");
5058 http_release_netconn(request
, FALSE
);
5063 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5064 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
5067 http_process_keep_alive(request
);
5068 HTTP_ProcessCookies(request
);
5069 HTTP_ProcessExpires(request
);
5070 HTTP_ProcessLastModified(request
);
5072 res
= set_content_length(request
);
5073 if(res
!= ERROR_SUCCESS
)
5075 if(!request
->contentLength
&& !secure_proxy_connect
)
5076 http_release_netconn(request
, TRUE
);
5078 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && responseLen
)
5082 switch(request
->status_code
) {
5083 case HTTP_STATUS_REDIRECT
:
5084 case HTTP_STATUS_MOVED
:
5085 case HTTP_STATUS_REDIRECT_KEEP_VERB
:
5086 case HTTP_STATUS_REDIRECT_METHOD
:
5087 new_url
= get_redirect_url(request
);
5091 if (wcscmp(request
->verb
, L
"GET") && wcscmp(request
->verb
, L
"HEAD") &&
5092 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5094 heap_free(request
->verb
);
5095 request
->verb
= heap_strdupW(L
"GET");
5097 http_release_netconn(request
, drain_content(request
, FALSE
) == ERROR_SUCCESS
);
5098 res
= HTTP_HandleRedirect(request
, new_url
);
5100 if (res
== ERROR_SUCCESS
)
5105 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTH
) && res
== ERROR_SUCCESS
)
5107 WCHAR szAuthValue
[2048];
5109 if (request
->status_code
== HTTP_STATUS_DENIED
)
5111 WCHAR
*host
= heap_strdupW( request
->server
->canon_host_port
);
5113 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_WWW_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5115 if (HTTP_DoAuthorization(request
, szAuthValue
,
5117 request
->session
->userName
,
5118 request
->session
->password
, host
))
5120 if (drain_content(request
, TRUE
) != ERROR_SUCCESS
)
5122 FIXME("Could not drain content\n");
5123 http_release_netconn(request
, FALSE
);
5129 dwBufferSize
= 2048;
5134 TRACE("Cleaning wrong authorization data\n");
5135 destroy_authinfo(request
->authInfo
);
5136 request
->authInfo
= NULL
;
5139 if (request
->status_code
== HTTP_STATUS_PROXY_AUTH_REQ
)
5142 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_PROXY_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5144 if (HTTP_DoAuthorization(request
, szAuthValue
,
5145 &request
->proxyAuthInfo
,
5146 request
->session
->appInfo
->proxyUsername
,
5147 request
->session
->appInfo
->proxyPassword
,
5150 if (drain_content(request
, TRUE
) != ERROR_SUCCESS
)
5152 FIXME("Could not drain content\n");
5153 http_release_netconn(request
, FALSE
);
5159 dwBufferSize
= 2048;
5163 TRACE("Cleaning wrong proxy authorization data\n");
5164 destroy_authinfo(request
->proxyAuthInfo
);
5165 request
->proxyAuthInfo
= NULL
;
5169 if (secure_proxy_connect
&& request
->status_code
== HTTP_STATUS_OK
)
5171 res
= NETCON_secure_connect(request
->netconn
, request
->server
);
5172 if (res
!= ERROR_SUCCESS
)
5174 WARN("failed to upgrade to secure proxy connection\n");
5175 http_release_netconn( request
, FALSE
);
5178 remove_header(request
, L
"Proxy-Authorization", TRUE
);
5179 destroy_authinfo(request
->proxyAuthInfo
);
5180 request
->proxyAuthInfo
= NULL
;
5181 request
->contentLength
= 0;
5182 request
->netconn_stream
.content_length
= 0;
5184 secure_proxy_connect
= FALSE
;
5189 res
= ERROR_SUCCESS
;
5194 /* TODO: send notification for P3P header */
5196 if(res
== ERROR_SUCCESS
)
5197 create_cache_entry(request
);
5199 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5201 if (res
== ERROR_SUCCESS
) {
5202 if(bEndRequest
&& request
->contentLength
&& request
->bytesWritten
== request
->bytesToWrite
)
5203 HTTP_ReceiveRequestData(request
);
5205 send_request_complete(request
,
5206 request
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)request
->hdr
.hInternet
: 1, 0);
5208 send_request_complete(request
, 0, res
);
5224 } send_request_task_t
;
5226 /***********************************************************************
5228 * Helper functions for the HttpSendRequest(Ex) functions
5231 static void AsyncHttpSendRequestProc(task_header_t
*hdr
)
5233 send_request_task_t
*task
= (send_request_task_t
*)hdr
;
5234 http_request_t
*request
= (http_request_t
*)task
->hdr
.hdr
;
5236 TRACE("%p\n", request
);
5238 HTTP_HttpSendRequestW(request
, task
->headers
, task
->headers_len
, task
->optional
,
5239 task
->optional_len
, task
->content_len
, task
->end_request
);
5241 heap_free(task
->headers
);
5245 static DWORD
HTTP_HttpEndRequestW(http_request_t
*request
, DWORD dwFlags
, DWORD_PTR dwContext
)
5248 DWORD res
= ERROR_SUCCESS
;
5250 if(!is_valid_netconn(request
->netconn
)) {
5251 WARN("Not connected\n");
5252 send_request_complete(request
, 0, ERROR_INTERNET_OPERATION_CANCELLED
);
5253 return ERROR_INTERNET_OPERATION_CANCELLED
;
5256 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5257 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
5259 if (HTTP_GetResponseHeaders(request
, &responseLen
) || !responseLen
)
5260 res
= ERROR_HTTP_HEADER_NOT_FOUND
;
5262 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5263 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
5265 /* process cookies here. Is this right? */
5266 http_process_keep_alive(request
);
5267 HTTP_ProcessCookies(request
);
5268 HTTP_ProcessExpires(request
);
5269 HTTP_ProcessLastModified(request
);
5271 if ((res
= set_content_length(request
)) == ERROR_SUCCESS
) {
5272 if(!request
->contentLength
)
5273 http_release_netconn(request
, TRUE
);
5276 if (res
== ERROR_SUCCESS
&& !(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
5278 switch(request
->status_code
) {
5279 case HTTP_STATUS_REDIRECT
:
5280 case HTTP_STATUS_MOVED
:
5281 case HTTP_STATUS_REDIRECT_METHOD
:
5282 case HTTP_STATUS_REDIRECT_KEEP_VERB
: {
5285 new_url
= get_redirect_url(request
);
5289 if (wcscmp(request
->verb
, L
"GET") && wcscmp(request
->verb
, L
"HEAD") &&
5290 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5292 heap_free(request
->verb
);
5293 request
->verb
= heap_strdupW(L
"GET");
5295 http_release_netconn(request
, drain_content(request
, FALSE
) == ERROR_SUCCESS
);
5296 res
= HTTP_HandleRedirect(request
, new_url
);
5298 if (res
== ERROR_SUCCESS
)
5299 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, TRUE
);
5304 if(res
== ERROR_SUCCESS
)
5305 create_cache_entry(request
);
5307 if (res
== ERROR_SUCCESS
&& request
->contentLength
)
5308 HTTP_ReceiveRequestData(request
);
5310 send_request_complete(request
, res
== ERROR_SUCCESS
, res
);
5315 /***********************************************************************
5316 * HttpEndRequestA (WININET.@)
5318 * Ends an HTTP request that was started by HttpSendRequestEx
5321 * TRUE if successful
5325 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
5326 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5328 TRACE("(%p, %p, %08x, %08lx)\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5332 SetLastError(ERROR_INVALID_PARAMETER
);
5336 return HttpEndRequestW(hRequest
, NULL
, dwFlags
, dwContext
);
5343 } end_request_task_t
;
5345 static void AsyncHttpEndRequestProc(task_header_t
*hdr
)
5347 end_request_task_t
*task
= (end_request_task_t
*)hdr
;
5348 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
5352 HTTP_HttpEndRequestW(req
, task
->flags
, task
->context
);
5355 /***********************************************************************
5356 * HttpEndRequestW (WININET.@)
5358 * Ends an HTTP request that was started by HttpSendRequestEx
5361 * TRUE if successful
5365 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
5366 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5368 http_request_t
*request
;
5371 TRACE("%p %p %x %lx -->\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5375 SetLastError(ERROR_INVALID_PARAMETER
);
5379 request
= (http_request_t
*) get_handle_object( hRequest
);
5381 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5383 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
5385 WININET_Release( &request
->hdr
);
5388 request
->hdr
.dwFlags
|= dwFlags
;
5390 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5392 end_request_task_t
*task
;
5394 task
= alloc_async_task(&request
->hdr
, AsyncHttpEndRequestProc
, sizeof(*task
));
5395 task
->flags
= dwFlags
;
5396 task
->context
= dwContext
;
5398 INTERNET_AsyncCall(&task
->hdr
);
5399 res
= ERROR_IO_PENDING
;
5402 res
= HTTP_HttpEndRequestW(request
, dwFlags
, dwContext
);
5404 WININET_Release( &request
->hdr
);
5405 TRACE("%u <--\n", res
);
5406 if(res
!= ERROR_SUCCESS
)
5408 return res
== ERROR_SUCCESS
;
5411 /***********************************************************************
5412 * HttpSendRequestExA (WININET.@)
5414 * Sends the specified request to the HTTP server and allows chunked
5419 * Failure: FALSE, call GetLastError() for more information.
5421 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
5422 LPINTERNET_BUFFERSA lpBuffersIn
,
5423 LPINTERNET_BUFFERSA lpBuffersOut
,
5424 DWORD dwFlags
, DWORD_PTR dwContext
)
5426 INTERNET_BUFFERSW BuffersInW
;
5429 LPWSTR header
= NULL
;
5431 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5432 lpBuffersOut
, dwFlags
, dwContext
);
5436 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
5437 if (lpBuffersIn
->lpcszHeader
)
5439 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
5440 lpBuffersIn
->dwHeadersLength
,0,0);
5441 header
= heap_alloc(headerlen
*sizeof(WCHAR
));
5442 if (!(BuffersInW
.lpcszHeader
= header
))
5444 SetLastError(ERROR_OUTOFMEMORY
);
5447 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
5448 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5452 BuffersInW
.lpcszHeader
= NULL
;
5453 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
5454 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
5455 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
5456 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
5457 BuffersInW
.Next
= NULL
;
5460 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
5466 /***********************************************************************
5467 * HttpSendRequestExW (WININET.@)
5469 * Sends the specified request to the HTTP server and allows chunked
5474 * Failure: FALSE, call GetLastError() for more information.
5476 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
5477 LPINTERNET_BUFFERSW lpBuffersIn
,
5478 LPINTERNET_BUFFERSW lpBuffersOut
,
5479 DWORD dwFlags
, DWORD_PTR dwContext
)
5481 http_request_t
*request
;
5482 http_session_t
*session
;
5486 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5487 lpBuffersOut
, dwFlags
, dwContext
);
5489 request
= (http_request_t
*) get_handle_object( hRequest
);
5491 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5493 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5497 session
= request
->session
;
5498 assert(session
->hdr
.htype
== WH_HHTTPSESSION
);
5499 hIC
= session
->appInfo
;
5500 assert(hIC
->hdr
.htype
== WH_HINIT
);
5502 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5504 send_request_task_t
*task
;
5506 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5511 if (lpBuffersIn
->lpcszHeader
)
5513 if (lpBuffersIn
->dwHeadersLength
== ~0u)
5514 size
= (lstrlenW( lpBuffersIn
->lpcszHeader
) + 1) * sizeof(WCHAR
);
5516 size
= lpBuffersIn
->dwHeadersLength
* sizeof(WCHAR
);
5518 task
->headers
= heap_alloc(size
);
5519 memcpy(task
->headers
, lpBuffersIn
->lpcszHeader
, size
);
5521 else task
->headers
= NULL
;
5523 task
->headers_len
= size
/ sizeof(WCHAR
);
5524 task
->optional
= lpBuffersIn
->lpvBuffer
;
5525 task
->optional_len
= lpBuffersIn
->dwBufferLength
;
5526 task
->content_len
= lpBuffersIn
->dwBufferTotal
;
5530 task
->headers
= NULL
;
5531 task
->headers_len
= 0;
5532 task
->optional
= NULL
;
5533 task
->optional_len
= 0;
5534 task
->content_len
= 0;
5537 task
->end_request
= FALSE
;
5539 INTERNET_AsyncCall(&task
->hdr
);
5540 res
= ERROR_IO_PENDING
;
5545 res
= HTTP_HttpSendRequestW(request
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5546 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
5547 lpBuffersIn
->dwBufferTotal
, FALSE
);
5549 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, FALSE
);
5554 WININET_Release( &request
->hdr
);
5558 return res
== ERROR_SUCCESS
;
5561 /***********************************************************************
5562 * HttpSendRequestW (WININET.@)
5564 * Sends the specified request to the HTTP server
5571 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
5572 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5574 http_request_t
*request
;
5575 http_session_t
*session
= NULL
;
5576 appinfo_t
*hIC
= NULL
;
5577 DWORD res
= ERROR_SUCCESS
;
5579 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest
,
5580 debugstr_wn(lpszHeaders
, dwHeaderLength
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
5582 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
5583 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5585 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5589 session
= request
->session
;
5590 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
5592 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5596 hIC
= session
->appInfo
;
5597 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
5599 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5603 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5605 send_request_task_t
*task
;
5607 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5612 if (dwHeaderLength
== ~0u) size
= (lstrlenW(lpszHeaders
) + 1) * sizeof(WCHAR
);
5613 else size
= dwHeaderLength
* sizeof(WCHAR
);
5615 task
->headers
= heap_alloc(size
);
5616 memcpy(task
->headers
, lpszHeaders
, size
);
5619 task
->headers
= NULL
;
5620 task
->headers_len
= dwHeaderLength
;
5621 task
->optional
= lpOptional
;
5622 task
->optional_len
= dwOptionalLength
;
5623 task
->content_len
= dwOptionalLength
;
5624 task
->end_request
= TRUE
;
5626 INTERNET_AsyncCall(&task
->hdr
);
5627 res
= ERROR_IO_PENDING
;
5631 res
= HTTP_HttpSendRequestW(request
, lpszHeaders
,
5632 dwHeaderLength
, lpOptional
, dwOptionalLength
,
5633 dwOptionalLength
, TRUE
);
5637 WININET_Release( &request
->hdr
);
5640 return res
== ERROR_SUCCESS
;
5643 /***********************************************************************
5644 * HttpSendRequestA (WININET.@)
5646 * Sends the specified request to the HTTP server
5653 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
5654 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5657 LPWSTR szHeaders
=NULL
;
5658 DWORD nLen
=dwHeaderLength
;
5659 if(lpszHeaders
!=NULL
)
5661 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
5662 szHeaders
= heap_alloc(nLen
*sizeof(WCHAR
));
5663 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
5665 result
= HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
5666 heap_free(szHeaders
);
5670 /***********************************************************************
5671 * HTTPSESSION_Destroy (internal)
5673 * Deallocate session handle
5676 static void HTTPSESSION_Destroy(object_header_t
*hdr
)
5678 http_session_t
*session
= (http_session_t
*) hdr
;
5680 TRACE("%p\n", session
);
5682 WININET_Release(&session
->appInfo
->hdr
);
5684 heap_free(session
->hostName
);
5685 heap_free(session
->password
);
5686 heap_free(session
->userName
);
5689 static DWORD
HTTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
5691 http_session_t
*ses
= (http_session_t
*)hdr
;
5694 case INTERNET_OPTION_HANDLE_TYPE
:
5695 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5697 if (*size
< sizeof(ULONG
))
5698 return ERROR_INSUFFICIENT_BUFFER
;
5700 *size
= sizeof(DWORD
);
5701 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_HTTP
;
5702 return ERROR_SUCCESS
;
5703 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5704 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5706 if (*size
< sizeof(DWORD
))
5707 return ERROR_INSUFFICIENT_BUFFER
;
5709 *size
= sizeof(DWORD
);
5710 *(DWORD
*)buffer
= ses
->connect_timeout
;
5711 return ERROR_SUCCESS
;
5713 case INTERNET_OPTION_SEND_TIMEOUT
:
5714 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5716 if (*size
< sizeof(DWORD
))
5717 return ERROR_INSUFFICIENT_BUFFER
;
5719 *size
= sizeof(DWORD
);
5720 *(DWORD
*)buffer
= ses
->send_timeout
;
5721 return ERROR_SUCCESS
;
5723 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5724 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5726 if (*size
< sizeof(DWORD
))
5727 return ERROR_INSUFFICIENT_BUFFER
;
5729 *size
= sizeof(DWORD
);
5730 *(DWORD
*)buffer
= ses
->receive_timeout
;
5731 return ERROR_SUCCESS
;
5734 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
5737 static DWORD
HTTPSESSION_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
5739 http_session_t
*ses
= (http_session_t
*)hdr
;
5742 case INTERNET_OPTION_USERNAME
:
5744 heap_free(ses
->userName
);
5745 if (!(ses
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5746 return ERROR_SUCCESS
;
5748 case INTERNET_OPTION_PASSWORD
:
5750 heap_free(ses
->password
);
5751 if (!(ses
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5752 return ERROR_SUCCESS
;
5754 case INTERNET_OPTION_PROXY_USERNAME
:
5756 heap_free(ses
->appInfo
->proxyUsername
);
5757 if (!(ses
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5758 return ERROR_SUCCESS
;
5760 case INTERNET_OPTION_PROXY_PASSWORD
:
5762 heap_free(ses
->appInfo
->proxyPassword
);
5763 if (!(ses
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5764 return ERROR_SUCCESS
;
5766 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5768 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5769 ses
->connect_timeout
= *(DWORD
*)buffer
;
5770 return ERROR_SUCCESS
;
5772 case INTERNET_OPTION_SEND_TIMEOUT
:
5774 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5775 ses
->send_timeout
= *(DWORD
*)buffer
;
5776 return ERROR_SUCCESS
;
5778 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5780 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5781 ses
->receive_timeout
= *(DWORD
*)buffer
;
5782 return ERROR_SUCCESS
;
5787 return INET_SetOption(hdr
, option
, buffer
, size
);
5790 static const object_vtbl_t HTTPSESSIONVtbl
= {
5791 HTTPSESSION_Destroy
,
5793 HTTPSESSION_QueryOption
,
5794 HTTPSESSION_SetOption
,
5802 /***********************************************************************
5803 * HTTP_Connect (internal)
5805 * Create http session handle
5808 * HINTERNET a session handle on success
5812 DWORD
HTTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
5813 INTERNET_PORT serverPort
, LPCWSTR lpszUserName
,
5814 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
5815 DWORD dwInternalFlags
, HINTERNET
*ret
)
5817 http_session_t
*session
= NULL
;
5821 if (!lpszServerName
|| !lpszServerName
[0])
5822 return ERROR_INVALID_PARAMETER
;
5824 assert( hIC
->hdr
.htype
== WH_HINIT
);
5826 session
= alloc_object(&hIC
->hdr
, &HTTPSESSIONVtbl
, sizeof(http_session_t
));
5828 return ERROR_OUTOFMEMORY
;
5831 * According to my tests. The name is not resolved until a request is sent
5834 session
->hdr
.htype
= WH_HHTTPSESSION
;
5835 session
->hdr
.dwFlags
= dwFlags
;
5836 session
->hdr
.dwContext
= dwContext
;
5837 session
->hdr
.dwInternalFlags
|= dwInternalFlags
;
5838 session
->hdr
.decoding
= hIC
->hdr
.decoding
;
5840 WININET_AddRef( &hIC
->hdr
);
5841 session
->appInfo
= hIC
;
5842 list_add_head( &hIC
->hdr
.children
, &session
->hdr
.entry
);
5844 session
->hostName
= heap_strdupW(lpszServerName
);
5845 if (lpszUserName
&& lpszUserName
[0])
5846 session
->userName
= heap_strdupW(lpszUserName
);
5847 session
->password
= heap_strdupW(lpszPassword
);
5848 session
->hostPort
= serverPort
;
5849 session
->connect_timeout
= hIC
->connect_timeout
;
5850 session
->send_timeout
= 0;
5851 session
->receive_timeout
= 0;
5853 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5854 if (!(session
->hdr
.dwInternalFlags
& INET_OPENURL
))
5856 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
5857 INTERNET_STATUS_HANDLE_CREATED
, &session
->hdr
.hInternet
,
5862 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5866 TRACE("%p --> %p\n", hIC
, session
);
5868 *ret
= session
->hdr
.hInternet
;
5869 return ERROR_SUCCESS
;
5872 /***********************************************************************
5873 * HTTP_clear_response_headers (internal)
5875 * clear out any old response headers
5877 static void HTTP_clear_response_headers( http_request_t
*request
)
5881 EnterCriticalSection( &request
->headers_section
);
5883 for( i
=0; i
<request
->nCustHeaders
; i
++)
5885 if( !request
->custHeaders
[i
].lpszField
)
5887 if( !request
->custHeaders
[i
].lpszValue
)
5889 if ( request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
5891 HTTP_DeleteCustomHeader( request
, i
);
5895 LeaveCriticalSection( &request
->headers_section
);
5898 /***********************************************************************
5899 * HTTP_GetResponseHeaders (internal)
5901 * Read server response
5908 static DWORD
HTTP_GetResponseHeaders(http_request_t
*request
, INT
*len
)
5911 WCHAR buffer
[MAX_REPLY_LEN
];
5912 DWORD buflen
= MAX_REPLY_LEN
;
5914 char bufferA
[MAX_REPLY_LEN
];
5915 LPWSTR status_code
= NULL
, status_text
= NULL
;
5916 DWORD res
= ERROR_HTTP_INVALID_SERVER_RESPONSE
;
5917 BOOL codeHundred
= FALSE
;
5921 if(!is_valid_netconn(request
->netconn
))
5924 /* clear old response headers (eg. from a redirect response) */
5925 HTTP_clear_response_headers( request
);
5927 NETCON_set_timeout( request
->netconn
, FALSE
, request
->receive_timeout
);
5930 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5932 buflen
= MAX_REPLY_LEN
;
5933 if ((res
= read_line(request
, bufferA
, &buflen
)))
5936 if (!buflen
) goto lend
;
5939 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
5940 /* check is this a status code line? */
5941 if (!wcsncmp(buffer
, L
"HTTP/1.0", 4))
5943 /* split the version from the status code */
5944 status_code
= wcschr( buffer
, ' ' );
5949 /* split the status code from the status text */
5950 status_text
= wcschr( status_code
, ' ' );
5954 request
->status_code
= wcstol(status_code
, NULL
, 10);
5956 TRACE("version [%s] status code [%s] status text [%s]\n",
5957 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
5959 codeHundred
= request
->status_code
== HTTP_STATUS_CONTINUE
;
5961 else if (!codeHundred
)
5963 WARN("No status line at head of response (%s)\n", debugstr_w(buffer
));
5965 heap_free(request
->version
);
5966 heap_free(request
->statusText
);
5968 request
->status_code
= HTTP_STATUS_OK
;
5969 request
->version
= heap_strdupW(L
"HTTP/1.0");
5970 request
->statusText
= heap_strdupW(L
"OK");
5974 } while (codeHundred
);
5976 /* Add status code */
5977 HTTP_ProcessHeader(request
, L
"Status", status_code
,
5978 HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
5980 heap_free(request
->version
);
5981 heap_free(request
->statusText
);
5983 request
->version
= heap_strdupW(buffer
);
5984 request
->statusText
= heap_strdupW(status_text
? status_text
: L
"");
5986 /* Restore the spaces */
5987 *(status_code
-1) = ' ';
5989 *(status_text
-1) = ' ';
5991 /* Parse each response line */
5994 buflen
= MAX_REPLY_LEN
;
5995 if (!read_line(request
, bufferA
, &buflen
) && buflen
)
5997 LPWSTR
* pFieldAndValue
;
5999 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
6001 if (!bufferA
[0]) break;
6002 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
6004 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
6007 HTTP_ProcessHeader(request
, pFieldAndValue
[0], pFieldAndValue
[1],
6008 HTTP_ADDREQ_FLAG_ADD
);
6009 HTTP_FreeTokens(pFieldAndValue
);
6020 res
= ERROR_SUCCESS
;
6029 /***********************************************************************
6030 * HTTP_InterpretHttpHeader (internal)
6032 * Parse server response
6036 * Pointer to array of field, value, NULL on success.
6039 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
6041 LPWSTR
* pTokenPair
;
6045 pTokenPair
= heap_alloc_zero(sizeof(*pTokenPair
)*3);
6047 pszColon
= wcschr(buffer
, ':');
6048 /* must have two tokens */
6051 HTTP_FreeTokens(pTokenPair
);
6053 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
6057 pTokenPair
[0] = heap_alloc((pszColon
- buffer
+ 1) * sizeof(WCHAR
));
6060 HTTP_FreeTokens(pTokenPair
);
6063 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
6064 pTokenPair
[0][pszColon
- buffer
] = '\0';
6068 len
= lstrlenW(pszColon
);
6069 pTokenPair
[1] = heap_alloc((len
+ 1) * sizeof(WCHAR
));
6072 HTTP_FreeTokens(pTokenPair
);
6075 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
6077 strip_spaces(pTokenPair
[0]);
6078 strip_spaces(pTokenPair
[1]);
6080 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
6084 /***********************************************************************
6085 * HTTP_ProcessHeader (internal)
6087 * Stuff header into header tables according to <dwModifier>
6091 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6093 static DWORD
HTTP_ProcessHeader(http_request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
6095 LPHTTPHEADERW lphttpHdr
= NULL
;
6097 BOOL request_only
= !!(dwModifier
& HTTP_ADDHDR_FLAG_REQ
);
6098 DWORD res
= ERROR_HTTP_INVALID_HEADER
;
6100 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
6102 EnterCriticalSection( &request
->headers_section
);
6104 /* REPLACE wins out over ADD */
6105 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
6106 dwModifier
&= ~HTTP_ADDHDR_FLAG_ADD
;
6108 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD
)
6111 index
= HTTP_GetCustomHeaderIndex(request
, field
, 0, request_only
);
6115 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
6117 LeaveCriticalSection( &request
->headers_section
);
6118 return ERROR_HTTP_INVALID_HEADER
;
6120 lphttpHdr
= &request
->custHeaders
[index
];
6126 hdr
.lpszField
= (LPWSTR
)field
;
6127 hdr
.lpszValue
= (LPWSTR
)value
;
6128 hdr
.wFlags
= hdr
.wCount
= 0;
6130 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6131 hdr
.wFlags
|= HDR_ISREQUEST
;
6133 res
= HTTP_InsertCustomHeader(request
, &hdr
);
6134 LeaveCriticalSection( &request
->headers_section
);
6137 /* no value to delete */
6140 LeaveCriticalSection( &request
->headers_section
);
6141 return ERROR_SUCCESS
;
6144 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6145 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
6147 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
6149 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
6151 HTTP_DeleteCustomHeader( request
, index
);
6153 if (value
&& value
[0])
6157 hdr
.lpszField
= (LPWSTR
)field
;
6158 hdr
.lpszValue
= (LPWSTR
)value
;
6159 hdr
.wFlags
= hdr
.wCount
= 0;
6161 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6162 hdr
.wFlags
|= HDR_ISREQUEST
;
6164 res
= HTTP_InsertCustomHeader(request
, &hdr
);
6165 LeaveCriticalSection( &request
->headers_section
);
6169 LeaveCriticalSection( &request
->headers_section
);
6170 return ERROR_SUCCESS
;
6172 else if (dwModifier
& COALESCEFLAGS
)
6177 INT origlen
= lstrlenW(lphttpHdr
->lpszValue
);
6178 INT valuelen
= lstrlenW(value
);
6180 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
6183 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6185 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
6188 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6191 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
6193 lpsztmp
= heap_realloc(lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
6196 lphttpHdr
->lpszValue
= lpsztmp
;
6197 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6200 lphttpHdr
->lpszValue
[origlen
] = ch
;
6202 lphttpHdr
->lpszValue
[origlen
] = ' ';
6206 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
6207 lphttpHdr
->lpszValue
[len
] = '\0';
6208 res
= ERROR_SUCCESS
;
6212 WARN("heap_realloc (%d bytes) failed\n",len
+1);
6213 res
= ERROR_OUTOFMEMORY
;
6216 TRACE("<-- %d\n", res
);
6217 LeaveCriticalSection( &request
->headers_section
);
6221 /***********************************************************************
6222 * HTTP_GetCustomHeaderIndex (internal)
6224 * Return index of custom header from header array
6225 * Headers section must be held
6227 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*request
, LPCWSTR lpszField
,
6228 int requested_index
, BOOL request_only
)
6232 TRACE("%s, %d, %d\n", debugstr_w(lpszField
), requested_index
, request_only
);
6234 for (index
= 0; index
< request
->nCustHeaders
; index
++)
6236 if (wcsicmp(request
->custHeaders
[index
].lpszField
, lpszField
))
6239 if (request_only
&& !(request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6242 if (!request_only
&& (request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6245 if (requested_index
== 0)
6250 if (index
>= request
->nCustHeaders
)
6253 TRACE("Return: %d\n", index
);
6258 /***********************************************************************
6259 * HTTP_InsertCustomHeader (internal)
6261 * Insert header into array
6262 * Headers section must be held
6264 static DWORD
HTTP_InsertCustomHeader(http_request_t
*request
, LPHTTPHEADERW lpHdr
)
6267 LPHTTPHEADERW lph
= NULL
;
6269 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
6270 count
= request
->nCustHeaders
+ 1;
6272 lph
= heap_realloc_zero(request
->custHeaders
, sizeof(HTTPHEADERW
) * count
);
6274 lph
= heap_alloc_zero(sizeof(HTTPHEADERW
) * count
);
6277 return ERROR_OUTOFMEMORY
;
6279 request
->custHeaders
= lph
;
6280 request
->custHeaders
[count
-1].lpszField
= heap_strdupW(lpHdr
->lpszField
);
6281 request
->custHeaders
[count
-1].lpszValue
= heap_strdupW(lpHdr
->lpszValue
);
6282 request
->custHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
6283 request
->custHeaders
[count
-1].wCount
= lpHdr
->wCount
;
6284 request
->nCustHeaders
++;
6286 return ERROR_SUCCESS
;
6290 /***********************************************************************
6291 * HTTP_DeleteCustomHeader (internal)
6293 * Delete header from array
6294 * If this function is called, the index may change.
6295 * Headers section must be held
6297 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*request
, DWORD index
)
6299 if( request
->nCustHeaders
<= 0 )
6301 if( index
>= request
->nCustHeaders
)
6303 request
->nCustHeaders
--;
6305 heap_free(request
->custHeaders
[index
].lpszField
);
6306 heap_free(request
->custHeaders
[index
].lpszValue
);
6308 memmove( &request
->custHeaders
[index
], &request
->custHeaders
[index
+1],
6309 (request
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
6310 memset( &request
->custHeaders
[request
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
6316 /***********************************************************************
6317 * IsHostInProxyBypassList (WININET.@)
6319 BOOL WINAPI
IsHostInProxyBypassList(INTERNET_SCHEME scheme
, LPCSTR szHost
, DWORD length
)
6321 FIXME("STUB: scheme=%d host=%s length=%d\n", scheme
, szHost
, length
);