1 #ifndef _library__httpreq__hpp__included__
2 #define _library__httpreq__hpp__included__
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
);
24 * Get length of input.
26 virtual uint64_t get_length() = 0;
28 * Read block of input.
30 virtual size_t read(char* target
, size_t maxread
) = 0;
34 void cancel() { canceled
= true; }
38 volatile bool canceled
;
43 class null_input_handler
: public input_handler
46 ~null_input_handler();
47 uint64_t get_length();
48 size_t read(char* target
, size_t maxread
);
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
);
63 virtual void header(const std::string
& name
, const std::string
& cotent
) = 0;
67 virtual void write(const char* source
, size_t srcsize
) = 0;
71 void cancel() { canceled
= true; }
75 volatile bool canceled
;
78 * Obtain WWW-Authenticate responses.
80 class www_authenticate_extractor
: public output_handler
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
);
88 std::function
<void(const std::string
& value
)> callback
;
91 * Create a new request instance.
93 http_request(const std::string
& verb
, const std::string
& url
);
99 * Set the authorization header to use.
101 void set_authorization(const std::string
& hdr
)
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();
122 long get_http_code();
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
&);
129 std::string authorization
;
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 long 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.
161 struct property_upload_request
: public http_request::input_handler
164 property_upload_request();
165 ~property_upload_request();
166 uint64_t get_length();
167 size_t read(char* target
, size_t maxread
);
172 std::map
<std::string
, std::string
> data
;
175 std::map
<std::string
, std::string
>::iterator itr
;
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
);