📄 jk_ajp_common.c
字号:
*is_error = JK_REQUEST_TOO_LARGE; jk_log(l, JK_LOG_INFO, "Creating AJP message failed, " "without recovery"); JK_TRACE_EXIT(l); return JK_CLIENT_ERROR; } if (JK_IS_DEBUG_LEVEL(l)) jk_log(l, JK_LOG_DEBUG, "processing with %d retries", s->retries); /* * JK_RETRIES could be replaced by the number of workers in * a load-balancing configuration */ for (i = 0; i < s->retries; i++) { /* * We're using reqmsg which hold initial request * if Tomcat is stopped or restarted, we will pass reqmsg * to next valid tomcat. */ err = ajp_send_request(e, s, l, p, op); if (err == JK_TRUE) { /* If we have the no recoverable error, it's probably because * the sender (browser) stopped sending data before the end * (certainly in a big post) */ if (!op->recoverable) { *is_error = JK_HTTP_SERVER_ERROR; jk_log(l, JK_LOG_ERROR, "sending request to tomcat failed " "without recovery in send loop %d", i); JK_TRACE_EXIT(l); return JK_FALSE; } /* Up to there we can recover */ err = ajp_get_reply(e, s, l, p, op); if (err == JK_TRUE) { *is_error = JK_HTTP_OK; /* Done with the request */ JK_TRACE_EXIT(l); return JK_TRUE; } if (err != JK_CLIENT_ERROR) { /* if we can't get reply, check if no recover flag was set * if is_recoverable_error is cleared, we have started * receiving upload data and we must consider that * operation is no more recoverable */ if (!op->recoverable) { *is_error = JK_HTTP_BAD_GATEWAY; jk_log(l, JK_LOG_ERROR, "receiving reply from tomcat failed " "without recovery in send loop %d", i); JK_TRACE_EXIT(l); return JK_FALSE; } jk_log(l, JK_LOG_INFO, "Receiving from tomcat failed, " "recoverable operation attempt=%d", i); /* Check for custom retries */ if (i >= JK_RETRIES) { jk_sleep_def(); } } else { *is_error = JK_HTTP_BAD_REQUEST; jk_log(l, JK_LOG_INFO, "Receiving from tomcat failed, " "because of client error " "without recovery in send loop %d", i); JK_TRACE_EXIT(l); return JK_CLIENT_ERROR; } } if (err == JK_CLIENT_ERROR) { *is_error = JK_HTTP_BAD_REQUEST; jk_log(l, JK_LOG_INFO, "Sending request to tomcat failed, " "because of client error " "without recovery in send loop %d", i); JK_TRACE_EXIT(l); return JK_CLIENT_ERROR; } else { jk_log(l, JK_LOG_INFO, "Sending request to tomcat failed, " "recoverable operation attempt=%d", i + 1); } /* Get another connection from the pool and try again */ ajp_next_connection(p, l); } *is_error = JK_HTTP_SERVER_BUSY; /* Log the error only once per failed request. */ jk_log(l, JK_LOG_ERROR, "Error connecting to tomcat. Tomcat is probably not started " "or is listening on the wrong port. worker=%s failed", p->worker->name); } else { jk_log(l, JK_LOG_ERROR, "end of service with error"); } JK_TRACE_EXIT(l); return JK_FALSE;}/* * Validate the worker (ajp13/ajp14) */int ajp_validate(jk_worker_t *pThis, jk_map_t *props, jk_worker_env_t *we, jk_logger_t *l, int proto){ int port; const char *host; JK_TRACE_ENTER(l); if (proto == AJP13_PROTO) { port = AJP13_DEF_PORT; host = AJP13_DEF_HOST; } else if (proto == AJP14_PROTO) { port = AJP14_DEF_PORT; host = AJP14_DEF_HOST; } else { jk_log(l, JK_LOG_ERROR, "unknown protocol %d", proto); JK_TRACE_EXIT(l); return JK_FALSE; } if (pThis && pThis->worker_private) { ajp_worker_t *p = pThis->worker_private; p->port = jk_get_worker_port(props, p->name, port); p->host = jk_get_worker_host(props, p->name, host); if (JK_IS_DEBUG_LEVEL(l)) jk_log(l, JK_LOG_DEBUG, "worker %s contact is '%s:%d'", p->name, p->host, p->port); if (p->port > 1024) { if (jk_resolve(p->host, p->port, &p->worker_inet_addr)) { JK_TRACE_EXIT(l); return JK_TRUE; } jk_log(l, JK_LOG_ERROR, "can't resolve tomcat address %s", host); } jk_log(l, JK_LOG_ERROR, "invalid host and port %s %d", ((p->host == NULL) ? "NULL" : p->host), p->port); } else { JK_LOG_NULL_PARAMS(l); } JK_TRACE_EXIT(l); return JK_FALSE;}static int ajp_create_endpoint_cache(ajp_worker_t *p, int proto, jk_logger_t *l){ unsigned int i; time_t now = time(NULL); JK_TRACE_ENTER(l); p->ep_cache = (ajp_endpoint_t **)calloc(1, sizeof(ajp_endpoint_t *) * p->ep_cache_sz); if (!p->ep_cache) { JK_TRACE_EXIT(l); return JK_FALSE; } if (JK_IS_DEBUG_LEVEL(l)) jk_log(l, JK_LOG_DEBUG, "setting connection cache size to %d", p->ep_cache_sz); for (i = 0; i < p->ep_cache_sz; i++) { p->ep_cache[i] = (ajp_endpoint_t *)calloc(1, sizeof(ajp_endpoint_t)); if (!p->ep_cache[i]) { jk_log(l, JK_LOG_ERROR, "creating endpont cache slot %d errno=%d", i, errno); JK_TRACE_EXIT(l); return JK_FALSE; } p->ep_cache[i]->sd = -1; p->ep_cache[i]->reuse = JK_FALSE; p->ep_cache[i]->last_access = now; jk_open_pool(&(p->ep_cache[i]->pool), p->ep_cache[i]->buf, sizeof(p->ep_cache[i]->buf)); p->ep_cache[i]->worker = p; p->ep_cache[i]->endpoint.endpoint_private = p->ep_cache[i]; p->ep_cache[i]->proto = proto; p->ep_cache[i]->endpoint.service = ajp_service; p->ep_cache[i]->endpoint.done = ajp_done; } JK_TRACE_EXIT(l); return JK_TRUE;}int ajp_init(jk_worker_t *pThis, jk_map_t *props, jk_worker_env_t *we, jk_logger_t *l, int proto){ int rc = JK_FALSE; int cache; /* * start the connection cache */ JK_TRACE_ENTER(l); cache = jk_get_worker_def_cache_size(proto); if (pThis && pThis->worker_private) { ajp_worker_t *p = pThis->worker_private; p->ep_cache_sz = jk_get_worker_cache_size(props, p->name, cache); p->socket_timeout = jk_get_worker_socket_timeout(props, p->name, AJP_DEF_SOCKET_TIMEOUT); p->socket_buf = jk_get_worker_socket_buffer(props, p->name, 8192); p->keepalive = jk_get_worker_socket_keepalive(props, p->name, JK_FALSE); jk_log(l, JK_LOG_DEBUG, "setting socket keepalive to %d", p->keepalive); p->recycle_timeout = jk_get_worker_recycle_timeout(props, p->name, AJP13_DEF_TIMEOUT); p->cache_timeout = jk_get_worker_cache_timeout(props, p->name, AJP_DEF_CACHE_TIMEOUT); p->connect_timeout = jk_get_worker_connect_timeout(props, p->name, AJP_DEF_CONNECT_TIMEOUT); p->reply_timeout = jk_get_worker_reply_timeout(props, p->name, AJP_DEF_REPLY_TIMEOUT); p->prepost_timeout = jk_get_worker_prepost_timeout(props, p->name, AJP_DEF_PREPOST_TIMEOUT); p->recovery_opts = jk_get_worker_recovery_opts(props, p->name, AJP_DEF_RECOVERY_OPTS); pThis->retries = jk_get_worker_retries(props, p->name, JK_RETRIES); if (pThis->retries < 1) { jk_log(l, JK_LOG_INFO, "number of retries must be grater then 1. Setting to default=%d", JK_RETRIES); pThis->retries = JK_RETRIES; } if (JK_IS_DEBUG_LEVEL(l)) { jk_log(l, JK_LOG_DEBUG, "setting socket timeout to %d", p->socket_timeout); jk_log(l, JK_LOG_DEBUG, "setting socket buffer size to %d", p->socket_buf); jk_log(l, JK_LOG_DEBUG, "setting connection recycle timeout to %d", p->recycle_timeout); jk_log(l, JK_LOG_DEBUG, "setting cache timeout to %d", p->cache_timeout); jk_log(l, JK_LOG_DEBUG, "setting connect timeout to %d", p->connect_timeout); jk_log(l, JK_LOG_DEBUG, "setting reply timeout to %d", p->reply_timeout); jk_log(l, JK_LOG_DEBUG, "setting prepost timeout to %d", p->prepost_timeout); jk_log(l, JK_LOG_DEBUG, "setting recovery opts to %d", p->recovery_opts); jk_log(l, JK_LOG_DEBUG, "setting number of retries to %d", pThis->retries); } /* * Need to initialize secret here since we could return from inside * of the following loop */ p->secret = jk_get_worker_secret(props, p->name); p->ep_mincache_sz = 1; /* Initialize cache slots */ JK_INIT_CS(&(p->cs), rc); if (!rc) { jk_log(l, JK_LOG_ERROR, "creating thread lock errno=%d", errno); JK_TRACE_EXIT(l); return JK_FALSE; } if (!ajp_create_endpoint_cache(p, proto, l)) { jk_log(l, JK_LOG_ERROR, "allocating ep_cache of size %d", p->ep_cache_sz); JK_TRACE_EXIT(l); return JK_FALSE; } rc = JK_TRUE; } else { JK_LOG_NULL_PARAMS(l); } JK_TRACE_EXIT(l); return rc;}int ajp_destroy(jk_worker_t **pThis, jk_logger_t *l, int proto){ JK_TRACE_ENTER(l); if (pThis && *pThis && (*pThis)->worker_private) { unsigned int i; ajp_worker_t *aw = (*pThis)->worker_private; if (JK_IS_DEBUG_LEVEL(l)) jk_log(l, JK_LOG_DEBUG, "up to %d endpoints to close", aw->ep_cache_sz); for (i = 0; i < aw->ep_cache_sz; i++) { if (aw->ep_cache[i]) ajp_close_endpoint(aw->ep_cache[i], l); } free(aw->ep_cache); JK_DELETE_CS(&(aw->cs), i); if (aw->login) { /* take care of removing previously allocated data */ if (aw->login->servlet_engine_name) free(aw->login->servlet_engine_name); free(aw->login); aw->login = NULL; } free(aw); JK_TRACE_EXIT(l); return JK_TRUE; } JK_LOG_NULL_PARAMS(l); JK_TRACE_EXIT(l); return JK_FALSE;}int JK_METHOD ajp_done(jk_endpoint_t **e, jk_logger_t *l){ JK_TRACE_ENTER(l); if (e && *e && (*e)->endpoint_private) { ajp_endpoint_t *p = (*e)->endpoint_private; int rc; ajp_worker_t *w = p->worker; JK_ENTER_CS(&w->cs, rc); if (rc) { int i, sock = -1; if (p->sd > 0 && !p->reuse) { sock = p->sd; p->sd = -1; } for(i = w->ep_cache_sz - 1; i >= 0; i--) { if (w->ep_cache[i] == NULL) { w->ep_cache[i] = p; break; } } ajp_reset_endpoint(p, l); *e = NULL; /* set last_access only if needed */ if (w->cache_timeout > 0 || w->recycle_timeout > 0) p->last_access = time(NULL); JK_LEAVE_CS(&w->cs, rc); if (sock >= 0) jk_shutdown_socket(sock); if (i >= 0) { if (JK_IS_DEBUG_LEVEL(l))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -