push 52cba0a224aad01bcbdb26d79e43229ba950650e
[wine/hacks.git] / dlls / wininet / tests / http.c
blob7e3c08cc55eed4feed97fe6ae64bec4a329f34ff
1 /*
2 * Wininet - Http tests
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winsock.h"
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) \
41 expect[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) \
61 do { \
62 if (!expect[status] && !optional[status] && wine_allow[status]) \
63 { \
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]--; \
68 } \
69 else \
70 { \
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]--; \
76 } \
77 notified[status]++; \
78 }while(0)
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) \
86 do { \
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); \
92 }while(0)
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 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
111 static VOID WINAPI callback(
112 HINTERNET hInternet,
113 DWORD_PTR dwContext,
114 DWORD dwInternetStatus,
115 LPVOID lpvStatusInformation,
116 DWORD dwStatusInformationLength
119 CHECK_EXPECT(dwInternetStatus);
120 switch (dwInternetStatus)
122 case INTERNET_STATUS_RESOLVING_NAME:
123 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
124 GetCurrentThreadId(), hInternet, dwContext,
125 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
126 *(LPSTR)lpvStatusInformation = '\0';
127 break;
128 case INTERNET_STATUS_NAME_RESOLVED:
129 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
130 GetCurrentThreadId(), hInternet, dwContext,
131 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
132 *(LPSTR)lpvStatusInformation = '\0';
133 break;
134 case INTERNET_STATUS_CONNECTING_TO_SERVER:
135 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
136 GetCurrentThreadId(), hInternet, dwContext,
137 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
138 *(LPSTR)lpvStatusInformation = '\0';
139 break;
140 case INTERNET_STATUS_CONNECTED_TO_SERVER:
141 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
142 GetCurrentThreadId(), hInternet, dwContext,
143 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
144 *(LPSTR)lpvStatusInformation = '\0';
145 break;
146 case INTERNET_STATUS_SENDING_REQUEST:
147 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
148 GetCurrentThreadId(), hInternet, dwContext,
149 lpvStatusInformation,dwStatusInformationLength);
150 break;
151 case INTERNET_STATUS_REQUEST_SENT:
152 ok(dwStatusInformationLength == sizeof(DWORD),
153 "info length should be sizeof(DWORD) instead of %d\n",
154 dwStatusInformationLength);
155 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
156 GetCurrentThreadId(), hInternet, dwContext,
157 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
158 break;
159 case INTERNET_STATUS_RECEIVING_RESPONSE:
160 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
161 GetCurrentThreadId(), hInternet, dwContext,
162 lpvStatusInformation,dwStatusInformationLength);
163 break;
164 case INTERNET_STATUS_RESPONSE_RECEIVED:
165 ok(dwStatusInformationLength == sizeof(DWORD),
166 "info length should be sizeof(DWORD) instead of %d\n",
167 dwStatusInformationLength);
168 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
169 GetCurrentThreadId(), hInternet, dwContext,
170 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
171 break;
172 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
173 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
174 GetCurrentThreadId(), hInternet,dwContext,
175 lpvStatusInformation,dwStatusInformationLength);
176 break;
177 case INTERNET_STATUS_PREFETCH:
178 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
179 GetCurrentThreadId(), hInternet, dwContext,
180 lpvStatusInformation,dwStatusInformationLength);
181 break;
182 case INTERNET_STATUS_CLOSING_CONNECTION:
183 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
184 GetCurrentThreadId(), hInternet, dwContext,
185 lpvStatusInformation,dwStatusInformationLength);
186 break;
187 case INTERNET_STATUS_CONNECTION_CLOSED:
188 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
189 GetCurrentThreadId(), hInternet, dwContext,
190 lpvStatusInformation,dwStatusInformationLength);
191 break;
192 case INTERNET_STATUS_HANDLE_CREATED:
193 ok(dwStatusInformationLength == sizeof(HINTERNET),
194 "info length should be sizeof(HINTERNET) instead of %d\n",
195 dwStatusInformationLength);
196 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
197 GetCurrentThreadId(), hInternet, dwContext,
198 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
199 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
200 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
201 break;
202 case INTERNET_STATUS_HANDLE_CLOSING:
203 ok(dwStatusInformationLength == sizeof(HINTERNET),
204 "info length should be sizeof(HINTERNET) instead of %d\n",
205 dwStatusInformationLength);
206 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
207 GetCurrentThreadId(), hInternet, dwContext,
208 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
209 break;
210 case INTERNET_STATUS_REQUEST_COMPLETE:
212 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
213 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
214 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
215 dwStatusInformationLength);
216 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
217 GetCurrentThreadId(), hInternet, dwContext,
218 iar->dwResult,iar->dwError,dwStatusInformationLength);
219 SetEvent(hCompleteEvent);
220 break;
222 case INTERNET_STATUS_REDIRECT:
223 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
224 GetCurrentThreadId(), hInternet, dwContext,
225 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
226 *(LPSTR)lpvStatusInformation = '\0';
227 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
228 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
229 break;
230 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
231 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
232 GetCurrentThreadId(), hInternet, dwContext,
233 lpvStatusInformation, dwStatusInformationLength);
234 break;
235 default:
236 trace("%04x:Callback %p 0x%lx %d %p %d\n",
237 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
238 lpvStatusInformation, dwStatusInformationLength);
242 static void InternetReadFile_test(int flags)
244 BOOL res;
245 CHAR buffer[4000];
246 DWORD length;
247 DWORD out;
248 const char *types[2] = { "*", NULL };
249 HINTERNET hi, hic = 0, hor = 0;
251 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
253 trace("Starting InternetReadFile test with flags 0x%x\n",flags);
255 trace("InternetOpenA <--\n");
256 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
257 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
258 trace("InternetOpenA -->\n");
260 if (hi == 0x0) goto abort;
262 pInternetSetStatusCallbackA(hi,&callback);
264 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
266 trace("InternetConnectA <--\n");
267 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
268 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
269 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
270 trace("InternetConnectA -->\n");
272 if (hic == 0x0) goto abort;
274 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
275 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
277 trace("HttpOpenRequestA <--\n");
278 hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
279 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
280 0xdeadbead);
281 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
283 * If the internet name can't be resolved we are probably behind
284 * a firewall or in some other way not directly connected to the
285 * Internet. Not enough reason to fail the test. Just ignore and
286 * abort.
288 } else {
289 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
291 trace("HttpOpenRequestA -->\n");
293 if (hor == 0x0) goto abort;
295 length = sizeof(buffer);
296 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
297 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
298 ok(!strcmp(buffer, "http://test.winehq.org/testredirect"), "Wrong URL %s\n", buffer);
300 length = sizeof(buffer);
301 res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
302 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
303 ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
304 ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
306 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
307 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
308 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
309 if (first_connection_to_test_url)
311 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
312 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
313 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
314 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
316 else
318 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
319 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
321 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
322 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
323 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
324 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
325 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
326 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
327 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
328 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
329 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
330 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
331 SET_EXPECT(INTERNET_STATUS_REDIRECT);
332 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
333 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
334 if (flags & INTERNET_FLAG_ASYNC)
335 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
336 else
337 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
339 trace("HttpSendRequestA -->\n");
340 SetLastError(0xdeadbeef);
341 res = HttpSendRequestA(hor, "", -1, NULL, 0);
342 if (flags & INTERNET_FLAG_ASYNC)
343 ok(!res && (GetLastError() == ERROR_IO_PENDING),
344 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
345 else
346 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
347 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
348 trace("HttpSendRequestA <--\n");
350 if (flags & INTERNET_FLAG_ASYNC)
351 WaitForSingleObject(hCompleteEvent, INFINITE);
353 if (first_connection_to_test_url)
355 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
356 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
358 else todo_wine
360 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
361 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
363 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
364 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
365 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
366 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
367 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
368 if (flags & INTERNET_FLAG_ASYNC)
369 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
370 else
371 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
372 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
373 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
374 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
376 length = 4;
377 res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
378 ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
380 length = 100;
381 res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
382 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
384 length = sizeof(buffer);
385 res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
386 ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
387 buffer[length]=0;
389 length = sizeof(buffer);
390 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
391 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
392 ok(!strcmp(buffer, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", buffer);
394 length = 16;
395 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
396 trace("Option 0x5 -> %i %s (%u)\n",res,buffer,GetLastError());
398 length = 100;
399 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
400 buffer[length]=0;
401 trace("Option 0x1 -> %i %s\n",res,buffer);
403 SetLastError(0xdeadbeef);
404 res = InternetReadFile(NULL, buffer, 100, &length);
405 ok(!res, "InternetReadFile should have failed\n");
406 ok(GetLastError() == ERROR_INVALID_HANDLE,
407 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
408 GetLastError());
410 length = 100;
411 trace("Entering Query loop\n");
413 while (TRUE)
415 if (flags & INTERNET_FLAG_ASYNC)
416 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
417 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
418 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
419 ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
420 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
421 if (flags & INTERNET_FLAG_ASYNC)
423 if (res)
425 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
427 else if (GetLastError() == ERROR_IO_PENDING)
429 WaitForSingleObject(hCompleteEvent, INFINITE);
430 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
431 continue;
434 if (length)
436 char *buffer;
437 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
439 res = InternetReadFile(hor,buffer,length,&length);
441 buffer[length]=0;
443 trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
445 HeapFree(GetProcessHeap(),0,buffer);
447 if (length == 0)
448 break;
450 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
451 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
452 abort:
453 trace("aborting\n");
454 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
455 if (hor != 0x0) {
456 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
457 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
458 SetLastError(0xdeadbeef);
459 trace("closing\n");
460 res = InternetCloseHandle(hor);
461 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
462 SetLastError(0xdeadbeef);
463 res = InternetCloseHandle(hor);
464 ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
465 ok (GetLastError() == ERROR_INVALID_HANDLE,
466 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
467 GetLastError());
469 /* We intentionally do not close the handle opened by InternetConnectA as this
470 * tickles bug #9479: native closes child internet handles when the parent handles
471 * are closed. This is verified below by checking that the number of
472 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
473 if (hi != 0x0) {
474 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
475 trace("closing 2\n");
476 res = InternetCloseHandle(hi);
477 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
478 if (flags & INTERNET_FLAG_ASYNC)
479 Sleep(100);
481 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
482 if (hor != 0x0) todo_wine
484 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
485 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
487 else
489 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
490 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
492 CloseHandle(hCompleteEvent);
493 first_connection_to_test_url = FALSE;
496 static void InternetReadFile_chunked_test(void)
498 BOOL res;
499 CHAR buffer[4000];
500 DWORD length;
501 const char *types[2] = { "*", NULL };
502 HINTERNET hi, hic = 0, hor = 0;
504 trace("Starting InternetReadFile chunked test\n");
506 trace("InternetOpenA <--\n");
507 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
508 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
509 trace("InternetOpenA -->\n");
511 if (hi == 0x0) goto abort;
513 trace("InternetConnectA <--\n");
514 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
515 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
516 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
517 trace("InternetConnectA -->\n");
519 if (hic == 0x0) goto abort;
521 trace("HttpOpenRequestA <--\n");
522 hor = HttpOpenRequestA(hic, "GET", "/testchunked", NULL, NULL, types,
523 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
524 0xdeadbead);
525 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
527 * If the internet name can't be resolved we are probably behind
528 * a firewall or in some other way not directly connected to the
529 * Internet. Not enough reason to fail the test. Just ignore and
530 * abort.
532 } else {
533 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
535 trace("HttpOpenRequestA -->\n");
537 if (hor == 0x0) goto abort;
539 trace("HttpSendRequestA -->\n");
540 SetLastError(0xdeadbeef);
541 res = HttpSendRequestA(hor, "", -1, NULL, 0);
542 ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
543 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
544 trace("HttpSendRequestA <--\n");
546 length = 100;
547 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
548 buffer[length]=0;
549 trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
551 SetLastError( 0xdeadbeef );
552 length = 100;
553 res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
554 buffer[length]=0;
555 trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
556 ok( res, "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
557 ok( !strcmp( buffer, "chunked" ), "Wrong transfer encoding '%s'\n", buffer );
559 SetLastError( 0xdeadbeef );
560 length = 16;
561 res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
562 ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
563 ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
565 length = 100;
566 trace("Entering Query loop\n");
568 while (TRUE)
570 res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
571 ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
572 ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
573 trace("got %u available\n",length);
574 if (length)
576 DWORD got;
577 char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
579 res = InternetReadFile(hor,buffer,length,&got);
581 buffer[got]=0;
582 trace("ReadFile -> %i %i\n",res,got);
583 ok( length == got, "only got %u of %u available\n", got, length );
584 ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
586 HeapFree(GetProcessHeap(),0,buffer);
587 if (!got) break;
589 if (length == 0)
590 break;
592 abort:
593 trace("aborting\n");
594 if (hor != 0x0) {
595 res = InternetCloseHandle(hor);
596 ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
598 if (hi != 0x0) {
599 res = InternetCloseHandle(hi);
600 ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
604 static void InternetReadFileExA_test(int flags)
606 DWORD rc;
607 DWORD length;
608 const char *types[2] = { "*", NULL };
609 HINTERNET hi, hic = 0, hor = 0;
610 INTERNET_BUFFERS inetbuffers;
612 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
614 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
616 trace("InternetOpenA <--\n");
617 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
618 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
619 trace("InternetOpenA -->\n");
621 if (hi == 0x0) goto abort;
623 pInternetSetStatusCallbackA(hi,&callback);
625 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
627 trace("InternetConnectA <--\n");
628 hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
629 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
630 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
631 trace("InternetConnectA -->\n");
633 if (hic == 0x0) goto abort;
635 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
636 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
638 trace("HttpOpenRequestA <--\n");
639 hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
640 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
641 0xdeadbead);
642 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
644 * If the internet name can't be resolved we are probably behind
645 * a firewall or in some other way not directly connected to the
646 * Internet. Not enough reason to fail the test. Just ignore and
647 * abort.
649 } else {
650 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
652 trace("HttpOpenRequestA -->\n");
654 if (hor == 0x0) goto abort;
656 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
657 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
658 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
659 if (first_connection_to_test_url)
661 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
662 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
663 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
664 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
666 else
668 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
669 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
671 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
672 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
673 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
674 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
675 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
676 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
677 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
678 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
679 SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
680 SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
681 SET_EXPECT(INTERNET_STATUS_REDIRECT);
682 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
683 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
684 if (flags & INTERNET_FLAG_ASYNC)
685 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
686 else
687 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
689 trace("HttpSendRequestA -->\n");
690 SetLastError(0xdeadbeef);
691 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
692 if (flags & INTERNET_FLAG_ASYNC)
693 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
694 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
695 else
696 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
697 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
698 trace("HttpSendRequestA <--\n");
700 if (!rc && (GetLastError() == ERROR_IO_PENDING))
701 WaitForSingleObject(hCompleteEvent, INFINITE);
703 if (first_connection_to_test_url)
705 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
706 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
708 else todo_wine
710 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
711 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
713 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
714 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
715 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
716 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
717 CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
718 CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
719 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
720 if (flags & INTERNET_FLAG_ASYNC)
721 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
722 else
723 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
724 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
725 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
726 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
728 /* tests invalid dwStructSize */
729 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
730 inetbuffers.lpcszHeader = NULL;
731 inetbuffers.dwHeadersLength = 0;
732 inetbuffers.dwBufferLength = 10;
733 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
734 inetbuffers.dwOffsetHigh = 1234;
735 inetbuffers.dwOffsetLow = 5678;
736 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
737 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
738 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
739 rc ? "TRUE" : "FALSE", GetLastError());
740 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
742 /* tests to see whether lpcszHeader is used - it isn't */
743 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
744 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
745 inetbuffers.dwHeadersLength = 255;
746 inetbuffers.dwBufferLength = 0;
747 inetbuffers.lpvBuffer = NULL;
748 inetbuffers.dwOffsetHigh = 1234;
749 inetbuffers.dwOffsetLow = 5678;
750 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
751 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
752 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
753 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
754 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
755 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
756 trace("read %i bytes\n", inetbuffers.dwBufferLength);
757 todo_wine
759 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
760 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
763 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
764 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
765 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
766 rc ? "TRUE" : "FALSE", GetLastError());
768 length = 0;
769 trace("Entering Query loop\n");
771 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
772 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
773 while (TRUE)
775 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
776 inetbuffers.dwBufferLength = 1024;
777 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
778 inetbuffers.dwOffsetHigh = 1234;
779 inetbuffers.dwOffsetLow = 5678;
781 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
782 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
783 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
784 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
785 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
786 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
787 if (!rc)
789 if (GetLastError() == ERROR_IO_PENDING)
791 trace("InternetReadFileEx -> PENDING\n");
792 ok(flags & INTERNET_FLAG_ASYNC,
793 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
794 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
795 WaitForSingleObject(hCompleteEvent, INFINITE);
796 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
797 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
799 else
801 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
802 break;
805 else
807 trace("InternetReadFileEx -> SUCCEEDED\n");
808 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
809 if (inetbuffers.dwBufferLength)
811 todo_wine {
812 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
813 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
816 else
818 /* Win98 still sends these when 0 bytes are read, WinXP does not */
819 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
820 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
824 trace("read %i bytes\n", inetbuffers.dwBufferLength);
825 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
827 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
828 "InternetReadFileEx sets offsets to 0x%x%08x\n",
829 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
831 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
833 if (!inetbuffers.dwBufferLength)
834 break;
836 length += inetbuffers.dwBufferLength;
838 ok(length > 0, "failed to read any of the document\n");
839 trace("Finished. Read %d bytes\n", length);
841 /* WinXP does not send, but Win98 does */
842 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
843 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
844 abort:
845 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
846 if (hor) {
847 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
848 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
849 rc = InternetCloseHandle(hor);
850 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
851 rc = InternetCloseHandle(hor);
852 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
854 if (hic) {
855 rc = InternetCloseHandle(hic);
856 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
858 if (hi) {
859 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
860 rc = InternetCloseHandle(hi);
861 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
862 if (flags & INTERNET_FLAG_ASYNC)
863 Sleep(100);
864 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
866 /* to enable once Wine is fixed to never send it
867 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
868 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
870 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
871 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
872 CloseHandle(hCompleteEvent);
873 first_connection_to_test_url = FALSE;
876 static void InternetOpenUrlA_test(void)
878 HINTERNET myhinternet, myhttp;
879 char buffer[0x400];
880 DWORD size, readbytes, totalbytes=0;
881 BOOL ret;
883 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
884 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
885 size = 0x400;
886 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
887 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
889 SetLastError(0);
890 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
891 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
892 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
893 return; /* WinXP returns this when not connected to the net */
894 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
895 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
896 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
897 totalbytes += readbytes;
898 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
899 totalbytes += readbytes;
900 trace("read 0x%08x bytes\n",totalbytes);
902 InternetCloseHandle(myhttp);
903 InternetCloseHandle(myhinternet);
906 static void HttpSendRequestEx_test(void)
908 HINTERNET hSession;
909 HINTERNET hConnect;
910 HINTERNET hRequest;
912 INTERNET_BUFFERS BufferIn;
913 DWORD dwBytesWritten;
914 DWORD dwBytesRead;
915 CHAR szBuffer[256];
916 int i;
917 BOOL ret;
919 static char szPostData[] = "mode=Test";
920 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
922 hSession = InternetOpen("Wine Regression Test",
923 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
924 ok( hSession != NULL ,"Unable to open Internet session\n");
925 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
926 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
928 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
929 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
930 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
931 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
933 skip( "Network unreachable, skipping test\n" );
934 goto done;
936 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
939 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
940 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
941 BufferIn.lpcszHeader = szContentType;
942 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
943 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
944 BufferIn.lpvBuffer = szPostData;
945 BufferIn.dwBufferLength = 3;
946 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
947 BufferIn.dwOffsetLow = 0;
948 BufferIn.dwOffsetHigh = 0;
950 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
951 ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
953 for (i = 3; szPostData[i]; i++)
954 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
955 "InternetWriteFile failed\n");
957 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
959 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
960 "Unable to read response\n");
961 szBuffer[dwBytesRead] = 0;
963 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
964 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
966 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
967 done:
968 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
969 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
972 static void InternetOpenRequest_test(void)
974 HINTERNET session, connect, request;
975 static const char *types[] = { "*", "", NULL };
976 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
977 static const WCHAR *typesW[] = { any, empty, NULL };
978 BOOL ret;
980 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
981 ok(session != NULL ,"Unable to open Internet session\n");
983 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
984 INTERNET_SERVICE_HTTP, 0, 0);
985 ok(connect == NULL, "InternetConnectA should have failed\n");
986 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
988 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
989 INTERNET_SERVICE_HTTP, 0, 0);
990 ok(connect == NULL, "InternetConnectA should have failed\n");
991 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
993 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
994 INTERNET_SERVICE_HTTP, 0, 0);
995 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
997 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
998 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1000 skip( "Network unreachable, skipping test\n" );
1001 goto done;
1003 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1005 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1006 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1007 ok(InternetCloseHandle(request), "Close request handle failed\n");
1009 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1010 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1012 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1013 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1014 ok(InternetCloseHandle(request), "Close request handle failed\n");
1016 done:
1017 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1018 ok(InternetCloseHandle(session), "Close session handle failed\n");
1021 static void test_http_cache(void)
1023 HINTERNET session, connect, request;
1024 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1025 DWORD size, file_size;
1026 BYTE buf[100];
1027 HANDLE file;
1028 BOOL ret;
1030 static const char *types[] = { "*", "", NULL };
1032 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1033 ok(session != NULL ,"Unable to open Internet session\n");
1035 connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1036 INTERNET_SERVICE_HTTP, 0, 0);
1037 ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1039 request = HttpOpenRequestA(connect, NULL, "/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1040 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1042 skip( "Network unreachable, skipping test\n" );
1044 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1045 ok(InternetCloseHandle(session), "Close session handle failed\n");
1047 return;
1049 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1051 size = sizeof(url);
1052 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1053 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1054 ok(!strcmp(url, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", url);
1056 size = sizeof(file_name);
1057 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1058 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1059 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1060 ok(!size, "size = %d\n", size);
1062 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1063 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1065 size = sizeof(file_name);
1066 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1067 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1069 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1070 FILE_ATTRIBUTE_NORMAL, NULL);
1071 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1072 file_size = GetFileSize(file, NULL);
1073 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1075 size = sizeof(buf);
1076 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1077 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1078 ok(size == 100, "size = %u\n", size);
1080 file_size = GetFileSize(file, NULL);
1081 todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1082 CloseHandle(file);
1084 ok(InternetCloseHandle(request), "Close request handle failed\n");
1086 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1087 FILE_ATTRIBUTE_NORMAL, NULL);
1088 todo_wine ok(file != INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1089 CloseHandle(file);
1091 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1092 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1094 size = sizeof(file_name);
1095 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1096 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1097 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1098 ok(!size, "size = %d\n", size);
1100 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1101 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1103 size = sizeof(file_name);
1104 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1105 todo_wine ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed %u\n", GetLastError());
1107 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1108 FILE_ATTRIBUTE_NORMAL, NULL);
1109 todo_wine ok(file != INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1110 CloseHandle(file);
1112 ok(InternetCloseHandle(request), "Close request handle failed\n");
1113 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1114 ok(InternetCloseHandle(session), "Close session handle failed\n");
1117 static void HttpHeaders_test(void)
1119 HINTERNET hSession;
1120 HINTERNET hConnect;
1121 HINTERNET hRequest;
1122 CHAR buffer[256];
1123 WCHAR wbuffer[256];
1124 DWORD len = 256;
1125 DWORD oldlen;
1126 DWORD index = 0;
1128 hSession = InternetOpen("Wine Regression Test",
1129 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1130 ok( hSession != NULL ,"Unable to open Internet session\n");
1131 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1132 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1134 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1135 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1136 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1137 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1139 skip( "Network unreachable, skipping test\n" );
1140 goto done;
1142 ok( hRequest != NULL, "Failed to open request handle\n");
1144 index = 0;
1145 len = sizeof(buffer);
1146 strcpy(buffer,"Warning");
1147 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1148 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1150 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1151 "Failed to add new header\n");
1153 index = 0;
1154 len = sizeof(buffer);
1155 strcpy(buffer,"Warning");
1156 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1157 buffer,&len,&index),"Unable to query header\n");
1158 ok(index == 1, "Index was not incremented\n");
1159 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1160 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1161 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1162 len = sizeof(buffer);
1163 strcpy(buffer,"Warning");
1164 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1165 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1167 index = 0;
1168 len = 5; /* could store the string but not the NULL terminator */
1169 strcpy(buffer,"Warning");
1170 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1171 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1172 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1173 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1175 /* a call with NULL will fail but will return the length */
1176 index = 0;
1177 len = sizeof(buffer);
1178 SetLastError(0xdeadbeef);
1179 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1180 NULL,&len,&index) == FALSE,"Query worked\n");
1181 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1182 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1183 ok(index == 0, "Index was incremented\n");
1185 /* even for a len that is too small */
1186 index = 0;
1187 len = 15;
1188 SetLastError(0xdeadbeef);
1189 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1190 NULL,&len,&index) == FALSE,"Query worked\n");
1191 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1192 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1193 ok(index == 0, "Index was incremented\n");
1195 index = 0;
1196 len = 0;
1197 SetLastError(0xdeadbeef);
1198 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1199 NULL,&len,&index) == FALSE,"Query worked\n");
1200 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1201 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1202 ok(index == 0, "Index was incremented\n");
1203 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1206 /* a working query */
1207 index = 0;
1208 len = sizeof(buffer);
1209 memset(buffer, 'x', sizeof(buffer));
1210 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1211 buffer,&len,&index),"Unable to query header\n");
1212 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1213 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1214 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1215 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1216 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1217 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1218 ok(index == 0, "Index was incremented\n");
1220 /* Like above two tests, but for W version */
1222 index = 0;
1223 len = 0;
1224 SetLastError(0xdeadbeef);
1225 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1226 NULL,&len,&index) == FALSE,"Query worked\n");
1227 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1228 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1229 ok(index == 0, "Index was incremented\n");
1230 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1232 /* a working query */
1233 index = 0;
1234 len = sizeof(wbuffer);
1235 memset(wbuffer, 'x', sizeof(wbuffer));
1236 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1237 wbuffer,&len,&index),"Unable to query header\n");
1238 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1239 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1240 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1241 ok(index == 0, "Index was incremented\n");
1243 /* end of W version tests */
1245 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1246 index = 0;
1247 len = sizeof(buffer);
1248 memset(buffer, 'x', sizeof(buffer));
1249 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1250 buffer,&len,&index) == TRUE,"Query failed\n");
1251 ok(len == 2, "Expected 2, got %d\n", len);
1252 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1253 ok(index == 0, "Index was incremented\n");
1255 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1256 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1258 index = 0;
1259 len = sizeof(buffer);
1260 strcpy(buffer,"Warning");
1261 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1262 buffer,&len,&index),"Unable to query header\n");
1263 ok(index == 1, "Index was not incremented\n");
1264 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1265 len = sizeof(buffer);
1266 strcpy(buffer,"Warning");
1267 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1268 buffer,&len,&index),"Failed to get second header\n");
1269 ok(index == 2, "Index was not incremented\n");
1270 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1271 len = sizeof(buffer);
1272 strcpy(buffer,"Warning");
1273 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1274 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1276 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1278 index = 0;
1279 len = sizeof(buffer);
1280 strcpy(buffer,"Warning");
1281 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1282 buffer,&len,&index),"Unable to query header\n");
1283 ok(index == 1, "Index was not incremented\n");
1284 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1285 len = sizeof(buffer);
1286 strcpy(buffer,"Warning");
1287 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1288 buffer,&len,&index),"Failed to get second header\n");
1289 ok(index == 2, "Index was not incremented\n");
1290 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1291 len = sizeof(buffer);
1292 strcpy(buffer,"Warning");
1293 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1294 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1296 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1298 index = 0;
1299 len = sizeof(buffer);
1300 strcpy(buffer,"Warning");
1301 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1302 buffer,&len,&index),"Unable to query header\n");
1303 ok(index == 1, "Index was not incremented\n");
1304 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1305 len = sizeof(buffer);
1306 strcpy(buffer,"Warning");
1307 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1308 buffer,&len,&index),"Failed to get second header\n");
1309 ok(index == 2, "Index was not incremented\n");
1310 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1311 len = sizeof(buffer);
1312 strcpy(buffer,"Warning");
1313 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1314 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1316 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1318 index = 0;
1319 len = sizeof(buffer);
1320 strcpy(buffer,"Warning");
1321 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1322 buffer,&len,&index),"Unable to query header\n");
1323 ok(index == 1, "Index was not incremented\n");
1324 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1325 len = sizeof(buffer);
1326 strcpy(buffer,"Warning");
1327 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1328 ok(index == 2, "Index was not incremented\n");
1329 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1330 len = sizeof(buffer);
1331 strcpy(buffer,"Warning");
1332 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1334 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1336 index = 0;
1337 len = sizeof(buffer);
1338 strcpy(buffer,"Warning");
1339 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1340 ok(index == 1, "Index was not incremented\n");
1341 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1342 len = sizeof(buffer);
1343 strcpy(buffer,"Warning");
1344 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1345 ok(index == 2, "Index was not incremented\n");
1346 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1347 len = sizeof(buffer);
1348 strcpy(buffer,"Warning");
1349 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1351 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1353 index = 0;
1354 len = sizeof(buffer);
1355 strcpy(buffer,"Warning");
1356 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1357 ok(index == 1, "Index was not incremented\n");
1358 ok(strcmp(buffer,"test2, test4, test5; test6")==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, buffer,&len,&index),"Failed to get second header\n");
1362 ok(index == 2, "Index was not incremented\n");
1363 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1364 len = sizeof(buffer);
1365 strcpy(buffer,"Warning");
1366 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1368 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");
1370 index = 0;
1371 len = sizeof(buffer);
1372 strcpy(buffer,"Warning");
1373 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1374 ok(index == 1, "Index was not incremented\n");
1375 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1376 len = sizeof(buffer);
1377 strcpy(buffer,"Warning");
1378 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1379 ok(index == 2, "Index was not incremented\n");
1380 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1381 len = sizeof(buffer);
1382 strcpy(buffer,"Warning");
1383 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1385 /* Ensure that blank headers are ignored and don't cause a failure */
1386 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");
1388 index = 0;
1389 len = sizeof(buffer);
1390 strcpy(buffer,"BlankTest");
1391 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1392 ok(index == 1, "Index was not incremented\n");
1393 ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1395 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1396 done:
1397 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1398 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1401 static const char contmsg[] =
1402 "HTTP/1.1 100 Continue\r\n";
1404 static const char okmsg[] =
1405 "HTTP/1.1 200 OK\r\n"
1406 "Server: winetest\r\n"
1407 "\r\n";
1409 static const char okmsg2[] =
1410 "HTTP/1.1 200 OK\r\n"
1411 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1412 "Server: winetest\r\n"
1413 "Content-Length: 0\r\n"
1414 "Set-Cookie: one\r\n"
1415 "Set-Cookie: two\r\n"
1416 "\r\n";
1418 static const char notokmsg[] =
1419 "HTTP/1.1 400 Bad Request\r\n"
1420 "Server: winetest\r\n"
1421 "\r\n";
1423 static const char noauthmsg[] =
1424 "HTTP/1.1 401 Unauthorized\r\n"
1425 "Server: winetest\r\n"
1426 "Connection: close\r\n"
1427 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1428 "\r\n";
1430 static const char noauthmsg2[] =
1431 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1432 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1433 "\0d`0|6\n"
1434 "Server: winetest\r\n";
1436 static const char proxymsg[] =
1437 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1438 "Server: winetest\r\n"
1439 "Proxy-Connection: close\r\n"
1440 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1441 "\r\n";
1443 static const char page1[] =
1444 "<HTML>\r\n"
1445 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1446 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1447 "</HTML>\r\n\r\n";
1449 struct server_info {
1450 HANDLE hEvent;
1451 int port;
1454 static DWORD CALLBACK server_thread(LPVOID param)
1456 struct server_info *si = param;
1457 int r, c, i, on;
1458 SOCKET s;
1459 struct sockaddr_in sa;
1460 char buffer[0x100];
1461 WSADATA wsaData;
1462 int last_request = 0;
1463 char host_header[22];
1464 static int test_b = 0;
1466 WSAStartup(MAKEWORD(1,1), &wsaData);
1468 s = socket(AF_INET, SOCK_STREAM, 0);
1469 if (s == INVALID_SOCKET)
1470 return 1;
1472 on = 1;
1473 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1475 memset(&sa, 0, sizeof sa);
1476 sa.sin_family = AF_INET;
1477 sa.sin_port = htons(si->port);
1478 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1480 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1481 if (r<0)
1482 return 1;
1484 listen(s, 0);
1486 SetEvent(si->hEvent);
1488 sprintf(host_header, "Host: localhost:%d", si->port);
1492 c = accept(s, NULL, NULL);
1494 memset(buffer, 0, sizeof buffer);
1495 for(i=0; i<(sizeof buffer-1); i++)
1497 r = recv(c, &buffer[i], 1, 0);
1498 if (r != 1)
1499 break;
1500 if (i<4) continue;
1501 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1502 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1503 break;
1505 if (strstr(buffer, "GET /test1"))
1507 if (!strstr(buffer, "Content-Length: 0"))
1509 send(c, okmsg, sizeof okmsg-1, 0);
1510 send(c, page1, sizeof page1-1, 0);
1512 else
1513 send(c, notokmsg, sizeof notokmsg-1, 0);
1515 if (strstr(buffer, "/test2"))
1517 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1519 send(c, okmsg, sizeof okmsg-1, 0);
1520 send(c, page1, sizeof page1-1, 0);
1522 else
1523 send(c, proxymsg, sizeof proxymsg-1, 0);
1525 if (strstr(buffer, "/test3"))
1527 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1528 send(c, okmsg, sizeof okmsg-1, 0);
1529 else
1530 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1532 if (strstr(buffer, "/test4"))
1534 if (strstr(buffer, "Connection: Close"))
1535 send(c, okmsg, sizeof okmsg-1, 0);
1536 else
1537 send(c, notokmsg, sizeof notokmsg-1, 0);
1539 if (strstr(buffer, "POST /test5") ||
1540 strstr(buffer, "RPC_IN_DATA /test5") ||
1541 strstr(buffer, "RPC_OUT_DATA /test5"))
1543 if (strstr(buffer, "Content-Length: 0"))
1545 send(c, okmsg, sizeof okmsg-1, 0);
1546 send(c, page1, sizeof page1-1, 0);
1548 else
1549 send(c, notokmsg, sizeof notokmsg-1, 0);
1551 if (strstr(buffer, "GET /test6"))
1553 send(c, contmsg, sizeof contmsg-1, 0);
1554 send(c, contmsg, sizeof contmsg-1, 0);
1555 send(c, okmsg, sizeof okmsg-1, 0);
1556 send(c, page1, sizeof page1-1, 0);
1558 if (strstr(buffer, "POST /test7"))
1560 if (strstr(buffer, "Content-Length: 100"))
1562 send(c, okmsg, sizeof okmsg-1, 0);
1563 send(c, page1, sizeof page1-1, 0);
1565 else
1566 send(c, notokmsg, sizeof notokmsg-1, 0);
1568 if (strstr(buffer, "/test8"))
1570 if (!strstr(buffer, "Connection: Close") &&
1571 strstr(buffer, "Connection: Keep-Alive") &&
1572 !strstr(buffer, "Cache-Control: no-cache") &&
1573 !strstr(buffer, "Pragma: no-cache") &&
1574 strstr(buffer, host_header))
1575 send(c, okmsg, sizeof okmsg-1, 0);
1576 else
1577 send(c, notokmsg, sizeof notokmsg-1, 0);
1579 if (strstr(buffer, "/test9"))
1581 if (!strstr(buffer, "Connection: Close") &&
1582 !strstr(buffer, "Connection: Keep-Alive") &&
1583 !strstr(buffer, "Cache-Control: no-cache") &&
1584 !strstr(buffer, "Pragma: no-cache") &&
1585 strstr(buffer, host_header))
1586 send(c, okmsg, sizeof okmsg-1, 0);
1587 else
1588 send(c, notokmsg, sizeof notokmsg-1, 0);
1590 if (strstr(buffer, "/testA"))
1592 if (!strstr(buffer, "Connection: Close") &&
1593 !strstr(buffer, "Connection: Keep-Alive") &&
1594 (strstr(buffer, "Cache-Control: no-cache") ||
1595 strstr(buffer, "Pragma: no-cache")) &&
1596 strstr(buffer, host_header))
1597 send(c, okmsg, sizeof okmsg-1, 0);
1598 else
1599 send(c, notokmsg, sizeof notokmsg-1, 0);
1601 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1603 test_b = 1;
1604 send(c, okmsg, sizeof okmsg-1, 0);
1605 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1606 send(c, okmsg, sizeof okmsg-1, 0);
1608 if (strstr(buffer, "/testC"))
1610 if (strstr(buffer, "Cookie: cookie=biscuit"))
1611 send(c, okmsg, sizeof okmsg-1, 0);
1612 else
1613 send(c, notokmsg, sizeof notokmsg-1, 0);
1615 if (strstr(buffer, "/testD"))
1617 send(c, okmsg2, sizeof okmsg2-1, 0);
1619 if (strstr(buffer, "/testE"))
1621 send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1623 if (strstr(buffer, "GET /quit"))
1625 send(c, okmsg, sizeof okmsg-1, 0);
1626 send(c, page1, sizeof page1-1, 0);
1627 last_request = 1;
1630 shutdown(c, 2);
1631 closesocket(c);
1632 } while (!last_request);
1634 closesocket(s);
1636 return 0;
1639 static void test_basic_request(int port, const char *verb, const char *url)
1641 HINTERNET hi, hc, hr;
1642 DWORD r, count;
1643 char buffer[0x100];
1645 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1646 ok(hi != NULL, "open failed\n");
1648 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1649 ok(hc != NULL, "connect failed\n");
1651 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1652 ok(hr != NULL, "HttpOpenRequest failed\n");
1654 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1655 ok(r, "HttpSendRequest failed\n");
1657 count = 0;
1658 memset(buffer, 0, sizeof buffer);
1659 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1660 ok(r, "InternetReadFile failed\n");
1661 ok(count == sizeof page1 - 1, "count was wrong\n");
1662 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1664 InternetCloseHandle(hr);
1665 InternetCloseHandle(hc);
1666 InternetCloseHandle(hi);
1669 static void test_proxy_indirect(int port)
1671 HINTERNET hi, hc, hr;
1672 DWORD r, sz, val;
1673 char buffer[0x40];
1675 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1676 ok(hi != NULL, "open failed\n");
1678 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1679 ok(hc != NULL, "connect failed\n");
1681 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1682 ok(hr != NULL, "HttpOpenRequest failed\n");
1684 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1685 ok(r, "HttpSendRequest failed\n");
1687 sz = sizeof buffer;
1688 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1689 ok(r, "HttpQueryInfo failed\n");
1690 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1692 sz = sizeof buffer;
1693 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1694 ok(r, "HttpQueryInfo failed\n");
1695 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1697 sz = sizeof val;
1698 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1699 ok(r, "HttpQueryInfo failed\n");
1700 ok(val == 407, "proxy code wrong\n");
1702 sz = sizeof buffer;
1703 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1704 ok(r, "HttpQueryInfo failed\n");
1705 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1707 sz = sizeof buffer;
1708 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1709 ok(r, "HttpQueryInfo failed\n");
1710 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1712 sz = sizeof buffer;
1713 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1714 ok(r, "HttpQueryInfo failed\n");
1715 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1717 sz = sizeof buffer;
1718 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1719 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1720 ok(r == FALSE, "HttpQueryInfo failed\n");
1722 InternetCloseHandle(hr);
1723 InternetCloseHandle(hc);
1724 InternetCloseHandle(hi);
1727 static void test_proxy_direct(int port)
1729 HINTERNET hi, hc, hr;
1730 DWORD r, sz;
1731 char buffer[0x40];
1732 static CHAR username[] = "mike",
1733 password[] = "1101";
1735 sprintf(buffer, "localhost:%d\n", port);
1736 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1737 ok(hi != NULL, "open failed\n");
1739 /* try connect without authorization */
1740 hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1741 ok(hc != NULL, "connect failed\n");
1743 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1744 ok(hr != NULL, "HttpOpenRequest failed\n");
1746 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1747 ok(r, "HttpSendRequest failed\n");
1749 sz = sizeof buffer;
1750 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1751 ok(r, "HttpQueryInfo failed\n");
1752 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1755 /* set the user + password then try again */
1756 todo_wine {
1757 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1758 ok(r, "failed to set user\n");
1760 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1761 ok(r, "failed to set password\n");
1764 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1765 ok(r, "HttpSendRequest failed\n");
1766 sz = sizeof buffer;
1767 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1768 ok(r, "HttpQueryInfo failed\n");
1769 todo_wine {
1770 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1774 InternetCloseHandle(hr);
1775 InternetCloseHandle(hc);
1776 InternetCloseHandle(hi);
1779 static void test_header_handling_order(int port)
1781 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1782 static char connection[] = "Connection: Close";
1784 static const char *types[2] = { "*", NULL };
1785 HINTERNET session, connect, request;
1786 DWORD size, status;
1787 BOOL ret;
1789 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1790 ok(session != NULL, "InternetOpen failed\n");
1792 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1793 ok(connect != NULL, "InternetConnect failed\n");
1795 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1796 ok(request != NULL, "HttpOpenRequest failed\n");
1798 ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1799 ok(ret, "HttpAddRequestHeaders failed\n");
1801 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1802 ok(ret, "HttpSendRequest failed\n");
1804 status = 0;
1805 size = sizeof(status);
1806 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1807 ok(ret, "HttpQueryInfo failed\n");
1808 ok(status == 200, "request failed with status %u\n", status);
1810 InternetCloseHandle(request);
1812 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1813 ok(request != NULL, "HttpOpenRequest failed\n");
1815 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1816 ok(ret, "HttpSendRequest failed\n");
1818 status = 0;
1819 size = sizeof(status);
1820 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1821 ok(ret, "HttpQueryInfo failed\n");
1822 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1824 InternetCloseHandle(request);
1826 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1827 ok(request != NULL, "HttpOpenRequest failed\n");
1829 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1830 ok(ret, "HttpAddRequestHeaders failed\n");
1832 ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1833 ok(ret, "HttpSendRequest failed\n");
1835 status = 0;
1836 size = sizeof(status);
1837 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1838 ok(ret, "HttpQueryInfo failed\n");
1839 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1841 InternetCloseHandle(request);
1842 InternetCloseHandle(connect);
1843 InternetCloseHandle(session);
1846 static void test_connection_header(int port)
1848 HINTERNET ses, con, req;
1849 DWORD size, status;
1850 BOOL ret;
1852 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1853 ok(ses != NULL, "InternetOpen failed\n");
1855 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1856 ok(con != NULL, "InternetConnect failed\n");
1858 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1859 ok(req != NULL, "HttpOpenRequest failed\n");
1861 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1862 ok(ret, "HttpSendRequest failed\n");
1864 status = 0;
1865 size = sizeof(status);
1866 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1867 ok(ret, "HttpQueryInfo failed\n");
1868 ok(status == 200, "request failed with status %u\n", status);
1870 InternetCloseHandle(req);
1872 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1873 ok(req != NULL, "HttpOpenRequest failed\n");
1875 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1876 ok(ret, "HttpSendRequest failed\n");
1878 status = 0;
1879 size = sizeof(status);
1880 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1881 ok(ret, "HttpQueryInfo failed\n");
1882 ok(status == 200, "request failed with status %u\n", status);
1884 InternetCloseHandle(req);
1886 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1887 ok(req != NULL, "HttpOpenRequest failed\n");
1889 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1890 ok(ret, "HttpSendRequest failed\n");
1892 status = 0;
1893 size = sizeof(status);
1894 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1895 ok(ret, "HttpQueryInfo failed\n");
1896 ok(status == 200, "request failed with status %u\n", status);
1898 InternetCloseHandle(req);
1900 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1901 ok(req != NULL, "HttpOpenRequest failed\n");
1903 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1904 ok(ret, "HttpSendRequest failed\n");
1906 status = 0;
1907 size = sizeof(status);
1908 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1909 ok(ret, "HttpQueryInfo failed\n");
1910 ok(status == 200, "request failed with status %u\n", status);
1912 InternetCloseHandle(req);
1913 InternetCloseHandle(con);
1914 InternetCloseHandle(ses);
1917 static void test_http1_1(int port)
1919 HINTERNET ses, con, req;
1920 BOOL ret;
1922 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1923 ok(ses != NULL, "InternetOpen failed\n");
1925 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1926 ok(con != NULL, "InternetConnect failed\n");
1928 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1929 ok(req != NULL, "HttpOpenRequest failed\n");
1931 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1932 if (ret)
1934 InternetCloseHandle(req);
1936 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1937 ok(req != NULL, "HttpOpenRequest failed\n");
1939 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1940 todo_wine
1941 ok(ret, "HttpSendRequest failed\n");
1944 InternetCloseHandle(req);
1945 InternetCloseHandle(con);
1946 InternetCloseHandle(ses);
1949 static void test_HttpSendRequestW(int port)
1951 static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
1952 HINTERNET ses, con, req;
1953 DWORD error;
1954 BOOL ret;
1956 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
1957 ok(ses != NULL, "InternetOpen failed\n");
1959 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1960 ok(con != NULL, "InternetConnect failed\n");
1962 req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1963 ok(req != NULL, "HttpOpenRequest failed\n");
1965 SetLastError(0xdeadbeef);
1966 ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
1967 error = GetLastError();
1968 ok(!ret, "HttpSendRequestW succeeded\n");
1969 ok(error == ERROR_IO_PENDING ||
1970 broken(error == ERROR_HTTP_HEADER_NOT_FOUND) || /* IE6 */
1971 broken(error == ERROR_INVALID_PARAMETER), /* IE5 */
1972 "got %u expected ERROR_IO_PENDING\n", error);
1974 InternetCloseHandle(req);
1975 InternetCloseHandle(con);
1976 InternetCloseHandle(ses);
1979 static void test_cookie_header(int port)
1981 HINTERNET ses, con, req;
1982 DWORD size, status, error;
1983 BOOL ret;
1984 char buffer[64];
1986 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1987 ok(ses != NULL, "InternetOpen failed\n");
1989 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1990 ok(con != NULL, "InternetConnect failed\n");
1992 InternetSetCookie("http://localhost", "cookie", "biscuit");
1994 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1995 ok(req != NULL, "HttpOpenRequest failed\n");
1997 buffer[0] = 0;
1998 size = sizeof(buffer);
1999 SetLastError(0xdeadbeef);
2000 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2001 error = GetLastError();
2002 ok(!ret, "HttpQueryInfo succeeded\n");
2003 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
2005 ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
2006 ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
2008 buffer[0] = 0;
2009 size = sizeof(buffer);
2010 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2011 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2012 ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
2014 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2015 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
2017 status = 0;
2018 size = sizeof(status);
2019 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2020 ok(ret, "HttpQueryInfo failed\n");
2021 ok(status == 200, "request failed with status %u\n", status);
2023 buffer[0] = 0;
2024 size = sizeof(buffer);
2025 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2026 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2027 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
2029 InternetCloseHandle(req);
2030 InternetCloseHandle(con);
2031 InternetCloseHandle(ses);
2034 static void test_basic_authentication(int port)
2036 HINTERNET session, connect, request;
2037 DWORD size, status;
2038 BOOL ret;
2040 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2041 ok(session != NULL, "InternetOpen failed\n");
2043 connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
2044 ok(connect != NULL, "InternetConnect failed\n");
2046 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
2047 ok(request != NULL, "HttpOpenRequest failed\n");
2049 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2050 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2052 status = 0;
2053 size = sizeof(status);
2054 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2055 ok(ret, "HttpQueryInfo failed\n");
2056 ok(status == 200, "request failed with status %u\n", status);
2058 InternetCloseHandle(request);
2059 InternetCloseHandle(connect);
2060 InternetCloseHandle(session);
2063 static void test_invalid_response_headers(int port)
2065 HINTERNET session, connect, request;
2066 DWORD size, status;
2067 BOOL ret;
2068 char buffer[256];
2070 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2071 ok(session != NULL, "InternetOpen failed\n");
2073 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2074 ok(connect != NULL, "InternetConnect failed\n");
2076 request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
2077 ok(request != NULL, "HttpOpenRequest failed\n");
2079 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2080 ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2082 status = 0;
2083 size = sizeof(status);
2084 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2085 ok(ret, "HttpQueryInfo failed\n");
2086 ok(status == 401, "unexpected status %u\n", status);
2088 buffer[0] = 0;
2089 size = sizeof(buffer);
2090 ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2091 ok(ret, "HttpQueryInfo failed\n");
2092 ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
2093 "headers wrong \"%s\"\n", buffer);
2095 buffer[0] = 0;
2096 size = sizeof(buffer);
2097 ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
2098 ok(ret, "HttpQueryInfo failed\n");
2099 ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
2101 InternetCloseHandle(request);
2102 InternetCloseHandle(connect);
2103 InternetCloseHandle(session);
2106 static void test_HttpQueryInfo(int port)
2108 HINTERNET hi, hc, hr;
2109 DWORD size, index;
2110 char buffer[1024];
2111 BOOL ret;
2113 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2114 ok(hi != NULL, "InternetOpen failed\n");
2116 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2117 ok(hc != NULL, "InternetConnect failed\n");
2119 hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2120 ok(hr != NULL, "HttpOpenRequest failed\n");
2122 ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2123 ok(ret, "HttpSendRequest failed\n");
2125 index = 0;
2126 size = sizeof(buffer);
2127 ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2128 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2129 ok(index == 1, "expected 1 got %u\n", index);
2131 index = 0;
2132 size = sizeof(buffer);
2133 ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2134 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2135 ok(index == 1, "expected 1 got %u\n", index);
2137 index = 0;
2138 size = sizeof(buffer);
2139 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2140 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2141 ok(index == 0, "expected 0 got %u\n", index);
2143 size = sizeof(buffer);
2144 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2145 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2146 ok(index == 0, "expected 0 got %u\n", index);
2148 size = sizeof(buffer);
2149 ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2150 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2151 ok(index == 0, "expected 0 got %u\n", index);
2153 size = sizeof(buffer);
2154 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2155 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2156 ok(index == 0, "expected 0 got %u\n", index);
2158 size = sizeof(buffer);
2159 ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2160 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2161 ok(index == 0, "expected 0 got %u\n", index);
2163 index = 0;
2164 size = sizeof(buffer);
2165 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2166 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2167 ok(index == 0, "expected 0 got %u\n", index);
2169 index = 0;
2170 size = sizeof(buffer);
2171 ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2172 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2173 ok(index == 0, "expected 0 got %u\n", index);
2175 index = 0xdeadbeef;
2176 size = sizeof(buffer);
2177 ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2178 ok(!ret, "HttpQueryInfo succeeded\n");
2179 ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2181 index = 0;
2182 size = sizeof(buffer);
2183 ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2184 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2185 ok(index == 1, "expected 1 got %u\n", index);
2187 index = 0;
2188 size = sizeof(buffer);
2189 strcpy(buffer, "Server");
2190 ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2191 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2192 ok(index == 1, "expected 1 got %u\n", index);
2194 index = 0;
2195 size = sizeof(buffer);
2196 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2197 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2198 ok(index == 1, "expected 1 got %u\n", index);
2200 size = sizeof(buffer);
2201 ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2202 ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2203 ok(index == 2, "expected 2 got %u\n", index);
2205 InternetCloseHandle(hr);
2206 InternetCloseHandle(hc);
2207 InternetCloseHandle(hi);
2210 static void test_http_connection(void)
2212 struct server_info si;
2213 HANDLE hThread;
2214 DWORD id = 0, r;
2216 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2217 si.port = 7531;
2219 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2220 ok( hThread != NULL, "create thread failed\n");
2222 r = WaitForSingleObject(si.hEvent, 10000);
2223 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2224 if (r != WAIT_OBJECT_0)
2225 return;
2227 test_basic_request(si.port, "GET", "/test1");
2228 test_proxy_indirect(si.port);
2229 test_proxy_direct(si.port);
2230 test_header_handling_order(si.port);
2231 test_basic_request(si.port, "POST", "/test5");
2232 test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2233 test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2234 test_basic_request(si.port, "GET", "/test6");
2235 test_connection_header(si.port);
2236 test_http1_1(si.port);
2237 test_cookie_header(si.port);
2238 test_basic_authentication(si.port);
2239 test_invalid_response_headers(si.port);
2240 test_HttpQueryInfo(si.port);
2241 test_HttpSendRequestW(si.port);
2243 /* send the basic request again to shutdown the server thread */
2244 test_basic_request(si.port, "GET", "/quit");
2246 r = WaitForSingleObject(hThread, 3000);
2247 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2248 CloseHandle(hThread);
2251 static void test_user_agent_header(void)
2253 HINTERNET ses, con, req;
2254 DWORD size, err;
2255 char buffer[64];
2256 BOOL ret;
2258 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2259 ok(ses != NULL, "InternetOpen failed\n");
2261 con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2262 ok(con != NULL, "InternetConnect failed\n");
2264 req = HttpOpenRequest(con, "GET", "/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2265 ok(req != NULL, "HttpOpenRequest failed\n");
2267 size = sizeof(buffer);
2268 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2269 err = GetLastError();
2270 ok(!ret, "HttpQueryInfo succeeded\n");
2271 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2273 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2274 ok(ret, "HttpAddRequestHeaders succeeded\n");
2276 size = sizeof(buffer);
2277 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2278 err = GetLastError();
2279 ok(ret, "HttpQueryInfo failed\n");
2280 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2282 InternetCloseHandle(req);
2284 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2285 ok(req != NULL, "HttpOpenRequest failed\n");
2287 size = sizeof(buffer);
2288 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2289 err = GetLastError();
2290 ok(!ret, "HttpQueryInfo succeeded\n");
2291 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2293 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2294 ok(ret, "HttpAddRequestHeaders failed\n");
2296 buffer[0] = 0;
2297 size = sizeof(buffer);
2298 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2299 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2300 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2302 InternetCloseHandle(req);
2303 InternetCloseHandle(con);
2304 InternetCloseHandle(ses);
2307 static void test_bogus_accept_types_array(void)
2309 HINTERNET ses, con, req;
2310 static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2311 DWORD size;
2312 char buffer[32];
2313 BOOL ret;
2315 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2316 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2317 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2319 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2321 buffer[0] = 0;
2322 size = sizeof(buffer);
2323 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2324 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2325 ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2326 !strcmp(buffer, "*/*, %p, */*"),
2327 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2329 InternetCloseHandle(req);
2330 InternetCloseHandle(con);
2331 InternetCloseHandle(ses);
2334 struct context
2336 HANDLE event;
2337 HINTERNET req;
2340 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2342 INTERNET_ASYNC_RESULT *result = info;
2343 struct context *ctx = (struct context *)context;
2345 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2347 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2349 trace("request handle: 0x%08lx\n", result->dwResult);
2350 ctx->req = (HINTERNET)result->dwResult;
2351 SetEvent(ctx->event);
2353 if (status == INTERNET_STATUS_HANDLE_CLOSING)
2355 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2357 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2358 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2359 SetEvent(ctx->event);
2363 static void test_open_url_async(void)
2365 BOOL ret;
2366 HINTERNET ses, req;
2367 DWORD size, error;
2368 struct context ctx;
2369 ULONG type;
2371 ctx.req = NULL;
2372 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2374 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2375 ok(ses != NULL, "InternetOpen failed\n");
2377 SetLastError(0xdeadbeef);
2378 ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2379 error = GetLastError();
2380 ok(!ret, "InternetSetOptionA succeeded\n");
2381 ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2383 ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2384 error = GetLastError();
2385 ok(!ret, "InternetSetOptionA failed\n");
2386 ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2388 pInternetSetStatusCallbackA(ses, cb);
2389 ResetEvent(ctx.event);
2391 req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2392 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2394 WaitForSingleObject(ctx.event, INFINITE);
2396 type = 0;
2397 size = sizeof(type);
2398 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2399 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2400 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2401 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2403 size = 0;
2404 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2405 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2406 ok(size > 0, "expected size > 0\n");
2408 ResetEvent(ctx.event);
2409 InternetCloseHandle(ctx.req);
2410 WaitForSingleObject(ctx.event, INFINITE);
2412 InternetCloseHandle(ses);
2413 CloseHandle(ctx.event);
2416 enum api
2418 internet_connect = 1,
2419 http_open_request,
2420 http_send_request_ex,
2421 internet_writefile,
2422 http_end_request,
2423 internet_close_handle
2426 struct notification
2428 enum api function; /* api responsible for notification */
2429 unsigned int status; /* status received */
2430 int async; /* delivered from another thread? */
2431 int todo;
2432 int optional;
2435 struct info
2437 enum api function;
2438 const struct notification *test;
2439 unsigned int count;
2440 unsigned int index;
2441 HANDLE wait;
2442 DWORD thread;
2443 unsigned int line;
2446 static CRITICAL_SECTION notification_cs;
2448 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
2450 BOOL status_ok, function_ok;
2451 struct info *info = (struct info *)context;
2452 unsigned int i;
2454 EnterCriticalSection( &notification_cs );
2456 if (status == INTERNET_STATUS_HANDLE_CREATED)
2458 DWORD size = sizeof(struct info *);
2459 HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
2461 i = info->index;
2462 if (i >= info->count)
2464 LeaveCriticalSection( &notification_cs );
2465 return;
2468 while (info->test[i].status != status && info->test[i].optional &&
2469 i < info->count - 1 &&
2470 info->test[i].function == info->test[i + 1].function)
2472 i++;
2475 status_ok = (info->test[i].status == status);
2476 function_ok = (info->test[i].function == info->function);
2478 if (!info->test[i].todo)
2480 ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2481 ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2483 if (info->test[i].async)
2484 ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
2485 info->line, info->thread, GetCurrentThreadId());
2487 else
2489 todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2490 if (status_ok)
2491 todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2493 if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
2494 info->index = i+1;
2496 LeaveCriticalSection( &notification_cs );
2499 static void setup_test( struct info *info, enum api function, unsigned int line )
2501 info->function = function;
2502 info->line = line;
2505 static const struct notification async_send_request_ex_test[] =
2507 { internet_connect, INTERNET_STATUS_HANDLE_CREATED, 0 },
2508 { http_open_request, INTERNET_STATUS_HANDLE_CREATED, 0 },
2509 { http_send_request_ex, INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
2510 { http_send_request_ex, INTERNET_STATUS_RESOLVING_NAME, 1 },
2511 { http_send_request_ex, INTERNET_STATUS_NAME_RESOLVED, 1 },
2512 { http_send_request_ex, INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
2513 { http_send_request_ex, INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
2514 { http_send_request_ex, INTERNET_STATUS_SENDING_REQUEST, 1 },
2515 { http_send_request_ex, INTERNET_STATUS_REQUEST_SENT, 1 },
2516 { http_send_request_ex, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2517 { internet_writefile, INTERNET_STATUS_SENDING_REQUEST, 0 },
2518 { internet_writefile, INTERNET_STATUS_REQUEST_SENT, 0 },
2519 { http_end_request, INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
2520 { http_end_request, INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
2521 { http_end_request, INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2522 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 },
2523 { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 }
2526 static void test_async_HttpSendRequestEx(void)
2528 BOOL ret;
2529 HINTERNET ses, req, con;
2530 struct info info;
2531 DWORD size, written, error;
2532 INTERNET_BUFFERSA b;
2533 static const char *accept[2] = {"*/*", NULL};
2534 static char data[] = "Public ID=codeweavers";
2535 char buffer[32];
2537 InitializeCriticalSection( &notification_cs );
2539 info.test = async_send_request_ex_test;
2540 info.count = sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]);
2541 info.index = 0;
2542 info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
2543 info.thread = GetCurrentThreadId();
2545 ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
2546 ok( ses != NULL, "InternetOpen failed\n" );
2548 pInternetSetStatusCallbackA( ses, check_notification );
2550 setup_test( &info, internet_connect, __LINE__ );
2551 con = InternetConnect( ses, "crossover.codeweavers.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
2552 ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
2554 WaitForSingleObject( info.wait, 10000 );
2556 setup_test( &info, http_open_request, __LINE__ );
2557 req = HttpOpenRequest( con, "POST", "posttest.php", NULL, NULL, accept, 0, (DWORD_PTR)&info );
2558 ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
2560 WaitForSingleObject( info.wait, 10000 );
2562 memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
2563 b.dwStructSize = sizeof(INTERNET_BUFFERSA);
2564 b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
2565 b.dwHeadersLength = strlen( b.lpcszHeader );
2566 b.dwBufferTotal = strlen( data );
2568 setup_test( &info, http_send_request_ex, __LINE__ );
2569 ret = HttpSendRequestExA( req, &b, NULL, 0x28, 0 );
2570 ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
2572 WaitForSingleObject( info.wait, 10000 );
2574 size = sizeof(buffer);
2575 SetLastError( 0xdeadbeef );
2576 ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
2577 error = GetLastError();
2578 ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
2579 todo_wine
2580 ok( error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
2581 "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
2583 written = 0;
2584 size = strlen( data );
2585 setup_test( &info, internet_writefile, __LINE__ );
2586 ret = InternetWriteFile( req, data, size, &written );
2587 ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
2588 ok( written == size, "expected %u got %u\n", written, size );
2590 WaitForSingleObject( info.wait, 10000 );
2592 SetLastError( 0xdeadbeef );
2593 ret = HttpEndRequestA( req, (void *)data, 0x28, 0 );
2594 error = GetLastError();
2595 ok( !ret, "HttpEndRequestA succeeded\n" );
2596 ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
2598 SetLastError( 0xdeadbeef );
2599 setup_test( &info, http_end_request, __LINE__ );
2600 ret = HttpEndRequestA( req, NULL, 0x28, 0 );
2601 error = GetLastError();
2602 ok( !ret, "HttpEndRequestA succeeded\n" );
2603 ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
2605 WaitForSingleObject( info.wait, 10000 );
2607 setup_test( &info, internet_close_handle, __LINE__ );
2608 InternetCloseHandle( req );
2609 InternetCloseHandle( con );
2610 InternetCloseHandle( ses );
2612 WaitForSingleObject( info.wait, 10000 );
2613 Sleep(100);
2614 CloseHandle( info.wait );
2617 #define STATUS_STRING(status) \
2618 memcpy(status_string[status], #status, sizeof(CHAR) * \
2619 (strlen(#status) < MAX_STATUS_NAME ? \
2620 strlen(#status) : \
2621 MAX_STATUS_NAME - 1))
2622 static void init_status_tests(void)
2624 memset(expect, 0, sizeof(expect));
2625 memset(optional, 0, sizeof(optional));
2626 memset(wine_allow, 0, sizeof(wine_allow));
2627 memset(notified, 0, sizeof(notified));
2628 memset(status_string, 0, sizeof(status_string));
2629 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2630 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2631 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2632 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2633 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2634 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2635 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2636 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2637 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2638 STATUS_STRING(INTERNET_STATUS_PREFETCH);
2639 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2640 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2641 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2642 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2643 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2644 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2645 STATUS_STRING(INTERNET_STATUS_REDIRECT);
2646 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2647 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2648 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2649 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2650 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2651 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2652 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2653 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2654 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2656 #undef STATUS_STRING
2658 START_TEST(http)
2660 HMODULE hdll;
2661 hdll = GetModuleHandleA("wininet.dll");
2662 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2664 if (!pInternetSetStatusCallbackA)
2665 skip("skipping the InternetReadFile tests\n");
2666 else
2668 init_status_tests();
2669 InternetReadFile_test(INTERNET_FLAG_ASYNC);
2670 InternetReadFile_test(0);
2671 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2672 test_open_url_async();
2673 test_async_HttpSendRequestEx();
2675 InternetOpenRequest_test();
2676 test_http_cache();
2677 InternetOpenUrlA_test();
2678 HttpHeaders_test();
2679 test_http_connection();
2680 test_user_agent_header();
2681 test_bogus_accept_types_array();
2682 InternetReadFile_chunked_test();
2683 HttpSendRequestEx_test();