diff --git a/src/main/java/lab07/HttpHandler.java b/src/main/java/lab07/HttpHandler.java index 3d7d66c7e5480d03b3c47156dd079ea1d738ccc5..11d7c021e53b189f7c5034680c01d5ff03d3c600 100644 --- a/src/main/java/lab07/HttpHandler.java +++ b/src/main/java/lab07/HttpHandler.java @@ -1,13 +1,7 @@ package lab07; -import static jakarta.ws.rs.core.Response.Status.NOT_FOUND; -import static jakarta.ws.rs.core.Response.Status.OK; - -import java.io.FileNotFoundException; import java.io.IOException; -import java.io.RandomAccessFile; import java.nio.ByteBuffer; -import java.nio.channels.FileChannel; import java.nio.channels.SocketChannel; import java.nio.file.Path; import java.nio.file.Paths; @@ -31,14 +25,7 @@ class HttpHandler { try { request = getRequest(channel); String resource = getResource(request); - CompletableFuture<Void> getFileTransfer = transferFile(resource, channel); - getFileTransfer.thenRun(() -> { - try { - channel.close(); - } catch (IOException e) { - log.error("close", channel); - } - }); + } catch (IOException e) { writeStatus(Status.INTERNAL_SERVER_ERROR, channel); writeString("", channel); @@ -48,38 +35,25 @@ class HttpHandler { } } - private void writeStatus(Status status,SocketChannel channel) { - writeString("HTTP/1.1 %d %s".formatted(status.getStatusCode(), status.getReasonPhrase()), channel); - writeString("", channel); - } - - - private CompletableFuture<Void> transferFile(String resource, SocketChannel channel) { - return CompletableFuture.runAsync(() -> doTransferFile(resource, channel)); + return null; } private void doTransferFile(final String resource, SocketChannel channel) { - try { - String resourcePath; - if (resource.length() > 1) { - resourcePath = resource.substring(1); - } else { - resourcePath = resource; - } - RandomAccessFile raf = new RandomAccessFile(root.resolve(Paths.get(resourcePath)).toFile(), "r"); - - try(FileChannel fileChannel = raf.getChannel()) { - writeStatus(OK, channel); - fileChannel.transferTo(0, raf.length(), channel); - } - - } catch (FileNotFoundException e) { - writeStatus(NOT_FOUND, channel); - writeString("%s %s".formatted(resource, NOT_FOUND.getReasonPhrase()), channel); - } catch (IOException e) { - log.error("transfer", e); - } + @SuppressWarnings("unused") + Path filePath = getFilePath(resource); + //TODO implement data transfer from file to socket + } + + private Path getFilePath(String resource) { + //remove trailing '/' to relative path + String resourcePath; + if (resource.length() > 1) { + resourcePath = resource.substring(1); + } else { + resourcePath = resource; + } + return root.resolve(Paths.get(resourcePath)); } private String getRequest(SocketChannel channel) throws IOException { @@ -90,18 +64,11 @@ class HttpHandler { return result; } - private static String getResource(String readedData) { - try (Scanner scanner = new Scanner(readedData)) { - scanner.useDelimiter("\\s"); - String method = scanner.next(); - if (!method.equals("GET")) { - throw new UnsupportedOperationException(method); - } - return scanner.next(); - } + private void writeStatus(Status status,SocketChannel channel) { + writeString("HTTP/1.1 %d %s".formatted(status.getStatusCode(), status.getReasonPhrase()), channel); + writeString("", channel); } - - + private void writeString(String value, SocketChannel socketChannel) { buffer.put(value.getBytes()); buffer.put("\n".getBytes()); @@ -113,4 +80,15 @@ class HttpHandler { } buffer.clear(); } + + private static String getResource(String readedData) { + try (Scanner scanner = new Scanner(readedData)) { + scanner.useDelimiter("\\s"); + String method = scanner.next(); + if (!method.equals("GET")) { + throw new UnsupportedOperationException(method); + } + return scanner.next(); + } + } } diff --git a/src/main/java/lab07/Main.java b/src/main/java/lab07/Main.java index e7efdbb6ccb01f1b0aeedcd28531aca206d212e2..a824c5bbe1259ecc8b365108400b97fa35d6e4b3 100644 --- a/src/main/java/lab07/Main.java +++ b/src/main/java/lab07/Main.java @@ -2,10 +2,10 @@ package lab07; import java.io.IOException; import java.net.InetSocketAddress; -import java.net.URI; import java.net.URISyntaxException; import java.nio.channels.ServerSocketChannel; import java.nio.channels.SocketChannel; +import java.nio.file.Path; import java.nio.file.Paths; import lombok.extern.log4j.Log4j2; @@ -13,6 +13,10 @@ import lombok.extern.log4j.Log4j2; @Log4j2 public class Main { public static void main(String[] args) throws IOException, URISyntaxException { + int port = 8000; + String basePathName = ".."; + Path basePath = Paths.get(basePathName).toRealPath(); + log.info("Started server on http://localhost:{} with base path {}", port, basePath); HttpHandler handler = new HttpHandler(Paths.get(".")); ServerSocketChannel ssc = ServerSocketChannel.open(); ssc.bind(new InetSocketAddress(8000));