设为首页 收藏本站
查看: 472|回复: 0

[经验分享] Apache扩展对于webservice指定机器请求

[复制链接]

尚未签到

发表于 2017-1-7 08:51:09 | 显示全部楼层 |阅读模式
  Mod_jk是apache module模块中鼎鼎大名的

static int jk_handler(request_rec * r)
{
const char *worker_name;
jk_server_conf_t *xconf;
jk_request_conf_t *rconf;
int rc, dmt = 1;
int worker_name_extension = JK_FALSE;
/* We do DIR_MAGIC_TYPE here to make sure TC gets all requests, even
* if they are directory requests, in case there are no static files
* visible to Apache and/or DirectoryIndex was not used. This is only
* used when JkOptions has ForwardDirectories set. */
/* Not for me, try next handler */
if (strcmp(r->handler, JK_HANDLER)
&& (dmt = strcmp(r->handler, DIR_MAGIC_TYPE)))
return DECLINED;
xconf = (jk_server_conf_t *) ap_get_module_config(r->server->module_config,
&jk_module);
JK_TRACE_ENTER(xconf->log);
if (apr_table_get(r->subprocess_env, "no-jk")) {
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG,
"Into handler no-jk env var detected for uri=%s, declined",
r->uri);
JK_TRACE_EXIT(xconf->log);
return DECLINED;
}
/* Was the option to forward directories to Tomcat set? */
if (!dmt && !(xconf->options & JK_OPT_FWDDIRS)) {
JK_TRACE_EXIT(xconf->log);
return DECLINED;
}
worker_name = apr_table_get(r->notes, JK_NOTE_WORKER_NAME);
if (worker_name == NULL) {
/* we may be here because of a manual directive ( that overrides
translate and
sets the handler directly ). We still need to know the worker.
*/
worker_name = apr_table_get(r->subprocess_env, xconf->worker_indicator);
if (worker_name) {
/* The JkWorkerIndicator environment variable has
* been used to explicitely set the worker without JkMount.
* This is useful in combination with LocationMatch or mod_rewrite.
*/
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG,
"Retrieved worker (%s) from env %s for %s",
worker_name, xconf->worker_indicator, r->uri);
if (ap_strchr_c(worker_name, ';')) {
rule_extension_t *e = apr_palloc(r->pool, sizeof(rule_extension_t));
char *w = apr_pstrdup(r->pool, worker_name);
worker_name_extension = JK_TRUE;
parse_rule_extensions(w, e, xconf->log);
worker_name = w;
rconf = (jk_request_conf_t *)ap_get_module_config(r->request_config,
&jk_module);
rconf->rule_extensions = e;
}
}
else if (worker_env.num_of_workers == 1) {
/** We have a single worker ( the common case ).
( lb is a bit special, it should count as a single worker but
I'm not sure how ). We also have a manual config directive that
explicitely give control to us. */
worker_name = worker_env.worker_list[0];
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG,
"Single worker (%s) configuration for %s",
worker_name, r->uri);
}
else {
if (!xconf->uw_map) {
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG,
"missing uri map for %s:%s",
xconf->s->server_hostname ? xconf->s->server_hostname : "_default_",
r->uri);
}
else {
rule_extension_t *e;
worker_name = map_uri_to_worker_ext(xconf->uw_map, r->uri,
NULL, &e, NULL, xconf->log);
rconf = (jk_request_conf_t *)ap_get_module_config(r->request_config,
&jk_module);
rconf->rule_extensions = e;
}
if (worker_name == NULL && worker_env.num_of_workers) {
worker_name = worker_env.worker_list[0];
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG,
"Using first worker (%s) from %d workers for %s",
worker_name, worker_env.num_of_workers, r->uri);
}
}
if (worker_name)
apr_table_setn(r->notes, JK_NOTE_WORKER_NAME, worker_name);
}
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG, "Into handler %s worker=%s"
" r->proxyreq=%d",
r->handler, STRNULL_FOR_NULL(worker_name), r->proxyreq);
rconf = (jk_request_conf_t *)ap_get_module_config(r->request_config,
&jk_module);
rconf->jk_handled = JK_TRUE;
/* If this is a proxy request, we'll notify an error */
if (r->proxyreq) {
jk_log(xconf->log, JK_LOG_INFO, "Proxy request for worker=%s"
" is not allowed",
STRNULL_FOR_NULL(worker_name));
JK_TRACE_EXIT(xconf->log);
return HTTP_INTERNAL_SERVER_ERROR;
}
/* Set up r->read_chunked flags for chunked encoding, if present */
if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK)) != APR_SUCCESS) {
JK_TRACE_EXIT(xconf->log);
return rc;
}
if (worker_name) {
jk_worker_t *worker = wc_get_worker_for_name(worker_name, xconf->log);
/* If the remote client has aborted, just ignore the request */
if (r->connection->aborted) {
jk_log(xconf->log, JK_LOG_INFO, "Client connection aborted for"
" worker=%s",
worker_name);
JK_TRACE_EXIT(xconf->log);
return OK;
}
if (worker) {
long micro, seconds;
char *duration = NULL;
apr_time_t rd;
apr_time_t request_begin = 0;
int is_error = HTTP_INTERNAL_SERVER_ERROR;
int rc = JK_FALSE;
apache_private_data_t private_data;
jk_ws_service_t s;
jk_pool_atom_t buf[SMALL_POOL_SIZE];
jk_open_pool(&private_data.p, buf, sizeof(buf));
private_data.read_body_started = JK_FALSE;
private_data.r = r;
if (worker_name_extension == JK_TRUE) {
extension_fix(&private_data.p, worker_name,
rconf->rule_extensions, xconf->log);
}
/* Maintain will be done by watchdog thread */
if (!jk_watchdog_interval)
wc_maintain(xconf->log);
jk_init_ws_service(&s);
s.ws_private = &private_data;
s.pool = &private_data.p;
apr_table_setn(r->notes, JK_NOTE_WORKER_TYPE,
wc_get_name_for_type(worker->type, xconf->log));
request_begin = apr_time_now();
if (init_ws_service(&private_data, &s, xconf)) {
jk_endpoint_t *end = NULL;
/* Use per/thread pool ( or "context" ) to reuse the
endpoint. It's a bit faster, but I don't know
how to deal with load balancing - but it's usefull for JNI
*/
/* worker->get_endpoint might fail if we are out of memory so check */
/* and handle it */
if (worker->get_endpoint(worker, &end, xconf->log)) {
rc = end->service(end, &s, xconf->log,
&is_error);
end->done(&end, xconf->log);
if (s.content_read < s.content_length ||
(s.is_chunked && !s.no_more_chunks)) {
/*
* If the servlet engine didn't consume all of the
* request data, consume and discard all further
* characters left to read from client
*/
char *buff = apr_palloc(r->pool, 2048);
int consumed = 0;
if (buff != NULL) {
int rd;
while ((rd =
ap_get_client_block(r, buff, 2048)) > 0) {
s.content_read += rd;
consumed += rd;
}
}
if (JK_IS_DEBUG_LEVEL(xconf->log)) {
jk_log(xconf->log, JK_LOG_DEBUG,
"Consumed %d bytes of remaining request data for worker=%s",
consumed, STRNULL_FOR_NULL(worker_name));
}
}
}
else {            /* this means we couldn't get an endpoint */
jk_log(xconf->log, JK_LOG_ERROR, "Could not get endpoint"
" for worker=%s",
worker_name);
rc = 0;       /* just to make sure that we know we've failed */
}
}
else {
jk_log(xconf->log, JK_LOG_ERROR, "Could not init service"
" for worker=%s",
worker_name);
jk_close_pool(&private_data.p);
JK_TRACE_EXIT(xconf->log);
return HTTP_INTERNAL_SERVER_ERROR;
}
rd = apr_time_now() - request_begin;
seconds = (long)apr_time_sec(rd);
micro = (long)(rd - apr_time_from_sec(seconds));
duration = apr_psprintf(r->pool, "%.1ld.%.6ld", seconds, micro);
apr_table_setn(r->notes, JK_NOTE_REQUEST_DURATION, duration);
if (s.route && *s.route)
apr_table_setn(r->notes, JK_NOTE_WORKER_ROUTE, s.route);
jk_close_pool(&private_data.p);
if (rc > 0) {
if (s.extension.use_server_error_pages &&
s.http_response_status >= s.extension.use_server_error_pages) {
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG, "Forwarding status=%d"
" for worker=%s",
s.http_response_status, worker_name);
JK_TRACE_EXIT(xconf->log);
return s.http_response_status;
}
/* If tomcat returned no body and the status is not OK,
let apache handle the error code */
if (!r->sent_bodyct && r->status >= HTTP_BAD_REQUEST) {
jk_log(xconf->log, JK_LOG_INFO, "No body with status=%d"
" for worker=%s",
r->status, worker_name);
JK_TRACE_EXIT(xconf->log);
return r->status;
}
if (JK_IS_DEBUG_LEVEL(xconf->log))
jk_log(xconf->log, JK_LOG_DEBUG, "Service finished"
" with status=%d for worker=%s",
r->status, worker_name);
JK_TRACE_EXIT(xconf->log);
return OK;      /* NOT r->status, even if it has changed. */
}
else if (rc == JK_CLIENT_ERROR) {
if (is_error != HTTP_REQUEST_ENTITY_TOO_LARGE)
r->connection->aborted = 1;
jk_log(xconf->log, JK_LOG_INFO, "Aborting connection"
" for worker=%s",
worker_name);
JK_TRACE_EXIT(xconf->log);
return is_error;
}
else {
jk_log(xconf->log, JK_LOG_INFO, "Service error=%d"
" for worker=%s",
rc, worker_name);
JK_TRACE_EXIT(xconf->log);
return is_error;
}
}
else {
jk_log(xconf->log, JK_LOG_INFO, "Could not find a worker"
" for worker name=%s",
worker_name);
JK_TRACE_EXIT(xconf->log);
return HTTP_INTERNAL_SERVER_ERROR;
}
}
rconf->jk_handled = JK_FALSE;
JK_TRACE_EXIT(xconf->log);
return DECLINED;
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-324906-1-1.html 上篇帖子: apache mina 自定义编码解码 下篇帖子: Apache服务挂起Asynchronous AcceptEx failed.
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表