mod_serf.c revision 09eec2b9e10f4fd1b8c5f7f1a105a354ce270caf
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering/* Licensed to the Apache Software Foundation (ASF) under one or more
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * contributor license agreements. See the NOTICE file distributed with
12b42c76672a66c2d4ea7212c14f8f1b5a62b78dTom Gundersen * this work for additional information regarding copyright ownership.
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * The ASF licenses this file to You under the Apache License, Version 2.0
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * (the "License"); you may not use this file except in compliance with
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * the License. You may obtain a copy of the License at
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * http://www.apache.org/licenses/LICENSE-2.0
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * Unless required by applicable law or agreed to in writing, software
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * distributed under the License is distributed on an "AS IS" BASIS,
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * See the License for the specific language governing permissions and
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering * limitations under the License.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekmodule AP_MODULE_DECLARE_DATA serf_module;
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* name -> serf_cluster_t* */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * This works right now because all timers are invoked in the single listener
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * thread in the Event MPM -- the same thread that serf callbacks are made
af105d03145244d00b33c2474d459eb375bccf1akayrus * from, so we don't technically need a mutex yet, but with the Simple MPM,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * invocations are made from worker threads, and we need to figure out locking
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic void timed_cleanup_callback(void *baton)
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* Causes all serf connections to unregister from the event mpm */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_log_rerror(APLOG_MARK, APLOG_ERR, ctx->rstatus, ctx->r,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek "serf: request returned: %d", ctx->rstatus);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_pool_destroy(ctx->serf_pool);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_brigade_cleanup(ctx->tmpbb);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek e = apr_bucket_flush_create(ctx->r->connection->bucket_alloc);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, e);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek e = apr_bucket_eos_create(ctx->r->connection->bucket_alloc);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, e);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* TODO: return code? bleh */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_pass_brigade(ctx->r->output_filters, ctx->tmpbb);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_pool_destroy(ctx->serf_pool);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_finalize_request_protocol(ctx->r);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_process_request_after_handler(ctx->r);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic void closed_connection(serf_connection_t *conn,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* justin says that error handling isn't done yet. hah. */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* XXXXXX: review */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_log_rerror(APLOG_MARK, APLOG_ERR, why, ctx->r, "Closed Connection Error");
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ctx->rstatus = HTTP_INTERNAL_SERVER_ERROR;
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_mpm_register_timed_callback(apr_time_from_msec(1),
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic serf_bucket_t* conn_setup(apr_socket_t *sock,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek c = serf_bucket_socket_create(sock, ctx->bkt_alloc);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek c = serf_bucket_ssl_decrypt_create(c, ctx->ssl_ctx, ctx->bkt_alloc);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic int copy_headers_in(void *vbaton, const char *key, const char *value)
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_bucket_t *hdrs_bkt = (serf_bucket_t *)vbaton;
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* XXXXX: List of headers not to copy to serf. serf's serf_bucket_headers_setn,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * doesn't actually overwrite a header if we set it once, so we need to ignore anything
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * we might want to toggle or combine.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Accept-Encoding", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Connection", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Host", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Keep-Alive", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("TE", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Trailer", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Upgrade", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_bucket_headers_setn(hdrs_bkt, key, value);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic int copy_headers_out(void *vbaton, const char *key, const char *value)
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* XXXXX: Special Treatment required for MANY other headers. fixme.*/
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Content-Type", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_set_content_type(ctx->r, value);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek else if (strcasecmp("Connection", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek else if (strcasecmp("Content-Encoding", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek else if (strcasecmp("Content-Length", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (strcasecmp("Transfer-Encoding", key) == 0) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_table_addn(ctx->r->headers_out, key, value);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic serf_bucket_t* accept_response(serf_request_t *request,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* get the per-request bucket allocator */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek bkt_alloc = serf_request_get_alloc(request);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* Create a barrier so the response doesn't eat us! */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek c = serf_bucket_barrier_create(stream, bkt_alloc);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek return serf_bucket_response_create(c, bkt_alloc);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic apr_status_t handle_response(serf_request_t *request,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* XXXXXXX: Create better error message. */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek rv = serf_bucket_response_status(response, &sl);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, ctx->r, "serf_bucket_response_status...");
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ctx->rstatus = HTTP_INTERNAL_SERVER_ERROR;
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_mpm_register_timed_callback(apr_time_from_msec(1),
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * XXXXX: If I understood serf buckets better, it might be possible to not
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek * copy all of the data here, and better stream it to the client.
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_brigade_cleanup(ctx->tmpbb);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek rv = serf_bucket_read(response, AP_IOBUFSIZE, &data, &len);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek if (SERF_BUCKET_READ_ERROR(rv)) {
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, ctx->r, "serf_bucket_read(response)");
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* TODO: improve */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_bucket_response_status(response, &line);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek hdrs = serf_bucket_response_get_headers(response);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_bucket_headers_do(hdrs, copy_headers_out, ctx);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* TODO: make APR bucket <-> serf bucket stuff more magical. */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_brigade_write(ctx->tmpbb, NULL, NULL, data, len);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ctx->rstatus = ap_pass_brigade(ctx->r->output_filters, ctx->tmpbb);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ap_mpm_register_timed_callback(apr_time_from_msec(1),
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek ctx->rstatus = ap_pass_brigade(ctx->r->output_filters, ctx->tmpbb);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek /* XXXX: Should we send a flush now? */
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmekstatic apr_status_t setup_request(serf_request_t *request,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_response_acceptor_t *acceptor,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_response_handler_t *handler,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek *req_bkt = serf_bucket_request_create(ctx->r->method, ctx->r->unparsed_uri,
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_request_get_alloc(request));
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_table_do(copy_headers_in, hdrs_bkt, ctx->r->headers_in, NULL);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_bucket_headers_setn(hdrs_bkt, "Host",
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek apr_table_get(ctx->r->headers_in, "Host"));
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_bucket_headers_setn(hdrs_bkt, "Host", ctx->conf->url.hostname);
798d3a524ea57aaf40cb53858aaa45ec702f012dZbigniew Jędrzejewski-Szmek serf_bucket_headers_setn(hdrs_bkt, "Accept-Encoding", "gzip");
return APR_SUCCESS;
#ifndef apr_time_from_msec
/* XXXXX: make persistent/per-process or something.*/
&serf_module);
int rc;
if (!cluster) {
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
&servers);
return HTTP_INTERNAL_SERVER_ERROR;
return HTTP_INTERNAL_SERVER_ERROR;
pool);
pool);
return HTTP_INTERNAL_SERVER_ERROR;
if (mpm_supprts_serf) {
if (!serfme) {
return HTTP_INTERNAL_SERVER_ERROR;
baton->r = r;
if (rv) {
return rv;
if (ap_should_client_block(r)) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "mod_serf: Unable to create temp request body buffer file.");
return HTTP_INTERNAL_SERVER_ERROR;
if (rv > 0) {
if (rv) {
return HTTP_INTERNAL_SERVER_ERROR;
} while(rv > 0);
if (rv < 0) {
return HTTP_INTERNAL_SERVER_ERROR;
pool);
baton);
if (mpm_supprts_serf) {
return SUSPENDED;
return HTTP_INTERNAL_SERVER_ERROR;
&serf_module);
return DECLINED;
static int is_true(const char *w)
const char *p = argv[i];
return NULL;
const char *rv;
&serf_module);
return err;
const char *p = argv[i];
if (rv) {
return rv;
return NULL;
return new;
return ctx;
return ctx;
{NULL}
typedef struct hb_table_baton_t {
apr_pool_t *p;
const char *msg;
key);
if (b.msg) {
return b.msg;
return NULL;
typedef struct hb_server_t {
const char *ip;
int busy;
int ready;
int seen;
} hb_server_t;
char *key;
char *value;
char *strtok_state;
while (key) {
if (value) {
if (!path) {
return APR_SUCCESS;
if (rv) {
return rv;
if (rv) {
return rv;
int lineno = 0;
const char *ip;
lineno++;
return APR_SUCCESS;
request_rec *r,
if (rv) {
return HTTP_INTERNAL_SERVER_ERROR;
ap_serf_server_t *x;
return OK;
NULL,
NULL,
key);
if (b.msg) {
return b.msg;
return NULL;
request_rec *r,
char *ip;
char *strtok_state;
while (ip) {
char *host_str;
char *scope_id;
if (!rv) {
return OK;
NULL,
NULL,
mpm_supprts_serf = 0;
NULL,