Commit ab39f64f authored by Max Kellermann's avatar Max Kellermann

lib/curl/Easy: add setter functions

parent 185fbca2
/*
* 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
......@@ -90,6 +90,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);
}
CurlString Escape(const char *string, int length=0) const noexcept {
return CurlString(curl_easy_escape(handle, string, length));
}
......
......@@ -52,17 +52,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);
}
......@@ -220,14 +218,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;
}
......@@ -254,7 +252,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;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment