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/* There is no limit for iovec count on Windows, but apr_socket_sendv 27 allocates WSABUF structures on stack if vecs_count <= 50. */ 28#define IOV_MAX 50 29#endif 30 31/* Older versions of APR do not have this macro. */ 32#ifdef APR_SIZE_MAX 33#define REQUESTED_MAX APR_SIZE_MAX 34#else 35#define REQUESTED_MAX (~((apr_size_t)0)) 36#endif 37 38#define SERF_IO_CLIENT (1) 39#define SERF_IO_CONN (2) 40#define SERF_IO_LISTENER (3) 41 42/* Internal logging facilities, set flag to 1 to enable console logging for 43 the selected component. */ 44#define SSL_VERBOSE 0 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 50 51/* Older versions of APR do not have the APR_VERSION_AT_LEAST macro. Those 52 implementations are safe. 53 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. 56 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 61#endif 62#endif 63 64typedef struct serf__authn_scheme_t serf__authn_scheme_t; 65 66typedef struct serf_io_baton_t { 67 int type; 68 union { 69 serf_incoming_t *client; 70 serf_connection_t *conn; 71 serf_listener_t *listener; 72 } u; 73} serf_io_baton_t; 74 75/* Holds all the information corresponding to a request/response pair. */ 76struct serf_request_t { 77 serf_connection_t *conn; 78 79 apr_pool_t *respool; 80 serf_bucket_alloc_t *allocator; 81 82 /* The bucket corresponding to the request. Will be NULL once the 83 * bucket has been emptied (for delivery into the socket). 84 */ 85 serf_bucket_t *req_bkt; 86 87 serf_request_setup_t setup; 88 void *setup_baton; 89 90 serf_response_acceptor_t acceptor; 91 void *acceptor_baton; 92 93 serf_response_handler_t handler; 94 void *handler_baton; 95 96 serf_bucket_t *resp_bkt; 97 98 int writing_started; 99 int priority; 100 /* 1 if this is a request to setup a SSL tunnel, 0 for normal requests. */ 101 int ssltunnel; 102 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 107 anymore. */ 108 void *auth_baton; 109 110 struct serf_request_t *next; 111}; 112 113typedef struct serf_pollset_t { 114 /* the set of connections to poll */ 115 apr_pollset_t *pollset; 116} serf_pollset_t; 117 118typedef struct serf__authn_info_t { 119 const serf__authn_scheme_t *scheme; 120 121 void *baton; 122 123 int failed_authn_types; 124} serf__authn_info_t; 125 126struct serf_context_t { 127 /* the pool used for self and for other allocations */ 128 apr_pool_t *pool; 129 130 void *pollset_baton; 131 serf_socket_add_t pollset_add; 132 serf_socket_remove_t pollset_rm; 133 134 /* one of our connections has a dirty pollset state. */ 135 int dirty_pollset; 136 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]) 140 141 /* Proxy server address */ 142 apr_sockaddr_t *proxy_address; 143 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; 149 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 * 154 */ 155 apr_hash_t *server_authn_info; 156 157 /* authentication info for the proxy configured in this context, shared by 158 all connections. */ 159 serf__authn_info_t proxy_authn_info; 160 161 /* List of authn types supported by the client.*/ 162 int authn_types; 163 /* Callback function used to get credentials for a realm. */ 164 serf_credentials_callback_t cred_cb; 165}; 166 167struct serf_listener_t { 168 serf_context_t *ctx; 169 serf_io_baton_t baton; 170 apr_socket_t *skt; 171 apr_pool_t *pool; 172 apr_pollfd_t desc; 173 void *accept_baton; 174 serf_accept_client_t accept_func; 175}; 176 177struct serf_incoming_t { 178 serf_context_t *ctx; 179 serf_io_baton_t baton; 180 void *request_baton; 181 serf_incoming_request_cb_t request; 182 apr_socket_t *skt; 183 apr_pollfd_t desc; 184}; 185 186/* States for the different stages in the lifecyle of a connection. */ 187typedef enum { 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; 194 195struct serf_connection_t { 196 serf_context_t *ctx; 197 198 apr_status_t status; 199 serf_io_baton_t baton; 200 201 apr_pool_t *pool; 202 serf_bucket_alloc_t *allocator; 203 204 apr_sockaddr_t *address; 205 206 apr_socket_t *skt; 207 apr_pool_t *skt_pool; 208 209 /* the last reqevents we gave to pollset_add */ 210 apr_int16_t reqevents; 211 212 /* the events we've seen for this connection in our returned pollset */ 213 apr_int16_t seen_in_pollset; 214 215 /* are we a dirty connection that needs its poll status updated? */ 216 int dirty_conn; 217 218 /* number of completed requests we've sent */ 219 unsigned int completed_requests; 220 221 /* number of completed responses we've got */ 222 unsigned int completed_responses; 223 224 /* keepalive */ 225 unsigned int probable_keepalive_limit; 226 227 /* Current state of the connection (whether or not it is connected). */ 228 serf__connection_state_t state; 229 230 /* This connection may have responses without a request! */ 231 int async_responses; 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; 237 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. 242 */ 243 serf_bucket_t *ostream_head; 244 serf_bucket_t *ostream_tail; 245 246 /* Aggregate bucket used to send the CONNECT request. */ 247 serf_bucket_t *ssltunnel_ostream; 248 249 /* The list of active requests. */ 250 serf_request_t *requests; 251 serf_request_t *requests_tail; 252 253 struct iovec vec[IOV_MAX]; 254 int vec_len; 255 256 serf_connection_setup_t setup; 257 void *setup_baton; 258 serf_connection_closed_t closed; 259 void *closed_baton; 260 261 /* Max. number of outstanding requests. */ 262 unsigned int max_outstanding_requests; 263 264 int hit_eof; 265 266 /* Host url, path ommitted, syntax: https://svn.apache.org . */ 267 const char *host_url; 268 269 /* Exploded host url, path ommitted. Only scheme, hostinfo, hostname & 270 port values are filled in. */ 271 apr_uri_t host_info; 272 273 /* authentication info for this connection. */ 274 serf__authn_info_t authn_info; 275 276 /* Time marker when connection begins. */ 277 apr_time_t connect_time; 278 279 /* Calculated connection latency. Negative value if latency is unknown. */ 280 apr_interval_time_t latency; 281 282 /* Needs to read first before we can write again. */ 283 int stop_writing; 284}; 285 286/*** Internal bucket functions ***/ 287 288/** Transform a response_bucket in-place into an aggregate bucket. Restore the 289 status line and all headers, not just the body. 290 291 This can only be used when we haven't started reading the body of the 292 response yet. 293 294 Keep internal for now, probably only useful within serf. 295 */ 296apr_status_t serf_response_full_become_aggregate(serf_bucket_t *bucket); 297 298/** 299 * Remove the header from the list, do nothing if the header wasn't added. 300 */ 301void serf__bucket_headers_remove(serf_bucket_t *headers_bucket, 302 const char *header); 303 304/*** Authentication handler declarations ***/ 305 306typedef enum { PROXY, HOST } peer_t; 307 308/** 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. 312 */ 313typedef apr_status_t 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, 319 void *baton, 320 apr_pool_t *pool); 321 322/** 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. 326 */ 327typedef apr_status_t 328(*serf__init_context_func_t)(int code, 329 serf_context_t *conn, 330 apr_pool_t *pool); 331 332/** 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. 336 */ 337typedef apr_status_t 338(*serf__init_conn_func_t)(const serf__authn_scheme_t *scheme, 339 int code, 340 serf_connection_t *conn, 341 apr_pool_t *pool); 342 343/** 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). 348 */ 349typedef apr_status_t 350(*serf__setup_request_func_t)(peer_t peer, 351 int code, 352 serf_connection_t *conn, 353 serf_request_t *request, 354 const char *method, 355 const char *uri, 356 serf_bucket_t *hdrs_bkt); 357 358/** 359 * This function will be called when a response is received, so that the 360 * scheme handler can validate the Authentication related response headers 361 * (if needed). 362 */ 363typedef apr_status_t 364(*serf__validate_response_func_t)(const serf__authn_scheme_t *scheme, 365 peer_t peer, 366 int code, 367 serf_connection_t *conn, 368 serf_request_t *request, 369 serf_bucket_t *response, 370 apr_pool_t *pool); 371 372/** 373 * serf__authn_scheme_t: vtable for an authn scheme provider. 374 */ 375struct serf__authn_scheme_t { 376 /* The name of this authentication scheme. Used in headers of requests and 377 for logging. */ 378 const char *name; 379 380 /* Key is the name of the authentication scheme in lower case, to 381 facilitate case insensitive matching of the response headers. */ 382 const char *key; 383 384 /* Internal code used for this authn type. */ 385 int type; 386 387 /* The context initialization function if any; otherwise, NULL */ 388 serf__init_context_func_t init_ctx_func; 389 390 /* The connection initialization function if any; otherwise, NULL */ 391 serf__init_conn_func_t init_conn_func; 392 393 /* The authentication handler function */ 394 serf__auth_handler_func_t handle_func; 395 396 /* Function to set up the authentication header of a request */ 397 serf__setup_request_func_t setup_request_func; 398 399 /* Function to validate the authentication header of a response */ 400 serf__validate_response_func_t validate_response_func; 401}; 402 403/** 404 * Handles a 401 or 407 response, tries the different available authentication 405 * handlers. 406 */ 407apr_status_t serf__handle_auth_response(int *consumed_response, 408 serf_request_t *request, 409 serf_bucket_t *response, 410 void *baton, 411 apr_pool_t *pool); 412 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. */ 419serf__authn_info_t *serf__get_authn_info_for_server(serf_connection_t *conn); 420 421/* fromt context.c */ 422void serf__context_progress_delta(void *progress_baton, apr_off_t read, 423 apr_off_t written); 424 425/* from incoming.c */ 426apr_status_t serf__process_client(serf_incoming_t *l, apr_int16_t events); 427apr_status_t serf__process_listener(serf_listener_t *l); 428 429/* from outgoing.c */ 430apr_status_t serf__open_connections(serf_context_t *ctx); 431apr_status_t serf__process_connection(serf_connection_t *conn, 432 apr_int16_t events); 433apr_status_t serf__conn_update_pollset(serf_connection_t *conn); 434serf_request_t *serf__ssltunnel_request_create(serf_connection_t *conn, 435 serf_request_setup_t setup, 436 void *setup_baton); 437apr_status_t serf__provide_credentials(serf_context_t *ctx, 438 char **username, 439 char **password, 440 serf_request_t *request, 441 void *baton, 442 int code, const char *authn_type, 443 const char *realm, 444 apr_pool_t *pool); 445 446/* from ssltunnel.c */ 447apr_status_t serf__ssltunnel_connect(serf_connection_t *conn); 448 449 450/** Logging functions. Use one of the [COMP]_VERBOSE flags to enable specific 451 logging. 452 **/ 453 454/* Logs a standard event, with filename & timestamp header */ 455void serf__log(int verbose_flag, const char *filename, const char *fmt, ...); 456 457/* Logs a standard event, but without prefix. This is useful to build up 458 log lines in parts. */ 459void serf__log_nopref(int verbose_flag, const char *fmt, ...); 460 461/* Logs a socket event, add local and remote ip address:port */ 462void serf__log_skt(int verbose_flag, const char *filename, apr_socket_t *skt, 463 const char *fmt, ...); 464 465#endif 466