]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/serf/serf_private.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / serf / serf_private.h
1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #ifndef _SERF_PRIVATE_H_
17 #define _SERF_PRIVATE_H_
18
19 /* ### what the hell? why does the APR interface have a "size" ??
20    ### the implication is that, if we bust this limit, we'd need to
21    ### stop, rebuild a pollset, and repopulate it. what suckage.  */
22 #define MAX_CONN 16
23
24 /* Windows does not define IOV_MAX, so we need to ensure it is defined. */
25 #ifndef IOV_MAX
26 #define IOV_MAX 16
27 #endif
28
29 /* Older versions of APR do not have this macro.  */
30 #ifdef APR_SIZE_MAX
31 #define REQUESTED_MAX APR_SIZE_MAX
32 #else
33 #define REQUESTED_MAX (~((apr_size_t)0))
34 #endif
35
36 #define SERF_IO_CLIENT (1)
37 #define SERF_IO_CONN (2)
38 #define SERF_IO_LISTENER (3)
39
40 /* Internal logging facilities, set flag to 1 to enable console logging for
41    the selected component. */
42 #define SSL_VERBOSE 0
43 #define SSL_MSG_VERBOSE 0  /* logs decrypted requests and responses. */
44 #define SOCK_VERBOSE 0
45 #define SOCK_MSG_VERBOSE 0 /* logs bytes received from or written to a socket. */
46 #define CONN_VERBOSE 0
47 #define AUTH_VERBOSE 0
48
49 /* Older versions of APR do not have the APR_VERSION_AT_LEAST macro. Those
50    implementations are safe.
51
52    If the macro *is* defined, and we're on WIN32, and APR is version 1.4.0+,
53    then we have a broken WSAPoll() implementation.
54
55    See serf_context_create_ex() below.  */
56 #if defined(APR_VERSION_AT_LEAST) && defined(WIN32)
57 #if APR_VERSION_AT_LEAST(1,4,0)
58 #define BROKEN_WSAPOLL
59 #endif
60 #endif
61
62 typedef struct serf__authn_scheme_t serf__authn_scheme_t;
63
64 typedef struct serf_io_baton_t {
65     int type;
66     union {
67         serf_incoming_t *client;
68         serf_connection_t *conn;
69         serf_listener_t *listener;
70     } u;
71 } serf_io_baton_t;
72
73 /* Holds all the information corresponding to a request/response pair. */
74 struct serf_request_t {
75     serf_connection_t *conn;
76
77     apr_pool_t *respool;
78     serf_bucket_alloc_t *allocator;
79
80     /* The bucket corresponding to the request. Will be NULL once the
81      * bucket has been emptied (for delivery into the socket).
82      */
83     serf_bucket_t *req_bkt;
84
85     serf_request_setup_t setup;
86     void *setup_baton;
87
88     serf_response_acceptor_t acceptor;
89     void *acceptor_baton;
90
91     serf_response_handler_t handler;
92     void *handler_baton;
93
94     serf_bucket_t *resp_bkt;
95
96     int written;
97     int priority;
98     /* 1 if this is a request to setup a SSL tunnel, 0 for normal requests. */
99     int ssltunnel;
100
101     /* This baton is currently only used for digest authentication, which
102        needs access to the uri of the request in the response handler.
103        If serf_request_t is replaced by a serf_http_request_t in the future,
104        which knows about uri and method and such, this baton won't be needed
105        anymore. */
106     void *auth_baton;
107
108     struct serf_request_t *next;
109 };
110
111 typedef struct serf_pollset_t {
112     /* the set of connections to poll */
113     apr_pollset_t *pollset;
114 } serf_pollset_t;
115
116 typedef struct serf__authn_info_t {
117     const serf__authn_scheme_t *scheme;
118
119     void *baton;
120 } serf__authn_info_t;
121
122 struct serf_context_t {
123     /* the pool used for self and for other allocations */
124     apr_pool_t *pool;
125
126     void *pollset_baton;
127     serf_socket_add_t pollset_add;
128     serf_socket_remove_t pollset_rm;
129
130     /* one of our connections has a dirty pollset state. */
131     int dirty_pollset;
132
133     /* the list of active connections */
134     apr_array_header_t *conns;
135 #define GET_CONN(ctx, i) (((serf_connection_t **)(ctx)->conns->elts)[i])
136
137     /* Proxy server address */
138     apr_sockaddr_t *proxy_address;
139
140     /* Progress callback */
141     serf_progress_t progress_func;
142     void *progress_baton;
143     apr_off_t progress_read;
144     apr_off_t progress_written;
145
146     /* authentication info for the servers used in this context. Shared by all
147        connections to the same server.
148        Structure of the hashtable:  key: host url, e.g. https://localhost:80
149                                   value: serf__authn_info_t *
150      */
151     apr_hash_t *server_authn_info;
152
153     /* authentication info for the proxy configured in this context, shared by
154        all connections. */
155     serf__authn_info_t proxy_authn_info;
156
157     /* List of authn types supported by the client.*/
158     int authn_types;
159     /* Callback function used to get credentials for a realm. */
160     serf_credentials_callback_t cred_cb; 
161 };
162
163 struct serf_listener_t {
164     serf_context_t *ctx;
165     serf_io_baton_t baton;
166     apr_socket_t *skt;
167     apr_pool_t *pool;
168     apr_pollfd_t desc;
169     void *accept_baton;
170     serf_accept_client_t accept_func;
171 };
172
173 struct serf_incoming_t {
174     serf_context_t *ctx;
175     serf_io_baton_t baton;
176     void *request_baton;
177     serf_incoming_request_cb_t request;
178     apr_socket_t *skt;
179     apr_pollfd_t desc;
180 };
181
182 /* States for the different stages in the lifecyle of a connection. */
183 typedef enum {
184     SERF_CONN_INIT,             /* no socket created yet */
185     SERF_CONN_SETUP_SSLTUNNEL,  /* ssl tunnel being setup, no requests sent */
186     SERF_CONN_CONNECTED,        /* conn is ready to send requests */
187     SERF_CONN_CLOSING           /* conn is closing, no more requests,
188                                    start a new socket */
189 } serf__connection_state_t;
190
191 struct serf_connection_t {
192     serf_context_t *ctx;
193
194     apr_status_t status;
195     serf_io_baton_t baton;
196
197     apr_pool_t *pool;
198     serf_bucket_alloc_t *allocator;
199
200     apr_sockaddr_t *address;
201
202     apr_socket_t *skt;
203     apr_pool_t *skt_pool;
204
205     /* the last reqevents we gave to pollset_add */
206     apr_int16_t reqevents;
207
208     /* the events we've seen for this connection in our returned pollset */
209     apr_int16_t seen_in_pollset;
210
211     /* are we a dirty connection that needs its poll status updated? */
212     int dirty_conn;
213
214     /* number of completed requests we've sent */
215     unsigned int completed_requests;
216
217     /* number of completed responses we've got */
218     unsigned int completed_responses;
219
220     /* keepalive */
221     unsigned int probable_keepalive_limit;
222
223     /* Current state of the connection (whether or not it is connected). */
224     serf__connection_state_t state;
225
226     /* This connection may have responses without a request! */
227     int async_responses;
228     serf_bucket_t *current_async_response;
229     serf_response_acceptor_t async_acceptor;
230     void *async_acceptor_baton;
231     serf_response_handler_t async_handler;
232     void *async_handler_baton;
233
234     /* A bucket wrapped around our socket (for reading responses). */
235     serf_bucket_t *stream;
236     /* A reference to the aggregate bucket that provides the boundary between
237      * request level buckets and connection level buckets.
238      */
239     serf_bucket_t *ostream_head;
240     serf_bucket_t *ostream_tail;
241
242     /* Aggregate bucket used to send the CONNECT request. */
243     serf_bucket_t *ssltunnel_ostream;
244
245     /* The list of active requests. */
246     serf_request_t *requests;
247     serf_request_t *requests_tail;
248
249     struct iovec vec[IOV_MAX];
250     int vec_len;
251
252     serf_connection_setup_t setup;
253     void *setup_baton;
254     serf_connection_closed_t closed;
255     void *closed_baton;
256
257     /* Max. number of outstanding requests. */
258     unsigned int max_outstanding_requests;
259
260     int hit_eof;
261
262     /* Host url, path ommitted, syntax: https://svn.apache.org . */
263     const char *host_url;
264     
265     /* Exploded host url, path ommitted. Only scheme, hostinfo, hostname &
266        port values are filled in. */
267     apr_uri_t host_info;
268
269     /* connection and authentication scheme specific information */ 
270     void *authn_baton;
271     void *proxy_authn_baton;
272
273     /* Time marker when connection begins. */
274     apr_time_t connect_time;
275
276     /* Calculated connection latency. Negative value if latency is unknown. */
277     apr_interval_time_t latency;
278
279     /* Needs to read first before we can write again. */
280     int stop_writing;
281 };
282
283 /*** Internal bucket functions ***/
284
285 /** Transform a response_bucket in-place into an aggregate bucket. Restore the
286     status line and all headers, not just the body.
287  
288     This can only be used when we haven't started reading the body of the
289     response yet.
290  
291     Keep internal for now, probably only useful within serf.
292  */
293 apr_status_t serf_response_full_become_aggregate(serf_bucket_t *bucket);
294
295 /*** Authentication handler declarations ***/
296
297 typedef enum { PROXY, HOST } peer_t;
298
299 /**
300  * For each authentication scheme we need a handler function of type
301  * serf__auth_handler_func_t. This function will be called when an
302  * authentication challenge is received in a session.
303  */
304 typedef apr_status_t
305 (*serf__auth_handler_func_t)(int code,
306                              serf_request_t *request,
307                              serf_bucket_t *response,
308                              const char *auth_hdr,
309                              const char *auth_attr,
310                              void *baton,
311                              apr_pool_t *pool);
312
313 /**
314  * For each authentication scheme we need an initialization function of type
315  * serf__init_context_func_t. This function will be called the first time
316  * serf tries a specific authentication scheme handler.
317  */
318 typedef apr_status_t
319 (*serf__init_context_func_t)(int code,
320                              serf_context_t *conn,
321                              apr_pool_t *pool);
322
323 /**
324  * For each authentication scheme we need an initialization function of type
325  * serf__init_conn_func_t. This function will be called when a new
326  * connection is opened.
327  */
328 typedef apr_status_t
329 (*serf__init_conn_func_t)(const serf__authn_scheme_t *scheme,
330                           int code,
331                           serf_connection_t *conn,
332                           apr_pool_t *pool);
333
334 /**
335  * For each authentication scheme we need a setup_request function of type
336  * serf__setup_request_func_t. This function will be called when a
337  * new serf_request_t object is created and should fill in the correct
338  * authentication headers (if needed).
339  */
340 typedef apr_status_t
341 (*serf__setup_request_func_t)(peer_t peer,
342                               int code,
343                               serf_connection_t *conn,
344                               serf_request_t *request,
345                               const char *method,
346                               const char *uri,
347                               serf_bucket_t *hdrs_bkt);
348
349 /**
350  * This function will be called when a response is received, so that the 
351  * scheme handler can validate the Authentication related response headers
352  * (if needed).
353  */
354 typedef apr_status_t
355 (*serf__validate_response_func_t)(peer_t peer,
356                                   int code,
357                                   serf_connection_t *conn,
358                                   serf_request_t *request,
359                                   serf_bucket_t *response,
360                                   apr_pool_t *pool);
361
362 /**
363  * serf__authn_scheme_t: vtable for an authn scheme provider.
364  */
365 struct serf__authn_scheme_t {
366     /* The name of this authentication scheme. Used in headers of requests and
367        for logging. */
368     const char *name;
369
370     /* Key is the name of the authentication scheme in lower case, to
371        facilitate case insensitive matching of the response headers. */
372     const char *key;
373
374     /* Internal code used for this authn type. */
375     int type;
376
377     /* The context initialization function if any; otherwise, NULL */
378     serf__init_context_func_t init_ctx_func;
379
380     /* The connection initialization function if any; otherwise, NULL */
381     serf__init_conn_func_t init_conn_func;
382
383     /* The authentication handler function */
384     serf__auth_handler_func_t handle_func;
385
386     /* Function to set up the authentication header of a request */
387     serf__setup_request_func_t setup_request_func;
388
389     /* Function to validate the authentication header of a response */
390     serf__validate_response_func_t validate_response_func;
391 };
392
393 /**
394  * Handles a 401 or 407 response, tries the different available authentication
395  * handlers.
396  */
397 apr_status_t serf__handle_auth_response(int *consumed_response,
398                                         serf_request_t *request,
399                                         serf_bucket_t *response,
400                                         void *baton,
401                                         apr_pool_t *pool);
402
403 /* Get the cached serf__authn_info_t object for the target server, or create one
404    when this is the first connection to the server.
405    TODO: The serf__authn_info_t objects are allocated in the context pool, so
406    a context that's used to connect to many different servers using Basic or 
407    Digest authencation will hold on to many objects indefinitely. We should be
408    able to cleanup stale objects from time to time. */
409 serf__authn_info_t *serf__get_authn_info_for_server(serf_connection_t *conn);
410
411 /* fromt context.c */
412 void serf__context_progress_delta(void *progress_baton, apr_off_t read,
413                                   apr_off_t written);
414
415 /* from incoming.c */
416 apr_status_t serf__process_client(serf_incoming_t *l, apr_int16_t events);
417 apr_status_t serf__process_listener(serf_listener_t *l);
418
419 /* from outgoing.c */
420 apr_status_t serf__open_connections(serf_context_t *ctx);
421 apr_status_t serf__process_connection(serf_connection_t *conn,
422                                        apr_int16_t events);
423 apr_status_t serf__conn_update_pollset(serf_connection_t *conn);
424 serf_request_t *serf__ssltunnel_request_create(serf_connection_t *conn,
425                                                serf_request_setup_t setup,
426                                                void *setup_baton);
427 apr_status_t serf__provide_credentials(serf_context_t *ctx,
428                                        char **username,
429                                        char **password,
430                                        serf_request_t *request,
431                                        void *baton,
432                                        int code, const char *authn_type,
433                                        const char *realm,
434                                        apr_pool_t *pool);
435
436 /* from ssltunnel.c */
437 apr_status_t serf__ssltunnel_connect(serf_connection_t *conn);
438
439
440 /** Logging functions. Use one of the [COMP]_VERBOSE flags to enable specific
441     logging. 
442  **/
443
444 /* Logs a standard event, with filename & timestamp header */
445 void serf__log(int verbose_flag, const char *filename, const char *fmt, ...);
446
447 /* Logs a standard event, but without prefix. This is useful to build up
448  log lines in parts. */
449 void serf__log_nopref(int verbose_flag, const char *fmt, ...);
450
451 /* Logs a socket event, add local and remote ip address:port */
452 void serf__log_skt(int verbose_flag, const char *filename, apr_socket_t *skt,
453                    const char *fmt, ...);
454
455 #endif