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. */
26 /* There is no limit for iovec count on Windows, but apr_socket_sendv
27 allocates WSABUF structures on stack if vecs_count <= 50. */
31 /* Older versions of APR do not have this macro. */
33 #define REQUESTED_MAX APR_SIZE_MAX
35 #define REQUESTED_MAX (~((apr_size_t)0))
38 #define SERF_IO_CLIENT (1)
39 #define SERF_IO_CONN (2)
40 #define SERF_IO_LISTENER (3)
42 /* Internal logging facilities, set flag to 1 to enable console logging for
43 the selected component. */
45 #define SSL_MSG_VERBOSE 0 /* logs decrypted requests and responses. */
46 #define SOCK_VERBOSE 0
47 #define SOCK_MSG_VERBOSE 0 /* logs bytes received from or written to a socket. */
48 #define CONN_VERBOSE 0
49 #define AUTH_VERBOSE 0
51 /* Older versions of APR do not have the APR_VERSION_AT_LEAST macro. Those
52 implementations are safe.
54 If the macro *is* defined, and we're on WIN32, and APR is version 1.4.0+,
55 then we have a broken WSAPoll() implementation.
57 See serf_context_create_ex() below. */
58 #if defined(APR_VERSION_AT_LEAST) && defined(WIN32)
59 #if APR_VERSION_AT_LEAST(1,4,0)
60 #define BROKEN_WSAPOLL
64 typedef struct serf__authn_scheme_t serf__authn_scheme_t;
66 typedef struct serf_io_baton_t {
69 serf_incoming_t *client;
70 serf_connection_t *conn;
71 serf_listener_t *listener;
75 /* Holds all the information corresponding to a request/response pair. */
76 struct serf_request_t {
77 serf_connection_t *conn;
80 serf_bucket_alloc_t *allocator;
82 /* The bucket corresponding to the request. Will be NULL once the
83 * bucket has been emptied (for delivery into the socket).
85 serf_bucket_t *req_bkt;
87 serf_request_setup_t setup;
90 serf_response_acceptor_t acceptor;
93 serf_response_handler_t handler;
96 serf_bucket_t *resp_bkt;
100 /* 1 if this is a request to setup a SSL tunnel, 0 for normal requests. */
103 /* This baton is currently only used for digest authentication, which
104 needs access to the uri of the request in the response handler.
105 If serf_request_t is replaced by a serf_http_request_t in the future,
106 which knows about uri and method and such, this baton won't be needed
110 struct serf_request_t *next;
113 typedef struct serf_pollset_t {
114 /* the set of connections to poll */
115 apr_pollset_t *pollset;
118 typedef struct serf__authn_info_t {
119 const serf__authn_scheme_t *scheme;
123 int failed_authn_types;
124 } serf__authn_info_t;
126 struct serf_context_t {
127 /* the pool used for self and for other allocations */
131 serf_socket_add_t pollset_add;
132 serf_socket_remove_t pollset_rm;
134 /* one of our connections has a dirty pollset state. */
137 /* the list of active connections */
138 apr_array_header_t *conns;
139 #define GET_CONN(ctx, i) (((serf_connection_t **)(ctx)->conns->elts)[i])
141 /* Proxy server address */
142 apr_sockaddr_t *proxy_address;
144 /* Progress callback */
145 serf_progress_t progress_func;
146 void *progress_baton;
147 apr_off_t progress_read;
148 apr_off_t progress_written;
150 /* authentication info for the servers used in this context. Shared by all
151 connections to the same server.
152 Structure of the hashtable: key: host url, e.g. https://localhost:80
153 value: serf__authn_info_t *
155 apr_hash_t *server_authn_info;
157 /* authentication info for the proxy configured in this context, shared by
159 serf__authn_info_t proxy_authn_info;
161 /* List of authn types supported by the client.*/
163 /* Callback function used to get credentials for a realm. */
164 serf_credentials_callback_t cred_cb;
167 struct serf_listener_t {
169 serf_io_baton_t baton;
174 serf_accept_client_t accept_func;
177 struct serf_incoming_t {
179 serf_io_baton_t baton;
181 serf_incoming_request_cb_t request;
186 /* States for the different stages in the lifecyle of a connection. */
188 SERF_CONN_INIT, /* no socket created yet */
189 SERF_CONN_SETUP_SSLTUNNEL, /* ssl tunnel being setup, no requests sent */
190 SERF_CONN_CONNECTED, /* conn is ready to send requests */
191 SERF_CONN_CLOSING /* conn is closing, no more requests,
192 start a new socket */
193 } serf__connection_state_t;
195 struct serf_connection_t {
199 serf_io_baton_t baton;
202 serf_bucket_alloc_t *allocator;
204 apr_sockaddr_t *address;
207 apr_pool_t *skt_pool;
209 /* the last reqevents we gave to pollset_add */
210 apr_int16_t reqevents;
212 /* the events we've seen for this connection in our returned pollset */
213 apr_int16_t seen_in_pollset;
215 /* are we a dirty connection that needs its poll status updated? */
218 /* number of completed requests we've sent */
219 unsigned int completed_requests;
221 /* number of completed responses we've got */
222 unsigned int completed_responses;
225 unsigned int probable_keepalive_limit;
227 /* Current state of the connection (whether or not it is connected). */
228 serf__connection_state_t state;
230 /* This connection may have responses without a request! */
232 serf_bucket_t *current_async_response;
233 serf_response_acceptor_t async_acceptor;
234 void *async_acceptor_baton;
235 serf_response_handler_t async_handler;
236 void *async_handler_baton;
238 /* A bucket wrapped around our socket (for reading responses). */
239 serf_bucket_t *stream;
240 /* A reference to the aggregate bucket that provides the boundary between
241 * request level buckets and connection level buckets.
243 serf_bucket_t *ostream_head;
244 serf_bucket_t *ostream_tail;
246 /* Aggregate bucket used to send the CONNECT request. */
247 serf_bucket_t *ssltunnel_ostream;
249 /* The list of active requests. */
250 serf_request_t *requests;
251 serf_request_t *requests_tail;
253 struct iovec vec[IOV_MAX];
256 serf_connection_setup_t setup;
258 serf_connection_closed_t closed;
261 /* Max. number of outstanding requests. */
262 unsigned int max_outstanding_requests;
266 /* Host url, path ommitted, syntax: https://svn.apache.org . */
267 const char *host_url;
269 /* Exploded host url, path ommitted. Only scheme, hostinfo, hostname &
270 port values are filled in. */
273 /* authentication info for this connection. */
274 serf__authn_info_t authn_info;
276 /* Time marker when connection begins. */
277 apr_time_t connect_time;
279 /* Calculated connection latency. Negative value if latency is unknown. */
280 apr_interval_time_t latency;
282 /* Needs to read first before we can write again. */
286 /*** Internal bucket functions ***/
288 /** Transform a response_bucket in-place into an aggregate bucket. Restore the
289 status line and all headers, not just the body.
291 This can only be used when we haven't started reading the body of the
294 Keep internal for now, probably only useful within serf.
296 apr_status_t serf_response_full_become_aggregate(serf_bucket_t *bucket);
299 * Remove the header from the list, do nothing if the header wasn't added.
301 void serf__bucket_headers_remove(serf_bucket_t *headers_bucket,
304 /*** Authentication handler declarations ***/
306 typedef enum { PROXY, HOST } peer_t;
309 * For each authentication scheme we need a handler function of type
310 * serf__auth_handler_func_t. This function will be called when an
311 * authentication challenge is received in a session.
314 (*serf__auth_handler_func_t)(int code,
315 serf_request_t *request,
316 serf_bucket_t *response,
317 const char *auth_hdr,
318 const char *auth_attr,
323 * For each authentication scheme we need an initialization function of type
324 * serf__init_context_func_t. This function will be called the first time
325 * serf tries a specific authentication scheme handler.
328 (*serf__init_context_func_t)(int code,
329 serf_context_t *conn,
333 * For each authentication scheme we need an initialization function of type
334 * serf__init_conn_func_t. This function will be called when a new
335 * connection is opened.
338 (*serf__init_conn_func_t)(const serf__authn_scheme_t *scheme,
340 serf_connection_t *conn,
344 * For each authentication scheme we need a setup_request function of type
345 * serf__setup_request_func_t. This function will be called when a
346 * new serf_request_t object is created and should fill in the correct
347 * authentication headers (if needed).
350 (*serf__setup_request_func_t)(peer_t peer,
352 serf_connection_t *conn,
353 serf_request_t *request,
356 serf_bucket_t *hdrs_bkt);
359 * This function will be called when a response is received, so that the
360 * scheme handler can validate the Authentication related response headers
364 (*serf__validate_response_func_t)(const serf__authn_scheme_t *scheme,
367 serf_connection_t *conn,
368 serf_request_t *request,
369 serf_bucket_t *response,
373 * serf__authn_scheme_t: vtable for an authn scheme provider.
375 struct serf__authn_scheme_t {
376 /* The name of this authentication scheme. Used in headers of requests and
380 /* Key is the name of the authentication scheme in lower case, to
381 facilitate case insensitive matching of the response headers. */
384 /* Internal code used for this authn type. */
387 /* The context initialization function if any; otherwise, NULL */
388 serf__init_context_func_t init_ctx_func;
390 /* The connection initialization function if any; otherwise, NULL */
391 serf__init_conn_func_t init_conn_func;
393 /* The authentication handler function */
394 serf__auth_handler_func_t handle_func;
396 /* Function to set up the authentication header of a request */
397 serf__setup_request_func_t setup_request_func;
399 /* Function to validate the authentication header of a response */
400 serf__validate_response_func_t validate_response_func;
404 * Handles a 401 or 407 response, tries the different available authentication
407 apr_status_t serf__handle_auth_response(int *consumed_response,
408 serf_request_t *request,
409 serf_bucket_t *response,
413 /* Get the cached serf__authn_info_t object for the target server, or create one
414 when this is the first connection to the server.
415 TODO: The serf__authn_info_t objects are allocated in the context pool, so
416 a context that's used to connect to many different servers using Basic or
417 Digest authencation will hold on to many objects indefinitely. We should be
418 able to cleanup stale objects from time to time. */
419 serf__authn_info_t *serf__get_authn_info_for_server(serf_connection_t *conn);
421 /* fromt context.c */
422 void serf__context_progress_delta(void *progress_baton, apr_off_t read,
425 /* from incoming.c */
426 apr_status_t serf__process_client(serf_incoming_t *l, apr_int16_t events);
427 apr_status_t serf__process_listener(serf_listener_t *l);
429 /* from outgoing.c */
430 apr_status_t serf__open_connections(serf_context_t *ctx);
431 apr_status_t serf__process_connection(serf_connection_t *conn,
433 apr_status_t serf__conn_update_pollset(serf_connection_t *conn);
434 serf_request_t *serf__ssltunnel_request_create(serf_connection_t *conn,
435 serf_request_setup_t setup,
437 apr_status_t serf__provide_credentials(serf_context_t *ctx,
440 serf_request_t *request,
442 int code, const char *authn_type,
446 /* from ssltunnel.c */
447 apr_status_t serf__ssltunnel_connect(serf_connection_t *conn);
450 /** Logging functions. Use one of the [COMP]_VERBOSE flags to enable specific
454 /* Logs a standard event, with filename & timestamp header */
455 void serf__log(int verbose_flag, const char *filename, const char *fmt, ...);
457 /* Logs a standard event, but without prefix. This is useful to build up
458 log lines in parts. */
459 void serf__log_nopref(int verbose_flag, const char *fmt, ...);
461 /* Logs a socket event, add local and remote ip address:port */
462 void serf__log_skt(int verbose_flag, const char *filename, apr_socket_t *skt,
463 const char *fmt, ...);