4 * Copyright 2002 Aric Stewart
5 * Copyright 2004 Mike McCormack
6 * Copyright 2005 Hans Leidekker
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32 #include "wine/test.h"
34 #define TEST_URL "http://test.winehq.org/tests/hello.html"
36 static BOOL first_connection_to_test_url = TRUE;
38 /* Adapted from dlls/urlmon/tests/protocol.c */
40 #define SET_EXPECT2(status, num) \
43 #define SET_EXPECT(status) \
44 SET_EXPECT2(status, 1)
46 #define SET_OPTIONAL2(status, num) \
47 optional[status] = num
49 #define SET_OPTIONAL(status) \
50 SET_OPTIONAL2(status, 1)
52 /* SET_WINE_ALLOW's should be used with an appropriate
53 * todo_wine CHECK_NOTIFIED at a later point in the code */
54 #define SET_WINE_ALLOW2(status, num) \
55 wine_allow[status] = num
57 #define SET_WINE_ALLOW(status) \
58 SET_WINE_ALLOW2(status, 1)
60 #define CHECK_EXPECT(status) \
62 if (!expect[status] && !optional[status] && wine_allow[status]) \
64 todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
65 status < MAX_INTERNET_STATUS && status_string[status] ? \
66 status_string[status] : "unknown"); \
67 wine_allow[status]--; \
71 ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
72 status < MAX_INTERNET_STATUS && status_string[status] ? \
73 status_string[status] : "unknown"); \
74 if (expect[status]) expect[status]--; \
75 else optional[status]--; \
80 /* CLEAR_NOTIFIED used in cases when notification behavior
81 * differs between Windows versions */
82 #define CLEAR_NOTIFIED(status) \
83 expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
85 #define CHECK_NOTIFIED2(status, num) \
87 ok(notified[status] + optional[status] == (num), \
88 "expected status %d (%s) %d times, received %d times\n", \
89 status, status < MAX_INTERNET_STATUS && status_string[status] ? \
90 status_string[status] : "unknown", (num), notified[status]); \
91 CLEAR_NOTIFIED(status); \
94 #define CHECK_NOTIFIED(status) \
95 CHECK_NOTIFIED2(status, 1)
97 #define CHECK_NOT_NOTIFIED(status) \
98 CHECK_NOTIFIED2(status, 0)
100 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
101 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
102 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
103 static const char *status_string[MAX_INTERNET_STATUS];
105 static HANDLE hCompleteEvent;
107 #define TESTF_REDIRECT 0x01
108 #define TESTF_COMPRESSED 0x02
109 #define TESTF_ALLOW_COOKIE 0x04
110 #define TESTF_CHUNKED 0x08
114 const char *redirected_url;
119 const char *post_data;
123 static const test_data_t test_data[] = {
125 "http://test.winehq.org/tests/data.php",
126 "http://test.winehq.org/tests/data.php",
133 "http://test.winehq.org/tests/redirect",
134 "http://test.winehq.org/tests/hello.html",
141 "http://www.codeweavers.com/",
142 "http://www.codeweavers.com/",
143 "www.codeweavers.com",
145 "Accept-Encoding: gzip, deflate",
146 TESTF_COMPRESSED|TESTF_ALLOW_COOKIE
149 "http://crossover.codeweavers.com/posttest.php",
150 "http://crossover.codeweavers.com/posttest.php",
151 "crossover.codeweavers.com",
153 "Content-Type: application/x-www-form-urlencoded",
160 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
162 static BOOL proxy_active(void)
164 HKEY internet_settings;
168 if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
169 0, KEY_QUERY_VALUE, &internet_settings) != ERROR_SUCCESS)
172 size = sizeof(DWORD);
173 if (RegQueryValueExA(internet_settings, "ProxyEnable", NULL, NULL, (LPBYTE) &proxy_enable, &size) != ERROR_SUCCESS)
176 RegCloseKey(internet_settings);
178 return proxy_enable != 0;
181 static VOID WINAPI callback(
184 DWORD dwInternetStatus,
185 LPVOID lpvStatusInformation,
186 DWORD dwStatusInformationLength
189 CHECK_EXPECT(dwInternetStatus);
190 switch (dwInternetStatus)
192 case INTERNET_STATUS_RESOLVING_NAME:
193 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
194 GetCurrentThreadId(), hInternet, dwContext,
195 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
196 *(LPSTR)lpvStatusInformation = '\0';
198 case INTERNET_STATUS_NAME_RESOLVED:
199 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
200 GetCurrentThreadId(), hInternet, dwContext,
201 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
202 *(LPSTR)lpvStatusInformation = '\0';
204 case INTERNET_STATUS_CONNECTING_TO_SERVER:
205 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
206 GetCurrentThreadId(), hInternet, dwContext,
207 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
208 *(LPSTR)lpvStatusInformation = '\0';
210 case INTERNET_STATUS_CONNECTED_TO_SERVER:
211 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
212 GetCurrentThreadId(), hInternet, dwContext,
213 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
214 *(LPSTR)lpvStatusInformation = '\0';
216 case INTERNET_STATUS_SENDING_REQUEST:
217 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
218 GetCurrentThreadId(), hInternet, dwContext,
219 lpvStatusInformation,dwStatusInformationLength);
221 case INTERNET_STATUS_REQUEST_SENT:
222 ok(dwStatusInformationLength == sizeof(DWORD),
223 "info length should be sizeof(DWORD) instead of %d\n",
224 dwStatusInformationLength);
225 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
226 GetCurrentThreadId(), hInternet, dwContext,
227 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
229 case INTERNET_STATUS_RECEIVING_RESPONSE:
230 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
231 GetCurrentThreadId(), hInternet, dwContext,
232 lpvStatusInformation,dwStatusInformationLength);
234 case INTERNET_STATUS_RESPONSE_RECEIVED:
235 ok(dwStatusInformationLength == sizeof(DWORD),
236 "info length should be sizeof(DWORD) instead of %d\n",
237 dwStatusInformationLength);
238 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
239 GetCurrentThreadId(), hInternet, dwContext,
240 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
242 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
243 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
244 GetCurrentThreadId(), hInternet,dwContext,
245 lpvStatusInformation,dwStatusInformationLength);
247 case INTERNET_STATUS_PREFETCH:
248 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
249 GetCurrentThreadId(), hInternet, dwContext,
250 lpvStatusInformation,dwStatusInformationLength);
252 case INTERNET_STATUS_CLOSING_CONNECTION:
253 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
254 GetCurrentThreadId(), hInternet, dwContext,
255 lpvStatusInformation,dwStatusInformationLength);
257 case INTERNET_STATUS_CONNECTION_CLOSED:
258 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
259 GetCurrentThreadId(), hInternet, dwContext,
260 lpvStatusInformation,dwStatusInformationLength);
262 case INTERNET_STATUS_HANDLE_CREATED:
263 ok(dwStatusInformationLength == sizeof(HINTERNET),
264 "info length should be sizeof(HINTERNET) instead of %d\n",
265 dwStatusInformationLength);
266 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
267 GetCurrentThreadId(), hInternet, dwContext,
268 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
269 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
270 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
272 case INTERNET_STATUS_HANDLE_CLOSING:
273 ok(dwStatusInformationLength == sizeof(HINTERNET),
274 "info length should be sizeof(HINTERNET) instead of %d\n",
275 dwStatusInformationLength);
276 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
277 GetCurrentThreadId(), hInternet, dwContext,
278 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
280 case INTERNET_STATUS_REQUEST_COMPLETE:
282 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
283 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
284 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
285 dwStatusInformationLength);
286 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
287 GetCurrentThreadId(), hInternet, dwContext,
288 iar->dwResult,iar->dwError,dwStatusInformationLength);
289 SetEvent(hCompleteEvent);
292 case INTERNET_STATUS_REDIRECT:
293 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
294 GetCurrentThreadId(), hInternet, dwContext,
295 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
296 *(LPSTR)lpvStatusInformation = '\0';
297 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
298 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
300 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
301 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
302 GetCurrentThreadId(), hInternet, dwContext,
303 lpvStatusInformation, dwStatusInformationLength);
306 trace("%04x:Callback %p 0x%lx %d %p %d\n",
307 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
308 lpvStatusInformation, dwStatusInformationLength);
312 static void InternetReadFile_test(int flags, const test_data_t *test)
314 char *post_data = NULL;
315 BOOL res, on_async = TRUE;
317 DWORD length, post_len = 0;
319 const char *types[2] = { "*", NULL };
320 HINTERNET hi, hic = 0, hor = 0;
322 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
324 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
326 trace("InternetOpenA <--\n");
327 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
328 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
329 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
330 trace("InternetOpenA -->\n");
332 if (hi == 0x0) goto abort;
334 pInternetSetStatusCallbackA(hi,&callback);
336 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
338 trace("InternetConnectA <--\n");
339 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
340 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
341 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
342 trace("InternetConnectA -->\n");
344 if (hic == 0x0) goto abort;
346 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
347 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
349 trace("HttpOpenRequestA <--\n");
350 hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
351 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
353 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
355 * If the internet name can't be resolved we are probably behind
356 * a firewall or in some other way not directly connected to the
357 * Internet. Not enough reason to fail the test. Just ignore and
361 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
363 trace("HttpOpenRequestA -->\n");
365 if (hor == 0x0) goto abort;
367 length = sizeof(buffer);
368 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
369 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
370 ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
372 length = sizeof(buffer);
373 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
374 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
375 ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
376 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
378 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
379 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
380 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
381 if(test->flags & TESTF_ALLOW_COOKIE) {
382 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
383 SET_OPTIONAL(INTERNET_STATUS_COOKIE_RECEIVED);
385 if (first_connection_to_test_url)
387 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
388 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
389 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
390 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
394 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
395 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
397 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
398 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
399 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
400 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
401 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
402 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
403 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
404 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
405 if(test->flags & TESTF_REDIRECT) {
406 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
407 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
409 SET_EXPECT(INTERNET_STATUS_REDIRECT);
410 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
411 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
412 if (flags & INTERNET_FLAG_ASYNC)
413 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
415 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
417 if(test->flags & TESTF_COMPRESSED) {
420 res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
421 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
422 "InternetSetOption failed: %u\n", GetLastError());
427 trace("HttpSendRequestA -->\n");
428 if(test->post_data) {
429 post_len = strlen(test->post_data);
430 post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
431 memcpy(post_data, test->post_data, post_len);
433 SetLastError(0xdeadbeef);
434 res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
435 if (flags & INTERNET_FLAG_ASYNC)
436 ok(!res && (GetLastError() == ERROR_IO_PENDING),
437 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
439 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
440 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
441 trace("HttpSendRequestA <--\n");
443 if (flags & INTERNET_FLAG_ASYNC)
444 WaitForSingleObject(hCompleteEvent, INFINITE);
445 HeapFree(GetProcessHeap(), 0, post_data);
447 if(test->flags & TESTF_ALLOW_COOKIE) {
448 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
449 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
451 if (first_connection_to_test_url)
453 if (! proxy_active())
455 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
456 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
460 CLEAR_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
461 CLEAR_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
466 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
467 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
469 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
470 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
471 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
472 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
473 if(test->flags & TESTF_REDIRECT)
474 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
475 if (flags & INTERNET_FLAG_ASYNC)
476 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
478 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
479 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
480 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
481 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
484 res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
485 ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
488 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
489 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
491 length = sizeof(buffer);
492 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
493 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
496 length = sizeof(buffer);
497 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
498 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
499 ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
502 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
503 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
504 if(test->flags & TESTF_COMPRESSED)
505 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
506 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
509 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
511 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
514 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
516 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
518 SetLastError(0xdeadbeef);
519 res = InternetReadFile(NULL, buffer, 100, &length);
520 ok(!res, "InternetReadFile should have failed\n");
521 ok(GetLastError() == ERROR_INVALID_HANDLE,
522 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
526 trace("Entering Query loop\n");
530 if (flags & INTERNET_FLAG_ASYNC)
531 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
532 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
533 if (flags & INTERNET_FLAG_ASYNC)
537 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
539 else if (GetLastError() == ERROR_IO_PENDING)
542 /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
543 if(!(test->flags & TESTF_CHUNKED))
544 ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
545 WaitForSingleObject(hCompleteEvent, INFINITE);
546 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
549 ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
552 ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
554 trace("LENGTH %d\n", length);
555 if(test->flags & TESTF_CHUNKED)
556 ok(length <= 8192, "length = %d, expected <= 8192\n", length);
560 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
562 res = InternetReadFile(hor,buffer,length,&length);
566 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
569 ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
570 HeapFree(GetProcessHeap(),0,buffer);
572 ok(!on_async, "Returned zero size in response to request complete\n");
577 if(test->flags & TESTF_REDIRECT) {
578 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
579 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
583 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
585 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
586 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
587 SetLastError(0xdeadbeef);
589 res = InternetCloseHandle(hor);
590 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
591 SetLastError(0xdeadbeef);
592 res = InternetCloseHandle(hor);
593 ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
594 ok (GetLastError() == ERROR_INVALID_HANDLE,
595 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
598 /* We intentionally do not close the handle opened by InternetConnectA as this
599 * tickles bug #9479: native closes child internet handles when the parent handles
600 * are closed. This is verified below by checking that the number of
601 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
603 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
604 trace("closing 2\n");
605 res = InternetCloseHandle(hi);
606 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
607 if (flags & INTERNET_FLAG_ASYNC)
610 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
611 if (hor != 0x0) todo_wine
613 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
614 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
618 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
619 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
621 CloseHandle(hCompleteEvent);
622 first_connection_to_test_url = FALSE;
625 static void InternetReadFile_chunked_test(void)
630 const char *types[2] = { "*", NULL };
631 HINTERNET hi, hic = 0, hor = 0;
633 trace("Starting InternetReadFile chunked test\n");
635 trace("InternetOpenA <--\n");
636 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
637 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
638 trace("InternetOpenA -->\n");
640 if (hi == 0x0) goto abort;
642 trace("InternetConnectA <--\n");
643 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
644 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
645 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
646 trace("InternetConnectA -->\n");
648 if (hic == 0x0) goto abort;
650 trace("HttpOpenRequestA <--\n");
651 hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
652 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
654 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
656 * If the internet name can't be resolved we are probably behind
657 * a firewall or in some other way not directly connected to the
658 * Internet. Not enough reason to fail the test. Just ignore and
662 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
664 trace("HttpOpenRequestA -->\n");
666 if (hor == 0x0) goto abort;
668 trace("HttpSendRequestA -->\n");
669 SetLastError(0xdeadbeef);
670 res = HttpSendRequestA(hor, "", -1, NULL, 0);
671 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
672 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
673 trace("HttpSendRequestA <--\n");
676 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
678 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
680 SetLastError( 0xdeadbeef );
682 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
684 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
685 ok( res || ( proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
686 "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
687 ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
688 "Wrong transfer encoding '%s'\n", buffer );
690 SetLastError( 0xdeadbeef );
692 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
693 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
694 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
697 trace("Entering Query loop\n");
701 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
702 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
703 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
704 trace("got %u available\n",length);
708 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
710 res = InternetReadFile(hor,buffer,length,&got);
713 trace("ReadFile -> %i %i\n",res,got);
714 ok( length == got, "only got %u of %u available\n", got, length );
715 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
717 HeapFree(GetProcessHeap(),0,buffer);
726 res = InternetCloseHandle(hor);
727 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
730 res = InternetCloseHandle(hi);
731 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
735 static void InternetReadFileExA_test(int flags)
739 const char *types[2] = { "*", NULL };
740 HINTERNET hi, hic = 0, hor = 0;
741 INTERNET_BUFFERS inetbuffers;
743 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
745 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
747 trace("InternetOpenA <--\n");
748 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
749 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
750 trace("InternetOpenA -->\n");
752 if (hi == 0x0) goto abort;
754 pInternetSetStatusCallbackA(hi,&callback);
756 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
758 trace("InternetConnectA <--\n");
759 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
760 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
761 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
762 trace("InternetConnectA -->\n");
764 if (hic == 0x0) goto abort;
766 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
767 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
769 trace("HttpOpenRequestA <--\n");
770 hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
771 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
773 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
775 * If the internet name can't be resolved we are probably behind
776 * a firewall or in some other way not directly connected to the
777 * Internet. Not enough reason to fail the test. Just ignore and
781 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
783 trace("HttpOpenRequestA -->\n");
785 if (hor == 0x0) goto abort;
787 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
788 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
789 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
790 if (first_connection_to_test_url)
792 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
793 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
794 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
795 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
799 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
800 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
802 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
803 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
804 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
805 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
806 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
807 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
808 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
809 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
810 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
811 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
812 SET_EXPECT(INTERNET_STATUS_REDIRECT);
813 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
814 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
815 if (flags & INTERNET_FLAG_ASYNC)
816 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
818 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
820 trace("HttpSendRequestA -->\n");
821 SetLastError(0xdeadbeef);
822 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
823 if (flags & INTERNET_FLAG_ASYNC)
824 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
825 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
827 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
828 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
829 trace("HttpSendRequestA <--\n");
831 if (!rc && (GetLastError() == ERROR_IO_PENDING))
832 WaitForSingleObject(hCompleteEvent, INFINITE);
834 if (first_connection_to_test_url)
836 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
837 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
841 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
842 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
844 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
845 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
846 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
847 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
848 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
849 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
850 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
851 if (flags & INTERNET_FLAG_ASYNC)
852 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
854 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
855 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
856 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
857 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
859 /* tests invalid dwStructSize */
860 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
861 inetbuffers.lpcszHeader = NULL;
862 inetbuffers.dwHeadersLength = 0;
863 inetbuffers.dwBufferLength = 10;
864 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
865 inetbuffers.dwOffsetHigh = 1234;
866 inetbuffers.dwOffsetLow = 5678;
867 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
868 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
869 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
870 rc ? "TRUE" : "FALSE", GetLastError());
871 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
873 /* tests to see whether lpcszHeader is used - it isn't */
874 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
875 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
876 inetbuffers.dwHeadersLength = 255;
877 inetbuffers.dwBufferLength = 0;
878 inetbuffers.lpvBuffer = NULL;
879 inetbuffers.dwOffsetHigh = 1234;
880 inetbuffers.dwOffsetLow = 5678;
881 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
882 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
883 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
884 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
885 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
886 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
887 trace("read %i bytes\n", inetbuffers.dwBufferLength);
890 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
891 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
894 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
895 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
896 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
897 rc ? "TRUE" : "FALSE", GetLastError());
900 trace("Entering Query loop\n");
902 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
903 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
906 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
907 inetbuffers.dwBufferLength = 1024;
908 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
909 inetbuffers.dwOffsetHigh = 1234;
910 inetbuffers.dwOffsetLow = 5678;
912 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
913 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
914 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
915 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
916 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
917 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
920 if (GetLastError() == ERROR_IO_PENDING)
922 trace("InternetReadFileEx -> PENDING\n");
923 ok(flags & INTERNET_FLAG_ASYNC,
924 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
925 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
926 WaitForSingleObject(hCompleteEvent, INFINITE);
927 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
928 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
932 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
938 trace("InternetReadFileEx -> SUCCEEDED\n");
939 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
940 if (inetbuffers.dwBufferLength)
943 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
944 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
949 /* Win98 still sends these when 0 bytes are read, WinXP does not */
950 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
951 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
955 trace("read %i bytes\n", inetbuffers.dwBufferLength);
956 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
958 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
959 "InternetReadFileEx sets offsets to 0x%x%08x\n",
960 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
962 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
964 if (!inetbuffers.dwBufferLength)
967 length += inetbuffers.dwBufferLength;
969 ok(length > 0, "failed to read any of the document\n");
970 trace("Finished. Read %d bytes\n", length);
972 /* WinXP does not send, but Win98 does */
973 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
974 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
976 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
978 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
979 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
980 rc = InternetCloseHandle(hor);
981 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
982 rc = InternetCloseHandle(hor);
983 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
986 rc = InternetCloseHandle(hic);
987 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
990 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
991 rc = InternetCloseHandle(hi);
992 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
993 if (flags & INTERNET_FLAG_ASYNC)
995 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
997 /* to enable once Wine is fixed to never send it
998 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
999 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
1001 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
1002 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
1003 CloseHandle(hCompleteEvent);
1004 first_connection_to_test_url = FALSE;
1007 static void InternetOpenUrlA_test(void)
1009 HINTERNET myhinternet, myhttp;
1011 DWORD size, readbytes, totalbytes=0;
1014 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1015 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1017 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
1018 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1021 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
1022 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
1023 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1024 return; /* WinXP returns this when not connected to the net */
1025 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1026 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1027 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1028 totalbytes += readbytes;
1029 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1030 totalbytes += readbytes;
1031 trace("read 0x%08x bytes\n",totalbytes);
1033 InternetCloseHandle(myhttp);
1034 InternetCloseHandle(myhinternet);
1037 static void HttpSendRequestEx_test(void)
1043 INTERNET_BUFFERS BufferIn;
1044 DWORD dwBytesWritten, dwBytesRead, error;
1049 static char szPostData[] = "mode=Test";
1050 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1052 hSession = InternetOpen("Wine Regression Test",
1053 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1054 ok( hSession != NULL ,"Unable to open Internet session\n");
1055 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1056 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1058 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1059 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1060 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1061 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1063 skip( "Network unreachable, skipping test\n" );
1066 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1069 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1070 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1071 BufferIn.lpcszHeader = szContentType;
1072 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1073 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1074 BufferIn.lpvBuffer = szPostData;
1075 BufferIn.dwBufferLength = 3;
1076 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1077 BufferIn.dwOffsetLow = 0;
1078 BufferIn.dwOffsetHigh = 0;
1080 SetLastError(0xdeadbeef);
1081 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1082 error = GetLastError();
1083 ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1084 ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1086 for (i = 3; szPostData[i]; i++)
1087 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1088 "InternetWriteFile failed\n");
1090 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1092 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1093 "Unable to read response\n");
1094 szBuffer[dwBytesRead] = 0;
1096 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1097 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1099 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1101 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1102 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1105 static void InternetOpenRequest_test(void)
1107 HINTERNET session, connect, request;
1108 static const char *types[] = { "*", "", NULL };
1109 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1110 static const WCHAR *typesW[] = { any, empty, NULL };
1113 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1114 ok(session != NULL ,"Unable to open Internet session\n");
1116 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1117 INTERNET_SERVICE_HTTP, 0, 0);
1118 ok(connect == NULL, "InternetConnectA should have failed\n");
1119 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1121 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1122 INTERNET_SERVICE_HTTP, 0, 0);
1123 ok(connect == NULL, "InternetConnectA should have failed\n");
1124 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1126 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1127 INTERNET_SERVICE_HTTP, 0, 0);
1128 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1130 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1131 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1133 skip( "Network unreachable, skipping test\n" );
1136 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1138 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1139 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1140 ok(InternetCloseHandle(request), "Close request handle failed\n");
1142 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1143 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1145 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1146 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1147 ok(InternetCloseHandle(request), "Close request handle failed\n");
1150 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1151 ok(InternetCloseHandle(session), "Close session handle failed\n");
1154 static void test_http_cache(void)
1156 HINTERNET session, connect, request;
1157 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1158 DWORD size, file_size;
1163 static const char *types[] = { "*", "", NULL };
1165 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1166 ok(session != NULL ,"Unable to open Internet session\n");
1168 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1169 INTERNET_SERVICE_HTTP, 0, 0);
1170 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1172 request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1173 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1175 skip( "Network unreachable, skipping test\n" );
1177 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1178 ok(InternetCloseHandle(session), "Close session handle failed\n");
1182 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1185 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1186 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1187 ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1189 size = sizeof(file_name);
1190 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1191 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1192 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1193 ok(!size, "size = %d\n", size);
1195 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1196 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1198 size = sizeof(file_name);
1199 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1200 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1202 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1203 FILE_ATTRIBUTE_NORMAL, NULL);
1204 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1205 file_size = GetFileSize(file, NULL);
1206 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1209 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1210 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1211 ok(size == 100, "size = %u\n", size);
1213 file_size = GetFileSize(file, NULL);
1214 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1217 ok(InternetCloseHandle(request), "Close request handle failed\n");
1219 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1220 FILE_ATTRIBUTE_NORMAL, NULL);
1221 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1224 /* Send the same request, requiring it to be retrieved from the cache */
1225 request = HttpOpenRequest(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1227 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1228 ok(ret, "HttpSendRequest failed\n");
1231 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1232 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1233 ok(size == 100, "size = %u\n", size);
1235 ok(InternetCloseHandle(request), "Close request handle failed\n");
1237 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1238 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1240 size = sizeof(file_name);
1241 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1242 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1243 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1244 ok(!size, "size = %d\n", size);
1246 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1247 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1249 size = sizeof(file_name);
1251 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1254 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1255 FILE_ATTRIBUTE_NORMAL, NULL);
1256 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1262 ok(file_name[0] == 0, "Didn't expect a file name\n");
1265 ok(InternetCloseHandle(request), "Close request handle failed\n");
1266 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1267 ok(InternetCloseHandle(session), "Close session handle failed\n");
1270 static void HttpHeaders_test(void)
1281 hSession = InternetOpen("Wine Regression Test",
1282 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1283 ok( hSession != NULL ,"Unable to open Internet session\n");
1284 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1285 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1287 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1288 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1289 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1290 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1292 skip( "Network unreachable, skipping test\n" );
1295 ok( hRequest != NULL, "Failed to open request handle\n");
1298 len = sizeof(buffer);
1299 strcpy(buffer,"Warning");
1300 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1301 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1303 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1304 "Failed to add new header\n");
1307 len = sizeof(buffer);
1308 strcpy(buffer,"Warning");
1309 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1310 buffer,&len,&index),"Unable to query header\n");
1311 ok(index == 1, "Index was not incremented\n");
1312 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1313 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1314 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1315 len = sizeof(buffer);
1316 strcpy(buffer,"Warning");
1317 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1318 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1321 len = 5; /* could store the string but not the NULL terminator */
1322 strcpy(buffer,"Warning");
1323 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1324 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1325 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1326 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1328 /* a call with NULL will fail but will return the length */
1330 len = sizeof(buffer);
1331 SetLastError(0xdeadbeef);
1332 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1333 NULL,&len,&index) == FALSE,"Query worked\n");
1334 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1335 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1336 ok(index == 0, "Index was incremented\n");
1338 /* even for a len that is too small */
1341 SetLastError(0xdeadbeef);
1342 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1343 NULL,&len,&index) == FALSE,"Query worked\n");
1344 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1345 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1346 ok(index == 0, "Index was incremented\n");
1350 SetLastError(0xdeadbeef);
1351 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1352 NULL,&len,&index) == FALSE,"Query worked\n");
1353 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1354 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1355 ok(index == 0, "Index was incremented\n");
1356 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1359 /* a working query */
1361 len = sizeof(buffer);
1362 memset(buffer, 'x', sizeof(buffer));
1363 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1364 buffer,&len,&index),"Unable to query header\n");
1365 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1366 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1367 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1368 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1369 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1370 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1371 ok(index == 0, "Index was incremented\n");
1373 /* Like above two tests, but for W version */
1377 SetLastError(0xdeadbeef);
1378 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1379 NULL,&len,&index) == FALSE,"Query worked\n");
1380 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1381 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1382 ok(index == 0, "Index was incremented\n");
1383 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1385 /* a working query */
1387 len = sizeof(wbuffer);
1388 memset(wbuffer, 'x', sizeof(wbuffer));
1389 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1390 wbuffer,&len,&index),"Unable to query header\n");
1391 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1392 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1393 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1394 ok(index == 0, "Index was incremented\n");
1396 /* end of W version tests */
1398 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1400 len = sizeof(buffer);
1401 memset(buffer, 'x', sizeof(buffer));
1402 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1403 buffer,&len,&index) == TRUE,"Query failed\n");
1404 ok(len == 2, "Expected 2, got %d\n", len);
1405 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1406 ok(index == 0, "Index was incremented\n");
1408 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1409 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1412 len = sizeof(buffer);
1413 strcpy(buffer,"Warning");
1414 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1415 buffer,&len,&index),"Unable to query header\n");
1416 ok(index == 1, "Index was not incremented\n");
1417 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1418 len = sizeof(buffer);
1419 strcpy(buffer,"Warning");
1420 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1421 buffer,&len,&index),"Failed to get second header\n");
1422 ok(index == 2, "Index was not incremented\n");
1423 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1424 len = sizeof(buffer);
1425 strcpy(buffer,"Warning");
1426 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1427 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1429 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1432 len = sizeof(buffer);
1433 strcpy(buffer,"Warning");
1434 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1435 buffer,&len,&index),"Unable to query header\n");
1436 ok(index == 1, "Index was not incremented\n");
1437 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1438 len = sizeof(buffer);
1439 strcpy(buffer,"Warning");
1440 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1441 buffer,&len,&index),"Failed to get second header\n");
1442 ok(index == 2, "Index was not incremented\n");
1443 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1444 len = sizeof(buffer);
1445 strcpy(buffer,"Warning");
1446 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1447 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1449 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1452 len = sizeof(buffer);
1453 strcpy(buffer,"Warning");
1454 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1455 buffer,&len,&index),"Unable to query header\n");
1456 ok(index == 1, "Index was not incremented\n");
1457 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1458 len = sizeof(buffer);
1459 strcpy(buffer,"Warning");
1460 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1461 buffer,&len,&index),"Failed to get second header\n");
1462 ok(index == 2, "Index was not incremented\n");
1463 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1464 len = sizeof(buffer);
1465 strcpy(buffer,"Warning");
1466 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1467 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1469 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1472 len = sizeof(buffer);
1473 strcpy(buffer,"Warning");
1474 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1475 buffer,&len,&index),"Unable to query header\n");
1476 ok(index == 1, "Index was not incremented\n");
1477 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1478 len = sizeof(buffer);
1479 strcpy(buffer,"Warning");
1480 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1481 ok(index == 2, "Index was not incremented\n");
1482 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1483 len = sizeof(buffer);
1484 strcpy(buffer,"Warning");
1485 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1487 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1490 len = sizeof(buffer);
1491 strcpy(buffer,"Warning");
1492 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1493 ok(index == 1, "Index was not incremented\n");
1494 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1495 len = sizeof(buffer);
1496 strcpy(buffer,"Warning");
1497 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1498 ok(index == 2, "Index was not incremented\n");
1499 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1500 len = sizeof(buffer);
1501 strcpy(buffer,"Warning");
1502 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1504 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1507 len = sizeof(buffer);
1508 strcpy(buffer,"Warning");
1509 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1510 ok(index == 1, "Index was not incremented\n");
1511 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1512 len = sizeof(buffer);
1513 strcpy(buffer,"Warning");
1514 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1515 ok(index == 2, "Index was not incremented\n");
1516 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1517 len = sizeof(buffer);
1518 strcpy(buffer,"Warning");
1519 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1521 ok(HttpAddRequestHeaders(hRequest,"Warning:test7",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE), "HTTP_ADDREQ_FLAG_ADD with HTTP_ADDREQ_FLAG_REPALCE Did not work\n");
1524 len = sizeof(buffer);
1525 strcpy(buffer,"Warning");
1526 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1527 ok(index == 1, "Index was not incremented\n");
1528 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1529 len = sizeof(buffer);
1530 strcpy(buffer,"Warning");
1531 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1532 ok(index == 2, "Index was not incremented\n");
1533 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1534 len = sizeof(buffer);
1535 strcpy(buffer,"Warning");
1536 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1538 /* Ensure that blank headers are ignored and don't cause a failure */
1539 ok(HttpAddRequestHeaders(hRequest,"\r\nBlankTest:value\r\n\r\n",-1, HTTP_ADDREQ_FLAG_ADD_IF_NEW), "Failed to add header with blank entries in list\n");
1542 len = sizeof(buffer);
1543 strcpy(buffer,"BlankTest");
1544 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1545 ok(index == 1, "Index was not incremented\n");
1546 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1548 /* Ensure that malformed header separators are ignored and don't cause a failure */
1549 ok(HttpAddRequestHeaders(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1550 "Failed to add header with malformed entries in list\n");
1553 len = sizeof(buffer);
1554 strcpy(buffer,"MalformedTest");
1555 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1556 ok(index == 1, "Index was not incremented\n");
1557 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1559 len = sizeof(buffer);
1560 strcpy(buffer,"MalformedTestTwo");
1561 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1562 ok(index == 1, "Index was not incremented\n");
1563 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1565 len = sizeof(buffer);
1566 strcpy(buffer,"MalformedTestThree");
1567 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1568 ok(index == 1, "Index was not incremented\n");
1569 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1571 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1573 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1574 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1577 static const char garbagemsg[] =
1578 "Garbage: Header\r\n";
1580 static const char contmsg[] =
1581 "HTTP/1.1 100 Continue\r\n";
1583 static const char expandcontmsg[] =
1584 "HTTP/1.1 100 Continue\r\n"
1585 "Server: winecontinue\r\n"
1586 "Tag: something witty\r\n"
1589 static const char okmsg[] =
1590 "HTTP/1.1 200 OK\r\n"
1591 "Server: winetest\r\n"
1594 static const char okmsg2[] =
1595 "HTTP/1.1 200 OK\r\n"
1596 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1597 "Server: winetest\r\n"
1598 "Content-Length: 0\r\n"
1599 "Set-Cookie: one\r\n"
1600 "Set-Cookie: two\r\n"
1603 static const char notokmsg[] =
1604 "HTTP/1.1 400 Bad Request\r\n"
1605 "Server: winetest\r\n"
1608 static const char noauthmsg[] =
1609 "HTTP/1.1 401 Unauthorized\r\n"
1610 "Server: winetest\r\n"
1611 "Connection: close\r\n"
1612 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1615 static const char noauthmsg2[] =
1616 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1617 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1619 "Server: winetest\r\n";
1621 static const char proxymsg[] =
1622 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1623 "Server: winetest\r\n"
1624 "Proxy-Connection: close\r\n"
1625 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1628 static const char page1[] =
1630 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1631 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1634 struct server_info {
1637 int num_testH_retrievals;
1640 static DWORD CALLBACK server_thread(LPVOID param)
1642 struct server_info *si = param;
1645 struct sockaddr_in sa;
1648 int last_request = 0;
1649 char host_header[22];
1650 static int test_b = 0;
1652 WSAStartup(MAKEWORD(1,1), &wsaData);
1654 s = socket(AF_INET, SOCK_STREAM, 0);
1655 if (s == INVALID_SOCKET)
1659 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1661 memset(&sa, 0, sizeof sa);
1662 sa.sin_family = AF_INET;
1663 sa.sin_port = htons(si->port);
1664 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1666 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1672 SetEvent(si->hEvent);
1674 sprintf(host_header, "Host: localhost:%d", si->port);
1678 c = accept(s, NULL, NULL);
1680 memset(buffer, 0, sizeof buffer);
1681 for(i=0; i<(sizeof buffer-1); i++)
1683 r = recv(c, &buffer[i], 1, 0);
1687 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1688 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1691 if (strstr(buffer, "GET /test1"))
1693 if (!strstr(buffer, "Content-Length: 0"))
1695 send(c, okmsg, sizeof okmsg-1, 0);
1696 send(c, page1, sizeof page1-1, 0);
1699 send(c, notokmsg, sizeof notokmsg-1, 0);
1701 if (strstr(buffer, "/test2"))
1703 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1705 send(c, okmsg, sizeof okmsg-1, 0);
1706 send(c, page1, sizeof page1-1, 0);
1709 send(c, proxymsg, sizeof proxymsg-1, 0);
1711 if (strstr(buffer, "/test3"))
1713 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1714 send(c, okmsg, sizeof okmsg-1, 0);
1716 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1718 if (strstr(buffer, "/test4"))
1720 if (strstr(buffer, "Connection: Close"))
1721 send(c, okmsg, sizeof okmsg-1, 0);
1723 send(c, notokmsg, sizeof notokmsg-1, 0);
1725 if (strstr(buffer, "POST /test5") ||
1726 strstr(buffer, "RPC_IN_DATA /test5") ||
1727 strstr(buffer, "RPC_OUT_DATA /test5"))
1729 if (strstr(buffer, "Content-Length: 0"))
1731 send(c, okmsg, sizeof okmsg-1, 0);
1732 send(c, page1, sizeof page1-1, 0);
1735 send(c, notokmsg, sizeof notokmsg-1, 0);
1737 if (strstr(buffer, "GET /test6"))
1739 send(c, contmsg, sizeof contmsg-1, 0);
1740 send(c, contmsg, sizeof contmsg-1, 0);
1741 send(c, okmsg, sizeof okmsg-1, 0);
1742 send(c, page1, sizeof page1-1, 0);
1744 if (strstr(buffer, "POST /test7"))
1746 if (strstr(buffer, "Content-Length: 100"))
1748 send(c, okmsg, sizeof okmsg-1, 0);
1749 send(c, page1, sizeof page1-1, 0);
1752 send(c, notokmsg, sizeof notokmsg-1, 0);
1754 if (strstr(buffer, "/test8"))
1756 if (!strstr(buffer, "Connection: Close") &&
1757 strstr(buffer, "Connection: Keep-Alive") &&
1758 !strstr(buffer, "Cache-Control: no-cache") &&
1759 !strstr(buffer, "Pragma: no-cache") &&
1760 strstr(buffer, host_header))
1761 send(c, okmsg, sizeof okmsg-1, 0);
1763 send(c, notokmsg, sizeof notokmsg-1, 0);
1765 if (strstr(buffer, "/test9"))
1767 if (!strstr(buffer, "Connection: Close") &&
1768 !strstr(buffer, "Connection: Keep-Alive") &&
1769 !strstr(buffer, "Cache-Control: no-cache") &&
1770 !strstr(buffer, "Pragma: no-cache") &&
1771 strstr(buffer, host_header))
1772 send(c, okmsg, sizeof okmsg-1, 0);
1774 send(c, notokmsg, sizeof notokmsg-1, 0);
1776 if (strstr(buffer, "/testA"))
1778 if (!strstr(buffer, "Connection: Close") &&
1779 !strstr(buffer, "Connection: Keep-Alive") &&
1780 (strstr(buffer, "Cache-Control: no-cache") ||
1781 strstr(buffer, "Pragma: no-cache")) &&
1782 strstr(buffer, host_header))
1783 send(c, okmsg, sizeof okmsg-1, 0);
1785 send(c, notokmsg, sizeof notokmsg-1, 0);
1787 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1790 send(c, okmsg, sizeof okmsg-1, 0);
1791 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1792 send(c, okmsg, sizeof okmsg-1, 0);
1794 if (strstr(buffer, "/testC"))
1796 if (strstr(buffer, "Cookie: cookie=biscuit"))
1797 send(c, okmsg, sizeof okmsg-1, 0);
1799 send(c, notokmsg, sizeof notokmsg-1, 0);
1801 if (strstr(buffer, "/testD"))
1803 send(c, okmsg2, sizeof okmsg2-1, 0);
1805 if (strstr(buffer, "/testE"))
1807 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1809 if (strstr(buffer, "GET /quit"))
1811 send(c, okmsg, sizeof okmsg-1, 0);
1812 send(c, page1, sizeof page1-1, 0);
1815 if (strstr(buffer, "GET /testF"))
1817 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
1818 send(c, garbagemsg, sizeof garbagemsg-1, 0);
1819 send(c, contmsg, sizeof contmsg-1, 0);
1820 send(c, garbagemsg, sizeof garbagemsg-1, 0);
1821 send(c, okmsg, sizeof okmsg-1, 0);
1822 send(c, page1, sizeof page1-1, 0);
1824 if (strstr(buffer, "GET /testG"))
1826 send(c, page1, sizeof page1-1, 0);
1828 if (strstr(buffer, "GET /testH"))
1830 si->num_testH_retrievals++;
1831 if (!strstr(buffer, "Content-Length: 0"))
1833 send(c, okmsg, sizeof okmsg-1, 0);
1834 send(c, page1, sizeof page1-1, 0);
1837 send(c, notokmsg, sizeof notokmsg-1, 0);
1842 } while (!last_request);
1849 static void test_basic_request(int port, const char *verb, const char *url)
1851 HINTERNET hi, hc, hr;
1855 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1856 ok(hi != NULL, "open failed\n");
1858 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1859 ok(hc != NULL, "connect failed\n");
1861 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1862 ok(hr != NULL, "HttpOpenRequest failed\n");
1864 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1865 ok(r, "HttpSendRequest failed\n");
1868 memset(buffer, 0, sizeof buffer);
1869 SetLastError(0xdeadbeef);
1870 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1871 ok(r, "InternetReadFile failed %u\n", GetLastError());
1872 ok(count == sizeof page1 - 1, "count was wrong\n");
1873 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
1875 InternetCloseHandle(hr);
1876 InternetCloseHandle(hc);
1877 InternetCloseHandle(hi);
1880 static void test_last_error(int port)
1882 HINTERNET hi, hc, hr;
1886 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1887 ok(hi != NULL, "open failed\n");
1889 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1890 ok(hc != NULL, "connect failed\n");
1892 hr = HttpOpenRequest(hc, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1893 ok(hr != NULL, "HttpOpenRequest failed\n");
1895 SetLastError(0xdeadbeef);
1896 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1897 error = GetLastError();
1898 ok(r, "HttpSendRequest failed\n");
1899 ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
1901 InternetCloseHandle(hr);
1902 InternetCloseHandle(hc);
1903 InternetCloseHandle(hi);
1906 static void test_proxy_indirect(int port)
1908 HINTERNET hi, hc, hr;
1912 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1913 ok(hi != NULL, "open failed\n");
1915 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1916 ok(hc != NULL, "connect failed\n");
1918 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1919 ok(hr != NULL, "HttpOpenRequest failed\n");
1921 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1922 ok(r, "HttpSendRequest failed\n");
1925 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1926 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
1929 skip("missing proxy header, not testing remaining proxy headers\n");
1932 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1935 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1936 ok(r, "HttpQueryInfo failed\n");
1937 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1940 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1941 ok(r, "HttpQueryInfo failed\n");
1942 ok(val == 407, "proxy code wrong\n");
1945 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1946 ok(r, "HttpQueryInfo failed\n");
1947 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1950 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1951 ok(r, "HttpQueryInfo failed\n");
1952 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1955 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1956 ok(r, "HttpQueryInfo failed\n");
1957 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1960 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1961 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1962 ok(r == FALSE, "HttpQueryInfo failed\n");
1965 InternetCloseHandle(hr);
1966 InternetCloseHandle(hc);
1967 InternetCloseHandle(hi);
1970 static void test_proxy_direct(int port)
1972 HINTERNET hi, hc, hr;
1975 static CHAR username[] = "mike",
1976 password[] = "1101";
1978 sprintf(buffer, "localhost:%d\n", port);
1979 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1980 ok(hi != NULL, "open failed\n");
1982 /* try connect without authorization */
1983 hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1984 ok(hc != NULL, "connect failed\n");
1986 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1987 ok(hr != NULL, "HttpOpenRequest failed\n");
1989 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1990 ok(r, "HttpSendRequest failed\n");
1993 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1994 ok(r, "HttpQueryInfo failed\n");
1995 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1998 /* set the user + password then try again */
2000 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
2001 ok(r, "failed to set user\n");
2003 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
2004 ok(r, "failed to set password\n");
2007 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2008 ok(r, "HttpSendRequest failed\n");
2010 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
2011 ok(r, "HttpQueryInfo failed\n");
2013 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
2017 InternetCloseHandle(hr);
2018 InternetCloseHandle(hc);
2019 InternetCloseHandle(hi);
2022 static void test_header_handling_order(int port)
2024 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
2025 static char connection[] = "Connection: Close";
2027 static const char *types[2] = { "*", NULL };
2028 HINTERNET session, connect, request;
2032 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2033 ok(session != NULL, "InternetOpen failed\n");
2035 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2036 ok(connect != NULL, "InternetConnect failed\n");
2038 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2039 ok(request != NULL, "HttpOpenRequest failed\n");
2041 ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
2042 ok(ret, "HttpAddRequestHeaders failed\n");
2044 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2045 ok(ret, "HttpSendRequest failed\n");
2048 size = sizeof(status);
2049 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2050 ok(ret, "HttpQueryInfo failed\n");
2051 ok(status == 200, "request failed with status %u\n", status);
2053 InternetCloseHandle(request);
2055 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2056 ok(request != NULL, "HttpOpenRequest failed\n");
2058 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2059 ok(ret, "HttpSendRequest failed\n");
2062 size = sizeof(status);
2063 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2064 ok(ret, "HttpQueryInfo failed\n");
2065 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2067 InternetCloseHandle(request);
2069 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
2070 ok(request != NULL, "HttpOpenRequest failed\n");
2072 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2073 ok(ret, "HttpAddRequestHeaders failed\n");
2075 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
2076 ok(ret, "HttpSendRequest failed\n");
2079 size = sizeof(status);
2080 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2081 ok(ret, "HttpQueryInfo failed\n");
2082 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
2084 InternetCloseHandle(request);
2085 InternetCloseHandle(connect);
2086 InternetCloseHandle(session);
2089 static void test_connection_header(int port)
2091 HINTERNET ses, con, req;
2095 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2096 ok(ses != NULL, "InternetOpen failed\n");
2098 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2099 ok(con != NULL, "InternetConnect failed\n");
2101 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2102 ok(req != NULL, "HttpOpenRequest failed\n");
2104 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2105 ok(ret, "HttpSendRequest failed\n");
2108 size = sizeof(status);
2109 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2110 ok(ret, "HttpQueryInfo failed\n");
2111 ok(status == 200, "request failed with status %u\n", status);
2113 InternetCloseHandle(req);
2115 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
2116 ok(req != NULL, "HttpOpenRequest failed\n");
2118 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2119 ok(ret, "HttpSendRequest failed\n");
2122 size = sizeof(status);
2123 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2124 ok(ret, "HttpQueryInfo failed\n");
2125 ok(status == 200, "request failed with status %u\n", status);
2127 InternetCloseHandle(req);
2129 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2130 ok(req != NULL, "HttpOpenRequest failed\n");
2132 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2133 ok(ret, "HttpSendRequest failed\n");
2136 size = sizeof(status);
2137 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2138 ok(ret, "HttpQueryInfo failed\n");
2139 ok(status == 200, "request failed with status %u\n", status);
2141 InternetCloseHandle(req);
2143 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2144 ok(req != NULL, "HttpOpenRequest failed\n");
2146 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2147 ok(ret, "HttpSendRequest failed\n");
2150 size = sizeof(status);
2151 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2152 ok(ret, "HttpQueryInfo failed\n");
2153 ok(status == 200, "request failed with status %u\n", status);
2155 InternetCloseHandle(req);
2156 InternetCloseHandle(con);
2157 InternetCloseHandle(ses);
2160 static void test_http1_1(int port)
2162 HINTERNET ses, con, req;
2165 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2166 ok(ses != NULL, "InternetOpen failed\n");
2168 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2169 ok(con != NULL, "InternetConnect failed\n");
2171 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2172 ok(req != NULL, "HttpOpenRequest failed\n");
2174 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2177 InternetCloseHandle(req);
2179 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2180 ok(req != NULL, "HttpOpenRequest failed\n");
2182 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2183 ok(ret, "HttpSendRequest failed\n");
2186 InternetCloseHandle(req);
2187 InternetCloseHandle(con);
2188 InternetCloseHandle(ses);
2191 static void test_HttpSendRequestW(int port)
2193 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
2194 HINTERNET ses, con, req;
2198 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2199 ok(ses != NULL, "InternetOpen failed\n");
2201 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2202 ok(con != NULL, "InternetConnect failed\n");
2204 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2205 ok(req != NULL, "HttpOpenRequest failed\n");
2207 SetLastError(0xdeadbeef);
2208 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
2209 error = GetLastError();
2210 ok(!ret, "HttpSendRequestW succeeded\n");
2211 ok(error == ERROR_IO_PENDING ||
2212 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
2213 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
2214 "got %u expected ERROR_IO_PENDING\n", error);
2216 InternetCloseHandle(req);
2217 InternetCloseHandle(con);
2218 InternetCloseHandle(ses);
2221 static void test_cookie_header(int port)
2223 HINTERNET ses, con, req;
2224 DWORD size, status, error;
2228 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2229 ok(ses != NULL, "InternetOpen failed\n");
2231 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2232 ok(con != NULL, "InternetConnect failed\n");
2234 InternetSetCookie("http://localhost", "cookie", "biscuit");
2236 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2237 ok(req != NULL, "HttpOpenRequest failed\n");
2240 size = sizeof(buffer);
2241 SetLastError(0xdeadbeef);
2242 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2243 error = GetLastError();
2244 ok(!ret, "HttpQueryInfo succeeded\n");
2245 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
2247 ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
2248 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
2251 size = sizeof(buffer);
2252 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2253 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2254 ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
2256 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2257 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
2260 size = sizeof(status);
2261 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2262 ok(ret, "HttpQueryInfo failed\n");
2263 ok(status == 200, "request failed with status %u\n", status);
2266 size = sizeof(buffer);
2267 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2268 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2269 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
2271 InternetCloseHandle(req);
2272 InternetCloseHandle(con);
2273 InternetCloseHandle(ses);
2276 static void test_basic_authentication(int port)
2278 HINTERNET session, connect, request;
2282 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2283 ok(session != NULL, "InternetOpen failed\n");
2285 connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
2286 ok(connect != NULL, "InternetConnect failed\n");
2288 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
2289 ok(request != NULL, "HttpOpenRequest failed\n");
2291 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2292 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2295 size = sizeof(status);
2296 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2297 ok(ret, "HttpQueryInfo failed\n");
2298 ok(status == 200, "request failed with status %u\n", status);
2300 InternetCloseHandle(request);
2301 InternetCloseHandle(connect);
2302 InternetCloseHandle(session);
2305 static void test_invalid_response_headers(int port)
2307 HINTERNET session, connect, request;
2312 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2313 ok(session != NULL, "InternetOpen failed\n");
2315 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2316 ok(connect != NULL, "InternetConnect failed\n");
2318 request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
2319 ok(request != NULL, "HttpOpenRequest failed\n");
2321 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2322 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2325 size = sizeof(status);
2326 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2327 ok(ret, "HttpQueryInfo failed\n");
2328 ok(status == 401, "unexpected status %u\n", status);
2331 size = sizeof(buffer);
2332 ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2333 ok(ret, "HttpQueryInfo failed\n");
2334 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
2335 "headers wrong \"%s\"\n", buffer);
2338 size = sizeof(buffer);
2339 ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
2340 ok(ret, "HttpQueryInfo failed\n");
2341 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
2343 InternetCloseHandle(request);
2344 InternetCloseHandle(connect);
2345 InternetCloseHandle(session);
2348 static void test_response_without_headers(int port)
2350 HINTERNET hi, hc, hr;
2351 DWORD r, count, size, status;
2354 SetLastError(0xdeadbeef);
2355 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2356 ok(hi != NULL, "open failed %u\n", GetLastError());
2358 SetLastError(0xdeadbeef);
2359 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2360 ok(hc != NULL, "connect failed %u\n", GetLastError());
2362 SetLastError(0xdeadbeef);
2363 hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
2364 ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
2366 SetLastError(0xdeadbeef);
2367 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2368 ok(r, "HttpSendRequest failed %u\n", GetLastError());
2371 memset(buffer, 0, sizeof buffer);
2372 SetLastError(0xdeadbeef);
2373 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2374 ok(r, "InternetReadFile failed %u\n", GetLastError());
2375 todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
2376 todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2379 size = sizeof(status);
2380 SetLastError(0xdeadbeef);
2381 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2382 todo_wine ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2383 todo_wine ok(status == 200, "expected status 200 got %u\n", status);
2386 size = sizeof(buffer);
2387 SetLastError(0xdeadbeef);
2388 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
2389 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2390 ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
2393 size = sizeof(buffer);
2394 SetLastError(0xdeadbeef);
2395 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
2396 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2397 ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
2400 size = sizeof(buffer);
2401 SetLastError(0xdeadbeef);
2402 r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2403 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2404 ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
2406 InternetCloseHandle(hr);
2407 InternetCloseHandle(hc);
2408 InternetCloseHandle(hi);
2411 static void test_HttpQueryInfo(int port)
2413 HINTERNET hi, hc, hr;
2418 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2419 ok(hi != NULL, "InternetOpen failed\n");
2421 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2422 ok(hc != NULL, "InternetConnect failed\n");
2424 hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2425 ok(hr != NULL, "HttpOpenRequest failed\n");
2427 ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2428 ok(ret, "HttpSendRequest failed\n");
2431 size = sizeof(buffer);
2432 ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2433 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2434 ok(index == 1, "expected 1 got %u\n", index);
2437 size = sizeof(buffer);
2438 ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2439 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2440 ok(index == 1, "expected 1 got %u\n", index);
2443 size = sizeof(buffer);
2444 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2445 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2446 ok(index == 0, "expected 0 got %u\n", index);
2448 size = sizeof(buffer);
2449 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2450 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2451 ok(index == 0, "expected 0 got %u\n", index);
2453 size = sizeof(buffer);
2454 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2455 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2456 ok(index == 0, "expected 0 got %u\n", index);
2458 size = sizeof(buffer);
2459 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2460 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2461 ok(index == 0, "expected 0 got %u\n", index);
2463 size = sizeof(buffer);
2464 ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2465 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2466 ok(index == 0, "expected 0 got %u\n", index);
2469 size = sizeof(buffer);
2470 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2471 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2472 ok(index == 0, "expected 0 got %u\n", index);
2475 size = sizeof(buffer);
2476 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2477 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2478 ok(index == 0, "expected 0 got %u\n", index);
2481 size = sizeof(buffer);
2482 ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2483 ok(!ret, "HttpQueryInfo succeeded\n");
2484 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2487 size = sizeof(buffer);
2488 ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2489 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2490 ok(index == 1, "expected 1 got %u\n", index);
2493 size = sizeof(buffer);
2494 strcpy(buffer, "Server");
2495 ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2496 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2497 ok(index == 1, "expected 1 got %u\n", index);
2500 size = sizeof(buffer);
2501 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2502 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2503 ok(index == 1, "expected 1 got %u\n", index);
2505 size = sizeof(buffer);
2506 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2507 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2508 ok(index == 2, "expected 2 got %u\n", index);
2510 InternetCloseHandle(hr);
2511 InternetCloseHandle(hc);
2512 InternetCloseHandle(hi);
2515 static void test_options(int port)
2517 HINTERNET ses, con, req;
2522 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2523 ok(ses != NULL, "InternetOpen failed\n");
2525 SetLastError(0xdeadbeef);
2526 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, 0);
2527 error = GetLastError();
2528 ok(!ret, "InternetSetOption succeeded\n");
2529 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2531 SetLastError(0xdeadbeef);
2532 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, sizeof(ctx));
2533 ok(!ret, "InternetSetOption succeeded\n");
2534 error = GetLastError();
2535 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2537 SetLastError(0xdeadbeef);
2538 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, 0);
2539 ok(!ret, "InternetSetOption succeeded\n");
2540 error = GetLastError();
2541 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2544 ret = InternetSetOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2545 ok(ret, "InternetSetOption failed %u\n", GetLastError());
2547 SetLastError(0xdeadbeef);
2548 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, NULL);
2549 error = GetLastError();
2550 ok(!ret, "InternetQueryOption succeeded\n");
2551 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2553 SetLastError(0xdeadbeef);
2554 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, NULL);
2555 error = GetLastError();
2556 ok(!ret, "InternetQueryOption succeeded\n");
2557 ok(error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER, got %u\n", error);
2560 SetLastError(0xdeadbeef);
2561 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, NULL, &size);
2562 error = GetLastError();
2563 ok(!ret, "InternetQueryOption succeeded\n");
2564 ok(error == ERROR_INSUFFICIENT_BUFFER, "expected ERROR_INSUFFICIENT_BUFFER, got %u\n", error);
2567 SetLastError(0xdeadbeef);
2568 ret = InternetQueryOption(NULL, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2569 error = GetLastError();
2570 ok(!ret, "InternetQueryOption succeeded\n");
2571 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE, got %u\n", error);
2575 ret = InternetQueryOption(ses, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2576 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2577 ok(ctx == 1, "expected 1 got %lu\n", ctx);
2579 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2580 ok(con != NULL, "InternetConnect failed\n");
2584 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2585 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2586 ok(ctx == 0, "expected 0 got %lu\n", ctx);
2589 ret = InternetSetOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2590 ok(ret, "InternetSetOption failed %u\n", GetLastError());
2594 ret = InternetQueryOption(con, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2595 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2596 ok(ctx == 2, "expected 2 got %lu\n", ctx);
2598 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2599 ok(req != NULL, "HttpOpenRequest failed\n");
2603 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2604 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2605 ok(ctx == 0, "expected 0 got %lu\n", ctx);
2608 ret = InternetSetOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, sizeof(ctx));
2609 ok(ret, "InternetSetOption failed %u\n", GetLastError());
2613 ret = InternetQueryOption(req, INTERNET_OPTION_CONTEXT_VALUE, &ctx, &size);
2614 ok(ret, "InternetQueryOption failed %u\n", GetLastError());
2615 ok(ctx == 3, "expected 3 got %lu\n", ctx);
2617 InternetCloseHandle(req);
2618 InternetCloseHandle(con);
2619 InternetCloseHandle(ses);
2622 static void test_url_caching(int port, int *num_retrievals)
2624 HINTERNET hi, hc, hr;
2628 ok(*num_retrievals == 0, "expected 0 retrievals prior to test\n");
2630 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2631 ok(hi != NULL, "open failed\n");
2633 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2634 ok(hc != NULL, "connect failed\n");
2636 /* Pre-load the cache: */
2637 hr = HttpOpenRequest(hc, "GET", "/testH", NULL, NULL, NULL, 0, 0);
2638 ok(hr != NULL, "HttpOpenRequest failed\n");
2640 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2641 ok(r, "HttpSendRequest failed\n");
2643 ok(*num_retrievals == 1, "expected 1 retrievals\n");
2646 memset(buffer, 0, sizeof buffer);
2647 SetLastError(0xdeadbeef);
2648 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2649 ok(r, "InternetReadFile failed %u\n", GetLastError());
2650 ok(count == sizeof page1 - 1, "count was wrong\n");
2651 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2653 InternetCloseHandle(hr);
2655 /* Send the same request, requiring it to be retrieved from the cache */
2656 hr = HttpOpenRequest(hc, "GET", "/testH", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
2657 ok(hr != NULL, "HttpOpenRequest failed\n");
2659 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2660 /* Older Windows versions succeed with this request, newer ones fail with
2661 * ERROR_FILE_NOT_FOUND. Accept either, as the older version allows us
2662 * to verify that the server isn't contacted.
2665 ok(GetLastError() == ERROR_FILE_NOT_FOUND,
2666 "expected ERROR_FILE_NOT_FOUND, got %d\n", GetLastError());
2669 /* The server shouldn't be contacted for this request. */
2671 ok(*num_retrievals == 1, "expected 1 retrievals\n");
2674 memset(buffer, 0, sizeof buffer);
2675 SetLastError(0xdeadbeef);
2676 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2677 ok(r, "InternetReadFile failed %u\n", GetLastError());
2678 ok(count == sizeof page1 - 1, "count was wrong\n");
2679 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2682 InternetCloseHandle(hc);
2683 InternetCloseHandle(hi);
2686 static void test_http_connection(void)
2688 struct server_info si;
2692 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2694 si.num_testH_retrievals = 0;
2696 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2697 ok( hThread != NULL, "create thread failed\n");
2699 r = WaitForSingleObject(si.hEvent, 10000);
2700 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2701 if (r != WAIT_OBJECT_0)
2704 test_basic_request(si.port, "GET", "/test1");
2705 test_proxy_indirect(si.port);
2706 test_proxy_direct(si.port);
2707 test_header_handling_order(si.port);
2708 test_basic_request(si.port, "POST", "/test5");
2709 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2710 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2711 test_basic_request(si.port, "GET", "/test6");
2712 test_basic_request(si.port, "GET", "/testF");
2713 test_connection_header(si.port);
2714 test_http1_1(si.port);
2715 test_cookie_header(si.port);
2716 test_basic_authentication(si.port);
2717 test_invalid_response_headers(si.port);
2718 test_response_without_headers(si.port);
2719 test_HttpQueryInfo(si.port);
2720 test_HttpSendRequestW(si.port);
2721 test_last_error(si.port);
2722 test_options(si.port);
2723 test_url_caching(si.port, &si.num_testH_retrievals);
2725 /* send the basic request again to shutdown the server thread */
2726 test_basic_request(si.port, "GET", "/quit");
2728 r = WaitForSingleObject(hThread, 3000);
2729 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2730 CloseHandle(hThread);
2733 static void release_cert_info(INTERNET_CERTIFICATE_INFOA *info)
2735 LocalFree(info->lpszSubjectInfo);
2736 LocalFree(info->lpszIssuerInfo);
2737 LocalFree(info->lpszProtocolName);
2738 LocalFree(info->lpszSignatureAlgName);
2739 LocalFree(info->lpszEncryptionAlgName);
2742 static void test_secure_connection(void)
2744 static const WCHAR gizmo5[] = {'G','i','z','m','o','5',0};
2745 static const WCHAR testbot[] = {'t','e','s','t','b','o','t','.','w','i','n','e','h','q','.','o','r','g',0};
2746 static const WCHAR get[] = {'G','E','T',0};
2747 static const WCHAR slash[] = {'/',0};
2748 HINTERNET ses, con, req;
2750 INTERNET_CERTIFICATE_INFOA *certificate_structA = NULL;
2751 INTERNET_CERTIFICATE_INFOW *certificate_structW = NULL;
2754 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2755 ok(ses != NULL, "InternetOpen failed\n");
2757 con = InternetConnect(ses, "testbot.winehq.org",
2758 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2759 INTERNET_SERVICE_HTTP, 0, 0);
2760 ok(con != NULL, "InternetConnect failed\n");
2762 req = HttpOpenRequest(con, "GET", "/", NULL, NULL, NULL,
2763 INTERNET_FLAG_SECURE, 0);
2764 ok(req != NULL, "HttpOpenRequest failed\n");
2766 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2767 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2769 size = sizeof(flags);
2770 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2771 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2772 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2774 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2776 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2777 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2778 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2779 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2780 certificate_structA, &size);
2781 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2784 ok(certificate_structA->lpszSubjectInfo &&
2785 strlen(certificate_structA->lpszSubjectInfo) > 1,
2786 "expected a non-empty subject name\n");
2787 ok(certificate_structA->lpszIssuerInfo &&
2788 strlen(certificate_structA->lpszIssuerInfo) > 1,
2789 "expected a non-empty issuer name\n");
2790 ok(!certificate_structA->lpszSignatureAlgName,
2791 "unexpected signature algorithm name\n");
2792 ok(!certificate_structA->lpszEncryptionAlgName,
2793 "unexpected encryption algorithm name\n");
2794 ok(!certificate_structA->lpszProtocolName,
2795 "unexpected protocol name\n");
2796 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2797 release_cert_info(certificate_structA);
2799 HeapFree(GetProcessHeap(), 0, certificate_structA);
2801 /* Querying the same option through InternetQueryOptionW still results in
2802 * ASCII strings being returned.
2805 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2807 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2808 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
2809 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
2810 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2811 certificate_structW, &size);
2812 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
2813 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2816 ok(certificate_structA->lpszSubjectInfo &&
2817 strlen(certificate_structA->lpszSubjectInfo) > 1,
2818 "expected a non-empty subject name\n");
2819 ok(certificate_structA->lpszIssuerInfo &&
2820 strlen(certificate_structA->lpszIssuerInfo) > 1,
2821 "expected a non-empty issuer name\n");
2822 ok(!certificate_structA->lpszSignatureAlgName,
2823 "unexpected signature algorithm name\n");
2824 ok(!certificate_structA->lpszEncryptionAlgName,
2825 "unexpected encryption algorithm name\n");
2826 ok(!certificate_structA->lpszProtocolName,
2827 "unexpected protocol name\n");
2828 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2829 release_cert_info(certificate_structA);
2831 HeapFree(GetProcessHeap(), 0, certificate_structW);
2833 InternetCloseHandle(req);
2834 InternetCloseHandle(con);
2835 InternetCloseHandle(ses);
2837 /* Repeating the tests with the W functions has the same result: */
2838 ses = InternetOpenW(gizmo5, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2839 ok(ses != NULL, "InternetOpen failed\n");
2841 con = InternetConnectW(ses, testbot,
2842 INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL,
2843 INTERNET_SERVICE_HTTP, 0, 0);
2844 ok(con != NULL, "InternetConnect failed\n");
2846 req = HttpOpenRequestW(con, get, slash, NULL, NULL, NULL,
2847 INTERNET_FLAG_SECURE, 0);
2848 ok(req != NULL, "HttpOpenRequest failed\n");
2850 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2851 ok(ret, "HttpSendRequest failed: %d\n", GetLastError());
2853 size = sizeof(flags);
2854 ret = InternetQueryOption(req, INTERNET_OPTION_SECURITY_FLAGS, &flags, &size);
2855 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2856 ok(flags & SECURITY_FLAG_SECURE, "expected secure flag to be set\n");
2858 ret = InternetQueryOptionA(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2860 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2861 ok(size == sizeof(INTERNET_CERTIFICATE_INFOA), "size = %d\n", size);
2862 certificate_structA = HeapAlloc(GetProcessHeap(), 0, size);
2863 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2864 certificate_structA, &size);
2865 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2868 ok(certificate_structA->lpszSubjectInfo &&
2869 strlen(certificate_structA->lpszSubjectInfo) > 1,
2870 "expected a non-empty subject name\n");
2871 ok(certificate_structA->lpszIssuerInfo &&
2872 strlen(certificate_structA->lpszIssuerInfo) > 1,
2873 "expected a non-empty issuer name\n");
2874 ok(!certificate_structA->lpszSignatureAlgName,
2875 "unexpected signature algorithm name\n");
2876 ok(!certificate_structA->lpszEncryptionAlgName,
2877 "unexpected encryption algorithm name\n");
2878 ok(!certificate_structA->lpszProtocolName,
2879 "unexpected protocol name\n");
2880 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2881 release_cert_info(certificate_structA);
2883 HeapFree(GetProcessHeap(), 0, certificate_structA);
2885 /* Again, querying the same option through InternetQueryOptionW still
2886 * results in ASCII strings being returned.
2889 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2891 ok(ret || GetLastError() == ERROR_INSUFFICIENT_BUFFER, "InternetQueryOption failed: %d\n", GetLastError());
2892 ok(size == sizeof(INTERNET_CERTIFICATE_INFOW), "size = %d\n", size);
2893 certificate_structW = HeapAlloc(GetProcessHeap(), 0, size);
2894 ret = InternetQueryOptionW(req, INTERNET_OPTION_SECURITY_CERTIFICATE_STRUCT,
2895 certificate_structW, &size);
2896 certificate_structA = (INTERNET_CERTIFICATE_INFOA *)certificate_structW;
2897 ok(ret, "InternetQueryOption failed: %d\n", GetLastError());
2900 ok(certificate_structA->lpszSubjectInfo &&
2901 strlen(certificate_structA->lpszSubjectInfo) > 1,
2902 "expected a non-empty subject name\n");
2903 ok(certificate_structA->lpszIssuerInfo &&
2904 strlen(certificate_structA->lpszIssuerInfo) > 1,
2905 "expected a non-empty issuer name\n");
2906 ok(!certificate_structA->lpszSignatureAlgName,
2907 "unexpected signature algorithm name\n");
2908 ok(!certificate_structA->lpszEncryptionAlgName,
2909 "unexpected encryption algorithm name\n");
2910 ok(!certificate_structA->lpszProtocolName,
2911 "unexpected protocol name\n");
2912 ok(certificate_structA->dwKeySize, "expected a non-zero key size\n");
2913 release_cert_info(certificate_structA);
2915 HeapFree(GetProcessHeap(), 0, certificate_structW);
2917 InternetCloseHandle(req);
2918 InternetCloseHandle(con);
2919 InternetCloseHandle(ses);
2922 static void test_user_agent_header(void)
2924 HINTERNET ses, con, req;
2929 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
2930 ok(ses != NULL, "InternetOpen failed\n");
2932 con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2933 ok(con != NULL, "InternetConnect failed\n");
2935 req = HttpOpenRequest(con, "GET", "/tests/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2936 ok(req != NULL, "HttpOpenRequest failed\n");
2938 size = sizeof(buffer);
2939 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2940 err = GetLastError();
2941 ok(!ret, "HttpQueryInfo succeeded\n");
2942 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2944 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2945 ok(ret, "HttpAddRequestHeaders succeeded\n");
2947 size = sizeof(buffer);
2948 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2949 err = GetLastError();
2950 ok(ret, "HttpQueryInfo failed\n");
2951 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2953 InternetCloseHandle(req);
2955 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2956 ok(req != NULL, "HttpOpenRequest failed\n");
2958 size = sizeof(buffer);
2959 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2960 err = GetLastError();
2961 ok(!ret, "HttpQueryInfo succeeded\n");
2962 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2964 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2965 ok(ret, "HttpAddRequestHeaders failed\n");
2968 size = sizeof(buffer);
2969 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2970 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2971 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2973 InternetCloseHandle(req);
2974 InternetCloseHandle(con);
2975 InternetCloseHandle(ses);
2978 static void test_bogus_accept_types_array(void)
2980 HINTERNET ses, con, req;
2981 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2986 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2987 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2988 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2990 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2993 size = sizeof(buffer);
2994 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2995 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2996 ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2997 !strcmp(buffer, "*/*, %p, */*"),
2998 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
3000 InternetCloseHandle(req);
3001 InternetCloseHandle(con);
3002 InternetCloseHandle(ses);
3011 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
3013 INTERNET_ASYNC_RESULT *result = info;
3014 struct context *ctx = (struct context *)context;
3016 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
3018 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
3020 trace("request handle: 0x%08lx\n", result->dwResult);
3021 ctx->req = (HINTERNET)result->dwResult;
3022 SetEvent(ctx->event);
3024 if (status == INTERNET_STATUS_HANDLE_CLOSING)
3026 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
3028 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
3029 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
3030 SetEvent(ctx->event);
3034 static void test_open_url_async(void)
3043 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
3045 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
3046 ok(ses != NULL, "InternetOpen failed\n");
3048 SetLastError(0xdeadbeef);
3049 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
3050 error = GetLastError();
3051 ok(!ret, "InternetSetOptionA succeeded\n");
3052 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
3054 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
3055 error = GetLastError();
3056 ok(!ret, "InternetSetOptionA failed\n");
3057 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
3059 pInternetSetStatusCallbackA(ses, cb);
3060 ResetEvent(ctx.event);
3062 req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
3063 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
3065 WaitForSingleObject(ctx.event, INFINITE);
3068 size = sizeof(type);
3069 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
3070 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
3071 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
3072 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
3075 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
3076 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
3077 ok(size > 0, "expected size > 0\n");
3079 ResetEvent(ctx.event);
3080 InternetCloseHandle(ctx.req);
3081 WaitForSingleObject(ctx.event, INFINITE);
3083 InternetCloseHandle(ses);
3084 CloseHandle(ctx.event);
3089 internet_connect = 1,
3091 http_send_request_ex,
3094 internet_close_handle
3099 enum api function; /* api responsible for notification */
3100 unsigned int status; /* status received */
3101 int async; /* delivered from another thread? */
3109 const struct notification *test;
3117 static CRITICAL_SECTION notification_cs;
3119 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
3121 BOOL status_ok, function_ok;
3122 struct info *info = (struct info *)context;
3125 EnterCriticalSection( ¬ification_cs );
3127 if (status == INTERNET_STATUS_HANDLE_CREATED)
3129 DWORD size = sizeof(struct info *);
3130 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
3133 if (i >= info->count)
3135 LeaveCriticalSection( ¬ification_cs );
3139 while (info->test[i].status != status && info->test[i].optional &&
3140 i < info->count - 1 &&
3141 info->test[i].function == info->test[i + 1].function)
3146 status_ok = (info->test[i].status == status);
3147 function_ok = (info->test[i].function == info->function);
3149 if (!info->test[i].todo)
3151 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3152 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3154 if (info->test[i].async)
3155 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
3156 info->line, info->thread, GetCurrentThreadId());
3160 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
3162 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
3164 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
3167 LeaveCriticalSection( ¬ification_cs );
3170 static void setup_test( struct info *info, enum api function, unsigned int line )
3172 info->function = function;
3176 static const struct notification async_send_request_ex_test[] =
3178 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
3179 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
3180 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
3181 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1, 0, 1 },
3182 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1, 0, 1 },
3183 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
3184 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
3185 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
3186 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
3187 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3188 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, 0 },
3189 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, 0 },
3190 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
3191 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
3192 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
3193 { internet_close_handle, INTERNET_STATUS_CLOSING_CONNECTION, 0, 0, 1 },
3194 { internet_close_handle, INTERNET_STATUS_CONNECTION_CLOSED, 0, 0, 1 },
3195 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, },
3196 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, }
3199 static void test_async_HttpSendRequestEx(void)
3202 HINTERNET ses, req, con;
3204 DWORD size, written, error;
3205 INTERNET_BUFFERSA b;
3206 static const char *accept[2] = {"*/*", NULL};
3207 static char data[] = "Public ID=codeweavers";
3210 InitializeCriticalSection( ¬ification_cs );
3212 info.test = async_send_request_ex_test;
3213 info.count = sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]);
3215 info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
3216 info.thread = GetCurrentThreadId();
3218 ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
3219 ok( ses != NULL, "InternetOpen failed\n" );
3221 pInternetSetStatusCallbackA( ses, check_notification );
3223 setup_test( &info, internet_connect, __LINE__ );
3224 con = InternetConnect( ses, "crossover.codeweavers.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
3225 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
3227 WaitForSingleObject( info.wait, 10000 );
3229 setup_test( &info, http_open_request, __LINE__ );
3230 req = HttpOpenRequest( con, "POST", "posttest.php", NULL, NULL, accept, 0, (DWORD_PTR)&info );
3231 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
3233 WaitForSingleObject( info.wait, 10000 );
3235 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
3236 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
3237 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
3238 b.dwHeadersLength = strlen( b.lpcszHeader );
3239 b.dwBufferTotal = strlen( data );
3241 setup_test( &info, http_send_request_ex, __LINE__ );
3242 ret = HttpSendRequestExA( req, &b, NULL, 0x28, 0 );
3243 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
3245 WaitForSingleObject( info.wait, 10000 );
3247 size = sizeof(buffer);
3248 SetLastError( 0xdeadbeef );
3249 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
3250 error = GetLastError();
3251 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
3253 ok( error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
3254 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
3257 size = strlen( data );
3258 setup_test( &info, internet_writefile, __LINE__ );
3259 ret = InternetWriteFile( req, data, size, &written );
3260 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
3261 ok( written == size, "expected %u got %u\n", written, size );
3263 WaitForSingleObject( info.wait, 10000 );
3265 SetLastError( 0xdeadbeef );
3266 ret = HttpEndRequestA( req, (void *)data, 0x28, 0 );
3267 error = GetLastError();
3268 ok( !ret, "HttpEndRequestA succeeded\n" );
3269 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
3271 SetLastError( 0xdeadbeef );
3272 setup_test( &info, http_end_request, __LINE__ );
3273 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
3274 error = GetLastError();
3275 ok( !ret, "HttpEndRequestA succeeded\n" );
3276 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
3278 WaitForSingleObject( info.wait, 10000 );
3280 setup_test( &info, internet_close_handle, __LINE__ );
3281 InternetCloseHandle( req );
3282 InternetCloseHandle( con );
3283 InternetCloseHandle( ses );
3285 WaitForSingleObject( info.wait, 10000 );
3287 CloseHandle( info.wait );
3290 static HINTERNET closetest_session, closetest_req, closetest_conn;
3291 static BOOL closetest_closed;
3293 static void WINAPI closetest_callback(HINTERNET hInternet, DWORD_PTR dwContext, DWORD dwInternetStatus,
3294 LPVOID lpvStatusInformation, DWORD dwStatusInformationLength)
3299 trace("closetest_callback %p: %d\n", hInternet, dwInternetStatus);
3301 ok(hInternet == closetest_session || hInternet == closetest_conn || hInternet == closetest_req,
3302 "Unexpected hInternet %p\n", hInternet);
3303 if(!closetest_closed)
3307 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_HANDLE_TYPE, &type, &len);
3308 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3309 "InternetQueryOptionA(%p INTERNET_OPTION_HANDLE_TYPE) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3310 closetest_req, res, GetLastError());
3313 static void test_InternetCloseHandle(void)
3318 closetest_session = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, INTERNET_FLAG_ASYNC);
3319 ok(closetest_session != NULL,"InternetOpen failed with error %u\n", GetLastError());
3321 pInternetSetStatusCallbackA(closetest_session, closetest_callback);
3323 closetest_conn = InternetConnectA(closetest_session, "source.winehq.org", INTERNET_INVALID_PORT_NUMBER,
3324 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
3325 ok(closetest_conn != NULL,"InternetConnect failed with error %u\n", GetLastError());
3327 closetest_req = HttpOpenRequestA(closetest_conn, "GET", "winegecko.php", NULL, NULL, NULL,
3328 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE, 0xdeadbead);
3330 res = HttpSendRequestA(closetest_req, NULL, -1, NULL, 0);
3331 ok(!res && (GetLastError() == ERROR_IO_PENDING),
3332 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
3334 len = sizeof(flags);
3335 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3336 ok(res, "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %u\n", closetest_req, GetLastError());
3338 res = InternetCloseHandle(closetest_session);
3339 ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
3340 closetest_closed = TRUE;
3341 trace("Closed session handle\n");
3343 res = InternetCloseHandle(closetest_conn);
3344 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(conn) failed: %x %u\n",
3345 res, GetLastError());
3347 res = InternetCloseHandle(closetest_req);
3348 ok(!res && GetLastError() == ERROR_INVALID_HANDLE, "InternetCloseConnection(req) failed: %x %u\n",
3349 res, GetLastError());
3351 len = sizeof(flags);
3352 res = InternetQueryOptionA(closetest_req, INTERNET_OPTION_REQUEST_FLAGS, &flags, &len);
3353 ok(!res && GetLastError() == ERROR_INVALID_HANDLE,
3354 "InternetQueryOptionA(%p INTERNET_OPTION_URL) failed: %x %u, expected TRUE ERROR_INVALID_HANDLE\n",
3355 closetest_req, res, GetLastError());
3358 static void init_status_tests(void)
3360 memset(expect, 0, sizeof(expect));
3361 memset(optional, 0, sizeof(optional));
3362 memset(wine_allow, 0, sizeof(wine_allow));
3363 memset(notified, 0, sizeof(notified));
3364 memset(status_string, 0, sizeof(status_string));
3366 #define STATUS_STRING(status) status_string[status] = #status
3367 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
3368 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
3369 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
3370 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
3371 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
3372 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
3373 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
3374 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
3375 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
3376 STATUS_STRING(INTERNET_STATUS_PREFETCH);
3377 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
3378 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
3379 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
3380 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
3381 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
3382 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
3383 STATUS_STRING(INTERNET_STATUS_REDIRECT);
3384 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
3385 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
3386 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
3387 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
3388 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
3389 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
3390 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
3391 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
3392 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
3393 #undef STATUS_STRING
3399 hdll = GetModuleHandleA("wininet.dll");
3401 if(!GetProcAddress(hdll, "InternetGetCookieExW")) {
3402 win_skip("Too old IE (older than 6.0)\n");
3406 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
3408 init_status_tests();
3409 if(0)test_InternetCloseHandle();
3410 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
3411 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
3412 InternetReadFile_test(0, &test_data[1]);
3413 first_connection_to_test_url = TRUE;
3414 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[2]);
3415 InternetReadFile_test(0, &test_data[2]);
3416 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
3417 test_open_url_async();
3418 test_async_HttpSendRequestEx();
3419 InternetOpenRequest_test();
3421 InternetOpenUrlA_test();
3423 test_http_connection();
3424 test_secure_connection();
3425 test_user_agent_header();
3426 test_bogus_accept_types_array();
3427 InternetReadFile_chunked_test();
3428 HttpSendRequestEx_test();
3429 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[3]);