Skip to content

HTTP 웹 서버 리펙토링 #6

Description

@mjj111

📄 구현 포인트

  • 요청과 응답에 대한 로직을 클래스로 분리하여 메서드를 호출할 수 있도록 변경
  • 요청 값 관련 로직을 담은 가진 클래스 구현
  • 응답 로직 책임을 가진 클래스 구현
  • 요청 로직 책임을 가진 클래스 구현
  • 다형성을 이용해 분기 삭제

✅ 간략한 코드 설명

  1. 핸들러에서는 분기점만 갖도록, 요청 로직 책임을 가진 클래스 구현합니다.
  public HttpRequest(final InputStream in) {
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(in, "UTF-8"));
            String line = br.readLine();
            if (line == null) {
                return;
            }

            requestLine = new RequestLine(line);
  1. 요청으로 들어온 Line들에 대해 메서드에 따라 값을 파싱하고 반환하는 클래스를 구현합니다.
       if(HttpRequestMethod.POST.equals(method)) {
            path = tokens[1];
            return;
        }

        int index = tokens[1].indexOf("?");
        if(index == 1) {
            path = tokens[1];
        }else {
            path = tokens[1].substring(0, index);
            params = HttpRequestUtils.parseQueryString(tokens[1].substring(index + 1));
        }
  1. 핸들러에서는 분기점만 갖도록, 따로 응답 로직 책임을 가진 클래스 구현합니다.,
    public void sendResponse(final String response) {
    private void responseBody(final DataOutputStream dos, final byte[] body) {
    private void response200Header(final DataOutputStream dos, final int length) {
  1. 요청과 응답에 대한 로직을 클래스로 분리하여 Handler에서는 분기만 적용하도록 합니다.
    이에 따라 요청과 응답에 대한 로직은 다른 클래스에게 위임되어 코드가 줄어듭니다.
image
  1. 다형성을 이용해 분기삭제합니다.
           String path = request.getPath();

            Controller controller = RequestMapping.getController(path);
            if(controller == null) {
                response.sendResponse("안녕하세요!");
            }
            else {
                controller.service(request, response);
            }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions