Skip to content

Instantly share code, notes, and snippets.

@mmitou
Created December 20, 2016 08:00
Show Gist options
  • Save mmitou/ee192c0e34e0a287e586b5c445110608 to your computer and use it in GitHub Desktop.
Save mmitou/ee192c0e34e0a287e586b5c445110608 to your computer and use it in GitHub Desktop.
httpdのようなもの
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Stream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.charset.StandardCharsets;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
public class Main {
private static final String DOCUMENT_ROOT = "./";
private static List<String> readLines(InputStream input)
throws IOException {
StringBuilder builder = new StringBuilder();
List<String> result = new ArrayList<String>();
for(int c; (c = input.read()) != -1; ) {
if (c == '\r') {
// ignore \r
} else if (c == '\n') {
if (builder.length() == 0) {
break;
} else {
String str = builder.toString();
result.add(str);
builder = new StringBuilder();
}
} else {
builder.append((char)c);
}
}
if (builder.length() != 0) {
result.add(builder.toString());
}
return result;
}
private static String getCurrentDateUtc() {
return ZonedDateTime.now(ZoneOffset.UTC)
.format(DateTimeFormatter
.ofPattern("EEE, dd MMM yyy HH:m:ss", Locale.US))
+ " GMT";
}
public static Path requestedFilePath(List<String> httpRequestHeaderLines) {
String getLine = httpRequestHeaderLines.stream()
.filter(line -> line.startsWith("GET"))
.findAny()
.get();
return Paths.get(DOCUMENT_ROOT + getLine.split(" ")[1]);
}
public static void writeLine(OutputStream output, String line) throws IOException {
for(int c : (line + "\r\n").toCharArray()) {
output.write(c);
}
}
public static void sendResponseHeader(OutputStream output) throws IOException {
List<String> headerLines = Arrays.asList("HTTP/1.1 200 OK",
"Date: " + getCurrentDateUtc(),
"Server: phttpd/0.1",
"Connection: close",
"Content-type: text/html",
"");
for(String line : headerLines) {
writeLine(output, line);
}
}
public static void sendResponseBody(OutputStream output, Path path) throws IOException {
BufferedReader input = Files.newBufferedReader(path, StandardCharsets.UTF_8);
for(int c; (c = input.read()) != -1; ) {
output.write(c);
}
}
public static void main(String[] args) throws Exception {
try (ServerSocket server = new ServerSocket(8001);) {
System.out.println("server start...");
try (Socket socket = server.accept();) {
System.out.println("accepted...");
InputStream input = socket.getInputStream();
List<String> httpRequestHeaderLines = readLines(input);
Path path = requestedFilePath(httpRequestHeaderLines);
System.out.println(path);
OutputStream output = socket.getOutputStream();
sendResponseHeader(output);
sendResponseBody(output, path);
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment