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

[tomcat 구현 미션 - 현재 한 부분까지] #32

Open
wants to merge 124 commits into
base: GGH
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
124 commits
Select commit Hold shift + click to select a range
2c76bb2
refactor(http): remove `HttpRequest` class
KOKEONHO Nov 3, 2023
6820798
refactor(http): reorganize Http11Processor class for improved readabi…
KOKEONHO Nov 5, 2023
3d5306e
refactor(build): remove lombok dependency
KOKEONHO Nov 5, 2023
5b4e16d
feat(http): add BufferedReader to read lines from `inputStream`
KOKEONHO Nov 5, 2023
2718e82
feat(http): parse request line
KOKEONHO Nov 5, 2023
2c1a152
refactor(http): replace magic numbers by using constants
KOKEONHO Nov 5, 2023
4ea8df9
refactor(http): replace poor readability argument with constant
KOKEONHO Nov 5, 2023
63f790b
feat(http): add HttpMethod enum
KOKEONHO Nov 5, 2023
a1e9018
refactor(http): rename constant
KOKEONHO Nov 5, 2023
00cae42
refactor(http): add constants to improve readability
KOKEONHO Nov 5, 2023
f9b3249
refactor(http): add logs to check flow
KOKEONHO Nov 5, 2023
0dac2b9
feat(http): add parsing HTTP request headers
KOKEONHO Nov 5, 2023
41a83a5
feat(http): add parsing HTTP request body
KOKEONHO Nov 6, 2023
5097b61
refactor(http): correct `HEADER_VALUE`
KOKEONHO Nov 6, 2023
9376c03
style(http): add explanatory comments
KOKEONHO Nov 6, 2023
9cdde5a
feat(http): add HttpStatus enum
KOKEONHO Nov 6, 2023
7b15d14
refactor(http): use HttpMethod enum for request parsing
KOKEONHO Nov 6, 2023
82a15dd
feat(http): add response handling for GET HTTP method
KOKEONHO Nov 6, 2023
a91d6a9
feat(http): add `findResources` method that read static resources
KOKEONHO Nov 7, 2023
bf2d754
refactor(http): add logs to check flow
KOKEONHO Nov 7, 2023
8bedc88
feat(http): add HTTP response creation logic
KOKEONHO Nov 7, 2023
99a5f3a
feat(http): add ContentType enum
KOKEONHO Nov 7, 2023
fce34e8
refactor(http): remove response confirming log
KOKEONHO Nov 7, 2023
64726c7
feat(http): improve logic to determine MIME types dynamically
KOKEONHO Nov 7, 2023
1ac03dd
feat(http): add query parsing logic
KOKEONHO Nov 8, 2023
73a4543
feat(HttpRequest): add HttpRequest class
KOKEONHO Nov 8, 2023
c1ba78a
refactor(HttpRequest): rename HttpRequest class fields
KOKEONHO Nov 8, 2023
4d3e026
refactor(HttpRequest): change static factory method parameter to Buff…
KOKEONHO Nov 8, 2023
fcd4d13
refactor(HttpRequest): rename static factory method to `from`
KOKEONHO Nov 8, 2023
e384975
feat(HttpRequest): add `getRequestline` method to read request line
KOKEONHO Nov 8, 2023
70b9d3e
feat(HttpRequest): add `getRequestHeader` method to read request header
KOKEONHO Nov 8, 2023
3d3b387
feat(HttpRequest): add `getRequestBody` method to read request body
KOKEONHO Nov 8, 2023
3108ea2
refactor(HttpRequest): replace low readability code with constant
KOKEONHO Nov 8, 2023
525280c
feat(HttpRequest): complete static factory method by constructor code
KOKEONHO Nov 8, 2023
6ef3e71
feat(HttpRequest): add HttpRequest class fields
KOKEONHO Nov 8, 2023
a94e7ab
refactor(HttpRequest): remove useless class fields
KOKEONHO Nov 8, 2023
6251ee3
refactor(HttpRequest): remove initialization of removed class fields
KOKEONHO Nov 8, 2023
b4af063
refactor(HttpRequest): replace low readability code with constant
KOKEONHO Nov 8, 2023
8f570dc
feat(HttpRequest): add `requestHeaders` field
KOKEONHO Nov 8, 2023
98f3382
feat(HttpRequest): add `parseRequestLine` method
KOKEONHO Nov 8, 2023
43a25c7
feat(HttpRequest): add `parseRequestHeaders` method
KOKEONHO Nov 8, 2023
8c1b170
refactor(HttpRequest): replace low readability code with constant
KOKEONHO Nov 8, 2023
be057a5
refactor(HttpRequest): replace `httpMethod` with HttpMethod enum
KOKEONHO Nov 8, 2023
dc794bd
feat(HttpRequest): add `requestBody` field
KOKEONHO Nov 8, 2023
a2e7c3f
build(build.gradle): add lombok dependency
KOKEONHO Nov 10, 2023
e98a752
refactor(Http11Processor): extract HTTP request parsing to `HttpReque…
KOKEONHO Nov 10, 2023
537873f
feat(HttpRequest): add `@Getter` lombok annotation
KOKEONHO Nov 10, 2023
4a5eacd
feat(HttpRequest): add parsing method for transfer encoding
KOKEONHO Nov 10, 2023
5d3ac16
refactor(HttpRequest): update request body parsing method
KOKEONHO Nov 10, 2023
3dc34d9
refactor(HttpRequest): update request headers parsing method
KOKEONHO Nov 10, 2023
6234095
refactor(HttpRequest): replace low readability code with constant
KOKEONHO Nov 10, 2023
bcf1b1a
refactor(HttpRequest): complete `HttpRequest`constructor to use Buffe…
KOKEONHO Nov 10, 2023
432e63f
refactor(HttpRequest): replace low readability code with `isEmpty`method
KOKEONHO Nov 10, 2023
b72c360
refactor(HttpRequest): Initialize `requestBody` field
KOKEONHO Nov 10, 2023
1eb9993
refactor(HttpRequest): modify code to pass BufferedReader directly to…
KOKEONHO Nov 10, 2023
3d90368
refactor(HttpRequest): modify `parseRequestLine` method to read reque…
KOKEONHO Nov 10, 2023
fd1dbc8
refactor(HttpRequest): add import for java.util.Arrays
KOKEONHO Nov 10, 2023
e4ddc88
refactor(Http11Processor): remove resource mapping code
KOKEONHO Nov 10, 2023
70b3bce
refactor(Http11Processor): modify code to get `uri` from `HttpRequest`
KOKEONHO Nov 10, 2023
29372f0
refactor(Http11Processor): add logs to confirm normal operation
KOKEONHO Nov 10, 2023
bc51512
feat(HttpResponse): add `HttpResponse` class
KOKEONHO Nov 10, 2023
12dd7b7
feat(HttpResponse): add `@Getter` lombok annotation
KOKEONHO Nov 10, 2023
6d3c6d8
feat(RequestMapping): add custom annotation for dynamic controller ma…
KOKEONHO Nov 14, 2023
47cc025
feat(Controller): add `Controller` interface
KOKEONHO Nov 14, 2023
cbb8596
feat(Controller): add `handleRequest` method
KOKEONHO Nov 14, 2023
ef99510
refactor(RequestMapping): move `RequestMapping` custom annotation to …
KOKEONHO Nov 14, 2023
0579ee6
feat(ControllerScanner): add `ControllerScanner` class
KOKEONHO Nov 14, 2023
db09525
refactor(Controller): remove `Controller` class
KOKEONHO Nov 14, 2023
897e296
refactor(ContentType): replace `getMimeType` method with `@Getter` lo…
KOKEONHO Nov 14, 2023
c687897
refactor(Http11Processor): remove unnecessary constants
KOKEONHO Nov 15, 2023
c4a7b75
refactor(Http11Processor): remove unnecessary `findResources` method
KOKEONHO Nov 15, 2023
9c02586
refactor(Http11Processor): remove `response` creating part
KOKEONHO Nov 15, 2023
dde757e
refactor(Application): replace low readability code with constant
KOKEONHO Nov 15, 2023
c504091
feat(Application): add `ControllerScanner` instance to scan `@Request…
KOKEONHO Nov 15, 2023
2c4679e
refactor(Application): add logs to confirm normal operation
KOKEONHO Nov 15, 2023
d8e114a
refactor(Application): modify `ControllerScanner` instance creating code
KOKEONHO Nov 15, 2023
e640c6d
refactor(Http11Processor): add singleton `ControllerScanner` instance
KOKEONHO Nov 15, 2023
857bf75
refactor(RequestMapping): move `RequestMapping` custom annotation to …
KOKEONHO Nov 17, 2023
3344292
feat(Controller): add custom annotation for dynamic controller mapping
KOKEONHO Nov 17, 2023
2a5aff7
refactor(ControllerScanner): remove unnecessary `ControllerScanner` c…
KOKEONHO Nov 17, 2023
ef22820
feat(MethodScanner): add `MethodScanner` class to scan custom annotat…
KOKEONHO Nov 17, 2023
9a47eb6
feat(ControllerScanner): add `ControllerScanner` class to scan custom…
KOKEONHO Nov 17, 2023
7e5fe36
refactor(HttpRequest): rename package to lowercase
KOKEONHO Nov 17, 2023
61122e0
refactor(HttpResponse): rename package to lowercase
KOKEONHO Nov 17, 2023
62db3c9
refactor(common): move package
KOKEONHO Nov 17, 2023
24d59c8
refactor(HttpRequest): code modified due to package move
KOKEONHO Nov 17, 2023
c5a258a
feat(RequestHandler): add `RequestHandler` to handle parsed HTTP Request
KOKEONHO Nov 17, 2023
c64688c
feat(Application): initialize `controllerScanner` and `methodScanner`
KOKEONHO Nov 17, 2023
e5ae265
feat(Application): scan `@RequestMapping` annotated methods in contro…
KOKEONHO Nov 17, 2023
677aaa8
refactor(Application): add logs to confirm normal operation
KOKEONHO Nov 17, 2023
0d1ba94
feat(Http11Processor): initialize `HttpResponse`
KOKEONHO Nov 17, 2023
343ac0a
feat(Http11Processor): initialize `RequestHandler` with `httpRequest`…
KOKEONHO Nov 17, 2023
4caf248
feat(HttpResponse): add fields
KOKEONHO Nov 17, 2023
81624f5
feat(HttpResponse): declare default constructor
KOKEONHO Nov 17, 2023
b63c675
feat(HttpResponse): add `@Builder` lombok annotation
KOKEONHO Nov 17, 2023
2f9f414
feat(IndexPageController): add temporary controller class to confirm …
KOKEONHO Nov 17, 2023
ee06c5d
feat(RequestHandler): add fields
KOKEONHO Nov 17, 2023
ac20d43
feat(RequestHandler): add static factory method
KOKEONHO Nov 17, 2023
cf3f456
feat(GetMapping): add custom annotation to handle `GET` HTTP method
KOKEONHO Nov 17, 2023
2135590
feat(PostMapping): add custom annotation to handle `POST` HTTP method
KOKEONHO Nov 17, 2023
518702c
feat(MethodScanner): add `@Getter` lombok annotation
KOKEONHO Nov 17, 2023
906c3f1
feat(MethodScanner): divide `@RequestMapping` annotation into `@GetMa…
KOKEONHO Nov 17, 2023
6f22667
feat(MethodScanner): add `scanMapping` method to scan annotated metho…
KOKEONHO Nov 17, 2023
0e651d6
refactor(RequestHandler): divide `uriToMethodMap` into `uriToGetMetho…
KOKEONHO Nov 17, 2023
4b929b6
refactor(IndexPageController): replace `@RequestMapping` annotation w…
KOKEONHO Nov 17, 2023
d5258c2
refactor(Application): modify `scan` method to `scanMapping`
KOKEONHO Nov 17, 2023
b87ba85
refactor(RequestHandler): separate roles by adding `createMethodMap` …
KOKEONHO Nov 18, 2023
7b14007
refactor(HttpResponse): remove `@Builder` lombok annotation
KOKEONHO Nov 18, 2023
de729e3
refactor(Http11Processor): remove unnecessary comment
KOKEONHO Nov 18, 2023
1300cb6
refactor(RequestHandler): remove `httpResponse` field
KOKEONHO Nov 18, 2023
af7dae4
refactor(Application): modify method name by adding `MethodScanner` i…
KOKEONHO Nov 20, 2023
a2f7e08
refactor(Application): modify logs
KOKEONHO Nov 20, 2023
6166786
feat(MethodScanner): add `methodMapByHttpMethod` field
KOKEONHO Nov 21, 2023
fe4aaa3
feat(MethodScanner): add `methodMapByHttpMethod` field on constructor…
KOKEONHO Nov 21, 2023
466db35
refactor(MethodScanner): rename `scanMapping` method to `scanMethods`
KOKEONHO Nov 21, 2023
0de632c
refactor(MethodScanner): add static import
KOKEONHO Nov 21, 2023
fe5a747
feat(MethodScanner): add `mapMethodsWithHttpMethod` method for mapping
KOKEONHO Nov 21, 2023
bd6f30d
refactor(RequestHandler): integrate existing fields into `methodMapBy…
KOKEONHO Nov 21, 2023
188b57b
refactor(RequestHandler): remove unnecessary `createMethodMap` method
KOKEONHO Nov 21, 2023
f91214f
feat(RequestHandler): add `findMethod` method
KOKEONHO Nov 21, 2023
8cb19f6
feat(RequestHandler): add `invokeMethod` method
KOKEONHO Nov 21, 2023
551fcc5
refactor(Application): divide `createMethodMapByHttpMethod` method in…
KOKEONHO Nov 22, 2023
c809ee3
refactor(Http11Processor): create `RequestHandler` instance with stat…
KOKEONHO Nov 22, 2023
d6aa724
refactor(RequestHandler): add declaration of the return value of `inv…
KOKEONHO Nov 22, 2023
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
4 changes: 2 additions & 2 deletions tomcat/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ dependencies {

implementation 'org.apache.commons:commons-lang3:3.12.0'

implementation 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.20'
compileOnly 'org.projectlombok:lombok:1.18.20'

testImplementation 'org.assertj:assertj-core:3.22.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
Expand Down
21 changes: 15 additions & 6 deletions tomcat/src/main/java/nextstep/Application.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
package nextstep;

import org.apache.catalina.startup.Tomcat;
import org.apache.coyote.http11.scanner.ControllerScanner;
import org.apache.coyote.http11.scanner.MethodScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Application {

private static final Logger log = LoggerFactory.getLogger(Application.class);
private static final String BASE_PACKAGE = "org.apache.coyote.http11.controller";

public static void main(String[] args) {
log.info("web server start.");
final var tomcat = new Tomcat();
tomcat.start();
}
private static final Logger log = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
log.info("web server start.");
ControllerScanner controllerScanner = new ControllerScanner(BASE_PACKAGE);
MethodScanner methodScanner = MethodScanner.getInstance();
methodScanner.scanMethods(controllerScanner.getControllerClasses());
methodScanner.mapMethodsWithHttpMethod();
log.info("methodMapByHttpMethod: {}", methodScanner.getMethodMapByHttpMethod());
final var tomcat = new Tomcat();
tomcat.start();
}
}
59 changes: 30 additions & 29 deletions tomcat/src/main/java/org/apache/coyote/http11/Http11Processor.java
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
package org.apache.coyote.http11;

