HTTP.java 4.0 KB

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