Example #1
0
  private static void sendStatus(
      HttpResponse response,
      @Nullable HttpRequest request,
      Channel channel,
      @Nullable String description) {
    response.setHeader(CONTENT_TYPE, "text/html");
    if (request == null || request.getMethod() != HttpMethod.HEAD) {
      String message = response.getStatus().toString();

      StringBuilder builder = new StringBuilder();
      builder
          .append("<!doctype html><title>")
          .append(message)
          .append("</title>")
          .append("<h1 style=\"text-align: center\">")
          .append(message)
          .append("</h1>");
      if (description != null) {
        builder.append("<p>").append(description).append("</p>");
      }
      builder
          .append("<hr/><p style=\"text-align: center\">")
          .append(StringUtil.notNullize(getServerHeaderValue(), ""))
          .append("</p>");

      response.setContent(ChannelBuffers.copiedBuffer(builder, CharsetUtil.UTF_8));
    }
    send(response, channel, request);
  }
Example #2
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);
      }
    }
  }
Example #3
0
 public void handleJsRequest(String path, HttpRequest req, HttpResponse resp) {
   if (req.containsHeader(IF_NONE_MATCH) || req.containsHeader(IF_MODIFIED_SINCE)) {
     resp.setStatus(HttpResponseStatus.NOT_MODIFIED);
     return;
   }
   String jsName = path.substring(1);
   ChannelBuffer buf = jsBufferMap.get(jsName);
   if (buf == null) {
     tryReloadJs();
     if (jsName == null) {
       log.error("需要访问的JS名称为空。[req: " + req.getUri() + "]");
     }
     buf = jsBufferMap.get(jsName);
   }
   if (buf != null) {
     resp.setContent(buf);
     resp.setHeader(CONTENT_LENGTH, buf.capacity());
     resp.setHeader(CONTENT_TYPE, "text/javascript; charset=UTF-8");
     resp.setHeader(CONTENT_ENCODING, "gzip");
     //			resp.setHeader(EXPIRES,  System.currentTimeMillis() + expired);
     StringBuilder etagBuilder =
         new StringBuilder("W/\"")
             .append(buf.capacity())
             .append("-")
             .append(System.currentTimeMillis())
             .append("\"");
     resp.setHeader(ETAG, etagBuilder.toString());
     LogRequestParser lrp = logRequestParserThreadLocal.get();
     if (lrp == null) {
       lrp = new LogRequestParser();
     }
     lrp.cdate.setTime(System.currentTimeMillis() + expired);
     resp.setHeader(EXPIRES, lrp.sdf.format(lrp.cdate));
   } else {
     resp.setStatus(HttpResponseStatus.NOT_FOUND);
   }
 }