Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Níma: Static content handling rework #5543

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,18 @@ public int capacity() {
return capacity;
}

/**
* Clear all records in the cache.
*/
public void clear() {
writeLock.lock();
try {
backingMap.clear();
} finally {
writeLock.unlock();
}
}

// for unit testing
V directGet(K key) {
return backingMap.get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ protected void describeMismatchSafely(Headers item, Description mismatchDescript
mismatchDescription.appendValue(name.defaultCase()).appendText(" header is present with wrong values ");
valuesMatcher.describeMismatch(all, mismatchDescription);
} else {
mismatchDescription.appendValue(name.defaultCase()).appendText("header is not present");
mismatchDescription.appendValue(name.defaultCase()).appendText(" header is not present");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ private void registerClasspathStaticContent(Config config) {
config.get("tmp-dir")
.as(Path.class)
.ifPresent(cpBuilder::tmpDir);

config.get("cache-in-memory")
.asList(String.class)
.stream()
.flatMap(List::stream)
.forEach(cpBuilder::addCacheInMemory);

StaticContentSupport staticContent = cpBuilder.build();

if (context.exists()) {
Expand Down
6 changes: 6 additions & 0 deletions nima/webserver/static-content/etc/spotbugs/exclude.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
<Class name="io.helidon.nima.webserver.staticcontent.ClassPathContentHandler"/>
<Bug pattern="URLCONNECTION_SSRF_FD"/>
</Match>

<Match>
<!-- URL is obtained from classloader for a resource on classpath and then used to read it -->
<Class name="io.helidon.nima.webserver.staticcontent.CachedHandlerUrlStream"/>
<Bug pattern="URLCONNECTION_SSRF_FD"/>
</Match>
</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.webserver.staticcontent;

import java.io.IOException;

import io.helidon.common.configurable.LruCache;
import io.helidon.common.http.Http;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;

interface CachedHandler {
boolean handle(LruCache<String, CachedHandler> cache,
Http.Method method,
ServerRequest request,
ServerResponse response,
String requestedResource) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.webserver.staticcontent;

import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiConsumer;

import io.helidon.common.configurable.LruCache;
import io.helidon.common.http.Http;
import io.helidon.common.http.HttpException;
import io.helidon.common.http.ServerRequestHeaders;
import io.helidon.common.http.ServerResponseHeaders;
import io.helidon.common.media.type.MediaType;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;

import static io.helidon.nima.webserver.staticcontent.StaticContentHandler.processEtag;
import static io.helidon.nima.webserver.staticcontent.StaticContentHandler.processModifyHeaders;

record CachedHandlerInMemory(MediaType mediaType,
Instant lastModified,
BiConsumer<ServerResponseHeaders, Instant> setLastModifiedHeader,
byte[] bytes,
int contentLength,
Http.HeaderValue contentLengthHeader) implements CachedHandler {

@Override
public boolean handle(LruCache<String, CachedHandler> cache,
Http.Method method,
ServerRequest request,
ServerResponse response,
String requestedResource) {
// etag etc.
if (lastModified != null) {
processEtag(String.valueOf(lastModified.toEpochMilli()), request.headers(), response.headers());
processModifyHeaders(lastModified, request.headers(), response.headers(), setLastModifiedHeader);
}

response.headers().contentType(mediaType);

if (method == Http.Method.GET) {
send(request, response);
} else {
response.headers().set(contentLengthHeader());
response.send();
}

return true;
}

private void send(ServerRequest request, ServerResponse response) {
ServerRequestHeaders headers = request.headers();

if (headers.contains(Http.Header.RANGE)) {
long contentLength = contentLength();
List<ByteRangeRequest> ranges = ByteRangeRequest.parse(request,
response,
headers.get(Http.Header.RANGE).values(),
contentLength);
if (ranges.size() == 1) {
// single response
ByteRangeRequest range = ranges.get(0);

if (range.offset() > contentLength()) {
throw new HttpException("Invalid range offset", Http.Status.REQUESTED_RANGE_NOT_SATISFIABLE_416, true);
}
if (range.length() > (contentLength() - range.offset())) {
throw new HttpException("Invalid length", Http.Status.REQUESTED_RANGE_NOT_SATISFIABLE_416, true);
}

range.setContentRange(response);

// only send a part of the file
response.send(Arrays.copyOfRange(bytes(), (int) range.offset(), (int) range.length()));
} else {
// not supported, send full
send(response);
}
} else {
send(response);
}
}

private void send(ServerResponse response) {
response.headers().set(contentLengthHeader());
response.send(bytes());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/


package io.helidon.nima.webserver.staticcontent;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.function.BiConsumer;

import io.helidon.common.configurable.LruCache;
import io.helidon.common.http.Http;
import io.helidon.common.http.ServerResponseHeaders;
import io.helidon.common.media.type.MediaType;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;

import static io.helidon.nima.webserver.staticcontent.FileBasedContentHandler.contentLength;
import static io.helidon.nima.webserver.staticcontent.FileBasedContentHandler.send;
import static io.helidon.nima.webserver.staticcontent.StaticContentHandler.processEtag;
import static io.helidon.nima.webserver.staticcontent.StaticContentHandler.processModifyHeaders;

record CachedHandlerJar(Path path,
MediaType mediaType,
Instant lastModified,
BiConsumer<ServerResponseHeaders, Instant> setLastModifiedHeader) implements CachedHandler {
private static final System.Logger LOGGER = System.getLogger(CachedHandlerJar.class.getName());

@Override
public boolean handle(LruCache<String, CachedHandler> cache,
Http.Method method,
ServerRequest request,
ServerResponse response,
String requestedResource) throws IOException {

// check if file still exists (the tmp may have been removed, file may have been removed
// there is still a race change, but we do not want to keep cached records for invalid files
if (!Files.exists(path)) {
cache.remove(requestedResource);
return false;
}
danielkec marked this conversation as resolved.
Show resolved Hide resolved

if (LOGGER.isLoggable(System.Logger.Level.TRACE)) {
LOGGER.log(System.Logger.Level.TRACE, "Sending static content from jar: " + requestedResource);
}

// etag etc.
if (lastModified != null) {
processEtag(String.valueOf(lastModified.toEpochMilli()), request.headers(), response.headers());
processModifyHeaders(lastModified, request.headers(), response.headers(), setLastModifiedHeader());
}

response.headers().contentType(mediaType);

if (method == Http.Method.GET) {
send(request, response, path);
} else {
response.headers().contentLength(contentLength(path));
response.send();
}

return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.nima.webserver.staticcontent;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Optional;
import java.util.function.BiConsumer;

import io.helidon.common.configurable.LruCache;
import io.helidon.common.http.ForbiddenException;
import io.helidon.common.http.Http;
import io.helidon.common.http.ServerResponseHeaders;
import io.helidon.common.media.type.MediaType;
import io.helidon.nima.webserver.http.ServerRequest;
import io.helidon.nima.webserver.http.ServerResponse;

import static io.helidon.nima.webserver.staticcontent.FileBasedContentHandler.processContentLength;
import static io.helidon.nima.webserver.staticcontent.FileBasedContentHandler.send;
import static io.helidon.nima.webserver.staticcontent.StaticContentHandler.processEtag;
import static io.helidon.nima.webserver.staticcontent.StaticContentHandler.processModifyHeaders;

record CachedHandlerPath(Path path,
MediaType mediaType,
IoFunction<Path, Optional<Instant>> lastModified,
BiConsumer<ServerResponseHeaders, Instant> setLastModifiedHeader) implements CachedHandler {
private static final System.Logger LOGGER = System.getLogger(CachedHandlerPath.class.getName());

@Override
public boolean handle(LruCache<String, CachedHandler> cache,
Http.Method method,
ServerRequest request,
ServerResponse response,
String requestedResource) throws IOException {

if (LOGGER.isLoggable(System.Logger.Level.TRACE)) {
LOGGER.log(System.Logger.Level.TRACE, "Sending static content from path: " + path);
}

// now it exists and is a file
if (!Files.exists(path) || !Files.isRegularFile(path) || !Files.isReadable(path) || Files.isHidden(path)) {
// check if file still exists (the tmp may have been removed, file may have been removed
// there is still a race change, but we do not want to keep cached records for invalid files
cache.remove(requestedResource);
throw new ForbiddenException("File is not accessible");
}

Instant lastModified = lastModified().apply(path).orElse(null);

// etag etc.
if (lastModified != null) {
processEtag(String.valueOf(lastModified.toEpochMilli()), request.headers(), response.headers());
processModifyHeaders(lastModified, request.headers(), response.headers(), setLastModifiedHeader());
}

response.headers().contentType(mediaType);

if (method == Http.Method.GET) {
send(request, response, path);
} else {
processContentLength(path, response.headers());
response.send();
}

return true;
}
}
Loading