Search in sources :

Example 1 with ContentEncodedResourceManager

use of io.undertow.server.handlers.encoding.ContentEncodedResourceManager in project undertow by undertow-io.

the class ContentEncodedResourceTestCase method setup.

@BeforeClass
public static void setup() throws IOException {
    tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), DIR_NAME);
    final PathResourceManager resourceManager = new PathResourceManager(tmpDir, 10485760);
    DefaultServer.setRootHandler(new ResourceHandler(resourceManager).setContentEncodedResourceManager(new ContentEncodedResourceManager(tmpDir, new CachingResourceManager(100, 10000, null, resourceManager, -1), new ContentEncodingRepository().addEncodingHandler("deflate", new DeflateEncodingProvider(), 50, null), 0, 100000, null)));
}
Also used : ContentEncodedResourceManager(io.undertow.server.handlers.encoding.ContentEncodedResourceManager) DeflateEncodingProvider(io.undertow.server.handlers.encoding.DeflateEncodingProvider) ContentEncodingRepository(io.undertow.server.handlers.encoding.ContentEncodingRepository) ResourceHandler(io.undertow.server.handlers.resource.ResourceHandler) CachingResourceManager(io.undertow.server.handlers.resource.CachingResourceManager) PathResourceManager(io.undertow.server.handlers.resource.PathResourceManager) BeforeClass(org.junit.BeforeClass)

Example 2 with ContentEncodedResourceManager

use of io.undertow.server.handlers.encoding.ContentEncodedResourceManager in project undertow by undertow-io.

the class ResourceHandler method serveResource.

