]> git.sesse.net Git - cubemap/blob - httpinput.h
6b0c87512dffee116dea7c86c89404a5e0090e56
[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
11 class InputProto;
12
13 class HTTPInput : public Input {
14 public:
15         HTTPInput(const std::string &url);
16
17         // Serialization/deserialization.
18         HTTPInput(const InputProto &serialized);
19         virtual InputProto serialize() const;
20         
21         virtual void close_socket();
22
23         virtual std::string get_url() const { return url; }
24
25         virtual void add_destination(int stream_index);
26
27         virtual InputStats get_stats() const;
28
29 private:
30         // Actually does the download.
31         virtual void do_work();
32         
33         // Open a socket that connects to the given host and port. Does DNS resolving.
34         int lookup_and_connect(const std::string &host, const std::string &port);
35
36         // Parses a HTTP response. Returns false if it not a 200.
37         bool parse_response(const std::string &response);
38
39         // Stores the given data, looks for Metacube blocks (skipping data if needed),
40         // and calls process_block() for each one.
41         void process_data(char *ptr, size_t bytes);
42
43         // Drops <num_bytes> bytes from the head of <pending_data>,
44         // and outputs a warning.
45         void drop_pending_data(size_t num_bytes);
46
47         enum State {
48                 NOT_CONNECTED,
49                 SENDING_REQUEST,
50                 RECEIVING_HEADER,
51                 RECEIVING_DATA,
52                 CLOSING_SOCKET,  // Due to error.
53         };
54         State state;
55
56         std::vector<int> stream_indices;
57
58         // The URL and its parsed components.
59         std::string url;
60         std::string host, port, path;
61
62         // The HTTP request, with headers and all.
63         // Only relevant for SENDING_REQUEST.
64         std::string request;
65
66         // How many bytes we've sent of the request so far.
67         // Only relevant for SENDING_REQUEST.
68         size_t request_bytes_sent;
69
70         // The HTTP response we've received so far. Only relevant for RECEIVING_HEADER.
71         std::string response;
72
73         // The HTTP response headers we want to give clients for this input.
74         std::string http_header;
75
76         // The stream heder we want to give clients for this input.
77         std::string stream_header;
78
79         // Data we have received but not fully processed yet.
80         std::vector<char> pending_data;
81
82         // If <pending_data> starts with a Metacube header,
83         // this is true.
84         bool has_metacube_header;
85
86         // The socket we are downloading on (or -1).
87         int sock;
88
89         // Mutex protecting <stats>.
90         mutable pthread_mutex_t stats_mutex;
91
92         // The current statistics for this connection. Protected by <stats_mutex>.
93         InputStats stats;
94 };
95
96 #endif  // !defined(_HTTPINPUT_H)