wininet: HTTP_Connect should fail if a NULL or empty hostname is passed in.
[wine] / dlls / wininet / tests / http.c
1 /*
2  * Wininet - Http tests
3  *
4  * Copyright 2002 Aric Stewart
5  * Copyright 2004 Mike McCormack
6  * Copyright 2005 Hans Leidekker
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wininet.h"
30 #include "winsock.h"
31
32 #include "wine/test.h"
33
34 #define TEST_URL "http://www.winehq.org/site/about"
35
36 static BOOL first_connection_to_test_url = TRUE;
37
38 /* Adapted from dlls/urlmon/tests/protocol.c */
39
40 #define SET_EXPECT2(status, num) \
41     expect[status] = num
42
43 #define SET_EXPECT(status) \
44     SET_EXPECT2(status, 1)
45
46 /* SET_WINE_ALLOW's should be used with an appropriate
47  * todo_wine CHECK_NOTIFIED at a later point in the code */
48 #define SET_WINE_ALLOW2(status, num) \
49     wine_allow[status] = num
50
51 #define SET_WINE_ALLOW(status) \
52     SET_WINE_ALLOW2(status, 1)
53
54 #define CHECK_EXPECT(status) \
55     do { \
56         if (!expect[status] && wine_allow[status]) \
57         { \
58             todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
59                          status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
60                          status_string[status] : "unknown");            \
61             wine_allow[status]--; \
62         } \
63         else \
64         { \
65             ok(expect[status], "unexpected status %d (%s)\n", status,   \
66                status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
67                status_string[status] : "unknown");                      \
68             expect[status]--;                                           \
69         } \
70         notified[status]++; \
71     }while(0)
72
73 /* CLEAR_NOTIFIED used in cases when notification behavior
74  * differs between Windows versions */
75 #define CLEAR_NOTIFIED(status) \
76     expect[status] = wine_allow[status] = notified[status] = 0;
77
78 #define CHECK_NOTIFIED2(status, num) \
79     do { \
80         ok(notified[status] == (num), "expected status %d (%s) %d times, received %d times\n", \
81            status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
82            status_string[status] : "unknown", (num), notified[status]); \
83         CLEAR_NOTIFIED(status);                                         \
84     }while(0)
85
86 #define CHECK_NOTIFIED(status) \
87     CHECK_NOTIFIED2(status, 1)
88
89 #define CHECK_NOT_NOTIFIED(status) \
90     CHECK_NOTIFIED2(status, 0)
91
92 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
93 #define MAX_STATUS_NAME 50
94 static int expect[MAX_INTERNET_STATUS], wine_allow[MAX_INTERNET_STATUS],
95     notified[MAX_INTERNET_STATUS];
96 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
97
98 static HANDLE hCompleteEvent;
99
100 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
101 static BOOL (WINAPI *pInternetTimeFromSystemTimeA)(CONST SYSTEMTIME *,DWORD ,LPSTR ,DWORD);
102 static BOOL (WINAPI *pInternetTimeFromSystemTimeW)(CONST SYSTEMTIME *,DWORD ,LPWSTR ,DWORD);
103 static BOOL (WINAPI *pInternetTimeToSystemTimeA)(LPCSTR ,SYSTEMTIME *,DWORD);
104 static BOOL (WINAPI *pInternetTimeToSystemTimeW)(LPCWSTR ,SYSTEMTIME *,DWORD);
105
106
107 static VOID WINAPI callback(
108      HINTERNET hInternet,
109      DWORD_PTR dwContext,
110      DWORD dwInternetStatus,
111      LPVOID lpvStatusInformation,
112      DWORD dwStatusInformationLength
113 )
114 {
115     CHECK_EXPECT(dwInternetStatus);
116     switch (dwInternetStatus)
117     {
118         case INTERNET_STATUS_RESOLVING_NAME:
119             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
120                 GetCurrentThreadId(), hInternet, dwContext,
121                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
122             *(LPSTR)lpvStatusInformation = '\0';
123             break;
124         case INTERNET_STATUS_NAME_RESOLVED:
125             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
126                 GetCurrentThreadId(), hInternet, dwContext,
127                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
128             *(LPSTR)lpvStatusInformation = '\0';
129             break;
130         case INTERNET_STATUS_CONNECTING_TO_SERVER:
131             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
132                 GetCurrentThreadId(), hInternet, dwContext,
133                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
134             *(LPSTR)lpvStatusInformation = '\0';
135             break;
136         case INTERNET_STATUS_CONNECTED_TO_SERVER:
137             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
138                 GetCurrentThreadId(), hInternet, dwContext,
139                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
140             *(LPSTR)lpvStatusInformation = '\0';
141             break;
142         case INTERNET_STATUS_SENDING_REQUEST:
143             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
144                 GetCurrentThreadId(), hInternet, dwContext,
145                 lpvStatusInformation,dwStatusInformationLength);
146             break;
147         case INTERNET_STATUS_REQUEST_SENT:
148             ok(dwStatusInformationLength == sizeof(DWORD),
149                 "info length should be sizeof(DWORD) instead of %d\n",
150                 dwStatusInformationLength);
151             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
152                 GetCurrentThreadId(), hInternet, dwContext,
153                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
154             break;
155         case INTERNET_STATUS_RECEIVING_RESPONSE:
156             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
157                 GetCurrentThreadId(), hInternet, dwContext,
158                 lpvStatusInformation,dwStatusInformationLength);
159             break;
160         case INTERNET_STATUS_RESPONSE_RECEIVED:
161             ok(dwStatusInformationLength == sizeof(DWORD),
162                 "info length should be sizeof(DWORD) instead of %d\n",
163                 dwStatusInformationLength);
164             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
165                 GetCurrentThreadId(), hInternet, dwContext,
166                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
167             break;
168         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
169             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
170                 GetCurrentThreadId(), hInternet,dwContext,
171                 lpvStatusInformation,dwStatusInformationLength);
172             break;
173         case INTERNET_STATUS_PREFETCH:
174             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
175                 GetCurrentThreadId(), hInternet, dwContext,
176                 lpvStatusInformation,dwStatusInformationLength);
177             break;
178         case INTERNET_STATUS_CLOSING_CONNECTION:
179             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
180                 GetCurrentThreadId(), hInternet, dwContext,
181                 lpvStatusInformation,dwStatusInformationLength);
182             break;
183         case INTERNET_STATUS_CONNECTION_CLOSED:
184             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
185                 GetCurrentThreadId(), hInternet, dwContext,
186                 lpvStatusInformation,dwStatusInformationLength);
187             break;
188         case INTERNET_STATUS_HANDLE_CREATED:
189             ok(dwStatusInformationLength == sizeof(HINTERNET),
190                 "info length should be sizeof(HINTERNET) instead of %d\n",
191                 dwStatusInformationLength);
192             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
193                 GetCurrentThreadId(), hInternet, dwContext,
194                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
195             break;
196         case INTERNET_STATUS_HANDLE_CLOSING:
197             ok(dwStatusInformationLength == sizeof(HINTERNET),
198                 "info length should be sizeof(HINTERNET) instead of %d\n",
199                 dwStatusInformationLength);
200             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
201                 GetCurrentThreadId(), hInternet, dwContext,
202                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
203             break;
204         case INTERNET_STATUS_REQUEST_COMPLETE:
205         {
206             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
207             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
208                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
209                 dwStatusInformationLength);
210             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
211                 GetCurrentThreadId(), hInternet, dwContext,
212                 iar->dwResult,iar->dwError,dwStatusInformationLength);
213             SetEvent(hCompleteEvent);
214             break;
215         }
216         case INTERNET_STATUS_REDIRECT:
217             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
218                 GetCurrentThreadId(), hInternet, dwContext,
219                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
220             *(LPSTR)lpvStatusInformation = '\0';
221             break;
222         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
223             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
224                 GetCurrentThreadId(), hInternet, dwContext,
225                 lpvStatusInformation, dwStatusInformationLength);
226             break;
227         default:
228             trace("%04x:Callback %p 0x%lx %d %p %d\n",
229                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
230                 lpvStatusInformation, dwStatusInformationLength);
231     }
232 }
233
234 static void InternetReadFile_test(int flags)
235 {
236     DWORD rc;
237     CHAR buffer[4000];
238     DWORD length;
239     DWORD out;
240     const char *types[2] = { "*", NULL };
241     HINTERNET hi, hic = 0, hor = 0;
242
243     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
244
245     trace("Starting InternetReadFile test with flags 0x%x\n",flags);
246
247     trace("InternetOpenA <--\n");
248     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
249     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
250     trace("InternetOpenA -->\n");
251
252     if (hi == 0x0) goto abort;
253
254     pInternetSetStatusCallbackA(hi,&callback);
255
256     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
257
258     trace("InternetConnectA <--\n");
259     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
260                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
261     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
262     trace("InternetConnectA -->\n");
263
264     if (hic == 0x0) goto abort;
265
266     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
267     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
268     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
269     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
270
271     trace("HttpOpenRequestA <--\n");
272     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
273                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
274                            0xdeadbead);
275     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
276         /*
277          * If the internet name can't be resolved we are probably behind
278          * a firewall or in some other way not directly connected to the
279          * Internet. Not enough reason to fail the test. Just ignore and
280          * abort.
281          */
282     } else  {
283         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
284     }
285     trace("HttpOpenRequestA -->\n");
286
287     if (hor == 0x0) goto abort;
288
289     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
290     todo_wine
291     {
292         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
293         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
294     }
295     if (first_connection_to_test_url)
296     {
297         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
298         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
299     }
300     else
301     {
302         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
303         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
304     }
305     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
306     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
307     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
308     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
309     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
310     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
311     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
312     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
313     SET_EXPECT(INTERNET_STATUS_REDIRECT);
314     if (flags & INTERNET_FLAG_ASYNC)
315         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
316     else
317         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
318
319     trace("HttpSendRequestA -->\n");
320     SetLastError(0xdeadbeef);
321     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
322     if (flags & INTERNET_FLAG_ASYNC)
323         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
324             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
325     else
326         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
327            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
328     trace("HttpSendRequestA <--\n");
329
330     if (flags & INTERNET_FLAG_ASYNC)
331         WaitForSingleObject(hCompleteEvent, INFINITE);
332
333     if (first_connection_to_test_url)
334     {
335         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
336         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
337     }
338     else todo_wine
339     {
340         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
341         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
342     }
343     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
344     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
345     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
346     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
347     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
348     if (flags & INTERNET_FLAG_ASYNC)
349         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
350     else
351         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
352     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
353     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
354     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
355
356     length = 4;
357     rc = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
358     trace("Option 0x17 -> %i  %i\n",rc,out);
359
360     length = 100;
361     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
362     trace("Option 0x22 -> %i  %s\n",rc,buffer);
363
364     length = 4000;
365     rc = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
366     buffer[length]=0;
367     trace("Option 0x16 -> %i  %s\n",rc,buffer);
368
369     length = 4000;
370     rc = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
371     buffer[length]=0;
372     trace("Option 0x22 -> %i  %s\n",rc,buffer);
373
374     length = 16;
375     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
376     trace("Option 0x5 -> %i  %s  (%u)\n",rc,buffer,GetLastError());
377
378     length = 100;
379     rc = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
380     buffer[length]=0;
381     trace("Option 0x1 -> %i  %s\n",rc,buffer);
382
383     SetLastError(0xdeadbeef);
384     rc = InternetReadFile(NULL, buffer, 100, &length);
385     ok(!rc, "InternetReadFile should have failed\n");
386     ok(GetLastError() == ERROR_INVALID_HANDLE,
387         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
388         GetLastError());
389
390     length = 100;
391     trace("Entering Query loop\n");
392
393     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
394     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
395     while (TRUE)
396     {
397         if (flags & INTERNET_FLAG_ASYNC)
398             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
399         rc = InternetQueryDataAvailable(hor,&length,0x0,0x0);
400         ok(!(rc == 0 && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
401         ok(rc != 0 || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
402            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
403         if (flags & INTERNET_FLAG_ASYNC)
404         {
405             if (rc != 0)
406             {
407                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
408             }
409             else if (GetLastError() == ERROR_IO_PENDING)
410             {
411                 WaitForSingleObject(hCompleteEvent, INFINITE);
412                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
413                 continue;
414             }
415         }
416         if (length)
417         {
418             char *buffer;
419             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
420
421             rc = InternetReadFile(hor,buffer,length,&length);
422
423             buffer[length]=0;
424
425             trace("ReadFile -> %i %i\n",rc,length);
426
427             HeapFree(GetProcessHeap(),0,buffer);
428         }
429         if (length == 0)
430             break;
431     }
432     /* WinXP does not send, but Win98 does */
433     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
434     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
435 abort:
436     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
437     if (hor != 0x0) {
438         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
439         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
440         SetLastError(0xdeadbeef);
441         rc = InternetCloseHandle(hor);
442         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
443         SetLastError(0xdeadbeef);
444         rc = InternetCloseHandle(hor);
445         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
446         ok (GetLastError() == ERROR_INVALID_HANDLE,
447             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
448             GetLastError());
449     }
450     /* We intentionally do not close the handle opened by InternetConnectA as this
451      * tickles bug #9479: native closes child internet handles when the parent handles
452      * are closed. This is verified below by checking that the number of
453      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
454     if (hi != 0x0) {
455       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
456       rc = InternetCloseHandle(hi);
457       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
458       if (flags & INTERNET_FLAG_ASYNC)
459           Sleep(100);
460     }
461     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
462     if (hor != 0x0) todo_wine
463     {
464         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
465         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
466     }
467     else
468     {
469         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
470         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
471     }
472     CloseHandle(hCompleteEvent);
473     first_connection_to_test_url = FALSE;
474 }
475
476 static void InternetReadFileExA_test(int flags)
477 {
478     DWORD rc;
479     DWORD length;
480     const char *types[2] = { "*", NULL };
481     HINTERNET hi, hic = 0, hor = 0;
482     INTERNET_BUFFERS inetbuffers;
483
484     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
485
486     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
487
488     trace("InternetOpenA <--\n");
489     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
490     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
491     trace("InternetOpenA -->\n");
492
493     if (hi == 0x0) goto abort;
494
495     pInternetSetStatusCallbackA(hi,&callback);
496
497     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
498
499     trace("InternetConnectA <--\n");
500     hic=InternetConnectA(hi, "www.winehq.org", INTERNET_INVALID_PORT_NUMBER,
501                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
502     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
503     trace("InternetConnectA -->\n");
504
505     if (hic == 0x0) goto abort;
506
507     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
508     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
509     SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
510     SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
511
512     trace("HttpOpenRequestA <--\n");
513     hor = HttpOpenRequestA(hic, "GET", "/about/", NULL, NULL, types,
514                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
515                            0xdeadbead);
516     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
517         /*
518          * If the internet name can't be resolved we are probably behind
519          * a firewall or in some other way not directly connected to the
520          * Internet. Not enough reason to fail the test. Just ignore and
521          * abort.
522          */
523     } else  {
524         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
525     }
526     trace("HttpOpenRequestA -->\n");
527
528     if (hor == 0x0) goto abort;
529
530     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
531     todo_wine
532     {
533         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
534         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
535     }
536     if (first_connection_to_test_url)
537     {
538         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
539         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
540     }
541     else
542     {
543         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
544         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
545     }
546     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
547     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
548     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
549     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
550     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
551     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
552     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
553     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
554     SET_EXPECT(INTERNET_STATUS_REDIRECT);
555     if (flags & INTERNET_FLAG_ASYNC)
556         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
557     else
558         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
559
560     trace("HttpSendRequestA -->\n");
561     SetLastError(0xdeadbeef);
562     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
563     if (flags & INTERNET_FLAG_ASYNC)
564         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
565             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
566     else
567         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
568            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
569     trace("HttpSendRequestA <--\n");
570
571     if (!rc && (GetLastError() == ERROR_IO_PENDING))
572         WaitForSingleObject(hCompleteEvent, INFINITE);
573
574     if (first_connection_to_test_url)
575     {
576         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
577         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
578     }
579     else todo_wine
580     {
581         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
582         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
583     }
584     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
585     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
586     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
587     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
588     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
589     if (flags & INTERNET_FLAG_ASYNC)
590         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
591     else
592         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
593     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
594     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
595     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
596
597     /* tests invalid dwStructSize */
598     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
599     inetbuffers.lpcszHeader = NULL;
600     inetbuffers.dwHeadersLength = 0;
601     inetbuffers.dwBufferLength = 10;
602     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
603     inetbuffers.dwOffsetHigh = 1234;
604     inetbuffers.dwOffsetLow = 5678;
605     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
606     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
607         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
608         rc ? "TRUE" : "FALSE", GetLastError());
609     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
610
611     /* tests to see whether lpcszHeader is used - it isn't */
612     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
613     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
614     inetbuffers.dwHeadersLength = 255;
615     inetbuffers.dwBufferLength = 0;
616     inetbuffers.lpvBuffer = NULL;
617     inetbuffers.dwOffsetHigh = 1234;
618     inetbuffers.dwOffsetLow = 5678;
619     SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
620     SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
621     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
622     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
623     todo_wine
624     {
625         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
626         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
627     }
628
629     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
630     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
631         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
632         rc ? "TRUE" : "FALSE", GetLastError());
633
634     length = 0;
635     trace("Entering Query loop\n");
636
637     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
638     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
639     while (TRUE)
640     {
641         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
642         inetbuffers.dwBufferLength = 1024;
643         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
644         inetbuffers.dwOffsetHigh = 1234;
645         inetbuffers.dwOffsetLow = 5678;
646
647         SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
648         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
649         SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
650         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
651         if (!rc)
652         {
653             if (GetLastError() == ERROR_IO_PENDING)
654             {
655                 trace("InternetReadFileEx -> PENDING\n");
656                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
657                 WaitForSingleObject(hCompleteEvent, INFINITE);
658                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
659                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
660             }
661             else
662             {
663                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
664                 break;
665             }
666         }
667         else
668         {
669             trace("InternetReadFileEx -> SUCCEEDED\n");
670             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
671             if (inetbuffers.dwBufferLength)
672             {
673                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
674                 CHECK_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
675             }
676             else
677             {
678                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
679                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
680                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
681             }
682         }
683
684         trace("read %i bytes\n", inetbuffers.dwBufferLength);
685         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
686
687         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
688             "InternetReadFileEx sets offsets to 0x%x%08x\n",
689             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
690
691         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
692
693         if (!inetbuffers.dwBufferLength)
694             break;
695
696         length += inetbuffers.dwBufferLength;
697     }
698     ok(length > 0, "failed to read any of the document\n");
699     trace("Finished. Read %d bytes\n", length);
700
701     /* WinXP does not send, but Win98 does */
702     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
703     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
704 abort:
705     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
706     if (hor) {
707         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
708         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
709         rc = InternetCloseHandle(hor);
710         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
711         rc = InternetCloseHandle(hor);
712         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
713     }
714     if (hic) {
715         rc = InternetCloseHandle(hic);
716         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
717     }
718     if (hi) {
719       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
720       rc = InternetCloseHandle(hi);
721       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
722       if (flags & INTERNET_FLAG_ASYNC)
723           Sleep(100);
724       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
725     }
726     if (hor != 0x0) todo_wine
727     {
728         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
729         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
730     }
731     else
732     {
733         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
734         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
735     }
736     CloseHandle(hCompleteEvent);
737     first_connection_to_test_url = FALSE;
738 }
739
740 static void InternetOpenUrlA_test(void)
741 {
742   HINTERNET myhinternet, myhttp;
743   char buffer[0x400];
744   DWORD size, readbytes, totalbytes=0;
745   BOOL ret;
746   
747   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
748   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
749   size = 0x400;
750   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
751   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
752
753   SetLastError(0);
754   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
755                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
756   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
757     return; /* WinXP returns this when not connected to the net */
758   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
759   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
760   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
761   totalbytes += readbytes;
762   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
763     totalbytes += readbytes;
764   trace("read 0x%08x bytes\n",totalbytes);
765
766   InternetCloseHandle(myhttp);
767   InternetCloseHandle(myhinternet);
768 }
769
770 static void InternetTimeFromSystemTimeA_test(void)
771 {
772     BOOL ret;
773     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
774     char string[INTERNET_RFC1123_BUFSIZE];
775     static const char expect[] = "Fri, 07 Jan 2005 12:06:35 GMT";
776
777     ret = pInternetTimeFromSystemTimeA( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
778     ok( ret, "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
779
780     ok( !memcmp( string, expect, sizeof(expect) ),
781         "InternetTimeFromSystemTimeA failed (%u)\n", GetLastError() );
782 }
783
784 static void InternetTimeFromSystemTimeW_test(void)
785 {
786     BOOL ret;
787     static const SYSTEMTIME time = { 2005, 1, 5, 7, 12, 6, 35, 0 };
788     WCHAR string[INTERNET_RFC1123_BUFSIZE + 1];
789     static const WCHAR expect[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
790                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
791
792     ret = pInternetTimeFromSystemTimeW( &time, INTERNET_RFC1123_FORMAT, string, sizeof(string) );
793     ok( ret, "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
794
795     ok( !memcmp( string, expect, sizeof(expect) ),
796         "InternetTimeFromSystemTimeW failed (%u)\n", GetLastError() );
797 }
798
799 static void InternetTimeToSystemTimeA_test(void)
800 {
801     BOOL ret;
802     SYSTEMTIME time;
803     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
804     static const char string[] = "Fri, 07 Jan 2005 12:06:35 GMT";
805     static const char string2[] = " fri 7 jan 2005 12 06 35";
806
807     ret = pInternetTimeToSystemTimeA( string, &time, 0 );
808     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
809     ok( !memcmp( &time, &expect, sizeof(expect) ),
810         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
811
812     ret = pInternetTimeToSystemTimeA( string2, &time, 0 );
813     ok( ret, "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
814     ok( !memcmp( &time, &expect, sizeof(expect) ),
815         "InternetTimeToSystemTimeA failed (%u)\n", GetLastError() );
816 }
817
818 static void InternetTimeToSystemTimeW_test(void)
819 {
820     BOOL ret;
821     SYSTEMTIME time;
822     static const SYSTEMTIME expect = { 2005, 1, 5, 7, 12, 6, 35, 0 };
823     static const WCHAR string[] = { 'F','r','i',',',' ','0','7',' ','J','a','n',' ','2','0','0','5',' ',
824                                     '1','2',':','0','6',':','3','5',' ','G','M','T',0 };
825     static const WCHAR string2[] = { ' ','f','r','i',' ','7',' ','j','a','n',' ','2','0','0','5',' ',
826                                      '1','2',' ','0','6',' ','3','5',0 };
827     static const WCHAR string3[] = { 'F','r',0 };
828
829     ret = pInternetTimeToSystemTimeW( NULL, NULL, 0 );
830     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
831
832     ret = pInternetTimeToSystemTimeW( NULL, &time, 0 );
833     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
834
835     ret = pInternetTimeToSystemTimeW( string, NULL, 0 );
836     ok( !ret, "InternetTimeToSystemTimeW succeeded (%u)\n", GetLastError() );
837
838     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
839     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
840
841     ret = pInternetTimeToSystemTimeW( string, &time, 0 );
842     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
843     ok( !memcmp( &time, &expect, sizeof(expect) ),
844         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
845
846     ret = pInternetTimeToSystemTimeW( string2, &time, 0 );
847     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
848     ok( !memcmp( &time, &expect, sizeof(expect) ),
849         "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
850
851     ret = pInternetTimeToSystemTimeW( string3, &time, 0 );
852     ok( ret, "InternetTimeToSystemTimeW failed (%u)\n", GetLastError() );
853 }
854
855 static void HttpSendRequestEx_test(void)
856 {
857     HINTERNET hSession;
858     HINTERNET hConnect;
859     HINTERNET hRequest;
860
861     INTERNET_BUFFERS BufferIn;
862     DWORD dwBytesWritten;
863     DWORD dwBytesRead;
864     CHAR szBuffer[256];
865     int i;
866     BOOL ret;
867
868     static char szPostData[] = "mode=Test";
869     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
870
871     hSession = InternetOpen("Wine Regression Test",
872             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
873     ok( hSession != NULL ,"Unable to open Internet session\n");
874     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
875             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
876             0);
877     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
878     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
879             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
880     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
881     {
882         trace( "Network unreachable, skipping test\n" );
883         goto done;
884     }
885     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
886
887
888     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
889     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
890     BufferIn.lpcszHeader = szContentType;
891     BufferIn.dwHeadersLength = sizeof(szContentType);
892     BufferIn.dwHeadersTotal = sizeof(szContentType);
893     BufferIn.lpvBuffer = szPostData;
894     BufferIn.dwBufferLength = 3;
895     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
896     BufferIn.dwOffsetLow = 0;
897     BufferIn.dwOffsetHigh = 0;
898
899     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
900     ok(ret, "HttpSendRequestEx Failed with error %u\n", GetLastError());
901
902     for (i = 3; szPostData[i]; i++)
903         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
904                 "InternetWriteFile failed\n");
905
906     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
907
908     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
909             "Unable to read response\n");
910     szBuffer[dwBytesRead] = 0;
911
912     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
913     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
914
915     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
916 done:
917     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
918     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
919 }
920
921 static void InternetOpenRequest_test(void)
922 {
923     HINTERNET session, connect, request;
924     static const char *types[] = { "*", "", NULL };
925     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
926     static const WCHAR *typesW[] = { any, empty, NULL };
927     BOOL ret;
928
929     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
930     ok(session != NULL ,"Unable to open Internet session\n");
931
932     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
933                               INTERNET_SERVICE_HTTP, 0, 0);
934     ok(connect == NULL, "InternetConnectA should have failed\n");
935     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
936
937     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
938                               INTERNET_SERVICE_HTTP, 0, 0);
939     ok(connect == NULL, "InternetConnectA should have failed\n");
940     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
941
942     connect = InternetConnectA(session, "winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
943                               INTERNET_SERVICE_HTTP, 0, 0);
944     ok(connect != NULL, "Unable to connect to http://winehq.org with error %d\n", GetLastError());
945
946     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
947     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
948     {
949         trace( "Network unreachable, skipping test\n" );
950         goto done;
951     }
952     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
953
954     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
955     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
956     ok(InternetCloseHandle(request), "Close request handle failed\n");
957
958     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
959     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
960
961     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
962     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
963     ok(InternetCloseHandle(request), "Close request handle failed\n");
964
965 done:
966     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
967     ok(InternetCloseHandle(session), "Close session handle failed\n");
968 }
969
970 static void HttpHeaders_test(void)
971 {
972     HINTERNET hSession;
973     HINTERNET hConnect;
974     HINTERNET hRequest;
975     CHAR      buffer[256];
976     DWORD     len = 256;
977     DWORD     index = 0;
978
979     hSession = InternetOpen("Wine Regression Test",
980             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
981     ok( hSession != NULL ,"Unable to open Internet session\n");
982     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
983             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
984             0);
985     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
986     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
987             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
988     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
989     {
990         trace( "Network unreachable, skipping test\n" );
991         goto done;
992     }
993     ok( hRequest != NULL, "Failed to open request handle\n");
994
995     index = 0;
996     len = sizeof(buffer);
997     strcpy(buffer,"Warning");
998     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
999                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1000     
1001     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1002             "Failed to add new header\n");
1003
1004     index = 0;
1005     len = sizeof(buffer);
1006     strcpy(buffer,"Warning");
1007     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1008                 buffer,&len,&index),"Unable to query header\n");
1009     ok(index == 1, "Index was not incremented\n");
1010     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1011     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1012     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1013     len = sizeof(buffer);
1014     strcpy(buffer,"Warning");
1015     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1016                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1017
1018     index = 0;
1019     len = 5; /* could store the string but not the NULL terminator */
1020     strcpy(buffer,"Warning");
1021     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1022                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1023     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1024     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1025
1026     /* a call with NULL will fail but will return the length */
1027     index = 0;
1028     len = sizeof(buffer);
1029     SetLastError(0xdeadbeef);
1030     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1031                 NULL,&len,&index) == FALSE,"Query worked\n");
1032     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1033     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1034     ok(index == 0, "Index was incremented\n");
1035
1036     /* even for a len that is too small */
1037     index = 0;
1038     len = 15;
1039     SetLastError(0xdeadbeef);
1040     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1041                 NULL,&len,&index) == FALSE,"Query worked\n");
1042     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1043     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1044     ok(index == 0, "Index was incremented\n");
1045
1046     index = 0;
1047     len = 0;
1048     SetLastError(0xdeadbeef);
1049     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1050                 NULL,&len,&index) == FALSE,"Query worked\n");
1051     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1052     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1053     ok(index == 0, "Index was incremented\n");
1054
1055
1056     /* a working query */
1057     index = 0;
1058     len = sizeof(buffer);
1059     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1060                 buffer,&len,&index),"Unable to query header\n");
1061     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1062     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1063     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1064     ok(index == 0, "Index was incremented\n");
1065
1066
1067
1068     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1069             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1070
1071     index = 0;
1072     len = sizeof(buffer);
1073     strcpy(buffer,"Warning");
1074     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1075                 buffer,&len,&index),"Unable to query header\n");
1076     ok(index == 1, "Index was not incremented\n");
1077     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1078     len = sizeof(buffer);
1079     strcpy(buffer,"Warning");
1080     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1081                 buffer,&len,&index),"Failed to get second header\n");
1082     ok(index == 2, "Index was not incremented\n");
1083     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1084     len = sizeof(buffer);
1085     strcpy(buffer,"Warning");
1086     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1087                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1088
1089     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1090
1091     index = 0;
1092     len = sizeof(buffer);
1093     strcpy(buffer,"Warning");
1094     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1095                 buffer,&len,&index),"Unable to query header\n");
1096     ok(index == 1, "Index was not incremented\n");
1097     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1098     len = sizeof(buffer);
1099     strcpy(buffer,"Warning");
1100     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1101                 buffer,&len,&index),"Failed to get second header\n");
1102     ok(index == 2, "Index was not incremented\n");
1103     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1104     len = sizeof(buffer);
1105     strcpy(buffer,"Warning");
1106     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1107                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1108     
1109     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1110
1111     index = 0;
1112     len = sizeof(buffer);
1113     strcpy(buffer,"Warning");
1114     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1115                 buffer,&len,&index),"Unable to query header\n");
1116     ok(index == 1, "Index was not incremented\n");
1117     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1118     len = sizeof(buffer);
1119     strcpy(buffer,"Warning");
1120     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1121                 buffer,&len,&index),"Failed to get second header\n");
1122     ok(index == 2, "Index was not incremented\n");
1123     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1124     len = sizeof(buffer);
1125     strcpy(buffer,"Warning");
1126     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1127                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1128
1129     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1130
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),"Unable to query header\n");
1136     ok(index == 1, "Index was not incremented\n");
1137     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1138     len = sizeof(buffer);
1139     strcpy(buffer,"Warning");
1140     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1141     ok(index == 2, "Index was not incremented\n");
1142     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1143     len = sizeof(buffer);
1144     strcpy(buffer,"Warning");
1145     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1146
1147     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1148
1149     index = 0;
1150     len = sizeof(buffer);
1151     strcpy(buffer,"Warning");
1152     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1153     ok(index == 1, "Index was not incremented\n");
1154     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1155     len = sizeof(buffer);
1156     strcpy(buffer,"Warning");
1157     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1158     ok(index == 2, "Index was not incremented\n");
1159     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1160     len = sizeof(buffer);
1161     strcpy(buffer,"Warning");
1162     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1163
1164     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1165
1166     index = 0;
1167     len = sizeof(buffer);
1168     strcpy(buffer,"Warning");
1169     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1170     ok(index == 1, "Index was not incremented\n");
1171     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1172     len = sizeof(buffer);
1173     strcpy(buffer,"Warning");
1174     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1175     ok(index == 2, "Index was not incremented\n");
1176     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1177     len = sizeof(buffer);
1178     strcpy(buffer,"Warning");
1179     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1180
1181     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");
1182
1183     index = 0;
1184     len = sizeof(buffer);
1185     strcpy(buffer,"Warning");
1186     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1187     ok(index == 1, "Index was not incremented\n");
1188     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1189     len = sizeof(buffer);
1190     strcpy(buffer,"Warning");
1191     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1192     ok(index == 2, "Index was not incremented\n");
1193     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1194     len = sizeof(buffer);
1195     strcpy(buffer,"Warning");
1196     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1197
1198     
1199     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1200 done:
1201     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1202     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1203 }
1204
1205 static const char okmsg[] =
1206 "HTTP/1.0 200 OK\r\n"
1207 "Server: winetest\r\n"
1208 "\r\n";
1209
1210 static const char notokmsg[] =
1211 "HTTP/1.0 400 Bad Request\r\n"
1212 "Server: winetest\r\n"
1213 "\r\n";
1214
1215 static const char noauthmsg[] =
1216 "HTTP/1.0 401 Unauthorized\r\n"
1217 "Server: winetest\r\n"
1218 "\r\n";
1219
1220 static const char proxymsg[] =
1221 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1222 "Server: winetest\r\n"
1223 "Proxy-Connection: close\r\n"
1224 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1225 "\r\n";
1226
1227 static const char page1[] =
1228 "<HTML>\r\n"
1229 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1230 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1231 "</HTML>\r\n\r\n";
1232
1233 struct server_info {
1234     HANDLE hEvent;
1235     int port;
1236 };
1237
1238 static DWORD CALLBACK server_thread(LPVOID param)
1239 {
1240     struct server_info *si = param;
1241     int r, c, i, on;
1242     SOCKET s;
1243     struct sockaddr_in sa;
1244     char buffer[0x100];
1245     WSADATA wsaData;
1246     int last_request = 0;
1247
1248     WSAStartup(MAKEWORD(1,1), &wsaData);
1249
1250     s = socket(AF_INET, SOCK_STREAM, 0);
1251     if (s == INVALID_SOCKET)
1252         return 1;
1253
1254     on = 1;
1255     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1256
1257     memset(&sa, 0, sizeof sa);
1258     sa.sin_family = AF_INET;
1259     sa.sin_port = htons(si->port);
1260     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1261
1262     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1263     if (r<0)
1264         return 1;
1265
1266     listen(s, 0);
1267
1268     SetEvent(si->hEvent);
1269
1270     do
1271     {
1272         c = accept(s, NULL, NULL);
1273
1274         memset(buffer, 0, sizeof buffer);
1275         for(i=0; i<(sizeof buffer-1); i++)
1276         {
1277             r = recv(c, &buffer[i], 1, 0);
1278             if (r != 1)
1279                 break;
1280             if (i<4) continue;
1281             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1282                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1283                 break;
1284         }
1285
1286         if (strstr(buffer, "GET /test1"))
1287         {
1288             send(c, okmsg, sizeof okmsg-1, 0);
1289             send(c, page1, sizeof page1-1, 0);
1290         }
1291
1292         if (strstr(buffer, "/test2"))
1293         {
1294             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1295             {
1296                 send(c, okmsg, sizeof okmsg-1, 0);
1297                 send(c, page1, sizeof page1-1, 0);
1298             }
1299             else
1300                 send(c, proxymsg, sizeof proxymsg-1, 0);
1301         }
1302
1303         if (strstr(buffer, "/test3"))
1304         {
1305             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1306                 send(c, okmsg, sizeof okmsg-1, 0);
1307             else
1308                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1309         }
1310
1311         if (strstr(buffer, "/test4"))
1312         {
1313             if (strstr(buffer, "Connection: Close"))
1314                 send(c, okmsg, sizeof okmsg-1, 0);
1315             else
1316                 send(c, notokmsg, sizeof notokmsg-1, 0);
1317         }
1318
1319         if (strstr(buffer, "/test5"))
1320         {
1321             if (strstr(buffer, "Content-Length: 0"))
1322             {
1323                 send(c, okmsg, sizeof okmsg-1, 0);
1324                 send(c, page1, sizeof page1-1, 0);
1325             }
1326             else
1327                 send(c, notokmsg, sizeof notokmsg-1, 0);
1328         }
1329
1330         if (strstr(buffer, "/quit"))
1331         {
1332             send(c, okmsg, sizeof okmsg-1, 0);
1333             send(c, page1, sizeof page1-1, 0);
1334             last_request = 1;
1335         }
1336
1337         shutdown(c, 2);
1338         closesocket(c);
1339     } while (!last_request);
1340
1341     closesocket(s);
1342
1343     return 0;
1344 }
1345
1346 static void test_basic_request(int port, const char *url)
1347 {
1348     HINTERNET hi, hc, hr;
1349     DWORD r, count;
1350     char buffer[0x100];
1351
1352     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1353     ok(hi != NULL, "open failed\n");
1354
1355     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1356     ok(hc != NULL, "connect failed\n");
1357
1358     hr = HttpOpenRequest(hc, NULL, url, NULL, NULL, NULL, 0, 0);
1359     ok(hr != NULL, "HttpOpenRequest failed\n");
1360
1361     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1362     ok(r, "HttpSendRequest failed\n");
1363
1364     count = 0;
1365     memset(buffer, 0, sizeof buffer);
1366     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1367     ok(r, "InternetReadFile failed\n");
1368     ok(count == sizeof page1 - 1, "count was wrong\n");
1369     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1370
1371     InternetCloseHandle(hr);
1372     InternetCloseHandle(hc);
1373     InternetCloseHandle(hi);
1374 }
1375
1376 static void test_proxy_indirect(int port)
1377 {
1378     HINTERNET hi, hc, hr;
1379     DWORD r, sz, val;
1380     char buffer[0x40];
1381
1382     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1383     ok(hi != NULL, "open failed\n");
1384
1385     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1386     ok(hc != NULL, "connect failed\n");
1387
1388     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1389     ok(hr != NULL, "HttpOpenRequest failed\n");
1390
1391     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1392     ok(r, "HttpSendRequest failed\n");
1393
1394     sz = sizeof buffer;
1395     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1396     ok(r, "HttpQueryInfo failed\n");
1397     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1398
1399     sz = sizeof buffer;
1400     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1401     ok(r, "HttpQueryInfo failed\n");
1402     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1403
1404     sz = sizeof val;
1405     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1406     ok(r, "HttpQueryInfo failed\n");
1407     ok(val == 407, "proxy code wrong\n");
1408
1409     sz = sizeof buffer;
1410     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1411     ok(r, "HttpQueryInfo failed\n");
1412     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1413
1414     sz = sizeof buffer;
1415     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1416     ok(r, "HttpQueryInfo failed\n");
1417     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1418
1419     sz = sizeof buffer;
1420     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1421     ok(r, "HttpQueryInfo failed\n");
1422     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1423
1424     sz = sizeof buffer;
1425     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1426     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1427     ok(r == FALSE, "HttpQueryInfo failed\n");
1428
1429     InternetCloseHandle(hr);
1430     InternetCloseHandle(hc);
1431     InternetCloseHandle(hi);
1432 }
1433
1434 static void test_proxy_direct(int port)
1435 {
1436     HINTERNET hi, hc, hr;
1437     DWORD r, sz;
1438     char buffer[0x40];
1439     static CHAR username[] = "mike",
1440                 password[] = "1101";
1441
1442     sprintf(buffer, "localhost:%d\n", port);
1443     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1444     ok(hi != NULL, "open failed\n");
1445
1446     /* try connect without authorization */
1447     hc = InternetConnect(hi, "www.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1448     ok(hc != NULL, "connect failed\n");
1449
1450     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1451     ok(hr != NULL, "HttpOpenRequest failed\n");
1452
1453     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1454     ok(r, "HttpSendRequest failed\n");
1455
1456     sz = sizeof buffer;
1457     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1458     ok(r, "HttpQueryInfo failed\n");
1459     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1460
1461
1462     /* set the user + password then try again */
1463     todo_wine {
1464     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1465     ok(r, "failed to set user\n");
1466
1467     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1468     ok(r, "failed to set password\n");
1469     }
1470
1471     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1472     ok(r, "HttpSendRequest failed\n");
1473     sz = sizeof buffer;
1474     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1475     ok(r, "HttpQueryInfo failed\n");
1476     todo_wine {
1477     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1478     }
1479
1480
1481     InternetCloseHandle(hr);
1482     InternetCloseHandle(hc);
1483     InternetCloseHandle(hi);
1484 }
1485
1486 static void test_header_handling_order(int port)
1487 {
1488     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1489     static char connection[]    = "Connection: Close";
1490
1491     static const char *types[2] = { "*", NULL };
1492     HINTERNET session, connect, request;
1493     DWORD size, status;
1494     BOOL ret;
1495
1496     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1497     ok(session != NULL, "InternetOpen failed\n");
1498
1499     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1500     ok(connect != NULL, "InternetConnect failed\n");
1501
1502     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1503     ok(request != NULL, "HttpOpenRequest failed\n");
1504
1505     ret = HttpSendRequest(request, authorization, ~0UL, NULL, 0);
1506     ok(ret, "HttpSendRequest failed\n");
1507
1508     status = 0;
1509     size = sizeof(status);
1510     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1511     ok(ret, "HttpQueryInfo failed\n");
1512     ok(status == 200, "request failed with status %u\n", status);
1513
1514     InternetCloseHandle(request);
1515
1516     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1517     ok(request != NULL, "HttpOpenRequest failed\n");
1518
1519     ret = HttpSendRequest(request, connection, ~0UL, NULL, 0);
1520     ok(ret, "HttpSendRequest failed\n");
1521
1522     status = 0;
1523     size = sizeof(status);
1524     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1525     ok(ret, "HttpQueryInfo failed\n");
1526     ok(status == 200, "request failed with status %u\n", status);
1527
1528     InternetCloseHandle(request);
1529     InternetCloseHandle(connect);
1530     InternetCloseHandle(session);
1531 }
1532
1533 static void test_http_connection(void)
1534 {
1535     struct server_info si;
1536     HANDLE hThread;
1537     DWORD id = 0, r;
1538
1539     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
1540     si.port = 7531;
1541
1542     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
1543     ok( hThread != NULL, "create thread failed\n");
1544
1545     r = WaitForSingleObject(si.hEvent, 10000);
1546     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
1547     if (r != WAIT_OBJECT_0)
1548         return;
1549
1550     test_basic_request(si.port, "/test1");
1551     test_proxy_indirect(si.port);
1552     test_proxy_direct(si.port);
1553     test_header_handling_order(si.port);
1554     test_basic_request(si.port, "/test5");
1555
1556     /* send the basic request again to shutdown the server thread */
1557     test_basic_request(si.port, "/quit");
1558
1559     r = WaitForSingleObject(hThread, 3000);
1560     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
1561     CloseHandle(hThread);
1562 }
1563
1564 #define STATUS_STRING(status) \
1565     memcpy(status_string[status], #status, sizeof(CHAR) * \
1566            (strlen(#status) < MAX_STATUS_NAME ? \
1567             strlen(#status) : \
1568             MAX_STATUS_NAME - 1))
1569 static void init_status_tests(void)
1570 {
1571     memset(expect, 0, sizeof(expect));
1572     memset(wine_allow, 0, sizeof(wine_allow));
1573     memset(notified, 0, sizeof(notified));
1574     memset(status_string, 0, sizeof(status_string));
1575     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
1576     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
1577     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
1578     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
1579     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
1580     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
1581     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
1582     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
1583     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
1584     STATUS_STRING(INTERNET_STATUS_PREFETCH);
1585     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
1586     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
1587     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
1588     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
1589     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
1590     STATUS_STRING(INTERNET_STATUS_REDIRECT);
1591     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
1592     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
1593     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
1594     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
1595     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
1596     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
1597     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
1598     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
1599     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
1600 }
1601 #undef STATUS_STRING
1602
1603 START_TEST(http)
1604 {
1605     HMODULE hdll;
1606     hdll = GetModuleHandleA("wininet.dll");
1607     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
1608     pInternetTimeFromSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeA");
1609     pInternetTimeFromSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeFromSystemTimeW");
1610     pInternetTimeToSystemTimeA = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeA");
1611     pInternetTimeToSystemTimeW = (void*)GetProcAddress(hdll, "InternetTimeToSystemTimeW");
1612
1613     if (!pInternetSetStatusCallbackA)
1614         skip("skipping the InternetReadFile tests\n");
1615     else
1616     {
1617         init_status_tests();
1618         InternetReadFile_test(INTERNET_FLAG_ASYNC);
1619         InternetReadFile_test(0);
1620         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
1621     }
1622     InternetOpenRequest_test();
1623     InternetOpenUrlA_test();
1624     if (!pInternetTimeFromSystemTimeA)
1625         skip("skipping the InternetTime tests\n");
1626     else
1627     {
1628         InternetTimeFromSystemTimeA_test();
1629         InternetTimeFromSystemTimeW_test();
1630         InternetTimeToSystemTimeA_test();
1631         InternetTimeToSystemTimeW_test();
1632     }
1633     HttpSendRequestEx_test();
1634     HttpHeaders_test();
1635     test_http_connection();
1636 }