lib/curl/Easy: add setter functions
This commit is contained in:
parent
4c46ca6b59
commit
402f429b17
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2016-2018 Max Kellermann <max.kellermann@gmail.com>
|
||||
* Copyright 2016-2018 Max Kellermann <max.kellermann@gmail.com>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -88,6 +88,78 @@ public:
|
||||
throw std::runtime_error(curl_easy_strerror(code));
|
||||
}
|
||||
|
||||
void SetPrivate(void *pointer) {
|
||||
SetOption(CURLOPT_PRIVATE, pointer);
|
||||
}
|
||||
|
||||
void SetErrorBuffer(char *buf) {
|
||||
SetOption(CURLOPT_ERRORBUFFER, buf);
|
||||
}
|
||||
|
||||
void SetURL(const char *value) {
|
||||
SetOption(CURLOPT_URL, value);
|
||||
}
|
||||
|
||||
void SetUserAgent(const char *value) {
|
||||
SetOption(CURLOPT_USERAGENT, value);
|
||||
}
|
||||
|
||||
void SetRequestHeaders(struct curl_slist *headers) {
|
||||
SetOption(CURLOPT_HTTPHEADER, headers);
|
||||
}
|
||||
|
||||
void SetBasicAuth(const char *userpwd) {
|
||||
SetOption(CURLOPT_USERPWD, userpwd);
|
||||
}
|
||||
|
||||
void SetNoProgress(bool value=true) {
|
||||
SetOption(CURLOPT_NOPROGRESS, (long)value);
|
||||
}
|
||||
|
||||
void SetNoSignal(bool value=true) {
|
||||
SetOption(CURLOPT_NOSIGNAL, (long)value);
|
||||
}
|
||||
|
||||
void SetFailOnError(bool value=true) {
|
||||
SetOption(CURLOPT_FAILONERROR, (long)value);
|
||||
}
|
||||
|
||||
void SetConnectTimeout(long timeout) {
|
||||
SetOption(CURLOPT_CONNECTTIMEOUT, timeout);
|
||||
}
|
||||
|
||||
void SetHeaderFunction(size_t (*function)(char *buffer, size_t size,
|
||||
size_t nitems,
|
||||
void *userdata),
|
||||
void *userdata) {
|
||||
SetOption(CURLOPT_HEADERFUNCTION, function);
|
||||
SetOption(CURLOPT_HEADERDATA, userdata);
|
||||
}
|
||||
|
||||
void SetWriteFunction(size_t (*function)(char *ptr, size_t size,
|
||||
size_t nmemb, void *userdata),
|
||||
void *userdata) {
|
||||
SetOption(CURLOPT_WRITEFUNCTION, function);
|
||||
SetOption(CURLOPT_WRITEDATA, userdata);
|
||||
}
|
||||
|
||||
void SetNoBody(bool value=true) {
|
||||
SetOption(CURLOPT_NOBODY, (long)value);
|
||||
}
|
||||
|
||||
void SetPost(bool value=true) {
|
||||
SetOption(CURLOPT_POST, (long)value);
|
||||
}
|
||||
|
||||
void SetRequestBody(const void *data, size_t size) {
|
||||
SetOption(CURLOPT_POSTFIELDS, data);
|
||||
SetOption(CURLOPT_POSTFIELDSIZE, (long)size);
|
||||
}
|
||||
|
||||
void SetHttpPost(const struct curl_httppost *post) {
|
||||
SetOption(CURLOPT_HTTPPOST, post);
|
||||
}
|
||||
|
||||
char *Escape(const char *string, int length=0) const noexcept {
|
||||
return curl_easy_escape(handle, string, length);
|
||||
}
|
||||
|
@ -53,17 +53,15 @@ CurlRequest::CurlRequest(CurlGlobal &_global,
|
||||
{
|
||||
error_buffer[0] = 0;
|
||||
|
||||
easy.SetOption(CURLOPT_PRIVATE, (void *)this);
|
||||
easy.SetOption(CURLOPT_USERAGENT, "Music Player Daemon " VERSION);
|
||||
easy.SetOption(CURLOPT_HEADERFUNCTION, _HeaderFunction);
|
||||
easy.SetOption(CURLOPT_WRITEHEADER, this);
|
||||
easy.SetOption(CURLOPT_WRITEFUNCTION, WriteFunction);
|
||||
easy.SetOption(CURLOPT_WRITEDATA, this);
|
||||
easy.SetPrivate((void *)this);
|
||||
easy.SetUserAgent("Music Player Daemon " VERSION);
|
||||
easy.SetHeaderFunction(_HeaderFunction, this);
|
||||
easy.SetWriteFunction(WriteFunction, this);
|
||||
easy.SetOption(CURLOPT_NETRC, 1l);
|
||||
easy.SetOption(CURLOPT_ERRORBUFFER, error_buffer);
|
||||
easy.SetOption(CURLOPT_NOPROGRESS, 1l);
|
||||
easy.SetOption(CURLOPT_NOSIGNAL, 1l);
|
||||
easy.SetOption(CURLOPT_CONNECTTIMEOUT, 10l);
|
||||
easy.SetErrorBuffer(error_buffer);
|
||||
easy.SetNoProgress();
|
||||
easy.SetNoSignal();
|
||||
easy.SetConnectTimeout(10);
|
||||
easy.SetOption(CURLOPT_HTTPAUTH, (long) CURLAUTH_ANY);
|
||||
}
|
||||
|
||||
@ -227,14 +225,14 @@ CurlRequest::HeaderFunction(StringView s) noexcept
|
||||
}
|
||||
|
||||
size_t
|
||||
CurlRequest::_HeaderFunction(void *ptr, size_t size, size_t nmemb,
|
||||
CurlRequest::_HeaderFunction(char *ptr, size_t size, size_t nmemb,
|
||||
void *stream) noexcept
|
||||
{
|
||||
CurlRequest &c = *(CurlRequest *)stream;
|
||||
|
||||
size *= nmemb;
|
||||
|
||||
c.HeaderFunction({(const char *)ptr, size});
|
||||
c.HeaderFunction({ptr, size});
|
||||
return size;
|
||||
}
|
||||
|
||||
@ -261,7 +259,7 @@ CurlRequest::DataReceived(const void *ptr, size_t received_size) noexcept
|
||||
}
|
||||
|
||||
size_t
|
||||
CurlRequest::WriteFunction(void *ptr, size_t size, size_t nmemb,
|
||||
CurlRequest::WriteFunction(char *ptr, size_t size, size_t nmemb,
|
||||
void *stream) noexcept
|
||||
{
|
||||
CurlRequest &c = *(CurlRequest *)stream;
|
||||
|
@ -127,7 +127,7 @@ public:
|
||||
}
|
||||
|
||||
void SetUrl(const char *url) {
|
||||
easy.SetOption(CURLOPT_URL, url);
|
||||
easy.SetURL(url);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -160,11 +160,11 @@ private:
|
||||
void OnPostponeError() noexcept;
|
||||
|
||||
/** called by curl when new data is available */
|
||||
static size_t _HeaderFunction(void *ptr, size_t size, size_t nmemb,
|
||||
static size_t _HeaderFunction(char *ptr, size_t size, size_t nmemb,
|
||||
void *stream) noexcept;
|
||||
|
||||
/** called by curl when new data is available */
|
||||
static size_t WriteFunction(void *ptr, size_t size, size_t nmemb,
|
||||
static size_t WriteFunction(char *ptr, size_t size, size_t nmemb,
|
||||
void *stream) noexcept;
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user