added error category and error codes for HTTP errors

This commit is contained in:
Arvid Norberg
2011-01-16 02:54:59 +00:00
parent f87b0b4617
commit e31aceeda4
8 changed files with 123 additions and 6 deletions

View File

@ -1,3 +1,4 @@
* added error category and error codes for HTTP errors
* made the DHT implementation slightly more robust against routing table poisoning and node ID spoofing
* support chunked encoding in http downloads (http_connection)
* support adding torrents by url to the .torrent file

View File

@ -7140,6 +7140,47 @@ code symbol description
The UPnP errors are declared in the ``libtorrent::upnp_errors`` namespace.
HTTP errors are reported in the ``libtorrent::http_category``, with error code enums in
the ``libtorrent::errors`` namespace.
====== =========================================
code symbol
====== =========================================
100 cont
------ -----------------------------------------
200 ok
------ -----------------------------------------
201 created
------ -----------------------------------------
202 accepted
------ -----------------------------------------
204 no_content
------ -----------------------------------------
300 multiple_choices
------ -----------------------------------------
301 moved_permanently
------ -----------------------------------------
302 moved_temporarily
------ -----------------------------------------
304 not_modified
------ -----------------------------------------
400 bad_request
------ -----------------------------------------
401 unauthorized
------ -----------------------------------------
403 forbidden
------ -----------------------------------------
404 not_found
------ -----------------------------------------
500 internal_server_error
------ -----------------------------------------
501 not_implemented
------ -----------------------------------------
502 bad_gateway
------ -----------------------------------------
503 service_unavailable
====== =========================================
translating error codes
-----------------------

View File

@ -266,6 +266,27 @@ namespace libtorrent
error_code_max
};
enum http_errors
{
cont = 100,
ok = 200,
created = 201,
accepted = 202,
no_content = 204,
multiple_choices = 300,
moved_permanently = 301,
moved_temporarily = 302,
not_modified = 304,
bad_request = 400,
unauthorized = 401,
forbidden = 403,
not_found = 404,
internal_server_error = 500,
not_implemented = 501,
bad_gateway = 502,
service_unavailable = 503
};
}
}
@ -296,6 +317,12 @@ namespace libtorrent
return libtorrent_category;
}
boost::system::error_category const& get_http_category()
{
static ::asio::error::error_category http_category(21);
return http_category;
}
#else
struct TORRENT_EXPORT libtorrent_error_category : boost::system::error_category
@ -312,6 +339,20 @@ namespace libtorrent
return libtorrent_category;
}
struct TORRENT_EXPORT http_error_category : boost::system::error_category
{
virtual const char* name() const;
virtual std::string message(int ev) const;
virtual boost::system::error_condition default_error_condition(int ev) const
{ return boost::system::error_condition(ev, *this); }
};
inline boost::system::error_category& get_http_category()
{
static http_error_category http_category;
return http_category;
}
namespace errors
{
inline boost::system::error_code make_error_code(error_code_enum e)

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include "libtorrent/error_code.hpp"
#include "libtorrent/escape_string.hpp" // for to_string()
namespace libtorrent
{
@ -258,6 +259,39 @@ namespace libtorrent
return msgs[ev];
}
const char* http_error_category::name() const
{
return "http error";
}
std::string http_error_category::message(int ev) const
{
std::string ret;
ret += to_string(ev).elems;
ret += " ";
switch (ev)
{
case errors::cont: ret += "Continue"; break;
case errors::ok: ret += "OK"; break;
case errors::created: ret += "Created"; break;
case errors::accepted: ret += "Accepted"; break;
case errors::no_content: ret += "No Content"; break;
case errors::multiple_choices: ret += "Multiple Choices"; break;
case errors::moved_permanently: ret += "Moved Permanently"; break;
case errors::moved_temporarily: ret += "Moved Temporarily"; break;
case errors::not_modified: ret += "Not Modified"; break;
case errors::bad_request: ret += "Bad Request"; break;
case errors::unauthorized: ret += "Unauthorized"; break;
case errors::forbidden: ret += "Forbidden"; break;
case errors::not_found: ret += "Not Found"; break;
case errors::internal_server_error: ret += "Internal Server Error"; break;
case errors::not_implemented: ret += "Not Implemented"; break;
case errors::bad_gateway: ret += "Bad Gateway"; break;
case errors::service_unavailable: ret += "Service Unavailable"; break;
default: ret += "(unknown HTTP error)"; break;
}
return ret;
}
#endif
}

View File

@ -279,7 +279,7 @@ namespace libtorrent
, error_msg));
}
m_statistics.received_bytes(0, bytes_transferred);
disconnect(errors::http_error, 1);
disconnect(error_code(m_parser.status_code(), get_http_category()), 1);
return;
}
if (!m_parser.header_finished())
@ -414,7 +414,7 @@ namespace libtorrent
m_statistics.received_bytes(0, bytes_transferred);
// temporarily unavailable, retry later
t->retry_web_seed(this, retry_time);
disconnect(errors::http_error, 1);
disconnect(error_code(m_parser.status_code(), get_http_category()), 1);
return;
}

View File

@ -305,7 +305,8 @@ namespace libtorrent
if (parser.status_code() != 200)
{
fail(error_code(errors::http_error), parser.status_code(), parser.message().c_str());
fail(error_code(parser.status_code(), get_http_category())
, parser.status_code(), parser.message().c_str());
return;
}

View File

@ -468,8 +468,7 @@ namespace libtorrent
if (parser.header_finished() && parser.status_code() != 200)
{
// #error there should really be an error code category for HTTP
set_error(errors::http_error, parser.message());
set_error(error_code(parser.status_code(), get_http_category()), parser.message());
pause();
return;
}

View File

@ -381,7 +381,7 @@ namespace libtorrent
, error_msg));
}
m_statistics.received_bytes(0, bytes_transferred);
disconnect(errors::http_error, 1);
disconnect(error_code(m_parser.status_code(), get_http_category()), 1);
#ifdef TORRENT_DEBUG
TORRENT_ASSERT(m_statistics.last_payload_downloaded()
+ m_statistics.last_protocol_downloaded()