Skip to content

Commit

Permalink
Make chunked transfer-encoding optional
Browse files Browse the repository at this point in the history
  • Loading branch information
gitblit committed May 3, 2016
1 parent c024a56 commit bafb890
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Upgrade [pippo-tomcat] to Tomcat 8.0.33
- Upgrade [pippo-jade] to Jade 1.1.4
- Upgrade [pippo-pebble] to Pebble 2.2.1
- Make `chunked` transfer-encoding optional, not the default

#### Added
- [#245]: Route groups
Expand Down
43 changes: 38 additions & 5 deletions pippo-core/src/main/java/ro/pippo/core/Response.java
Expand Up @@ -58,6 +58,7 @@ public final class Response {
private MimeTypes mimeTypes;

private int status;
private boolean chunked;

public Response(HttpServletResponse httpServletResponse, Application application) {
this.httpServletResponse = httpServletResponse;
Expand Down Expand Up @@ -356,6 +357,29 @@ public Response status(int status) {
return this;
}

/**
* Sets the chunked transfer encoding.
*
* @return the response
*/
public Response chunked() {
return chunked(true);
}

/**
* Controls the chunked transfer encoding.
*
* @param chunked
* @return the response
*/
public Response chunked(boolean chunked) {
checkCommitted();

this.chunked = chunked;

return this;
}

/**
* Redirect the browser to a location which may be...
* <ul>
Expand Down Expand Up @@ -890,8 +914,10 @@ public void resource(InputStream input) {
// by calling httpServletResponse.getOutputStream() we are committing the response
IoUtils.copy(input, httpServletResponse.getOutputStream());

// flushing the buffer forces chunked-encoding
httpServletResponse.flushBuffer();
if (chunked) {
// flushing the buffer forces chunked-encoding
httpServletResponse.flushBuffer();
}
} catch (Exception e) {
throw new PippoRuntimeException(e);
} finally {
Expand All @@ -907,6 +933,7 @@ public void resource(InputStream input) {
*/
public void file(File file) {
try {
contentLength(file.length());
file(file.getName(), new FileInputStream(file));
} catch (FileNotFoundException e) {
throw new PippoRuntimeException(e);
Expand Down Expand Up @@ -942,8 +969,10 @@ public void file(String filename, InputStream input) {
// by calling httpServletResponse.getOutputStream() we are committing the response
IoUtils.copy(input, httpServletResponse.getOutputStream());

// flushing the buffer forces chunked-encoding
httpServletResponse.flushBuffer();
if (chunked) {
// flushing the buffer forces chunked-encoding
httpServletResponse.flushBuffer();
}
} catch (Exception e) {
throw new PippoRuntimeException(e);
} finally {
Expand Down Expand Up @@ -1020,12 +1049,16 @@ private void commit(CharSequence content) {
contentType(HttpConstants.ContentType.TEXT_HTML);
}


try {
if (content != null) {
contentLength(content.length());
httpServletResponse.getWriter().append(content);
}
log.trace("Response committed");
httpServletResponse.flushBuffer();
if (chunked) {
httpServletResponse.flushBuffer();
}
} catch (IOException e) {
throw new PippoRuntimeException(e);
}
Expand Down
25 changes: 22 additions & 3 deletions pippo-core/src/main/java/ro/pippo/core/route/DirectoryHandler.java
Expand Up @@ -57,6 +57,7 @@ public class DirectoryHandler implements RouteHandler {
private String timestampPattern = "yyyy-MM-dd HH:mm Z";
private String fileSizePattern = "#,000";
private String directoryTemplate;
private boolean chunked;

public DirectoryHandler(String urlPath, File directory) {
this.urlPath = urlPath;
Expand Down Expand Up @@ -112,6 +113,16 @@ public DirectoryHandler setDirectoryTemplate(String template) {
return this;
}

public boolean getChunkedTransfer() {
return chunked;
}

public DirectoryHandler setChunkedTransfer(boolean chunked) {
this.chunked = chunked;

return this;
}

@Override
public final void handle(RouteContext routeContext) {
String resourcePath = getResourcePath(routeContext);
Expand All @@ -134,6 +145,7 @@ protected void handle(RouteContext routeContext, String resourcePath) {
File file = requestedPath.toFile();
if (file.exists()) {
if (file.isFile()) {
routeContext.getResponse().contentLength(file.length());
URL url = requestedPath.toUri().toURL();
switch (routeContext.getRequestMethod()) {
case HttpConstants.Method.HEAD:
Expand Down Expand Up @@ -174,6 +186,7 @@ protected File getIndexFile(File dir) {
protected void handleDirectoryRequest(RouteContext routeContext, File dir) throws MalformedURLException {
File index = getIndexFile(dir);
if (index != null) {
routeContext.getResponse().contentLength(index.length());
URL url = index.toURI().toURL();
streamResource(url, routeContext);
return;
Expand Down Expand Up @@ -225,6 +238,13 @@ protected void setResponseHeaders(URL resourceUrl, RouteContext routeContext) {
try {
long lastModified = resourceUrl.openConnection().getLastModified();
routeContext.getApplication().getHttpCacheToolkit().addEtag(routeContext, lastModified);

String filename = resourceUrl.getFile();
String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename);
if (!StringUtils.isNullOrEmpty(mimeType)) {
routeContext.getResponse().contentType(mimeType);
}

} catch (Exception e) {
throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl);
}
Expand All @@ -250,12 +270,11 @@ protected void sendResource(URL resourceUrl, RouteContext routeContext) throws I
if (!StringUtils.isNullOrEmpty(mimeType)) {
// stream the resource
log.debug("Streaming as resource '{}'", resourceUrl);
routeContext.getResponse().contentType(mimeType);
routeContext.getResponse().ok().resource(resourceUrl.openStream());
routeContext.getResponse().ok().chunked(chunked).resource(resourceUrl.openStream());
} else {
// stream the file
log.debug("Streaming as file '{}'", resourceUrl);
routeContext.getResponse().ok().file(filename, resourceUrl.openStream());
routeContext.getResponse().ok().chunked(chunked).file(filename, resourceUrl.openStream());
}
}

Expand Down

0 comments on commit bafb890

Please sign in to comment.