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