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
[테리_쿠킴] 웹서버 2 단계 - GET으로 회원가입 기능 구현 #20
Conversation
- 서버에서 InputStream in 을 통해 클라이언트 HTTP Request을 입력 받아 Request 객체를 생성한다. - Request에는 method, uri, version, queryStringMap, RequestHandlerMap으로 파싱된다.
- GET /user/create?queryString 의 예처럼 쿼리스트링으로 회원가입 포맷이 들어오면 해당 경로 uri 처리하며 User 생성 - queryString으로 들어온 값은 퍼센트 인코딩되어있기 때문에 HttpRequestUtils.parseQueryString에서 URLDecoder.decode 메서드를 사용해 디코드하여 한글 문제 해결 - HttpRequestUtilsTest.parseQueryString_korean 테스트 코드 추가 - User 생성 시 log.debug로 유저 생성 여부 로깅
- query string이 null일 때 NPE발생하여 비어있는 Map 리턴하도록 수정
- query string이 null일 때 NPE발생하여 비어있는 Map 리턴하도록 수정
- URLDecoder.decode()메서드 사용할 떄 "UTF-8" -> StandardCharsets.UTF_8 으로 변경하여 불필요한 예외처리 제거
- 간단한 GET 요청일 때 Request 객체 생성 여부 확인 - 쿼리파라미터가 있는 GET 요청일 때 Request 객체 생성 여부 확인
- Request(String requestLine, List<String> rawData) 생성자 구조 변경과 메서드 추출
매개변수 null일 때 NPE문제 발생과 해결 - query string이 null일 때 NPE발생하여 비어있는 Map 리턴하도록 수정
import util.HttpRequestUtils; | ||
import util.HttpRequestUtils.Pair; | ||
|
||
public class Request { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Request 를 추가했군요. 좋습니다.
private final Map<String, String> queryStringMap; | ||
private final Map<String, String> requestHeaderMap = new HashMap<>(); | ||
|
||
public Request(String requestLine, List<String> rawData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requestLine을 따로 분리한 이유가 있을까요? 처음부터 다 받아보면 어때요?
public void run() { | ||
log.debug("New Client Connect! Connected IP : {}, Port : {}", connection.getInetAddress(), | ||
connection.getPort()); | ||
|
||
try (InputStream in = connection.getInputStream(); | ||
OutputStream out = connection.getOutputStream()) { | ||
Request request = createRequest(in); | ||
|
||
byte[] body = null; | ||
if ("/user/create".equals(request.getUri())) { | ||
User user = new User(request.getParam("userId"), | ||
request.getParam("password"), | ||
request.getParam("name"), | ||
request.getParam("email")); | ||
log.debug("회원가입완료 {}",user ); | ||
body = Files.readAllBytes(new File("./webapp/" + "index.html").toPath()); | ||
} else { | ||
body = Files.readAllBytes(new File("./webapp/" + request.getUri()).toPath()); | ||
} | ||
|
||
DataOutputStream dos = new DataOutputStream(out); | ||
response200Header(dos, body.length); | ||
responseBody(dos, body); | ||
|
||
} catch (IOException e) { | ||
log.error(e.getMessage()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
run() 메소드의 역할을 나눠서 코드분리를 하면 좋을 것 같아요.
} | ||
} | ||
|
||
private Request createRequest(InputStream in) throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
줄 단위로 읽는 이 부분의 코드가 이후에는 개선되야 할 것 같은데 일단 지금 현재는 동작하니 괜찮습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2단계는 잘 돌아가도록 구현된 코드군요. 수고하셨습니다.
안녕하세요 쿠킴. 테리입니다.
2단계 요구사항 구현하여 PR보내드립니다.
미리 리뷰 감사합니다. 😀
구현, 요구 사항
클라이언트 HTTP 요청 메세지를 모두 읽은 뒤 이를 가지고 Request 객체를 만들었습니다.
응답에 대한 부분은 리팩토링 되어있지 않습니다. 다음 과제 진행하며 응답 부분도 구분하려 합니다.
현재 .html 파일에 필요한 css, js 파일 전송은 전송되지 않습니다.
결과
view
localhost:8080/index.html
localhost:8080/user/form.html
한글 이름 회원가입 (회원가입시 서버에 로그 확인 가능)
테스트