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