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 TRACE("inserting %s: %s\n", debugstr_w(header
->field
), debugstr_w(header
->value
));
283 count
= request
->num_headers
+ 1;
285 hdrs
= heap_realloc_zero( request
->headers
, sizeof(header_t
) * count
);
287 hdrs
= heap_alloc_zero( sizeof(header_t
) * count
);
291 request
->headers
= hdrs
;
292 request
->headers
[count
- 1].field
= strdupW( header
->field
);
293 request
->headers
[count
- 1].value
= strdupW( header
->value
);
294 request
->headers
[count
- 1].is_request
= header
->is_request
;
295 request
->num_headers
++;
301 static BOOL
delete_header( request_t
*request
, DWORD index
)
303 if (!request
->num_headers
) return FALSE
;
304 if (index
>= request
->num_headers
) return FALSE
;
305 request
->num_headers
--;
307 heap_free( request
->headers
[index
].field
);
308 heap_free( request
->headers
[index
].value
);
310 memmove( &request
->headers
[index
], &request
->headers
[index
+ 1], (request
->num_headers
- index
) * sizeof(header_t
) );
311 memset( &request
->headers
[request
->num_headers
], 0, sizeof(header_t
) );
315 static BOOL
process_header( request_t
*request
, LPCWSTR field
, LPCWSTR value
, DWORD flags
, BOOL request_only
)
320 TRACE("%s: %s 0x%08x\n", debugstr_w(field
), debugstr_w(value
), flags
);
322 /* replace wins out over add */
323 if (flags
& WINHTTP_ADDREQ_FLAG_REPLACE
) flags
&= ~WINHTTP_ADDREQ_FLAG_ADD
;
325 if (flags
& WINHTTP_ADDREQ_FLAG_ADD
) index
= -1;
327 index
= get_header_index( request
, field
, 0, request_only
);
331 if (flags
& WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
) return FALSE
;
332 header
= &request
->headers
[index
];
338 hdr
.field
= (LPWSTR
)field
;
339 hdr
.value
= (LPWSTR
)value
;
340 hdr
.is_request
= request_only
;
342 return insert_header( request
, &hdr
);
344 /* no value to delete */
347 if (flags
& WINHTTP_ADDREQ_FLAG_REPLACE
)
349 delete_header( request
, index
);
354 hdr
.field
= (LPWSTR
)field
;
355 hdr
.value
= (LPWSTR
)value
;
356 hdr
.is_request
= request_only
;
358 return insert_header( request
, &hdr
);
362 else if (flags
& (WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
| WINHTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON
))
365 int len
, orig_len
, value_len
;
367 orig_len
= strlenW( header
->value
);
368 value_len
= strlenW( value
);
370 if (flags
& WINHTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA
) sep
= ',';
373 len
= orig_len
+ value_len
+ 2;
374 if ((tmp
= heap_realloc( header
->value
, (len
+ 1) * sizeof(WCHAR
) )))
378 header
->value
[orig_len
] = sep
;
380 header
->value
[orig_len
] = ' ';
383 memcpy( &header
->value
[orig_len
], value
, value_len
* sizeof(WCHAR
) );
384 header
->value
[len
] = 0;
391 static BOOL
add_request_headers( request_t
*request
, LPCWSTR headers
, DWORD len
, DWORD flags
)
394 WCHAR
*buffer
, *p
, *q
;
397 if (len
== ~0UL) len
= strlenW( headers
);
398 if (!(buffer
= heap_alloc( (len
+ 1) * sizeof(WCHAR
) ))) return FALSE
;
399 strcpyW( buffer
, headers
);
407 if (q
[0] == '\r' && q
[1] == '\n') break;
414 q
+= 2; /* jump over \r\n */
416 if ((header
= parse_header( p
)))
418 ret
= process_header( request
, header
->field
, header
->value
, flags
, TRUE
);
419 free_header( header
);
428 /***********************************************************************
429 * WinHttpAddRequestHeaders (winhttp.@)
431 BOOL WINAPI
WinHttpAddRequestHeaders( HINTERNET hrequest
, LPCWSTR headers
, DWORD len
, DWORD flags
)
436 TRACE("%p, %s, 0x%x, 0x%08x\n", hrequest
, debugstr_w(headers
), len
, flags
);
440 set_last_error( ERROR_INVALID_PARAMETER
);
443 if (!(request
= (request_t
*)grab_object( hrequest
)))
445 set_last_error( ERROR_INVALID_HANDLE
);
448 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
450 release_object( &request
->hdr
);
451 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
455 ret
= add_request_headers( request
, headers
, len
, flags
);
457 release_object( &request
->hdr
);
461 static WCHAR
*build_request_string( request_t
*request
, LPCWSTR verb
, LPCWSTR path
, LPCWSTR version
)
463 static const WCHAR space
[] = {' ',0};
464 static const WCHAR crlf
[] = {'\r','\n',0};
465 static const WCHAR colon
[] = {':',' ',0};
466 static const WCHAR twocrlf
[] = {'\r','\n','\r','\n',0};
469 const WCHAR
**headers
, **p
;
470 unsigned int len
, i
= 0, j
;
472 /* allocate space for an array of all the string pointers to be added */
473 len
= request
->num_headers
* 4 + 7;
474 if (!(headers
= heap_alloc( len
* sizeof(LPCWSTR
) ))) return NULL
;
477 headers
[i
++] = space
;
479 headers
[i
++] = space
;
480 headers
[i
++] = version
;
482 for (j
= 0; j
< request
->num_headers
; j
++)
484 if (request
->headers
[j
].is_request
)
487 headers
[i
++] = request
->headers
[j
].field
;
488 headers
[i
++] = colon
;
489 headers
[i
++] = request
->headers
[j
].value
;
491 TRACE("adding header %s (%s)\n", debugstr_w(request
->headers
[j
].field
),
492 debugstr_w(request
->headers
[j
].value
));
495 headers
[i
++] = twocrlf
;
499 for (p
= headers
; *p
; p
++) len
+= strlenW( *p
);
502 if (!(ret
= heap_alloc( len
* sizeof(WCHAR
) )))
504 heap_free( headers
);
508 for (p
= headers
; *p
; p
++) strcatW( ret
, *p
);
510 heap_free( headers
);
514 #define QUERY_MODIFIER_MASK (WINHTTP_QUERY_FLAG_REQUEST_HEADERS | WINHTTP_QUERY_FLAG_SYSTEMTIME | WINHTTP_QUERY_FLAG_NUMBER)
516 static BOOL
query_headers( request_t
*request
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
518 header_t
*header
= NULL
;
519 BOOL request_only
, ret
= FALSE
;
520 int requested_index
, header_index
= -1;
523 request_only
= level
& WINHTTP_QUERY_FLAG_REQUEST_HEADERS
;
524 requested_index
= index
? *index
: 0;
526 attr
= level
& ~QUERY_MODIFIER_MASK
;
529 case WINHTTP_QUERY_CUSTOM
:
531 header_index
= get_header_index( request
, name
, requested_index
, request_only
);
534 case WINHTTP_QUERY_RAW_HEADERS_CRLF
:
540 headers
= build_request_string( request
, request
->verb
, request
->path
, request
->version
);
542 headers
= request
->raw_headers
;
544 len
= strlenW( headers
) * sizeof(WCHAR
);
545 if (len
+ sizeof(WCHAR
) > *buflen
)
547 len
+= sizeof(WCHAR
);
548 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
552 memcpy( buffer
, headers
, len
+ sizeof(WCHAR
) );
553 TRACE("returning data: %s\n", debugstr_wn(buffer
, len
/ sizeof(WCHAR
)));
557 if (request_only
) heap_free( headers
);
562 if (attr
> sizeof(attribute_table
)/sizeof(attribute_table
[0]) || !attribute_table
[attr
])
564 FIXME("attribute %u not implemented\n", attr
);
567 TRACE("attribute %s\n", debugstr_w(attribute_table
[attr
]));
568 header_index
= get_header_index( request
, attribute_table
[attr
], requested_index
, request_only
);
572 if (header_index
>= 0)
574 header
= &request
->headers
[header_index
];
576 if (!header
|| (request_only
&& !header
->is_request
))
578 set_last_error( ERROR_WINHTTP_HEADER_NOT_FOUND
);
581 if (index
) *index
+= 1;
582 if (level
& WINHTTP_QUERY_FLAG_NUMBER
)
584 int *number
= buffer
;
585 if (sizeof(int) > *buflen
)
587 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
591 *number
= atoiW( header
->value
);
592 TRACE("returning number: %d\n", *number
);
595 *buflen
= sizeof(int);
597 else if (level
& WINHTTP_QUERY_FLAG_SYSTEMTIME
)
599 SYSTEMTIME
*st
= buffer
;
600 if (sizeof(SYSTEMTIME
) > *buflen
)
602 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
604 else if (st
&& (ret
= WinHttpTimeToSystemTime( header
->value
, st
)))
606 TRACE("returning time: %04d/%02d/%02d - %d - %02d:%02d:%02d.%02d\n",
607 st
->wYear
, st
->wMonth
, st
->wDay
, st
->wDayOfWeek
,
608 st
->wHour
, st
->wMinute
, st
->wSecond
, st
->wMilliseconds
);
610 *buflen
= sizeof(SYSTEMTIME
);
612 else if (header
->value
)
614 WCHAR
*string
= buffer
;
615 DWORD len
= (strlenW( header
->value
) + 1) * sizeof(WCHAR
);
618 set_last_error( ERROR_INSUFFICIENT_BUFFER
);
624 strcpyW( string
, header
->value
);
625 TRACE("returning string: %s\n", debugstr_w(string
));
628 *buflen
= len
- sizeof(WCHAR
);
633 /***********************************************************************
634 * WinHttpQueryHeaders (winhttp.@)
636 BOOL WINAPI
WinHttpQueryHeaders( HINTERNET hrequest
, DWORD level
, LPCWSTR name
, LPVOID buffer
, LPDWORD buflen
, LPDWORD index
)
641 TRACE("%p, 0x%08x, %s, %p, %p, %p\n", hrequest
, level
, debugstr_w(name
), buffer
, buflen
, index
);
643 if (!(request
= (request_t
*)grab_object( hrequest
)))
645 set_last_error( ERROR_INVALID_HANDLE
);
648 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
650 release_object( &request
->hdr
);
651 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
655 ret
= query_headers( request
, level
, name
, buffer
, buflen
, index
);
657 release_object( &request
->hdr
);
661 static BOOL
open_connection( request_t
*request
)
667 if (netconn_connected( &request
->netconn
)) return TRUE
;
669 connect
= request
->connect
;
670 if (!netconn_resolve( connect
->servername
, connect
->serverport
, &connect
->sockaddr
)) return FALSE
;
672 inet_ntop( connect
->sockaddr
.sin_family
, &connect
->sockaddr
.sin_addr
, address
, sizeof(address
) );
673 TRACE("connecting to %s:%u\n", address
, ntohs(connect
->sockaddr
.sin_port
));
674 addressW
= strdupAW( address
);
676 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER
, addressW
, 0 );
678 if (!netconn_create( &request
->netconn
, connect
->sockaddr
.sin_family
, SOCK_STREAM
, 0 ))
680 heap_free( addressW
);
683 if (!netconn_connect( &request
->netconn
, (struct sockaddr
*)&connect
->sockaddr
, sizeof(struct sockaddr_in
) ))
685 netconn_close( &request
->netconn
);
686 heap_free( addressW
);
690 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER
, addressW
, 0 );
692 heap_free( addressW
);
696 void close_connection( request_t
*request
)
698 if (!netconn_connected( &request
->netconn
)) return;
700 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION
, 0, 0 );
701 netconn_close( &request
->netconn
);
702 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED
, 0, 0 );
705 static BOOL
send_request( request_t
*request
, LPCWSTR headers
, DWORD headers_len
, LPVOID optional
,
706 DWORD optional_len
, DWORD total_len
, DWORD_PTR context
)
708 static const WCHAR keep_alive
[] = {'K','e','e','p','-','A','l','i','v','e',0};
709 static const WCHAR no_cache
[] = {'n','o','-','c','a','c','h','e',0};
710 static const WCHAR length_fmt
[] = {'%','l','d',0};
713 connect_t
*connect
= request
->connect
;
714 session_t
*session
= connect
->session
;
721 process_header( request
, attr_user_agent
, session
->agent
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
723 if (connect
->hostname
)
724 process_header( request
, attr_host
, connect
->hostname
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
728 WCHAR length
[21]; /* decimal long int + null */
729 sprintfW( length
, length_fmt
, optional_len
);
730 process_header( request
, attr_content_length
, length
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
732 if (!(request
->hdr
.flags
& WINHTTP_DISABLE_KEEP_ALIVE
))
734 process_header( request
, attr_connection
, keep_alive
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
736 if (request
->hdr
.flags
& WINHTTP_FLAG_REFRESH
)
738 process_header( request
, attr_pragma
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
739 process_header( request
, attr_cache_control
, no_cache
, WINHTTP_ADDREQ_FLAG_ADD_IF_NEW
, TRUE
);
741 if (headers
&& !add_request_headers( request
, headers
, headers_len
, WINHTTP_ADDREQ_FLAG_ADD
| WINHTTP_ADDREQ_FLAG_REPLACE
))
743 TRACE("failed to add request headers\n");
747 if (!(ret
= open_connection( request
))) goto end
;
748 if (!(req
= build_request_string( request
, request
->verb
, request
->path
, request
->version
))) goto end
;
750 if (!(req_ascii
= strdupWA( req
))) goto end
;
751 TRACE("full request: %s\n", debugstr_a(req_ascii
));
752 len
= strlen(req_ascii
);
754 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST
, NULL
, 0 );
756 ret
= netconn_send( &request
->netconn
, req_ascii
, len
, 0, &bytes_sent
);
757 heap_free( req_ascii
);
760 if (optional_len
&& !netconn_send( &request
->netconn
, optional
, optional_len
, 0, &bytes_sent
)) goto end
;
763 send_callback( &request
->hdr
, WINHTTP_CALLBACK_STATUS_REQUEST_SENT
, &len
, sizeof(DWORD
) );
770 /***********************************************************************
771 * WinHttpSendRequest (winhttp.@)
773 BOOL WINAPI
WinHttpSendRequest( HINTERNET hrequest
, LPCWSTR headers
, DWORD headers_len
,
774 LPVOID optional
, DWORD optional_len
, DWORD total_len
, DWORD_PTR context
)
779 TRACE("%p, %s, 0x%x, %u, %u, %lx\n",
780 hrequest
, debugstr_w(headers
), headers_len
, optional_len
, total_len
, context
);
782 if (!(request
= (request_t
*)grab_object( hrequest
)))
784 set_last_error( ERROR_INVALID_HANDLE
);
787 if (request
->hdr
.type
!= WINHTTP_HANDLE_TYPE_REQUEST
)
789 release_object( &request
->hdr
);
790 set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_TYPE
);
794 ret
= send_request( request
, headers
, headers_len
, optional
, optional_len
, total_len
, context
);
796 release_object( &request
->hdr
);