Created
February 8, 2012 22:12
-
-
Save insom/1774452 to your computer and use it in GitHub Desktop.
Space Core Apache Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ==================================================================== | |
* Copyright (c) 2012 The Apache Group. All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions | |
* are met: | |
* | |
* 1. Redistributions of source code must retain the above copyright | |
* notice, this list of conditions and the following disclaimer. | |
* | |
* 2. Redistributions in binary form must reproduce the above copyright | |
* notice, this list of conditions and the following disclaimer in | |
* the documentation and/or other materials provided with the | |
* distribution. | |
* | |
* 3. All advertising materials mentioning features or use of this | |
* software must display the following acknowledgment: | |
* "This product includes software developed by the Apache Group | |
* for use in the Apache HTTP server project (http://www.apache.org/)." | |
* | |
* 4. The names "Apache Server" and "Apache Group" must not be used to | |
* endorse or promote products derived from this software without | |
* prior written permission. | |
* | |
* 5. Redistributions of any form whatsoever must retain the following | |
* acknowledgment: | |
* "This product includes software developed by the Apache Group | |
* for use in the Apache HTTP server project (http://www.apache.org/)." | |
* | |
* THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY | |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR | |
* IT'S CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
* OF THE POSSIBILITY OF SUCH DAMAGE. | |
* ==================================================================== | |
* | |
* This software consists of voluntary contributions made by many | |
* individuals on behalf of the Apache Group and was originally based | |
* on public domain software written at the National Center for | |
* Supercomputing Applications, University of Illinois, Urbana-Champaign. | |
* For more information on the Apache Group and the Apache HTTP server | |
* project, please see <http://www.apache.org/>. | |
* | |
*/ | |
#include "httpd.h" | |
#include "http_config.h" | |
#include "http_core.h" | |
#include "http_log.h" | |
#include "http_protocol.h" | |
#include "http_vhost.h" | |
#include "apr_strings.h" | |
#include "apr_ring.h" | |
#include <stdlib.h> | |
typedef struct _quote_t { | |
APR_RING_ENTRY(_quote_t) link; | |
const char *quote; | |
} quote_t; | |
typedef struct _quote_ring_t quote_ring_t; | |
APR_RING_HEAD(_quote_ring_t, _quote_t); | |
module AP_MODULE_DECLARE_DATA space_core_module; | |
typedef struct { | |
quote_ring_t *ring; | |
int count; | |
} space_core_server_cfg; | |
static void *space_core_create_server_cfg(apr_pool_t *p, server_rec *s) { | |
space_core_server_cfg *cfg = (space_core_server_cfg *)apr_pcalloc(p, sizeof(space_core_server_cfg)); | |
if (!cfg) | |
return NULL; | |
cfg->ring = apr_palloc(p, sizeof(quote_ring_t)); | |
cfg->count = 0; | |
APR_RING_INIT(cfg->ring, _quote_t, link); | |
return (void *)cfg; | |
} | |
static const char *space_core_file(cmd_parms *cmd, void *dummy, char *filename) { | |
server_rec *s = cmd->server; | |
space_core_server_cfg *cfg = (space_core_server_cfg *)ap_get_module_config(s->module_config, &space_core_module); | |
char buf[1024] = {0}; | |
apr_file_t *fd; | |
apr_file_open(&fd, filename, APR_READ, APR_OS_DEFAULT, cmd->pool); | |
while(APR_EOF != apr_file_gets(buf, 1024, fd)) { | |
if(strlen(buf) > 1) { | |
buf[strlen(buf)-1] = '\0'; | |
quote_t *elem = apr_palloc(cmd->pool, sizeof(quote_t)); | |
elem->quote = apr_pstrdup(cmd->pool, buf); | |
APR_RING_INSERT_TAIL(cfg->ring, elem, _quote_t, link); | |
cfg->count++; | |
} | |
} | |
return NULL; | |
} | |
static const command_rec space_core_cmds[] = { | |
AP_INIT_TAKE1( | |
"SpaceCoreFile", | |
space_core_file, | |
NULL, | |
RSRC_CONF, | |
"Location of Quotes File" | |
), | |
{ NULL } | |
}; | |
static int ap_headers_fixup(request_rec *r) { | |
space_core_server_cfg *cfg = (space_core_server_cfg *)ap_get_module_config(r->server->module_config, &space_core_module); | |
int rand = random() % cfg->count; | |
quote_t* elem; | |
for(elem = APR_RING_FIRST(cfg->ring); elem && rand; elem = APR_RING_NEXT(elem, link)) | |
rand--; | |
if(elem) | |
apr_table_set(r->headers_out, "X-Space-Core", elem->quote); | |
return DECLINED; | |
} | |
static void register_hooks(apr_pool_t *p) { | |
ap_hook_fixups(ap_headers_fixup, NULL, NULL, APR_HOOK_LAST); | |
} | |
module AP_MODULE_DECLARE_DATA space_core_module = { | |
STANDARD20_MODULE_STUFF, | |
NULL, | |
NULL, | |
space_core_create_server_cfg, | |
NULL, | |
space_core_cmds, | |
register_hooks, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
=== An example Apache Software License 1.1 file ===
This is the original Apache License which applies only to very old versions of Apache packages (such as version 1.2 of the Web server).
/* ====================================================================
*/