Skip to content

Commit

Permalink
[#3] feat: ☁️ CSS 파일을 로딩할 수 있도록 한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
tmdgusya committed Mar 30, 2021
1 parent e368189 commit 05404c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/main/java/handler/returnValueHandle/ReturnValueHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,27 @@ public void handle() throws IOException {
}
}

public static void responseStaticFile(DataOutputStream dos, String url) throws IOException {
byte[] body = Files.readAllBytes(new File(PREFIX + url).toPath());
try {
dos.writeBytes("HTTP/1.1 200 OK \r\n");
dos.writeBytes("Content-Type: text/css;charset=utf-8 \r\n");
dos.writeBytes(String.format("Content-Length: %d \r\n", body.length));
dos.writeBytes("\r\n");
dos.write(body, 0, body.length);
dos.flush();
} catch (IOException e) {
log.error(e.getMessage());
}
}

private void response302HeaderWithCookies(DataOutputStream dos, String location, String cookie) {
try {
dos.writeBytes("HTTP/1.1 302 \r\n");
dos.writeBytes(String.format("Location: %s \r\n", location));
dos.writeBytes(String.format("Set-Cookie: %s \r\n", cookie));
dos.writeBytes("\r\n");

} catch (IOException e) {
log.error(e.getMessage());
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/webserver/WebCat.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public void run() {
HttpResponse httpResponse = new HttpResponse(out);
MappingUrlHandler mappingUrlHandler = new MappingUrlHandler(httpRequest.getMethod(), httpRequest.getUrl());
DataOutputStream dos = new DataOutputStream(out);
if(httpRequest.getUrl().contains(".")) {
ReturnValueHandler.responseStaticFile(dos, httpRequest.getUrl());
}
try {
Object returnValue = mappingUrlHandler.invokeMethod(httpRequest, httpResponse);
//TODO 여기서 returnValue 를 resolve 하는 방식을 찾아야 한다.
ReturnValueHandler returnValueHandler = new ReturnValueHandler(returnValue, dos);
returnValueHandler.handle();
} catch (IllegalAccessException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException | InstantiationException e) {
Expand Down

0 comments on commit 05404c9

Please sign in to comment.