diff --git a/src/ovh/alexisdelhaie/endpoint/Controller.java b/src/ovh/alexisdelhaie/endpoint/Controller.java index 5a19baa..289c57a 100644 --- a/src/ovh/alexisdelhaie/endpoint/Controller.java +++ b/src/ovh/alexisdelhaie/endpoint/Controller.java @@ -110,7 +110,12 @@ public class Controller implements Initializable { sp.getItems().add(ta); sp.setOrientation(Orientation.VERTICAL); - return new Tab("untitled", sp); + Tab tab = new Tab("untitled", sp); + tab.setOnCloseRequest(arg0 -> { + requests.remove(tab.hashCode()); + }); + + return tab; } @SuppressWarnings("unchecked") diff --git a/src/ovh/alexisdelhaie/endpoint/about.fxml b/src/ovh/alexisdelhaie/endpoint/about.fxml index c493019..942aa44 100644 --- a/src/ovh/alexisdelhaie/endpoint/about.fxml +++ b/src/ovh/alexisdelhaie/endpoint/about.fxml @@ -7,36 +7,51 @@ - - - diff --git a/src/ovh/alexisdelhaie/endpoint/http/HttpClient.java b/src/ovh/alexisdelhaie/endpoint/http/HttpClient.java index 901c744..10581d7 100644 --- a/src/ovh/alexisdelhaie/endpoint/http/HttpClient.java +++ b/src/ovh/alexisdelhaie/endpoint/http/HttpClient.java @@ -19,7 +19,7 @@ public class HttpClient { public final static String CRLF = "\r\n"; public final static int DEFAULT_TIMEOUT = 10000; - private boolean allowInvalidSsl; + private final boolean allowInvalidSsl; public HttpClient() { this(false); } public HttpClient(boolean allowInvalidSsl) { this.allowInvalidSsl = allowInvalidSsl; } diff --git a/src/ovh/alexisdelhaie/endpoint/http/HttpStatus.java b/src/ovh/alexisdelhaie/endpoint/http/HttpStatus.java new file mode 100644 index 0000000..f126a06 --- /dev/null +++ b/src/ovh/alexisdelhaie/endpoint/http/HttpStatus.java @@ -0,0 +1,94 @@ +package ovh.alexisdelhaie.endpoint.http; + +import java.util.HashMap; +import java.util.Map; + +public enum HttpStatus { + CONTINUE(100, "Continue"), + SWITCHING_PROTOCOLS(101, "Switching Protocols"), + + OK(200, "OK"), + 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(301, "Moved Permanently"), + FOUND(302, "Found"), + SEE_OTHER(303, "See Other"), + NOT_MODIFIED(304, "Not Modified"), + USE_PROXY(305, "Use Proxy"), + SWITCH_PROXY(306, "Switch Proxy"), + TEMPORARY_REDIRECT(307, "Temporary Redirect"), + PERMANENT_REDIRECT(308, "Permanent Redirect"), + TOO_MANY_REDIRECTS(310, "Too many Redirects"), + + 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 Timeout"), + CONFLICT(409, "Conflict"), + GONE(410, "Gone"), + LENGTH_REQUIRED(411, "Length Required"), + PRECONDITION_FAILED(412, "Precondition Failed"), + PAYLOAD_TOO_LARGE(413, "Payload Too Large"), + URI_TOO_LONG(414, "URI Too Long"), + UNSUPPORTED_MEDIA_TYPE(415, "Unsupported Media Type"), + RANGE_NOT_SATISFIABLE(416, "Range Not Satisfiable "), + EXPECTATION_FAILED(417, "Expectation Failed"), + IM_A_TEAPOT(418, "I'm a teapot"), //for fun + MISDIRECTED_REQUEST(421, "Misdirected Request"), + TOO_EARLY(425, "Too Early"), + UPGRADE_REQUIRED(426, "Upgrade Required"), + PRECONDITION_REQUIRED(428, "Precondition Required"), + TOO_MANY_REQUESTS(429, "Too Many Requests"), + REQUEST_HEADER_FIELDS_TOO_LARGE(431, "Request Header Fields Too Large"), + RETRY_WITH(449, "Retry With"), + BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS(450, "Blocked by Windows Parental Controls"), + UNAVAILABLE_FOR_LEGAL_REASONS(451, "Unavailable For Legal Reasons"), + + 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 Timeout"), + HTTP_VERSION_NOT_SUPPORTED(505, "HTTP Version Not Supported"), + VARIANT_ALSO_NEGOTIATES(506, "Variant Also Negotiates"), + BANDWIDTH_LIMIT_EXCEEDED(509, "Bandwidth Limit Exceeded"), + NOT_EXTENDED(510, "Not Extended"), + NETWORK_AUTHENTICATION_REQUIRED(511, "Network Authentication Required"); + + private final int code; + private final String message; + + HttpStatus(int code, String message) { + this.code = code; + this.message = message; + } + + private static final Map map; + + static { + map = new HashMap<>(); + for (HttpStatus v : HttpStatus.values()) { + map.put(v.code, v); + } + } + + public static HttpStatus findByCode(int i) { + return map.get(i); + } + + public String getMessage() { + return this.message; + } + +} diff --git a/src/ovh/alexisdelhaie/endpoint/http/Request.java b/src/ovh/alexisdelhaie/endpoint/http/Request.java index 41fd469..1431640 100644 --- a/src/ovh/alexisdelhaie/endpoint/http/Request.java +++ b/src/ovh/alexisdelhaie/endpoint/http/Request.java @@ -7,13 +7,13 @@ import java.util.Objects; public class Request { - private String host; - private String scheme; - private String path; - private int port; - private HashMap params; - private HashMap customHeaders; - private String body; + private final String host; + private final String scheme; + private final String path; + private final int port; + private final HashMap params; + private final HashMap customHeaders; + private final String body; private String rawRequest; Request(String host, String scheme, String path, int port, diff --git a/src/ovh/alexisdelhaie/endpoint/http/RequestBuilder.java b/src/ovh/alexisdelhaie/endpoint/http/RequestBuilder.java index 78d243d..9edd524 100644 --- a/src/ovh/alexisdelhaie/endpoint/http/RequestBuilder.java +++ b/src/ovh/alexisdelhaie/endpoint/http/RequestBuilder.java @@ -16,8 +16,8 @@ public class RequestBuilder { private String scheme; private String path; private int port; - private HashMap params; - private HashMap customHeaders; + private final HashMap params; + private final HashMap customHeaders; private String body; public RequestBuilder (String url) throws MalformedURLException { diff --git a/src/ovh/alexisdelhaie/endpoint/http/Response.java b/src/ovh/alexisdelhaie/endpoint/http/Response.java index 33d01d9..ad5ade7 100644 --- a/src/ovh/alexisdelhaie/endpoint/http/Response.java +++ b/src/ovh/alexisdelhaie/endpoint/http/Response.java @@ -12,21 +12,20 @@ public class Response { public final static String CRLF = "\r\n"; public final static String DOUBLE_CRLF = "\r\n\r\n"; - private HashMap headers; - private String rawHeaders; + private final HashMap headers; + private final String rawHeaders; private String rawResponse; - private String body; + private final String body; private int statusCode; private String status; - private long time; - private Request request; + private final long time; + private final Request request; Response(byte[] res, long time, Request r) throws UnsupportedEncodingException { headers = new HashMap<>(); rawResponse = new String(res, StandardCharsets.UTF_8); int crlf = rawResponse.indexOf(DOUBLE_CRLF); - String h = rawResponse.substring(0, crlf); - rawHeaders = h; + rawHeaders = rawResponse.substring(0, crlf); parseHeaders(); rawResponse = new String(res, getEncoding()); body = rawResponse.substring(crlf + DOUBLE_CRLF.length()); @@ -46,11 +45,22 @@ public class Response { } private void parseStatus(String l) { - Pattern p = Pattern.compile("(HTTP/1.1)\\s([0-9]{3})\\s(.+)"); + Pattern p = Pattern.compile("^(HTTP/1.1)\\s([0-9]{3})\\s(.+)$"); Matcher m = p.matcher(l); if (m.matches()) { statusCode = Integer.parseInt(m.group(2)); status = m.group(3); + } else { + p = Pattern.compile("^(HTTP/1.1)\\s([0-9]{3})$"); + m = p.matcher(l); + if (m.matches()) { + statusCode = Integer.parseInt(m.group(2)); + HttpStatus httpStatus = HttpStatus.findByCode(statusCode); + status = (httpStatus != null) ? httpStatus.getMessage() : ""; + } else { + statusCode = -1; + status = "Cannot get status form HTTP Response"; + } } }