1 /*
2 * Copyright 2008 Hans Leidekker for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #ifndef _WINE_WINHTTP_PRIVATE_H_
20 #define _WINE_WINHTTP_PRIVATE_H_
21
22 #ifndef __WINE_CONFIG_H
23 # error You must include config.h to use this header
24 #endif
25
26 #include "wine/heap.h"
27 #include "wine/list.h"
28 #include "wine/unicode.h"
29
30 #include "ole2.h"
31 #include "sspi.h"
32 #include "wincrypt.h"
33
34 static const WCHAR getW[] = {'G','E','T',0};
35 static const WCHAR postW[] = {'P','O','S','T',0};
36 static const WCHAR headW[] = {'H','E','A','D',0};
37 static const WCHAR slashW[] = {'/',0};
38 static const WCHAR http1_0[] = {'H','T','T','P','/','1','.','0',0};
39 static const WCHAR http1_1[] = {'H','T','T','P','/','1','.','1',0};
40 static const WCHAR chunkedW[] = {'c','h','u','n','k','e','d',0};
41
42 struct object_header;
43 struct object_vtbl
44 {
45 void (*destroy)( struct object_header * );
46 BOOL (*query_option)( struct object_header *, DWORD, void *, DWORD * );
47 BOOL (*set_option)( struct object_header *, DWORD, void *, DWORD );
48 };
49
50 struct object_header
51 {
52 DWORD type;
53 HINTERNET handle;
54 const struct object_vtbl *vtbl;
55 DWORD flags;
56 DWORD disable_flags;
57 DWORD logon_policy;
58 DWORD redirect_policy;
59 DWORD error;
60 DWORD_PTR context;
61 LONG refs;
62 WINHTTP_STATUS_CALLBACK callback;
63 DWORD notify_mask;
64 struct list entry;
65 struct list children;
66 };
67
68 struct hostdata
69 {
70 struct list entry;
71 LONG ref;
72 WCHAR *hostname;
73 INTERNET_PORT port;
74 BOOL secure;
75 struct list connections;
76 };
77
78 struct session
79 {
80 struct object_header hdr;
81 CRITICAL_SECTION cs;
82 WCHAR *agent;
83 DWORD access;
84 int resolve_timeout;
85 int connect_timeout;
86 int send_timeout;
87 int receive_timeout;
88 int receive_response_timeout;
89 WCHAR *proxy_server;
90 WCHAR *proxy_bypass;
91 WCHAR *proxy_username;
92 WCHAR *proxy_password;
93 struct list cookie_cache;
94 HANDLE unload_event;
95 DWORD secure_protocols;
96 DWORD passport_flags;
97 };
98
99 struct connect
100 {
101 struct object_header hdr;
102 struct session *session;
103 WCHAR *hostname; /* final destination of the request */
104 WCHAR *servername; /* name of the server we directly connect to */
105 WCHAR *username;
106 WCHAR *password;
107 INTERNET_PORT hostport;
108 INTERNET_PORT serverport;
109 struct sockaddr_storage sockaddr;
110 BOOL resolved;
111 };
112
113 struct netconn
114 {
115 struct list entry;
116 int socket;
117 struct sockaddr_storage sockaddr;
118 BOOL secure; /* SSL active on connection? */
119 struct hostdata *host;
120 ULONGLONG keep_until;
121 CtxtHandle ssl_ctx;
122 SecPkgContext_StreamSizes ssl_sizes;
123 char *ssl_buf;
124 char *extra_buf;
125 size_t extra_len;
126 char *peek_msg;
127 char *peek_msg_mem;
128 size_t peek_len;
129 };
130
131 struct header
132 {
133 WCHAR *field;
134 WCHAR *value;
135 BOOL is_request; /* part of request headers? */
136 };
137
138 enum auth_target
139 {
140 TARGET_INVALID = -1,
141 TARGET_SERVER,
142 TARGET_PROXY,
143 TARGET_MAX
144 };
145
146 enum auth_scheme
147 {
148 SCHEME_INVALID = -1,
149 SCHEME_BASIC,
150 SCHEME_NTLM,
151 SCHEME_PASSPORT,
152 SCHEME_DIGEST,
153 SCHEME_NEGOTIATE,
154 SCHEME_MAX
155 };
156
157 struct authinfo
158 {
159 enum auth_scheme scheme;
160 CredHandle cred;
161 CtxtHandle ctx;
162 TimeStamp exp;
163 ULONG attr;
164 ULONG max_token;
165 char *data;
166 unsigned int data_len;
167 BOOL finished; /* finished authenticating */
168 };
169
170 struct request
171 {
172 struct object_header hdr;
173 struct connect *connect;
174 WCHAR *verb;
175 WCHAR *path;
176 WCHAR *version;
177 WCHAR *raw_headers;
178 void *optional;
179 DWORD optional_len;
180 struct netconn *netconn;
181 DWORD security_flags;
182 BOOL check_revocation;
183 const CERT_CONTEXT *server_cert;
184 const CERT_CONTEXT *client_cert;
185 CredHandle cred_handle;
186 BOOL cred_handle_initialized;
187 int resolve_timeout;
188 int connect_timeout;
189 int send_timeout;
190 int receive_timeout;
191 int receive_response_timeout;
192 WCHAR *status_text;
193 DWORD content_length; /* total number of bytes to be read */
194 DWORD content_read; /* bytes read so far */
195 BOOL read_chunked; /* are we reading in chunked mode? */
196 BOOL read_chunked_eof; /* end of stream in chunked mode */
197 BOOL read_chunked_size; /* chunk size remaining */
198 DWORD read_pos; /* current read position in read_buf */
199 DWORD read_size; /* valid data size in read_buf */
200 char read_buf[8192]; /* buffer for already read but not returned data */
201 struct header *headers;
202 DWORD num_headers;
203 struct authinfo *authinfo;
204 struct authinfo *proxy_authinfo;
205 HANDLE task_wait;
206 HANDLE task_cancel;
207 #ifdef __REACTOS__
208 HANDLE task_thread;
209 #else
210 BOOL task_proc_running;
211 #endif
212 struct list task_queue;
213 CRITICAL_SECTION task_cs;
214 struct
215 {
216 WCHAR *username;
217 WCHAR *password;
218 } creds[TARGET_MAX][SCHEME_MAX];
219 };
220
221 struct task_header
222 {
223 struct list entry;
224 struct request *request;
225 void (*proc)( struct task_header * );
226 };
227
228 struct send_request
229 {
230 struct task_header hdr;
231 WCHAR *headers;
232 DWORD headers_len;
233 void *optional;
234 DWORD optional_len;
235 DWORD total_len;
236 DWORD_PTR context;
237 };
238
239 struct receive_response
240 {
241 struct task_header hdr;
242 };
243
244 struct query_data
245 {
246 struct task_header hdr;
247 DWORD *available;
248 };
249
250 struct read_data
251 {
252 struct task_header hdr;
253 void *buffer;
254 DWORD to_read;
255 DWORD *read;
256 };
257
258 struct write_data
259 {
260 struct task_header hdr;
261 const void *buffer;
262 DWORD to_write;
263 DWORD *written;
264 };
265
266 struct object_header *addref_object( struct object_header * ) DECLSPEC_HIDDEN;
267 struct object_header *grab_object( HINTERNET ) DECLSPEC_HIDDEN;
268 void release_object( struct object_header * ) DECLSPEC_HIDDEN;
269 HINTERNET alloc_handle( struct object_header * ) DECLSPEC_HIDDEN;
270 BOOL free_handle( HINTERNET ) DECLSPEC_HIDDEN;
271
272 void send_callback( struct object_header *, DWORD, LPVOID, DWORD ) DECLSPEC_HIDDEN;
273 void close_connection( struct request * ) DECLSPEC_HIDDEN;
274
275 void netconn_close( struct netconn * ) DECLSPEC_HIDDEN;
276 struct netconn *netconn_create( struct hostdata *, const struct sockaddr_storage *, int ) DECLSPEC_HIDDEN;
277 void netconn_unload( void ) DECLSPEC_HIDDEN;
278 ULONG netconn_query_data_available( struct netconn * ) DECLSPEC_HIDDEN;
279 BOOL netconn_recv( struct netconn *, void *, size_t, int, int * ) DECLSPEC_HIDDEN;
280 BOOL netconn_resolve( WCHAR *, INTERNET_PORT, struct sockaddr_storage *, int ) DECLSPEC_HIDDEN;
281 BOOL netconn_secure_connect( struct netconn *, WCHAR *, DWORD, CredHandle *, BOOL ) DECLSPEC_HIDDEN;
282 BOOL netconn_send( struct netconn *, const void *, size_t, int * ) DECLSPEC_HIDDEN;
283 DWORD netconn_set_timeout( struct netconn *, BOOL, int ) DECLSPEC_HIDDEN;
284 BOOL netconn_is_alive( struct netconn * ) DECLSPEC_HIDDEN;
285 const void *netconn_get_certificate( struct netconn * ) DECLSPEC_HIDDEN;
286 int netconn_get_cipher_strength( struct netconn * ) DECLSPEC_HIDDEN;
287
288 BOOL set_cookies( struct request *, const WCHAR * ) DECLSPEC_HIDDEN;
289 BOOL add_cookie_headers( struct request * ) DECLSPEC_HIDDEN;
290 BOOL add_request_headers( struct request *, const WCHAR *, DWORD, DWORD ) DECLSPEC_HIDDEN;
291 void destroy_cookies( struct session * ) DECLSPEC_HIDDEN;
292 BOOL set_server_for_hostname( struct connect *, const WCHAR *, INTERNET_PORT ) DECLSPEC_HIDDEN;
293 void destroy_authinfo( struct authinfo * ) DECLSPEC_HIDDEN;
294
295 void release_host( struct hostdata * ) DECLSPEC_HIDDEN;
296 BOOL process_header( struct request *, const WCHAR *, const WCHAR *, DWORD, BOOL ) DECLSPEC_HIDDEN;
297
298 extern HRESULT WinHttpRequest_create( void ** ) DECLSPEC_HIDDEN;
299 void release_typelib( void ) DECLSPEC_HIDDEN;
300
heap_realloc_zero(LPVOID mem,SIZE_T size)301 static inline void* __WINE_ALLOC_SIZE(2) heap_realloc_zero( LPVOID mem, SIZE_T size )
302 {
303 return HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, mem, size );
304 }
305
strdupW(const WCHAR * src)306 static inline WCHAR *strdupW( const WCHAR *src )
307 {
308 WCHAR *dst;
309
310 if (!src) return NULL;
311 dst = heap_alloc( (strlenW( src ) + 1) * sizeof(WCHAR) );
312 if (dst) strcpyW( dst, src );
313 return dst;
314 }
315
strdupAW(const char * src)316 static inline WCHAR *strdupAW( const char *src )
317 {
318 WCHAR *dst = NULL;
319 if (src)
320 {
321 DWORD len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
322 if ((dst = heap_alloc( len * sizeof(WCHAR) )))
323 MultiByteToWideChar( CP_ACP, 0, src, -1, dst, len );
324 }
325 return dst;
326 }
327
strdupWA(const WCHAR * src)328 static inline char *strdupWA( const WCHAR *src )
329 {
330 char *dst = NULL;
331 if (src)
332 {
333 int len = WideCharToMultiByte( CP_ACP, 0, src, -1, NULL, 0, NULL, NULL );
334 if ((dst = heap_alloc( len )))
335 WideCharToMultiByte( CP_ACP, 0, src, -1, dst, len, NULL, NULL );
336 }
337 return dst;
338 }
339
strdupWA_sized(const WCHAR * src,DWORD size)340 static inline char *strdupWA_sized( const WCHAR *src, DWORD size )
341 {
342 char *dst = NULL;
343 if (src)
344 {
345 int len = WideCharToMultiByte( CP_ACP, 0, src, size, NULL, 0, NULL, NULL ) + 1;
346 if ((dst = heap_alloc( len )))
347 {
348 WideCharToMultiByte( CP_ACP, 0, src, len, dst, size, NULL, NULL );
349 dst[len - 1] = 0;
350 }
351 }
352 return dst;
353 }
354
355 extern HINSTANCE winhttp_instance DECLSPEC_HIDDEN;
356
357 #endif /* _WINE_WINHTTP_PRIVATE_H_ */
358