HTTP.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.security.SecureRandom;
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Calendar;
  6. import java.util.List;
  7. import java.util.Locale;
  8. import java.util.TimeZone;
  9. import de.tudarmstadt.informatik.hostage.commons.HelperUtils;
  10. import de.tudarmstadt.informatik.hostage.wrapper.Packet;
  11. /**
  12. * HTTP protocol. Implementation of RFC document 1945. It can handle the
  13. * following requests: GET, HEAD, TRACE, POST, DELETE. For all other requests
  14. * '400 Bad Request' will be replied.
  15. *
  16. * @author Wulf Pfeiffer
  17. */
  18. public class HTTP implements Protocol {
  19. /**
  20. * Get the current time in html header format.
  21. *
  22. * @return the formatted server time.
  23. */
  24. private static String getServerTime() {
  25. Calendar calendar = Calendar.getInstance();
  26. SimpleDateFormat dateFormat = new SimpleDateFormat(
  27. "EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
  28. dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  29. return dateFormat.format(calendar.getTime());
  30. }
  31. /** Whole request that was sent by the client */
  32. private String request = "";
  33. // version stuff
  34. private static String[][][] possibleHttpVersions = {
  35. {
  36. { "Apache/2.0." },
  37. { "28", "32", "35", "36", "39", "40", "42", "43", "44",
  38. "45", "46", "47", "48", "49", "50", "51", "52",
  39. "53", "54", "55", "58", "59", "61", "63", "64",
  40. "65" } },
  41. {
  42. { "Apache/2.2." },
  43. { "0", "2", "3", "4", "6", "8", "9", "10", "11", "12",
  44. "13", "14", "15", "16", "17", "18", "19", "20",
  45. "21", "22", "23", "24", "25" } },
  46. { { "Apache/2.3." },
  47. { "4", "5", "6", "8", "10", "11", "12", "14", "15", "16" } },
  48. { { "Apache/2.4." }, { "1", "2", "3", "4", "6" } } };
  49. private static String serverVersion = initServerVersion();
  50. private static String initServerVersion() {
  51. SecureRandom rndm = new SecureRandom();
  52. int majorVersion = rndm.nextInt(possibleHttpVersions.length);
  53. return possibleHttpVersions[majorVersion][0][0]
  54. + possibleHttpVersions[majorVersion][1][rndm
  55. .nextInt(possibleHttpVersions[majorVersion][1].length)];
  56. }
  57. private String httpVersion = "HTTP/1.1";
  58. private static String htmlDocumentContent = HelperUtils.getRandomString(32,
  59. false);
  60. // request codes
  61. private static final String OPTIONS = "OPTIONS";
  62. private static final String GET = "GET";
  63. private static final String HEAD = "HEAD";
  64. private static final String POST = "POST";
  65. private static final String PUT = "PUT";
  66. private static final String DELETE = "DELETE";
  67. private static final String TRACE = "TRACE";
  68. private static final String CONNECT = "CONNECT";
  69. private static final String STATUS_CODE_200 = "200 OK\r\n";
  70. private static final String STATUS_CODE_400 = "400 Bad Request\r\n";
  71. private static final String STATUS_CODE_505 = "505 HTTP Version not supported\r\n";
  72. /**
  73. * Sets the html document content for HTTP and HTTPS.
  74. *
  75. * @param htmlDocumentContent
  76. */
  77. public static void setHtmlDocumentContent(String htmlDocumentContent) {
  78. HTTP.htmlDocumentContent = htmlDocumentContent;
  79. }
  80. // html header pre and suffix
  81. private String headerPrefix = "Date: " + getServerTime() + "\r\n"
  82. + "Server: " + serverVersion + " \r\n"
  83. + "Vary: Accept-Encoding\r\n" + "Content-Length: ";
  84. private String headerSuffix = "\r\n" + "Keep-Alive: timeout=5, max=100\r\n"
  85. + "Connection: Keep-Alive\r\n" + "Content-Type: text/html\r\n"
  86. + "\r\n";
  87. // html website
  88. private String htmlDocument = "<!doctype html>\n" + "<html lang=\"en\">\n"
  89. + "<head>\n" + "<meta charset=\"UTF-8\">\n" + "<title>"
  90. + htmlDocumentContent + "</title>\n" + "<body>"
  91. + htmlDocumentContent + "</body>\n" + "</head>\n" + "</html>";
  92. // html error pre and suffix
  93. private String errorHtmlPrefix = "<!doctype html>\n"
  94. + "<html lang=\"en\">\n" + "<head>\n"
  95. + "<meta charset=\"UTF-8\">\n" + "<title>";
  96. private String errorHtmlSuffix = "</title>\n" + "</head>\n" + "</html>";
  97. @Override
  98. public int getPort() {
  99. return 80;
  100. }
  101. @Override
  102. public boolean isClosed() {
  103. return true;
  104. }
  105. @Override
  106. public boolean isSecure() {
  107. return false;
  108. }
  109. @Override
  110. public List<Packet> processMessage(Packet requestPacket) {
  111. String request = null;
  112. if (requestPacket != null) {
  113. request = requestPacket.toString();
  114. }
  115. List<Packet> responsePackets = new ArrayList<Packet>();
  116. this.request = request;
  117. if (!request.contains(httpVersion)) {
  118. responsePackets.add(buildPacket(STATUS_CODE_505, ""));
  119. } else if (request.contains(GET)) {
  120. responsePackets.add(buildPacket(STATUS_CODE_200, GET));
  121. } else if (request.contains(HEAD)) {
  122. responsePackets.add(buildPacket(STATUS_CODE_200, HEAD));
  123. } else if (request.contains(TRACE)) {
  124. responsePackets.add(buildPacket(STATUS_CODE_200, TRACE));
  125. } else if (request.contains(OPTIONS)) {
  126. responsePackets.add(buildPacket(STATUS_CODE_400, OPTIONS));
  127. } else if (request.contains(POST)) {
  128. responsePackets.add(buildPacket(STATUS_CODE_200, POST));
  129. } else if (request.contains(PUT)) {
  130. responsePackets.add(buildPacket(STATUS_CODE_400, PUT));
  131. } else if (request.contains(DELETE)) {
  132. responsePackets.add(buildPacket(STATUS_CODE_200, DELETE));
  133. } else if (request.contains(CONNECT)) {
  134. responsePackets.add(buildPacket(STATUS_CODE_400, CONNECT));
  135. } else {
  136. responsePackets.add(buildPacket(STATUS_CODE_400, ""));
  137. }
  138. return responsePackets;
  139. }
  140. @Override
  141. public String toString() {
  142. return "HTTP";
  143. }
  144. @Override
  145. public TALK_FIRST whoTalksFirst() {
  146. return TALK_FIRST.CLIENT;
  147. }
  148. /**
  149. * Builds a html response that can be sent
  150. *
  151. * @param code
  152. * response code that was determined
  153. * @param type
  154. * request type that was sent by the client
  155. * @return the html response
  156. */
  157. private Packet buildPacket(String code, String type) {
  158. String document = "";
  159. if (type.equals(GET)) {
  160. document = htmlDocument;
  161. } else if (type.equals(HEAD) || type.equals(DELETE)) {
  162. document = "";
  163. } else if (type.equals(TRACE)) {
  164. document = request;
  165. } else {
  166. document = errorHtmlPrefix + " " + code + errorHtmlSuffix;
  167. }
  168. return new Packet(httpVersion + " " + code + headerPrefix
  169. + document.length() + headerSuffix + document);
  170. }
  171. }