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