Пример #1
1
  private static HttpResponse addEtag(
      HttpRequest nettyRequest, HttpResponse httpResponse, File file) {
    if (Play.mode == Play.Mode.DEV) {
      httpResponse.setHeader(CACHE_CONTROL, "no-cache");
    } else {
      String maxAge = Play.configuration.getProperty("http.cacheControl", "3600");
      if (maxAge.equals("0")) {
        httpResponse.setHeader(CACHE_CONTROL, "no-cache");
      } else {
        httpResponse.setHeader(CACHE_CONTROL, "max-age=" + maxAge);
      }
    }
    boolean useEtag = Play.configuration.getProperty("http.useETag", "true").equals("true");
    long last = file.lastModified();
    final String etag = "\"" + last + "-" + file.hashCode() + "\"";
    if (!isModified(etag, last, nettyRequest)) {
      if (nettyRequest.getMethod().equals(HttpMethod.GET)) {
        httpResponse.setStatus(HttpResponseStatus.NOT_MODIFIED);
      }
      if (useEtag) {
        httpResponse.setHeader(ETAG, etag);
      }

    } else {
      httpResponse.setHeader(LAST_MODIFIED, Utils.getHttpDateFormatter().format(new Date(last)));
      if (useEtag) {
        httpResponse.setHeader(ETAG, etag);
      }
    }
    return httpResponse;
  }
Пример #2
0
  protected static void writeResponse(
      ChannelHandlerContext ctx,
      Response response,
      HttpResponse nettyResponse,
      HttpRequest nettyRequest) {
    Logger.trace("writeResponse: begin");
    byte[] content = null;

    final boolean keepAlive = isKeepAlive(nettyRequest);
    if (nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
      content = new byte[0];
    } else {
      content = response.out.toByteArray();
    }

    ChannelBuffer buf = ChannelBuffers.copiedBuffer(content);
    nettyResponse.setContent(buf);

    if (keepAlive) {
      // Add 'Content-Length' header only for a keep-alive connection.
      Logger.trace("writeResponse: content length [" + response.out.size() + "]");
      setContentLength(nettyResponse, response.out.size());
    }

    ChannelFuture f = ctx.getChannel().write(nettyResponse);

    // Decide whether to close the connection or not.
    if (!keepAlive) {
      // Close the connection when the whole content is written out.
      f.addListener(ChannelFutureListener.CLOSE);
    }
    Logger.trace("writeResponse: end");
  }
Пример #3
0
  public void serveStatic(
      RenderStatic renderStatic,
      ChannelHandlerContext ctx,
      Request request,
      Response response,
      HttpRequest nettyRequest,
      MessageEvent e) {
    Logger.trace("serveStatic: begin");
    HttpResponse nettyResponse =
        new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.status));
    if (exposePlayServer) {
      nettyResponse.setHeader(SERVER, signature);
    }
    try {
      VirtualFile file = Play.getVirtualFile(renderStatic.file);
      if (file != null && file.exists() && file.isDirectory()) {
        file = file.child("index.html");
        if (file != null) {
          renderStatic.file = file.relativePath();
        }
      }
      if ((file == null || !file.exists())) {
        serve404(
            new NotFound("The file " + renderStatic.file + " does not exist"),
            ctx,
            request,
            nettyRequest);
      } else {
        boolean raw = false;
        for (PlayPlugin plugin : Play.plugins) {
          if (plugin.serveStatic(file, Request.current(), Response.current())) {
            raw = true;
            break;
          }
        }
        if (raw) {
          copyResponse(ctx, request, response, nettyRequest);
        } else {
          final File localFile = file.getRealFile();
          final boolean keepAlive = isKeepAlive(nettyRequest);
          nettyResponse = addEtag(nettyRequest, nettyResponse, localFile);

          if (nettyResponse.getStatus().equals(HttpResponseStatus.NOT_MODIFIED)) {

            Channel ch = e.getChannel();

            // Write the initial line and the header.
            ChannelFuture writeFuture = ch.write(nettyResponse);
            if (!keepAlive) {
              // Write the content.
              writeFuture.addListener(ChannelFutureListener.CLOSE);
            }
          } else {

            final RandomAccessFile raf = new RandomAccessFile(localFile, "r");
            try {
              long fileLength = raf.length();

              Logger.trace("keep alive " + keepAlive);
              Logger.trace(
                  "content type " + (MimeTypes.getContentType(localFile.getName(), "text/plain")));

              if (keepAlive && !nettyResponse.getStatus().equals(HttpResponseStatus.NOT_MODIFIED)) {
                // Add 'Content-Length' header only for a keep-alive connection.
                Logger.trace("file length " + fileLength);
                setContentLength(nettyResponse, fileLength);
              }

              nettyResponse.setHeader(
                  CONTENT_TYPE, (MimeTypes.getContentType(localFile.getName(), "text/plain")));

              Channel ch = e.getChannel();

              // Write the initial line and the header.
              ChannelFuture writeFuture = ch.write(nettyResponse);

              // Write the content.
              if (!nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
                writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192));
              } else {
                raf.close();
              }

              if (!keepAlive) {
                // Close the connection when the whole content is written out.
                writeFuture.addListener(ChannelFutureListener.CLOSE);
              }
            } catch (Throwable exx) {
              try {
                raf.close();
              } catch (Throwable ex) {
                /* Left empty */
              }
              try {
                ctx.getChannel().close();
              } catch (Throwable ex) {
                /* Left empty */
              }
            }
          }
        }
      }
    } catch (Throwable ez) {
      Logger.error(ez, "serveStatic for request %s", request.method + " " + request.url);
      try {
        HttpResponse errorResponse =
            new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
        ChannelBuffer buf =
            ChannelBuffers.copiedBuffer("Internal Error (check logs)".getBytes("utf-8"));
        errorResponse.setContent(buf);
        ChannelFuture future = ctx.getChannel().write(errorResponse);
        future.addListener(ChannelFutureListener.CLOSE);
      } catch (Exception ex) {
        Logger.error(ez, "serveStatic for request %s", request.method + " " + request.url);
      }
    }
    Logger.trace("serveStatic: end");
  }