import nextstep.jwp.exception.UncheckedServletException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import org.apache.coyote.Processor;
import org.apache.coyote.http11.handler.RequestHandler;
import org.apache.coyote.http11.httprequest.HttpRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.Socket;
import nextstep.jwp.exception.UncheckedServletException;

public class Http11Processor implements Runnable, Processor {

private static final Logger log = LoggerFactory.getLogger(Http11Processor.class);
private static final Logger log = LoggerFactory.getLogger(Http11Processor.class);

private final Socket connection;

private final Socket connection;
public Http11Processor(final Socket connection) {
this.connection = connection;
}

public Http11Processor(final Socket connection) {
this.connection = connection;
}
@Override
public void run() {
process(connection);
}

@Override
public void run() {
process(connection);
}
@Override
public void process(final Socket connection) {
try (final var inputStream = connection.getInputStream();
final var outputStream = connection.getOutputStream()) {

@Override
public void process(final Socket connection) {
try (final var inputStream = connection.getInputStream();
final var outputStream = connection.getOutputStream()) {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

final var responseBody = "Hello world!";
HttpRequest httpRequest = HttpRequest.from(bufferedReader);
RequestHandler requestHandler = RequestHandler.of(httpRequest);

final var response = String.join("\r\n",
"HTTP/1.1 200 OK ",
"Content-Type: text/html;charset=utf-8 ",
"Content-Length: " + responseBody.getBytes().length + " ",
"",
responseBody);
outputStream.flush();
} catch (IOException | UncheckedServletException e) {
log.error(e.getMessage(), e);
}
}

outputStream.write(response.getBytes());
outputStream.flush();
} catch (IOException | UncheckedServletException e) {
log.error(e.getMessage(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.apache.coyote.http11.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Controller {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.apache.coyote.http11.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface GetMapping {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.apache.coyote.http11.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PostMapping {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.apache.coyote.http11.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
public @interface RequestMapping {
String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.apache.coyote.http11.common;

import java.util.Arrays;

import lombok.Getter;

public enum ContentType {

HTML("html", "text/html;charset=utf-8"),
CSS("css", "text/css;charset=utf-8"),
JS("js", "application/javascript;charset=utf-8"),
PNG("png", "image/png"),
JPG("jpg", "image/jpeg"),
JPEG("jpeg", "image/jpeg"),
GIF("gif", "image/gif"),
DEFAULT("", "text/plain;charset=utf-8");

private final String extension;
@Getter
private final String mimeType;

ContentType(String extension, String mimeType) {
this.extension = extension;
this.mimeType = mimeType;
}

public static ContentType findByExtension(String extension) {
return Arrays.stream(values())
.filter(mimeType -> mimeType.extension.equalsIgnoreCase(extension))
.findFirst()
.orElse(DEFAULT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.apache.coyote.http11.common;

public enum HttpMethod {

GET,
POST,
PUT,
DELETE;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.apache.coyote.http11.common;

public enum HttpStatus {

OK(200, "OK"),
CREATED(201, "Created"),
FOUND(302, "Found"),
NOT_FOUND(404, "Not Found"),
UNAUTHORIZED(401, "Unauthorized"),
INTERNAL_SERVER_ERROR(500, "Internal Server Erro"),
BAD_REQUEST(400, "Bad Request");

private final int code;
private final String status;

HttpStatus(int code, String status) {
this.code = code;
this.status = status;
}

public int getCode() {
return code;
}

public String getStatus() {
return status;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.apache.coyote.http11.controller;

import org.apache.coyote.http11.annotation.Controller;
import org.apache.coyote.http11.annotation.GetMapping;

@Controller
public class IndexPageController {

@GetMapping("/")
public String showIndexPage() {
return "hello world !!";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.apache.coyote.http11.handler;

import java.lang.reflect.Method;
import java.util.Map;

import org.apache.coyote.http11.common.HttpMethod;
import org.apache.coyote.http11.httprequest.HttpRequest;
import org.apache.coyote.http11.scanner.MethodScanner;

public class RequestHandler {

private HttpRequest httpRequest;
private Map<HttpMethod, Map<String, Method>> methodMapByHttpMethod;

private RequestHandler(HttpRequest httpRequest) {
this.httpRequest = httpRequest;
this.methodMapByHttpMethod = MethodScanner.getInstance().getMethodMapByHttpMethod();
}

public static RequestHandler of(HttpRequest httpRequest) {
return new RequestHandler(httpRequest);
}

private Method findMethod(HttpRequest httpRequest) {
Map<String, Method> methodsForHttpMethod = methodMapByHttpMethod.get(httpRequest.getHttpMethod());
return methodsForHttpMethod.get(httpRequest.getUri());
}

private void invokeMethod(Method method, HttpRequest httpRequest) throws Exception {
Class<?> controllerClass = method.getDeclaringClass();
Object controllerInstance = controllerClass.getDeclaredConstructor().newInstance();
Object invokedMethodResult = method.invoke(controllerInstance, httpRequest);

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.apache.coyote.http11.httprequest;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.coyote.http11.common.HttpMethod;

import lombok.Getter;

@Getter
public class HttpRequest {

private static final String CRLF = "\r\n";
private static final String REGEX_CRLF = "\\r?\\n";
private static final String BLANK_SPACE = " ";
private static final String CONTENT_LENGTH = "Content-Length";
private static final String TRANSFER_ENCODING = "Transfer-Encoding";
private static final String CHUNKED = "chunked";
private HttpMethod httpMethod;
private String uri;
private String httpVersion;
private Map<String, String> requestHeaders;
private String requestBody = null;

private HttpRequest(BufferedReader bufferedReader) throws IOException {
parseRequestLine(bufferedReader);
parseRequestHeaders(bufferedReader);
if (requestHeaders.containsKey(CONTENT_LENGTH)) {
parseContentLengthRequestBody(bufferedReader);
} else if (requestHeaders.containsKey(TRANSFER_ENCODING)) {
parseTransferEncodingRequestBody(bufferedReader);
}
}

public static HttpRequest from(BufferedReader bufferedReader) throws IOException {
return new HttpRequest(bufferedReader);
}

private static String getRequestLine(BufferedReader bufferedReader) throws IOException {
return bufferedReader.readLine();
}

private void parseRequestLine(BufferedReader bufferedReader) throws IOException {
String requestLine = getRequestLine(bufferedReader);
String[] splittedRequestLine = requestLine.split(BLANK_SPACE);
this.httpMethod = HttpMethod.valueOf(splittedRequestLine[0]);
this.uri = splittedRequestLine[1];
this.httpVersion = splittedRequestLine[2];
}

private static String getRequestHeader(BufferedReader bufferedReader) throws IOException {
StringBuilder requestHeaderBuilder = new StringBuilder();
String headerLine;
while ((headerLine = bufferedReader.readLine()) != null && !headerLine.isEmpty()) {
requestHeaderBuilder.append(headerLine).append(CRLF);
}
return requestHeaderBuilder.toString();
}

private void parseRequestHeaders(BufferedReader bufferedReader) throws IOException {
String requestHeader = getRequestHeader(bufferedReader);
this.requestHeaders = Arrays.stream(requestHeader.split(REGEX_CRLF))
.filter(line -> line.contains(":"))
.map(line -> line.split(":", 2))
.collect(Collectors.toMap(
arr -> arr[0].trim(),
arr -> arr[1].trim(),
(value1, value2) -> value1));
}

private void parseContentLengthRequestBody(BufferedReader bufferedReader) throws IOException {
int contentLength = Integer.parseInt(requestHeaders.get(CONTENT_LENGTH));
char[] charRequestBody = new char[contentLength];
int bytesRead = bufferedReader.read(charRequestBody, 0, contentLength);
this.requestBody = new String(charRequestBody, 0, bytesRead);
}

private void parseTransferEncodingRequestBody(BufferedReader bufferedReader) throws IOException {
StringBuilder requestBodyBuilder = new StringBuilder();
String chunkSizeLine;
while (!(chunkSizeLine = bufferedReader.readLine()).equals("0")) {
int chunkSize = Integer.parseInt(chunkSizeLine, 16);
char[] chunkData = new char[chunkSize];
int bytesRead = bufferedReader.read(chunkData, 0, chunkSize);
requestBodyBuilder.append(chunkData, 0, bytesRead);
bufferedReader.readLine();
}
bufferedReader.readLine();
this.requestBody = requestBodyBuilder.toString();
}
}
Loading