HTTP.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package de.tudarmstadt.informatik.hostage.protocol;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.net.ssl.SSLContext;
  5. /**
  6. * HTTP protocol
  7. * @author Wulf Pfeiffer
  8. */
  9. public final class HTTP implements Protocol<String> {
  10. /**
  11. * Represents the states of the protocol
  12. */
  13. private enum STATE {
  14. NONE, CLOSED
  15. }
  16. /**
  17. * Denotes in which state the protocol is right now
  18. */
  19. private STATE state = STATE.NONE;
  20. @Override
  21. public int getPort() {
  22. return 80;
  23. }
  24. @Override
  25. public TALK_FIRST whoTalksFirst() {
  26. return TALK_FIRST.CLIENT;
  27. }
  28. @Override
  29. public List<String> processMessage(String message) {
  30. List<String> response = new ArrayList<String>();
  31. request = message + request;
  32. switch(state) {
  33. case NONE:
  34. if(!message.contains(version)){
  35. response.add(buildPacket(versionUnsupported, ""));
  36. } else if(message.contains(get)) {
  37. response.add(buildPacket(ok, get));
  38. } else if(message.contains(head)) {
  39. response.add(buildPacket(ok, head));
  40. } else if(message.contains(trace)){
  41. response.add(buildPacket(ok, trace));
  42. } else if(message.contains(options)){
  43. response.add(buildPacket(methodNotAllowed, options));
  44. } else if(message.contains(post)){
  45. response.add(buildPacket(methodNotAllowed, post));
  46. } else if(message.contains(put)){
  47. response.add(buildPacket(methodNotAllowed, put));
  48. } else if(message.contains(delete)){
  49. response.add(buildPacket(methodNotAllowed, delete));
  50. } else if(message.contains(connect)){
  51. response.add(buildPacket(methodNotAllowed, connect));
  52. } else {
  53. response.add(buildPacket(badRequest, ""));
  54. }
  55. state = STATE.CLOSED;
  56. default:
  57. state = STATE.CLOSED;
  58. }
  59. return response;
  60. }
  61. @Override
  62. public boolean isClosed() {
  63. return state == STATE.CLOSED;
  64. }
  65. @Override
  66. public boolean isSecure() {
  67. return false;
  68. }
  69. @Override
  70. public Class<String> getType() {
  71. return String.class;
  72. }
  73. @Override
  74. public String toString() {
  75. return "HTTP";
  76. }
  77. /**
  78. * Builds a html response that can be sent
  79. * @param code response code that was determined
  80. * @param type request type that was sent by the client
  81. * @return the html response
  82. */
  83. private String buildPacket(String code, String type) {
  84. String doc = "";
  85. if(type.equals(get)) doc = htmlDoc;
  86. else if(type.equals(head)) doc = "";
  87. else if(type.equals(trace)) doc = request;
  88. else doc = errorHtmlPrefix + code + errorHtmlSuffix;
  89. return version + code + headerPrefix + doc.length() + headerSuffix + doc;
  90. }
  91. /** Whole request that was sent by the client */
  92. private String request = "";
  93. private String version = "HTTP/1.1";
  94. //request codes
  95. private String options = "OPTIONS";
  96. private String get = "GET";
  97. private String head = "HEAD";
  98. private String post = "POST";
  99. private String put = "PUT";
  100. private String delete = "DELETE";
  101. private String trace = "TRACE";
  102. private String connect = "CONNECT";
  103. //response codes
  104. private String ok = " 200 OK\r\n";
  105. private String badRequest = " 400 Bad Request\r\n";
  106. private String methodNotAllowed = " 405 Method Not Allowed\r\n";
  107. private String versionUnsupported = " 505 HTTP Version not supported\r\n";
  108. //html header pre and suffix
  109. private String headerPrefix =
  110. "Date: Mon, 01 Jul 2013 18:27:55 GMT\r\n" +
  111. "Server: Apache/2.2.22 (Debian)\r\n" +
  112. "Vary: Accept-Encoding\r\n" +
  113. "Content-Length: ";
  114. private String headerSuffix =
  115. "\r\n" +
  116. "Keep-Alive: timeout=5, max=100\r\n" +
  117. "Connection: Keep-Alive\r\n" +
  118. "Content-Type: text/html\r\n" +
  119. "\r\n";
  120. //html website
  121. private String htmlDoc =
  122. "<!doctype html>\n" +
  123. "<html lang=\"en\">\n" +
  124. "<head>\n" +
  125. "<meta charset=\"UTF-8\">\n" +
  126. "<title>Test successful</title>\n" +
  127. "</head>\n" +
  128. "<body>\n" +
  129. "<h1>Test successful</h1>\n" +
  130. "<p>Congratulations.</p>\n" +
  131. "</body>\n" +
  132. "</html>";
  133. //html error pre and suffix
  134. private String errorHtmlPrefix =
  135. "<!doctype html>\n" +
  136. "<html lang=\"en\">\n" +
  137. "<head>\n" +
  138. "<meta charset=\"UTF-8\">\n" +
  139. "<title>";
  140. private String errorHtmlSuffix =
  141. "</title>\n" +
  142. "</head>\n" +
  143. "<body>\n" +
  144. "</body>\n" +
  145. "</html>";
  146. }