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