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