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"
30741f9360fff567fe3450441aaf1e96d19bae9ftrawick#include "apr_version.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;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna serf_bucket_t *body_bkt;
c6d33447e28403a90ad817dba4df75fae785be28pquerna} s_baton_t;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
30741f9360fff567fe3450441aaf1e96d19bae9ftrawick#if !APR_VERSION_AT_LEAST(1,4,0)
30741f9360fff567fe3450441aaf1e96d19bae9ftrawick#define apr_time_from_msec(x) (x * 1000)
30741f9360fff567fe3450441aaf1e96d19bae9ftrawick#endif
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;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna /* Causes all serf connections to unregister from the event mpm */
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna if (ctx->rstatus) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, ctx->rstatus, ctx->r, APLOGNO(01119)
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna "serf: request returned: %d", ctx->rstatus);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ctx->r->status = HTTP_OK;
602b8c83188d37e4782411a3829a0f410839e78bpquerna apr_pool_destroy(ctx->serf_pool);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna ap_die(ctx->rstatus, ctx->r);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna else {
602b8c83188d37e4782411a3829a0f410839e78bpquerna apr_bucket *e;
602b8c83188d37e4782411a3829a0f410839e78bpquerna apr_brigade_cleanup(ctx->tmpbb);
602b8c83188d37e4782411a3829a0f410839e78bpquerna e = apr_bucket_flush_create(ctx->r->connection->bucket_alloc);
602b8c83188d37e4782411a3829a0f410839e78bpquerna APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, e);
602b8c83188d37e4782411a3829a0f410839e78bpquerna e = apr_bucket_eos_create(ctx->r->connection->bucket_alloc);
602b8c83188d37e4782411a3829a0f410839e78bpquerna APR_BRIGADE_INSERT_TAIL(ctx->tmpbb, e);
602b8c83188d37e4782411a3829a0f410839e78bpquerna
602b8c83188d37e4782411a3829a0f410839e78bpquerna /* TODO: return code? bleh */
602b8c83188d37e4782411a3829a0f410839e78bpquerna ap_pass_brigade(ctx->r->output_filters, ctx->tmpbb);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
602b8c83188d37e4782411a3829a0f410839e78bpquerna apr_pool_destroy(ctx->serf_pool);
602b8c83188d37e4782411a3829a0f410839e78bpquerna
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
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (why) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* justin says that error handling isn't done yet. hah. */
c6d33447e28403a90ad817dba4df75fae785be28pquerna /* XXXXXX: review */
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, why, ctx->r, APLOGNO(01120) "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 }
09b4ea40821c7f8663d47a42f31ab74ee9c0f352jfclere ctx->keep_reading = 0;
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
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
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim /* 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{
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *c;
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_alloc_t *bkt_alloc;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
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
09b4ea40821c7f8663d47a42f31ab74ee9c0f352jfclere if (response == NULL) {
09b4ea40821c7f8663d47a42f31ab74ee9c0f352jfclere ctx->rstatus = HTTP_INTERNAL_SERVER_ERROR;
09b4ea40821c7f8663d47a42f31ab74ee9c0f352jfclere return APR_EGENERAL;
09b4ea40821c7f8663d47a42f31ab74ee9c0f352jfclere }
09b4ea40821c7f8663d47a42f31ab74ee9c0f352jfclere
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
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, ctx->r, APLOGNO(01121) "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 }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
c6d33447e28403a90ad817dba4df75fae785be28pquerna /**
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * 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_brigade_cleanup(ctx->tmpbb);
c6d33447e28403a90ad817dba4df75fae785be28pquerna rv = serf_bucket_read(response, AP_IOBUFSIZE, &data, &len);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (SERF_BUCKET_READ_ERROR(rv)) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, ctx->r, APLOGNO(01122) "serf_bucket_read(response)");
c6d33447e28403a90ad817dba4df75fae785be28pquerna return rv;
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (!ctx->done_headers) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna serf_bucket_t *hdrs;
602b8c83188d37e4782411a3829a0f410839e78bpquerna serf_status_line line;
602b8c83188d37e4782411a3829a0f410839e78bpquerna
602b8c83188d37e4782411a3829a0f410839e78bpquerna /* TODO: improve */
602b8c83188d37e4782411a3829a0f410839e78bpquerna serf_bucket_response_status(response, &line);
602b8c83188d37e4782411a3829a0f410839e78bpquerna ctx->r->status = line.code;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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. */
602b8c83188d37e4782411a3829a0f410839e78bpquerna apr_brigade_write(ctx->tmpbb, NULL, NULL, data, len);
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna }
ee55ac2cd2fd8c48de97c754758004a7a0176336pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna if (APR_STATUS_IS_EOF(rv)) {
c6d33447e28403a90ad817dba4df75fae785be28pquerna ctx->keep_reading = 0;
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
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna *req_bkt = serf_bucket_request_create(ctx->r->method, ctx->r->unparsed_uri,
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna ctx->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 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 }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
c6d33447e28403a90ad817dba4df75fae785be28pquerna *acceptor = accept_response;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *acceptor_baton = ctx;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *handler = handle_response;
c6d33447e28403a90ad817dba4df75fae785be28pquerna *handler_baton = ctx;
c6d33447e28403a90ad817dba4df75fae785be28pquerna
c6d33447e28403a90ad817dba4df75fae785be28pquerna return APR_SUCCESS;
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna/* TOOD: rewrite drive_serf to make it async */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int drive_serf(request_rec *r, serf_config_t *conf)
c6d33447e28403a90ad817dba4df75fae785be28pquerna{
c4880e5f9ac1641acf44c68fd9ae1a49bff8e00fjfclere apr_status_t rv = 0;
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;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim 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;
c4880e5f9ac1641acf44c68fd9ae1a49bff8e00fjfclere apr_uint32_t pick = 0;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_serf_server_t *choice;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim /* 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) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01123)
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");
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (cp == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01124)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: unable to find provider %s", cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (cp->list_servers == NULL) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01125)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: %s is missing list servers provider.", cluster->provider);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rc = cp->list_servers(cp->baton,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna r,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna cluster->params,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna &servers);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rc != OK) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(01126)
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)) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, APLOGNO(01127)
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 !! */
7bf4eb9638a127fbfbc402d2c0e4ec0085934cf0sf pick = ap_random_pick(0, servers->nelts-1);
c4880e5f9ac1641acf44c68fd9ae1a49bff8e00fjfclere choice = APR_ARRAY_IDX(servers, pick, 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) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01128) "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) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01129) "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);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna baton->body_bkt = 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
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna rv = ap_setup_client_block(baton->r, REQUEST_CHUNKED_DECHUNK);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna if (rv) {
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna return rv;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna }
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna /* TODO: create custom serf bucket, which does async request body reads */
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna if (ap_should_client_block(r)) {
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna apr_size_t len;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna apr_off_t flen = 0;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna char buf[AP_IOBUFSIZE];
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna apr_file_t *fp;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna rv = apr_file_mktemp(&fp, "mod_serf_buffer.XXXXXX", 0, pool);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna if (rv != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01130) "Unable to create temp request body buffer file.");
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna return HTTP_INTERNAL_SERVER_ERROR;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna }
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna do {
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna len = sizeof(buf);
7f598c776b539f51e52ba49fa61bc9ddab6511b8minfrin /* FIXME: ap_get_client_block() returns long, not apr_status_t */
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna rv = ap_get_client_block(baton->r, buf, len);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna if (rv > 0) {
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna rv = apr_file_write_full(fp, buf, rv, NULL);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna if (rv) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01131) "failed to read request body");
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna return HTTP_INTERNAL_SERVER_ERROR;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna }
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna }
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna } while(rv > 0);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna if (rv < 0) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01132) "failed to read request body");
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna return HTTP_INTERNAL_SERVER_ERROR;
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna }
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna apr_file_seek(fp, APR_SET, &flen);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna baton->body_bkt = serf_bucket_file_create(fp, baton->bkt_alloc);
d3b07cc74a282e33675ae3f5166cd72ddb8de2acpquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
c6d33447e28403a90ad817dba4df75fae785be28pquerna conn = serf_connection_create(serfme, address,
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna conn_setup, baton,
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna closed_connection, baton,
c6d33447e28403a90ad817dba4df75fae785be28pquerna pool);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
a9a941ed6ca86039137d64bdc7b1c4fda9d07d12sf /* XXX: Is it correct that we don't use the returned serf_request_t? */
a9a941ed6ca86039137d64bdc7b1c4fda9d07d12sf serf_connection_request_create(conn, setup_request, baton);
c6d33447e28403a90ad817dba4df75fae785be28pquerna
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (mpm_supprts_serf) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna return SUSPENDED;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna else {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna do {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna rv = serf_context_run(serfme, SERF_DURATION_FOREVER, pool);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna /* XXXX: Handle timeouts */
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (APR_STATUS_IS_TIMEUP(rv)) {
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna continue;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna if (rv != APR_SUCCESS) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01133) "serf_context_run() for %pI", address);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim return HTTP_INTERNAL_SERVER_ERROR;
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna serf_debug__closed_conn(baton->bkt_alloc);
82d8a5c340e2d50ebadc542a6422bacf3c244432pquerna } while (baton->keep_reading);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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{
3c290fd0361d6d9d84d97725eaf299456bddd6cfsf if (strcasecmp(w, "on") == 0 || strcmp(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) {
678a15e91d6a44569c956445442731bb64a98a63sf return "Unable to parse SerfPass url.";
c6d33447e28403a90ad817dba4df75fae785be28pquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
9703c0ad7ad67aa1af2e48c6b48a0d0c7ff5cb2dsf if (!conf->url.scheme) {
678a15e91d6a44569c956445442731bb64a98a63sf return "Need scheme part in url.";
9703c0ad7ad67aa1af2e48c6b48a0d0c7ff5cb2dsf }
9703c0ad7ad67aa1af2e48c6b48a0d0c7ff5cb2dsf
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];
e39aeaad2bcadf5063b114e8deff424a1bd3502ftrawick const char *x = ap_strchr_c(p, '=');
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna if (x) {
3c290fd0361d6d9d84d97725eaf299456bddd6cfsf if (strncmp(p, "preservehost", x-p) == 0) {
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna conf->preservehost = is_true(x+1);
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna }
c6d33447e28403a90ad817dba4df75fae785be28pquerna
fa9496078a83e18311b90b33574fdeb9c115ed7dpquerna conf->on = 1;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim 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);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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 }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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");
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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];
e39aeaad2bcadf5063b114e8deff424a1bd3502ftrawick const char *x = ap_strchr_c(p, '=');
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (x && strlen(p) > 1) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_addn(cluster->params,
9f29446ad25e6170e8b6c1b604471958f2bb9abfjailletc apr_pstrmemdup(cmd->pool, p, x-p),
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna x+1);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna else {
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim 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);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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{
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim 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;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ctx->clusters = apr_hash_overlay(p, base->clusters, overrides->clusters);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return ctx;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim}
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)) {
8292ec4228fbb1fe0e6875e68bdc5e2fd095fe6dtrawick return "SerfCluster Heartbeat requires a path to the heartbeat information.";
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b.p = cmd->pool;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna b.msg = NULL;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_do(hb_table_check, &b, params, NULL);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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;
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere unsigned int port;
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;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!path) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return APR_SUCCESS;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_file_open(&fp, path, APR_READ|APR_BINARY|APR_BUFFERED,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna APR_OS_DEFAULT, pool);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna rv = apr_file_info_get(&fi, APR_FINFO_SIZE, fp);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (rv) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return rv;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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 }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna /* line format: <IP> <query_string>\n */
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna t = strchr(buf, ' ');
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (!t) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna continue;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
9f29446ad25e6170e8b6c1b604471958f2bb9abfjailletc ip = apr_pstrmemdup(pool, buf, t - buf);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna t++;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server = apr_pcalloc(pool, sizeof(hb_server_t));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->ip = ip;
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere server->port = 80;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->seen = -1;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_table_clear(hbt);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna argstr_to_table(pool, apr_pstrdup(pool, t), hbt);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (apr_table_get(hbt, "busy")) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->busy = atoi(apr_table_get(hbt, "busy"));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (apr_table_get(hbt, "ready")) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->ready = atoi(apr_table_get(hbt, "ready"));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (apr_table_get(hbt, "lastseen")) {
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna server->seen = atoi(apr_table_get(hbt, "lastseen"));
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere if (apr_table_get(hbt, "port")) {
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere server->port = atoi(apr_table_get(hbt, "port"));
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna if (server->busy == 0 && server->ready != 0) {
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim /* Server has zero threads active, but lots of them ready,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * it likely just started up, so lets /4 the number ready,
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim * 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 }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return APR_SUCCESS;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna}
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquernastatic int hb_server_sort(const void *a_, const void *b_)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna{
09eec2b9e10f4fd1b8c5f7f1a105a354ce270cafpquerna hb_server_t *a = (hb_server_t*)a_;
09eec2b9e10f4fd1b8c5f7f1a105a354ce270cafpquerna 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) {
185aa71728867671e105178b4c66fbc22b65ae26sf ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, APLOGNO(01134)
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "SerfCluster: Heartbeat unable to read '%s'", path);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna apr_pool_destroy(tpool);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna return HTTP_INTERNAL_SERVER_ERROR;
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
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);
c87ed6a25f669f7137024ccde3771d95b9c37e07jfclere x->port = hbs->port;
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
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquernastatic int static_table_check(void *rec, const char *key, const char *value)
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna{
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna hb_table_baton_t *b = (hb_table_baton_t*)rec;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna if (strcmp(key, "hosts") != 0 &&
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna strcmp(key, "order") != 0) {
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna b->msg = apr_psprintf(b->p,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna "SerfCluster Static Invalid parameter '%s'",
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna key);
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna return 1;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna return 0;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna}
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquernastatic const char* static_config_check(void *baton,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna cmd_parms *cmd,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna apr_table_t *params)
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna{
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna hb_table_baton_t b;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna if (apr_is_empty_table(params)) {
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna return "SerfCluster Static requires at least a host list.";
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna b.p = cmd->pool;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna b.msg = NULL;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna apr_table_do(static_table_check, &b, params, NULL);
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna if (b.msg) {
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna return b.msg;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna if (apr_table_get(params, "hosts") == NULL) {
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna return "SerfCluster Static requires at least a hosts parameter";
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna }
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna return NULL;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna}
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquernastatic int static_list_servers(void *baton,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna request_rec *r,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna apr_table_t *params,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna apr_array_header_t **out_servers)
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna{
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna apr_status_t rv;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna char *ip;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna char *strtok_state;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna apr_array_header_t *servers;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna const char *hosts = apr_table_get(params, "hosts");
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna const char *order = apr_table_get(params, "order");
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna servers = apr_array_make(r->pool, 10, sizeof(ap_serf_server_t *));
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna ip = apr_strtok(apr_pstrdup(r->pool, hosts), ",", &strtok_state);
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna while (ip) {
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna char *host_str;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna char *scope_id;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna apr_port_t port = 0;
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna rv = apr_parse_addr_port(&host_str, &scope_id, &port, ip, r->pool);
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna if (!rv) {
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna ap_serf_server_t *s = apr_palloc(r->pool, sizeof(ap_serf_server_t));
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna s->ip = host_str;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna s->port = port ? port : 80;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna APR_ARRAY_PUSH(servers, ap_serf_server_t *) = s;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna }
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna ip = apr_strtok(NULL, ",", &strtok_state);
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna }
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna if (strcmp(order, "random") == 0) {
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna /* TODO: support order=random */
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna }
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna *out_servers = servers;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna return OK;
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna}
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquernastatic const ap_serf_cluster_provider_t builtin_static =
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna{
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna "static",
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna NULL,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna &static_config_check,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna &static_list_servers,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna NULL,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna NULL
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna};
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
12ff9b4152aa61427100734621a8f589ee422915trawickstatic int serf_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptemp, server_rec *s)
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 }
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim
5bfaaf573bacb45c1cf290ce85ecc676587e8a64jim return OK;
12ff9b4152aa61427100734621a8f589ee422915trawick}
12ff9b4152aa61427100734621a8f589ee422915trawick
12ff9b4152aa61427100734621a8f589ee422915trawickstatic void register_hooks(apr_pool_t *p)
12ff9b4152aa61427100734621a8f589ee422915trawick{
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna ap_register_provider(p, AP_SERF_CLUSTER_PROVIDER,
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna "heartbeat", "0", &builtin_heartbeat);
546d45814f6de2976187f3eaad9ba7a4b01c8b77pquerna
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna ap_register_provider(p, AP_SERF_CLUSTER_PROVIDER,
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna "static", "0", &builtin_static);
27f3bbeac4088b6826481a57adf54c0d375b0d7bpquerna
12ff9b4152aa61427100734621a8f589ee422915trawick ap_hook_post_config(serf_post_config, NULL, NULL, APR_HOOK_MIDDLE);
12ff9b4152aa61427100734621a8f589ee422915trawick
c6d33447e28403a90ad817dba4df75fae785be28pquerna ap_hook_handler(serf_handler, NULL, NULL, APR_HOOK_FIRST);
c6d33447e28403a90ad817dba4df75fae785be28pquerna}
c6d33447e28403a90ad817dba4df75fae785be28pquerna
36ef8f77bffe75d1aa327882be1b5bdbe2ff567asfAP_DECLARE_MODULE(serf) =
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};