mod_serf.c revision ee55ac2cd2fd8c48de97c754758004a7a0176336
c6d33447e28403a90ad817dba4df75fae785be28pquerna/* Licensed to the Apache Software Foundation (ASF) under one or more
c6d33447e28403a90ad817dba4df75fae785be28pquerna * contributor license agreements. See the NOTICE file distributed with
c6d33447e28403a90ad817dba4df75fae785be28pquerna * this work for additional information regarding copyright ownership.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * The ASF licenses this file to You under the Apache License, Version 2.0
c6d33447e28403a90ad817dba4df75fae785be28pquerna * (the "License"); you may not use this file except in compliance with
c6d33447e28403a90ad817dba4df75fae785be28pquerna * the License. You may obtain a copy of the License at
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * http://www.apache.org/licenses/LICENSE-2.0
c6d33447e28403a90ad817dba4df75fae785be28pquerna *
c6d33447e28403a90ad817dba4df75fae785be28pquerna * Unless required by applicable law or agreed to in writing, software
c6d33447e28403a90ad817dba4df75fae785be28pquerna * distributed under the License is distributed on an "AS IS" BASIS,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
c6d33447e28403a90ad817dba4df75fae785be28pquerna * See the License for the specific language governing permissions and
c6d33447e28403a90ad817dba4df75fae785be28pquerna * limitations under the License.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna#include "mod_serf.h"
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "httpd.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_core.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_config.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_protocol.h"
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna#include "http_request.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "http_log.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "serf.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna#include "apr_uri.h"
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna#include "apr_strings.h"
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna#include "ap_mpm.h"
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernamodule AP_MODULE_DECLARE_DATA serf_module;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquernastatic int mpm_supprts_serf = 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernatypedef struct {
c6d33447e28403a90ad817dba4df75fae785be28pquerna int on;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna int preservehost;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_uri_t url;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna} serf_config_t;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernatypedef struct {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *name;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *provider;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_t *params;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna} serf_cluster_t;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernatypedef struct {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* name -> serf_cluster_t* */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_hash_t *clusters;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna} serf_server_config_t;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernatypedef struct {
c6d33447e28403a90ad817dba4df75fae785be28pquerna int rstatus;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int want_ssl;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int done_headers;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int keep_reading;
c6d33447e28403a90ad817dba4df75fae785be28pquerna request_rec *r;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna apr_pool_t *serf_pool;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna apr_bucket_brigade *tmpbb;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_config_t *conf;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_ssl_context_t *ssl_ctx;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_alloc_t *bkt_alloc;
c6d33447e28403a90ad817dba4df75fae785be28pquerna} s_baton_t;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna/**
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna * This works right now because all timers are invoked in the single listener
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna * thread in the Event MPM -- the same thread that serf callbacks are made
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna * from, so we don't technically need a mutex yet, but with the Simple MPM,
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna * invocations are made from worker threads, and we need to figure out locking
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna */
ee55ac2cd2fd8c48de97c754758004a7a0176336pquernastatic void timed_cleanup_callback(void *baton)
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna{
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna s_baton_t *ctx = baton;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "serf: timed_cleanup_callback");
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna /* Causes all serf connections to unregister from the event mpm */
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna apr_pool_destroy(ctx->serf_pool);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna if (ctx->rstatus) {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, ctx->rstatus, ctx->r,
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna "serf: request returned: %d", ctx->rstatus);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ctx->r->status = HTTP_OK;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_die(ctx->rstatus, ctx->r);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna else {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_finalize_request_protocol(ctx->r);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_process_request_after_handler(ctx->r);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna return;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna}
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void closed_connection(serf_connection_t *conn,
c6d33447e28403a90ad817dba4df75fae785be28pquerna void *closed_baton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_status_t why,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_pool_t *pool)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna s_baton_t *ctx = closed_baton;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "serf: closed_connection");
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (why) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* justin says that error handling isn't done yet. hah. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXXXX: review */
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, why, ctx->r, "Closed Connection Error");
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->rstatus = HTTP_INTERNAL_SERVER_ERROR;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna if (mpm_supprts_serf) {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_mpm_register_timed_callback(apr_time_from_msec(1),
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna timed_cleanup_callback, ctx);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic serf_bucket_t* conn_setup(apr_socket_t *sock,
c6d33447e28403a90ad817dba4df75fae785be28pquerna void *setup_baton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_pool_t *pool)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *c;
c6d33447e28403a90ad817dba4df75fae785be28pquerna s_baton_t *ctx = setup_baton;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "serf: conn_setup ");
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna c = serf_bucket_socket_create(sock, ctx->bkt_alloc);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ctx->want_ssl) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna c = serf_bucket_ssl_decrypt_create(c, ctx->ssl_ctx, ctx->bkt_alloc);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return c;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int copy_headers_in(void *vbaton, const char *key, const char *value)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *hdrs_bkt = (serf_bucket_t *)vbaton;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXXX: List of headers not to copy to serf. serf's serf_bucket_headers_setn,
c6d33447e28403a90ad817dba4df75fae785be28pquerna * doesn't actually overwrite a header if we set it once, so we need to ignore anything
c6d33447e28403a90ad817dba4df75fae785be28pquerna * we might want to toggle or combine.
c6d33447e28403a90ad817dba4df75fae785be28pquerna */
c6d33447e28403a90ad817dba4df75fae785be28pquerna switch (key[0]) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'a':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'A':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Accept-Encoding", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'c':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'C':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Connection", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'h':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'H':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Host", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'k':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'K':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Keep-Alive", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 't':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'T':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("TE", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Trailer", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'u':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'U':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Upgrade", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna default:
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_headers_setn(hdrs_bkt, key, value);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int copy_headers_out(void *vbaton, const char *key, const char *value)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna s_baton_t *ctx = vbaton;
c6d33447e28403a90ad817dba4df75fae785be28pquerna int done = 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXXX: Special Treatment required for MANY other headers. fixme.*/
c6d33447e28403a90ad817dba4df75fae785be28pquerna switch (key[0]) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'c':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'C':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Content-Type", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_set_content_type(ctx->r, value);
c6d33447e28403a90ad817dba4df75fae785be28pquerna done = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else if (strcasecmp("Connection", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna done = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else if (strcasecmp("Content-Encoding", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna done = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else if (strcasecmp("Content-Length", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna done = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 't':
c6d33447e28403a90ad817dba4df75fae785be28pquerna case 'T':
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp("Transfer-Encoding", key) == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna done = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna default:
c6d33447e28403a90ad817dba4df75fae785be28pquerna break;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!done) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_table_addn(ctx->r->headers_out, key, value);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic serf_bucket_t* accept_response(serf_request_t *request,
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *stream,
c6d33447e28403a90ad817dba4df75fae785be28pquerna void *acceptor_baton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_pool_t *pool)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna s_baton_t *ctx = acceptor_baton;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *c;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_alloc_t *bkt_alloc;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "serf: accept_response");
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* get the per-request bucket allocator */
c6d33447e28403a90ad817dba4df75fae785be28pquerna bkt_alloc = serf_request_get_alloc(request);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* Create a barrier so the response doesn't eat us! */
c6d33447e28403a90ad817dba4df75fae785be28pquerna c = serf_bucket_barrier_create(stream, bkt_alloc);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return serf_bucket_response_create(c, bkt_alloc);
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic apr_status_t handle_response(serf_request_t *request,
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *response,
c6d33447e28403a90ad817dba4df75fae785be28pquerna void *vbaton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_pool_t *pool)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_status_t rv;
c6d33447e28403a90ad817dba4df75fae785be28pquerna s_baton_t *ctx = vbaton;
c6d33447e28403a90ad817dba4df75fae785be28pquerna const char *data;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_size_t len;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_status_line sl;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "serf: handle_response");
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXXXXX: Create better error message. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna rv = serf_bucket_response_status(response, &sl);
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (rv) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (APR_STATUS_IS_EAGAIN(rv)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_SUCCESS;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, ctx->r, "serf_bucket_response_status...");
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->rstatus = HTTP_INTERNAL_SERVER_ERROR;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna if (mpm_supprts_serf) {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_mpm_register_timed_callback(apr_time_from_msec(1),
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna timed_cleanup_callback, ctx);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return rv;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /**
c6d33447e28403a90ad817dba4df75fae785be28pquerna * XXXXX: If I understood serf buckets better, it might be possible to not
c6d33447e28403a90ad817dba4df75fae785be28pquerna * copy all of the data here, and better stream it to the client.
c6d33447e28403a90ad817dba4df75fae785be28pquerna **/
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna do {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna apr_bucket *e;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna apr_brigade_cleanup(ctx->tmpbb);
c6d33447e28403a90ad817dba4df75fae785be28pquerna rv = serf_bucket_read(response, AP_IOBUFSIZE, &data, &len);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (SERF_BUCKET_READ_ERROR(rv)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, ctx->r, "serf_bucket_read(response)");
c6d33447e28403a90ad817dba4df75fae785be28pquerna return rv;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!ctx->done_headers) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *hdrs;
c6d33447e28403a90ad817dba4df75fae785be28pquerna hdrs = serf_bucket_response_get_headers(response);
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_headers_do(hdrs, copy_headers_out, ctx);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->done_headers = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna if (len > 0) {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna /* TODO: make APR bucket <-> serf bucket stuff more magical. */
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna e = apr_bucket_immortal_create(data, len, ctx->r->connection->bucket_alloc);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, e);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "serf: writing %"APR_SIZE_T_FMT" bytes", len);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (APR_STATUS_IS_EOF(rv)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->keep_reading = 0;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna e = apr_bucket_flush_create(ctx->r->connection->bucket_alloc);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, e);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ctx->rstatus = ap_pass_brigade(ctx->r->output_filters, ctx->tmpbb);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna if (mpm_supprts_serf) {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_mpm_register_timed_callback(apr_time_from_msec(1),
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna timed_cleanup_callback, ctx);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_EOF;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ctx->rstatus = ap_pass_brigade(ctx->r->output_filters, ctx->tmpbb);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXX: Should we send a flush now? */
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (APR_STATUS_IS_EAGAIN(rv)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_SUCCESS;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna } while (1);
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic apr_status_t setup_request(serf_request_t *request,
c6d33447e28403a90ad817dba4df75fae785be28pquerna void *vbaton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t **req_bkt,
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_response_acceptor_t *acceptor,
c6d33447e28403a90ad817dba4df75fae785be28pquerna void **acceptor_baton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_response_handler_t *handler,
c6d33447e28403a90ad817dba4df75fae785be28pquerna void **handler_baton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_pool_t *pool)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna s_baton_t *ctx = vbaton;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *hdrs_bkt;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *body_bkt = NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, ctx->r, "serf: setup_request");
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXXX: handle incoming request bodies */
c6d33447e28403a90ad817dba4df75fae785be28pquerna *req_bkt = serf_bucket_request_create(ctx->r->method, ctx->r->unparsed_uri, body_bkt,
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_request_get_alloc(request));
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna hdrs_bkt = serf_bucket_request_get_headers(*req_bkt);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_table_do(copy_headers_in, hdrs_bkt, ctx->r->headers_in, NULL);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (ctx->conf->preservehost) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna serf_bucket_headers_setn(hdrs_bkt, "Host",
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna apr_table_get(ctx->r->headers_in, "Host"));
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna else {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna serf_bucket_headers_setn(hdrs_bkt, "Host", ctx->conf->url.hostname);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_headers_setn(hdrs_bkt, "Accept-Encoding", "gzip");
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ctx->want_ssl) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_alloc_t *req_alloc;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna req_alloc = serf_request_get_alloc(request);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (ctx->ssl_ctx == NULL) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna *req_bkt = serf_bucket_ssl_encrypt_create(*req_bkt, NULL,
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->bkt_alloc);
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->ssl_ctx = serf_bucket_ssl_encrypt_context_get(*req_bkt);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else {
c6d33447e28403a90ad817dba4df75fae785be28pquerna *req_bkt = serf_bucket_ssl_encrypt_create(*req_bkt, ctx->ssl_ctx,
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->bkt_alloc);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna *acceptor = accept_response;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *acceptor_baton = ctx;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *handler = handle_response;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *handler_baton = ctx;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_SUCCESS;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna#ifndef apr_time_from_msec
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna#define apr_time_from_msec(x) (x * 1000)
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna#endif
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna/* TOOD: rewrite drive_serf to make it async */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int drive_serf(request_rec *r, serf_config_t *conf)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_status_t rv;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna apr_pool_t *pool;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_sockaddr_t *address;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna s_baton_t *baton = apr_palloc(r->pool, sizeof(s_baton_t));
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXXX: make persistent/per-process or something.*/
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_context_t *serfme;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_connection_t *conn;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_request_t *srequest;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_server_config_t *ctx =
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna (serf_server_config_t *)ap_get_module_config(r->server->module_config,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &serf_module);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna /* Allocate everything out of a subpool, with a shorter lifetime than
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna * the main request, so that we can cleanup safely and remove our events
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna * from the main serf context in the async mpm mode.
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna */
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna apr_pool_create(&pool, r->pool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (strcmp(conf->url.scheme, "cluster") == 0) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int rc;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_serf_cluster_provider_t *cp;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_cluster_t *cluster;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_array_header_t *servers = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_serf_server_t *choice;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* TODO: could this be optimized in post-config to pre-setup the
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * pointers to the right cluster inside the conf structure?
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster = apr_hash_get(ctx->clusters,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna conf->url.hostname,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_HASH_KEY_STRING);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!cluster) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: unable to find cluster %s", conf->url.hostname);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cp = ap_lookup_provider(AP_SERF_CLUSTER_PROVIDER, cluster->provider, "0");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (cp == NULL) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: unable to find provider %s", cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (cp->list_servers == NULL) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: %s is missing list servers provider.", cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rc = cp->list_servers(cp->baton,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster->params,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &servers);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rc != OK) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: %s list servers returned failure", cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (servers == NULL || apr_is_empty_array(servers)) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: %s failed to provide a list of servers", cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* TOOD: restructure try all servers in the array !! */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna choice = APR_ARRAY_IDX(servers, 0, ap_serf_server_t *);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_sockaddr_info_get(&address, choice->ip,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_UNSPEC, choice->port, 0,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna pool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* XXXXX: cache dns? */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_sockaddr_info_get(&address, conf->url.hostname,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_UNSPEC, conf->url.port, 0,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna pool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (rv != APR_SUCCESS) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "Unable to resolve: %s", conf->url.hostname);
c6d33447e28403a90ad817dba4df75fae785be28pquerna return HTTP_INTERNAL_SERVER_ERROR;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (mpm_supprts_serf) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna serfme = ap_lookup_provider("mpm_serf", "instance", "0");
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (!serfme) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "mpm lied to us about supporting serf.");
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna return HTTP_INTERNAL_SERVER_ERROR;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna else {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna serfme = serf_context_create(pool);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->r = r;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->conf = conf;
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna baton->serf_pool = pool;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->bkt_alloc = serf_bucket_allocator_create(pool, NULL, NULL);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->ssl_ctx = NULL;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->rstatus = OK;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna baton->tmpbb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->done_headers = 0;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->keep_reading = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (strcasecmp(conf->url.scheme, "https") == 0) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->want_ssl = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna else {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton->want_ssl = 0;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna conn = serf_connection_create(serfme, address,
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna conn_setup, baton,
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna closed_connection, baton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna pool);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna srequest = serf_connection_request_create(conn, setup_request,
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna baton);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (mpm_supprts_serf) {
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, baton->r, "handing off serf request to mpm");
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna return SUSPENDED;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna else {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna do {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna rv = serf_context_run(serfme, SERF_DURATION_FOREVER, pool);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna /* XXXX: Handle timeouts */
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (APR_STATUS_IS_TIMEUP(rv)) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna continue;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (rv != APR_SUCCESS) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "serf_context_run()");
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna return HTTP_INTERNAL_SERVER_ERROR;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna serf_debug__closed_conn(baton->bkt_alloc);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna } while (baton->keep_reading);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna return baton->rstatus;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic int serf_handler(request_rec *r)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_config_t *conf = ap_get_module_config(r->per_dir_config,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &serf_module);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (conf->on == 0) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return DECLINED;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return drive_serf(r, conf);
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquernastatic int is_true(const char *w)
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna{
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (strcasecmp(w, "on") == 0 ||
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna strcasecmp(w, "1") == 0 ||
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna strcasecmp(w, "true") == 0)
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna return 1;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna return 0;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const char *add_pass(cmd_parms *cmd, void *vconf,
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna int argc, char *const argv[])
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna int i;
c6d33447e28403a90ad817dba4df75fae785be28pquerna apr_status_t rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_config_t *conf = (serf_config_t *) vconf;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (argc < 1) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna return "SerfPass must have at least a URI.";
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna rv = apr_uri_parse(cmd->pool, argv[0], &conf->url);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (rv != APR_SUCCESS) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna return "mod_serf: Unable to parse SerfPass url.";
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXX: These are bugs in apr_uri_parse. Fixme. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!conf->url.port) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->url.port = apr_uri_port_of_scheme(conf->url.scheme);
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!conf->url.path) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna conf->url.path = "/";
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna for (i = 1; i < argc; i++) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna const char *p = argv[i];
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna const char *x = ap_strchr(p, '=');
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (x) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna char *key = apr_pstrndup(cmd->pool, p, x-p);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (strcmp(key, "preservehost") == 0) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna conf->preservehost = is_true(x+1);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna conf->on = 1;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return NULL;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna/* SerfCluster <name> <provider> <key=value_params_to_provider> ... */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic const char *add_cluster(cmd_parms *cmd, void *d,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int argc, char *const argv[])
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_serf_cluster_provider_t *backend;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int i;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_cluster_t *cluster = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_server_config_t *ctx =
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna (serf_server_config_t *)ap_get_module_config(cmd->server->module_config,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &serf_module);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (err != NULL) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return err;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (argc < 2) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return "SerfCluster must have at least a name and provider.";
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster = apr_palloc(cmd->pool, sizeof(serf_cluster_t));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster->name = apr_pstrdup(cmd->pool, argv[0]);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster->provider = apr_pstrdup(cmd->pool, argv[1]);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster->params = apr_table_make(cmd->pool, 6);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna backend = ap_lookup_provider(AP_SERF_CLUSTER_PROVIDER, cluster->provider, "0");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (backend == NULL) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return apr_psprintf(cmd->pool, "SerfCluster: unable to find "
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "provider '%s'", cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna for (i = 2; i < argc; i++) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *p = argv[i];
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *x = ap_strchr(p, '=');
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (x && strlen(p) > 1) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_addn(cluster->params,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pstrndup(cmd->pool, p, x-p),
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna x+1);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_addn(cluster->params,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pstrdup(cmd->pool, p),
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (backend->check_config == NULL) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return apr_psprintf(cmd->pool, "SerfCluster: Provider '%s' failed to "
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "provider a configuration checker",
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = backend->check_config(backend->baton, cmd, cluster->params);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_hash_set(ctx->clusters, cluster->name, APR_HASH_KEY_STRING, cluster);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic void *create_dir_config(apr_pool_t *p, char *dummy)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_config_t *new = (serf_config_t *) apr_pcalloc(p, sizeof(serf_config_t));
c6d33447e28403a90ad817dba4df75fae785be28pquerna new->on = 0;
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna new->preservehost = 1;
c6d33447e28403a90ad817dba4df75fae785be28pquerna return new;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic void *create_server_config(apr_pool_t *p, server_rec *s)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_server_config_t *ctx =
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna (serf_server_config_t *) apr_pcalloc(p, sizeof(serf_server_config_t));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ctx->clusters = apr_hash_make(p);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return ctx;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic void * merge_server_config(apr_pool_t *p, void *basev, void *overridesv)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_server_config_t *ctx = apr_pcalloc(p, sizeof(serf_server_config_t));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_server_config_t *base = (serf_server_config_t *) basev;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna serf_server_config_t *overrides = (serf_server_config_t *) overridesv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ctx->clusters = apr_hash_overlay(p, base->clusters, overrides->clusters);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return ctx;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic const command_rec serf_cmds[] =
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna AP_INIT_TAKE_ARGV("SerfCluster", add_cluster, NULL, RSRC_CONF,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "Configure a cluster backend"),
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna AP_INIT_TAKE_ARGV("SerfPass", add_pass, NULL, OR_INDEXES,
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna "URL to reverse proxy to"),
c6d33447e28403a90ad817dba4df75fae785be28pquerna {NULL}
c6d33447e28403a90ad817dba4df75fae785be28pquerna};
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernatypedef struct hb_table_baton_t {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_t *p;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *msg;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna} hb_table_baton_t;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int hb_table_check(void *rec, const char *key, const char *value)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hb_table_baton_t *b = (hb_table_baton_t*)rec;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (strcmp(key, "path") != 0) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b->msg = apr_psprintf(b->p,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster Heartbeat Invalid parameter '%s'",
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna key);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return 1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic const char* hb_config_check(void *baton,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cmd_parms *cmd,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_t *params)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hb_table_baton_t b;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (apr_is_empty_table(params)) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return "SerfCluster Heartbeat requires a path to the heartbat information.";
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b.p = cmd->pool;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b.msg = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_do(hb_table_check, &b, params, NULL);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (b.msg) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return b.msg;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernatypedef struct hb_server_t {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *ip;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int busy;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int ready;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int seen;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna} hb_server_t;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic void
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernaargstr_to_table(apr_pool_t *p, char *str, apr_table_t *parms)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char *key;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char *value;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char *strtok_state;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna key = apr_strtok(str, "&", &strtok_state);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna while (key) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna value = strchr(key, '=');
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (value) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *value = '\0'; /* Split the string in two */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna value++; /* Skip passed the = */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna value = "1";
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_unescape_url(key);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_unescape_url(value);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_set(parms, key, value);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna key = apr_strtok(NULL, "&", &strtok_state);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic apr_status_t read_heartbeats(const char *path,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_array_header_t *servers,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_t *pool)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_finfo_t fi;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_status_t rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_file_t *fp;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!path) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return APR_SUCCESS;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_file_open(&fp, path, APR_READ|APR_BINARY|APR_BUFFERED,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_OS_DEFAULT, pool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_file_info_get(&fi, APR_FINFO_SIZE, fp);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char *t;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int lineno = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_t *hbt = apr_table_make(pool, 10);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna char buf[4096];
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna while (apr_file_gets(buf, sizeof(buf), fp) == APR_SUCCESS) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hb_server_t *server;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *ip;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna lineno++;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* comment */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (buf[0] == '#') {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna continue;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* line format: <IP> <query_string>\n */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna t = strchr(buf, ' ');
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!t) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna continue;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ip = apr_pstrndup(pool, buf, t - buf);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna t++;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server = apr_pcalloc(pool, sizeof(hb_server_t));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->ip = ip;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->seen = -1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_clear(hbt);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna argstr_to_table(pool, apr_pstrdup(pool, t), hbt);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (apr_table_get(hbt, "busy")) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->busy = atoi(apr_table_get(hbt, "busy"));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (apr_table_get(hbt, "ready")) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->ready = atoi(apr_table_get(hbt, "ready"));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (apr_table_get(hbt, "lastseen")) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->seen = atoi(apr_table_get(hbt, "lastseen"));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (server->busy == 0 && server->ready != 0) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* Server has zero threads active, but lots of them ready,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * it likely just started up, so lets /4 the number ready,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * to prevent us from completely flooding it with all new
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna * requests.
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->ready = server->ready / 4;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_ARRAY_PUSH(servers, hb_server_t *) = server;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return APR_SUCCESS;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int hb_server_sort(const void *a_, const void *b_)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hb_server_t *a = (hb_server_t*)a;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hb_server_t *b = (hb_server_t*)b;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (a->ready == b->ready) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else if (a->ready > b->ready) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return -1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return 1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int hb_list_servers(void *baton,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna request_rec *r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_t *params,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_array_header_t **out_servers)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna int i;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hb_server_t *hbs;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_status_t rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_t *tpool;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_array_header_t *tmpservers;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_array_header_t *servers;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna const char *path = apr_table_get(params, "path");
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_create(&tpool, r->pool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
34d7b4e5fca8d933d55caefebd7238b38c928b25pquerna path = ap_server_root_relative(tpool, path);
34d7b4e5fca8d933d55caefebd7238b38c928b25pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna tmpservers = apr_array_make(tpool, 32, sizeof(hb_server_t *));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = read_heartbeats(path, tmpservers, tpool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: Heartbeat unable to read '%s'", path);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_destroy(tpool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna qsort(tmpservers->elts, tmpservers->nelts, sizeof(hb_server_t *),
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hb_server_sort);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna servers = apr_array_make(r->pool, tmpservers->nelts, sizeof(ap_serf_server_t *));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna for (i = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna i < tmpservers->nelts;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna i++)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_serf_server_t *x;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna hbs = APR_ARRAY_IDX(tmpservers, i, hb_server_t *);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (hbs->ready > 0) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna x = apr_palloc(r->pool, sizeof(ap_serf_server_t));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna x->ip = apr_pstrdup(r->pool, hbs->ip);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* TODO: expand multicast format to support ports? */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna x->port = 80;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_ARRAY_PUSH(servers, ap_serf_server_t *) = x;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna *out_servers = servers;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_destroy(tpool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return OK;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic const ap_serf_cluster_provider_t builtin_heartbeat =
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "heartbeat",
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna NULL,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &hb_config_check,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &hb_list_servers,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna NULL,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna NULL
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna};
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernastatic void register_hooks(apr_pool_t *p)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna apr_status_t rv;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna rv = ap_mpm_query(AP_MPMQ_HAS_SERF, &mpm_supprts_serf);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (rv != APR_SUCCESS) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna mpm_supprts_serf = 0;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_register_provider(p, AP_SERF_CLUSTER_PROVIDER,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "heartbeat", "0", &builtin_heartbeat);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_hook_handler(serf_handler, NULL, NULL, APR_HOOK_FIRST);
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquernamodule AP_MODULE_DECLARE_DATA serf_module =
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c6d33447e28403a90ad817dba4df75fae785be28pquerna STANDARD20_MODULE_STUFF,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna create_dir_config,
c6d33447e28403a90ad817dba4df75fae785be28pquerna NULL,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna create_server_config,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna merge_server_config,
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_cmds,
c6d33447e28403a90ad817dba4df75fae785be28pquerna register_hooks
c6d33447e28403a90ad817dba4df75fae785be28pquerna};