wininet/tests: Add tests for a prematurely closed connection.
[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     int socket;
93     BOOL secure;
94     CtxtHandle ssl_ctx;
95     SecPkgContext_StreamSizes ssl_sizes;
96     server_t *server;
97     char *ssl_buf;
98     char *extra_buf;
99     size_t extra_len;
100     char *peek_msg;
101     char *peek_msg_mem;
102     size_t peek_len;
103     DWORD security_flags;
104     BOOL mask_errors;
105
106     BOOL keep_alive;
107     DWORD64 keep_until;
108     struct list pool_entry;
109 } netconn_t;
110
111 static inline void * __WINE_ALLOC_SIZE(1) heap_alloc(size_t len)
112 {
113     return HeapAlloc(GetProcessHeap(), 0, len);
114 }
115
116 static inline void * __WINE_ALLOC_SIZE(1) heap_alloc_zero(size_t len)
117 {
118     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
119 }
120
121 static inline void * __WINE_ALLOC_SIZE(2) heap_realloc(void *mem, size_t len)
122 {
123     return HeapReAlloc(GetProcessHeap(), 0, mem, len);
124 }
125
126 static inline void * __WINE_ALLOC_SIZE(2) heap_realloc_zero(void *mem, size_t len)
127 {
128     return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, len);
129 }
130
131 static inline BOOL heap_free(void *mem)
132 {
133     return HeapFree(GetProcessHeap(), 0, mem);
134 }
135
136 static inline LPWSTR heap_strdupW(LPCWSTR str)
137 {
138     LPWSTR ret = NULL;
139
140     if(str) {
141         DWORD size;
142
143         size = (strlenW(str)+1)*sizeof(WCHAR);
144         ret = heap_alloc(size);
145         if(ret)
146             memcpy(ret, str, size);
147     }
148
149     return ret;
150 }
151
152 static inline char *heap_strdupA(const char *str)
153 {
154     char *ret = NULL;
155
156     if(str) {
157         DWORD size = strlen(str)+1;
158
159         ret = heap_alloc(size);
160         if(ret)
161             memcpy(ret, str, size);
162     }
163
164     return ret;
165 }
166
167 static inline LPWSTR heap_strndupW(LPCWSTR str, UINT max_len)
168 {
169     LPWSTR ret;
170     UINT len;
171
172     if(!str)
173         return NULL;
174
175     for(len=0; len<max_len; len++)
176         if(str[len] == '\0')
177             break;
178
179     ret = heap_alloc(sizeof(WCHAR)*(len+1));
180     if(ret) {
181         memcpy(ret, str, sizeof(WCHAR)*len);
182         ret[len] = '\0';
183     }
184
185     return ret;
186 }
187
188 static inline WCHAR *heap_strdupAtoW(const char *str)
189 {
190     LPWSTR ret = NULL;
191
192     if(str) {
193         DWORD len;
194
195         len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
196         ret = heap_alloc(len*sizeof(WCHAR));
197         if(ret)
198             MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
199     }
200
201     return ret;
202 }
203
204 static inline char *heap_strdupWtoA(LPCWSTR str)
205 {
206     char *ret = NULL;
207
208     if(str) {
209         DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
210         ret = heap_alloc(size);
211         if(ret)
212             WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL);
213     }
214
215     return ret;
216 }
217
218 static inline void WININET_find_data_WtoA(LPWIN32_FIND_DATAW dataW, LPWIN32_FIND_DATAA dataA)
219 {
220     dataA->dwFileAttributes = dataW->dwFileAttributes;
221     dataA->ftCreationTime   = dataW->ftCreationTime;
222     dataA->ftLastAccessTime = dataW->ftLastAccessTime;
223     dataA->ftLastWriteTime  = dataW->ftLastWriteTime;
224     dataA->nFileSizeHigh    = dataW->nFileSizeHigh;
225     dataA->nFileSizeLow     = dataW->nFileSizeLow;
226     dataA->dwReserved0      = dataW->dwReserved0;
227     dataA->dwReserved1      = dataW->dwReserved1;
228     WideCharToMultiByte(CP_ACP, 0, dataW->cFileName, -1, 
229         dataA->cFileName, sizeof(dataA->cFileName),
230         NULL, NULL);
231     WideCharToMultiByte(CP_ACP, 0, dataW->cAlternateFileName, -1, 
232         dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName),
233         NULL, NULL);
234 }
235
236 typedef enum
237 {
238     WH_HINIT = INTERNET_HANDLE_TYPE_INTERNET,
239     WH_HFTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_FTP,
240     WH_HGOPHERSESSION = INTERNET_HANDLE_TYPE_CONNECT_GOPHER,
241     WH_HHTTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_HTTP,
242     WH_HFILE = INTERNET_HANDLE_TYPE_FTP_FILE,
243     WH_HFTPFINDNEXT = INTERNET_HANDLE_TYPE_FTP_FIND,
244     WH_HHTTPREQ = INTERNET_HANDLE_TYPE_HTTP_REQUEST,
245 } WH_TYPE;
246
247 #define INET_OPENURL 0x0001
248 #define INET_CALLBACKW 0x0002
249
250 typedef struct _object_header_t object_header_t;
251
252 typedef struct {
253     void (*Destroy)(object_header_t*);
254     void (*CloseConnection)(object_header_t*);
255     DWORD (*QueryOption)(object_header_t*,DWORD,void*,DWORD*,BOOL);
256     DWORD (*SetOption)(object_header_t*,DWORD,void*,DWORD);
257     DWORD (*ReadFile)(object_header_t*,void*,DWORD,DWORD*);
258     DWORD (*ReadFileEx)(object_header_t*,void*,DWORD,DWORD*,DWORD,DWORD_PTR);
259     DWORD (*WriteFile)(object_header_t*,const void*,DWORD,DWORD*);
260     DWORD (*QueryDataAvailable)(object_header_t*,DWORD*,DWORD,DWORD_PTR);
261     DWORD (*FindNextFileW)(object_header_t*,void*);
262 } object_vtbl_t;
263
264 #define INTERNET_HANDLE_IN_USE 1
265
266 struct _object_header_t
267 {
268     WH_TYPE htype;
269     const object_vtbl_t *vtbl;
270     HINTERNET hInternet;
271     BOOL valid_handle;
272     DWORD  dwFlags;
273     DWORD_PTR dwContext;
274     DWORD  dwError;
275     ULONG  ErrorMask;
276     DWORD  dwInternalFlags;
277     LONG   refs;
278     INTERNET_STATUS_CALLBACK lpfnStatusCB;
279     struct list entry;
280     struct list children;
281 };
282
283
284 typedef struct
285 {
286     object_header_t hdr;
287     LPWSTR  agent;
288     LPWSTR  proxy;
289     LPWSTR  proxyBypass;
290     LPWSTR  proxyUsername;
291     LPWSTR  proxyPassword;
292     DWORD   accessType;
293     DWORD   connect_timeout;
294 } appinfo_t;
295
296 typedef struct
297 {
298     object_header_t hdr;
299     appinfo_t *appInfo;
300     LPWSTR  hostName; /* the final destination of the request */
301     LPWSTR  userName;
302     LPWSTR  password;
303     INTERNET_PORT hostPort; /* the final destination port of the request */
304     DWORD connect_timeout;
305     DWORD send_timeout;
306     DWORD receive_timeout;
307 } http_session_t;
308
309 #define HDR_ISREQUEST           0x0001
310 #define HDR_COMMADELIMITED      0x0002
311 #define HDR_SEMIDELIMITED       0x0004
312
313 typedef struct
314 {
315     LPWSTR lpszField;
316     LPWSTR lpszValue;
317     WORD wFlags;
318     WORD wCount;
319 } HTTPHEADERW, *LPHTTPHEADERW;
320
321
322 struct HttpAuthInfo;
323
324 typedef struct data_stream_vtbl_t data_stream_vtbl_t;
325
326 typedef struct {
327     const data_stream_vtbl_t *vtbl;
328 }  data_stream_t;
329
330 typedef struct {
331     data_stream_t data_stream;
332     DWORD content_length;
333     DWORD content_read;
334 } netconn_stream_t;
335
336 #define READ_BUFFER_SIZE 8192
337
338 typedef struct
339 {
340     object_header_t hdr;
341     http_session_t *session;
342     server_t *server;
343     server_t *proxy;
344     LPWSTR path;
345     LPWSTR verb;
346     LPWSTR rawHeaders;
347     netconn_t *netconn;
348     DWORD security_flags;
349     DWORD connect_timeout;
350     DWORD send_timeout;
351     DWORD receive_timeout;
352     LPWSTR version;
353     DWORD status_code;
354     LPWSTR statusText;
355     DWORD bytesToWrite;
356     DWORD bytesWritten;
357     HTTPHEADERW *custHeaders;
358     DWORD nCustHeaders;
359     FILETIME last_modified;
360     HANDLE hCacheFile;
361     LPWSTR cacheFile;
362     FILETIME expires;
363     struct HttpAuthInfo *authInfo;
364     struct HttpAuthInfo *proxyAuthInfo;
365
366     CRITICAL_SECTION read_section;  /* section to protect the following fields */
367     DWORD contentLength;  /* total number of bytes to be read */
368     BOOL  read_chunked;   /* are we reading in chunked mode? */
369     BOOL  read_gzip;      /* are we reading in gzip mode? */
370     DWORD read_pos;       /* current read position in read_buf */
371     DWORD read_size;      /* valid data size in read_buf */
372     BYTE  read_buf[READ_BUFFER_SIZE]; /* buffer for already read but not returned data */
373
374     BOOL decoding;
375     data_stream_t *data_stream;
376     netconn_stream_t netconn_stream;
377 } http_request_t;
378
379 typedef struct task_header_t task_header_t;
380 typedef void (*async_task_proc_t)(task_header_t*);
381
382 struct task_header_t
383 {
384     async_task_proc_t proc;
385     object_header_t *hdr;
386 };
387
388 void *alloc_async_task(object_header_t*,async_task_proc_t,size_t) DECLSPEC_HIDDEN;
389
390 void *alloc_object(object_header_t*,const object_vtbl_t*,size_t) DECLSPEC_HIDDEN;
391 object_header_t *get_handle_object( HINTERNET hinternet ) DECLSPEC_HIDDEN;
392 object_header_t *WININET_AddRef( object_header_t *info ) DECLSPEC_HIDDEN;
393 BOOL WININET_Release( object_header_t *info ) DECLSPEC_HIDDEN;
394
395 DWORD INET_QueryOption(object_header_t*,DWORD,void*,DWORD*,BOOL) DECLSPEC_HIDDEN;
396 DWORD INET_SetOption(object_header_t*,DWORD,void*,DWORD) DECLSPEC_HIDDEN;
397
398 time_t ConvertTimeString(LPCWSTR asctime) DECLSPEC_HIDDEN;
399
400 HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
401         INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
402         LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
403         DWORD dwInternalFlags) DECLSPEC_HIDDEN;
404
405 DWORD HTTP_Connect(appinfo_t*,LPCWSTR,
406         INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
407         LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
408         DWORD dwInternalFlags, HINTERNET*) DECLSPEC_HIDDEN;
409
410 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
411         struct sockaddr *psa, socklen_t *sa_len) DECLSPEC_HIDDEN;
412
413 DWORD get_cookie(const WCHAR*,const WCHAR*,WCHAR*,DWORD*) DECLSPEC_HIDDEN;
414 BOOL set_cookie(const WCHAR*,const WCHAR*,const WCHAR*,const WCHAR*) DECLSPEC_HIDDEN;
415
416 void INTERNET_SetLastError(DWORD dwError) DECLSPEC_HIDDEN;
417 DWORD INTERNET_GetLastError(void) DECLSPEC_HIDDEN;
418 DWORD INTERNET_AsyncCall(task_header_t*) DECLSPEC_HIDDEN;
419 LPSTR INTERNET_GetResponseBuffer(void) DECLSPEC_HIDDEN;
420 LPSTR INTERNET_GetNextLine(INT nSocket, LPDWORD dwLen) DECLSPEC_HIDDEN;
421
422 VOID SendAsyncCallback(object_header_t *hdr, DWORD_PTR dwContext,
423                        DWORD dwInternetStatus, LPVOID lpvStatusInfo,
424                        DWORD dwStatusInfoLength) DECLSPEC_HIDDEN;
425
426 VOID INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR dwContext,
427                            DWORD dwInternetStatus, LPVOID lpvStatusInfo,
428                            DWORD dwStatusInfoLength) DECLSPEC_HIDDEN;
429 BOOL INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto, WCHAR *foundProxy, DWORD *foundProxyLen) DECLSPEC_HIDDEN;
430
431 DWORD create_netconn(BOOL,server_t*,DWORD,BOOL,DWORD,netconn_t**) DECLSPEC_HIDDEN;
432 void free_netconn(netconn_t*) DECLSPEC_HIDDEN;
433 void NETCON_unload(void) DECLSPEC_HIDDEN;
434 DWORD NETCON_secure_connect(netconn_t*,server_t*) DECLSPEC_HIDDEN;
435 DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
436                 int *sent /* out */) DECLSPEC_HIDDEN;
437 DWORD NETCON_recv(netconn_t *connection, void *buf, size_t len, int flags,
438                 int *recvd /* out */) DECLSPEC_HIDDEN;
439 BOOL NETCON_query_data_available(netconn_t *connection, DWORD *available) DECLSPEC_HIDDEN;
440 BOOL NETCON_is_alive(netconn_t*) DECLSPEC_HIDDEN;
441 LPCVOID NETCON_GetCert(netconn_t *connection) DECLSPEC_HIDDEN;
442 int NETCON_GetCipherStrength(netconn_t*) DECLSPEC_HIDDEN;
443 DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, DWORD value) DECLSPEC_HIDDEN;
444 int sock_get_error(int) DECLSPEC_HIDDEN;
445
446 server_t *get_server(const WCHAR*,INTERNET_PORT,BOOL,BOOL);
447
448 BOOL init_urlcache(void) DECLSPEC_HIDDEN;
449 void free_urlcache(void) DECLSPEC_HIDDEN;
450 void free_cookie(void) DECLSPEC_HIDDEN;
451
452 #define MAX_REPLY_LEN           0x5B4
453
454 /* Used for debugging - maybe need to be shared in the Wine debugging code ? */
455 typedef struct
456 {
457     DWORD val;
458     const char* name;
459 } wininet_flag_info;
460
461 /* Undocumented security flags */
462 #define _SECURITY_FLAG_CERT_REV_FAILED    0x00800000
463 #define _SECURITY_FLAG_CERT_INVALID_CA    0x01000000
464 #define _SECURITY_FLAG_CERT_INVALID_CN    0x02000000
465 #define _SECURITY_FLAG_CERT_INVALID_DATE  0x04000000
466
467 #define _SECURITY_ERROR_FLAGS_MASK              \
468     (_SECURITY_FLAG_CERT_REV_FAILED             \
469     |_SECURITY_FLAG_CERT_INVALID_CA             \
470     |_SECURITY_FLAG_CERT_INVALID_CN             \
471     |_SECURITY_FLAG_CERT_INVALID_DATE)
472
473 #endif /* _WINE_INTERNET_H_ */