|
@@ -8,13 +8,11 @@ import java.util.List;
|
|
|
import javax.net.ssl.KeyManagerFactory;
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
|
+/**
|
|
|
+ * HTTPS protocol
|
|
|
+ * @author Wulf Pfeiffer
|
|
|
+ */
|
|
|
public class HTTPS implements SSLProtocol<String> {
|
|
|
-
|
|
|
- private static enum STATE {
|
|
|
- NONE, OPEN, CLOSED
|
|
|
- };
|
|
|
-
|
|
|
- private STATE state = STATE.NONE;
|
|
|
|
|
|
@Override
|
|
|
public int getPort() {
|
|
@@ -30,14 +28,35 @@ public class HTTPS implements SSLProtocol<String> {
|
|
|
@Override
|
|
|
public List<String> processMessage(String message) {
|
|
|
List<String> response = new ArrayList<String>();
|
|
|
- response.add(header+html);
|
|
|
- state = STATE.CLOSED;
|
|
|
+ request = message + request;
|
|
|
+
|
|
|
+ if(!message.contains(version)){
|
|
|
+ response.add(buildPacket(c505, ""));
|
|
|
+ } else if(message.contains(get)) {
|
|
|
+ response.add(buildPacket(c200, get));
|
|
|
+ } else if(message.contains(head)) {
|
|
|
+ response.add(buildPacket(c200, head));
|
|
|
+ } else if(message.contains(trace)){
|
|
|
+ response.add(buildPacket(c200, trace));
|
|
|
+ } else if(message.contains(options)){
|
|
|
+ response.add(buildPacket(c400, options));
|
|
|
+ } else if(message.contains(post)){
|
|
|
+ response.add(buildPacket(c400, post));
|
|
|
+ } else if(message.contains(put)){
|
|
|
+ response.add(buildPacket(c400, put));
|
|
|
+ } else if(message.contains(delete)){
|
|
|
+ response.add(buildPacket(c400, delete));
|
|
|
+ } else if(message.contains(connect)){
|
|
|
+ response.add(buildPacket(c400, connect));
|
|
|
+ } else {
|
|
|
+ response.add(buildPacket(c400, ""));
|
|
|
+ }
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public boolean isClosed() {
|
|
|
- return state == STATE.CLOSED;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -81,19 +100,53 @@ public class HTTPS implements SSLProtocol<String> {
|
|
|
return sslcontext;
|
|
|
}
|
|
|
|
|
|
- private String header =
|
|
|
- "HTTP/1.1 200 OK\r\n" +
|
|
|
+ /**
|
|
|
+ * Builds a html response that can be sent
|
|
|
+ * @param code response code that was determined
|
|
|
+ * @param type request type that was sent by the client
|
|
|
+ * @return the html response
|
|
|
+ */
|
|
|
+ private String buildPacket(String code, String type) {
|
|
|
+ String doc = "";
|
|
|
+ if(type.equals(get)) doc = htmlDoc;
|
|
|
+ else if(type.equals(head)) doc = "";
|
|
|
+ else if(type.equals(trace)) doc = request;
|
|
|
+ else doc = errorHtmlPrefix + code + errorHtmlSuffix;
|
|
|
+
|
|
|
+ return version + code + headerPrefix + doc.length() + headerSuffix + doc;
|
|
|
+ }
|
|
|
+
|
|
|
+ /** Whole request that was sent by the client */
|
|
|
+ private String request = "";
|
|
|
+ private String version = "HTTP/1.1";
|
|
|
+ //request codes
|
|
|
+ private String options = "OPTIONS";
|
|
|
+ private String get = "GET";
|
|
|
+ private String head = "HEAD";
|
|
|
+ private String post = "POST";
|
|
|
+ private String put = "PUT";
|
|
|
+ private String delete = "DELETE";
|
|
|
+ private String trace = "TRACE";
|
|
|
+ private String connect = "CONNECT";
|
|
|
+ //response codes
|
|
|
+ private String c200 = " 200 OK\r\n";
|
|
|
+ private String c400 = " 400 Bad Request\r\n";
|
|
|
+ private String c505 = " 505 HTTP Version not supported\r\n";
|
|
|
+
|
|
|
+ //html header pre and suffix
|
|
|
+ private String headerPrefix =
|
|
|
"Date: Mon, 01 Jul 2013 18:27:55 GMT\r\n" +
|
|
|
"Server: Apache/2.2.22 (Debian)\r\n" +
|
|
|
- "X-Powered-By: PHP/5.4.4.-14\r\n" +
|
|
|
"Vary: Accept-Encoding\r\n" +
|
|
|
- "Content-Length: 173\r\n" +
|
|
|
+ "Content-Length: ";
|
|
|
+ private String headerSuffix =
|
|
|
+ "\r\n" +
|
|
|
"Keep-Alive: timeout=5, max=100\r\n" +
|
|
|
"Connection: Keep-Alive\r\n" +
|
|
|
"Content-Type: text/html\r\n" +
|
|
|
"\r\n";
|
|
|
-
|
|
|
- private String html =
|
|
|
+ //html website
|
|
|
+ private String htmlDoc =
|
|
|
"<!doctype html>\n" +
|
|
|
"<html lang=\"en\">\n" +
|
|
|
"<head>\n" +
|
|
@@ -105,5 +158,18 @@ public class HTTPS implements SSLProtocol<String> {
|
|
|
"<p>Congratulations.</p>\n" +
|
|
|
"</body>\n" +
|
|
|
"</html>";
|
|
|
+ //html error pre and suffix
|
|
|
+ private String errorHtmlPrefix =
|
|
|
+ "<!doctype html>\n" +
|
|
|
+ "<html lang=\"en\">\n" +
|
|
|
+ "<head>\n" +
|
|
|
+ "<meta charset=\"UTF-8\">\n" +
|
|
|
+ "<title>";
|
|
|
+ private String errorHtmlSuffix =
|
|
|
+ "</title>\n" +
|
|
|
+ "</head>\n" +
|
|
|
+ "<body>\n" +
|
|
|
+ "</body>\n" +
|
|
|
+ "</html>";
|
|
|
|
|
|
}
|