@Test
 public void createACookieWithNameAndValueAndChangeDomain() {
   Http.Cookie cookie = Http.Cookie.builder("name", "value").withDomain(".example.com").build();
   assertEquals("name", cookie.name());
   assertEquals("value", cookie.value());
   assertEquals("/", cookie.path());
   assertEquals(".example.com", cookie.domain());
   assertEquals(null, cookie.maxAge());
   assertEquals(false, cookie.secure());
   assertEquals(false, cookie.httpOnly());
 }
 @Test
 public void createACookieWithNameAndValueAndKeepDefaults() {
   Http.Cookie cookie = Http.Cookie.builder("name", "value").build();
   assertEquals("name", cookie.name());
   assertEquals("value", cookie.value());
   assertEquals("/", cookie.path());
   assertEquals(null, cookie.domain());
   assertEquals(null, cookie.maxAge());
   assertEquals(false, cookie.secure());
   assertEquals(false, cookie.httpOnly());
 }
 @Test
 public void createACookieWithNameAndValueWithSecureAndHttpOnlyEqualToTrue() {
   Http.Cookie cookie =
       Http.Cookie.builder("name", "value").withSecure(true).withHttpOnly(true).build();
   assertEquals("name", cookie.name());
   assertEquals("value", cookie.value());
   assertEquals("/", cookie.path());
   assertEquals(null, cookie.domain());
   assertEquals(null, cookie.maxAge());
   assertEquals(true, cookie.secure());
   assertEquals(true, cookie.httpOnly());
 }
Esempio n. 4
0
  protected static void addToRequest(HttpRequest nettyRequest, Request request) {
    for (String key : nettyRequest.getHeaderNames()) {
      Http.Header hd = new Http.Header();
      hd.name = key.toLowerCase();
      hd.values = new ArrayList<String>();
      for (String next : nettyRequest.getHeaders(key)) {
        hd.values.add(next);
      }
      request.headers.put(hd.name, hd);
    }

    String value = nettyRequest.getHeader(COOKIE);
    if (value != null) {
      Set<Cookie> cookies = new CookieDecoder().decode(value);
      if (cookies != null) {
        for (Cookie cookie : cookies) {
          Http.Cookie playCookie = new Http.Cookie();
          playCookie.name = cookie.getName();
          playCookie.path = cookie.getPath();
          playCookie.domain = cookie.getDomain();
          playCookie.secure = cookie.isSecure();
          playCookie.value = cookie.getValue();
          playCookie.httpOnly = cookie.isHttpOnly();
          request.cookies.put(playCookie.name, playCookie);
        }
      }
    }
  }
Esempio n. 5
0
  public F.Promise<Result> call(Http.Context ctx) throws Throwable {
    try {
      return delegate.call(ctx);
    } catch (Exception e) {
      e.printStackTrace();
      StringBuilder sb = new StringBuilder();

      sb.append("Error for request at " + ctx.request().uri() + "\n");
      sb.append("Headers: \n");
      Map<String, String[]> headers = ctx.request().headers();
      for (String key : headers.keySet()) {
        sb.append("  " + key + " --> ");
        for (String val : headers.get(key)) {
          sb.append(val + "|||");
        }
        sb.append("\n");
      }

      sb.append("Cookies: \n");
      for (Http.Cookie cookie : ctx.request().cookies()) {
        sb.append("  " + cookie.name() + " --> " + cookie.value() + "\n");
      }

      Http.RequestBody body = ctx.request().body();
      Map<String, String[]> body_vals = body.asFormUrlEncoded();
      if (body_vals != null) {
        sb.append("Body (as form URL encoded): \n");
        for (String key : body_vals.keySet()) {
          sb.append("  " + key + " --> ");
          for (String val : body_vals.get(key)) {
            sb.append(val + "|||");
          }
          sb.append("\n");
        }
      }

      Logger.error(sb.toString());
      throw e;
    }
  }
Esempio n. 6
0
  void saveExceededSizeError(HttpRequest nettyRequest, Request request, Response response) {

    String warning = nettyRequest.getHeader(HttpHeaders.Names.WARNING);
    String length = nettyRequest.getHeader(HttpHeaders.Names.CONTENT_LENGTH);
    if (warning != null) {
      Logger.trace("saveExceededSizeError: begin");
      try {
        StringBuilder error = new StringBuilder();
        error.append("\u0000");
        // Cannot put warning which is play.netty.content.length.exceeded
        // as Key as it will result error when printing error
        error.append("play.netty.maxContentLength");
        error.append(":");
        String size = null;
        try {
          size = JavaExtensions.formatSize(Long.parseLong(length));
        } catch (Exception e) {
          size = length + " bytes";
        }
        error.append(Messages.get(warning, size));
        error.append("\u0001");
        error.append(size);
        error.append("\u0000");
        if (request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS") != null
            && request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS").value != null) {
          error.append(request.cookies.get(Scope.COOKIE_PREFIX + "_ERRORS").value);
        }
        String errorData = URLEncoder.encode(error.toString(), "utf-8");
        Http.Cookie c = new Http.Cookie();
        c.value = errorData;
        c.name = Scope.COOKIE_PREFIX + "_ERRORS";
        request.cookies.put(Scope.COOKIE_PREFIX + "_ERRORS", c);
        Logger.trace("saveExceededSizeError: end");
      } catch (Exception e) {
        throw new UnexpectedException("Error serialization problem", e);
      }
    }
  }
Esempio n. 7
0
  protected static Map<String, Http.Cookie> getCookies(HttpServletRequest httpServletRequest) {
    Map<String, Http.Cookie> cookies = new HashMap<String, Http.Cookie>(16);
    javax.servlet.http.Cookie[] cookiesViaServlet = httpServletRequest.getCookies();
    if (cookiesViaServlet != null) {
      for (javax.servlet.http.Cookie cookie : cookiesViaServlet) {
        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();
        cookies.put(playCookie.name, playCookie);
      }
    }

    return cookies;
  }
Esempio n. 8
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;
  }