Blame serf.h

Packit 3adb1e
/* ====================================================================
Packit 3adb1e
 *    Licensed to the Apache Software Foundation (ASF) under one
Packit 3adb1e
 *    or more contributor license agreements.  See the NOTICE file
Packit 3adb1e
 *    distributed with this work for additional information
Packit 3adb1e
 *    regarding copyright ownership.  The ASF licenses this file
Packit 3adb1e
 *    to you under the Apache License, Version 2.0 (the
Packit 3adb1e
 *    "License"); you may not use this file except in compliance
Packit 3adb1e
 *    with the License.  You may obtain a copy of the License at
Packit 3adb1e
 *
Packit 3adb1e
 *      http://www.apache.org/licenses/LICENSE-2.0
Packit 3adb1e
 *
Packit 3adb1e
 *    Unless required by applicable law or agreed to in writing,
Packit 3adb1e
 *    software distributed under the License is distributed on an
Packit 3adb1e
 *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
Packit 3adb1e
 *    KIND, either express or implied.  See the License for the
Packit 3adb1e
 *    specific language governing permissions and limitations
Packit 3adb1e
 *    under the License.
Packit 3adb1e
 * ====================================================================
Packit 3adb1e
 */
Packit 3adb1e
Packit 3adb1e
#ifndef SERF_H
Packit 3adb1e
#define SERF_H
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * @file serf.h
Packit 3adb1e
 * @brief Main serf header file
Packit 3adb1e
 */
