pro.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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/FileStream.h>
  27. #include <AK/GenericLexer.h>
  28. #include <AK/LexicalPath.h>
  29. #include <AK/NumberFormat.h>
  30. #include <AK/URL.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCore/EventLoop.h>
  33. #include <LibCore/File.h>
  34. #include <LibProtocol/Client.h>
  35. #include <LibProtocol/Download.h>
  36. #include <ctype.h>
  37. #include <stdio.h>
  38. // FIXME: Move this somewhere else when it's needed (e.g. in the Browser)
  39. class ContentDispositionParser {
  40. public:
  41. ContentDispositionParser(const StringView& value)
  42. {
  43. GenericLexer lexer(value);
  44. lexer.ignore_while(isspace);
  45. if (lexer.consume_specific("inline")) {
  46. m_kind = Kind::Inline;
  47. if (!lexer.is_eof())
  48. m_might_be_wrong = true;
  49. return;
  50. }
  51. if (lexer.consume_specific("attachment")) {
  52. m_kind = Kind::Attachment;
  53. if (lexer.consume_specific(";")) {
  54. lexer.ignore_while(isspace);
  55. if (lexer.consume_specific("filename=")) {
  56. // RFC 2183: "A short (length <= 78 characters)
  57. // parameter value containing only non-`tspecials' characters SHOULD be
  58. // represented as a single `token'."
  59. // Some people seem to take this as generic advice of "if it doesn't have special characters,
  60. // it's safe to specify as a single token"
  61. // So let's just be as lenient as possible.
  62. if (lexer.next_is('"'))
  63. m_filename = lexer.consume_quoted_string();
  64. else
  65. m_filename = lexer.consume_until(is_any_of("()<>@,;:\\\"/[]?= "));
  66. } else {
  67. m_might_be_wrong = true;
  68. }
  69. }
  70. return;
  71. }
  72. if (lexer.consume_specific("form-data")) {
  73. m_kind = Kind::FormData;
  74. while (lexer.consume_specific(";")) {
  75. lexer.ignore_while(isspace);
  76. if (lexer.consume_specific("name=")) {
  77. m_name = lexer.consume_quoted_string();
  78. } else if (lexer.consume_specific("filename=")) {
  79. if (lexer.next_is('"'))
  80. m_filename = lexer.consume_quoted_string();
  81. else
  82. m_filename = lexer.consume_until(is_any_of("()<>@,;:\\\"/[]?= "));
  83. } else {
  84. m_might_be_wrong = true;
  85. }
  86. }
  87. return;
  88. }
  89. // FIXME: Support 'filename*'
  90. m_might_be_wrong = true;
  91. }
  92. enum class Kind {
  93. Inline,
  94. Attachment,
  95. FormData,
  96. };
  97. const StringView& filename() const { return m_filename; }
  98. const StringView& name() const { return m_name; }
  99. Kind kind() const { return m_kind; }
  100. bool might_be_wrong() const { return m_might_be_wrong; }
  101. private:
  102. StringView m_filename;
  103. StringView m_name;
  104. Kind m_kind { Kind::Inline };
  105. bool m_might_be_wrong { false };
  106. };
  107. template<typename ConditionT>
  108. class ConditionalOutputFileStream final : public OutputFileStream {
  109. public:
  110. template<typename... Args>
  111. ConditionalOutputFileStream(ConditionT&& condition, Args... args)
  112. : OutputFileStream(args...)
  113. , m_condition(condition)
  114. {
  115. }
  116. ~ConditionalOutputFileStream()
  117. {
  118. if (!m_condition())
  119. return;
  120. if (!m_buffer.is_empty()) {
  121. OutputFileStream::write(m_buffer);
  122. m_buffer.clear();
  123. }
  124. }
  125. private:
  126. size_t write(ReadonlyBytes bytes) override
  127. {
  128. if (!m_condition()) {
  129. write_to_buffer:;
  130. m_buffer.append(bytes.data(), bytes.size());
  131. return bytes.size();
  132. }
  133. if (!m_buffer.is_empty()) {
  134. auto size = OutputFileStream::write(m_buffer);
  135. m_buffer = m_buffer.slice(size, m_buffer.size() - size);
  136. }
  137. if (!m_buffer.is_empty())
  138. goto write_to_buffer;
  139. return OutputFileStream::write(bytes);
  140. }
  141. ConditionT m_condition;
  142. ByteBuffer m_buffer;
  143. };
  144. int main(int argc, char** argv)
  145. {
  146. const char* url_str = nullptr;
  147. bool save_at_provided_name = false;
  148. const char* data = nullptr;
  149. String method = "GET";
  150. HashMap<String, String, CaseInsensitiveStringTraits> request_headers;
  151. Core::ArgsParser args_parser;
  152. args_parser.set_general_help(
  153. "Download a file from an arbitrary URL. This command uses ProtocolServer, "
  154. "and thus supports at least http, https, and gemini.");
  155. args_parser.add_option(save_at_provided_name, "Write to a file named as the remote file", nullptr, 'O');
  156. args_parser.add_option(data, "(HTTP only) Send the provided data via an HTTP POST request", "data", 'd', "data");
  157. args_parser.add_option(Core::ArgsParser::Option {
  158. .requires_argument = true,
  159. .help_string = "Add a header entry to the request",
  160. .long_name = "header",
  161. .short_name = 'H',
  162. .value_name = "header-value",
  163. .accept_value = [&](auto* s) {
  164. StringView header { s };
  165. auto split = header.find_first_of(':');
  166. if (!split.has_value())
  167. return false;
  168. request_headers.set(header.substring_view(0, split.value()), header.substring_view(split.value() + 1));
  169. return true;
  170. } });
  171. args_parser.add_positional_argument(url_str, "URL to download from", "url");
  172. args_parser.parse(argc, argv);
  173. if (data) {
  174. method = "POST";
  175. // FIXME: Content-Type?
  176. }
  177. URL url(url_str);
  178. if (!url.is_valid()) {
  179. fprintf(stderr, "'%s' is not a valid URL\n", url_str);
  180. return 1;
  181. }
  182. Core::EventLoop loop;
  183. auto protocol_client = Protocol::Client::construct();
  184. auto download = protocol_client->start_download(method, url.to_string(), request_headers, data ? StringView { data }.bytes() : ReadonlyBytes {});
  185. if (!download) {
  186. fprintf(stderr, "Failed to start download for '%s'\n", url_str);
  187. return 1;
  188. }
  189. u32 previous_downloaded_size { 0 };
  190. timeval prev_time, current_time, time_diff;
  191. gettimeofday(&prev_time, nullptr);
  192. bool received_actual_headers = false;
  193. download->on_progress = [&](Optional<u32> maybe_total_size, u32 downloaded_size) {
  194. fprintf(stderr, "\r\033[2K");
  195. if (maybe_total_size.has_value()) {
  196. fprintf(stderr, "\033]9;%d;%d;\033\\", downloaded_size, maybe_total_size.value());
  197. fprintf(stderr, "Download progress: %s / %s", human_readable_size(downloaded_size).characters(), human_readable_size(maybe_total_size.value()).characters());
  198. } else {
  199. fprintf(stderr, "Download progress: %s / ???", human_readable_size(downloaded_size).characters());
  200. }
  201. gettimeofday(&current_time, nullptr);
  202. timersub(&current_time, &prev_time, &time_diff);
  203. auto time_diff_ms = time_diff.tv_sec * 1000 + time_diff.tv_usec / 1000;
  204. auto size_diff = downloaded_size - previous_downloaded_size;
  205. fprintf(stderr, " at %s/s", human_readable_size(((float)size_diff / (float)time_diff_ms) * 1000).characters());
  206. previous_downloaded_size = downloaded_size;
  207. prev_time = current_time;
  208. };
  209. if (save_at_provided_name) {
  210. download->on_headers_received = [&](auto& response_headers, auto status_code) {
  211. if (received_actual_headers)
  212. return;
  213. dbgln("Received headers! response code = {}", status_code.value_or(0));
  214. received_actual_headers = true; // And not trailers!
  215. String output_name;
  216. if (auto content_disposition = response_headers.get("Content-Disposition"); content_disposition.has_value()) {
  217. auto& value = content_disposition.value();
  218. ContentDispositionParser parser(value);
  219. output_name = parser.filename();
  220. }
  221. if (output_name.is_empty())
  222. output_name = url.path();
  223. LexicalPath path { output_name };
  224. output_name = path.basename();
  225. // The URL didn't have a name component, e.g. 'serenityos.org'
  226. if (output_name.is_empty() || output_name == "/") {
  227. int i = -1;
  228. do {
  229. output_name = url.host();
  230. if (i > -1)
  231. output_name = String::formatted("{}.{}", output_name, i);
  232. ++i;
  233. } while (Core::File::exists(output_name));
  234. }
  235. if (freopen(output_name.characters(), "w", stdout) == nullptr) {
  236. perror("freopen");
  237. loop.quit(1);
  238. return;
  239. }
  240. };
  241. }
  242. download->on_finish = [&](bool success, auto) {
  243. fprintf(stderr, "\033]9;-1;\033\\");
  244. fprintf(stderr, "\n");
  245. if (!success)
  246. fprintf(stderr, "Download failed :(\n");
  247. loop.quit(0);
  248. };
  249. auto output_stream = ConditionalOutputFileStream { [&] { return save_at_provided_name ? received_actual_headers : true; }, stdout };
  250. download->stream_into(output_stream);
  251. dbgln("started download with id {}", download->id());
  252. auto rc = loop.exec();
  253. // FIXME: This shouldn't be needed.
  254. fclose(stdout);
  255. return rc;
  256. }