]> git.sesse.net Git - cubemap/blob - httpinput.h
Release Cubemap 1.4.3.
[cubemap] / httpinput.h
1 #ifndef _HTTPINPUT_H
2 #define _HTTPINPUT_H 1
3
4 #include <stddef.h>
5 #include <mutex>
6 #include <string>
7 #include <vector>
8
9 #include "input.h"
10 #include "metacube2.h"
11 #include "stream.h"
12
13 class InputProto;
14
15 class HTTPInput : public Input {
16 public:
17         HTTPInput(const std::string &url, Input::Encoding encoding);
18
19         // Serialization/deserialization.
20         HTTPInput(const InputProto &serialized);
21         virtual InputProto serialize() const;
22         
23         virtual void close_socket();
24
25         virtual std::string get_url() const { return url; }
26
27         virtual void add_destination(int stream_index);
28
29         virtual InputStats get_stats() const;
30
31 private:
32         // Actually does the download.
33         virtual void do_work();
34         
35         // Open a socket that connects to the given host and port. Does DNS resolving.
36         int lookup_and_connect(const std::string &host, const std::string &port);
37
38         // Parses a HTTP response. Returns false if it not a 200.
39         bool parse_response(const std::string &response);
40
41         // Stores the given data, looks for Metacube blocks (skipping data if needed),
42         // and calls process_block() for each one.
43         void process_data(char *ptr, size_t bytes);
44
45         // Drops <num_bytes> bytes from the head of <pending_data>,
46         // and outputs a warning.
47         void drop_pending_data(size_t num_bytes);
48
49         void process_metacube_metadata_block(const metacube2_block_header &hdr, const char *payload, uint32_t payload_size);
50
51         enum State {
52                 NOT_CONNECTED,
53                 SENDING_REQUEST,
54                 RECEIVING_HEADER,
55                 RECEIVING_DATA,
56                 CLOSING_SOCKET,  // Due to error.
57         };
58         State state;
59
60         std::vector<int> stream_indices;
61
62         // The URL and its parsed components.
63         std::string url;
64         std::string host, port, path;
65
66         // What the input stream is to be interpreted as (normally Metacube).
67         Input::Encoding encoding;
68
69         // The HTTP request, with headers and all.
70         // Only relevant for SENDING_REQUEST.
71         std::string request;
72
73         // How many bytes we've sent of the request so far.
74         // Only relevant for SENDING_REQUEST.
75         size_t request_bytes_sent;
76
77         // The HTTP response we've received so far. Only relevant for RECEIVING_HEADER.
78         std::string response;
79
80         // The HTTP response headers we want to give clients for this input.
81         std::string http_header;
82
83         // The stream heder we want to give clients for this input.
84         std::string stream_header;
85
86         // Data we have received but not fully processed yet.
87         std::vector<char> pending_data;
88
89         // If <pending_data> starts with a Metacube header,
90         // this is true.
91         bool has_metacube_header = false;
92
93         // The socket we are downloading on (or -1).
94         int sock = -1;
95
96         // Mutex protecting <stats>.
97         mutable std::mutex stats_mutex;
98
99         // The current statistics for this connection. Protected by <stats_mutex>.
100         InputStats stats;
101
102         // Number of (started) connection attempts since last data byte was successfully read.
103         unsigned num_connection_attempts = 0;
104
105         // If set, don't log anything related to connections.
106         // (Only set if we've had enough unsuccessful connection attempts.)
107         bool suppress_logging = false;
108
109         // Last time we made a connection with logging enabled.
110         // (Initially at some point before the epoch.)
111         timespec last_verbose_connection { -3600, 0 };
112
113         // If we've received a Metacube2 PTS metadata block, it belongs to the
114         // next regular block we receive, and is stored here in the meantime.
115         // If we haven't received one yet (or we've already received the
116         // corresponding data block), this is empty, ie., timebase_num == 0.
117         RationalPTS next_block_pts;
118 };
119
120 #endif  // !defined(_HTTPINPUT_H)