mod_caucho.c

来自「《jsp编程起步》里面的所有源代码」· C语言 代码 · 共 944 行 · 第 1/2 页

C
944
字号
/* * Copyright (c) 1999-2000 Caucho Technology.  All rights reserved. * * Caucho Technology permits redistribution, modification and use * of this file in source and binary form ("the Software") under the * Caucho Developer Source License ("the License").  In particular, the following * conditions must be met: * * 1. Each copy or derived work of the Software must preserve the copyright *    notice and this notice unmodified. * * 2. Redistributions of the Software in source or binary form must include  *    an unmodified copy of the License, normally in a plain ASCII text * * 3. The names "Resin" or "Caucho" are trademarks of Caucho Technology and *    may not be used to endorse products derived from this software. *    "Resin" or "Caucho" may not appear in the names of products derived *    from this software. * * 4. Caucho Technology requests that attribution be given to Resin *    in any manner possible.  We suggest using the "Resin Powered" *    button or creating a "powered by Resin(tm)" link to *    http://www.caucho.com for each page served by Resin. * * This Software is provided "AS IS," without a warranty of any kind.  * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY * OF SUCH DAMAGES. * * @author Scott Ferguson * * $Id: mod_caucho.c,v 1.40 2000/10/26 19:21:07 ferg Exp $ */#include "httpd.h"#include "http_config.h"#include "http_request.h"#include "http_core.h"#include "http_protocol.h"#include "http_log.h"#include "http_main.h"#include "util_script.h"#include "http_conf_globals.h"#include <stdlib.h>#include <errno.h>#include "cse.h"#include "version.h"/* * Apache magic module declaration. */module MODULE_VAR_EXPORT caucho_module;#define BUF_LENGTH 8192#define DEFAULT_PORT 6802static char *g_error_page = 0;voidcse_log(char *fmt, ...){#ifdef DEBUG  va_list args;  FILE *file = fopen("/tmp/log", "a+");  if (file) {      va_start(args, fmt);    vfprintf(file, fmt, args);    va_end(args);    fclose(file);  }#endif}void *cse_create_lock(){#ifdef WIN32	return CreateMutex(0, 0, "resin");#else	return 0;#endif}intcse_lock(void *lock){#ifdef WIN32	if (lock)		WaitForSingleObject(lock, INFINITE);	return 1;#else  return 1;#endif}voidcse_unlock(void *lock){#ifdef WIN32	if (lock)		ReleaseMutex(lock);#endif}voidcse_error(config_t *config, char *format, ...){  char buf[BUF_LENGTH];  va_list args;  va_start(args, format);  vsprintf(buf, format, args);  va_end(args);  config->error = cse_strdup(config->p, buf);}voidcse_set_socket_cleanup(int socket, void *pool){  LOG(("set cleanup %d\n", socket));  if (socket > 0)    ap_note_cleanups_for_socket(pool, socket);}voidcse_kill_socket_cleanup(int socket, void *pool){  LOG(("kill cleanup %d\n", socket));  if (socket > 0)    ap_kill_cleanups_for_socket(pool, socket);}void *cse_malloc(int size){  return malloc(size);}void cse_free(config_t *config, void *data) {}static voidcse_module_init(struct server_rec *server, struct pool *pool){  ap_add_version_component(VERSION);}static void *cse_create_server_config(pool *p, server_rec *server){  config_t *config = (config_t *) malloc(sizeof(config_t));  srun_t *srun_list = (srun_t *) malloc(16 * sizeof(srun_t));  srun_t *srun;  memset(config, 0, sizeof(config_t));  memset(srun_list, 0, 16 * sizeof(srun_t));  config->server = server;  config->p = cse_create_pool();  config->srun_list = srun_list;  config->srun_capacity = 16;  config->srun_size = 1;    srun = config->srun_list;  srun->port = DEFAULT_PORT;  srun->session = 0;    srun->max_sockets = 32;  return (void *) config;}/** * Retrieves the caucho configuration from Apache */static config_t *cse_get_module_config(server_rec *s){  return (config_t *) ap_get_module_config(s->module_config, &caucho_module);}/** * Parse the CauchoConfigHost configuration in the apache config file. */static const char *cse_config_file_command(cmd_parms *cmd, void *mconfig, char *value){  config_t *config = cse_get_module_config(cmd->server);   config->path = ap_server_root_relative(cmd->pool, value);  cse_init_config(config);  return config->error;}/** * Parse the CauchoHosts configuration in the apache config file. */static const char *cse_host_command(cmd_parms *cmd, void *mconfig, char *host_arg, char *port_arg){  config_t *config = cse_get_module_config(cmd->server);  int port = port_arg ? atoi(port_arg) : DEFAULT_PORT;  cse_add_host(config, host_arg, port);  return config->error;}/** * Parse the CauchoBackup configuration in the apache config file. */static const char *cse_backup_command(cmd_parms *cmd, void *mconfig,		   char *host_arg, char *port_arg){  config_t *config = cse_get_module_config(cmd->server);  int port = port_arg ? atoi(port_arg) : DEFAULT_PORT;  cse_add_backup(config, host_arg, port);  return config->error;}/** * Parse the CauchoKeepalive configuration in the apache config file. */static const char *cse_keepalive_command(cmd_parms *cmd, void *mconfig, int flag){  /* g_srun_keepalive = flag; */  return 0;}/** * Parse the CauchoHosts configuration in the apache config file. */static const char *cse_error_page_command(cmd_parms *cmd, void *mconfig, char *error_page_arg){  g_error_page = error_page_arg;  return 0;}/** * Look at the request to see if Caucho should handle it. */static intcse_dispatch(request_rec *r){  config_t *config = cse_get_module_config(r->server);  const char *host = ap_get_server_name(r);  const char *uri = r->uri;  if (config == NULL)    return DECLINED;  cse_update_config(config, r->request_time);    /* Check for exact virtual host match */  if (cse_match_request(config, host, ap_get_server_port(r), uri)) {    ap_set_module_config(r->request_config, &caucho_module, 0);    r->handler = "caucho-request";    return OK;  }  if (! config->disable_caucho_status && ! strcmp(uri, "/caucho-status")) {    ap_set_module_config(r->request_config, &caucho_module, 0);    r->handler = "caucho-status";    return OK;  }  return DECLINED;}/** * Gets the session index from the request * * Cookies have priority over the query * * @return -1 if no session */static intget_session_index(request_rec *r){  array_header *hdrs_arr = ap_table_elts(r->headers_in);  table_entry *hdrs = (table_entry *) hdrs_arr->elts;  int i;  int session;  for (i = 0; i < hdrs_arr->nelts; ++i) {    if (! hdrs[i].key || ! hdrs[i].val)      continue;    if (strcasecmp(hdrs[i].key, "Cookie"))      continue;    session = cse_session_from_string(hdrs[i].val, "JSESSIONID=");    if (session >= 0)      return session;  }  return cse_session_from_string(r->uri, "jsessionid=");}/** * Forward to an error page. */static inthandle_include(char *uri, request_rec *r){  request_rec *subrequest = ap_sub_req_lookup_uri(uri, r);  int status;  LOG(("forwarding %s %d\n", uri, subrequest->status));    if (subrequest->status != HTTP_OK)    return -1;  /* copy the args from the original request */  subrequest->path_info = r->path_info;  subrequest->args = r->args;  /* Run it. */  status = ap_run_sub_req(subrequest);  ap_destroy_sub_req(subrequest);  return status;}static intconnection_error(config_t *config, srun_t *srun, request_rec *r){  char *error_page = config->error_page;    if (g_error_page)    error_page = g_error_page;  if (error_page) {    int status = handle_include(error_page, r);    if (status == OK)      return status;  }  r->content_type = "text/html";  if (error_page) {    r->status = 302;    ap_table_set(r->headers_out, "Location", error_page);  }  else    r->status = 503;  ap_send_http_header(r);  ap_rputs("<html><body bgcolor='white'>", r);  ap_rprintf(r, "<h1>Can't contact servlet runner at %s:%d</h1>", 	     srun->hostname ? srun->hostname : "localhost",	     srun->port);  ap_rputs("</body></html>", r);    ap_rputs("\n\n\n\n", r);  ap_rputs("<!--\n", r);  ap_rputs("  -- Unfortunately, Microsoft has added a clever new\n", r);  ap_rputs("  -- \"feature\" to Internet Explorer.  If the text in\n", r);  ap_rputs("  -- an error's message is \"too small\", specifically\n", r);  ap_rputs("  -- less than 512 bytes, Internet Explorer returns\n", r);  ap_rputs("  -- its own error message.  Yes, you can turn that\n", r);  ap_rputs("  -- off, but *surprise* it's pretty tricky to find\n", r);  ap_rputs("  -- buried as a switch called \"smart error\n", r);  ap_rputs("  -- messages\"  That means, of course, that many of\n", r);  ap_rputs("  -- Resin's error messages are censored by default.\n", r);  ap_rputs("  -- And, of course, you'll be shocked to learn that\n", r);  ap_rputs("  -- IIS always returns error messages that are long\n", r);  ap_rputs("  -- enough to make Internet Explorer happy.  The\n", r);  ap_rputs("  -- workaround is pretty simple: pad the error\n", r);  ap_rputs("  -- message with a big comment to push it over the\n", r);  ap_rputs("  -- five hundred and twelve byte minimum.  Of course,\n", r);  ap_rputs("  -- that's exactly what you're reading right now.\n", r);  ap_rputs("  -->\n", r);  ap_kill_timeout(r);  return OK;}/** * Writes request parameters to srun. */static voidwrite_env(stream_t *s, request_rec *r){  char buf[4096];  int ch;  int i;    conn_rec *c = r->connection;  const char *host;  int port;  cse_write_string(s, CSE_PROTOCOL, r->protocol);  cse_write_string(s, CSE_METHOD, r->method);  for (i = 0; (ch = r->uri[i]) && ch != '?' && i + 1 < sizeof(buf); i++)     buf[i] = ch;  buf[i] = 0;  cse_write_string(s, CSE_URI, buf);  if (r->args)    cse_write_string(s, CSE_QUERY_STRING, r->args);  /* Gets the server name */  host = ap_get_server_name(r);  port = ap_get_server_port(r);  cse_write_string(s, CSE_SERVER_NAME, host);  cse_write_string(s, CSE_SERVER_PORT, ap_psprintf(r->pool, "%u", port));  host = ap_get_remote_host(c, r->per_dir_config, REMOTE_HOST);  if (host)    cse_write_string(s, CSE_REMOTE_HOST, host);  else    cse_write_string(s, CSE_REMOTE_HOST, c->remote_ip);  cse_write_string(s, CSE_REMOTE_ADDR, c->remote_ip);  cse_write_string(s, CSE_REMOTE_PORT,		   ap_psprintf(r->pool, "%d", ntohs(c->remote_addr.sin_port)));    if (c->user)    cse_write_string(s, CSE_REMOTE_USER, c->user);  if (c->ap_auth_type)    cse_write_string(s, CSE_AUTH_TYPE, c->ap_auth_type);  cse_write_string(s, CSE_SESSION_GROUP,		   ap_psprintf(r->pool, "%d", s->srun->session));  /* mod_ssl */#ifdef EAPI  {    static char *vars[] = { "SSL_CLIENT_S_DN",                            "SSL_CIPHER",                            "SSL_CIPHER_EXPORT",                            "SSL_PROTOCOL",                            "SSL_CIPHER_USEKEYSIZE",                            "SSL_CIPHER_ALGKEYSIZE",                            0};    char *var;    int i;    int v;        if ((v = ap_hook_call("ap::mod_ssl::var_lookup", &var, r->pool, r->server,                          r->connection, r, "SSL_CLIENT_CERT"))) {      cse_write_string(s, CSE_CLIENT_CERT, var);    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?