Пример #4
0
  public Request parseRequest(ChannelHandlerContext ctx, HttpRequest nettyRequest)
      throws Exception {
    Logger.trace("parseRequest: begin");
    Logger.trace("parseRequest: URI = " + nettyRequest.getUri());
    int index = nettyRequest.getUri().indexOf("?");
    String querystring = "";

    String uri = nettyRequest.getUri();
    // Remove domain and port from URI if it's present.
    if (uri.startsWith("http://") || uri.startsWith("https://")) {
      // Begins searching / after 9th character (last / of https://)
      uri = uri.substring(uri.indexOf("/", 9));
    }

    String path = URLDecoder.decode(uri, "UTF-8");
    if (index != -1) {
      path = URLDecoder.decode(uri.substring(0, index), "UTF-8");
      querystring = uri.substring(index + 1);
    }

    final Request request = new Request();

    request.remoteAddress = getRemoteIPAddress(ctx);
    request.method = nettyRequest.getMethod().getName();
    request.path = path;
    request.querystring = querystring;
    final String contentType = nettyRequest.getHeader(CONTENT_TYPE);
    if (contentType != null) {
      request.contentType = contentType.split(";")[0].trim().toLowerCase();
    } else {
      request.contentType = "text/html";
    }

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

    ChannelBuffer b = nettyRequest.getContent();
    if (b instanceof FileChannelBuffer) {
      FileChannelBuffer buffer = (FileChannelBuffer) b;
      // An error occurred
      Integer max =
          Integer.valueOf(Play.configuration.getProperty("play.netty.maxContentLength", "-1"));

      request.body = buffer.getInputStream();
      if (!(max == -1 || request.body.available() < max)) {
        request.body = new ByteArrayInputStream(new byte[0]);
      }

    } else {
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      IOUtils.copy(new ChannelBufferInputStream(b), out);
      byte[] n = out.toByteArray();
      request.body = new ByteArrayInputStream(n);
    }

    request.url = uri;
    request.host = nettyRequest.getHeader(HOST);
    request.isLoopback =
        ((InetSocketAddress) ctx.getChannel().getRemoteAddress()).getAddress().isLoopbackAddress()
            && request.host.matches("^127\\.0\\.0\\.1:?[0-9]*$");

    if (request.host == null) {
      request.host = "";
      request.port = 80;
      request.domain = "";
    } else {
      if (request.host.contains(":")) {
        final String[] host = request.host.split(":");
        request.port = Integer.parseInt(host[1]);
        request.domain = host[0];
      } else {
        request.port = 80;
        request.domain = request.host;
      }
    }

    if (Play.configuration.containsKey("XForwardedSupport")
        && nettyRequest.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: " + request.remoteAddress);
      } else {
        request.secure =
            ("https".equals(Play.configuration.get("XForwardedProto"))
                || "https".equals(nettyRequest.getHeader("X-Forwarded-Proto"))
                || "on".equals(nettyRequest.getHeader("X-Forwarded-Ssl")));
        if (Play.configuration.containsKey("XForwardedHost")) {
          request.host = (String) Play.configuration.get("XForwardedHost");
        } else if (nettyRequest.getHeader("X-Forwarded-Host") != null) {
          request.host = nettyRequest.getHeader("X-Forwarded-Host");
        }
        if (nettyRequest.getHeader("X-Forwarded-For") != null) {
          request.remoteAddress = nettyRequest.getHeader("X-Forwarded-For");
        }
      }
    }

    addToRequest(nettyRequest, request);

    request.resolveFormat();

    request._init();

    Logger.trace("parseRequest: end");
    return request;
  }
