http request 요청을 보낸 후 status가 pending 상태임
public class ParsingUtil {
public Request parseRequest(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String requestLine = reader.readLine();
String[] requestParts = requestLine.split(" ");
String method = requestParts[0];
String path = requestParts[1];
String contentType = "";
int contentLength = 0;
String line;
while ((line = reader.readLine()) != null && !line.isEmpty()) {
if (line.startsWith("Content-Type: ")) {
contentType = line.substring("Content-Type: ".length());
} else if (line.startsWith("Content-Length: ")) {
contentLength = Integer.parseInt(line.substring("Content-Length: ".length()));
}
}
RequestHeader requestHeader = new RequestHeader(method, path, contentType, contentLength);
StringBuilder bodyBuilder = new StringBuilder();
while ((line = reader.readLine()) != null) {
bodyBuilder.append(line).append("\r\n");
}
RequestBody requestBody = new RequestBody(bodyBuilder.toString());
return new Request(requestHeader, requestBody);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
while ((line = reader.readLine()) != null) {
bodyBuilder.append(line).append("\r\n");
}
readLine(): 더 이상 읽어드릴 값이 없다면 null 반환(Stream의 끝에 도달한 경우)
ex) 빈줄, 공백을 읽어드릴 경우 ""를 반환한다. null을 반환하지 않는다.
public class ParsingUtil {
public Request parseRequest(InputStream inputStream) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String requestLine = reader.readLine();
String[] requestParts = requestLine.split(" ");
String method = requestParts[0];
String path = requestParts[1];
String contentType = "";
int contentLength = 0;
String line;
while ((line = reader.readLine()) != null && !line.isEmpty()) {
if (line.startsWith("Content-Type: ")) {
contentType = line.substring("Content-Type: ".length());
} else if (line.startsWith("Content-Length: ")) {
contentLength = Integer.parseInt(line.substring("Content-Length: ".length()));
}
}
RequestHeader requestHeader = new RequestHeader(method, path, contentType, contentLength);
StringBuilder bodyBuilder = new StringBuilder();
if(contentLength> 0) {
while ((line = reader.readLine()) != null && !line.isEmpty()) {
bodyBuilder.append(line).append("\r\n");
}
}
RequestBody requestBody = new RequestBody(bodyBuilder.toString());
return new Request(requestHeader, requestBody);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}