1 #ifndef _library__httpreq__hpp__included__
2 #define _library__httpreq__hpp__included__
10 #include "threads.hpp"
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
);
25 * Get length of input.
27 virtual uint64_t get_length() = 0;
29 * Read block of input.
31 virtual size_t read(char* target
, size_t maxread
) = 0;
35 void cancel() { canceled
= true; }
39 volatile bool canceled
;
44 class null_input_handler
: public input_handler
47 ~null_input_handler();
48 uint64_t get_length();
49 size_t read(char* target
, size_t maxread
);
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
);
64 virtual void header(const std::string
& name
, const std::string
& cotent
) = 0;
68 virtual void write(const char* source
, size_t srcsize
) = 0;
72 void cancel() { canceled
= true; }
76 volatile bool canceled
;
79 * Obtain WWW-Authenticate responses.
81 class www_authenticate_extractor
: public output_handler
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
);
89 std::function
<void(const std::string
& value
)> callback
;
92 * Create a new request instance.
94 http_request(const std::string
& verb
, const std::string
& url
);
100 * Set the authorization header to use.
102 void set_authorization(const std::string
& hdr
)
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();
123 uint32_t get_http_code();
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
&);
130 std::string authorization
;
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.
162 struct property_upload_request
: public http_request::input_handler
165 property_upload_request();
166 ~property_upload_request();
167 uint64_t get_length();
168 size_t read(char* target
, size_t maxread
);
173 std::map
<std::string
, std::string
> data
;
176 std::map
<std::string
, std::string
>::iterator itr
;
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
);