From 8f93855bbd826353ca77a30996a4100cea47b55d Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Sat, 6 Aug 2011 22:43:24 +0900 Subject: [PATCH] Rewritten ProtocolDetector using BufferedFile --- src/ProtocolDetector.cc | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/ProtocolDetector.cc b/src/ProtocolDetector.cc index 1daa1991..7ca0ba8a 100644 --- a/src/ProtocolDetector.cc +++ b/src/ProtocolDetector.cc @@ -35,7 +35,6 @@ #include "ProtocolDetector.h" #include -#include #include #include "Request.h" @@ -43,6 +42,7 @@ #include "util.h" #include "RecoverableException.h" #include "uri.h" +#include "BufferedFile.h" #ifdef ENABLE_BITTORRENT # include "bittorrent_helper.h" #endif // ENABLE_BITTORRENT @@ -61,14 +61,14 @@ bool ProtocolDetector::isStreamProtocol(const std::string& uri) const bool ProtocolDetector::guessTorrentFile(const std::string& uri) const { - if(!File(uri).isFile()) { - return false; - } - std::ifstream in(uri.c_str(), std::ios::binary); - if(in) { - char head; - in >> head; - return head == 'd'; + BufferedFile fp(uri, BufferedFile::READ); + if(fp) { + char head[1]; + if(fp.read(head, sizeof(head)) == sizeof(head)) { + return head[0] == 'd'; + } else { + return false; + } } else { return false; } @@ -90,14 +90,14 @@ bool ProtocolDetector::guessTorrentMagnet(const std::string& uri) const bool ProtocolDetector::guessMetalinkFile(const std::string& uri) const { - if(!File(uri).isFile()) { - return false; - } - std::ifstream in(uri.c_str(), std::ios::binary); - if(in) { - char head[6]; - in >> std::setw(6) >> head; - return strcmp(head, "