1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef _SERF_PRIVATE_H_
17 #define _SERF_PRIVATE_H_
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. */
24 /* Windows does not define IOV_MAX, so we need to ensure it is defined. */
29 /* Older versions of APR do not have this macro. */
31 #define REQUESTED_MAX APR_SIZE_MAX
33 #define REQUESTED_MAX (~((apr_size_t)0))
36 #define SERF_IO_CLIENT (1)
37 #define SERF_IO_CONN (2)
38 #define SERF_IO_LISTENER (3)
40 /* Internal logging facilities, set flag to 1 to enable console logging for
41 the selected component. */
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
49 /* Older versions of APR do not have the APR_VERSION_AT_LEAST macro. Those
50 implementations are safe.
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.
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
62 typedef struct serf__authn_scheme_t serf__authn_scheme_t;
64 typedef struct serf_io_baton_t {
67 serf_incoming_t *client;
68 serf_connection_t *conn;
69 serf_listener_t *listener;
73 /* Holds all the information corresponding to a request/response pair. */
74 struct serf_request_t {
75 serf_connection_t *conn;
78 serf_bucket_alloc_t *allocator;
80 /* The bucket corresponding to the request. Will be NULL once the
81 * bucket has been emptied (for delivery into the socket).
83 serf_bucket_t *req_bkt;
85 serf_request_setup_t setup;
88 serf_response_acceptor_t acceptor;
91 serf_response_handler_t handler;
94 serf_bucket_t *resp_bkt;
98 /* 1 if this is a request to setup a SSL tunnel, 0 for normal requests. */
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
108 struct serf_request_t *next;
111 typedef struct serf_pollset_t {
112 /* the set of connections to poll */
113 apr_pollset_t *pollset;
116 typedef struct serf__authn_info_t {
117 const serf__authn_scheme_t *scheme;
120 } serf__authn_info_t;
122 struct serf_context_t {
123 /* the pool used for self and for other allocations */
127 serf_socket_add_t pollset_add;
128 serf_socket_remove_t pollset_rm;
130 /* one of our connections has a dirty pollset state. */
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])
137 /* Proxy server address */
138 apr_sockaddr_t *proxy_address;
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;
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 *
151 apr_hash_t *server_authn_info;
153 /* authentication info for the proxy configured in this context, shared by
155 serf__authn_info_t proxy_authn_info;
157 /* List of authn types supported by the client.*/
159 /* Callback function used to get credentials for a realm. */
160 serf_credentials_callback_t cred_cb;
163 struct serf_listener_t {
165 serf_io_baton_t baton;
170 serf_accept_client_t accept_func;
173 struct serf_incoming_t {
175 serf_io_baton_t baton;
177 serf_incoming_request_cb_t request;
182 /* States for the different stages in the lifecyle of a connection. */
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;
191 struct serf_connection_t {
195 serf_io_baton_t baton;
198 serf_bucket_alloc_t *allocator;
200 apr_sockaddr_t *address;
203 apr_pool_t *skt_pool;
205 /* the last reqevents we gave to pollset_add */
206 apr_int16_t reqevents;
208 /* the events we've seen for this connection in our returned pollset */
209 apr_int16_t seen_in_pollset;
211 /* are we a dirty connection that needs its poll status updated? */
214 /* number of completed requests we've sent */
215 unsigned int completed_requests;
217 /* number of completed responses we've got */
218 unsigned int completed_responses;
221 unsigned int probable_keepalive_limit;
223 /* Current state of the connection (whether or not it is connected). */
224 serf__connection_state_t state;
226 /* This connection may have responses without a request! */
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;
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.
239 serf_bucket_t *ostream_head;
240 serf_bucket_t *ostream_tail;
242 /* Aggregate bucket used to send the CONNECT request. */
243 serf_bucket_t *ssltunnel_ostream;
245 /* The list of active requests. */
246 serf_request_t *requests;
247 serf_request_t *requests_tail;
249 struct iovec vec[IOV_MAX];
252 serf_connection_setup_t setup;
254 serf_connection_closed_t closed;
257 /* Max. number of outstanding requests. */
258 unsigned int max_outstanding_requests;
262 /* Host url, path ommitted, syntax: https://svn.apache.org . */
263 const char *host_url;
265 /* Exploded host url, path ommitted. Only scheme, hostinfo, hostname &
266 port values are filled in. */
269 /* connection and authentication scheme specific information */
271 void *proxy_authn_baton;
273 /* Time marker when connection begins. */
274 apr_time_t connect_time;
276 /* Calculated connection latency. Negative value if latency is unknown. */
277 apr_interval_time_t latency;
279 /* Needs to read first before we can write again. */
283 /*** Internal bucket functions ***/
285 /** Transform a response_bucket in-place into an aggregate bucket. Restore the
286 status line and all headers, not just the body.
288 This can only be used when we haven't started reading the body of the
291 Keep internal for now, probably only useful within serf.
293 apr_status_t serf_response_full_become_aggregate(serf_bucket_t *bucket);
295 /*** Authentication handler declarations ***/
297 typedef enum { PROXY, HOST } peer_t;
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.
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,
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.
319 (*serf__init_context_func_t)(int code,
320 serf_context_t *conn,
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.
329 (*serf__init_conn_func_t)(const serf__authn_scheme_t *scheme,
331 serf_connection_t *conn,
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).
341 (*serf__setup_request_func_t)(peer_t peer,
343 serf_connection_t *conn,
344 serf_request_t *request,
347 serf_bucket_t *hdrs_bkt);
350 * This function will be called when a response is received, so that the
351 * scheme handler can validate the Authentication related response headers
355 (*serf__validate_response_func_t)(peer_t peer,
357 serf_connection_t *conn,
358 serf_request_t *request,
359 serf_bucket_t *response,
363 * serf__authn_scheme_t: vtable for an authn scheme provider.
365 struct serf__authn_scheme_t {
366 /* The name of this authentication scheme. Used in headers of requests and
370 /* Key is the name of the authentication scheme in lower case, to
371 facilitate case insensitive matching of the response headers. */
374 /* Internal code used for this authn type. */
377 /* The context initialization function if any; otherwise, NULL */
378 serf__init_context_func_t init_ctx_func;
380 /* The connection initialization function if any; otherwise, NULL */
381 serf__init_conn_func_t init_conn_func;
383 /* The authentication handler function */
384 serf__auth_handler_func_t handle_func;
386 /* Function to set up the authentication header of a request */
387 serf__setup_request_func_t setup_request_func;
389 /* Function to validate the authentication header of a response */
390 serf__validate_response_func_t validate_response_func;
394 * Handles a 401 or 407 response, tries the different available authentication
397 apr_status_t serf__handle_auth_response(int *consumed_response,
398 serf_request_t *request,
399 serf_bucket_t *response,
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);
411 /* fromt context.c */
412 void serf__context_progress_delta(void *progress_baton, apr_off_t read,
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);
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,
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,
427 apr_status_t serf__provide_credentials(serf_context_t *ctx,
430 serf_request_t *request,
432 int code, const char *authn_type,
436 /* from ssltunnel.c */
437 apr_status_t serf__ssltunnel_connect(serf_connection_t *conn);
440 /** Logging functions. Use one of the [COMP]_VERBOSE flags to enable specific
444 /* Logs a standard event, with filename & timestamp header */
445 void serf__log(int verbose_flag, const char *filename, const char *fmt, ...);
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, ...);
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, ...);