|
@@ -8,9 +8,10 @@ import java.util.List;
|
|
|
import javax.net.ssl.KeyManagerFactory;
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
|
|
|
+import de.tudarmstadt.informatik.hostage.ui.MainActivity;
|
|
|
+
|
|
|
/**
|
|
|
* HTTPS protocol
|
|
|
- *
|
|
|
* @author Wulf Pfeiffer
|
|
|
*/
|
|
|
public class HTTPS implements SSLProtocol<String> {
|
|
@@ -30,23 +31,23 @@ public class HTTPS implements SSLProtocol<String> {
|
|
|
List<String> response = new ArrayList<String>();
|
|
|
request = message + request;
|
|
|
|
|
|
- if (!message.contains(version)) {
|
|
|
+ if(!message.contains(version)){
|
|
|
response.add(buildPacket(c505, ""));
|
|
|
- } else if (message.contains(get)) {
|
|
|
+ } else if(message.contains(get)) {
|
|
|
response.add(buildPacket(c200, get));
|
|
|
- } else if (message.contains(head)) {
|
|
|
+ } else if(message.contains(head)) {
|
|
|
response.add(buildPacket(c200, head));
|
|
|
- } else if (message.contains(trace)) {
|
|
|
+ } else if(message.contains(trace)){
|
|
|
response.add(buildPacket(c200, trace));
|
|
|
- } else if (message.contains(options)) {
|
|
|
+ } else if(message.contains(options)){
|
|
|
response.add(buildPacket(c400, options));
|
|
|
- } else if (message.contains(post)) {
|
|
|
+ } else if(message.contains(post)){
|
|
|
response.add(buildPacket(c400, post));
|
|
|
- } else if (message.contains(put)) {
|
|
|
+ } else if(message.contains(put)){
|
|
|
response.add(buildPacket(c400, put));
|
|
|
- } else if (message.contains(delete)) {
|
|
|
+ } else if(message.contains(delete)){
|
|
|
response.add(buildPacket(c400, delete));
|
|
|
- } else if (message.contains(connect)) {
|
|
|
+ } else if(message.contains(connect)){
|
|
|
response.add(buildPacket(c400, connect));
|
|
|
} else {
|
|
|
response.add(buildPacket(c400, ""));
|
|
@@ -76,14 +77,13 @@ public class HTTPS implements SSLProtocol<String> {
|
|
|
|
|
|
@Override
|
|
|
public SSLContext getSSLContext() {
|
|
|
- String ksName = "/storage/sdcard0/server.bks";
|
|
|
+ String ksName = "https_cert.bks";
|
|
|
char ksPass[] = "password".toCharArray();
|
|
|
-
|
|
|
KeyStore ks;
|
|
|
KeyManagerFactory kmf = null;
|
|
|
try {
|
|
|
ks = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
|
- ks.load(new FileInputStream(ksName), ksPass);
|
|
|
+ ks.load(MainActivity.getContext().getAssets().open(ksName), ksPass);
|
|
|
kmf = KeyManagerFactory.getInstance(KeyManagerFactory
|
|
|
.getDefaultAlgorithm());
|
|
|
kmf.init(ks, ksPass);
|
|
@@ -103,63 +103,73 @@ public class HTTPS implements SSLProtocol<String> {
|
|
|
|
|
|
/**
|
|
|
* 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
|
|
|
+ * @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;
|
|
|
+ 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"
|
|
|
- + "Vary: Accept-Encoding\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";
|
|
|
- // html website
|
|
|
- private String htmlDoc = "<!doctype html>\n" + "<html lang=\"en\">\n"
|
|
|
- + "<head>\n" + "<meta charset=\"UTF-8\">\n"
|
|
|
- + "<title>Test successful</title>\n" + "</head>\n" + "<body>\n"
|
|
|
- + "<h1>Test successful</h1>\n" + "<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>";
|
|
|
-
|
|
|
+ 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" +
|
|
|
+ "Vary: Accept-Encoding\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";
|
|
|
+ //html website
|
|
|
+ private String htmlDoc =
|
|
|
+ "<!doctype html>\n" +
|
|
|
+ "<html lang=\"en\">\n" +
|
|
|
+ "<head>\n" +
|
|
|
+ "<meta charset=\"UTF-8\">\n" +
|
|
|
+ "<title>Test successful</title>\n" +
|
|
|
+ "</head>\n" +
|
|
|
+ "<body>\n" +
|
|
|
+ "<h1>Test successful</h1>\n" +
|
|
|
+ "<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>";
|
|
|
}
|