jk_ajp_common.c

来自「Tomcat 4.1与WebServer集成组件的源代码包.」· C语言 代码 · 共 1,483 行 · 第 1/4 页

C
1,483
字号
    if (s->jvm_route) {        if (jk_b_append_byte(msg, SC_A_JVM_ROUTE) ||            jk_b_append_string(msg, s->jvm_route)) {            jk_log(l, JK_LOG_ERROR, "Error ajp_marshal_into_msgb - Error appending the jvm route\n");            return JK_FALSE;        }    }    if (s->ssl_cert_len) {        if (jk_b_append_byte(msg, SC_A_SSL_CERT) ||            jk_b_append_string(msg, s->ssl_cert)) {            jk_log(l, JK_LOG_ERROR, "Error ajp_marshal_into_msgb - Error appending the SSL certificates\n");            return JK_FALSE;        }    }    if (s->ssl_cipher) {        if (jk_b_append_byte(msg, SC_A_SSL_CIPHER) ||            jk_b_append_string(msg, s->ssl_cipher)) {            jk_log(l, JK_LOG_ERROR, "Error ajp_marshal_into_msgb - Error appending the SSL ciphers\n");            return JK_FALSE;        }    }    if (s->ssl_session) {        if (jk_b_append_byte(msg, SC_A_SSL_SESSION) ||            jk_b_append_string(msg, s->ssl_session)) {            jk_log(l, JK_LOG_ERROR, "Error ajp_marshal_into_msgb - Error appending the SSL session\n");            return JK_FALSE;        }    }    /*     * ssl_key_size is required by Servlet 2.3 API     * added support only in ajp14 mode     * JFC removed: ae->proto == AJP14_PROTO     */    if (s->ssl_key_size != -1) {        if (jk_b_append_byte(msg, SC_A_SSL_KEY_SIZE) ||            jk_b_append_int(msg, (unsigned short) s->ssl_key_size)) {            jk_log(l, JK_LOG_ERROR, "Error ajp_marshal_into_msgb - Error appending the SSL key size\n");            return JK_FALSE;        }    }    if (s->num_attributes > 0) {        for (i = 0 ; i < s->num_attributes ; i++) {            if (jk_b_append_byte(msg, SC_A_REQ_ATTRIBUTE)       ||                jk_b_append_string(msg, s->attributes_names[i]) ||                jk_b_append_string(msg, s->attributes_values[i])) {                jk_log(l, JK_LOG_ERROR, "Error ajp_marshal_into_msgb - Error appending attribute %s=%s\n",                      s->attributes_names[i], s->attributes_values[i]);                return JK_FALSE;            }        }    }    if (jk_b_append_byte(msg, SC_A_ARE_DONE)) {        jk_log(l, JK_LOG_ERROR, "Error ajp_marshal_into_msgb - Error appending the message end\n");        return JK_FALSE;    }        jk_log(l, JK_LOG_DEBUG, "ajp_marshal_into_msgb - Done\n");    return JK_TRUE;}/*AJPV13_RESPONSE/AJPV14_RESPONSE:=    response_prefix (2)    status          (short)    status_msg      (short)    num_headers     (short)    num_headers*(res_header_name header_value)    *body_chunk    terminator      boolean <! -- recycle connection or not  -->req_header_name :=     sc_req_header_name | (string)res_header_name :=     sc_res_header_name | (string)header_value :=    (string)body_chunk :=    length  (short)    body    length*(var binary) */static int ajp_unmarshal_response(jk_msg_buf_t   *msg,                                  jk_res_data_t  *d,                                  ajp_endpoint_t *ae,                                  jk_logger_t    *l){    jk_pool_t * p = &ae->pool;    d->status = jk_b_get_int(msg);    if (!d->status) {        jk_log(l, JK_LOG_ERROR, "Error ajp_unmarshal_response - Null status\n");        return JK_FALSE;    }    d->msg = (char *)jk_b_get_string(msg);    if (d->msg) {#if defined(AS400) || defined(_OSD_POSIX)        jk_xlate_from_ascii(d->msg, strlen(d->msg));#endif    }    jk_log(l, JK_LOG_DEBUG, "ajp_unmarshal_response: status = %d\n", d->status);    d->num_headers = jk_b_get_int(msg);    d->header_names = d->header_values = NULL;    jk_log(l, JK_LOG_DEBUG, "ajp_unmarshal_response: Number of headers is = %d\n", d->num_headers);    if (d->num_headers) {        d->header_names = jk_pool_alloc(p, sizeof(char *) * d->num_headers);        d->header_values = jk_pool_alloc(p, sizeof(char *) * d->num_headers);        if (d->header_names && d->header_values) {            unsigned i;            for(i = 0 ; i < d->num_headers ; i++) {                unsigned short name = jk_b_pget_int(msg, jk_b_get_pos(msg)) ;                                if ((name & 0XFF00) == 0XA000) {                    jk_b_get_int(msg);                    name = name & 0X00FF;                    if (name <= SC_RES_HEADERS_NUM) {                        d->header_names[i] = (char *)long_res_header_for_sc(name);                    } else {                        jk_log(l, JK_LOG_ERROR, "Error ajp_unmarshal_response - No such sc (%d)\n", name);                        return JK_FALSE;                    }                } else {                    d->header_names[i] = (char *)jk_b_get_string(msg);                    if (!d->header_names[i]) {                        jk_log(l, JK_LOG_ERROR, "Error ajp_unmarshal_response - Null header name\n");                        return JK_FALSE;                    }#if defined(AS400) || defined(_OSD_POSIX)                    jk_xlate_from_ascii(d->header_names[i],                                 strlen(d->header_names[i]));#endif                }                d->header_values[i] = (char *)jk_b_get_string(msg);                if (!d->header_values[i]) {                    jk_log(l, JK_LOG_ERROR, "Error ajp_unmarshal_response - Null header value\n");                    return JK_FALSE;                }#if defined(AS400) || defined(_OSD_POSIX)                jk_xlate_from_ascii(d->header_values[i],                             strlen(d->header_values[i]));#endif                jk_log(l, JK_LOG_DEBUG, "ajp_unmarshal_response: Header[%d] [%s] = [%s]\n",                        i,                       d->header_names[i],                       d->header_values[i]);            }        }    }    return JK_TRUE;}/* * Reset the endpoint (clean buf) */static void ajp_reset_endpoint(ajp_endpoint_t *ae){    ae->reuse = JK_FALSE;    jk_reset_pool(&(ae->pool));}/* * Close the endpoint (clean buf and close socket) */void ajp_close_endpoint(ajp_endpoint_t *ae,                        jk_logger_t    *l){    jk_log(l, JK_LOG_DEBUG, "In jk_endpoint_t::ajp_close_endpoint\n");    ajp_reset_endpoint(ae);    jk_close_pool(&(ae->pool));    if (ae->sd > 0) {         jk_close_socket(ae->sd);        jk_log(l, JK_LOG_DEBUG, "In jk_endpoint_t::ajp_close_endpoint, closed sd = %d\n", ae->sd);        ae->sd = -1; /* just to avoid twice close */    }    free(ae);}/* * Try to reuse a previous connection */static void ajp_reuse_connection(ajp_endpoint_t *ae,                                 jk_logger_t    *l){    ajp_worker_t *aw = ae->worker;    if (aw->ep_cache_sz) {        int rc;        JK_ENTER_CS(&aw->cs, rc);        if (rc) {            unsigned i;            for (i = 0 ; i < aw->ep_cache_sz ; i++) {                if (aw->ep_cache[i]) {                    ae->sd = aw->ep_cache[i]->sd;                    aw->ep_cache[i]->sd = -1;                    ajp_close_endpoint(aw->ep_cache[i], l);                    aw->ep_cache[i] = NULL;                    break;                 }            }            JK_LEAVE_CS(&aw->cs, rc);        }    }}int ajp_connect_to_endpoint(ajp_endpoint_t *ae,                            jk_logger_t    *l){    unsigned attempt;    for(attempt = 0 ; attempt < ae->worker->connect_retry_attempts ; attempt++) {        ae->sd = jk_open_socket(&ae->worker->worker_inet_addr, JK_TRUE, ae->worker->keepalive, l);        if(ae->sd >= 0) {            jk_log(l, JK_LOG_DEBUG, "In jk_endpoint_t::ajp_connect_to_endpoint, connected sd = %d\n", ae->sd);             /* set last_access */             ae->last_access = time(NULL);            /* Check if we must execute a logon after the physical connect */            if (ae->worker->logon != NULL)                return (ae->worker->logon(ae, l));            return JK_TRUE;        }    }    jk_log(l, JK_LOG_INFO,           "Error connecting to tomcat. Tomcat is probably not started or is listenning on the wrong port. Failed errno = %d\n",           errno);    return JK_FALSE;}/* * Send a message to endpoint, using corresponding PROTO HEADER */int ajp_connection_tcp_send_message(ajp_endpoint_t *ae,                                    jk_msg_buf_t   *msg,                                    jk_logger_t    *l){    if (ae->proto == AJP13_PROTO) {        jk_b_end(msg, AJP13_WS_HEADER);        jk_dump_buff(l, JK_LOG_DEBUG, "sending to ajp13", msg);    }    else if (ae->proto == AJP14_PROTO) {        jk_b_end(msg, AJP14_WS_HEADER);        jk_dump_buff(l, JK_LOG_DEBUG, "sending to ajp14", msg);    }    else {        jk_log(l, JK_LOG_ERROR, "In jk_endpoint_t::ajp_connection_tcp_send_message, unknown protocol %d, supported are AJP13/AJP14\n", ae->proto);        return JK_FALSE;    }    if(0 > jk_tcp_socket_sendfull(ae->sd, jk_b_get_buff(msg), jk_b_get_len(msg))) {        return JK_FALSE;    }    return JK_TRUE;}/* * Receive a message from endpoint, checking PROTO HEADER */int ajp_connection_tcp_get_message(ajp_endpoint_t *ae,                                   jk_msg_buf_t   *msg,                                   jk_logger_t    *l){    unsigned char head[AJP_HEADER_LEN];    int           rc;    int           msglen;    unsigned int  header;    if ((ae->proto != AJP13_PROTO) && (ae->proto != AJP14_PROTO)) {        jk_log(l, JK_LOG_ERROR, "ajp_connection_tcp_get_message: Can't handle unknown protocol %d\n", ae->proto);        return JK_FALSE;    }    rc = jk_tcp_socket_recvfull(ae->sd, head, AJP_HEADER_LEN);    if(rc < 0) {        jk_log(l, JK_LOG_ERROR, "ERROR: can't receive the response message from tomcat, network problems or tomcat is down.\n");        return JK_FALSE;    }    header = ((unsigned int)head[0] << 8) | head[1];      if (ae->proto == AJP13_PROTO) {        if (header != AJP13_SW_HEADER) {            if (header == AJP14_SW_HEADER)                jk_log(l, JK_LOG_ERROR, "ajp_connection_tcp_get_message: Error - received AJP14 reply on an AJP13 connection\n");            else                jk_log(l, JK_LOG_ERROR, "ajp_connection_tcp_get_message: Error - Wrong message format 0x%04x\n", header);            return JK_FALSE;        }    }    else if (ae->proto == AJP14_PROTO) {        if (header != AJP14_SW_HEADER) {            if (header == AJP13_SW_HEADER)                jk_log(l, JK_LOG_ERROR, "ajp_connection_tcp_get_message: Error - received AJP13 reply on an AJP14 connection\n");            else                jk_log(l, JK_LOG_ERROR, "ajp_connection_tcp_get_message: Error - Wrong message format 0x%04x\n", header);            return JK_FALSE;        }    }       msglen  = ((head[2]&0xff)<<8);    msglen += (head[3] & 0xFF);    if(msglen > jk_b_get_size(msg)) {        jk_log(l, JK_LOG_ERROR, "ajp_connection_tcp_get_message: Error - Wrong message size %d %d\n",               msglen, jk_b_get_size(msg));        return JK_FALSE;    }    jk_b_set_len(msg, msglen);    jk_b_set_pos(msg, 0);    rc = jk_tcp_socket_recvfull(ae->sd, jk_b_get_buff(msg), msglen);    if(rc < 0) {        jk_log(l, JK_LOG_ERROR, "ERROR: can't receive the response message from tomcat, network problems or tomcat is down\n");        return JK_FALSE;    }    if (ae->proto == AJP13_PROTO)         jk_dump_buff(l, JK_LOG_DEBUG, "received from ajp13", msg);    else if (ae->proto == AJP14_PROTO)        jk_dump_buff(l, JK_LOG_DEBUG, "received from ajp14", msg);        return JK_TRUE;}/* * Read all the data from the socket. * * Socket API didn't garanty all the data will be kept in a single  * read, so we must loop up to all awaited data are received  */

⌨️ 快捷键说明

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