HttpRequest.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2018-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 <AK/StringBuilder.h>
  27. #include <LibHTTP/HttpJob.h>
  28. #include <LibHTTP/HttpRequest.h>
  29. namespace HTTP {
  30. HttpRequest::HttpRequest()
  31. {
  32. }
  33. HttpRequest::~HttpRequest()
  34. {
  35. }
  36. String HttpRequest::method_name() const
  37. {
  38. switch (m_method) {
  39. case Method::GET:
  40. return "GET";
  41. case Method::HEAD:
  42. return "HEAD";
  43. case Method::POST:
  44. return "POST";
  45. default:
  46. ASSERT_NOT_REACHED();
  47. }
  48. }
  49. ByteBuffer HttpRequest::to_raw_request() const
  50. {
  51. StringBuilder builder;
  52. builder.append(method_name());
  53. builder.append(' ');
  54. builder.append(m_url.path());
  55. if (!m_url.query().is_empty()) {
  56. builder.append('?');
  57. builder.append(m_url.query());
  58. }
  59. builder.append(" HTTP/1.1\r\nHost: ");
  60. builder.append(m_url.host());
  61. builder.append("\r\n");
  62. for (auto& header : m_headers) {
  63. builder.append(header.name);
  64. builder.append(": ");
  65. builder.append(header.value);
  66. builder.append("\r\n");
  67. }
  68. builder.append("Connection: close\r\n\r\n");
  69. if (!m_body.is_empty()) {
  70. builder.append((const char*)m_body.data(), m_body.size());
  71. builder.append("\r\n");
  72. }
  73. return builder.to_byte_buffer();
  74. }
  75. Optional<HttpRequest> HttpRequest::from_raw_request(ReadonlyBytes raw_request)
  76. {
  77. enum class State {
  78. InMethod,
  79. InResource,
  80. InProtocol,
  81. InHeaderName,
  82. InHeaderValue,
  83. };
  84. State state { State::InMethod };
  85. size_t index = 0;
  86. auto peek = [&](int offset = 0) -> u8 {
  87. if (index + offset >= raw_request.size())
  88. return 0;
  89. return raw_request[index + offset];
  90. };
  91. auto consume = [&]() -> u8 {
  92. ASSERT(index < raw_request.size());
  93. return raw_request[index++];
  94. };
  95. Vector<u8, 256> buffer;
  96. String method;
  97. String resource;
  98. String protocol;
  99. Vector<Header> headers;
  100. Header current_header;
  101. auto commit_and_advance_to = [&](auto& output, State new_state) {
  102. output = String::copy(buffer);
  103. buffer.clear();
  104. state = new_state;
  105. };
  106. while (index < raw_request.size()) {
  107. // FIXME: Figure out what the appropriate limitations should be.
  108. if (buffer.size() > 65536)
  109. return {};
  110. switch (state) {
  111. case State::InMethod:
  112. if (peek() == ' ') {
  113. consume();
  114. commit_and_advance_to(method, State::InResource);
  115. break;
  116. }
  117. buffer.append(consume());
  118. break;
  119. case State::InResource:
  120. if (peek() == ' ') {
  121. consume();
  122. commit_and_advance_to(resource, State::InProtocol);
  123. break;
  124. }
  125. buffer.append(consume());
  126. break;
  127. case State::InProtocol:
  128. if (peek(0) == '\r' && peek(1) == '\n') {
  129. consume();
  130. consume();
  131. commit_and_advance_to(protocol, State::InHeaderName);
  132. break;
  133. }
  134. buffer.append(consume());
  135. break;
  136. case State::InHeaderName:
  137. if (peek(0) == ':' && peek(1) == ' ') {
  138. consume();
  139. consume();
  140. commit_and_advance_to(current_header.name, State::InHeaderValue);
  141. break;
  142. }
  143. buffer.append(consume());
  144. break;
  145. case State::InHeaderValue:
  146. if (peek(0) == '\r' && peek(1) == '\n') {
  147. consume();
  148. consume();
  149. commit_and_advance_to(current_header.value, State::InHeaderName);
  150. headers.append(move(current_header));
  151. break;
  152. }
  153. buffer.append(consume());
  154. break;
  155. }
  156. }
  157. HttpRequest request;
  158. if (method == "GET")
  159. request.m_method = Method::GET;
  160. else if (method == "HEAD")
  161. request.m_method = Method::HEAD;
  162. else if (method == "POST")
  163. request.m_method = Method::POST;
  164. else
  165. return {};
  166. request.m_resource = resource;
  167. request.m_headers = move(headers);
  168. return request;
  169. }
  170. void HttpRequest::set_headers(const HashMap<String, String>& headers)
  171. {
  172. for (auto& it : headers)
  173. m_headers.append({ it.key, it.value });
  174. }
  175. }