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
38 #include <wine/exception.h>
40 static const WCHAR g_szHttp1_0
[] = {'H','T','T','P','/','1','.','0',0};
41 static const WCHAR g_szHttp1_1
[] = {'H','T','T','P','/','1','.','1',0};
42 static const WCHAR szOK
[] = {'O','K',0};
43 static const WCHAR hostW
[] = { 'H','o','s','t',0 };
44 static const WCHAR szAuthorization
[] = { 'A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
45 static const WCHAR szProxy_Authorization
[] = { 'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0 };
46 static const WCHAR szStatus
[] = { 'S','t','a','t','u','s',0 };
47 static const WCHAR szKeepAlive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
48 static const WCHAR szGET
[] = { 'G','E','T', 0 };
49 static const WCHAR szHEAD
[] = { 'H','E','A','D', 0 };
51 static const WCHAR szAccept
[] = { 'A','c','c','e','p','t',0 };
52 static const WCHAR szAccept_Charset
[] = { 'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0 };
53 static const WCHAR szAccept_Encoding
[] = { 'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0 };
54 static const WCHAR szAccept_Language
[] = { 'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0 };
55 static const WCHAR szAccept_Ranges
[] = { 'A','c','c','e','p','t','-','R','a','n','g','e','s',0 };
56 static const WCHAR szAge
[] = { 'A','g','e',0 };
57 static const WCHAR szAllow
[] = { 'A','l','l','o','w',0 };
58 static const WCHAR szCache_Control
[] = { 'C','a','c','h','e','-','C','o','n','t','r','o','l',0 };
59 static const WCHAR szConnection
[] = { 'C','o','n','n','e','c','t','i','o','n',0 };
60 static const WCHAR szContent_Base
[] = { 'C','o','n','t','e','n','t','-','B','a','s','e',0 };
61 static const WCHAR szContent_Disposition
[] = { 'C','o','n','t','e','n','t','-','D','i','s','p','o','s','i','t','i','o','n',0 };
62 static const WCHAR szContent_Encoding
[] = { 'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0 };
63 static const WCHAR szContent_ID
[] = { 'C','o','n','t','e','n','t','-','I','D',0 };
64 static const WCHAR szContent_Language
[] = { 'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0 };
65 static const WCHAR szContent_Length
[] = { 'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0 };
66 static const WCHAR szContent_Location
[] = { 'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0 };
67 static const WCHAR szContent_MD5
[] = { 'C','o','n','t','e','n','t','-','M','D','5',0 };
68 static const WCHAR szContent_Range
[] = { 'C','o','n','t','e','n','t','-','R','a','n','g','e',0 };
69 static const WCHAR szContent_Transfer_Encoding
[] = { 'C','o','n','t','e','n','t','-','T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
70 static const WCHAR szContent_Type
[] = { 'C','o','n','t','e','n','t','-','T','y','p','e',0 };
71 static const WCHAR szCookie
[] = { 'C','o','o','k','i','e',0 };
72 static const WCHAR szDate
[] = { 'D','a','t','e',0 };
73 static const WCHAR szFrom
[] = { 'F','r','o','m',0 };
74 static const WCHAR szETag
[] = { 'E','T','a','g',0 };
75 static const WCHAR szExpect
[] = { 'E','x','p','e','c','t',0 };
76 static const WCHAR szExpires
[] = { 'E','x','p','i','r','e','s',0 };
77 static const WCHAR szIf_Match
[] = { 'I','f','-','M','a','t','c','h',0 };
78 static const WCHAR szIf_Modified_Since
[] = { 'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
79 static const WCHAR szIf_None_Match
[] = { 'I','f','-','N','o','n','e','-','M','a','t','c','h',0 };
80 static const WCHAR szIf_Range
[] = { 'I','f','-','R','a','n','g','e',0 };
81 static const WCHAR szIf_Unmodified_Since
[] = { 'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
82 static const WCHAR szLast_Modified
[] = { 'L','a','s','t','-','M','o','d','i','f','i','e','d',0 };
83 static const WCHAR szLocation
[] = { 'L','o','c','a','t','i','o','n',0 };
84 static const WCHAR szMax_Forwards
[] = { 'M','a','x','-','F','o','r','w','a','r','d','s',0 };
85 static const WCHAR szMime_Version
[] = { 'M','i','m','e','-','V','e','r','s','i','o','n',0 };
86 static const WCHAR szPragma
[] = { 'P','r','a','g','m','a',0 };
87 static const WCHAR szProxy_Authenticate
[] = { 'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
88 static const WCHAR szProxy_Connection
[] = { 'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0 };
89 static const WCHAR szPublic
[] = { 'P','u','b','l','i','c',0 };
90 static const WCHAR szRange
[] = { 'R','a','n','g','e',0 };
91 static const WCHAR szReferer
[] = { 'R','e','f','e','r','e','r',0 };
92 static const WCHAR szRetry_After
[] = { 'R','e','t','r','y','-','A','f','t','e','r',0 };
93 static const WCHAR szServer
[] = { 'S','e','r','v','e','r',0 };
94 static const WCHAR szSet_Cookie
[] = { 'S','e','t','-','C','o','o','k','i','e',0 };
95 static const WCHAR szTransfer_Encoding
[] = { 'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0 };
96 static const WCHAR szUnless_Modified_Since
[] = { 'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0 };
97 static const WCHAR szUpgrade
[] = { 'U','p','g','r','a','d','e',0 };
98 static const WCHAR szURI
[] = { 'U','R','I',0 };
99 static const WCHAR szUser_Agent
[] = { 'U','s','e','r','-','A','g','e','n','t',0 };
100 static const WCHAR szVary
[] = { 'V','a','r','y',0 };
101 static const WCHAR szVia
[] = { 'V','i','a',0 };
102 static const WCHAR szWarning
[] = { 'W','a','r','n','i','n','g',0 };
103 static const WCHAR szWWW_Authenticate
[] = { 'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0 };
105 static const WCHAR emptyW
[] = {0};
107 #define HTTP_REFERER szReferer
108 #define HTTP_ACCEPT szAccept
109 #define HTTP_USERAGENT szUser_Agent
111 #define HTTP_ADDHDR_FLAG_ADD 0x20000000
112 #define HTTP_ADDHDR_FLAG_ADD_IF_NEW 0x10000000
113 #define HTTP_ADDHDR_FLAG_COALESCE 0x40000000
114 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA 0x40000000
115 #define HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON 0x01000000
116 #define HTTP_ADDHDR_FLAG_REPLACE 0x80000000
117 #define HTTP_ADDHDR_FLAG_REQ 0x02000000
119 #define COLLECT_TIME 60000
122 #define ARRAYSIZE(array) (sizeof(array)/sizeof((array)[0]))
133 unsigned int auth_data_len
;
134 BOOL finished
; /* finished authenticating */
138 typedef struct _basicAuthorizationData
145 UINT authorizationLen
;
146 } basicAuthorizationData
;
148 typedef struct _authorizationData
162 static struct list basicAuthorizationCache
= LIST_INIT(basicAuthorizationCache
);
163 static struct list authorizationCache
= LIST_INIT(authorizationCache
);
165 static CRITICAL_SECTION authcache_cs
;
166 static CRITICAL_SECTION_DEBUG critsect_debug
=
169 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
170 0, 0, { (DWORD_PTR
)(__FILE__
": authcache_cs") }
172 static CRITICAL_SECTION authcache_cs
= { &critsect_debug
, -1, 0, 0, 0, 0 };
174 static DWORD
HTTP_GetResponseHeaders(http_request_t
*req
, INT
*len
);
175 static DWORD
HTTP_ProcessHeader(http_request_t
*req
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
);
176 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
);
177 static DWORD
HTTP_InsertCustomHeader(http_request_t
*req
, LPHTTPHEADERW lpHdr
);
178 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*req
, LPCWSTR lpszField
, INT index
, BOOL Request
);
179 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*req
, DWORD index
);
180 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
);
181 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*, DWORD
, LPVOID
, LPDWORD
, LPDWORD
);
182 static UINT
HTTP_DecodeBase64(LPCWSTR base64
, LPSTR bin
);
183 static DWORD
drain_content(http_request_t
*,BOOL
);
185 static CRITICAL_SECTION connection_pool_cs
;
186 static CRITICAL_SECTION_DEBUG connection_pool_debug
=
188 0, 0, &connection_pool_cs
,
189 { &connection_pool_debug
.ProcessLocksList
, &connection_pool_debug
.ProcessLocksList
},
190 0, 0, { (DWORD_PTR
)(__FILE__
": connection_pool_cs") }
192 static CRITICAL_SECTION connection_pool_cs
= { &connection_pool_debug
, -1, 0, 0, 0, 0 };
194 static struct list connection_pool
= LIST_INIT(connection_pool
);
195 static BOOL collector_running
;
197 void server_addref(server_t
*server
)
199 InterlockedIncrement(&server
->ref
);
202 void server_release(server_t
*server
)
204 if(InterlockedDecrement(&server
->ref
))
208 EnterCriticalSection(&connection_pool_cs
);
210 list_remove(&server
->entry
);
212 LeaveCriticalSection(&connection_pool_cs
);
215 if(server
->cert_chain
)
216 CertFreeCertificateChain(server
->cert_chain
);
217 heap_free(server
->name
);
218 heap_free(server
->scheme_host_port
);
222 static BOOL
process_host_port(server_t
*server
)
228 static const WCHAR httpW
[] = {'h','t','t','p',0};
229 static const WCHAR httpsW
[] = {'h','t','t','p','s',0};
230 static const WCHAR formatW
[] = {'%','s',':','/','/','%','s',':','%','u',0};
232 name_len
= strlenW(server
->name
);
233 buf
= heap_alloc((name_len
+ 10 /* strlen("://:<port>") */)*sizeof(WCHAR
) + sizeof(httpsW
));
237 sprintfW(buf
, formatW
, server
->is_https
? httpsW
: httpW
, server
->name
, server
->port
);
238 server
->scheme_host_port
= buf
;
240 server
->host_port
= server
->scheme_host_port
+ 7 /* strlen("http://") */;
244 default_port
= server
->port
== (server
->is_https
? INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
);
245 server
->canon_host_port
= default_port
? server
->name
: server
->host_port
;
249 server_t
*get_server(substr_t name
, INTERNET_PORT port
, BOOL is_https
, BOOL do_create
)
251 server_t
*iter
, *server
= NULL
;
253 EnterCriticalSection(&connection_pool_cs
);
255 LIST_FOR_EACH_ENTRY(iter
, &connection_pool
, server_t
, entry
) {
256 if(iter
->port
== port
&& name
.len
== strlenW(iter
->name
) && !strncmpW(iter
->name
, name
.str
, name
.len
)
257 && iter
->is_https
== is_https
) {
259 server_addref(server
);
264 if(!server
&& do_create
) {
265 server
= heap_alloc_zero(sizeof(*server
));
267 server
->ref
= 2; /* list reference and return */
269 server
->is_https
= is_https
;
270 list_init(&server
->conn_pool
);
271 server
->name
= heap_strndupW(name
.str
, name
.len
);
272 if(server
->name
&& process_host_port(server
)) {
273 list_add_head(&connection_pool
, &server
->entry
);
281 LeaveCriticalSection(&connection_pool_cs
);
286 BOOL
collect_connections(collect_type_t collect_type
)
288 netconn_t
*netconn
, *netconn_safe
;
289 server_t
*server
, *server_safe
;
290 BOOL remaining
= FALSE
;
294 now
= GetTickCount();
296 now
= GetTickCount64();
299 LIST_FOR_EACH_ENTRY_SAFE(server
, server_safe
, &connection_pool
, server_t
, entry
) {
300 LIST_FOR_EACH_ENTRY_SAFE(netconn
, netconn_safe
, &server
->conn_pool
, netconn_t
, pool_entry
) {
301 if(collect_type
> COLLECT_TIMEOUT
|| netconn
->keep_until
< now
) {
302 TRACE("freeing %p\n", netconn
);
303 list_remove(&netconn
->pool_entry
);
304 free_netconn(netconn
);
310 if(collect_type
== COLLECT_CLEANUP
) {
311 list_remove(&server
->entry
);
312 list_init(&server
->entry
);
313 server_release(server
);
320 static DWORD WINAPI
collect_connections_proc(void *arg
)
322 BOOL remaining_conns
;
325 /* FIXME: Use more sophisticated method */
328 EnterCriticalSection(&connection_pool_cs
);
330 remaining_conns
= collect_connections(COLLECT_TIMEOUT
);
332 collector_running
= FALSE
;
334 LeaveCriticalSection(&connection_pool_cs
);
335 }while(remaining_conns
);
337 FreeLibraryAndExitThread(WININET_hModule
, 0);
340 /***********************************************************************
341 * HTTP_GetHeader (internal)
343 * Headers section must be held
345 static LPHTTPHEADERW
HTTP_GetHeader(http_request_t
*req
, LPCWSTR head
)
348 HeaderIndex
= HTTP_GetCustomHeaderIndex(req
, head
, 0, TRUE
);
349 if (HeaderIndex
== -1)
352 return &req
->custHeaders
[HeaderIndex
];
355 static WCHAR
*get_host_header( http_request_t
*req
)
360 EnterCriticalSection( &req
->headers_section
);
361 if ((header
= HTTP_GetHeader( req
, hostW
))) ret
= heap_strdupW( header
->lpszValue
);
362 else ret
= heap_strdupW( req
->server
->canon_host_port
);
363 LeaveCriticalSection( &req
->headers_section
);
367 struct data_stream_vtbl_t
{
368 BOOL (*end_of_data
)(data_stream_t
*,http_request_t
*);
369 DWORD (*read
)(data_stream_t
*,http_request_t
*,BYTE
*,DWORD
,DWORD
*,BOOL
);
370 DWORD (*drain_content
)(data_stream_t
*,http_request_t
*,BOOL
);
371 void (*destroy
)(data_stream_t
*);
375 data_stream_t data_stream
;
377 BYTE buf
[READ_BUFFER_SIZE
];
383 CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
,
384 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
,
385 CHUNKED_STREAM_STATE_READING_CHUNK
,
386 CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
,
387 CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
,
388 CHUNKED_STREAM_STATE_END_OF_STREAM
,
389 CHUNKED_STREAM_STATE_ERROR
393 static inline void destroy_data_stream(data_stream_t
*stream
)
395 stream
->vtbl
->destroy(stream
);
398 static void reset_data_stream(http_request_t
*req
)
400 destroy_data_stream(req
->data_stream
);
401 req
->data_stream
= &req
->netconn_stream
.data_stream
;
402 req
->read_pos
= req
->read_size
= req
->netconn_stream
.content_read
= 0;
403 req
->read_gzip
= FALSE
;
406 static void remove_header( http_request_t
*request
, const WCHAR
*str
, BOOL from_request
)
409 EnterCriticalSection( &request
->headers_section
);
410 index
= HTTP_GetCustomHeaderIndex( request
, str
, 0, from_request
);
411 if (index
!= -1) HTTP_DeleteCustomHeader( request
, index
);
412 LeaveCriticalSection( &request
->headers_section
);
418 data_stream_t stream
;
419 data_stream_t
*parent_stream
;
421 BYTE buf
[READ_BUFFER_SIZE
];
427 static BOOL
gzip_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
429 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
430 return gzip_stream
->end_of_data
431 || (!gzip_stream
->buf_size
&& gzip_stream
->parent_stream
->vtbl
->end_of_data(gzip_stream
->parent_stream
, req
));
434 static DWORD
gzip_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
435 DWORD
*read
, BOOL allow_blocking
)
437 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
438 z_stream
*zstream
= &gzip_stream
->zstream
;
439 DWORD current_read
, ret_read
= 0;
441 DWORD res
= ERROR_SUCCESS
;
443 TRACE("(%d %x)\n", size
, allow_blocking
);
445 while(size
&& !gzip_stream
->end_of_data
) {
446 if(!gzip_stream
->buf_size
) {
447 if(gzip_stream
->buf_pos
) {
448 if(gzip_stream
->buf_size
)
449 memmove(gzip_stream
->buf
, gzip_stream
->buf
+gzip_stream
->buf_pos
, gzip_stream
->buf_size
);
450 gzip_stream
->buf_pos
= 0;
452 res
= gzip_stream
->parent_stream
->vtbl
->read(gzip_stream
->parent_stream
, req
, gzip_stream
->buf
+gzip_stream
->buf_size
,
453 sizeof(gzip_stream
->buf
)-gzip_stream
->buf_size
, ¤t_read
, allow_blocking
);
454 if(res
!= ERROR_SUCCESS
)
457 gzip_stream
->buf_size
+= current_read
;
459 WARN("unexpected end of data\n");
460 gzip_stream
->end_of_data
= TRUE
;
465 zstream
->next_in
= gzip_stream
->buf
+gzip_stream
->buf_pos
;
466 zstream
->avail_in
= gzip_stream
->buf_size
;
467 zstream
->next_out
= buf
+ret_read
;
468 zstream
->avail_out
= size
;
469 zres
= inflate(&gzip_stream
->zstream
, 0);
470 current_read
= size
- zstream
->avail_out
;
471 size
-= current_read
;
472 ret_read
+= current_read
;
473 gzip_stream
->buf_size
-= zstream
->next_in
- (gzip_stream
->buf
+gzip_stream
->buf_pos
);
474 gzip_stream
->buf_pos
= zstream
->next_in
-gzip_stream
->buf
;
475 if(zres
== Z_STREAM_END
) {
476 TRACE("end of data\n");
477 gzip_stream
->end_of_data
= TRUE
;
479 }else if(zres
!= Z_OK
) {
480 WARN("inflate failed %d: %s\n", zres
, debugstr_a(zstream
->msg
));
482 res
= ERROR_INTERNET_DECODING_FAILED
;
487 allow_blocking
= FALSE
;
490 TRACE("read %u bytes\n", ret_read
);
497 static DWORD
gzip_drain_content(data_stream_t
*stream
, http_request_t
*req
, BOOL allow_blocking
)
499 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
500 return gzip_stream
->parent_stream
->vtbl
->drain_content(gzip_stream
->parent_stream
, req
, allow_blocking
);
503 static void gzip_destroy(data_stream_t
*stream
)
505 gzip_stream_t
*gzip_stream
= (gzip_stream_t
*)stream
;
507 destroy_data_stream(gzip_stream
->parent_stream
);
509 if(!gzip_stream
->end_of_data
)
510 inflateEnd(&gzip_stream
->zstream
);
511 heap_free(gzip_stream
);
514 static const data_stream_vtbl_t gzip_stream_vtbl
= {
521 static voidpf
wininet_zalloc(voidpf opaque
, uInt items
, uInt size
)
523 return heap_alloc(items
*size
);
526 static void wininet_zfree(voidpf opaque
, voidpf address
)
531 static DWORD
init_gzip_stream(http_request_t
*req
, BOOL is_gzip
)
533 gzip_stream_t
*gzip_stream
;
536 gzip_stream
= heap_alloc_zero(sizeof(gzip_stream_t
));
538 return ERROR_OUTOFMEMORY
;
540 gzip_stream
->stream
.vtbl
= &gzip_stream_vtbl
;
541 gzip_stream
->zstream
.zalloc
= wininet_zalloc
;
542 gzip_stream
->zstream
.zfree
= wininet_zfree
;
544 zres
= inflateInit2(&gzip_stream
->zstream
, is_gzip
? 0x1f : -15);
546 ERR("inflateInit failed: %d\n", zres
);
547 heap_free(gzip_stream
);
548 return ERROR_OUTOFMEMORY
;
551 remove_header(req
, szContent_Length
, FALSE
);
554 memcpy(gzip_stream
->buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
555 gzip_stream
->buf_size
= req
->read_size
;
556 req
->read_pos
= req
->read_size
= 0;
559 req
->read_gzip
= TRUE
;
560 gzip_stream
->parent_stream
= req
->data_stream
;
561 req
->data_stream
= &gzip_stream
->stream
;
562 return ERROR_SUCCESS
;
567 static DWORD
init_gzip_stream(http_request_t
*req
, BOOL is_gzip
)
569 ERR("gzip stream not supported, missing zlib.\n");
570 return ERROR_SUCCESS
;
575 /***********************************************************************
576 * HTTP_FreeTokens (internal)
578 * Frees table of pointers.
580 static void HTTP_FreeTokens(LPWSTR
* token_array
)
583 for (i
= 0; token_array
[i
]; i
++) heap_free(token_array
[i
]);
584 heap_free(token_array
);
587 static void HTTP_FixURL(http_request_t
*request
)
589 static const WCHAR szSlash
[] = { '/',0 };
590 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/', 0 };
592 /* If we don't have a path we set it to root */
593 if (NULL
== request
->path
)
594 request
->path
= heap_strdupW(szSlash
);
595 else /* remove \r and \n*/
597 int nLen
= strlenW(request
->path
);
598 while ((nLen
>0 ) && ((request
->path
[nLen
-1] == '\r')||(request
->path
[nLen
-1] == '\n')))
601 request
->path
[nLen
]='\0';
603 /* Replace '\' with '/' */
606 if (request
->path
[nLen
] == '\\') request
->path
[nLen
]='/';
610 if(CSTR_EQUAL
!= CompareStringW( LOCALE_INVARIANT
, NORM_IGNORECASE
,
611 request
->path
, strlenW(request
->path
), szHttp
, strlenW(szHttp
) )
612 && request
->path
[0] != '/') /* not an absolute path ?? --> fix it !! */
614 WCHAR
*fixurl
= heap_alloc((strlenW(request
->path
) + 2)*sizeof(WCHAR
));
616 strcpyW(fixurl
+ 1, request
->path
);
617 heap_free( request
->path
);
618 request
->path
= fixurl
;
622 static WCHAR
* build_request_header(http_request_t
*request
, const WCHAR
*verb
,
623 const WCHAR
*path
, const WCHAR
*version
, BOOL use_cr
)
625 static const WCHAR szSpace
[] = {' ',0};
626 static const WCHAR szColon
[] = {':',' ',0};
627 static const WCHAR szCr
[] = {'\r',0};
628 static const WCHAR szLf
[] = {'\n',0};
629 LPWSTR requestString
;
634 EnterCriticalSection( &request
->headers_section
);
636 /* allocate space for an array of all the string pointers to be added */
637 len
= request
->nCustHeaders
* 5 + 10;
638 if (!(req
= heap_alloc( len
* sizeof(const WCHAR
*) )))
640 LeaveCriticalSection( &request
->headers_section
);
644 /* add the verb, path and HTTP version string */
655 /* Append custom request headers */
656 for (i
= 0; i
< request
->nCustHeaders
; i
++)
658 if (request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
660 req
[n
++] = request
->custHeaders
[i
].lpszField
;
662 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
667 TRACE("Adding custom header %s (%s)\n",
668 debugstr_w(request
->custHeaders
[i
].lpszField
),
669 debugstr_w(request
->custHeaders
[i
].lpszValue
));
677 requestString
= HTTP_build_req( req
, 4 );
679 LeaveCriticalSection( &request
->headers_section
);
680 return requestString
;
683 static WCHAR
* build_response_header(http_request_t
*request
, BOOL use_cr
)
685 static const WCHAR colonW
[] = { ':',' ',0 };
686 static const WCHAR crW
[] = { '\r',0 };
687 static const WCHAR lfW
[] = { '\n',0 };
688 static const WCHAR status_fmt
[] = { ' ','%','u',' ',0 };
693 EnterCriticalSection( &request
->headers_section
);
695 if (!(req
= heap_alloc( (request
->nCustHeaders
* 5 + 8) * sizeof(WCHAR
*) )))
697 LeaveCriticalSection( &request
->headers_section
);
701 if (request
->status_code
)
703 req
[n
++] = request
->version
;
704 sprintfW(buf
, status_fmt
, request
->status_code
);
706 req
[n
++] = request
->statusText
;
712 for(i
= 0; i
< request
->nCustHeaders
; i
++)
714 if(!(request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
715 && strcmpW(request
->custHeaders
[i
].lpszField
, szStatus
))
717 req
[n
++] = request
->custHeaders
[i
].lpszField
;
719 req
[n
++] = request
->custHeaders
[i
].lpszValue
;
724 TRACE("Adding custom header %s (%s)\n",
725 debugstr_w(request
->custHeaders
[i
].lpszField
),
726 debugstr_w(request
->custHeaders
[i
].lpszValue
));
734 ret
= HTTP_build_req(req
, 0);
736 LeaveCriticalSection( &request
->headers_section
);
740 static void HTTP_ProcessCookies( http_request_t
*request
)
744 LPHTTPHEADERW setCookieHeader
;
747 if(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
)
750 path
= heap_strdupW(request
->path
);
754 tmp
= strrchrW(path
, '/');
755 if (tmp
&& tmp
[1]) tmp
[1] = 0;
757 EnterCriticalSection( &request
->headers_section
);
759 while((HeaderIndex
= HTTP_GetCustomHeaderIndex(request
, szSet_Cookie
, numCookies
++, FALSE
)) != -1)
764 setCookieHeader
= &request
->custHeaders
[HeaderIndex
];
766 if (!setCookieHeader
->lpszValue
)
769 data
= strchrW(setCookieHeader
->lpszValue
, '=');
773 name
= substr(setCookieHeader
->lpszValue
, data
- setCookieHeader
->lpszValue
);
775 set_cookie(substrz(request
->server
->name
), substrz(path
), name
, substrz(data
), INTERNET_COOKIE_HTTPONLY
);
778 LeaveCriticalSection( &request
->headers_section
);
782 static void strip_spaces(LPWSTR start
)
791 memmove(start
, str
, sizeof(WCHAR
) * (strlenW(str
) + 1));
793 end
= start
+ strlenW(start
) - 1;
794 while (end
>= start
&& *end
== ' ')
801 static inline BOOL
is_basic_auth_value( LPCWSTR pszAuthValue
, LPWSTR
*pszRealm
)
803 static const WCHAR szBasic
[] = {'B','a','s','i','c'}; /* Note: not nul-terminated */
804 static const WCHAR szRealm
[] = {'r','e','a','l','m'}; /* Note: not nul-terminated */
806 is_basic
= !strncmpiW(pszAuthValue
, szBasic
, ARRAYSIZE(szBasic
)) &&
807 ((pszAuthValue
[ARRAYSIZE(szBasic
)] == ' ') || !pszAuthValue
[ARRAYSIZE(szBasic
)]);
808 if (is_basic
&& pszRealm
)
811 LPCWSTR ptr
= &pszAuthValue
[ARRAYSIZE(szBasic
)];
815 token
= strchrW(ptr
,'=');
819 while (*realm
== ' ')
821 if(!strncmpiW(realm
, szRealm
, ARRAYSIZE(szRealm
)) &&
822 (realm
[ARRAYSIZE(szRealm
)] == ' ' || realm
[ARRAYSIZE(szRealm
)] == '='))
825 while (*token
== ' ')
829 *pszRealm
= heap_strdupW(token
);
830 strip_spaces(*pszRealm
);
837 static void destroy_authinfo( struct HttpAuthInfo
*authinfo
)
839 if (!authinfo
) return;
841 if (SecIsValidHandle(&authinfo
->ctx
))
842 DeleteSecurityContext(&authinfo
->ctx
);
843 if (SecIsValidHandle(&authinfo
->cred
))
844 FreeCredentialsHandle(&authinfo
->cred
);
846 heap_free(authinfo
->auth_data
);
847 heap_free(authinfo
->scheme
);
851 static UINT
retrieve_cached_basic_authorization(const WCHAR
*host
, const WCHAR
*realm
, char **auth_data
)
853 basicAuthorizationData
*ad
;
856 TRACE("Looking for authorization for %s:%s\n",debugstr_w(host
),debugstr_w(realm
));
858 EnterCriticalSection(&authcache_cs
);
859 LIST_FOR_EACH_ENTRY(ad
, &basicAuthorizationCache
, basicAuthorizationData
, entry
)
861 if (!strcmpiW(host
, ad
->host
) && (!realm
|| !strcmpW(realm
, ad
->realm
)))
863 TRACE("Authorization found in cache\n");
864 *auth_data
= heap_alloc(ad
->authorizationLen
);
865 memcpy(*auth_data
,ad
->authorization
,ad
->authorizationLen
);
866 rc
= ad
->authorizationLen
;
870 LeaveCriticalSection(&authcache_cs
);
874 static void cache_basic_authorization(LPWSTR host
, LPWSTR realm
, LPSTR auth_data
, UINT auth_data_len
)
877 basicAuthorizationData
* ad
= NULL
;
879 TRACE("caching authorization for %s:%s = %s\n",debugstr_w(host
),debugstr_w(realm
),debugstr_an(auth_data
,auth_data_len
));
881 EnterCriticalSection(&authcache_cs
);
882 LIST_FOR_EACH(cursor
, &basicAuthorizationCache
)
884 basicAuthorizationData
*check
= LIST_ENTRY(cursor
,basicAuthorizationData
,entry
);
885 if (!strcmpiW(host
,check
->host
) && !strcmpW(realm
,check
->realm
))
894 TRACE("Found match in cache, replacing\n");
895 heap_free(ad
->authorization
);
896 ad
->authorization
= heap_alloc(auth_data_len
);
897 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
898 ad
->authorizationLen
= auth_data_len
;
902 ad
= heap_alloc(sizeof(basicAuthorizationData
));
903 ad
->host
= heap_strdupW(host
);
904 ad
->realm
= heap_strdupW(realm
);
905 ad
->authorization
= heap_alloc(auth_data_len
);
906 memcpy(ad
->authorization
, auth_data
, auth_data_len
);
907 ad
->authorizationLen
= auth_data_len
;
908 list_add_head(&basicAuthorizationCache
,&ad
->entry
);
909 TRACE("authorization cached\n");
911 LeaveCriticalSection(&authcache_cs
);
914 static BOOL
retrieve_cached_authorization(LPWSTR host
, LPWSTR scheme
,
915 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
917 authorizationData
*ad
;
919 TRACE("Looking for authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
921 EnterCriticalSection(&authcache_cs
);
922 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
) {
923 if(!strcmpiW(host
, ad
->host
) && !strcmpiW(scheme
, ad
->scheme
)) {
924 TRACE("Authorization found in cache\n");
926 nt_auth_identity
->User
= heap_strdupW(ad
->user
);
927 nt_auth_identity
->Password
= heap_strdupW(ad
->password
);
928 nt_auth_identity
->Domain
= heap_alloc(sizeof(WCHAR
)*ad
->domain_len
);
929 if(!nt_auth_identity
->User
|| !nt_auth_identity
->Password
||
930 (!nt_auth_identity
->Domain
&& ad
->domain_len
)) {
931 heap_free(nt_auth_identity
->User
);
932 heap_free(nt_auth_identity
->Password
);
933 heap_free(nt_auth_identity
->Domain
);
937 nt_auth_identity
->Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
938 nt_auth_identity
->UserLength
= ad
->user_len
;
939 nt_auth_identity
->PasswordLength
= ad
->password_len
;
940 memcpy(nt_auth_identity
->Domain
, ad
->domain
, sizeof(WCHAR
)*ad
->domain_len
);
941 nt_auth_identity
->DomainLength
= ad
->domain_len
;
942 LeaveCriticalSection(&authcache_cs
);
946 LeaveCriticalSection(&authcache_cs
);
951 static void cache_authorization(LPWSTR host
, LPWSTR scheme
,
952 SEC_WINNT_AUTH_IDENTITY_W
*nt_auth_identity
)
954 authorizationData
*ad
;
957 TRACE("Caching authorization for %s:%s\n", debugstr_w(host
), debugstr_w(scheme
));
959 EnterCriticalSection(&authcache_cs
);
960 LIST_FOR_EACH_ENTRY(ad
, &authorizationCache
, authorizationData
, entry
)
961 if(!strcmpiW(host
, ad
->host
) && !strcmpiW(scheme
, ad
->scheme
)) {
968 heap_free(ad
->password
);
969 heap_free(ad
->domain
);
971 ad
= heap_alloc(sizeof(authorizationData
));
973 LeaveCriticalSection(&authcache_cs
);
977 ad
->host
= heap_strdupW(host
);
978 ad
->scheme
= heap_strdupW(scheme
);
979 list_add_head(&authorizationCache
, &ad
->entry
);
982 ad
->user
= heap_strndupW(nt_auth_identity
->User
, nt_auth_identity
->UserLength
);
983 ad
->password
= heap_strndupW(nt_auth_identity
->Password
, nt_auth_identity
->PasswordLength
);
984 ad
->domain
= heap_strndupW(nt_auth_identity
->Domain
, nt_auth_identity
->DomainLength
);
985 ad
->user_len
= nt_auth_identity
->UserLength
;
986 ad
->password_len
= nt_auth_identity
->PasswordLength
;
987 ad
->domain_len
= nt_auth_identity
->DomainLength
;
989 if(!ad
->host
|| !ad
->scheme
|| !ad
->user
|| !ad
->password
990 || (nt_auth_identity
->Domain
&& !ad
->domain
)) {
992 heap_free(ad
->scheme
);
994 heap_free(ad
->password
);
995 heap_free(ad
->domain
);
996 list_remove(&ad
->entry
);
1000 LeaveCriticalSection(&authcache_cs
);
1003 static BOOL
HTTP_DoAuthorization( http_request_t
*request
, LPCWSTR pszAuthValue
,
1004 struct HttpAuthInfo
**ppAuthInfo
,
1005 LPWSTR domain_and_username
, LPWSTR password
,
1008 SECURITY_STATUS sec_status
;
1009 struct HttpAuthInfo
*pAuthInfo
= *ppAuthInfo
;
1011 LPWSTR szRealm
= NULL
;
1013 TRACE("%s\n", debugstr_w(pszAuthValue
));
1020 pAuthInfo
= heap_alloc(sizeof(*pAuthInfo
));
1024 SecInvalidateHandle(&pAuthInfo
->cred
);
1025 SecInvalidateHandle(&pAuthInfo
->ctx
);
1026 memset(&pAuthInfo
->exp
, 0, sizeof(pAuthInfo
->exp
));
1027 pAuthInfo
->attr
= 0;
1028 pAuthInfo
->auth_data
= NULL
;
1029 pAuthInfo
->auth_data_len
= 0;
1030 pAuthInfo
->finished
= FALSE
;
1032 if (is_basic_auth_value(pszAuthValue
,NULL
))
1034 static const WCHAR szBasic
[] = {'B','a','s','i','c',0};
1035 pAuthInfo
->scheme
= heap_strdupW(szBasic
);
1036 if (!pAuthInfo
->scheme
)
1038 heap_free(pAuthInfo
);
1045 SEC_WINNT_AUTH_IDENTITY_W nt_auth_identity
;
1047 pAuthInfo
->scheme
= heap_strdupW(pszAuthValue
);
1048 if (!pAuthInfo
->scheme
)
1050 heap_free(pAuthInfo
);
1054 if (domain_and_username
)
1056 WCHAR
*user
= strchrW(domain_and_username
, '\\');
1057 WCHAR
*domain
= domain_and_username
;
1059 /* FIXME: make sure scheme accepts SEC_WINNT_AUTH_IDENTITY before calling AcquireCredentialsHandle */
1061 pAuthData
= &nt_auth_identity
;
1066 user
= domain_and_username
;
1070 nt_auth_identity
.Flags
= SEC_WINNT_AUTH_IDENTITY_UNICODE
;
1071 nt_auth_identity
.User
= user
;
1072 nt_auth_identity
.UserLength
= strlenW(nt_auth_identity
.User
);
1073 nt_auth_identity
.Domain
= domain
;
1074 nt_auth_identity
.DomainLength
= domain
? user
- domain
- 1 : 0;
1075 nt_auth_identity
.Password
= password
;
1076 nt_auth_identity
.PasswordLength
= strlenW(nt_auth_identity
.Password
);
1078 cache_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
);
1080 else if(retrieve_cached_authorization(host
, pAuthInfo
->scheme
, &nt_auth_identity
))
1081 pAuthData
= &nt_auth_identity
;
1083 /* use default credentials */
1086 sec_status
= AcquireCredentialsHandleW(NULL
, pAuthInfo
->scheme
,
1087 SECPKG_CRED_OUTBOUND
, NULL
,
1089 NULL
, &pAuthInfo
->cred
,
1092 if(pAuthData
&& !domain_and_username
) {
1093 heap_free(nt_auth_identity
.User
);
1094 heap_free(nt_auth_identity
.Domain
);
1095 heap_free(nt_auth_identity
.Password
);
1098 if (sec_status
== SEC_E_OK
)
1100 PSecPkgInfoW sec_pkg_info
;
1101 sec_status
= QuerySecurityPackageInfoW(pAuthInfo
->scheme
, &sec_pkg_info
);
1102 if (sec_status
== SEC_E_OK
)
1104 pAuthInfo
->max_token
= sec_pkg_info
->cbMaxToken
;
1105 FreeContextBuffer(sec_pkg_info
);
1108 if (sec_status
!= SEC_E_OK
)
1110 WARN("AcquireCredentialsHandleW for scheme %s failed with error 0x%08x\n",
1111 debugstr_w(pAuthInfo
->scheme
), sec_status
);
1112 heap_free(pAuthInfo
->scheme
);
1113 heap_free(pAuthInfo
);
1117 *ppAuthInfo
= pAuthInfo
;
1119 else if (pAuthInfo
->finished
)
1122 if ((strlenW(pszAuthValue
) < strlenW(pAuthInfo
->scheme
)) ||
1123 strncmpiW(pszAuthValue
, pAuthInfo
->scheme
, strlenW(pAuthInfo
->scheme
)))
1125 ERR("authentication scheme changed from %s to %s\n",
1126 debugstr_w(pAuthInfo
->scheme
), debugstr_w(pszAuthValue
));
1130 if (is_basic_auth_value(pszAuthValue
,&szRealm
))
1134 char *auth_data
= NULL
;
1135 UINT auth_data_len
= 0;
1137 TRACE("basic authentication realm %s\n",debugstr_w(szRealm
));
1139 if (!domain_and_username
)
1141 if (host
&& szRealm
)
1142 auth_data_len
= retrieve_cached_basic_authorization(host
, szRealm
,&auth_data
);
1143 if (auth_data_len
== 0)
1151 userlen
= WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, lstrlenW(domain_and_username
), NULL
, 0, NULL
, NULL
);
1152 passlen
= WideCharToMultiByte(CP_UTF8
, 0, password
, lstrlenW(password
), NULL
, 0, NULL
, NULL
);
1154 /* length includes a nul terminator, which will be re-used for the ':' */
1155 auth_data
= heap_alloc(userlen
+ 1 + passlen
);
1162 WideCharToMultiByte(CP_UTF8
, 0, domain_and_username
, -1, auth_data
, userlen
, NULL
, NULL
);
1163 auth_data
[userlen
] = ':';
1164 WideCharToMultiByte(CP_UTF8
, 0, password
, -1, &auth_data
[userlen
+1], passlen
, NULL
, NULL
);
1165 auth_data_len
= userlen
+ 1 + passlen
;
1166 if (host
&& szRealm
)
1167 cache_basic_authorization(host
, szRealm
, auth_data
, auth_data_len
);
1170 pAuthInfo
->auth_data
= auth_data
;
1171 pAuthInfo
->auth_data_len
= auth_data_len
;
1172 pAuthInfo
->finished
= TRUE
;
1178 LPCWSTR pszAuthData
;
1179 SecBufferDesc out_desc
, in_desc
;
1181 unsigned char *buffer
;
1182 ULONG context_req
= ISC_REQ_CONNECTION
| ISC_REQ_USE_DCE_STYLE
|
1183 ISC_REQ_MUTUAL_AUTH
| ISC_REQ_DELEGATE
;
1185 in
.BufferType
= SECBUFFER_TOKEN
;
1189 in_desc
.ulVersion
= 0;
1190 in_desc
.cBuffers
= 1;
1191 in_desc
.pBuffers
= &in
;
1193 pszAuthData
= pszAuthValue
+ strlenW(pAuthInfo
->scheme
);
1194 if (*pszAuthData
== ' ')
1197 in
.cbBuffer
= HTTP_DecodeBase64(pszAuthData
, NULL
);
1198 in
.pvBuffer
= heap_alloc(in
.cbBuffer
);
1199 HTTP_DecodeBase64(pszAuthData
, in
.pvBuffer
);
1202 buffer
= heap_alloc(pAuthInfo
->max_token
);
1204 out
.BufferType
= SECBUFFER_TOKEN
;
1205 out
.cbBuffer
= pAuthInfo
->max_token
;
1206 out
.pvBuffer
= buffer
;
1208 out_desc
.ulVersion
= 0;
1209 out_desc
.cBuffers
= 1;
1210 out_desc
.pBuffers
= &out
;
1212 sec_status
= InitializeSecurityContextW(first
? &pAuthInfo
->cred
: NULL
,
1213 first
? NULL
: &pAuthInfo
->ctx
,
1214 first
? request
->server
->name
: NULL
,
1215 context_req
, 0, SECURITY_NETWORK_DREP
,
1216 in
.pvBuffer
? &in_desc
: NULL
,
1217 0, &pAuthInfo
->ctx
, &out_desc
,
1218 &pAuthInfo
->attr
, &pAuthInfo
->exp
);
1219 if (sec_status
== SEC_E_OK
)
1221 pAuthInfo
->finished
= TRUE
;
1222 pAuthInfo
->auth_data
= out
.pvBuffer
;
1223 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1224 TRACE("sending last auth packet\n");
1226 else if (sec_status
== SEC_I_CONTINUE_NEEDED
)
1228 pAuthInfo
->auth_data
= out
.pvBuffer
;
1229 pAuthInfo
->auth_data_len
= out
.cbBuffer
;
1230 TRACE("sending next auth packet\n");
1234 ERR("InitializeSecurityContextW returned error 0x%08x\n", sec_status
);
1235 heap_free(out
.pvBuffer
);
1236 destroy_authinfo(pAuthInfo
);
1245 /***********************************************************************
1246 * HTTP_HttpAddRequestHeadersW (internal)
1248 static DWORD
HTTP_HttpAddRequestHeadersW(http_request_t
*request
,
1249 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1254 DWORD len
, res
= ERROR_HTTP_INVALID_HEADER
;
1256 TRACE("copying header: %s\n", debugstr_wn(lpszHeader
, dwHeaderLength
));
1258 if( dwHeaderLength
== ~0U )
1259 len
= strlenW(lpszHeader
);
1261 len
= dwHeaderLength
;
1262 buffer
= heap_alloc(sizeof(WCHAR
)*(len
+1));
1263 lstrcpynW( buffer
, lpszHeader
, len
+ 1);
1269 LPWSTR
* pFieldAndValue
;
1271 lpszEnd
= lpszStart
;
1273 while (*lpszEnd
!= '\0')
1275 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1280 if (*lpszStart
== '\0')
1283 if (*lpszEnd
== '\r' || *lpszEnd
== '\n')
1286 lpszEnd
++; /* Jump over newline */
1288 TRACE("interpreting header %s\n", debugstr_w(lpszStart
));
1289 if (*lpszStart
== '\0')
1291 /* Skip 0-length headers */
1292 lpszStart
= lpszEnd
;
1293 res
= ERROR_SUCCESS
;
1296 pFieldAndValue
= HTTP_InterpretHttpHeader(lpszStart
);
1299 res
= HTTP_ProcessHeader(request
, pFieldAndValue
[0],
1300 pFieldAndValue
[1], dwModifier
| HTTP_ADDHDR_FLAG_REQ
);
1301 HTTP_FreeTokens(pFieldAndValue
);
1304 lpszStart
= lpszEnd
;
1305 } while (res
== ERROR_SUCCESS
);
1311 /***********************************************************************
1312 * HttpAddRequestHeadersW (WININET.@)
1314 * Adds one or more HTTP header to the request handler
1317 * On Windows if dwHeaderLength includes the trailing '\0', then
1318 * HttpAddRequestHeadersW() adds it too. However this results in an
1319 * invalid HTTP header which is rejected by some servers so we probably
1320 * don't need to match Windows on that point.
1327 BOOL WINAPI
HttpAddRequestHeadersW(HINTERNET hHttpRequest
,
1328 LPCWSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1330 http_request_t
*request
;
1331 DWORD res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
1333 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_wn(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1338 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
1339 if (request
&& request
->hdr
.htype
== WH_HHTTPREQ
)
1340 res
= HTTP_HttpAddRequestHeadersW( request
, lpszHeader
, dwHeaderLength
, dwModifier
);
1342 WININET_Release( &request
->hdr
);
1344 if(res
!= ERROR_SUCCESS
)
1346 return res
== ERROR_SUCCESS
;
1349 /***********************************************************************
1350 * HttpAddRequestHeadersA (WININET.@)
1352 * Adds one or more HTTP header to the request handler
1359 BOOL WINAPI
HttpAddRequestHeadersA(HINTERNET hHttpRequest
,
1360 LPCSTR lpszHeader
, DWORD dwHeaderLength
, DWORD dwModifier
)
1362 WCHAR
*headers
= NULL
;
1365 TRACE("%p, %s, %i, %i\n", hHttpRequest
, debugstr_an(lpszHeader
, dwHeaderLength
), dwHeaderLength
, dwModifier
);
1368 headers
= heap_strndupAtoW(lpszHeader
, dwHeaderLength
, &dwHeaderLength
);
1370 r
= HttpAddRequestHeadersW(hHttpRequest
, headers
, dwHeaderLength
, dwModifier
);
1376 static void free_accept_types( WCHAR
**accept_types
)
1378 WCHAR
*ptr
, **types
= accept_types
;
1381 while ((ptr
= *types
))
1386 heap_free( accept_types
);
1389 static WCHAR
**convert_accept_types( const char **accept_types
)
1392 const char **types
= accept_types
;
1394 BOOL invalid_pointer
= FALSE
;
1396 if (!types
) return NULL
;
1402 /* find out how many there are */
1403 if (*types
&& **types
)
1405 TRACE("accept type: %s\n", debugstr_a(*types
));
1411 WARN("invalid accept type pointer\n");
1412 invalid_pointer
= TRUE
;
1417 if (invalid_pointer
) return NULL
;
1418 if (!(typesW
= heap_alloc( sizeof(WCHAR
*) * (count
+ 1) ))) return NULL
;
1420 types
= accept_types
;
1423 if (*types
&& **types
) typesW
[count
++] = heap_strdupAtoW( *types
);
1426 typesW
[count
] = NULL
;
1430 /***********************************************************************
1431 * HttpOpenRequestA (WININET.@)
1433 * Open a HTTP request handle
1436 * HINTERNET a HTTP request handle on success
1440 HINTERNET WINAPI
HttpOpenRequestA(HINTERNET hHttpSession
,
1441 LPCSTR lpszVerb
, LPCSTR lpszObjectName
, LPCSTR lpszVersion
,
1442 LPCSTR lpszReferrer
, LPCSTR
*lpszAcceptTypes
,
1443 DWORD dwFlags
, DWORD_PTR dwContext
)
1445 LPWSTR szVerb
= NULL
, szObjectName
= NULL
;
1446 LPWSTR szVersion
= NULL
, szReferrer
= NULL
, *szAcceptTypes
= NULL
;
1447 HINTERNET rc
= NULL
;
1449 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
1450 debugstr_a(lpszVerb
), debugstr_a(lpszObjectName
),
1451 debugstr_a(lpszVersion
), debugstr_a(lpszReferrer
), lpszAcceptTypes
,
1452 dwFlags
, dwContext
);
1456 szVerb
= heap_strdupAtoW(lpszVerb
);
1463 szObjectName
= heap_strdupAtoW(lpszObjectName
);
1464 if ( !szObjectName
)
1470 szVersion
= heap_strdupAtoW(lpszVersion
);
1477 szReferrer
= heap_strdupAtoW(lpszReferrer
);
1482 szAcceptTypes
= convert_accept_types( lpszAcceptTypes
);
1483 rc
= HttpOpenRequestW(hHttpSession
, szVerb
, szObjectName
, szVersion
, szReferrer
,
1484 (const WCHAR
**)szAcceptTypes
, dwFlags
, dwContext
);
1487 free_accept_types(szAcceptTypes
);
1488 heap_free(szReferrer
);
1489 heap_free(szVersion
);
1490 heap_free(szObjectName
);
1495 /***********************************************************************
1498 static UINT
HTTP_EncodeBase64( LPCSTR bin
, unsigned int len
, LPWSTR base64
)
1501 static const CHAR HTTP_Base64Enc
[] =
1502 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1506 /* first 6 bits, all from bin[0] */
1507 base64
[n
++] = HTTP_Base64Enc
[(bin
[0] & 0xfc) >> 2];
1508 x
= (bin
[0] & 3) << 4;
1510 /* next 6 bits, 2 from bin[0] and 4 from bin[1] */
1513 base64
[n
++] = HTTP_Base64Enc
[x
];
1518 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[1]&0xf0) >> 4 ) ];
1519 x
= ( bin
[1] & 0x0f ) << 2;
1521 /* next 6 bits 4 from bin[1] and 2 from bin[2] */
1524 base64
[n
++] = HTTP_Base64Enc
[x
];
1528 base64
[n
++] = HTTP_Base64Enc
[ x
| ( (bin
[2]&0xc0 ) >> 6 ) ];
1530 /* last 6 bits, all from bin [2] */
1531 base64
[n
++] = HTTP_Base64Enc
[ bin
[2] & 0x3f ];
1539 static const signed char HTTP_Base64Dec
[] =
1541 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00 */
1542 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10 */
1543 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 */
1544 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 */
1545 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 */
1546 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 */
1547 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 */
1548 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70 */
1551 /***********************************************************************
1554 static UINT
HTTP_DecodeBase64( LPCWSTR base64
, LPSTR bin
)
1562 if (base64
[0] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1563 ((in
[0] = HTTP_Base64Dec
[base64
[0]]) == -1) ||
1564 base64
[1] >= ARRAYSIZE(HTTP_Base64Dec
) ||
1565 ((in
[1] = HTTP_Base64Dec
[base64
[1]]) == -1))
1567 WARN("invalid base64: %s\n", debugstr_w(base64
));
1571 bin
[n
] = (unsigned char) (in
[0] << 2 | in
[1] >> 4);
1574 if ((base64
[2] == '=') && (base64
[3] == '='))
1576 if (base64
[2] > ARRAYSIZE(HTTP_Base64Dec
) ||
1577 ((in
[2] = HTTP_Base64Dec
[base64
[2]]) == -1))
1579 WARN("invalid base64: %s\n", debugstr_w(&base64
[2]));
1583 bin
[n
] = (unsigned char) (in
[1] << 4 | in
[2] >> 2);
1586 if (base64
[3] == '=')
1588 if (base64
[3] > ARRAYSIZE(HTTP_Base64Dec
) ||
1589 ((in
[3] = HTTP_Base64Dec
[base64
[3]]) == -1))
1591 WARN("invalid base64: %s\n", debugstr_w(&base64
[3]));
1595 bin
[n
] = (unsigned char) (((in
[2] << 6) & 0xc0) | in
[3]);
1604 static WCHAR
*encode_auth_data( const WCHAR
*scheme
, const char *data
, UINT data_len
)
1607 UINT len
, scheme_len
= strlenW( scheme
);
1609 /* scheme + space + base64 encoded data (3/2/1 bytes data -> 4 bytes of characters) */
1610 len
= scheme_len
+ 1 + ((data_len
+ 2) * 4) / 3;
1611 if (!(ret
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return NULL
;
1612 memcpy( ret
, scheme
, scheme_len
* sizeof(WCHAR
) );
1613 ret
[scheme_len
] = ' ';
1614 HTTP_EncodeBase64( data
, data_len
, ret
+ scheme_len
+ 1 );
1619 /***********************************************************************
1620 * HTTP_InsertAuthorization
1622 * Insert or delete the authorization field in the request header.
1624 static BOOL
HTTP_InsertAuthorization( http_request_t
*request
, struct HttpAuthInfo
*pAuthInfo
, LPCWSTR header
)
1626 static const WCHAR wszBasic
[] = {'B','a','s','i','c',0};
1627 WCHAR
*host
, *authorization
= NULL
;
1631 if (pAuthInfo
->auth_data_len
)
1633 if (!(authorization
= encode_auth_data(pAuthInfo
->scheme
, pAuthInfo
->auth_data
, pAuthInfo
->auth_data_len
)))
1636 /* clear the data as it isn't valid now that it has been sent to the
1637 * server, unless it's Basic authentication which doesn't do
1638 * connection tracking */
1639 if (strcmpiW(pAuthInfo
->scheme
, wszBasic
))
1641 heap_free(pAuthInfo
->auth_data
);
1642 pAuthInfo
->auth_data
= NULL
;
1643 pAuthInfo
->auth_data_len
= 0;
1647 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1649 HTTP_ProcessHeader(request
, header
, authorization
,
1650 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
1651 heap_free(authorization
);
1653 else if (!strcmpW(header
, szAuthorization
) && (host
= get_host_header(request
)))
1658 if ((data_len
= retrieve_cached_basic_authorization(host
, NULL
, &data
)))
1660 TRACE("Found cached basic authorization for %s\n", debugstr_w(host
));
1662 if (!(authorization
= encode_auth_data(wszBasic
, data
, data_len
)))
1669 TRACE("Inserting authorization: %s\n", debugstr_w(authorization
));
1671 HTTP_ProcessHeader(request
, header
, authorization
,
1672 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
1674 heap_free(authorization
);
1681 static WCHAR
*build_proxy_path_url(http_request_t
*req
)
1686 len
= strlenW(req
->server
->scheme_host_port
);
1687 size
= len
+ strlenW(req
->path
) + 1;
1688 if(*req
->path
!= '/')
1690 url
= heap_alloc(size
* sizeof(WCHAR
));
1694 memcpy(url
, req
->server
->scheme_host_port
, len
*sizeof(WCHAR
));
1695 if(*req
->path
!= '/')
1698 strcpyW(url
+len
, req
->path
);
1700 TRACE("url=%s\n", debugstr_w(url
));
1704 static BOOL
HTTP_DomainMatches(LPCWSTR server
, substr_t domain
)
1706 static const WCHAR localW
[] = { '<','l','o','c','a','l','>',0 };
1707 const WCHAR
*dot
, *ptr
;
1710 if(domain
.len
== sizeof(localW
)/sizeof(WCHAR
)-1 && !strncmpiW(domain
.str
, localW
, domain
.len
) && !strchrW(server
, '.' ))
1713 if(domain
.len
&& *domain
.str
!= '*')
1714 return domain
.len
== strlenW(server
) && !strncmpiW(server
, domain
.str
, domain
.len
);
1716 if(domain
.len
< 2 || domain
.str
[1] != '.')
1719 /* For a hostname to match a wildcard, the last domain must match
1720 * the wildcard exactly. E.g. if the wildcard is *.a.b, and the
1721 * hostname is www.foo.a.b, it matches, but a.b does not.
1723 dot
= strchrW(server
, '.');
1727 len
= strlenW(dot
+ 1);
1728 if(len
<= domain
.len
- 2)
1731 /* The server's domain is longer than the wildcard, so it
1732 * could be a subdomain. Compare the last portion of the
1735 ptr
= dot
+ 1 + len
- domain
.len
+ 2;
1736 if(!strncmpiW(ptr
, domain
.str
+2, domain
.len
-2))
1737 /* This is only a match if the preceding character is
1738 * a '.', i.e. that it is a matching domain. E.g.
1739 * if domain is '*.b.c' and server is 'www.ab.c' they
1742 return *(ptr
- 1) == '.';
1744 return len
== domain
.len
-2 && !strncmpiW(dot
+ 1, domain
.str
+ 2, len
);
1747 static BOOL
HTTP_ShouldBypassProxy(appinfo_t
*lpwai
, LPCWSTR server
)
1752 if (!lpwai
->proxyBypass
) return FALSE
;
1753 ptr
= lpwai
->proxyBypass
;
1757 ptr
= strchrW( ptr
, ';' );
1759 ptr
= strchrW( tmp
, ' ' );
1761 ptr
= tmp
+ strlenW(tmp
);
1762 ret
= HTTP_DomainMatches( server
, substr(tmp
, ptr
-tmp
) );
1770 /***********************************************************************
1771 * HTTP_DealWithProxy
1773 static BOOL
HTTP_DealWithProxy(appinfo_t
*hIC
, http_session_t
*session
, http_request_t
*request
)
1775 static const WCHAR protoHttp
[] = { 'h','t','t','p',0 };
1776 static const WCHAR szHttp
[] = { 'h','t','t','p',':','/','/',0 };
1777 static WCHAR szNul
[] = { 0 };
1778 URL_COMPONENTSW UrlComponents
= { sizeof(UrlComponents
) };
1779 server_t
*new_server
= NULL
;
1782 proxy
= INTERNET_FindProxyForProtocol(hIC
->proxy
, protoHttp
);
1785 if(CSTR_EQUAL
!= CompareStringW(LOCALE_SYSTEM_DEFAULT
, NORM_IGNORECASE
,
1786 proxy
, strlenW(szHttp
), szHttp
, strlenW(szHttp
))) {
1787 WCHAR
*proxy_url
= heap_alloc(strlenW(proxy
)*sizeof(WCHAR
) + sizeof(szHttp
));
1792 strcpyW(proxy_url
, szHttp
);
1793 strcatW(proxy_url
, proxy
);
1798 UrlComponents
.dwHostNameLength
= 1;
1799 if(InternetCrackUrlW(proxy
, 0, 0, &UrlComponents
) && UrlComponents
.dwHostNameLength
) {
1800 if( !request
->path
)
1801 request
->path
= szNul
;
1803 new_server
= get_server(substr(UrlComponents
.lpszHostName
, UrlComponents
.dwHostNameLength
),
1804 UrlComponents
.nPort
, UrlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
, TRUE
);
1810 request
->proxy
= new_server
;
1812 TRACE("proxy server=%s port=%d\n", debugstr_w(new_server
->name
), new_server
->port
);
1816 static DWORD
HTTP_ResolveName(http_request_t
*request
)
1818 server_t
*server
= request
->proxy
? request
->proxy
: request
->server
;
1821 if(server
->addr_len
)
1822 return ERROR_SUCCESS
;
1824 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1825 INTERNET_STATUS_RESOLVING_NAME
,
1827 (strlenW(server
->name
)+1) * sizeof(WCHAR
));
1829 addr_len
= sizeof(server
->addr
);
1830 if (!GetAddress(server
->name
, server
->port
, (SOCKADDR
*)&server
->addr
, &addr_len
, server
->addr_str
))
1831 return ERROR_INTERNET_NAME_NOT_RESOLVED
;
1833 server
->addr_len
= addr_len
;
1834 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
1835 INTERNET_STATUS_NAME_RESOLVED
,
1836 server
->addr_str
, strlen(server
->addr_str
)+1);
1838 TRACE("resolved %s to %s\n", debugstr_w(server
->name
), server
->addr_str
);
1839 return ERROR_SUCCESS
;
1842 static WCHAR
*compose_request_url(http_request_t
*req
)
1844 static const WCHAR http
[] = { 'h','t','t','p',':','/','/',0 };
1845 static const WCHAR https
[] = { 'h','t','t','p','s',':','/','/',0 };
1846 const WCHAR
*host
, *scheme
;
1850 host
= req
->server
->canon_host_port
;
1852 if (req
->server
->is_https
)
1857 len
= strlenW(scheme
) + strlenW(host
) + (req
->path
[0] != '/' ? 1 : 0) + strlenW(req
->path
);
1858 ptr
= buf
= heap_alloc((len
+1) * sizeof(WCHAR
));
1860 strcpyW(ptr
, scheme
);
1861 ptr
+= strlenW(ptr
);
1864 ptr
+= strlenW(ptr
);
1866 if(req
->path
[0] != '/')
1869 strcpyW(ptr
, req
->path
);
1870 ptr
+= strlenW(ptr
);
1878 /***********************************************************************
1879 * HTTPREQ_Destroy (internal)
1881 * Deallocate request handle
1884 static void HTTPREQ_Destroy(object_header_t
*hdr
)
1886 http_request_t
*request
= (http_request_t
*) hdr
;
1891 if(request
->hCacheFile
)
1892 CloseHandle(request
->hCacheFile
);
1893 if(request
->req_file
)
1894 req_file_release(request
->req_file
);
1896 request
->headers_section
.DebugInfo
->Spare
[0] = 0;
1897 DeleteCriticalSection( &request
->headers_section
);
1898 request
->read_section
.DebugInfo
->Spare
[0] = 0;
1899 DeleteCriticalSection( &request
->read_section
);
1900 WININET_Release(&request
->session
->hdr
);
1902 destroy_authinfo(request
->authInfo
);
1903 destroy_authinfo(request
->proxyAuthInfo
);
1906 server_release(request
->server
);
1908 server_release(request
->proxy
);
1910 heap_free(request
->path
);
1911 heap_free(request
->verb
);
1912 heap_free(request
->version
);
1913 heap_free(request
->statusText
);
1915 for (i
= 0; i
< request
->nCustHeaders
; i
++)
1917 heap_free(request
->custHeaders
[i
].lpszField
);
1918 heap_free(request
->custHeaders
[i
].lpszValue
);
1920 destroy_data_stream(request
->data_stream
);
1921 heap_free(request
->custHeaders
);
1924 static void http_release_netconn(http_request_t
*req
, BOOL reuse
)
1926 TRACE("%p %p %x\n",req
, req
->netconn
, reuse
);
1928 if(!is_valid_netconn(req
->netconn
))
1932 if(reuse
&& req
->netconn
->keep_alive
) {
1935 EnterCriticalSection(&connection_pool_cs
);
1937 list_add_head(&req
->netconn
->server
->conn_pool
, &req
->netconn
->pool_entry
);
1938 req
->netconn
->keep_until
= (DWORD64
)GetTickCount() + COLLECT_TIME
;
1939 req
->netconn
= NULL
;
1941 run_collector
= !collector_running
;
1942 collector_running
= TRUE
;
1944 LeaveCriticalSection(&connection_pool_cs
);
1947 HANDLE thread
= NULL
;
1950 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
, (const WCHAR
*)WININET_hModule
, &module
);
1952 thread
= CreateThread(NULL
, 0, collect_connections_proc
, NULL
, 0, NULL
);
1954 EnterCriticalSection(&connection_pool_cs
);
1955 collector_running
= FALSE
;
1956 LeaveCriticalSection(&connection_pool_cs
);
1959 FreeLibrary(module
);
1962 CloseHandle(thread
);
1967 /* Silence unused function warning */
1968 (void)collect_connections_proc
;
1971 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1972 INTERNET_STATUS_CLOSING_CONNECTION
, 0, 0);
1974 close_netconn(req
->netconn
);
1976 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
,
1977 INTERNET_STATUS_CONNECTION_CLOSED
, 0, 0);
1980 static BOOL
HTTP_KeepAlive(http_request_t
*request
)
1982 WCHAR szVersion
[10];
1983 WCHAR szConnectionResponse
[20];
1984 DWORD dwBufferSize
= sizeof(szVersion
);
1985 BOOL keepalive
= FALSE
;
1987 /* as per RFC 2068, S8.1.2.1, if the client is HTTP/1.1 then assume that
1988 * the connection is keep-alive by default */
1989 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_VERSION
, szVersion
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
1990 && !strcmpiW(szVersion
, g_szHttp1_1
))
1995 dwBufferSize
= sizeof(szConnectionResponse
);
1996 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_PROXY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
1997 || HTTP_HttpQueryInfoW(request
, HTTP_QUERY_CONNECTION
, szConnectionResponse
, &dwBufferSize
, NULL
) == ERROR_SUCCESS
)
1999 keepalive
= !strcmpiW(szConnectionResponse
, szKeepAlive
);
2005 static void HTTPREQ_CloseConnection(object_header_t
*hdr
)
2007 http_request_t
*req
= (http_request_t
*)hdr
;
2009 http_release_netconn(req
, drain_content(req
, FALSE
) == ERROR_SUCCESS
);
2012 static DWORD
str_to_buffer(const WCHAR
*str
, void *buffer
, DWORD
*size
, BOOL unicode
)
2017 WCHAR
*buf
= buffer
;
2019 if (str
) len
= strlenW(str
);
2021 if (*size
< (len
+ 1) * sizeof(WCHAR
))
2023 *size
= (len
+ 1) * sizeof(WCHAR
);
2024 return ERROR_INSUFFICIENT_BUFFER
;
2026 if (str
) strcpyW(buf
, str
);
2030 return ERROR_SUCCESS
;
2036 if (str
) len
= WideCharToMultiByte(CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
2041 return ERROR_INSUFFICIENT_BUFFER
;
2043 if (str
) WideCharToMultiByte(CP_ACP
, 0, str
, -1, buf
, *size
, NULL
, NULL
);
2047 return ERROR_SUCCESS
;
2051 static DWORD
HTTPREQ_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
2053 http_request_t
*req
= (http_request_t
*)hdr
;
2056 case INTERNET_OPTION_DIAGNOSTIC_SOCKET_INFO
:
2058 INTERNET_DIAGNOSTIC_SOCKET_INFO
*info
= buffer
;
2060 FIXME("INTERNET_DIAGNOSTIC_SOCKET_INFO stub\n");
2062 if (*size
< sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
))
2063 return ERROR_INSUFFICIENT_BUFFER
;
2064 *size
= sizeof(INTERNET_DIAGNOSTIC_SOCKET_INFO
);
2065 /* FIXME: can't get a SOCKET from our connection since we don't use
2069 /* FIXME: get source port from req->netConnection */
2070 info
->SourcePort
= 0;
2071 info
->DestPort
= req
->server
->port
;
2073 if (HTTP_KeepAlive(req
))
2074 info
->Flags
|= IDSI_FLAG_KEEP_ALIVE
;
2076 info
->Flags
|= IDSI_FLAG_PROXY
;
2077 if (is_valid_netconn(req
->netconn
) && req
->netconn
->secure
)
2078 info
->Flags
|= IDSI_FLAG_SECURE
;
2080 return ERROR_SUCCESS
;
2084 TRACE("Queried undocumented option 98, forwarding to INTERNET_OPTION_SECURITY_FLAGS\n");
2086 case INTERNET_OPTION_SECURITY_FLAGS
:
2090 if (*size
< sizeof(ULONG
))
2091 return ERROR_INSUFFICIENT_BUFFER
;
2093 *size
= sizeof(DWORD
);
2094 flags
= is_valid_netconn(req
->netconn
) ? req
->netconn
->security_flags
: req
->security_flags
| req
->server
->security_flags
;
2095 *(DWORD
*)buffer
= flags
;
2097 TRACE("INTERNET_OPTION_SECURITY_FLAGS %x\n", flags
);
2098 return ERROR_SUCCESS
;
2101 case INTERNET_OPTION_HANDLE_TYPE
:
2102 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
2104 if (*size
< sizeof(ULONG
))
2105 return ERROR_INSUFFICIENT_BUFFER
;
2107 *size
= sizeof(DWORD
);
2108 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_HTTP_REQUEST
;
2109 return ERROR_SUCCESS
;
2111 case INTERNET_OPTION_URL
: {
2115 TRACE("INTERNET_OPTION_URL\n");
2117 url
= compose_request_url(req
);
2119 return ERROR_OUTOFMEMORY
;
2121 res
= str_to_buffer(url
, buffer
, size
, unicode
);
2125 case INTERNET_OPTION_USER_AGENT
:
2126 return str_to_buffer(req
->session
->appInfo
->agent
, buffer
, size
, unicode
);
2127 case INTERNET_OPTION_USERNAME
:
2128 return str_to_buffer(req
->session
->userName
, buffer
, size
, unicode
);
2129 case INTERNET_OPTION_PASSWORD
:
2130 return str_to_buffer(req
->session
->password
, buffer
, size
, unicode
);
2131 case INTERNET_OPTION_PROXY_USERNAME
:
2132 return str_to_buffer(req
->session
->appInfo
->proxyUsername
, buffer
, size
, unicode
);
2133 case INTERNET_OPTION_PROXY_PASSWORD
:
2134 return str_to_buffer(req
->session
->appInfo
->proxyPassword
, buffer
, size
, unicode
);
2136 case INTERNET_OPTION_CACHE_TIMESTAMPS
: {
2137 INTERNET_CACHE_ENTRY_INFOW
*info
;
2138 INTERNET_CACHE_TIMESTAMPS
*ts
= buffer
;
2139 DWORD nbytes
, error
;
2142 TRACE("INTERNET_OPTION_CACHE_TIMESTAMPS\n");
2145 return ERROR_FILE_NOT_FOUND
;
2147 if (*size
< sizeof(*ts
))
2149 *size
= sizeof(*ts
);
2150 return ERROR_INSUFFICIENT_BUFFER
;
2154 ret
= GetUrlCacheEntryInfoW(req
->req_file
->url
, NULL
, &nbytes
);
2155 error
= GetLastError();
2156 if (!ret
&& error
== ERROR_INSUFFICIENT_BUFFER
)
2158 if (!(info
= heap_alloc(nbytes
)))
2159 return ERROR_OUTOFMEMORY
;
2161 GetUrlCacheEntryInfoW(req
->req_file
->url
, info
, &nbytes
);
2163 ts
->ftExpires
= info
->ExpireTime
;
2164 ts
->ftLastModified
= info
->LastModifiedTime
;
2167 *size
= sizeof(*ts
);
2168 return ERROR_SUCCESS
;
2173 case INTERNET_OPTION_DATAFILE_NAME
: {
2176 TRACE("INTERNET_OPTION_DATAFILE_NAME\n");
2178 if(!req
->req_file
) {
2180 return ERROR_INTERNET_ITEM_NOT_FOUND
;
2184 req_size
= (lstrlenW(req
->req_file
->file_name
)+1) * sizeof(WCHAR
);
2185 if(*size
< req_size
)
2186 return ERROR_INSUFFICIENT_BUFFER
;
2189 memcpy(buffer
, req
->req_file
->file_name
, *size
);
2190 return ERROR_SUCCESS
;
2192 req_size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
, -1, NULL
, 0, NULL
, NULL
);
2193 if (req_size
> *size
)
2194 return ERROR_INSUFFICIENT_BUFFER
;
2196 *size
= WideCharToMultiByte(CP_ACP
, 0, req
->req_file
->file_name
,
2197 -1, buffer
, *size
, NULL
, NULL
);
2198 return ERROR_SUCCESS
;
2202 case INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT
: {
2203 PCCERT_CONTEXT context
;
2206 return ERROR_INTERNET_INVALID_OPERATION
;
2208 if(*size
< sizeof(INTERNET_CERTIFICATE_INFOA
)) {
2209 *size
= sizeof(INTERNET_CERTIFICATE_INFOA
);
2210 return ERROR_INSUFFICIENT_BUFFER
;
2213 context
= (PCCERT_CONTEXT
)NETCON_GetCert(req
->netconn
);
2215 INTERNET_CERTIFICATE_INFOA
*info
= (INTERNET_CERTIFICATE_INFOA
*)buffer
;
2218 memset(info
, 0, sizeof(*info
));
2219 info
->ftExpiry
= context
->pCertInfo
->NotAfter
;
2220 info
->ftStart
= context
->pCertInfo
->NotBefore
;
2221 len
= CertNameToStrA(context
->dwCertEncodingType
,
2222 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2223 info
->lpszSubjectInfo
= LocalAlloc(0, len
);
2224 if(info
->lpszSubjectInfo
)
2225 CertNameToStrA(context
->dwCertEncodingType
,
2226 &context
->pCertInfo
->Subject
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2227 info
->lpszSubjectInfo
, len
);
2228 len
= CertNameToStrA(context
->dwCertEncodingType
,
2229 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
, NULL
, 0);
2230 info
->lpszIssuerInfo
= LocalAlloc(0, len
);
2231 if(info
->lpszIssuerInfo
)
2232 CertNameToStrA(context
->dwCertEncodingType
,
2233 &context
->pCertInfo
->Issuer
, CERT_SIMPLE_NAME_STR
|CERT_NAME_STR_CRLF_FLAG
,
2234 info
->lpszIssuerInfo
, len
);
2235 info
->dwKeySize
= NETCON_GetCipherStrength(req
->netconn
);
2236 CertFreeCertificateContext(context
);
2237 return ERROR_SUCCESS
;
2239 return ERROR_NOT_SUPPORTED
;
2241 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2242 if (*size
< sizeof(DWORD
))
2243 return ERROR_INSUFFICIENT_BUFFER
;
2245 *size
= sizeof(DWORD
);
2246 *(DWORD
*)buffer
= req
->connect_timeout
;
2247 return ERROR_SUCCESS
;
2248 case INTERNET_OPTION_REQUEST_FLAGS
: {
2251 if (*size
< sizeof(DWORD
))
2252 return ERROR_INSUFFICIENT_BUFFER
;
2254 /* FIXME: Add support for:
2255 * INTERNET_REQFLAG_FROM_CACHE
2256 * INTERNET_REQFLAG_CACHE_WRITE_DISABLED
2260 flags
|= INTERNET_REQFLAG_VIA_PROXY
;
2261 if(!req
->status_code
)
2262 flags
|= INTERNET_REQFLAG_NO_HEADERS
;
2264 TRACE("INTERNET_OPTION_REQUEST_FLAGS returning %x\n", flags
);
2266 *size
= sizeof(DWORD
);
2267 *(DWORD
*)buffer
= flags
;
2268 return ERROR_SUCCESS
;
2272 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
2275 static DWORD
HTTPREQ_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
2277 http_request_t
*req
= (http_request_t
*)hdr
;
2280 case 99: /* Undocumented, seems to be INTERNET_OPTION_SECURITY_FLAGS with argument validation */
2281 TRACE("Undocumented option 99\n");
2283 if (!buffer
|| size
!= sizeof(DWORD
))
2284 return ERROR_INVALID_PARAMETER
;
2285 if(*(DWORD
*)buffer
& ~SECURITY_SET_MASK
)
2286 return ERROR_INTERNET_OPTION_NOT_SETTABLE
;
2289 case INTERNET_OPTION_SECURITY_FLAGS
:
2293 if (!buffer
|| size
!= sizeof(DWORD
))
2294 return ERROR_INVALID_PARAMETER
;
2295 flags
= *(DWORD
*)buffer
;
2296 TRACE("INTERNET_OPTION_SECURITY_FLAGS %08x\n", flags
);
2297 flags
&= SECURITY_SET_MASK
;
2298 req
->security_flags
|= flags
;
2299 if(is_valid_netconn(req
->netconn
))
2300 req
->netconn
->security_flags
|= flags
;
2301 return ERROR_SUCCESS
;
2303 case INTERNET_OPTION_CONNECT_TIMEOUT
:
2304 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2305 req
->connect_timeout
= *(DWORD
*)buffer
;
2306 return ERROR_SUCCESS
;
2308 case INTERNET_OPTION_SEND_TIMEOUT
:
2309 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2310 req
->send_timeout
= *(DWORD
*)buffer
;
2311 return ERROR_SUCCESS
;
2313 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
2314 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
2315 req
->receive_timeout
= *(DWORD
*)buffer
;
2316 return ERROR_SUCCESS
;
2318 case INTERNET_OPTION_USERNAME
:
2319 heap_free(req
->session
->userName
);
2320 if (!(req
->session
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2321 return ERROR_SUCCESS
;
2323 case INTERNET_OPTION_PASSWORD
:
2324 heap_free(req
->session
->password
);
2325 if (!(req
->session
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2326 return ERROR_SUCCESS
;
2328 case INTERNET_OPTION_PROXY_USERNAME
:
2329 heap_free(req
->session
->appInfo
->proxyUsername
);
2330 if (!(req
->session
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2331 return ERROR_SUCCESS
;
2333 case INTERNET_OPTION_PROXY_PASSWORD
:
2334 heap_free(req
->session
->appInfo
->proxyPassword
);
2335 if (!(req
->session
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
2336 return ERROR_SUCCESS
;
2340 return INET_SetOption(hdr
, option
, buffer
, size
);
2343 static void commit_cache_entry(http_request_t
*req
)
2351 CloseHandle(req
->hCacheFile
);
2352 req
->hCacheFile
= NULL
;
2354 header
= build_response_header(req
, TRUE
);
2355 header_len
= (header
? strlenW(header
) : 0);
2356 res
= CommitUrlCacheEntryW(req
->req_file
->url
, req
->req_file
->file_name
, req
->expires
,
2357 req
->last_modified
, NORMAL_CACHE_ENTRY
,
2358 header
, header_len
, NULL
, 0);
2360 req
->req_file
->is_committed
= TRUE
;
2362 WARN("CommitUrlCacheEntry failed: %u\n", GetLastError());
2366 static void create_cache_entry(http_request_t
*req
)
2368 static const WCHAR no_cacheW
[] = {'n','o','-','c','a','c','h','e',0};
2369 static const WCHAR no_storeW
[] = {'n','o','-','s','t','o','r','e',0};
2371 WCHAR file_name
[MAX_PATH
+1];
2375 /* FIXME: We should free previous cache file earlier */
2377 req_file_release(req
->req_file
);
2378 req
->req_file
= NULL
;
2380 if(req
->hCacheFile
) {
2381 CloseHandle(req
->hCacheFile
);
2382 req
->hCacheFile
= NULL
;
2385 if(req
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
)
2391 EnterCriticalSection( &req
->headers_section
);
2393 header_idx
= HTTP_GetCustomHeaderIndex(req
, szCache_Control
, 0, FALSE
);
2394 if(header_idx
!= -1) {
2397 for(ptr
=req
->custHeaders
[header_idx
].lpszValue
; *ptr
; ) {
2400 while(*ptr
==' ' || *ptr
=='\t')
2403 end
= strchrW(ptr
, ',');
2405 end
= ptr
+ strlenW(ptr
);
2407 if(!strncmpiW(ptr
, no_cacheW
, sizeof(no_cacheW
)/sizeof(*no_cacheW
)-1)
2408 || !strncmpiW(ptr
, no_storeW
, sizeof(no_storeW
)/sizeof(*no_storeW
)-1)) {
2419 LeaveCriticalSection( &req
->headers_section
);
2423 if(!(req
->hdr
.dwFlags
& INTERNET_FLAG_NEED_FILE
))
2426 FIXME("INTERNET_FLAG_NEED_FILE is not supported correctly\n");
2429 url
= compose_request_url(req
);
2431 WARN("Could not get URL\n");
2435 b
= CreateUrlCacheEntryW(url
, req
->contentLength
== ~0u ? 0 : req
->contentLength
, NULL
, file_name
, 0);
2437 WARN("Could not create cache entry: %08x\n", GetLastError());
2441 create_req_file(file_name
, &req
->req_file
);
2442 req
->req_file
->url
= url
;
2444 req
->hCacheFile
= CreateFileW(file_name
, GENERIC_WRITE
, FILE_SHARE_READ
|FILE_SHARE_WRITE
,
2445 NULL
, CREATE_ALWAYS
, FILE_ATTRIBUTE_NORMAL
, NULL
);
2446 if(req
->hCacheFile
== INVALID_HANDLE_VALUE
) {
2447 WARN("Could not create file: %u\n", GetLastError());
2448 req
->hCacheFile
= NULL
;
2452 if(req
->read_size
) {
2455 b
= WriteFile(req
->hCacheFile
, req
->read_buf
+req
->read_pos
, req
->read_size
, &written
, NULL
);
2457 FIXME("WriteFile failed: %u\n", GetLastError());
2459 if(req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2460 commit_cache_entry(req
);
2464 /* read some more data into the read buffer (the read section must be held) */
2465 static DWORD
read_more_data( http_request_t
*req
, int maxlen
)
2472 /* move existing data to the start of the buffer */
2474 memmove( req
->read_buf
, req
->read_buf
+ req
->read_pos
, req
->read_size
);
2478 if (maxlen
== -1) maxlen
= sizeof(req
->read_buf
);
2480 res
= NETCON_recv( req
->netconn
, req
->read_buf
+ req
->read_size
,
2481 maxlen
- req
->read_size
, TRUE
, &len
);
2482 if(res
== ERROR_SUCCESS
)
2483 req
->read_size
+= len
;
2488 /* remove some amount of data from the read buffer (the read section must be held) */
2489 static void remove_data( http_request_t
*req
, int count
)
2491 if (!(req
->read_size
-= count
)) req
->read_pos
= 0;
2492 else req
->read_pos
+= count
;
2495 static DWORD
read_line( http_request_t
*req
, LPSTR buffer
, DWORD
*len
)
2497 int count
, bytes_read
, pos
= 0;
2500 EnterCriticalSection( &req
->read_section
);
2503 BYTE
*eol
= memchr( req
->read_buf
+ req
->read_pos
, '\n', req
->read_size
);
2507 count
= eol
- (req
->read_buf
+ req
->read_pos
);
2508 bytes_read
= count
+ 1;
2510 else count
= bytes_read
= req
->read_size
;
2512 count
= min( count
, *len
- pos
);
2513 memcpy( buffer
+ pos
, req
->read_buf
+ req
->read_pos
, count
);
2515 remove_data( req
, bytes_read
);
2518 if ((res
= read_more_data( req
, -1 )))
2520 WARN( "read failed %u\n", res
);
2521 LeaveCriticalSection( &req
->read_section
);
2524 if (!req
->read_size
)
2527 TRACE( "returning empty string\n" );
2528 LeaveCriticalSection( &req
->read_section
);
2529 return ERROR_SUCCESS
;
2532 LeaveCriticalSection( &req
->read_section
);
2536 if (pos
&& buffer
[pos
- 1] == '\r') pos
--;
2539 buffer
[*len
- 1] = 0;
2540 TRACE( "returning %s\n", debugstr_a(buffer
));
2541 return ERROR_SUCCESS
;
2544 /* check if we have reached the end of the data to read (the read section must be held) */
2545 static BOOL
end_of_read_data( http_request_t
*req
)
2547 return !req
->read_size
&& req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
);
2550 static DWORD
read_http_stream(http_request_t
*req
, BYTE
*buf
, DWORD size
, DWORD
*read
, BOOL allow_blocking
)
2554 res
= req
->data_stream
->vtbl
->read(req
->data_stream
, req
, buf
, size
, read
, allow_blocking
);
2555 if(res
!= ERROR_SUCCESS
)
2557 assert(*read
<= size
);
2559 if(req
->hCacheFile
) {
2564 bres
= WriteFile(req
->hCacheFile
, buf
, *read
, &written
, NULL
);
2566 FIXME("WriteFile failed: %u\n", GetLastError());
2569 if((res
== ERROR_SUCCESS
&& !*read
) || req
->data_stream
->vtbl
->end_of_data(req
->data_stream
, req
))
2570 commit_cache_entry(req
);
2576 /* fetch some more data into the read buffer (the read section must be held) */
2577 static DWORD
refill_read_buffer(http_request_t
*req
, BOOL allow_blocking
, DWORD
*read_bytes
)
2581 if(req
->read_size
== sizeof(req
->read_buf
))
2582 return ERROR_SUCCESS
;
2586 memmove(req
->read_buf
, req
->read_buf
+req
->read_pos
, req
->read_size
);
2590 res
= read_http_stream(req
, req
->read_buf
+req
->read_size
, sizeof(req
->read_buf
) - req
->read_size
,
2591 &read
, allow_blocking
);
2592 if(res
!= ERROR_SUCCESS
)
2595 req
->read_size
+= read
;
2597 TRACE("read %u bytes, read_size %u\n", read
, req
->read_size
);
2603 static BOOL
netconn_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2605 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2606 return netconn_stream
->content_read
== netconn_stream
->content_length
|| !is_valid_netconn(req
->netconn
);
2609 static DWORD
netconn_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2610 DWORD
*read
, BOOL allow_blocking
)
2612 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2613 DWORD res
= ERROR_SUCCESS
;
2616 size
= min(size
, netconn_stream
->content_length
-netconn_stream
->content_read
);
2618 if(size
&& is_valid_netconn(req
->netconn
)) {
2619 res
= NETCON_recv(req
->netconn
, buf
, size
, allow_blocking
, &ret
);
2620 if(res
== ERROR_SUCCESS
) {
2622 netconn_stream
->content_length
= netconn_stream
->content_read
;
2623 netconn_stream
->content_read
+= ret
;
2627 TRACE("res %u read %u bytes\n", res
, ret
);
2632 static DWORD
netconn_drain_content(data_stream_t
*stream
, http_request_t
*req
, BOOL allow_blocking
)
2634 netconn_stream_t
*netconn_stream
= (netconn_stream_t
*)stream
;
2639 if(netconn_stream
->content_length
== ~0u)
2642 while(netconn_stream
->content_read
< netconn_stream
->content_length
) {
2643 size
= min(sizeof(buf
), netconn_stream
->content_length
-netconn_stream
->content_read
);
2644 res
= NETCON_recv(req
->netconn
, buf
, size
, allow_blocking
, &len
);
2648 return WSAECONNABORTED
;
2650 netconn_stream
->content_read
+= len
;
2653 return ERROR_SUCCESS
;
2656 static void netconn_destroy(data_stream_t
*stream
)
2660 static const data_stream_vtbl_t netconn_stream_vtbl
= {
2661 netconn_end_of_data
,
2663 netconn_drain_content
,
2667 static char next_chunked_data_char(chunked_stream_t
*stream
)
2669 assert(stream
->buf_size
);
2672 return stream
->buf
[stream
->buf_pos
++];
2675 static BOOL
chunked_end_of_data(data_stream_t
*stream
, http_request_t
*req
)
2677 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2678 switch(chunked_stream
->state
) {
2679 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
:
2680 case CHUNKED_STREAM_STATE_END_OF_STREAM
:
2681 case CHUNKED_STREAM_STATE_ERROR
:
2688 static DWORD
chunked_read(data_stream_t
*stream
, http_request_t
*req
, BYTE
*buf
, DWORD size
,
2689 DWORD
*read
, BOOL allow_blocking
)
2691 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2692 DWORD ret_read
= 0, res
= ERROR_SUCCESS
;
2693 BOOL continue_read
= TRUE
;
2698 TRACE("state %d\n", chunked_stream
->state
);
2700 /* Ensure that we have data in the buffer for states that need it. */
2701 if(!chunked_stream
->buf_size
) {
2702 BOOL blocking_read
= allow_blocking
;
2704 switch(chunked_stream
->state
) {
2705 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
:
2706 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
:
2707 /* never allow blocking after 0 chunk size */
2708 if(!chunked_stream
->chunk_size
)
2709 blocking_read
= FALSE
;
2711 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
:
2712 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
:
2713 chunked_stream
->buf_pos
= 0;
2714 res
= NETCON_recv(req
->netconn
, chunked_stream
->buf
, sizeof(chunked_stream
->buf
), blocking_read
, &read_bytes
);
2715 if(res
== ERROR_SUCCESS
&& read_bytes
) {
2716 chunked_stream
->buf_size
+= read_bytes
;
2717 }else if(res
== WSAEWOULDBLOCK
) {
2718 if(ret_read
|| allow_blocking
)
2719 res
= ERROR_SUCCESS
;
2720 continue_read
= FALSE
;
2723 chunked_stream
->state
= CHUNKED_STREAM_STATE_ERROR
;
2731 switch(chunked_stream
->state
) {
2732 case CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
:
2733 ch
= next_chunked_data_char(chunked_stream
);
2735 if(ch
>= '0' && ch
<= '9') {
2736 chunked_stream
->chunk_size
= chunked_stream
->chunk_size
* 16 + ch
- '0';
2737 }else if(ch
>= 'a' && ch
<= 'f') {
2738 chunked_stream
->chunk_size
= chunked_stream
->chunk_size
* 16 + ch
- 'a' + 10;
2739 }else if (ch
>= 'A' && ch
<= 'F') {
2740 chunked_stream
->chunk_size
= chunked_stream
->chunk_size
* 16 + ch
- 'A' + 10;
2741 }else if (ch
== ';' || ch
== '\r' || ch
== '\n') {
2742 TRACE("reading %u byte chunk\n", chunked_stream
->chunk_size
);
2743 chunked_stream
->buf_size
++;
2744 chunked_stream
->buf_pos
--;
2745 if(req
->contentLength
== ~0u) req
->contentLength
= chunked_stream
->chunk_size
;
2746 else req
->contentLength
+= chunked_stream
->chunk_size
;
2747 chunked_stream
->state
= CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
;
2751 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_SIZE
:
2752 ch
= next_chunked_data_char(chunked_stream
);
2754 chunked_stream
->state
= chunked_stream
->chunk_size
2755 ? CHUNKED_STREAM_STATE_READING_CHUNK
2756 : CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
;
2758 WARN("unexpected char '%c'\n", ch
);
2761 case CHUNKED_STREAM_STATE_READING_CHUNK
:
2762 assert(chunked_stream
->chunk_size
);
2764 continue_read
= FALSE
;
2767 read_bytes
= min(size
, chunked_stream
->chunk_size
);
2769 if(chunked_stream
->buf_size
) {
2770 if(read_bytes
> chunked_stream
->buf_size
)
2771 read_bytes
= chunked_stream
->buf_size
;
2773 memcpy(buf
+ret_read
, chunked_stream
->buf
+chunked_stream
->buf_pos
, read_bytes
);
2774 chunked_stream
->buf_pos
+= read_bytes
;
2775 chunked_stream
->buf_size
-= read_bytes
;
2777 res
= NETCON_recv(req
->netconn
, (char*)buf
+ret_read
, read_bytes
,
2778 allow_blocking
, (int*)&read_bytes
);
2779 if(res
!= ERROR_SUCCESS
) {
2780 continue_read
= FALSE
;
2785 chunked_stream
->state
= CHUNKED_STREAM_STATE_ERROR
;
2790 chunked_stream
->chunk_size
-= read_bytes
;
2792 ret_read
+= read_bytes
;
2793 if(!chunked_stream
->chunk_size
)
2794 chunked_stream
->state
= CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
;
2795 allow_blocking
= FALSE
;
2798 case CHUNKED_STREAM_STATE_DISCARD_EOL_AFTER_DATA
:
2799 ch
= next_chunked_data_char(chunked_stream
);
2801 chunked_stream
->state
= CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
;
2803 WARN("unexpected char '%c'\n", ch
);
2806 case CHUNKED_STREAM_STATE_DISCARD_EOL_AT_END
:
2807 ch
= next_chunked_data_char(chunked_stream
);
2809 chunked_stream
->state
= CHUNKED_STREAM_STATE_END_OF_STREAM
;
2811 WARN("unexpected char '%c'\n", ch
);
2814 case CHUNKED_STREAM_STATE_END_OF_STREAM
:
2815 case CHUNKED_STREAM_STATE_ERROR
:
2816 continue_read
= FALSE
;
2819 } while(continue_read
);
2822 res
= ERROR_SUCCESS
;
2823 if(res
!= ERROR_SUCCESS
)
2826 TRACE("read %d bytes\n", ret_read
);
2828 return ERROR_SUCCESS
;
2831 static DWORD
chunked_drain_content(data_stream_t
*stream
, http_request_t
*req
, BOOL allow_blocking
)
2833 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2837 while(chunked_stream
->state
!= CHUNKED_STREAM_STATE_END_OF_STREAM
2838 && chunked_stream
->state
!= CHUNKED_STREAM_STATE_ERROR
) {
2839 res
= chunked_read(stream
, req
, buf
, sizeof(buf
), &size
, allow_blocking
);
2840 if(res
!= ERROR_SUCCESS
)
2844 if(chunked_stream
->state
!= CHUNKED_STREAM_STATE_END_OF_STREAM
)
2845 return ERROR_NO_DATA
;
2846 return ERROR_SUCCESS
;
2849 static void chunked_destroy(data_stream_t
*stream
)
2851 chunked_stream_t
*chunked_stream
= (chunked_stream_t
*)stream
;
2852 heap_free(chunked_stream
);
2855 static const data_stream_vtbl_t chunked_stream_vtbl
= {
2856 chunked_end_of_data
,
2858 chunked_drain_content
,
2862 /* set the request content length based on the headers */
2863 static DWORD
set_content_length(http_request_t
*request
)
2865 static const WCHAR szChunked
[] = {'c','h','u','n','k','e','d',0};
2866 static const WCHAR headW
[] = {'H','E','A','D',0};
2870 if(request
->status_code
== HTTP_STATUS_NO_CONTENT
|| !strcmpW(request
->verb
, headW
)) {
2871 request
->contentLength
= request
->netconn_stream
.content_length
= 0;
2872 return ERROR_SUCCESS
;
2875 size
= sizeof(request
->contentLength
);
2876 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_FLAG_NUMBER
|HTTP_QUERY_CONTENT_LENGTH
,
2877 &request
->contentLength
, &size
, NULL
) != ERROR_SUCCESS
)
2878 request
->contentLength
= ~0u;
2879 request
->netconn_stream
.content_length
= request
->contentLength
;
2880 request
->netconn_stream
.content_read
= request
->read_size
;
2882 size
= sizeof(encoding
);
2883 if (HTTP_HttpQueryInfoW(request
, HTTP_QUERY_TRANSFER_ENCODING
, encoding
, &size
, NULL
) == ERROR_SUCCESS
&&
2884 !strcmpiW(encoding
, szChunked
))
2886 chunked_stream_t
*chunked_stream
;
2888 chunked_stream
= heap_alloc(sizeof(*chunked_stream
));
2890 return ERROR_OUTOFMEMORY
;
2892 chunked_stream
->data_stream
.vtbl
= &chunked_stream_vtbl
;
2893 chunked_stream
->buf_size
= chunked_stream
->buf_pos
= 0;
2894 chunked_stream
->chunk_size
= 0;
2895 chunked_stream
->state
= CHUNKED_STREAM_STATE_READING_CHUNK_SIZE
;
2897 if(request
->read_size
) {
2898 memcpy(chunked_stream
->buf
, request
->read_buf
+request
->read_pos
, request
->read_size
);
2899 chunked_stream
->buf_size
= request
->read_size
;
2900 request
->read_size
= request
->read_pos
= 0;
2903 request
->data_stream
= &chunked_stream
->data_stream
;
2904 request
->contentLength
= ~0u;
2907 if(request
->hdr
.decoding
) {
2910 static const WCHAR deflateW
[] = {'d','e','f','l','a','t','e',0};
2911 static const WCHAR gzipW
[] = {'g','z','i','p',0};
2913 EnterCriticalSection( &request
->headers_section
);
2915 encoding_idx
= HTTP_GetCustomHeaderIndex(request
, szContent_Encoding
, 0, FALSE
);
2916 if(encoding_idx
!= -1) {
2917 if(!strcmpiW(request
->custHeaders
[encoding_idx
].lpszValue
, gzipW
)) {
2918 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2919 LeaveCriticalSection( &request
->headers_section
);
2920 return init_gzip_stream(request
, TRUE
);
2922 if(!strcmpiW(request
->custHeaders
[encoding_idx
].lpszValue
, deflateW
)) {
2923 HTTP_DeleteCustomHeader(request
, encoding_idx
);
2924 LeaveCriticalSection( &request
->headers_section
);
2925 return init_gzip_stream(request
, FALSE
);
2929 LeaveCriticalSection( &request
->headers_section
);
2932 return ERROR_SUCCESS
;
2935 static void send_request_complete(http_request_t
*req
, DWORD_PTR result
, DWORD error
)
2937 INTERNET_ASYNC_RESULT iar
;
2939 iar
.dwResult
= result
;
2940 iar
.dwError
= error
;
2942 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_COMPLETE
, &iar
,
2943 sizeof(INTERNET_ASYNC_RESULT
));
2946 static void HTTP_ReceiveRequestData(http_request_t
*req
)
2948 DWORD res
, read
= 0;
2952 EnterCriticalSection( &req
->read_section
);
2954 res
= refill_read_buffer(req
, FALSE
, &read
);
2955 if(res
== ERROR_SUCCESS
)
2956 read
+= req
->read_size
;
2958 LeaveCriticalSection( &req
->read_section
);
2960 if(res
!= WSAEWOULDBLOCK
&& (res
!= ERROR_SUCCESS
|| !read
)) {
2961 WARN("res %u read %u, closing connection\n", res
, read
);
2962 http_release_netconn(req
, FALSE
);
2965 if(res
!= ERROR_SUCCESS
&& res
!= WSAEWOULDBLOCK
) {
2966 send_request_complete(req
, 0, res
);
2970 send_request_complete(req
, req
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)req
->hdr
.hInternet
: 1, 0);
2973 /* read data from the http connection (the read section must be held) */
2974 static DWORD
HTTPREQ_Read(http_request_t
*req
, void *buffer
, DWORD size
, DWORD
*read
, BOOL allow_blocking
)
2976 DWORD current_read
= 0, ret_read
= 0;
2977 DWORD res
= ERROR_SUCCESS
;
2979 EnterCriticalSection( &req
->read_section
);
2981 if(req
->read_size
) {
2982 ret_read
= min(size
, req
->read_size
);
2983 memcpy(buffer
, req
->read_buf
+req
->read_pos
, ret_read
);
2984 req
->read_size
-= ret_read
;
2985 req
->read_pos
+= ret_read
;
2986 allow_blocking
= FALSE
;
2989 if(ret_read
< size
) {
2990 res
= read_http_stream(req
, (BYTE
*)buffer
+ret_read
, size
-ret_read
, ¤t_read
, allow_blocking
);
2991 if(res
== ERROR_SUCCESS
)
2992 ret_read
+= current_read
;
2993 else if(res
== WSAEWOULDBLOCK
&& ret_read
)
2994 res
= ERROR_SUCCESS
;
2997 LeaveCriticalSection( &req
->read_section
);
3000 TRACE( "retrieved %u bytes (res %u)\n", ret_read
, res
);
3002 if(res
!= WSAEWOULDBLOCK
) {
3003 if(res
!= ERROR_SUCCESS
)
3004 http_release_netconn(req
, FALSE
);
3005 else if(!ret_read
&& drain_content(req
, FALSE
) == ERROR_SUCCESS
)
3006 http_release_netconn(req
, TRUE
);
3012 static DWORD
drain_content(http_request_t
*req
, BOOL blocking
)
3016 TRACE("%p\n", req
->netconn
);
3018 if(!is_valid_netconn(req
->netconn
))
3019 return ERROR_NO_DATA
;
3021 if(!strcmpW(req
->verb
, szHEAD
))
3022 return ERROR_SUCCESS
;
3024 EnterCriticalSection( &req
->read_section
);
3025 res
= req
->data_stream
->vtbl
->drain_content(req
->data_stream
, req
, blocking
);
3026 LeaveCriticalSection( &req
->read_section
);
3038 static void async_read_file_proc(task_header_t
*hdr
)
3040 read_file_task_t
*task
= (read_file_task_t
*)hdr
;
3041 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
3042 DWORD res
= ERROR_SUCCESS
, read
= task
->read_pos
, complete_arg
= 0;
3044 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
);
3048 while (read
< task
->size
) {
3049 res
= HTTPREQ_Read(req
, (char*)task
->buf
+ read
, task
->size
- read
, &read_bytes
, TRUE
);
3050 if (res
!= ERROR_SUCCESS
|| !read_bytes
)
3055 EnterCriticalSection(&req
->read_section
);
3056 res
= refill_read_buffer(req
, TRUE
, &read
);
3057 LeaveCriticalSection(&req
->read_section
);
3060 complete_arg
= read
; /* QueryDataAvailable reports read bytes in request complete notification */
3061 if(res
!= ERROR_SUCCESS
|| !read
)
3062 http_release_netconn(req
, drain_content(req
, FALSE
) == ERROR_SUCCESS
);
3065 TRACE("res %u read %u\n", res
, read
);
3068 *task
->ret_read
= read
;
3070 /* FIXME: We should report bytes transferred before decoding content. */
3071 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
, &read
, sizeof(read
));
3073 if(res
!= ERROR_SUCCESS
)
3075 send_request_complete(req
, res
== ERROR_SUCCESS
, complete_arg
);
3078 static DWORD
async_read(http_request_t
*req
, void *buf
, DWORD size
, DWORD read_pos
, DWORD
*ret_read
)
3080 read_file_task_t
*task
;
3082 task
= alloc_async_task(&req
->hdr
, async_read_file_proc
, sizeof(*task
));
3084 return ERROR_OUTOFMEMORY
;
3088 task
->read_pos
= read_pos
;
3089 task
->ret_read
= ret_read
;
3091 INTERNET_AsyncCall(&task
->hdr
);
3092 return ERROR_IO_PENDING
;
3095 static DWORD
HTTPREQ_ReadFile(object_header_t
*hdr
, void *buf
, DWORD size
, DWORD
*ret_read
,
3096 DWORD flags
, DWORD_PTR context
)
3098 http_request_t
*req
= (http_request_t
*)hdr
;
3099 DWORD res
= ERROR_SUCCESS
, read
= 0, cread
, error
= ERROR_SUCCESS
;
3100 BOOL allow_blocking
, notify_received
= FALSE
;
3102 TRACE("(%p %p %u %x)\n", req
, buf
, size
, flags
);
3104 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
3105 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
3107 allow_blocking
= !(req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
);
3109 if(allow_blocking
|| TryEnterCriticalSection(&req
->read_section
)) {
3111 EnterCriticalSection(&req
->read_section
);
3112 if(hdr
->dwError
== ERROR_SUCCESS
)
3113 hdr
->dwError
= INTERNET_HANDLE_IN_USE
;
3114 else if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3115 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3117 if(req
->read_size
) {
3118 read
= min(size
, req
->read_size
);
3119 memcpy(buf
, req
->read_buf
+ req
->read_pos
, read
);
3120 req
->read_size
-= read
;
3121 req
->read_pos
+= read
;
3124 if(read
< size
&& (!read
|| !(flags
& IRF_NO_WAIT
)) && !end_of_read_data(req
)) {
3125 LeaveCriticalSection(&req
->read_section
);
3126 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3127 EnterCriticalSection( &req
->read_section
);
3128 notify_received
= TRUE
;
3130 while(read
< size
) {
3131 res
= HTTPREQ_Read(req
, (char*)buf
+read
, size
-read
, &cread
, allow_blocking
);
3133 if (res
!= ERROR_SUCCESS
|| !cread
)
3138 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3139 hdr
->dwError
= ERROR_SUCCESS
;
3141 error
= hdr
->dwError
;
3143 LeaveCriticalSection( &req
->read_section
);
3145 res
= WSAEWOULDBLOCK
;
3148 if(res
== WSAEWOULDBLOCK
) {
3149 if(!(flags
& IRF_NO_WAIT
))
3150 return async_read(req
, buf
, size
, read
, ret_read
);
3152 return async_read(req
, NULL
, 0, 0, NULL
);
3153 res
= ERROR_SUCCESS
;
3157 if (res
!= ERROR_SUCCESS
)
3161 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3162 &read
, sizeof(read
));
3166 static DWORD
HTTPREQ_WriteFile(object_header_t
*hdr
, const void *buffer
, DWORD size
, DWORD
*written
)
3169 http_request_t
*request
= (http_request_t
*)hdr
;
3171 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
3174 res
= NETCON_send(request
->netconn
, buffer
, size
, 0, (LPINT
)written
);
3175 if (res
== ERROR_SUCCESS
)
3176 request
->bytesWritten
+= *written
;
3178 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REQUEST_SENT
, written
, sizeof(DWORD
));
3182 static DWORD
HTTPREQ_QueryDataAvailable(object_header_t
*hdr
, DWORD
*available
, DWORD flags
, DWORD_PTR ctx
)
3184 http_request_t
*req
= (http_request_t
*)hdr
;
3185 DWORD res
= ERROR_SUCCESS
, avail
= 0, error
= ERROR_SUCCESS
;
3186 BOOL allow_blocking
, notify_received
= FALSE
;
3188 TRACE("(%p %p %x %lx)\n", req
, available
, flags
, ctx
);
3190 if (flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
))
3191 FIXME("these dwFlags aren't implemented: 0x%x\n", flags
& ~(IRF_ASYNC
|IRF_NO_WAIT
));
3194 allow_blocking
= !(req
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
);
3196 if(allow_blocking
|| TryEnterCriticalSection(&req
->read_section
)) {
3198 EnterCriticalSection(&req
->read_section
);
3199 if(hdr
->dwError
== ERROR_SUCCESS
)
3200 hdr
->dwError
= INTERNET_HANDLE_IN_USE
;
3201 else if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3202 hdr
->dwError
= ERROR_INTERNET_INTERNAL_ERROR
;
3204 avail
= req
->read_size
;
3206 if(!avail
&& !end_of_read_data(req
)) {
3207 LeaveCriticalSection(&req
->read_section
);
3208 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
3209 EnterCriticalSection( &req
->read_section
);
3210 notify_received
= TRUE
;
3212 res
= refill_read_buffer(req
, allow_blocking
, &avail
);
3215 if(hdr
->dwError
== INTERNET_HANDLE_IN_USE
)
3216 hdr
->dwError
= ERROR_SUCCESS
;
3218 error
= hdr
->dwError
;
3220 LeaveCriticalSection( &req
->read_section
);
3222 res
= WSAEWOULDBLOCK
;
3225 if(res
== WSAEWOULDBLOCK
)
3226 return async_read(req
, NULL
, 0, 0, available
);
3228 if (res
!= ERROR_SUCCESS
)
3233 INTERNET_SendCallback(&req
->hdr
, req
->hdr
.dwContext
, INTERNET_STATUS_RESPONSE_RECEIVED
,
3234 &avail
, sizeof(avail
));
3238 static DWORD
HTTPREQ_LockRequestFile(object_header_t
*hdr
, req_file_t
**ret
)
3240 http_request_t
*req
= (http_request_t
*)hdr
;
3242 TRACE("(%p)\n", req
);
3244 if(!req
->req_file
) {
3245 WARN("No cache file name available\n");
3246 return ERROR_FILE_NOT_FOUND
;
3249 *ret
= req_file_addref(req
->req_file
);
3250 return ERROR_SUCCESS
;
3253 static const object_vtbl_t HTTPREQVtbl
= {
3255 HTTPREQ_CloseConnection
,
3256 HTTPREQ_QueryOption
,
3260 HTTPREQ_QueryDataAvailable
,
3262 HTTPREQ_LockRequestFile
3265 /***********************************************************************
3266 * HTTP_HttpOpenRequestW (internal)
3268 * Open a HTTP request handle
3271 * HINTERNET a HTTP request handle on success
3275 static DWORD
HTTP_HttpOpenRequestW(http_session_t
*session
,
3276 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3277 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3278 DWORD dwFlags
, DWORD_PTR dwContext
, HINTERNET
*ret
)
3280 appinfo_t
*hIC
= session
->appInfo
;
3281 http_request_t
*request
;
3286 request
= alloc_object(&session
->hdr
, &HTTPREQVtbl
, sizeof(http_request_t
));
3288 return ERROR_OUTOFMEMORY
;
3290 request
->hdr
.htype
= WH_HHTTPREQ
;
3291 request
->hdr
.dwFlags
= dwFlags
;
3292 request
->hdr
.dwContext
= dwContext
;
3293 request
->hdr
.decoding
= session
->hdr
.decoding
;
3294 request
->contentLength
= ~0u;
3296 request
->netconn_stream
.data_stream
.vtbl
= &netconn_stream_vtbl
;
3297 request
->data_stream
= &request
->netconn_stream
.data_stream
;
3298 request
->connect_timeout
= session
->connect_timeout
;
3299 request
->send_timeout
= session
->send_timeout
;
3300 request
->receive_timeout
= session
->receive_timeout
;
3302 InitializeCriticalSection( &request
->headers_section
);
3303 request
->headers_section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": http_request_t.headers_section");
3305 InitializeCriticalSection( &request
->read_section
);
3306 request
->read_section
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": http_request_t.read_section");
3308 WININET_AddRef( &session
->hdr
);
3309 request
->session
= session
;
3310 list_add_head( &session
->hdr
.children
, &request
->hdr
.entry
);
3312 port
= session
->hostPort
;
3313 if (port
== INTERNET_INVALID_PORT_NUMBER
)
3314 port
= (session
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ?
3315 INTERNET_DEFAULT_HTTPS_PORT
: INTERNET_DEFAULT_HTTP_PORT
;
3317 request
->server
= get_server(substrz(session
->hostName
), port
, (dwFlags
& INTERNET_FLAG_SECURE
) != 0, TRUE
);
3318 if(!request
->server
) {
3319 WININET_Release(&request
->hdr
);
3320 return ERROR_OUTOFMEMORY
;
3323 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_CN_INVALID
)
3324 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_CN_INVALID
;
3325 if (dwFlags
& INTERNET_FLAG_IGNORE_CERT_DATE_INVALID
)
3326 request
->security_flags
|= SECURITY_FLAG_IGNORE_CERT_DATE_INVALID
;
3328 if (lpszObjectName
&& *lpszObjectName
) {
3333 rc
= UrlCanonicalizeW(lpszObjectName
, &dummy
, &len
, URL_ESCAPE_SPACES_ONLY
);
3334 if (rc
!= E_POINTER
)
3335 len
= strlenW(lpszObjectName
)+1;
3336 request
->path
= heap_alloc(len
*sizeof(WCHAR
));
3337 rc
= UrlCanonicalizeW(lpszObjectName
, request
->path
, &len
,
3338 URL_ESCAPE_SPACES_ONLY
);
3341 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(lpszObjectName
),rc
);
3342 strcpyW(request
->path
,lpszObjectName
);
3345 static const WCHAR slashW
[] = {'/',0};
3347 request
->path
= heap_strdupW(slashW
);
3350 if (lpszReferrer
&& *lpszReferrer
)
3351 HTTP_ProcessHeader(request
, HTTP_REFERER
, lpszReferrer
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REQ
);
3353 if (lpszAcceptTypes
)
3356 for (i
= 0; lpszAcceptTypes
[i
]; i
++)
3358 if (!*lpszAcceptTypes
[i
]) continue;
3359 HTTP_ProcessHeader(request
, HTTP_ACCEPT
, lpszAcceptTypes
[i
],
3360 HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
|
3361 HTTP_ADDHDR_FLAG_REQ
|
3362 (i
== 0 ? (HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
) : 0));
3366 request
->verb
= heap_strdupW(lpszVerb
&& *lpszVerb
? lpszVerb
: szGET
);
3367 request
->version
= heap_strdupW(lpszVersion
&& *lpszVersion
? lpszVersion
: g_szHttp1_1
);
3369 if (hIC
->proxy
&& hIC
->proxy
[0] && !HTTP_ShouldBypassProxy(hIC
, session
->hostName
))
3370 HTTP_DealWithProxy( hIC
, session
, request
);
3372 INTERNET_SendCallback(&session
->hdr
, dwContext
,
3373 INTERNET_STATUS_HANDLE_CREATED
, &request
->hdr
.hInternet
,
3376 TRACE("<-- (%p)\n", request
);
3378 *ret
= request
->hdr
.hInternet
;
3379 return ERROR_SUCCESS
;
3382 /***********************************************************************
3383 * HttpOpenRequestW (WININET.@)
3385 * Open a HTTP request handle
3388 * HINTERNET a HTTP request handle on success
3392 HINTERNET WINAPI
HttpOpenRequestW(HINTERNET hHttpSession
,
3393 LPCWSTR lpszVerb
, LPCWSTR lpszObjectName
, LPCWSTR lpszVersion
,
3394 LPCWSTR lpszReferrer
, LPCWSTR
*lpszAcceptTypes
,
3395 DWORD dwFlags
, DWORD_PTR dwContext
)
3397 http_session_t
*session
;
3398 HINTERNET handle
= NULL
;
3401 TRACE("(%p, %s, %s, %s, %s, %p, %08x, %08lx)\n", hHttpSession
,
3402 debugstr_w(lpszVerb
), debugstr_w(lpszObjectName
),
3403 debugstr_w(lpszVersion
), debugstr_w(lpszReferrer
), lpszAcceptTypes
,
3404 dwFlags
, dwContext
);
3405 if(lpszAcceptTypes
!=NULL
)
3408 for(i
=0;lpszAcceptTypes
[i
]!=NULL
;i
++)
3409 TRACE("\taccept type: %s\n",debugstr_w(lpszAcceptTypes
[i
]));
3412 session
= (http_session_t
*) get_handle_object( hHttpSession
);
3413 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
3415 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3420 * My tests seem to show that the windows version does not
3421 * become asynchronous until after this point. And anyhow
3422 * if this call was asynchronous then how would you get the
3423 * necessary HINTERNET pointer returned by this function.
3426 res
= HTTP_HttpOpenRequestW(session
, lpszVerb
, lpszObjectName
,
3427 lpszVersion
, lpszReferrer
, lpszAcceptTypes
,
3428 dwFlags
, dwContext
, &handle
);
3431 WININET_Release( &session
->hdr
);
3432 TRACE("returning %p\n", handle
);
3433 if(res
!= ERROR_SUCCESS
)
3438 static const LPCWSTR header_lookup
[] = {
3439 szMime_Version
, /* HTTP_QUERY_MIME_VERSION = 0 */
3440 szContent_Type
, /* HTTP_QUERY_CONTENT_TYPE = 1 */
3441 szContent_Transfer_Encoding
,/* HTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
3442 szContent_ID
, /* HTTP_QUERY_CONTENT_ID = 3 */
3443 NULL
, /* HTTP_QUERY_CONTENT_DESCRIPTION = 4 */
3444 szContent_Length
, /* HTTP_QUERY_CONTENT_LENGTH = 5 */
3445 szContent_Language
, /* HTTP_QUERY_CONTENT_LANGUAGE = 6 */
3446 szAllow
, /* HTTP_QUERY_ALLOW = 7 */
3447 szPublic
, /* HTTP_QUERY_PUBLIC = 8 */
3448 szDate
, /* HTTP_QUERY_DATE = 9 */
3449 szExpires
, /* HTTP_QUERY_EXPIRES = 10 */
3450 szLast_Modified
, /* HTTP_QUERY_LAST_MODIFIED = 11 */
3451 NULL
, /* HTTP_QUERY_MESSAGE_ID = 12 */
3452 szURI
, /* HTTP_QUERY_URI = 13 */
3453 szFrom
, /* HTTP_QUERY_DERIVED_FROM = 14 */
3454 NULL
, /* HTTP_QUERY_COST = 15 */
3455 NULL
, /* HTTP_QUERY_LINK = 16 */
3456 szPragma
, /* HTTP_QUERY_PRAGMA = 17 */
3457 NULL
, /* HTTP_QUERY_VERSION = 18 */
3458 szStatus
, /* HTTP_QUERY_STATUS_CODE = 19 */
3459 NULL
, /* HTTP_QUERY_STATUS_TEXT = 20 */
3460 NULL
, /* HTTP_QUERY_RAW_HEADERS = 21 */
3461 NULL
, /* HTTP_QUERY_RAW_HEADERS_CRLF = 22 */
3462 szConnection
, /* HTTP_QUERY_CONNECTION = 23 */
3463 szAccept
, /* HTTP_QUERY_ACCEPT = 24 */
3464 szAccept_Charset
, /* HTTP_QUERY_ACCEPT_CHARSET = 25 */
3465 szAccept_Encoding
, /* HTTP_QUERY_ACCEPT_ENCODING = 26 */
3466 szAccept_Language
, /* HTTP_QUERY_ACCEPT_LANGUAGE = 27 */
3467 szAuthorization
, /* HTTP_QUERY_AUTHORIZATION = 28 */
3468 szContent_Encoding
, /* HTTP_QUERY_CONTENT_ENCODING = 29 */
3469 NULL
, /* HTTP_QUERY_FORWARDED = 30 */
3470 NULL
, /* HTTP_QUERY_FROM = 31 */
3471 szIf_Modified_Since
, /* HTTP_QUERY_IF_MODIFIED_SINCE = 32 */
3472 szLocation
, /* HTTP_QUERY_LOCATION = 33 */
3473 NULL
, /* HTTP_QUERY_ORIG_URI = 34 */
3474 szReferer
, /* HTTP_QUERY_REFERER = 35 */
3475 szRetry_After
, /* HTTP_QUERY_RETRY_AFTER = 36 */
3476 szServer
, /* HTTP_QUERY_SERVER = 37 */
3477 NULL
, /* HTTP_TITLE = 38 */
3478 szUser_Agent
, /* HTTP_QUERY_USER_AGENT = 39 */
3479 szWWW_Authenticate
, /* HTTP_QUERY_WWW_AUTHENTICATE = 40 */
3480 szProxy_Authenticate
, /* HTTP_QUERY_PROXY_AUTHENTICATE = 41 */
3481 szAccept_Ranges
, /* HTTP_QUERY_ACCEPT_RANGES = 42 */
3482 szSet_Cookie
, /* HTTP_QUERY_SET_COOKIE = 43 */
3483 szCookie
, /* HTTP_QUERY_COOKIE = 44 */
3484 NULL
, /* HTTP_QUERY_REQUEST_METHOD = 45 */
3485 NULL
, /* HTTP_QUERY_REFRESH = 46 */
3486 szContent_Disposition
, /* HTTP_QUERY_CONTENT_DISPOSITION = 47 */
3487 szAge
, /* HTTP_QUERY_AGE = 48 */
3488 szCache_Control
, /* HTTP_QUERY_CACHE_CONTROL = 49 */
3489 szContent_Base
, /* HTTP_QUERY_CONTENT_BASE = 50 */
3490 szContent_Location
, /* HTTP_QUERY_CONTENT_LOCATION = 51 */
3491 szContent_MD5
, /* HTTP_QUERY_CONTENT_MD5 = 52 */
3492 szContent_Range
, /* HTTP_QUERY_CONTENT_RANGE = 53 */
3493 szETag
, /* HTTP_QUERY_ETAG = 54 */
3494 hostW
, /* HTTP_QUERY_HOST = 55 */
3495 szIf_Match
, /* HTTP_QUERY_IF_MATCH = 56 */
3496 szIf_None_Match
, /* HTTP_QUERY_IF_NONE_MATCH = 57 */
3497 szIf_Range
, /* HTTP_QUERY_IF_RANGE = 58 */
3498 szIf_Unmodified_Since
, /* HTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
3499 szMax_Forwards
, /* HTTP_QUERY_MAX_FORWARDS = 60 */
3500 szProxy_Authorization
, /* HTTP_QUERY_PROXY_AUTHORIZATION = 61 */
3501 szRange
, /* HTTP_QUERY_RANGE = 62 */
3502 szTransfer_Encoding
, /* HTTP_QUERY_TRANSFER_ENCODING = 63 */
3503 szUpgrade
, /* HTTP_QUERY_UPGRADE = 64 */
3504 szVary
, /* HTTP_QUERY_VARY = 65 */
3505 szVia
, /* HTTP_QUERY_VIA = 66 */
3506 szWarning
, /* HTTP_QUERY_WARNING = 67 */
3507 szExpect
, /* HTTP_QUERY_EXPECT = 68 */
3508 szProxy_Connection
, /* HTTP_QUERY_PROXY_CONNECTION = 69 */
3509 szUnless_Modified_Since
, /* HTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
3512 #define LAST_TABLE_HEADER (sizeof(header_lookup)/sizeof(header_lookup[0]))
3514 /***********************************************************************
3515 * HTTP_HttpQueryInfoW (internal)
3517 static DWORD
HTTP_HttpQueryInfoW(http_request_t
*request
, DWORD dwInfoLevel
,
3518 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3520 LPHTTPHEADERW lphttpHdr
= NULL
;
3521 BOOL request_only
= !!(dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
);
3522 INT requested_index
= lpdwIndex
? *lpdwIndex
: 0;
3523 DWORD level
= (dwInfoLevel
& ~HTTP_QUERY_MODIFIER_FLAGS_MASK
);
3526 EnterCriticalSection( &request
->headers_section
);
3528 /* Find requested header structure */
3531 case HTTP_QUERY_CUSTOM
:
3534 LeaveCriticalSection( &request
->headers_section
);
3535 return ERROR_INVALID_PARAMETER
;
3537 index
= HTTP_GetCustomHeaderIndex(request
, lpBuffer
, requested_index
, request_only
);
3539 case HTTP_QUERY_RAW_HEADERS_CRLF
:
3543 DWORD res
= ERROR_INVALID_PARAMETER
;
3546 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
3548 headers
= build_response_header(request
, TRUE
);
3551 LeaveCriticalSection( &request
->headers_section
);
3552 return ERROR_OUTOFMEMORY
;
3555 len
= strlenW(headers
) * sizeof(WCHAR
);
3556 if (len
+ sizeof(WCHAR
) > *lpdwBufferLength
)
3558 len
+= sizeof(WCHAR
);
3559 res
= ERROR_INSUFFICIENT_BUFFER
;
3563 memcpy(lpBuffer
, headers
, len
+ sizeof(WCHAR
));
3564 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
/ sizeof(WCHAR
)));
3565 res
= ERROR_SUCCESS
;
3567 *lpdwBufferLength
= len
;
3570 LeaveCriticalSection( &request
->headers_section
);
3573 case HTTP_QUERY_RAW_HEADERS
:
3579 headers
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, FALSE
);
3581 headers
= build_response_header(request
, FALSE
);
3585 LeaveCriticalSection( &request
->headers_section
);
3586 return ERROR_OUTOFMEMORY
;
3589 len
= strlenW(headers
) * sizeof(WCHAR
);
3590 if (len
> *lpdwBufferLength
)
3592 *lpdwBufferLength
= len
;
3594 LeaveCriticalSection( &request
->headers_section
);
3595 return ERROR_INSUFFICIENT_BUFFER
;
3602 TRACE("returning data: %s\n", debugstr_wn(headers
, len
/ sizeof(WCHAR
)));
3604 for (i
= 0; i
< len
/ sizeof(WCHAR
); i
++)
3606 if (headers
[i
] == '\n')
3609 memcpy(lpBuffer
, headers
, len
);
3611 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3614 LeaveCriticalSection( &request
->headers_section
);
3615 return ERROR_SUCCESS
;
3617 case HTTP_QUERY_STATUS_TEXT
:
3618 if (request
->statusText
)
3620 DWORD len
= strlenW(request
->statusText
);
3621 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3623 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3624 LeaveCriticalSection( &request
->headers_section
);
3625 return ERROR_INSUFFICIENT_BUFFER
;
3629 memcpy(lpBuffer
, request
->statusText
, (len
+ 1) * sizeof(WCHAR
));
3630 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3632 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3633 LeaveCriticalSection( &request
->headers_section
);
3634 return ERROR_SUCCESS
;
3637 case HTTP_QUERY_VERSION
:
3638 if (request
->version
)
3640 DWORD len
= strlenW(request
->version
);
3641 if (len
+ 1 > *lpdwBufferLength
/sizeof(WCHAR
))
3643 *lpdwBufferLength
= (len
+ 1) * sizeof(WCHAR
);
3644 LeaveCriticalSection( &request
->headers_section
);
3645 return ERROR_INSUFFICIENT_BUFFER
;
3649 memcpy(lpBuffer
, request
->version
, (len
+ 1) * sizeof(WCHAR
));
3650 TRACE("returning data: %s\n", debugstr_wn(lpBuffer
, len
));
3652 *lpdwBufferLength
= len
* sizeof(WCHAR
);
3653 LeaveCriticalSection( &request
->headers_section
);
3654 return ERROR_SUCCESS
;
3657 case HTTP_QUERY_CONTENT_ENCODING
:
3658 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[request
->read_gzip
? HTTP_QUERY_CONTENT_TYPE
: level
],
3659 requested_index
,request_only
);
3661 case HTTP_QUERY_STATUS_CODE
: {
3662 DWORD res
= ERROR_SUCCESS
;
3666 LeaveCriticalSection( &request
->headers_section
);
3667 return ERROR_HTTP_INVALID_QUERY_REQUEST
;
3673 if(dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) {
3674 if(*lpdwBufferLength
>= sizeof(DWORD
))
3675 *(DWORD
*)lpBuffer
= request
->status_code
;
3677 res
= ERROR_INSUFFICIENT_BUFFER
;
3678 *lpdwBufferLength
= sizeof(DWORD
);
3682 static const WCHAR formatW
[] = {'%','u',0};
3684 size
= sprintfW(buf
, formatW
, request
->status_code
) * sizeof(WCHAR
);
3686 if(size
<= *lpdwBufferLength
) {
3687 memcpy(lpBuffer
, buf
, size
+sizeof(WCHAR
));
3689 size
+= sizeof(WCHAR
);
3690 res
= ERROR_INSUFFICIENT_BUFFER
;
3693 *lpdwBufferLength
= size
;
3695 LeaveCriticalSection( &request
->headers_section
);
3699 assert (LAST_TABLE_HEADER
== (HTTP_QUERY_UNLESS_MODIFIED_SINCE
+ 1));
3701 if (level
< LAST_TABLE_HEADER
&& header_lookup
[level
])
3702 index
= HTTP_GetCustomHeaderIndex(request
, header_lookup
[level
],
3703 requested_index
,request_only
);
3707 lphttpHdr
= &request
->custHeaders
[index
];
3709 /* Ensure header satisfies requested attributes */
3711 ((dwInfoLevel
& HTTP_QUERY_FLAG_REQUEST_HEADERS
) &&
3712 (~lphttpHdr
->wFlags
& HDR_ISREQUEST
)))
3714 LeaveCriticalSection( &request
->headers_section
);
3715 return ERROR_HTTP_HEADER_NOT_FOUND
;
3718 /* coalesce value to requested type */
3719 if (dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
&& lpBuffer
)
3721 *(int *)lpBuffer
= atoiW(lphttpHdr
->lpszValue
);
3722 TRACE(" returning number: %d\n", *(int *)lpBuffer
);
3724 else if (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
&& lpBuffer
)
3730 tmpTime
= ConvertTimeString(lphttpHdr
->lpszValue
);
3732 tmpTM
= *gmtime(&tmpTime
);
3733 STHook
= (SYSTEMTIME
*)lpBuffer
;
3734 STHook
->wDay
= tmpTM
.tm_mday
;
3735 STHook
->wHour
= tmpTM
.tm_hour
;
3736 STHook
->wMilliseconds
= 0;
3737 STHook
->wMinute
= tmpTM
.tm_min
;
3738 STHook
->wDayOfWeek
= tmpTM
.tm_wday
;
3739 STHook
->wMonth
= tmpTM
.tm_mon
+ 1;
3740 STHook
->wSecond
= tmpTM
.tm_sec
;
3741 STHook
->wYear
= 1900+tmpTM
.tm_year
;
3743 TRACE(" returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
3744 STHook
->wYear
, STHook
->wMonth
, STHook
->wDay
, STHook
->wDayOfWeek
,
3745 STHook
->wHour
, STHook
->wMinute
, STHook
->wSecond
, STHook
->wMilliseconds
);
3747 else if (lphttpHdr
->lpszValue
)
3749 DWORD len
= (strlenW(lphttpHdr
->lpszValue
) + 1) * sizeof(WCHAR
);
3751 if (len
> *lpdwBufferLength
)
3753 *lpdwBufferLength
= len
;
3754 LeaveCriticalSection( &request
->headers_section
);
3755 return ERROR_INSUFFICIENT_BUFFER
;
3759 memcpy(lpBuffer
, lphttpHdr
->lpszValue
, len
);
3760 TRACE("! returning string: %s\n", debugstr_w(lpBuffer
));
3762 *lpdwBufferLength
= len
- sizeof(WCHAR
);
3764 if (lpdwIndex
) (*lpdwIndex
)++;
3766 LeaveCriticalSection( &request
->headers_section
);
3767 return ERROR_SUCCESS
;
3770 /***********************************************************************
3771 * HttpQueryInfoW (WININET.@)
3773 * Queries for information about an HTTP request
3780 BOOL WINAPI
HttpQueryInfoW(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3781 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3783 http_request_t
*request
;
3786 if (TRACE_ON(wininet
)) {
3787 #define FE(x) { x, #x }
3788 static const wininet_flag_info query_flags
[] = {
3789 FE(HTTP_QUERY_MIME_VERSION
),
3790 FE(HTTP_QUERY_CONTENT_TYPE
),
3791 FE(HTTP_QUERY_CONTENT_TRANSFER_ENCODING
),
3792 FE(HTTP_QUERY_CONTENT_ID
),
3793 FE(HTTP_QUERY_CONTENT_DESCRIPTION
),
3794 FE(HTTP_QUERY_CONTENT_LENGTH
),
3795 FE(HTTP_QUERY_CONTENT_LANGUAGE
),
3796 FE(HTTP_QUERY_ALLOW
),
3797 FE(HTTP_QUERY_PUBLIC
),
3798 FE(HTTP_QUERY_DATE
),
3799 FE(HTTP_QUERY_EXPIRES
),
3800 FE(HTTP_QUERY_LAST_MODIFIED
),
3801 FE(HTTP_QUERY_MESSAGE_ID
),
3803 FE(HTTP_QUERY_DERIVED_FROM
),
3804 FE(HTTP_QUERY_COST
),
3805 FE(HTTP_QUERY_LINK
),
3806 FE(HTTP_QUERY_PRAGMA
),
3807 FE(HTTP_QUERY_VERSION
),
3808 FE(HTTP_QUERY_STATUS_CODE
),
3809 FE(HTTP_QUERY_STATUS_TEXT
),
3810 FE(HTTP_QUERY_RAW_HEADERS
),
3811 FE(HTTP_QUERY_RAW_HEADERS_CRLF
),
3812 FE(HTTP_QUERY_CONNECTION
),
3813 FE(HTTP_QUERY_ACCEPT
),
3814 FE(HTTP_QUERY_ACCEPT_CHARSET
),
3815 FE(HTTP_QUERY_ACCEPT_ENCODING
),
3816 FE(HTTP_QUERY_ACCEPT_LANGUAGE
),
3817 FE(HTTP_QUERY_AUTHORIZATION
),
3818 FE(HTTP_QUERY_CONTENT_ENCODING
),
3819 FE(HTTP_QUERY_FORWARDED
),
3820 FE(HTTP_QUERY_FROM
),
3821 FE(HTTP_QUERY_IF_MODIFIED_SINCE
),
3822 FE(HTTP_QUERY_LOCATION
),
3823 FE(HTTP_QUERY_ORIG_URI
),
3824 FE(HTTP_QUERY_REFERER
),
3825 FE(HTTP_QUERY_RETRY_AFTER
),
3826 FE(HTTP_QUERY_SERVER
),
3827 FE(HTTP_QUERY_TITLE
),
3828 FE(HTTP_QUERY_USER_AGENT
),
3829 FE(HTTP_QUERY_WWW_AUTHENTICATE
),
3830 FE(HTTP_QUERY_PROXY_AUTHENTICATE
),
3831 FE(HTTP_QUERY_ACCEPT_RANGES
),
3832 FE(HTTP_QUERY_SET_COOKIE
),
3833 FE(HTTP_QUERY_COOKIE
),
3834 FE(HTTP_QUERY_REQUEST_METHOD
),
3835 FE(HTTP_QUERY_REFRESH
),
3836 FE(HTTP_QUERY_CONTENT_DISPOSITION
),
3838 FE(HTTP_QUERY_CACHE_CONTROL
),
3839 FE(HTTP_QUERY_CONTENT_BASE
),
3840 FE(HTTP_QUERY_CONTENT_LOCATION
),
3841 FE(HTTP_QUERY_CONTENT_MD5
),
3842 FE(HTTP_QUERY_CONTENT_RANGE
),
3843 FE(HTTP_QUERY_ETAG
),
3844 FE(HTTP_QUERY_HOST
),
3845 FE(HTTP_QUERY_IF_MATCH
),
3846 FE(HTTP_QUERY_IF_NONE_MATCH
),
3847 FE(HTTP_QUERY_IF_RANGE
),
3848 FE(HTTP_QUERY_IF_UNMODIFIED_SINCE
),
3849 FE(HTTP_QUERY_MAX_FORWARDS
),
3850 FE(HTTP_QUERY_PROXY_AUTHORIZATION
),
3851 FE(HTTP_QUERY_RANGE
),
3852 FE(HTTP_QUERY_TRANSFER_ENCODING
),
3853 FE(HTTP_QUERY_UPGRADE
),
3854 FE(HTTP_QUERY_VARY
),
3856 FE(HTTP_QUERY_WARNING
),
3857 FE(HTTP_QUERY_CUSTOM
)
3859 static const wininet_flag_info modifier_flags
[] = {
3860 FE(HTTP_QUERY_FLAG_REQUEST_HEADERS
),
3861 FE(HTTP_QUERY_FLAG_SYSTEMTIME
),
3862 FE(HTTP_QUERY_FLAG_NUMBER
),
3863 FE(HTTP_QUERY_FLAG_COALESCE
)
3866 DWORD info_mod
= dwInfoLevel
& HTTP_QUERY_MODIFIER_FLAGS_MASK
;
3867 DWORD info
= dwInfoLevel
& HTTP_QUERY_HEADER_MASK
;
3870 TRACE("(%p, 0x%08x)--> %d\n", hHttpRequest
, dwInfoLevel
, info
);
3871 TRACE(" Attribute:");
3872 for (i
= 0; i
< (sizeof(query_flags
) / sizeof(query_flags
[0])); i
++) {
3873 if (query_flags
[i
].val
== info
) {
3874 TRACE(" %s", query_flags
[i
].name
);
3878 if (i
== (sizeof(query_flags
) / sizeof(query_flags
[0]))) {
3879 TRACE(" Unknown (%08x)", info
);
3882 TRACE(" Modifier:");
3883 for (i
= 0; i
< (sizeof(modifier_flags
) / sizeof(modifier_flags
[0])); i
++) {
3884 if (modifier_flags
[i
].val
& info_mod
) {
3885 TRACE(" %s", modifier_flags
[i
].name
);
3886 info_mod
&= ~ modifier_flags
[i
].val
;
3891 TRACE(" Unknown (%08x)", info_mod
);
3896 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
3897 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
3899 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
3903 if (lpBuffer
== NULL
)
3904 *lpdwBufferLength
= 0;
3905 res
= HTTP_HttpQueryInfoW( request
, dwInfoLevel
,
3906 lpBuffer
, lpdwBufferLength
, lpdwIndex
);
3910 WININET_Release( &request
->hdr
);
3912 TRACE("%u <--\n", res
);
3913 if(res
!= ERROR_SUCCESS
)
3915 return res
== ERROR_SUCCESS
;
3918 /***********************************************************************
3919 * HttpQueryInfoA (WININET.@)
3921 * Queries for information about an HTTP request
3928 BOOL WINAPI
HttpQueryInfoA(HINTERNET hHttpRequest
, DWORD dwInfoLevel
,
3929 LPVOID lpBuffer
, LPDWORD lpdwBufferLength
, LPDWORD lpdwIndex
)
3935 TRACE("%p %x\n", hHttpRequest
, dwInfoLevel
);
3937 if((dwInfoLevel
& HTTP_QUERY_FLAG_NUMBER
) ||
3938 (dwInfoLevel
& HTTP_QUERY_FLAG_SYSTEMTIME
))
3940 return HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, lpBuffer
,
3941 lpdwBufferLength
, lpdwIndex
);
3947 len
= (*lpdwBufferLength
)*sizeof(WCHAR
);
3948 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
3950 alloclen
= MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, NULL
, 0 ) * sizeof(WCHAR
);
3956 bufferW
= heap_alloc(alloclen
);
3957 /* buffer is in/out because of HTTP_QUERY_CUSTOM */
3958 if ((dwInfoLevel
& HTTP_QUERY_HEADER_MASK
) == HTTP_QUERY_CUSTOM
)
3959 MultiByteToWideChar( CP_ACP
, 0, lpBuffer
, -1, bufferW
, alloclen
/ sizeof(WCHAR
) );
3966 result
= HttpQueryInfoW( hHttpRequest
, dwInfoLevel
, bufferW
,
3970 len
= WideCharToMultiByte( CP_ACP
,0, bufferW
, len
/ sizeof(WCHAR
) + 1,
3971 lpBuffer
, *lpdwBufferLength
, NULL
, NULL
);
3972 *lpdwBufferLength
= len
- 1;
3974 TRACE("lpBuffer: %s\n", debugstr_a(lpBuffer
));
3977 /* since the strings being returned from HttpQueryInfoW should be
3978 * only ASCII characters, it is reasonable to assume that all of
3979 * the Unicode characters can be reduced to a single byte */
3980 *lpdwBufferLength
= len
/ sizeof(WCHAR
);
3982 heap_free( bufferW
);
3986 static WCHAR
*get_redirect_url(http_request_t
*request
)
3988 static WCHAR szHttp
[] = {'h','t','t','p',0};
3989 static WCHAR szHttps
[] = {'h','t','t','p','s',0};
3990 http_session_t
*session
= request
->session
;
3991 URL_COMPONENTSW urlComponents
= { sizeof(urlComponents
) };
3992 WCHAR
*orig_url
= NULL
, *redirect_url
= NULL
, *combined_url
= NULL
;
3993 DWORD url_length
= 0, res
;
3997 res
= HTTP_HttpQueryInfoW(request
, HTTP_QUERY_LOCATION
, redirect_url
, &url_length
, NULL
);
3998 if(res
== ERROR_INSUFFICIENT_BUFFER
) {
3999 redirect_url
= heap_alloc(url_length
);
4000 res
= HTTP_HttpQueryInfoW(request
, HTTP_QUERY_LOCATION
, redirect_url
, &url_length
, NULL
);
4002 if(res
!= ERROR_SUCCESS
) {
4003 heap_free(redirect_url
);
4007 urlComponents
.dwSchemeLength
= 1;
4008 b
= InternetCrackUrlW(redirect_url
, url_length
/ sizeof(WCHAR
), 0, &urlComponents
);
4009 if(b
&& urlComponents
.dwSchemeLength
&&
4010 urlComponents
.nScheme
!= INTERNET_SCHEME_HTTP
&& urlComponents
.nScheme
!= INTERNET_SCHEME_HTTPS
) {
4011 TRACE("redirect to non-http URL\n");
4015 urlComponents
.lpszScheme
= (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) ? szHttps
: szHttp
;
4016 urlComponents
.dwSchemeLength
= 0;
4017 urlComponents
.lpszHostName
= request
->server
->name
;
4018 urlComponents
.nPort
= request
->server
->port
;
4019 urlComponents
.lpszUserName
= session
->userName
;
4020 urlComponents
.lpszUrlPath
= request
->path
;
4022 b
= InternetCreateUrlW(&urlComponents
, 0, NULL
, &url_length
);
4023 if(!b
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
4024 orig_url
= heap_alloc(url_length
);
4026 /* convert from bytes to characters */
4027 url_length
= url_length
/ sizeof(WCHAR
) - 1;
4028 b
= InternetCreateUrlW(&urlComponents
, 0, orig_url
, &url_length
);
4033 b
= InternetCombineUrlW(orig_url
, redirect_url
, NULL
, &url_length
, ICU_ENCODE_SPACES_ONLY
);
4034 if(!b
&& GetLastError() == ERROR_INSUFFICIENT_BUFFER
) {
4035 combined_url
= heap_alloc(url_length
* sizeof(WCHAR
));
4036 b
= InternetCombineUrlW(orig_url
, redirect_url
, combined_url
, &url_length
, ICU_ENCODE_SPACES_ONLY
);
4038 heap_free(combined_url
);
4039 combined_url
= NULL
;
4044 heap_free(orig_url
);
4045 heap_free(redirect_url
);
4046 return combined_url
;
4050 /***********************************************************************
4051 * HTTP_HandleRedirect (internal)
4053 static DWORD
HTTP_HandleRedirect(http_request_t
*request
, WCHAR
*url
)
4055 URL_COMPONENTSW urlComponents
= { sizeof(urlComponents
) };
4056 http_session_t
*session
= request
->session
;
4057 size_t url_len
= strlenW(url
);
4061 /* if it's an absolute path, keep the same session info */
4062 urlComponents
.lpszUrlPath
= url
;
4063 urlComponents
.dwUrlPathLength
= url_len
;
4067 urlComponents
.dwHostNameLength
= 1;
4068 urlComponents
.dwUserNameLength
= 1;
4069 urlComponents
.dwUrlPathLength
= 1;
4070 if(!InternetCrackUrlW(url
, url_len
, 0, &urlComponents
))
4071 return INTERNET_GetLastError();
4073 if(!urlComponents
.dwHostNameLength
)
4074 return ERROR_INTERNET_INVALID_URL
;
4077 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
, INTERNET_STATUS_REDIRECT
,
4078 url
, (url_len
+ 1) * sizeof(WCHAR
));
4080 if(urlComponents
.dwHostNameLength
) {
4081 BOOL custom_port
= FALSE
;
4084 if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTP
) {
4085 if(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) {
4086 TRACE("redirect from secure page to non-secure page\n");
4087 /* FIXME: warn about from secure redirect to non-secure page */
4088 request
->hdr
.dwFlags
&= ~INTERNET_FLAG_SECURE
;
4091 custom_port
= urlComponents
.nPort
!= INTERNET_DEFAULT_HTTP_PORT
;
4092 }else if(urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
) {
4093 if(!(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
)) {
4094 TRACE("redirect from non-secure page to secure page\n");
4095 /* FIXME: notify about redirect to secure page */
4096 request
->hdr
.dwFlags
|= INTERNET_FLAG_SECURE
;
4099 custom_port
= urlComponents
.nPort
!= INTERNET_DEFAULT_HTTPS_PORT
;
4102 heap_free(session
->hostName
);
4104 session
->hostName
= heap_strndupW(urlComponents
.lpszHostName
, urlComponents
.dwHostNameLength
);
4105 session
->hostPort
= urlComponents
.nPort
;
4107 heap_free(session
->userName
);
4108 session
->userName
= NULL
;
4109 if (urlComponents
.dwUserNameLength
)
4110 session
->userName
= heap_strndupW(urlComponents
.lpszUserName
, urlComponents
.dwUserNameLength
);
4112 reset_data_stream(request
);
4114 host
= substr(urlComponents
.lpszHostName
, urlComponents
.dwHostNameLength
);
4116 if(host
.len
!= strlenW(request
->server
->name
) || strncmpiW(request
->server
->name
, host
.str
, host
.len
)
4117 || request
->server
->port
!= urlComponents
.nPort
) {
4118 server_t
*new_server
;
4120 new_server
= get_server(host
, urlComponents
.nPort
, urlComponents
.nScheme
== INTERNET_SCHEME_HTTPS
, TRUE
);
4121 server_release(request
->server
);
4122 request
->server
= new_server
;
4126 HTTP_ProcessHeader(request
, hostW
, request
->server
->host_port
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
4128 HTTP_ProcessHeader(request
, hostW
, request
->server
->name
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_REQ
);
4131 heap_free(request
->path
);
4132 request
->path
= NULL
;
4133 if(urlComponents
.dwUrlPathLength
)
4140 path
= heap_strndupW(urlComponents
.lpszUrlPath
, urlComponents
.dwUrlPathLength
);
4141 rc
= UrlEscapeW(path
, &dummy
, &needed
, URL_ESCAPE_SPACES_ONLY
);
4142 if (rc
!= E_POINTER
)
4143 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
4144 request
->path
= heap_alloc(needed
*sizeof(WCHAR
));
4145 rc
= UrlEscapeW(path
, request
->path
, &needed
,
4146 URL_ESCAPE_SPACES_ONLY
);
4149 ERR("Unable to escape string!(%s) (%d)\n",debugstr_w(path
),rc
);
4150 strcpyW(request
->path
, path
);
4155 /* Remove custom content-type/length headers on redirects. */
4156 remove_header(request
, szContent_Type
, TRUE
);
4157 remove_header(request
, szContent_Length
, TRUE
);
4159 return ERROR_SUCCESS
;
4162 /***********************************************************************
4163 * HTTP_build_req (internal)
4165 * concatenate all the strings in the request together
4167 static LPWSTR
HTTP_build_req( LPCWSTR
*list
, int len
)
4172 for( t
= list
; *t
; t
++ )
4173 len
+= strlenW( *t
);
4176 str
= heap_alloc(len
*sizeof(WCHAR
));
4179 for( t
= list
; *t
; t
++ )
4185 static void HTTP_InsertCookies(http_request_t
*request
)
4190 res
= get_cookie_header(request
->server
->name
, request
->path
, &cookies
);
4191 if(res
!= ERROR_SUCCESS
|| !cookies
)
4194 HTTP_HttpAddRequestHeadersW(request
, cookies
, strlenW(cookies
),
4195 HTTP_ADDREQ_FLAG_REPLACE
| HTTP_ADDREQ_FLAG_ADD
);
4199 static WORD
HTTP_ParseWkday(LPCWSTR day
)
4201 static const WCHAR days
[7][4] = {{ 's','u','n',0 },
4209 for (i
= 0; i
< sizeof(days
)/sizeof(*days
); i
++)
4210 if (!strcmpiW(day
, days
[i
]))
4217 static WORD
HTTP_ParseMonth(LPCWSTR month
)
4219 static const WCHAR jan
[] = { 'j','a','n',0 };
4220 static const WCHAR feb
[] = { 'f','e','b',0 };
4221 static const WCHAR mar
[] = { 'm','a','r',0 };
4222 static const WCHAR apr
[] = { 'a','p','r',0 };
4223 static const WCHAR may
[] = { 'm','a','y',0 };
4224 static const WCHAR jun
[] = { 'j','u','n',0 };
4225 static const WCHAR jul
[] = { 'j','u','l',0 };
4226 static const WCHAR aug
[] = { 'a','u','g',0 };
4227 static const WCHAR sep
[] = { 's','e','p',0 };
4228 static const WCHAR oct
[] = { 'o','c','t',0 };
4229 static const WCHAR nov
[] = { 'n','o','v',0 };
4230 static const WCHAR dec
[] = { 'd','e','c',0 };
4232 if (!strcmpiW(month
, jan
)) return 1;
4233 if (!strcmpiW(month
, feb
)) return 2;
4234 if (!strcmpiW(month
, mar
)) return 3;
4235 if (!strcmpiW(month
, apr
)) return 4;
4236 if (!strcmpiW(month
, may
)) return 5;
4237 if (!strcmpiW(month
, jun
)) return 6;
4238 if (!strcmpiW(month
, jul
)) return 7;
4239 if (!strcmpiW(month
, aug
)) return 8;
4240 if (!strcmpiW(month
, sep
)) return 9;
4241 if (!strcmpiW(month
, oct
)) return 10;
4242 if (!strcmpiW(month
, nov
)) return 11;
4243 if (!strcmpiW(month
, dec
)) return 12;
4248 /* Parses the string pointed to by *str, assumed to be a 24-hour time HH:MM:SS,
4249 * optionally preceded by whitespace.
4250 * Upon success, returns TRUE, sets the wHour, wMinute, and wSecond fields of
4251 * st, and sets *str to the first character after the time format.
4253 static BOOL
HTTP_ParseTime(SYSTEMTIME
*st
, LPCWSTR
*str
)
4259 while (isspaceW(*ptr
))
4262 num
= strtoulW(ptr
, &nextPtr
, 10);
4263 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4265 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4270 ERR("unexpected hour in time format %s\n", debugstr_w(ptr
));
4274 st
->wHour
= (WORD
)num
;
4275 num
= strtoulW(ptr
, &nextPtr
, 10);
4276 if (!nextPtr
|| nextPtr
<= ptr
|| *nextPtr
!= ':')
4278 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4283 ERR("unexpected minute in time format %s\n", debugstr_w(ptr
));
4287 st
->wMinute
= (WORD
)num
;
4288 num
= strtoulW(ptr
, &nextPtr
, 10);
4289 if (!nextPtr
|| nextPtr
<= ptr
)
4291 ERR("unexpected time format %s\n", debugstr_w(ptr
));
4296 ERR("unexpected second in time format %s\n", debugstr_w(ptr
));
4301 st
->wSecond
= (WORD
)num
;
4305 static BOOL
HTTP_ParseDateAsAsctime(LPCWSTR value
, FILETIME
*ft
)
4307 static const WCHAR gmt
[]= { 'G','M','T',0 };
4308 WCHAR day
[4], *dayPtr
, month
[4], *monthPtr
, *nextPtr
;
4310 SYSTEMTIME st
= { 0 };
4313 for (ptr
= value
, dayPtr
= day
; *ptr
&& !isspaceW(*ptr
) &&
4314 dayPtr
- day
< sizeof(day
) / sizeof(day
[0]) - 1; ptr
++, dayPtr
++)
4317 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4318 if (st
.wDayOfWeek
>= 7)
4320 ERR("unexpected weekday %s\n", debugstr_w(day
));
4324 while (isspaceW(*ptr
))
4327 for (monthPtr
= month
; !isspaceW(*ptr
) &&
4328 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4332 st
.wMonth
= HTTP_ParseMonth(month
);
4333 if (!st
.wMonth
|| st
.wMonth
> 12)
4335 ERR("unexpected month %s\n", debugstr_w(month
));
4339 while (isspaceW(*ptr
))
4342 num
= strtoulW(ptr
, &nextPtr
, 10);
4343 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4345 ERR("unexpected day %s\n", debugstr_w(ptr
));
4349 st
.wDay
= (WORD
)num
;
4351 while (isspaceW(*ptr
))
4354 if (!HTTP_ParseTime(&st
, &ptr
))
4357 while (isspaceW(*ptr
))
4360 num
= strtoulW(ptr
, &nextPtr
, 10);
4361 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4363 ERR("unexpected year %s\n", debugstr_w(ptr
));
4367 st
.wYear
= (WORD
)num
;
4369 while (isspaceW(*ptr
))
4372 /* asctime() doesn't report a timezone, but some web servers do, so accept
4373 * with or without GMT.
4375 if (*ptr
&& strcmpW(ptr
, gmt
))
4377 ERR("unexpected timezone %s\n", debugstr_w(ptr
));
4380 return SystemTimeToFileTime(&st
, ft
);
4383 static BOOL
HTTP_ParseRfc1123Date(LPCWSTR value
, FILETIME
*ft
)
4385 static const WCHAR gmt
[]= { 'G','M','T',0 };
4386 WCHAR
*nextPtr
, day
[4], month
[4], *monthPtr
;
4389 SYSTEMTIME st
= { 0 };
4391 ptr
= strchrW(value
, ',');
4394 if (ptr
- value
!= 3)
4396 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4399 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4401 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4402 if (st
.wDayOfWeek
> 6)
4404 WARN("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4409 while (isspaceW(*ptr
))
4412 num
= strtoulW(ptr
, &nextPtr
, 10);
4413 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4415 WARN("unexpected day %s\n", debugstr_w(value
));
4419 st
.wDay
= (WORD
)num
;
4421 while (isspaceW(*ptr
))
4424 for (monthPtr
= month
; !isspaceW(*ptr
) &&
4425 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4429 st
.wMonth
= HTTP_ParseMonth(month
);
4430 if (!st
.wMonth
|| st
.wMonth
> 12)
4432 WARN("unexpected month %s\n", debugstr_w(month
));
4436 while (isspaceW(*ptr
))
4439 num
= strtoulW(ptr
, &nextPtr
, 10);
4440 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4442 ERR("unexpected year %s\n", debugstr_w(value
));
4446 st
.wYear
= (WORD
)num
;
4448 if (!HTTP_ParseTime(&st
, &ptr
))
4451 while (isspaceW(*ptr
))
4454 if (strcmpW(ptr
, gmt
))
4456 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4459 return SystemTimeToFileTime(&st
, ft
);
4462 static WORD
HTTP_ParseWeekday(LPCWSTR day
)
4464 static const WCHAR days
[7][10] = {{ 's','u','n','d','a','y',0 },
4465 { 'm','o','n','d','a','y',0 },
4466 { 't','u','e','s','d','a','y',0 },
4467 { 'w','e','d','n','e','s','d','a','y',0 },
4468 { 't','h','u','r','s','d','a','y',0 },
4469 { 'f','r','i','d','a','y',0 },
4470 { 's','a','t','u','r','d','a','y',0 }};
4472 for (i
= 0; i
< sizeof(days
)/sizeof(*days
); i
++)
4473 if (!strcmpiW(day
, days
[i
]))
4480 static BOOL
HTTP_ParseRfc850Date(LPCWSTR value
, FILETIME
*ft
)
4482 static const WCHAR gmt
[]= { 'G','M','T',0 };
4483 WCHAR
*nextPtr
, day
[10], month
[4], *monthPtr
;
4486 SYSTEMTIME st
= { 0 };
4488 ptr
= strchrW(value
, ',');
4491 if (ptr
- value
== 3)
4493 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4495 st
.wDayOfWeek
= HTTP_ParseWkday(day
);
4496 if (st
.wDayOfWeek
> 6)
4498 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4502 else if (ptr
- value
< sizeof(day
) / sizeof(day
[0]))
4504 memcpy(day
, value
, (ptr
- value
) * sizeof(WCHAR
));
4505 day
[ptr
- value
+ 1] = 0;
4506 st
.wDayOfWeek
= HTTP_ParseWeekday(day
);
4507 if (st
.wDayOfWeek
> 6)
4509 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4515 ERR("unexpected weekday %s\n", debugstr_wn(value
, ptr
- value
));
4520 while (isspaceW(*ptr
))
4523 num
= strtoulW(ptr
, &nextPtr
, 10);
4524 if (!nextPtr
|| nextPtr
<= ptr
|| !num
|| num
> 31)
4526 ERR("unexpected day %s\n", debugstr_w(value
));
4530 st
.wDay
= (WORD
)num
;
4534 ERR("unexpected month format %s\n", debugstr_w(ptr
));
4539 for (monthPtr
= month
; *ptr
!= '-' &&
4540 monthPtr
- month
< sizeof(month
) / sizeof(month
[0]) - 1;
4544 st
.wMonth
= HTTP_ParseMonth(month
);
4545 if (!st
.wMonth
|| st
.wMonth
> 12)
4547 ERR("unexpected month %s\n", debugstr_w(month
));
4553 ERR("unexpected year format %s\n", debugstr_w(ptr
));
4558 num
= strtoulW(ptr
, &nextPtr
, 10);
4559 if (!nextPtr
|| nextPtr
<= ptr
|| num
< 1601 || num
> 30827)
4561 ERR("unexpected year %s\n", debugstr_w(value
));
4565 st
.wYear
= (WORD
)num
;
4567 if (!HTTP_ParseTime(&st
, &ptr
))
4570 while (isspaceW(*ptr
))
4573 if (strcmpW(ptr
, gmt
))
4575 ERR("unexpected time zone %s\n", debugstr_w(ptr
));
4578 return SystemTimeToFileTime(&st
, ft
);
4581 static BOOL
HTTP_ParseDate(LPCWSTR value
, FILETIME
*ft
)
4583 static const WCHAR zero
[] = { '0',0 };
4586 if (!strcmpW(value
, zero
))
4588 ft
->dwLowDateTime
= ft
->dwHighDateTime
= 0;
4591 else if (strchrW(value
, ','))
4593 ret
= HTTP_ParseRfc1123Date(value
, ft
);
4596 ret
= HTTP_ParseRfc850Date(value
, ft
);
4598 ERR("unexpected date format %s\n", debugstr_w(value
));
4603 ret
= HTTP_ParseDateAsAsctime(value
, ft
);
4605 ERR("unexpected date format %s\n", debugstr_w(value
));
4610 static void HTTP_ProcessExpires(http_request_t
*request
)
4612 BOOL expirationFound
= FALSE
;
4615 EnterCriticalSection( &request
->headers_section
);
4617 /* Look for a Cache-Control header with a max-age directive, as it takes
4618 * precedence over the Expires header.
4620 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szCache_Control
, 0, FALSE
);
4621 if (headerIndex
!= -1)
4623 LPHTTPHEADERW ccHeader
= &request
->custHeaders
[headerIndex
];
4626 for (ptr
= ccHeader
->lpszValue
; ptr
&& *ptr
; )
4628 LPWSTR comma
= strchrW(ptr
, ','), end
, equal
;
4633 end
= ptr
+ strlenW(ptr
);
4634 for (equal
= end
- 1; equal
> ptr
&& *equal
!= '='; equal
--)
4638 static const WCHAR max_age
[] = {
4639 'm','a','x','-','a','g','e',0 };
4641 if (!strncmpiW(ptr
, max_age
, equal
- ptr
- 1))
4646 age
= strtoulW(equal
+ 1, &nextPtr
, 10);
4647 if (nextPtr
> equal
+ 1)
4651 NtQuerySystemTime( &ft
);
4652 /* Age is in seconds, FILETIME resolution is in
4653 * 100 nanosecond intervals.
4655 ft
.QuadPart
+= age
* (ULONGLONG
)1000000;
4656 request
->expires
.dwLowDateTime
= ft
.u
.LowPart
;
4657 request
->expires
.dwHighDateTime
= ft
.u
.HighPart
;
4658 expirationFound
= TRUE
;
4665 while (isspaceW(*ptr
))
4672 if (!expirationFound
)
4674 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szExpires
, 0, FALSE
);
4675 if (headerIndex
!= -1)
4677 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4680 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4682 expirationFound
= TRUE
;
4683 request
->expires
= ft
;
4687 if (!expirationFound
)
4691 /* With no known age, default to 10 minutes until expiration. */
4692 NtQuerySystemTime( &t
);
4693 t
.QuadPart
+= 10 * 60 * (ULONGLONG
)10000000;
4694 request
->expires
.dwLowDateTime
= t
.u
.LowPart
;
4695 request
->expires
.dwHighDateTime
= t
.u
.HighPart
;
4698 LeaveCriticalSection( &request
->headers_section
);
4701 static void HTTP_ProcessLastModified(http_request_t
*request
)
4705 EnterCriticalSection( &request
->headers_section
);
4707 headerIndex
= HTTP_GetCustomHeaderIndex(request
, szLast_Modified
, 0, FALSE
);
4708 if (headerIndex
!= -1)
4710 LPHTTPHEADERW expiresHeader
= &request
->custHeaders
[headerIndex
];
4713 if (HTTP_ParseDate(expiresHeader
->lpszValue
, &ft
))
4714 request
->last_modified
= ft
;
4717 LeaveCriticalSection( &request
->headers_section
);
4720 static void http_process_keep_alive(http_request_t
*req
)
4724 EnterCriticalSection( &req
->headers_section
);
4726 if ((index
= HTTP_GetCustomHeaderIndex(req
, szConnection
, 0, FALSE
)) != -1)
4727 req
->netconn
->keep_alive
= !strcmpiW(req
->custHeaders
[index
].lpszValue
, szKeepAlive
);
4728 else if ((index
= HTTP_GetCustomHeaderIndex(req
, szProxy_Connection
, 0, FALSE
)) != -1)
4729 req
->netconn
->keep_alive
= !strcmpiW(req
->custHeaders
[index
].lpszValue
, szKeepAlive
);
4731 req
->netconn
->keep_alive
= !strcmpiW(req
->version
, g_szHttp1_1
);
4733 LeaveCriticalSection( &req
->headers_section
);
4736 static DWORD
open_http_connection(http_request_t
*request
, BOOL
*reusing
)
4738 const BOOL is_https
= (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
) != 0;
4739 netconn_t
*netconn
= NULL
;
4742 if (request
->netconn
)
4744 if (NETCON_is_alive(request
->netconn
) && drain_content(request
, TRUE
) == ERROR_SUCCESS
)
4746 reset_data_stream(request
);
4748 return ERROR_SUCCESS
;
4751 TRACE("freeing netconn\n");
4752 free_netconn(request
->netconn
);
4753 request
->netconn
= NULL
;
4756 reset_data_stream(request
);
4758 res
= HTTP_ResolveName(request
);
4759 if(res
!= ERROR_SUCCESS
)
4762 EnterCriticalSection(&connection_pool_cs
);
4764 while(!list_empty(&request
->server
->conn_pool
)) {
4765 netconn
= LIST_ENTRY(list_head(&request
->server
->conn_pool
), netconn_t
, pool_entry
);
4766 list_remove(&netconn
->pool_entry
);
4768 if(is_valid_netconn(netconn
) && NETCON_is_alive(netconn
))
4771 TRACE("connection %p closed during idle\n", netconn
);
4772 free_netconn(netconn
);
4776 LeaveCriticalSection(&connection_pool_cs
);
4779 TRACE("<-- reusing %p netconn\n", netconn
);
4780 request
->netconn
= netconn
;
4782 return ERROR_SUCCESS
;
4785 TRACE("connecting to %s, proxy %s\n", debugstr_w(request
->server
->name
),
4786 request
->proxy
? debugstr_w(request
->proxy
->name
) : "(null)");
4788 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4789 INTERNET_STATUS_CONNECTING_TO_SERVER
,
4790 request
->server
->addr_str
,
4791 strlen(request
->server
->addr_str
)+1);
4793 res
= create_netconn(is_https
, request
->proxy
? request
->proxy
: request
->server
, request
->security_flags
,
4794 (request
->hdr
.ErrorMask
& INTERNET_ERROR_MASK_COMBINED_SEC_CERT
) != 0,
4795 request
->connect_timeout
, &netconn
);
4796 if(res
!= ERROR_SUCCESS
) {
4797 ERR("create_netconn failed: %u\n", res
);
4801 request
->netconn
= netconn
;
4803 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4804 INTERNET_STATUS_CONNECTED_TO_SERVER
,
4805 request
->server
->addr_str
, strlen(request
->server
->addr_str
)+1);
4808 TRACE("Created connection to %s: %p\n", debugstr_w(request
->server
->name
), netconn
);
4809 return ERROR_SUCCESS
;
4812 static char *build_ascii_request( const WCHAR
*str
, void *data
, DWORD data_len
, DWORD
*out_len
)
4814 int len
= WideCharToMultiByte( CP_ACP
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
4817 if (!(ret
= heap_alloc( len
+ data_len
))) return NULL
;
4818 WideCharToMultiByte( CP_ACP
, 0, str
, -1, ret
, len
, NULL
, NULL
);
4819 if (data_len
) memcpy( ret
+ len
- 1, data
, data_len
);
4820 *out_len
= len
+ data_len
- 1;
4825 static void set_content_length_header( http_request_t
*request
, DWORD len
, DWORD flags
)
4827 static const WCHAR fmtW
[] =
4828 {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',':',' ','%','u','\r','\n',0};
4829 WCHAR buf
[sizeof(fmtW
)/sizeof(fmtW
[0]) + 10];
4831 sprintfW( buf
, fmtW
, len
);
4832 HTTP_HttpAddRequestHeadersW( request
, buf
, ~0u, flags
);
4835 /***********************************************************************
4836 * HTTP_HttpSendRequestW (internal)
4838 * Sends the specified request to the HTTP server
4841 * ERROR_SUCCESS on success
4842 * win32 error code on failure
4845 static DWORD
HTTP_HttpSendRequestW(http_request_t
*request
, LPCWSTR lpszHeaders
,
4846 DWORD dwHeaderLength
, LPVOID lpOptional
, DWORD dwOptionalLength
,
4847 DWORD dwContentLength
, BOOL bEndRequest
)
4849 BOOL redirected
= FALSE
, secure_proxy_connect
= FALSE
, loop_next
;
4850 LPWSTR requestString
= NULL
;
4851 INT responseLen
, cnt
;
4854 TRACE("--> %p\n", request
);
4856 assert(request
->hdr
.htype
== WH_HHTTPREQ
);
4858 /* if the verb is NULL default to GET */
4860 request
->verb
= heap_strdupW(szGET
);
4862 HTTP_ProcessHeader(request
, hostW
, request
->server
->canon_host_port
,
4863 HTTP_ADDREQ_FLAG_ADD_IF_NEW
| HTTP_ADDHDR_FLAG_REQ
);
4865 if (dwContentLength
|| strcmpW(request
->verb
, szGET
))
4867 set_content_length_header(request
, dwContentLength
, HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4868 request
->bytesToWrite
= dwContentLength
;
4870 if (request
->session
->appInfo
->agent
)
4872 WCHAR
*agent_header
;
4873 static const WCHAR user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',':',' ','%','s','\r','\n',0};
4876 len
= strlenW(request
->session
->appInfo
->agent
) + strlenW(user_agent
);
4877 agent_header
= heap_alloc(len
* sizeof(WCHAR
));
4878 sprintfW(agent_header
, user_agent
, request
->session
->appInfo
->agent
);
4880 HTTP_HttpAddRequestHeadersW(request
, agent_header
, strlenW(agent_header
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4881 heap_free(agent_header
);
4883 if (request
->hdr
.dwFlags
& INTERNET_FLAG_PRAGMA_NOCACHE
)
4885 static const WCHAR pragma_nocache
[] = {'P','r','a','g','m','a',':',' ','n','o','-','c','a','c','h','e','\r','\n',0};
4886 HTTP_HttpAddRequestHeadersW(request
, pragma_nocache
, strlenW(pragma_nocache
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4888 if ((request
->hdr
.dwFlags
& INTERNET_FLAG_NO_CACHE_WRITE
) && strcmpW(request
->verb
, szGET
))
4890 static const WCHAR cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',':',
4891 ' ','n','o','-','c','a','c','h','e','\r','\n',0};
4892 HTTP_HttpAddRequestHeadersW(request
, cache_control
, strlenW(cache_control
), HTTP_ADDREQ_FLAG_ADD_IF_NEW
);
4895 /* add the headers the caller supplied */
4896 if( lpszHeaders
&& dwHeaderLength
)
4897 HTTP_HttpAddRequestHeadersW(request
, lpszHeaders
, dwHeaderLength
, HTTP_ADDREQ_FLAG_ADD
| HTTP_ADDHDR_FLAG_REPLACE
);
4901 DWORD len
, data_len
= dwOptionalLength
;
4902 BOOL reusing_connection
;
4908 request
->contentLength
= ~0u;
4909 request
->bytesToWrite
= 0;
4912 if (TRACE_ON(wininet
))
4916 EnterCriticalSection( &request
->headers_section
);
4917 host
= HTTP_GetHeader( request
, hostW
);
4918 TRACE("Going to url %s %s\n", debugstr_w(host
->lpszValue
), debugstr_w(request
->path
));
4919 LeaveCriticalSection( &request
->headers_section
);
4922 HTTP_FixURL(request
);
4923 if (request
->hdr
.dwFlags
& INTERNET_FLAG_KEEP_CONNECTION
)
4925 HTTP_ProcessHeader(request
, szConnection
, szKeepAlive
,
4926 HTTP_ADDHDR_FLAG_REQ
| HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
4928 HTTP_InsertAuthorization(request
, request
->authInfo
, szAuthorization
);
4929 HTTP_InsertAuthorization(request
, request
->proxyAuthInfo
, szProxy_Authorization
);
4931 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_COOKIES
))
4932 HTTP_InsertCookies(request
);
4934 res
= open_http_connection(request
, &reusing_connection
);
4935 if (res
!= ERROR_SUCCESS
)
4938 if (!reusing_connection
&& (request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
4940 if (request
->proxy
) secure_proxy_connect
= TRUE
;
4943 res
= NETCON_secure_connect(request
->netconn
, request
->server
);
4944 if (res
!= ERROR_SUCCESS
)
4946 WARN("failed to upgrade to secure connection\n");
4947 http_release_netconn(request
, FALSE
);
4952 if (secure_proxy_connect
)
4954 static const WCHAR connectW
[] = {'C','O','N','N','E','C','T',0};
4955 const WCHAR
*target
= request
->server
->host_port
;
4957 if (HTTP_GetCustomHeaderIndex(request
, szContent_Length
, 0, TRUE
) >= 0)
4958 set_content_length_header(request
, 0, HTTP_ADDREQ_FLAG_REPLACE
);
4960 requestString
= build_request_header(request
, connectW
, target
, g_szHttp1_1
, TRUE
);
4962 else if (request
->proxy
&& !(request
->hdr
.dwFlags
& INTERNET_FLAG_SECURE
))
4964 WCHAR
*url
= build_proxy_path_url(request
);
4965 requestString
= build_request_header(request
, request
->verb
, url
, request
->version
, TRUE
);
4970 if (request
->proxy
&& HTTP_GetCustomHeaderIndex(request
, szContent_Length
, 0, TRUE
) >= 0)
4971 set_content_length_header(request
, dwContentLength
, HTTP_ADDREQ_FLAG_REPLACE
);
4973 requestString
= build_request_header(request
, request
->verb
, request
->path
, request
->version
, TRUE
);
4976 TRACE("Request header -> %s\n", debugstr_w(requestString
) );
4978 /* send the request as ASCII, tack on the optional data */
4979 if (!lpOptional
|| redirected
|| secure_proxy_connect
)
4982 ascii_req
= build_ascii_request( requestString
, lpOptional
, data_len
, &len
);
4983 TRACE("full request -> %s\n", debugstr_a(ascii_req
) );
4985 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
4986 INTERNET_STATUS_SENDING_REQUEST
, NULL
, 0);
4988 NETCON_set_timeout( request
->netconn
, TRUE
, request
->send_timeout
);
4989 res
= NETCON_send(request
->netconn
, ascii_req
, len
, 0, &cnt
);
4990 heap_free( ascii_req
);
4991 if(res
!= ERROR_SUCCESS
) {
4992 TRACE("send failed: %u\n", res
);
4993 if(!reusing_connection
)
4995 http_release_netconn(request
, FALSE
);
5000 request
->bytesWritten
= data_len
;
5002 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5003 INTERNET_STATUS_REQUEST_SENT
,
5004 &len
, sizeof(DWORD
));
5010 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5011 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
5013 if (HTTP_GetResponseHeaders(request
, &responseLen
))
5015 http_release_netconn(request
, FALSE
);
5016 res
= ERROR_INTERNET_CONNECTION_ABORTED
;
5019 /* FIXME: We should know that connection is closed before sending
5020 * headers. Otherwise wrong callbacks are executed */
5021 if(!responseLen
&& reusing_connection
) {
5022 TRACE("Connection closed by server, reconnecting\n");
5023 http_release_netconn(request
, FALSE
);
5028 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5029 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
,
5032 http_process_keep_alive(request
);
5033 HTTP_ProcessCookies(request
);
5034 HTTP_ProcessExpires(request
);
5035 HTTP_ProcessLastModified(request
);
5037 res
= set_content_length(request
);
5038 if(res
!= ERROR_SUCCESS
)
5040 if(!request
->contentLength
)
5041 http_release_netconn(request
, TRUE
);
5043 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
) && responseLen
)
5047 switch(request
->status_code
) {
5048 case HTTP_STATUS_REDIRECT
:
5049 case HTTP_STATUS_MOVED
:
5050 case HTTP_STATUS_REDIRECT_KEEP_VERB
:
5051 case HTTP_STATUS_REDIRECT_METHOD
:
5052 new_url
= get_redirect_url(request
);
5056 if (strcmpW(request
->verb
, szGET
) && strcmpW(request
->verb
, szHEAD
) &&
5057 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5059 heap_free(request
->verb
);
5060 request
->verb
= heap_strdupW(szGET
);
5062 http_release_netconn(request
, drain_content(request
, FALSE
) == ERROR_SUCCESS
);
5063 res
= HTTP_HandleRedirect(request
, new_url
);
5065 if (res
== ERROR_SUCCESS
) {
5066 heap_free(requestString
);
5072 if (!(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTH
) && res
== ERROR_SUCCESS
)
5074 WCHAR szAuthValue
[2048];
5076 if (request
->status_code
== HTTP_STATUS_DENIED
)
5078 WCHAR
*host
= heap_strdupW( request
->server
->canon_host_port
);
5080 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_WWW_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5082 if (HTTP_DoAuthorization(request
, szAuthValue
,
5084 request
->session
->userName
,
5085 request
->session
->password
, host
))
5087 heap_free(requestString
);
5088 if(!drain_content(request
, TRUE
) == ERROR_SUCCESS
) {
5089 FIXME("Could not drain content\n");
5090 http_release_netconn(request
, FALSE
);
5099 TRACE("Cleaning wrong authorization data\n");
5100 destroy_authinfo(request
->authInfo
);
5101 request
->authInfo
= NULL
;
5104 if (request
->status_code
== HTTP_STATUS_PROXY_AUTH_REQ
)
5107 while (HTTP_HttpQueryInfoW(request
,HTTP_QUERY_PROXY_AUTHENTICATE
,szAuthValue
,&dwBufferSize
,&dwIndex
) == ERROR_SUCCESS
)
5109 if (HTTP_DoAuthorization(request
, szAuthValue
,
5110 &request
->proxyAuthInfo
,
5111 request
->session
->appInfo
->proxyUsername
,
5112 request
->session
->appInfo
->proxyPassword
,
5115 heap_free(requestString
);
5116 if(!drain_content(request
, TRUE
) == ERROR_SUCCESS
) {
5117 FIXME("Could not drain content\n");
5118 http_release_netconn(request
, FALSE
);
5126 TRACE("Cleaning wrong proxy authorization data\n");
5127 destroy_authinfo(request
->proxyAuthInfo
);
5128 request
->proxyAuthInfo
= NULL
;
5132 if (secure_proxy_connect
&& request
->status_code
== HTTP_STATUS_OK
)
5134 res
= NETCON_secure_connect(request
->netconn
, request
->server
);
5135 if (res
!= ERROR_SUCCESS
)
5137 WARN("failed to upgrade to secure proxy connection\n");
5138 http_release_netconn( request
, FALSE
);
5141 remove_header(request
, szProxy_Authorization
, TRUE
);
5142 destroy_authinfo(request
->proxyAuthInfo
);
5143 request
->proxyAuthInfo
= NULL
;
5145 secure_proxy_connect
= FALSE
;
5150 res
= ERROR_SUCCESS
;
5155 heap_free(requestString
);
5157 /* TODO: send notification for P3P header */
5159 if(res
== ERROR_SUCCESS
)
5160 create_cache_entry(request
);
5162 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5164 if (res
== ERROR_SUCCESS
) {
5165 if(bEndRequest
&& request
->contentLength
&& request
->bytesWritten
== request
->bytesToWrite
)
5166 HTTP_ReceiveRequestData(request
);
5168 send_request_complete(request
,
5169 request
->session
->hdr
.dwInternalFlags
& INET_OPENURL
? (DWORD_PTR
)request
->hdr
.hInternet
: 1, 0);
5171 send_request_complete(request
, 0, res
);
5187 } send_request_task_t
;
5189 /***********************************************************************
5191 * Helper functions for the HttpSendRequest(Ex) functions
5194 static void AsyncHttpSendRequestProc(task_header_t
*hdr
)
5196 send_request_task_t
*task
= (send_request_task_t
*)hdr
;
5197 http_request_t
*request
= (http_request_t
*)task
->hdr
.hdr
;
5199 TRACE("%p\n", request
);
5201 HTTP_HttpSendRequestW(request
, task
->headers
, task
->headers_len
, task
->optional
,
5202 task
->optional_len
, task
->content_len
, task
->end_request
);
5204 heap_free(task
->headers
);
5208 static DWORD
HTTP_HttpEndRequestW(http_request_t
*request
, DWORD dwFlags
, DWORD_PTR dwContext
)
5211 DWORD res
= ERROR_SUCCESS
;
5213 if(!is_valid_netconn(request
->netconn
)) {
5214 WARN("Not connected\n");
5215 send_request_complete(request
, 0, ERROR_INTERNET_OPERATION_CANCELLED
);
5216 return ERROR_INTERNET_OPERATION_CANCELLED
;
5219 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5220 INTERNET_STATUS_RECEIVING_RESPONSE
, NULL
, 0);
5222 if (HTTP_GetResponseHeaders(request
, &responseLen
) || !responseLen
)
5223 res
= ERROR_HTTP_HEADER_NOT_FOUND
;
5225 INTERNET_SendCallback(&request
->hdr
, request
->hdr
.dwContext
,
5226 INTERNET_STATUS_RESPONSE_RECEIVED
, &responseLen
, sizeof(DWORD
));
5228 /* process cookies here. Is this right? */
5229 http_process_keep_alive(request
);
5230 HTTP_ProcessCookies(request
);
5231 HTTP_ProcessExpires(request
);
5232 HTTP_ProcessLastModified(request
);
5234 if ((res
= set_content_length(request
)) == ERROR_SUCCESS
) {
5235 if(!request
->contentLength
)
5236 http_release_netconn(request
, TRUE
);
5239 if (res
== ERROR_SUCCESS
&& !(request
->hdr
.dwFlags
& INTERNET_FLAG_NO_AUTO_REDIRECT
))
5241 switch(request
->status_code
) {
5242 case HTTP_STATUS_REDIRECT
:
5243 case HTTP_STATUS_MOVED
:
5244 case HTTP_STATUS_REDIRECT_METHOD
:
5245 case HTTP_STATUS_REDIRECT_KEEP_VERB
: {
5248 new_url
= get_redirect_url(request
);
5252 if (strcmpW(request
->verb
, szGET
) && strcmpW(request
->verb
, szHEAD
) &&
5253 request
->status_code
!= HTTP_STATUS_REDIRECT_KEEP_VERB
)
5255 heap_free(request
->verb
);
5256 request
->verb
= heap_strdupW(szGET
);
5258 http_release_netconn(request
, drain_content(request
, FALSE
) == ERROR_SUCCESS
);
5259 res
= HTTP_HandleRedirect(request
, new_url
);
5261 if (res
== ERROR_SUCCESS
)
5262 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, TRUE
);
5267 if(res
== ERROR_SUCCESS
)
5268 create_cache_entry(request
);
5270 if (res
== ERROR_SUCCESS
&& request
->contentLength
)
5271 HTTP_ReceiveRequestData(request
);
5273 send_request_complete(request
, res
== ERROR_SUCCESS
, res
);
5278 /***********************************************************************
5279 * HttpEndRequestA (WININET.@)
5281 * Ends an HTTP request that was started by HttpSendRequestEx
5284 * TRUE if successful
5288 BOOL WINAPI
HttpEndRequestA(HINTERNET hRequest
,
5289 LPINTERNET_BUFFERSA lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5291 TRACE("(%p, %p, %08x, %08lx)\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5295 SetLastError(ERROR_INVALID_PARAMETER
);
5299 return HttpEndRequestW(hRequest
, NULL
, dwFlags
, dwContext
);
5306 } end_request_task_t
;
5308 static void AsyncHttpEndRequestProc(task_header_t
*hdr
)
5310 end_request_task_t
*task
= (end_request_task_t
*)hdr
;
5311 http_request_t
*req
= (http_request_t
*)task
->hdr
.hdr
;
5315 HTTP_HttpEndRequestW(req
, task
->flags
, task
->context
);
5318 /***********************************************************************
5319 * HttpEndRequestW (WININET.@)
5321 * Ends an HTTP request that was started by HttpSendRequestEx
5324 * TRUE if successful
5328 BOOL WINAPI
HttpEndRequestW(HINTERNET hRequest
,
5329 LPINTERNET_BUFFERSW lpBuffersOut
, DWORD dwFlags
, DWORD_PTR dwContext
)
5331 http_request_t
*request
;
5334 TRACE("%p %p %x %lx -->\n", hRequest
, lpBuffersOut
, dwFlags
, dwContext
);
5338 SetLastError(ERROR_INVALID_PARAMETER
);
5342 request
= (http_request_t
*) get_handle_object( hRequest
);
5344 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5346 SetLastError(ERROR_INTERNET_INCORRECT_HANDLE_TYPE
);
5348 WININET_Release( &request
->hdr
);
5351 request
->hdr
.dwFlags
|= dwFlags
;
5353 if (request
->session
->appInfo
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5355 end_request_task_t
*task
;
5357 task
= alloc_async_task(&request
->hdr
, AsyncHttpEndRequestProc
, sizeof(*task
));
5358 task
->flags
= dwFlags
;
5359 task
->context
= dwContext
;
5361 INTERNET_AsyncCall(&task
->hdr
);
5362 res
= ERROR_IO_PENDING
;
5365 res
= HTTP_HttpEndRequestW(request
, dwFlags
, dwContext
);
5367 WININET_Release( &request
->hdr
);
5368 TRACE("%u <--\n", res
);
5369 if(res
!= ERROR_SUCCESS
)
5371 return res
== ERROR_SUCCESS
;
5374 /***********************************************************************
5375 * HttpSendRequestExA (WININET.@)
5377 * Sends the specified request to the HTTP server and allows chunked
5382 * Failure: FALSE, call GetLastError() for more information.
5384 BOOL WINAPI
HttpSendRequestExA(HINTERNET hRequest
,
5385 LPINTERNET_BUFFERSA lpBuffersIn
,
5386 LPINTERNET_BUFFERSA lpBuffersOut
,
5387 DWORD dwFlags
, DWORD_PTR dwContext
)
5389 INTERNET_BUFFERSW BuffersInW
;
5392 LPWSTR header
= NULL
;
5394 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5395 lpBuffersOut
, dwFlags
, dwContext
);
5399 BuffersInW
.dwStructSize
= sizeof(LPINTERNET_BUFFERSW
);
5400 if (lpBuffersIn
->lpcszHeader
)
5402 headerlen
= MultiByteToWideChar(CP_ACP
,0,lpBuffersIn
->lpcszHeader
,
5403 lpBuffersIn
->dwHeadersLength
,0,0);
5404 header
= heap_alloc(headerlen
*sizeof(WCHAR
));
5405 if (!(BuffersInW
.lpcszHeader
= header
))
5407 SetLastError(ERROR_OUTOFMEMORY
);
5410 BuffersInW
.dwHeadersLength
= MultiByteToWideChar(CP_ACP
, 0,
5411 lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5415 BuffersInW
.lpcszHeader
= NULL
;
5416 BuffersInW
.dwHeadersTotal
= lpBuffersIn
->dwHeadersTotal
;
5417 BuffersInW
.lpvBuffer
= lpBuffersIn
->lpvBuffer
;
5418 BuffersInW
.dwBufferLength
= lpBuffersIn
->dwBufferLength
;
5419 BuffersInW
.dwBufferTotal
= lpBuffersIn
->dwBufferTotal
;
5420 BuffersInW
.Next
= NULL
;
5423 rc
= HttpSendRequestExW(hRequest
, lpBuffersIn
? &BuffersInW
: NULL
, NULL
, dwFlags
, dwContext
);
5429 /***********************************************************************
5430 * HttpSendRequestExW (WININET.@)
5432 * Sends the specified request to the HTTP server and allows chunked
5437 * Failure: FALSE, call GetLastError() for more information.
5439 BOOL WINAPI
HttpSendRequestExW(HINTERNET hRequest
,
5440 LPINTERNET_BUFFERSW lpBuffersIn
,
5441 LPINTERNET_BUFFERSW lpBuffersOut
,
5442 DWORD dwFlags
, DWORD_PTR dwContext
)
5444 http_request_t
*request
;
5445 http_session_t
*session
;
5449 TRACE("(%p, %p, %p, %08x, %08lx)\n", hRequest
, lpBuffersIn
,
5450 lpBuffersOut
, dwFlags
, dwContext
);
5452 request
= (http_request_t
*) get_handle_object( hRequest
);
5454 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5456 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5460 session
= request
->session
;
5461 assert(session
->hdr
.htype
== WH_HHTTPSESSION
);
5462 hIC
= session
->appInfo
;
5463 assert(hIC
->hdr
.htype
== WH_HINIT
);
5465 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5467 send_request_task_t
*task
;
5469 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5474 if (lpBuffersIn
->lpcszHeader
)
5476 if (lpBuffersIn
->dwHeadersLength
== ~0u)
5477 size
= (strlenW( lpBuffersIn
->lpcszHeader
) + 1) * sizeof(WCHAR
);
5479 size
= lpBuffersIn
->dwHeadersLength
* sizeof(WCHAR
);
5481 task
->headers
= heap_alloc(size
);
5482 memcpy(task
->headers
, lpBuffersIn
->lpcszHeader
, size
);
5484 else task
->headers
= NULL
;
5486 task
->headers_len
= size
/ sizeof(WCHAR
);
5487 task
->optional
= lpBuffersIn
->lpvBuffer
;
5488 task
->optional_len
= lpBuffersIn
->dwBufferLength
;
5489 task
->content_len
= lpBuffersIn
->dwBufferTotal
;
5493 task
->headers
= NULL
;
5494 task
->headers_len
= 0;
5495 task
->optional
= NULL
;
5496 task
->optional_len
= 0;
5497 task
->content_len
= 0;
5500 task
->end_request
= FALSE
;
5502 INTERNET_AsyncCall(&task
->hdr
);
5503 res
= ERROR_IO_PENDING
;
5508 res
= HTTP_HttpSendRequestW(request
, lpBuffersIn
->lpcszHeader
, lpBuffersIn
->dwHeadersLength
,
5509 lpBuffersIn
->lpvBuffer
, lpBuffersIn
->dwBufferLength
,
5510 lpBuffersIn
->dwBufferTotal
, FALSE
);
5512 res
= HTTP_HttpSendRequestW(request
, NULL
, 0, NULL
, 0, 0, FALSE
);
5517 WININET_Release( &request
->hdr
);
5521 return res
== ERROR_SUCCESS
;
5524 /***********************************************************************
5525 * HttpSendRequestW (WININET.@)
5527 * Sends the specified request to the HTTP server
5534 BOOL WINAPI
HttpSendRequestW(HINTERNET hHttpRequest
, LPCWSTR lpszHeaders
,
5535 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5537 http_request_t
*request
;
5538 http_session_t
*session
= NULL
;
5539 appinfo_t
*hIC
= NULL
;
5540 DWORD res
= ERROR_SUCCESS
;
5542 TRACE("%p, %s, %i, %p, %i)\n", hHttpRequest
,
5543 debugstr_wn(lpszHeaders
, dwHeaderLength
), dwHeaderLength
, lpOptional
, dwOptionalLength
);
5545 request
= (http_request_t
*) get_handle_object( hHttpRequest
);
5546 if (NULL
== request
|| request
->hdr
.htype
!= WH_HHTTPREQ
)
5548 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5552 session
= request
->session
;
5553 if (NULL
== session
|| session
->hdr
.htype
!= WH_HHTTPSESSION
)
5555 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5559 hIC
= session
->appInfo
;
5560 if (NULL
== hIC
|| hIC
->hdr
.htype
!= WH_HINIT
)
5562 res
= ERROR_INTERNET_INCORRECT_HANDLE_TYPE
;
5566 if (hIC
->hdr
.dwFlags
& INTERNET_FLAG_ASYNC
)
5568 send_request_task_t
*task
;
5570 task
= alloc_async_task(&request
->hdr
, AsyncHttpSendRequestProc
, sizeof(*task
));
5575 if (dwHeaderLength
== ~0u) size
= (strlenW(lpszHeaders
) + 1) * sizeof(WCHAR
);
5576 else size
= dwHeaderLength
* sizeof(WCHAR
);
5578 task
->headers
= heap_alloc(size
);
5579 memcpy(task
->headers
, lpszHeaders
, size
);
5582 task
->headers
= NULL
;
5583 task
->headers_len
= dwHeaderLength
;
5584 task
->optional
= lpOptional
;
5585 task
->optional_len
= dwOptionalLength
;
5586 task
->content_len
= dwOptionalLength
;
5587 task
->end_request
= TRUE
;
5589 INTERNET_AsyncCall(&task
->hdr
);
5590 res
= ERROR_IO_PENDING
;
5594 res
= HTTP_HttpSendRequestW(request
, lpszHeaders
,
5595 dwHeaderLength
, lpOptional
, dwOptionalLength
,
5596 dwOptionalLength
, TRUE
);
5600 WININET_Release( &request
->hdr
);
5603 return res
== ERROR_SUCCESS
;
5606 /***********************************************************************
5607 * HttpSendRequestA (WININET.@)
5609 * Sends the specified request to the HTTP server
5616 BOOL WINAPI
HttpSendRequestA(HINTERNET hHttpRequest
, LPCSTR lpszHeaders
,
5617 DWORD dwHeaderLength
, LPVOID lpOptional
,DWORD dwOptionalLength
)
5620 LPWSTR szHeaders
=NULL
;
5621 DWORD nLen
=dwHeaderLength
;
5622 if(lpszHeaders
!=NULL
)
5624 nLen
=MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,NULL
,0);
5625 szHeaders
= heap_alloc(nLen
*sizeof(WCHAR
));
5626 MultiByteToWideChar(CP_ACP
,0,lpszHeaders
,dwHeaderLength
,szHeaders
,nLen
);
5628 result
= HttpSendRequestW(hHttpRequest
, szHeaders
, nLen
, lpOptional
, dwOptionalLength
);
5629 heap_free(szHeaders
);
5633 /***********************************************************************
5634 * HTTPSESSION_Destroy (internal)
5636 * Deallocate session handle
5639 static void HTTPSESSION_Destroy(object_header_t
*hdr
)
5641 http_session_t
*session
= (http_session_t
*) hdr
;
5643 TRACE("%p\n", session
);
5645 WININET_Release(&session
->appInfo
->hdr
);
5647 heap_free(session
->hostName
);
5648 heap_free(session
->password
);
5649 heap_free(session
->userName
);
5652 static DWORD
HTTPSESSION_QueryOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD
*size
, BOOL unicode
)
5654 http_session_t
*ses
= (http_session_t
*)hdr
;
5657 case INTERNET_OPTION_HANDLE_TYPE
:
5658 TRACE("INTERNET_OPTION_HANDLE_TYPE\n");
5660 if (*size
< sizeof(ULONG
))
5661 return ERROR_INSUFFICIENT_BUFFER
;
5663 *size
= sizeof(DWORD
);
5664 *(DWORD
*)buffer
= INTERNET_HANDLE_TYPE_CONNECT_HTTP
;
5665 return ERROR_SUCCESS
;
5666 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5667 TRACE("INTERNET_OPTION_CONNECT_TIMEOUT\n");
5669 if (*size
< sizeof(DWORD
))
5670 return ERROR_INSUFFICIENT_BUFFER
;
5672 *size
= sizeof(DWORD
);
5673 *(DWORD
*)buffer
= ses
->connect_timeout
;
5674 return ERROR_SUCCESS
;
5676 case INTERNET_OPTION_SEND_TIMEOUT
:
5677 TRACE("INTERNET_OPTION_SEND_TIMEOUT\n");
5679 if (*size
< sizeof(DWORD
))
5680 return ERROR_INSUFFICIENT_BUFFER
;
5682 *size
= sizeof(DWORD
);
5683 *(DWORD
*)buffer
= ses
->send_timeout
;
5684 return ERROR_SUCCESS
;
5686 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5687 TRACE("INTERNET_OPTION_RECEIVE_TIMEOUT\n");
5689 if (*size
< sizeof(DWORD
))
5690 return ERROR_INSUFFICIENT_BUFFER
;
5692 *size
= sizeof(DWORD
);
5693 *(DWORD
*)buffer
= ses
->receive_timeout
;
5694 return ERROR_SUCCESS
;
5697 return INET_QueryOption(hdr
, option
, buffer
, size
, unicode
);
5700 static DWORD
HTTPSESSION_SetOption(object_header_t
*hdr
, DWORD option
, void *buffer
, DWORD size
)
5702 http_session_t
*ses
= (http_session_t
*)hdr
;
5705 case INTERNET_OPTION_USERNAME
:
5707 heap_free(ses
->userName
);
5708 if (!(ses
->userName
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5709 return ERROR_SUCCESS
;
5711 case INTERNET_OPTION_PASSWORD
:
5713 heap_free(ses
->password
);
5714 if (!(ses
->password
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5715 return ERROR_SUCCESS
;
5717 case INTERNET_OPTION_PROXY_USERNAME
:
5719 heap_free(ses
->appInfo
->proxyUsername
);
5720 if (!(ses
->appInfo
->proxyUsername
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5721 return ERROR_SUCCESS
;
5723 case INTERNET_OPTION_PROXY_PASSWORD
:
5725 heap_free(ses
->appInfo
->proxyPassword
);
5726 if (!(ses
->appInfo
->proxyPassword
= heap_strdupW(buffer
))) return ERROR_OUTOFMEMORY
;
5727 return ERROR_SUCCESS
;
5729 case INTERNET_OPTION_CONNECT_TIMEOUT
:
5731 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5732 ses
->connect_timeout
= *(DWORD
*)buffer
;
5733 return ERROR_SUCCESS
;
5735 case INTERNET_OPTION_SEND_TIMEOUT
:
5737 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5738 ses
->send_timeout
= *(DWORD
*)buffer
;
5739 return ERROR_SUCCESS
;
5741 case INTERNET_OPTION_RECEIVE_TIMEOUT
:
5743 if (!buffer
|| size
!= sizeof(DWORD
)) return ERROR_INVALID_PARAMETER
;
5744 ses
->receive_timeout
= *(DWORD
*)buffer
;
5745 return ERROR_SUCCESS
;
5750 return INET_SetOption(hdr
, option
, buffer
, size
);
5753 static const object_vtbl_t HTTPSESSIONVtbl
= {
5754 HTTPSESSION_Destroy
,
5756 HTTPSESSION_QueryOption
,
5757 HTTPSESSION_SetOption
,
5765 /***********************************************************************
5766 * HTTP_Connect (internal)
5768 * Create http session handle
5771 * HINTERNET a session handle on success
5775 DWORD
HTTP_Connect(appinfo_t
*hIC
, LPCWSTR lpszServerName
,
5776 INTERNET_PORT serverPort
, LPCWSTR lpszUserName
,
5777 LPCWSTR lpszPassword
, DWORD dwFlags
, DWORD_PTR dwContext
,
5778 DWORD dwInternalFlags
, HINTERNET
*ret
)
5780 http_session_t
*session
= NULL
;
5784 if (!lpszServerName
|| !lpszServerName
[0])
5785 return ERROR_INVALID_PARAMETER
;
5787 assert( hIC
->hdr
.htype
== WH_HINIT
);
5789 session
= alloc_object(&hIC
->hdr
, &HTTPSESSIONVtbl
, sizeof(http_session_t
));
5791 return ERROR_OUTOFMEMORY
;
5794 * According to my tests. The name is not resolved until a request is sent
5797 session
->hdr
.htype
= WH_HHTTPSESSION
;
5798 session
->hdr
.dwFlags
= dwFlags
;
5799 session
->hdr
.dwContext
= dwContext
;
5800 session
->hdr
.dwInternalFlags
|= dwInternalFlags
;
5801 session
->hdr
.decoding
= hIC
->hdr
.decoding
;
5803 WININET_AddRef( &hIC
->hdr
);
5804 session
->appInfo
= hIC
;
5805 list_add_head( &hIC
->hdr
.children
, &session
->hdr
.entry
);
5807 session
->hostName
= heap_strdupW(lpszServerName
);
5808 if (lpszUserName
&& lpszUserName
[0])
5809 session
->userName
= heap_strdupW(lpszUserName
);
5810 if (lpszPassword
&& lpszPassword
[0])
5811 session
->password
= heap_strdupW(lpszPassword
);
5812 session
->hostPort
= serverPort
;
5813 session
->connect_timeout
= hIC
->connect_timeout
;
5814 session
->send_timeout
= 0;
5815 session
->receive_timeout
= 0;
5817 /* Don't send a handle created callback if this handle was created with InternetOpenUrl */
5818 if (!(session
->hdr
.dwInternalFlags
& INET_OPENURL
))
5820 INTERNET_SendCallback(&hIC
->hdr
, dwContext
,
5821 INTERNET_STATUS_HANDLE_CREATED
, &session
->hdr
.hInternet
,
5826 * an INTERNET_STATUS_REQUEST_COMPLETE is NOT sent here as per my tests on
5830 TRACE("%p --> %p\n", hIC
, session
);
5832 *ret
= session
->hdr
.hInternet
;
5833 return ERROR_SUCCESS
;
5836 /***********************************************************************
5837 * HTTP_clear_response_headers (internal)
5839 * clear out any old response headers
5841 static void HTTP_clear_response_headers( http_request_t
*request
)
5845 EnterCriticalSection( &request
->headers_section
);
5847 for( i
=0; i
<request
->nCustHeaders
; i
++)
5849 if( !request
->custHeaders
[i
].lpszField
)
5851 if( !request
->custHeaders
[i
].lpszValue
)
5853 if ( request
->custHeaders
[i
].wFlags
& HDR_ISREQUEST
)
5855 HTTP_DeleteCustomHeader( request
, i
);
5859 LeaveCriticalSection( &request
->headers_section
);
5862 /***********************************************************************
5863 * HTTP_GetResponseHeaders (internal)
5865 * Read server response
5872 static DWORD
HTTP_GetResponseHeaders(http_request_t
*request
, INT
*len
)
5875 WCHAR buffer
[MAX_REPLY_LEN
];
5876 DWORD buflen
= MAX_REPLY_LEN
;
5878 char bufferA
[MAX_REPLY_LEN
];
5879 LPWSTR status_code
= NULL
, status_text
= NULL
;
5880 DWORD res
= ERROR_HTTP_INVALID_SERVER_RESPONSE
;
5881 BOOL codeHundred
= FALSE
;
5885 if(!is_valid_netconn(request
->netconn
))
5888 /* clear old response headers (eg. from a redirect response) */
5889 HTTP_clear_response_headers( request
);
5891 NETCON_set_timeout( request
->netconn
, FALSE
, request
->receive_timeout
);
5894 * We should first receive 'HTTP/1.x nnn OK' where nnn is the status code.
5896 buflen
= MAX_REPLY_LEN
;
5897 if ((res
= read_line(request
, bufferA
, &buflen
)))
5900 if (!buflen
) goto lend
;
5903 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
5904 /* check is this a status code line? */
5905 if (!strncmpW(buffer
, g_szHttp1_0
, 4))
5907 /* split the version from the status code */
5908 status_code
= strchrW( buffer
, ' ' );
5913 /* split the status code from the status text */
5914 status_text
= strchrW( status_code
, ' ' );
5918 request
->status_code
= atoiW(status_code
);
5920 TRACE("version [%s] status code [%s] status text [%s]\n",
5921 debugstr_w(buffer
), debugstr_w(status_code
), debugstr_w(status_text
) );
5923 codeHundred
= request
->status_code
== HTTP_STATUS_CONTINUE
;
5925 else if (!codeHundred
)
5927 WARN("No status line at head of response (%s)\n", debugstr_w(buffer
));
5929 heap_free(request
->version
);
5930 heap_free(request
->statusText
);
5932 request
->status_code
= HTTP_STATUS_OK
;
5933 request
->version
= heap_strdupW(g_szHttp1_0
);
5934 request
->statusText
= heap_strdupW(szOK
);
5938 } while (codeHundred
);
5940 /* Add status code */
5941 HTTP_ProcessHeader(request
, szStatus
, status_code
,
5942 HTTP_ADDHDR_FLAG_REPLACE
| HTTP_ADDHDR_FLAG_ADD
);
5944 heap_free(request
->version
);
5945 heap_free(request
->statusText
);
5947 request
->version
= heap_strdupW(buffer
);
5948 request
->statusText
= heap_strdupW(status_text
? status_text
: emptyW
);
5950 /* Restore the spaces */
5951 *(status_code
-1) = ' ';
5953 *(status_text
-1) = ' ';
5955 /* Parse each response line */
5958 buflen
= MAX_REPLY_LEN
;
5959 if (!read_line(request
, bufferA
, &buflen
) && buflen
)
5961 LPWSTR
* pFieldAndValue
;
5963 TRACE("got line %s, now interpreting\n", debugstr_a(bufferA
));
5965 if (!bufferA
[0]) break;
5966 MultiByteToWideChar( CP_ACP
, 0, bufferA
, buflen
, buffer
, MAX_REPLY_LEN
);
5968 pFieldAndValue
= HTTP_InterpretHttpHeader(buffer
);
5971 HTTP_ProcessHeader(request
, pFieldAndValue
[0], pFieldAndValue
[1],
5972 HTTP_ADDREQ_FLAG_ADD
);
5973 HTTP_FreeTokens(pFieldAndValue
);
5984 res
= ERROR_SUCCESS
;
5993 /***********************************************************************
5994 * HTTP_InterpretHttpHeader (internal)
5996 * Parse server response
6000 * Pointer to array of field, value, NULL on success.
6003 static LPWSTR
* HTTP_InterpretHttpHeader(LPCWSTR buffer
)
6005 LPWSTR
* pTokenPair
;
6009 pTokenPair
= heap_alloc_zero(sizeof(*pTokenPair
)*3);
6011 pszColon
= strchrW(buffer
, ':');
6012 /* must have two tokens */
6015 HTTP_FreeTokens(pTokenPair
);
6017 TRACE("No ':' in line: %s\n", debugstr_w(buffer
));
6021 pTokenPair
[0] = heap_alloc((pszColon
- buffer
+ 1) * sizeof(WCHAR
));
6024 HTTP_FreeTokens(pTokenPair
);
6027 memcpy(pTokenPair
[0], buffer
, (pszColon
- buffer
) * sizeof(WCHAR
));
6028 pTokenPair
[0][pszColon
- buffer
] = '\0';
6032 len
= strlenW(pszColon
);
6033 pTokenPair
[1] = heap_alloc((len
+ 1) * sizeof(WCHAR
));
6036 HTTP_FreeTokens(pTokenPair
);
6039 memcpy(pTokenPair
[1], pszColon
, (len
+ 1) * sizeof(WCHAR
));
6041 strip_spaces(pTokenPair
[0]);
6042 strip_spaces(pTokenPair
[1]);
6044 TRACE("field(%s) Value(%s)\n", debugstr_w(pTokenPair
[0]), debugstr_w(pTokenPair
[1]));
6048 /***********************************************************************
6049 * HTTP_ProcessHeader (internal)
6051 * Stuff header into header tables according to <dwModifier>
6055 #define COALESCEFLAGS (HTTP_ADDHDR_FLAG_COALESCE|HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA|HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON)
6057 static DWORD
HTTP_ProcessHeader(http_request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD dwModifier
)
6059 LPHTTPHEADERW lphttpHdr
;
6061 BOOL request_only
= !!(dwModifier
& HTTP_ADDHDR_FLAG_REQ
);
6062 DWORD res
= ERROR_SUCCESS
;
6064 TRACE("--> %s: %s - 0x%08x\n", debugstr_w(field
), debugstr_w(value
), dwModifier
);
6066 EnterCriticalSection( &request
->headers_section
);
6068 index
= HTTP_GetCustomHeaderIndex(request
, field
, 0, request_only
);
6071 lphttpHdr
= &request
->custHeaders
[index
];
6073 /* replace existing header if FLAG_REPLACE is given */
6074 if (dwModifier
& HTTP_ADDHDR_FLAG_REPLACE
)
6076 HTTP_DeleteCustomHeader( request
, index
);
6078 if (value
&& value
[0])
6082 hdr
.lpszField
= (LPWSTR
)field
;
6083 hdr
.lpszValue
= (LPWSTR
)value
;
6084 hdr
.wFlags
= hdr
.wCount
= 0;
6086 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6087 hdr
.wFlags
|= HDR_ISREQUEST
;
6089 res
= HTTP_InsertCustomHeader( request
, &hdr
);
6095 /* do not add new header if FLAG_ADD_IF_NEW is set */
6096 if (dwModifier
& HTTP_ADDHDR_FLAG_ADD_IF_NEW
)
6098 res
= ERROR_HTTP_INVALID_HEADER
; /* FIXME */
6102 /* handle appending to existing header */
6103 if (dwModifier
& COALESCEFLAGS
)
6108 INT origlen
= strlenW(lphttpHdr
->lpszValue
);
6109 INT valuelen
= strlenW(value
);
6111 /* FIXME: Should it really clear HDR_ISREQUEST? */
6112 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6113 lphttpHdr
->wFlags
|= HDR_ISREQUEST
;
6115 lphttpHdr
->wFlags
&= ~HDR_ISREQUEST
;
6117 if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_COMMA
)
6120 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6122 else if (dwModifier
& HTTP_ADDHDR_FLAG_COALESCE_WITH_SEMICOLON
)
6125 lphttpHdr
->wFlags
|= HDR_COMMADELIMITED
;
6128 len
= origlen
+ valuelen
+ ((ch
> 0) ? 2 : 0);
6130 lpsztmp
= heap_realloc(lphttpHdr
->lpszValue
, (len
+1)*sizeof(WCHAR
));
6133 lphttpHdr
->lpszValue
= lpsztmp
;
6134 /* FIXME: Increment lphttpHdr->wCount. Perhaps lpszValue should be an array */
6137 lphttpHdr
->lpszValue
[origlen
] = ch
;
6139 lphttpHdr
->lpszValue
[origlen
] = ' ';
6143 memcpy(&lphttpHdr
->lpszValue
[origlen
], value
, valuelen
*sizeof(WCHAR
));
6144 lphttpHdr
->lpszValue
[len
] = '\0';
6148 WARN("heap_realloc (%d bytes) failed\n",len
+1);
6149 res
= ERROR_OUTOFMEMORY
;
6156 /* FIXME: What about other combinations? */
6157 if ((dwModifier
& ~HTTP_ADDHDR_FLAG_REQ
) == HTTP_ADDHDR_FLAG_REPLACE
)
6159 res
= ERROR_HTTP_HEADER_NOT_FOUND
;
6163 /* FIXME: What if value == ""? */
6168 hdr
.lpszField
= (LPWSTR
)field
;
6169 hdr
.lpszValue
= (LPWSTR
)value
;
6170 hdr
.wFlags
= hdr
.wCount
= 0;
6172 if (dwModifier
& HTTP_ADDHDR_FLAG_REQ
)
6173 hdr
.wFlags
|= HDR_ISREQUEST
;
6175 res
= HTTP_InsertCustomHeader( request
, &hdr
);
6179 /* FIXME: What if value == NULL? */
6181 TRACE("<-- %d\n", res
);
6182 LeaveCriticalSection( &request
->headers_section
);
6186 /***********************************************************************
6187 * HTTP_GetCustomHeaderIndex (internal)
6189 * Return index of custom header from header array
6190 * Headers section must be held
6192 static INT
HTTP_GetCustomHeaderIndex(http_request_t
*request
, LPCWSTR lpszField
,
6193 int requested_index
, BOOL request_only
)
6197 TRACE("%s, %d, %d\n", debugstr_w(lpszField
), requested_index
, request_only
);
6199 for (index
= 0; index
< request
->nCustHeaders
; index
++)
6201 if (strcmpiW(request
->custHeaders
[index
].lpszField
, lpszField
))
6204 if (request_only
&& !(request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6207 if (!request_only
&& (request
->custHeaders
[index
].wFlags
& HDR_ISREQUEST
))
6210 if (requested_index
== 0)
6215 if (index
>= request
->nCustHeaders
)
6218 TRACE("Return: %d\n", index
);
6223 /***********************************************************************
6224 * HTTP_InsertCustomHeader (internal)
6226 * Insert header into array
6227 * Headers section must be held
6229 static DWORD
HTTP_InsertCustomHeader(http_request_t
*request
, LPHTTPHEADERW lpHdr
)
6232 LPHTTPHEADERW lph
= NULL
;
6234 TRACE("--> %s: %s\n", debugstr_w(lpHdr
->lpszField
), debugstr_w(lpHdr
->lpszValue
));
6235 count
= request
->nCustHeaders
+ 1;
6237 lph
= heap_realloc_zero(request
->custHeaders
, sizeof(HTTPHEADERW
) * count
);
6239 lph
= heap_alloc_zero(sizeof(HTTPHEADERW
) * count
);
6242 return ERROR_OUTOFMEMORY
;
6244 request
->custHeaders
= lph
;
6245 request
->custHeaders
[count
-1].lpszField
= heap_strdupW(lpHdr
->lpszField
);
6246 request
->custHeaders
[count
-1].lpszValue
= heap_strdupW(lpHdr
->lpszValue
);
6247 request
->custHeaders
[count
-1].wFlags
= lpHdr
->wFlags
;
6248 request
->custHeaders
[count
-1].wCount
= lpHdr
->wCount
;
6249 request
->nCustHeaders
++;
6251 return ERROR_SUCCESS
;
6255 /***********************************************************************
6256 * HTTP_DeleteCustomHeader (internal)
6258 * Delete header from array
6259 * If this function is called, the index may change.
6260 * Headers section must be held
6262 static BOOL
HTTP_DeleteCustomHeader(http_request_t
*request
, DWORD index
)
6264 if( request
->nCustHeaders
<= 0 )
6266 if( index
>= request
->nCustHeaders
)
6268 request
->nCustHeaders
--;
6270 heap_free(request
->custHeaders
[index
].lpszField
);
6271 heap_free(request
->custHeaders
[index
].lpszValue
);
6273 memmove( &request
->custHeaders
[index
], &request
->custHeaders
[index
+1],
6274 (request
->nCustHeaders
- index
)* sizeof(HTTPHEADERW
) );
6275 memset( &request
->custHeaders
[request
->nCustHeaders
], 0, sizeof(HTTPHEADERW
) );
6281 /***********************************************************************
6282 * IsHostInProxyBypassList (@)
6287 BOOL WINAPI
IsHostInProxyBypassList(DWORD flags
, LPCSTR szHost
, DWORD length
)
6289 FIXME("STUB: flags=%d host=%s length=%d\n",flags
,szHost
,length
);