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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
18 * ====================================================================
21 #ifndef _SERF_PRIVATE_H_
22 #define _SERF_PRIVATE_H_
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. */
29 /* Windows does not define IOV_MAX, so we need to ensure it is defined. */
31 /* There is no limit for iovec count on Windows, but apr_socket_sendv
32 allocates WSABUF structures on stack if vecs_count <= 50. */
36 /* Older versions of APR do not have this macro. */
38 #define REQUESTED_MAX APR_SIZE_MAX
40 #define REQUESTED_MAX (~((apr_size_t)0))
43 #define SERF_IO_CLIENT (1)
44 #define SERF_IO_CONN (2)
45 #define SERF_IO_LISTENER (3)
47 /* Internal logging facilities, set flag to 1 to enable console logging for
48 the selected component. */
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
56 /* Older versions of APR do not have the APR_VERSION_AT_LEAST macro. Those
57 implementations are safe.
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.
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
69 typedef struct serf__authn_scheme_t serf__authn_scheme_t;
71 typedef struct serf_io_baton_t {
74 serf_incoming_t *client;
75 serf_connection_t *conn;
76 serf_listener_t *listener;
80 /* Holds all the information corresponding to a request/response pair. */
81 struct serf_request_t {
82 serf_connection_t *conn;
85 serf_bucket_alloc_t *allocator;
87 /* The bucket corresponding to the request. Will be NULL once the
88 * bucket has been emptied (for delivery into the socket).
90 serf_bucket_t *req_bkt;
92 serf_request_setup_t setup;
95 serf_response_acceptor_t acceptor;
98 serf_response_handler_t handler;
101 serf_bucket_t *resp_bkt;
105 /* 1 if this is a request to setup a SSL tunnel, 0 for normal requests. */
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
115 struct serf_request_t *next;
118 typedef struct serf_pollset_t {
119 /* the set of connections to poll */
120 apr_pollset_t *pollset;
123 typedef struct serf__authn_info_t {
124 const serf__authn_scheme_t *scheme;
128 int failed_authn_types;
129 } serf__authn_info_t;
131 struct serf_context_t {
132 /* the pool used for self and for other allocations */
136 serf_socket_add_t pollset_add;
137 serf_socket_remove_t pollset_rm;
139 /* one of our connections has a dirty pollset state. */
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])
146 /* Proxy server address */
147 apr_sockaddr_t *proxy_address;
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;
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 *
160 apr_hash_t *server_authn_info;
162 /* authentication info for the proxy configured in this context, shared by
164 serf__authn_info_t proxy_authn_info;
166 /* List of authn types supported by the client.*/
168 /* Callback function used to get credentials for a realm. */
169 serf_credentials_callback_t cred_cb;
172 struct serf_listener_t {
174 serf_io_baton_t baton;
179 serf_accept_client_t accept_func;
182 struct serf_incoming_t {
184 serf_io_baton_t baton;
186 serf_incoming_request_cb_t request;
191 /* States for the different stages in the lifecyle of a connection. */
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;
200 struct serf_connection_t {
204 serf_io_baton_t baton;
207 serf_bucket_alloc_t *allocator;
209 apr_sockaddr_t *address;
212 apr_pool_t *skt_pool;
214 /* the last reqevents we gave to pollset_add */
215 apr_int16_t reqevents;
217 /* the events we've seen for this connection in our returned pollset */
218 apr_int16_t seen_in_pollset;
220 /* are we a dirty connection that needs its poll status updated? */
223 /* number of completed requests we've sent */
224 unsigned int completed_requests;
226 /* number of completed responses we've got */
227 unsigned int completed_responses;
230 unsigned int probable_keepalive_limit;
232 /* Current state of the connection (whether or not it is connected). */
233 serf__connection_state_t state;
235 /* This connection may have responses without a request! */
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;
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.
248 serf_bucket_t *ostream_head;
249 serf_bucket_t *ostream_tail;
251 /* Aggregate bucket used to send the CONNECT request. */
252 serf_bucket_t *ssltunnel_ostream;
254 /* The list of active requests. */
255 serf_request_t *requests;
256 serf_request_t *requests_tail;
258 struct iovec vec[IOV_MAX];
261 serf_connection_setup_t setup;
263 serf_connection_closed_t closed;
266 /* Max. number of outstanding requests. */
267 unsigned int max_outstanding_requests;
271 /* Host url, path ommitted, syntax: https://svn.apache.org . */
272 const char *host_url;
274 /* Exploded host url, path ommitted. Only scheme, hostinfo, hostname &
275 port values are filled in. */
278 /* authentication info for this connection. */
279 serf__authn_info_t authn_info;
281 /* Time marker when connection begins. */
282 apr_time_t connect_time;
284 /* Calculated connection latency. Negative value if latency is unknown. */
285 apr_interval_time_t latency;
287 /* Needs to read first before we can write again. */
291 /*** Internal bucket functions ***/
293 /** Transform a response_bucket in-place into an aggregate bucket. Restore the
294 status line and all headers, not just the body.
296 This can only be used when we haven't started reading the body of the
299 Keep internal for now, probably only useful within serf.
301 apr_status_t serf_response_full_become_aggregate(serf_bucket_t *bucket);
304 * Remove the header from the list, do nothing if the header wasn't added.
306 void serf__bucket_headers_remove(serf_bucket_t *headers_bucket,
309 /*** Authentication handler declarations ***/
311 typedef enum { PROXY, HOST } peer_t;
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.
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,
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.
333 (*serf__init_context_func_t)(int code,
334 serf_context_t *conn,
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.
343 (*serf__init_conn_func_t)(const serf__authn_scheme_t *scheme,
345 serf_connection_t *conn,
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).
355 (*serf__setup_request_func_t)(peer_t peer,
357 serf_connection_t *conn,
358 serf_request_t *request,
361 serf_bucket_t *hdrs_bkt);
364 * This function will be called when a response is received, so that the
365 * scheme handler can validate the Authentication related response headers
369 (*serf__validate_response_func_t)(const serf__authn_scheme_t *scheme,
372 serf_connection_t *conn,
373 serf_request_t *request,
374 serf_bucket_t *response,
378 * serf__authn_scheme_t: vtable for an authn scheme provider.
380 struct serf__authn_scheme_t {
381 /* The name of this authentication scheme. Used in headers of requests and
385 /* Key is the name of the authentication scheme in lower case, to
386 facilitate case insensitive matching of the response headers. */
389 /* Internal code used for this authn type. */
392 /* The context initialization function if any; otherwise, NULL */
393 serf__init_context_func_t init_ctx_func;
395 /* The connection initialization function if any; otherwise, NULL */
396 serf__init_conn_func_t init_conn_func;
398 /* The authentication handler function */
399 serf__auth_handler_func_t handle_func;
401 /* Function to set up the authentication header of a request */
402 serf__setup_request_func_t setup_request_func;
404 /* Function to validate the authentication header of a response */
405 serf__validate_response_func_t validate_response_func;
409 * Handles a 401 or 407 response, tries the different available authentication
412 apr_status_t serf__handle_auth_response(int *consumed_response,
413 serf_request_t *request,
414 serf_bucket_t *response,
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);
426 /* fromt context.c */
427 void serf__context_progress_delta(void *progress_baton, apr_off_t read,
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);
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,
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,
442 apr_status_t serf__provide_credentials(serf_context_t *ctx,
445 serf_request_t *request,
447 int code, const char *authn_type,
451 /* from ssltunnel.c */
452 apr_status_t serf__ssltunnel_connect(serf_connection_t *conn);
455 /** Logging functions. Use one of the [COMP]_VERBOSE flags to enable specific
459 /* Logs a standard event, with filename & timestamp header */
460 void serf__log(int verbose_flag, const char *filename, const char *fmt, ...);
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, ...);
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, ...);