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