Compare commits
1 Commits
main
...
concurrenc
Author | SHA1 | Date |
---|---|---|
|
4c5fd51b88 |
88
Main.java
88
Main.java
|
@ -1,18 +1,17 @@
|
||||||
import java.io.BufferedReader;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
|
||||||
import java.io.OutputStream;
|
|
||||||
import java.net.ServerSocket;
|
import java.net.ServerSocket;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.nio.file.Files;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.nio.file.Path;
|
import java.util.concurrent.Executors;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.ArrayList;
|
import workers.HandleClient;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
ExecutorService executor = Executors.newFixedThreadPool(3);
|
||||||
|
|
||||||
int attempts = 1;
|
int attempts = 1;
|
||||||
|
|
||||||
try (ServerSocket serverSocket = new ServerSocket(8080)) {
|
try (ServerSocket serverSocket = new ServerSocket(8080)) {
|
||||||
|
@ -20,7 +19,8 @@ public class Main {
|
||||||
while (true) {
|
while (true) {
|
||||||
// .accept() blocks until we have a client
|
// .accept() blocks until we have a client
|
||||||
try (Socket client = serverSocket.accept()) {
|
try (Socket client = serverSocket.accept()) {
|
||||||
handleClient(client);
|
HandleClient task = new HandleClient(client);
|
||||||
|
executor.submit(task);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.printf("Error accepting client: %s\n", e);
|
System.out.printf("Error accepting client: %s\n", e);
|
||||||
}
|
}
|
||||||
|
@ -38,74 +38,4 @@ public class Main {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void handleClient(Socket client) throws IOException {
|
|
||||||
|
|
||||||
System.out.println("Debug: got new client" + client.toString());
|
|
||||||
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
|
||||||
|
|
||||||
StringBuilder requestBuilder = new StringBuilder();
|
|
||||||
String line;
|
|
||||||
while (!(line = br.readLine()).isBlank()) {
|
|
||||||
requestBuilder.append(line + "\r\n");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
String request = requestBuilder.toString();
|
|
||||||
// System.out.println(request);
|
|
||||||
// PARSE THE REQUEST
|
|
||||||
String[] requestLines = request.split("\r\n");
|
|
||||||
String[] requestLine = requestLines[0].split(" ");
|
|
||||||
String method = requestLine[0];
|
|
||||||
String path = requestLine[1];
|
|
||||||
String version = requestLine[2];
|
|
||||||
String host = requestLines[1].split(" ")[1];
|
|
||||||
|
|
||||||
List<String> headers = new ArrayList<>();
|
|
||||||
// we start the loop from 2 because 0 is GET / HTTP/1.1 and 1 is the host
|
|
||||||
for (int h = 2; h < requestLines.length; h++) {
|
|
||||||
String header = requestLines[h];
|
|
||||||
headers.add(header);
|
|
||||||
}
|
|
||||||
|
|
||||||
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
|
|
||||||
client.toString(),
|
|
||||||
method, path, version, host, headers.toString());
|
|
||||||
System.out.println(accessLog);
|
|
||||||
|
|
||||||
Path filepath = getFilePath(path);
|
|
||||||
if (Files.exists(filepath)) {
|
|
||||||
// file exist
|
|
||||||
String contentType = guessContentType(filepath);
|
|
||||||
sendResponse(client, "200 OK", contentType, Files.readAllBytes(filepath));
|
|
||||||
} else {
|
|
||||||
// 404
|
|
||||||
sendResponse(client, "404 Not Found", "text/html", Files.readAllBytes(getFilePath("404.html")));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void sendResponse(Socket client, String status, String contentType, byte[] content)
|
|
||||||
throws IOException {
|
|
||||||
OutputStream clientOutput = client.getOutputStream();
|
|
||||||
clientOutput.write("HTTP/1.1 200 OK\r\n".getBytes());
|
|
||||||
clientOutput.write("ContentType: text/html\r\n".getBytes());
|
|
||||||
clientOutput.write("\r\n".getBytes());
|
|
||||||
clientOutput.write(content);
|
|
||||||
// These (windows) newlines indicate that the response is over
|
|
||||||
clientOutput.write("\r\n\r\n".getBytes());
|
|
||||||
clientOutput.flush();
|
|
||||||
client.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String guessContentType(Path filepath) throws IOException {
|
|
||||||
return Files.probeContentType(filepath);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Path getFilePath(String path) {
|
|
||||||
if ("/".equals(path)) {
|
|
||||||
path = "/index.html";
|
|
||||||
}
|
|
||||||
return Paths.get("/home/andrzej/dev/java-http", path);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
|
@ -0,0 +1,93 @@
|
||||||
|
package workers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Callable;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
import java.net.Socket;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class HandleClient implements Callable<Void> {
|
||||||
|
|
||||||
|
private Socket client;
|
||||||
|
|
||||||
|
public HandleClient(Socket c) {
|
||||||
|
client = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Void call() throws IOException {
|
||||||
|
System.out.println("Debug: got new client" + client.toString());
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
|
||||||
|
|
||||||
|
StringBuilder requestBuilder = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while (!(line = br.readLine()).isBlank()) {
|
||||||
|
requestBuilder.append(line + "\r\n");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
String request = requestBuilder.toString();
|
||||||
|
// System.out.println(request);
|
||||||
|
// PARSE THE REQUEST
|
||||||
|
String[] requestLines = request.split("\r\n");
|
||||||
|
String[] requestLine = requestLines[0].split(" ");
|
||||||
|
String method = requestLine[0];
|
||||||
|
String path = requestLine[1];
|
||||||
|
String version = requestLine[2];
|
||||||
|
String host = requestLines[1].split(" ")[1];
|
||||||
|
|
||||||
|
List<String> headers = new ArrayList<>();
|
||||||
|
// we start the loop from 2 because 0 is GET / HTTP/1.1 and 1 is the host
|
||||||
|
for (int h = 2; h < requestLines.length; h++) {
|
||||||
|
String header = requestLines[h];
|
||||||
|
headers.add(header);
|
||||||
|
}
|
||||||
|
|
||||||
|
String accessLog = String.format("Client %s, method %s, path %s, version %s, host %s, headers %s",
|
||||||
|
client.toString(),
|
||||||
|
method, path, version, host, headers.toString());
|
||||||
|
System.out.println(accessLog);
|
||||||
|
|
||||||
|
Path filepath = getFilePath(path);
|
||||||
|
if (Files.exists(filepath)) {
|
||||||
|
// file exist
|
||||||
|
String contentType = guessContentType(filepath);
|
||||||
|
sendResponse(client, "200 OK", contentType, Files.readAllBytes(filepath));
|
||||||
|
} else {
|
||||||
|
// 404
|
||||||
|
sendResponse(client, "404 Not Found", "text/html",
|
||||||
|
Files.readAllBytes(getFilePath("404.html")));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sendResponse(Socket client, String status, String contentType, byte[] content)
|
||||||
|
throws IOException {
|
||||||
|
OutputStream clientOutput = client.getOutputStream();
|
||||||
|
clientOutput.write("HTTP/1.1 200 OK\r\n".getBytes());
|
||||||
|
clientOutput.write("ContentType: text/html\r\n".getBytes());
|
||||||
|
clientOutput.write("\r\n".getBytes());
|
||||||
|
clientOutput.write(content);
|
||||||
|
// These (windows) newlines indicate that the response is over
|
||||||
|
clientOutput.write("\r\n\r\n".getBytes());
|
||||||
|
clientOutput.flush();
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String guessContentType(Path filepath) throws IOException {
|
||||||
|
return Files.probeContentType(filepath);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Path getFilePath(String path) {
|
||||||
|
if ("/".equals(path)) {
|
||||||
|
path = "/index.html";
|
||||||
|
}
|
||||||
|
return Paths.get("/home/andrzej/dev/java-http", path);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue