internet.h 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Wininet
 *
 * Copyright 1999 Corel Corporation
 *
 * Ulrich Czekalla
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 24 25
#ifndef _WINE_INTERNET_H_
#define _WINE_INTERNET_H_

26
#include "wine/heap.h"
27
#include "wine/list.h"
28

29
#include <time.h>
30

31 32
#include "winineti.h"

33
extern HMODULE WININET_hModule DECLSPEC_HIDDEN;
34

35 36 37
typedef struct {
    WCHAR *name;
    INTERNET_PORT port;
38
    BOOL is_https;
39
    struct sockaddr_storage addr;
40
    int addr_len;
41 42
    char addr_str[INET6_ADDRSTRLEN];

43 44 45 46
    WCHAR *scheme_host_port;
    const WCHAR *host_port;
    const WCHAR *canon_host_port;

47 48
    LONG ref;

49
    DWORD security_flags;
50
    const CERT_CHAIN_CONTEXT *cert_chain;
51

52 53 54 55
    struct list entry;
    struct list conn_pool;
} server_t;

56 57
void server_addref(server_t*) DECLSPEC_HIDDEN;
void server_release(server_t*) DECLSPEC_HIDDEN;
58 59 60 61 62 63 64

typedef enum {
    COLLECT_TIMEOUT,
    COLLECT_CONNECTIONS,
    COLLECT_CLEANUP
} collect_type_t;
BOOL collect_connections(collect_type_t) DECLSPEC_HIDDEN;
65

66 67 68
/* used for netconnection.c stuff */
typedef struct
{
69
    int socket;
70
    BOOL secure;
71
    BOOL is_blocking;
72 73
    CtxtHandle ssl_ctx;
    SecPkgContext_StreamSizes ssl_sizes;
74
    server_t *server;
75 76 77
    char *ssl_buf;
    char *extra_buf;
    size_t extra_len;
78 79 80
    char *peek_msg;
    char *peek_msg_mem;
    size_t peek_len;
81
    DWORD security_flags;
82
    BOOL mask_errors;
83 84 85 86

    BOOL keep_alive;
    DWORD64 keep_until;
    struct list pool_entry;
87
} netconn_t;
88

89 90 91
BOOL is_valid_netconn(netconn_t *) DECLSPEC_HIDDEN;
void close_netconn(netconn_t *) DECLSPEC_HIDDEN;

92 93 94 95 96
static inline void * __WINE_ALLOC_SIZE(2) heap_realloc_zero(void *mem, size_t len)
{
    return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mem, len);
}

97
static inline LPWSTR heap_strdupW(LPCWSTR str)
98
{
99 100 101 102 103
    LPWSTR ret = NULL;

    if(str) {
        DWORD size;

104
        size = (lstrlenW(str)+1)*sizeof(WCHAR);
105
        ret = heap_alloc(size);
106 107 108 109
        if(ret)
            memcpy(ret, str, size);
    }

110 111 112
    return ret;
}

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
static inline char *heap_strdupA(const char *str)
{
    char *ret = NULL;

    if(str) {
        DWORD size = strlen(str)+1;

        ret = heap_alloc(size);
        if(ret)
            memcpy(ret, str, size);
    }

    return ret;
}

128 129 130 131 132 133 134 135 136 137 138 139
static inline LPWSTR heap_strndupW(LPCWSTR str, UINT max_len)
{
    LPWSTR ret;
    UINT len;

    if(!str)
        return NULL;

    for(len=0; len<max_len; len++)
        if(str[len] == '\0')
            break;

140
    ret = heap_alloc(sizeof(WCHAR)*(len+1));
141 142 143 144 145 146 147 148
    if(ret) {
        memcpy(ret, str, sizeof(WCHAR)*len);
        ret[len] = '\0';
    }

    return ret;
}

149 150 151 152 153 154
static inline WCHAR *heap_strndupAtoW(const char *str, int len_a, DWORD *len_w)
{
    WCHAR *ret = NULL;

    if(str) {
        size_t len;
155 156 157 158
        if(len_a < 0)
            len_a = strlen(str);
        else if(len_a > 0)
            len_a = strnlen(str, len_a);
159 160 161 162 163 164 165 166 167 168 169 170
        len = MultiByteToWideChar(CP_ACP, 0, str, len_a, NULL, 0);
        ret = heap_alloc((len+1)*sizeof(WCHAR));
        if(ret) {
            MultiByteToWideChar(CP_ACP, 0, str, len_a, ret, len);
            ret[len] = 0;
            *len_w = len;
        }
    }

    return ret;
}

171
static inline WCHAR *heap_strdupAtoW(const char *str)
172
{
173 174 175 176 177 178
    LPWSTR ret = NULL;

    if(str) {
        DWORD len;

        len = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, 0);
179
        ret = heap_alloc(len*sizeof(WCHAR));
180 181 182 183
        if(ret)
            MultiByteToWideChar(CP_ACP, 0, str, -1, ret, len);
    }

