1 /* 2 3 Copyright (c) 2003-2018, Arvid Norberg 4 All rights reserved. 5 6 Redistribution and use in source and binary forms, with or without 7 modification, are permitted provided that the following conditions 8 are met: 9 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. 18 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. 30 31 */ 32 33 #ifndef TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED 34 #define TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED 35 36 #include <ctime> 37 #include <algorithm> 38 #include <vector> 39 #include <deque> 40 #include <string> 41 #include <cstdint> 42 43 #include "libtorrent/config.hpp" 44 #include "libtorrent/web_connection_base.hpp" 45 #include "libtorrent/piece_block_progress.hpp" 46 #include "libtorrent/operations.hpp" // for operation_t enum 47 #include "libtorrent/aux_/vector.hpp" 48 49 namespace libtorrent { 50 51 class TORRENT_EXTRA_EXPORT web_peer_connection 52 : public web_connection_base 53 { 54 friend class invariant_access; 55 public: 56 57 // this is the constructor where the we are the active part. 58 // The peer_connection should handshake and verify that the 59 // other end has the correct id 60 web_peer_connection(peer_connection_args const& pack 61 , web_seed_t& web); 62 63 void on_connected() override; 64 type() const65 connection_type type() const override 66 { return connection_type::url_seed; } 67 68 // called from the main loop when this connection has any 69 // work to do. 70 void on_receive(error_code const& error 71 , std::size_t bytes_transferred) override; 72 url() const73 std::string const& url() const override { return m_url; } 74 75 void get_specific_peer_info(peer_info& p) const override; 76 void disconnect(error_code const& ec 77 , operation_t op, disconnect_severity_t error = peer_connection_interface::normal) override; 78 79 void write_request(peer_request const& r) override; 80 81 bool received_invalid_data(piece_index_t index, bool single_peer) override; 82 83 private: 84 85 void on_receive_padfile(); 86 void incoming_payload(char const* buf, int len); 87 void incoming_zeroes(int len); 88 void handle_redirect(int bytes_left); 89 void handle_error(int bytes_left); 90 void maybe_harvest_piece(); 91 92 // returns the block currently being 93 // downloaded. And the progress of that 94 // block. If the peer isn't downloading 95 // a piece for the moment, the boost::optional 96 // will be invalid. 97 piece_block_progress downloading_piece_progress() const override; 98 99 void handle_padfile(); 100 101 // this has one entry per http-request 102 // (might be more than the bt requests) 103 struct file_request_t 104 { 105 file_index_t file_index; 106 int length; 107 std::int64_t start; 108 }; 109 std::deque<file_request_t> m_file_requests; 110 111 std::string m_url; 112 113 web_seed_t* m_web; 114 115 // this is used for intermediate storage of pieces to be delivered to the 116 // bittorrent engine 117 // TODO: 3 if we make this be a disk_buffer_holder instead 118 // we would save a copy 119 // use allocate_disk_receive_buffer and release_disk_receive_buffer 120 aux::vector<char> m_piece; 121 122 // the number of bytes we've forwarded to the incoming_payload() function 123 // in the current HTTP response. used to know where in the buffer the 124 // next response starts 125 int m_received_body; 126 127 // this is the offset inside the current receive 128 // buffer where the next chunk header will be. 129 // this is updated for each chunk header that's 130 // parsed. It does not necessarily point to a valid 131 // offset in the receive buffer, if we haven't received 132 // it yet. This offset never includes the HTTP header 133 int m_chunk_pos; 134 135 // this is the number of bytes we've already received 136 // from the next chunk header we're waiting for 137 int m_partial_chunk_header; 138 139 // the number of responses we've received so far on 140 // this connection 141 int m_num_responses; 142 }; 143 } 144 145 #endif // TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED 146