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