HttpServerCommand.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 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 "HttpServerCommand.h"
  36. #include <sstream>
  37. #include <algorithm>
  38. #include <iostream>
  39. #include <iomanip>
  40. #include "SocketCore.h"
  41. #include "DownloadEngine.h"
  42. #include "HttpServer.h"
  43. #include "HttpHeader.h"
  44. #include "Logger.h"
  45. #include "RequestGroup.h"
  46. #include "RequestGroupMan.h"
  47. #include "BtContext.h"
  48. #include "Util.h"
  49. #include "HttpServerResponseCommand.h"
  50. #include "CheckIntegrityEntry.h"
  51. #include "FileAllocationEntry.h"
  52. #include "RecoverableException.h"
  53. namespace aria2 {
  54. HttpServerCommand::HttpServerCommand(int32_t cuid, DownloadEngine* e,
  55. const SharedHandle<SocketCore>& socket):
  56. Command(cuid),
  57. _e(e),
  58. _socket(socket),
  59. _httpServer(new HttpServer(socket, e))
  60. {
  61. _e->addSocketForReadCheck(_socket, this);
  62. }
  63. HttpServerCommand::HttpServerCommand(int32_t cuid,
  64. const SharedHandle<HttpServer>& httpServer,
  65. DownloadEngine* e,
  66. const SharedHandle<SocketCore>& socket):
  67. Command(cuid),
  68. _e(e),
  69. _socket(socket),
  70. _httpServer(httpServer)
  71. {
  72. _e->addSocketForReadCheck(_socket, this);
  73. }
  74. HttpServerCommand::~HttpServerCommand()
  75. {
  76. _e->deleteSocketForReadCheck(_socket, this);
  77. }
  78. class PrintSummaryHtml
  79. {
  80. private:
  81. std::ostream& _o;
  82. public:
  83. PrintSummaryHtml(std::ostream& o):_o(o) {}
  84. void operator()(const SharedHandle<RequestGroup>& rg)
  85. {
  86. _o << "<div id=\"gid" << rg->getGID() << "\">"
  87. << "[#" << rg->getGID() << "]"
  88. << " FILE:" << "<strong>"
  89. << Util::htmlEscape(rg->getFilePath()) << "</strong>";
  90. #ifdef ENABLE_BITTORRENT
  91. SharedHandle<BtContext> btContext =
  92. dynamic_pointer_cast<BtContext>(rg->getDownloadContext());
  93. if(!btContext.isNull()) {
  94. _o << "<br />" << " Info Hash:" << btContext->getInfoHashAsString();
  95. }
  96. #endif // ENABLE_BITTORRENT
  97. _o << "<br />";
  98. TransferStat stat = rg->calculateStat();
  99. unsigned int eta = 0;
  100. if(rg->getTotalLength() > 0 && stat.getDownloadSpeed() > 0) {
  101. eta =
  102. (rg->getTotalLength()-rg->getCompletedLength())/stat.getDownloadSpeed();
  103. }
  104. #ifdef ENABLE_BITTORRENT
  105. if(!btContext.isNull() && rg->downloadFinished()) {
  106. _o << "SEEDING" << "(" << "ratio:"
  107. << std::fixed << std::setprecision(1)
  108. << ((stat.getAllTimeUploadLength()*10)/rg->getCompletedLength())/10.0
  109. << ") ";
  110. }
  111. #endif // ENABLE_BITTORRENT
  112. _o << Util::abbrevSize(rg->getCompletedLength())
  113. << "B"
  114. << "/"
  115. << Util::abbrevSize(rg->getTotalLength())
  116. << "B";
  117. if(rg->getTotalLength() > 0) {
  118. _o << "("
  119. << 100*rg->getCompletedLength()/rg->getTotalLength()
  120. << "%)";
  121. }
  122. _o << " "
  123. << "CN:"
  124. << rg->getNumConnection();
  125. if(!rg->downloadFinished()) {
  126. _o << " "
  127. << "SPD:"
  128. << std::fixed << std::setprecision(2)
  129. << stat.getDownloadSpeed()/1024.0 << "KiB/s";
  130. }
  131. if(stat.getSessionUploadLength() > 0) {
  132. _o << " "
  133. << "UP:"
  134. << std::fixed << std::setprecision(2)
  135. << stat.getUploadSpeed()/1024.0 << "KiB/s"
  136. << "(" << Util::abbrevSize(stat.getAllTimeUploadLength()) << "B)";
  137. }
  138. if(eta > 0) {
  139. _o << " "
  140. << "ETA:"
  141. << Util::htmlEscape(Util::secfmt(eta));
  142. }
  143. _o << "</div>"
  144. << "<hr />";
  145. }
  146. };
  147. static std::string createResponse(DownloadEngine* e)
  148. {
  149. std::ostringstream strm;
  150. const std::deque<SharedHandle<RequestGroup> > groups =
  151. e->_requestGroupMan->getRequestGroups();
  152. std::for_each(groups.begin(), groups.end(), PrintSummaryHtml(strm));
  153. {
  154. SharedHandle<FileAllocationEntry> entry =
  155. e->_fileAllocationMan->getPickedEntry();
  156. if(!entry.isNull()) {
  157. strm << "<div id=\"filealloc\">"
  158. << "[FileAlloc:"
  159. << "#" << entry->getRequestGroup()->getGID() << " "
  160. << Util::abbrevSize(entry->getCurrentLength())
  161. << "B"
  162. << "/"
  163. << Util::abbrevSize(entry->getTotalLength())
  164. << "B"
  165. << "(";
  166. if(entry->getTotalLength() > 0) {
  167. strm << 100*entry->getCurrentLength()/entry->getTotalLength();
  168. } else {
  169. strm << "--";
  170. }
  171. strm << "%)"
  172. << "]";
  173. if(e->_fileAllocationMan->hasNext()) {
  174. strm << "("
  175. << e->_fileAllocationMan->countEntryInQueue()
  176. << "waiting...)";
  177. }
  178. strm << "</div><hr />";
  179. }
  180. }
  181. #ifdef ENABLE_MESSAGE_DIGEST
  182. {
  183. SharedHandle<CheckIntegrityEntry> entry =
  184. e->_checkIntegrityMan->getPickedEntry();
  185. if(!entry.isNull()) {
  186. strm << "<div id=\"hashcheck\">"
  187. << "[HashCheck:"
  188. << "#" << entry->getRequestGroup()->getGID() << " "
  189. << Util::abbrevSize(entry->getCurrentLength())
  190. << "B"
  191. << "/"
  192. << Util::abbrevSize(entry->getTotalLength())
  193. << "B"
  194. << "("
  195. << 100*entry->getCurrentLength()/entry->getTotalLength()
  196. << "%)"
  197. << "]";
  198. if(e->_checkIntegrityMan->hasNext()) {
  199. strm << "("
  200. << e->_checkIntegrityMan->countEntryInQueue()
  201. << "waiting...)";
  202. }
  203. strm << "</div><hr />";
  204. }
  205. }
  206. #endif // ENABLE_MESSAGE_DIGEST
  207. std::string body =
  208. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\""
  209. " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
  210. "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">"
  211. "<head>"
  212. "<meta http-equiv=\"refresh\" content=\"1\" />"
  213. "<title>aria2</title>"
  214. "</head>"
  215. "<body><h1>aria2 - Download Progress</h1>"+strm.str()+"</body>"
  216. "</html>";
  217. return body;
  218. }
  219. bool HttpServerCommand::execute()
  220. {
  221. if(_e->_requestGroupMan->downloadFinished() || _e->isHaltRequested()) {
  222. return true;
  223. }
  224. if(_socket->isReadable(0)) {
  225. _timeout.reset();
  226. SharedHandle<HttpHeader> header;
  227. try {
  228. header = _httpServer->receiveRequest();
  229. } catch(RecoverableException& e) {
  230. logger->info("CUID#%d - Error occurred while reading HTTP request",
  231. e, cuid);
  232. return true;
  233. }
  234. if(header.isNull()) {
  235. _e->commands.push_back(this);
  236. return false;
  237. } else {
  238. _httpServer->feedResponse(createResponse(_e));
  239. Command* command = new HttpServerResponseCommand(cuid, _httpServer, _e,
  240. _socket);
  241. command->setStatus(Command::STATUS_ONESHOT_REALTIME);
  242. _e->commands.push_back(command);
  243. _e->setNoWait(true);
  244. return true;
  245. }
  246. } else {
  247. if(_timeout.elapsed(30)) {
  248. logger->info("HTTP request timeout.");
  249. return true;
  250. } else {
  251. _e->commands.push_back(this);
  252. return false;
  253. }
  254. }
  255. }
  256. } // namespace aria2