184 185 186
    return ret;
}

187
static inline char *heap_strdupWtoA(LPCWSTR str)
188
{
189 190 191 192
    char *ret = NULL;

    if(str) {
        DWORD size = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL);
193
        ret = heap_alloc(size);
194 195 196 197
        if(ret)
            WideCharToMultiByte(CP_ACP, 0, str, -1, ret, size, NULL, NULL);
    }

198 199 200
    return ret;
}

201 202 203 204 205 206 207 208 209 210 211 212 213
typedef struct {
    const WCHAR *str;
    size_t len;
} substr_t;

static inline substr_t substr(const WCHAR *str, size_t len)
{
    substr_t r = {str, len};
    return r;
}

static inline substr_t substrz(const WCHAR *str)
{
214
    return substr(str, lstrlenW(str));
215 216
}

217
static inline void WININET_find_data_WtoA(LPWIN32_FIND_DATAW dataW, LPWIN32_FIND_DATAA dataA)
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
{
    dataA->dwFileAttributes = dataW->dwFileAttributes;
    dataA->ftCreationTime   = dataW->ftCreationTime;
    dataA->ftLastAccessTime = dataW->ftLastAccessTime;
    dataA->ftLastWriteTime  = dataW->ftLastWriteTime;
    dataA->nFileSizeHigh    = dataW->nFileSizeHigh;
    dataA->nFileSizeLow     = dataW->nFileSizeLow;
    dataA->dwReserved0      = dataW->dwReserved0;
    dataA->dwReserved1      = dataW->dwReserved1;
    WideCharToMultiByte(CP_ACP, 0, dataW->cFileName, -1, 
        dataA->cFileName, sizeof(dataA->cFileName),
        NULL, NULL);
    WideCharToMultiByte(CP_ACP, 0, dataW->cAlternateFileName, -1, 
        dataA->cAlternateFileName, sizeof(dataA->cAlternateFileName),
        NULL, NULL);
}

235 236
typedef enum
{
237 238 239 240 241
    WH_HINIT = INTERNET_HANDLE_TYPE_INTERNET,
    WH_HFTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_FTP,
    WH_HGOPHERSESSION = INTERNET_HANDLE_TYPE_CONNECT_GOPHER,
    WH_HHTTPSESSION = INTERNET_HANDLE_TYPE_CONNECT_HTTP,
    WH_HFILE = INTERNET_HANDLE_TYPE_FTP_FILE,
242
    WH_HFTPFINDNEXT = INTERNET_HANDLE_TYPE_FTP_FIND,
243
    WH_HHTTPREQ = INTERNET_HANDLE_TYPE_HTTP_REQUEST,
244 245
} WH_TYPE;

246
#define INET_OPENURL 0x0001
247
#define INET_CALLBACKW 0x0002
248

249 250 251
typedef struct
{
    LONG ref;
252
    HANDLE file_handle;
253
    WCHAR *file_name;
254
    WCHAR *url;
255 256 257
    BOOL is_committed;
} req_file_t;

258
typedef struct _object_header_t object_header_t;
259

260
typedef struct {
261 262 263 264
    void (*Destroy)(object_header_t*);
    void (*CloseConnection)(object_header_t*);
    DWORD (*QueryOption)(object_header_t*,DWORD,void*,DWORD*,BOOL);
    DWORD (*SetOption)(object_header_t*,DWORD,void*,DWORD);
265
    DWORD (*ReadFile)(object_header_t*,void*,DWORD,DWORD*,DWORD,DWORD_PTR);
266
    DWORD (*WriteFile)(object_header_t*,const void*,DWORD,DWORD*);
267 268
    DWORD (*QueryDataAvailable)(object_header_t*,DWORD*,DWORD,DWORD_PTR);
    DWORD (*FindNextFileW)(object_header_t*,void*);
269
    DWORD (*LockRequestFile)(object_header_t*,req_file_t**);
270 271
} object_vtbl_t;

272 273
#define INTERNET_HANDLE_IN_USE 1

274
struct _object_header_t
275 276
{
    WH_TYPE htype;
277
    const object_vtbl_t *vtbl;
278
    HINTERNET hInternet;
279
    BOOL valid_handle;
280
    DWORD  dwFlags;
281
    DWORD_PTR dwContext;
282
    DWORD  dwError;
283
    ULONG  ErrorMask;
284
    DWORD  dwInternalFlags;
285
    LONG   refs;
286
    BOOL   decoding;
287
    INTERNET_STATUS_CALLBACK lpfnStatusCB;
288 289
    struct list entry;
    struct list children;
290
};
291 292 293

