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