2 * Copyright 2008 Hans Leidekker for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wine/port.h"
21 #include "wine/debug.h"
24 #ifdef HAVE_ARPA_INET_H
25 # include <arpa/inet.h>
33 #include "winhttp_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(winhttp
);
37 static const WCHAR attr_accept
[] = {'A','c','c','e','p','t',0};
38 static const WCHAR attr_accept_charset
[] = {'A','c','c','e','p','t','-','C','h','a','r','s','e','t', 0};
39 static const WCHAR attr_accept_encoding
[] = {'A','c','c','e','p','t','-','E','n','c','o','d','i','n','g',0};
40 static const WCHAR attr_accept_language
[] = {'A','c','c','e','p','t','-','L','a','n','g','u','a','g','e',0};
41 static const WCHAR attr_accept_ranges
[] = {'A','c','c','e','p','t','-','R','a','n','g','e','s',0};
42 static const WCHAR attr_age
[] = {'A','g','e',0};
43 static const WCHAR attr_allow
[] = {'A','l','l','o','w',0};
44 static const WCHAR attr_authorization
[] = {'A','u','t','h','o','r','i','z','a','t','i','o','n',0};
45 static const WCHAR attr_cache_control
[] = {'C','a','c','h','e','-','C','o','n','t','r','o','l',0};
46 static const WCHAR attr_connection
[] = {'C','o','n','n','e','c','t','i','o','n',0};
47 static const WCHAR attr_content_base
[] = {'C','o','n','t','e','n','t','-','B','a','s','e',0};
48 static const WCHAR attr_content_encoding
[] = {'C','o','n','t','e','n','t','-','E','n','c','o','d','i','n','g',0};
49 static const WCHAR attr_content_id
[] = {'C','o','n','t','e','n','t','-','I','D',0};
50 static const WCHAR attr_content_language
[] = {'C','o','n','t','e','n','t','-','L','a','n','g','u','a','g','e',0};
51 static const WCHAR attr_content_length
[] = {'C','o','n','t','e','n','t','-','L','e','n','g','t','h',0};
52 static const WCHAR attr_content_location
[] = {'C','o','n','t','e','n','t','-','L','o','c','a','t','i','o','n',0};
53 static const WCHAR attr_content_md5
[] = {'C','o','n','t','e','n','t','-','M','D','5',0};
54 static const WCHAR attr_content_range
[] = {'C','o','n','t','e','n','t','-','R','a','n','g','e',0};
55 static const WCHAR attr_content_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};
56 static const WCHAR attr_content_type
[] = {'C','o','n','t','e','n','t','-','T','y','p','e',0};
57 static const WCHAR attr_cookie
[] = {'C','o','o','k','i','e',0};
58 static const WCHAR attr_date
[] = {'D','a','t','e',0};
59 static const WCHAR attr_from
[] = {'F','r','o','m',0};
60 static const WCHAR attr_etag
[] = {'E','T','a','g',0};
61 static const WCHAR attr_expect
[] = {'E','x','p','e','c','t',0};
62 static const WCHAR attr_expires
[] = {'E','x','p','i','r','e','s',0};
63 static const WCHAR attr_host
[] = {'H','o','s','t',0};
64 static const WCHAR attr_if_match
[] = {'I','f','-','M','a','t','c','h',0};
65 static const WCHAR attr_if_modified_since
[] = {'I','f','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
66 static const WCHAR attr_if_none_match
[] = {'I','f','-','N','o','n','e','-','M','a','t','c','h',0};
67 static const WCHAR attr_if_range
[] = {'I','f','-','R','a','n','g','e',0};
68 static const WCHAR attr_if_unmodified_since
[] = {'I','f','-','U','n','m','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
69 static const WCHAR attr_last_modified
[] = {'L','a','s','t','-','M','o','d','i','f','i','e','d',0};
70 static const WCHAR attr_location
[] = {'L','o','c','a','t','i','o','n',0};
71 static const WCHAR attr_max_forwards
[] = {'M','a','x','-','F','o','r','w','a','r','d','s',0};
72 static const WCHAR attr_mime_version
[] = {'M','i','m','e','-','V','e','r','s','i','o','n',0};
73 static const WCHAR attr_pragma
[] = {'P','r','a','g','m','a',0};
74 static const WCHAR attr_proxy_authenticate
[] = {'P','r','o','x','y','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
75 static const WCHAR attr_proxy_authorization
[] = {'P','r','o','x','y','-','A','u','t','h','o','r','i','z','a','t','i','o','n',0};
76 static const WCHAR attr_proxy_connection
[] = {'P','r','o','x','y','-','C','o','n','n','e','c','t','i','o','n',0};
77 static const WCHAR attr_public
[] = {'P','u','b','l','i','c',0};
78 static const WCHAR attr_range
[] = {'R','a','n','g','e',0};
79 static const WCHAR attr_referer
[] = {'R','e','f','e','r','e','r',0};
80 static const WCHAR attr_retry_after
[] = {'R','e','t','r','y','-','A','f','t','e','r',0};
81 static const WCHAR attr_server
[] = {'S','e','r','v','e','r',0};
82 static const WCHAR attr_set_cookie
[] = {'S','e','t','-','C','o','o','k','i','e',0};
83 static const WCHAR attr_status
[] = {'S','t','a','t','u','s',0};
84 static const WCHAR attr_transfer_encoding
[] = {'T','r','a','n','s','f','e','r','-','E','n','c','o','d','i','n','g',0};
85 static const WCHAR attr_unless_modified_since
[] = {'U','n','l','e','s','s','-','M','o','d','i','f','i','e','d','-','S','i','n','c','e',0};
86 static const WCHAR attr_upgrade
[] = {'U','p','g','r','a','d','e',0};
87 static const WCHAR attr_uri
[] = {'U','R','I',0};
88 static const WCHAR attr_user_agent
[] = {'U','s','e','r','-','A','g','e','n','t',0};
89 static const WCHAR attr_vary
[] = {'V','a','r','y',0};
90 static const WCHAR attr_via
[] = {'V','i','a',0};
91 static const WCHAR attr_warning
[] = {'W','a','r','n','i','n','g',0};
92 static const WCHAR attr_www_authenticate
[] = {'W','W','W','-','A','u','t','h','e','n','t','i','c','a','t','e',0};
94 static const WCHAR
*attribute_table
[] =
96 attr_mime_version
, /* WINHTTP_QUERY_MIME_VERSION = 0 */
97 attr_content_type
, /* WINHTTP_QUERY_CONTENT_TYPE = 1 */
98 attr_content_transfer_encoding
, /* WINHTTP_QUERY_CONTENT_TRANSFER_ENCODING = 2 */
99 attr_content_id
, /* WINHTTP_QUERY_CONTENT_ID = 3 */
100 NULL
, /* WINHTTP_QUERY_CONTENT_DESCRIPTION = 4 */
101 attr_content_length
, /* WINHTTP_QUERY_CONTENT_LENGTH = 5 */
102 attr_content_language
, /* WINHTTP_QUERY_CONTENT_LANGUAGE = 6 */
103 attr_allow
, /* WINHTTP_QUERY_ALLOW = 7 */
104 attr_public
, /* WINHTTP_QUERY_PUBLIC = 8 */
105 attr_date
, /* WINHTTP_QUERY_DATE = 9 */
106 attr_expires
, /* WINHTTP_QUERY_EXPIRES = 10 */
107 attr_last_modified
, /* WINHTTP_QUERY_LAST_MODIFIEDcw = 11 */
108 NULL
, /* WINHTTP_QUERY_MESSAGE_ID = 12 */
109 attr_uri
, /* WINHTTP_QUERY_URI = 13 */
110 attr_from
, /* WINHTTP_QUERY_DERIVED_FROM = 14 */
111 NULL
, /* WINHTTP_QUERY_COST = 15 */
112 NULL
, /* WINHTTP_QUERY_LINK = 16 */
113 attr_pragma
, /* WINHTTP_QUERY_PRAGMA = 17 */
114 NULL
, /* WINHTTP_QUERY_VERSION = 18 */
115 attr_status
, /* WINHTTP_QUERY_STATUS_CODE = 19 */
116 NULL
, /* WINHTTP_QUERY_STATUS_TEXT = 20 */
117 NULL
, /* WINHTTP_QUERY_RAW_HEADERS = 21 */
118 NULL
, /* WINHTTP_QUERY_RAW_HEADERS_CRLF = 22 */
119 attr_connection
, /* WINHTTP_QUERY_CONNECTION = 23 */
120 attr_accept
, /* WINHTTP_QUERY_ACCEPT = 24 */
121 attr_accept_charset
, /* WINHTTP_QUERY_ACCEPT_CHARSET = 25 */
122 attr_accept_encoding
, /* WINHTTP_QUERY_ACCEPT_ENCODING = 26 */
123 attr_accept_language
, /* WINHTTP_QUERY_ACCEPT_LANGUAGE = 27 */
124 attr_authorization
, /* WINHTTP_QUERY_AUTHORIZATION = 28 */
125 attr_content_encoding
, /* WINHTTP_QUERY_CONTENT_ENCODING = 29 */
126 NULL
, /* WINHTTP_QUERY_FORWARDED = 30 */
127 NULL
, /* WINHTTP_QUERY_FROM = 31 */
128 attr_if_modified_since
, /* WINHTTP_QUERY_IF_MODIFIED_SINCE = 32 */
129 attr_location
, /* WINHTTP_QUERY_LOCATION = 33 */
130 NULL
, /* WINHTTP_QUERY_ORIG_URI = 34 */
131 attr_referer
, /* WINHTTP_QUERY_REFERER = 35 */
132 attr_retry_after
, /* WINHTTP_QUERY_RETRY_AFTER = 36 */
133 attr_server
, /* WINHTTP_QUERY_SERVER = 37 */
134 NULL
, /* WINHTTP_TITLE = 38 */
135 attr_user_agent
, /* WINHTTP_QUERY_USER_AGENT = 39 */
136 attr_www_authenticate
, /* WINHTTP_QUERY_WWW_AUTHENTICATE = 40 */
137 attr_proxy_authenticate
, /* WINHTTP_QUERY_PROXY_AUTHENTICATE = 41 */
138 attr_accept_ranges
, /* WINHTTP_QUERY_ACCEPT_RANGES = 42 */
139 attr_set_cookie
, /* WINHTTP_QUERY_SET_COOKIE = 43 */
140 attr_cookie
, /* WINHTTP_QUERY_COOKIE = 44 */
141 NULL
, /* WINHTTP_QUERY_REQUEST_METHOD = 45 */
142 NULL
, /* WINHTTP_QUERY_REFRESH = 46 */
143 NULL
, /* WINHTTP_QUERY_CONTENT_DISPOSITION = 47 */
144 attr_age
, /* WINHTTP_QUERY_AGE = 48 */
145 attr_cache_control
, /* WINHTTP_QUERY_CACHE_CONTROL = 49 */
146 attr_content_base
, /* WINHTTP_QUERY_CONTENT_BASE = 50 */
147 attr_content_location
, /* WINHTTP_QUERY_CONTENT_LOCATION = 51 */
148 attr_content_md5
, /* WINHTTP_QUERY_CONTENT_MD5 = 52 */
149 attr_content_range
, /* WINHTTP_QUERY_CONTENT_RANGE = 53 */
150 attr_etag
, /* WINHTTP_QUERY_ETAG = 54 */
151 attr_host
, /* WINHTTP_QUERY_HOST = 55 */
152 attr_if_match
, /* WINHTTP_QUERY_IF_MATCH = 56 */
153 attr_if_none_match
, /* WINHTTP_QUERY_IF_NONE_MATCH = 57 */
154 attr_if_range
, /* WINHTTP_QUERY_IF_RANGE = 58 */
155 attr_if_unmodified_since
, /* WINHTTP_QUERY_IF_UNMODIFIED_SINCE = 59 */
156 attr_max_forwards
, /* WINHTTP_QUERY_MAX_FORWARDS = 60 */
157 attr_proxy_authorization
, /* WINHTTP_QUERY_PROXY_AUTHORIZATION = 61 */
158 attr_range
, /* WINHTTP_QUERY_RANGE = 62 */
159 attr_transfer_encoding
, /* WINHTTP_QUERY_TRANSFER_ENCODING = 63 */
160 attr_upgrade
, /* WINHTTP_QUERY_UPGRADE = 64 */
161 attr_vary
, /* WINHTTP_QUERY_VARY = 65 */
162 attr_via
, /* WINHTTP_QUERY_VIA = 66 */
163 attr_warning
, /* WINHTTP_QUERY_WARNING = 67 */
164 attr_expect
, /* WINHTTP_QUERY_EXPECT = 68 */
165 attr_proxy_connection
, /* WINHTTP_QUERY_PROXY_CONNECTION = 69 */
166 attr_unless_modified_since
, /* WINHTTP_QUERY_UNLESS_MODIFIED_SINCE = 70 */
167 NULL
, /* WINHTTP_QUERY_PROXY_SUPPORT = 75 */
168 NULL
, /* WINHTTP_QUERY_AUTHENTICATION_INFO = 76 */
169 NULL
, /* WINHTTP_QUERY_PASSPORT_URLS = 77 */
170 NULL
/* WINHTTP_QUERY_PASSPORT_CONFIG = 78 */
173 static void free_header( header_t
*header
)
175 heap_free( header
->field
);
176 heap_free( header
->value
);
180 static BOOL
valid_token_char( WCHAR c
)
182 if (c
< 32 || c
== 127) return FALSE
;
189 case '\\': case '\"':
201 static header_t
*parse_header( LPCWSTR string
)
208 if (!(q
= strchrW( p
, ':' )))
210 WARN("no ':' in line %s\n", debugstr_w(string
));
215 WARN("empty field name in line %s\n", debugstr_w(string
));
220 if (!valid_token_char( *p
))
222 WARN("invalid character in field name %s\n", debugstr_w(string
));
228 if (!(header
= heap_alloc_zero( sizeof(header_t
) ))) return NULL
;
229 if (!(header
->field
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
234 memcpy( header
->field
, string
, len
* sizeof(WCHAR
) );
235 header
->field
[len
] = 0;
237 q
++; /* skip past colon */
238 while (*q
== ' ') q
++;
241 WARN("no value in line %s\n", debugstr_w(string
));
245 if (!(header
->value
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) )))
247 free_header( header
);
250 memcpy( header
->value
, q
, len
* sizeof(WCHAR
) );
251 header
->value
[len
] = 0;
256 static int get_header_index( request_t
*request
, LPCWSTR field
, int requested_index
, BOOL request_only
)
260 TRACE("%s\n", debugstr_w(field
));
262 for (index
= 0; index
< request
->num_headers
; index
++)
264 if (strcmpiW( request
->headers
[index
].field
, field
)) continue;
265 if (request_only
&& !request
->headers
[index
].is_request
) continue;
266 if (!request_only
&& request
->headers
[index
].is_request
) continue;
268 if (!requested_index
) break;
271 if (index
>= request
->num_headers
) index
= -1;
272 TRACE("returning %d\n", index
);
276 static BOOL
insert_header( request_t
*request
, header_t
*header
)
281 count
= request
->num_headers
+ 1;
283 hdrs
= heap_realloc_zero( request
->headers
, sizeof(header_t
) * count
);
285 hdrs
= heap_alloc_zero( sizeof(header_t
) * count
);
289 request
->headers
= hdrs
;
290 request
->headers
[count
- 1].field
= strdupW( header
->field
);
291 request
->headers
[count
- 1].value
= strdupW( header
->value
);
292 request
->headers
[count
- 1].is_request
= header
->is_request
;
293 request
->num_headers
++;
299 static BOOL
delete_header( request_t
*request
, DWORD index
)
301 if (!request
->num_headers
) return FALSE
;
302 if (index
>= request
->num_headers
) return FALSE
;
303 request
->num_headers
--;
305 heap_free( request
->headers
[index
].field
);
306 heap_free( request
->headers
[index
].value
);
308 memmove( &request
->headers
[index
], &request
->headers
[index
+ 1], (request
->num_headers
- index
) * sizeof(header_t
) );
309 memset( &request
->headers
[request
->num_headers
], 0, sizeof(header_t
) );
313 static BOOL
process_header( request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD flags
, BOOL request_only
)
318 TRACE("%s: %s 0x%08x\n", debugstr_w(field
), debugstr_w(value
), flags
);
320 /* replace wins out over add */
321 if (flags
& WINHTTP_ADDREQ_FLAG_REPLACE
) flags
&= ~WINHTTP_ADDREQ_FLAG_ADD
;
323 if (flags
& WINHTTP_ADDREQ_FLAG_ADD
) index
= -1;
325 index
= get_header_index( request
, field
, 0, request_only
);
329 if (flags
& WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
) return FALSE
;
330 header
= &request
->headers
[index
];
336 hdr
.field
= (LPWSTR
)field
;
337 hdr
.value
= (LPWSTR
)value
;
338 hdr
.is_request
= request_only
;
340 return insert_header( request
, &hdr
);
342 /* no value to delete */
345 if (flags
& WINHTTP_ADDREQ_FLAG_REPLACE
)
347 delete_header( request
, index
);
352 hdr
.field
= (LPWSTR
)field
;
353 hdr
.value
= (LPWSTR
)value
;
354 hdr
.is_request
= request_only
;
356 return insert_header( request
, &hdr
);
360 else if (flags
& (WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
| WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
))
363 int len
, orig_len
, value_len
;
365 orig_len
= strlenW( header
->value
);
366 value_len
= strlenW( value
);
368 if (flags
& WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
) sep
= ',';
371 len
= orig_len
+ value_len
+ 2;
372 if ((tmp
= heap_realloc( header
->value
, (len
+ 1) * sizeof(WCHAR
) )))
376 header
->value
[orig_len
] = sep
;
378 header
->value
[orig_len
] = ' ';
381 memcpy( &header
->value
[orig_len
], value
, value_len
* sizeof(WCHAR
) );
382 header
->value
[len
] = 0;
389 static BOOL
add_request_headers( request_t
*request
, LPCWSTR headers
, DWORD len
, DWORD flags
)
392 WCHAR
*buffer
, *p
, *q
;
395 if (len
== ~0UL) len
= strlenW( headers
);
396 if (!(buffer
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
397 strcpyW( buffer
, headers
);
405 if (q
[0] == '\r' && q
[1] == '\n') break;
412 q
+= 2; /* jump over \r\n */
414 if ((header
= parse_header( p
)))
416 ret
= process_header( request
, header
->field
, header
->value
, flags
, TRUE
);
417 free_header( header
);
426 /***********************************************************************
427 * WinHttpAddRequestHeaders (winhttp.@)
429 BOOL WINAPI
WinHttpAddRequestHeaders( HINTERNET hrequest
, LPCWSTR headers
, DWORD len
, DWORD flags
)
434 TRACE("%p, %s, 0x%x, 0x%08x\n", hrequest
, debugstr_w(headers
), len
, flags
);
438 set_last_error( ERROR_INVALID_PARAMETER
);
441 if (!(request
= (request_t
*)grab_object( hrequest
)))
443 set_last_error( ERROR_INVALID_HANDLE
);
446 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
448 release_object( &request
->hdr
);
449 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
453 ret
= add_request_headers( request
, headers
, len
, flags
);
455 release_object( &request
->hdr
);
459 static WCHAR
*build_request_string( request_t
*request
, LPCWSTR verb
, LPCWSTR path
, LPCWSTR version
)
461 static const WCHAR space
[] = {' ',0};
462 static const WCHAR crlf
[] = {'\r','\n',0};
463 static const WCHAR colon
[] = {':',' ',0};
464 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n',0};
467 const WCHAR
**headers
, **p
;
468 unsigned int len
, i
= 0, j
;
470 /* allocate space for an array of all the string pointers to be added */
471 len
= request
->num_headers
* 4 + 7;
472 if (!(headers
= heap_alloc( len
* sizeof(LPCWSTR
) ))) return NULL
;
475 headers
[i
++] = space
;
477 headers
[i
++] = space
;
478 headers
[i
++] = version
;
480 for (j
= 0; j
< request
->num_headers
; j
++)
482 if (request
->headers
[j
].is_request
)
485 headers
[i
++] = request
->headers
[j
].field
;
486 headers
[i
++] = colon
;
487 headers
[i
++] = request
->headers
[j
].value
;
489 TRACE("adding header %s (%s)\n", debugstr_w(request
->headers
[j
].field
),
490 debugstr_w(request
->headers
[j
].value
));
493 headers
[i
++] = twocrlf
;
497 for (p
= headers
; *p
; p
++) len
+= strlenW( *p
);
500 if (!(ret
= heap_alloc( len
* sizeof(WCHAR
) )))
502 heap_free( headers
);
506 for (p
= headers
; *p
; p
++) strcatW( ret
, *p
);
508 heap_free( headers
);
512 #define QUERY_MODIFIER_MASK (WINHTTP_QUERY_FLAG_REQUEST_HEADERS | WINHTTP_QUERY_FLAG_SYSTEMTIME | WINHTTP_QUERY_FLAG_NUMBER)
514 static BOOL
query_headers( request_t
*request
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
516 header_t
*header
= NULL
;
517 BOOL request_only
, ret
= FALSE
;
518 int requested_index
, header_index
= -1;
521 request_only
= level
& WINHTTP_QUERY_FLAG_REQUEST_HEADERS
;
522 requested_index
= index
? *index
: 0;
524 attr
= level
& ~QUERY_MODIFIER_MASK
;
527 case WINHTTP_QUERY_CUSTOM
:
529 header_index
= get_header_index( request
, name
, requested_index
, request_only
);
532 case WINHTTP_QUERY_RAW_HEADERS_CRLF
:
538 headers
= build_request_string( request
, request
->verb
, request
->path
, request
->version
);
540 headers
= request
->raw_headers
;
542 len
= strlenW( headers
) * sizeof(WCHAR
);
543 if (len
+ sizeof(WCHAR
) > *buflen
)
545 len
+= sizeof(WCHAR
);
546 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
550 memcpy( buffer
, headers
, len
+ sizeof(WCHAR
) );
551 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
/ sizeof(WCHAR
)));
555 if (request_only
) heap_free( headers
);
560 if (attr
> sizeof(attribute_table
)/sizeof(attribute_table
[0]) || !attribute_table
[attr
])
562 FIXME("attribute %u not implemented\n", attr
);
565 TRACE("attribute %s\n", debugstr_w(attribute_table
[attr
]));
566 header_index
= get_header_index( request
, attribute_table
[attr
], requested_index
, request_only
);
570 if (header_index
>= 0)
572 header
= &request
->headers
[header_index
];
574 if (!header
|| (request_only
&& !header
->is_request
))
576 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
579 if (index
) *index
+= 1;
580 if (level
& WINHTTP_QUERY_FLAG_NUMBER
)
582 int *number
= buffer
;
583 if (sizeof(int) > *buflen
)
585 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
589 *number
= atoiW( header
->value
);
590 TRACE("returning number: %d\n", *number
);
593 *buflen
= sizeof(int);
595 else if (level
& WINHTTP_QUERY_FLAG_SYSTEMTIME
)
597 SYSTEMTIME
*st
= buffer
;
598 if (sizeof(SYSTEMTIME
) > *buflen
)
600 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
602 else if (st
&& (ret
= WinHttpTimeToSystemTime( header
->value
, st
)))
604 TRACE("returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
605 st
->wYear
, st
->wMonth
, st
->wDay
, st
->wDayOfWeek
,
606 st
->wHour
, st
->wMinute
, st
->wSecond
, st
->wMilliseconds
);
608 *buflen
= sizeof(SYSTEMTIME
);
610 else if (header
->value
)
612 WCHAR
*string
= buffer
;
613 DWORD len
= (strlenW( header
->value
) + 1) * sizeof(WCHAR
);
616 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
622 strcpyW( string
, header
->value
);
623 TRACE("returning string: %s\n", debugstr_w(string
));
626 *buflen
= len
- sizeof(WCHAR
);
631 /***********************************************************************
632 * WinHttpQueryHeaders (winhttp.@)
634 BOOL WINAPI
WinHttpQueryHeaders( HINTERNET hrequest
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
639 TRACE("%p, 0x%08x, %s, %p, %p, %p\n", hrequest
, level
, debugstr_w(name
), buffer
, buflen
, index
);
641 if (!(request
= (request_t
*)grab_object( hrequest
)))
643 set_last_error( ERROR_INVALID_HANDLE
);
646 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
648 release_object( &request
->hdr
);
649 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
653 ret
= query_headers( request
, level
, name
, buffer
, buflen
, index
);
655 release_object( &request
->hdr
);
659 static BOOL
open_connection( request_t
*request
)
665 if (netconn_connected( &request
->netconn
)) return TRUE
;
667 connect
= request
->connect
;
668 if (!netconn_resolve( connect
->servername
, connect
->serverport
, &connect
->sockaddr
)) return FALSE
;
670 inet_ntop( connect
->sockaddr
.sin_family
, &connect
->sockaddr
.sin_addr
, address
, sizeof(address
) );
671 TRACE("connecting to %s:%u\n", address
, ntohs(connect
->sockaddr
.sin_port
));
672 addressW
= strdupAW( address
);
674 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
, addressW
, 0 );
676 if (!netconn_create( &request
->netconn
, connect
->sockaddr
.sin_family
, SOCK_STREAM
, 0 ))
678 heap_free( addressW
);
681 if (!netconn_connect( &request
->netconn
, (struct sockaddr
*)&connect
->sockaddr
, sizeof(struct sockaddr_in
) ))
683 netconn_close( &request
->netconn
);
684 heap_free( addressW
);
688 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
, addressW
, 0 );
690 heap_free( addressW
);
694 void close_connection( request_t
*request
)
696 if (!netconn_connected( &request
->netconn
)) return;
698 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
, 0, 0 );
699 netconn_close( &request
->netconn
);
700 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
, 0, 0 );
703 static BOOL
send_request( request_t
*request
, LPCWSTR headers
, DWORD headers_len
, LPVOID optional
,
704 DWORD optional_len
, DWORD total_len
, DWORD_PTR context
)
706 static const WCHAR keep_alive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
707 static const WCHAR no_cache
[] = {'n','o','-','c','a','c','h','e',0};
708 static const WCHAR length_fmt
[] = {'%','l','d',0};
711 connect_t
*connect
= request
->connect
;
712 session_t
*session
= connect
->session
;
719 process_header( request
, attr_user_agent
, session
->agent
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
721 if (connect
->hostname
)
722 process_header( request
, attr_host
, connect
->hostname
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
726 WCHAR length
[21]; /* decimal long int + null */
727 sprintfW( length
, length_fmt
, optional_len
);
728 process_header( request
, attr_content_length
, length
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
730 if (!(request
->hdr
.flags
& WINHTTP_DISABLE_KEEP_ALIVE
))
732 process_header( request
, attr_connection
, keep_alive
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
734 if (request
->hdr
.flags
& WINHTTP_FLAG_REFRESH
)
736 process_header( request
, attr_pragma
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
737 process_header( request
, attr_cache_control
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
739 if (headers
&& !add_request_headers( request
, headers
, headers_len
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))
741 TRACE("failed to add request headers\n");
745 if (!(ret
= open_connection( request
))) goto end
;
746 if (!(req
= build_request_string( request
, request
->verb
, request
->path
, request
->version
))) goto end
;
748 if (!(req_ascii
= strdupWA( req
))) goto end
;
749 TRACE("full request: %s\n", debugstr_a(req_ascii
));
750 len
= strlen(req_ascii
);
752 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
, NULL
, 0 );
754 ret
= netconn_send( &request
->netconn
, req_ascii
, len
, 0, &bytes_sent
);
755 heap_free( req_ascii
);
758 if (optional_len
&& !netconn_send( &request
->netconn
, optional
, optional_len
, 0, &bytes_sent
)) goto end
;
761 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_SENT
, &len
, sizeof(DWORD
) );
768 /***********************************************************************
769 * WinHttpSendRequest (winhttp.@)
771 BOOL WINAPI
WinHttpSendRequest( HINTERNET hrequest
, LPCWSTR headers
, DWORD headers_len
,
772 LPVOID optional
, DWORD optional_len
, DWORD total_len
, DWORD_PTR context
)
777 TRACE("%p, %s, 0x%x, %u, %u, %lx\n",
778 hrequest
, debugstr_w(headers
), headers_len
, optional_len
, total_len
, context
);
780 if (!(request
= (request_t
*)grab_object( hrequest
)))
782 set_last_error( ERROR_INVALID_HANDLE
);
785 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
787 release_object( &request
->hdr
);
788 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
792 ret
= send_request( request
, headers
, headers_len
, optional
, optional_len
, total_len
, context
);
794 release_object( &request
->hdr
);
798 static void clear_response_headers( request_t
*request
)
802 for (i
= 0; i
< request
->num_headers
; i
++)
804 if (!request
->headers
[i
].field
) continue;
805 if (!request
->headers
[i
].value
) continue;
806 if (request
->headers
[i
].is_request
) continue;
807 delete_header( request
, i
);
812 #define MAX_REPLY_LEN 1460
813 #define INITIAL_HEADER_BUFFER_SIZE 512
815 static BOOL
receive_response( request_t
*request
, BOOL clear
)
817 static const WCHAR crlf
[] = {'\r','\n',0};
819 char buffer
[MAX_REPLY_LEN
];
820 DWORD buflen
, len
, offset
, received_len
, crlf_len
= 2; /* strlenW(crlf) */
821 char *status_code
, *status_text
;
822 WCHAR
*versionW
, *status_textW
, *raw_headers
;
823 WCHAR status_codeW
[4]; /* sizeof("nnn") */
825 if (!netconn_connected( &request
->netconn
)) return FALSE
;
827 /* clear old response headers (eg. from a redirect response) */
828 if (clear
) clear_response_headers( request
);
830 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE
, NULL
, 0 );
835 buflen
= MAX_REPLY_LEN
;
836 if (!netconn_get_next_line( &request
->netconn
, buffer
, &buflen
)) return FALSE
;
837 received_len
+= buflen
;
839 /* first line should look like 'HTTP/1.x nnn OK' where nnn is the status code */
840 if (!(status_code
= strchr( buffer
, ' ' ))) return FALSE
;
842 if (!(status_text
= strchr( status_code
, ' ' ))) return FALSE
;
843 if ((len
= status_text
- status_code
) != sizeof("nnn") - 1) return FALSE
;
846 TRACE("version [%s] status code [%s] status text [%s]\n",
847 debugstr_an(buffer
, status_code
- buffer
- 1),
848 debugstr_an(status_code
, len
),
849 debugstr_a(status_text
));
851 } while (!memcmp( status_code
, "100", len
)); /* ignore "100 Continue" responses */
853 /* we rely on the fact that the protocol is ascii */
854 MultiByteToWideChar( CP_ACP
, 0, status_code
, len
, status_codeW
, len
);
855 status_codeW
[len
] = 0;
856 if (!(process_header( request
, attr_status
, status_codeW
, WINHTTP_ADDREQ_FLAG_REPLACE
, FALSE
))) return FALSE
;
858 len
= status_code
- buffer
;
859 if (!(versionW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
860 MultiByteToWideChar( CP_ACP
, 0, buffer
, len
- 1, versionW
, len
-1 );
861 versionW
[len
- 1] = 0;
863 heap_free( request
->version
);
864 request
->version
= versionW
;
866 len
= buflen
- (status_text
- buffer
);
867 if (!(status_textW
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
868 MultiByteToWideChar( CP_ACP
, 0, status_text
, len
, status_textW
, len
);
870 heap_free( request
->status_text
);
871 request
->status_text
= status_textW
;
873 len
= max( buflen
+ crlf_len
, INITIAL_HEADER_BUFFER_SIZE
);
874 if (!(raw_headers
= heap_alloc( len
* sizeof(WCHAR
) ))) return FALSE
;
875 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
, buflen
);
876 memcpy( raw_headers
+ buflen
- 1, crlf
, sizeof(crlf
) );
878 heap_free( request
->raw_headers
);
879 request
->raw_headers
= raw_headers
;
881 offset
= buflen
+ crlf_len
- 1;
886 buflen
= MAX_REPLY_LEN
;
887 if (!netconn_get_next_line( &request
->netconn
, buffer
, &buflen
)) goto end
;
888 received_len
+= buflen
;
891 while (len
- offset
< buflen
+ crlf_len
)
895 if (!(tmp
= heap_realloc( raw_headers
, len
* sizeof(WCHAR
) ))) return FALSE
;
896 request
->raw_headers
= raw_headers
= tmp
;
898 MultiByteToWideChar( CP_ACP
, 0, buffer
, buflen
, raw_headers
+ offset
, buflen
);
900 if (!(header
= parse_header( raw_headers
+ offset
))) break;
901 if (!(process_header( request
, header
->field
, header
->value
, WINHTTP_ADDREQ_FLAG_ADD
, FALSE
)))
903 free_header( header
);
906 free_header( header
);
907 memcpy( raw_headers
+ offset
+ buflen
- 1, crlf
, sizeof(crlf
) );
908 offset
+= buflen
+ crlf_len
- 1;
911 TRACE("raw headers: %s\n", debugstr_w(raw_headers
));
914 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED
, &received_len
, sizeof(DWORD
) );
918 /***********************************************************************
919 * WinHttpReceiveResponse (winhttp.@)
921 BOOL WINAPI
WinHttpReceiveResponse( HINTERNET hrequest
, LPVOID reserved
)
927 TRACE("%p, %p\n", hrequest
, reserved
);
929 if (!(request
= (request_t
*)grab_object( hrequest
)))
931 set_last_error( ERROR_INVALID_HANDLE
);
934 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
936 release_object( &request
->hdr
);
937 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
941 ret
= receive_response( request
, TRUE
);
943 size
= sizeof(DWORD
);
944 query
= WINHTTP_QUERY_CONTENT_LENGTH
| WINHTTP_QUERY_FLAG_NUMBER
;
945 if (!query_headers( request
, query
, NULL
, &request
->content_length
, &size
, NULL
))
946 request
->content_length
= ~0UL;
948 release_object( &request
->hdr
);