private void serveResource(final HttpServerExchange exchange, final boolean sendContent) throws Exception {
    if (DirectoryUtils.sendRequestedBlobs(exchange)) {
        return;
    }
    if (!allowed.resolve(exchange)) {
        exchange.setStatusCode(StatusCodes.FORBIDDEN);
        exchange.endExchange();
        return;
    }
    ResponseCache cache = exchange.getAttachment(ResponseCache.ATTACHMENT_KEY);
    final boolean cachable = this.cachable.resolve(exchange);
    // we set caching headers before we try and serve from the cache
    if (cachable && cacheTime != null) {
        exchange.getResponseHeaders().put(Headers.CACHE_CONTROL, "public, max-age=" + cacheTime);
        long date = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(cacheTime);
        String dateHeader = DateUtils.toDateString(new Date(date));
        exchange.getResponseHeaders().put(Headers.EXPIRES, dateHeader);
    }
    if (cache != null && cachable) {
        if (cache.tryServeResponse()) {
            return;
        }
    }
    // we now dispatch to a worker thread
    // as resource manager methods are potentially blocking
    HttpHandler dispatchTask = new HttpHandler() {

        @Override
        public void handleRequest(HttpServerExchange exchange) throws Exception {
            Resource resource = null;
            try {
                if (File.separatorChar == '/' || !exchange.getRelativePath().contains(File.separator)) {
                    // we don't process resources that contain the sperator character if this is not /
                    // this prevents attacks where people use windows path seperators in file URLS's
                    resource = resourceSupplier.getResource(exchange, canonicalize(exchange.getRelativePath()));
                }
            } catch (IOException e) {
                clearCacheHeaders(exchange);
                UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
                exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                exchange.endExchange();
                return;
            }
            if (resource == null) {
                clearCacheHeaders(exchange);
                // usually a 404 handler
                next.handleRequest(exchange);
                return;
            }
            if (resource.isDirectory()) {
                Resource indexResource;
                try {
                    indexResource = getIndexFiles(exchange, resourceSupplier, resource.getPath(), welcomeFiles);
                } catch (IOException e) {
                    UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
                    exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                    exchange.endExchange();
                    return;
                }
                if (indexResource == null) {
                    if (directoryListingEnabled) {
                        DirectoryUtils.renderDirectoryListing(exchange, resource);
                        return;
                    } else {
                        exchange.setStatusCode(StatusCodes.FORBIDDEN);
                        exchange.endExchange();
                        return;
                    }
                } else if (!exchange.getRequestPath().endsWith("/")) {
                    exchange.setStatusCode(StatusCodes.FOUND);
                    exchange.getResponseHeaders().put(Headers.LOCATION, RedirectBuilder.redirect(exchange, exchange.getRelativePath() + "/", true));
                    exchange.endExchange();
                    return;
                }
                resource = indexResource;
            } else if (exchange.getRelativePath().endsWith("/")) {
                // UNDERTOW-432
                exchange.setStatusCode(StatusCodes.NOT_FOUND);
                exchange.endExchange();
                return;
            }
            final ETag etag = resource.getETag();
            final Date lastModified = resource.getLastModified();
            if (!ETagUtils.handleIfMatch(exchange, etag, false) || !DateUtils.handleIfUnmodifiedSince(exchange, lastModified)) {
                exchange.setStatusCode(StatusCodes.PRECONDITION_FAILED);
                exchange.endExchange();
                return;
            }
            if (!ETagUtils.handleIfNoneMatch(exchange, etag, true) || !DateUtils.handleIfModifiedSince(exchange, lastModified)) {
                exchange.setStatusCode(StatusCodes.NOT_MODIFIED);
                exchange.endExchange();
                return;
            }
            final ContentEncodedResourceManager contentEncodedResourceManager = ResourceHandler.this.contentEncodedResourceManager;
            Long contentLength = resource.getContentLength();
            if (contentLength != null && !exchange.getResponseHeaders().contains(Headers.TRANSFER_ENCODING)) {
                exchange.setResponseContentLength(contentLength);
            }
            ByteRange.RangeResponseResult rangeResponse = null;
            long start = -1, end = -1;
            if (resource instanceof RangeAwareResource && ((RangeAwareResource) resource).isRangeSupported() && contentLength != null && contentEncodedResourceManager == null) {
                exchange.getResponseHeaders().put(Headers.ACCEPT_RANGES, "bytes");
                // TODO: figure out what to do with the content encoded resource manager
                ByteRange range = ByteRange.parse(exchange.getRequestHeaders().getFirst(Headers.RANGE));
                if (range != null && range.getRanges() == 1 && resource.getContentLength() != null) {
                    rangeResponse = range.getResponseResult(resource.getContentLength(), exchange.getRequestHeaders().getFirst(Headers.IF_RANGE), resource.getLastModified(), resource.getETag() == null ? null : resource.getETag().getTag());
                    if (rangeResponse != null) {
                        start = rangeResponse.getStart();
                        end = rangeResponse.getEnd();
                        exchange.setStatusCode(rangeResponse.getStatusCode());
                        exchange.getResponseHeaders().put(Headers.CONTENT_RANGE, rangeResponse.getContentRange());
                        long length = rangeResponse.getContentLength();
                        exchange.setResponseContentLength(length);
                        if (rangeResponse.getStatusCode() == StatusCodes.REQUEST_RANGE_NOT_SATISFIABLE) {
                            return;
                        }
                    }
                }
            }
            if (!exchange.getResponseHeaders().contains(Headers.CONTENT_TYPE)) {
                final String contentType = resource.getContentType(mimeMappings);
                if (contentType != null) {
                    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, contentType);
                } else {
                    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/octet-stream");
                }
            }
            if (lastModified != null) {
                exchange.getResponseHeaders().put(Headers.LAST_MODIFIED, resource.getLastModifiedString());
            }
            if (etag != null) {
                exchange.getResponseHeaders().put(Headers.ETAG, etag.toString());
            }
            if (contentEncodedResourceManager != null) {
                try {
                    ContentEncodedResource encoded = contentEncodedResourceManager.getResource(resource, exchange);
                    if (encoded != null) {
                        exchange.getResponseHeaders().put(Headers.CONTENT_ENCODING, encoded.getContentEncoding());
                        exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, encoded.getResource().getContentLength());
                        encoded.getResource().serve(exchange.getResponseSender(), exchange, IoCallback.END_EXCHANGE);
                        return;
                    }
                } catch (IOException e) {
                    // TODO: should this be fatal
                    UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
                    exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR);
                    exchange.endExchange();
                    return;
                }
            }
            if (!sendContent) {
                exchange.endExchange();
            } else if (rangeResponse != null) {
                ((RangeAwareResource) resource).serveRange(exchange.getResponseSender(), exchange, start, end, IoCallback.END_EXCHANGE);
            } else {
                resource.serve(exchange.getResponseSender(), exchange, IoCallback.END_EXCHANGE);
            }
        }
    };
    if (exchange.isInIoThread()) {
        exchange.dispatch(dispatchTask);
    } else {
        dispatchTask.handleRequest(exchange);
    }
}
Also used : HttpHandler(io.undertow.server.HttpHandler) ByteRange(io.undertow.util.ByteRange) ContentEncodedResource(io.undertow.server.handlers.encoding.ContentEncodedResource) HttpString(io.undertow.util.HttpString) IOException(java.io.IOException) ContentEncodedResource(io.undertow.server.handlers.encoding.ContentEncodedResource) Date(java.util.Date) HttpServerExchange(io.undertow.server.HttpServerExchange) ContentEncodedResourceManager(io.undertow.server.handlers.encoding.ContentEncodedResourceManager) ETag(io.undertow.util.ETag) ResponseCache(io.undertow.server.handlers.cache.ResponseCache)

Aggregations

ContentEncodedResourceManager (io.undertow.server.handlers.encoding.ContentEncodedResourceManager)2 HttpHandler (io.undertow.server.HttpHandler)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 ResponseCache (io.undertow.server.handlers.cache.ResponseCache)1 ContentEncodedResource (io.undertow.server.handlers.encoding.ContentEncodedResource)1 ContentEncodingRepository (io.undertow.server.handlers.encoding.ContentEncodingRepository)1 DeflateEncodingProvider (io.undertow.server.handlers.encoding.DeflateEncodingProvider)1 CachingResourceManager (io.undertow.server.handlers.resource.CachingResourceManager)1 PathResourceManager (io.undertow.server.handlers.resource.PathResourceManager)1 ResourceHandler (io.undertow.server.handlers.resource.ResourceHandler)1 ByteRange (io.undertow.util.ByteRange)1 ETag (io.undertow.util.ETag)1 HttpString (io.undertow.util.HttpString)1 IOException (java.io.IOException)1 Date (java.util.Date)1 BeforeClass (org.junit.BeforeClass)1