1 /*
2 
3 Copyright (c) 2008-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_HTTP_SEED_CONNECTION_HPP_INCLUDED
34 #define TORRENT_HTTP_SEED_CONNECTION_HPP_INCLUDED
35 
36 #include <ctime>
37 #include <algorithm>
38 #include <string>
39 #include <cstdint>
40 
41 #include "libtorrent/config.hpp"
42 #include "libtorrent/web_connection_base.hpp"
43 #include "libtorrent/piece_block_progress.hpp"
44 
45 namespace libtorrent {
46 
47 	class torrent;
48 	struct peer_request;
49 
50 	class TORRENT_EXTRA_EXPORT http_seed_connection
51 		: public web_connection_base
52 	{
53 	friend class invariant_access;
54 	public:
55 
56 		// this is the constructor where the we are the active part.
57 		// The peer_connection should handshake and verify that the
58 		// other end has the correct id
59 		http_seed_connection(peer_connection_args const& pack
60 			, web_seed_t& web);
61 
type() const62 		connection_type type() const override
63 		{ return connection_type::http_seed; }
64 
65 		// called from the main loop when this connection has any
66 		// work to do.
67 		void on_receive(error_code const& error
68 			, std::size_t bytes_transferred) override;
69 
70 		void on_connected() override;
71 
url() const72 		std::string const& url() const override { return m_url; }
73 
74 		void get_specific_peer_info(peer_info& p) const override;
75 		void disconnect(error_code const& ec, operation_t op
76 			, disconnect_severity_t error = peer_connection_interface::normal) override;
77 
78 		void write_request(peer_request const& r) override;
79 
80 	private:
81 
82 		// returns the block currently being
83 		// downloaded. And the progress of that
84 		// block. If the peer isn't downloading
85 		// a piece for the moment, the boost::optional
86 		// will be invalid.
87 		piece_block_progress downloading_piece_progress() const override;
88 
89 		// this is const since it's used as a key in the web seed list in the torrent
90 		// if it's changed referencing back into that list will fail
91 		const std::string m_url;
92 
93 		web_seed_t* m_web;
94 
95 		// the number of bytes left to receive of the response we're
96 		// currently parsing
97 		std::int64_t m_response_left;
98 
99 		// this is the offset inside the current receive
100 		// buffer where the next chunk header will be.
101 		// this is updated for each chunk header that's
102 		// parsed. It does not necessarily point to a valid
103 		// offset in the receive buffer, if we haven't received
104 		// it yet. This offset never includes the HTTP header
105 		std::int64_t m_chunk_pos;
106 
107 		// this is the number of bytes we've already received
108 		// from the next chunk header we're waiting for
109 		int m_partial_chunk_header;
110 	};
111 }
112 
113 #endif // TORRENT_WEB_PEER_CONNECTION_HPP_INCLUDED
114