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

[经验分享] Tomcat对HTTP请求的处理(二)

[复制链接]

尚未签到

发表于 2017-12-26 09:48:28 | 显示全部楼层 |阅读模式
//代码清单13  /**
  * Parse additional request parameters.
  */
  protected boolean postParseRequest(org.apache.coyote.Request req,
  Request request,
  org.apache.coyote.Response res,
  Response response)
  throws Exception {
  // XXX the processor may have set a correct scheme and port prior to this point,
  // in ajp13 protocols dont make sense to get the port from the connector...
  // otherwise, use connector configuration
  if (! req.scheme().isNull()) {
  // use processor specified scheme to determine secure state
  request.setSecure(req.scheme().equals("https"));
  } else {
  // use connector scheme and secure configuration, (defaults to
  // "http" and false respectively)
  req.scheme().setString(connector.getScheme());
  request.setSecure(connector.getSecure());
  }
  // FIXME: the code below doesnt belongs to here,
  // this is only have sense
  // in Http11, not in ajp13..
  // At this point the Host header has been processed.
  // Override if the proxyPort/proxyHost are set
  String proxyName = connector.getProxyName();
  int proxyPort = connector.getProxyPort();
  if (proxyPort != 0) {
  req.setServerPort(proxyPort);
  }
  if (proxyName != null) {
  req.serverName().setString(proxyName);
  }
  // Copy the raw URI to the decodedURI
  MessageBytes decodedURI = req.decodedURI();
  decodedURI.duplicate(req.requestURI());
  // Parse the path parameters. This will:
  //   - strip out the path parameters
  //   - convert the decodedURI to bytes
  //解析请求中的参数
  parsePathParameters(req, request);
  // URI decoding
  // %xx decoding of the URL
  try {
  //URI decoding的转换
  req.getURLDecoder().convert(decodedURI, false);
  } catch (IOException ioe) {
  res.setStatus(400);
  res.setMessage("Invalid URI: " + ioe.getMessage());
  connector.getService().getContainer().logAccess(
  request, response, 0, true);
  return false;
  }
  // Normalization
  //处理请求中的特殊字符 比如// ./ ../ \ 等
  if (!normalize(req.decodedURI())) {
  res.setStatus(400);
  res.setMessage("Invalid URI");
  connector.getService().getContainer().logAccess(
  request, response, 0, true);
  return false;
  }
  // Character decoding
  //将字节转换为字符
  convertURI(decodedURI, request);
  // Check that the URI is still normalized
  // 判断URI中是否包含特殊字符
  if (!checkNormalize(req.decodedURI())) {
  res.setStatus(400);
  res.setMessage("Invalid URI character encoding");
  connector.getService().getContainer().logAccess(
  request, response, 0, true);
  return false;
  }
  // Request mapping.
  MessageBytes serverName;
  if (connector.getUseIPVHosts()) {
  serverName = req.localName();
  if (serverName.isNull()) {
  // well, they did ask for it
  res.action(ActionCode.REQ_LOCAL_NAME_ATTRIBUTE, null);
  }
  } else {
  serverName = req.serverName();
  }
  if (request.isAsyncStarted()) {
  //TODO SERVLET3 - async
  //reset mapping data, should prolly be done elsewhere
  request.getMappingData().recycle();
  }
  // Version for the second mapping loop and
  // Context that we expect to get for that version
  String version = null;
  Context versionContext = null;
  boolean mapRequired = true;
  while (mapRequired) {
  // This will map the the latest version by default
  //匹配请求对应的Host Context Wrapper
  // 1111111111111
  connector.getMapper().map(serverName, decodedURI, version,
  request.getMappingData());
  request.setContext((Context) request.getMappingData().context);
  request.setWrapper((Wrapper) request.getMappingData().wrapper);
  // If there is no context at this point, it is likely no ROOT context
  // has been deployed
  if (request.getContext() == null) {
  res.setStatus(404);
  res.setMessage("Not found");
  // No context, so use host
  Host host = request.getHost();
  // Make sure there is a host (might not be during shutdown)
  if (host != null) {
  host.logAccess(request, response, 0, true);
  }
  return false;
  }

  // Now we have the context, we can parse the session>  // (if any). Need to do this before we redirect in case we need to

  // include the session>  String sessionID;
  if (request.getServletContext().getEffectiveSessionTrackingModes()
  .contains(SessionTrackingMode.URL)) {

  // Get the session>  sessionID = request.getPathParameter(
  SessionConfig.getSessionUriParamName(
  request.getContext()));
  if (sessionID != null) {
  request.setRequestedSessionId(sessionID);
  request.setRequestedSessionURL(true);
  }
  }

  // Look for session>  //从cookie中解析sessionId
  parseSessionCookiesId(req, request);
  //从ssl中解析sessionId
  parseSessionSslId(request);
  sessionID = request.getRequestedSessionId();
  mapRequired = false;
  if (version != null && request.getContext() == versionContext) {
  // We got the version that we asked for. That is it.
  } else {
  version = null;
  versionContext = null;
  Object[] contexts = request.getMappingData().contexts;
  // Single contextVersion means no need to remap

  // No session>  if (contexts != null && sessionID != null) {
  // Find the context associated with the session
  for (int i = (contexts.length); i > 0; i--) {
  Context ctxt = (Context) contexts[i - 1];
  if (ctxt.getManager().findSession(sessionID) != null) {
  // We found a context. Is it the one that has
  // already been mapped?
  if (!ctxt.equals(request.getMappingData().context)) {
  // Set version so second time through mapping
  // the correct context is found
  version = ctxt.getWebappVersion();
  versionContext = ctxt;
  // Reset mapping
  request.getMappingData().recycle();
  mapRequired = true;
  // Recycle session info in case the correct
  // context is configured with different settings
  request.recycleSessionInfo();
  }
  break;
  }
  }
  }
  }
  if (!mapRequired && request.getContext().getPaused()) {
  // Found a matching context but it is paused. Mapping data will
  // be wrong since some Wrappers may not be registered at this
  // point.
  try {
  Thread.sleep(1000);
  } catch (InterruptedException e) {
  // Should never happen
  }
  // Reset mapping
  request.getMappingData().recycle();
  mapRequired = true;
  }
  }
  // Possible redirect
  MessageBytes redirectPathMB = request.getMappingData().redirectPath;
  if (!redirectPathMB.isNull()) {
  String redirectPath = urlEncoder.encode(redirectPathMB.toString());
  String query = request.getQueryString();
  if (request.isRequestedSessionIdFromURL()) {
  // This is not optimal, but as this is not very common, it
  // shouldn't matter
  redirectPath = redirectPath + ";" +
  SessionConfig.getSessionUriParamName(
  request.getContext()) +
  "=" + request.getRequestedSessionId();
  }
  if (query != null) {
  // This is not optimal, but as this is not very common, it
  // shouldn't matter
  redirectPath = redirectPath + "?" + query;
  }
  response.sendRedirect(redirectPath);
  request.getContext().logAccess(request, response, 0, true);
  return false;
  }
  // Filter trace method
  if (!connector.getAllowTrace()
  && req.method().equalsIgnoreCase("TRACE")) {
  Wrapper wrapper = request.getWrapper();
  String header = null;
  if (wrapper != null) {
  String[] methods = wrapper.getServletMethods();
  if (methods != null) {
  for (int i=0; i<methods.length; i++) {
  if (&quot;TRACE&quot;.equals(methods)) {
  continue;
  }
  if (header == null) {
  header = methods;
  } else {
  header += &quot;, &quot; + methods;
  }
  }
  }
  }
  res.setStatus(405);
  res.addHeader(&quot;Allow&quot;, header);
  res.setMessage(&quot;TRACE method is not allowed&quot;);
  request.getContext().logAccess(request, response, 0, true);
  return false;
  }
  doConnectorAuthenticationAuthorization(req, request);
  return true;
  }

运维网声明 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-428146-1-1.html 上篇帖子: springmvc ajax tomcat简单配置实现跨域访问 下篇帖子: linux下启动tomcat服务的命令是什么
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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