Sync with trunk revision 63128.
[reactos.git] / dll / win32 / wininet / internet.h
1 /*
2 * Wininet
3 *
4 * Copyright 1999 Corel Corporation
5 *
6 * Ulrich Czekalla
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 #ifndef _WINE_INTERNET_H_
24 #define _WINE_INTERNET_H_
25
26 #include <wine/config.h>
27
28 #include <assert.h>
29 #include <stdio.h>
30
31 #define _INC_WINDOWS
32 #define COM_NO_WINDOWS_H
33
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
36
37 #include <windef.h>
38 #include <winbase.h>
39 #include <winreg.h>
40 #include <winuser.h>
41 #include <wininet.h>
42 #define NO_SHLWAPI_STREAM
43 #define NO_SHLWAPI_REG
44 #define NO_SHLWAPI_GDI
45 #include <shlwapi.h>
46
47 #include <wine/list.h>
48 #include <wine/debug.h>
49 #include <wine/unicode.h>
50
51 #ifdef HAVE_ARPA_INET_H
52 # include <arpa/inet.h>
53 #endif
54 #ifdef HAVE_NETDB_H
55 # include <netdb.h>
56 #endif
57 #ifdef HAVE_NETINET_IN_H
58 # include <sys/types.h>
59 # include <netinet/in.h>
60 #endif
61 #ifdef HAVE_SYS_IOCTL_H
62 # include <sys/ioctl.h>
63 #endif
64 #ifdef HAVE_SYS_POLL_H
65 # include <sys/poll.h>
66 #endif
67 #ifdef HAVE_SYS_SOCKET_H
68 # include <sys/socket.h>
69 #endif
70 #ifdef HAVE_SYS_TIME_H
71 # include <sys/time.h>
72 #endif
73 #ifdef HAVE_UNISTD_H
74 # include <unistd.h>
75 #endif
76
77 #if defined(__MINGW32__) || defined (_MSC_VER)
78 #include <ws2tcpip.h>
79 #else
80 #define closesocket close
81 #define ioctlsocket ioctl
82 #endif /* __MINGW32__ */
83
84 #include <winineti.h>
85
86 #include "resource.h"
87
88 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
89
90 extern HMODULE WININET_hModule DECLSPEC_HIDDEN;
91
92 #ifndef INET6_ADDRSTRLEN
93 #define INET6_ADDRSTRLEN 46
94 #endif
95
96 typedef struct {
97 WCHAR *name;
98 INTERNET_PORT port;
99 BOOL is_https;
100 struct sockaddr_storage addr;
101 socklen_t addr_len;
102 char addr_str[INET6_ADDRSTRLEN];
103
104 WCHAR *scheme_host_port;
105 const WCHAR *host_port;
106 const WCHAR *canon_host_port;
107
108 LONG ref;
109
110 DWORD security_flags;
111 const CERT_CHAIN_CONTEXT *cert_chain;
112
113 struct list entry;
114 struct list conn_pool;
115 } server_t;
116
117 void server_addref(server_t*) DECLSPEC_HIDDEN;
118 void server_release(server_t*) DECLSPEC_HIDDEN;
119
120 typedef enum {
121 COLLECT_TIMEOUT,
122 COLLECT_CONNECTIONS,
123 COLLECT_CLEANUP
124 } collect_type_t;
125 BOOL collect_connections(collect_type_t) DECLSPEC_HIDDEN;
126
127 /* used for netconnection.c stuff */
128 typedef struct
129 {
130 int socket;
131 BOOL secure;
132 CtxtHandle ssl_ctx;
133 SecPkgContext_StreamSizes ssl_sizes;
134 server_t *server;
135 char *ssl_buf;
136 char *extra_buf;
137 size_t extra_len;
138 char *peek_msg;
139 char *peek_msg_mem;
140 size_t peek_len;
141 DWORD security_flags;
142 BOOL mask_errors;
143
144 BOOL keep_alive;
145 DWORD64 keep_until;
146 struct list pool_entry;
147 } netconn_t;
148
149 BOOL is_valid_netconn(netconn_t *) DECLSPEC_HIDDEN;
150 void close_netconn(netconn_t *) DECLSPEC_HIDDEN;
151
152 static inline void * __WINE_ALLOC_SIZE(1) heap_alloc(size_t len)
153 {
154 return HeapAlloc(GetProcessHeap(), 0, len);
155 }
156
157 static inline void * __WINE_ALLOC_SIZE(1) heap_alloc_zero(size_t len)
158 {
159 return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
160 }
161
162 static inline void * __WINE_ALLOC_SIZE(2) heap_realloc(void *mem, size_t len)
163 {
164 return HeapReAlloc(GetProcessHeap(), 0, mem, len);
165 }
166
167 static inline void * __WINE_ALLOC_SIZE(2) heap_realloc_zero(void *mem, size_t len)
168 {
169 return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, len);
170 }
171
172 static inline BOOL heap_free(void *mem)
173 {
174 return HeapFree(GetProcessHeap(), 0, mem);
175 }
176
177 static inline LPWSTR heap_strdupW(LPCWSTR str)
178 {
179 LPWSTR ret = NULL;
180
181 if(str) {
182 DWORD size;
183
184 size = (strlenW(str)+1)*sizeof(WCHAR);
185 ret = heap_alloc(size);
186 if(ret)
187 memcpy(ret, str, size);
188 }
189
190 return ret;
191 }
192
193 static inline char *heap_strdupA(const char *str)
194 {
195 char *ret = NULL;
196
197 if(str) {
198 DWORD size = strlen(str)+1;
199
200 ret = heap_alloc(size);
201 if(ret)
202 memcpy(ret, str, size);
203 }
204
205 return ret;
206 }
207
208 static inline LPWSTR heap_strndupW(LPCWSTR str, UINT max_len)
209 {
210 LPWSTR ret;
211 UINT len;
212
213 if(!str)
214 return NULL;
215
216 for(len=0; len<max_len; len++)
217 if(str[len] == '\0')
218 break;
219
220 ret = heap_alloc(sizeof(WCHAR)*(len+1));
221 if(ret) {
222 memcpy(ret, str, sizeof(WCHAR)*len);
223 ret[len] = '\0';
224 }
225
226 return ret;
227 }
228
229 static inline WCHAR *heap_strdupAtoW(const char *str)
230 {
231 LPWSTR ret = NULL;
232
233 if(str) {
234 DWORD len;
235
236 len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
237 ret = heap_alloc(len*sizeof(WCHAR));
238 if(ret)
239 MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
240 }
241
242 return ret;
243 }
244
245 static inline char *heap_strdupWtoA(LPCWSTR str)
246 {
247 char *ret = NULL;
248
249 if(str) {
250 DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
251 ret = heap_alloc(size);
252 if(ret)
253 WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL);
254 }
255
256 return ret;
257 }
258
259 static inline void WININET_find_data_WtoA(LPWIN32_FIND_DATAW dataW, LPWIN32_FIND_DATAA dataA)
260 {
261 dataA->dwFileAttributes = dataW->dwFileAttributes;
262 dataA->ftCreationTime = dataW->ftCreationTime;
263 dataA->ftLastAccessTime = dataW->ftLastAccessTime;
264 dataA->ftLastWriteTime = dataW->ftLastWriteTime;
265 dataA->nFileSizeHigh = dataW->nFileSizeHigh;
266 dataA->nFileSizeLow = dataW->nFileSizeLow;
267 dataA->dwReserved0 = dataW->dwReserved0;
268 dataA->dwReserved1 = dataW->dwReserved1;
269 WideCharToMultiByte(CP_ACP, 0, dataW->cFileName, -1,
270 dataA->cFileName, sizeof(dataA->cFileName),
271 NULL, NULL);
272 WideCharToMultiByte(CP_ACP, 0, dataW->cAlternateFileName, -1,
273 dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName),
274 NULL, NULL);
275 }
276
277 typedef enum
278 {
279 WH_HINIT = INTERNET_HANDLE_TYPE_INTERNET,
280 WH_HFTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_FTP,
281 WH_HGOPHERSESSION = INTERNET_HANDLE_TYPE_CONNECT_GOPHER,
282 WH_HHTTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_HTTP,
283 WH_HFILE = INTERNET_HANDLE_TYPE_FTP_FILE,
284 WH_HFTPFINDNEXT = INTERNET_HANDLE_TYPE_FTP_FIND,
285 WH_HHTTPREQ = INTERNET_HANDLE_TYPE_HTTP_REQUEST,
286 } WH_TYPE;
287
288 #define INET_OPENURL 0x0001
289 #define INET_CALLBACKW 0x0002
290
291 typedef struct
292 {
293 LONG ref;
294 HANDLE file_handle;
295 WCHAR *file_name;
296 BOOL is_committed;
297 } req_file_t;
298
299 typedef struct _object_header_t object_header_t;
300
301 typedef struct {
302 void (*Destroy)(object_header_t*);
303 void (*CloseConnection)(object_header_t*);
304 DWORD (*QueryOption)(object_header_t*,DWORD,void*,DWORD*,BOOL);
305 DWORD (*SetOption)(object_header_t*,DWORD,void*,DWORD);
306 DWORD (*ReadFile)(object_header_t*,void*,DWORD,DWORD*);
307 DWORD (*ReadFileEx)(object_header_t*,void*,DWORD,DWORD*,DWORD,DWORD_PTR);
308 DWORD (*WriteFile)(object_header_t*,const void*,DWORD,DWORD*);
309 DWORD (*QueryDataAvailable)(object_header_t*,DWORD*,DWORD,DWORD_PTR);
310 DWORD (*FindNextFileW)(object_header_t*,void*);
311 DWORD (*LockRequestFile)(object_header_t*,req_file_t**);
312 } object_vtbl_t;
313
314 #define INTERNET_HANDLE_IN_USE 1
315
316 struct _object_header_t
317 {
318 WH_TYPE htype;
319 const object_vtbl_t *vtbl;
320 HINTERNET hInternet;
321 BOOL valid_handle;
322 DWORD dwFlags;
323 DWORD_PTR dwContext;
324 DWORD dwError;
325 ULONG ErrorMask;
326 DWORD dwInternalFlags;
327 LONG refs;
328 INTERNET_STATUS_CALLBACK lpfnStatusCB;
329 struct list entry;
330 struct list children;
331 };
332
333 typedef struct
334 {
335 object_header_t hdr;
336 LPWSTR agent;
337 LPWSTR proxy;
338 LPWSTR proxyBypass;
339 LPWSTR proxyUsername;
340 LPWSTR proxyPassword;
341 DWORD accessType;
342 DWORD connect_timeout;
343 } appinfo_t;
344
345 typedef struct
346 {
347 object_header_t hdr;
348 appinfo_t *appInfo;
349 LPWSTR hostName; /* the final destination of the request */
350 LPWSTR userName;
351 LPWSTR password;
352 INTERNET_PORT hostPort; /* the final destination port of the request */
353 DWORD connect_timeout;
354 DWORD send_timeout;
355 DWORD receive_timeout;
356 } http_session_t;
357
358 #define HDR_ISREQUEST 0x0001
359 #define HDR_COMMADELIMITED 0x0002
360 #define HDR_SEMIDELIMITED 0x0004
361
362 typedef struct
363 {
364 LPWSTR lpszField;
365 LPWSTR lpszValue;
366 WORD wFlags;
367 WORD wCount;
368 } HTTPHEADERW, *LPHTTPHEADERW;
369
370
371 struct HttpAuthInfo;
372
373 typedef struct data_stream_vtbl_t data_stream_vtbl_t;
374
375 typedef struct {
376 const data_stream_vtbl_t *vtbl;
377 } data_stream_t;
378
379 typedef struct {
380 data_stream_t data_stream;
381 DWORD content_length;
382 DWORD content_read;
383 } netconn_stream_t;
384
385 #define READ_BUFFER_SIZE 8192
386
387 typedef struct
388 {
389 object_header_t hdr;
390 http_session_t *session;
391 server_t *server;
392 server_t *proxy;
393 LPWSTR path;
394 LPWSTR verb;
395 netconn_t *netconn;
396 DWORD security_flags;
397 DWORD connect_timeout;
398 DWORD send_timeout;
399 DWORD receive_timeout;
400 LPWSTR version;
401 DWORD status_code;
402 LPWSTR statusText;
403 DWORD bytesToWrite;
404 DWORD bytesWritten;
405 HTTPHEADERW *custHeaders;
406 DWORD nCustHeaders;
407 FILETIME last_modified;
408 HANDLE hCacheFile;
409 req_file_t *req_file;
410 FILETIME expires;
411 struct HttpAuthInfo *authInfo;
412 struct HttpAuthInfo *proxyAuthInfo;
413
414 CRITICAL_SECTION read_section; /* section to protect the following fields */
415 DWORD contentLength; /* total number of bytes to be read */
416 BOOL read_chunked; /* are we reading in chunked mode? */
417 BOOL read_gzip; /* are we reading in gzip mode? */
418 DWORD read_pos; /* current read position in read_buf */
419 DWORD read_size; /* valid data size in read_buf */
420 BYTE read_buf[READ_BUFFER_SIZE]; /* buffer for already read but not returned data */
421
422 BOOL decoding;
423 data_stream_t *data_stream;
424 netconn_stream_t netconn_stream;
425 } http_request_t;
426
427 typedef struct task_header_t task_header_t;
428 typedef void (*async_task_proc_t)(task_header_t*);
429
430 struct task_header_t
431 {
432 async_task_proc_t proc;
433 object_header_t *hdr;
434 };
435
436 void *alloc_async_task(object_header_t*,async_task_proc_t,size_t) DECLSPEC_HIDDEN;
437
438 void *alloc_object(object_header_t*,const object_vtbl_t*,size_t) DECLSPEC_HIDDEN;
439 object_header_t *get_handle_object( HINTERNET hinternet ) DECLSPEC_HIDDEN;
440 object_header_t *WININET_AddRef( object_header_t *info ) DECLSPEC_HIDDEN;
441 BOOL WININET_Release( object_header_t *info ) DECLSPEC_HIDDEN;
442
443 DWORD INET_QueryOption(object_header_t*,DWORD,void*,DWORD*,BOOL) DECLSPEC_HIDDEN;
444 DWORD INET_SetOption(object_header_t*,DWORD,void*,DWORD) DECLSPEC_HIDDEN;
445
446 time_t ConvertTimeString(LPCWSTR asctime) DECLSPEC_HIDDEN;
447
448 HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
449 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
450 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
451 DWORD dwInternalFlags) DECLSPEC_HIDDEN;
452
453 DWORD HTTP_Connect(appinfo_t*,LPCWSTR,
454 INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
455 LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
456 DWORD dwInternalFlags, HINTERNET*) DECLSPEC_HIDDEN;
457
458 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
459 struct sockaddr *psa, socklen_t *sa_len) DECLSPEC_HIDDEN;
460
461 DWORD get_cookie(const WCHAR*,const WCHAR*,WCHAR*,DWORD*) DECLSPEC_HIDDEN;
462 BOOL set_cookie(const WCHAR*,const WCHAR*,const WCHAR*,const WCHAR*) DECLSPEC_HIDDEN;
463
464 void INTERNET_SetLastError(DWORD dwError) DECLSPEC_HIDDEN;
465 DWORD INTERNET_GetLastError(void) DECLSPEC_HIDDEN;
466 DWORD INTERNET_AsyncCall(task_header_t*) DECLSPEC_HIDDEN;
467 LPSTR INTERNET_GetResponseBuffer(void) DECLSPEC_HIDDEN;
468 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen) DECLSPEC_HIDDEN;
469
470 VOID SendAsyncCallback(object_header_t *hdr, DWORD_PTR dwContext,
471 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
472 DWORD dwStatusInfoLength) DECLSPEC_HIDDEN;
473
474 VOID INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR dwContext,
475 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
476 DWORD dwStatusInfoLength) DECLSPEC_HIDDEN;
477 BOOL INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto, WCHAR *foundProxy, DWORD *foundProxyLen) DECLSPEC_HIDDEN;
478
479 typedef enum {
480 BLOCKING_ALLOW,
481 BLOCKING_DISALLOW,
482 BLOCKING_WAITALL
483 } blocking_mode_t;
484
485 DWORD create_netconn(BOOL,server_t*,DWORD,BOOL,DWORD,netconn_t**) DECLSPEC_HIDDEN;
486 void free_netconn(netconn_t*) DECLSPEC_HIDDEN;
487 void NETCON_unload(void) DECLSPEC_HIDDEN;
488 DWORD NETCON_secure_connect(netconn_t*,server_t*) DECLSPEC_HIDDEN;
489 DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
490 int *sent /* out */) DECLSPEC_HIDDEN;
491 DWORD NETCON_recv(netconn_t*,void*,size_t,blocking_mode_t,int*) DECLSPEC_HIDDEN;
492 BOOL NETCON_query_data_available(netconn_t *connection, DWORD *available) DECLSPEC_HIDDEN;
493 BOOL NETCON_is_alive(netconn_t*) DECLSPEC_HIDDEN;
494 LPCVOID NETCON_GetCert(netconn_t *connection) DECLSPEC_HIDDEN;
495 int NETCON_GetCipherStrength(netconn_t*) DECLSPEC_HIDDEN;
496 DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, DWORD value) DECLSPEC_HIDDEN;
497 #ifndef __REACTOS__
498 int sock_get_error(int) DECLSPEC_HIDDEN;
499 #else
500 #define sock_get_error(x) WSAGetLastError()
501 const char *inet_ntop(int, const void *, char *, socklen_t);
502
503 static inline long unix_recv(int socket, void *buffer, size_t length, int flags)
504 {
505 return recv(socket, buffer, length, flags);
506 }
507 #define recv unix_recv
508
509 static inline int unix_ioctl(int filedes, long request, void *arg)
510 {
511 return ioctlsocket(filedes, request, arg);
512 }
513 #define ioctlsocket unix_ioctl
514
515 static inline int unix_getsockopt(int socket, int level, int option_name, void *option_value, socklen_t *option_len)
516 {
517 return getsockopt(socket, level, option_name, option_value, option_len);
518 }
519 #define getsockopt unix_getsockopt
520 #endif
521
522 server_t *get_server(const WCHAR*,INTERNET_PORT,BOOL,BOOL);
523
524 DWORD create_req_file(const WCHAR*,req_file_t**) DECLSPEC_HIDDEN;
525 void req_file_release(req_file_t*) DECLSPEC_HIDDEN;
526
527 static inline req_file_t *req_file_addref(req_file_t *req_file)
528 {
529 InterlockedIncrement(&req_file->ref);
530 return req_file;
531 }
532
533 BOOL init_urlcache(void) DECLSPEC_HIDDEN;
534 void free_urlcache(void) DECLSPEC_HIDDEN;
535 void free_cookie(void) DECLSPEC_HIDDEN;
536
537 #define MAX_REPLY_LEN 0x5B4
538
539 /* Used for debugging - maybe need to be shared in the Wine debugging code ? */
540 typedef struct
541 {
542 DWORD val;
543 const char* name;
544 } wininet_flag_info;
545
546 /* Undocumented security flags */
547 #define _SECURITY_FLAG_CERT_REV_FAILED 0x00800000
548 #define _SECURITY_FLAG_CERT_INVALID_CA 0x01000000
549 #define _SECURITY_FLAG_CERT_INVALID_CN 0x02000000
550 #define _SECURITY_FLAG_CERT_INVALID_DATE 0x04000000
551
552 #define _SECURITY_ERROR_FLAGS_MASK \
553 (_SECURITY_FLAG_CERT_REV_FAILED \
554 |_SECURITY_FLAG_CERT_INVALID_CA \
555 |_SECURITY_FLAG_CERT_INVALID_CN \
556 |_SECURITY_FLAG_CERT_INVALID_DATE)
557
558 #endif /* _WINE_INTERNET_H_ */