HTTP.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. @Override
  78. public SSLContext getSSLContext() {
  79. return null;
  80. }
  81. /**
  82. * Builds a html response that can be sent
  83. * @param code response code that was determined
  84. * @param type request type that was sent by the client
  85. * @return the html response
  86. */
  87. private String buildPacket(String code, String type) {
  88. String doc = "";
  89. if(type.equals(get)) doc = htmlDoc;
  90. else if(type.equals(head)) doc = "";
  91. else if(type.equals(trace)) doc = request;
  92. else doc = errorHtmlPrefix + code + errorHtmlSuffix;
  93. return version + code + headerPrefix + doc.length() + headerSuffix + doc;
  94. }
  95. /** Whole request that was sent by the client */
  96. private String request = "";
  97. private String version = "HTTP/1.1";
  98. //request codes
  99. private String options = "OPTIONS";
  100. private String get = "GET";
  101. private String head = "HEAD";
  102. private String post = "POST";
  103. private String put = "PUT";
  104. private String delete = "DELETE";
  105. private String trace = "TRACE";
  106. private String connect = "CONNECT";
  107. //response codes
  108. private String ok = " 200 OK\r\n";
  109. private String badRequest = " 400 Bad Request\r\n";
  110. private String methodNotAllowed = " 405 Method Not Allowed\r\n";
  111. private String versionUnsupported = " 505 HTTP Version not supported\r\n";
  112. //html header pre and suffix
  113. private String headerPrefix =
  114. "Date: Mon, 01 Jul 2013 18:27:55 GMT\r\n" +
  115. "Server: Apache/2.2.22 (Debian)\r\n" +
  116. "Vary: Accept-Encoding\r\n" +
  117. "Content-Length: ";
  118. private String headerSuffix =
  119. "\r\n" +
  120. "Keep-Alive: timeout=5, max=100\r\n" +
  121. "Connection: Keep-Alive\r\n" +
  122. "Content-Type: text/html\r\n" +
  123. "\r\n";
  124. //html website
  125. private String htmlDoc =
  126. "<!doctype html>\n" +
  127. "<html lang=\"en\">\n" +
  128. "<head>\n" +
  129. "<meta charset=\"UTF-8\">\n" +
  130. "<title>Test successful</title>\n" +
  131. "</head>\n" +
  132. "<body>\n" +
  133. "<h1>Test successful</h1>\n" +
  134. "<p>Congratulations.</p>\n" +
  135. "</body>\n" +
  136. "</html>";
  137. //html error pre and suffix
  138. private String errorHtmlPrefix =
  139. "<!doctype html>\n" +
  140. "<html lang=\"en\">\n" +
  141. "<head>\n" +
  142. "<meta charset=\"UTF-8\">\n" +
  143. "<title>";
  144. private String errorHtmlSuffix =
  145. "</title>\n" +
  146. "</head>\n" +
  147. "<body>\n" +
  148. "</body>\n" +
  149. "</html>";
  150. }