Skip to content

Commit b945051

Browse files
Sehun-Kimjavajigi
authored andcommitted
Step1 (#38)
* request line, headers * login // todo login 완성해야함 * content-type header set
1 parent 793027d commit b945051

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1107
-104
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Tue Jan 22 20:46:16 KST 2019
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
34
zipStoreBase=GRADLE_USER_HOME
45
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.4.1-all.zip

mvnw.cmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
@REM "License"); you may not use this file except in compliance
88
@REM with the License. You may obtain a copy of the License at
99
@REM
10-
@REM http://www.apache.org/licenses/LICENSE-2.0
10+
@REM webserver.http://www.apache.org/licenses/LICENSE-2.0
1111
@REM
1212
@REM Unless required by applicable law or agreed to in writing,
1313
@REM software distributed under the License is distributed on an

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="webserver.http://maven.apache.org/POM/4.0.0" xmlns:xsi="webserver.http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="webserver.http://maven.apache.org/POM/4.0.0 webserver.http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.nhnnext</groupId>
55
<artifactId>web-application-server</artifactId>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package controller;
2+
3+
public abstract class AbstractController {
4+
private static final String HTML_EXTENTION = ".html";
5+
private static final String CSS_DIRECTIVE = ",*/*;q=0.1";
6+
private static final String HTML_DIRECTIVE = ";charset=utf-8";
7+
8+
static String makeHtmlUrl(String uri) {
9+
if (uri.endsWith(HTML_EXTENTION))
10+
return uri;
11+
return uri + HTML_EXTENTION;
12+
}
13+
14+
static String makeContentType(String acceptType) {
15+
if (acceptType.endsWith("/css"))
16+
return acceptType + CSS_DIRECTIVE;
17+
return acceptType + HTML_DIRECTIVE;
18+
}
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package controller;
2+
3+
import webserver.http.request.HttpRequest;
4+
import webserver.http.response.HttpResponse;
5+
import util.HttpResponseUtils;
6+
7+
import java.io.IOException;
8+
9+
public class MainController extends AbstractController {
10+
public static void index(HttpRequest request, HttpResponse response) throws IOException {
11+
String uri = "/index.html";
12+
byte[] body = HttpResponseUtils.generateBody(uri);
13+
HttpResponseUtils.response200Header(response, body.length, makeContentType(request.getAcceptType()));
14+
HttpResponseUtils.responseBody(response, body);
15+
HttpResponseUtils.responseSend(response);
16+
}
17+
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package controller;
2+
3+
import util.HttpResponseUtils;
4+
import webserver.http.request.HttpRequest;
5+
import webserver.http.response.HttpResponse;
6+
7+
import java.io.IOException;
8+
9+
public class StyleSheetController extends AbstractController {
10+
public static void styleSheet(HttpRequest request, HttpResponse response) throws IOException {
11+
byte[] body = HttpResponseUtils.generateBody(request.getUri());
12+
HttpResponseUtils.response200Header(response, body.length, makeContentType(request.getAcceptType()));
13+
HttpResponseUtils.responseBody(response, body);
14+
HttpResponseUtils.responseSend(response);
15+
}
16+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package controller;
2+
3+
import db.DataBase;
4+
import exception.UnAuthenticationException;
5+
import model.User;
6+
import service.UserService;
7+
import util.ObjectMaker;
8+
import webserver.http.request.HttpRequest;
9+
import webserver.http.response.HttpResponse;
10+
import util.HttpResponseUtils;
11+
12+
import java.io.IOException;
13+
import java.util.List;
14+
15+
public class UserController extends AbstractController {
16+
public static void createForm(HttpRequest request, HttpResponse response) throws IOException {
17+
byte[] body = HttpResponseUtils.generateBody(makeHtmlUrl(request.getUri()));
18+
HttpResponseUtils.response200Header(response, body.length, makeContentType(request.getAcceptType()));
19+
HttpResponseUtils.responseBody(response, body);
20+
HttpResponseUtils.responseSend(response);
21+
}
22+
23+
public static void create(HttpRequest request, HttpResponse response) {
24+
User newUser = ObjectMaker.makeNewUser(request.getRequestBody());
25+
UserService.addUser(newUser);
26+
HttpResponseUtils.response302Header(response, "/");
27+
HttpResponseUtils.responseBody(response, new byte[]{});
28+
HttpResponseUtils.responseSend(response);
29+
}
30+
31+
public static void loginForm(HttpRequest request, HttpResponse response) throws IOException {
32+
byte[] body = HttpResponseUtils.generateBody(makeHtmlUrl(request.getUri()));
33+
HttpResponseUtils.response200Header(response, body.length, makeContentType(request.getAcceptType()));
34+
HttpResponseUtils.responseBody(response, body);
35+
HttpResponseUtils.responseSend(response);
36+
}
37+
38+
public static void loginForm_failed(HttpRequest request, HttpResponse response) throws IOException {
39+
byte[] body = HttpResponseUtils.generateBody(makeHtmlUrl(request.getUri()));
40+
HttpResponseUtils.response200Header(response, body.length, makeContentType(request.getAcceptType()));
41+
HttpResponseUtils.responseBody(response, body);
42+
HttpResponseUtils.responseSend(response);
43+
}
44+
45+
public static void login(HttpRequest request, HttpResponse response) {
46+
User loginUser = ObjectMaker.makeNewUser(request.getRequestBody());
47+
try {
48+
UserService.login(loginUser);
49+
HttpResponseUtils.response302Header(response, "/");
50+
HttpResponseUtils.responseCookieHeader(response, true);
51+
} catch (UnAuthenticationException e) {
52+
HttpResponseUtils.response302Header(response, "/user/login_failed.html");
53+
HttpResponseUtils.responseCookieHeader(response, false);
54+
} finally {
55+
HttpResponseUtils.responseSend(response);
56+
}
57+
}
58+
59+
public static void list(HttpRequest request, HttpResponse response) {
60+
if (request.isLogined()) {
61+
List<User> users = UserService.findAll();
62+
byte[] body = HttpResponseUtils.generateUsersBody(users);
63+
HttpResponseUtils.response200Header(response, body.length, makeContentType(request.getAcceptType()));
64+
HttpResponseUtils.responseBody(response, body);
65+
}
66+
if (!request.isLogined()) {
67+
HttpResponseUtils.response302Header(response, "/user/login.html");
68+
}
69+
HttpResponseUtils.responseSend(response);
70+
}
71+
}

src/main/java/db/DataBase.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collection;
44
import java.util.Map;
5+
import java.util.Optional;
56

67
import com.google.common.collect.Maps;
78

@@ -14,8 +15,8 @@ public static void addUser(User user) {
1415
users.put(user.getUserId(), user);
1516
}
1617

17-
public static User findUserById(String userId) {
18-
return users.get(userId);
18+
public static Optional<User> findUserById(String userId) {
19+
return Optional.ofNullable(users.get(userId));
1920
}
2021

2122
public static Collection<User> findAll() {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package exception;
2+
3+
public class UnAuthenticationException extends Exception {
4+
public UnAuthenticationException() {
5+
}
6+
7+
public UnAuthenticationException(String message) {
8+
super(message);
9+
}
10+
}

src/main/java/model/User.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public String getEmail() {
2929
return email;
3030
}
3131

32+
public boolean matchPassword(User loginUser) {
33+
return this.password.equals(loginUser.password);
34+
}
35+
3236
@Override
3337
public String toString() {
3438
return "User [userId=" + userId + ", password=" + password + ", name=" + name + ", email=" + email + "]";

0 commit comments

Comments
 (0)