ReactOS  0.4.14-dev-583-g2a1ba2c
http.c
Go to the documentation of this file.
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 #include <limits.h>
27 
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wininet.h"
31 #include "winineti.h"
32 #include "winsock2.h"
33 
34 #include "wine/test.h"
35 
36 /* Undocumented security flags */
37 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000
38 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000
39 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000
40 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000
41 
42 #define TEST_URL "http://test.winehq.org/tests/hello.html"
43 
45 
46 /* Adapted from dlls/urlmon/tests/protocol.c */
47 
48 #define SET_EXPECT2(status, num) \
49  expect[status] = num
50 
51 #define SET_EXPECT(status) \
52  SET_EXPECT2(status, 1)
53 
54 #define SET_OPTIONAL2(status, num) \
55  optional[status] = num
56 
57 #define SET_OPTIONAL(status) \
58  SET_OPTIONAL2(status, 1)
59 
60 /* SET_WINE_ALLOW's should be used with an appropriate
61  * todo_wine CHECK_NOTIFIED at a later point in the code */
62 #define SET_WINE_ALLOW2(status, num) \
63  wine_allow[status] = num
64 
65 #define SET_WINE_ALLOW(status) \
66  SET_WINE_ALLOW2(status, 1)
67 
68 #define CHECK_EXPECT(status) \
69  do { \
70  if (!expect[status] && !optional[status] && wine_allow[status]) \
71  { \
72  todo_wine ok(expect[status], "unexpected status %d (%s)\n", status, \
73  status < MAX_INTERNET_STATUS && status_string[status] ? \
74  status_string[status] : "unknown"); \
75  wine_allow[status]--; \
76  } \
77  else \
78  { \
79  ok(expect[status] || optional[status], "unexpected status %d (%s)\n", status, \
80  status < MAX_INTERNET_STATUS && status_string[status] ? \
81  status_string[status] : "unknown"); \
82  if (expect[status]) expect[status]--; \
83  else if(optional[status]) optional[status]--; \
84  } \
85  notified[status]++; \
86  }while(0)
87 
88 /* CLEAR_NOTIFIED used in cases when notification behavior
89  * differs between Windows versions */
90 #define CLEAR_NOTIFIED(status) \
91  expect[status] = optional[status] = wine_allow[status] = notified[status] = 0;
92 
93 #define CHECK_NOTIFIED2(status, num) \
94  do { \
95  ok(notified[status] + optional[status] == (num), \
96  "expected status %d (%s) %d times, received %d times\n", \
97  status, status < MAX_INTERNET_STATUS && status_string[status] ? \
98  status_string[status] : "unknown", (num), notified[status]); \
99  CLEAR_NOTIFIED(status); \
100  }while(0)
101 
102 #define CHECK_NOTIFIED(status) \
103  CHECK_NOTIFIED2(status, 1)
104 
105 #define CHECK_NOT_NOTIFIED(status) \
106  CHECK_NOTIFIED2(status, 0)
107 
108 #define MAX_INTERNET_STATUS (INTERNET_STATUS_COOKIE_HISTORY+1)
111 static const char *status_string[MAX_INTERNET_STATUS];
112 
116 
117 #define TESTF_REDIRECT 0x01
118 #define TESTF_COMPRESSED 0x02
119 #define TESTF_CHUNKED 0x04
120 
121 typedef struct {
122  const char *url;
123  const char *redirected_url;
124  const char *host;
125  const char *path;
126  const char *headers;
128  const char *post_data;
129  const char *content;
130 } test_data_t;
131 
132 static const test_data_t test_data[] = {
133  {
134  "http://test.winehq.org/tests/data.php",
135  "http://test.winehq.org/tests/data.php",
136  "test.winehq.org",
137  "/tests/data.php",
138  "",
140  },
141  {
142  "http://test.winehq.org/tests/redirect",
143  "http://test.winehq.org/tests/hello.html",
144  "test.winehq.org",
145  "/tests/redirect",
146  "",
148  },
149  {
150  "http://test.winehq.org/tests/gzip.php",
151  "http://test.winehq.org/tests/gzip.php",
152  "test.winehq.org",
153  "/tests/gzip.php",
154  "Accept-Encoding: gzip, deflate",
156  },
157  {
158  "http://test.winehq.org/tests/post.php",
159  "http://test.winehq.org/tests/post.php",
160  "test.winehq.org",
161  "/tests/post.php",
162  "Content-Type: application/x-www-form-urlencoded",
163  0,
164  "mode=Test",
165  "mode => Test\n"
166  }
167 };
168 
169 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackA)(HINTERNET ,INTERNET_STATUS_CALLBACK);
170 static INTERNET_STATUS_CALLBACK (WINAPI *pInternetSetStatusCallbackW)(HINTERNET ,INTERNET_STATUS_CALLBACK);
171 static BOOL (WINAPI *pInternetGetSecurityInfoByURLA)(LPSTR,PCCERT_CHAIN_CONTEXT*,DWORD*);
172 
173 static int strcmp_wa(LPCWSTR strw, const char *stra)
174 {
175  WCHAR buf[512];
176  MultiByteToWideChar(CP_ACP, 0, stra, -1, buf, ARRAY_SIZE(buf));
177  return lstrcmpW(strw, buf);
178 }
179 
180 static BOOL proxy_active(void)
181 {
184  DWORD size;
185 
186  if (RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
188  return FALSE;
189 
190  size = sizeof(DWORD);
192  proxy_enable = 0;
193 
195 
196  return proxy_enable != 0;
197 }
198 
199 static void init_events(void)
200 {
206 }
207 
208 static void free_events(void)
209 {
215 }
216 
217 static void reset_events(void)
218 {
224 }
225 
226 #define test_status_code(a,b) _test_status_code(__LINE__,a,b, FALSE)
227 #define test_status_code_todo(a,b) _test_status_code(__LINE__,a,b, TRUE)
228 static void _test_status_code(unsigned line, HINTERNET req, DWORD excode, BOOL is_todo)
229 {
230  DWORD code, size, index;
231  char exbuf[12], bufa[10];
232  WCHAR bufw[10];
233  BOOL res;
234 
235  code = 0xdeadbeef;
236  size = sizeof(code);
238  ok_(__FILE__,line)(res, "[1] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number) failed: %u\n", GetLastError());
239  todo_wine_if (is_todo)
240  ok_(__FILE__,line)(code == excode, "code = %d, expected %d\n", code, excode);
241  ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
242 
243  code = 0xdeadbeef;
244  index = 0;
245  size = sizeof(code);
247  ok_(__FILE__,line)(res, "[2] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE|number index) failed: %u\n", GetLastError());
248  ok_(__FILE__,line)(!index, "index = %d, expected 0\n", index);
249  ok_(__FILE__,line)(size == sizeof(code), "size = %u\n", size);
250 
251  sprintf(exbuf, "%u", excode);
252 
253  size = sizeof(bufa);
255  ok_(__FILE__,line)(res, "[3] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
256  todo_wine_if (is_todo)
257  ok_(__FILE__,line)(!strcmp(bufa, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
258  ok_(__FILE__,line)(size == strlen(exbuf), "unexpected size %d for \"%s\"\n", size, exbuf);
259 
260  size = 0;
263  "[4] HttpQueryInfoA(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
264  ok_(__FILE__,line)(size == strlen(exbuf)+1, "unexpected size %d for \"%s\"\n", size, exbuf);
265 
266  size = sizeof(bufw);
268  ok_(__FILE__,line)(res, "[5] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
269  todo_wine_if (is_todo)
270  ok_(__FILE__,line)(!strcmp_wa(bufw, exbuf), "unexpected status code %s, expected %s\n", bufa, exbuf);
271  ok_(__FILE__,line)(size == strlen(exbuf)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
272 
273  size = 0;
276  "[6] HttpQueryInfoW(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
277  ok_(__FILE__,line)(size == (strlen(exbuf)+1)*sizeof(WCHAR), "unexpected size %d for \"%s\"\n", size, exbuf);
278 
279  if(0) {
280  size = sizeof(bufw);
282  ok(!res && GetLastError() == ERROR_INVALID_PARAMETER, "HttpQueryInfo(HTTP_QUERY_STATUS_CODE) failed: %u\n", GetLastError());
283  ok(size == sizeof(bufw), "unexpected size %d\n", size);
284  }
285 
286  code = 0xdeadbeef;
287  index = 1;
288  size = sizeof(code);
291  "[7] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
292 
293  code = 0xdeadbeef;
294  size = sizeof(code);
297  "[8] HttpQueryInfoA failed: %x(%d)\n", res, GetLastError());
298 }
299 
300 #define test_request_flags(a,b) _test_request_flags(__LINE__,a,b,FALSE)
301 #define test_request_flags_todo(a,b) _test_request_flags(__LINE__,a,b,TRUE)
302 static void _test_request_flags(unsigned line, HINTERNET req, DWORD exflags, BOOL is_todo)
303 {
304  DWORD flags, size;
305  BOOL res;
306 
307  flags = 0xdeadbeef;
308  size = sizeof(flags);
310  ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_REQUEST_FLAGS) failed: %u\n", GetLastError());
311 
312  /* FIXME: Remove once we have INTERNET_REQFLAG_CACHE_WRITE_DISABLED implementation */
314  todo_wine_if (is_todo)
315  ok_(__FILE__,line)(flags == exflags, "flags = %x, expected %x\n", flags, exflags);
316 }
317 
318 #define test_request_url(a,b) _test_request_url(__LINE__,a,b)
319 static void _test_request_url(unsigned line, HINTERNET req, const char *expected_url)
320 {
322  DWORD size = sizeof(buf);
323  BOOL res;
324 
326  ok_(__FILE__,line)(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
327  ok_(__FILE__,line)(size == strlen(expected_url), "size = %u\n", size);
328  ok_(__FILE__,line)(!strcmp(buf, expected_url), "unexpected URL %s, expected %s\n", buf, expected_url);
329 }
330 
331 #define test_http_version(a) _test_http_version(__LINE__,a)
332 static void _test_http_version(unsigned line, HINTERNET req)
333 {
334  HTTP_VERSION_INFO v = {0xdeadbeef, 0xdeadbeef};
335  DWORD size;
336  BOOL res;
337 
338  size = sizeof(v);
340  ok_(__FILE__,line)(res, "InternetQueryOptionW(INTERNET_OPTION_HTTP_VERSION) failed: %u\n", GetLastError());
341  ok_(__FILE__,line)(v.dwMajorVersion == 1, "dwMajorVersion = %d\n", v.dwMajorVersion);
342  ok_(__FILE__,line)(v.dwMinorVersion == 1, "dwMinorVersion = %d\n", v.dwMinorVersion);
343 }
344 
345 static int close_handle_cnt;
346 
348  HINTERNET hInternet,
349  DWORD_PTR dwContext,
350  DWORD dwInternetStatus,
351  LPVOID lpvStatusInformation,
352  DWORD dwStatusInformationLength
353 )
354 {
356  CHECK_EXPECT(dwInternetStatus);
357  switch (dwInternetStatus)
358  {
360  if(winetest_debug > 1)
361  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESOLVING_NAME \"%s\" %d\n",
362  GetCurrentThreadId(), hInternet, dwContext,
363  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
364  *(LPSTR)lpvStatusInformation = '\0';
365  break;
367  if(winetest_debug > 1)
368  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_NAME_RESOLVED \"%s\" %d\n",
369  GetCurrentThreadId(), hInternet, dwContext,
370  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
371  *(LPSTR)lpvStatusInformation = '\0';
372  break;
374  if(winetest_debug > 1)
375  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTING_TO_SERVER \"%s\" %d\n",
376  GetCurrentThreadId(), hInternet, dwContext,
377  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
378  ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
379  dwStatusInformationLength);
380  *(LPSTR)lpvStatusInformation = '\0';
381  break;
383  if(winetest_debug > 1)
384  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTED_TO_SERVER \"%s\" %d\n",
385  GetCurrentThreadId(), hInternet, dwContext,
386  (LPCSTR)lpvStatusInformation,dwStatusInformationLength);
387  ok(dwStatusInformationLength == strlen(lpvStatusInformation)+1, "unexpected size %u\n",
388  dwStatusInformationLength);
389  *(LPSTR)lpvStatusInformation = '\0';
390  break;
392  if(winetest_debug > 1)
393  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_SENDING_REQUEST %p %d\n",
394  GetCurrentThreadId(), hInternet, dwContext,
395  lpvStatusInformation,dwStatusInformationLength);
396  break;
398  ok(dwStatusInformationLength == sizeof(DWORD),
399  "info length should be sizeof(DWORD) instead of %d\n",
400  dwStatusInformationLength);
401  if(winetest_debug > 1)
402  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_SENT 0x%x %d\n",
403  GetCurrentThreadId(), hInternet, dwContext,
404  *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
405  break;
407  if(winetest_debug > 1)
408  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RECEIVING_RESPONSE %p %d\n",
409  GetCurrentThreadId(), hInternet, dwContext,
410  lpvStatusInformation,dwStatusInformationLength);
411  break;
413  ok(dwStatusInformationLength == sizeof(DWORD),
414  "info length should be sizeof(DWORD) instead of %d\n",
415  dwStatusInformationLength);
416  if(winetest_debug > 1)
417  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_RESPONSE_RECEIVED 0x%x %d\n",
418  GetCurrentThreadId(), hInternet, dwContext,
419  *(DWORD *)lpvStatusInformation,dwStatusInformationLength);
420  break;
422  if(winetest_debug > 1)
423  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CTL_RESPONSE_RECEIVED %p %d\n",
424  GetCurrentThreadId(), hInternet,dwContext,
425  lpvStatusInformation,dwStatusInformationLength);
426  break;
428  if(winetest_debug > 1)
429  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_PREFETCH %p %d\n",
430  GetCurrentThreadId(), hInternet, dwContext,
431  lpvStatusInformation,dwStatusInformationLength);
432  break;
434  if(winetest_debug > 1)
435  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CLOSING_CONNECTION %p %d\n",
436  GetCurrentThreadId(), hInternet, dwContext,
437  lpvStatusInformation,dwStatusInformationLength);
438  break;
440  if(winetest_debug > 1)
441  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_CONNECTION_CLOSED %p %d\n",
442  GetCurrentThreadId(), hInternet, dwContext,
443  lpvStatusInformation,dwStatusInformationLength);
444  break;
446  ok(dwStatusInformationLength == sizeof(HINTERNET),
447  "info length should be sizeof(HINTERNET) instead of %d\n",
448  dwStatusInformationLength);
449  if(winetest_debug > 1)
450  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CREATED %p %d\n",
451  GetCurrentThreadId(), hInternet, dwContext,
452  *(HINTERNET *)lpvStatusInformation,dwStatusInformationLength);
455  break;
457  ok(dwStatusInformationLength == sizeof(HINTERNET),
458  "info length should be sizeof(HINTERNET) instead of %d\n",
459  dwStatusInformationLength);
460  if(winetest_debug > 1)
461  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_HANDLE_CLOSING %p %d\n",
462  GetCurrentThreadId(), hInternet, dwContext,
463  *(HINTERNET *)lpvStatusInformation, dwStatusInformationLength);
466  break;
468  {
469  INTERNET_ASYNC_RESULT *iar = (INTERNET_ASYNC_RESULT *)lpvStatusInformation;
470  ok(dwStatusInformationLength == sizeof(INTERNET_ASYNC_RESULT),
471  "info length should be sizeof(INTERNET_ASYNC_RESULT) instead of %d\n",
472  dwStatusInformationLength);
473  ok(iar->dwResult == 1 || iar->dwResult == 0, "iar->dwResult = %ld\n", iar->dwResult);
474  if(winetest_debug > 1)
475  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REQUEST_COMPLETE {%ld,%d} %d\n",
476  GetCurrentThreadId(), hInternet, dwContext,
477  iar->dwResult,iar->dwError,dwStatusInformationLength);
478  req_error = iar->dwError;
479  if(!close_handle_cnt)
481  break;
482  }
484  if(winetest_debug > 1)
485  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_REDIRECT \"%s\" %d\n",
486  GetCurrentThreadId(), hInternet, dwContext,
487  (LPCSTR)lpvStatusInformation, dwStatusInformationLength);
488  *(LPSTR)lpvStatusInformation = '\0';
491  break;
493  if(winetest_debug > 1)
494  trace("%04x:Callback %p 0x%lx INTERNET_STATUS_INTERMEDIATE_RESPONSE %p %d\n",
495  GetCurrentThreadId(), hInternet, dwContext,
496  lpvStatusInformation, dwStatusInformationLength);
497  break;
498  default:
499  if(winetest_debug > 1)
500  trace("%04x:Callback %p 0x%lx %d %p %d\n",
501  GetCurrentThreadId(), hInternet, dwContext, dwInternetStatus,
502  lpvStatusInformation, dwStatusInformationLength);
503  }
504 }
505 
506 typedef struct {
511 
512 #define open_simple_request(a,b,c,d,e) _open_simple_request(__LINE__,a,b,c,d,e)
513 static void _open_simple_request(unsigned line, test_request_t *req, const char *host,
514  int port, const char *verb, const char *url)
515 {
517  ok_(__FILE__,line)(req->session != NULL, "InternetOpenA failed: %u\n", GetLastError());
518 
520  ok_(__FILE__,line)(req->connection != NULL, "InternetConnectA failed: %u\n", GetLastError());
521 
522  req->request = HttpOpenRequestA(req->connection, verb, url, NULL, NULL, NULL, 0, 0);
523  ok_(__FILE__,line)(req->request != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
524 }
525 
526 #define close_request(a) _close_request(__LINE__,a)
527 static void _close_request(unsigned line, test_request_t *req)
528 {
529  BOOL ret;
530 
532  ok_(__FILE__,line)(ret, "InternetCloseHandle(request) failed: %u\n", GetLastError());
534  ok_(__FILE__,line)(ret, "InternetCloseHandle(connection) failed: %u\n", GetLastError());
536  ok_(__FILE__,line)(ret, "InternetCloseHandle(session) failed: %u\n", GetLastError());
537 }
538 
539 #define receive_simple_request(a,b,c) _receive_simple_request(__LINE__,a,b,c)
540 static DWORD _receive_simple_request(unsigned line, HINTERNET req, char *buf, size_t buf_size)
541 {
542  DWORD read = 0;
543  BOOL ret;
544 
545  ret = InternetReadFile(req, buf, buf_size, &read);
546  ok_(__FILE__,line)(ret, "InternetReadFile failed: %u\n", GetLastError());
547 
548  return read;
549 }
550 
551 static void close_async_handle(HINTERNET handle, int handle_cnt)
552 {
553  BOOL res;
554 
555  close_handle_cnt = handle_cnt;
556 
559  ok(res, "InternetCloseHandle failed: %u\n", GetLastError());
562 }
563 
564 static void InternetReadFile_test(int flags, const test_data_t *test)
565 {
566  char *post_data = NULL;
567  BOOL res, on_async = TRUE;
568  CHAR buffer[4000];
569  WCHAR wbuffer[4000];
570  DWORD length, length2, index, exlen = 0, post_len = 0;
571  const char *types[2] = { "*", NULL };
572  HINTERNET hi, hic = 0, hor = 0;
573 
574  trace("Starting InternetReadFile test with flags 0x%x on url %s\n",flags,test->url);
575  reset_events();
576 
577  trace("InternetOpenA <--\n");
578  hi = InternetOpenA((test->flags & TESTF_COMPRESSED) ? "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" : "",
580  ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
581  trace("InternetOpenA -->\n");
582 
583  if (hi == 0x0) goto abort;
584 
585  pInternetSetStatusCallbackA(hi,&callback);
586 
588 
589  trace("InternetConnectA <--\n");
591  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
592  ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
593  trace("InternetConnectA -->\n");
594 
595  if (hic == 0x0) goto abort;
596 
599 
600  trace("HttpOpenRequestA <--\n");
601  hor = HttpOpenRequestA(hic, test->post_data ? "POST" : "GET", test->path, NULL, NULL, types,
603  0xdeadbead);
604  if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
605  /*
606  * If the internet name can't be resolved we are probably behind
607  * a firewall or in some other way not directly connected to the
608  * Internet. Not enough reason to fail the test. Just ignore and
609  * abort.
610  */
611  } else {
612  ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
613  }
614  trace("HttpOpenRequestA -->\n");
615 
616  if (hor == 0x0) goto abort;
617 
619  test_request_url(hor, test->url);
620 
621  length = sizeof(buffer);
623  ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
624  ok(length == 0 || (length == 1 && !*buffer) /* win10 */, "HTTP_QUERY_RAW_HEADERS: expected length 0, but got %d\n", length);
625  ok(!strcmp(buffer, ""), "HTTP_QUERY_RAW_HEADERS: expected string \"\", but got \"%s\"\n", buffer);
626 
633  {
636  }
643  if(test->flags & TESTF_REDIRECT) {
646  }
652 
653  if(test->flags & TESTF_COMPRESSED) {
654  BOOL b = TRUE;
655 
658  "InternetSetOption failed: %u\n", GetLastError());
659  if(!res)
660  goto abort;
661  }
662 
663  test_status_code(hor, 0);
664 
665  trace("HttpSendRequestA -->\n");
666  if(test->post_data) {
667  post_len = strlen(test->post_data);
668  post_data = HeapAlloc(GetProcessHeap(), 0, post_len);
669  memcpy(post_data, test->post_data, post_len);
670  }
671  SetLastError(0xdeadbeef);
672  res = HttpSendRequestA(hor, test->headers, -1, post_data, post_len);
674  ok(!res && (GetLastError() == ERROR_IO_PENDING),
675  "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
676  else
678  "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
679  trace("HttpSendRequestA <--\n");
680 
681  if (flags & INTERNET_FLAG_ASYNC) {
683  ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
684  }
686 
690  {
691  if (! proxy_active())
692  {
695  }
696  else
697  {
700  }
701  }
702  else
703  {
706  }
711  if(test->flags & TESTF_REDIRECT)
715  /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
718 
719  test_request_flags(hor, 0);
720 
721  length = 100;
723  ok(res, "InternetQueryOptionA(INTERNET_OPTION_URL) failed with error %d\n", GetLastError());
724 
725  length = sizeof(buffer)-2;
726  memset(buffer, 0x77, sizeof(buffer));
727  SetLastError(0xdeadbeef);
729  ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
730  ok(GetLastError() == 0 ||
731  broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
732  /* show that the function writes data past the length returned */
733  ok(buffer[length-2], "Expected any header character, got 0x00\n");
734  ok(!buffer[length-1], "Expected 0x00, got %02X\n", buffer[length-1]);
735  ok(!buffer[length], "Expected 0x00, got %02X\n", buffer[length]);
736  ok(buffer[length+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length+1]);
737 
738  length2 = length;
740  ok(!res, "Expected 0x00, got %d\n", res);
741  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
742  ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
743  /* the in length of the buffer must be +1 but the length returned does not count this */
744  length2 = length+1;
745  memset(buffer, 0x77, sizeof(buffer));
747  ok(res, "HttpQueryInfoA(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
748  ok(buffer[length2] == 0x00, "Expected 0x00, got %02X\n", buffer[length2]);
749  ok(buffer[length2+1] == 0x77, "Expected 0x77, got %02X\n", buffer[length2+1]);
750  ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
751 
752  length = sizeof(wbuffer)-2*sizeof(WCHAR);
753  memset(wbuffer, 0x77, sizeof(wbuffer));
754  res = HttpQueryInfoW(hor, HTTP_QUERY_RAW_HEADERS, wbuffer, &length, 0x0);
755  ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
756  ok(length % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length);
757  length /= sizeof(WCHAR);
758  /* show that the function writes data past the length returned */
759  ok(wbuffer[length-2], "Expected any header character, got 0x0000\n");
760  ok(!wbuffer[length-1], "Expected 0x0000, got %04X\n", wbuffer[length-1]);
761  ok(!wbuffer[length], "Expected 0x0000, got %04X\n", wbuffer[length]);
762  ok(wbuffer[length+1] == 0x7777 || broken(wbuffer[length+1] != 0x7777),
763  "Expected 0x7777, got %04X\n", wbuffer[length+1]);
764 
765  length2 = length*sizeof(WCHAR);
766  res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
767  ok(!res, "Expected 0x00, got %d\n", res);
768  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
769  ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
770  length2 /= sizeof(WCHAR);
771  ok(length2 == length+1, "Expected %d, got %d\n", length+1, length2);
772  /* the in length of the buffer must be +1 but the length returned does not count this */
773  length2 = (length+1)*sizeof(WCHAR);
774  memset(wbuffer, 0x77, sizeof(wbuffer));
775  res = HttpQueryInfoW(hor,HTTP_QUERY_RAW_HEADERS,wbuffer,&length2,0x0);
776  ok(res, "HttpQueryInfoW(HTTP_QUERY_RAW_HEADERS) failed with error %d\n", GetLastError());
777  ok(length2 % sizeof(WCHAR) == 0, "Expected that length is a multiple of sizeof(WCHAR), got %d.\n", length2);
778  length2 /= sizeof(WCHAR);
779  ok(!wbuffer[length2], "Expected 0x0000, got %04X\n", wbuffer[length2]);
780  ok(wbuffer[length2+1] == 0x7777, "Expected 0x7777, got %04X\n", wbuffer[length2+1]);
781  ok(length2 == length, "Value should not have changed: %d != %d\n", length2, length);
782 
783  test_request_url(hor, test->redirected_url);
784 
785  index = 0;
786  length = 0;
787  SetLastError(0xdeadbeef);
788  ok(HttpQueryInfoA(hor,HTTP_QUERY_CONTENT_LENGTH,NULL,&length,&index) == FALSE,"Query worked\n");
789  if(test->flags & TESTF_COMPRESSED)
791  "expected ERROR_HTTP_HEADER_NOT_FOUND, got %u\n", GetLastError());
792  ok(index == 0, "Index was incremented\n");
793 
794  index = 0;
795  length = 16;
797  trace("Option HTTP_QUERY_CONTENT_LENGTH -> %i %s (%u)\n",res,buffer,GetLastError());
798  if(test->flags & TESTF_COMPRESSED)
800  "expected ERROR_HTTP_HEADER_NOT_FOUND, got %x (%u)\n", res, GetLastError());
801  ok(!res || index == 1, "Index was not incremented although result is %x (index = %u)\n", res, index);
802 
803  length = 100;
805  buffer[length]=0;
806  trace("Option HTTP_QUERY_CONTENT_TYPE -> %i %s\n",res,buffer);
807 
808  length = 100;
810  buffer[length]=0;
811  trace("Option HTTP_QUERY_CONTENT_ENCODING -> %i %s\n",res,buffer);
812 
813  SetLastError(0xdeadbeef);
815  ok(!res, "InternetReadFile should have failed\n");
817  "InternetReadFile should have set last error to ERROR_INVALID_HANDLE instead of %u\n",
818  GetLastError());
819 
820  length = 100;
821  if(winetest_debug > 1)
822  trace("Entering Query loop\n");
823 
824  while (TRUE)
825  {
828 
829  /* IE11 calls those in InternetQueryDataAvailable call. */
832 
833  length = 0;
835 
837 
839  {
840  if (res)
841  {
843  if(exlen) {
844  ok(length >= exlen, "length %u < exlen %u\n", length, exlen);
845  exlen = 0;
846  }
847  }
848  else if (GetLastError() == ERROR_IO_PENDING)
849  {
850  if(winetest_debug > 1)
851  trace("pending\n");
852  /* on some tests, InternetQueryDataAvailable returns non-zero length and ERROR_IO_PENDING */
853  if(!(test->flags & TESTF_CHUNKED))
854  ok(!length, "InternetQueryDataAvailable returned ERROR_IO_PENDING and %u length\n", length);
856  exlen = length;
857  ok(exlen, "length = 0\n");
860  ok(req_error, "req_error = 0\n");
861  continue;
862  }else {
863  ok(0, "InternetQueryDataAvailable failed: %u\n", GetLastError());
864  }
865  }else {
866  ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
867  }
869 
870  if(winetest_debug > 1)
871  trace("length %u\n", length);
872  if(test->flags & TESTF_CHUNKED)
873  ok(length <= 8192, "length = %d, expected <= 8192\n", length);
874  if (length)
875  {
876  char *buffer;
878 
880 
881  buffer[length]=0;
882 
883  if(winetest_debug > 1)
884  trace("ReadFile -> %s %i\n", res ? "TRUE" : "FALSE", length);
885 
886  if(test->content)
887  ok(!strcmp(buffer, test->content), "buffer = '%s', expected '%s'\n", buffer, test->content);
889  }else {
890  ok(!on_async, "Returned zero size in response to request complete\n");
891  break;
892  }
893  on_async = FALSE;
894  }
895  if(test->flags & TESTF_REDIRECT) {
898  }
899 abort:
900  if(winetest_debug > 1)
901  trace("aborting\n");
902  close_async_handle(hi, 2);
904 }
905 
907 {
908  BOOL res;
909  CHAR buffer[4000];
910  DWORD length, got;
911  const char *types[2] = { "*", NULL };
912  HINTERNET hi, hic = 0, hor = 0;
913 
914  trace("Starting InternetReadFile chunked test\n");
915 
916  trace("InternetOpenA <--\n");
918  ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
919  trace("InternetOpenA -->\n");
920 
921  if (hi == 0x0) goto abort;
922 
923  trace("InternetConnectA <--\n");
924  hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
925  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
926  ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
927  trace("InternetConnectA -->\n");
928 
929  if (hic == 0x0) goto abort;
930 
931  trace("HttpOpenRequestA <--\n");
932  hor = HttpOpenRequestA(hic, "GET", "/tests/chunked", NULL, NULL, types,
934  0xdeadbead);
935  if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
936  /*
937  * If the internet name can't be resolved we are probably behind
938  * a firewall or in some other way not directly connected to the
939  * Internet. Not enough reason to fail the test. Just ignore and
940  * abort.
941  */
942  } else {
943  ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
944  }
945  trace("HttpOpenRequestA -->\n");
946 
947  if (hor == 0x0) goto abort;
948 
949  trace("HttpSendRequestA -->\n");
950  SetLastError(0xdeadbeef);
951  res = HttpSendRequestA(hor, "", -1, NULL, 0);
953  "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
954  trace("HttpSendRequestA <--\n");
955 
956  test_request_flags(hor, 0);
957 
958  length = 100;
960  buffer[length]=0;
961  trace("Option CONTENT_TYPE -> %i %s\n",res,buffer);
962 
963  SetLastError( 0xdeadbeef );
964  length = 100;
966  buffer[length]=0;
967  trace("Option TRANSFER_ENCODING -> %i %s\n",res,buffer);
969  "Failed to get TRANSFER_ENCODING option, error %u\n", GetLastError() );
970  ok( !strcmp( buffer, "chunked" ) || ( ! res && proxy_active() && GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND ),
971  "Wrong transfer encoding '%s'\n", buffer );
972 
973  SetLastError( 0xdeadbeef );
974  length = 16;
976  ok( !res, "Found CONTENT_LENGTH option '%s'\n", buffer );
977  ok( GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "Wrong error %u\n", GetLastError() );
978 
979  length = 100;
980  trace("Entering Query loop\n");
981 
982  while (TRUE)
983  {
985  ok(!(!res && length != 0),"InternetQueryDataAvailable failed with non-zero length\n");
986  ok(res, "InternetQueryDataAvailable failed, error %d\n", GetLastError());
987  trace("got %u available\n",length);
988  if (length)
989  {
990  char *buffer = HeapAlloc(GetProcessHeap(),0,length+1);
991 
992  SetLastError(0xdeadbeef);
993  res = InternetReadFile(hor,buffer,length,&got);
994  ok(GetLastError() == 0 ||
995  broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
996 
997  buffer[got]=0;
998  trace("ReadFile -> %i %i\n",res,got);
999  ok( length == got, "only got %u of %u available\n", got, length );
1000  ok( buffer[got-1] == '\n', "received partial line '%s'\n", buffer );
1001 
1003  if (!got) break;
1004  }
1005  if (length == 0)
1006  {
1007  got = 0xdeadbeef;
1008  SetLastError(0xdeadbeef);
1009  res = InternetReadFile( hor, buffer, 1, &got );
1010  ok( res, "InternetReadFile failed: %u\n", GetLastError() );
1011  ok(GetLastError() == 0 ||
1012  broken(GetLastError() == 0xdeadbeef /* XP/W2K3 */), "Last Error not reset %u\n", GetLastError());
1013  ok( !got, "got %u\n", got );
1014  break;
1015  }
1016  }
1017 abort:
1018  trace("aborting\n");
1019  if (hor != 0x0) {
1020  res = InternetCloseHandle(hor);
1021  ok (res, "InternetCloseHandle of handle opened by HttpOpenRequestA failed\n");
1022  }
1023  if (hi != 0x0) {
1024  res = InternetCloseHandle(hi);
1025  ok (res, "InternetCloseHandle of handle opened by InternetOpenA failed\n");
1026  }
1027 }
1028 
1030 {
1031  DWORD rc;
1032  DWORD length;
1033  const char *types[2] = { "*", NULL };
1034  HINTERNET hi, hic = 0, hor = 0;
1035  INTERNET_BUFFERSA inetbuffers;
1036 
1037  trace("Starting InternetReadFileExA test with flags 0x%x\n",flags);
1038  reset_events();
1039 
1040  trace("InternetOpenA <--\n");
1042  ok((hi != 0x0),"InternetOpen failed with error %u\n", GetLastError());
1043  trace("InternetOpenA -->\n");
1044 
1045  if (hi == 0x0) goto abort;
1046 
1047  pInternetSetStatusCallbackA(hi,&callback);
1048 
1050 
1051  trace("InternetConnectA <--\n");
1052  hic=InternetConnectA(hi, "test.winehq.org", INTERNET_INVALID_PORT_NUMBER,
1053  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1054  ok((hic != 0x0),"InternetConnect failed with error %u\n", GetLastError());
1055  trace("InternetConnectA -->\n");
1056 
1057  if (hic == 0x0) goto abort;
1058 
1061 
1062  trace("HttpOpenRequestA <--\n");
1063  hor = HttpOpenRequestA(hic, "GET", "/tests/redirect", NULL, NULL, types,
1065  0xdeadbead);
1066  if (hor == 0x0 && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED) {
1067  /*
1068  * If the internet name can't be resolved we are probably behind
1069  * a firewall or in some other way not directly connected to the
1070  * Internet. Not enough reason to fail the test. Just ignore and
1071  * abort.
1072  */
1073  } else {
1074  ok((hor != 0x0),"HttpOpenRequest failed with error %u\n", GetLastError());
1075  }
1076  trace("HttpOpenRequestA -->\n");
1077 
1078  if (hor == 0x0) goto abort;
1079 
1084  {
1087  }
1100  if (flags & INTERNET_FLAG_ASYNC)
1102  else
1104 
1105  trace("HttpSendRequestA -->\n");
1106  SetLastError(0xdeadbeef);
1107  rc = HttpSendRequestA(hor, "", -1, NULL, 0);
1108  if (flags & INTERNET_FLAG_ASYNC)
1109  ok(((rc == 0)&&(GetLastError() == ERROR_IO_PENDING)),
1110  "Asynchronous HttpSendRequest NOT returning 0 with error ERROR_IO_PENDING\n");
1111  else
1113  "Synchronous HttpSendRequest returning 0, error %u\n", GetLastError());
1114  trace("HttpSendRequestA <--\n");
1115 
1116  if (!rc && (GetLastError() == ERROR_IO_PENDING)) {
1118  ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1119  }
1120 
1122  {
1125  }
1126  else
1127  {
1130  }
1138  if (flags & INTERNET_FLAG_ASYNC)
1140  else
1143  /* Sent on WinXP only if first_connection_to_test_url is TRUE, on Win98 always sent */
1146 
1147  if(is_ie7plus) {
1148  rc = InternetReadFileExW(hor, NULL, 0, 0xdeadcafe);
1149  ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1150  "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1151  rc ? "TRUE" : "FALSE", GetLastError());
1152  }
1153 
1154  /* tests invalid dwStructSize */
1155  inetbuffers.dwStructSize = sizeof(inetbuffers)+1;
1156  inetbuffers.lpcszHeader = NULL;
1157  inetbuffers.dwHeadersLength = 0;
1158  inetbuffers.dwBufferLength = 10;
1159  inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, 10);
1160  inetbuffers.dwOffsetHigh = 1234;
1161  inetbuffers.dwOffsetLow = 5678;
1162  rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1163  ok(!rc && (GetLastError() == ERROR_INVALID_PARAMETER),
1164  "InternetReadFileEx should have failed with ERROR_INVALID_PARAMETER instead of %s, %u\n",
1165  rc ? "TRUE" : "FALSE", GetLastError());
1166  HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1167 
1168  test_request_flags(hor, 0);
1169 
1170  /* tests to see whether lpcszHeader is used - it isn't */
1171  inetbuffers.dwStructSize = sizeof(inetbuffers);
1172  inetbuffers.lpcszHeader = (LPCSTR)0xdeadbeef;
1173  inetbuffers.dwHeadersLength = 255;
1174  inetbuffers.dwBufferLength = 0;
1175  inetbuffers.lpvBuffer = NULL;
1176  inetbuffers.dwOffsetHigh = 1234;
1177  inetbuffers.dwOffsetLow = 5678;
1178  rc = InternetReadFileExA(hor, &inetbuffers, 0, 0xdeadcafe);
1179  ok(rc, "InternetReadFileEx failed with error %u\n", GetLastError());
1180  trace("read %i bytes\n", inetbuffers.dwBufferLength);
1181 
1182  rc = InternetReadFileExA(NULL, &inetbuffers, 0, 0xdeadcafe);
1183  ok(!rc && (GetLastError() == ERROR_INVALID_HANDLE),
1184  "InternetReadFileEx should have failed with ERROR_INVALID_HANDLE instead of %s, %u\n",
1185  rc ? "TRUE" : "FALSE", GetLastError());
1186 
1187  length = 0;
1188  trace("Entering Query loop\n");
1189 
1190  while (TRUE)
1191  {
1192  inetbuffers.dwStructSize = sizeof(inetbuffers);
1193  inetbuffers.dwBufferLength = 1024;
1194  inetbuffers.lpvBuffer = HeapAlloc(GetProcessHeap(), 0, inetbuffers.dwBufferLength+1);
1195  inetbuffers.dwOffsetHigh = 1234;
1196  inetbuffers.dwOffsetLow = 5678;
1197 
1201  rc = InternetReadFileExA(hor, &inetbuffers, IRF_ASYNC | IRF_USE_CONTEXT, 0xcafebabe);
1202  if (!rc)
1203  {
1204  if (GetLastError() == ERROR_IO_PENDING)
1205  {
1206  trace("InternetReadFileEx -> PENDING\n");
1208  "Should not get ERROR_IO_PENDING without INTERNET_FLAG_ASYNC\n");
1213  ok(req_error == ERROR_SUCCESS, "req_error = %u\n", req_error);
1214  }
1215  else
1216  {
1217  trace("InternetReadFileEx -> FAILED %u\n", GetLastError());
1218  break;
1219  }
1220  }
1221  else
1222  {
1223  trace("InternetReadFileEx -> SUCCEEDED\n");
1225  if (inetbuffers.dwBufferLength)
1226  {
1229  }
1230  else
1231  {
1232  /* Win98 still sends these when 0 bytes are read, WinXP does not */
1235  }
1236  }
1237 
1238  trace("read %i bytes\n", inetbuffers.dwBufferLength);
1239  ((char *)inetbuffers.lpvBuffer)[inetbuffers.dwBufferLength] = '\0';
1240 
1241  ok(inetbuffers.dwOffsetHigh == 1234 && inetbuffers.dwOffsetLow == 5678,
1242  "InternetReadFileEx sets offsets to 0x%x%08x\n",
1243  inetbuffers.dwOffsetHigh, inetbuffers.dwOffsetLow);
1244 
1245  HeapFree(GetProcessHeap(), 0, inetbuffers.lpvBuffer);
1246 
1247  if (!inetbuffers.dwBufferLength)
1248  break;
1249 
1250  length += inetbuffers.dwBufferLength;
1251  }
1252  ok(length > 0, "failed to read any of the document\n");
1253  trace("Finished. Read %d bytes\n", length);
1254 
1255 abort:
1256  close_async_handle(hi, 2);
1258 }
1259 
1260 static void InternetOpenUrlA_test(void)
1261 {
1262  HINTERNET myhinternet, myhttp;
1263  char buffer[0x400];
1264  DWORD size, readbytes, totalbytes=0;
1265  BOOL ret;
1266 
1269  "DeleteUrlCacheEntry returned %x, GetLastError() = %d\n", ret, GetLastError());
1270 
1271  myhinternet = InternetOpenA("Winetest",0,NULL,NULL,INTERNET_FLAG_NO_CACHE_WRITE);
1272  ok((myhinternet != 0), "InternetOpen failed, error %u\n",GetLastError());
1273  size = 0x400;
1275  ok( ret, "InternetCanonicalizeUrl failed, error %u\n",GetLastError());
1276 
1277  SetLastError(0);
1278  myhttp = InternetOpenUrlA(myhinternet, TEST_URL, 0, 0,
1281  return; /* WinXP returns this when not connected to the net */
1282  ok((myhttp != 0),"InternetOpenUrl failed, error %u\n",GetLastError());
1283  ret = InternetReadFile(myhttp, buffer,0x400,&readbytes);
1284  ok( ret, "InternetReadFile failed, error %u\n",GetLastError());
1285  totalbytes += readbytes;
1286  while (readbytes && InternetReadFile(myhttp, buffer,0x400,&readbytes))
1287  totalbytes += readbytes;
1288  trace("read 0x%08x bytes\n",totalbytes);
1289 
1290  InternetCloseHandle(myhttp);
1291  InternetCloseHandle(myhinternet);
1292 
1294  ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "INTERNET_FLAG_NO_CACHE_WRITE flag doesn't work\n");
1295 }
1296 
1297 static void HttpSendRequestEx_test(void)
1298 {
1299  HINTERNET hSession;
1300  HINTERNET hConnect;
1301  HINTERNET hRequest;
1302 
1303  INTERNET_BUFFERSA BufferIn;
1304  DWORD dwBytesWritten, dwBytesRead, error;
1305  CHAR szBuffer[256];
1306  int i;
1307  BOOL ret;
1308 
1309  static char szPostData[] = "mode=Test";
1310  static const char szContentType[] = "Content-Type: application/x-www-form-urlencoded";
1311 
1312  hSession = InternetOpenA("Wine Regression Test",
1314  ok( hSession != NULL ,"Unable to open Internet session\n");
1315  hConnect = InternetConnectA(hSession, "test.winehq.org",
1317  0);
1318  ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1319  hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1321  if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1322  {
1323  skip( "Network unreachable, skipping test\n" );
1324  goto done;
1325  }
1326  ok( hRequest != NULL, "Failed to open request handle err %u\n", GetLastError());
1327 
1329 
1330  BufferIn.dwStructSize = sizeof(BufferIn);
1331  BufferIn.Next = (INTERNET_BUFFERSA*)0xdeadcab;
1332  BufferIn.lpcszHeader = szContentType;
1333  BufferIn.dwHeadersLength = sizeof(szContentType)-1;
1334  BufferIn.dwHeadersTotal = sizeof(szContentType)-1;
1335  BufferIn.lpvBuffer = szPostData;
1336  BufferIn.dwBufferLength = 3;
1337  BufferIn.dwBufferTotal = sizeof(szPostData)-1;
1338  BufferIn.dwOffsetLow = 0;
1339  BufferIn.dwOffsetHigh = 0;
1340 
1341  SetLastError(0xdeadbeef);
1342  ret = HttpSendRequestExA(hRequest, &BufferIn, NULL, 0 ,0);
1343  error = GetLastError();
1344  ok(ret, "HttpSendRequestEx Failed with error %u\n", error);
1345  ok(error == ERROR_SUCCESS, "expected ERROR_SUCCESS, got %u\n", error);
1346 
1348 
1349  for (i = 3; szPostData[i]; i++)
1350  ok(InternetWriteFile(hRequest, &szPostData[i], 1, &dwBytesWritten),
1351  "InternetWriteFile failed\n");
1352 
1354 
1355  ok(HttpEndRequestA(hRequest, NULL, 0, 0), "HttpEndRequest Failed\n");
1356 
1357  test_request_flags(hRequest, 0);
1358 
1359  ok(InternetReadFile(hRequest, szBuffer, 255, &dwBytesRead),
1360  "Unable to read response\n");
1361  szBuffer[dwBytesRead] = 0;
1362 
1363  ok(dwBytesRead == 13,"Read %u bytes instead of 13\n",dwBytesRead);
1364  ok(strncmp(szBuffer,"mode => Test\n",dwBytesRead)==0 || broken(proxy_active()),"Got string %s\n",szBuffer);
1365 
1366  ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
1367 done:
1368  ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
1369  ok(InternetCloseHandle(hSession), "Close session handle failed\n");
1370 }
1371 
1372 static void InternetOpenRequest_test(void)
1373 {
1375  static const char *types[] = { "*", "", NULL };
1376  static const WCHAR slash[] = {'/', 0}, any[] = {'*', 0}, empty[] = {0};
1377  static const WCHAR *typesW[] = { any, empty, NULL };
1378  BOOL ret;
1379 
1380  session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1381  ok(session != NULL ,"Unable to open Internet session\n");
1382 
1384  INTERNET_SERVICE_HTTP, 0, 0);
1385  ok(connect == NULL, "InternetConnectA should have failed\n");
1386  ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with NULL server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1387 
1389  INTERNET_SERVICE_HTTP, 0, 0);
1390  ok(connect == NULL, "InternetConnectA should have failed\n");
1391  ok(GetLastError() == ERROR_INVALID_PARAMETER, "InternetConnectA with blank server named should have failed with ERROR_INVALID_PARAMETER instead of %d\n", GetLastError());
1392 
1394  INTERNET_SERVICE_HTTP, 0, 0);
1395  ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1396 
1399  {
1400  skip( "Network unreachable, skipping test\n" );
1401  goto done;
1402  }
1403  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1404 
1405  ret = HttpSendRequestW(request, NULL, 0, NULL, 0);
1406  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1407  ok(InternetCloseHandle(request), "Close request handle failed\n");
1408 
1410  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1411 
1412  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1413  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1414  ok(InternetCloseHandle(request), "Close request handle failed\n");
1415 
1416 done:
1417  ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1418  ok(InternetCloseHandle(session), "Close session handle failed\n");
1419 }
1420 
1421 static void test_cache_read(void)
1422 {
1423  HINTERNET session, connection, req;
1424  FILETIME now, tomorrow, yesterday;
1425  BYTE content[1000], buf[2000];
1426  char file_path[MAX_PATH];
1427  ULARGE_INTEGER li;
1428  HANDLE file;
1429  DWORD size;
1430  unsigned i;
1431  BOOL res;
1432 
1433  static const char cache_only_url[] = "http://test.winehq.org/tests/cache-only";
1434  BYTE cache_headers[] = "HTTP/1.1 200 OK\r\n\r\n";
1435 
1436  trace("Testing cache read...\n");
1437  reset_events();
1438 
1439  for(i = 0; i < sizeof(content); i++)
1440  content[i] = '0' + (i%10);
1441 
1443  li.u.HighPart = now.dwHighDateTime;
1444  li.u.LowPart = now.dwLowDateTime;
1445  li.QuadPart += (LONGLONG)10000000 * 3600 * 24;
1446  tomorrow.dwHighDateTime = li.u.HighPart;
1447  tomorrow.dwLowDateTime = li.u.LowPart;
1448  li.QuadPart -= (LONGLONG)10000000 * 3600 * 24 * 2;
1449  yesterday.dwHighDateTime = li.u.HighPart;
1450  yesterday.dwLowDateTime = li.u.LowPart;
1451 
1452  res = CreateUrlCacheEntryA(cache_only_url, sizeof(content), "", file_path, 0);
1453  ok(res, "CreateUrlCacheEntryA failed: %u\n", GetLastError());
1454 
1456  ok(file != INVALID_HANDLE_VALUE, "CreateFile failed\n");
1457 
1458  WriteFile(file, content, sizeof(content), &size, NULL);
1459  CloseHandle(file);
1460 
1461  res = CommitUrlCacheEntryA(cache_only_url, file_path, tomorrow, yesterday, NORMAL_CACHE_ENTRY,
1462  cache_headers, sizeof(cache_headers)-1, "", 0);
1463  ok(res, "CommitUrlCacheEntryA failed: %u\n", GetLastError());
1464 
1466  ok(session != NULL,"InternetOpen failed with error %u\n", GetLastError());
1467 
1468  pInternetSetStatusCallbackA(session, callback);
1469 
1471  connection = InternetConnectA(session, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT,
1472  NULL, NULL, INTERNET_SERVICE_HTTP, 0x0, 0xdeadbeef);
1473  ok(connection != NULL,"InternetConnect failed with error %u\n", GetLastError());
1475 
1477  req = HttpOpenRequestA(connection, "GET", "/tests/cache-only", NULL, NULL, NULL, 0, 0xdeadbead);
1478  ok(req != NULL, "HttpOpenRequest failed: %u\n", GetLastError());
1480 
1488 
1489  res = HttpSendRequestA(req, NULL, -1, NULL, 0);
1490  todo_wine
1491  ok(res, "HttpSendRequest failed: %u\n", GetLastError());
1492 
1493  if(res) {
1494  size = 0;
1495  res = InternetQueryDataAvailable(req, &size, 0, 0);
1496  ok(res, "InternetQueryDataAvailable failed: %u\n", GetLastError());
1497  ok(size == sizeof(content), "size = %u\n", size);
1498 
1499  size = sizeof(buf);
1500  res = InternetReadFile(req, buf, sizeof(buf), &size);
1501  ok(res, "InternetReadFile failed: %u\n", GetLastError());
1502  ok(size == sizeof(content), "size = %u\n", size);
1503  ok(!memcmp(content, buf, sizeof(content)), "unexpected content\n");
1504  }
1505 
1507 
1515 
1516  res = DeleteUrlCacheEntryA(cache_only_url);
1517  ok(res, "DeleteUrlCacheEntryA failed: %u\n", GetLastError());
1518 }
1519 
1520 static void test_http_cache(void)
1521 {
1524  DWORD size, file_size;
1525  BYTE buf[100];
1526  HANDLE file;
1527  BOOL ret;
1528  FILETIME filetime_zero = {0};
1529 
1530  static const char cached_content[] = "data read from cache";
1531  static const char *types[] = { "*", "", NULL };
1532 
1533  session = InternetOpenA("Wine Regression Test", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
1534  ok(session != NULL ,"Unable to open Internet session\n");
1535 
1537  INTERNET_SERVICE_HTTP, 0, 0);
1538  ok(connect != NULL, "Unable to connect to http://test.winehq.org with error %d\n", GetLastError());
1539 
1540  request = HttpOpenRequestA(connect, NULL, "/tests/hello.html", NULL, NULL, types, INTERNET_FLAG_NEED_FILE, 0);
1542  {
1543  skip( "Network unreachable, skipping test\n" );
1544 
1545  ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1546  ok(InternetCloseHandle(session), "Close session handle failed\n");
1547 
1548  return;
1549  }
1550  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1551 
1552  size = sizeof(url);
1554  ok(ret, "InternetQueryOptionA(INTERNET_OPTION_URL) failed: %u\n", GetLastError());
1555  ok(!strcmp(url, "http://test.winehq.org/tests/hello.html"), "Wrong URL %s\n", url);
1556 
1557  size = sizeof(file_name);
1559  ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1560  ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1561  ok(!size, "size = %d\n", size);
1562 
1563  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1564  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1565 
1566  size = sizeof(file_name);
1568  ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1569 
1572  ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1574  ok(file_size == 106, "file size = %u\n", file_size);
1575 
1576  size = sizeof(buf);
1577  ret = InternetReadFile(request, buf, sizeof(buf), &size);
1578  ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1579  ok(size == 100, "size = %u\n", size);
1580 
1582  ok(file_size == 106, "file size = %u\n", file_size);
1583  CloseHandle(file);
1584 
1586  ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1587 
1588  ok(InternetCloseHandle(request), "Close request handle failed\n");
1589 
1592  ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1593  ret = WriteFile(file, cached_content, sizeof(cached_content), &size, NULL);
1594  ok(ret && size, "WriteFile failed: %d, %d\n", ret, size);
1595  ret = CommitUrlCacheEntryA(url, file_name, filetime_zero, filetime_zero, NORMAL_CACHE_ENTRY, NULL, 0, NULL, 0);
1596  ok(ret, "CommitUrlCacheEntry failed: %d\n", GetLastError());
1597  CloseHandle(file);
1598 
1599  /* Send the same request, requiring it to be retrieved from the cache */
1600  request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1601 
1602  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1603  ok(ret, "HttpSendRequest failed\n");
1604 
1605  size = sizeof(buf);
1606  ret = InternetReadFile(request, buf, sizeof(buf), &size);
1607  ok(ret, "InternetReadFile failed: %u\n", GetLastError());
1608  ok(size == 100, "size = %u\n", size);
1609  buf[99] = 0;
1610  todo_wine ok(!strcmp((char*)buf, cached_content), "incorrect page data: %s\n", (char*)buf);
1611 
1612  ok(InternetCloseHandle(request), "Close request handle failed\n");
1613 
1615  request = HttpOpenRequestA(connect, "GET", "/tests/hello.html", NULL, NULL, NULL, INTERNET_FLAG_FROM_CACHE, 0);
1616  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1617  todo_wine ok(!ret, "HttpSendRequest succeeded\n");
1618  if(!ret)
1619  ok(GetLastError() == ERROR_FILE_NOT_FOUND, "GetLastError() = %d\n", GetLastError());
1620  ok(InternetCloseHandle(request), "Close request handle failed\n");
1621 
1623  ok(request != NULL, "Failed to open request handle err %u\n", GetLastError());
1624 
1625  size = sizeof(file_name);
1627  ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1628  ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1629  ok(!size, "size = %d\n", size);
1630 
1631  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
1632  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1633 
1634  size = sizeof(file_name);
1635  file_name[0] = 0;
1637  if (ret)
1638  {
1641  ok(file != INVALID_HANDLE_VALUE, "Could not create file: %u\n", GetLastError());
1642  CloseHandle(file);
1643  }
1644  else
1645  {
1646  /* < IE8 */
1647  ok(file_name[0] == 0, "Didn't expect a file name\n");
1648  }
1649 
1650  ok(InternetCloseHandle(request), "Close request handle failed\n");
1651  ok(InternetCloseHandle(connect), "Close connect handle failed\n");
1652  ok(InternetCloseHandle(session), "Close session handle failed\n");
1653 
1654  test_cache_read();
1655 }
1656 
1658 {
1659  char file_name[MAX_PATH];
1660  test_request_t req;
1661  HANDLE lock, lock2;
1662  DWORD size;
1663  BOOL ret;
1664 
1665  open_simple_request(&req, "test.winehq.org", INTERNET_DEFAULT_HTTP_PORT, NULL, "/tests/hello.html");
1666 
1667  size = sizeof(file_name);
1669  ok(!ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) succeeded\n");
1670  ok(GetLastError() == ERROR_INTERNET_ITEM_NOT_FOUND, "GetLastError()=%u\n", GetLastError());
1671  ok(!size, "size = %d\n", size);
1672 
1673  lock = NULL;
1675  ok(!ret && GetLastError() == ERROR_FILE_NOT_FOUND, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1676 
1677  ret = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
1678  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
1679 
1680  size = sizeof(file_name);
1682  ok(ret, "InternetQueryOptionA(INTERNET_OPTION_DATAFILE_NAME) failed: %u\n", GetLastError());
1683 
1685  ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1686  ok(lock != NULL, "lock == NULL\n");
1687 
1688  ret = InternetLockRequestFile(req.request, &lock2);
1689  ok(ret, "InternetLockRequestFile returned: %x(%u)\n", ret, GetLastError());
1690  ok(lock == lock2, "lock != lock2\n");
1691 
1692  ret = InternetUnlockRequestFile(lock2);
1693  ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1694 
1696  ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1697 
1698  ok(InternetCloseHandle(req.request), "Close request handle failed\n");
1699 
1701  ok(!ret && GetLastError() == ERROR_SHARING_VIOLATION, "Deleting file returned %x(%u)\n", ret, GetLastError());
1702 
1704  ok(ret, "InternetUnlockRequestFile failed: %u\n", GetLastError());
1705 
1707  ok(ret, "Deleting file returned %x(%u)\n", ret, GetLastError());
1708 }
1709 
1710 static void HttpHeaders_test(void)
1711 {
1712  HINTERNET hSession;
1713  HINTERNET hConnect;
1714  HINTERNET hRequest;
1715  CHAR buffer[256];
1716  WCHAR wbuffer[256];
1717  DWORD len = 256;
1718  DWORD oldlen;
1719  DWORD index = 0;
1720  BOOL ret;
1721 
1722  hSession = InternetOpenA("Wine Regression Test",
1724  ok( hSession != NULL ,"Unable to open Internet session\n");
1725  hConnect = InternetConnectA(hSession, "test.winehq.org",
1727  0);
1728  ok( hConnect != NULL, "Unable to connect to http://test.winehq.org\n");
1729  hRequest = HttpOpenRequestA(hConnect, "POST", "/tests/post.php",
1731  if (!hRequest && GetLastError() == ERROR_INTERNET_NAME_NOT_RESOLVED)
1732  {
1733  skip( "Network unreachable, skipping test\n" );
1734  goto done;
1735  }
1736  ok( hRequest != NULL, "Failed to open request handle\n");
1737 
1738  index = 0;
1739  len = sizeof(buffer);
1740  strcpy(buffer,"Warning");
1742  buffer,&len,&index)==0,"Warning hearder reported as Existing\n");
1743 
1744  ok(HttpAddRequestHeadersA(hRequest,"Warning:test1",-1,HTTP_ADDREQ_FLAG_ADD),
1745  "Failed to add new header\n");
1746 
1747  index = 0;
1748  len = sizeof(buffer);
1749  strcpy(buffer,"Warning");
1751  buffer,&len,&index),"Unable to query header\n");
1752  ok(index == 1, "Index was not incremented\n");
1753  ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1754  ok(len == 5, "Invalid length (exp. 5, got %d)\n", len);
1755  ok((len < sizeof(buffer)) && (buffer[len] == 0), "Buffer not NULL-terminated\n"); /* len show only 5 characters but the buffer is NULL-terminated*/
1756  len = sizeof(buffer);
1757  strcpy(buffer,"Warning");
1759  buffer,&len,&index)==0,"Second Index Should Not Exist\n");
1760 
1761  index = 0;
1762  len = 5; /* could store the string but not the NULL terminator */
1763  strcpy(buffer,"Warning");
1765  buffer,&len,&index) == FALSE,"Query succeeded on a too small buffer\n");
1766  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1767  ok(index == 0, "Index was incremented\n");
1768  ok(strcmp(buffer,"Warning")==0, "incorrect string was returned(%s)\n",buffer); /* string not touched */
1769  ok(len == 6, "Invalid length (exp. 6, got %d)\n", len); /* unlike success, the length includes the NULL-terminator */
1770 
1771  /* a call with NULL will fail but will return the length */
1772  index = 0;
1773  len = sizeof(buffer);
1774  SetLastError(0xdeadbeef);
1776  NULL,&len,&index) == FALSE,"Query worked\n");
1777  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1778  ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1779  ok(index == 0, "Index was incremented\n");
1780 
1781  /* even for a len that is too small */
1782  index = 0;
1783  len = 15;
1784  SetLastError(0xdeadbeef);
1786  NULL,&len,&index) == FALSE,"Query worked\n");
1787  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1788  ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1789  ok(index == 0, "Index was incremented\n");
1790 
1791  index = 0;
1792  len = 0;
1793  SetLastError(0xdeadbeef);
1795  NULL,&len,&index) == FALSE,"Query worked\n");
1796  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1797  ok(len > 40, "Invalid length (exp. more than 40, got %d)\n", len);
1798  ok(index == 0, "Index was incremented\n");
1799  oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1800 
1801 
1802  /* a working query */
1803  index = 0;
1804  len = sizeof(buffer);
1805  memset(buffer, 'x', sizeof(buffer));
1807  buffer,&len,&index),"Unable to query header\n");
1808  ok(len + sizeof(CHAR) <= oldlen, "Result longer than advertised\n");
1809  ok((len < sizeof(buffer)-sizeof(CHAR)) && (buffer[len/sizeof(CHAR)] == 0),"No NUL at end\n");
1810  ok(len == strlen(buffer) * sizeof(CHAR), "Length wrong\n");
1811  /* what's in the middle differs between Wine and Windows so currently we check only the beginning and the end */
1812  ok(strncmp(buffer, "POST /tests/post.php HTTP/1", 25)==0, "Invalid beginning of headers string\n");
1813  ok(strcmp(buffer + strlen(buffer) - 4, "\r\n\r\n")==0, "Invalid end of headers string\n");
1814  ok(index == 0, "Index was incremented\n");
1815 
1816  /* Like above two tests, but for W version */
1817 
1818  index = 0;
1819  len = 0;
1820  SetLastError(0xdeadbeef);
1822  NULL,&len,&index) == FALSE,"Query worked\n");
1823  ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Unexpected last error: %d\n", GetLastError());
1824  ok(len > 80, "Invalid length (exp. more than 80, got %d)\n", len);
1825  ok(index == 0, "Index was incremented\n");
1826  oldlen = len; /* bytes; at least long enough to hold buffer & nul */
1827 
1828  /* a working query */
1829  index = 0;
1830  len = sizeof(wbuffer);
1831  memset(wbuffer, 'x', sizeof(wbuffer));
1833  wbuffer,&len,&index),"Unable to query header\n");
1834  ok(len + sizeof(WCHAR) <= oldlen, "Result longer than advertised\n");
1835  ok(len == lstrlenW(wbuffer) * sizeof(WCHAR), "Length wrong\n");
1836  ok((len < sizeof(wbuffer)-sizeof(WCHAR)) && (wbuffer[len/sizeof(WCHAR)] == 0),"No NUL at end\n");
1837  ok(index == 0, "Index was incremented\n");
1838 
1839  /* end of W version tests */
1840 
1841  /* Without HTTP_QUERY_FLAG_REQUEST_HEADERS */
1842  index = 0;
1843  len = sizeof(buffer);
1844  memset(buffer, 'x', sizeof(buffer));
1846  buffer,&len,&index) == TRUE,"Query failed\n");
1847  ok(len == 2 || len == 4 /* win10 */, "Expected 2 or 4, got %d\n", len);
1848  ok(memcmp(buffer, "\r\n\r\n", len) == 0, "Expected CRLF, got '%s'\n", buffer);
1849  ok(index == 0, "Index was incremented\n");
1850 
1851  ok(HttpAddRequestHeadersA(hRequest,"Warning:test2",-1,HTTP_ADDREQ_FLAG_ADD),
1852  "Failed to add duplicate header using HTTP_ADDREQ_FLAG_ADD\n");
1853 
1854  index = 0;
1855  len = sizeof(buffer);
1856  strcpy(buffer,"Warning");
1858  buffer,&len,&index),"Unable to query header\n");
1859  ok(index == 1, "Index was not incremented\n");
1860  ok(strcmp(buffer,"test1")==0, "incorrect string was returned(%s)\n",buffer);
1861  len = sizeof(buffer);
1862  strcpy(buffer,"Warning");
1864  buffer,&len,&index),"Failed to get second header\n");
1865  ok(index == 2, "Index was not incremented\n");
1866  ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1867  len = sizeof(buffer);
1868  strcpy(buffer,"Warning");
1870  buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1871 
1872  ok(HttpAddRequestHeadersA(hRequest,"Warning:test3",-1,HTTP_ADDREQ_FLAG_REPLACE), "Failed to replace header using HTTP_ADDREQ_FLAG_REPLACE\n");
1873 
1874  index = 0;
1875  len = sizeof(buffer);
1876  strcpy(buffer,"Warning");
1878  buffer,&len,&index),"Unable to query header\n");
1879  ok(index == 1, "Index was not incremented\n");
1880  ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1881  len = sizeof(buffer);
1882  strcpy(buffer,"Warning");
1884  buffer,&len,&index),"Failed to get second header\n");
1885  ok(index == 2, "Index was not incremented\n");
1886  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1887  len = sizeof(buffer);
1888  strcpy(buffer,"Warning");
1890  buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1891 
1892  ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1,HTTP_ADDREQ_FLAG_ADD_IF_NEW)==0, "HTTP_ADDREQ_FLAG_ADD_IF_NEW replaced existing header\n");
1893 
1894  index = 0;
1895  len = sizeof(buffer);
1896  strcpy(buffer,"Warning");
1898  buffer,&len,&index),"Unable to query header\n");
1899  ok(index == 1, "Index was not incremented\n");
1900  ok(strcmp(buffer,"test2")==0, "incorrect string was returned(%s)\n",buffer);
1901  len = sizeof(buffer);
1902  strcpy(buffer,"Warning");
1904  buffer,&len,&index),"Failed to get second header\n");
1905  ok(index == 2, "Index was not incremented\n");
1906  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1907  len = sizeof(buffer);
1908  strcpy(buffer,"Warning");
1910  buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1911 
1912  ok(HttpAddRequestHeadersA(hRequest,"Warning:test4",-1, HTTP_ADDREQ_FLAG_COALESCE), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1913 
1914  index = 0;
1915  len = sizeof(buffer);
1916  strcpy(buffer,"Warning");
1918  buffer,&len,&index),"Unable to query header\n");
1919  ok(index == 1, "Index was not incremented\n");
1920  ok(strcmp(buffer,"test2, test4")==0, "incorrect string was returned(%s)\n", buffer);
1921  len = sizeof(buffer);
1922  strcpy(buffer,"Warning");
1923  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1924  ok(index == 2, "Index was not incremented\n");
1925  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1926  len = sizeof(buffer);
1927  strcpy(buffer,"Warning");
1928  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1929 
1930  ok(HttpAddRequestHeadersA(hRequest,"Warning:test5",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_COMMA), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1931 
1932  index = 0;
1933  len = sizeof(buffer);
1934  strcpy(buffer,"Warning");
1935  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1936  ok(index == 1, "Index was not incremented\n");
1937  ok(strcmp(buffer,"test2, test4, test5")==0, "incorrect string was returned(%s)\n",buffer);
1938  len = sizeof(buffer);
1939  strcpy(buffer,"Warning");
1940  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1941  ok(index == 2, "Index was not incremented\n");
1942  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1943  len = sizeof(buffer);
1944  strcpy(buffer,"Warning");
1945  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1946 
1947  ok(HttpAddRequestHeadersA(hRequest,"Warning:test6",-1, HTTP_ADDREQ_FLAG_COALESCE_WITH_SEMICOLON), "HTTP_ADDREQ_FLAG_COALESCE Did not work\n");
1948 
1949  index = 0;
1950  len = sizeof(buffer);
1951  strcpy(buffer,"Warning");
1952  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1953  ok(index == 1, "Index was not incremented\n");
1954  ok(strcmp(buffer,"test2, test4, test5; test6")==0, "incorrect string was returned(%s)\n",buffer);
1955  len = sizeof(buffer);
1956  strcpy(buffer,"Warning");
1957  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1958  ok(index == 2, "Index was not incremented\n");
1959  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1960  len = sizeof(buffer);
1961  strcpy(buffer,"Warning");
1962  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1963 
1964  ok(HttpAddRequestHeadersA(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");
1965 
1966  index = 0;
1967  len = sizeof(buffer);
1968  strcpy(buffer,"Warning");
1969  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1970  ok(index == 1, "Index was not incremented\n");
1971  ok(strcmp(buffer,"test3")==0, "incorrect string was returned(%s)\n",buffer);
1972  len = sizeof(buffer);
1973  strcpy(buffer,"Warning");
1974  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Failed to get second header\n");
1975  ok(index == 2, "Index was not incremented\n");
1976  ok(strcmp(buffer,"test7")==0, "incorrect string was returned(%s)\n",buffer);
1977  len = sizeof(buffer);
1978  strcpy(buffer,"Warning");
1979  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index)==0,"Third Header Should Not Exist\n");
1980 
1981  /* Ensure that blank headers are ignored and don't cause a failure */
1982  ok(HttpAddRequestHeadersA(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");
1983 
1984  index = 0;
1985  len = sizeof(buffer);
1986  strcpy(buffer,"BlankTest");
1987  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1988  ok(index == 1, "Index was not incremented\n");
1989  ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
1990 
1991  /* Ensure that malformed header separators are ignored and don't cause a failure */
1992  ok(HttpAddRequestHeadersA(hRequest,"\r\rMalformedTest:value\n\nMalformedTestTwo: value2\rMalformedTestThree: value3\n\n\r\r\n",-1, HTTP_ADDREQ_FLAG_ADD|HTTP_ADDREQ_FLAG_REPLACE),
1993  "Failed to add header with malformed entries in list\n");
1994 
1995  index = 0;
1996  len = sizeof(buffer);
1997  strcpy(buffer,"MalformedTest");
1998  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
1999  ok(index == 1, "Index was not incremented\n");
2000  ok(strcmp(buffer,"value")==0, "incorrect string was returned(%s)\n",buffer);
2001  index = 0;
2002  len = sizeof(buffer);
2003  strcpy(buffer,"MalformedTestTwo");
2004  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2005  ok(index == 1, "Index was not incremented\n");
2006  ok(strcmp(buffer,"value2")==0, "incorrect string was returned(%s)\n",buffer);
2007  index = 0;
2008  len = sizeof(buffer);
2009  strcpy(buffer,"MalformedTestThree");
2010  ok(HttpQueryInfoA(hRequest,HTTP_QUERY_CUSTOM|HTTP_QUERY_FLAG_REQUEST_HEADERS, buffer,&len,&index),"Unable to query header\n");
2011  ok(index == 1, "Index was not incremented\n");
2012  ok(strcmp(buffer,"value3")==0, "incorrect string was returned(%s)\n",buffer);
2013 
2014  ret = HttpAddRequestHeadersA(hRequest, "Authorization: Basic\r\n", -1, HTTP_ADDREQ_FLAG_ADD);
2015  ok(ret, "unable to add header %u\n", GetLastError());
2016 
2017  index = 0;
2018  buffer[0] = 0;
2019  len = sizeof(buffer);
2021  ok(ret, "unable to query header %u\n", GetLastError());
2022  ok(index == 1, "index was not incremented\n");
2023  ok(!strcmp(buffer, "Basic"), "incorrect string was returned (%s)\n", buffer);
2024 
2025  ret = HttpAddRequestHeadersA(hRequest, "Authorization:\r\n", -1, HTTP_ADDREQ_FLAG_REPLACE);
2026  ok(ret, "unable to remove header %u\n", GetLastError());
2027 
2028  index = 0;
2029  len = sizeof(buffer);
2030  SetLastError(0xdeadbeef);
2032  "header still present\n");
2034 
2035  ok(InternetCloseHandle(hRequest), "Close request handle failed\n");
2036 done:
2037  ok(InternetCloseHandle(hConnect), "Close connect handle failed\n");
2038  ok(InternetCloseHandle(hSession), "Close session handle failed\n");
2039 }
2040 
2041 static const char garbagemsg[] =
2042 "Garbage: Header\r\n";
2043 
2044 static const char contmsg[] =
2045 "HTTP/1.1 100 Continue\r\n";
2046 
2047 static const char expandcontmsg[] =
2048 "HTTP/1.1 100 Continue\r\n"
2049 "Server: winecontinue\r\n"
2050 "Tag: something witty\r\n";
2051 
2052 static const char okmsg[] =
2053 "HTTP/1.1 200 OK\r\n"
2054 "Server: winetest\r\n"
2055 "\r\n";
2056 
2057 static const char okmsg201[] =
2058 "HTTP/1.1 201 OK\r\n"
2059 "Server: winetest\r\n"
2060 "\r\n";
2061 
2062 static const char okmsg2[] =
2063 "HTTP/1.1 200 OK\r\n"
2064 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2065 "Server: winetest\r\n"
2066 "Content-Length: 0\r\n"
2067 "Set-Cookie: one\r\n"
2068 "Set-Cookie: two\r\n"
2069 "\r\n";
2070 
2072 static const char largemsg[] =
2073 "HTTP/1.1 200 OK\r\n"
2074 "Content-Length: %I64u\r\n"
2075 "\r\n";
2076 
2077 static const char okmsg_cookie_path[] =
2078 "HTTP/1.1 200 OK\r\n"
2079 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2080 "Server: winetest\r\n"
2081 "Content-Length: 0\r\n"
2082 "Set-Cookie: subcookie2=data; path=/test_cookie_set_path\r\n"
2083 "\r\n";
2084 
2085 static const char okmsg_cookie[] =
2086 "HTTP/1.1 200 OK\r\n"
2087 "Date: Mon, 01 Dec 2008 13:44:34 GMT\r\n"
2088 "Server: winetest\r\n"
2089 "Content-Length: 0\r\n"
2090 "Set-Cookie: testcookie=testvalue\r\n"
2091 "\r\n";
2092 
2093 static const char notokmsg[] =
2094 "HTTP/1.1 400 Bad Request\r\n"
2095 "Server: winetest\r\n"
2096 "\r\n";
2097 
2098 static const char noauthmsg[] =
2099 "HTTP/1.1 401 Unauthorized\r\n"
2100 "Server: winetest\r\n"
2101 "Connection: close\r\n"
2102 "WWW-Authenticate: Basic realm=\"placebo\"\r\n"
2103 "\r\n";
2104 
2105 static const char noauthmsg2[] =
2106 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed\r\n"
2107 "HTTP/1.0 401 Anonymous requests or requests on unsecure channel are not allowed"
2108 "\0d`0|6\n"
2109 "Server: winetest\r\n";
2110 
2111 static const char proxymsg[] =
2112 "HTTP/1.1 407 Proxy Authentication Required\r\n"
2113 "Server: winetest\r\n"
2114 "Proxy-Connection: close\r\n"
2115 "Proxy-Authenticate: Basic realm=\"placebo\"\r\n"
2116 "\r\n";
2117 
2118 static const char page1[] =
2119 "<HTML>\r\n"
2120 "<HEAD><TITLE>wininet test page</TITLE></HEAD>\r\n"
2121 "<BODY>The quick brown fox jumped over the lazy dog<P></BODY>\r\n"
2122 "</HTML>\r\n\r\n";
2123 
2124 static const char ok_with_length[] =
2125 "HTTP/1.1 200 OK\r\n"
2126 "Connection: Keep-Alive\r\n"
2127 "Content-Length: 18\r\n\r\n"
2128 "HTTP/1.1 211 OK\r\n\r\n";
2129 
2130 static const char ok_with_length2[] =
2131 "HTTP/1.1 210 OK\r\n"
2132 "Connection: Keep-Alive\r\n"
2133 "Content-Length: 19\r\n\r\n"
2134 "HTTP/1.1 211 OK\r\n\r\n";
2135 
2136 struct server_info {
2138  int port;
2139 };
2140 
2141 static int test_cache_gzip;
2142 static const char *send_buffer;
2143 static int server_socket;
2144 
2146 {
2147  struct server_info *si = param;
2148  int r, c = -1, i, on, count = 0;
2149  SOCKET s;
2150  struct sockaddr_in sa;
2151  char *buffer;
2152  size_t buffer_size;
2153  WSADATA wsaData;
2154  int last_request = 0;
2155  char host_header[22];
2156  char host_header_override[30];
2157  static int test_no_cache = 0;
2158 
2159  WSAStartup(MAKEWORD(1,1), &wsaData);
2160 
2161  s = socket(AF_INET, SOCK_STREAM, 0);
2162  if (s == INVALID_SOCKET)
2163  return 1;
2164 
2165  on = 1;
2166  setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof on);
2167 
2168  memset(&sa, 0, sizeof sa);
2169  sa.sin_family = AF_INET;
2170  sa.sin_port = htons(si->port);
2171  sa.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
2172 
2173  r = bind(s, (struct sockaddr*) &sa, sizeof sa);
2174  if (r<0)
2175  return 1;
2176 
2177  listen(s, 0);
2178 
2179  SetEvent(si->hEvent);
2180 
2181  sprintf(host_header, "Host: localhost:%d", si->port);
2182  sprintf(host_header_override, "Host: test.local:%d\r\n", si->port);
2183  buffer = HeapAlloc(GetProcessHeap(), 0, buffer_size = 1000);
2184 
2185  do
2186  {
2187  if(c == -1)
2188  c = accept(s, NULL, NULL);
2189 
2190  memset(buffer, 0, buffer_size);
2191  for(i=0;; i++)
2192  {
2193  if(i == buffer_size)
2195 
2196  r = recv(c, buffer+i, 1, 0);
2197  if (r != 1)
2198  break;
2199  if (i<4) continue;
2200  if (buffer[i-2] == '\n' && buffer[i] == '\n' &&
2201  buffer[i-3] == '\r' && buffer[i-1] == '\r')
2202  break;
2203  }
2204  if (strstr(buffer, "GET /test1"))
2205  {
2206  if (!strstr(buffer, "Content-Length: 0"))
2207  {
2208  send(c, okmsg, sizeof okmsg-1, 0);
2209  send(c, page1, sizeof page1-1, 0);
2210  }
2211  else
2212  send(c, notokmsg, sizeof notokmsg-1, 0);
2213  }
2214  if (strstr(buffer, "CONNECT "))
2215  {
2216  if (!strstr(buffer, "Content-Length: 0"))
2217  send(c, notokmsg, sizeof notokmsg-1, 0);
2218  else
2219  send(c, proxymsg, sizeof proxymsg-1, 0);
2220  }
2221  if (strstr(buffer, "/test2"))
2222  {
2223  if (strstr(buffer, "Proxy-Authorization: Basic bWlrZToxMTAx"))
2224  {
2225  send(c, okmsg, sizeof okmsg-1, 0);
2226  send(c, page1, sizeof page1-1, 0);
2227  }
2228  else
2229  send(c, proxymsg, sizeof proxymsg-1, 0);
2230  }
2231  if (strstr(buffer, "/test3"))
2232  {
2233  if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2234  send(c, okmsg, sizeof okmsg-1, 0);
2235  else
2236  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2237  }
2238  if (strstr(buffer, "/test4"))
2239  {
2240  if (strstr(buffer, "Connection: Close"))
2241  send(c, okmsg, sizeof okmsg-1, 0);
2242  else
2243  send(c, notokmsg, sizeof notokmsg-1, 0);
2244  }
2245  if (strstr(buffer, "POST /test5") ||
2246  strstr(buffer, "RPC_IN_DATA /test5") ||
2247  strstr(buffer, "RPC_OUT_DATA /test5"))
2248  {
2249  if (strstr(buffer, "Content-Length: 0"))
2250  {
2251  send(c, okmsg, sizeof okmsg-1, 0);
2252  send(c, page1, sizeof page1-1, 0);
2253  }
2254  else
2255  send(c, notokmsg, sizeof notokmsg-1, 0);
2256  }
2257  if (strstr(buffer, "GET /test6"))
2258  {
2259  send(c, contmsg, sizeof contmsg-1, 0);
2260  send(c, contmsg, sizeof contmsg-1, 0);
2261  send(c, okmsg, sizeof okmsg-1, 0);
2262  send(c, page1, sizeof page1-1, 0);
2263  }
2264  if (strstr(buffer, "POST /test7"))
2265  {
2266  if (strstr(buffer, "Content-Length: 100"))
2267  {
2268  if (strstr(buffer, "POST /test7b"))
2270  send(c, okmsg, sizeof okmsg-1, 0);
2271  send(c, page1, sizeof page1-1, 0);
2272  }
2273  else
2274  send(c, notokmsg, sizeof notokmsg-1, 0);
2275  }
2276  if (strstr(buffer, "/test8"))
2277  {
2278  if (!strstr(buffer, "Connection: Close") &&
2279  strstr(buffer, "Connection: Keep-Alive") &&
2280  !strstr(buffer, "Cache-Control: no-cache") &&
2281  !strstr(buffer, "Pragma: no-cache") &&
2282  strstr(buffer, host_header))
2283  send(c, okmsg, sizeof okmsg-1, 0);
2284  else
2285  send(c, notokmsg, sizeof notokmsg-1, 0);
2286  }
2287  if (strstr(buffer, "/test9"))
2288  {
2289  if (!strstr(buffer, "Connection: Close") &&
2290  !strstr(buffer, "Connection: Keep-Alive") &&
2291  !strstr(buffer, "Cache-Control: no-cache") &&
2292  !strstr(buffer, "Pragma: no-cache") &&
2293  strstr(buffer, host_header))
2294  send(c, okmsg, sizeof okmsg-1, 0);
2295  else
2296  send(c, notokmsg, sizeof notokmsg-1, 0);
2297  }
2298  if (strstr(buffer, "/testA"))
2299  {
2300  if (!strstr(buffer, "Connection: Close") &&
2301  !strstr(buffer, "Connection: Keep-Alive") &&
2302  (strstr(buffer, "Cache-Control: no-cache") ||
2303  strstr(buffer, "Pragma: no-cache")) &&
2304  strstr(buffer, host_header))
2305  send(c, okmsg, sizeof okmsg-1, 0);
2306  else
2307  send(c, notokmsg, sizeof notokmsg-1, 0);
2308  }
2309  if (strstr(buffer, "/testC"))
2310  {
2311  if (strstr(buffer, "cookie=biscuit"))
2312  send(c, okmsg, sizeof okmsg-1, 0);
2313  else
2314  send(c, notokmsg, sizeof notokmsg-1, 0);
2315  }
2316  if (strstr(buffer, "/testD"))
2317  {
2318  send(c, okmsg2, sizeof okmsg2-1, 0);
2319  }
2320  if (strstr(buffer, "/testE"))
2321  {
2322  send(c, noauthmsg2, sizeof noauthmsg2-1, 0);
2323  }
2324  if (strstr(buffer, "GET /quit"))
2325  {
2326  send(c, okmsg, sizeof okmsg-1, 0);
2327  send(c, page1, sizeof page1-1, 0);
2328  last_request = 1;
2329  }
2330  if (strstr(buffer, "GET /testF"))
2331  {
2332  send(c, expandcontmsg, sizeof expandcontmsg-1, 0);
2333  send(c, garbagemsg, sizeof garbagemsg-1, 0);
2334  send(c, contmsg, sizeof contmsg-1, 0);
2335  send(c, garbagemsg, sizeof garbagemsg-1, 0);
2336  send(c, okmsg, sizeof okmsg-1, 0);
2337  send(c, page1, sizeof page1-1, 0);
2338  }
2339  if (strstr(buffer, "GET /testG"))
2340  {
2341  send(c, page1, sizeof page1-1, 0);
2342  }
2343 
2344  if (strstr(buffer, "GET /testJ"))
2345  {
2346  if (count == 0)
2347  {
2348  count++;
2349  send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2350  }
2351  else
2352  {
2353  send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2354  count = 0;
2355  }
2356  }
2357  if (strstr(buffer, "GET /testH"))
2358  {
2359  send(c, ok_with_length2, sizeof(ok_with_length2)-1, 0);
2361  send(c, ok_with_length, sizeof(ok_with_length)-1, 0);
2362  }
2363 
2364  if (strstr(buffer, "GET /test_no_content"))
2365  {
2366  static const char nocontentmsg[] = "HTTP/1.1 204 No Content\r\nConnection: close\r\n\r\n";
2367  send(c, nocontentmsg, sizeof(nocontentmsg)-1, 0);
2368  }
2369  if (strstr(buffer, "GET /test_conn_close"))
2370  {
2371  static const char conn_close_response[] = "HTTP/1.1 200 OK\r\nConnection: close\r\n\r\nsome content";
2372  send(c, conn_close_response, sizeof(conn_close_response)-1, 0);
2374  trace("closing connection\n");
2375  }
2376  if (strstr(buffer, "GET /test_cache_control_no_cache"))
2377  {
2378  static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: no-cache\r\n\r\nsome content";
2379  if(!test_no_cache++)
2380  send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2381  else
2382  send(c, okmsg, sizeof(okmsg)-1, 0);
2383  }
2384  if (strstr(buffer, "GET /test_cache_control_no_store"))
2385  {
2386  static const char no_cache_response[] = "HTTP/1.1 200 OK\r\nCache-Control: junk, \t No-StOrE\r\n\r\nsome content";
2387  send(c, no_cache_response, sizeof(no_cache_response)-1, 0);
2388  }
2389  if (strstr(buffer, "GET /test_cache_gzip"))
2390  {
2391  static const char gzip_response[] = "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: text/html\r\n\r\n"
2392  "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03\x4b\xaf\xca\x2c\x50\x28"
2393  "\x49\x2d\x2e\xe1\x02\x00\x62\x92\xc7\x6c\x0a\x00\x00\x00";
2394  if(!test_cache_gzip++)
2395  send(c, gzip_response, sizeof(gzip_response), 0);
2396  else
2397  send(c, notokmsg, sizeof(notokmsg)-1, 0);
2398  }
2399  if (strstr(buffer, "HEAD /test_head")) {
2400  static const char head_response[] =
2401  "HTTP/1.1 200 OK\r\n"
2402  "Connection: Keep-Alive\r\n"
2403  "Content-Length: 100\r\n"
2404  "\r\n";
2405 
2406  send(c, head_response, sizeof(head_response), 0);
2407  continue;
2408  }
2409  if (strstr(buffer, "GET /send_from_buffer"))
2411  if (strstr(buffer, "/test_cache_control_verb"))
2412  {
2413  if (!memcmp(buffer, "GET ", sizeof("GET ")-1) &&
2414  !strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2415  else if (strstr(buffer, "Cache-Control: no-cache\r\n")) send(c, okmsg, sizeof(okmsg)-1, 0);
2416  else send(c, notokmsg, sizeof(notokmsg)-1, 0);
2417  }
2418  if (strstr(buffer, "/test_request_content_length"))
2419  {
2420  static char msg[] = "HTTP/1.1 200 OK\r\nConnection: Keep-Alive\r\n\r\n";
2421  static int seen_content_length;
2422 
2423  if (!seen_content_length)
2424  {
2425  if (strstr(buffer, "Content-Length: 0"))
2426  {
2427  seen_content_length = 1;
2428  send(c, msg, sizeof msg-1, 0);
2429  }
2430  else send(c, notokmsg, sizeof notokmsg-1, 0);
2432  }
2433  else
2434  {
2435  if (strstr(buffer, "Content-Length: 0")) send(c, msg, sizeof msg-1, 0);
2436  else send(c, notokmsg, sizeof notokmsg-1, 0);
2438  }
2439  }
2440  if (strstr(buffer, "GET /test_premature_disconnect"))
2441  trace("closing connection\n");
2442  if (strstr(buffer, "HEAD /upload.txt"))
2443  {
2444  if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2445  send(c, okmsg, sizeof okmsg-1, 0);
2446  else
2447  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2448  }
2449  if (strstr(buffer, "PUT /upload2.txt"))
2450  {
2451  if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2452  send(c, okmsg, sizeof okmsg-1, 0);
2453  else
2454  send(c, notokmsg, sizeof notokmsg-1, 0);
2455  }
2456  if (strstr(buffer, "HEAD /upload3.txt"))
2457  {
2458  if (strstr(buffer, "Authorization: Basic dXNlcjE6cHdkMQ=="))
2459  send(c, okmsg, sizeof okmsg-1, 0);
2460  else
2461  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2462  }
2463  if (strstr(buffer, "HEAD /upload4.txt"))
2464  {
2465  if (strstr(buffer, "Authorization: Bearer dXNlcjE6cHdkMQ=="))
2466  send(c, okmsg, sizeof okmsg-1, 0);
2467  else if (strstr(buffer, "Authorization: Basic dXNlcjpwd2Q="))
2468  send(c, okmsg201, sizeof okmsg-1, 0);
2469  else
2470  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2471  }
2472  if (strstr(buffer, "/test_cookie_path1"))
2473  {
2474  if (strstr(buffer, "subcookie=data"))
2475  send(c, okmsg, sizeof okmsg-1, 0);
2476  else
2477  send(c, notokmsg, sizeof notokmsg-1, 0);
2478  }
2479  if (strstr(buffer, "/test_cookie_path2"))
2480  {
2481  if (strstr(buffer, "subcookie2=data"))
2482  send(c, okmsg, sizeof okmsg-1, 0);
2483  else
2484  send(c, notokmsg, sizeof notokmsg-1, 0);
2485  }
2486  if (strstr(buffer, "/test_cookie_set_path"))
2487  {
2488  send(c, okmsg_cookie_path, sizeof okmsg_cookie_path-1, 0);
2489  }
2490  if (strstr(buffer, "/test_cookie_merge"))
2491  {
2492  if (strstr(buffer, "subcookie=data") &&
2493  !strstr(buffer, "manual_cookie=test"))
2494  send(c, okmsg, sizeof okmsg-1, 0);
2495  else
2496  send(c, notokmsg, sizeof notokmsg-1, 0);
2497  }
2498  if (strstr(buffer, "/test_cookie_set_host_override"))
2499  {
2500  send(c, okmsg_cookie, sizeof okmsg_cookie-1, 0);
2501  }
2502  if (strstr(buffer, "/test_cookie_check_host_override"))
2503  {
2504  if (strstr(buffer, "Cookie:") && strstr(buffer, "testcookie=testvalue"))
2505  send(c, okmsg, sizeof okmsg-1, 0);
2506  else
2507  send(c, notokmsg, sizeof notokmsg-1, 0);
2508  }
2509  if (strstr(buffer, "/test_cookie_check_different_host"))
2510  {
2511  if (!strstr(buffer, "foo") &&
2512  strstr(buffer, "cookie=biscuit"))
2513  send(c, okmsg, sizeof okmsg-1, 0);
2514  else
2515  send(c, notokmsg, sizeof notokmsg-1, 0);
2516  }
2517  if (strstr(buffer, "/test_host_override"))
2518  {
2519  if (strstr(buffer, host_header_override))
2520  send(c, okmsg, sizeof okmsg-1, 0);
2521  else
2522  send(c, notokmsg, sizeof notokmsg-1, 0);
2523  }
2524  if (strstr(buffer, "/async_read"))
2525  {
2526  const char *page1_mid = page1 + (sizeof page1 - 1)/2;
2527  const char *page1_end = page1 + sizeof page1 - 1;
2528  send(c, okmsg, sizeof okmsg-1, 0);
2529  send(c, page1, page1_mid - page1, 0);
2531  send(c, page1_mid, page1_end - page1_mid, 0);
2532  }
2533  if (strstr(buffer, "/socket"))
2534  {
2535  server_socket = c;
2538  }
2539  if (strstr(buffer, "/echo_request"))
2540  {
2541  send(c, okmsg, sizeof(okmsg)-1, 0);
2542  send(c, buffer, strlen(buffer), 0);
2543  }
2544  if (strstr(buffer, "GET /test_remove_dot_segments"))
2545  {
2546  send(c, okmsg, sizeof(okmsg)-1, 0);
2547  }
2548 
2549  if (strstr(buffer, "HEAD /test_large_content"))
2550  {
2551  char msg[sizeof(largemsg) + 16];
2553  send(c, msg, strlen(msg), 0);
2554  }
2555  if (strstr(buffer, "HEAD /test_auth_host1"))
2556  {
2557  if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzcw=="))
2558  send(c, okmsg, sizeof okmsg-1, 0);
2559  else
2560  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2561  }
2562  if (strstr(buffer, "HEAD /test_auth_host2"))
2563  {
2564  if (strstr(buffer, "Authorization: Basic dGVzdDE6cGFzczI="))
2565  send(c, okmsg, sizeof okmsg-1, 0);
2566  else
2567  send(c, noauthmsg, sizeof noauthmsg-1, 0);
2568  }
2569  shutdown(c, 2);
2570  closesocket(c);
2571  c = -1;
2572  } while (!last_request);
2573 
2574  closesocket(s);
2576 
2577  return 0;
2578 }
2579 
2580 static void test_basic_request(int port, const char *verb, const char *url)
2581 {
2582  test_request_t req;
2583  DWORD r, count, error;
2584  char buffer[0x100];
2585 
2586  trace("basic request %s %s\n", verb, url);
2587 
2588  open_simple_request(&req, "localhost", port, verb, url);
2589 
2590  SetLastError(0xdeadbeef);
2591  r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2592  error = GetLastError();
2593  ok(error == ERROR_SUCCESS || broken(error != ERROR_SUCCESS), "expected ERROR_SUCCESS, got %u\n", error);
2594  ok(r, "HttpSendRequest failed: %u\n", GetLastError());
2595 
2596  count = 0;
2597  memset(buffer, 0, sizeof buffer);
2598  SetLastError(0xdeadbeef);
2599  r = InternetReadFile(req.request, buffer, sizeof buffer, &count);
2600  ok(r, "InternetReadFile failed %u\n", GetLastError());
2601  ok(count == sizeof page1 - 1, "count was wrong\n");
2602  ok(!memcmp(buffer, page1, sizeof page1), "http data wrong, got: %s\n", buffer);
2603 
2604  close_request(&req);
2605 }
2606 
2607 static void test_proxy_indirect(int port)
2608 {
2609  test_request_t req;
2610  DWORD r, sz;
2611  char buffer[0x40];
2612 
2613  open_simple_request(&req, "localhost", port, NULL, "/test2");
2614 
2615  r = HttpSendRequestA(req.request, NULL, 0, NULL, 0);
2616  ok(r, "HttpSendRequest failed %u\n", GetLastError());
2617 
2618  sz = sizeof buffer;
2620  ok(r || GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo failed: %d\n", GetLastError());
2621  if (!r)
2622  {
2623  skip("missing proxy header, not testing remaining proxy headers\n");
2624  goto out;
2625  }
2626  ok(!strcmp(buffer, "Basic realm=\"placebo\""), "proxy auth info wrong\n");
2627 
2628  test_status_code(req.request, 407);
2629  test_request_flags(req.request, 0);
2630 
2631  sz = sizeof buffer;
2633  ok(r, "HttpQueryInfo failed\n");
2634  ok(!strcmp(buffer, "Proxy Authentication Required"), "proxy text wrong\n");
2635 
2636  sz = sizeof buffer;
2638  ok(r, "HttpQueryInfo failed\n");
2639  ok(!strcmp(buffer, "HTTP/1.1"), "http version wrong\n");
2640 
2641  sz = sizeof buffer;
2643  ok(r, "HttpQueryInfo failed\n");
2644  ok(!strcmp(buffer, "winetest"), "http server wrong\n");
2645 
2646  sz = sizeof buffer;
2648  ok(GetLastError() == ERROR_HTTP_HEADER_NOT_FOUND, "HttpQueryInfo should fail\n");
2649  ok(r == FALSE, "HttpQueryInfo failed\n");
2650 
2651 out:
2652  close_request(&req);
2653 }
2654 
2655 static void test_proxy_direct(int port)
2656 {
2657  HINTERNET hi, hc, hr;
2658  DWORD r, sz, error;
2659  char buffer[0x40], *url;
2660  WCHAR bufferW[0x40];
2661  static const char url_fmt[] = "http://test.winehq.org:%u/test2";
2662  static CHAR username[] = "mike",
2663  password[] = "1101",
2664  useragent[] = "winetest";
2665  static const WCHAR usernameW[] = {'m','i','k','e',0},
2666  passwordW[] = {'1','1','0','1',0},
2667  useragentW[] = {'w','i','n','e','t','e','s','t',0};
2668 
2669  /* specify proxy type without the proxy and bypass */
2670  SetLastError(0xdeadbeef);
2672  error = GetLastError();
2674  broken(error == ERROR_SUCCESS) /* WinXPProSP2 */, "got %u\n", error);
2675  ok(hi == NULL || broken(!!hi) /* WinXPProSP2 */, "open should have failed\n");
2676 
2677  sprintf(buffer, "localhost:%d\n", port);
2679  ok(hi != NULL, "open failed\n");
2680 
2681  /* try connect without authorization */
2682  hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
2683  ok(hc != NULL, "connect failed\n");
2684 
2685  hr = HttpOpenRequestA(hc, NULL, "/test2", NULL, NULL, NULL, 0, 0);
2686  ok(hr != NULL, "HttpOpenRequest failed\n");
2687 
2688  sz = 0;
2689  SetLastError(0xdeadbeef);
2692  ok(!r, "unexpected success\n");
2693  ok(sz == 1, "got %u\n", sz);
2694 
2695  sz = 0;
2696  SetLastError(0xdeadbeef);
2699  ok(!r, "unexpected success\n");
2700  ok(sz == 1, "got %u\n", sz);
2701 
2702  sz = sizeof(buffer);
2703  SetLastError(0xdeadbeef);
2705  ok(r, "unexpected failure %u\n", GetLastError());
2706  ok(!sz, "got %u\n", sz);
2707 
2708  sz = sizeof(buffer);
2709  SetLastError(0xdeadbeef);
2711  ok(r, "unexpected failure %u\n", GetLastError());
2712  ok(!sz, "got %u\n", sz);
2713 
2714  sz = 0;
2715  SetLastError(0xdeadbeef);
2718  ok(!r, "unexpected success\n");
2719  ok(sz == 1, "got %u\n", sz);
2720 
2721  sz = 0;
2722  SetLastError(0xdeadbeef);
2725  ok(!r, "unexpected success\n");
2726  ok(sz == 1, "got %u\n", sz);
2727 
2728  sz = sizeof(buffer);
2729  SetLastError(0xdeadbeef);
2731  ok(r, "unexpected failure %u\n", GetLastError());
2732  ok(!sz, "got %u\n", sz);
2733 
2734  sz = sizeof(buffer);
2735  SetLastError(0xdeadbeef);
2737  ok(r, "unexpected failure %u\n", GetLastError());
2738  ok(!sz, "got %u\n", sz);
2739 
2740  sz = 0;
2741  SetLastError(0xdeadbeef);
2744  ok(!r, "unexpected success\n");
2745  ok(sz == 34, "got %u\n", sz);
2746 
2747  sz = sizeof(buffer);
2748  SetLastError(0xdeadbeef);
2750  ok(r, "unexpected failure %u\n", GetLastError());
2751  ok(sz == 33, "got %u\n", sz);
2752 
2753  r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
2754  ok(r || broken(!r), "HttpSendRequest failed %u\n", GetLastError());
2755  if (!r)
2756  {
2757  win_skip("skipping proxy tests on broken wininet\n");
2758  goto done;
2759  }
2760 
2761  test_status_code(hr, 407);
2762 
2763  /* set the user + password then try again */
2765  ok(!r, "unexpected success\n");
2766 
2768  ok(r, "failed to set user\n");
2769 
2771  ok(r, "failed to set user\n");
2772 
2773  buffer[0] = 0;
2774  sz = 3;
2775  SetLastError(0xdeadbeef);
2777  ok(!r, "unexpected failure %u\n", GetLastError());
2778  ok(!buffer[0], "got %s\n", buffer);
2779  ok(sz == strlen(username) + 1, "got %u\n", sz);
2780 
2781  buffer[0] = 0;
2782  sz = 0;
2783  SetLastError(0xdeadbeef);
2786  ok(!r, "unexpected success\n");
2787  ok(sz == strlen(username) + 1, "got %u\n", sz);
2788 
2789  bufferW[0] = 0;
2790  sz = 0;
2791  SetLastError(0xdeadbeef);
2794  ok(!r, "unexpected success\n");
2795  ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2796 
2797  buffer[0] = 0;
2798  sz = sizeof(buffer);
2800  ok(r, "failed to get username\n");
2801  ok(!strcmp(buffer, username), "got %s\n", buffer);
2802  ok(sz == strlen(username), "got %u\n", sz);
2803 
2804  buffer[0] = 0;
2805  sz = sizeof(bufferW);
2807  ok(r, "failed to get username\n");
2808  ok(!lstrcmpW(bufferW, usernameW), "wrong username\n");
2809  ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2810 
2812  ok(r, "failed to set user\n");
2813 
2814  buffer[0] = 0;
2815  sz = sizeof(buffer);
2817  ok(r, "failed to get username\n");
2818  ok(!strcmp(buffer, username), "got %s\n", buffer);
2819  ok(sz == strlen(username), "got %u\n", sz);
2820 
2821  r = InternetSetOptionA(hi, INTERNET_OPTION_USER_AGENT, useragent, 1);
2822  ok(r, "failed to set useragent\n");
2823 
2824  buffer[0] = 0;
2825  sz = 0;
2826  SetLastError(0xdeadbeef);
2829  ok(!r, "unexpected success\n");
2830  ok(sz == strlen(useragent) + 1, "got %u\n", sz);
2831 
2832  buffer[0] = 0;
2833  sz = sizeof(buffer);
2835  ok(r, "failed to get user agent\n");
2836  ok(!strcmp(buffer, useragent), "got %s\n", buffer);
2837  ok(sz == strlen(useragent), "got %u\n", sz);
2838 
2839  bufferW[0] = 0;
2840  sz = 0;
2841  SetLastError(0xdeadbeef);
2842  r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2844  ok(!r, "unexpected success\n");
2845  ok(sz == (lstrlenW(useragentW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2846 
2847  bufferW[0] = 0;
2848  sz = sizeof(bufferW);
2849  r = InternetQueryOptionW(hi, INTERNET_OPTION_USER_AGENT, bufferW, &sz);
2850  ok(r, "failed to get user agent\n");
2851  ok(!lstrcmpW(bufferW, useragentW), "wrong user agent\n");
2852  ok(sz == lstrlenW(useragentW), "got %u\n", sz);
2853 
2855  ok(r, "failed to set user\n");
2856 
2857  buffer[0] = 0;
2858  sz = 0;
2859  SetLastError(0xdeadbeef);
2862  ok(!r, "unexpected success\n");
2863  ok(sz == strlen(username) + 1, "got %u\n", sz);
2864 
2865  buffer[0] = 0;
2866  sz = sizeof(buffer);
2868  ok(r, "failed to get user\n");
2869  ok(!strcmp(buffer, username), "got %s\n", buffer);
2870  ok(sz == strlen(username), "got %u\n", sz);
2871 
2872  bufferW[0] = 0;
2873  sz = 0;
2874  SetLastError(0xdeadbeef);
2877  ok(!r, "unexpected success\n");
2878  ok(sz == (lstrlenW(usernameW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2879 
2880  bufferW[0] = 0;
2881  sz = sizeof(bufferW);
2883  ok(r, "failed to get user\n");
2884  ok(!lstrcmpW(bufferW, usernameW), "wrong user\n");
2885  ok(sz == lstrlenW(usernameW), "got %u\n", sz);
2886 
2888  ok(r, "failed to set password\n");
2889 
2890  buffer[0] = 0;
2891  sz = 0;
2892  SetLastError(0xdeadbeef);
2895  ok(!r, "unexpected success\n");
2896  ok(sz == strlen(password) + 1, "got %u\n", sz);
2897 
2898  buffer[0] = 0;
2899  sz = sizeof(buffer);
2901  ok(r, "failed to get password\n");
2902  ok(!strcmp(buffer, password), "got %s\n", buffer);
2903  ok(sz == strlen(password), "got %u\n", sz);
2904 
2905  bufferW[0] = 0;
2906  sz = 0;
2907  SetLastError(0xdeadbeef);
2910  ok(!r, "unexpected success\n");
2911  ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2912 
2913  bufferW[0] = 0;
2914  sz = sizeof(bufferW);
2916  ok(r, "failed to get password\n");
2917  ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2918  ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2919 
2920  url = HeapAlloc(GetProcessHeap(), 0, strlen(url_fmt) + 11);
2921  sprintf(url, url_fmt, port);
2922  buffer[0] = 0;
2923  sz = 0;
2924  SetLastError(0xdeadbeef);
2927  ok(!r, "unexpected success\n");
2928  ok(sz == strlen(url) + 1, "got %u\n", sz);
2929 
2930  buffer[0] = 0;
2931  sz = sizeof(buffer);
2933  ok(r, "failed to get url\n");
2934  ok(!strcmp(buffer, url), "got %s\n", buffer);
2935  ok(sz == strlen(url), "got %u\n", sz);
2936 
2937  bufferW[0] = 0;
2938  sz = 0;
2939  SetLastError(0xdeadbeef);
2940  r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2942  ok(!r, "unexpected success\n");
2943  ok(sz == (strlen(url) + 1) * sizeof(WCHAR), "got %u\n", sz);
2944 
2945  bufferW[0] = 0;
2946  sz = sizeof(bufferW);
2947  r = InternetQueryOptionW(hr, INTERNET_OPTION_URL, bufferW, &sz);
2948  ok(r, "failed to get url\n");
2949  ok(!strcmp_wa(bufferW, url), "wrong url\n");
2950  ok(sz == strlen(url), "got %u\n", sz);
2951  HeapFree(GetProcessHeap(), 0, url);
2952 
2954  ok(r, "failed to set password\n");
2955 
2956  buffer[0] = 0;
2957  sz = 0;
2958  SetLastError(0xdeadbeef);
2961  ok(!r, "unexpected success\n");
2962  ok(sz == strlen(password) + 1, "got %u\n", sz);
2963 
2964  buffer[0] = 0;
2965  sz = sizeof(buffer);
2967  ok(r, "failed to get password\n");
2968  ok(!strcmp(buffer, password), "got %s\n", buffer);
2969  ok(sz == strlen(password), "got %u\n", sz);
2970 
2971  bufferW[0] = 0;
2972  sz = 0;
2973  SetLastError(0xdeadbeef);
2976  ok(!r, "unexpected success\n");
2977  ok(sz == (lstrlenW(passwordW) + 1) * sizeof(WCHAR), "got %u\n", sz);
2978 
2979  bufferW[0] = 0;
2980  sz = sizeof(bufferW);
2982  ok(r, "failed to get password\n");
2983  ok(!lstrcmpW(bufferW, passwordW), "wrong password\n");
2984  ok(sz == lstrlenW(passwordW), "got %u\n", sz);
2985 
2986  r = HttpSendRequestW(hr, NULL, 0, NULL, 0);
2987  if (!r)
2988  {
2989  win_skip("skipping proxy tests on broken wininet\n");
2990  goto done;
2991  }
2992  ok(r, "HttpSendRequest failed %u\n", GetLastError());
2993  sz = sizeof buffer;
2995  ok(r, "HttpQueryInfo failed\n");
2996  ok(!strcmp(buffer, "200"), "proxy code wrong\n");
2997 
2999  InternetCloseHandle(hc);
3000  InternetCloseHandle(hi);
3001 
3002  sprintf(buffer, "localhost:%d\n", port);
3003  hi = InternetOpenA("winetest", INTERNET_OPEN_TYPE_PROXY, buffer, NULL, 0);
3004  ok(hi != NULL, "InternetOpen failed\n");
3005 
3006  hc = InternetConnectA(hi, "test.winehq.org", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3007  ok(hc != NULL, "InternetConnect failed\n");
3008 
3009  hr = HttpOpenRequestA(hc, "POST", "/test2", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0);
3010  ok(hr != NULL, "HttpOpenRequest failed\n");
3011 
3012  r = HttpSendRequestA(hr, NULL, 0, (char *)"data", sizeof("data"));
3013  ok(r, "HttpSendRequest failed %u\n", GetLastError());
3014 
3015  test_status_code(hr, 407);
3016 
3017 done:
3019  InternetCloseHandle(hc);
3020  InternetCloseHandle(hi);
3021 }
3022 
3024 {
3025  static const char authorization[] = "Authorization: Basic dXNlcjpwd2Q=";
3026  static const char connection[] = "Connection: Close";
3027  static const char *types[2] = { "*", NULL };
3028  char data[32];
3030  DWORD size, status, data_len;
3031  BOOL ret;
3032 
3034  ok(session != NULL, "InternetOpen failed\n");
3035 
3037  ok(connect != NULL, "InternetConnect failed\n");
3038 
3040  ok(request != NULL, "HttpOpenRequest failed\n");
3041 
3043  ok(ret, "HttpAddRequestHeaders failed\n");
3044 
3045  ret = HttpSendRequestA(request, NULL, 0, NULL, 0);
3046  ok(ret, "HttpSendRequest failed\n");
3047 
3048  test_status_code(request, 200);
3050 
3052 
3054  ok(request != NULL, "HttpOpenRequest failed\n");
3055 
3056  ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
3057  ok(ret, "HttpSendRequest failed\n");
3058 
3059  status = 0;
3060  size = sizeof(status);
3062  ok(ret, "HttpQueryInfo failed\n");
3063  ok(status == 200 || status == 400 /* IE6 */, "got status %u, expected 200 or 400\n", status);
3064 
3067 
3069  ok(connect != NULL, "InternetConnect failed\n");
3070 
3072  ok(request != NULL, "HttpOpenRequest failed\n");
3073 
3074  ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3075  ok(ret, "HttpAddRequestHeaders failed\n");
3076 
3077  ret = HttpSendRequestA(request, connection, ~0u, NULL, 0);
3078  ok(ret, "HttpSendRequest failed\n");
3079 
3080  status = 0;
3081  size = sizeof(status);
3083  ok(ret, "HttpQueryInfo failed\n");
3084  ok(status == 200, "got status %u, expected 200\n", status);
3085 
3088 
3090  ok(connect != NULL, "InternetConnect failed\n");
3091 
3093  ok(request != NULL, "HttpOpenRequest failed\n");
3094 
3095  ret = HttpAddRequestHeadersA(request, "Content-Length: 100\r\n", ~0u, HTTP_ADDREQ_FLAG_ADD_IF_NEW);
3096  ok(ret, "HttpAddRequestHeaders failed\n");
3097 
3098  data_len = sizeof(data);
3099  memset(data, 'a', sizeof(data));
3100  ret = HttpSendRequestA(request, NULL, 0, data, data_len);
3101  ok(ret, "HttpSendRequest failed: %u\n", GetLastError());
3102 
3103  status = 0;
3104  size = sizeof(status);
3106  ok(ret, "HttpQueryInfo failed\n");
3107  ok(status == 200, "got status %u, expected 200\n", status);
3108 
3112 }
3113 
3115 {
3116  HINTERNET ses, con, req;
3117  BOOL ret;
3118 
3119  ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3120  ok(ses != NULL, "InternetOpen failed\n");
3121 
3122  con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3123  ok(con != NULL, "InternetConnect failed\n");
3124 
3125  req = HttpOpenRequestA(con, NULL, "/test8", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3126  ok(req != NULL, "HttpOpenRequest failed\n");
3127 
3128  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3129  ok(ret, "HttpSendRequest failed\n");
3130 
3131  test_status_code(req, 200);
3132 
3133  InternetCloseHandle(req);
3134 
3135  req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, 0, 0);
3136  ok(req != NULL, "HttpOpenRequest failed\n");
3137 
3138  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3139  ok(ret, "HttpSendRequest failed\n");
3140 
3141  test_status_code(req, 200);
3142 
3143  InternetCloseHandle(req);
3144 
3145  req = HttpOpenRequestA(con, NULL, "/test9", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3146  ok(req != NULL, "HttpOpenRequest failed\n");
3147 
3148  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3149  ok(ret, "HttpSendRequest failed\n");
3150 
3151  test_status_code(req, 200);
3152 
3153  InternetCloseHandle(req);
3154 
3155  req = HttpOpenRequestA(con, "POST", "/testA", NULL, NULL, NULL, INTERNET_FLAG_NO_CACHE_WRITE, 0);
3156  ok(req != NULL, "HttpOpenRequest failed\n");
3157 
3158  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3159  ok(ret, "HttpSendRequest failed\n");
3160 
3161  test_status_code(req, 200);
3162 
3163  InternetCloseHandle(req);
3164  InternetCloseHandle(con);
3165  InternetCloseHandle(ses);
3166 }
3167 
3168 static void test_header_override(int port)
3169 {
3170  char buffer[128], host_header_override[30], full_url[128];
3171  HINTERNET ses, con, req;
3172  DWORD size, count, err;
3173  BOOL ret;
3174 
3175  sprintf(host_header_override, "Host: test.local:%d\r\n", port);
3176  sprintf(full_url, "http://localhost:%d/test_host_override", port);
3177 
3178  ses = InternetOpenA("winetest", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
3179  ok(ses != NULL, "InternetOpen failed\n");
3180 
3181  con = InternetConnectA(ses, "localhost", port, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
3182  ok(con != NULL, "InternetConnect failed\n");
3183 
3184  req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3185  ok(req != NULL, "HttpOpenRequest failed\n");
3186 
3187  size = sizeof(buffer) - 1;
3188  count = 0;
3189  memset(buffer, 0, sizeof(buffer));
3191  err = GetLastError();
3192  ok(!ret, "HttpQueryInfo succeeded\n");
3193  ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %d\n", err);
3194 
3195  test_request_url(req, full_url);
3196 
3197  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3198  ok(ret, "HttpAddRequestHeaders failed\n");
3199 
3200  size = sizeof(buffer) - 1;
3201  count = 0;
3202  memset(buffer, 0, sizeof(buffer));
3204  ok(ret, "HttpQueryInfo failed\n");
3205 
3206  test_request_url(req, full_url);
3207 
3208  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3209  ok(ret, "HttpSendRequest failed\n");
3210 
3211  test_status_code(req, 200);
3212 
3213  InternetCloseHandle(req);
3214  req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3215  ok(req != NULL, "HttpOpenRequest failed\n");
3216 
3217  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3218  ok(ret, "HttpAddRequestHeaders failed\n");
3219 
3220  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_COALESCE);
3221  ok(ret, "HttpAddRequestHeaders failed\n");
3222 
3223  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3224  ok(ret, "HttpSendRequest failed\n");
3225 
3226  test_status_code(req, 400);
3227 
3228  InternetCloseHandle(req);
3229  req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3230  ok(req != NULL, "HttpOpenRequest failed\n");
3231 
3232  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3233  ok(ret, "HttpAddRequestHeaders failed\n");
3234 
3235  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3236  ok(ret, "HttpSendRequest failed\n");
3237 
3238  test_status_code(req, 200);
3239 
3240  InternetCloseHandle(req);
3241  req = HttpOpenRequestA(con, NULL, "/test_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3242  ok(req != NULL, "HttpOpenRequest failed\n");
3243 
3244  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_REPLACE);
3245  if(ret) { /* win10 returns success */
3246  trace("replacing host header is supported.\n");
3247 
3248  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3249  ok(ret, "HttpSendRequest failed\n");
3250 
3251  test_status_code(req, 200);
3252  }else {
3253  trace("replacing host header is not supported.\n");
3254 
3255  err = GetLastError();
3256  ok(err == ERROR_HTTP_HEADER_NOT_FOUND, "Expected error ERROR_HTTP_HEADER_NOT_FOUND, got %d\n", err);
3257 
3258  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3259  ok(ret, "HttpSendRequest failed\n");
3260 
3261  test_status_code(req, 400);
3262  }
3263 
3264  InternetCloseHandle(req);
3265  InternetSetCookieA("http://localhost", "cookie", "biscuit");
3266  req = HttpOpenRequestA(con, NULL, "/testC", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3267  ok(req != NULL, "HttpOpenRequest failed\n");
3268 
3269  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3270  ok(ret, "HttpAddRequestHeaders failed\n");
3271 
3272  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3273  ok(ret, "HttpSendRequest failed\n");
3274 
3275  test_status_code(req, 200);
3276 
3277  InternetCloseHandle(req);
3278  req = HttpOpenRequestA(con, NULL, "/test_cookie_set_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3279  ok(req != NULL, "HttpOpenRequest failed\n");
3280 
3281  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3282  ok(ret, "HttpAddRequestHeaders failed\n");
3283 
3284  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3285  ok(ret, "HttpSendRequest failed\n");
3286 
3287  test_status_code(req, 200);
3288 
3289  InternetCloseHandle(req);
3290  req = HttpOpenRequestA(con, NULL, "/test_cookie_check_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3291  ok(req != NULL, "HttpOpenRequest failed\n");
3292 
3293  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3294  ok(ret, "HttpAddRequestHeaders failed\n");
3295 
3296  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3297  ok(ret, "HttpSendRequest failed\n");
3298 
3299  test_status_code(req, 200);
3300 
3301  InternetCloseHandle(req);
3302  req = HttpOpenRequestA(con, NULL, "/test_cookie_check_host_override", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3303  ok(req != NULL, "HttpOpenRequest failed\n");
3304 
3305  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3306  ok(ret, "HttpSendRequest failed\n");
3307 
3308  test_status_code(req, 200);
3309 
3310  InternetCloseHandle(req);
3311  InternetSetCookieA("http://test.local", "foo", "bar");
3312  req = HttpOpenRequestA(con, NULL, "/test_cookie_check_different_host", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3313  ok(req != NULL, "HttpOpenRequest failed\n");
3314 
3315  ret = HttpSendRequestA(req, NULL, 0, NULL, 0);
3316  ok(ret, "HttpSendRequest failed\n");
3317 
3318  test_status_code(req, 200);
3319 
3320  InternetCloseHandle(req);
3321  req = HttpOpenRequestA(con, NULL, "/test_cookie_check_different_host", NULL, NULL, NULL, INTERNET_FLAG_KEEP_CONNECTION, 0);
3322  ok(req != NULL, "HttpOpenRequest failed\n");
3323 
3324  ret = HttpAddRequestHeadersA(req, host_header_override, ~0u, HTTP_ADDREQ_FLAG_ADD);
3325  ok(ret, "HttpAddRequestHeaders failed\n");
3326 
3327  ret =