Packit 3adb1e
Packit 3adb1e
#include <apr.h>
Packit 3adb1e
#include <apr_errno.h>
Packit 3adb1e
#include <apr_allocator.h>
Packit 3adb1e
#include <apr_pools.h>
Packit 3adb1e
#include <apr_network_io.h>
Packit 3adb1e
#include <apr_time.h>
Packit 3adb1e
#include <apr_poll.h>
Packit 3adb1e
#include <apr_uri.h>
Packit 3adb1e
Packit 3adb1e
#ifdef __cplusplus
Packit 3adb1e
extern "C" {
Packit 3adb1e
#endif
Packit 3adb1e
Packit 3adb1e
/* Forward declare some structures */
Packit 3adb1e
typedef struct serf_context_t serf_context_t;
Packit 3adb1e
Packit 3adb1e
typedef struct serf_bucket_t serf_bucket_t;
Packit 3adb1e
typedef struct serf_bucket_type_t serf_bucket_type_t;
Packit 3adb1e
typedef struct serf_bucket_alloc_t serf_bucket_alloc_t;
Packit 3adb1e
Packit 3adb1e
typedef struct serf_connection_t serf_connection_t;
Packit 3adb1e
typedef struct serf_listener_t serf_listener_t;
Packit 3adb1e
typedef struct serf_incoming_t serf_incoming_t;
Packit 3adb1e
typedef struct serf_incoming_request_t serf_incoming_request_t;
Packit 3adb1e
Packit 3adb1e
typedef struct serf_request_t serf_request_t;
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * @defgroup serf high-level constructs
Packit 3adb1e
 * @ingroup serf
Packit 3adb1e
 * @{
Packit 3adb1e
 */
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Serf-specific error codes
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_ERROR_RANGE 100
Packit 3adb1e
#define SERF_ERROR_START (APR_OS_START_USERERR + SERF_ERROR_RANGE)
Packit 3adb1e
Packit 3adb1e
/* This code is for when this is the last response on this connection:
Packit 3adb1e
 * i.e. do not send any more requests on this connection or expect
Packit 3adb1e
 * any more responses.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_ERROR_CLOSING (SERF_ERROR_START + 1)
Packit 3adb1e
/* This code is for when the connection terminated before the request
Packit 3adb1e
 * could be processed on the other side.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_ERROR_REQUEST_LOST (SERF_ERROR_START + 2)
Packit 3adb1e
/* This code is for when the connection is blocked - we can not proceed
Packit 3adb1e
 * until something happens - generally due to SSL negotiation-like behavior
Packit 3adb1e
 * where a write() is blocked until a read() is processed.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_ERROR_WAIT_CONN (SERF_ERROR_START + 3)
Packit 3adb1e
/* This code is for when something went wrong during deflating compressed
Packit 3adb1e
 * data e.g. a CRC error. */
Packit 3adb1e
#define SERF_ERROR_DECOMPRESSION_FAILED (SERF_ERROR_START + 4)
Packit 3adb1e
/* This code is for when a response received from a http server is not in
Packit 3adb1e
 * http-compliant syntax. */
Packit 3adb1e
#define SERF_ERROR_BAD_HTTP_RESPONSE (SERF_ERROR_START + 5)
Packit 3adb1e
/* The server sent less data than what was announced. */
Packit 3adb1e
#define SERF_ERROR_TRUNCATED_HTTP_RESPONSE (SERF_ERROR_START + 6)
Packit 3adb1e
/* The proxy server returned an error while setting up the SSL tunnel. */
Packit 3adb1e
#define SERF_ERROR_SSLTUNNEL_SETUP_FAILED (SERF_ERROR_START + 7)
Packit 3adb1e
/* The server unexpectedly closed the connection prematurely. */
Packit 3adb1e
#define SERF_ERROR_ABORTED_CONNECTION (SERF_ERROR_START + 8)
Packit 3adb1e
Packit 3adb1e
/* SSL certificates related errors */
Packit 3adb1e
#define SERF_ERROR_SSL_CERT_FAILED (SERF_ERROR_START + 70)
Packit 3adb1e
Packit 3adb1e
/* SSL communications related errors */
Packit 3adb1e
#define SERF_ERROR_SSL_COMM_FAILED (SERF_ERROR_START + 71)
Packit 3adb1e
Packit 3adb1e
/* General authentication related errors */
Packit 3adb1e
#define SERF_ERROR_AUTHN_FAILED (SERF_ERROR_START + 90)
Packit 3adb1e
Packit 3adb1e
/* None of the available authn mechanisms for the request are supported */
Packit 3adb1e
#define SERF_ERROR_AUTHN_NOT_SUPPORTED (SERF_ERROR_START + 91)
Packit 3adb1e
Packit 3adb1e
/* Authn was requested by the server but the header lacked some attribute  */
Packit 3adb1e
#define SERF_ERROR_AUTHN_MISSING_ATTRIBUTE (SERF_ERROR_START + 92)
Packit 3adb1e
Packit 3adb1e
/* Authentication handler initialization related errors */
Packit 3adb1e
#define SERF_ERROR_AUTHN_INITALIZATION_FAILED (SERF_ERROR_START + 93)
Packit 3adb1e
Packit 3adb1e
/* Error code reserved for use in the test suite. */
Packit 3adb1e
#define SERF_ERROR_ISSUE_IN_TESTSUITE (SERF_ERROR_START + 99)
Packit 3adb1e
Packit 3adb1e
/* This macro groups errors potentially raised when reading a http response.  */
Packit 3adb1e
#define SERF_BAD_RESPONSE_ERROR(status) ((status) \
Packit 3adb1e
    && ((SERF_ERROR_DECOMPRESSION_FAILED == (status)) \
Packit 3adb1e
        ||(SERF_ERROR_BAD_HTTP_RESPONSE == (status)) \
Packit 3adb1e
        ||(SERF_ERROR_TRUNCATED_HTTP_RESPONSE == (status))))
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Return a string that describes the specified error code.
Packit 3adb1e
 *
Packit 3adb1e
 * If the error code is not one of the above Serf error codes, then
Packit 3adb1e
 * NULL will be returned.
Packit 3adb1e
 *
Packit 3adb1e
 * Note regarding lifetime: the string is a statically-allocated constant
Packit 3adb1e
 */
Packit 3adb1e
const char *serf_error_string(apr_status_t errcode);
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Create a new context for serf operations.
Packit 3adb1e
 *
Packit 3adb1e
 * A serf context defines a control loop which processes multiple
Packit 3adb1e
 * connections simultaneously.
Packit 3adb1e
 *
Packit 3adb1e
 * The context will be allocated within @a pool.
Packit 3adb1e
 */
Packit 3adb1e
serf_context_t *serf_context_create(
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Callback function. Add a socket to the externally managed poll set.
Packit 3adb1e
 *
Packit 3adb1e
 * Both @a pfd and @a serf_baton should be used when calling serf_event_trigger
Packit 3adb1e
 * later.
Packit 3adb1e
 */
Packit 3adb1e
typedef apr_status_t (*serf_socket_add_t)(
Packit 3adb1e
    void *user_baton,
Packit 3adb1e
    apr_pollfd_t *pfd,
Packit 3adb1e
    void *serf_baton);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Callback function. Remove the socket, identified by both @a pfd and
Packit 3adb1e
 * @a serf_baton from the externally managed poll set.
Packit 3adb1e
 */
Packit 3adb1e
typedef apr_status_t (*serf_socket_remove_t)(
Packit 3adb1e
    void *user_baton,
Packit 3adb1e
    apr_pollfd_t *pfd,
Packit 3adb1e
    void *serf_baton);
Packit 3adb1e
Packit 3adb1e
/* Create a new context for serf operations.
Packit 3adb1e
 *
Packit 3adb1e
 * Use this function to make serf not use its internal control loop, but
Packit 3adb1e
 * instead rely on an external event loop. Serf will use the @a addf and @a rmf
Packit 3adb1e
 * callbacks to notify of any event on a connection. The @a user_baton will be
Packit 3adb1e
 * passed through the addf and rmf callbacks.
Packit 3adb1e
 *
Packit 3adb1e
 * The context will be allocated within @a pool.
Packit 3adb1e
 */
Packit 3adb1e
serf_context_t *serf_context_create_ex(
Packit 3adb1e
    void *user_baton,
Packit 3adb1e
    serf_socket_add_t addf,
Packit 3adb1e
    serf_socket_remove_t rmf,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Make serf process events on a connection, identified by both @a pfd and
Packit 3adb1e
 * @a serf_baton.
Packit 3adb1e
 *
Packit 3adb1e
 * Any outbound data is delivered, and incoming data is made available to
Packit 3adb1e
 * the associated response handlers and their buckets.
Packit 3adb1e
 *
Packit 3adb1e
 * If any data is processed (incoming or outgoing), then this function will
Packit 3adb1e
 * return with APR_SUCCESS.
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_event_trigger(
Packit 3adb1e
    serf_context_t *s,
Packit 3adb1e
    void *serf_baton,
Packit 3adb1e
    const apr_pollfd_t *pfd);
Packit 3adb1e
Packit 3adb1e
/** @see serf_context_run should not block at all. */
Packit 3adb1e
#define SERF_DURATION_NOBLOCK 0
Packit 3adb1e
/** @see serf_context_run should run for (nearly) "forever". */
Packit 3adb1e
#define SERF_DURATION_FOREVER 2000000000        /* approx 1^31 */
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Run the main networking control loop.
Packit 3adb1e
 *
Packit 3adb1e
 * The set of connections defined by the serf context @a ctx are processed.
Packit 3adb1e
 * Any outbound data is delivered, and incoming data is made available to
Packit 3adb1e
 * the associated response handlers and their buckets. This function will
Packit 3adb1e
 * block on the network for no longer than @a duration microseconds.
Packit 3adb1e
 *
Packit 3adb1e
 * If any data is processed (incoming or outgoing), then this function will
Packit 3adb1e
 * return with APR_SUCCESS. Typically, the caller will just want to call it
Packit 3adb1e
 * again to continue processing data.
Packit 3adb1e
 *
Packit 3adb1e
 * If no activity occurs within the specified timeout duration, then
Packit 3adb1e
 * APR_TIMEUP is returned.
Packit 3adb1e
 *
Packit 3adb1e
 * All temporary allocations will be made in @a pool.
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_context_run(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    apr_short_interval_time_t duration,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
apr_status_t serf_context_prerun(
Packit 3adb1e
    serf_context_t *ctx);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Callback function for progress information. @a progress indicates cumulative
Packit 3adb1e
 * number of bytes read or written, for the whole context.
Packit 3adb1e
 */
Packit 3adb1e
typedef void (*serf_progress_t)(
Packit 3adb1e
    void *progress_baton,
Packit 3adb1e
    apr_off_t read,
Packit 3adb1e
    apr_off_t write);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Sets the progress callback function. @a progress_func will be called every
Packit 3adb1e
 * time bytes are read of or written on a socket.
Packit 3adb1e
 */
Packit 3adb1e
void serf_context_set_progress_cb(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    const serf_progress_t progress_func,
Packit 3adb1e
    void *progress_baton);
Packit 3adb1e
Packit 3adb1e
/** @} */
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * @defgroup serf connections and requests
Packit 3adb1e
 * @ingroup serf
Packit 3adb1e
 * @{
Packit 3adb1e
 */
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * When a connection is established, the application needs to wrap some
Packit 3adb1e
 * buckets around @a skt to enable serf to process incoming responses. This
Packit 3adb1e
 * is the control point for assembling connection-level processing logic
Packit 3adb1e
 * around the given socket.
Packit 3adb1e
 *
Packit 3adb1e
 * The @a setup_baton is the baton established at connection creation time.
Packit 3adb1e
 *
Packit 3adb1e
 * This callback corresponds to reading from the server. Since this is an
Packit 3adb1e
 * on-demand activity, we use a callback. The corresponding write operation
Packit 3adb1e
 * is based on the @see serf_request_deliver function, where the application
Packit 3adb1e
 * can assemble the appropriate bucket(s) before delivery.
Packit 3adb1e
 *
Packit 3adb1e
 * The returned bucket should live at least as long as the connection itself.
Packit 3adb1e
 * It is assumed that an appropriate allocator is passed in @a setup_baton.
Packit 3adb1e
 * ### we may want to create a connection-level allocator and pass that
Packit 3adb1e
 * ### along. however, that allocator would *only* be used for this
Packit 3adb1e
 * ### callback. it may be wasteful to create a per-conn allocator, so this
Packit 3adb1e
 * ### baton-based, app-responsible form might be best.
Packit 3adb1e
 *
Packit 3adb1e
 * Responsibility for the buckets is passed to the serf library. They will be
Packit 3adb1e
 * destroyed when the connection is closed.
Packit 3adb1e
 *
Packit 3adb1e
 * All temporary allocations should be made in @a pool.
Packit 3adb1e
 */
Packit 3adb1e
typedef apr_status_t (*serf_connection_setup_t)(
Packit 3adb1e
    apr_socket_t *skt,
Packit 3adb1e
    serf_bucket_t **read_bkt,
Packit 3adb1e
    serf_bucket_t **write_bkt,
Packit 3adb1e
    void *setup_baton,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * ### need to update docco w.r.t socket. became "stream" recently.
Packit 3adb1e
 * ### the stream does not have a barrier, this callback should generally
Packit 3adb1e
 * ### add a barrier around the stream before incorporating it into a
Packit 3adb1e
 * ### response bucket stack.
Packit 3adb1e
 * ### should serf add the barrier automatically to protect its data
Packit 3adb1e
 * ### structure? i.e. the passed bucket becomes owned rather than
Packit 3adb1e
 * ### borrowed. that might suit overall semantics better.
Packit 3adb1e
 * Accept an incoming response for @a request, and its @a socket. A bucket
Packit 3adb1e
 * for the response should be constructed and returned. This is the control
Packit 3adb1e
 * point for assembling the appropriate wrapper buckets around the socket to
Packit 3adb1e
 * enable processing of the incoming response.
Packit 3adb1e
 *
Packit 3adb1e
 * The @a acceptor_baton is the baton provided when the specified request
Packit 3adb1e
 * was created.
Packit 3adb1e
 *
Packit 3adb1e
 * The request's pool and bucket allocator should be used for any allocations
Packit 3adb1e
 * that need to live for the duration of the response. Care should be taken
Packit 3adb1e
 * to bound the amount of memory stored in this pool -- to ensure that
Packit 3adb1e
 * allocations are not proportional to the amount of data in the response.
Packit 3adb1e
 *
Packit 3adb1e
 * Responsibility for the bucket is passed to the serf library. It will be
Packit 3adb1e
 * destroyed when the response has been fully read (the bucket returns an
Packit 3adb1e
 * APR_EOF status from its read functions).
Packit 3adb1e
 *
Packit 3adb1e
 * All temporary allocations should be made in @a pool.
Packit 3adb1e
 */
Packit 3adb1e
/* ### do we need to return an error? */
Packit 3adb1e
typedef serf_bucket_t * (*serf_response_acceptor_t)(
Packit 3adb1e
    serf_request_t *request,
Packit 3adb1e
    serf_bucket_t *stream,
Packit 3adb1e
    void *acceptor_baton,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Notification callback for when a connection closes.
Packit 3adb1e
 *
Packit 3adb1e
 * This callback is used to inform an application that the @a conn
Packit 3adb1e
 * connection has been (abnormally) closed. The @a closed_baton is the
Packit 3adb1e
 * baton provided when the connection was first opened. The reason for
Packit 3adb1e
 * closure is given in @a why, and will be APR_SUCCESS if the application
Packit 3adb1e
 * requested closure (by clearing the pool used to allocate this
Packit 3adb1e
 * connection or calling serf_connection_close).
Packit 3adb1e
 *
Packit 3adb1e
 * All temporary allocations should be made in @a pool.
Packit 3adb1e
 */
Packit 3adb1e
typedef void (*serf_connection_closed_t)(
Packit 3adb1e
    serf_connection_t *conn,
Packit 3adb1e
    void *closed_baton,
Packit 3adb1e
    apr_status_t why,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Response data has arrived and should be processed.
Packit 3adb1e
 *
Packit 3adb1e
 * Whenever response data for @a request arrives (initially, or continued data
Packit 3adb1e
 * arrival), this handler is invoked. The response data is available in the
Packit 3adb1e
 * @a response bucket. The @a handler_baton is passed along from the baton
Packit 3adb1e
 * provided by the request setup callback (@see serf_request_setup_t).
Packit 3adb1e
 *
Packit 3adb1e
 * The handler MUST process data from the @a response bucket until the
Packit 3adb1e
 * bucket's read function states it would block (see APR_STATUS_IS_EAGAIN).
Packit 3adb1e
 * The handler is invoked only when new data arrives. If no further data
Packit 3adb1e
 * arrives, and the handler does not process all available data, then the
Packit 3adb1e
 * system can result in a deadlock around the unprocessed, but read, data.
Packit 3adb1e
 *
Packit 3adb1e
 * The handler should return APR_EOF when the response has been fully read.
Packit 3adb1e
 * If calling the handler again would block, APR_EAGAIN should be returned.
Packit 3adb1e
 * If the handler should be invoked again, simply return APR_SUCCESS.
Packit 3adb1e
 *
Packit 3adb1e
 * Note: if the connection closed (at the request of the application, or
Packit 3adb1e
 * because of an (abnormal) termination) while a request is being delivered,
Packit 3adb1e
 * or before a response arrives, then @a response will be NULL. This is the
Packit 3adb1e
 * signal that the request was not delivered properly, and no further
Packit 3adb1e
 * response should be expected (this callback will not be invoked again).
Packit 3adb1e
 * If a request is injected into the connection (during this callback's
Packit 3adb1e
 * execution, or otherwise), then the connection will be reopened.
Packit 3adb1e
 *
Packit 3adb1e
 * All temporary allocations should be made in @a pool.
Packit 3adb1e
 */
Packit 3adb1e
typedef apr_status_t (*serf_response_handler_t)(
Packit 3adb1e
    serf_request_t *request,
Packit 3adb1e
    serf_bucket_t *response,
Packit 3adb1e
    void *handler_baton,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Callback function to be implemented by the application, so that serf
Packit 3adb1e
 * can handle server and proxy authentication.
Packit 3adb1e
 * code = 401 (server) or 407 (proxy).
Packit 3adb1e
 * baton = the baton passed to serf_context_run.
Packit 3adb1e
 * authn_type = one of "Basic", "Digest".
Packit 3adb1e
 */
Packit 3adb1e
typedef apr_status_t (*serf_credentials_callback_t)(
Packit 3adb1e
    char **username,
Packit 3adb1e
    char **password,
Packit 3adb1e
    serf_request_t *request, void *baton,
Packit 3adb1e
    int code, const char *authn_type,
Packit 3adb1e
    const char *realm,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Create a new connection associated with the @a ctx serf context.
Packit 3adb1e
 *
Packit 3adb1e
 * If no proxy server is configured, a connection will be created to
Packit 3adb1e
 * (eventually) connect to the address specified by @a address. The address must
Packit 3adb1e
 * live at least as long as @a pool (thus, as long as the connection object).
Packit 3adb1e
 * If a proxy server is configured, @address will be ignored.
Packit 3adb1e
 *
Packit 3adb1e
 * The connection object will be allocated within @a pool. Clearing or
Packit 3adb1e
 * destroying this pool will close the connection, and terminate any
Packit 3adb1e
 * outstanding requests or responses.
Packit 3adb1e
 *
Packit 3adb1e
 * When the connection is closed (upon request or because of an error),
Packit 3adb1e
 * then the @a closed callback is invoked, and @a closed_baton is passed.
Packit 3adb1e
 *
Packit 3adb1e
 * ### doc on setup(_baton). tweak below comment re: acceptor.
Packit 3adb1e
 * NULL may be passed for @a acceptor and @a closed; default implementations
Packit 3adb1e
 * will be used.
Packit 3adb1e
 *
Packit 3adb1e
 * Note: the connection is not made immediately. It will be opened on
Packit 3adb1e
 * the next call to @see serf_context_run.
Packit 3adb1e
 */
Packit 3adb1e
serf_connection_t *serf_connection_create(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    apr_sockaddr_t *address,
Packit 3adb1e
    serf_connection_setup_t setup,
Packit 3adb1e
    void *setup_baton,
Packit 3adb1e
    serf_connection_closed_t closed,
Packit 3adb1e
    void *closed_baton,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Create a new connection associated with the @a ctx serf context.
Packit 3adb1e
 *
Packit 3adb1e
 * A connection will be created to (eventually) connect to the address
Packit 3adb1e
 * specified by @a address. The address must live at least as long as
Packit 3adb1e
 * @a pool (thus, as long as the connection object).
Packit 3adb1e
 *
Packit 3adb1e
 * The host address will be looked up based on the hostname in @a host_info.
Packit 3adb1e
 *
Packit 3adb1e
 * The connection object will be allocated within @a pool. Clearing or
Packit 3adb1e
 * destroying this pool will close the connection, and terminate any
Packit 3adb1e
 * outstanding requests or responses.
Packit 3adb1e
 *
Packit 3adb1e
 * When the connection is closed (upon request or because of an error),
Packit 3adb1e
 * then the @a closed callback is invoked, and @a closed_baton is passed.
Packit 3adb1e
 *
Packit 3adb1e
 * ### doc on setup(_baton). tweak below comment re: acceptor.
Packit 3adb1e
 * NULL may be passed for @a acceptor and @a closed; default implementations
Packit 3adb1e
 * will be used.
Packit 3adb1e
 *
Packit 3adb1e
 * Note: the connection is not made immediately. It will be opened on
Packit 3adb1e
 * the next call to @see serf_context_run.
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_connection_create2(
Packit 3adb1e
    serf_connection_t **conn,
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    apr_uri_t host_info,
Packit 3adb1e
    serf_connection_setup_t setup,
Packit 3adb1e
    void *setup_baton,
Packit 3adb1e
    serf_connection_closed_t closed,
Packit 3adb1e
    void *closed_baton,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
typedef apr_status_t (*serf_accept_client_t)(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    serf_listener_t *l,
Packit 3adb1e
    void *accept_baton,
Packit 3adb1e
    apr_socket_t *insock,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
apr_status_t serf_listener_create(
Packit 3adb1e
    serf_listener_t **listener,
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    const char *host,
Packit 3adb1e
    apr_uint16_t port,
Packit 3adb1e
    void *accept_baton,
Packit 3adb1e
    serf_accept_client_t accept_func,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
typedef apr_status_t (*serf_incoming_request_cb_t)(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    serf_incoming_request_t *req,
Packit 3adb1e
    void *request_baton,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
apr_status_t serf_incoming_create(
Packit 3adb1e
    serf_incoming_t **client,
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    apr_socket_t *insock,
Packit 3adb1e
    void *request_baton,
Packit 3adb1e
    serf_incoming_request_cb_t request,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Reset the connection, but re-open the socket again.
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_connection_reset(
Packit 3adb1e
    serf_connection_t *conn);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Close the connection associated with @a conn and cancel all pending requests.
Packit 3adb1e
 *
Packit 3adb1e
 * The closed callback passed to serf_connection_create() will be invoked
Packit 3adb1e
 * with APR_SUCCESS.
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_connection_close(
Packit 3adb1e
    serf_connection_t *conn);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Sets the maximum number of outstanding requests @a max_requests on the
Packit 3adb1e
 * connection @a conn. Setting max_requests to 0 means unlimited (the default).
Packit 3adb1e
 * Ex.: setting max_requests to 1 means a request is sent when a response on the
Packit 3adb1e
 * previous request was received and handled.
Packit 3adb1e
 *
Packit 3adb1e
 * In general, serf tends to take around 16KB per outstanding request.
Packit 3adb1e
 */
Packit 3adb1e
void serf_connection_set_max_outstanding_requests(
Packit 3adb1e
    serf_connection_t *conn,
Packit 3adb1e
    unsigned int max_requests);
Packit 3adb1e
Packit 3adb1e
void serf_connection_set_async_responses(
Packit 3adb1e
    serf_connection_t *conn,
Packit 3adb1e
    serf_response_acceptor_t acceptor,
Packit 3adb1e
    void *acceptor_baton,
Packit 3adb1e
    serf_response_handler_t handler,
Packit 3adb1e
    void *handler_baton);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Setup the @a request for delivery on its connection.
Packit 3adb1e
 *
Packit 3adb1e
 * Right before this is invoked, @a pool will be built within the
Packit 3adb1e
 * connection's pool for the request to use.  The associated response will
Packit 3adb1e
 * be allocated within that subpool. An associated bucket allocator will
Packit 3adb1e
 * be built. These items may be fetched from the request object through
Packit 3adb1e
 * @see serf_request_get_pool or @see serf_request_get_alloc.
Packit 3adb1e
 *
Packit 3adb1e
 * The content of the request is specified by the @a req_bkt bucket. When
Packit 3adb1e
 * a response arrives, the @a acceptor callback will be invoked (along with
Packit 3adb1e
 * the @a acceptor_baton) to produce a response bucket. That bucket will then
Packit 3adb1e
 * be passed to @a handler, along with the @a handler_baton.
Packit 3adb1e
 *
Packit 3adb1e
 * The responsibility for the request bucket is passed to the request
Packit 3adb1e
 * object. When the request is done with the bucket, it will be destroyed.
Packit 3adb1e
 */
Packit 3adb1e
typedef apr_status_t (*serf_request_setup_t)(
Packit 3adb1e
    serf_request_t *request,
Packit 3adb1e
    void *setup_baton,
Packit 3adb1e
    serf_bucket_t **req_bkt,
Packit 3adb1e
    serf_response_acceptor_t *acceptor,
Packit 3adb1e
    void **acceptor_baton,
Packit 3adb1e
    serf_response_handler_t *handler,
Packit 3adb1e
    void **handler_baton,
Packit 3adb1e
    apr_pool_t *pool);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Construct a request object for the @a conn connection.
Packit 3adb1e
 *
Packit 3adb1e
 * When it is time to deliver the request, the @a setup callback will
Packit 3adb1e
 * be invoked with the @a setup_baton passed into it to complete the
Packit 3adb1e
 * construction of the request object.
Packit 3adb1e
 *
Packit 3adb1e
 * If the request has not (yet) been delivered, then it may be canceled
Packit 3adb1e
 * with @see serf_request_cancel.
Packit 3adb1e
 *
Packit 3adb1e
 * Invoking any calls other than @see serf_request_cancel before the setup
Packit 3adb1e
 * callback executes is not supported.
Packit 3adb1e
 */
Packit 3adb1e
serf_request_t *serf_connection_request_create(
Packit 3adb1e
    serf_connection_t *conn,
Packit 3adb1e
    serf_request_setup_t setup,
Packit 3adb1e
    void *setup_baton);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Construct a request object for the @a conn connection, add it in the
Packit 3adb1e
 * list as the next to-be-written request before all unwritten requests.
Packit 3adb1e
 *
Packit 3adb1e
 * When it is time to deliver the request, the @a setup callback will
Packit 3adb1e
 * be invoked with the @a setup_baton passed into it to complete the
Packit 3adb1e
 * construction of the request object.
Packit 3adb1e
 *
Packit 3adb1e
 * If the request has not (yet) been delivered, then it may be canceled
Packit 3adb1e
 * with @see serf_request_cancel.
Packit 3adb1e
 *
Packit 3adb1e
 * Invoking any calls other than @see serf_request_cancel before the setup
Packit 3adb1e
 * callback executes is not supported.
Packit 3adb1e
 */
Packit 3adb1e
serf_request_t *serf_connection_priority_request_create(
Packit 3adb1e
    serf_connection_t *conn,
Packit 3adb1e
    serf_request_setup_t setup,
Packit 3adb1e
    void *setup_baton);
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/** Returns detected network latency for the @a conn connection. Negative
Packit 3adb1e
 *  value means that latency is unknwon.
Packit 3adb1e
 */
Packit 3adb1e
apr_interval_time_t serf_connection_get_latency(serf_connection_t *conn);
Packit 3adb1e
Packit 3adb1e
/** Check if a @a request has been completely written.
Packit 3adb1e
 *
Packit 3adb1e
 * Returns APR_SUCCESS if the request was written completely on the connection.
Packit 3adb1e
 * Returns APR_EBUSY if the request is not yet or partially written.
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_request_is_written(
Packit 3adb1e
    serf_request_t *request);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Cancel the request specified by the @a request object.
Packit 3adb1e
 *
Packit 3adb1e
 * If the request has been scheduled for delivery, then its response
Packit 3adb1e
 * handler will be run, passing NULL for the response bucket.
Packit 3adb1e
 *
Packit 3adb1e
 * If the request has already been (partially or fully) delivered, then
Packit 3adb1e
 * APR_EBUSY is returned and the request is *NOT* canceled. To properly
Packit 3adb1e
 * cancel the request, the connection must be closed (by clearing or
Packit 3adb1e
 * destroying its associated pool).
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_request_cancel(
Packit 3adb1e
    serf_request_t *request);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Return the pool associated with @a request.
Packit 3adb1e
 *
Packit 3adb1e
 * WARNING: be very careful about the kinds of things placed into this
Packit 3adb1e
 * pool. In particular, all allocation should be bounded in size, rather
Packit 3adb1e
 * than proportional to any data stream.
Packit 3adb1e
 */
Packit 3adb1e
apr_pool_t *serf_request_get_pool(
Packit 3adb1e
    const serf_request_t *request);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Return the bucket allocator associated with @a request.
Packit 3adb1e
 */
Packit 3adb1e
serf_bucket_alloc_t *serf_request_get_alloc(
Packit 3adb1e
    const serf_request_t *request);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Return the connection associated with @a request.
Packit 3adb1e
 */
Packit 3adb1e
serf_connection_t *serf_request_get_conn(
Packit 3adb1e
    const serf_request_t *request);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Update the @a handler and @a handler_baton for this @a request.
Packit 3adb1e
 *
Packit 3adb1e
 * This can be called after the request has started processing -
Packit 3adb1e
 * subsequent data will be delivered to this new handler.
Packit 3adb1e
 */
Packit 3adb1e
void serf_request_set_handler(
Packit 3adb1e
    serf_request_t *request,
Packit 3adb1e
    const serf_response_handler_t handler,
Packit 3adb1e
    const void **handler_baton);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Configure proxy server settings, to be used by all connections associated
Packit 3adb1e
 * with the @a ctx serf context.
Packit 3adb1e
 *
Packit 3adb1e
 * The next connection will be created to connect to the proxy server
Packit 3adb1e
 * specified by @a address. The address must live at least as long as the
Packit 3adb1e
 * serf context.
Packit 3adb1e
 */
Packit 3adb1e
void serf_config_proxy(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    apr_sockaddr_t *address);
Packit 3adb1e
Packit 3adb1e
/* Supported authentication types. */
Packit 3adb1e
#define SERF_AUTHN_NONE      0x00
Packit 3adb1e
#define SERF_AUTHN_BASIC     0x01
Packit 3adb1e
#define SERF_AUTHN_DIGEST    0x02
Packit 3adb1e
#define SERF_AUTHN_NTLM      0x04
Packit 3adb1e
#define SERF_AUTHN_NEGOTIATE 0x08
Packit 3adb1e
#define SERF_AUTHN_ALL       0xFF
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Define the authentication handlers that serf will try on incoming requests.
Packit 3adb1e
 */
Packit 3adb1e
void serf_config_authn_types(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    int authn_types);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Set the credentials callback handler.
Packit 3adb1e
 */
Packit 3adb1e
void serf_config_credentials_callback(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    serf_credentials_callback_t cred_cb);
Packit 3adb1e
Packit 3adb1e
/* ### maybe some connection control functions for flood? */
Packit 3adb1e
Packit 3adb1e
/*** Special bucket creation functions ***/
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Create a bucket of type 'socket bucket'.
Packit 3adb1e
 * This is basically a wrapper around @a serf_bucket_socket_create, which
Packit 3adb1e
 * initializes the bucket using connection and/or context specific settings.
Packit 3adb1e
 */
Packit 3adb1e
serf_bucket_t *serf_context_bucket_socket_create(
Packit 3adb1e
    serf_context_t *ctx,
Packit 3adb1e
    apr_socket_t *skt,
Packit 3adb1e
    serf_bucket_alloc_t *allocator);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Create a bucket of type 'request bucket'.
Packit 3adb1e
 * This is basically a wrapper around @a serf_bucket_request_create, which
Packit 3adb1e
 * initializes the bucket using request, connection and/or context specific
Packit 3adb1e
 * settings.
Packit 3adb1e
 *
Packit 3adb1e
 * This function will set following header(s):
Packit 3adb1e
 * - Host: if the connection was created with @a serf_connection_create2.
Packit 3adb1e
 */
Packit 3adb1e
serf_bucket_t *serf_request_bucket_request_create(
Packit 3adb1e
    serf_request_t *request,
Packit 3adb1e
    const char *method,
Packit 3adb1e
    const char *uri,
Packit 3adb1e
    serf_bucket_t *body,
Packit 3adb1e
    serf_bucket_alloc_t *allocator);
Packit 3adb1e
Packit 3adb1e
/** @} */
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * @defgroup serf buckets
Packit 3adb1e
 * @ingroup serf
Packit 3adb1e
 * @{
Packit 3adb1e
 */
Packit 3adb1e
Packit 3adb1e
/** Pass as REQUESTED to the read function of a bucket to read, consume,
Packit 3adb1e
 * and return all available data.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_READ_ALL_AVAIL ((apr_size_t)-1)
Packit 3adb1e
Packit 3adb1e
/** Acceptable newline types for bucket->readline(). */
Packit 3adb1e
#define SERF_NEWLINE_CR    0x0001
Packit 3adb1e
#define SERF_NEWLINE_CRLF  0x0002
Packit 3adb1e
#define SERF_NEWLINE_LF    0x0004
Packit 3adb1e
#define SERF_NEWLINE_ANY   0x0007
Packit 3adb1e
Packit 3adb1e
/** Used to indicate that a newline is not present in the data buffer. */
Packit 3adb1e
/* ### should we make this zero? */
Packit 3adb1e
#define SERF_NEWLINE_NONE  0x0008
Packit 3adb1e
Packit 3adb1e
/** Used to indicate that a CR was found at the end of a buffer, and CRLF
Packit 3adb1e
 * was acceptable. It may be that the LF is present, but it needs to be
Packit 3adb1e
 * read first.
Packit 3adb1e
 *
Packit 3adb1e
 * Note: an alternative to using this symbol would be for callers to see
Packit 3adb1e
 * the SERF_NEWLINE_CR return value, and know that some "end of buffer" was
Packit 3adb1e
 * reached. While this works well for @see serf_util_readline, it does not
Packit 3adb1e
 * necessary work as well for buckets (there is no obvious "end of buffer",
Packit 3adb1e
 * although there is an "end of bucket"). The other problem with that
Packit 3adb1e
 * alternative is that developers might miss the condition. This symbol
Packit 3adb1e
 * calls out the possibility and ensures that callers will watch for it.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_NEWLINE_CRLF_SPLIT 0x0010
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
struct serf_bucket_type_t {
Packit 3adb1e
Packit 3adb1e
    /** name of this bucket type */
Packit 3adb1e
    const char *name;
Packit 3adb1e
Packit 3adb1e
    /**
Packit 3adb1e
     * Read (and consume) up to @a requested bytes from @a bucket.
Packit 3adb1e
     *
Packit 3adb1e
     * A pointer to the data will be returned in @a data, and its length
Packit 3adb1e
     * is specified by @a len.
Packit 3adb1e
     *
Packit 3adb1e
     * The data will exist until one of two conditions occur:
Packit 3adb1e
     *
Packit 3adb1e
     * 1) this bucket is destroyed
Packit 3adb1e
     * 2) another call to any read function or to peek()
Packit 3adb1e
     *
Packit 3adb1e
     * If an application needs the data to exist for a longer duration,
Packit 3adb1e
     * then it must make a copy.
Packit 3adb1e
     */
Packit 3adb1e
    apr_status_t (*read)(serf_bucket_t *bucket, apr_size_t requested,
Packit 3adb1e
                         const char **data, apr_size_t *len);
Packit 3adb1e
Packit 3adb1e
    /**
Packit 3adb1e
     * Read (and consume) a line of data from @a bucket.
Packit 3adb1e
     *
Packit 3adb1e
     * The acceptable forms of a newline are given by @a acceptable, and
Packit 3adb1e
     * the type found is returned in @a found. If a newline is not present
Packit 3adb1e
     * in the returned data, then SERF_NEWLINE_NONE is stored into @a found.
Packit 3adb1e
     *
Packit 3adb1e
     * A pointer to the data is returned in @a data, and its length is
Packit 3adb1e
     * specified by @a len. The data will include the newline, if present.
Packit 3adb1e
     *
Packit 3adb1e
     * Note that there is no way to limit the amount of data returned
Packit 3adb1e
     * by this function.
Packit 3adb1e
     *
Packit 3adb1e
     * The lifetime of the data is the same as that of the @see read
Packit 3adb1e
     * function above.
Packit 3adb1e
     */
Packit 3adb1e
    apr_status_t (*readline)(serf_bucket_t *bucket, int acceptable,
Packit 3adb1e
                             int *found,
Packit 3adb1e
                             const char **data, apr_size_t *len);
Packit 3adb1e
Packit 3adb1e
    /**
Packit 3adb1e
     * Read a set of pointer/length pairs from the bucket.
Packit 3adb1e
     *
Packit 3adb1e
     * The size of the @a vecs array is specified by @a vecs_size. The
Packit 3adb1e
     * bucket should fill in elements of the array, and return the number
Packit 3adb1e
     * used in @a vecs_used.
Packit 3adb1e
     *
Packit 3adb1e
     * Each element of @a vecs should specify a pointer to a block of
Packit 3adb1e
     * data and a length of that data.
Packit 3adb1e
     *
Packit 3adb1e
     * The total length of all data elements should not exceed the
Packit 3adb1e
     * amount specified in @a requested.
Packit 3adb1e
     *
Packit 3adb1e
     * The lifetime of the data is the same as that of the @see read
Packit 3adb1e
     * function above.
Packit 3adb1e
     */
Packit 3adb1e
    apr_status_t (*read_iovec)(serf_bucket_t *bucket, apr_size_t requested,
Packit 3adb1e
                               int vecs_size, struct iovec *vecs,
Packit 3adb1e
                               int *vecs_used);
Packit 3adb1e
Packit 3adb1e
    /**
Packit 3adb1e
     * Read data from the bucket in a form suitable for apr_socket_sendfile()
Packit 3adb1e
     *
Packit 3adb1e
     * On input, hdtr->numheaders and hdtr->numtrailers specify the size
Packit 3adb1e
     * of the hdtr->headers and hdtr->trailers arrays, respectively. The
Packit 3adb1e
     * bucket should fill in the headers and trailers, up to the specified
Packit 3adb1e
     * limits, and set numheaders and numtrailers to the number of iovecs
Packit 3adb1e
     * filled in for each item.
Packit 3adb1e
     *
Packit 3adb1e
     * @a file should be filled in with a file that can be read. If a file
Packit 3adb1e
     * is not available or appropriate, then NULL should be stored. The
Packit 3adb1e
     * file offset for the data should be stored in @a offset, and the
Packit 3adb1e
     * length of that data should be stored in @a len. If a file is not
Packit 3adb1e
     * returned, then @a offset and @a len should be ignored.
Packit 3adb1e
     *
Packit 3adb1e
     * The file position is not required to correspond to @a offset, and
Packit 3adb1e
     * the caller may manipulate it at will.
Packit 3adb1e
     *
Packit 3adb1e
     * The total length of all data elements, and the portion of the
Packit 3adb1e
     * file should not exceed the amount specified in @a requested.
Packit 3adb1e
     *
Packit 3adb1e
     * The lifetime of the data is the same as that of the @see read
Packit 3adb1e
     * function above.
Packit 3adb1e
     */
Packit 3adb1e
    apr_status_t (*read_for_sendfile)(serf_bucket_t *bucket,
Packit 3adb1e
                                      apr_size_t requested, apr_hdtr_t *hdtr,
Packit 3adb1e
                                      apr_file_t **file, apr_off_t *offset,
Packit 3adb1e
                                      apr_size_t *len);
Packit 3adb1e
Packit 3adb1e
    /**
Packit 3adb1e
     * Look within @a bucket for a bucket of the given @a type. The bucket
Packit 3adb1e
     * must be the "initial" data because it will be consumed by this
Packit 3adb1e
     * function. If the given bucket type is available, then read and consume
Packit 3adb1e
     * it, and return it to the caller.
Packit 3adb1e
     *
Packit 3adb1e
     * This function is usually used by readers that have custom handling
Packit 3adb1e
     * for specific bucket types (e.g. looking for a file bucket to pass
Packit 3adb1e
     * to apr_socket_sendfile).
Packit 3adb1e
     *
Packit 3adb1e
     * If a bucket of the given type is not found, then NULL is returned.
Packit 3adb1e
     *
Packit 3adb1e
     * The returned bucket becomes the responsibility of the caller. When
Packit 3adb1e
     * the caller is done with the bucket, it should be destroyed.
Packit 3adb1e
     */
Packit 3adb1e
    serf_bucket_t * (*read_bucket)(serf_bucket_t *bucket,
Packit 3adb1e
                                   const serf_bucket_type_t *type);
Packit 3adb1e
Packit 3adb1e
    /**
Packit 3adb1e
     * Peek, but don't consume, the data in @a bucket.
Packit 3adb1e
     *
Packit 3adb1e
     * Since this function is non-destructive, the implicit read size is
Packit 3adb1e
     * SERF_READ_ALL_AVAIL. The caller can then use whatever amount is
Packit 3adb1e
     * appropriate.
Packit 3adb1e
     *
Packit 3adb1e
     * The @a data parameter will point to the data, and @a len will
Packit 3adb1e
     * specify how much data is available. The lifetime of the data follows
Packit 3adb1e
     * the same rules as the @see read function above.
Packit 3adb1e
     *
Packit 3adb1e
     * Note: if the peek does not return enough data for your particular
Packit 3adb1e
     * use, then you must read/consume some first, then peek again.
Packit 3adb1e
     *
Packit 3adb1e
     * If the returned data represents all available data, then APR_EOF
Packit 3adb1e
     * will be returned. Since this function does not consume data, it
Packit 3adb1e
     * can return the same data repeatedly rather than blocking; thus,
Packit 3adb1e
     * APR_EAGAIN will never be returned.
Packit 3adb1e
     */
Packit 3adb1e
    apr_status_t (*peek)(serf_bucket_t *bucket,
Packit 3adb1e
                         const char **data, apr_size_t *len);
Packit 3adb1e
Packit 3adb1e
    /**
Packit 3adb1e
     * Destroy @a bucket, along with any associated resources.
Packit 3adb1e
     */
Packit 3adb1e
    void (*destroy)(serf_bucket_t *bucket);
Packit 3adb1e
Packit 3adb1e
    /* ### apr buckets have 'copy', 'split', and 'setaside' functions.
Packit 3adb1e
       ### not sure whether those will be needed in this bucket model.
Packit 3adb1e
    */
Packit 3adb1e
};
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Should the use and lifecycle of buckets be tracked?
Packit 3adb1e
 *
Packit 3adb1e
 * When tracking, the system will ensure several semantic requirements
Packit 3adb1e
 * of bucket use:
Packit 3adb1e
 *
Packit 3adb1e
 *   - if a bucket returns APR_EAGAIN, one of its read functions should
Packit 3adb1e
 *     not be called immediately. the context's run loop should be called.
Packit 3adb1e
 *     ### and for APR_EOF, too?
Packit 3adb1e
 *   - all buckets must be drained of input before returning to the
Packit 3adb1e
 *     context's run loop.
Packit 3adb1e
 *   - buckets should not be destroyed before they return APR_EOF unless
Packit 3adb1e
 *     the connection is closed for some reason.
Packit 3adb1e
 *
Packit 3adb1e
 * Undefine this symbol to avoid the tracking (and a performance gain).
Packit 3adb1e
 *
Packit 3adb1e
 * ### we may want to examine when/how we provide this. should it always
Packit 3adb1e
 * ### be compiled in? and apps select it before including this header?
Packit 3adb1e
 */
Packit 3adb1e
/* #define SERF_DEBUG_BUCKET_USE */
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/* Internal macros for tracking bucket use. */
Packit 3adb1e
#ifdef SERF_DEBUG_BUCKET_USE
Packit 3adb1e
#define SERF__RECREAD(b,s) serf_debug__record_read(b,s)
Packit 3adb1e
#else
Packit 3adb1e
#define SERF__RECREAD(b,s) (s)
Packit 3adb1e
#endif
Packit 3adb1e
Packit 3adb1e
#define serf_bucket_read(b,r,d,l) SERF__RECREAD(b, (b)->type->read(b,r,d,l))
Packit 3adb1e
#define serf_bucket_readline(b,a,f,d,l) \
Packit 3adb1e
    SERF__RECREAD(b, (b)->type->readline(b,a,f,d,l))
Packit 3adb1e
#define serf_bucket_read_iovec(b,r,s,v,u) \
Packit 3adb1e
    SERF__RECREAD(b, (b)->type->read_iovec(b,r,s,v,u))
Packit 3adb1e
#define serf_bucket_read_for_sendfile(b,r,h,f,o,l) \
Packit 3adb1e
    SERF__RECREAD(b, (b)->type->read_for_sendfile(b,r,h,f,o,l))
Packit 3adb1e
#define serf_bucket_read_bucket(b,t) ((b)->type->read_bucket(b,t))
Packit 3adb1e
#define serf_bucket_peek(b,d,l) ((b)->type->peek(b,d,l))
Packit 3adb1e
#define serf_bucket_destroy(b) ((b)->type->destroy(b))
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Check whether a real error occurred. Note that bucket read functions
Packit 3adb1e
 * can return EOF and EAGAIN as part of their "normal" operation, so they
Packit 3adb1e
 * should not be considered an error.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_BUCKET_READ_ERROR(status) ((status) \
Packit 3adb1e
                                        && !APR_STATUS_IS_EOF(status) \
Packit 3adb1e
                                        && !APR_STATUS_IS_EAGAIN(status) \
Packit 3adb1e
                                        && (SERF_ERROR_WAIT_CONN != status))
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
struct serf_bucket_t {
Packit 3adb1e
Packit 3adb1e
    /** the type of this bucket */
Packit 3adb1e
    const serf_bucket_type_t *type;
Packit 3adb1e
Packit 3adb1e
    /** bucket-private data */
Packit 3adb1e
    void *data;
Packit 3adb1e
Packit 3adb1e
    /** the allocator used for this bucket (needed at destroy time) */
Packit 3adb1e
    serf_bucket_alloc_t *allocator;
Packit 3adb1e
};
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Generic macro to construct "is TYPE" macros.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_BUCKET_CHECK(b, btype) ((b)->type == &serf_bucket_type_ ## btype)
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Notification callback for a block that was not returned to the bucket
Packit 3adb1e
 * allocator when its pool was destroyed.
Packit 3adb1e
 *
Packit 3adb1e
 * The block of memory is given by @a block. The baton provided when the
Packit 3adb1e
 * allocator was constructed is passed as @a unfreed_baton.
Packit 3adb1e
 */
Packit 3adb1e
typedef void (*serf_unfreed_func_t)(
Packit 3adb1e
    void *unfreed_baton,
Packit 3adb1e
    void *block);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Create a new allocator for buckets.
Packit 3adb1e
 *
Packit 3adb1e
 * All buckets are associated with a serf bucket allocator. This allocator
Packit 3adb1e
 * will be created within @a pool and will be destroyed when that pool is
Packit 3adb1e
 * cleared or destroyed.
Packit 3adb1e
 *
Packit 3adb1e
 * When the allocator is destroyed, if any allocations were not explicitly
Packit 3adb1e
 * returned (by calling serf_bucket_mem_free), then the @a unfreed callback
Packit 3adb1e
 * will be invoked for each block. @a unfreed_baton will be passed to the
Packit 3adb1e
 * callback.
Packit 3adb1e
 *
Packit 3adb1e
 * If @a unfreed is NULL, then the library will invoke the abort() stdlib
Packit 3adb1e
 * call. Any failure to return memory is a bug in the application, and an
Packit 3adb1e
 * abort can assist with determining what kinds of memory were not freed.
Packit 3adb1e
 */
Packit 3adb1e
serf_bucket_alloc_t *serf_bucket_allocator_create(
Packit 3adb1e
    apr_pool_t *pool,
Packit 3adb1e
    serf_unfreed_func_t unfreed,
Packit 3adb1e
    void *unfreed_baton);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Return the pool that was used for this @a allocator.
Packit 3adb1e
 *
Packit 3adb1e
 * WARNING: the use of this pool for allocations requires a very
Packit 3adb1e
 *   detailed understanding of pool behaviors, the bucket system,
Packit 3adb1e
 *   and knowledge of the bucket's use within the overall pattern
Packit 3adb1e
 *   of request/response behavior.
Packit 3adb1e
 *
Packit 3adb1e
 * See design-guide.txt for more information about pool usage.
Packit 3adb1e
 */
Packit 3adb1e
apr_pool_t *serf_bucket_allocator_get_pool(
Packit 3adb1e
    const serf_bucket_alloc_t *allocator);
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Utility structure for reading a complete line of input from a bucket.
Packit 3adb1e
 *
Packit 3adb1e
 * Since it is entirely possible for a line to be broken by APR_EAGAIN,
Packit 3adb1e
 * this structure can be used to accumulate the data until a complete line
Packit 3adb1e
 * has been read from a bucket.
Packit 3adb1e
 */
Packit 3adb1e
Packit 3adb1e
/* This limit applies to the line buffer functions. If an application needs
Packit 3adb1e
 * longer lines, then they will need to manually handle line buffering.
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_LINEBUF_LIMIT 8000
Packit 3adb1e
Packit 3adb1e
typedef struct {
Packit 3adb1e
Packit 3adb1e
    /* Current state of the buffer. */
Packit 3adb1e
    enum {
Packit 3adb1e
        SERF_LINEBUF_EMPTY,
Packit 3adb1e
        SERF_LINEBUF_READY,
Packit 3adb1e
        SERF_LINEBUF_PARTIAL,
Packit 3adb1e
        SERF_LINEBUF_CRLF_SPLIT
Packit 3adb1e
    } state;
Packit 3adb1e
Packit 3adb1e
    /* How much of the buffer have we used? */
Packit 3adb1e
    apr_size_t used;
Packit 3adb1e
Packit 3adb1e
    /* The line is read into this buffer, minus CR/LF */
Packit 3adb1e
    char line[SERF_LINEBUF_LIMIT];
Packit 3adb1e
Packit 3adb1e
} serf_linebuf_t;
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Initialize the @a linebuf structure.
Packit 3adb1e
 */
Packit 3adb1e
void serf_linebuf_init(serf_linebuf_t *linebuf);
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Fetch a line of text from @a bucket, accumulating the line into
Packit 3adb1e
 * @a linebuf. @a acceptable specifies the types of newlines which are
Packit 3adb1e
 * acceptable for this fetch.
Packit 3adb1e
 *
Packit 3adb1e
 * ### we should return a data/len pair so that we can avoid a copy,
Packit 3adb1e
 * ### rather than having callers look into our state and line buffer.
Packit 3adb1e
 */
Packit 3adb1e
apr_status_t serf_linebuf_fetch(
Packit 3adb1e
    serf_linebuf_t *linebuf,
Packit 3adb1e
    serf_bucket_t *bucket,
Packit 3adb1e
    int acceptable);
Packit 3adb1e
Packit 3adb1e
/** @} */
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/* Internal functions for bucket use and lifecycle tracking */
Packit 3adb1e
apr_status_t serf_debug__record_read(
Packit 3adb1e
    const serf_bucket_t *bucket,
Packit 3adb1e
    apr_status_t status);
Packit 3adb1e
void serf_debug__entered_loop(
Packit 3adb1e
    serf_bucket_alloc_t *allocator);
Packit 3adb1e
void serf_debug__closed_conn(
Packit 3adb1e
    serf_bucket_alloc_t *allocator);
Packit 3adb1e
void serf_debug__bucket_destroy(
Packit 3adb1e
    const serf_bucket_t *bucket);
Packit 3adb1e
void serf_debug__bucket_alloc_check(
Packit 3adb1e
    serf_bucket_alloc_t *allocator);
Packit 3adb1e
Packit 3adb1e
/* Version info */
Packit 3adb1e
#define SERF_MAJOR_VERSION 1
Packit 3adb1e
#define SERF_MINOR_VERSION 3
Packit 3adb1e
#define SERF_PATCH_VERSION 9
Packit 3adb1e
Packit 3adb1e
/* Version number string */
Packit 3adb1e
#define SERF_VERSION_STRING APR_STRINGIFY(SERF_MAJOR_VERSION) "." \
Packit 3adb1e
                            APR_STRINGIFY(SERF_MINOR_VERSION) "." \
Packit 3adb1e
                            APR_STRINGIFY(SERF_PATCH_VERSION)
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Check at compile time if the Serf version is at least a certain
Packit 3adb1e
 * level.
Packit 3adb1e
 * @param major The major version component of the version checked
Packit 3adb1e
 * for (e.g., the "1" of "1.3.0").
Packit 3adb1e
 * @param minor The minor version component of the version checked
Packit 3adb1e
 * for (e.g., the "3" of "1.3.0").
Packit 3adb1e
 * @param patch The patch level component of the version checked
Packit 3adb1e
 * for (e.g., the "0" of "1.3.0").
Packit 3adb1e
 */
Packit 3adb1e
#define SERF_VERSION_AT_LEAST(major,minor,patch)                         \
Packit 3adb1e
(((major) < SERF_MAJOR_VERSION)                                          \
Packit 3adb1e
  || ((major) == SERF_MAJOR_VERSION && (minor) < SERF_MINOR_VERSION)     \
Packit 3adb1e
   || ((major) == SERF_MAJOR_VERSION && (minor) == SERF_MINOR_VERSION && \
Packit 3adb1e
            (patch) <= SERF_PATCH_VERSION))
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/**
Packit 3adb1e
 * Returns the version of the library the application has linked/loaded.
Packit 3adb1e
 * Values are returned in @a major, @a minor, and @a patch.
Packit 3adb1e
 *
Packit 3adb1e
 * Applications will want to use this function to verify compatibility,
Packit 3adb1e
 * expecially while serf has not reached a 1.0 milestone. APIs and
Packit 3adb1e
 * semantics may change drastically until the library hits 1.0.
Packit 3adb1e
 */
Packit 3adb1e
void serf_lib_version(
Packit 3adb1e
    int *major,
Packit 3adb1e
    int *minor,
Packit 3adb1e
    int *patch);
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
#ifdef __cplusplus
Packit 3adb1e
}
Packit 3adb1e
#endif
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
/*
Packit 3adb1e
 * Every user of serf will want to deal with our various bucket types.
Packit 3adb1e
 * Go ahead and include that header right now.
Packit 3adb1e
 *
Packit 3adb1e
 * Note: make sure this occurs outside of the C++ namespace block
Packit 3adb1e
 */
Packit 3adb1e
#include "serf_bucket_types.h"
Packit 3adb1e
Packit 3adb1e
Packit 3adb1e
#endif    /* !SERF_H */