Add <functional> to files that use std::function
[lsnes.git] / include / library / httpreq.hpp
blobf9406b17c4db89922fdedb7ec6c6ad98cf49c0a1
1 #ifndef _library__httpreq__hpp__included__
2 #define _library__httpreq__hpp__included__
4 #include <string>
5 #include <cstdint>
6 #include <cstdlib>
7 #include <map>
8 #include <string>
9 #include <functional>
10 #include "threads.hpp"
12 class http_request
14 public:
15 /**
16 * Input handler.
18 class input_handler
20 public:
21 input_handler() { canceled = false; }
22 virtual ~input_handler();
23 static size_t read_fn(char* ptr, size_t size, size_t nmemb, void* userdata);
24 /**
25 * Get length of input.
27 virtual uint64_t get_length() = 0;
28 /**
29 * Read block of input.
31 virtual size_t read(char* target, size_t maxread) = 0;
32 /**
33 * Cancel.
35 void cancel() { canceled = true; }
36 /**
37 * Cancel flag.
39 volatile bool canceled;
41 /**
42 * NULL input handler.
44 class null_input_handler : public input_handler
46 public:
47 ~null_input_handler();
48 uint64_t get_length();
49 size_t read(char* target, size_t maxread);
51 /**
52 * Output handler.
54 class output_handler
56 public:
57 output_handler() { canceled = false; }
58 virtual ~output_handler();
59 static size_t write_fn(char* ptr, size_t size, size_t nmemb, void* userdata);
60 static size_t header_fn(void* ptr, size_t size, size_t nmemb, void* userdata);
61 /**
62 * Sink header.
64 virtual void header(const std::string& name, const std::string& cotent) = 0;
65 /**
66 * Sink data.
68 virtual void write(const char* source, size_t srcsize) = 0;
69 /**
70 * Cancel.
72 void cancel() { canceled = true; }
73 /**
74 * Cancel flag.
76 volatile bool canceled;
78 /**
79 * Obtain WWW-Authenticate responses.
81 class www_authenticate_extractor : public output_handler
83 public:
84 www_authenticate_extractor(std::function<void(const std::string& value)> _callback);
85 ~www_authenticate_extractor();
86 void header(const std::string& name, const std::string& cotent);
87 void write(const char* source, size_t srcsize);
88 private:
89 std::function<void(const std::string& value)> callback;
91 /**
92 * Create a new request instance.
94 http_request(const std::string& verb, const std::string& url);
95 /**
96 * Destructor.
98 ~http_request();
99 /**
100 * Set the authorization header to use.
102 void set_authorization(const std::string& hdr)
104 authorization = hdr;
107 * Do the transfer.
109 * Input handler is only used if verb is PUT or POST. It may be NULL for GET/HEAD.
111 void do_transfer(input_handler* inhandler, output_handler* outhandler);
113 * Get status of transfer. Safe to call from another thread while do_transfer is in progress.
115 void get_xfer_status(int64_t& dnow, int64_t& dtotal, int64_t& unow, int64_t& utotal);
117 * Do global initialization.
119 static void global_init();
121 * Get final code.
123 uint32_t get_http_code();
124 private:
125 static int progress(void* userdata, double dltotal, double dlnow, double ultotal, double ulnow);
126 int _progress(double dltotal, double dlnow, double ultotal, double ulnow);
127 http_request(const http_request&);
128 http_request& operator=(const http_request&);
129 void* handle;
130 std::string authorization;
131 bool has_body;
132 double dltotal, dlnow, ultotal, ulnow;
136 * HTTP asynchronous request structure.
138 struct http_async_request
140 http_async_request();
141 http_request::input_handler* ihandler; //Input handler (INPUT).
142 http_request::output_handler* ohandler; //Output handler (INPUT).
143 std::string verb; //HTTP verb (INPUT)
144 std::string url; //URL to access (INPUT)
145 std::string authorization; //Authorization to use (INPUT)
146 int64_t final_dl; //Final amount downloaded (OUTPUT).
147 int64_t final_ul; //Final amound uploaded (OUTPUT).
148 std::string errormsg; //Final error (OUTPUT).
149 uint32_t http_code; //HTTP error code (OUTPUT).
150 volatile bool finished; //Finished flag (semi-transient).
151 threads::cv finished_cond; //This condition variable is fired on finish.
152 http_request* req; //The HTTP request object (TRANSIENT).
153 threads::lock m; //Lock protecting the object (TRANSIENT).
154 void get_xfer_status(int64_t& dnow, int64_t& dtotal, int64_t& unow, int64_t& utotal);
155 void lauch_async(); //Lauch asynchronous request.
156 void cancel(); //Cancel request in flight.
160 * Property upload.
162 struct property_upload_request : public http_request::input_handler
164 //Inherited methods.
165 property_upload_request();
166 ~property_upload_request();
167 uint64_t get_length();
168 size_t read(char* target, size_t maxread);
169 void rewind();
171 * Data to upload.
173 std::map<std::string, std::string> data;
174 private:
175 unsigned state;
176 std::map<std::string, std::string>::iterator itr;
177 size_t sent;
178 void str_helper(const std::string& str, char*& target, size_t& maxread, size_t& x, unsigned next);
179 void chr_helper(char ch, char*& target, size_t& maxread, size_t& x, unsigned next);
180 void len_helper(size_t len, char*& target, size_t& maxread, size_t& x, unsigned next);
183 //Lowercase a string.
184 std::string http_strlower(const std::string& name);
186 #endif