]> git.sesse.net Git - cubemap/blob - httpinput.h
Since we just broke upgrade compatibility, kill some older stuff in the state protos.
[cubemap] / httpinput.h
1 #ifndef _HTTPINPUT_H
2 #define _HTTPINPUT_H 1
3
4 #include <vector>
5 #include <string>
6
7 #include "input.h"
8
9 class InputProto;
10
11 class HTTPInput : public Input {
12 public:
13         HTTPInput(const std::string &stream_id, const std::string &url);
14
15         // Serialization/deserialization.
16         HTTPInput(const InputProto &serialized);
17         virtual InputProto serialize() const;
18         
19         virtual void close_socket();
20
21         virtual std::string get_url() const { return url; }
22
23 private:
24         // Actually does the download.
25         virtual void do_work();
26         
27         // Open a socket that connects to the given host and port. Does DNS resolving.
28         int lookup_and_connect(const std::string &host, const std::string &port);
29
30         // Parses a HTTP response. Returns false if it not a 200.
31         bool parse_response(const std::string &response);
32
33         // Stores the given data, looks for Metacube blocks (skipping data if needed),
34         // and calls process_block() for each one.
35         void process_data(char *ptr, size_t bytes);
36
37         // Drops <num_bytes> bytes from the head of <pending_data>,
38         // and outputs a warning.
39         void drop_pending_data(size_t num_bytes);
40
41         enum State {
42                 NOT_CONNECTED,
43                 SENDING_REQUEST,
44                 RECEIVING_HEADER,
45                 RECEIVING_DATA,
46                 CLOSING_SOCKET,  // Due to error.
47         };
48         State state;
49
50         std::string stream_id;
51
52         // The URL and its parsed components.
53         std::string url;
54         std::string host, port, path;
55
56         // The HTTP request, with headers and all.
57         // Only relevant for SENDING_REQUEST.
58         std::string request;
59
60         // How many bytes we've sent of the request so far.
61         // Only relevant for SENDING_REQUEST.
62         size_t request_bytes_sent;
63
64         // The HTTP response we've received so far. Only relevant for RECEIVING_HEADER.
65         std::string response;
66
67         // The HTTP respones headers we want to give clients for this input.
68         std::string http_header;
69
70         // Data we have received but not fully processed yet.
71         std::vector<char> pending_data;
72
73         // If <pending_data> starts with a Metacube header,
74         // this is true.
75         bool has_metacube_header;
76
77         // The socket we are downloading on (or -1).
78         int sock;       
79 };
80
81 #endif  // !defined(_HTTPINPUT_H)