HttpRequest.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. builder.append(" HTTP/1.1\r\nHost: ");
  62. builder.append(m_url.host());
  63. builder.append("\r\nConnection: close\r\n\r\n");
  64. return builder.to_byte_buffer();
  65. }
  66. Optional<HttpRequest> HttpRequest::from_raw_request(const ByteBuffer& raw_request)
  67. {
  68. enum class State {
  69. InMethod,
  70. InResource,
  71. InProtocol,
  72. InHeaderName,
  73. InHeaderValue,
  74. };
  75. State state { State::InMethod };
  76. size_t index = 0;
  77. auto peek = [&](int offset = 0) -> u8 {
  78. if (index + offset >= raw_request.size())
  79. return 0;
  80. return raw_request[index + offset];
  81. };
  82. auto consume = [&]() -> u8 {
  83. ASSERT(index < raw_request.size());
  84. return raw_request[index++];
  85. };
  86. Vector<u8, 256> buffer;
  87. String method;
  88. String resource;
  89. String protocol;
  90. Vector<Header> headers;
  91. Header current_header;
  92. auto commit_and_advance_to = [&](auto& output, State new_state) {
  93. output = String::copy(buffer);
  94. buffer.clear();
  95. state = new_state;
  96. };
  97. while (index < raw_request.size()) {
  98. // FIXME: Figure out what the appropriate limitations should be.
  99. if (buffer.size() > 65536)
  100. return {};
  101. switch (state) {
  102. case State::InMethod:
  103. if (peek() == ' ') {
  104. consume();
  105. commit_and_advance_to(method, State::InResource);
  106. break;
  107. }
  108. buffer.append(consume());
  109. break;
  110. case State::InResource:
  111. if (peek() == ' ') {
  112. consume();
  113. commit_and_advance_to(resource, State::InProtocol);
  114. break;
  115. }
  116. buffer.append(consume());
  117. break;
  118. case State::InProtocol:
  119. if (peek(0) == '\r' && peek(1) == '\n') {
  120. consume();
  121. consume();
  122. commit_and_advance_to(protocol, State::InHeaderName);
  123. break;
  124. }
  125. buffer.append(consume());
  126. break;
  127. case State::InHeaderName:
  128. if (peek(0) == ':' && peek(1) == ' ') {
  129. consume();
  130. consume();
  131. commit_and_advance_to(current_header.name, State::InHeaderValue);
  132. break;
  133. }
  134. buffer.append(consume());
  135. break;
  136. case State::InHeaderValue:
  137. if (peek(0) == '\r' && peek(1) == '\n') {
  138. consume();
  139. consume();
  140. commit_and_advance_to(current_header.value, State::InHeaderName);
  141. headers.append(move(current_header));
  142. break;
  143. }
  144. buffer.append(consume());
  145. break;
  146. }
  147. }
  148. HttpRequest request;
  149. if (method == "GET")
  150. request.m_method = Method::GET;
  151. else if (method == "HEAD")
  152. request.m_method = Method::HEAD;
  153. else if (method == "POST")
  154. request.m_method = Method::POST;
  155. else
  156. return {};
  157. request.m_resource = resource;
  158. request.m_headers = move(headers);
  159. return request;
  160. }
  161. }