HttpRequest.cpp 5.6 KB

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