mod_jk.c

来自「以便Apache与其他服务进行整合 Mod_JK安装」· C语言 代码 · 共 2,094 行 · 第 1/5 页

C
2,094
字号
                }            }            if (len && p->r->connection->aborted) {                /* Fail if there is something left to send and                 * the connection was aborted by the client                 */                return JK_FALSE;            }        }        return JK_TRUE;    }    return JK_FALSE;}static void JK_METHOD ws_add_log_items(jk_ws_service_t *s,                                       const char *const *log_names,                                       const char *const *log_values,                                       unsigned num_of_log_items){    unsigned h;    apache_private_data_t *p = s->ws_private;    request_rec *r = p->r;    for (h = 0; h < num_of_log_items; h++) {        if (log_names[h] && log_values[h]) {            ap_table_setn(r->notes, log_names[h], log_values[h]);        }    }}/* ====================================================================== *//* Utility functions                                                      *//* ====================================================================== *//* Log something to JK log file then exit */static void jk_error_exit(const char *file,                          int line,                          int level,                          const server_rec * s,                          ap_pool * p, const char *fmt, ...){    va_list ap;    char *res;    va_start(ap, fmt);    res = ap_pvsprintf(p, fmt, ap);    va_end(ap);    ap_log_error(file, line, level, s, res);    if ( s ) {        ap_log_error(file, line, level, NULL, res);    }    /* Exit process */    exit(1);}/* Return the content length associated with an Apache request structure */static jk_uint64_t get_content_length(request_rec * r){    if (r->clength > 0) {        return (jk_uint64_t)r->clength;    }    else {        char *lenp = (char *)ap_table_get(r->headers_in, "Content-Length");        if (lenp) {            jk_uint64_t rc = 0;            if (sscanf(lenp, "%" JK_UINT64_T_FMT, &rc) > 0 && rc > 0) {                return rc;            }        }    }    return 0;}/* * Set up an instance of a ws_service object for a single request.  This * particular instance will be of the Apache 1.3-specific subclass.  Copies * all of the important request information from the Apache request object * into the jk_ws_service_t object. * * Params * * private_data: The subclass-specific data structure, already initialized * (with a pointer to the Apache request_rec structure, among other things) * * s: The base class object. * * conf: Configuration information * * Called from jk_handler().  See jk_service.h for explanations of what most * of these fields mean. */static int init_ws_service(apache_private_data_t * private_data,                           jk_ws_service_t *s, jk_server_conf_t * conf){    int size;    request_rec *r = private_data->r;    char *ssl_temp = NULL;    s->route = NULL;        /* Used for sticky session routing */    /* Copy in function pointers (which are really methods) */    s->start_response = ws_start_response;    s->read = ws_read;    s->write = ws_write;    s->flush = ws_flush;    s->add_log_items = ws_add_log_items;    /* Clear RECO status */    s->reco_status = RECO_NONE;    s->auth_type = NULL_FOR_EMPTY(r->connection->ap_auth_type);    s->remote_user = NULL_FOR_EMPTY(r->connection->user);    s->protocol = r->protocol;    s->remote_host =        (char *)ap_get_remote_host(r->connection, r->per_dir_config,                                   REMOTE_HOST);    s->remote_host = NULL_FOR_EMPTY(s->remote_host);    if (conf->options & JK_OPT_FWDLOCAL)        s->remote_addr = NULL_FOR_EMPTY(r->connection->local_ip);    else        s->remote_addr = NULL_FOR_EMPTY(r->connection->remote_ip);    if (conf->options & JK_OPT_FLUSHPACKETS)        s->flush_packets = 1;    else        s->flush_packets = 0;    if (conf->options & JK_OPT_FLUSHEADER)        s->flush_header = 1;    else        s->flush_header = 0;    if (conf->options & JK_OPT_DISABLEREUSE)        s->disable_reuse = 1;    else        s->disable_reuse = 0;    /* get server name */    /* s->server_name  = (char *)(r->hostname ? r->hostname : r->server->server_hostname); */    /* XXX : a la jk2 */    s->server_name = (char *)ap_get_server_name(r);    /* get the real port (otherwise redirect failed) */    /* s->server_port     = htons( r->connection->local_addr.sin_port ); */    /* XXX : a la jk2 */    s->server_port = ap_get_server_port(r);    s->server_software = (char *)ap_get_server_version();    s->method = (char *)r->method;    s->content_length = get_content_length(r);    s->is_chunked = r->read_chunked;    s->no_more_chunks = 0;    s->query_string = r->args;    /*     * The 2.2 servlet spec errata says the uri from     * HttpServletRequest.getRequestURI() should remain encoded.     * [http://java.sun.com/products/servlet/errata_042700.html]     *     * We use JkOptions to determine which method to be used     *     * ap_escape_uri is the latest recommanded but require     *               some java decoding (in TC 3.3 rc2)     *     * unparsed_uri is used for strict compliance with spec and     *              old Tomcat (3.2.3 for example)     *     * uri is use for compatibilty with mod_rewrite with old Tomcats     */    switch (conf->options & JK_OPT_FWDURIMASK) {    case JK_OPT_FWDURICOMPATUNPARSED:        s->req_uri = r->unparsed_uri;        if (s->req_uri != NULL) {            char *query_str = strchr(s->req_uri, '?');            if (query_str != NULL) {                *query_str = 0;            }        }        break;    case JK_OPT_FWDURICOMPAT:        s->req_uri = r->uri;        break;    case JK_OPT_FWDURIPROXY:        size = 3 * strlen(r->uri) + 1;        s->req_uri = ap_palloc(r->pool, size);        jk_canonenc(r->uri, s->req_uri, size);        break;    case JK_OPT_FWDURIESCAPED:        s->req_uri = ap_escape_uri(r->pool, r->uri);        break;    default:        return JK_FALSE;    }    s->is_ssl = JK_FALSE;    s->ssl_cert = NULL;    s->ssl_cert_len = 0;    s->ssl_cipher = NULL;       /* required by Servlet 2.3 Api, allready in original ajp13 */    s->ssl_session = NULL;    s->ssl_key_size = -1;       /* required by Servlet 2.3 Api, added in jtc */    if (conf->ssl_enable || conf->envvars_in_use) {        ap_add_common_vars(r);        if (conf->ssl_enable) {            ssl_temp =                (char *)ap_table_get(r->subprocess_env,                                     conf->https_indicator);            if (ssl_temp && !strcasecmp(ssl_temp, "on")) {                s->is_ssl = JK_TRUE;                s->ssl_cert =                    (char *)ap_table_get(r->subprocess_env,                                         conf->certs_indicator);                if (conf->options & JK_OPT_FWDCERTCHAIN) {                    array_header *t = ap_table_elts(r->subprocess_env);                    if (t && t->nelts) {                        int i;                        table_entry *elts = (table_entry *) t->elts;                        array_header *certs = ap_make_array(r->pool, 1,                                                            sizeof(char *));                        *(const char **)ap_push_array(certs) = s->ssl_cert;                        for (i = 0; i < t->nelts; i++) {                            if (!elts[i].key)                                continue;                            if (!strncasecmp(elts[i].key,                                             conf->certchain_prefix,                                             strlen(conf->certchain_prefix)))                            *(const char **)ap_push_array(certs) = elts[i].val;                        }                        s->ssl_cert = ap_array_pstrcat(r->pool, certs, '\0');                     }                }                if (s->ssl_cert) {                    s->ssl_cert_len = strlen(s->ssl_cert);                    if (JK_IS_DEBUG_LEVEL(conf->log)) {                        jk_log(conf->log, JK_LOG_DEBUG,                               "SSL client certificate (%d bytes): %s",                               s->ssl_cert_len, s->ssl_cert);                    }                }                /* Servlet 2.3 API */                s->ssl_cipher =                    (char *)ap_table_get(r->subprocess_env,                                         conf->cipher_indicator);                s->ssl_session =                    (char *)ap_table_get(r->subprocess_env,                                         conf->session_indicator);                if (conf->options & JK_OPT_FWDKEYSIZE) {                    /* Servlet 2.3 API */                    ssl_temp =                        (char *)ap_table_get(r->subprocess_env,                                             conf->key_size_indicator);                    if (ssl_temp)                        s->ssl_key_size = atoi(ssl_temp);                }            }        }        if (conf->envvars_in_use) {            const array_header *t = conf->envvar_items;            if (t && t->nelts) {                int i;                int j = 0;                envvar_item *elts = (envvar_item *) t->elts;                s->attributes_names =                    ap_palloc(r->pool, sizeof(char *) * t->nelts);                s->attributes_values =                    ap_palloc(r->pool, sizeof(char *) * t->nelts);                for (i = 0; i < t->nelts; i++) {                    s->attributes_names[i - j] = elts[i].name;                    s->attributes_values[i - j] =                        (char *)ap_table_get(r->subprocess_env, elts[i].name);                    if (!s->attributes_values[i - j]) {                        if (elts[i].has_default) {                            s->attributes_values[i - j] = elts[i].value;                        }                        else {                            s->attributes_values[i - j] = "";                            s->attributes_names[i - j] = "";                            j++;                        }                    }                }                s->num_attributes = t->nelts - j;            }        }    }    s->headers_names = NULL;    s->headers_values = NULL;    s->num_headers = 0;    if (r->headers_in && ap_table_elts(r->headers_in)) {        int need_content_length_header = (!s->is_chunked                                          && s->content_length ==                                          0) ? JK_TRUE : JK_FALSE;        array_header *t = ap_table_elts(r->headers_in);        if (t && t->nelts) {            int i;            table_entry *elts = (table_entry *) t->elts;            s->num_headers = t->nelts;            /* allocate an extra header slot in case we need to add a content-length header */            s->headers_names =                ap_palloc(r->pool, sizeof(char *) * (t->nelts + 1));            s->headers_values =                ap_palloc(r->pool, sizeof(char *) * (t->nelts + 1));            if (!s->headers_names || !s->headers_values)                return JK_FALSE;            for (i = 0; i < t->nelts; i++) {                char *hname = ap_pstrdup(r->pool, elts[i].key);                s->headers_values[i] = ap_pstrdup(r->pool, elts[i].val);                s->headers_names[i] = hname;                if (need_content_length_header &&                    !strcasecmp(s->headers_names[i], "content-length")) {                    need_content_length_header = JK_FALSE;                }            }            /* Add a content-length = 0 header if needed.             * Ajp13 assumes an absent content-length header means an unknown,             * but non-zero length body.             */            if (need_content_length_header) {                s->headers_names[s->num_headers] = "content-length";                s->headers_values[s->num_headers] = "0";                s->num_headers++;            }        }        /* Add a content-length = 0 header if needed. */        else if (need_content_length_header) {            s->headers_names = ap_palloc(r->pool, sizeof(char *));            s->headers_values = ap_palloc(r->pool, sizeof(char *));            if (!s->headers_names || !s->headers_values)                return JK_FALSE;            s->headers_names[0] = "content-length";            s->headers_values[0] = "0";            s->num_headers++;        }    }    s->uw_map = conf->uw_map;    /* Dump all connection param so we can trace what's going to     * the remote tomcat     */    if (JK_IS_DEBUG_LEVEL(conf->log)) {        jk_log(conf->log, JK_LOG_DEBUG,               "Service protocol=%s method=%s host=%s addr=%s name=%s port=%d auth=%s user=%s laddr=%s raddr=%s uri=%s",               STRNULL_FOR_NULL(s->protocol),               STRNULL_FOR_NULL(s->method),               STRNULL_FOR_NULL(s->remote_host),               STRNULL_FOR_NULL(s->remote_addr),               STRNULL_FOR_NULL(s->server_name),               s->server_port,               STRNULL_FOR_NULL(s->auth_type),               STRNULL_FOR_NULL(s->remote_user),               STRNULL_FOR_NULL(r->connection->local_ip),               STRNULL_FOR_NULL(r->connection->remote_ip),               STRNULL_FOR_NULL(s->req_uri));    }    return JK_TRUE;}/* * The JK module command processors * * The below are all installed so that Apache calls them while it is * processing its config files.  This allows configuration info to be * copied into a jk_server_conf_t object, which is then used for request * filtering/processing. * * See jk_cmds definition below for explanations of these options. *//* * JkMountCopy directive handling * * JkMountCopy On/Off */static const char *jk_set_mountcopy(cmd_parms * cmd, void *dummy, int flag){    server_rec *s = cmd->server;    jk_server_conf_t *conf =        (jk_server_conf_t *) ap_get_module_config(s->module_config,                                                  &jk_module);    /* Set up our value */    conf->mountcopy = flag ? JK_TRUE : JK_FALSE;    return NULL;}/* * JkMount directive handling * * JkMount URI(context) worker */static const char *jk_mount_context(cmd_parms * cmd,                                    void *dummy,                                    char *context,

⌨️ 快捷键说明

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