Esempio n. 1
0
    @Override
    public boolean init() {
      Logger.trace("init: begin");
      Request.current.set(request);
      Response.current.set(response);
      try {
        super.init();
        if (Play.mode == Play.Mode.PROD && staticPathsCache.containsKey(request.path)) {
          RenderStatic rs = null;
          synchronized (staticPathsCache) {
            rs = staticPathsCache.get(request.path);
          }
          serveStatic(rs, ctx, request, response, nettyRequest, event);
          Logger.trace("init: end false");
          return false;
        }
        Router.routeOnlyStatic(request);
      } catch (NotFound nf) {
        serve404(nf, ctx, request, nettyRequest);
        Logger.trace("init: end false");
        return false;
      } catch (RenderStatic rs) {
        if (Play.mode == Play.Mode.PROD) {
          synchronized (staticPathsCache) {
            staticPathsCache.put(request.path, rs);
          }
        }
        serveStatic(rs, ctx, request, response, nettyRequest, this.event);
        Logger.trace("init: end false");
        return false;
      }

      Logger.trace("init: end true");
      return true;
    }
Esempio n. 2
0
  public static Request parseRequest(HttpServletRequest httpServletRequest) throws Exception {

    URI uri = new URI(httpServletRequest.getRequestURI());
    String method = httpServletRequest.getMethod().intern();
    String path = uri.getRawPath();
    if (path != null) {
      path = URLDecoder.decode(path, "utf-8");
    }
    String querystring =
        httpServletRequest.getQueryString() == null ? "" : httpServletRequest.getQueryString();

    if (Logger.isTraceEnabled()) {
      Logger.trace("httpServletRequest.getContextPath(): " + httpServletRequest.getContextPath());
      Logger.trace("request.path: " + path + ", request.querystring: " + querystring);
    }

    String contentType = null;
    if (httpServletRequest.getHeader("Content-Type") != null) {
      contentType =
          httpServletRequest.getHeader("Content-Type").split(";")[0].trim().toLowerCase().intern();
    } else {
      contentType = "text/html".intern();
    }

    if (httpServletRequest.getHeader("X-HTTP-Method-Override") != null) {
      method = httpServletRequest.getHeader("X-HTTP-Method-Override").intern();
    }

    InputStream body = httpServletRequest.getInputStream();
    boolean secure = httpServletRequest.isSecure();

    String url =
        uri.toString()
            + (httpServletRequest.getQueryString() == null
                ? ""
                : "?" + httpServletRequest.getQueryString());
    String host = httpServletRequest.getHeader("host");
    int port = 0;
    String domain = null;
    if (host.contains(":")) {
      port = Integer.parseInt(host.split(":")[1]);
      domain = host.split(":")[0];
    } else {
      port = 80;
      domain = host;
    }

    String remoteAddress = httpServletRequest.getRemoteAddr();

    boolean isLoopback = host.matches("^127\\.0\\.0\\.1:?[0-9]*$");

    final Request request =
        Request.createRequest(
            remoteAddress,
            method,
            path,
            querystring,
            contentType,
            body,
            url,
            host,
            isLoopback,
            port,
            domain,
            secure,
            getHeaders(httpServletRequest),
            getCookies(httpServletRequest));

    Request.current.set(request);
    Router.routeOnlyStatic(request);

    return request;
  }
Esempio n. 3
0
  public static Request parseRequest(HttpServletRequest httpServletRequest) throws Exception {
    Request request = new Http.Request();
    Request.current.set(request);
    URI uri = new URI(httpServletRequest.getRequestURI());
    request.method = httpServletRequest.getMethod().intern();
    request.path = uri.getPath();
    request.querystring =
        httpServletRequest.getQueryString() == null ? "" : httpServletRequest.getQueryString();
    Logger.trace("httpServletRequest.getContextPath(): " + httpServletRequest.getContextPath());
    Logger.trace("request.path: " + request.path + ", request.querystring: " + request.querystring);

    Router.routeOnlyStatic(request);

    if (httpServletRequest.getHeader("Content-Type") != null) {
      request.contentType =
          httpServletRequest.getHeader("Content-Type").split(";")[0].trim().toLowerCase().intern();
    } else {
      request.contentType = "text/html".intern();
    }

    if (httpServletRequest.getHeader("X-HTTP-Method-Override") != null) {
      request.method = httpServletRequest.getHeader("X-HTTP-Method-Override").intern();
    }

    request.body = httpServletRequest.getInputStream();
    request.secure = httpServletRequest.isSecure();

    request.url =
        uri.toString()
            + (httpServletRequest.getQueryString() == null
                ? ""
                : "?" + httpServletRequest.getQueryString());
    request.host = httpServletRequest.getHeader("host");
    if (request.host.contains(":")) {
      request.port = Integer.parseInt(request.host.split(":")[1]);
      request.domain = request.host.split(":")[0];
    } else {
      request.port = 80;
      request.domain = request.host;
    }

    request.remoteAddress = httpServletRequest.getRemoteAddr();

    if (Play.configuration.containsKey("XForwardedSupport")
        && httpServletRequest.getHeader("X-Forwarded-For") != null) {
      if (!Arrays.asList(
              Play.configuration.getProperty("XForwardedSupport", "127.0.0.1").split(","))
          .contains(request.remoteAddress)) {
        throw new RuntimeException("This proxy request is not authorized");
      } else {
        request.secure =
            ("https".equals(Play.configuration.get("XForwardedProto"))
                || "https".equals(httpServletRequest.getHeader("X-Forwarded-Proto"))
                || "on".equals(httpServletRequest.getHeader("X-Forwarded-Ssl")));
        if (Play.configuration.containsKey("XForwardedHost")) {
          request.host = (String) Play.configuration.get("XForwardedHost");
        } else if (httpServletRequest.getHeader("X-Forwarded-Host") != null) {
          request.host = httpServletRequest.getHeader("X-Forwarded-Host");
        }
        if (httpServletRequest.getHeader("X-Forwarded-For") != null) {
          request.remoteAddress = httpServletRequest.getHeader("X-Forwarded-For");
        }
      }
    }

    Enumeration headersNames = httpServletRequest.getHeaderNames();
    while (headersNames.hasMoreElements()) {
      Http.Header hd = new Http.Header();
      hd.name = (String) headersNames.nextElement();
      hd.values = new ArrayList<String>();
      Enumeration enumValues = httpServletRequest.getHeaders(hd.name);
      while (enumValues.hasMoreElements()) {
        String value = (String) enumValues.nextElement();
        hd.values.add(value);
      }
      request.headers.put(hd.name.toLowerCase(), hd);
    }

    request.resolveFormat();

    javax.servlet.http.Cookie[] cookies = httpServletRequest.getCookies();
    if (cookies != null) {
      for (javax.servlet.http.Cookie cookie : cookies) {
        Http.Cookie playCookie = new Http.Cookie();
        playCookie.name = cookie.getName();
        playCookie.path = cookie.getPath();
        playCookie.domain = cookie.getDomain();
        playCookie.secure = cookie.getSecure();
        playCookie.value = cookie.getValue();
        playCookie.maxAge = cookie.getMaxAge();
        request.cookies.put(playCookie.name, playCookie);
      }
    }

    request._init();

    return request;
  }