Skip to content

Commit

Permalink
Merge pull request #12 from Kerosene-Labs/rebuild-query-parameter-parser
Browse files Browse the repository at this point in the history
Rebuild query parameter parser
  • Loading branch information
hlafaille authored Nov 20, 2024
2 parents 25fc015 + 3db8c5e commit 54b135f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
25 changes: 18 additions & 7 deletions src/java/com/kerosenelabs/kindling/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class HttpRequest {
private String protocolVersion;
private HashMap<String, String> headers;
private String content;
private String[] x;

public HttpRequest(BufferedReader bufferedReader) throws KindlingException {
List<String> messageHead;
Expand Down Expand Up @@ -160,13 +159,25 @@ private static HttpRequestHead parseRequestLine(List<String> messageHead) {
*/
private static HashMap<String, String> parseQueryParameters(String resource) throws KindlingException {
HashMap<String, String> params = new HashMap<>();
String[] split = resource.split("&");
for (String pair : split) {
int idx = pair.indexOf("=");

// split our resource, ensure we were given query params
String[] splitResource = resource.split("\\?");
if (splitResource.length == 1) {
return params;
} else if (splitResource.length > 2) {
throw new KindlingException("Malformed query parameters, multiple '?' separators");
}

// split again, this time by param separators
String[] separatedParams = splitResource[1].split("\\&");

// iterate over the split params, this time splitting by key/value pair and
// decoding the keys and values
for (String keyValuePair : separatedParams) {
String[] splitKeyValuePair = keyValuePair.split("\\=");
try {
String key = URLDecoder.decode(pair.substring(0, idx), "UTF-8");
String value = URLDecoder.decode(pair.substring(idx + 1), "UTF-8");
params.put(key, value);
params.put(URLDecoder.decode(splitKeyValuePair[0], "UTF8"),
URLDecoder.decode(splitKeyValuePair[1], "UTF8"));
} catch (UnsupportedEncodingException e) {
throw new KindlingException(e);
}
Expand Down
25 changes: 23 additions & 2 deletions src/java/com/kerosenelabs/kindling/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,36 @@
import java.util.HashMap;

import com.kerosenelabs.kindling.constant.HttpStatus;
import com.kerosenelabs.kindling.constant.MimeType;
import com.kerosenelabs.kindling.exception.KindlingException;

public class HttpResponse {
private HttpStatus httpStatus;
private HashMap<String, String> headers = new HashMap<>();
private String content;
private MimeType contentType;

HttpResponse(Builder builder) {
HttpResponse(Builder builder) throws KindlingException {
this.httpStatus = builder.httpStatus;
this.headers = builder.headers;
this.content = builder.content;
this.contentType = builder.contentType;

// implicitly calculate Content-Length
if (content != null) {
headers.put("Content-Length", Integer.toString(content.getBytes().length));
}

// check if the content-type header has already been set
for (String key : headers.keySet()) {
if (key.toLowerCase().equals("content-type") && this.contentType != null) {
throw new KindlingException(
"Programming error, you're trying to set the 'Content-Type' header manually AND set it through 'HttpResponse.Builder'");
}
}
if (contentType == null) {
throw new KindlingException("Programming error, you must specify a Content");
}
}

public String toString() {
Expand All @@ -44,6 +59,7 @@ public static class Builder {
private HttpStatus httpStatus;
private HashMap<String, String> headers = new HashMap<>();
private String content;
private MimeType contentType;

public Builder status(HttpStatus httpStatus) {
this.httpStatus = httpStatus;
Expand All @@ -60,7 +76,12 @@ public Builder content(String content) {
return this;
}

public HttpResponse build() {
public Builder contentType(MimeType contentType) {
this.contentType = contentType;
return this;
}

public HttpResponse build() throws KindlingException {
return new HttpResponse(this);
}
}
Expand Down
9 changes: 2 additions & 7 deletions src/java/com/kerosenelabs/kindling/Main.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.kerosenelabs.kindling;

import java.nio.file.Path;
import java.util.HashMap;

import com.kerosenelabs.kindling.constant.HttpMethod;
import com.kerosenelabs.kindling.constant.HttpStatus;
import com.kerosenelabs.kindling.constant.MimeType;
import com.kerosenelabs.kindling.exception.KindlingException;
import com.kerosenelabs.kindling.handler.RequestHandler;

Expand All @@ -28,14 +28,9 @@ public boolean accepts(HttpRequest httpRequest) throws KindlingException {
*/
@Override
public HttpResponse handle(HttpRequest httpRequest) throws KindlingException {
System.out.println(httpRequest.getQueryParmeters().toString());
return new HttpResponse.Builder()
.status(HttpStatus.OK)
.headers(new HashMap<>() {
{
put("Content-Type", "application/json");
}
})
.contentType(MimeType.APPLICATION_JSON)
.content("{\"key\": \"value\"}")
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.kerosenelabs.kindling.HttpRequest;
import com.kerosenelabs.kindling.HttpResponse;
import com.kerosenelabs.kindling.constant.HttpMethod;
import com.kerosenelabs.kindling.constant.HttpStatus;
import com.kerosenelabs.kindling.exception.KindlingException;

Expand All @@ -18,8 +17,10 @@ public abstract class RequestHandler {
*
* @param t the Throwable that occurred
* @return
* @throws KindlingException
*/
public HttpResponse handleError(Throwable t) {
public HttpResponse handleError(Throwable t) throws KindlingException {
t.printStackTrace();
return new HttpResponse.Builder()
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.content("Internal Server Error")
Expand Down

0 comments on commit 54b135f

Please sign in to comment.