typedef struct
{
294
    object_header_t hdr;
295 296 297 298 299 300
    LPWSTR  agent;
    LPWSTR  proxy;
    LPWSTR  proxyBypass;
    LPWSTR  proxyUsername;
    LPWSTR  proxyPassword;
    DWORD   accessType;
301
    DWORD   connect_timeout;
302
} appinfo_t;
303 304 305

typedef struct
{
306
    object_header_t hdr;
307 308 309 310 311
    appinfo_t *appInfo;
    LPWSTR  hostName; /* the final destination of the request */
    LPWSTR  userName;
    LPWSTR  password;
    INTERNET_PORT hostPort; /* the final destination port of the request */
312
    DWORD connect_timeout;
313 314
    DWORD send_timeout;
    DWORD receive_timeout;
315
} http_session_t;
316

317 318 319 320 321 322
#define HDR_ISREQUEST		0x0001
#define HDR_COMMADELIMITED	0x0002
#define HDR_SEMIDELIMITED	0x0004

typedef struct
{
323 324
    LPWSTR lpszField;
    LPWSTR lpszValue;
325 326
    WORD wFlags;
    WORD wCount;
327
} HTTPHEADERW, *LPHTTPHEADERW;
328

329

330 331
struct HttpAuthInfo;

332 333 334 335 336 337 338 339
typedef struct data_stream_vtbl_t data_stream_vtbl_t;

typedef struct {
    const data_stream_vtbl_t *vtbl;
}  data_stream_t;

typedef struct {
    data_stream_t data_stream;
340 341
    ULONGLONG content_length;
    ULONGLONG content_read;
342 343 344
} netconn_stream_t;

#define READ_BUFFER_SIZE 8192
345

346 347
typedef struct
{
348
    object_header_t hdr;
349
    http_session_t *session;
350
    server_t *server;
351
    server_t *proxy;
352 353
    LPWSTR path;
    LPWSTR verb;
354 355
    netconn_t *netconn;
    DWORD security_flags;
356
    DWORD connect_timeout;
357 358
    DWORD send_timeout;
    DWORD receive_timeout;
359
    LPWSTR version;
360
    DWORD status_code;
361 362 363
    LPWSTR statusText;
    DWORD bytesToWrite;
    DWORD bytesWritten;
364 365

    CRITICAL_SECTION headers_section;  /* section to protect the headers array */
366
    HTTPHEADERW *custHeaders;
367
    DWORD nCustHeaders;
368

369
    FILETIME last_modified;
370
    HANDLE hCacheFile;
371
    req_file_t *req_file;
372
    FILETIME expires;
373 374
    struct HttpAuthInfo *authInfo;
    struct HttpAuthInfo *proxyAuthInfo;
375 376

    CRITICAL_SECTION read_section;  /* section to protect the following fields */
377
    ULONGLONG contentLength;  /* total number of bytes to be read */
378
    BOOL  read_gzip;      /* are we reading in gzip mode? */
379 380
    DWORD read_pos;       /* current read position in read_buf */
    DWORD read_size;      /* valid data size in read_buf */
381
    BYTE  read_buf[READ_BUFFER_SIZE]; /* buffer for already read but not returned data */
382

383 384
    data_stream_t *data_stream;
    netconn_stream_t netconn_stream;
385
} http_request_t;
386

387
typedef struct task_header_t task_header_t;
388 389
typedef void (*async_task_proc_t)(task_header_t*);

390
struct task_header_t
391
{
392
    async_task_proc_t proc;
393
    object_header_t *hdr;
394
};
395

396 397
void *alloc_async_task(object_header_t*,async_task_proc_t,size_t) DECLSPEC_HIDDEN;

398 399 400 401
void *alloc_object(object_header_t*,const object_vtbl_t*,size_t) DECLSPEC_HIDDEN;
object_header_t *get_handle_object( HINTERNET hinternet ) DECLSPEC_HIDDEN;
object_header_t *WININET_AddRef( object_header_t *info ) DECLSPEC_HIDDEN;
BOOL WININET_Release( object_header_t *info ) DECLSPEC_HIDDEN;
402

403 404
DWORD INET_QueryOption(object_header_t*,DWORD,void*,DWORD*,BOOL) DECLSPEC_HIDDEN;
DWORD INET_SetOption(object_header_t*,DWORD,void*,DWORD) DECLSPEC_HIDDEN;
405

406
time_t ConvertTimeString(LPCWSTR asctime) DECLSPEC_HIDDEN;
407

408
HINTERNET FTP_Connect(appinfo_t *hIC, LPCWSTR lpszServerName,
409
	INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
410
	LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
411
	DWORD dwInternalFlags) DECLSPEC_HIDDEN;
412

