wininet: Add tests for InternetTimeFromSystemTimeW with insufficient buffer.
[wine/wine64.git] / dlls / wininet / tests / http.c
blobdb4f075c370c47414b0999afea1c7799645d760c
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);
108 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
109 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
110 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
111 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
114 static VOID WINAPI callback(
115 HINTERNET hInternet,
116 DWORD_PTR dwContext,
117 DWORD dwInternetStatus,
118 LPVOID lpvStatusInformation,
119 DWORD dwStatusInformationLength
122 CHECK_EXPECT(dwInternetStatus);
123 switch (dwInternetStatus)
125 case INTERNET_STATUS_RESOLVING_NAME:
126 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
127 GetCurrentThreadId(), hInternet, dwContext,
128 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
129 *(LPSTR)lpvStatusInformation = '\0';
130 break;
131 case INTERNET_STATUS_NAME_RESOLVED:
132 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
133 GetCurrentThreadId(), hInternet, dwContext,
134 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
135 *(LPSTR)lpvStatusInformation = '\0';
136 break;
137 case INTERNET_STATUS_CONNECTING_TO_SERVER:
138 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
139 GetCurrentThreadId(), hInternet, dwContext,
140 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
141 *(LPSTR)lpvStatusInformation = '\0';
142 break;
143 case INTERNET_STATUS_CONNECTED_TO_SERVER:
144 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
145 GetCurrentThreadId(), hInternet, dwContext,
146 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
147 *(LPSTR)lpvStatusInformation = '\0';
148 break;
149 case INTERNET_STATUS_SENDING_REQUEST:
150 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
151 GetCurrentThreadId(), hInternet, dwContext,
152 lpvStatusInformation,dwStatusInformationLength);
153 break;
154 case INTERNET_STATUS_REQUEST_SENT:
155 ok(dwStatusInformationLength == sizeof(DWORD),
156 "info length should be sizeof(DWORD) instead of %d\n",
157 dwStatusInformationLength);
158 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
159 GetCurrentThreadId(), hInternet, dwContext,
160 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
161 break;
162 case INTERNET_STATUS_RECEIVING_RESPONSE:
163 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
164 GetCurrentThreadId(), hInternet, dwContext,
165 lpvStatusInformation,dwStatusInformationLength);
166 break;
167 case INTERNET_STATUS_RESPONSE_RECEIVED:
168 ok(dwStatusInformationLength == sizeof(DWORD),
169 "info length should be sizeof(DWORD) instead of %d\n",
170 dwStatusInformationLength);
171 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
172 GetCurrentThreadId(), hInternet, dwContext,
173 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
174 break;
175 case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
176 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
177 GetCurrentThreadId(), hInternet,dwContext,
178 lpvStatusInformation,dwStatusInformationLength);
179 break;
180 case INTERNET_STATUS_PREFETCH:
181 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
182 GetCurrentThreadId(), hInternet, dwContext,
183 lpvStatusInformation,dwStatusInformationLength);
184 break;
185 case INTERNET_STATUS_CLOSING_CONNECTION:
186 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
187 GetCurrentThreadId(), hInternet, dwContext,
188 lpvStatusInformation,dwStatusInformationLength);
189 break;
190 case INTERNET_STATUS_CONNECTION_CLOSED:
191 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
192 GetCurrentThreadId(), hInternet, dwContext,
193 lpvStatusInformation,dwStatusInformationLength);
194 break;
195 case INTERNET_STATUS_HANDLE_CREATED:
196 ok(dwStatusInformationLength == sizeof(HINTERNET),
197 "info length should be sizeof(HINTERNET) instead of %d\n",
198 dwStatusInformationLength);
199 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
200 GetCurrentThreadId(), hInternet, dwContext,
201 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
202 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
203 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
204 break;
205 case INTERNET_STATUS_HANDLE_CLOSING:
206 ok(dwStatusInformationLength == sizeof(HINTERNET),
207 "info length should be sizeof(HINTERNET) instead of %d\n",
208 dwStatusInformationLength);
209 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
210 GetCurrentThreadId(), hInternet, dwContext,
211 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
212 break;
213 case INTERNET_STATUS_REQUEST_COMPLETE:
215 INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
216 ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
217 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
218 dwStatusInformationLength);
219 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
220 GetCurrentThreadId(), hInternet, dwContext,
221 iar->dwResult,iar->dwError,dwStatusInformationLength);
222 SetEvent(hCompleteEvent);
223 break;
225 case INTERNET_STATUS_REDIRECT:
226 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
227 GetCurrentThreadId(), hInternet, dwContext,
228 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
229 *(LPSTR)lpvStatusInformation = '\0';
230 CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
231 SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
232 break;
233 case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
234 trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
235 GetCurrentThreadId(), hInternet, dwContext,
236 lpvStatusInformation, dwStatusInformationLength);
237 break;
238 default:
239 trace("%04x:Callback %p 0x%lx %d %p %d\n",
240 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
241 lpvStatusInformation, dwStatusInformationLength);
245 static void InternetReadFile_test(int flags)
247 BOOL res;
248 DWORD rc;
249 CHAR buffer[4000];
250 DWORD length;
251 DWORD out;
252 const char *types[2] = { "*", NULL };
253 HINTERNET hi, hic = 0, hor = 0;
255 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
257 trace("Starting InternetReadFile test with flags 0x%x\n",flags);
259 trace("InternetOpenA <--\n");
260 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
261 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
262 trace("InternetOpenA -->\n");
264 if (hi == 0x0) goto abort;
266 pInternetSetStatusCallbackA(hi,&callback);
268 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
270 trace("InternetConnectA <--\n");
271 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
272 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
273 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
274 trace("InternetConnectA -->\n");
276 if (hic == 0x0) goto abort;
278 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
279 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
281 trace("HttpOpenRequestA <--\n");
282 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
283 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
284 0xdeadbead);
285 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
287 * If the internet name can't be resolved we are probably behind
288 * a firewall or in some other way not directly connected to the
289 * Internet. Not enough reason to fail the test. Just ignore and
290 * abort.
292 } else {
293 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
295 trace("HttpOpenRequestA -->\n");
297 if (hor == 0x0) goto abort;
299 length = sizeof(buffer);
300 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
301 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
302 ok(!strcmp(buffer, "http://www.winehq.org/about/"), "Wrong URL %s\n", buffer);
304 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
305 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
306 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
307 if (first_connection_to_test_url)
309 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
310 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
311 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
312 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
314 else
316 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
317 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
319 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
320 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
321 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
322 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
323 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
324 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
325 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
326 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
327 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
328 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
329 SET_EXPECT(INTERNET_STATUS_REDIRECT);
330 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
331 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
332 if (flags & INTERNET_FLAG_ASYNC)
333 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
334 else
335 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
337 trace("HttpSendRequestA -->\n");
338 SetLastError(0xdeadbeef);
339 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
340 if (flags & INTERNET_FLAG_ASYNC)
341 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
342 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
343 else
344 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
345 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
346 trace("HttpSendRequestA <--\n");
348 if (flags & INTERNET_FLAG_ASYNC)
349 WaitForSingleObject(hCompleteEvent, INFINITE);
351 todo_wine if (first_connection_to_test_url)
353 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
354 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
356 else
358 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
359 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
361 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
362 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
363 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
364 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
365 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
366 if (flags & INTERNET_FLAG_ASYNC)
367 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
368 else
369 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
370 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
371 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
372 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
374 length = 4;
375 rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
376 trace("Option 0x17 -> %i %i\n",rc,out);
378 length = 100;
379 rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
380 trace("Option 0x22 -> %i %s\n",rc,buffer);
382 length = 4000;
383 rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
384 buffer[length]=0;
385 trace("Option 0x16 -> %i %s\n",rc,buffer);
387 length = sizeof(buffer);
388 res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
389 ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
390 ok(!strcmp(buffer, "http://www.winehq.org/site/about"), "Wrong URL %s\n", buffer);
392 length = 16;
393 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
394 trace("Option 0x5 -> %i %s (%u)\n",rc,buffer,GetLastError());
396 length = 100;
397 rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
398 buffer[length]=0;
399 trace("Option 0x1 -> %i %s\n",rc,buffer);
401 SetLastError(0xdeadbeef);
402 rc = InternetReadFile(NULL, buffer, 100, &length);
403 ok(!rc, "InternetReadFile should have failed\n");
404 ok(GetLastError() == ERROR_INVALID_HANDLE,
405 "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
406 GetLastError());
408 length = 100;
409 trace("Entering Query loop\n");
411 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
412 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
413 while (TRUE)
415 if (flags & INTERNET_FLAG_ASYNC)
416 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
417 rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
418 ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
419 ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
420 "InternetQueryDataAvailable failed, error %d\n", GetLastError());
421 if (flags & INTERNET_FLAG_ASYNC)
423 if (rc != 0)
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 rc = InternetReadFile(hor,buffer,length,&length);
441 buffer[length]=0;
443 trace("ReadFile -> %i %i\n",rc,length);
445 HeapFree(GetProcessHeap(),0,buffer);
447 if (length == 0)
448 break;
450 /* WinXP does not send, but Win98 does */
451 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
452 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
453 abort:
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 rc = InternetCloseHandle(hor);
460 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
461 SetLastError(0xdeadbeef);
462 rc = InternetCloseHandle(hor);
463 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
464 ok (GetLastError() == ERROR_INVALID_HANDLE,
465 "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
466 GetLastError());
468 /* We intentionally do not close the handle opened by InternetConnectA as this
469 * tickles bug #9479: native closes child internet handles when the parent handles
470 * are closed. This is verified below by checking that the number of
471 * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
472 if (hi != 0x0) {
473 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
474 rc = InternetCloseHandle(hi);
475 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
476 if (flags & INTERNET_FLAG_ASYNC)
477 Sleep(100);
479 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
480 if (hor != 0x0 && (flags & INTERNET_FLAG_ASYNC)) todo_wine
482 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
483 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
485 else
487 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
488 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
490 CloseHandle(hCompleteEvent);
491 first_connection_to_test_url = FALSE;
494 static void InternetReadFileExA_test(int flags)
496 DWORD rc;
497 DWORD length;
498 const char *types[2] = { "*", NULL };
499 HINTERNET hi, hic = 0, hor = 0;
500 INTERNET_BUFFERS inetbuffers;
502 hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
504 trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
506 trace("InternetOpenA <--\n");
507 hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
508 ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
509 trace("InternetOpenA -->\n");
511 if (hi == 0x0) goto abort;
513 pInternetSetStatusCallbackA(hi,&callback);
515 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
517 trace("InternetConnectA <--\n");
518 hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
519 NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
520 ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
521 trace("InternetConnectA -->\n");
523 if (hic == 0x0) goto abort;
525 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
526 SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
528 trace("HttpOpenRequestA <--\n");
529 hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
530 INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
531 0xdeadbead);
532 if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
534 * If the internet name can't be resolved we are probably behind
535 * a firewall or in some other way not directly connected to the
536 * Internet. Not enough reason to fail the test. Just ignore and
537 * abort.
539 } else {
540 ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
542 trace("HttpOpenRequestA -->\n");
544 if (hor == 0x0) goto abort;
546 CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
547 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
548 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
549 if (first_connection_to_test_url)
551 SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
552 SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
553 SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
554 SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
556 else
558 SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
559 SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
561 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
562 SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
563 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
564 SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
565 SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
566 SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
567 SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
568 SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
569 SET_OPTIONAL(INTERNET_STATUS_CLOSING_CONNECTION);
570 SET_OPTIONAL(INTERNET_STATUS_CONNECTION_CLOSED);
571 SET_EXPECT(INTERNET_STATUS_REDIRECT);
572 SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
573 SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
574 if (flags & INTERNET_FLAG_ASYNC)
575 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
576 else
577 SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
579 trace("HttpSendRequestA -->\n");
580 SetLastError(0xdeadbeef);
581 rc = HttpSendRequestA(hor, "", -1, NULL, 0);
582 if (flags & INTERNET_FLAG_ASYNC)
583 ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
584 "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
585 else
586 ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
587 "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
588 trace("HttpSendRequestA <--\n");
590 if (!rc && (GetLastError() == ERROR_IO_PENDING))
591 WaitForSingleObject(hCompleteEvent, INFINITE);
593 if (first_connection_to_test_url)
595 CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
596 CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
598 else todo_wine
600 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
601 CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
603 CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
604 CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
605 CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
606 CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
607 CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
608 if (flags & INTERNET_FLAG_ASYNC)
609 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
610 else
611 todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
612 /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
613 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
614 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
616 /* tests invalid dwStructSize */
617 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
618 inetbuffers.lpcszHeader = NULL;
619 inetbuffers.dwHeadersLength = 0;
620 inetbuffers.dwBufferLength = 10;
621 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
622 inetbuffers.dwOffsetHigh = 1234;
623 inetbuffers.dwOffsetLow = 5678;
624 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
625 ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
626 "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
627 rc ? "TRUE" : "FALSE", GetLastError());
628 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
630 /* tests to see whether lpcszHeader is used - it isn't */
631 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
632 inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
633 inetbuffers.dwHeadersLength = 255;
634 inetbuffers.dwBufferLength = 0;
635 inetbuffers.lpvBuffer = NULL;
636 inetbuffers.dwOffsetHigh = 1234;
637 inetbuffers.dwOffsetLow = 5678;
638 SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
639 SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
640 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
641 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
642 rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
643 ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
644 trace("read %i bytes\n", inetbuffers.dwBufferLength);
645 todo_wine
647 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
648 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
651 rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
652 ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
653 "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
654 rc ? "TRUE" : "FALSE", GetLastError());
656 length = 0;
657 trace("Entering Query loop\n");
659 SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
660 SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
661 while (TRUE)
663 inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
664 inetbuffers.dwBufferLength = 1024;
665 inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
666 inetbuffers.dwOffsetHigh = 1234;
667 inetbuffers.dwOffsetLow = 5678;
669 SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
670 SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
671 SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
672 rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
673 if (!rc)
675 if (GetLastError() == ERROR_IO_PENDING)
677 trace("InternetReadFileEx -> PENDING\n");
678 ok(flags & INTERNET_FLAG_ASYNC,
679 "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
680 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
681 WaitForSingleObject(hCompleteEvent, INFINITE);
682 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
683 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
685 else
687 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
688 break;
691 else
693 trace("InternetReadFileEx -> SUCCEEDED\n");
694 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
695 if (inetbuffers.dwBufferLength)
697 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
698 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
700 else
702 /* Win98 still sends these when 0 bytes are read, WinXP does not */
703 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
704 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
708 trace("read %i bytes\n", inetbuffers.dwBufferLength);
709 ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
711 ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
712 "InternetReadFileEx sets offsets to 0x%x%08x\n",
713 inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
715 HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
717 if (!inetbuffers.dwBufferLength)
718 break;
720 length += inetbuffers.dwBufferLength;
722 todo_wine ok(length > 0, "failed to read any of the document\n");
723 trace("Finished. Read %d bytes\n", length);
725 /* WinXP does not send, but Win98 does */
726 CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
727 CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
728 abort:
729 SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
730 if (hor) {
731 SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
732 SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
733 rc = InternetCloseHandle(hor);
734 ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
735 rc = InternetCloseHandle(hor);
736 ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
738 if (hic) {
739 rc = InternetCloseHandle(hic);
740 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
742 if (hi) {
743 SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
744 rc = InternetCloseHandle(hi);
745 ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
746 if (flags & INTERNET_FLAG_ASYNC)
747 Sleep(100);
748 CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
750 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
751 CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
752 CloseHandle(hCompleteEvent);
753 first_connection_to_test_url = FALSE;
756 static void InternetOpenUrlA_test(void)
758 HINTERNET myhinternet, myhttp;
759 char buffer[0x400];
760 DWORD size, readbytes, totalbytes=0;
761 BOOL ret;
763 myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
764 ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
765 size = 0x400;
766 ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
767 ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
769 SetLastError(0);
770 myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
771 INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
772 if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
773 return; /* WinXP returns this when not connected to the net */
774 ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
775 ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
776 ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
777 totalbytes += readbytes;
778 while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
779 totalbytes += readbytes;
780 trace("read 0x%08x bytes\n",totalbytes);
782 InternetCloseHandle(myhttp);
783 InternetCloseHandle(myhinternet);
786 static void InternetTimeFromSystemTimeA_test(void)
788 BOOL ret;
789 static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
790 char string[INTERNET_RFC1123_BUFSIZE];
791 static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
792 DWORD error;
794 ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
795 ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
797 ok( !memcmp( string, expect, sizeof(expect) ),
798 "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
800 SetLastError(0xdeadbeef);
801 ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, 0 );
802 error = GetLastError();
803 todo_wine {
804 ok( !ret, "InternetTimeFromSystemTimeA should have returned FALSE\n" );
805 ok( error == ERROR_INSUFFICIENT_BUFFER,
806 "InternetTimeFromSystemTimeA failed with ERROR_INSUFFICIENT_BUFFER instead of %u\n",
807 error );
811 static void InternetTimeFromSystemTimeW_test(void)
813 BOOL ret;
814 static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
815 WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
816 static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
817 '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
818 DWORD error;
820 ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
821 ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
823 ok( !memcmp( string, expect, sizeof(expect) ),
824 "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
826 SetLastError(0xdeadbeef);
827 ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string)/sizeof(string[0]) );
828 error = GetLastError();
829 ok( !ret, "InternetTimeFromSystemTimeW should have returned FALSE\n" );
830 todo_wine
831 ok( error == ERROR_INSUFFICIENT_BUFFER,
832 "InternetTimeFromSystemTimeW failed with ERROR_INSUFFICIENT_BUFFER instead of %u\n",
833 error );
836 static void InternetTimeToSystemTimeA_test(void)
838 BOOL ret;
839 SYSTEMTIME time;
840 static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
841 static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
842 static const char string2[] = " fri 7 jan 2005 12 06 35";
844 ret = pInternetTimeToSystemTimeA( string, &time, 0 );
845 ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
846 ok( !memcmp( &time, &expect, sizeof(expect) ),
847 "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
849 ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
850 ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
851 ok( !memcmp( &time, &expect, sizeof(expect) ),
852 "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
855 static void InternetTimeToSystemTimeW_test(void)
857 BOOL ret;
858 SYSTEMTIME time;
859 static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
860 static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
861 '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
862 static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
863 '1','2',' ','0','6',' ','3','5',0 };
864 static const WCHAR string3[] = { 'F','r',0 };
866 ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
867 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
869 ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
870 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
872 ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
873 ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
875 ret = pInternetTimeToSystemTimeW( string, &time, 0 );
876 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
878 ret = pInternetTimeToSystemTimeW( string, &time, 0 );
879 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
880 ok( !memcmp( &time, &expect, sizeof(expect) ),
881 "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
883 ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
884 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
885 ok( !memcmp( &time, &expect, sizeof(expect) ),
886 "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
888 ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
889 ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
892 static void HttpSendRequestEx_test(void)
894 HINTERNET hSession;
895 HINTERNET hConnect;
896 HINTERNET hRequest;
898 INTERNET_BUFFERS BufferIn;
899 DWORD dwBytesWritten;
900 DWORD dwBytesRead;
901 CHAR szBuffer[256];
902 int i;
903 BOOL ret;
905 static char szPostData[] = "mode=Test";
906 static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
908 hSession = InternetOpen("Wine Regression Test",
909 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
910 ok( hSession != NULL ,"Unable to open Internet session\n");
911 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
912 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
914 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
915 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
916 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
917 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
919 skip( "Network unreachable, skipping test\n" );
920 goto done;
922 ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
925 BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
926 BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
927 BufferIn.lpcszHeader = szContentType;
928 BufferIn.dwHeadersLength = sizeof(szContentType)-1;
929 BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
930 BufferIn.lpvBuffer = szPostData;
931 BufferIn.dwBufferLength = 3;
932 BufferIn.dwBufferTotal = sizeof(szPostData)-1;
933 BufferIn.dwOffsetLow = 0;
934 BufferIn.dwOffsetHigh = 0;
936 ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
937 ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
939 for (i = 3; szPostData[i]; i++)
940 ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
941 "InternetWriteFile failed\n");
943 ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
945 ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
946 "Unable to read response\n");
947 szBuffer[dwBytesRead] = 0;
949 ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
950 ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
952 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
953 done:
954 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
955 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
958 static void InternetOpenRequest_test(void)
960 HINTERNET session, connect, request;
961 static const char *types[] = { "*", "", NULL };
962 static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
963 static const WCHAR *typesW[] = { any, empty, NULL };
964 BOOL ret;
966 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
967 ok(session != NULL ,"Unable to open Internet session\n");
969 connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
970 INTERNET_SERVICE_HTTP, 0, 0);
971 ok(connect == NULL, "InternetConnectA should have failed\n");
972 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
974 connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
975 INTERNET_SERVICE_HTTP, 0, 0);
976 ok(connect == NULL, "InternetConnectA should have failed\n");
977 ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
979 connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
980 INTERNET_SERVICE_HTTP, 0, 0);
981 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
983 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
984 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
986 skip( "Network unreachable, skipping test\n" );
987 goto done;
989 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
991 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
992 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
993 ok(InternetCloseHandle(request), "Close request handle failed\n");
995 request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
996 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
998 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
999 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1000 ok(InternetCloseHandle(request), "Close request handle failed\n");
1002 done:
1003 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1004 ok(InternetCloseHandle(session), "Close session handle failed\n");
1007 static void test_http_cache(void)
1009 HINTERNET session, connect, request;
1010 char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1011 DWORD size, file_size;
1012 BYTE buf[100];
1013 HANDLE file;
1014 BOOL ret;
1016 static const char *types[] = { "*", "", NULL };
1018 session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1019 ok(session != NULL ,"Unable to open Internet session\n");
1021 connect = InternetConnectA(session, "www.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1022 INTERNET_SERVICE_HTTP, 0, 0);
1023 ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
1025 request = HttpOpenRequestA(connect, NULL, "/site/about", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1026 if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1028 skip( "Network unreachable, skipping test\n" );
1030 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1031 ok(InternetCloseHandle(session), "Close session handle failed\n");
1033 return;
1035 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1037 size = sizeof(url);
1038 ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1039 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_url) failed: %u\n", GetLastError());
1040 ok(!strcmp(url, "http://www.winehq.org/site/about"), "Wrong URL %s\n", url);
1042 size = sizeof(file_name);
1043 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1044 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1045 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1046 ok(!size, "size = %d\n", size);
1048 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1049 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1051 size = sizeof(file_name);
1052 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1053 ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1055 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1056 FILE_ATTRIBUTE_NORMAL, NULL);
1057 ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1058 file_size = GetFileSize(file, NULL);
1059 ok(file_size == 0, "file size=%d\n", file_size);
1061 ret = InternetReadFile(request, buf, sizeof(buf), &size);
1062 ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1063 ok(size == sizeof(buf), "size=%d\n", size);
1065 file_size = GetFileSize(file, NULL);
1066 ok(file_size == sizeof(buf), "file size=%d\n", file_size);
1067 CloseHandle(file);
1069 ok(InternetCloseHandle(request), "Close request handle failed\n");
1071 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1072 FILE_ATTRIBUTE_NORMAL, NULL);
1073 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1074 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1076 request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1077 ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1079 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1080 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1081 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1082 ok(!size, "size = %d\n", size);
1084 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1085 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1087 size = sizeof(file_name);
1088 ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1089 ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1090 ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1091 ok(!size, "size = %d\n", size);
1093 file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1094 FILE_ATTRIBUTE_NORMAL, NULL);
1095 ok(file == INVALID_HANDLE_VALUE, "CreateFile succeeded\n");
1096 ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError()=%u, expected ERROR_FILE_NOT_FOUND\n", GetLastError());
1098 ok(InternetCloseHandle(request), "Close request handle failed\n");
1100 ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1101 ok(InternetCloseHandle(session), "Close session handle failed\n");
1104 static void HttpHeaders_test(void)
1106 HINTERNET hSession;
1107 HINTERNET hConnect;
1108 HINTERNET hRequest;
1109 CHAR buffer[256];
1110 WCHAR wbuffer[256];
1111 DWORD len = 256;
1112 DWORD oldlen;
1113 DWORD index = 0;
1115 hSession = InternetOpen("Wine Regression Test",
1116 INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1117 ok( hSession != NULL ,"Unable to open Internet session\n");
1118 hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1119 INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1121 ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1122 hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1123 NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1124 if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1126 skip( "Network unreachable, skipping test\n" );
1127 goto done;
1129 ok( hRequest != NULL, "Failed to open request handle\n");
1131 index = 0;
1132 len = sizeof(buffer);
1133 strcpy(buffer,"Warning");
1134 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1135 buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1137 ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1138 "Failed to add new header\n");
1140 index = 0;
1141 len = sizeof(buffer);
1142 strcpy(buffer,"Warning");
1143 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1144 buffer,&len,&index),"Unable to query header\n");
1145 ok(index == 1, "Index was not incremented\n");
1146 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1147 ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1148 ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1149 len = sizeof(buffer);
1150 strcpy(buffer,"Warning");
1151 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1152 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1154 index = 0;
1155 len = 5; /* could store the string but not the NULL terminator */
1156 strcpy(buffer,"Warning");
1157 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1158 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1159 ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1160 ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1162 /* a call with NULL will fail but will return the length */
1163 index = 0;
1164 len = sizeof(buffer);
1165 SetLastError(0xdeadbeef);
1166 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1167 NULL,&len,&index) == FALSE,"Query worked\n");
1168 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1169 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1170 ok(index == 0, "Index was incremented\n");
1172 /* even for a len that is too small */
1173 index = 0;
1174 len = 15;
1175 SetLastError(0xdeadbeef);
1176 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1177 NULL,&len,&index) == FALSE,"Query worked\n");
1178 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1179 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1180 ok(index == 0, "Index was incremented\n");
1182 index = 0;
1183 len = 0;
1184 SetLastError(0xdeadbeef);
1185 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1186 NULL,&len,&index) == FALSE,"Query worked\n");
1187 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1188 ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1189 ok(index == 0, "Index was incremented\n");
1190 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1193 /* a working query */
1194 index = 0;
1195 len = sizeof(buffer);
1196 memset(buffer, 'x', sizeof(buffer));
1197 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1198 buffer,&len,&index),"Unable to query header\n");
1199 ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1200 ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1201 ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1202 /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1203 ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1204 ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1205 ok(index == 0, "Index was incremented\n");
1207 /* Like above two tests, but for W version */
1209 index = 0;
1210 len = 0;
1211 SetLastError(0xdeadbeef);
1212 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1213 NULL,&len,&index) == FALSE,"Query worked\n");
1214 ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1215 ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1216 ok(index == 0, "Index was incremented\n");
1217 oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1219 /* a working query */
1220 index = 0;
1221 len = sizeof(wbuffer);
1222 memset(wbuffer, 'x', sizeof(wbuffer));
1223 ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1224 wbuffer,&len,&index),"Unable to query header\n");
1225 ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1226 ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1227 ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1228 ok(index == 0, "Index was incremented\n");
1230 /* end of W version tests */
1232 /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1233 index = 0;
1234 len = sizeof(buffer);
1235 memset(buffer, 'x', sizeof(buffer));
1236 ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1237 buffer,&len,&index) == TRUE,"Query failed\n");
1238 ok(len == 2, "Expected 2, got %d\n", len);
1239 ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1240 ok(index == 0, "Index was incremented\n");
1242 ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1243 "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1245 index = 0;
1246 len = sizeof(buffer);
1247 strcpy(buffer,"Warning");
1248 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1249 buffer,&len,&index),"Unable to query header\n");
1250 ok(index == 1, "Index was not incremented\n");
1251 ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1252 len = sizeof(buffer);
1253 strcpy(buffer,"Warning");
1254 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1255 buffer,&len,&index),"Failed to get second header\n");
1256 ok(index == 2, "Index was not incremented\n");
1257 ok(strcmp(buffer,"test2")==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,
1261 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1263 ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1265 index = 0;
1266 len = sizeof(buffer);
1267 strcpy(buffer,"Warning");
1268 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1269 buffer,&len,&index),"Unable to query header\n");
1270 ok(index == 1, "Index was not incremented\n");
1271 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1272 len = sizeof(buffer);
1273 strcpy(buffer,"Warning");
1274 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1275 buffer,&len,&index),"Failed to get second header\n");
1276 ok(index == 2, "Index was not incremented\n");
1277 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1278 len = sizeof(buffer);
1279 strcpy(buffer,"Warning");
1280 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1281 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1283 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1285 index = 0;
1286 len = sizeof(buffer);
1287 strcpy(buffer,"Warning");
1288 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1289 buffer,&len,&index),"Unable to query header\n");
1290 ok(index == 1, "Index was not incremented\n");
1291 ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1292 len = sizeof(buffer);
1293 strcpy(buffer,"Warning");
1294 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1295 buffer,&len,&index),"Failed to get second header\n");
1296 ok(index == 2, "Index was not incremented\n");
1297 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1298 len = sizeof(buffer);
1299 strcpy(buffer,"Warning");
1300 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1301 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1303 ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1305 index = 0;
1306 len = sizeof(buffer);
1307 strcpy(buffer,"Warning");
1308 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1309 buffer,&len,&index),"Unable to query header\n");
1310 ok(index == 1, "Index was not incremented\n");
1311 ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1312 len = sizeof(buffer);
1313 strcpy(buffer,"Warning");
1314 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1315 ok(index == 2, "Index was not incremented\n");
1316 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1317 len = sizeof(buffer);
1318 strcpy(buffer,"Warning");
1319 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1321 ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1323 index = 0;
1324 len = sizeof(buffer);
1325 strcpy(buffer,"Warning");
1326 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1327 ok(index == 1, "Index was not incremented\n");
1328 ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1329 len = sizeof(buffer);
1330 strcpy(buffer,"Warning");
1331 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1332 ok(index == 2, "Index was not incremented\n");
1333 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1334 len = sizeof(buffer);
1335 strcpy(buffer,"Warning");
1336 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1338 ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1340 index = 0;
1341 len = sizeof(buffer);
1342 strcpy(buffer,"Warning");
1343 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1344 ok(index == 1, "Index was not incremented\n");
1345 ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1346 len = sizeof(buffer);
1347 strcpy(buffer,"Warning");
1348 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1349 ok(index == 2, "Index was not incremented\n");
1350 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1351 len = sizeof(buffer);
1352 strcpy(buffer,"Warning");
1353 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1355 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");
1357 index = 0;
1358 len = sizeof(buffer);
1359 strcpy(buffer,"Warning");
1360 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1361 ok(index == 1, "Index was not incremented\n");
1362 ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1363 len = sizeof(buffer);
1364 strcpy(buffer,"Warning");
1365 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1366 ok(index == 2, "Index was not incremented\n");
1367 ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1368 len = sizeof(buffer);
1369 strcpy(buffer,"Warning");
1370 ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1373 ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1374 done:
1375 ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1376 ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1379 static const char contmsg[] =
1380 "HTTP/1.1 100 Continue\r\n";
1382 static const char okmsg[] =
1383 "HTTP/1.1 200 OK\r\n"
1384 "Server: winetest\r\n"
1385 "\r\n";
1387 static const char notokmsg[] =
1388 "HTTP/1.1 400 Bad Request\r\n"
1389 "Server: winetest\r\n"
1390 "\r\n";
1392 static const char noauthmsg[] =
1393 "HTTP/1.1 401 Unauthorized\r\n"
1394 "Server: winetest\r\n"
1395 "\r\n";
1397 static const char proxymsg[] =
1398 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1399 "Server: winetest\r\n"
1400 "Proxy-Connection: close\r\n"
1401 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1402 "\r\n";
1404 static const char page1[] =
1405 "<HTML>\r\n"
1406 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1407 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1408 "</HTML>\r\n\r\n";
1410 struct server_info {
1411 HANDLE hEvent;
1412 int port;
1415 static DWORD CALLBACK server_thread(LPVOID param)
1417 struct server_info *si = param;
1418 int r, c, i, on;
1419 SOCKET s;
1420 struct sockaddr_in sa;
1421 char buffer[0x100];
1422 WSADATA wsaData;
1423 int last_request = 0;
1424 char host_header[22];
1425 static int test_b = 0;
1427 WSAStartup(MAKEWORD(1,1), &wsaData);
1429 s = socket(AF_INET, SOCK_STREAM, 0);
1430 if (s == INVALID_SOCKET)
1431 return 1;
1433 on = 1;
1434 setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1436 memset(&sa, 0, sizeof sa);
1437 sa.sin_family = AF_INET;
1438 sa.sin_port = htons(si->port);
1439 sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1441 r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1442 if (r<0)
1443 return 1;
1445 listen(s, 0);
1447 SetEvent(si->hEvent);
1449 sprintf(host_header, "Host: localhost:%d", si->port);
1453 c = accept(s, NULL, NULL);
1455 memset(buffer, 0, sizeof buffer);
1456 for(i=0; i<(sizeof buffer-1); i++)
1458 r = recv(c, &buffer[i], 1, 0);
1459 if (r != 1)
1460 break;
1461 if (i<4) continue;
1462 if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1463 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1464 break;
1466 if (strstr(buffer, "GET /test1"))
1468 if (!strstr(buffer, "Content-Length: 0"))
1470 send(c, okmsg, sizeof okmsg-1, 0);
1471 send(c, page1, sizeof page1-1, 0);
1473 else
1474 send(c, notokmsg, sizeof notokmsg-1, 0);
1476 if (strstr(buffer, "/test2"))
1478 if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1480 send(c, okmsg, sizeof okmsg-1, 0);
1481 send(c, page1, sizeof page1-1, 0);
1483 else
1484 send(c, proxymsg, sizeof proxymsg-1, 0);
1486 if (strstr(buffer, "/test3"))
1488 if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1489 send(c, okmsg, sizeof okmsg-1, 0);
1490 else
1491 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1493 if (strstr(buffer, "/test4"))
1495 if (strstr(buffer, "Connection: Close"))
1496 send(c, okmsg, sizeof okmsg-1, 0);
1497 else
1498 send(c, notokmsg, sizeof notokmsg-1, 0);
1500 if (strstr(buffer, "POST /test5"))
1502 if (strstr(buffer, "Content-Length: 0"))
1504 send(c, okmsg, sizeof okmsg-1, 0);
1505 send(c, page1, sizeof page1-1, 0);
1507 else
1508 send(c, notokmsg, sizeof notokmsg-1, 0);
1510 if (strstr(buffer, "GET /test6"))
1512 send(c, contmsg, sizeof contmsg-1, 0);
1513 send(c, contmsg, sizeof contmsg-1, 0);
1514 send(c, okmsg, sizeof okmsg-1, 0);
1515 send(c, page1, sizeof page1-1, 0);
1517 if (strstr(buffer, "POST /test7"))
1519 if (strstr(buffer, "Content-Length: 100"))
1521 send(c, okmsg, sizeof okmsg-1, 0);
1522 send(c, page1, sizeof page1-1, 0);
1524 else
1525 send(c, notokmsg, sizeof notokmsg-1, 0);
1527 if (strstr(buffer, "/test8"))
1529 if (!strstr(buffer, "Connection: Close") &&
1530 strstr(buffer, "Connection: Keep-Alive") &&
1531 !strstr(buffer, "Cache-Control: no-cache") &&
1532 !strstr(buffer, "Pragma: no-cache") &&
1533 strstr(buffer, host_header))
1534 send(c, okmsg, sizeof okmsg-1, 0);
1535 else
1536 send(c, notokmsg, sizeof notokmsg-1, 0);
1538 if (strstr(buffer, "/test9"))
1540 if (!strstr(buffer, "Connection: Close") &&
1541 !strstr(buffer, "Connection: Keep-Alive") &&
1542 !strstr(buffer, "Cache-Control: no-cache") &&
1543 !strstr(buffer, "Pragma: no-cache") &&
1544 strstr(buffer, host_header))
1545 send(c, okmsg, sizeof okmsg-1, 0);
1546 else
1547 send(c, notokmsg, sizeof notokmsg-1, 0);
1549 if (strstr(buffer, "/testA"))
1551 if (!strstr(buffer, "Connection: Close") &&
1552 !strstr(buffer, "Connection: Keep-Alive") &&
1553 (strstr(buffer, "Cache-Control: no-cache") ||
1554 strstr(buffer, "Pragma: no-cache")) &&
1555 strstr(buffer, host_header))
1556 send(c, okmsg, sizeof okmsg-1, 0);
1557 else
1558 send(c, notokmsg, sizeof notokmsg-1, 0);
1560 if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1562 test_b = 1;
1563 send(c, okmsg, sizeof okmsg-1, 0);
1564 recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1565 send(c, okmsg, sizeof okmsg-1, 0);
1567 if (strstr(buffer, "/testC"))
1569 if (strstr(buffer, "Cookie: cookie=biscuit"))
1570 send(c, okmsg, sizeof okmsg-1, 0);
1571 else
1572 send(c, notokmsg, sizeof notokmsg-1, 0);
1574 if (strstr(buffer, "GET /quit"))
1576 send(c, okmsg, sizeof okmsg-1, 0);
1577 send(c, page1, sizeof page1-1, 0);
1578 last_request = 1;
1581 shutdown(c, 2);
1582 closesocket(c);
1583 } while (!last_request);
1585 closesocket(s);
1587 return 0;
1590 static void test_basic_request(int port, const char *verb, const char *url)
1592 HINTERNET hi, hc, hr;
1593 DWORD r, count;
1594 char buffer[0x100];
1596 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1597 ok(hi != NULL, "open failed\n");
1599 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1600 ok(hc != NULL, "connect failed\n");
1602 hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1603 ok(hr != NULL, "HttpOpenRequest failed\n");
1605 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1606 ok(r, "HttpSendRequest failed\n");
1608 count = 0;
1609 memset(buffer, 0, sizeof buffer);
1610 r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1611 ok(r, "InternetReadFile failed\n");
1612 ok(count == sizeof page1 - 1, "count was wrong\n");
1613 ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1615 InternetCloseHandle(hr);
1616 InternetCloseHandle(hc);
1617 InternetCloseHandle(hi);
1620 static void test_proxy_indirect(int port)
1622 HINTERNET hi, hc, hr;
1623 DWORD r, sz, val;
1624 char buffer[0x40];
1626 hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1627 ok(hi != NULL, "open failed\n");
1629 hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1630 ok(hc != NULL, "connect failed\n");
1632 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1633 ok(hr != NULL, "HttpOpenRequest failed\n");
1635 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1636 ok(r, "HttpSendRequest failed\n");
1638 sz = sizeof buffer;
1639 r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1640 ok(r, "HttpQueryInfo failed\n");
1641 ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1643 sz = sizeof buffer;
1644 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1645 ok(r, "HttpQueryInfo failed\n");
1646 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1648 sz = sizeof val;
1649 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1650 ok(r, "HttpQueryInfo failed\n");
1651 ok(val == 407, "proxy code wrong\n");
1653 sz = sizeof buffer;
1654 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1655 ok(r, "HttpQueryInfo failed\n");
1656 ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1658 sz = sizeof buffer;
1659 r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1660 ok(r, "HttpQueryInfo failed\n");
1661 ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1663 sz = sizeof buffer;
1664 r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1665 ok(r, "HttpQueryInfo failed\n");
1666 ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1668 sz = sizeof buffer;
1669 r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1670 ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1671 ok(r == FALSE, "HttpQueryInfo failed\n");
1673 InternetCloseHandle(hr);
1674 InternetCloseHandle(hc);
1675 InternetCloseHandle(hi);
1678 static void test_proxy_direct(int port)
1680 HINTERNET hi, hc, hr;
1681 DWORD r, sz;
1682 char buffer[0x40];
1683 static CHAR username[] = "mike",
1684 password[] = "1101";
1686 sprintf(buffer, "localhost:%d\n", port);
1687 hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1688 ok(hi != NULL, "open failed\n");
1690 /* try connect without authorization */
1691 hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1692 ok(hc != NULL, "connect failed\n");
1694 hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1695 ok(hr != NULL, "HttpOpenRequest failed\n");
1697 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1698 ok(r, "HttpSendRequest failed\n");
1700 sz = sizeof buffer;
1701 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1702 ok(r, "HttpQueryInfo failed\n");
1703 ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1706 /* set the user + password then try again */
1707 todo_wine {
1708 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1709 ok(r, "failed to set user\n");
1711 r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1712 ok(r, "failed to set password\n");
1715 r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1716 ok(r, "HttpSendRequest failed\n");
1717 sz = sizeof buffer;
1718 r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1719 ok(r, "HttpQueryInfo failed\n");
1720 todo_wine {
1721 ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1725 InternetCloseHandle(hr);
1726 InternetCloseHandle(hc);
1727 InternetCloseHandle(hi);
1730 static void test_header_handling_order(int port)
1732 static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1733 static char connection[] = "Connection: Close";
1735 static const char *types[2] = { "*", NULL };
1736 HINTERNET session, connect, request;
1737 DWORD size, status;
1738 BOOL ret;
1740 session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1741 ok(session != NULL, "InternetOpen failed\n");
1743 connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1744 ok(connect != NULL, "InternetConnect failed\n");
1746 request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1747 ok(request != NULL, "HttpOpenRequest failed\n");
1749 ret = HttpAddRequestHeaders(request, authorization, ~0UL, HTTP_ADDREQ_FLAG_ADD);
1750 ok(ret, "HttpAddRequestHeaders failed\n");
1752 ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1753 ok(ret, "HttpSendRequest failed\n");
1755 status = 0;
1756 size = sizeof(status);
1757 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1758 ok(ret, "HttpQueryInfo failed\n");
1759 ok(status == 200, "request failed with status %u\n", status);
1761 InternetCloseHandle(request);
1763 request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1764 ok(request != NULL, "HttpOpenRequest failed\n");
1766 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1767 ok(ret, "HttpSendRequest failed\n");
1769 status = 0;
1770 size = sizeof(status);
1771 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1772 ok(ret, "HttpQueryInfo failed\n");
1773 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1775 InternetCloseHandle(request);
1777 request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1778 ok(request != NULL, "HttpOpenRequest failed\n");
1780 ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1781 ok(ret, "HttpAddRequestHeaders failed\n");
1783 ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1784 ok(ret, "HttpSendRequest failed\n");
1786 status = 0;
1787 size = sizeof(status);
1788 ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1789 ok(ret, "HttpQueryInfo failed\n");
1790 ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1792 InternetCloseHandle(request);
1793 InternetCloseHandle(connect);
1794 InternetCloseHandle(session);
1797 static void test_connection_header(int port)
1799 HINTERNET ses, con, req;
1800 DWORD size, status;
1801 BOOL ret;
1803 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1804 ok(ses != NULL, "InternetOpen failed\n");
1806 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1807 ok(con != NULL, "InternetConnect failed\n");
1809 req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1810 ok(req != NULL, "HttpOpenRequest failed\n");
1812 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1813 ok(ret, "HttpSendRequest failed\n");
1815 status = 0;
1816 size = sizeof(status);
1817 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1818 ok(ret, "HttpQueryInfo failed\n");
1819 ok(status == 200, "request failed with status %u\n", status);
1821 InternetCloseHandle(req);
1823 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
1824 ok(req != NULL, "HttpOpenRequest failed\n");
1826 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1827 ok(ret, "HttpSendRequest failed\n");
1829 status = 0;
1830 size = sizeof(status);
1831 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1832 ok(ret, "HttpQueryInfo failed\n");
1833 ok(status == 200, "request failed with status %u\n", status);
1835 InternetCloseHandle(req);
1837 req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1838 ok(req != NULL, "HttpOpenRequest failed\n");
1840 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1841 ok(ret, "HttpSendRequest failed\n");
1843 status = 0;
1844 size = sizeof(status);
1845 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1846 ok(ret, "HttpQueryInfo failed\n");
1847 ok(status == 200, "request failed with status %u\n", status);
1849 InternetCloseHandle(req);
1851 req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1852 ok(req != NULL, "HttpOpenRequest failed\n");
1854 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1855 ok(ret, "HttpSendRequest failed\n");
1857 status = 0;
1858 size = sizeof(status);
1859 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1860 ok(ret, "HttpQueryInfo failed\n");
1861 ok(status == 200, "request failed with status %u\n", status);
1863 InternetCloseHandle(req);
1864 InternetCloseHandle(con);
1865 InternetCloseHandle(ses);
1868 static void test_http1_1(int port)
1870 HINTERNET ses, con, req;
1871 BOOL ret;
1873 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1874 ok(ses != NULL, "InternetOpen failed\n");
1876 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1877 ok(con != NULL, "InternetConnect failed\n");
1879 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1880 ok(req != NULL, "HttpOpenRequest failed\n");
1882 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1883 if (ret)
1885 InternetCloseHandle(req);
1887 req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1888 ok(req != NULL, "HttpOpenRequest failed\n");
1890 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1891 todo_wine
1892 ok(ret, "HttpSendRequest failed\n");
1895 InternetCloseHandle(req);
1896 InternetCloseHandle(con);
1897 InternetCloseHandle(ses);
1900 static void test_cookie_header(int port)
1902 HINTERNET ses, con, req;
1903 DWORD size, status, error;
1904 BOOL ret;
1905 char buffer[64];
1907 ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1908 ok(ses != NULL, "InternetOpen failed\n");
1910 con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1911 ok(con != NULL, "InternetConnect failed\n");
1913 InternetSetCookie("http://localhost", "cookie", "biscuit");
1915 req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
1916 ok(req != NULL, "HttpOpenRequest failed\n");
1918 buffer[0] = 0;
1919 size = sizeof(buffer);
1920 SetLastError(0xdeadbeef);
1921 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1922 error = GetLastError();
1923 ok(!ret, "HttpQueryInfo succeeded\n");
1924 ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
1926 ret = HttpSendRequest(req, NULL, 0, NULL, 0);
1927 ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1929 status = 0;
1930 size = sizeof(status);
1931 ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
1932 ok(ret, "HttpQueryInfo failed\n");
1933 ok(status == 200, "request failed with status %u\n", status);
1935 buffer[0] = 0;
1936 size = sizeof(buffer);
1937 ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1938 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
1939 ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
1941 InternetCloseHandle(req);
1942 InternetCloseHandle(con);
1943 InternetCloseHandle(ses);
1946 static void test_http_connection(void)
1948 struct server_info si;
1949 HANDLE hThread;
1950 DWORD id = 0, r;
1952 si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1953 si.port = 7531;
1955 hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1956 ok( hThread != NULL, "create thread failed\n");
1958 r = WaitForSingleObject(si.hEvent, 10000);
1959 ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1960 if (r != WAIT_OBJECT_0)
1961 return;
1963 test_basic_request(si.port, "GET", "/test1");
1964 test_proxy_indirect(si.port);
1965 test_proxy_direct(si.port);
1966 test_header_handling_order(si.port);
1967 test_basic_request(si.port, "POST", "/test5");
1968 test_basic_request(si.port, "GET", "/test6");
1969 test_connection_header(si.port);
1970 test_http1_1(si.port);
1971 test_cookie_header(si.port);
1973 /* send the basic request again to shutdown the server thread */
1974 test_basic_request(si.port, "GET", "/quit");
1976 r = WaitForSingleObject(hThread, 3000);
1977 ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1978 CloseHandle(hThread);
1981 static void test_user_agent_header(void)
1983 HINTERNET ses, con, req;
1984 DWORD size, err;
1985 char buffer[64];
1986 BOOL ret;
1988 ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1989 ok(ses != NULL, "InternetOpen failed\n");
1991 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1992 ok(con != NULL, "InternetConnect failed\n");
1994 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
1995 ok(req != NULL, "HttpOpenRequest failed\n");
1997 size = sizeof(buffer);
1998 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
1999 err = GetLastError();
2000 ok(!ret, "HttpQueryInfo succeeded\n");
2001 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2003 ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2004 ok(ret, "HttpAddRequestHeaders succeeded\n");
2006 size = sizeof(buffer);
2007 ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2008 err = GetLastError();
2009 ok(ret, "HttpQueryInfo failed\n");
2010 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2012 InternetCloseHandle(req);
2014 req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2015 ok(req != NULL, "HttpOpenRequest failed\n");
2017 size = sizeof(buffer);
2018 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2019 err = GetLastError();
2020 ok(!ret, "HttpQueryInfo succeeded\n");
2021 ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2023 ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0UL, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2024 ok(ret, "HttpAddRequestHeaders failed\n");
2026 buffer[0] = 0;
2027 size = sizeof(buffer);
2028 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2029 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2030 ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2032 InternetCloseHandle(req);
2033 InternetCloseHandle(con);
2034 InternetCloseHandle(ses);
2037 static void test_bogus_accept_types_array(void)
2039 HINTERNET ses, con, req;
2040 static const char *types[] = { (const char *)6240, "*/*", "%p", "", "*/*", NULL };
2041 DWORD size;
2042 char buffer[32];
2043 BOOL ret;
2045 ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2046 con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2047 req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2049 ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2051 buffer[0] = 0;
2052 size = sizeof(buffer);
2053 ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2054 ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2055 ok(!strcmp(buffer, ", */*, %p, , */*") || /* IE6 */
2056 !strcmp(buffer, "*/*, %p, */*"),
2057 "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , */*'\n", buffer);
2059 InternetCloseHandle(req);
2060 InternetCloseHandle(con);
2061 InternetCloseHandle(ses);
2064 struct context
2066 HANDLE event;
2067 HINTERNET req;
2070 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2072 INTERNET_ASYNC_RESULT *result = info;
2073 struct context *ctx = (struct context *)context;
2075 trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2077 if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2079 trace("request handle: 0x%08lx\n", result->dwResult);
2080 ctx->req = (HINTERNET)result->dwResult;
2081 SetEvent(ctx->event);
2083 if (status == INTERNET_STATUS_HANDLE_CLOSING)
2085 DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2087 if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2088 ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2089 SetEvent(ctx->event);
2093 static void test_open_url_async(void)
2095 BOOL ret;
2096 HINTERNET ses, req;
2097 DWORD size;
2098 struct context ctx;
2099 ULONG type;
2101 ctx.req = NULL;
2102 ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2104 ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2105 ok(ses != NULL, "InternetOpen failed\n");
2107 pInternetSetStatusCallbackA(ses, cb);
2108 ResetEvent(ctx.event);
2110 req = InternetOpenUrl(ses, "http://www.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2111 ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2113 WaitForSingleObject(ctx.event, INFINITE);
2115 type = 0;
2116 size = sizeof(type);
2117 ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2118 ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2119 ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2120 "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2122 size = 0;
2123 ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2124 ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2125 ok(size > 0, "expected size > 0\n");
2127 ResetEvent(ctx.event);
2128 InternetCloseHandle(ctx.req);
2129 WaitForSingleObject(ctx.event, INFINITE);
2131 InternetCloseHandle(ses);
2132 CloseHandle(ctx.event);
2135 #define STATUS_STRING(status) \
2136 memcpy(status_string[status], #status, sizeof(CHAR) * \
2137 (strlen(#status) < MAX_STATUS_NAME ? \
2138 strlen(#status) : \
2139 MAX_STATUS_NAME - 1))
2140 static void init_status_tests(void)
2142 memset(expect, 0, sizeof(expect));
2143 memset(optional, 0, sizeof(optional));
2144 memset(wine_allow, 0, sizeof(wine_allow));
2145 memset(notified, 0, sizeof(notified));
2146 memset(status_string, 0, sizeof(status_string));
2147 STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2148 STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2149 STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2150 STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2151 STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2152 STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2153 STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2154 STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2155 STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2156 STATUS_STRING(INTERNET_STATUS_PREFETCH);
2157 STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2158 STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2159 STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2160 STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2161 STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2162 STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2163 STATUS_STRING(INTERNET_STATUS_REDIRECT);
2164 STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2165 STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2166 STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2167 STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2168 STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2169 STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2170 STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2171 STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2172 STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2174 #undef STATUS_STRING
2176 START_TEST(http)
2178 HMODULE hdll;
2179 hdll = GetModuleHandleA("wininet.dll");
2180 pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2181 pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
2182 pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
2183 pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
2184 pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
2186 if (!pInternetSetStatusCallbackA)
2187 skip("skipping the InternetReadFile tests\n");
2188 else
2190 init_status_tests();
2191 InternetReadFile_test(INTERNET_FLAG_ASYNC);
2192 InternetReadFile_test(0);
2193 InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2194 test_open_url_async();
2196 InternetOpenRequest_test();
2197 test_http_cache();
2198 InternetOpenUrlA_test();
2199 if (!pInternetTimeFromSystemTimeA)
2200 skip("skipping the InternetTime tests\n");
2201 else
2203 InternetTimeFromSystemTimeA_test();
2204 InternetTimeFromSystemTimeW_test();
2205 InternetTimeToSystemTimeA_test();
2206 InternetTimeToSystemTimeW_test();
2208 HttpSendRequestEx_test();
2209 HttpHeaders_test();
2210 test_http_connection();
2211 test_user_agent_header();
2212 test_bogus_accept_types_array();