forked from softeerbootcamp-7th/be-was
-
Notifications
You must be signed in to change notification settings - Fork 0
[Http] HttpRequest.body 관련 기능 개발 #35
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
83af90f
Merge pull request #33 from codingbaraGo/refactor/web/renderer/#31
codingbaraGo ce29a81
feat(http): HttpRequest.body 변환 converter 개발
codingbaraGo 7b342a5
refactor(http): Query parameter에 대한 책임 이전 (http -> web)
codingbaraGo 84fccc4
refactor(web): 패키지 분리
codingbaraGo a858bcd
fix(web): NPE 취약점 제거
codingbaraGo b83aa04
fix(http): RequestConverter의 UnhandledException 처리
codingbaraGo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/http/request/BufferedReaderHttpRequestConverter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package http.request; | ||
|
|
||
| import exception.ErrorException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.*; | ||
| import java.net.Socket; | ||
|
|
||
| public class BufferedReaderHttpRequestConverter implements HttpRequestConverter { | ||
| private static final Logger logger = LoggerFactory.getLogger(BufferedReaderHttpRequestConverter.class); | ||
| public HttpRequest parseRequest(Socket connection){ | ||
|
|
||
| try { | ||
| BufferedReader bufferedReader = new BufferedReader( | ||
| new InputStreamReader( | ||
| connection.getInputStream())); | ||
|
|
||
| HttpRequest request = HttpRequest.from(bufferedReader.readLine()); | ||
| request.setRequestAddress(connection.getInetAddress()); | ||
|
|
||
| setHeaders(bufferedReader, request); | ||
|
|
||
| //TODO: Body 파싱 추가 | ||
|
|
||
| return request; | ||
|
|
||
| } catch (IOException e) { | ||
| throw new ErrorException("HttpRequestParseError: IO Exception", e); | ||
| } | ||
| } | ||
|
|
||
| private void setHeaders(BufferedReader bufferedReader, HttpRequest request) throws IOException { | ||
| String line; | ||
| while ((line = bufferedReader.readLine())!= null) { | ||
| if(line.isEmpty()) break; | ||
| int idx = line.indexOf(':'); | ||
| if(idx<0) throw new ErrorException("HttpRequestHeaderParseError"); | ||
| request.setHeader(line.substring(0, idx).strip(), line.substring(idx+1).strip()); | ||
| } | ||
| } | ||
| } | ||
44 changes: 0 additions & 44 deletions
44
src/main/java/http/request/HttpBufferedReaderRequestConverter.java
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,33 +5,47 @@ | |
| import exception.ServiceException; | ||
| import http.HttpMethod; | ||
|
|
||
| import java.io.UnsupportedEncodingException; | ||
| import java.net.InetAddress; | ||
| import java.net.URI; | ||
| import java.net.URLDecoder; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| public class HttpRequest { | ||
| private final HttpMethod method; | ||
| private final Map<String, String> headers; | ||
| private final URI uri; | ||
| private final Map<String, String> headers; //TODO: Map<String, List<String>> 타입으로 변경 | ||
| private String httpVersion; | ||
| private final URI uri; | ||
| private String contentType; | ||
|
|
||
| //TODO: Map<String, List<String>> 타입으로 변경 | ||
| private Map<String, String> queryMap; | ||
|
|
||
| private byte[] body; | ||
|
|
||
| private InetAddress requestAddress; | ||
|
|
||
| private HttpRequest (HttpMethod method, | ||
| String target, | ||
| String httpVersion) { | ||
| this.method = method; | ||
| this.uri = URI.create(target); | ||
| this.httpVersion = httpVersion; | ||
| this.headers = new HashMap<>(); | ||
| } | ||
|
|
||
| public byte[] getBody() { | ||
| return body; | ||
| } | ||
|
|
||
| public String getContentType() { | ||
| return contentType; | ||
| } | ||
| public String getHeader(String key){ | ||
| return headers.get(key.toLowerCase()); | ||
| } | ||
|
|
||
| public void setHeader(String key, String value){ | ||
|
Comment on lines
+39
to
44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Content-Type 설정 로직의 일관성 문제
개선 방안: |
||
| headers.put(key.toLowerCase(), value); | ||
| if (key.equalsIgnoreCase("Content-Type")) { | ||
| this.contentType = value; | ||
| } | ||
| } | ||
|
|
||
| public List<String> getHeaders(){ | ||
|
|
@@ -46,31 +60,10 @@ public String getQueryString(){ | |
| return uri.getQuery(); | ||
| } | ||
|
|
||
| public List<String> getQueryKeys(){ | ||
| return queryMap.keySet().stream().toList(); | ||
| } | ||
|
|
||
| public Optional<String> getQueryValue(String key){ | ||
| if (queryMap == null) { | ||
| queryMap = parseQueryToMap(uri.getQuery()); | ||
| } | ||
| return Optional.ofNullable(queryMap.get(key)); | ||
| } | ||
|
|
||
| public HttpMethod getMethod(){ | ||
| return this.method; | ||
| } | ||
|
|
||
| private HttpRequest (HttpMethod method, | ||
| String target, | ||
| String httpVersion) { | ||
| this.method = method; | ||
| this.uri = URI.create(target); | ||
| this.httpVersion = httpVersion; | ||
| this.headers = new HashMap<>(); | ||
|
|
||
| } | ||
|
|
||
| public void setRequestAddress(InetAddress requestAddress) { | ||
| this.requestAddress = requestAddress; | ||
| } | ||
|
|
@@ -93,33 +86,7 @@ public static HttpRequest from(String requestLine){ | |
| } | ||
| } | ||
|
|
||
|
|
||
| private Map<String, String> parseQueryToMap(String queryString) { | ||
| Map<String, String> map = new HashMap<>(); | ||
| if (queryString == null || queryString.isBlank()) { | ||
| return map; | ||
| } | ||
|
|
||
| String[] pairs = queryString.strip().split("&"); | ||
| for (String pair : pairs) { | ||
| if (pair.isEmpty()) continue; | ||
|
|
||
| String[] kv = pair.split("=", 2); // value에 '=' 들어가도 OK | ||
| String rawKey = kv[0]; | ||
| String rawValue = kv.length == 2 ? kv[1] : ""; | ||
|
|
||
| String key = urlDecode(rawKey); | ||
| String value = urlDecode(rawValue); | ||
| map.put(key, value); | ||
| } | ||
| return map; | ||
| } | ||
|
|
||
| private String urlDecode(String s) { | ||
| try { | ||
| return URLDecoder.decode(s, "UTF-8"); | ||
| } catch (UnsupportedEncodingException e) { | ||
| throw new IllegalStateException("UTF-8 not supported", e); | ||
| } | ||
| public void setBody(byte[] body){ | ||
| this.body = body; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
일관되지 않은 에러 처리
BufferedReaderHttpRequestConverter는 body 파싱을 구현하지 않았으면서InputStreamHttpRequestConverter는 전체 body를 파싱합니다. 두 converter 중 어느 것을 실제 사용하는지(AppConfig에서inputStreamHttpRequestConverter()로 설정) 명확하지 않으면 향후 유지보수 시 혼동이 발생할 수 있습니다.BufferedReaderHttpRequestConverter는 더 이상 필요 없다면 제거하거나, 명확한 용도 구분을 문서화하세요."