push 57b67fa3cb0bad7ec17bcd37e0e1e936855340bd
[wine/hacks.git] / dlls / wininet / tests / http.c
blobb6677b601272e76896d2df99239c0bdb82aa25d7
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://www.winehq.org/site/about"
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] == (num), "expected status %d (%s) %d times, received %d times\n", \
88 status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
89 status_string[status] : "unknown", (num), notified[status]); \
90 CLEAR_NOTIFIED(status); \
91 }while(0)
93 #define CHECK_NOTIFIED(status) \
94 CHECK_NOTIFIED2(status, 1)
96 #define CHECK_NOT_NOTIFIED(status) \
97 CHECK_NOTIFIED2(status, 0)
99 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
100 #define MAX_STATUS_NAME 50
101 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
102 wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
103 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
105 static HANDLE hCompleteEvent;
107 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
110 static VOID WINAPI callback(
111 HINTERNET hInternet,
112 DWORD_PTR dwContext,
113 DWORD dwInternetStatus,
114 LPVOID lpvStatusInformation,
115 DWORD dwStatusInformationLength
118 CHECK_EXPECT(dwInternetStatus);
119 switch (dwInternetStatus)
121 case INTERNET_STATUS_RESOLVING_NAME:
122 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
123 GetCurrentThreadId(), hInternet, dwContext,
124 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
125 *(LPSTR)lpvStatusInformation = '\0';
126 break;
127 case INTERNET_STATUS_NAME_RESOLVED:
128 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
129 GetCurrentThreadId(), hInternet, dwContext,
130 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
131 *(LPSTR)lpvStatusInformation = '\0';
132 break;
133 case INTERNET_STATUS_CONNECTING_TO_SERVER:
134 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
135 GetCurrentThreadId(), hInternet, dwContext,
136 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
137 *(LPSTR)lpvStatusInformation = '\0';
138 break;
139 case INTERNET_STATUS_CONNECTED_TO_SERVER:
140 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
141 GetCurrentThreadId(), hInternet, dwContext,
142 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
143 *(LPSTR)lpvStatusInformation = '\0';
144 break;
145 case INTERNET_STATUS_SENDING_REQUEST:
146 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
147 GetCurrentThreadId(), hInternet, dwContext,
148 lpvStatusInformation,dwStatusInformationLength);
149 break;
150 case INTERNET_STATUS_REQUEST_SENT:
151 ok(dwStatusInformationLength == sizeof(DWORD),
152 "info length should be sizeof(DWORD) instead of %d\n",
153 dwStatusInformationLength);
154 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
155 GetCurrentThreadId(), hInternet, dwContext,
156 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
157 break;
158 case INTERNET_STATUS_RECEIVING_RESPONSE:
159 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
160 GetCurrentThreadId(), hInternet, dwContext,
161 lpvStatusInformation,dwStatusInformationLength);
162 break;
163 case INTERNET_STATUS_RESPONSE_RECEIVED:
164 ok(dwStatusInformationLength == sizeof(DWORD),
165 "info length should be sizeof(DWORD) instead of %d\n",
166 dwStatusInformationLength);
167 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
168 GetCurrentThreadId(), hInternet, dwContext,
169 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
170 break;
171 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
172 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
173 GetCurrentThreadId(), hInternet,dwContext,
174 lpvStatusInformation,dwStatusInformationLength);
175 break;
176 case INTERNET_STATUS_PREFETCH:
177 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
178 GetCurrentThreadId(), hInternet, dwContext,
179 lpvStatusInformation,dwStatusInformationLength);
180 break;
181 case INTERNET_STATUS_CLOSING_CONNECTION:
182 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
183 GetCurrentThreadId(), hInternet, dwContext,
184 lpvStatusInformation,dwStatusInformationLength);
185 break;
186 case INTERNET_STATUS_CONNECTION_CLOSED:
187 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
188 GetCurrentThreadId(), hInternet, dwContext,
189 lpvStatusInformation,dwStatusInformationLength);
190 break;
191 case INTERNET_STATUS_HANDLE_CREATED:
192 ok(dwStatusInformationLength == sizeof(HINTERNET),
193 "info length should be sizeof(HINTERNET) instead of %d\n",
194 dwStatusInformationLength);
195 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
196 GetCurrentThreadId(), hInternet, dwContext,
197 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
198 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
199 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
200 break;
201 case INTERNET_STATUS_HANDLE_CLOSING:
202 ok(dwStatusInformationLength == sizeof(HINTERNET),
203 "info length should be sizeof(HINTERNET) instead of %d\n",
204 dwStatusInformationLength);
205 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
206 GetCurrentThreadId(), hInternet, dwContext,
207 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
208 break;
209 case INTERNET_STATUS_REQUEST_COMPLETE:
211 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
212 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
213 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
214 dwStatusInformationLength);
215 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
216 GetCurrentThreadId(), hInternet, dwContext,
217 iar->dwResult,iar->dwError,dwStatusInformationLength);
218 SetEvent(hCompleteEvent);
219 break;
221 case INTERNET_STATUS_REDIRECT:
222 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
223 GetCurrentThreadId(), hInternet, dwContext,
224 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
225 *(LPSTR)lpvStatusInformation = '\0';
226 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
227 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
228 break;
229 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
230 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
231 GetCurrentThreadId(), hInternet, dwContext,
232 lpvStatusInformation, dwStatusInformationLength);
233 break;
234 default:
235 trace("%04x:Callback %p 0x%lx %d %p %d\n",
236 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
237 lpvStatusInformation, dwStatusInformationLength);
241 static void InternetReadFile_test(int flags)
243 BOOL res;
244 DWORD rc;
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, "www.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", "/about/", 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://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
300 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
301 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
302 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
303 if (first_connection_to_test_url)
305 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
306 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
307 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
308 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
310 else
312 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
313 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
315 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
316 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
317 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
318 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
319 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
320 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
321 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
322 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
323 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
324 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
325 SET_EXPECT(INTERNET_STATUS_REDIRECT);
326 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
327 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
328 if (flags & INTERNET_FLAG_ASYNC)
329 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
330 else
331 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
333 trace("HttpSendRequestA -->\n");
334 SetLastError(0xdeadbeef);
335 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
336 if (flags & INTERNET_FLAG_ASYNC)
337 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
338 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
339 else
340 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
341 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
342 trace("HttpSendRequestA <--\n");
344 if (flags & INTERNET_FLAG_ASYNC)
345 WaitForSingleObject(hCompleteEvent, INFINITE);
347 todo_wine if (first_connection_to_test_url)
349 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
350 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
352 else
354 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
355 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
357 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
358 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
359 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
360 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
361 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
362 if (flags & INTERNET_FLAG_ASYNC)
363 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
364 else
365 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
366 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
367 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
368 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
370 length = 4;
371 rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
372 trace("Option 0x17 -> %i %i\n",rc,out);
374 length = 100;
375 rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
376 trace("Option 0x22 -> %i %s\n",rc,buffer);
378 length = 4000;
379 rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
380 buffer[length]=0;
381 trace("Option 0x16 -> %i %s\n",rc,buffer);
383 length = sizeof(buffer);
384 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
385 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
386 ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
388 length = 16;
389 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
390 trace("Option 0x5 -> %i %s (%u)\n",rc,buffer,GetLastError());
392 length = 100;
393 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
394 buffer[length]=0;
395 trace("Option 0x1 -> %i %s\n",rc,buffer);
397 SetLastError(0xdeadbeef);
398 rc = InternetReadFile(NULL, buffer, 100, &length);
399 ok(!rc, "InternetReadFile should have failed\n");
400 ok(GetLastError() == ERROR_INVALID_HANDLE,
401 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
402 GetLastError());
404 length = 100;
405 trace("Entering Query loop\n");
407 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
408 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
409 while (TRUE)
411 if (flags & INTERNET_FLAG_ASYNC)
412 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
413 rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
414 ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
415 ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
416 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
417 if (flags & INTERNET_FLAG_ASYNC)
419 if (rc != 0)
421 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
423 else if (GetLastError() == ERROR_IO_PENDING)
425 WaitForSingleObject(hCompleteEvent, INFINITE);
426 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
427 continue;
430 if (length)
432 char *buffer;
433 buffer = HeapAlloc(GetProcessHeap(),0,length+1);
435 rc = InternetReadFile(hor,buffer,length,&length);
437 buffer[length]=0;
439 trace("ReadFile -> %i %i\n",rc,length);
441 HeapFree(GetProcessHeap(),0,buffer);
443 if (length == 0)
444 break;
446 /* WinXP does not send, but Win98 does */
447 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
448 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
449 abort:
450 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
451 if (hor != 0x0) {
452 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
453 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
454 SetLastError(0xdeadbeef);
455 rc = InternetCloseHandle(hor);
456 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
457 SetLastError(0xdeadbeef);
458 rc = InternetCloseHandle(hor);
459 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
460 ok (GetLastError() == ERROR_INVALID_HANDLE,
461 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
462 GetLastError());
464 /* We intentionally do not close the handle opened by InternetConnectA as this
465 * tickles bug #9479: native closes child internet handles when the parent handles
466 * are closed. This is verified below by checking that the number of
467 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
468 if (hi != 0x0) {
469 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
470 rc = InternetCloseHandle(hi);
471 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
472 if (flags & INTERNET_FLAG_ASYNC)
473 Sleep(100);
475 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
476 if (hor != 0x0 && (flags & INTERNET_FLAG_ASYNC)) todo_wine
478 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
479 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
481 else
483 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
484 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
486 CloseHandle(hCompleteEvent);
487 first_connection_to_test_url = FALSE;
490 static void InternetReadFileExA_test(int flags)
492 DWORD rc;
493 DWORD length;
494 const char *types[2] = { "*", NULL };
495 HINTERNET hi, hic = 0, hor = 0;
496 INTERNET_BUFFERS inetbuffers;
498 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
500 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
502 trace("InternetOpenA <--\n");
503 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
504 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
505 trace("InternetOpenA -->\n");
507 if (hi == 0x0) goto abort;
509 pInternetSetStatusCallbackA(hi,&callback);
511 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
513 trace("InternetConnectA <--\n");
514 hic=InternetConnectA(hi, "www.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 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
522 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
524 trace("HttpOpenRequestA <--\n");
525 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
526 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
527 0xdeadbead);
528 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
530 * If the internet name can't be resolved we are probably behind
531 * a firewall or in some other way not directly connected to the
532 * Internet. Not enough reason to fail the test. Just ignore and
533 * abort.
535 } else {
536 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
538 trace("HttpOpenRequestA -->\n");
540 if (hor == 0x0) goto abort;
542 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
543 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
544 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
545 if (first_connection_to_test_url)
547 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
548 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
549 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
550 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
552 else
554 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
555 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
557 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
558 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
559 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
560 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
561 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
562 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
563 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
564 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
565 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
566 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
567 SET_EXPECT(INTERNET_STATUS_REDIRECT);
568 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
569 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
570 if (flags & INTERNET_FLAG_ASYNC)
571 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
572 else
573 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
575 trace("HttpSendRequestA -->\n");
576 SetLastError(0xdeadbeef);
577 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
578 if (flags & INTERNET_FLAG_ASYNC)
579 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
580 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
581 else
582 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
583 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
584 trace("HttpSendRequestA <--\n");
586 if (!rc && (GetLastError() == ERROR_IO_PENDING))
587 WaitForSingleObject(hCompleteEvent, INFINITE);
589 if (first_connection_to_test_url)
591 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
592 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
594 else todo_wine
596 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
597 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
599 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
600 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
601 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
602 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
603 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
604 if (flags & INTERNET_FLAG_ASYNC)
605 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
606 else
607 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
608 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
609 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
610 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
612 /* tests invalid dwStructSize */
613 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
614 inetbuffers.lpcszHeader = NULL;
615 inetbuffers.dwHeadersLength = 0;
616 inetbuffers.dwBufferLength = 10;
617 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
618 inetbuffers.dwOffsetHigh = 1234;
619 inetbuffers.dwOffsetLow = 5678;
620 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
621 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
622 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
623 rc ? "TRUE" : "FALSE", GetLastError());
624 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
626 /* tests to see whether lpcszHeader is used - it isn't */
627 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
628 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
629 inetbuffers.dwHeadersLength = 255;
630 inetbuffers.dwBufferLength = 0;
631 inetbuffers.lpvBuffer = NULL;
632 inetbuffers.dwOffsetHigh = 1234;
633 inetbuffers.dwOffsetLow = 5678;
634 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
635 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
636 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
637 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
638 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
639 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
640 trace("read %i bytes\n", inetbuffers.dwBufferLength);
641 todo_wine
643 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
644 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
647 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
648 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
649 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
650 rc ? "TRUE" : "FALSE", GetLastError());
652 length = 0;
653 trace("Entering Query loop\n");
655 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
656 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
657 while (TRUE)
659 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
660 inetbuffers.dwBufferLength = 1024;
661 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
662 inetbuffers.dwOffsetHigh = 1234;
663 inetbuffers.dwOffsetLow = 5678;
665 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
666 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
667 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
668 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
669 if (!rc)
671 if (GetLastError() == ERROR_IO_PENDING)
673 trace("InternetReadFileEx -> PENDING\n");
674 ok(flags & INTERNET_FLAG_ASYNC,
675 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
676 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
677 WaitForSingleObject(hCompleteEvent, INFINITE);
678 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
679 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
681 else
683 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
684 break;
687 else
689 trace("InternetReadFileEx -> SUCCEEDED\n");
690 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
691 if (inetbuffers.dwBufferLength)
693 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
694 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
696 else
698 /* Win98 still sends these when 0 bytes are read, WinXP does not */
699 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
700 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
704 trace("read %i bytes\n", inetbuffers.dwBufferLength);
705 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
707 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
708 "InternetReadFileEx sets offsets to 0x%x%08x\n",
709 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
711 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
713 if (!inetbuffers.dwBufferLength)
714 break;
716 length += inetbuffers.dwBufferLength;
718 todo_wine ok(length > 0, "failed to read any of the document\n");
719 trace("Finished. Read %d bytes\n", length);
721 /* WinXP does not send, but Win98 does */
722 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
723 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
724 abort:
725 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
726 if (hor) {
727 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
728 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
729 rc = InternetCloseHandle(hor);
730 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
731 rc = InternetCloseHandle(hor);
732 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
734 if (hic) {
735 rc = InternetCloseHandle(hic);
736 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
738 if (hi) {
739 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
740 rc = InternetCloseHandle(hi);
741 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
742 if (flags & INTERNET_FLAG_ASYNC)
743 Sleep(100);
744 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
746 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
747 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
748 CloseHandle(hCompleteEvent);
749 first_connection_to_test_url = FALSE;
752 static void InternetOpenUrlA_test(void)
754 HINTERNET myhinternet, myhttp;
755 char buffer[0x400];
756 DWORD size, readbytes, totalbytes=0;
757 BOOL ret;
759 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
760 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
761 size = 0x400;
762 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
763 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
765 SetLastError(0);
766 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
767 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
768 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
769 return; /* WinXP returns this when not connected to the net */
770 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
771 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
772 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
773 totalbytes += readbytes;
774 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
775 totalbytes += readbytes;
776 trace("read 0x%08x bytes\n",totalbytes);
778 InternetCloseHandle(myhttp);
779 InternetCloseHandle(myhinternet);
782 static void HttpSendRequestEx_test(void)
784 HINTERNET hSession;
785 HINTERNET hConnect;
786 HINTERNET hRequest;
788 INTERNET_BUFFERS BufferIn;
789 DWORD dwBytesWritten;
790 DWORD dwBytesRead;
791 CHAR szBuffer[256];
792 int i;
793 BOOL ret;
795 static char szPostData[] = "mode=Test";
796 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
798 hSession = InternetOpen("Wine Regression Test",
799 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
800 ok( hSession != NULL ,"Unable to open Internet session\n");
801 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
802 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
804 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
805 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
806 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
807 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
809 skip( "Network unreachable, skipping test\n" );
810 goto done;
812 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
815 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
816 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
817 BufferIn.lpcszHeader = szContentType;
818 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
819 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
820 BufferIn.lpvBuffer = szPostData;
821 BufferIn.dwBufferLength = 3;
822 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
823 BufferIn.dwOffsetLow = 0;
824 BufferIn.dwOffsetHigh = 0;
826 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
827 ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
829 for (i = 3; szPostData[i]; i++)
830 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
831 "InternetWriteFile failed\n");
833 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
835 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
836 "Unable to read response\n");
837 szBuffer[dwBytesRead] = 0;
839 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
840 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
842 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
843 done:
844 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
845 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
848 static void InternetOpenRequest_test(void)
850 HINTERNET session, connect, request;
851 static const char *types[] = { "*", "", NULL };
852 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
853 static const WCHAR *typesW[] = { any, empty, NULL };
854 BOOL ret;
856 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
857 ok(session != NULL ,"Unable to open Internet session\n");
859 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
860 INTERNET_SERVICE_HTTP, 0, 0);
861 ok(connect == NULL, "InternetConnectA should have failed\n");
862 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
864 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
865 INTERNET_SERVICE_HTTP, 0, 0);
866 ok(connect == NULL, "InternetConnectA should have failed\n");
867 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
869 connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
870 INTERNET_SERVICE_HTTP, 0, 0);
871 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
873 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
874 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
876 skip( "Network unreachable, skipping test\n" );
877 goto done;
879 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
881 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
882 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
883 ok(InternetCloseHandle(request), "Close request handle failed\n");
885 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
886 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
888 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
889 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
890 ok(InternetCloseHandle(request), "Close request handle failed\n");
892 done:
893 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
894 ok(InternetCloseHandle(session), "Close session handle failed\n");
897 static void test_http_cache(void)
899 HINTERNET session, connect, request;
900 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
901 DWORD size, file_size;
902 BYTE buf[100];
903 HANDLE file;
904 BOOL ret;
906 static const char *types[] = { "*", "", NULL };
908 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
909 ok(session != NULL ,"Unable to open Internet session\n");
911 connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
912 INTERNET_SERVICE_HTTP, 0, 0);
913 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
915 request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
916 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
918 skip( "Network unreachable, skipping test\n" );
920 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
921 ok(InternetCloseHandle(session), "Close session handle failed\n");
923 return;
925 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
927 size = sizeof(url);
928 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
929 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
930 ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
932 size = sizeof(file_name);
933 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
934 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
935 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
936 ok(!size, "size = %d\n", size);
938 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
939 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
941 size = sizeof(file_name);
942 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
943 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
945 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
946 FILE_ATTRIBUTE_NORMAL, NULL);
947 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
948 file_size = GetFileSize(file, NULL);
949 ok(file_size == 0, "file size=%d\n", file_size);
951 ret = InternetReadFile(request, buf, sizeof(buf), &size);
952 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
953 ok(size == sizeof(buf), "size=%d\n", size);
955 file_size = GetFileSize(file, NULL);
956 ok(file_size == sizeof(buf), "file size=%d\n", file_size);
957 CloseHandle(file);
959 ok(InternetCloseHandle(request), "Close request handle failed\n");
961 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
962 FILE_ATTRIBUTE_NORMAL, NULL);
963 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
964 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
966 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
967 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
969 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
970 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
971 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
972 ok(!size, "size = %d\n", size);
974 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
975 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
977 size = sizeof(file_name);
978 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
979 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
980 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
981 ok(!size, "size = %d\n", size);
983 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
984 FILE_ATTRIBUTE_NORMAL, NULL);
985 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
986 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
988 ok(InternetCloseHandle(request), "Close request handle failed\n");
990 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
991 ok(InternetCloseHandle(session), "Close session handle failed\n");
994 static void HttpHeaders_test(void)
996 HINTERNET hSession;
997 HINTERNET hConnect;
998 HINTERNET hRequest;
999 CHAR buffer[256];
1000 WCHAR wbuffer[256];
1001 DWORD len = 256;
1002 DWORD oldlen;
1003 DWORD index = 0;
1005 hSession = InternetOpen("Wine Regression Test",
1006 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1007 ok( hSession != NULL ,"Unable to open Internet session\n");
1008 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1009 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1011 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1012 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1013 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1014 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1016 skip( "Network unreachable, skipping test\n" );
1017 goto done;
1019 ok( hRequest != NULL, "Failed to open request handle\n");
1021 index = 0;
1022 len = sizeof(buffer);
1023 strcpy(buffer,"Warning");
1024 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1025 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1027 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1028 "Failed to add new header\n");
1030 index = 0;
1031 len = sizeof(buffer);
1032 strcpy(buffer,"Warning");
1033 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1034 buffer,&len,&index),"Unable to query header\n");
1035 ok(index == 1, "Index was not incremented\n");
1036 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1037 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1038 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1039 len = sizeof(buffer);
1040 strcpy(buffer,"Warning");
1041 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1042 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1044 index = 0;
1045 len = 5; /* could store the string but not the NULL terminator */
1046 strcpy(buffer,"Warning");
1047 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1048 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1049 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1050 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1052 /* a call with NULL will fail but will return the length */
1053 index = 0;
1054 len = sizeof(buffer);
1055 SetLastError(0xdeadbeef);
1056 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1057 NULL,&len,&index) == FALSE,"Query worked\n");
1058 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1059 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1060 ok(index == 0, "Index was incremented\n");
1062 /* even for a len that is too small */
1063 index = 0;
1064 len = 15;
1065 SetLastError(0xdeadbeef);
1066 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1067 NULL,&len,&index) == FALSE,"Query worked\n");
1068 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1069 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1070 ok(index == 0, "Index was incremented\n");
1072 index = 0;
1073 len = 0;
1074 SetLastError(0xdeadbeef);
1075 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1076 NULL,&len,&index) == FALSE,"Query worked\n");
1077 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1078 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1079 ok(index == 0, "Index was incremented\n");
1080 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1083 /* a working query */
1084 index = 0;
1085 len = sizeof(buffer);
1086 memset(buffer, 'x', sizeof(buffer));
1087 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1088 buffer,&len,&index),"Unable to query header\n");
1089 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1090 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1091 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1092 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1093 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1094 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1095 ok(index == 0, "Index was incremented\n");
1097 /* Like above two tests, but for W version */
1099 index = 0;
1100 len = 0;
1101 SetLastError(0xdeadbeef);
1102 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1103 NULL,&len,&index) == FALSE,"Query worked\n");
1104 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1105 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1106 ok(index == 0, "Index was incremented\n");
1107 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1109 /* a working query */
1110 index = 0;
1111 len = sizeof(wbuffer);
1112 memset(wbuffer, 'x', sizeof(wbuffer));
1113 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1114 wbuffer,&len,&index),"Unable to query header\n");
1115 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1116 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1117 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1118 ok(index == 0, "Index was incremented\n");
1120 /* end of W version tests */
1122 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1123 index = 0;
1124 len = sizeof(buffer);
1125 memset(buffer, 'x', sizeof(buffer));
1126 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1127 buffer,&len,&index) == TRUE,"Query failed\n");
1128 ok(len == 2, "Expected 2, got %d\n", len);
1129 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1130 ok(index == 0, "Index was incremented\n");
1132 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1133 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1135 index = 0;
1136 len = sizeof(buffer);
1137 strcpy(buffer,"Warning");
1138 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1139 buffer,&len,&index),"Unable to query header\n");
1140 ok(index == 1, "Index was not incremented\n");
1141 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1142 len = sizeof(buffer);
1143 strcpy(buffer,"Warning");
1144 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1145 buffer,&len,&index),"Failed to get second header\n");
1146 ok(index == 2, "Index was not incremented\n");
1147 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1148 len = sizeof(buffer);
1149 strcpy(buffer,"Warning");
1150 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1151 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1153 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1155 index = 0;
1156 len = sizeof(buffer);
1157 strcpy(buffer,"Warning");
1158 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1159 buffer,&len,&index),"Unable to query header\n");
1160 ok(index == 1, "Index was not incremented\n");
1161 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1162 len = sizeof(buffer);
1163 strcpy(buffer,"Warning");
1164 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1165 buffer,&len,&index),"Failed to get second header\n");
1166 ok(index == 2, "Index was not incremented\n");
1167 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1168 len = sizeof(buffer);
1169 strcpy(buffer,"Warning");
1170 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1171 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1173 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1175 index = 0;
1176 len = sizeof(buffer);
1177 strcpy(buffer,"Warning");
1178 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1179 buffer,&len,&index),"Unable to query header\n");
1180 ok(index == 1, "Index was not incremented\n");
1181 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1182 len = sizeof(buffer);
1183 strcpy(buffer,"Warning");
1184 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1185 buffer,&len,&index),"Failed to get second header\n");
1186 ok(index == 2, "Index was not incremented\n");
1187 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1188 len = sizeof(buffer);
1189 strcpy(buffer,"Warning");
1190 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1191 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1193 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1195 index = 0;
1196 len = sizeof(buffer);
1197 strcpy(buffer,"Warning");
1198 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1199 buffer,&len,&index),"Unable to query header\n");
1200 ok(index == 1, "Index was not incremented\n");
1201 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1202 len = sizeof(buffer);
1203 strcpy(buffer,"Warning");
1204 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1205 ok(index == 2, "Index was not incremented\n");
1206 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1207 len = sizeof(buffer);
1208 strcpy(buffer,"Warning");
1209 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1211 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1213 index = 0;
1214 len = sizeof(buffer);
1215 strcpy(buffer,"Warning");
1216 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1217 ok(index == 1, "Index was not incremented\n");
1218 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1219 len = sizeof(buffer);
1220 strcpy(buffer,"Warning");
1221 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1222 ok(index == 2, "Index was not incremented\n");
1223 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1224 len = sizeof(buffer);
1225 strcpy(buffer,"Warning");
1226 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1228 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1230 index = 0;
1231 len = sizeof(buffer);
1232 strcpy(buffer,"Warning");
1233 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1234 ok(index == 1, "Index was not incremented\n");
1235 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1236 len = sizeof(buffer);
1237 strcpy(buffer,"Warning");
1238 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1239 ok(index == 2, "Index was not incremented\n");
1240 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1241 len = sizeof(buffer);
1242 strcpy(buffer,"Warning");
1243 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1245 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");
1247 index = 0;
1248 len = sizeof(buffer);
1249 strcpy(buffer,"Warning");
1250 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1251 ok(index == 1, "Index was not incremented\n");
1252 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1253 len = sizeof(buffer);
1254 strcpy(buffer,"Warning");
1255 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1256 ok(index == 2, "Index was not incremented\n");
1257 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1258 len = sizeof(buffer);
1259 strcpy(buffer,"Warning");
1260 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1263 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1264 done:
1265 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1266 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1269 static const char contmsg[] =
1270 "HTTP/1.1 100 Continue\r\n";
1272 static const char okmsg[] =
1273 "HTTP/1.1 200 OK\r\n"
1274 "Server: winetest\r\n"
1275 "\r\n";
1277 static const char notokmsg[] =
1278 "HTTP/1.1 400 Bad Request\r\n"
1279 "Server: winetest\r\n"
1280 "\r\n";
1282 static const char noauthmsg[] =
1283 "HTTP/1.1 401 Unauthorized\r\n"
1284 "Server: winetest\r\n"
1285 "\r\n";
1287 static const char proxymsg[] =
1288 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1289 "Server: winetest\r\n"
1290 "Proxy-Connection: close\r\n"
1291 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1292 "\r\n";
1294 static const char page1[] =
1295 "<HTML>\r\n"
1296 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1297 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1298 "</HTML>\r\n\r\n";
1300 struct server_info {
1301 HANDLE hEvent;
1302 int port;
1305 static DWORD CALLBACK server_thread(LPVOID param)
1307 struct server_info *si = param;
1308 int r, c, i, on;
1309 SOCKET s;
1310 struct sockaddr_in sa;
1311 char buffer[0x100];
1312 WSADATA wsaData;
1313 int last_request = 0;
1314 char host_header[22];
1315 static int test_b = 0;
1317 WSAStartup(MAKEWORD(1,1), &wsaData);
1319 s = socket(AF_INET, SOCK_STREAM, 0);
1320 if (s == INVALID_SOCKET)
1321 return 1;
1323 on = 1;
1324 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1326 memset(&sa, 0, sizeof sa);
1327 sa.sin_family = AF_INET;
1328 sa.sin_port = htons(si->port);
1329 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1331 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1332 if (r<0)
1333 return 1;
1335 listen(s, 0);
1337 SetEvent(si->hEvent);
1339 sprintf(host_header, "Host: localhost:%d", si->port);
1343 c = accept(s, NULL, NULL);
1345 memset(buffer, 0, sizeof buffer);
1346 for(i=0; i<(sizeof buffer-1); i++)
1348 r = recv(c, &buffer[i], 1, 0);
1349 if (r != 1)
1350 break;
1351 if (i<4) continue;
1352 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1353 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1354 break;
1356 if (strstr(buffer, "GET /test1"))
1358 if (!strstr(buffer, "Content-Length: 0"))
1360 send(c, okmsg, sizeof okmsg-1, 0);
1361 send(c, page1, sizeof page1-1, 0);
1363 else
1364 send(c, notokmsg, sizeof notokmsg-1, 0);
1366 if (strstr(buffer, "/test2"))
1368 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1370 send(c, okmsg, sizeof okmsg-1, 0);
1371 send(c, page1, sizeof page1-1, 0);
1373 else
1374 send(c, proxymsg, sizeof proxymsg-1, 0);
1376 if (strstr(buffer, "/test3"))
1378 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1379 send(c, okmsg, sizeof okmsg-1, 0);
1380 else
1381 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1383 if (strstr(buffer, "/test4"))
1385 if (strstr(buffer, "Connection: Close"))
1386 send(c, okmsg, sizeof okmsg-1, 0);
1387 else
1388 send(c, notokmsg, sizeof notokmsg-1, 0);
1390 if (strstr(buffer, "POST /test5"))
1392 if (strstr(buffer, "Content-Length: 0"))
1394 send(c, okmsg, sizeof okmsg-1, 0);
1395 send(c, page1, sizeof page1-1, 0);
1397 else
1398 send(c, notokmsg, sizeof notokmsg-1, 0);
1400 if (strstr(buffer, "GET /test6"))
1402 send(c, contmsg, sizeof contmsg-1, 0);
1403 send(c, contmsg, sizeof contmsg-1, 0);
1404 send(c, okmsg, sizeof okmsg-1, 0);
1405 send(c, page1, sizeof page1-1, 0);
1407 if (strstr(buffer, "POST /test7"))
1409 if (strstr(buffer, "Content-Length: 100"))
1411 send(c, okmsg, sizeof okmsg-1, 0);
1412 send(c, page1, sizeof page1-1, 0);
1414 else
1415 send(c, notokmsg, sizeof notokmsg-1, 0);
1417 if (strstr(buffer, "/test8"))
1419 if (!strstr(buffer, "Connection: Close") &&
1420 strstr(buffer, "Connection: Keep-Alive") &&
1421 !strstr(buffer, "Cache-Control: no-cache") &&
1422 !strstr(buffer, "Pragma: no-cache") &&
1423 strstr(buffer, host_header))
1424 send(c, okmsg, sizeof okmsg-1, 0);
1425 else
1426 send(c, notokmsg, sizeof notokmsg-1, 0);
1428 if (strstr(buffer, "/test9"))
1430 if (!strstr(buffer, "Connection: Close") &&
1431 !strstr(buffer, "Connection: Keep-Alive") &&
1432 !strstr(buffer, "Cache-Control: no-cache") &&
1433 !strstr(buffer, "Pragma: no-cache") &&
1434 strstr(buffer, host_header))
1435 send(c, okmsg, sizeof okmsg-1, 0);
1436 else
1437 send(c, notokmsg, sizeof notokmsg-1, 0);
1439 if (strstr(buffer, "/testA"))
1441 if (!strstr(buffer, "Connection: Close") &&
1442 !strstr(buffer, "Connection: Keep-Alive") &&
1443 (strstr(buffer, "Cache-Control: no-cache") ||
1444 strstr(buffer, "Pragma: no-cache")) &&
1445 strstr(buffer, host_header))
1446 send(c, okmsg, sizeof okmsg-1, 0);
1447 else
1448 send(c, notokmsg, sizeof notokmsg-1, 0);
1450 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1452 test_b = 1;
1453 send(c, okmsg, sizeof okmsg-1, 0);
1454 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1455 send(c, okmsg, sizeof okmsg-1, 0);
1457 if (strstr(buffer, "/testC"))
1459 if (strstr(buffer, "Cookie: cookie=biscuit"))
1460 send(c, okmsg, sizeof okmsg-1, 0);
1461 else
1462 send(c, notokmsg, sizeof notokmsg-1, 0);
1464 if (strstr(buffer, "GET /quit"))
1466 send(c, okmsg, sizeof okmsg-1, 0);
1467 send(c, page1, sizeof page1-1, 0);
1468 last_request = 1;
1471 shutdown(c, 2);
1472 closesocket(c);
1473 } while (!last_request);
1475 closesocket(s);
1477 return 0;
1480 static void test_basic_request(int port, const char *verb, const char *url)
1482 HINTERNET hi, hc, hr;
1483 DWORD r, count;
1484 char buffer[0x100];
1486 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1487 ok(hi != NULL, "open failed\n");
1489 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1490 ok(hc != NULL, "connect failed\n");
1492 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1493 ok(hr != NULL, "HttpOpenRequest failed\n");
1495 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1496 ok(r, "HttpSendRequest failed\n");
1498 count = 0;
1499 memset(buffer, 0, sizeof buffer);
1500 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1501 ok(r, "InternetReadFile failed\n");
1502 ok(count == sizeof page1 - 1, "count was wrong\n");
1503 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1505 InternetCloseHandle(hr);
1506 InternetCloseHandle(hc);
1507 InternetCloseHandle(hi);
1510 static void test_proxy_indirect(int port)
1512 HINTERNET hi, hc, hr;
1513 DWORD r, sz, val;
1514 char buffer[0x40];
1516 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1517 ok(hi != NULL, "open failed\n");
1519 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1520 ok(hc != NULL, "connect failed\n");
1522 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1523 ok(hr != NULL, "HttpOpenRequest failed\n");
1525 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1526 ok(r, "HttpSendRequest failed\n");
1528 sz = sizeof buffer;
1529 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1530 ok(r, "HttpQueryInfo failed\n");
1531 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1533 sz = sizeof buffer;
1534 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1535 ok(r, "HttpQueryInfo failed\n");
1536 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1538 sz = sizeof val;
1539 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1540 ok(r, "HttpQueryInfo failed\n");
1541 ok(val == 407, "proxy code wrong\n");
1543 sz = sizeof buffer;
1544 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1545 ok(r, "HttpQueryInfo failed\n");
1546 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1548 sz = sizeof buffer;
1549 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1550 ok(r, "HttpQueryInfo failed\n");
1551 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1553 sz = sizeof buffer;
1554 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1555 ok(r, "HttpQueryInfo failed\n");
1556 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1558 sz = sizeof buffer;
1559 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1560 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1561 ok(r == FALSE, "HttpQueryInfo failed\n");
1563 InternetCloseHandle(hr);
1564 InternetCloseHandle(hc);
1565 InternetCloseHandle(hi);
1568 static void test_proxy_direct(int port)
1570 HINTERNET hi, hc, hr;
1571 DWORD r, sz;
1572 char buffer[0x40];
1573 static CHAR username[] = "mike",
1574 password[] = "1101";
1576 sprintf(buffer, "localhost:%d\n", port);
1577 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1578 ok(hi != NULL, "open failed\n");
1580 /* try connect without authorization */
1581 hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1582 ok(hc != NULL, "connect failed\n");
1584 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1585 ok(hr != NULL, "HttpOpenRequest failed\n");
1587 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1588 ok(r, "HttpSendRequest failed\n");
1590 sz = sizeof buffer;
1591 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1592 ok(r, "HttpQueryInfo failed\n");
1593 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1596 /* set the user + password then try again */
1597 todo_wine {
1598 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1599 ok(r, "failed to set user\n");
1601 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1602 ok(r, "failed to set password\n");
1605 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1606 ok(r, "HttpSendRequest failed\n");
1607 sz = sizeof buffer;
1608 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1609 ok(r, "HttpQueryInfo failed\n");
1610 todo_wine {
1611 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1615 InternetCloseHandle(hr);
1616 InternetCloseHandle(hc);
1617 InternetCloseHandle(hi);
1620 static void test_header_handling_order(int port)
1622 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1623 static char connection[] = "Connection: Close";
1625 static const char *types[2] = { "*", NULL };
1626 HINTERNET session, connect, request;
1627 DWORD size, status;
1628 BOOL ret;
1630 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1631 ok(session != NULL, "InternetOpen failed\n");
1633 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1634 ok(connect != NULL, "InternetConnect failed\n");
1636 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1637 ok(request != NULL, "HttpOpenRequest failed\n");
1639 ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1640 ok(ret, "HttpAddRequestHeaders failed\n");
1642 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1643 ok(ret, "HttpSendRequest failed\n");
1645 status = 0;
1646 size = sizeof(status);
1647 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1648 ok(ret, "HttpQueryInfo failed\n");
1649 ok(status == 200, "request failed with status %u\n", status);
1651 InternetCloseHandle(request);
1653 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1654 ok(request != NULL, "HttpOpenRequest failed\n");
1656 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1657 ok(ret, "HttpSendRequest failed\n");
1659 status = 0;
1660 size = sizeof(status);
1661 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1662 ok(ret, "HttpQueryInfo failed\n");
1663 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1665 InternetCloseHandle(request);
1667 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1668 ok(request != NULL, "HttpOpenRequest failed\n");
1670 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1671 ok(ret, "HttpAddRequestHeaders failed\n");
1673 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1674 ok(ret, "HttpSendRequest failed\n");
1676 status = 0;
1677 size = sizeof(status);
1678 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1679 ok(ret, "HttpQueryInfo failed\n");
1680 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1682 InternetCloseHandle(request);
1683 InternetCloseHandle(connect);
1684 InternetCloseHandle(session);
1687 static void test_connection_header(int port)
1689 HINTERNET ses, con, req;
1690 DWORD size, status;
1691 BOOL ret;
1693 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1694 ok(ses != NULL, "InternetOpen failed\n");
1696 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1697 ok(con != NULL, "InternetConnect failed\n");
1699 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1700 ok(req != NULL, "HttpOpenRequest failed\n");
1702 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1703 ok(ret, "HttpSendRequest failed\n");
1705 status = 0;
1706 size = sizeof(status);
1707 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1708 ok(ret, "HttpQueryInfo failed\n");
1709 ok(status == 200, "request failed with status %u\n", status);
1711 InternetCloseHandle(req);
1713 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1714 ok(req != NULL, "HttpOpenRequest failed\n");
1716 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1717 ok(ret, "HttpSendRequest failed\n");
1719 status = 0;
1720 size = sizeof(status);
1721 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1722 ok(ret, "HttpQueryInfo failed\n");
1723 ok(status == 200, "request failed with status %u\n", status);
1725 InternetCloseHandle(req);
1727 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1728 ok(req != NULL, "HttpOpenRequest failed\n");
1730 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1731 ok(ret, "HttpSendRequest failed\n");
1733 status = 0;
1734 size = sizeof(status);
1735 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1736 ok(ret, "HttpQueryInfo failed\n");
1737 ok(status == 200, "request failed with status %u\n", status);
1739 InternetCloseHandle(req);
1741 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1742 ok(req != NULL, "HttpOpenRequest failed\n");
1744 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1745 ok(ret, "HttpSendRequest failed\n");
1747 status = 0;
1748 size = sizeof(status);
1749 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1750 ok(ret, "HttpQueryInfo failed\n");
1751 ok(status == 200, "request failed with status %u\n", status);
1753 InternetCloseHandle(req);
1754 InternetCloseHandle(con);
1755 InternetCloseHandle(ses);
1758 static void test_http1_1(int port)
1760 HINTERNET ses, con, req;
1761 BOOL ret;
1763 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1764 ok(ses != NULL, "InternetOpen failed\n");
1766 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1767 ok(con != NULL, "InternetConnect failed\n");
1769 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1770 ok(req != NULL, "HttpOpenRequest failed\n");
1772 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1773 if (ret)
1775 InternetCloseHandle(req);
1777 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1778 ok(req != NULL, "HttpOpenRequest failed\n");
1780 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1781 todo_wine
1782 ok(ret, "HttpSendRequest failed\n");
1785 InternetCloseHandle(req);
1786 InternetCloseHandle(con);
1787 InternetCloseHandle(ses);
1790 static void test_cookie_header(int port)
1792 HINTERNET ses, con, req;
1793 DWORD size, status, error;
1794 BOOL ret;
1795 char buffer[64];
1797 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1798 ok(ses != NULL, "InternetOpen failed\n");
1800 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1801 ok(con != NULL, "InternetConnect failed\n");
1803 InternetSetCookie("http://localhost", "cookie", "biscuit");
1805 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1806 ok(req != NULL, "HttpOpenRequest failed\n");
1808 buffer[0] = 0;
1809 size = sizeof(buffer);
1810 SetLastError(0xdeadbeef);
1811 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1812 error = GetLastError();
1813 ok(!ret, "HttpQueryInfo succeeded\n");
1814 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
1816 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1817 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1819 status = 0;
1820 size = sizeof(status);
1821 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1822 ok(ret, "HttpQueryInfo failed\n");
1823 ok(status == 200, "request failed with status %u\n", status);
1825 buffer[0] = 0;
1826 size = sizeof(buffer);
1827 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1828 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1829 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
1831 InternetCloseHandle(req);
1832 InternetCloseHandle(con);
1833 InternetCloseHandle(ses);
1836 static void test_http_connection(void)
1838 struct server_info si;
1839 HANDLE hThread;
1840 DWORD id = 0, r;
1842 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1843 si.port = 7531;
1845 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1846 ok( hThread != NULL, "create thread failed\n");
1848 r = WaitForSingleObject(si.hEvent, 10000);
1849 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1850 if (r != WAIT_OBJECT_0)
1851 return;
1853 test_basic_request(si.port, "GET", "/test1");
1854 test_proxy_indirect(si.port);
1855 test_proxy_direct(si.port);
1856 test_header_handling_order(si.port);
1857 test_basic_request(si.port, "POST", "/test5");
1858 test_basic_request(si.port, "GET", "/test6");
1859 test_connection_header(si.port);
1860 test_http1_1(si.port);
1861 test_cookie_header(si.port);
1863 /* send the basic request again to shutdown the server thread */
1864 test_basic_request(si.port, "GET", "/quit");
1866 r = WaitForSingleObject(hThread, 3000);
1867 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1868 CloseHandle(hThread);
1871 static void test_user_agent_header(void)
1873 HINTERNET ses, con, req;
1874 DWORD size, err;
1875 char buffer[64];
1876 BOOL ret;
1878 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1879 ok(ses != NULL, "InternetOpen failed\n");
1881 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1882 ok(con != NULL, "InternetConnect failed\n");
1884 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1885 ok(req != NULL, "HttpOpenRequest failed\n");
1887 size = sizeof(buffer);
1888 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1889 err = GetLastError();
1890 ok(!ret, "HttpQueryInfo succeeded\n");
1891 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1893 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1894 ok(ret, "HttpAddRequestHeaders succeeded\n");
1896 size = sizeof(buffer);
1897 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1898 err = GetLastError();
1899 ok(ret, "HttpQueryInfo failed\n");
1900 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1902 InternetCloseHandle(req);
1904 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1905 ok(req != NULL, "HttpOpenRequest failed\n");
1907 size = sizeof(buffer);
1908 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1909 err = GetLastError();
1910 ok(!ret, "HttpQueryInfo succeeded\n");
1911 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
1913 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1914 ok(ret, "HttpAddRequestHeaders failed\n");
1916 buffer[0] = 0;
1917 size = sizeof(buffer);
1918 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1919 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1920 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
1922 InternetCloseHandle(req);
1923 InternetCloseHandle(con);
1924 InternetCloseHandle(ses);
1927 static void test_bogus_accept_types_array(void)
1929 HINTERNET ses, con, req;
1930 static const char *types[] = { (const char *)6240, "*/*", "%p", "", "*/*", NULL };
1931 DWORD size;
1932 char buffer[32];
1933 BOOL ret;
1935 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
1936 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1937 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
1939 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1941 buffer[0] = 0;
1942 size = sizeof(buffer);
1943 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1944 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1945 ok(!strcmp(buffer, ", */*, %p, , */*") || /* IE6 */
1946 !strcmp(buffer, "*/*, %p, */*"),
1947 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , */*'\n", buffer);
1949 InternetCloseHandle(req);
1950 InternetCloseHandle(con);
1951 InternetCloseHandle(ses);
1954 struct context
1956 HANDLE event;
1957 HINTERNET req;
1960 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
1962 INTERNET_ASYNC_RESULT *result = info;
1963 struct context *ctx = (struct context *)context;
1965 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
1967 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
1969 trace("request handle: 0x%08lx\n", result->dwResult);
1970 ctx->req = (HINTERNET)result->dwResult;
1971 SetEvent(ctx->event);
1973 if (status == INTERNET_STATUS_HANDLE_CLOSING)
1975 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
1977 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
1978 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
1979 SetEvent(ctx->event);
1983 static void test_open_url_async(void)
1985 BOOL ret;
1986 HINTERNET ses, req;
1987 DWORD size;
1988 struct context ctx;
1989 ULONG type;
1991 ctx.req = NULL;
1992 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
1994 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
1995 ok(ses != NULL, "InternetOpen failed\n");
1997 pInternetSetStatusCallbackA(ses, cb);
1998 ResetEvent(ctx.event);
2000 req = InternetOpenUrl(ses, "http://www.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2001 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2003 WaitForSingleObject(ctx.event, INFINITE);
2005 type = 0;
2006 size = sizeof(type);
2007 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2008 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2009 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2010 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2012 size = 0;
2013 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2014 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2015 ok(size > 0, "expected size > 0\n");
2017 ResetEvent(ctx.event);
2018 InternetCloseHandle(ctx.req);
2019 WaitForSingleObject(ctx.event, INFINITE);
2021 InternetCloseHandle(ses);
2022 CloseHandle(ctx.event);
2025 #define STATUS_STRING(status) \
2026 memcpy(status_string[status], #status, sizeof(CHAR) * \
2027 (strlen(#status) < MAX_STATUS_NAME ? \
2028 strlen(#status) : \
2029 MAX_STATUS_NAME - 1))
2030 static void init_status_tests(void)
2032 memset(expect, 0, sizeof(expect));
2033 memset(optional, 0, sizeof(optional));
2034 memset(wine_allow, 0, sizeof(wine_allow));
2035 memset(notified, 0, sizeof(notified));
2036 memset(status_string, 0, sizeof(status_string));
2037 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2038 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2039 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2040 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2041 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2042 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2043 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2044 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2045 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2046 STATUS_STRING(INTERNET_STATUS_PREFETCH);
2047 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2048 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2049 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2050 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2051 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2052 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2053 STATUS_STRING(INTERNET_STATUS_REDIRECT);
2054 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2055 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2056 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2057 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2058 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2059 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2060 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2061 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2062 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2064 #undef STATUS_STRING
2066 START_TEST(http)
2068 HMODULE hdll;
2069 hdll = GetModuleHandleA("wininet.dll");
2070 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2072 if (!pInternetSetStatusCallbackA)
2073 skip("skipping the InternetReadFile tests\n");
2074 else
2076 init_status_tests();
2077 InternetReadFile_test(INTERNET_FLAG_ASYNC);
2078 InternetReadFile_test(0);
2079 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2080 test_open_url_async();
2082 InternetOpenRequest_test();
2083 test_http_cache();
2084 InternetOpenUrlA_test();
2085 HttpSendRequestEx_test();
2086 HttpHeaders_test();
2087 test_http_connection();
2088 test_user_agent_header();
2089 test_bogus_accept_types_array();