HTTP.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.net.Socket;
  5. import java.security.SecureRandom;
  6. import java.text.SimpleDateFormat;
  7. import java.util.ArrayList;
  8. import java.util.Calendar;
  9. import java.util.List;
  10. import java.util.Locale;
  11. import java.util.TimeZone;
  12. import android.content.Context;
  13. import android.os.AsyncTask;
  14. import de.tudarmstadt.informatik.hostage.Hostage;
  15. import de.tudarmstadt.informatik.hostage.R;
  16. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  17. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  18. /**
  19. * HTTP protocol. Implementation of RFC document 1945. It can handle the
  20. * following requests: GET, HEAD, TRACE, POST, DELETE. For all other requests
  21. * '400 Bad Request' will be replied.
  22. *
  23. * @author Wulf Pfeiffer
  24. */
  25. public class HTTP implements Protocol {
  26. public HTTP() {
  27. boolean useQotd = Hostage.getContext().getSharedPreferences(Hostage.getContext().getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
  28. if (useQotd) {
  29. new QotdTask().execute(new String[] {});
  30. }
  31. }
  32. /**
  33. * Get the current time in html header format.
  34. *
  35. * @return the formatted server time.
  36. */
  37. private String getServerTime() {
  38. Calendar calendar = Calendar.getInstance();
  39. SimpleDateFormat dateFormat = new SimpleDateFormat(
  40. "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
  41. dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  42. return dateFormat.format(calendar.getTime());
  43. }
  44. /** Whole request that was sent by the client */
  45. private String request = "";
  46. // version stuff
  47. private String[][][] possibleHttpVersions = {
  48. {
  49. { "Apache/2.0." },
  50. { "28", "32", "35", "36", "39", "40", "42", "43", "44",
  51. "45", "46", "47", "48", "49", "50", "51", "52",
  52. "53", "54", "55", "58", "59", "61", "63", "64",
  53. "65" } },
  54. {
  55. { "Apache/2.2." },
  56. { "0", "2", "3", "4", "6", "8", "9", "10", "11", "12",
  57. "13", "14", "15", "16", "17", "18", "19", "20",
  58. "21", "22", "23", "24", "25" } },
  59. { { "Apache/2.3." },
  60. { "4", "5", "6", "8", "10", "11", "12", "14", "15", "16" } },
  61. { { "Apache/2.4." }, { "1", "2", "3", "4", "6" } },
  62. { { "Microsoft-IIS/" }, { "5.1", "7.0", "8.0" } } };
  63. private String serverVersion = initServerVersion();
  64. private String initServerVersion() {
  65. SecureRandom rndm = new SecureRandom();
  66. int majorVersion = rndm.nextInt(possibleHttpVersions.length);
  67. String version;
  68. String sharedPreferencePath = Hostage.getContext().getString(
  69. R.string.shared_preference_path);
  70. String profile = Hostage
  71. .getContext()
  72. .getSharedPreferences(sharedPreferencePath,
  73. Context.MODE_PRIVATE).getString("os", "");
  74. if (profile.equals("Windows 7") || profile.equals("Windows Server 2008")) {
  75. version = "Microsoft-IIS/7.5";
  76. } else if (profile.equals("Windows Server 2012") || profile.equals("Windows 8")) {
  77. version = "Microsoft-IIS/8.0";
  78. } else if (profile.equals("Windows XP")) {
  79. version = "Microsoft-IIS/5.1";
  80. } else {
  81. version = possibleHttpVersions[majorVersion][0][0]
  82. + possibleHttpVersions[majorVersion][1][rndm
  83. .nextInt(possibleHttpVersions[majorVersion][1].length)];
  84. }
  85. return version;
  86. }
  87. private String httpVersion = "HTTP/1.1";
  88. private static String htmlDocumentContent = HelperUtils.getRandomString(32, false);
  89. // request codes
  90. private static final String OPTIONS = "OPTIONS";
  91. private static final String GET = "GET";
  92. private static final String HEAD = "HEAD";
  93. private static final String POST = "POST";
  94. private static final String PUT = "PUT";
  95. private static final String DELETE = "DELETE";
  96. private static final String TRACE = "TRACE";
  97. private static final String CONNECT = "CONNECT";
  98. private static final String STATUS_CODE_200 = "200 OK\r\n";
  99. private static final String STATUS_CODE_400 = "400 Bad Request\r\n";
  100. private static final String STATUS_CODE_505 = "505 HTTP Version not supported\r\n";
  101. /**
  102. * Sets the html document content for HTTP and HTTPS.
  103. *
  104. * @param htmlDocumentContent
  105. */
  106. public static void setHtmlDocumentContent(String htmlDocumentContent) {
  107. HTTP.htmlDocumentContent = htmlDocumentContent;
  108. }
  109. // html header pre and suffix
  110. private String headerPrefix = "Date: " + getServerTime() + "\r\n"
  111. + "Server: " + serverVersion + " \r\n"
  112. + "Vary: Accept-Encoding\r\n" + "Content-Length: ";
  113. private String headerSuffix = "\r\n" + "Keep-Alive: timeout=5, max=100\r\n"
  114. + "Connection: Keep-Alive\r\n" + "Content-Type: text/html\r\n"
  115. + "\r\n";
  116. // html website
  117. private String htmlDocument = "<!doctype html>\n" + "<html lang=\"en\">\n"
  118. + "<head>\n" + "<meta charset=\"UTF-8\">\n" + "<title>"
  119. + htmlDocumentContent + "</title>\n" + "<body>"
  120. + htmlDocumentContent + "</body>\n" + "</head>\n" + "</html>";
  121. // html error pre and suffix
  122. private String errorHtmlPrefix = "<!doctype html>\n"
  123. + "<html lang=\"en\">\n" + "<head>\n"
  124. + "<meta charset=\"UTF-8\">\n" + "<title>";
  125. private String errorHtmlSuffix = "</title>\n" + "</head>\n" + "</html>";
  126. @Override
  127. public int getPort() {
  128. return 80;
  129. }
  130. @Override
  131. public boolean isClosed() {
  132. return true;
  133. }
  134. @Override
  135. public boolean isSecure() {
  136. return false;
  137. }
  138. @Override
  139. public List<Packet> processMessage(Packet requestPacket) {
  140. String request = null;
  141. if (requestPacket != null) {
  142. request = requestPacket.toString();
  143. }
  144. List<Packet> responsePackets = new ArrayList<Packet>();
  145. this.request = request;
  146. if (request.startsWith("G")) {
  147. //weird if clause but required for https
  148. responsePackets.add(buildPacket(STATUS_CODE_200, GET));
  149. } else if (!request.contains(httpVersion)) {
  150. responsePackets.add(buildPacket(STATUS_CODE_505, ""));
  151. } else if (request.contains(GET)) {
  152. responsePackets.add(buildPacket(STATUS_CODE_200, GET));
  153. } else if (request.contains(HEAD)) {
  154. responsePackets.add(buildPacket(STATUS_CODE_200, HEAD));
  155. } else if (request.contains(TRACE)) {
  156. responsePackets.add(buildPacket(STATUS_CODE_200, TRACE));
  157. } else if (request.contains(OPTIONS)) {
  158. responsePackets.add(buildPacket(STATUS_CODE_400, OPTIONS));
  159. } else if (request.contains(POST)) {
  160. responsePackets.add(buildPacket(STATUS_CODE_200, POST));
  161. } else if (request.contains(PUT)) {
  162. responsePackets.add(buildPacket(STATUS_CODE_400, PUT));
  163. } else if (request.contains(DELETE)) {
  164. responsePackets.add(buildPacket(STATUS_CODE_200, DELETE));
  165. } else if (request.contains(CONNECT)) {
  166. responsePackets.add(buildPacket(STATUS_CODE_400, CONNECT));
  167. } else {
  168. responsePackets.add(buildPacket(STATUS_CODE_400, ""));
  169. }
  170. boolean useQotd = Hostage.getContext().getSharedPreferences(Hostage.getContext().getString(R.string.shared_preference_path), Hostage.MODE_PRIVATE).getBoolean("useQotd", true);
  171. if (useQotd) {
  172. new QotdTask().execute(new String[] {});
  173. }
  174. return responsePackets;
  175. }
  176. @Override
  177. public String toString() {
  178. return "HTTP";
  179. }
  180. @Override
  181. public TALK_FIRST whoTalksFirst() {
  182. return TALK_FIRST.CLIENT;
  183. }
  184. /**
  185. * Builds a html response that can be sent
  186. *
  187. * @param code
  188. * response code that was determined
  189. * @param type
  190. * request type that was sent by the client
  191. * @return the html response
  192. */
  193. private Packet buildPacket(String code, String type) {
  194. String document = "";
  195. if (type.equals(GET)) {
  196. document = htmlDocument;
  197. } else if (type.equals(HEAD) || type.equals(DELETE)) {
  198. document = "";
  199. } else if (type.equals(TRACE)) {
  200. document = request;
  201. } else {
  202. document = errorHtmlPrefix + " " + code + errorHtmlSuffix;
  203. }
  204. return new Packet(httpVersion + " " + code + headerPrefix
  205. + document.length() + headerSuffix + document, toString());
  206. }
  207. /**
  208. * Task for accuiring a qotd from one of four possible servers.
  209. *
  210. * @author Wulf Pfeiffer
  211. */
  212. private class QotdTask extends AsyncTask<String, Void, String> {
  213. @Override
  214. protected String doInBackground(String... unused) {
  215. String[] sources = new String[] { "djxmmx.net", "ota.iambic.com", "alpha.mike-r.com", "electricbiscuit.org" };
  216. SecureRandom rndm = new SecureRandom();
  217. StringBuffer sb = new StringBuffer();
  218. try {
  219. Socket client = new Socket(sources[rndm.nextInt(4)], 17);
  220. BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
  221. while (!in.ready())
  222. ;
  223. while (in.ready()) {
  224. sb.append(in.readLine());
  225. }
  226. in.close();
  227. client.close();
  228. } catch (Exception e) {
  229. e.printStackTrace();
  230. }
  231. return sb.toString();
  232. }
  233. @Override
  234. protected void onPostExecute(String result) {
  235. if (result != null)
  236. HTTP.setHtmlDocumentContent(result);
  237. else
  238. HTTP.setHtmlDocumentContent(HelperUtils.getRandomString(32, false));
  239. }
  240. }
  241. }