NPE on sending from file URLs

This commit is contained in:
Jörg Prante 2024-04-11 12:31:45 +02:00
parent 0ea35b8af5
commit 6e83937cc1
2 changed files with 7 additions and 4 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib group = org.xbib
name = net-http name = net-http
version = 4.4.2 version = 4.4.3

View file

@ -234,12 +234,12 @@ public abstract class AbstractResourceHandler implements HttpHandler {
} }
} }
} }
if (ranges.isEmpty() || ranges.get(0) == full) { if (ranges.isEmpty() || ranges.getFirst() == full) {
context.header(HttpHeaderNames.CONTENT_RANGE, "bytes " + full.start + '-' + full.end + '/' + full.total) context.header(HttpHeaderNames.CONTENT_RANGE, "bytes " + full.start + '-' + full.end + '/' + full.total)
.header(HttpHeaderNames.CONTENT_LENGTH, Long.toString(full.length)); .header(HttpHeaderNames.CONTENT_LENGTH, Long.toString(full.length));
send(resource, HttpResponseStatus.OK, contentType, context, full.start, full.length); send(resource, HttpResponseStatus.OK, contentType, context, full.start, full.length);
} else if (ranges.size() == 1) { } else if (ranges.size() == 1) {
Range r = ranges.get(0); Range r = ranges.getFirst();
context.header(HttpHeaderNames.CONTENT_RANGE, "bytes " + r.start + '-' + r.end + '/' + r.total) context.header(HttpHeaderNames.CONTENT_RANGE, "bytes " + r.start + '-' + r.end + '/' + r.total)
.header(HttpHeaderNames.CONTENT_LENGTH, Long.toString(r.length)); .header(HttpHeaderNames.CONTENT_LENGTH, Long.toString(r.length));
send(resource, HttpResponseStatus.PARTIAL_CONTENT, contentType, context, r.start, r.length); send(resource, HttpResponseStatus.PARTIAL_CONTENT, contentType, context, r.start, r.length);
@ -275,7 +275,7 @@ public abstract class AbstractResourceHandler implements HttpHandler {
private static long sublong(String value, int beginIndex, int endIndex) { private static long sublong(String value, int beginIndex, int endIndex) {
String substring = value.substring(beginIndex, endIndex); String substring = value.substring(beginIndex, endIndex);
return substring.length() > 0 ? Long.parseLong(substring) : -1; return !substring.isEmpty() ? Long.parseLong(substring) : -1;
} }
protected void send(Resource resource, protected void send(Resource resource,
@ -300,6 +300,9 @@ public abstract class AbstractResourceHandler implements HttpHandler {
} else { } else {
if ("file".equals(url.getScheme())) { if ("file".equals(url.getScheme())) {
Path path = resource.getPath(); Path path = resource.getPath();
if (path == null) {
path = Paths.get(url.toURI());
}
try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(path)) { try (FileChannel fileChannel = (FileChannel) Files.newByteChannel(path)) {
send(fileChannel, httpResponseStatus, contentType, context, offset, size); send(fileChannel, httpResponseStatus, contentType, context, offset, size);
} catch (IOException e) { } catch (IOException e) {