413 414 415
DWORD HTTP_Connect(appinfo_t*,LPCWSTR,
        INTERNET_PORT nServerPort, LPCWSTR lpszUserName,
        LPCWSTR lpszPassword, DWORD dwFlags, DWORD_PTR dwContext,
416
        DWORD dwInternalFlags, HINTERNET*) DECLSPEC_HIDDEN;
417

418
BOOL GetAddress(const WCHAR*,INTERNET_PORT,SOCKADDR*,int*,char*) DECLSPEC_HIDDEN;
419

420
DWORD get_cookie_header(const WCHAR*,const WCHAR*,WCHAR**) DECLSPEC_HIDDEN;
421
DWORD set_cookie(substr_t,substr_t,substr_t,substr_t,DWORD) DECLSPEC_HIDDEN;
422

423 424
void INTERNET_SetLastError(DWORD dwError) DECLSPEC_HIDDEN;
DWORD INTERNET_GetLastError(void) DECLSPEC_HIDDEN;
425
DWORD INTERNET_AsyncCall(task_header_t*) DECLSPEC_HIDDEN;
426
LPSTR INTERNET_GetResponseBuffer(void) DECLSPEC_HIDDEN;
427

428
VOID INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR dwContext,
429
                           DWORD dwInternetStatus, LPVOID lpvStatusInfo,
430
                           DWORD dwStatusInfoLength) DECLSPEC_HIDDEN;
431
WCHAR *INTERNET_FindProxyForProtocol(LPCWSTR szProxy, LPCWSTR proto) DECLSPEC_HIDDEN;
432

433
DWORD create_netconn(server_t*,DWORD,BOOL,DWORD,netconn_t**) DECLSPEC_HIDDEN;
434
void free_netconn(netconn_t*) DECLSPEC_HIDDEN;
435
void NETCON_unload(void) DECLSPEC_HIDDEN;
436
DWORD NETCON_secure_connect(netconn_t*,server_t*) DECLSPEC_HIDDEN;
437
DWORD NETCON_send(netconn_t *connection, const void *msg, size_t len, int flags,
438
		int *sent /* out */) DECLSPEC_HIDDEN;
439
DWORD NETCON_recv(netconn_t*,void*,size_t,BOOL,int*) DECLSPEC_HIDDEN;
440
BOOL NETCON_is_alive(netconn_t*) DECLSPEC_HIDDEN;
441 442
LPCVOID NETCON_GetCert(netconn_t *connection) DECLSPEC_HIDDEN;
int NETCON_GetCipherStrength(netconn_t*) DECLSPEC_HIDDEN;
443
DWORD NETCON_set_timeout(netconn_t *connection, BOOL send, DWORD value) DECLSPEC_HIDDEN;
444
int sock_send(int fd, const void *msg, size_t len, int flags) DECLSPEC_HIDDEN;
445
int sock_recv(int fd, void *msg, size_t len, int flags) DECLSPEC_HIDDEN;
446

447
server_t *get_server(substr_t,INTERNET_PORT,BOOL,BOOL) DECLSPEC_HIDDEN;
448

449 450 451 452 453 454 455 456 457
DWORD create_req_file(const WCHAR*,req_file_t**) DECLSPEC_HIDDEN;
void req_file_release(req_file_t*) DECLSPEC_HIDDEN;

static inline req_file_t *req_file_addref(req_file_t *req_file)
{
    InterlockedIncrement(&req_file->ref);
    return req_file;
}

458 459
BOOL init_urlcache(void) DECLSPEC_HIDDEN;
void free_urlcache(void) DECLSPEC_HIDDEN;
460
void free_cookie(void) DECLSPEC_HIDDEN;
461
void free_authorization_cache(void) DECLSPEC_HIDDEN;
Mike McCormack's avatar
Mike McCormack committed
462

463 464
void init_winsock(void) DECLSPEC_HIDDEN;

465 466
#define MAX_REPLY_LEN	 	0x5B4

467 468 469 470 471 472
/* Used for debugging - maybe need to be shared in the Wine debugging code ? */
typedef struct
{
    DWORD val;
    const char* name;
} wininet_flag_info;
473

474
/* Undocumented security flags */
475
#define _SECURITY_FLAG_CERT_REV_FAILED    0x00800000
476
#define _SECURITY_FLAG_CERT_INVALID_CA    0x01000000
477 478
#define _SECURITY_FLAG_CERT_INVALID_CN    0x02000000
#define _SECURITY_FLAG_CERT_INVALID_DATE  0x04000000
479 480

#define _SECURITY_ERROR_FLAGS_MASK              \
481
    (_SECURITY_FLAG_CERT_REV_FAILED             \
482
    |_SECURITY_FLAG_CERT_INVALID_CA             \
483 484
    |_SECURITY_FLAG_CERT_INVALID_CN             \
    |_SECURITY_FLAG_CERT_INVALID_DATE)
485

486
#endif /* _WINE_INTERNET_H_ */