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/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][0] != 0 ? \
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][0] != 0 ? \
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][0] != 0 ? \
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 #define MAX_STATUS_NAME 50
102 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
103 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
104 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
106 static HANDLE hCompleteEvent;
108 #define TESTF_REDIRECT 0x01
109 #define TESTF_COMPRESSED 0x02
110 #define TESTF_ALLOW_COOKIE 0x04
114 const char *redirected_url;
120 static const test_data_t test_data[] = {
122 "http://test.winehq.org/testredirect",
123 "http://test.winehq.org/hello.html",
129 "http://www.codeweavers.com/",
130 "http://www.codeweavers.com/",
131 "www.codeweavers.com",
133 TESTF_COMPRESSED|TESTF_ALLOW_COOKIE
137 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
140 static VOID WINAPI callback(
143 DWORD dwInternetStatus,
144 LPVOID lpvStatusInformation,
145 DWORD dwStatusInformationLength
148 CHECK_EXPECT(dwInternetStatus);
149 switch (dwInternetStatus)
151 case INTERNET_STATUS_RESOLVING_NAME:
152 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
153 GetCurrentThreadId(), hInternet, dwContext,
154 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
155 *(LPSTR)lpvStatusInformation = '\0';
157 case INTERNET_STATUS_NAME_RESOLVED:
158 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
159 GetCurrentThreadId(), hInternet, dwContext,
160 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
161 *(LPSTR)lpvStatusInformation = '\0';
163 case INTERNET_STATUS_CONNECTING_TO_SERVER:
164 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
165 GetCurrentThreadId(), hInternet, dwContext,
166 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
167 *(LPSTR)lpvStatusInformation = '\0';
169 case INTERNET_STATUS_CONNECTED_TO_SERVER:
170 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
171 GetCurrentThreadId(), hInternet, dwContext,
172 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
173 *(LPSTR)lpvStatusInformation = '\0';
175 case INTERNET_STATUS_SENDING_REQUEST:
176 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
177 GetCurrentThreadId(), hInternet, dwContext,
178 lpvStatusInformation,dwStatusInformationLength);
180 case INTERNET_STATUS_REQUEST_SENT:
181 ok(dwStatusInformationLength == sizeof(DWORD),
182 "info length should be sizeof(DWORD) instead of %d\n",
183 dwStatusInformationLength);
184 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
185 GetCurrentThreadId(), hInternet, dwContext,
186 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
188 case INTERNET_STATUS_RECEIVING_RESPONSE:
189 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
190 GetCurrentThreadId(), hInternet, dwContext,
191 lpvStatusInformation,dwStatusInformationLength);
193 case INTERNET_STATUS_RESPONSE_RECEIVED:
194 ok(dwStatusInformationLength == sizeof(DWORD),
195 "info length should be sizeof(DWORD) instead of %d\n",
196 dwStatusInformationLength);
197 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
198 GetCurrentThreadId(), hInternet, dwContext,
199 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
201 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
202 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
203 GetCurrentThreadId(), hInternet,dwContext,
204 lpvStatusInformation,dwStatusInformationLength);
206 case INTERNET_STATUS_PREFETCH:
207 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
208 GetCurrentThreadId(), hInternet, dwContext,
209 lpvStatusInformation,dwStatusInformationLength);
211 case INTERNET_STATUS_CLOSING_CONNECTION:
212 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
213 GetCurrentThreadId(), hInternet, dwContext,
214 lpvStatusInformation,dwStatusInformationLength);
216 case INTERNET_STATUS_CONNECTION_CLOSED:
217 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
218 GetCurrentThreadId(), hInternet, dwContext,
219 lpvStatusInformation,dwStatusInformationLength);
221 case INTERNET_STATUS_HANDLE_CREATED:
222 ok(dwStatusInformationLength == sizeof(HINTERNET),
223 "info length should be sizeof(HINTERNET) instead of %d\n",
224 dwStatusInformationLength);
225 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
226 GetCurrentThreadId(), hInternet, dwContext,
227 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
228 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
229 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
231 case INTERNET_STATUS_HANDLE_CLOSING:
232 ok(dwStatusInformationLength == sizeof(HINTERNET),
233 "info length should be sizeof(HINTERNET) instead of %d\n",
234 dwStatusInformationLength);
235 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
236 GetCurrentThreadId(), hInternet, dwContext,
237 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
239 case INTERNET_STATUS_REQUEST_COMPLETE:
241 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
242 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
243 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
244 dwStatusInformationLength);
245 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
246 GetCurrentThreadId(), hInternet, dwContext,
247 iar->dwResult,iar->dwError,dwStatusInformationLength);
248 SetEvent(hCompleteEvent);
251 case INTERNET_STATUS_REDIRECT:
252 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
253 GetCurrentThreadId(), hInternet, dwContext,
254 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
255 *(LPSTR)lpvStatusInformation = '\0';
256 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
257 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
259 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
260 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
261 GetCurrentThreadId(), hInternet, dwContext,
262 lpvStatusInformation, dwStatusInformationLength);
265 trace("%04x:Callback %p 0x%lx %d %p %d\n",
266 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
267 lpvStatusInformation, dwStatusInformationLength);
271 static void InternetReadFile_test(int flags, const test_data_t *test)
277 const char *types[2] = { "*", NULL };
278 HINTERNET hi, hic = 0, hor = 0;
280 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
282 trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
284 trace("InternetOpenA <--\n");
285 hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
286 INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
287 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
288 trace("InternetOpenA -->\n");
290 if (hi == 0x0) goto abort;
292 pInternetSetStatusCallbackA(hi,&callback);
294 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
296 trace("InternetConnectA <--\n");
297 hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
298 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
299 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
300 trace("InternetConnectA -->\n");
302 if (hic == 0x0) goto abort;
304 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
305 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
307 trace("HttpOpenRequestA <--\n");
308 hor = HttpOpenRequestA(hic, "GET", test->path, NULL, NULL, types,
309 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
311 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
313 * If the internet name can't be resolved we are probably behind
314 * a firewall or in some other way not directly connected to the
315 * Internet. Not enough reason to fail the test. Just ignore and
319 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
321 trace("HttpOpenRequestA -->\n");
323 if (hor == 0x0) goto abort;
325 length = sizeof(buffer);
326 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
327 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
328 ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
330 length = sizeof(buffer);
331 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
332 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
333 ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
334 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
336 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
337 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
338 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
339 if(test->flags & TESTF_ALLOW_COOKIE) {
340 SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
341 SET_OPTIONAL(INTERNET_STATUS_COOKIE_RECEIVED);
343 if (first_connection_to_test_url)
345 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
346 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
347 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
348 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
352 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
353 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
355 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
356 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
357 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
358 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
359 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
360 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
361 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
362 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
363 if(test->flags & TESTF_REDIRECT) {
364 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
365 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
367 SET_EXPECT(INTERNET_STATUS_REDIRECT);
368 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
369 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
370 if (flags & INTERNET_FLAG_ASYNC)
371 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
373 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
375 if(test->flags & TESTF_COMPRESSED) {
378 res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
379 ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
380 "InternetSetOption failed: %u\n", GetLastError());
385 trace("HttpSendRequestA -->\n");
386 SetLastError(0xdeadbeef);
387 res = HttpSendRequestA(hor, (test->flags & TESTF_COMPRESSED) ? "Accept-Encoding: gzip, deflate" : "", -1, NULL, 0);
388 if (flags & INTERNET_FLAG_ASYNC)
389 ok(!res && (GetLastError() == ERROR_IO_PENDING),
390 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
392 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
393 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
394 trace("HttpSendRequestA <--\n");
396 if (flags & INTERNET_FLAG_ASYNC)
397 WaitForSingleObject(hCompleteEvent, INFINITE);
399 if(test->flags & TESTF_ALLOW_COOKIE) {
400 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
401 CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
403 if (first_connection_to_test_url)
405 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
406 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
410 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
411 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
413 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
414 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
415 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
416 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
417 if(test->flags & TESTF_REDIRECT)
418 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
419 if (flags & INTERNET_FLAG_ASYNC)
420 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
422 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
423 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
424 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
425 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
428 res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
429 ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
432 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
433 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
435 length = sizeof(buffer);
436 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
437 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
440 length = sizeof(buffer);
441 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
442 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
443 ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
446 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
447 trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
448 if(test->flags & TESTF_COMPRESSED)
449 ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
450 "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
453 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
455 trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
458 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
460 trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
462 SetLastError(0xdeadbeef);
463 res = InternetReadFile(NULL, buffer, 100, &length);
464 ok(!res, "InternetReadFile should have failed\n");
465 ok(GetLastError() == ERROR_INVALID_HANDLE,
466 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
470 trace("Entering Query loop\n");
474 if (flags & INTERNET_FLAG_ASYNC)
475 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
476 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
477 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
478 ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
479 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
480 if (flags & INTERNET_FLAG_ASYNC)
484 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
486 else if (GetLastError() == ERROR_IO_PENDING)
488 WaitForSingleObject(hCompleteEvent, INFINITE);
489 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
496 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
498 res = InternetReadFile(hor,buffer,length,&length);
502 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
504 HeapFree(GetProcessHeap(),0,buffer);
509 if(test->flags & TESTF_REDIRECT) {
510 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
511 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
515 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
517 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
518 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
519 SetLastError(0xdeadbeef);
521 res = InternetCloseHandle(hor);
522 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
523 SetLastError(0xdeadbeef);
524 res = InternetCloseHandle(hor);
525 ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
526 ok (GetLastError() == ERROR_INVALID_HANDLE,
527 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
530 /* We intentionally do not close the handle opened by InternetConnectA as this
531 * tickles bug #9479: native closes child internet handles when the parent handles
532 * are closed. This is verified below by checking that the number of
533 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
535 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
536 trace("closing 2\n");
537 res = InternetCloseHandle(hi);
538 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
539 if (flags & INTERNET_FLAG_ASYNC)
542 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
543 if (hor != 0x0) todo_wine
545 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
546 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
550 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
551 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
553 CloseHandle(hCompleteEvent);
554 first_connection_to_test_url = FALSE;
557 static void InternetReadFile_chunked_test(void)
562 const char *types[2] = { "*", NULL };
563 HINTERNET hi, hic = 0, hor = 0;
565 trace("Starting InternetReadFile chunked test\n");
567 trace("InternetOpenA <--\n");
568 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
569 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
570 trace("InternetOpenA -->\n");
572 if (hi == 0x0) goto abort;
574 trace("InternetConnectA <--\n");
575 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
576 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
577 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
578 trace("InternetConnectA -->\n");
580 if (hic == 0x0) goto abort;
582 trace("HttpOpenRequestA <--\n");
583 hor = HttpOpenRequestA(hic, "GET", "/testchunked", NULL, NULL, types,
584 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
586 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
588 * If the internet name can't be resolved we are probably behind
589 * a firewall or in some other way not directly connected to the
590 * Internet. Not enough reason to fail the test. Just ignore and
594 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
596 trace("HttpOpenRequestA -->\n");
598 if (hor == 0x0) goto abort;
600 trace("HttpSendRequestA -->\n");
601 SetLastError(0xdeadbeef);
602 res = HttpSendRequestA(hor, "", -1, NULL, 0);
603 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
604 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
605 trace("HttpSendRequestA <--\n");
608 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
610 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
612 SetLastError( 0xdeadbeef );
614 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
616 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
617 ok( res, "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
618 ok( !strcmp( buffer, "chunked" ), "Wrong transfer encoding '%s'\n", buffer );
620 SetLastError( 0xdeadbeef );
622 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
623 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
624 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
627 trace("Entering Query loop\n");
631 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
632 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
633 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
634 trace("got %u available\n",length);
638 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
640 res = InternetReadFile(hor,buffer,length,&got);
643 trace("ReadFile -> %i %i\n",res,got);
644 ok( length == got, "only got %u of %u available\n", got, length );
645 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
647 HeapFree(GetProcessHeap(),0,buffer);
656 res = InternetCloseHandle(hor);
657 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
660 res = InternetCloseHandle(hi);
661 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
665 static void InternetReadFileExA_test(int flags)
669 const char *types[2] = { "*", NULL };
670 HINTERNET hi, hic = 0, hor = 0;
671 INTERNET_BUFFERS inetbuffers;
673 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
675 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
677 trace("InternetOpenA <--\n");
678 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
679 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
680 trace("InternetOpenA -->\n");
682 if (hi == 0x0) goto abort;
684 pInternetSetStatusCallbackA(hi,&callback);
686 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
688 trace("InternetConnectA <--\n");
689 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
690 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
691 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
692 trace("InternetConnectA -->\n");
694 if (hic == 0x0) goto abort;
696 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
697 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
699 trace("HttpOpenRequestA <--\n");
700 hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
701 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
703 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
705 * If the internet name can't be resolved we are probably behind
706 * a firewall or in some other way not directly connected to the
707 * Internet. Not enough reason to fail the test. Just ignore and
711 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
713 trace("HttpOpenRequestA -->\n");
715 if (hor == 0x0) goto abort;
717 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
718 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
719 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
720 if (first_connection_to_test_url)
722 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
723 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
724 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
725 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
729 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
730 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
732 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
733 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
734 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
735 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
736 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
737 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
738 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
739 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
740 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
741 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
742 SET_EXPECT(INTERNET_STATUS_REDIRECT);
743 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
744 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
745 if (flags & INTERNET_FLAG_ASYNC)
746 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
748 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
750 trace("HttpSendRequestA -->\n");
751 SetLastError(0xdeadbeef);
752 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
753 if (flags & INTERNET_FLAG_ASYNC)
754 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
755 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
757 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
758 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
759 trace("HttpSendRequestA <--\n");
761 if (!rc && (GetLastError() == ERROR_IO_PENDING))
762 WaitForSingleObject(hCompleteEvent, INFINITE);
764 if (first_connection_to_test_url)
766 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
767 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
771 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
772 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
774 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
775 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
776 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
777 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
778 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
779 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
780 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
781 if (flags & INTERNET_FLAG_ASYNC)
782 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
784 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
785 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
786 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
787 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
789 /* tests invalid dwStructSize */
790 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
791 inetbuffers.lpcszHeader = NULL;
792 inetbuffers.dwHeadersLength = 0;
793 inetbuffers.dwBufferLength = 10;
794 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
795 inetbuffers.dwOffsetHigh = 1234;
796 inetbuffers.dwOffsetLow = 5678;
797 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
798 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
799 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
800 rc ? "TRUE" : "FALSE", GetLastError());
801 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
803 /* tests to see whether lpcszHeader is used - it isn't */
804 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
805 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
806 inetbuffers.dwHeadersLength = 255;
807 inetbuffers.dwBufferLength = 0;
808 inetbuffers.lpvBuffer = NULL;
809 inetbuffers.dwOffsetHigh = 1234;
810 inetbuffers.dwOffsetLow = 5678;
811 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
812 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
813 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
814 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
815 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
816 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
817 trace("read %i bytes\n", inetbuffers.dwBufferLength);
820 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
821 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
824 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
825 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
826 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
827 rc ? "TRUE" : "FALSE", GetLastError());
830 trace("Entering Query loop\n");
832 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
833 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
836 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
837 inetbuffers.dwBufferLength = 1024;
838 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
839 inetbuffers.dwOffsetHigh = 1234;
840 inetbuffers.dwOffsetLow = 5678;
842 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
843 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
844 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
845 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
846 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
847 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
850 if (GetLastError() == ERROR_IO_PENDING)
852 trace("InternetReadFileEx -> PENDING\n");
853 ok(flags & INTERNET_FLAG_ASYNC,
854 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
855 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
856 WaitForSingleObject(hCompleteEvent, INFINITE);
857 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
858 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
862 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
868 trace("InternetReadFileEx -> SUCCEEDED\n");
869 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
870 if (inetbuffers.dwBufferLength)
873 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
874 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
879 /* Win98 still sends these when 0 bytes are read, WinXP does not */
880 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
881 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
885 trace("read %i bytes\n", inetbuffers.dwBufferLength);
886 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
888 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
889 "InternetReadFileEx sets offsets to 0x%x%08x\n",
890 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
892 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
894 if (!inetbuffers.dwBufferLength)
897 length += inetbuffers.dwBufferLength;
899 ok(length > 0, "failed to read any of the document\n");
900 trace("Finished. Read %d bytes\n", length);
902 /* WinXP does not send, but Win98 does */
903 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
904 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
906 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
908 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
909 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
910 rc = InternetCloseHandle(hor);
911 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
912 rc = InternetCloseHandle(hor);
913 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
916 rc = InternetCloseHandle(hic);
917 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
920 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
921 rc = InternetCloseHandle(hi);
922 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
923 if (flags & INTERNET_FLAG_ASYNC)
925 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
927 /* to enable once Wine is fixed to never send it
928 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
929 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
931 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
932 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
933 CloseHandle(hCompleteEvent);
934 first_connection_to_test_url = FALSE;
937 static void InternetOpenUrlA_test(void)
939 HINTERNET myhinternet, myhttp;
941 DWORD size, readbytes, totalbytes=0;
944 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
945 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
947 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
948 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
951 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
952 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
953 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
954 return; /* WinXP returns this when not connected to the net */
955 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
956 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
957 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
958 totalbytes += readbytes;
959 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
960 totalbytes += readbytes;
961 trace("read 0x%08x bytes\n",totalbytes);
963 InternetCloseHandle(myhttp);
964 InternetCloseHandle(myhinternet);
967 static void HttpSendRequestEx_test(void)
973 INTERNET_BUFFERS BufferIn;
974 DWORD dwBytesWritten;
980 static char szPostData[] = "mode=Test";
981 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
983 hSession = InternetOpen("Wine Regression Test",
984 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
985 ok( hSession != NULL ,"Unable to open Internet session\n");
986 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
987 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
989 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
990 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
991 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
992 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
994 skip( "Network unreachable, skipping test\n" );
997 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1000 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1001 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1002 BufferIn.lpcszHeader = szContentType;
1003 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1004 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1005 BufferIn.lpvBuffer = szPostData;
1006 BufferIn.dwBufferLength = 3;
1007 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1008 BufferIn.dwOffsetLow = 0;
1009 BufferIn.dwOffsetHigh = 0;
1011 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1012 ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
1014 for (i = 3; szPostData[i]; i++)
1015 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1016 "InternetWriteFile failed\n");
1018 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1020 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1021 "Unable to read response\n");
1022 szBuffer[dwBytesRead] = 0;
1024 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1025 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
1027 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1029 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1030 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1033 static void InternetOpenRequest_test(void)
1035 HINTERNET session, connect, request;
1036 static const char *types[] = { "*", "", NULL };
1037 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1038 static const WCHAR *typesW[] = { any, empty, NULL };
1041 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1042 ok(session != NULL ,"Unable to open Internet session\n");
1044 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1045 INTERNET_SERVICE_HTTP, 0, 0);
1046 ok(connect == NULL, "InternetConnectA should have failed\n");
1047 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1049 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1050 INTERNET_SERVICE_HTTP, 0, 0);
1051 ok(connect == NULL, "InternetConnectA should have failed\n");
1052 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1054 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1055 INTERNET_SERVICE_HTTP, 0, 0);
1056 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1058 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1059 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1061 skip( "Network unreachable, skipping test\n" );
1064 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1066 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1067 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1068 ok(InternetCloseHandle(request), "Close request handle failed\n");
1070 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1071 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1073 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1074 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1075 ok(InternetCloseHandle(request), "Close request handle failed\n");
1078 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1079 ok(InternetCloseHandle(session), "Close session handle failed\n");
1082 static void test_http_cache(void)
1084 HINTERNET session, connect, request;
1085 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1086 DWORD size, file_size;
1091 static const char *types[] = { "*", "", NULL };
1093 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1094 ok(session != NULL ,"Unable to open Internet session\n");
1096 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1097 INTERNET_SERVICE_HTTP, 0, 0);
1098 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1100 request = HttpOpenRequestA(connect, NULL, "/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1101 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1103 skip( "Network unreachable, skipping test\n" );
1105 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1106 ok(InternetCloseHandle(session), "Close session handle failed\n");
1110 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1113 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1114 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1115 ok(!strcmp(url, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", url);
1117 size = sizeof(file_name);
1118 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1119 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1120 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1121 ok(!size, "size = %d\n", size);
1123 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1124 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1126 size = sizeof(file_name);
1127 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1128 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1130 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1131 FILE_ATTRIBUTE_NORMAL, NULL);
1132 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1133 file_size = GetFileSize(file, NULL);
1134 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1137 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1138 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1139 ok(size == 100, "size = %u\n", size);
1141 file_size = GetFileSize(file, NULL);
1142 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1145 ok(InternetCloseHandle(request), "Close request handle failed\n");
1147 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1148 FILE_ATTRIBUTE_NORMAL, NULL);
1149 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1152 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1153 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1155 size = sizeof(file_name);
1156 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1157 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1158 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1159 ok(!size, "size = %d\n", size);
1161 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1162 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1164 size = sizeof(file_name);
1166 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1169 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1170 FILE_ATTRIBUTE_NORMAL, NULL);
1171 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1177 ok(file_name[0] == 0, "Didn't expect a file name\n");
1180 ok(InternetCloseHandle(request), "Close request handle failed\n");
1181 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1182 ok(InternetCloseHandle(session), "Close session handle failed\n");
1185 static void HttpHeaders_test(void)
1196 hSession = InternetOpen("Wine Regression Test",
1197 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1198 ok( hSession != NULL ,"Unable to open Internet session\n");
1199 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1200 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1202 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1203 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1204 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1205 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1207 skip( "Network unreachable, skipping test\n" );
1210 ok( hRequest != NULL, "Failed to open request handle\n");
1213 len = sizeof(buffer);
1214 strcpy(buffer,"Warning");
1215 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1216 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1218 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1219 "Failed to add new header\n");
1222 len = sizeof(buffer);
1223 strcpy(buffer,"Warning");
1224 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1225 buffer,&len,&index),"Unable to query header\n");
1226 ok(index == 1, "Index was not incremented\n");
1227 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1228 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1229 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1230 len = sizeof(buffer);
1231 strcpy(buffer,"Warning");
1232 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1233 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1236 len = 5; /* could store the string but not the NULL terminator */
1237 strcpy(buffer,"Warning");
1238 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1239 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1240 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1241 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1243 /* a call with NULL will fail but will return the length */
1245 len = sizeof(buffer);
1246 SetLastError(0xdeadbeef);
1247 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1248 NULL,&len,&index) == FALSE,"Query worked\n");
1249 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1250 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1251 ok(index == 0, "Index was incremented\n");
1253 /* even for a len that is too small */
1256 SetLastError(0xdeadbeef);
1257 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1258 NULL,&len,&index) == FALSE,"Query worked\n");
1259 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1260 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1261 ok(index == 0, "Index was incremented\n");
1265 SetLastError(0xdeadbeef);
1266 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1267 NULL,&len,&index) == FALSE,"Query worked\n");
1268 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1269 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1270 ok(index == 0, "Index was incremented\n");
1271 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1274 /* a working query */
1276 len = sizeof(buffer);
1277 memset(buffer, 'x', sizeof(buffer));
1278 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1279 buffer,&len,&index),"Unable to query header\n");
1280 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1281 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1282 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1283 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1284 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1285 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1286 ok(index == 0, "Index was incremented\n");
1288 /* Like above two tests, but for W version */
1292 SetLastError(0xdeadbeef);
1293 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1294 NULL,&len,&index) == FALSE,"Query worked\n");
1295 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1296 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1297 ok(index == 0, "Index was incremented\n");
1298 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1300 /* a working query */
1302 len = sizeof(wbuffer);
1303 memset(wbuffer, 'x', sizeof(wbuffer));
1304 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1305 wbuffer,&len,&index),"Unable to query header\n");
1306 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1307 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1308 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1309 ok(index == 0, "Index was incremented\n");
1311 /* end of W version tests */
1313 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1315 len = sizeof(buffer);
1316 memset(buffer, 'x', sizeof(buffer));
1317 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1318 buffer,&len,&index) == TRUE,"Query failed\n");
1319 ok(len == 2, "Expected 2, got %d\n", len);
1320 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1321 ok(index == 0, "Index was incremented\n");
1323 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1324 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1327 len = sizeof(buffer);
1328 strcpy(buffer,"Warning");
1329 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1330 buffer,&len,&index),"Unable to query header\n");
1331 ok(index == 1, "Index was not incremented\n");
1332 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1333 len = sizeof(buffer);
1334 strcpy(buffer,"Warning");
1335 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1336 buffer,&len,&index),"Failed to get second header\n");
1337 ok(index == 2, "Index was not incremented\n");
1338 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1339 len = sizeof(buffer);
1340 strcpy(buffer,"Warning");
1341 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1342 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1344 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1347 len = sizeof(buffer);
1348 strcpy(buffer,"Warning");
1349 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1350 buffer,&len,&index),"Unable to query header\n");
1351 ok(index == 1, "Index was not incremented\n");
1352 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1353 len = sizeof(buffer);
1354 strcpy(buffer,"Warning");
1355 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1356 buffer,&len,&index),"Failed to get second header\n");
1357 ok(index == 2, "Index was not incremented\n");
1358 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1359 len = sizeof(buffer);
1360 strcpy(buffer,"Warning");
1361 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1362 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1364 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1367 len = sizeof(buffer);
1368 strcpy(buffer,"Warning");
1369 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1370 buffer,&len,&index),"Unable to query header\n");
1371 ok(index == 1, "Index was not incremented\n");
1372 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1373 len = sizeof(buffer);
1374 strcpy(buffer,"Warning");
1375 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1376 buffer,&len,&index),"Failed to get second header\n");
1377 ok(index == 2, "Index was not incremented\n");
1378 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1379 len = sizeof(buffer);
1380 strcpy(buffer,"Warning");
1381 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1382 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1384 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1387 len = sizeof(buffer);
1388 strcpy(buffer,"Warning");
1389 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1390 buffer,&len,&index),"Unable to query header\n");
1391 ok(index == 1, "Index was not incremented\n");
1392 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1393 len = sizeof(buffer);
1394 strcpy(buffer,"Warning");
1395 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1396 ok(index == 2, "Index was not incremented\n");
1397 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1398 len = sizeof(buffer);
1399 strcpy(buffer,"Warning");
1400 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1402 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1405 len = sizeof(buffer);
1406 strcpy(buffer,"Warning");
1407 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1408 ok(index == 1, "Index was not incremented\n");
1409 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1410 len = sizeof(buffer);
1411 strcpy(buffer,"Warning");
1412 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1413 ok(index == 2, "Index was not incremented\n");
1414 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1415 len = sizeof(buffer);
1416 strcpy(buffer,"Warning");
1417 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1419 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1422 len = sizeof(buffer);
1423 strcpy(buffer,"Warning");
1424 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1425 ok(index == 1, "Index was not incremented\n");
1426 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1427 len = sizeof(buffer);
1428 strcpy(buffer,"Warning");
1429 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1430 ok(index == 2, "Index was not incremented\n");
1431 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1432 len = sizeof(buffer);
1433 strcpy(buffer,"Warning");
1434 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1436 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");
1439 len = sizeof(buffer);
1440 strcpy(buffer,"Warning");
1441 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1442 ok(index == 1, "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, buffer,&len,&index),"Failed to get second header\n");
1447 ok(index == 2, "Index was not incremented\n");
1448 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1449 len = sizeof(buffer);
1450 strcpy(buffer,"Warning");
1451 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1453 /* Ensure that blank headers are ignored and don't cause a failure */
1454 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");
1457 len = sizeof(buffer);
1458 strcpy(buffer,"BlankTest");
1459 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1460 ok(index == 1, "Index was not incremented\n");
1461 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1463 /* Ensure that malformed header separators are ignored and don't cause a failure */
1464 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),
1465 "Failed to add header with malformed entries in list\n");
1468 len = sizeof(buffer);
1469 strcpy(buffer,"MalformedTest");
1470 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1471 ok(index == 1, "Index was not incremented\n");
1472 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1474 len = sizeof(buffer);
1475 strcpy(buffer,"MalformedTestTwo");
1476 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1477 ok(index == 1, "Index was not incremented\n");
1478 ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1480 len = sizeof(buffer);
1481 strcpy(buffer,"MalformedTestThree");
1482 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1483 ok(index == 1, "Index was not incremented\n");
1484 ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1486 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1488 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1489 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1492 static const char garbagemsg[] =
1493 "Garbage: Header\r\n";
1495 static const char contmsg[] =
1496 "HTTP/1.1 100 Continue\r\n";
1498 static const char expandcontmsg[] =
1499 "HTTP/1.1 100 Continue\r\n"
1500 "Server: winecontinue\r\n"
1501 "Tag: something witty\r\n"
1504 static const char okmsg[] =
1505 "HTTP/1.1 200 OK\r\n"
1506 "Server: winetest\r\n"
1509 static const char okmsg2[] =
1510 "HTTP/1.1 200 OK\r\n"
1511 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1512 "Server: winetest\r\n"
1513 "Content-Length: 0\r\n"
1514 "Set-Cookie: one\r\n"
1515 "Set-Cookie: two\r\n"
1518 static const char notokmsg[] =
1519 "HTTP/1.1 400 Bad Request\r\n"
1520 "Server: winetest\r\n"
1523 static const char noauthmsg[] =
1524 "HTTP/1.1 401 Unauthorized\r\n"
1525 "Server: winetest\r\n"
1526 "Connection: close\r\n"
1527 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1530 static const char noauthmsg2[] =
1531 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1532 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1534 "Server: winetest\r\n";
1536 static const char proxymsg[] =
1537 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1538 "Server: winetest\r\n"
1539 "Proxy-Connection: close\r\n"
1540 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1543 static const char page1[] =
1545 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1546 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1549 struct server_info {
1554 static DWORD CALLBACK server_thread(LPVOID param)
1556 struct server_info *si = param;
1559 struct sockaddr_in sa;
1562 int last_request = 0;
1563 char host_header[22];
1564 static int test_b = 0;
1566 WSAStartup(MAKEWORD(1,1), &wsaData);
1568 s = socket(AF_INET, SOCK_STREAM, 0);
1569 if (s == INVALID_SOCKET)
1573 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1575 memset(&sa, 0, sizeof sa);
1576 sa.sin_family = AF_INET;
1577 sa.sin_port = htons(si->port);
1578 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1580 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1586 SetEvent(si->hEvent);
1588 sprintf(host_header, "Host: localhost:%d", si->port);
1592 c = accept(s, NULL, NULL);
1594 memset(buffer, 0, sizeof buffer);
1595 for(i=0; i<(sizeof buffer-1); i++)
1597 r = recv(c, &buffer[i], 1, 0);
1601 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1602 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1605 if (strstr(buffer, "GET /test1"))
1607 if (!strstr(buffer, "Content-Length: 0"))
1609 send(c, okmsg, sizeof okmsg-1, 0);
1610 send(c, page1, sizeof page1-1, 0);
1613 send(c, notokmsg, sizeof notokmsg-1, 0);
1615 if (strstr(buffer, "/test2"))
1617 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1619 send(c, okmsg, sizeof okmsg-1, 0);
1620 send(c, page1, sizeof page1-1, 0);
1623 send(c, proxymsg, sizeof proxymsg-1, 0);
1625 if (strstr(buffer, "/test3"))
1627 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1628 send(c, okmsg, sizeof okmsg-1, 0);
1630 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1632 if (strstr(buffer, "/test4"))
1634 if (strstr(buffer, "Connection: Close"))
1635 send(c, okmsg, sizeof okmsg-1, 0);
1637 send(c, notokmsg, sizeof notokmsg-1, 0);
1639 if (strstr(buffer, "POST /test5") ||
1640 strstr(buffer, "RPC_IN_DATA /test5") ||
1641 strstr(buffer, "RPC_OUT_DATA /test5"))
1643 if (strstr(buffer, "Content-Length: 0"))
1645 send(c, okmsg, sizeof okmsg-1, 0);
1646 send(c, page1, sizeof page1-1, 0);
1649 send(c, notokmsg, sizeof notokmsg-1, 0);
1651 if (strstr(buffer, "GET /test6"))
1653 send(c, contmsg, sizeof contmsg-1, 0);
1654 send(c, contmsg, sizeof contmsg-1, 0);
1655 send(c, okmsg, sizeof okmsg-1, 0);
1656 send(c, page1, sizeof page1-1, 0);
1658 if (strstr(buffer, "POST /test7"))
1660 if (strstr(buffer, "Content-Length: 100"))
1662 send(c, okmsg, sizeof okmsg-1, 0);
1663 send(c, page1, sizeof page1-1, 0);
1666 send(c, notokmsg, sizeof notokmsg-1, 0);
1668 if (strstr(buffer, "/test8"))
1670 if (!strstr(buffer, "Connection: Close") &&
1671 strstr(buffer, "Connection: Keep-Alive") &&
1672 !strstr(buffer, "Cache-Control: no-cache") &&
1673 !strstr(buffer, "Pragma: no-cache") &&
1674 strstr(buffer, host_header))
1675 send(c, okmsg, sizeof okmsg-1, 0);
1677 send(c, notokmsg, sizeof notokmsg-1, 0);
1679 if (strstr(buffer, "/test9"))
1681 if (!strstr(buffer, "Connection: Close") &&
1682 !strstr(buffer, "Connection: Keep-Alive") &&
1683 !strstr(buffer, "Cache-Control: no-cache") &&
1684 !strstr(buffer, "Pragma: no-cache") &&
1685 strstr(buffer, host_header))
1686 send(c, okmsg, sizeof okmsg-1, 0);
1688 send(c, notokmsg, sizeof notokmsg-1, 0);
1690 if (strstr(buffer, "/testA"))
1692 if (!strstr(buffer, "Connection: Close") &&
1693 !strstr(buffer, "Connection: Keep-Alive") &&
1694 (strstr(buffer, "Cache-Control: no-cache") ||
1695 strstr(buffer, "Pragma: no-cache")) &&
1696 strstr(buffer, host_header))
1697 send(c, okmsg, sizeof okmsg-1, 0);
1699 send(c, notokmsg, sizeof notokmsg-1, 0);
1701 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1704 send(c, okmsg, sizeof okmsg-1, 0);
1705 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1706 send(c, okmsg, sizeof okmsg-1, 0);
1708 if (strstr(buffer, "/testC"))
1710 if (strstr(buffer, "Cookie: cookie=biscuit"))
1711 send(c, okmsg, sizeof okmsg-1, 0);
1713 send(c, notokmsg, sizeof notokmsg-1, 0);
1715 if (strstr(buffer, "/testD"))
1717 send(c, okmsg2, sizeof okmsg2-1, 0);
1719 if (strstr(buffer, "/testE"))
1721 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1723 if (strstr(buffer, "GET /quit"))
1725 send(c, okmsg, sizeof okmsg-1, 0);
1726 send(c, page1, sizeof page1-1, 0);
1729 if (strstr(buffer, "GET /testF"))
1731 send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
1732 send(c, garbagemsg, sizeof garbagemsg-1, 0);
1733 send(c, contmsg, sizeof contmsg-1, 0);
1734 send(c, garbagemsg, sizeof garbagemsg-1, 0);
1735 send(c, okmsg, sizeof okmsg-1, 0);
1736 send(c, page1, sizeof page1-1, 0);
1738 if (strstr(buffer, "GET /testG"))
1740 send(c, page1, sizeof page1-1, 0);
1745 } while (!last_request);
1752 static void test_basic_request(int port, const char *verb, const char *url)
1754 HINTERNET hi, hc, hr;
1758 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1759 ok(hi != NULL, "open failed\n");
1761 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1762 ok(hc != NULL, "connect failed\n");
1764 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1765 ok(hr != NULL, "HttpOpenRequest failed\n");
1767 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1768 ok(r, "HttpSendRequest failed\n");
1771 memset(buffer, 0, sizeof buffer);
1772 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1773 ok(r, "InternetReadFile failed\n");
1774 ok(count == sizeof page1 - 1, "count was wrong\n");
1775 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1777 InternetCloseHandle(hr);
1778 InternetCloseHandle(hc);
1779 InternetCloseHandle(hi);
1782 static void test_proxy_indirect(int port)
1784 HINTERNET hi, hc, hr;
1788 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1789 ok(hi != NULL, "open failed\n");
1791 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1792 ok(hc != NULL, "connect failed\n");
1794 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1795 ok(hr != NULL, "HttpOpenRequest failed\n");
1797 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1798 ok(r, "HttpSendRequest failed\n");
1801 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1802 ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
1805 skip("missing proxy header, not testing remaining proxy headers\n");
1808 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1811 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1812 ok(r, "HttpQueryInfo failed\n");
1813 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1816 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1817 ok(r, "HttpQueryInfo failed\n");
1818 ok(val == 407, "proxy code wrong\n");
1821 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1822 ok(r, "HttpQueryInfo failed\n");
1823 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1826 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1827 ok(r, "HttpQueryInfo failed\n");
1828 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1831 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1832 ok(r, "HttpQueryInfo failed\n");
1833 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1836 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1837 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1838 ok(r == FALSE, "HttpQueryInfo failed\n");
1841 InternetCloseHandle(hr);
1842 InternetCloseHandle(hc);
1843 InternetCloseHandle(hi);
1846 static void test_proxy_direct(int port)
1848 HINTERNET hi, hc, hr;
1851 static CHAR username[] = "mike",
1852 password[] = "1101";
1854 sprintf(buffer, "localhost:%d\n", port);
1855 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1856 ok(hi != NULL, "open failed\n");
1858 /* try connect without authorization */
1859 hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1860 ok(hc != NULL, "connect failed\n");
1862 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1863 ok(hr != NULL, "HttpOpenRequest failed\n");
1865 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1866 ok(r, "HttpSendRequest failed\n");
1869 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1870 ok(r, "HttpQueryInfo failed\n");
1871 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1874 /* set the user + password then try again */
1876 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1877 ok(r, "failed to set user\n");
1879 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1880 ok(r, "failed to set password\n");
1883 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1884 ok(r, "HttpSendRequest failed\n");
1886 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1887 ok(r, "HttpQueryInfo failed\n");
1889 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1893 InternetCloseHandle(hr);
1894 InternetCloseHandle(hc);
1895 InternetCloseHandle(hi);
1898 static void test_header_handling_order(int port)
1900 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1901 static char connection[] = "Connection: Close";
1903 static const char *types[2] = { "*", NULL };
1904 HINTERNET session, connect, request;
1908 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1909 ok(session != NULL, "InternetOpen failed\n");
1911 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1912 ok(connect != NULL, "InternetConnect failed\n");
1914 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1915 ok(request != NULL, "HttpOpenRequest failed\n");
1917 ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1918 ok(ret, "HttpAddRequestHeaders failed\n");
1920 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1921 ok(ret, "HttpSendRequest failed\n");
1924 size = sizeof(status);
1925 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1926 ok(ret, "HttpQueryInfo failed\n");
1927 ok(status == 200, "request failed with status %u\n", status);
1929 InternetCloseHandle(request);
1931 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1932 ok(request != NULL, "HttpOpenRequest failed\n");
1934 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1935 ok(ret, "HttpSendRequest failed\n");
1938 size = sizeof(status);
1939 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1940 ok(ret, "HttpQueryInfo failed\n");
1941 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1943 InternetCloseHandle(request);
1945 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1946 ok(request != NULL, "HttpOpenRequest failed\n");
1948 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1949 ok(ret, "HttpAddRequestHeaders failed\n");
1951 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1952 ok(ret, "HttpSendRequest failed\n");
1955 size = sizeof(status);
1956 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1957 ok(ret, "HttpQueryInfo failed\n");
1958 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1960 InternetCloseHandle(request);
1961 InternetCloseHandle(connect);
1962 InternetCloseHandle(session);
1965 static void test_connection_header(int port)
1967 HINTERNET ses, con, req;
1971 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1972 ok(ses != NULL, "InternetOpen failed\n");
1974 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1975 ok(con != NULL, "InternetConnect failed\n");
1977 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1978 ok(req != NULL, "HttpOpenRequest failed\n");
1980 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1981 ok(ret, "HttpSendRequest failed\n");
1984 size = sizeof(status);
1985 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1986 ok(ret, "HttpQueryInfo failed\n");
1987 ok(status == 200, "request failed with status %u\n", status);
1989 InternetCloseHandle(req);
1991 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1992 ok(req != NULL, "HttpOpenRequest failed\n");
1994 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1995 ok(ret, "HttpSendRequest failed\n");
1998 size = sizeof(status);
1999 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2000 ok(ret, "HttpQueryInfo failed\n");
2001 ok(status == 200, "request failed with status %u\n", status);
2003 InternetCloseHandle(req);
2005 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2006 ok(req != NULL, "HttpOpenRequest failed\n");
2008 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2009 ok(ret, "HttpSendRequest failed\n");
2012 size = sizeof(status);
2013 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2014 ok(ret, "HttpQueryInfo failed\n");
2015 ok(status == 200, "request failed with status %u\n", status);
2017 InternetCloseHandle(req);
2019 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2020 ok(req != NULL, "HttpOpenRequest failed\n");
2022 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2023 ok(ret, "HttpSendRequest failed\n");
2026 size = sizeof(status);
2027 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2028 ok(ret, "HttpQueryInfo failed\n");
2029 ok(status == 200, "request failed with status %u\n", status);
2031 InternetCloseHandle(req);
2032 InternetCloseHandle(con);
2033 InternetCloseHandle(ses);
2036 static void test_http1_1(int port)
2038 HINTERNET ses, con, req;
2041 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2042 ok(ses != NULL, "InternetOpen failed\n");
2044 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2045 ok(con != NULL, "InternetConnect failed\n");
2047 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2048 ok(req != NULL, "HttpOpenRequest failed\n");
2050 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2053 InternetCloseHandle(req);
2055 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2056 ok(req != NULL, "HttpOpenRequest failed\n");
2058 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2060 ok(ret, "HttpSendRequest failed\n");
2063 InternetCloseHandle(req);
2064 InternetCloseHandle(con);
2065 InternetCloseHandle(ses);
2068 static void test_HttpSendRequestW(int port)
2070 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
2071 HINTERNET ses, con, req;
2075 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2076 ok(ses != NULL, "InternetOpen failed\n");
2078 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2079 ok(con != NULL, "InternetConnect failed\n");
2081 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2082 ok(req != NULL, "HttpOpenRequest failed\n");
2084 SetLastError(0xdeadbeef);
2085 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
2086 error = GetLastError();
2087 ok(!ret, "HttpSendRequestW succeeded\n");
2088 ok(error == ERROR_IO_PENDING ||
2089 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
2090 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
2091 "got %u expected ERROR_IO_PENDING\n", error);
2093 InternetCloseHandle(req);
2094 InternetCloseHandle(con);
2095 InternetCloseHandle(ses);
2098 static void test_cookie_header(int port)
2100 HINTERNET ses, con, req;
2101 DWORD size, status, error;
2105 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2106 ok(ses != NULL, "InternetOpen failed\n");
2108 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2109 ok(con != NULL, "InternetConnect failed\n");
2111 InternetSetCookie("http://localhost", "cookie", "biscuit");
2113 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2114 ok(req != NULL, "HttpOpenRequest failed\n");
2117 size = sizeof(buffer);
2118 SetLastError(0xdeadbeef);
2119 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2120 error = GetLastError();
2121 ok(!ret, "HttpQueryInfo succeeded\n");
2122 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
2124 ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
2125 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
2128 size = sizeof(buffer);
2129 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2130 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2131 ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
2133 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2134 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
2137 size = sizeof(status);
2138 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2139 ok(ret, "HttpQueryInfo failed\n");
2140 ok(status == 200, "request failed with status %u\n", status);
2143 size = sizeof(buffer);
2144 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2145 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2146 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
2148 InternetCloseHandle(req);
2149 InternetCloseHandle(con);
2150 InternetCloseHandle(ses);
2153 static void test_basic_authentication(int port)
2155 HINTERNET session, connect, request;
2159 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2160 ok(session != NULL, "InternetOpen failed\n");
2162 connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
2163 ok(connect != NULL, "InternetConnect failed\n");
2165 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
2166 ok(request != NULL, "HttpOpenRequest failed\n");
2168 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2169 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2172 size = sizeof(status);
2173 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2174 ok(ret, "HttpQueryInfo failed\n");
2175 ok(status == 200, "request failed with status %u\n", status);
2177 InternetCloseHandle(request);
2178 InternetCloseHandle(connect);
2179 InternetCloseHandle(session);
2182 static void test_invalid_response_headers(int port)
2184 HINTERNET session, connect, request;
2189 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2190 ok(session != NULL, "InternetOpen failed\n");
2192 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2193 ok(connect != NULL, "InternetConnect failed\n");
2195 request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
2196 ok(request != NULL, "HttpOpenRequest failed\n");
2198 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2199 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2202 size = sizeof(status);
2203 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2204 ok(ret, "HttpQueryInfo failed\n");
2205 ok(status == 401, "unexpected status %u\n", status);
2208 size = sizeof(buffer);
2209 ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2210 ok(ret, "HttpQueryInfo failed\n");
2211 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
2212 "headers wrong \"%s\"\n", buffer);
2215 size = sizeof(buffer);
2216 ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
2217 ok(ret, "HttpQueryInfo failed\n");
2218 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
2220 InternetCloseHandle(request);
2221 InternetCloseHandle(connect);
2222 InternetCloseHandle(session);
2225 static void test_response_without_headers(int port)
2227 HINTERNET hi, hc, hr;
2228 DWORD r, count, size, status;
2231 SetLastError(0xdeadbeef);
2232 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2233 ok(hi != NULL, "open failed %u\n", GetLastError());
2235 SetLastError(0xdeadbeef);
2236 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2237 ok(hc != NULL, "connect failed %u\n", GetLastError());
2239 SetLastError(0xdeadbeef);
2240 hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
2241 ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
2243 SetLastError(0xdeadbeef);
2244 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2245 todo_wine ok(r, "HttpSendRequest failed %u\n", GetLastError());
2248 memset(buffer, 0, sizeof buffer);
2249 SetLastError(0xdeadbeef);
2250 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2251 ok(r, "InternetReadFile failed %u\n", GetLastError());
2252 todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
2253 todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2256 size = sizeof(status);
2257 SetLastError(0xdeadbeef);
2258 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2259 todo_wine ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2260 todo_wine ok(status == 200, "expected status 200 got %u\n", status);
2263 size = sizeof(buffer);
2264 SetLastError(0xdeadbeef);
2265 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
2266 todo_wine ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2267 todo_wine ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
2270 size = sizeof(buffer);
2271 SetLastError(0xdeadbeef);
2272 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
2273 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2274 todo_wine ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
2277 size = sizeof(buffer);
2278 SetLastError(0xdeadbeef);
2279 r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2280 ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2281 todo_wine ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
2283 InternetCloseHandle(hr);
2284 InternetCloseHandle(hc);
2285 InternetCloseHandle(hi);
2288 static void test_HttpQueryInfo(int port)
2290 HINTERNET hi, hc, hr;
2295 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2296 ok(hi != NULL, "InternetOpen failed\n");
2298 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2299 ok(hc != NULL, "InternetConnect failed\n");
2301 hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2302 ok(hr != NULL, "HttpOpenRequest failed\n");
2304 ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2305 ok(ret, "HttpSendRequest failed\n");
2308 size = sizeof(buffer);
2309 ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2310 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2311 ok(index == 1, "expected 1 got %u\n", index);
2314 size = sizeof(buffer);
2315 ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2316 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2317 ok(index == 1, "expected 1 got %u\n", index);
2320 size = sizeof(buffer);
2321 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2322 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2323 ok(index == 0, "expected 0 got %u\n", index);
2325 size = sizeof(buffer);
2326 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2327 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2328 ok(index == 0, "expected 0 got %u\n", index);
2330 size = sizeof(buffer);
2331 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2332 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2333 ok(index == 0, "expected 0 got %u\n", index);
2335 size = sizeof(buffer);
2336 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2337 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2338 ok(index == 0, "expected 0 got %u\n", index);
2340 size = sizeof(buffer);
2341 ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2342 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2343 ok(index == 0, "expected 0 got %u\n", index);
2346 size = sizeof(buffer);
2347 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2348 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2349 ok(index == 0, "expected 0 got %u\n", index);
2352 size = sizeof(buffer);
2353 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2354 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2355 ok(index == 0, "expected 0 got %u\n", index);
2358 size = sizeof(buffer);
2359 ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2360 ok(!ret, "HttpQueryInfo succeeded\n");
2361 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2364 size = sizeof(buffer);
2365 ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2366 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2367 ok(index == 1, "expected 1 got %u\n", index);
2370 size = sizeof(buffer);
2371 strcpy(buffer, "Server");
2372 ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2373 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2374 ok(index == 1, "expected 1 got %u\n", index);
2377 size = sizeof(buffer);
2378 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2379 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2380 ok(index == 1, "expected 1 got %u\n", index);
2382 size = sizeof(buffer);
2383 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2384 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2385 ok(index == 2, "expected 2 got %u\n", index);
2387 InternetCloseHandle(hr);
2388 InternetCloseHandle(hc);
2389 InternetCloseHandle(hi);
2392 static void test_http_connection(void)
2394 struct server_info si;
2398 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2401 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2402 ok( hThread != NULL, "create thread failed\n");
2404 r = WaitForSingleObject(si.hEvent, 10000);
2405 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2406 if (r != WAIT_OBJECT_0)
2409 test_basic_request(si.port, "GET", "/test1");
2410 test_proxy_indirect(si.port);
2411 test_proxy_direct(si.port);
2412 test_header_handling_order(si.port);
2413 test_basic_request(si.port, "POST", "/test5");
2414 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2415 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2416 test_basic_request(si.port, "GET", "/test6");
2417 test_basic_request(si.port, "GET", "/testF");
2418 test_connection_header(si.port);
2419 test_http1_1(si.port);
2420 test_cookie_header(si.port);
2421 test_basic_authentication(si.port);
2422 test_invalid_response_headers(si.port);
2423 test_response_without_headers(si.port);
2424 test_HttpQueryInfo(si.port);
2425 test_HttpSendRequestW(si.port);
2427 /* send the basic request again to shutdown the server thread */
2428 test_basic_request(si.port, "GET", "/quit");
2430 r = WaitForSingleObject(hThread, 3000);
2431 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2432 CloseHandle(hThread);
2435 static void test_user_agent_header(void)
2437 HINTERNET ses, con, req;
2442 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2443 ok(ses != NULL, "InternetOpen failed\n");
2445 con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2446 ok(con != NULL, "InternetConnect failed\n");
2448 req = HttpOpenRequest(con, "GET", "/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2449 ok(req != NULL, "HttpOpenRequest failed\n");
2451 size = sizeof(buffer);
2452 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2453 err = GetLastError();
2454 ok(!ret, "HttpQueryInfo succeeded\n");
2455 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2457 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2458 ok(ret, "HttpAddRequestHeaders succeeded\n");
2460 size = sizeof(buffer);
2461 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2462 err = GetLastError();
2463 ok(ret, "HttpQueryInfo failed\n");
2464 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2466 InternetCloseHandle(req);
2468 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2469 ok(req != NULL, "HttpOpenRequest failed\n");
2471 size = sizeof(buffer);
2472 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2473 err = GetLastError();
2474 ok(!ret, "HttpQueryInfo succeeded\n");
2475 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2477 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2478 ok(ret, "HttpAddRequestHeaders failed\n");
2481 size = sizeof(buffer);
2482 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2483 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2484 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2486 InternetCloseHandle(req);
2487 InternetCloseHandle(con);
2488 InternetCloseHandle(ses);
2491 static void test_bogus_accept_types_array(void)
2493 HINTERNET ses, con, req;
2494 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2499 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2500 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2501 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2503 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2506 size = sizeof(buffer);
2507 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2508 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2509 ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2510 !strcmp(buffer, "*/*, %p, */*"),
2511 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2513 InternetCloseHandle(req);
2514 InternetCloseHandle(con);
2515 InternetCloseHandle(ses);
2524 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2526 INTERNET_ASYNC_RESULT *result = info;
2527 struct context *ctx = (struct context *)context;
2529 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2531 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2533 trace("request handle: 0x%08lx\n", result->dwResult);
2534 ctx->req = (HINTERNET)result->dwResult;
2535 SetEvent(ctx->event);
2537 if (status == INTERNET_STATUS_HANDLE_CLOSING)
2539 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2541 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2542 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2543 SetEvent(ctx->event);
2547 static void test_open_url_async(void)
2556 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2558 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2559 ok(ses != NULL, "InternetOpen failed\n");
2561 SetLastError(0xdeadbeef);
2562 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2563 error = GetLastError();
2564 ok(!ret, "InternetSetOptionA succeeded\n");
2565 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2567 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2568 error = GetLastError();
2569 ok(!ret, "InternetSetOptionA failed\n");
2570 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2572 pInternetSetStatusCallbackA(ses, cb);
2573 ResetEvent(ctx.event);
2575 req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2576 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2578 WaitForSingleObject(ctx.event, INFINITE);
2581 size = sizeof(type);
2582 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2583 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2584 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2585 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2588 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2589 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2590 ok(size > 0, "expected size > 0\n");
2592 ResetEvent(ctx.event);
2593 InternetCloseHandle(ctx.req);
2594 WaitForSingleObject(ctx.event, INFINITE);
2596 InternetCloseHandle(ses);
2597 CloseHandle(ctx.event);
2602 internet_connect = 1,
2604 http_send_request_ex,
2607 internet_close_handle
2612 enum api function; /* api responsible for notification */
2613 unsigned int status; /* status received */
2614 int async; /* delivered from another thread? */
2622 const struct notification *test;
2630 static CRITICAL_SECTION notification_cs;
2632 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
2634 BOOL status_ok, function_ok;
2635 struct info *info = (struct info *)context;
2638 EnterCriticalSection( ¬ification_cs );
2640 if (status == INTERNET_STATUS_HANDLE_CREATED)
2642 DWORD size = sizeof(struct info *);
2643 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
2646 if (i >= info->count)
2648 LeaveCriticalSection( ¬ification_cs );
2652 while (info->test[i].status != status && info->test[i].optional &&
2653 i < info->count - 1 &&
2654 info->test[i].function == info->test[i + 1].function)
2659 status_ok = (info->test[i].status == status);
2660 function_ok = (info->test[i].function == info->function);
2662 if (!info->test[i].todo)
2664 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2665 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2667 if (info->test[i].async)
2668 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
2669 info->line, info->thread, GetCurrentThreadId());
2673 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2675 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2677 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
2680 LeaveCriticalSection( ¬ification_cs );
2683 static void setup_test( struct info *info, enum api function, unsigned int line )
2685 info->function = function;
2689 static const struct notification async_send_request_ex_test[] =
2691 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
2692 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
2693 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
2694 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1 },
2695 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1 },
2696 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
2697 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
2698 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
2699 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
2700 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2701 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, 0 },
2702 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, 0 },
2703 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
2704 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
2705 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2706 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 },
2707 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 }
2710 static void test_async_HttpSendRequestEx(void)
2713 HINTERNET ses, req, con;
2715 DWORD size, written, error;
2716 INTERNET_BUFFERSA b;
2717 static const char *accept[2] = {"*/*", NULL};
2718 static char data[] = "Public ID=codeweavers";
2721 InitializeCriticalSection( ¬ification_cs );
2723 info.test = async_send_request_ex_test;
2724 info.count = sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]);
2726 info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
2727 info.thread = GetCurrentThreadId();
2729 ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
2730 ok( ses != NULL, "InternetOpen failed\n" );
2732 pInternetSetStatusCallbackA( ses, check_notification );
2734 setup_test( &info, internet_connect, __LINE__ );
2735 con = InternetConnect( ses, "crossover.codeweavers.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
2736 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
2738 WaitForSingleObject( info.wait, 10000 );
2740 setup_test( &info, http_open_request, __LINE__ );
2741 req = HttpOpenRequest( con, "POST", "posttest.php", NULL, NULL, accept, 0, (DWORD_PTR)&info );
2742 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
2744 WaitForSingleObject( info.wait, 10000 );
2746 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
2747 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
2748 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
2749 b.dwHeadersLength = strlen( b.lpcszHeader );
2750 b.dwBufferTotal = strlen( data );
2752 setup_test( &info, http_send_request_ex, __LINE__ );
2753 ret = HttpSendRequestExA( req, &b, NULL, 0x28, 0 );
2754 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
2756 WaitForSingleObject( info.wait, 10000 );
2758 size = sizeof(buffer);
2759 SetLastError( 0xdeadbeef );
2760 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
2761 error = GetLastError();
2762 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
2764 ok( error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
2765 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
2768 size = strlen( data );
2769 setup_test( &info, internet_writefile, __LINE__ );
2770 ret = InternetWriteFile( req, data, size, &written );
2771 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
2772 ok( written == size, "expected %u got %u\n", written, size );
2774 WaitForSingleObject( info.wait, 10000 );
2776 SetLastError( 0xdeadbeef );
2777 ret = HttpEndRequestA( req, (void *)data, 0x28, 0 );
2778 error = GetLastError();
2779 ok( !ret, "HttpEndRequestA succeeded\n" );
2780 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
2782 SetLastError( 0xdeadbeef );
2783 setup_test( &info, http_end_request, __LINE__ );
2784 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
2785 error = GetLastError();
2786 ok( !ret, "HttpEndRequestA succeeded\n" );
2787 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
2789 WaitForSingleObject( info.wait, 10000 );
2791 setup_test( &info, internet_close_handle, __LINE__ );
2792 InternetCloseHandle( req );
2793 InternetCloseHandle( con );
2794 InternetCloseHandle( ses );
2796 WaitForSingleObject( info.wait, 10000 );
2798 CloseHandle( info.wait );
2801 #define STATUS_STRING(status) \
2802 memcpy(status_string[status], #status, sizeof(CHAR) * \
2803 (strlen(#status) < MAX_STATUS_NAME ? \
2805 MAX_STATUS_NAME - 1))
2806 static void init_status_tests(void)
2808 memset(expect, 0, sizeof(expect));
2809 memset(optional, 0, sizeof(optional));
2810 memset(wine_allow, 0, sizeof(wine_allow));
2811 memset(notified, 0, sizeof(notified));
2812 memset(status_string, 0, sizeof(status_string));
2813 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2814 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2815 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2816 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2817 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2818 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2819 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2820 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2821 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2822 STATUS_STRING(INTERNET_STATUS_PREFETCH);
2823 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2824 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2825 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2826 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2827 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2828 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2829 STATUS_STRING(INTERNET_STATUS_REDIRECT);
2830 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2831 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2832 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2833 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2834 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2835 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2836 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2837 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2838 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2840 #undef STATUS_STRING
2845 hdll = GetModuleHandleA("wininet.dll");
2846 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2848 if (!pInternetSetStatusCallbackA)
2849 skip("skipping the InternetReadFile tests\n");
2852 init_status_tests();
2853 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
2854 InternetReadFile_test(0, &test_data[0]);
2855 first_connection_to_test_url = TRUE;
2856 InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
2857 InternetReadFile_test(0, &test_data[1]);
2858 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2859 test_open_url_async();
2860 test_async_HttpSendRequestEx();
2862 InternetOpenRequest_test();
2864 InternetOpenUrlA_test();
2866 test_http_connection();
2867 test_user_agent_header();
2868 test_bogus_accept_types_array();
2869 InternetReadFile_chunked_test();
2870 HttpSendRequestEx_test();