added some precautionary checks in bdecoder
[libtorrent.git] / include / libtorrent / web_peer_connection.hpp
blobf8e7af99f2ed5daeffd572e2958aa89a60916cf6
1 /*
3 Copyright (c) 2003, Arvid Norberg
4 All rights reserved.
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the distribution.
15 * Neither the name of the author nor the names of its
16 contributors may be used to endorse or promote products derived
17 from this software without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 POSSIBILITY OF SUCH DAMAGE.
33 #ifndef TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED
34 #define TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED
36 #include <ctime>
37 #include <algorithm>
38 #include <vector>
39 #include <deque>
40 #include <string>
42 #include "libtorrent/debug.hpp"
44 #ifdef _MSC_VER
45 #pragma warning(push, 1)
46 #endif
48 #include <boost/smart_ptr.hpp>
49 #include <boost/weak_ptr.hpp>
50 #include <boost/noncopyable.hpp>
51 #include <boost/array.hpp>
52 #include <boost/optional.hpp>
53 #include <boost/cstdint.hpp>
55 #ifdef _MSC_VER
56 #pragma warning(pop)
57 #endif
59 #include "libtorrent/buffer.hpp"
60 #include "libtorrent/peer_connection.hpp"
61 #include "libtorrent/socket.hpp"
62 #include "libtorrent/peer_id.hpp"
63 #include "libtorrent/storage.hpp"
64 #include "libtorrent/stat.hpp"
65 #include "libtorrent/alert.hpp"
66 #include "libtorrent/torrent_handle.hpp"
67 #include "libtorrent/torrent.hpp"
68 #include "libtorrent/peer_request.hpp"
69 #include "libtorrent/piece_block_progress.hpp"
70 #include "libtorrent/config.hpp"
71 // parse_url
72 #include "libtorrent/tracker_manager.hpp"
73 #include "libtorrent/http_parser.hpp"
75 namespace libtorrent
77 class torrent;
79 namespace detail
81 struct session_impl;
84 class TORRENT_EXPORT web_peer_connection
85 : public peer_connection
87 friend class invariant_access;
88 public:
90 // this is the constructor where the we are the active part.
91 // The peer_conenction should handshake and verify that the
92 // other end has the correct id
93 web_peer_connection(
94 aux::session_impl& ses
95 , boost::weak_ptr<torrent> t
96 , boost::shared_ptr<socket_type> s
97 , tcp::endpoint const& remote
98 , std::string const& url
99 , policy::peer* peerinfo);
101 ~web_peer_connection();
103 // called from the main loop when this connection has any
104 // work to do.
105 void on_sent(error_code const& error
106 , std::size_t bytes_transferred);
107 void on_receive(error_code const& error
108 , std::size_t bytes_transferred);
110 std::string const& url() const { return m_url; }
112 virtual void get_specific_peer_info(peer_info& p) const;
113 virtual bool in_handshake() const;
115 // the following functions appends messages
116 // to the send buffer
117 void write_choke() {}
118 void write_unchoke() {}
119 void write_interested() {}
120 void write_not_interested() {}
121 void write_request(peer_request const& r);
122 void write_cancel(peer_request const& r)
123 { incoming_reject_request(r); }
124 void write_have(int index) {}
125 void write_piece(peer_request const& r, disk_buffer_holder& buffer) { TORRENT_ASSERT(false); }
126 void write_keepalive() {}
127 void on_connected();
128 void write_reject_request(peer_request const&) {}
129 void write_allow_fast(int) {}
131 #ifndef NDEBUG
132 void check_invariant() const;
133 #endif
135 private:
137 // returns the block currently being
138 // downloaded. And the progress of that
139 // block. If the peer isn't downloading
140 // a piece for the moment, the boost::optional
141 // will be invalid.
142 boost::optional<piece_block_progress> downloading_piece_progress() const;
144 // this has one entry per bittorrent request
145 std::deque<peer_request> m_requests;
146 // this has one entry per http-request
147 // (might be more than the bt requests)
148 std::deque<int> m_file_requests;
150 std::string m_server_string;
151 http_parser m_parser;
152 std::string m_auth;
153 std::string m_host;
154 int m_port;
155 std::string m_path;
156 std::string m_url;
158 // the first request will contain a little bit more data
159 // than subsequent ones, things that aren't critical are left
160 // out to save bandwidth.
161 bool m_first_request;
163 // this is used for intermediate storage of pieces
164 // that is received in more than on HTTP responses
165 std::vector<char> m_piece;
166 // the mapping of the data in the m_piece buffer
167 peer_request m_intermediate_piece;
169 // the number of bytes into the receive buffer where
170 // current read cursor is.
171 int m_body_start;
172 // the number of bytes received in the current HTTP
173 // response. used to know where in the buffer the
174 // next response starts
175 int m_received_body;
177 // position in the current range response
178 int m_range_pos;
182 #endif // TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED