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