add a debug mode - fill a Space structure with some info from CURLOPT_DEBUGFUNCTION

while here:
- allow to set headers from a string
- add a seek function (CURLOPT_SEEKFUNCTION)
This commit is contained in:
2022-07-30 04:00:35 +02:00
parent 9d8d35b64f
commit 82281b4363
2 changed files with 487 additions and 63 deletions

View File

@@ -59,12 +59,49 @@ public:
Http(const Http &) = delete;
Http(Http &&) = delete;
/*
* start a new request
* called automatically from cctor
* you have to call it manually to make another requests
*
*/
Http & begin();
/*
* add one header (should not end with \r\n or \n )
*
*/
Http & add_header(const pt::TextStream & header);
Http & add_header(const pt::WTextStream & header);
Http & add_header(const std::string & header);
Http & add_header(const std::wstring & header);
Http & add_header(const char * str, size_t len);
Http & add_header(const wchar_t * str, size_t len);
/*
* additional headers (we do not copy it too)
* headers can be separated with \r\n or just with \n
* last \n (or \r\n) can be omitted
* empty lines will be ignored
*
* headers from wchar_t* will be replaced to char* by using pt::wide_to_utf8
*/
Http & add_headers(const char * headers);
Http & add_headers(const wchar_t * headers);
Http & add_headers(const std::string & headers);
Http & add_headers(const std::wstring & headers);
/*
* we do not copy the space structure but only get a pointer to it
* so you have to preserve the structure until get()/put() is called
*/
Http & add_input_headers(pt::Space * headers);
Http & add_headers(pt::Space * headers);
Http & add_headers(pt::Space & headers);
/*
* output headers will be provided in a Space structure as key/value pairs (object)
@@ -90,6 +127,15 @@ public:
void initialize_curl_if_needed();
void uninitialize_curl();
/*
* if true set additional information how the curl library works when connection is made
* debug_info will be a table in such a case
*
* if debug is false then debug_info can be nullptr (if not null then will be set to Space null)
*
*/
void use_debug_mode(bool debug, pt::Space * debug_info);
/*
* we do not copy the string but only get a pointer to its c_str()
* so you have to preserve the string until get()/put() is called
@@ -124,12 +170,16 @@ private:
size_t read_function_index;
const std::string * read_function_input;
curl_slist * http_headers;
pt::Space * additional_headers_to_send;
pt::Space * additional_space_headers_to_send;
const char * additional_string_headers_to_send;
const wchar_t * additional_wstring_headers_to_send;
pt::Space * output_headers_space;
const wchar_t * bearer_token = nullptr;
pt::TextStream out_headers_stream;
bool change_header_names_to_lower;
std::wstring * output_content_type;
long debug_mode;
pt::Space * debug_info;
std::wstring temp_header;
std::string temp_header_ascii;
@@ -139,18 +189,59 @@ private:
bool fetch_internal(const char * url, const std::string * in, pt::TextStream & out);
static size_t fetch_read_function(char * ptr, size_t size, size_t nmemb, void * userdata);
static int fetch_seek_set(Http * http, curl_off_t offset);
static int fetch_seek_cur(Http * http, curl_off_t offset);
static int fetch_seek_end(Http * http, curl_off_t offset);
static int fetch_seek_function(void * userdata, curl_off_t offset, int origin);
static size_t fetch_write_function(char * ptr, size_t size, size_t nmemb, void * userdata);
static size_t fetch_header_function(char * ptr, size_t size, size_t nmemb, void * userdata);
static void debug_function_append(pt::Space & debug, const char * prefix, const char * start, size_t part_size);
static void debug_function_append(Http * http, const char * prefix, char * data, size_t size);
static int debug_function(CURL * handle, curl_infotype type, char * data, size_t size, void * userdata);
void clear_tmp_objects();
void reset_headers();
void add_additional_headers();
void add_additional_space_headers();
void add_additional_string_headers();
void add_bearer_token();
void add_header(const pt::WTextStream & header);
void add_header(const std::wstring & header);
void skip_white(pt::TextStream::iterator & i);
void parse_header_name(pt::TextStream::iterator & i, std::string & name);
void parse_header_value(pt::TextStream::iterator & i, std::string & value);
void parse_headers();
template<typename StringType>
void add_additional_string_headers(StringType * str)
{
StringType * part_start = str;
size_t part_size = 0;
size_t i = 0;
while( str[i] != 0 )
{
if( str[i]== '\r' && str[i+1]=='\n' )
{
add_header(part_start, part_size);
i += 2;
part_start = str + i;
part_size = 0;
}
else
if( str[i]== '\n' )
{
add_header(part_start, part_size);
i += 1;
part_start = str + i;
part_size = 0;
}
else
{
part_size += 1;
i += 1;
}
}
add_header(part_start, part_size);
}
};