]> git.sesse.net Git - cubemap/blob - httpinput.h
bf7d9534d6eeced4d06319f982981f450c197745
[cubemap] / httpinput.h
1 #ifndef _HTTPINPUT_H
2 #define _HTTPINPUT_H 1
3
4 #include <stddef.h>
5 #include <string>
6 #include <vector>
7
8 #include "input.h"
9
10 class InputProto;
11
12 class HTTPInput : public Input {
13 public:
14         HTTPInput(const std::string &url);
15
16         // Serialization/deserialization.
17         HTTPInput(const InputProto &serialized);
18         virtual InputProto serialize() const;
19         
20         virtual void close_socket();
21
22         virtual std::string get_url() const { return url; }
23
24         virtual void add_destination(int stream_index)
25         {
26                 stream_indices.push_back(stream_index);
27         }
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         enum State {
50                 NOT_CONNECTED,
51                 SENDING_REQUEST,
52                 RECEIVING_HEADER,
53                 RECEIVING_DATA,
54                 CLOSING_SOCKET,  // Due to error.
55         };
56         State state;
57
58         std::vector<int> stream_indices;
59
60         // The URL and its parsed components.
61         std::string url;
62         std::string host, port, path;
63
64         // The HTTP request, with headers and all.
65         // Only relevant for SENDING_REQUEST.
66         std::string request;
67
68         // How many bytes we've sent of the request so far.
69         // Only relevant for SENDING_REQUEST.
70         size_t request_bytes_sent;
71
72         // The HTTP response we've received so far. Only relevant for RECEIVING_HEADER.
73         std::string response;
74
75         // The HTTP respones headers we want to give clients for this input.
76         std::string http_header;
77
78         // Data we have received but not fully processed yet.
79         std::vector<char> pending_data;
80
81         // If <pending_data> starts with a Metacube header,
82         // this is true.
83         bool has_metacube_header;
84
85         // The socket we are downloading on (or -1).
86         int sock;
87
88         // Mutex protecting <stats>.
89         mutable pthread_mutex_t stats_mutex;
90
91         // The current statistics for this connection. Protected by <stats_mutex>.
92         InputStats stats;
93 };
94
95 #endif  // !defined(_HTTPINPUT_H)