samlib: Add stubbed samlib.dll.
[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://test.winehq.org/hello.html"
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 #define SET_OPTIONAL2(status, num) \
47     optional[status] = num
48
49 #define SET_OPTIONAL(status) \
50     SET_OPTIONAL2(status, 1)
51
52 /* SET_WINE_ALLOW's should be used with an appropriate
53  * todo_wine CHECK_NOTIFIED at a later point in the code */
54 #define SET_WINE_ALLOW2(status, num) \
55     wine_allow[status] = num
56
57 #define SET_WINE_ALLOW(status) \
58     SET_WINE_ALLOW2(status, 1)
59
60 #define CHECK_EXPECT(status) \
61     do { \
62         if (!expect[status] && !optional[status] && wine_allow[status]) \
63         { \
64             todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
65                          status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
66                          status_string[status] : "unknown");            \
67             wine_allow[status]--; \
68         } \
69         else \
70         { \
71             ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status,   \
72                status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
73                status_string[status] : "unknown");                      \
74             if (expect[status]) expect[status]--; \
75             else optional[status]--; \
76         } \
77         notified[status]++; \
78     }while(0)
79
80 /* CLEAR_NOTIFIED used in cases when notification behavior
81  * differs between Windows versions */
82 #define CLEAR_NOTIFIED(status) \
83     expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
84
85 #define CHECK_NOTIFIED2(status, num) \
86     do { \
87         ok(notified[status] + optional[status] == (num), \
88            "expected status %d (%s) %d times, received %d times\n", \
89            status, status < MAX_INTERNET_STATUS && status_string[status][0] != 0 ? \
90            status_string[status] : "unknown", (num), notified[status]); \
91         CLEAR_NOTIFIED(status);                                         \
92     }while(0)
93
94 #define CHECK_NOTIFIED(status) \
95     CHECK_NOTIFIED2(status, 1)
96
97 #define CHECK_NOT_NOTIFIED(status) \
98     CHECK_NOTIFIED2(status, 0)
99
100 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
101 #define MAX_STATUS_NAME 50
102 static int expect[MAX_INTERNET_STATUS], optional[MAX_INTERNET_STATUS],
103     wine_allow[MAX_INTERNET_STATUS], notified[MAX_INTERNET_STATUS];
104 static CHAR status_string[MAX_INTERNET_STATUS][MAX_STATUS_NAME];
105
106 static HANDLE hCompleteEvent;
107
108 #define TESTF_REDIRECT      0x01
109 #define TESTF_COMPRESSED    0x02
110 #define TESTF_ALLOW_COOKIE  0x04
111
112 typedef struct {
113     const char *url;
114     const char *redirected_url;
115     const char *host;
116     const char *path;
117     DWORD flags;
118 } test_data_t;
119
120 static const test_data_t test_data[] = {
121     {
122         "http://test.winehq.org/testredirect",
123         "http://test.winehq.org/hello.html",
124         "test.winehq.org",
125         "/testredirect",
126         TESTF_REDIRECT
127     },
128     {
129         "http://www.codeweavers.com/",
130         "http://www.codeweavers.com/",
131         "www.codeweavers.com",
132         "",
133         TESTF_COMPRESSED|TESTF_ALLOW_COOKIE
134     }
135 };
136
137 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
138
139
140 static VOID WINAPI callback(
141      HINTERNET hInternet,
142      DWORD_PTR dwContext,
143      DWORD dwInternetStatus,
144      LPVOID lpvStatusInformation,
145      DWORD dwStatusInformationLength
146 )
147 {
148     CHECK_EXPECT(dwInternetStatus);
149     switch (dwInternetStatus)
150     {
151         case INTERNET_STATUS_RESOLVING_NAME:
152             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
153                 GetCurrentThreadId(), hInternet, dwContext,
154                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
155             *(LPSTR)lpvStatusInformation = '\0';
156             break;
157         case INTERNET_STATUS_NAME_RESOLVED:
158             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
159                 GetCurrentThreadId(), hInternet, dwContext,
160                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
161             *(LPSTR)lpvStatusInformation = '\0';
162             break;
163         case INTERNET_STATUS_CONNECTING_TO_SERVER:
164             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
165                 GetCurrentThreadId(), hInternet, dwContext,
166                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
167             *(LPSTR)lpvStatusInformation = '\0';
168             break;
169         case INTERNET_STATUS_CONNECTED_TO_SERVER:
170             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
171                 GetCurrentThreadId(), hInternet, dwContext,
172                 (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
173             *(LPSTR)lpvStatusInformation = '\0';
174             break;
175         case INTERNET_STATUS_SENDING_REQUEST:
176             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
177                 GetCurrentThreadId(), hInternet, dwContext,
178                 lpvStatusInformation,dwStatusInformationLength);
179             break;
180         case INTERNET_STATUS_REQUEST_SENT:
181             ok(dwStatusInformationLength == sizeof(DWORD),
182                 "info length should be sizeof(DWORD) instead of %d\n",
183                 dwStatusInformationLength);
184             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
185                 GetCurrentThreadId(), hInternet, dwContext,
186                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
187             break;
188         case INTERNET_STATUS_RECEIVING_RESPONSE:
189             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
190                 GetCurrentThreadId(), hInternet, dwContext,
191                 lpvStatusInformation,dwStatusInformationLength);
192             break;
193         case INTERNET_STATUS_RESPONSE_RECEIVED:
194             ok(dwStatusInformationLength == sizeof(DWORD),
195                 "info length should be sizeof(DWORD) instead of %d\n",
196                 dwStatusInformationLength);
197             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
198                 GetCurrentThreadId(), hInternet, dwContext,
199                 *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
200             break;
201         case INTERNET_STATUS_CTL_RESPONSE_RECEIVED:
202             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
203                 GetCurrentThreadId(), hInternet,dwContext,
204                 lpvStatusInformation,dwStatusInformationLength);
205             break;
206         case INTERNET_STATUS_PREFETCH:
207             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
208                 GetCurrentThreadId(), hInternet, dwContext,
209                 lpvStatusInformation,dwStatusInformationLength);
210             break;
211         case INTERNET_STATUS_CLOSING_CONNECTION:
212             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
213                 GetCurrentThreadId(), hInternet, dwContext,
214                 lpvStatusInformation,dwStatusInformationLength);
215             break;
216         case INTERNET_STATUS_CONNECTION_CLOSED:
217             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
218                 GetCurrentThreadId(), hInternet, dwContext,
219                 lpvStatusInformation,dwStatusInformationLength);
220             break;
221         case INTERNET_STATUS_HANDLE_CREATED:
222             ok(dwStatusInformationLength == sizeof(HINTERNET),
223                 "info length should be sizeof(HINTERNET) instead of %d\n",
224                 dwStatusInformationLength);
225             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
226                 GetCurrentThreadId(), hInternet, dwContext,
227                 *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
228             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
229             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
230             break;
231         case INTERNET_STATUS_HANDLE_CLOSING:
232             ok(dwStatusInformationLength == sizeof(HINTERNET),
233                 "info length should be sizeof(HINTERNET) instead of %d\n",
234                 dwStatusInformationLength);
235             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
236                 GetCurrentThreadId(), hInternet, dwContext,
237                 *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
238             break;
239         case INTERNET_STATUS_REQUEST_COMPLETE:
240         {
241             INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
242             ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
243                 "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
244                 dwStatusInformationLength);
245             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
246                 GetCurrentThreadId(), hInternet, dwContext,
247                 iar->dwResult,iar->dwError,dwStatusInformationLength);
248             SetEvent(hCompleteEvent);
249             break;
250         }
251         case INTERNET_STATUS_REDIRECT:
252             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
253                 GetCurrentThreadId(), hInternet, dwContext,
254                 (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
255             *(LPSTR)lpvStatusInformation = '\0';
256             CLEAR_NOTIFIED(INTERNET_STATUS_DETECTING_PROXY);
257             SET_EXPECT(INTERNET_STATUS_DETECTING_PROXY);
258             break;
259         case INTERNET_STATUS_INTERMEDIATE_RESPONSE:
260             trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
261                 GetCurrentThreadId(), hInternet, dwContext,
262                 lpvStatusInformation, dwStatusInformationLength);
263             break;
264         default:
265             trace("%04x:Callback %p 0x%lx %d %p %d\n",
266                 GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
267                 lpvStatusInformation, dwStatusInformationLength);
268     }
269 }
270
271 static void InternetReadFile_test(int flags, const test_data_t *test)
272 {
273     BOOL res;
274     CHAR buffer[4000];
275     DWORD length;
276     DWORD out;
277     const char *types[2] = { "*", NULL };
278     HINTERNET hi, hic = 0, hor = 0;
279
280     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
281
282     trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
283
284     trace("InternetOpenA <--\n");
285     hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
286             INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
287     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
288     trace("InternetOpenA -->\n");
289
290     if (hi == 0x0) goto abort;
291
292     pInternetSetStatusCallbackA(hi,&callback);
293
294     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
295
296     trace("InternetConnectA <--\n");
297     hic=InternetConnectA(hi, test->host, INTERNET_INVALID_PORT_NUMBER,
298                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
299     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
300     trace("InternetConnectA -->\n");
301
302     if (hic == 0x0) goto abort;
303
304     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
305     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
306
307     trace("HttpOpenRequestA <--\n");
308     hor = HttpOpenRequestA(hic, "GET", test->path, NULL, NULL, types,
309                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
310                            0xdeadbead);
311     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
312         /*
313          * If the internet name can't be resolved we are probably behind
314          * a firewall or in some other way not directly connected to the
315          * Internet. Not enough reason to fail the test. Just ignore and
316          * abort.
317          */
318     } else  {
319         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
320     }
321     trace("HttpOpenRequestA -->\n");
322
323     if (hor == 0x0) goto abort;
324
325     length = sizeof(buffer);
326     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
327     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
328     ok(!strcmp(buffer, test->url), "Wrong URL %s, expected %s\n", buffer, test->url);
329
330     length = sizeof(buffer);
331     res = HttpQueryInfoA(hor, HTTP_QUERY_RAW_HEADERS, buffer, &length, 0x0);
332     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
333     ok(length == 0, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
334     ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
335
336     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
337     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
338     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
339     if(test->flags & TESTF_ALLOW_COOKIE) {
340         SET_OPTIONAL(INTERNET_STATUS_COOKIE_SENT);
341         SET_OPTIONAL(INTERNET_STATUS_COOKIE_RECEIVED);
342     }
343     if (first_connection_to_test_url)
344     {
345         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
346         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
347         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
348         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
349     }
350     else
351     {
352         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
353         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
354     }
355     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
356     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
357     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
358     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
359     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
360     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
361     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
362     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
363     if(test->flags & TESTF_REDIRECT) {
364         SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
365         SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
366     }
367     SET_EXPECT(INTERNET_STATUS_REDIRECT);
368     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
369     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
370     if (flags & INTERNET_FLAG_ASYNC)
371         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
372     else
373         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
374
375     if(test->flags & TESTF_COMPRESSED) {
376         BOOL b = TRUE;
377
378         res = InternetSetOption(hor, INTERNET_OPTION_HTTP_DECODING, &b, sizeof(b));
379         ok(res || broken(!res && GetLastError() == ERROR_INTERNET_INVALID_OPTION),
380            "InternetSetOption failed: %u\n", GetLastError());
381         if(!res)
382             goto abort;
383     }
384
385     trace("HttpSendRequestA -->\n");
386     SetLastError(0xdeadbeef);
387     res = HttpSendRequestA(hor, (test->flags & TESTF_COMPRESSED) ? "Accept-Encoding: gzip, deflate" : "", -1, NULL, 0);
388     if (flags & INTERNET_FLAG_ASYNC)
389         ok(!res && (GetLastError() == ERROR_IO_PENDING),
390             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
391     else
392         ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
393            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
394     trace("HttpSendRequestA <--\n");
395
396     if (flags & INTERNET_FLAG_ASYNC)
397         WaitForSingleObject(hCompleteEvent, INFINITE);
398
399     if(test->flags & TESTF_ALLOW_COOKIE) {
400         CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_SENT);
401         CLEAR_NOTIFIED(INTERNET_STATUS_COOKIE_RECEIVED);
402     }
403     if (first_connection_to_test_url)
404     {
405         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
406         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
407     }
408     else todo_wine
409     {
410         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
411         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
412     }
413     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, (test->flags & TESTF_REDIRECT) ? 2 : 1);
414     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, (test->flags & TESTF_REDIRECT) ? 2 : 1);
415     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, (test->flags & TESTF_REDIRECT) ? 2 : 1);
416     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, (test->flags & TESTF_REDIRECT) ? 2 : 1);
417     if(test->flags & TESTF_REDIRECT)
418         CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
419     if (flags & INTERNET_FLAG_ASYNC)
420         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
421     else
422         CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
423     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
424     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
425     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
426
427     length = 4;
428     res = InternetQueryOptionA(hor,INTERNET_OPTION_REQUEST_FLAGS,&out,&length);
429     ok(res, "InternetQueryOptionA(INTERNET_OPTION_REQUEST) failed with error %d\n", GetLastError());
430
431     length = 100;
432     res = InternetQueryOptionA(hor,INTERNET_OPTION_URL,buffer,&length);
433     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
434
435     length = sizeof(buffer);
436     res = HttpQueryInfoA(hor,HTTP_QUERY_RAW_HEADERS,buffer,&length,0x0);
437     ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
438     buffer[length]=0;
439
440     length = sizeof(buffer);
441     res = InternetQueryOptionA(hor, INTERNET_OPTION_URL, buffer, &length);
442     ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
443     ok(!strcmp(buffer, test->redirected_url), "Wrong URL %s\n", buffer);
444
445     length = 16;
446     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
447     trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i  %s  (%u)\n",res,buffer,GetLastError());
448     if(test->flags & TESTF_COMPRESSED)
449         ok(!res && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND,
450            "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
451
452     length = 100;
453     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
454     buffer[length]=0;
455     trace("Option HTTP_QUERY_CONTENT_TYPE -> %i  %s\n",res,buffer);
456
457     length = 100;
458     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_ENCODING,buffer,&length,0x0);
459     buffer[length]=0;
460     trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i  %s\n",res,buffer);
461
462     SetLastError(0xdeadbeef);
463     res = InternetReadFile(NULL, buffer, 100, &length);
464     ok(!res, "InternetReadFile should have failed\n");
465     ok(GetLastError() == ERROR_INVALID_HANDLE,
466         "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
467         GetLastError());
468
469     length = 100;
470     trace("Entering Query loop\n");
471
472     while (TRUE)
473     {
474         if (flags & INTERNET_FLAG_ASYNC)
475             SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
476         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
477         ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
478         ok(res || ((flags & INTERNET_FLAG_ASYNC) && GetLastError() == ERROR_IO_PENDING),
479            "InternetQueryDataAvailable failed, error %d\n", GetLastError());
480         if (flags & INTERNET_FLAG_ASYNC)
481         {
482             if (res)
483             {
484                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
485             }
486             else if (GetLastError() == ERROR_IO_PENDING)
487             {
488                 WaitForSingleObject(hCompleteEvent, INFINITE);
489                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
490                 continue;
491             }
492         }
493         if (length)
494         {
495             char *buffer;
496             buffer = HeapAlloc(GetProcessHeap(),0,length+1);
497
498             res = InternetReadFile(hor,buffer,length,&length);
499
500             buffer[length]=0;
501
502             trace("ReadFile -> %s %i\n",res?"TRUE":"FALSE",length);
503
504             HeapFree(GetProcessHeap(),0,buffer);
505         }
506         if (length == 0)
507             break;
508     }
509     if(test->flags & TESTF_REDIRECT) {
510         CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
511         CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
512     }
513 abort:
514     trace("aborting\n");
515     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
516     if (hor != 0x0) {
517         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
518         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
519         SetLastError(0xdeadbeef);
520         trace("closing\n");
521         res = InternetCloseHandle(hor);
522         ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
523         SetLastError(0xdeadbeef);
524         res = InternetCloseHandle(hor);
525         ok (!res, "Double close of handle opened by HttpOpenRequestA succeeded\n");
526         ok (GetLastError() == ERROR_INVALID_HANDLE,
527             "Double close of handle should have set ERROR_INVALID_HANDLE instead of %u\n",
528             GetLastError());
529     }
530     /* We intentionally do not close the handle opened by InternetConnectA as this
531      * tickles bug #9479: native closes child internet handles when the parent handles
532      * are closed. This is verified below by checking that the number of
533      * INTERNET_STATUS_HANDLE_CLOSING notifications matches the number expected. */
534     if (hi != 0x0) {
535       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
536         trace("closing 2\n");
537       res = InternetCloseHandle(hi);
538       ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
539       if (flags & INTERNET_FLAG_ASYNC)
540           Sleep(100);
541     }
542     CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
543     if (hor != 0x0) todo_wine
544     {
545         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
546         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
547     }
548     else
549     {
550         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
551         CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
552     }
553     CloseHandle(hCompleteEvent);
554     first_connection_to_test_url = FALSE;
555 }
556
557 static void InternetReadFile_chunked_test(void)
558 {
559     BOOL res;
560     CHAR buffer[4000];
561     DWORD length;
562     const char *types[2] = { "*", NULL };
563     HINTERNET hi, hic = 0, hor = 0;
564
565     trace("Starting InternetReadFile chunked test\n");
566
567     trace("InternetOpenA <--\n");
568     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
569     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
570     trace("InternetOpenA -->\n");
571
572     if (hi == 0x0) goto abort;
573
574     trace("InternetConnectA <--\n");
575     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
576                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
577     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
578     trace("InternetConnectA -->\n");
579
580     if (hic == 0x0) goto abort;
581
582     trace("HttpOpenRequestA <--\n");
583     hor = HttpOpenRequestA(hic, "GET", "/testchunked", NULL, NULL, types,
584                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
585                            0xdeadbead);
586     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
587         /*
588          * If the internet name can't be resolved we are probably behind
589          * a firewall or in some other way not directly connected to the
590          * Internet. Not enough reason to fail the test. Just ignore and
591          * abort.
592          */
593     } else  {
594         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
595     }
596     trace("HttpOpenRequestA -->\n");
597
598     if (hor == 0x0) goto abort;
599
600     trace("HttpSendRequestA -->\n");
601     SetLastError(0xdeadbeef);
602     res = HttpSendRequestA(hor, "", -1, NULL, 0);
603     ok(res || (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED),
604        "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
605     trace("HttpSendRequestA <--\n");
606
607     length = 100;
608     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_TYPE,buffer,&length,0x0);
609     buffer[length]=0;
610     trace("Option CONTENT_TYPE -> %i  %s\n",res,buffer);
611
612     SetLastError( 0xdeadbeef );
613     length = 100;
614     res = HttpQueryInfoA(hor,HTTP_QUERY_TRANSFER_ENCODING,buffer,&length,0x0);
615     buffer[length]=0;
616     trace("Option TRANSFER_ENCODING -> %i  %s\n",res,buffer);
617     ok( res, "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
618     ok( !strcmp( buffer, "chunked" ), "Wrong transfer encoding '%s'\n", buffer );
619
620     SetLastError( 0xdeadbeef );
621     length = 16;
622     res = HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,&buffer,&length,0x0);
623     ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
624     ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
625
626     length = 100;
627     trace("Entering Query loop\n");
628
629     while (TRUE)
630     {
631         res = InternetQueryDataAvailable(hor,&length,0x0,0x0);
632         ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
633         ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
634         trace("got %u available\n",length);
635         if (length)
636         {
637             DWORD got;
638             char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
639
640             res = InternetReadFile(hor,buffer,length,&got);
641
642             buffer[got]=0;
643             trace("ReadFile -> %i %i\n",res,got);
644             ok( length == got, "only got %u of %u available\n", got, length );
645             ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
646
647             HeapFree(GetProcessHeap(),0,buffer);
648             if (!got) break;
649         }
650         if (length == 0)
651             break;
652     }
653 abort:
654     trace("aborting\n");
655     if (hor != 0x0) {
656         res = InternetCloseHandle(hor);
657         ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
658     }
659     if (hi != 0x0) {
660         res = InternetCloseHandle(hi);
661         ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
662     }
663 }
664
665 static void InternetReadFileExA_test(int flags)
666 {
667     DWORD rc;
668     DWORD length;
669     const char *types[2] = { "*", NULL };
670     HINTERNET hi, hic = 0, hor = 0;
671     INTERNET_BUFFERS inetbuffers;
672
673     hCompleteEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
674
675     trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
676
677     trace("InternetOpenA <--\n");
678     hi = InternetOpenA("", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, flags);
679     ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
680     trace("InternetOpenA -->\n");
681
682     if (hi == 0x0) goto abort;
683
684     pInternetSetStatusCallbackA(hi,&callback);
685
686     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
687
688     trace("InternetConnectA <--\n");
689     hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
690                          NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
691     ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
692     trace("InternetConnectA -->\n");
693
694     if (hic == 0x0) goto abort;
695
696     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
697     SET_EXPECT(INTERNET_STATUS_HANDLE_CREATED);
698
699     trace("HttpOpenRequestA <--\n");
700     hor = HttpOpenRequestA(hic, "GET", "/testredirect", NULL, NULL, types,
701                            INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RESYNCHRONIZE,
702                            0xdeadbead);
703     if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
704         /*
705          * If the internet name can't be resolved we are probably behind
706          * a firewall or in some other way not directly connected to the
707          * Internet. Not enough reason to fail the test. Just ignore and
708          * abort.
709          */
710     } else  {
711         ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
712     }
713     trace("HttpOpenRequestA -->\n");
714
715     if (hor == 0x0) goto abort;
716
717     CHECK_NOTIFIED(INTERNET_STATUS_HANDLE_CREATED);
718     CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
719     CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
720     if (first_connection_to_test_url)
721     {
722         SET_EXPECT(INTERNET_STATUS_RESOLVING_NAME);
723         SET_EXPECT(INTERNET_STATUS_NAME_RESOLVED);
724         SET_WINE_ALLOW(INTERNET_STATUS_RESOLVING_NAME);
725         SET_WINE_ALLOW(INTERNET_STATUS_NAME_RESOLVED);
726     }
727     else
728     {
729         SET_WINE_ALLOW2(INTERNET_STATUS_RESOLVING_NAME,2);
730         SET_WINE_ALLOW2(INTERNET_STATUS_NAME_RESOLVED,2);
731     }
732     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTING_TO_SERVER);
733     SET_EXPECT(INTERNET_STATUS_CONNECTING_TO_SERVER);
734     SET_WINE_ALLOW(INTERNET_STATUS_CONNECTED_TO_SERVER);
735     SET_EXPECT(INTERNET_STATUS_CONNECTED_TO_SERVER);
736     SET_EXPECT2(INTERNET_STATUS_SENDING_REQUEST, 2);
737     SET_EXPECT2(INTERNET_STATUS_REQUEST_SENT, 2);
738     SET_EXPECT2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
739     SET_EXPECT2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
740     SET_OPTIONAL2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
741     SET_OPTIONAL2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
742     SET_EXPECT(INTERNET_STATUS_REDIRECT);
743     SET_OPTIONAL(INTERNET_STATUS_CONNECTING_TO_SERVER);
744     SET_OPTIONAL(INTERNET_STATUS_CONNECTED_TO_SERVER);
745     if (flags & INTERNET_FLAG_ASYNC)
746         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
747     else
748         SET_WINE_ALLOW(INTERNET_STATUS_REQUEST_COMPLETE);
749
750     trace("HttpSendRequestA -->\n");
751     SetLastError(0xdeadbeef);
752     rc = HttpSendRequestA(hor, "", -1, NULL, 0);
753     if (flags & INTERNET_FLAG_ASYNC)
754         ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
755             "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
756     else
757         ok((rc != 0) || GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED,
758            "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
759     trace("HttpSendRequestA <--\n");
760
761     if (!rc && (GetLastError() == ERROR_IO_PENDING))
762         WaitForSingleObject(hCompleteEvent, INFINITE);
763
764     if (first_connection_to_test_url)
765     {
766         CHECK_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
767         CHECK_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
768     }
769     else todo_wine
770     {
771         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESOLVING_NAME);
772         CHECK_NOT_NOTIFIED(INTERNET_STATUS_NAME_RESOLVED);
773     }
774     CHECK_NOTIFIED2(INTERNET_STATUS_SENDING_REQUEST, 2);
775     CHECK_NOTIFIED2(INTERNET_STATUS_REQUEST_SENT, 2);
776     CHECK_NOTIFIED2(INTERNET_STATUS_RECEIVING_RESPONSE, 2);
777     CHECK_NOTIFIED2(INTERNET_STATUS_RESPONSE_RECEIVED, 2);
778     CHECK_NOTIFIED2(INTERNET_STATUS_CLOSING_CONNECTION, 2);
779     CHECK_NOTIFIED2(INTERNET_STATUS_CONNECTION_CLOSED, 2);
780     CHECK_NOTIFIED(INTERNET_STATUS_REDIRECT);
781     if (flags & INTERNET_FLAG_ASYNC)
782         CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
783     else
784         todo_wine CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
785     /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
786     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTING_TO_SERVER);
787     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTED_TO_SERVER);
788
789     /* tests invalid dwStructSize */
790     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS)+1;
791     inetbuffers.lpcszHeader = NULL;
792     inetbuffers.dwHeadersLength = 0;
793     inetbuffers.dwBufferLength = 10;
794     inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
795     inetbuffers.dwOffsetHigh = 1234;
796     inetbuffers.dwOffsetLow = 5678;
797     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
798     ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
799         "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
800         rc ? "TRUE" : "FALSE", GetLastError());
801     HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
802
803     /* tests to see whether lpcszHeader is used - it isn't */
804     inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
805     inetbuffers.lpcszHeader = (LPCTSTR)0xdeadbeef;
806     inetbuffers.dwHeadersLength = 255;
807     inetbuffers.dwBufferLength = 0;
808     inetbuffers.lpvBuffer = NULL;
809     inetbuffers.dwOffsetHigh = 1234;
810     inetbuffers.dwOffsetLow = 5678;
811     SET_EXPECT(INTERNET_STATUS_RECEIVING_RESPONSE);
812     SET_EXPECT(INTERNET_STATUS_RESPONSE_RECEIVED);
813     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
814     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
815     rc = InternetReadFileEx(hor, &inetbuffers, 0, 0xdeadcafe);
816     ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
817         trace("read %i bytes\n", inetbuffers.dwBufferLength);
818     todo_wine
819     {
820         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
821         CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
822     }
823
824     rc = InternetReadFileEx(NULL, &inetbuffers, 0, 0xdeadcafe);
825     ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
826         "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
827         rc ? "TRUE" : "FALSE", GetLastError());
828
829     length = 0;
830     trace("Entering Query loop\n");
831
832     SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
833     SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
834     while (TRUE)
835     {
836         inetbuffers.dwStructSize = sizeof(INTERNET_BUFFERS);
837         inetbuffers.dwBufferLength = 1024;
838         inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
839         inetbuffers.dwOffsetHigh = 1234;
840         inetbuffers.dwOffsetLow = 5678;
841
842         SET_WINE_ALLOW(INTERNET_STATUS_RECEIVING_RESPONSE);
843         SET_WINE_ALLOW(INTERNET_STATUS_RESPONSE_RECEIVED);
844         SET_EXPECT(INTERNET_STATUS_CLOSING_CONNECTION);
845         SET_EXPECT(INTERNET_STATUS_CONNECTION_CLOSED);
846         SET_EXPECT(INTERNET_STATUS_REQUEST_COMPLETE);
847         rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
848         if (!rc)
849         {
850             if (GetLastError() == ERROR_IO_PENDING)
851             {
852                 trace("InternetReadFileEx -> PENDING\n");
853                 ok(flags & INTERNET_FLAG_ASYNC,
854                    "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
855                 CHECK_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
856                 WaitForSingleObject(hCompleteEvent, INFINITE);
857                 CHECK_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
858                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
859             }
860             else
861             {
862                 trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
863                 break;
864             }
865         }
866         else
867         {
868             trace("InternetReadFileEx -> SUCCEEDED\n");
869             CHECK_NOT_NOTIFIED(INTERNET_STATUS_REQUEST_COMPLETE);
870             if (inetbuffers.dwBufferLength)
871             {
872                 todo_wine {
873                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
874                 CHECK_NOT_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
875                 }
876             }
877             else
878             {
879                 /* Win98 still sends these when 0 bytes are read, WinXP does not */
880                 CLEAR_NOTIFIED(INTERNET_STATUS_RECEIVING_RESPONSE);
881                 CLEAR_NOTIFIED(INTERNET_STATUS_RESPONSE_RECEIVED);
882             }
883         }
884
885         trace("read %i bytes\n", inetbuffers.dwBufferLength);
886         ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
887
888         ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
889             "InternetReadFileEx sets offsets to 0x%x%08x\n",
890             inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
891
892         HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
893
894         if (!inetbuffers.dwBufferLength)
895             break;
896
897         length += inetbuffers.dwBufferLength;
898     }
899     ok(length > 0, "failed to read any of the document\n");
900     trace("Finished. Read %d bytes\n", length);
901
902     /* WinXP does not send, but Win98 does */
903     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
904     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
905 abort:
906     SET_EXPECT2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
907     if (hor) {
908         SET_WINE_ALLOW(INTERNET_STATUS_CLOSING_CONNECTION);
909         SET_WINE_ALLOW(INTERNET_STATUS_CONNECTION_CLOSED);
910         rc = InternetCloseHandle(hor);
911         ok ((rc != 0), "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
912         rc = InternetCloseHandle(hor);
913         ok ((rc == 0), "Double close of handle opened by HttpOpenRequestA succeeded\n");
914     }
915     if (hic) {
916         rc = InternetCloseHandle(hic);
917         ok ((rc != 0), "InternetCloseHandle of handle opened by InternetConnectA failed\n");
918     }
919     if (hi) {
920       SET_WINE_ALLOW(INTERNET_STATUS_HANDLE_CLOSING);
921       rc = InternetCloseHandle(hi);
922       ok ((rc != 0), "InternetCloseHandle of handle opened by InternetOpenA failed\n");
923       if (flags & INTERNET_FLAG_ASYNC)
924           Sleep(100);
925       CHECK_NOTIFIED2(INTERNET_STATUS_HANDLE_CLOSING, (hor != 0x0) + (hic != 0x0));
926     }
927     /* to enable once Wine is fixed to never send it
928     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
929     CHECK_NOT_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
930     */
931     CLEAR_NOTIFIED(INTERNET_STATUS_CLOSING_CONNECTION);
932     CLEAR_NOTIFIED(INTERNET_STATUS_CONNECTION_CLOSED);
933     CloseHandle(hCompleteEvent);
934     first_connection_to_test_url = FALSE;
935 }
936
937 static void InternetOpenUrlA_test(void)
938 {
939   HINTERNET myhinternet, myhttp;
940   char buffer[0x400];
941   DWORD size, readbytes, totalbytes=0;
942   BOOL ret;
943
944   myhinternet = InternetOpen("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
945   ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
946   size = 0x400;
947   ret = InternetCanonicalizeUrl(TEST_URL, buffer, &size,ICU_BROWSER_MODE);
948   ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
949
950   SetLastError(0);
951   myhttp = InternetOpenUrl(myhinternet, TEST_URL, 0, 0,
952                            INTERNET_FLAG_RELOAD|INTERNET_FLAG_NO_CACHE_WRITE|INTERNET_FLAG_TRANSFER_BINARY,0);
953   if (GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
954     return; /* WinXP returns this when not connected to the net */
955   ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
956   ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
957   ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
958   totalbytes += readbytes;
959   while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
960     totalbytes += readbytes;
961   trace("read 0x%08x bytes\n",totalbytes);
962
963   InternetCloseHandle(myhttp);
964   InternetCloseHandle(myhinternet);
965 }
966
967 static void HttpSendRequestEx_test(void)
968 {
969     HINTERNET hSession;
970     HINTERNET hConnect;
971     HINTERNET hRequest;
972
973     INTERNET_BUFFERS BufferIn;
974     DWORD dwBytesWritten, dwBytesRead, error;
975     CHAR szBuffer[256];
976     int i;
977     BOOL ret;
978
979     static char szPostData[] = "mode=Test";
980     static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
981
982     hSession = InternetOpen("Wine Regression Test",
983             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
984     ok( hSession != NULL ,"Unable to open Internet session\n");
985     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
986             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
987             0);
988     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
989     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
990             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
991     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
992     {
993         skip( "Network unreachable, skipping test\n" );
994         goto done;
995     }
996     ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
997
998
999     BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS);
1000     BufferIn.Next = (LPINTERNET_BUFFERS)0xdeadcab;
1001     BufferIn.lpcszHeader = szContentType;
1002     BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1003     BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1004     BufferIn.lpvBuffer = szPostData;
1005     BufferIn.dwBufferLength = 3;
1006     BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1007     BufferIn.dwOffsetLow = 0;
1008     BufferIn.dwOffsetHigh = 0;
1009
1010     SetLastError(0xdeadbeef);
1011     ret = HttpSendRequestEx(hRequest, &BufferIn, NULL, 0 ,0);
1012     error = GetLastError();
1013     ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1014     ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1015
1016     for (i = 3; szPostData[i]; i++)
1017         ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1018                 "InternetWriteFile failed\n");
1019
1020     ok(HttpEndRequest(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1021
1022     ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1023             "Unable to read response\n");
1024     szBuffer[dwBytesRead] = 0;
1025
1026     ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1027     ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0,"Got string %s\n",szBuffer);
1028
1029     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1030 done:
1031     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1032     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1033 }
1034
1035 static void InternetOpenRequest_test(void)
1036 {
1037     HINTERNET session, connect, request;
1038     static const char *types[] = { "*", "", NULL };
1039     static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1040     static const WCHAR *typesW[] = { any, empty, NULL };
1041     BOOL ret;
1042
1043     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1044     ok(session != NULL ,"Unable to open Internet session\n");
1045
1046     connect = InternetConnectA(session, NULL, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1047                               INTERNET_SERVICE_HTTP, 0, 0);
1048     ok(connect == NULL, "InternetConnectA should have failed\n");
1049     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1050
1051     connect = InternetConnectA(session, "", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1052                               INTERNET_SERVICE_HTTP, 0, 0);
1053     ok(connect == NULL, "InternetConnectA should have failed\n");
1054     ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1055
1056     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1057                               INTERNET_SERVICE_HTTP, 0, 0);
1058     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1059
1060     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1061     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1062     {
1063         skip( "Network unreachable, skipping test\n" );
1064         goto done;
1065     }
1066     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1067
1068     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1069     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1070     ok(InternetCloseHandle(request), "Close request handle failed\n");
1071
1072     request = HttpOpenRequestW(connect, NULL, slash, NULL, NULL, typesW, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1073     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1074
1075     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1076     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1077     ok(InternetCloseHandle(request), "Close request handle failed\n");
1078
1079 done:
1080     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1081     ok(InternetCloseHandle(session), "Close session handle failed\n");
1082 }
1083
1084 static void test_http_cache(void)
1085 {
1086     HINTERNET session, connect, request;
1087     char file_name[MAX_PATH], url[INTERNET_MAX_URL_LENGTH];
1088     DWORD size, file_size;
1089     BYTE buf[100];
1090     HANDLE file;
1091     BOOL ret;
1092
1093     static const char *types[] = { "*", "", NULL };
1094
1095     session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1096     ok(session != NULL ,"Unable to open Internet session\n");
1097
1098     connect = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
1099                               INTERNET_SERVICE_HTTP, 0, 0);
1100     ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1101
1102     request = HttpOpenRequestA(connect, NULL, "/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1103     if (!request && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1104     {
1105         skip( "Network unreachable, skipping test\n" );
1106
1107         ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1108         ok(InternetCloseHandle(session), "Close session handle failed\n");
1109
1110         return;
1111     }
1112     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1113
1114     size = sizeof(url);
1115     ret = InternetQueryOptionA(request, INTERNET_OPTION_URL, url, &size);
1116     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1117     ok(!strcmp(url, "http://test.winehq.org/hello.html"), "Wrong URL %s\n", url);
1118
1119     size = sizeof(file_name);
1120     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1121     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1122     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1123     ok(!size, "size = %d\n", size);
1124
1125     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1126     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1127
1128     size = sizeof(file_name);
1129     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1130     ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1131
1132     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1133                       FILE_ATTRIBUTE_NORMAL, NULL);
1134     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1135     file_size = GetFileSize(file, NULL);
1136     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1137
1138     size = sizeof(buf);
1139     ret = InternetReadFile(request, buf, sizeof(buf), &size);
1140     ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1141     ok(size == 100, "size = %u\n", size);
1142
1143     file_size = GetFileSize(file, NULL);
1144     todo_wine ok(file_size == 106, "file size = %u\n", file_size);
1145     CloseHandle(file);
1146
1147     ok(InternetCloseHandle(request), "Close request handle failed\n");
1148
1149     file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1150                       FILE_ATTRIBUTE_NORMAL, NULL);
1151     ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1152     CloseHandle(file);
1153
1154     request = HttpOpenRequestA(connect, NULL, "/", NULL, NULL, types, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1155     ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1156
1157     size = sizeof(file_name);
1158     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1159     ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1160     ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1161     ok(!size, "size = %d\n", size);
1162
1163     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1164     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1165
1166     size = sizeof(file_name);
1167     file_name[0] = 0;
1168     ret = InternetQueryOptionA(request, INTERNET_OPTION_DATAFILE_NAME, file_name, &size);
1169     if (ret)
1170     {
1171         file = CreateFile(file_name, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
1172                       FILE_ATTRIBUTE_NORMAL, NULL);
1173         ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1174         CloseHandle(file);
1175     }
1176     else
1177     {
1178         /* < IE8 */
1179         ok(file_name[0] == 0, "Didn't expect a file name\n");
1180     }
1181
1182     ok(InternetCloseHandle(request), "Close request handle failed\n");
1183     ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1184     ok(InternetCloseHandle(session), "Close session handle failed\n");
1185 }
1186
1187 static void HttpHeaders_test(void)
1188 {
1189     HINTERNET hSession;
1190     HINTERNET hConnect;
1191     HINTERNET hRequest;
1192     CHAR      buffer[256];
1193     WCHAR     wbuffer[256];
1194     DWORD     len = 256;
1195     DWORD     oldlen;
1196     DWORD     index = 0;
1197
1198     hSession = InternetOpen("Wine Regression Test",
1199             INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
1200     ok( hSession != NULL ,"Unable to open Internet session\n");
1201     hConnect = InternetConnect(hSession, "crossover.codeweavers.com",
1202             INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0,
1203             0);
1204     ok( hConnect != NULL, "Unable to connect to http://crossover.codeweavers.com\n");
1205     hRequest = HttpOpenRequest(hConnect, "POST", "/posttest.php",
1206             NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
1207     if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1208     {
1209         skip( "Network unreachable, skipping test\n" );
1210         goto done;
1211     }
1212     ok( hRequest != NULL, "Failed to open request handle\n");
1213
1214     index = 0;
1215     len = sizeof(buffer);
1216     strcpy(buffer,"Warning");
1217     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1218                buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1219
1220     ok(HttpAddRequestHeaders(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1221             "Failed to add new header\n");
1222
1223     index = 0;
1224     len = sizeof(buffer);
1225     strcpy(buffer,"Warning");
1226     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1227                 buffer,&len,&index),"Unable to query header\n");
1228     ok(index == 1, "Index was not incremented\n");
1229     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1230     ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1231     ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1232     len = sizeof(buffer);
1233     strcpy(buffer,"Warning");
1234     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1235                 buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1236
1237     index = 0;
1238     len = 5; /* could store the string but not the NULL terminator */
1239     strcpy(buffer,"Warning");
1240     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1241                 buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1242     ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1243     ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1244
1245     /* a call with NULL will fail but will return the length */
1246     index = 0;
1247     len = sizeof(buffer);
1248     SetLastError(0xdeadbeef);
1249     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1250                 NULL,&len,&index) == FALSE,"Query worked\n");
1251     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1252     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1253     ok(index == 0, "Index was incremented\n");
1254
1255     /* even for a len that is too small */
1256     index = 0;
1257     len = 15;
1258     SetLastError(0xdeadbeef);
1259     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1260                 NULL,&len,&index) == FALSE,"Query worked\n");
1261     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1262     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1263     ok(index == 0, "Index was incremented\n");
1264
1265     index = 0;
1266     len = 0;
1267     SetLastError(0xdeadbeef);
1268     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1269                 NULL,&len,&index) == FALSE,"Query worked\n");
1270     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1271     ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1272     ok(index == 0, "Index was incremented\n");
1273     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1274
1275
1276     /* a working query */
1277     index = 0;
1278     len = sizeof(buffer);
1279     memset(buffer, 'x', sizeof(buffer));
1280     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1281                 buffer,&len,&index),"Unable to query header\n");
1282     ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1283     ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1284     ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1285     /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1286     ok(strncmp(buffer, "POST /posttest.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1287     ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1288     ok(index == 0, "Index was incremented\n");
1289
1290     /* Like above two tests, but for W version */
1291
1292     index = 0;
1293     len = 0;
1294     SetLastError(0xdeadbeef);
1295     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1296                 NULL,&len,&index) == FALSE,"Query worked\n");
1297     ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1298     ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1299     ok(index == 0, "Index was incremented\n");
1300     oldlen = len;   /* bytes; at least long enough to hold buffer & nul */
1301
1302     /* a working query */
1303     index = 0;
1304     len = sizeof(wbuffer);
1305     memset(wbuffer, 'x', sizeof(wbuffer));
1306     ok(HttpQueryInfoW(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1307                 wbuffer,&len,&index),"Unable to query header\n");
1308     ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1309     ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1310     ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1311     ok(index == 0, "Index was incremented\n");
1312
1313     /* end of W version tests */
1314
1315     /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1316     index = 0;
1317     len = sizeof(buffer);
1318     memset(buffer, 'x', sizeof(buffer));
1319     ok(HttpQueryInfo(hRequest,HTTP_QUERY_RAW_HEADERS_CRLF,
1320                 buffer,&len,&index) == TRUE,"Query failed\n");
1321     ok(len == 2, "Expected 2, got %d\n", len);
1322     ok(strcmp(buffer, "\r\n") == 0, "Expected CRLF, got '%s'\n", buffer);
1323     ok(index == 0, "Index was incremented\n");
1324
1325     ok(HttpAddRequestHeaders(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1326             "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1327
1328     index = 0;
1329     len = sizeof(buffer);
1330     strcpy(buffer,"Warning");
1331     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1332                 buffer,&len,&index),"Unable to query header\n");
1333     ok(index == 1, "Index was not incremented\n");
1334     ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1335     len = sizeof(buffer);
1336     strcpy(buffer,"Warning");
1337     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1338                 buffer,&len,&index),"Failed to get second header\n");
1339     ok(index == 2, "Index was not incremented\n");
1340     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1341     len = sizeof(buffer);
1342     strcpy(buffer,"Warning");
1343     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1344                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1345
1346     ok(HttpAddRequestHeaders(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1347
1348     index = 0;
1349     len = sizeof(buffer);
1350     strcpy(buffer,"Warning");
1351     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1352                 buffer,&len,&index),"Unable to query header\n");
1353     ok(index == 1, "Index was not incremented\n");
1354     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1355     len = sizeof(buffer);
1356     strcpy(buffer,"Warning");
1357     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1358                 buffer,&len,&index),"Failed to get second header\n");
1359     ok(index == 2, "Index was not incremented\n");
1360     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1361     len = sizeof(buffer);
1362     strcpy(buffer,"Warning");
1363     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1364                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1365
1366     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1367
1368     index = 0;
1369     len = sizeof(buffer);
1370     strcpy(buffer,"Warning");
1371     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1372                 buffer,&len,&index),"Unable to query header\n");
1373     ok(index == 1, "Index was not incremented\n");
1374     ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1375     len = sizeof(buffer);
1376     strcpy(buffer,"Warning");
1377     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1378                 buffer,&len,&index),"Failed to get second header\n");
1379     ok(index == 2, "Index was not incremented\n");
1380     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1381     len = sizeof(buffer);
1382     strcpy(buffer,"Warning");
1383     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1384                 buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1385
1386     ok(HttpAddRequestHeaders(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1387
1388     index = 0;
1389     len = sizeof(buffer);
1390     strcpy(buffer,"Warning");
1391     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS,
1392                 buffer,&len,&index),"Unable to query header\n");
1393     ok(index == 1, "Index was not incremented\n");
1394     ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1395     len = sizeof(buffer);
1396     strcpy(buffer,"Warning");
1397     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1398     ok(index == 2, "Index was not incremented\n");
1399     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1400     len = sizeof(buffer);
1401     strcpy(buffer,"Warning");
1402     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1403
1404     ok(HttpAddRequestHeaders(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1405
1406     index = 0;
1407     len = sizeof(buffer);
1408     strcpy(buffer,"Warning");
1409     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1410     ok(index == 1, "Index was not incremented\n");
1411     ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1412     len = sizeof(buffer);
1413     strcpy(buffer,"Warning");
1414     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1415     ok(index == 2, "Index was not incremented\n");
1416     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1417     len = sizeof(buffer);
1418     strcpy(buffer,"Warning");
1419     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1420
1421     ok(HttpAddRequestHeaders(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1422
1423     index = 0;
1424     len = sizeof(buffer);
1425     strcpy(buffer,"Warning");
1426     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1427     ok(index == 1, "Index was not incremented\n");
1428     ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1429     len = sizeof(buffer);
1430     strcpy(buffer,"Warning");
1431     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1432     ok(index == 2, "Index was not incremented\n");
1433     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1434     len = sizeof(buffer);
1435     strcpy(buffer,"Warning");
1436     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1437
1438     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");
1439
1440     index = 0;
1441     len = sizeof(buffer);
1442     strcpy(buffer,"Warning");
1443     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1444     ok(index == 1, "Index was not incremented\n");
1445     ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1446     len = sizeof(buffer);
1447     strcpy(buffer,"Warning");
1448     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1449     ok(index == 2, "Index was not incremented\n");
1450     ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1451     len = sizeof(buffer);
1452     strcpy(buffer,"Warning");
1453     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1454
1455     /* Ensure that blank headers are ignored and don't cause a failure */
1456     ok(HttpAddRequestHeaders(hRequest,"\r\nBlankTest:value\r\n\r\n",-1, HTTP_ADDREQ_FLAG_ADD_IF_NEW), "Failed to add header with blank entries in list\n");
1457
1458     index = 0;
1459     len = sizeof(buffer);
1460     strcpy(buffer,"BlankTest");
1461     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1462     ok(index == 1, "Index was not incremented\n");
1463     ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1464
1465     /* Ensure that malformed header separators are ignored and don't cause a failure */
1466     ok(HttpAddRequestHeaders(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1467         "Failed to add header with malformed entries in list\n");
1468
1469     index = 0;
1470     len = sizeof(buffer);
1471     strcpy(buffer,"MalformedTest");
1472     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1473     ok(index == 1, "Index was not incremented\n");
1474     ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1475     index = 0;
1476     len = sizeof(buffer);
1477     strcpy(buffer,"MalformedTestTwo");
1478     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1479     ok(index == 1, "Index was not incremented\n");
1480     ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
1481     index = 0;
1482     len = sizeof(buffer);
1483     strcpy(buffer,"MalformedTestThree");
1484     ok(HttpQueryInfo(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1485     ok(index == 1, "Index was not incremented\n");
1486     ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
1487
1488     ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1489 done:
1490     ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1491     ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1492 }
1493
1494 static const char garbagemsg[] =
1495 "Garbage: Header\r\n";
1496
1497 static const char contmsg[] =
1498 "HTTP/1.1 100 Continue\r\n";
1499
1500 static const char expandcontmsg[] =
1501 "HTTP/1.1 100 Continue\r\n"
1502 "Server: winecontinue\r\n"
1503 "Tag: something witty\r\n"
1504 "\r\n";
1505
1506 static const char okmsg[] =
1507 "HTTP/1.1 200 OK\r\n"
1508 "Server: winetest\r\n"
1509 "\r\n";
1510
1511 static const char okmsg2[] =
1512 "HTTP/1.1 200 OK\r\n"
1513 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
1514 "Server: winetest\r\n"
1515 "Content-Length: 0\r\n"
1516 "Set-Cookie: one\r\n"
1517 "Set-Cookie: two\r\n"
1518 "\r\n";
1519
1520 static const char notokmsg[] =
1521 "HTTP/1.1 400 Bad Request\r\n"
1522 "Server: winetest\r\n"
1523 "\r\n";
1524
1525 static const char noauthmsg[] =
1526 "HTTP/1.1 401 Unauthorized\r\n"
1527 "Server: winetest\r\n"
1528 "Connection: close\r\n"
1529 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
1530 "\r\n";
1531
1532 static const char noauthmsg2[] =
1533 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
1534 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
1535 "\0d`0|6\n"
1536 "Server: winetest\r\n";
1537
1538 static const char proxymsg[] =
1539 "HTTP/1.1 407 Proxy Authentication Required\r\n"
1540 "Server: winetest\r\n"
1541 "Proxy-Connection: close\r\n"
1542 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
1543 "\r\n";
1544
1545 static const char page1[] =
1546 "<HTML>\r\n"
1547 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
1548 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
1549 "</HTML>\r\n\r\n";
1550
1551 struct server_info {
1552     HANDLE hEvent;
1553     int port;
1554 };
1555
1556 static DWORD CALLBACK server_thread(LPVOID param)
1557 {
1558     struct server_info *si = param;
1559     int r, c, i, on;
1560     SOCKET s;
1561     struct sockaddr_in sa;
1562     char buffer[0x100];
1563     WSADATA wsaData;
1564     int last_request = 0;
1565     char host_header[22];
1566     static int test_b = 0;
1567
1568     WSAStartup(MAKEWORD(1,1), &wsaData);
1569
1570     s = socket(AF_INET, SOCK_STREAM, 0);
1571     if (s == INVALID_SOCKET)
1572         return 1;
1573
1574     on = 1;
1575     setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
1576
1577     memset(&sa, 0, sizeof sa);
1578     sa.sin_family = AF_INET;
1579     sa.sin_port = htons(si->port);
1580     sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
1581
1582     r = bind(s, (struct sockaddr*) &sa, sizeof sa);
1583     if (r<0)
1584         return 1;
1585
1586     listen(s, 0);
1587
1588     SetEvent(si->hEvent);
1589
1590     sprintf(host_header, "Host: localhost:%d", si->port);
1591
1592     do
1593     {
1594         c = accept(s, NULL, NULL);
1595
1596         memset(buffer, 0, sizeof buffer);
1597         for(i=0; i<(sizeof buffer-1); i++)
1598         {
1599             r = recv(c, &buffer[i], 1, 0);
1600             if (r != 1)
1601                 break;
1602             if (i<4) continue;
1603             if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
1604                 buffer[i-3] == '\r' && buffer[i-1] == '\r')
1605                 break;
1606         }
1607         if (strstr(buffer, "GET /test1"))
1608         {
1609             if (!strstr(buffer, "Content-Length: 0"))
1610             {
1611                 send(c, okmsg, sizeof okmsg-1, 0);
1612                 send(c, page1, sizeof page1-1, 0);
1613             }
1614             else
1615                 send(c, notokmsg, sizeof notokmsg-1, 0);
1616         }
1617         if (strstr(buffer, "/test2"))
1618         {
1619             if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
1620             {
1621                 send(c, okmsg, sizeof okmsg-1, 0);
1622                 send(c, page1, sizeof page1-1, 0);
1623             }
1624             else
1625                 send(c, proxymsg, sizeof proxymsg-1, 0);
1626         }
1627         if (strstr(buffer, "/test3"))
1628         {
1629             if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
1630                 send(c, okmsg, sizeof okmsg-1, 0);
1631             else
1632                 send(c, noauthmsg, sizeof noauthmsg-1, 0);
1633         }
1634         if (strstr(buffer, "/test4"))
1635         {
1636             if (strstr(buffer, "Connection: Close"))
1637                 send(c, okmsg, sizeof okmsg-1, 0);
1638             else
1639                 send(c, notokmsg, sizeof notokmsg-1, 0);
1640         }
1641         if (strstr(buffer, "POST /test5") ||
1642             strstr(buffer, "RPC_IN_DATA /test5") ||
1643             strstr(buffer, "RPC_OUT_DATA /test5"))
1644         {
1645             if (strstr(buffer, "Content-Length: 0"))
1646             {
1647                 send(c, okmsg, sizeof okmsg-1, 0);
1648                 send(c, page1, sizeof page1-1, 0);
1649             }
1650             else
1651                 send(c, notokmsg, sizeof notokmsg-1, 0);
1652         }
1653         if (strstr(buffer, "GET /test6"))
1654         {
1655             send(c, contmsg, sizeof contmsg-1, 0);
1656             send(c, contmsg, sizeof contmsg-1, 0);
1657             send(c, okmsg, sizeof okmsg-1, 0);
1658             send(c, page1, sizeof page1-1, 0);
1659         }
1660         if (strstr(buffer, "POST /test7"))
1661         {
1662             if (strstr(buffer, "Content-Length: 100"))
1663             {
1664                 send(c, okmsg, sizeof okmsg-1, 0);
1665                 send(c, page1, sizeof page1-1, 0);
1666             }
1667             else
1668                 send(c, notokmsg, sizeof notokmsg-1, 0);
1669         }
1670         if (strstr(buffer, "/test8"))
1671         {
1672             if (!strstr(buffer, "Connection: Close") &&
1673                  strstr(buffer, "Connection: Keep-Alive") &&
1674                 !strstr(buffer, "Cache-Control: no-cache") &&
1675                 !strstr(buffer, "Pragma: no-cache") &&
1676                  strstr(buffer, host_header))
1677                 send(c, okmsg, sizeof okmsg-1, 0);
1678             else
1679                 send(c, notokmsg, sizeof notokmsg-1, 0);
1680         }
1681         if (strstr(buffer, "/test9"))
1682         {
1683             if (!strstr(buffer, "Connection: Close") &&
1684                 !strstr(buffer, "Connection: Keep-Alive") &&
1685                 !strstr(buffer, "Cache-Control: no-cache") &&
1686                 !strstr(buffer, "Pragma: no-cache") &&
1687                  strstr(buffer, host_header))
1688                 send(c, okmsg, sizeof okmsg-1, 0);
1689             else
1690                 send(c, notokmsg, sizeof notokmsg-1, 0);
1691         }
1692         if (strstr(buffer, "/testA"))
1693         {
1694             if (!strstr(buffer, "Connection: Close") &&
1695                 !strstr(buffer, "Connection: Keep-Alive") &&
1696                 (strstr(buffer, "Cache-Control: no-cache") ||
1697                  strstr(buffer, "Pragma: no-cache")) &&
1698                  strstr(buffer, host_header))
1699                 send(c, okmsg, sizeof okmsg-1, 0);
1700             else
1701                 send(c, notokmsg, sizeof notokmsg-1, 0);
1702         }
1703         if (!test_b && strstr(buffer, "/testB HTTP/1.1"))
1704         {
1705             test_b = 1;
1706             send(c, okmsg, sizeof okmsg-1, 0);
1707             recvfrom(c, buffer, sizeof buffer, 0, NULL, NULL);
1708             send(c, okmsg, sizeof okmsg-1, 0);
1709         }
1710         if (strstr(buffer, "/testC"))
1711         {
1712             if (strstr(buffer, "Cookie: cookie=biscuit"))
1713                 send(c, okmsg, sizeof okmsg-1, 0);
1714             else
1715                 send(c, notokmsg, sizeof notokmsg-1, 0);
1716         }
1717         if (strstr(buffer, "/testD"))
1718         {
1719             send(c, okmsg2, sizeof okmsg2-1, 0);
1720         }
1721         if (strstr(buffer, "/testE"))
1722         {
1723             send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
1724         }
1725         if (strstr(buffer, "GET /quit"))
1726         {
1727             send(c, okmsg, sizeof okmsg-1, 0);
1728             send(c, page1, sizeof page1-1, 0);
1729             last_request = 1;
1730         }
1731         if (strstr(buffer, "GET /testF"))
1732         {
1733             send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
1734             send(c, garbagemsg, sizeof garbagemsg-1, 0);
1735             send(c, contmsg, sizeof contmsg-1, 0);
1736             send(c, garbagemsg, sizeof garbagemsg-1, 0);
1737             send(c, okmsg, sizeof okmsg-1, 0);
1738             send(c, page1, sizeof page1-1, 0);
1739         }
1740         if (strstr(buffer, "GET /testG"))
1741         {
1742             send(c, page1, sizeof page1-1, 0);
1743         }
1744
1745         shutdown(c, 2);
1746         closesocket(c);
1747     } while (!last_request);
1748
1749     closesocket(s);
1750
1751     return 0;
1752 }
1753
1754 static void test_basic_request(int port, const char *verb, const char *url)
1755 {
1756     HINTERNET hi, hc, hr;
1757     DWORD r, count;
1758     char buffer[0x100];
1759
1760     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1761     ok(hi != NULL, "open failed\n");
1762
1763     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1764     ok(hc != NULL, "connect failed\n");
1765
1766     hr = HttpOpenRequest(hc, verb, url, NULL, NULL, NULL, 0, 0);
1767     ok(hr != NULL, "HttpOpenRequest failed\n");
1768
1769     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1770     ok(r, "HttpSendRequest failed\n");
1771
1772     count = 0;
1773     memset(buffer, 0, sizeof buffer);
1774     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
1775     ok(r, "InternetReadFile failed\n");
1776     ok(count == sizeof page1 - 1, "count was wrong\n");
1777     ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
1778
1779     InternetCloseHandle(hr);
1780     InternetCloseHandle(hc);
1781     InternetCloseHandle(hi);
1782 }
1783
1784 static void test_last_error(int port)
1785 {
1786     HINTERNET hi, hc, hr;
1787     DWORD error;
1788     BOOL r;
1789
1790     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1791     ok(hi != NULL, "open failed\n");
1792
1793     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1794     ok(hc != NULL, "connect failed\n");
1795
1796     hr = HttpOpenRequest(hc, NULL, "/test1", NULL, NULL, NULL, 0, 0);
1797     ok(hr != NULL, "HttpOpenRequest failed\n");
1798
1799     SetLastError(0xdeadbeef);
1800     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1801     error = GetLastError();
1802     ok(r, "HttpSendRequest failed\n");
1803     ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
1804
1805     InternetCloseHandle(hr);
1806     InternetCloseHandle(hc);
1807     InternetCloseHandle(hi);
1808 }
1809
1810 static void test_proxy_indirect(int port)
1811 {
1812     HINTERNET hi, hc, hr;
1813     DWORD r, sz, val;
1814     char buffer[0x40];
1815
1816     hi = InternetOpen(NULL, 0, NULL, NULL, 0);
1817     ok(hi != NULL, "open failed\n");
1818
1819     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1820     ok(hc != NULL, "connect failed\n");
1821
1822     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1823     ok(hr != NULL, "HttpOpenRequest failed\n");
1824
1825     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1826     ok(r, "HttpSendRequest failed\n");
1827
1828     sz = sizeof buffer;
1829     r = HttpQueryInfo(hr, HTTP_QUERY_PROXY_AUTHENTICATE, buffer, &sz, NULL);
1830     ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
1831     if (!r)
1832     {
1833         skip("missing proxy header, not testing remaining proxy headers\n");
1834         goto out;
1835     }
1836     ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
1837
1838     sz = sizeof buffer;
1839     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1840     ok(r, "HttpQueryInfo failed\n");
1841     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1842
1843     sz = sizeof val;
1844     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE|HTTP_QUERY_FLAG_NUMBER, &val, &sz, NULL);
1845     ok(r, "HttpQueryInfo failed\n");
1846     ok(val == 407, "proxy code wrong\n");
1847
1848     sz = sizeof buffer;
1849     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &sz, NULL);
1850     ok(r, "HttpQueryInfo failed\n");
1851     ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
1852
1853     sz = sizeof buffer;
1854     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &sz, NULL);
1855     ok(r, "HttpQueryInfo failed\n");
1856     ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
1857
1858     sz = sizeof buffer;
1859     r = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &sz, NULL);
1860     ok(r, "HttpQueryInfo failed\n");
1861     ok(!strcmp(buffer, "winetest"), "http server wrong\n");
1862
1863     sz = sizeof buffer;
1864     r = HttpQueryInfo(hr, HTTP_QUERY_CONTENT_ENCODING, buffer, &sz, NULL);
1865     ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
1866     ok(r == FALSE, "HttpQueryInfo failed\n");
1867
1868 out:
1869     InternetCloseHandle(hr);
1870     InternetCloseHandle(hc);
1871     InternetCloseHandle(hi);
1872 }
1873
1874 static void test_proxy_direct(int port)
1875 {
1876     HINTERNET hi, hc, hr;
1877     DWORD r, sz;
1878     char buffer[0x40];
1879     static CHAR username[] = "mike",
1880                 password[] = "1101";
1881
1882     sprintf(buffer, "localhost:%d\n", port);
1883     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
1884     ok(hi != NULL, "open failed\n");
1885
1886     /* try connect without authorization */
1887     hc = InternetConnect(hi, "test.winehq.org/", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1888     ok(hc != NULL, "connect failed\n");
1889
1890     hr = HttpOpenRequest(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
1891     ok(hr != NULL, "HttpOpenRequest failed\n");
1892
1893     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1894     ok(r, "HttpSendRequest failed\n");
1895
1896     sz = sizeof buffer;
1897     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1898     ok(r, "HttpQueryInfo failed\n");
1899     ok(!strcmp(buffer, "407"), "proxy code wrong\n");
1900
1901
1902     /* set the user + password then try again */
1903     todo_wine {
1904     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_USERNAME, username, 4);
1905     ok(r, "failed to set user\n");
1906
1907     r = InternetSetOption(hr, INTERNET_OPTION_PROXY_PASSWORD, password, 4);
1908     ok(r, "failed to set password\n");
1909     }
1910
1911     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
1912     ok(r, "HttpSendRequest failed\n");
1913     sz = sizeof buffer;
1914     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &sz, NULL);
1915     ok(r, "HttpQueryInfo failed\n");
1916     todo_wine {
1917     ok(!strcmp(buffer, "200"), "proxy code wrong\n");
1918     }
1919
1920
1921     InternetCloseHandle(hr);
1922     InternetCloseHandle(hc);
1923     InternetCloseHandle(hi);
1924 }
1925
1926 static void test_header_handling_order(int port)
1927 {
1928     static char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
1929     static char connection[]    = "Connection: Close";
1930
1931     static const char *types[2] = { "*", NULL };
1932     HINTERNET session, connect, request;
1933     DWORD size, status;
1934     BOOL ret;
1935
1936     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
1937     ok(session != NULL, "InternetOpen failed\n");
1938
1939     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
1940     ok(connect != NULL, "InternetConnect failed\n");
1941
1942     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1943     ok(request != NULL, "HttpOpenRequest failed\n");
1944
1945     ret = HttpAddRequestHeaders(request, authorization, ~0u, HTTP_ADDREQ_FLAG_ADD);
1946     ok(ret, "HttpAddRequestHeaders failed\n");
1947
1948     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
1949     ok(ret, "HttpSendRequest failed\n");
1950
1951     status = 0;
1952     size = sizeof(status);
1953     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1954     ok(ret, "HttpQueryInfo failed\n");
1955     ok(status == 200, "request failed with status %u\n", status);
1956
1957     InternetCloseHandle(request);
1958
1959     request = HttpOpenRequest(connect, NULL, "/test4", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1960     ok(request != NULL, "HttpOpenRequest failed\n");
1961
1962     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1963     ok(ret, "HttpSendRequest failed\n");
1964
1965     status = 0;
1966     size = sizeof(status);
1967     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1968     ok(ret, "HttpQueryInfo failed\n");
1969     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1970
1971     InternetCloseHandle(request);
1972
1973     request = HttpOpenRequest(connect, "POST", "/test7", NULL, NULL, types, INTERNET_FLAG_KEEP_CONNECTION, 0);
1974     ok(request != NULL, "HttpOpenRequest failed\n");
1975
1976     ret = HttpAddRequestHeaders(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
1977     ok(ret, "HttpAddRequestHeaders failed\n");
1978
1979     ret = HttpSendRequest(request, connection, ~0u, NULL, 0);
1980     ok(ret, "HttpSendRequest failed\n");
1981
1982     status = 0;
1983     size = sizeof(status);
1984     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
1985     ok(ret, "HttpQueryInfo failed\n");
1986     ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
1987
1988     InternetCloseHandle(request);
1989     InternetCloseHandle(connect);
1990     InternetCloseHandle(session);
1991 }
1992
1993 static void test_connection_header(int port)
1994 {
1995     HINTERNET ses, con, req;
1996     DWORD size, status;
1997     BOOL ret;
1998
1999     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2000     ok(ses != NULL, "InternetOpen failed\n");
2001
2002     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2003     ok(con != NULL, "InternetConnect failed\n");
2004
2005     req = HttpOpenRequest(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2006     ok(req != NULL, "HttpOpenRequest failed\n");
2007
2008     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2009     ok(ret, "HttpSendRequest failed\n");
2010
2011     status = 0;
2012     size = sizeof(status);
2013     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2014     ok(ret, "HttpQueryInfo failed\n");
2015     ok(status == 200, "request failed with status %u\n", status);
2016
2017     InternetCloseHandle(req);
2018
2019     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
2020     ok(req != NULL, "HttpOpenRequest failed\n");
2021
2022     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2023     ok(ret, "HttpSendRequest failed\n");
2024
2025     status = 0;
2026     size = sizeof(status);
2027     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2028     ok(ret, "HttpQueryInfo failed\n");
2029     ok(status == 200, "request failed with status %u\n", status);
2030
2031     InternetCloseHandle(req);
2032
2033     req = HttpOpenRequest(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2034     ok(req != NULL, "HttpOpenRequest failed\n");
2035
2036     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2037     ok(ret, "HttpSendRequest failed\n");
2038
2039     status = 0;
2040     size = sizeof(status);
2041     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2042     ok(ret, "HttpQueryInfo failed\n");
2043     ok(status == 200, "request failed with status %u\n", status);
2044
2045     InternetCloseHandle(req);
2046
2047     req = HttpOpenRequest(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
2048     ok(req != NULL, "HttpOpenRequest failed\n");
2049
2050     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2051     ok(ret, "HttpSendRequest failed\n");
2052
2053     status = 0;
2054     size = sizeof(status);
2055     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2056     ok(ret, "HttpQueryInfo failed\n");
2057     ok(status == 200, "request failed with status %u\n", status);
2058
2059     InternetCloseHandle(req);
2060     InternetCloseHandle(con);
2061     InternetCloseHandle(ses);
2062 }
2063
2064 static void test_http1_1(int port)
2065 {
2066     HINTERNET ses, con, req;
2067     BOOL ret;
2068
2069     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2070     ok(ses != NULL, "InternetOpen failed\n");
2071
2072     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2073     ok(con != NULL, "InternetConnect failed\n");
2074
2075     req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2076     ok(req != NULL, "HttpOpenRequest failed\n");
2077
2078     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2079     if (ret)
2080     {
2081         InternetCloseHandle(req);
2082
2083         req = HttpOpenRequest(con, NULL, "/testB", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2084         ok(req != NULL, "HttpOpenRequest failed\n");
2085
2086         ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2087         ok(ret, "HttpSendRequest failed\n");
2088     }
2089
2090     InternetCloseHandle(req);
2091     InternetCloseHandle(con);
2092     InternetCloseHandle(ses);
2093 }
2094
2095 static void test_HttpSendRequestW(int port)
2096 {
2097     static const WCHAR header[] = {'U','A','-','C','P','U',':',' ','x','8','6',0};
2098     HINTERNET ses, con, req;
2099     DWORD error;
2100     BOOL ret;
2101
2102     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, INTERNET_FLAG_ASYNC);
2103     ok(ses != NULL, "InternetOpen failed\n");
2104
2105     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2106     ok(con != NULL, "InternetConnect failed\n");
2107
2108     req = HttpOpenRequest(con, NULL, "/test1", NULL, NULL, NULL, 0, 0);
2109     ok(req != NULL, "HttpOpenRequest failed\n");
2110
2111     SetLastError(0xdeadbeef);
2112     ret = HttpSendRequestW(req, header, ~0u, NULL, 0);
2113     error = GetLastError();
2114     ok(!ret, "HttpSendRequestW succeeded\n");
2115     ok(error == ERROR_IO_PENDING ||
2116        broken(error == ERROR_HTTP_HEADER_NOT_FOUND) ||  /* IE6 */
2117        broken(error == ERROR_INVALID_PARAMETER),        /* IE5 */
2118        "got %u expected ERROR_IO_PENDING\n", error);
2119
2120     InternetCloseHandle(req);
2121     InternetCloseHandle(con);
2122     InternetCloseHandle(ses);
2123 }
2124
2125 static void test_cookie_header(int port)
2126 {
2127     HINTERNET ses, con, req;
2128     DWORD size, status, error;
2129     BOOL ret;
2130     char buffer[64];
2131
2132     ses = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2133     ok(ses != NULL, "InternetOpen failed\n");
2134
2135     con = InternetConnect(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2136     ok(con != NULL, "InternetConnect failed\n");
2137
2138     InternetSetCookie("http://localhost", "cookie", "biscuit");
2139
2140     req = HttpOpenRequest(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
2141     ok(req != NULL, "HttpOpenRequest failed\n");
2142
2143     buffer[0] = 0;
2144     size = sizeof(buffer);
2145     SetLastError(0xdeadbeef);
2146     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2147     error = GetLastError();
2148     ok(!ret, "HttpQueryInfo succeeded\n");
2149     ok(error == ERROR_HTTP_HEADER_NOT_FOUND, "got %u expected ERROR_HTTP_HEADER_NOT_FOUND\n", error);
2150
2151     ret = HttpAddRequestHeaders(req, "Cookie: cookie=not biscuit\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD);
2152     ok(ret, "HttpAddRequestHeaders failed: %u\n", GetLastError());
2153
2154     buffer[0] = 0;
2155     size = sizeof(buffer);
2156     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2157     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2158     ok(!strcmp(buffer, "cookie=not biscuit"), "got '%s' expected \'cookie=not biscuit\'\n", buffer);
2159
2160     ret = HttpSendRequest(req, NULL, 0, NULL, 0);
2161     ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
2162
2163     status = 0;
2164     size = sizeof(status);
2165     ret = HttpQueryInfo(req, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL);
2166     ok(ret, "HttpQueryInfo failed\n");
2167     ok(status == 200, "request failed with status %u\n", status);
2168
2169     buffer[0] = 0;
2170     size = sizeof(buffer);
2171     ret = HttpQueryInfo(req, HTTP_QUERY_COOKIE | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2172     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2173     ok(!strcmp(buffer, "cookie=biscuit"), "got '%s' expected \'cookie=biscuit\'\n", buffer);
2174
2175     InternetCloseHandle(req);
2176     InternetCloseHandle(con);
2177     InternetCloseHandle(ses);
2178 }
2179
2180 static void test_basic_authentication(int port)
2181 {
2182     HINTERNET session, connect, request;
2183     DWORD size, status;
2184     BOOL ret;
2185
2186     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2187     ok(session != NULL, "InternetOpen failed\n");
2188
2189     connect = InternetConnect(session, "localhost", port, "user", "pwd", INTERNET_SERVICE_HTTP, 0, 0);
2190     ok(connect != NULL, "InternetConnect failed\n");
2191
2192     request = HttpOpenRequest(connect, NULL, "/test3", NULL, NULL, NULL, 0, 0);
2193     ok(request != NULL, "HttpOpenRequest failed\n");
2194
2195     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2196     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2197
2198     status = 0;
2199     size = sizeof(status);
2200     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2201     ok(ret, "HttpQueryInfo failed\n");
2202     ok(status == 200, "request failed with status %u\n", status);
2203
2204     InternetCloseHandle(request);
2205     InternetCloseHandle(connect);
2206     InternetCloseHandle(session);
2207 }
2208
2209 static void test_invalid_response_headers(int port)
2210 {
2211     HINTERNET session, connect, request;
2212     DWORD size, status;
2213     BOOL ret;
2214     char buffer[256];
2215
2216     session = InternetOpen("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2217     ok(session != NULL, "InternetOpen failed\n");
2218
2219     connect = InternetConnect(session, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2220     ok(connect != NULL, "InternetConnect failed\n");
2221
2222     request = HttpOpenRequest(connect, NULL, "/testE", NULL, NULL, NULL, 0, 0);
2223     ok(request != NULL, "HttpOpenRequest failed\n");
2224
2225     ret = HttpSendRequest(request, NULL, 0, NULL, 0);
2226     ok(ret, "HttpSendRequest failed %u\n", GetLastError());
2227
2228     status = 0;
2229     size = sizeof(status);
2230     ret = HttpQueryInfo( request, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2231     ok(ret, "HttpQueryInfo failed\n");
2232     ok(status == 401, "unexpected status %u\n", status);
2233
2234     buffer[0] = 0;
2235     size = sizeof(buffer);
2236     ret = HttpQueryInfo( request, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2237     ok(ret, "HttpQueryInfo failed\n");
2238     ok(!strcmp(buffer, "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"),
2239        "headers wrong \"%s\"\n", buffer);
2240
2241     buffer[0] = 0;
2242     size = sizeof(buffer);
2243     ret = HttpQueryInfo( request, HTTP_QUERY_SERVER, buffer, &size, NULL);
2244     ok(ret, "HttpQueryInfo failed\n");
2245     ok(!strcmp(buffer, "winetest"), "server wrong \"%s\"\n", buffer);
2246
2247     InternetCloseHandle(request);
2248     InternetCloseHandle(connect);
2249     InternetCloseHandle(session);
2250 }
2251
2252 static void test_response_without_headers(int port)
2253 {
2254     HINTERNET hi, hc, hr;
2255     DWORD r, count, size, status;
2256     char buffer[1024];
2257
2258     SetLastError(0xdeadbeef);
2259     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2260     ok(hi != NULL, "open failed %u\n", GetLastError());
2261
2262     SetLastError(0xdeadbeef);
2263     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2264     ok(hc != NULL, "connect failed %u\n", GetLastError());
2265
2266     SetLastError(0xdeadbeef);
2267     hr = HttpOpenRequest(hc, NULL, "/testG", NULL, NULL, NULL, 0, 0);
2268     ok(hr != NULL, "HttpOpenRequest failed %u\n", GetLastError());
2269
2270     SetLastError(0xdeadbeef);
2271     r = HttpSendRequest(hr, NULL, 0, NULL, 0);
2272     ok(r, "HttpSendRequest failed %u\n", GetLastError());
2273
2274     count = 0;
2275     memset(buffer, 0, sizeof buffer);
2276     SetLastError(0xdeadbeef);
2277     r = InternetReadFile(hr, buffer, sizeof buffer, &count);
2278     ok(r, "InternetReadFile failed %u\n", GetLastError());
2279     todo_wine ok(count == sizeof page1 - 1, "count was wrong\n");
2280     todo_wine ok(!memcmp(buffer, page1, sizeof page1), "http data wrong\n");
2281
2282     status = 0;
2283     size = sizeof(status);
2284     SetLastError(0xdeadbeef);
2285     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &status, &size, NULL );
2286     todo_wine ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2287     todo_wine ok(status == 200, "expected status 200 got %u\n", status);
2288
2289     buffer[0] = 0;
2290     size = sizeof(buffer);
2291     SetLastError(0xdeadbeef);
2292     r = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, NULL );
2293     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2294     ok(!strcmp(buffer, "OK"), "expected OK got: \"%s\"\n", buffer);
2295
2296     buffer[0] = 0;
2297     size = sizeof(buffer);
2298     SetLastError(0xdeadbeef);
2299     r = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, NULL);
2300     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2301     ok(!strcmp(buffer, "HTTP/1.0"), "expected HTTP/1.0 got: \"%s\"\n", buffer);
2302
2303     buffer[0] = 0;
2304     size = sizeof(buffer);
2305     SetLastError(0xdeadbeef);
2306     r = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, NULL);
2307     ok(r, "HttpQueryInfo failed %u\n", GetLastError());
2308     ok(!strcmp(buffer, "HTTP/1.0 200 OK"), "raw headers wrong: \"%s\"\n", buffer);
2309
2310     InternetCloseHandle(hr);
2311     InternetCloseHandle(hc);
2312     InternetCloseHandle(hi);
2313 }
2314
2315 static void test_HttpQueryInfo(int port)
2316 {
2317     HINTERNET hi, hc, hr;
2318     DWORD size, index;
2319     char buffer[1024];
2320     BOOL ret;
2321
2322     hi = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2323     ok(hi != NULL, "InternetOpen failed\n");
2324
2325     hc = InternetConnect(hi, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2326     ok(hc != NULL, "InternetConnect failed\n");
2327
2328     hr = HttpOpenRequest(hc, NULL, "/testD", NULL, NULL, NULL, 0, 0);
2329     ok(hr != NULL, "HttpOpenRequest failed\n");
2330
2331     ret = HttpSendRequest(hr, NULL, 0, NULL, 0);
2332     ok(ret, "HttpSendRequest failed\n");
2333
2334     index = 0;
2335     size = sizeof(buffer);
2336     ret = HttpQueryInfo(hr, HTTP_QUERY_HOST | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, &index);
2337     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2338     ok(index == 1, "expected 1 got %u\n", index);
2339
2340     index = 0;
2341     size = sizeof(buffer);
2342     ret = HttpQueryInfo(hr, HTTP_QUERY_DATE | HTTP_QUERY_FLAG_SYSTEMTIME, buffer, &size, &index);
2343     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2344     ok(index == 1, "expected 1 got %u\n", index);
2345
2346     index = 0;
2347     size = sizeof(buffer);
2348     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2349     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2350     ok(index == 0, "expected 0 got %u\n", index);
2351
2352     size = sizeof(buffer);
2353     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS, buffer, &size, &index);
2354     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2355     ok(index == 0, "expected 0 got %u\n", index);
2356
2357     size = sizeof(buffer);
2358     ret = HttpQueryInfo(hr, HTTP_QUERY_RAW_HEADERS_CRLF, buffer, &size, &index);
2359     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2360     ok(index == 0, "expected 0 got %u\n", index);
2361
2362     size = sizeof(buffer);
2363     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_TEXT, buffer, &size, &index);
2364     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2365     ok(index == 0, "expected 0 got %u\n", index);
2366
2367     size = sizeof(buffer);
2368     ret = HttpQueryInfo(hr, HTTP_QUERY_VERSION, buffer, &size, &index);
2369     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2370     ok(index == 0, "expected 0 got %u\n", index);
2371
2372     index = 0;
2373     size = sizeof(buffer);
2374     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE, buffer, &size, &index);
2375     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2376     ok(index == 0, "expected 0 got %u\n", index);
2377
2378     index = 0;
2379     size = sizeof(buffer);
2380     ret = HttpQueryInfo(hr, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, buffer, &size, &index);
2381     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2382     ok(index == 0, "expected 0 got %u\n", index);
2383
2384     index = 0xdeadbeef;
2385     size = sizeof(buffer);
2386     ret = HttpQueryInfo(hr, HTTP_QUERY_FORWARDED, buffer, &size, &index);
2387     ok(!ret, "HttpQueryInfo succeeded\n");
2388     ok(index == 0xdeadbeef, "expected 0xdeadbeef got %u\n", index);
2389
2390     index = 0;
2391     size = sizeof(buffer);
2392     ret = HttpQueryInfo(hr, HTTP_QUERY_SERVER, buffer, &size, &index);
2393     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2394     ok(index == 1, "expected 1 got %u\n", index);
2395
2396     index = 0;
2397     size = sizeof(buffer);
2398     strcpy(buffer, "Server");
2399     ret = HttpQueryInfo(hr, HTTP_QUERY_CUSTOM, buffer, &size, &index);
2400     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2401     ok(index == 1, "expected 1 got %u\n", index);
2402
2403     index = 0;
2404     size = sizeof(buffer);
2405     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2406     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2407     ok(index == 1, "expected 1 got %u\n", index);
2408
2409     size = sizeof(buffer);
2410     ret = HttpQueryInfo(hr, HTTP_QUERY_SET_COOKIE, buffer, &size, &index);
2411     ok(ret, "HttpQueryInfo failed %u\n", GetLastError());
2412     ok(index == 2, "expected 2 got %u\n", index);
2413
2414     InternetCloseHandle(hr);
2415     InternetCloseHandle(hc);
2416     InternetCloseHandle(hi);
2417 }
2418
2419 static void test_http_connection(void)
2420 {
2421     struct server_info si;
2422     HANDLE hThread;
2423     DWORD id = 0, r;
2424
2425     si.hEvent = CreateEvent(NULL, 0, 0, NULL);
2426     si.port = 7531;
2427
2428     hThread = CreateThread(NULL, 0, server_thread, (LPVOID) &si, 0, &id);
2429     ok( hThread != NULL, "create thread failed\n");
2430
2431     r = WaitForSingleObject(si.hEvent, 10000);
2432     ok (r == WAIT_OBJECT_0, "failed to start wininet test server\n");
2433     if (r != WAIT_OBJECT_0)
2434         return;
2435
2436     test_basic_request(si.port, "GET", "/test1");
2437     test_proxy_indirect(si.port);
2438     test_proxy_direct(si.port);
2439     test_header_handling_order(si.port);
2440     test_basic_request(si.port, "POST", "/test5");
2441     test_basic_request(si.port, "RPC_IN_DATA", "/test5");
2442     test_basic_request(si.port, "RPC_OUT_DATA", "/test5");
2443     test_basic_request(si.port, "GET", "/test6");
2444     test_basic_request(si.port, "GET", "/testF");
2445     test_connection_header(si.port);
2446     test_http1_1(si.port);
2447     test_cookie_header(si.port);
2448     test_basic_authentication(si.port);
2449     test_invalid_response_headers(si.port);
2450     test_response_without_headers(si.port);
2451     test_HttpQueryInfo(si.port);
2452     test_HttpSendRequestW(si.port);
2453     test_last_error(si.port);
2454
2455     /* send the basic request again to shutdown the server thread */
2456     test_basic_request(si.port, "GET", "/quit");
2457
2458     r = WaitForSingleObject(hThread, 3000);
2459     ok( r == WAIT_OBJECT_0, "thread wait failed\n");
2460     CloseHandle(hThread);
2461 }
2462
2463 static void test_user_agent_header(void)
2464 {
2465     HINTERNET ses, con, req;
2466     DWORD size, err;
2467     char buffer[64];
2468     BOOL ret;
2469
2470     ses = InternetOpen("Gizmo5", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
2471     ok(ses != NULL, "InternetOpen failed\n");
2472
2473     con = InternetConnect(ses, "test.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2474     ok(con != NULL, "InternetConnect failed\n");
2475
2476     req = HttpOpenRequest(con, "GET", "/hello.html", "HTTP/1.0", NULL, NULL, 0, 0);
2477     ok(req != NULL, "HttpOpenRequest failed\n");
2478
2479     size = sizeof(buffer);
2480     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2481     err = GetLastError();
2482     ok(!ret, "HttpQueryInfo succeeded\n");
2483     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2484
2485     ret = HttpAddRequestHeaders(req, "User-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2486     ok(ret, "HttpAddRequestHeaders succeeded\n");
2487
2488     size = sizeof(buffer);
2489     ret = HttpQueryInfo(req, HTTP_QUERY_USER_AGENT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2490     err = GetLastError();
2491     ok(ret, "HttpQueryInfo failed\n");
2492     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2493
2494     InternetCloseHandle(req);
2495
2496     req = HttpOpenRequest(con, "GET", "/", "HTTP/1.0", NULL, NULL, 0, 0);
2497     ok(req != NULL, "HttpOpenRequest failed\n");
2498
2499     size = sizeof(buffer);
2500     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2501     err = GetLastError();
2502     ok(!ret, "HttpQueryInfo succeeded\n");
2503     ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", err);
2504
2505     ret = HttpAddRequestHeaders(req, "Accept: audio/*, image/*, text/*\r\nUser-Agent: Gizmo Project\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
2506     ok(ret, "HttpAddRequestHeaders failed\n");
2507
2508     buffer[0] = 0;
2509     size = sizeof(buffer);
2510     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2511     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2512     ok(!strcmp(buffer, "audio/*, image/*, text/*"), "got '%s' expected 'audio/*, image/*, text/*'\n", buffer);
2513
2514     InternetCloseHandle(req);
2515     InternetCloseHandle(con);
2516     InternetCloseHandle(ses);
2517 }
2518
2519 static void test_bogus_accept_types_array(void)
2520 {
2521     HINTERNET ses, con, req;
2522     static const char *types[] = { (const char *)6240, "*/*", "%p", "", (const char *)0xffffffff, "*/*", NULL };
2523     DWORD size;
2524     char buffer[32];
2525     BOOL ret;
2526
2527     ses = InternetOpen("MERONG(0.9/;p)", INTERNET_OPEN_TYPE_DIRECT, "", "", 0);
2528     con = InternetConnect(ses, "www.winehq.org", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2529     req = HttpOpenRequest(con, "POST", "/post/post_action.php", "HTTP/1.0", "", types, INTERNET_FLAG_FORMS_SUBMIT, 0);
2530
2531     ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
2532
2533     buffer[0] = 0;
2534     size = sizeof(buffer);
2535     ret = HttpQueryInfo(req, HTTP_QUERY_ACCEPT | HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer, &size, NULL);
2536     ok(ret, "HttpQueryInfo failed: %u\n", GetLastError());
2537     ok(!strcmp(buffer, ", */*, %p, , , */*") || /* IE6 */
2538        !strcmp(buffer, "*/*, %p, */*"),
2539        "got '%s' expected '*/*, %%p, */*' or ', */*, %%p, , , */*'\n", buffer);
2540
2541     InternetCloseHandle(req);
2542     InternetCloseHandle(con);
2543     InternetCloseHandle(ses);
2544 }
2545
2546 struct context
2547 {
2548     HANDLE event;
2549     HINTERNET req;
2550 };
2551
2552 static void WINAPI cb(HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID info, DWORD size)
2553 {
2554     INTERNET_ASYNC_RESULT *result = info;
2555     struct context *ctx = (struct context *)context;
2556
2557     trace("%p 0x%08lx %u %p 0x%08x\n", handle, context, status, info, size);
2558
2559     if (status == INTERNET_STATUS_REQUEST_COMPLETE)
2560     {
2561         trace("request handle: 0x%08lx\n", result->dwResult);
2562         ctx->req = (HINTERNET)result->dwResult;
2563         SetEvent(ctx->event);
2564     }
2565     if (status == INTERNET_STATUS_HANDLE_CLOSING)
2566     {
2567         DWORD type = INTERNET_HANDLE_TYPE_CONNECT_HTTP, size = sizeof(type);
2568
2569         if (InternetQueryOption(handle, INTERNET_OPTION_HANDLE_TYPE, &type, &size))
2570             ok(type != INTERNET_HANDLE_TYPE_CONNECT_HTTP, "unexpected callback\n");
2571         SetEvent(ctx->event);
2572     }
2573 }
2574
2575 static void test_open_url_async(void)
2576 {
2577     BOOL ret;
2578     HINTERNET ses, req;
2579     DWORD size, error;
2580     struct context ctx;
2581     ULONG type;
2582
2583     ctx.req = NULL;
2584     ctx.event = CreateEvent(NULL, TRUE, FALSE, "Z:_home_hans_jaman-installer.exe_ev1");
2585
2586     ses = InternetOpen("AdvancedInstaller", 0, NULL, NULL, INTERNET_FLAG_ASYNC);
2587     ok(ses != NULL, "InternetOpen failed\n");
2588
2589     SetLastError(0xdeadbeef);
2590     ret = InternetSetOptionA(NULL, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2591     error = GetLastError();
2592     ok(!ret, "InternetSetOptionA succeeded\n");
2593     ok(error == ERROR_INTERNET_INCORRECT_HANDLE_TYPE, "got %u expected ERROR_INTERNET_INCORRECT_HANDLE_TYPE\n", error);
2594
2595     ret = InternetSetOptionA(ses, INTERNET_OPTION_CALLBACK, &cb, sizeof(DWORD_PTR));
2596     error = GetLastError();
2597     ok(!ret, "InternetSetOptionA failed\n");
2598     ok(error == ERROR_INTERNET_OPTION_NOT_SETTABLE, "got %u expected ERROR_INTERNET_OPTION_NOT_SETTABLE\n", error);
2599
2600     pInternetSetStatusCallbackA(ses, cb);
2601     ResetEvent(ctx.event);
2602
2603     req = InternetOpenUrl(ses, "http://test.winehq.org", NULL, 0, 0, (DWORD_PTR)&ctx);
2604     ok(!req && GetLastError() == ERROR_IO_PENDING, "InternetOpenUrl failed\n");
2605
2606     WaitForSingleObject(ctx.event, INFINITE);
2607
2608     type = 0;
2609     size = sizeof(type);
2610     ret = InternetQueryOption(ctx.req, INTERNET_OPTION_HANDLE_TYPE, &type, &size);
2611     ok(ret, "InternetQueryOption failed: %u\n", GetLastError());
2612     ok(type == INTERNET_HANDLE_TYPE_HTTP_REQUEST,
2613        "expected INTERNET_HANDLE_TYPE_HTTP_REQUEST, got %u\n", type);
2614
2615     size = 0;
2616     ret = HttpQueryInfo(ctx.req, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &size, NULL);
2617     ok(!ret && GetLastError() == ERROR_INSUFFICIENT_BUFFER, "HttpQueryInfo failed\n");
2618     ok(size > 0, "expected size > 0\n");
2619
2620     ResetEvent(ctx.event);
2621     InternetCloseHandle(ctx.req);
2622     WaitForSingleObject(ctx.event, INFINITE);
2623
2624     InternetCloseHandle(ses);
2625     CloseHandle(ctx.event);
2626 }
2627
2628 enum api
2629 {
2630     internet_connect = 1,
2631     http_open_request,
2632     http_send_request_ex,
2633     internet_writefile,
2634     http_end_request,
2635     internet_close_handle
2636 };
2637
2638 struct notification
2639 {
2640     enum api     function; /* api responsible for notification */
2641     unsigned int status;   /* status received */
2642     int          async;    /* delivered from another thread? */
2643     int          todo;
2644     int          optional;
2645 };
2646
2647 struct info
2648 {
2649     enum api     function;
2650     const struct notification *test;
2651     unsigned int count;
2652     unsigned int index;
2653     HANDLE       wait;
2654     DWORD        thread;
2655     unsigned int line;
2656 };
2657
2658 static CRITICAL_SECTION notification_cs;
2659
2660 static void CALLBACK check_notification( HINTERNET handle, DWORD_PTR context, DWORD status, LPVOID buffer, DWORD buflen )
2661 {
2662     BOOL status_ok, function_ok;
2663     struct info *info = (struct info *)context;
2664     unsigned int i;
2665
2666     EnterCriticalSection( &notification_cs );
2667
2668     if (status == INTERNET_STATUS_HANDLE_CREATED)
2669     {
2670         DWORD size = sizeof(struct info *);
2671         HttpQueryInfoA( handle, INTERNET_OPTION_CONTEXT_VALUE, &info, &size, 0 );
2672     }
2673     i = info->index;
2674     if (i >= info->count)
2675     {
2676         LeaveCriticalSection( &notification_cs );
2677         return;
2678     }
2679
2680     while (info->test[i].status != status && info->test[i].optional &&
2681         i < info->count - 1 &&
2682         info->test[i].function == info->test[i + 1].function)
2683     {
2684         i++;
2685     }
2686
2687     status_ok   = (info->test[i].status == status);
2688     function_ok = (info->test[i].function == info->function);
2689
2690     if (!info->test[i].todo)
2691     {
2692         ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2693         ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2694
2695         if (info->test[i].async)
2696             ok(info->thread != GetCurrentThreadId(), "%u: expected thread %u got %u\n",
2697                info->line, info->thread, GetCurrentThreadId());
2698     }
2699     else
2700     {
2701         todo_wine ok( status_ok, "%u: expected status %u got %u\n", info->line, info->test[i].status, status );
2702         if (status_ok)
2703             todo_wine ok( function_ok, "%u: expected function %u got %u\n", info->line, info->test[i].function, info->function );
2704     }
2705     if (i == info->count - 1 || info->test[i].function != info->test[i + 1].function) SetEvent( info->wait );
2706     info->index = i+1;
2707
2708     LeaveCriticalSection( &notification_cs );
2709 }
2710
2711 static void setup_test( struct info *info, enum api function, unsigned int line )
2712 {
2713     info->function = function;
2714     info->line = line;
2715 }
2716
2717 static const struct notification async_send_request_ex_test[] =
2718 {
2719     { internet_connect,      INTERNET_STATUS_HANDLE_CREATED, 0 },
2720     { http_open_request,     INTERNET_STATUS_HANDLE_CREATED, 0 },
2721     { http_send_request_ex,  INTERNET_STATUS_DETECTING_PROXY, 1, 0, 1 },
2722     { http_send_request_ex,  INTERNET_STATUS_RESOLVING_NAME, 1 },
2723     { http_send_request_ex,  INTERNET_STATUS_NAME_RESOLVED, 1 },
2724     { http_send_request_ex,  INTERNET_STATUS_CONNECTING_TO_SERVER, 1 },
2725     { http_send_request_ex,  INTERNET_STATUS_CONNECTED_TO_SERVER, 1 },
2726     { http_send_request_ex,  INTERNET_STATUS_SENDING_REQUEST, 1 },
2727     { http_send_request_ex,  INTERNET_STATUS_REQUEST_SENT, 1 },
2728     { http_send_request_ex,  INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2729     { internet_writefile,    INTERNET_STATUS_SENDING_REQUEST, 0 },
2730     { internet_writefile,    INTERNET_STATUS_REQUEST_SENT, 0 },
2731     { http_end_request,      INTERNET_STATUS_RECEIVING_RESPONSE, 1 },
2732     { http_end_request,      INTERNET_STATUS_RESPONSE_RECEIVED, 1 },
2733     { http_end_request,      INTERNET_STATUS_REQUEST_COMPLETE, 1 },
2734     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 },
2735     { internet_close_handle, INTERNET_STATUS_HANDLE_CLOSING, 0, 1 }
2736 };
2737
2738 static void test_async_HttpSendRequestEx(void)
2739 {
2740     BOOL ret;
2741     HINTERNET ses, req, con;
2742     struct info info;
2743     DWORD size, written, error;
2744     INTERNET_BUFFERSA b;
2745     static const char *accept[2] = {"*/*", NULL};
2746     static char data[] = "Public ID=codeweavers";
2747     char buffer[32];
2748
2749     InitializeCriticalSection( &notification_cs );
2750
2751     info.test  = async_send_request_ex_test;
2752     info.count = sizeof(async_send_request_ex_test)/sizeof(async_send_request_ex_test[0]);
2753     info.index = 0;
2754     info.wait = CreateEvent( NULL, FALSE, FALSE, NULL );
2755     info.thread = GetCurrentThreadId();
2756
2757     ses = InternetOpen( "winetest", 0, NULL, NULL, INTERNET_FLAG_ASYNC );
2758     ok( ses != NULL, "InternetOpen failed\n" );
2759
2760     pInternetSetStatusCallbackA( ses, check_notification );
2761
2762     setup_test( &info, internet_connect, __LINE__ );
2763     con = InternetConnect( ses, "crossover.codeweavers.com", 80, NULL, NULL, INTERNET_SERVICE_HTTP, 0, (DWORD_PTR)&info );
2764     ok( con != NULL, "InternetConnect failed %u\n", GetLastError() );
2765
2766     WaitForSingleObject( info.wait, 10000 );
2767
2768     setup_test( &info, http_open_request, __LINE__ );
2769     req = HttpOpenRequest( con, "POST", "posttest.php", NULL, NULL, accept, 0, (DWORD_PTR)&info );
2770     ok( req != NULL, "HttpOpenRequest failed %u\n", GetLastError() );
2771
2772     WaitForSingleObject( info.wait, 10000 );
2773
2774     memset( &b, 0, sizeof(INTERNET_BUFFERSA) );
2775     b.dwStructSize = sizeof(INTERNET_BUFFERSA);
2776     b.lpcszHeader = "Content-Type: application/x-www-form-urlencoded";
2777     b.dwHeadersLength = strlen( b.lpcszHeader );
2778     b.dwBufferTotal = strlen( data );
2779
2780     setup_test( &info, http_send_request_ex, __LINE__ );
2781     ret = HttpSendRequestExA( req, &b, NULL, 0x28, 0 );
2782     ok( !ret && GetLastError() == ERROR_IO_PENDING, "HttpSendRequestExA failed %d %u\n", ret, GetLastError() );
2783
2784     WaitForSingleObject( info.wait, 10000 );
2785
2786     size = sizeof(buffer);
2787     SetLastError( 0xdeadbeef );
2788     ret = HttpQueryInfoA( req, HTTP_QUERY_CONTENT_ENCODING, buffer, &size, 0 );
2789     error = GetLastError();
2790     ok( !ret, "HttpQueryInfoA failed %u\n", GetLastError() );
2791     todo_wine
2792     ok( error == ERROR_INTERNET_INCORRECT_HANDLE_STATE,
2793         "expected ERROR_INTERNET_INCORRECT_HANDLE_STATE got %u\n", error );
2794
2795     written = 0;
2796     size = strlen( data );
2797     setup_test( &info, internet_writefile, __LINE__ );
2798     ret = InternetWriteFile( req, data, size, &written );
2799     ok( ret, "InternetWriteFile failed %u\n", GetLastError() );
2800     ok( written == size, "expected %u got %u\n", written, size );
2801
2802     WaitForSingleObject( info.wait, 10000 );
2803
2804     SetLastError( 0xdeadbeef );
2805     ret = HttpEndRequestA( req, (void *)data, 0x28, 0 );
2806     error = GetLastError();
2807     ok( !ret, "HttpEndRequestA succeeded\n" );
2808     ok( error == ERROR_INVALID_PARAMETER, "expected ERROR_INVALID_PARAMETER got %u\n", error );
2809
2810     SetLastError( 0xdeadbeef );
2811     setup_test( &info, http_end_request, __LINE__ );
2812     ret = HttpEndRequestA( req, NULL, 0x28, 0 );
2813     error = GetLastError();
2814     ok( !ret, "HttpEndRequestA succeeded\n" );
2815     ok( error == ERROR_IO_PENDING, "expected ERROR_IO_PENDING got %u\n", error );
2816
2817     WaitForSingleObject( info.wait, 10000 );
2818
2819     setup_test( &info, internet_close_handle, __LINE__ );
2820     InternetCloseHandle( req );
2821     InternetCloseHandle( con );
2822     InternetCloseHandle( ses );
2823
2824     WaitForSingleObject( info.wait, 10000 );
2825     Sleep(100);
2826     CloseHandle( info.wait );
2827 }
2828
2829 #define STATUS_STRING(status) \
2830     memcpy(status_string[status], #status, sizeof(CHAR) * \
2831            (strlen(#status) < MAX_STATUS_NAME ? \
2832             strlen(#status) : \
2833             MAX_STATUS_NAME - 1))
2834 static void init_status_tests(void)
2835 {
2836     memset(expect, 0, sizeof(expect));
2837     memset(optional, 0, sizeof(optional));
2838     memset(wine_allow, 0, sizeof(wine_allow));
2839     memset(notified, 0, sizeof(notified));
2840     memset(status_string, 0, sizeof(status_string));
2841     STATUS_STRING(INTERNET_STATUS_RESOLVING_NAME);
2842     STATUS_STRING(INTERNET_STATUS_NAME_RESOLVED);
2843     STATUS_STRING(INTERNET_STATUS_CONNECTING_TO_SERVER);
2844     STATUS_STRING(INTERNET_STATUS_CONNECTED_TO_SERVER);
2845     STATUS_STRING(INTERNET_STATUS_SENDING_REQUEST);
2846     STATUS_STRING(INTERNET_STATUS_REQUEST_SENT);
2847     STATUS_STRING(INTERNET_STATUS_RECEIVING_RESPONSE);
2848     STATUS_STRING(INTERNET_STATUS_RESPONSE_RECEIVED);
2849     STATUS_STRING(INTERNET_STATUS_CTL_RESPONSE_RECEIVED);
2850     STATUS_STRING(INTERNET_STATUS_PREFETCH);
2851     STATUS_STRING(INTERNET_STATUS_CLOSING_CONNECTION);
2852     STATUS_STRING(INTERNET_STATUS_CONNECTION_CLOSED);
2853     STATUS_STRING(INTERNET_STATUS_HANDLE_CREATED);
2854     STATUS_STRING(INTERNET_STATUS_HANDLE_CLOSING);
2855     STATUS_STRING(INTERNET_STATUS_DETECTING_PROXY);
2856     STATUS_STRING(INTERNET_STATUS_REQUEST_COMPLETE);
2857     STATUS_STRING(INTERNET_STATUS_REDIRECT);
2858     STATUS_STRING(INTERNET_STATUS_INTERMEDIATE_RESPONSE);
2859     STATUS_STRING(INTERNET_STATUS_USER_INPUT_REQUIRED);
2860     STATUS_STRING(INTERNET_STATUS_STATE_CHANGE);
2861     STATUS_STRING(INTERNET_STATUS_COOKIE_SENT);
2862     STATUS_STRING(INTERNET_STATUS_COOKIE_RECEIVED);
2863     STATUS_STRING(INTERNET_STATUS_PRIVACY_IMPACTED);
2864     STATUS_STRING(INTERNET_STATUS_P3P_HEADER);
2865     STATUS_STRING(INTERNET_STATUS_P3P_POLICYREF);
2866     STATUS_STRING(INTERNET_STATUS_COOKIE_HISTORY);
2867 }
2868 #undef STATUS_STRING
2869
2870 START_TEST(http)
2871 {
2872     HMODULE hdll;
2873     hdll = GetModuleHandleA("wininet.dll");
2874     pInternetSetStatusCallbackA = (void*)GetProcAddress(hdll, "InternetSetStatusCallbackA");
2875
2876     if (!pInternetSetStatusCallbackA)
2877         skip("skipping the InternetReadFile tests\n");
2878     else
2879     {
2880         init_status_tests();
2881         InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[0]);
2882         InternetReadFile_test(0, &test_data[0]);
2883         first_connection_to_test_url = TRUE;
2884         InternetReadFile_test(INTERNET_FLAG_ASYNC, &test_data[1]);
2885         InternetReadFile_test(0, &test_data[1]);
2886         InternetReadFileExA_test(INTERNET_FLAG_ASYNC);
2887         test_open_url_async();
2888         test_async_HttpSendRequestEx();
2889     }
2890     InternetOpenRequest_test();
2891     test_http_cache();
2892     InternetOpenUrlA_test();
2893     HttpHeaders_test();
2894     test_http_connection();
2895     test_user_agent_header();
2896     test_bogus_accept_types_array();
2897     InternetReadFile_chunked_test();
2898     HttpSendRequestEx_test();
2899 }