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

feat: 범수 과제 #15

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions assignment/network/http/simple_webserver/beomsu/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import java.io.IOException;
import java.net.ServerSocket;

public class Main {

public static void main(String[] args) throws IOException {
Server server = new Server();

server.run();
}
}
129 changes: 129 additions & 0 deletions assignment/network/http/simple_webserver/beomsu/Server.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import java.io.*;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.*;

public class Server {
private static final int BUFFER_SIZE = 1024;
private static final int PORT = 80;

private Selector selector;
private ByteBuffer buffer;

public Server() throws IOException {
selector = Selector.open();
buffer = ByteBuffer.allocate(BUFFER_SIZE);
}

public void run() throws IOException {
ServerSocketChannel serverChannel = ServerSocketChannel.open();
serverChannel.socket().bind(new InetSocketAddress(PORT));
serverChannel.configureBlocking(false);
System.out.println(PORT + "포트에 서버 소켓이 할당되었습니다.");

serverChannel.register(selector, SelectionKey.OP_ACCEPT);

while (true) {
int readyChannels = selector.select();
if (readyChannels == 0) {
continue;
}

Set<SelectionKey> selectedKeys = selector.selectedKeys();
Iterator<SelectionKey> keyIterator = selectedKeys.iterator();

while (keyIterator.hasNext()) {
SelectionKey key = keyIterator.next();

if (key.isAcceptable()) {
handleAccept(serverChannel);
} else if (key.isReadable()) {
handleRead(key);
}

keyIterator.remove();
}
}
}

private void handleAccept(ServerSocketChannel serverChannel) throws IOException {
SocketChannel clientChannel = serverChannel.accept();
clientChannel.configureBlocking(false);
clientChannel.register(selector, SelectionKey.OP_READ);
}

private void handleRead(SelectionKey key) throws IOException {
SocketChannel clientChannel = (SocketChannel) key.channel();

buffer.clear();
int bytesRead = clientChannel.read(buffer);

if (bytesRead == -1) {
clientChannel.close();
key.cancel();
return;
}

buffer.flip();
byte[] data = new byte[buffer.remaining()];
buffer.get(data);

String request = new String(data);
String[] requestLines = request.split("\n");
String[] tokenizedStartLine = requestLines[0].split(" ");

if (tokenizedStartLine[1].equals("/favicon.ico")) {
return;
}

String response = sendResponse(tokenizedStartLine[1]);
clientChannel.write(ByteBuffer.wrap(response.getBytes()));
clientChannel.close();
}

private String sendResponse(String endpoint) throws IOException {

StringBuilder fileContent = new StringBuilder();
addResponseHeader(fileContent);

if (endpoint.equals("/")) {
return generateResponse(fileContent, "/article1");
}

if (
endpoint.equals("/article2") ||
endpoint.equals("/article3")
) {
return generateResponse(fileContent, endpoint);
}

throw new IllegalArgumentException("요청받은 리소스는 존재하지 않습니다.");
}

private void addResponseHeader(StringBuilder fileContent) {
fileContent.append("""
HTTP/1.1 200 OK\r
Content-Type: text/html\r
\r
""");
}

private String generateResponse(StringBuilder fileContent, String endpoint) throws IOException {
BufferedReader reader = new BufferedReader(
new FileReader(String.format("src/resource%s.html", endpoint))
);

String line;
while ((line = reader.readLine()) != null) {
fileContent.append(line);
}

reader.close();

return fileContent.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>안녕하세요 나는 범수</title>
</head>
<body>
<h1>안녕하세요 누구냐고 묻는다면 나는 범수</h1>
<div>
<img style="width: 400px; height: 400px;" src="https://i.namu.wiki/i/uIt7OBpwNR2Cgk90eW_s_0iAZ6628xqGiRY1YyG5drpYaFwXo4ZAKKLltMDxLc2qPyky0s6D9bociJ770v2mwA.webp" />
</div>
<a href="https://github.com/shon5544">github 프로필. 이것은 내 것. 게시글 말고 내 프로필을 보세요.</a>
<br>
<a href="/article2">article2</a>
<br>
<a href="/article3">article3</a>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>안녕하세요 나는 범수</title>
</head>
<body>
<h1>안녕하세요 누구냐고 묻는다면 나는 범수</h1>
<div>
<img style="width: 400px; height: 400px;" src="https://i.namu.wiki/i/uIt7OBpwNR2Cgk90eW_s_0iAZ6628xqGiRY1YyG5drpYaFwXo4ZAKKLltMDxLc2qPyky0s6D9bociJ770v2mwA.webp" />
</div>
<a href="https://github.com/shon5544">여기는 아티클 2!! github 프로필. 이것은 내 것. 게시글 말고 내 프로필을 보세요.</a>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>안녕하세요 나는 범수</title>
</head>
<body>
<h1>안녕하세요 누구냐고 묻는다면 나는 범수</h1>
<div>
<img style="width: 400px; height: 400px;" src="https://i.namu.wiki/i/uIt7OBpwNR2Cgk90eW_s_0iAZ6628xqGiRY1YyG5drpYaFwXo4ZAKKLltMDxLc2qPyky0s6D9bociJ770v2mwA.webp" />
</div>
<a href="https://github.com/shon5544">여기는 아티클 3!! github 프로필. 이것은 내 것. 게시글 말고 내 프로필을 보세요.</a>
</body>
</html>