Lua: Fix type confusion between signed and unsigned
[lsnes.git] / include / library / httpreq.hpp
blob0786b63c0c2203312db9d8569dd1502bd666177d
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 "threads.hpp"
11 class http_request
13 public:
14 /**
15 * Input handler.
17 class input_handler
19 public:
20 input_handler() { canceled = false; }
21 virtual ~input_handler();
22 static size_t read_fn(char* ptr, size_t size, size_t nmemb, void* userdata);
23 /**
24 * Get length of input.
26 virtual uint64_t get_length() = 0;
27 /**
28 * Read block of input.
30 virtual size_t read(char* target, size_t maxread) = 0;
31 /**
32 * Cancel.
34 void cancel() { canceled = true; }
35 /**
36 * Cancel flag.
38 volatile bool canceled;
40 /**
41 * NULL input handler.
43 class null_input_handler : public input_handler
45 public:
46 ~null_input_handler();
47 uint64_t get_length();
48 size_t read(char* target, size_t maxread);
50 /**
51 * Output handler.
53 class output_handler
55 public:
56 output_handler() { canceled = false; }
57 virtual ~output_handler();
58 static size_t write_fn(char* ptr, size_t size, size_t nmemb, void* userdata);
59 static size_t header_fn(void* ptr, size_t size, size_t nmemb, void* userdata);
60 /**
61 * Sink header.
63 virtual void header(const std::string& name, const std::string& cotent) = 0;
64 /**
65 * Sink data.
67 virtual void write(const char* source, size_t srcsize) = 0;
68 /**
69 * Cancel.
71 void cancel() { canceled = true; }
72 /**
73 * Cancel flag.
75 volatile bool canceled;
77 /**
78 * Obtain WWW-Authenticate responses.
80 class www_authenticate_extractor : public output_handler
82 public:
83 www_authenticate_extractor(std::function<void(const std::string& value)> _callback);
84 ~www_authenticate_extractor();
85 void header(const std::string& name, const std::string& cotent);
86 void write(const char* source, size_t srcsize);
87 private:
88 std::function<void(const std::string& value)> callback;
90 /**
91 * Create a new request instance.
93 http_request(const std::string& verb, const std::string& url);
94 /**
95 * Destructor.
97 ~http_request();
98 /**
99 * Set the authorization header to use.
101 void set_authorization(const std::string& hdr)
103 authorization = hdr;
106 * Do the transfer.
108 * Input handler is only used if verb is PUT or POST. It may be NULL for GET/HEAD.
110 void do_transfer(input_handler* inhandler, output_handler* outhandler);
112 * Get status of transfer. Safe to call from another thread while do_transfer is in progress.
114 void get_xfer_status(int64_t& dnow, int64_t& dtotal, int64_t& unow, int64_t& utotal);
116 * Do global initialization.
118 static void global_init();
120 * Get final code.
122 uint32_t get_http_code();
123 private:
124 static int progress(void* userdata, double dltotal, double dlnow, double ultotal, double ulnow);
125 int _progress(double dltotal, double dlnow, double ultotal, double ulnow);
126 http_request(const http_request&);
127 http_request& operator=(const http_request&);
128 void* handle;
129 std::string authorization;
130 bool has_body;
131 double dltotal, dlnow, ultotal, ulnow;
135 * HTTP asynchronous request structure.
137 struct http_async_request
139 http_async_request();
140 http_request::input_handler* ihandler; //Input handler (INPUT).
141 http_request::output_handler* ohandler; //Output handler (INPUT).
142 std::string verb; //HTTP verb (INPUT)
143 std::string url; //URL to access (INPUT)
144 std::string authorization; //Authorization to use (INPUT)
145 int64_t final_dl; //Final amount downloaded (OUTPUT).
146 int64_t final_ul; //Final amound uploaded (OUTPUT).
147 std::string errormsg; //Final error (OUTPUT).
148 uint32_t http_code; //HTTP error code (OUTPUT).
149 volatile bool finished; //Finished flag (semi-transient).
150 threads::cv finished_cond; //This condition variable is fired on finish.
151 http_request* req; //The HTTP request object (TRANSIENT).
152 threads::lock m; //Lock protecting the object (TRANSIENT).
153 void get_xfer_status(int64_t& dnow, int64_t& dtotal, int64_t& unow, int64_t& utotal);
154 void lauch_async(); //Lauch asynchronous request.
155 void cancel(); //Cancel request in flight.
159 * Property upload.
161 struct property_upload_request : public http_request::input_handler
163 //Inherited methods.
164 property_upload_request();
165 ~property_upload_request();
166 uint64_t get_length();
167 size_t read(char* target, size_t maxread);
168 void rewind();
170 * Data to upload.
172 std::map<std::string, std::string> data;
173 private:
174 unsigned state;
175 std::map<std::string, std::string>::iterator itr;
176 size_t sent;
177 void str_helper(const std::string& str, char*& target, size_t& maxread, size_t& x, unsigned next);
178 void chr_helper(char ch, char*& target, size_t& maxread, size_t& x, unsigned next);
179 void len_helper(size_t len, char*& target, size_t& maxread, size_t& x, unsigned next);
182 //Lowercase a string.
183 std::string http_strlower(const std::string& name);
185 #endif