Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanGiles committed Apr 20, 2020
1 parent a41343f commit 4373da8
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/net/jonathangiles/teenyhttpd/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Header {
private String key;
private String value;

public Header(String keyValue) {
public Header(final String keyValue) {
this.keyValue = keyValue;
}

Expand All @@ -29,7 +29,7 @@ public String toString() {
}

private void parse() {
String[] split = keyValue.split(":", 2);
final String[] split = keyValue.split(":", 2);
key = split[0].trim();
value = split[1].trim();
}
Expand Down
4 changes: 2 additions & 2 deletions src/net/jonathangiles/teenyhttpd/TeenyHttpd.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void stop() {
executorService.shutdown();
}

public Response serve(Request request) {
public Response serve(final Request request) {
return new FileResponse(request);
}

Expand All @@ -81,7 +81,7 @@ private void run(final Socket connect) {
final String requestUri = parse.nextToken().toLowerCase();

// split it at the query param, if it exists
Request request;
final Request request;
if (requestUri.contains("?")) {
final String[] uriSplit = requestUri.split("\\?", 2);

Expand Down
5 changes: 2 additions & 3 deletions src/net/jonathangiles/teenyhttpd/request/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.jonathangiles.teenyhttpd.Header;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -15,7 +14,7 @@ public class Request {
private List<Header> headers;
private Map<String, String> headersMap;

public Request(final Method method, final String path, QueryParams queryParams) {
public Request(final Method method, final String path, final QueryParams queryParams) {
this.method = method;
this.path = path;
this.queryParams = queryParams;
Expand All @@ -29,7 +28,7 @@ public String getPath() {
return path;
}

public void addHeader(Header header) {
public void addHeader(final Header header) {
if (headers == null) {
headers = new ArrayList<>();
}
Expand Down
8 changes: 4 additions & 4 deletions src/net/jonathangiles/teenyhttpd/response/ByteResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ public class ByteResponse extends Response {
final List<String> headers;
final byte[] body;

public ByteResponse(final Request request, StatusCode statusCode) {
public ByteResponse(final Request request, final StatusCode statusCode) {
this(request, statusCode, Collections.emptyList());
}

public ByteResponse(final Request request, StatusCode statusCode, final List<String> headers) {
public ByteResponse(final Request request, final StatusCode statusCode, final List<String> headers) {
this(request, statusCode, headers, null);
}

public ByteResponse(final Request request, StatusCode statusCode, final byte[] body) {
public ByteResponse(final Request request, final StatusCode statusCode, final byte[] body) {
this(request, statusCode, Collections.emptyList(), body);
}

public ByteResponse(final Request request, StatusCode statusCode, final List<String> headers, final byte[] body) {
public ByteResponse(final Request request, final StatusCode statusCode, final List<String> headers, final byte[] body) {
super(request);
this.statusCode = statusCode;
this.headers = headers;
Expand Down
42 changes: 21 additions & 21 deletions src/net/jonathangiles/teenyhttpd/response/FileResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@ public class FileResponse extends Response {
static final String FILE_NOT_FOUND = "404.html";
static final String METHOD_NOT_SUPPORTED = "not_supported.html";

private StatusCode statusCode;
private List<String> headers;
private final StatusCode statusCode;
private final List<String> headers;
private File fileToReturn;
private int fileLength;

public FileResponse(final Request request) {
super(request);
Expand All @@ -29,32 +28,32 @@ public FileResponse(final Request request) {

fileToReturn = null;

// we support only GET and HEAD methods, we check
if (! (method == Method.GET || method == Method.HEAD)) {
// we return the not supported file to the client
fileToReturn = getFile(METHOD_NOT_SUPPORTED);
statusCode = StatusCode.NOT_IMPLEMENTED;
} else {
// GET or HEAD method
if (path.endsWith("/")) {
path += DEFAULT_FILE;
}
switch (method) {
case GET: {
if (path.endsWith("/")) {
path += DEFAULT_FILE;
}

if (method == Method.GET) { // GET method so we return content
fileToReturn = getFile(path);
if (!fileToReturn.exists()) {
fileToReturn = getFile(FILE_NOT_FOUND);
statusCode = StatusCode.FILE_NOT_FOUND;
statusCode = StatusCode.NOT_FOUND;
} else {
statusCode = StatusCode.OK;
}
break;
}
default: {
fileToReturn = getFile(METHOD_NOT_SUPPORTED);
statusCode = StatusCode.NOT_IMPLEMENTED;
break;
}
}

fileLength = (int) fileToReturn.length();
final int fileLength = (int) fileToReturn.length();

headers = new ArrayList<>();
headers.add("Content-type: " + getContentType(path));
headers.add("Content-type: " + getContentType(fileToReturn));
headers.add("Content-length: " + fileLength);
}

Expand All @@ -69,21 +68,22 @@ public List<String> getHeaders() {
}

@Override
public void writeBody(BufferedOutputStream dataOut) throws IOException {
public void writeBody(final BufferedOutputStream dataOut) throws IOException {
Files.copy(fileToReturn.toPath(), dataOut);
dataOut.flush();
}

// return supported MIME Types
private String getContentType(String fileRequested) {
if (fileRequested.endsWith(".htm") || fileRequested.endsWith(".html")) {
private String getContentType(final File file) {
final String filename = file.getName();
if (filename.endsWith(".htm") || filename.endsWith(".html")) {
return "text/html";
} else {
return "text/plain";
}
}

private File getFile(String filename) {
private File getFile(final String filename) {
return new File(WEB_ROOT, filename);
}
}
2 changes: 1 addition & 1 deletion src/net/jonathangiles/teenyhttpd/response/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public abstract class Response {
private final Request request;

protected Response(Request request) {
protected Response(final Request request) {
this.request = request;
}

Expand Down
42 changes: 40 additions & 2 deletions src/net/jonathangiles/teenyhttpd/response/StatusCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,46 @@ public enum StatusCode {
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec6.html

OK(200, "OK"),
FILE_NOT_FOUND(404, "File Not Found"),
NOT_IMPLEMENTED(501, "Not Implemented");
CREATED(201, "Created"),
ACCEPTED(202, "Accepted"),
NON_AUTHORITATIVE_INFORMATION(203, "Non-Authoritative Information"),
NO_CONTENT(204, "No Content"),
RESET_CONTENT(205, "Reset Content"),
PARTIAL_CONTENT(206, "Partial Content"),

MULTIPLE_CHOICES(300, "Multiple Choices"),
MOVED_PERMANENTLY(201, "Moved Permanently"),
FOUND(302, "Found"),
SEE_OTHER(303, "See Other"),
NOT_MODIFIED(304, "Not Modified"),
USE_PROXY(305, "Use Proxy"),
TEMPORARY_REDIRECT(307, "Temporary Redirect"),

BAD_REQUEST(400, "Bad Request"),
UNAUTHORIZED(401, "Unauthorized"),
PAYMENT_REQUIRED(402, "Payment Required"),
FORBIDDEN(403, "Forbidden"),
NOT_FOUND(404, "Not Found"),
METHOD_NOT_ALLOWED(405, "Method Not Allowed"),
NOT_ACCEPTABLE(406, "Not Acceptable"),
PROXY_AUTHENTICATION_REQUIRED(407, "Proxy Authentication Required"),
REQUEST_TIME_OUT(408, "Request Time-out"),
CONFLICT(409, "Conflict"),
GONE(410, "Gone"),
LENGTH_REQUIRED(411, "Length Required"),
PRECONDITION_FAILED(412, "Precondition Failed"),
REQUEST_ENTITY_TOO_LARGE(413, "Request Entity Too Large"),
REQUEST_URI_TOO_LARGE(414, "Request-URI Too Large"),
UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"),
REQUESTED_RANGE_NOT_SATISFIABLE(416, "Requested range not satisfiable"),
EXPECTATION_FAILED(417, "Expectation Failed"),

INTERNAL_SERVER_ERROR(500, "Internal Server Error"),
NOT_IMPLEMENTED(501, "Not Implemented"),
BAD_GATEWAY(502, "Bad Gateway"),
SERVICE_UNAVAILABLE(503, "Service Unavailable"),
GATEWAY_TIME_OUT(504, "Gateway Time-out"),
HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version not supported");

private final int code;
private final String reasonPhrase;
Expand Down

0 comments on commit 4373da8

Please sign in to comment.