diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..d29df2b --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,31 @@ +# This workflow will build a Java project with Maven, and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-maven + +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. + +name: Java CI with Maven + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + - name: Set up JDK 25 + uses: actions/setup-java@v4 + with: + java-version: '25' + distribution: 'temurin' + cache: maven + - name: Build with Maven + run: mvn -B package --file pom.xml diff --git a/src/main/java/com/imanimen/jttpserver/http/HttpParser.java b/src/main/java/com/imanimen/jttpserver/http/HttpParser.java index 2e230bc..ccce8a9 100644 --- a/src/main/java/com/imanimen/jttpserver/http/HttpParser.java +++ b/src/main/java/com/imanimen/jttpserver/http/HttpParser.java @@ -23,6 +23,8 @@ public HttpRequest parseHttpRequest(InputStream inputStream) throws IOException parseRequestLine(reader, request); } catch (IOException e) { e.printStackTrace(); + } catch (HttpParsingException e) { + throw new RuntimeException(e); } parseHeaders(reader, request); parseBody(reader, request); @@ -30,7 +32,7 @@ public HttpRequest parseHttpRequest(InputStream inputStream) throws IOException return request; } - private void parseRequestLine(InputStreamReader reader, HttpRequest request) throws IOException { + private void parseRequestLine(InputStreamReader reader, HttpRequest request) throws IOException, HttpParsingException { StringBuilder processingDataBuffer = new StringBuilder(); boolean methodParsed = false; diff --git a/src/main/java/com/imanimen/jttpserver/http/HttpRequest.java b/src/main/java/com/imanimen/jttpserver/http/HttpRequest.java index 02a177f..643ae71 100644 --- a/src/main/java/com/imanimen/jttpserver/http/HttpRequest.java +++ b/src/main/java/com/imanimen/jttpserver/http/HttpRequest.java @@ -14,8 +14,16 @@ public HttpMethod getMethod() { return method; } - public void setMethod(String methodName) { - this.method = HttpMethod.valueOf(methodName); + public void setMethod(String methodName) throws HttpParsingException{ + for (HttpMethod httpMethod: HttpMethod.values()) { + if (httpMethod.name().equals(methodName)) { + this.method = httpMethod; + return; + } + } + throw new HttpParsingException( + HttpStatusCode.SERVER_ERROR_501_NOT_IMPLEMENTED + ); } diff --git a/src/test/java/com/imanimen/jttpserver/http/HttpParserTest.java b/src/test/java/com/imanimen/jttpserver/http/HttpParserTest.java index 7255fcc..532f974 100644 --- a/src/test/java/com/imanimen/jttpserver/http/HttpParserTest.java +++ b/src/test/java/com/imanimen/jttpserver/http/HttpParserTest.java @@ -21,13 +21,21 @@ public static void beforeClass() { @Test void parseHttpRequest() throws IOException { HttpRequest request = httpParser.parseHttpRequest( - generateValidTestCase() + generateValidGETTestCase() ); assertEquals(request.getMethod(), HttpMethod.GET); } - private InputStream generateValidTestCase() { + @Test + void parseHttpBadRequest() throws IOException { + HttpRequest request = httpParser.parseHttpRequest( + generateInValidGETTestCase() + ); + + } + + private InputStream generateValidGETTestCase() { String rawHttpRequest = "GET / HTTP/1.1\r\n" + "Host: localhost:8080\r\n" + "Connection: keep-alive\r\n" + @@ -52,4 +60,18 @@ private InputStream generateValidTestCase() { ) ); } + + private InputStream generateInValidGETTestCase() { + String rawHttpRequest = "GET / HTTP/1.1\r\n" + + "Host: localhost:8080\r\n" + + "Cookie: Phpstorm-e23b83ee=e9b0b55c-7724-483f-93b4-b23824eeb394\r\n" + + "\r\n"; + + return new ByteArrayInputStream( + rawHttpRequest.getBytes( + StandardCharsets.US_ASCII + ) + ); + } + } \ No newline at end of file