Client.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "Client.h"
  27. #include <AK/LexicalPath.h>
  28. #include <AK/StringBuilder.h>
  29. #include <LibCore/DateTime.h>
  30. #include <LibCore/DirIterator.h>
  31. #include <LibCore/File.h>
  32. #include <LibHTTP/HttpRequest.h>
  33. #include <stdio.h>
  34. #include <sys/stat.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. namespace WebServer {
  38. Client::Client(NonnullRefPtr<Core::TCPSocket> socket, Core::Object* parent)
  39. : Core::Object(parent)
  40. , m_socket(socket)
  41. {
  42. }
  43. void Client::die()
  44. {
  45. remove_from_parent();
  46. }
  47. void Client::start()
  48. {
  49. m_socket->on_ready_to_read = [this] {
  50. auto raw_request = m_socket->read_all();
  51. if (raw_request.is_null()) {
  52. die();
  53. return;
  54. }
  55. dbg() << "Got raw request: '" << String::copy(raw_request) << "'";
  56. handle_request(move(raw_request));
  57. die();
  58. };
  59. }
  60. void Client::handle_request(ByteBuffer raw_request)
  61. {
  62. auto request_or_error = HTTP::HttpRequest::from_raw_request(raw_request);
  63. if (!request_or_error.has_value())
  64. return;
  65. auto& request = request_or_error.value();
  66. dbg() << "Got HTTP request: " << request.method_name() << " " << request.resource();
  67. for (auto& header : request.headers()) {
  68. dbg() << " " << header.name << " => " << header.value;
  69. }
  70. if (request.method() != HTTP::HttpRequest::Method::GET) {
  71. send_error_response(403, "Forbidden!", request);
  72. return;
  73. }
  74. auto requested_path = LexicalPath::canonicalized_path(request.resource());
  75. dbg() << "Canonical requested path: '" << requested_path << "'";
  76. StringBuilder path_builder;
  77. path_builder.append("/www/");
  78. path_builder.append(requested_path);
  79. auto real_path = path_builder.to_string();
  80. if (Core::File::is_directory(real_path)) {
  81. if (!request.resource().ends_with("/")) {
  82. StringBuilder red;
  83. red.append(requested_path);
  84. red.append("/");
  85. send_redirect(red.to_string(), request);
  86. return;
  87. }
  88. StringBuilder index_html_path_builder;
  89. index_html_path_builder.append(real_path);
  90. index_html_path_builder.append("/index.html");
  91. auto index_html_path = index_html_path_builder.to_string();
  92. if (!Core::File::exists(index_html_path)) {
  93. handle_directory_listing(requested_path, real_path, request);
  94. return;
  95. }
  96. real_path = index_html_path;
  97. }
  98. auto file = Core::File::construct(real_path);
  99. if (!file->open(Core::File::ReadOnly)) {
  100. send_error_response(404, "Not found!", request);
  101. return;
  102. }
  103. send_response(file->read_all(), request);
  104. }
  105. void Client::send_response(StringView response, const HTTP::HttpRequest& request)
  106. {
  107. StringBuilder builder;
  108. builder.append("HTTP/1.0 200 OK\r\n");
  109. builder.append("Server: WebServer (SerenityOS)\r\n");
  110. builder.append("Content-Type: text/html\r\n");
  111. builder.append("\r\n");
  112. m_socket->write(builder.to_string());
  113. m_socket->write(response);
  114. log_response(200, request);
  115. }
  116. void Client::send_redirect(StringView redirect_path, const HTTP::HttpRequest& request)
  117. {
  118. StringBuilder builder;
  119. builder.append("HTTP/1.0 301 Moved Permanently\r\n");
  120. builder.append("Location: ");
  121. builder.append(redirect_path);
  122. builder.append("\r\n");
  123. builder.append("\r\n");
  124. m_socket->write(builder.to_string());
  125. log_response(301, request);
  126. }
  127. void Client::handle_directory_listing(const String& requested_path, const String& real_path, const HTTP::HttpRequest& request)
  128. {
  129. StringBuilder builder;
  130. builder.append("<!DOCTYPE html>\n");
  131. builder.append("<html>\n");
  132. builder.append("<head><title>Index of ");
  133. builder.append(escape_html_entities(requested_path));
  134. builder.append("</title></head>\n");
  135. builder.append("<body>\n");
  136. builder.append("<h1>Index of ");
  137. builder.append(escape_html_entities(requested_path));
  138. builder.append("</h1>\n");
  139. builder.append("<hr>\n");
  140. builder.append("<pre>\n");
  141. Core::DirIterator dt(real_path);
  142. while (dt.has_next()) {
  143. auto name = dt.next_path();
  144. builder.append("<a href=\"");
  145. // FIXME: urlencode
  146. builder.append(name);
  147. builder.append("\">");
  148. builder.append(escape_html_entities(name));
  149. builder.append("</a>");
  150. for (size_t i = 0; i < (40 - name.length()); ++i)
  151. builder.append(' ');
  152. StringBuilder path_builder;
  153. path_builder.append(real_path);
  154. path_builder.append('/');
  155. path_builder.append(name);
  156. struct stat st;
  157. memset(&st, 0, sizeof(st));
  158. int rc = stat(path_builder.to_string().characters(), &st);
  159. if (rc < 0) {
  160. perror("stat");
  161. }
  162. builder.appendf(" %10d", st.st_size);
  163. builder.appendf(" ");
  164. builder.append(Core::DateTime::from_timestamp(st.st_mtime).to_string());
  165. builder.append("\n");
  166. }
  167. builder.append("</pre>\n");
  168. builder.append("<hr>\n");
  169. builder.append("<i>Generated by WebServer (SerenityOS)</i>\n");
  170. builder.append("</body>\n");
  171. builder.append("</html>\n");
  172. send_response(builder.to_string(), request);
  173. }
  174. void Client::send_error_response(unsigned code, const StringView& message, const HTTP::HttpRequest& request)
  175. {
  176. StringBuilder builder;
  177. builder.appendf("HTTP/1.0 %u ", code);
  178. builder.append(message);
  179. builder.append("\r\n\r\n");
  180. builder.append("<!DOCTYPE html><html><body><h1>");
  181. builder.appendf("%u ", code);
  182. builder.append(message);
  183. builder.append("</h1></body></html>");
  184. m_socket->write(builder.to_string());
  185. log_response(code, request);
  186. }
  187. void Client::log_response(unsigned code, const HTTP::HttpRequest& request)
  188. {
  189. printf("%s :: %03u :: %s %s\n",
  190. Core::DateTime::now().to_string().characters(),
  191. code,
  192. request.method_name().characters(),
  193. request.resource().characters());
  194. }
  195. }