metalink_helper.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "metalink_helper.h"
  36. #include <array>
  37. #include "Option.h"
  38. #include "MetalinkEntry.h"
  39. #include "MetalinkParserStateMachine.h"
  40. #include "Metalinker.h"
  41. #include "XmlParser.h"
  42. #include "prefs.h"
  43. #include "DlAbortEx.h"
  44. #include "BinaryStream.h"
  45. #include "MetalinkMetaurl.h"
  46. #include "a2functional.h"
  47. namespace aria2 {
  48. namespace metalink {
  49. namespace {
  50. std::vector<std::unique_ptr<MetalinkEntry>>
  51. query(const std::shared_ptr<Metalinker>& metalinker, const Option* option)
  52. {
  53. return metalinker->queryEntry(option->get(PREF_METALINK_VERSION),
  54. option->get(PREF_METALINK_LANGUAGE),
  55. option->get(PREF_METALINK_OS));
  56. }
  57. } // namespace
  58. std::vector<std::unique_ptr<MetalinkEntry>>
  59. parseAndQuery(const std::string& filename, const Option* option,
  60. const std::string& baseUri)
  61. {
  62. return query(parseFile(filename, baseUri), option);
  63. }
  64. std::vector<std::unique_ptr<MetalinkEntry>>
  65. parseAndQuery(BinaryStream* bs, const Option* option,
  66. const std::string& baseUri)
  67. {
  68. return query(parseBinaryStream(bs, baseUri), option);
  69. }
  70. std::vector<std::pair<std::string, std::vector<MetalinkEntry*>>>
  71. groupEntryByMetaurlName(
  72. const std::vector<std::unique_ptr<MetalinkEntry>>& entries)
  73. {
  74. std::vector<std::pair<std::string, std::vector<MetalinkEntry*>>> result;
  75. for (auto& entry : entries) {
  76. if (entry->metaurls.empty()) {
  77. // std::pair<std::string, std::vector<MetalinkEntry*>> p;
  78. // p.second.push_back(entry.get());
  79. result.push_back({"", {entry.get()}});
  80. }
  81. else {
  82. auto i = std::begin(result);
  83. if (entry->metaurls[0]->name.empty() || !entry->sizeKnown) {
  84. i = std::end(result);
  85. }
  86. for (; i != std::end(result); ++i) {
  87. if ((*i).first == entry->metaurls[0]->url &&
  88. !(*i).second[0]->metaurls[0]->name.empty()) {
  89. (*i).second.push_back(entry.get());
  90. break;
  91. }
  92. }
  93. if (i == std::end(result)) {
  94. result.push_back({entry->metaurls[0]->url, {entry.get()}});
  95. }
  96. }
  97. }
  98. return result;
  99. }
  100. std::unique_ptr<Metalinker> parseFile(const std::string& filename,
  101. const std::string& baseUri)
  102. {
  103. MetalinkParserStateMachine psm;
  104. psm.setBaseUri(baseUri);
  105. if (!xml::parseFile(filename, &psm)) {
  106. throw DL_ABORT_EX2("Could not parse Metalink XML document.",
  107. error_code::METALINK_PARSE_ERROR);
  108. }
  109. if (!psm.getErrors().empty()) {
  110. throw DL_ABORT_EX2(psm.getErrorString(), error_code::METALINK_PARSE_ERROR);
  111. }
  112. return psm.getResult();
  113. }
  114. std::unique_ptr<Metalinker> parseBinaryStream(BinaryStream* bs,
  115. const std::string& baseUri)
  116. {
  117. MetalinkParserStateMachine psm;
  118. psm.setBaseUri(baseUri);
  119. xml::XmlParser ps(&psm);
  120. std::array<unsigned char, 4_k> buf;
  121. ssize_t nread;
  122. int64_t offread = 0;
  123. bool retval = true;
  124. while ((nread = bs->readData(buf.data(), buf.size(), offread)) > 0) {
  125. if (ps.parseUpdate(reinterpret_cast<const char*>(buf.data()), nread) < 0) {
  126. retval = false;
  127. break;
  128. }
  129. offread += nread;
  130. }
  131. if (nread == 0 && retval) {
  132. if (ps.parseFinal(nullptr, 0) < 0) {
  133. retval = false;
  134. }
  135. }
  136. if (!retval) {
  137. throw DL_ABORT_EX2("Could not parse Metalink XML document.",
  138. error_code::METALINK_PARSE_ERROR);
  139. }
  140. if (!psm.getErrors().empty()) {
  141. throw DL_ABORT_EX2(psm.getErrorString(), error_code::METALINK_PARSE_ERROR);
  142. }
  143. return psm.getResult();
  144. }
  145. } // namespace metalink
  146. } // namespace aria2