Пример #5
0
  public void copyResponse(
      ChannelHandlerContext ctx, Request request, Response response, HttpRequest nettyRequest)
      throws Exception {
    Logger.trace("copyResponse: begin");
    // response.out.flush();

    // Decide whether to close the connection or not.

    HttpResponse nettyResponse =
        new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(response.status));
    if (exposePlayServer) {
      nettyResponse.setHeader(SERVER, signature);
    }

    if (response.contentType != null) {
      nettyResponse.setHeader(
          CONTENT_TYPE,
          response.contentType
              + (response.contentType.startsWith("text/")
                      && !response.contentType.contains("charset")
                  ? "; charset=utf-8"
                  : ""));
    } else {
      nettyResponse.setHeader(CONTENT_TYPE, "text/plain; charset=utf-8");
    }

    addToResponse(response, nettyResponse);

    final Object obj = response.direct;
    File file = null;
    ChunkedInput stream = null;
    InputStream is = null;
    if (obj instanceof File) {
      file = (File) obj;
    } else if (obj instanceof InputStream) {
      is = (InputStream) obj;
    } else if (obj instanceof ChunkedInput) {
      stream = (ChunkedInput) obj;
    }

    final boolean keepAlive = isKeepAlive(nettyRequest);
    if (file != null && file.isFile()) {
      try {
        nettyResponse = addEtag(nettyRequest, nettyResponse, file);
        if (nettyResponse.getStatus().equals(HttpResponseStatus.NOT_MODIFIED)) {

          Channel ch = ctx.getChannel();

          // Write the initial line and the header.
          ChannelFuture writeFuture = ch.write(nettyResponse);

          if (!keepAlive) {
            // Close the connection when the whole content is written out.
            writeFuture.addListener(ChannelFutureListener.CLOSE);
          }
        } else {
          nettyResponse.setHeader(
              CONTENT_TYPE, MimeTypes.getContentType(file.getName(), "text/plain"));
          final RandomAccessFile raf = new RandomAccessFile(file, "r");
          try {
            long fileLength = raf.length();

            if (keepAlive) {
              // Add 'Content-Length' header only for a keep-alive connection.
              Logger.trace("file length is [" + fileLength + "]");
              setContentLength(nettyResponse, fileLength);
            }

            Channel ch = ctx.getChannel();

            // Write the initial line and the header.
            ChannelFuture writeFuture = ch.write(nettyResponse);

            // Write the content.
            // If it is not a HEAD
            if (!nettyRequest.getMethod().equals(HttpMethod.HEAD)) {
              writeFuture = ch.write(new ChunkedFile(raf, 0, fileLength, 8192));
            } else {
              raf.close();
            }
            if (!keepAlive) {
              // Close the connection when the whole content is written out.
              writeFuture.addListener(ChannelFutureListener.CLOSE);
            }
          } catch (Throwable exx) {
            try {
              raf.close();
            } catch (Throwable ex) {
              /* Left empty */
            }
            try {
              ctx.getChannel().close();
            } catch (Throwable ex) {
              /* Left empty */
            }
          }
        }
      } catch (Exception e) {
        throw e;
      }
    } else if (is != null) {
      ChannelFuture writeFuture = ctx.getChannel().write(nettyResponse);
      if (!nettyRequest.getMethod().equals(HttpMethod.HEAD)
          && !nettyResponse.getStatus().equals(HttpResponseStatus.NOT_MODIFIED)) {
        writeFuture = ctx.getChannel().write(new ChunkedStream(is));
      } else {
        is.close();
      }
      if (!keepAlive) {
        writeFuture.addListener(ChannelFutureListener.CLOSE);
      }
    } else if (stream != null) {
      ChannelFuture writeFuture = ctx.getChannel().write(nettyResponse);
      if (!nettyRequest.getMethod().equals(HttpMethod.HEAD)
          && !nettyResponse.getStatus().equals(HttpResponseStatus.NOT_MODIFIED)) {
        writeFuture = ctx.getChannel().write(stream);
      } else {
        stream.close();
      }
      if (!keepAlive) {
        writeFuture.addListener(ChannelFutureListener.CLOSE);
      }
    } else {
      writeResponse(ctx, response, nettyResponse, nettyRequest);
    }
    Logger.trace("copyResponse: end");
  }