diff options
author | Max Kellermann <max@musicpd.org> | 2019-08-19 21:16:51 +0200 |
---|---|---|
committer | Max Kellermann <max@musicpd.org> | 2020-03-26 17:21:30 +0100 |
commit | ab39f64fc0131b4b7d9ebc7ad0d4bcab02d0cdc6 (patch) | |
tree | 340e296e87702522114c7836396bc29e9a793106 | |
parent | 185fbca28210581d9e003ffcf305e431a7c2a20e (diff) |
lib/curl/Easy: add setter functions
-rw-r--r-- | src/lib/curl/Easy.hxx | 74 | ||||
-rw-r--r-- | src/lib/curl/Request.cxx | 24 | ||||
-rw-r--r-- | src/lib/curl/Request.hxx | 6 |
3 files changed, 87 insertions, 17 deletions
diff --git a/src/lib/curl/Easy.hxx b/src/lib/curl/Easy.hxx index 4d813511e..91de2b69e 100644 --- a/src/lib/curl/Easy.hxx +++ b/src/lib/curl/Easy.hxx @@ -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 @@ -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)); } diff --git a/src/lib/curl/Request.cxx b/src/lib/curl/Request.cxx index a72fde549..326af6015 100644 --- a/src/lib/curl/Request.cxx +++ b/src/lib/curl/Request.cxx @@ -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; diff --git a/src/lib/curl/Request.hxx b/src/lib/curl/Request.hxx index 59af67b7c..baca4fb8e 100644 --- a/src/lib/curl/Request.hxx +++ b/src/lib/curl/Request.hxx @@ -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; }; |