Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
31 changes: 31 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion src/main/java/com/imanimen/jttpserver/http/HttpParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@ 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);

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;
Expand Down
12 changes: 10 additions & 2 deletions src/main/java/com/imanimen/jttpserver/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}


Expand Down
26 changes: 24 additions & 2 deletions src/test/java/com/imanimen/jttpserver/http/HttpParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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" +
Expand All @@ -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
)
);
}

}
Loading