lib/curl/Request: remove redundant CurlEasy wrapper methods

This commit is contained in:
Max Kellermann 2023-12-09 20:47:32 +01:00
parent e9c40dead8
commit 642de1510b
5 changed files with 32 additions and 44 deletions

View File

@ -456,7 +456,7 @@ CurlInputStream::~CurlInputStream() noexcept
} }
static CurlEasy static CurlEasy
CreateEasy(const char *url) CreateEasy(const char *url, struct curl_slist *headers)
{ {
CurlEasy easy{url}; CurlEasy easy{url};
@ -503,14 +503,17 @@ CreateEasy(const char *url)
easy.SetOption(CURLOPT_TCP_KEEPIDLE, tcp_keepidle); easy.SetOption(CURLOPT_TCP_KEEPIDLE, tcp_keepidle);
easy.SetOption(CURLOPT_TCP_KEEPINTVL, tcp_keepintvl); easy.SetOption(CURLOPT_TCP_KEEPINTVL, tcp_keepintvl);
easy.SetRequestHeaders(headers);
return easy; return easy;
} }
void void
CurlInputStream::InitEasy() CurlInputStream::InitEasy()
{ {
request = new CurlRequest(**curl_init, CreateEasy(GetURI()), *this); request = new CurlRequest(**curl_init,
request->SetRequestHeaders(request_headers.Get()); CreateEasy(GetURI(), request_headers.Get()),
*this);
} }
void void
@ -540,8 +543,8 @@ CurlInputStream::SeekInternal(offset_type new_offset)
/* send the "Range" header */ /* send the "Range" header */
if (offset > 0) if (offset > 0)
request->SetOption(CURLOPT_RANGE, request->GetEasy().SetOption(CURLOPT_RANGE,
fmt::format_int{offset}.c_str()); fmt::format_int{offset}.c_str());
StartRequest(); StartRequest();
} }

View File

@ -109,9 +109,9 @@ QobuzLoginRequest::QobuzLoginRequest(CurlGlobal &curl,
:request(curl, *this), :request(curl, *this),
handler(_handler) handler(_handler)
{ {
request.SetUrl(MakeLoginUrl(request.Get(), base_url, app_id, request.GetEasy().SetURL(MakeLoginUrl(request.Get(), base_url, app_id,
username, email, password, username, email, password,
device_manufacturer_id).c_str()); device_manufacturer_id).c_str());
} }
QobuzLoginRequest::~QobuzLoginRequest() noexcept QobuzLoginRequest::~QobuzLoginRequest() noexcept

View File

@ -69,7 +69,7 @@ QobuzTrackRequest::QobuzTrackRequest(QobuzClient &client,
{ {
request_headers.Append(("X-User-Auth-Token:" request_headers.Append(("X-User-Auth-Token:"
+ session.user_auth_token).c_str()); + session.user_auth_token).c_str());
request.SetOption(CURLOPT_HTTPHEADER, request_headers.Get()); request.GetEasy().SetRequestHeaders(request_headers.Get());
} }
QobuzTrackRequest::~QobuzTrackRequest() noexcept QobuzTrackRequest::~QobuzTrackRequest() noexcept

View File

@ -1,14 +1,11 @@
// SPDX-License-Identifier: BSD-2-Clause // SPDX-License-Identifier: BSD-2-Clause
// author: Max Kellermann <max.kellermann@gmail.com> // author: Max Kellermann <max.kellermann@gmail.com>
#ifndef CURL_REQUEST_HXX #pragma once
#define CURL_REQUEST_HXX
#include "Easy.hxx" #include "Easy.hxx"
#include "Adapter.hxx" #include "Adapter.hxx"
#include <cstddef>
class CurlGlobal; class CurlGlobal;
class CurlResponseHandler; class CurlResponseHandler;
@ -38,7 +35,7 @@ public:
CurlRequest(CurlGlobal &_global, const char *url, CurlRequest(CurlGlobal &_global, const char *url,
CurlResponseHandler &_handler) CurlResponseHandler &_handler)
:CurlRequest(_global, _handler) { :CurlRequest(_global, _handler) {
SetUrl(url); easy.SetURL(url);
} }
~CurlRequest() noexcept; ~CurlRequest() noexcept;
@ -84,19 +81,6 @@ public:
return easy; return easy;
} }
template<typename T>
void SetOption(CURLoption option, T value) {
easy.SetOption(option, value);
}
void SetUrl(const char *url) {
easy.SetURL(url);
}
void SetRequestHeaders(struct curl_slist *request_headers) {
easy.SetRequestHeaders(request_headers);
}
void Resume() noexcept; void Resume() noexcept;
/** /**
@ -113,5 +97,3 @@ private:
*/ */
void FreeEasy() noexcept; void FreeEasy() noexcept;
}; };
#endif

View File

@ -31,6 +31,8 @@
#include <string> #include <string>
#include <utility> #include <utility>
using std::string_view_literals::operator""sv;
class CurlStorage final : public Storage { class CurlStorage final : public Storage {
const std::string base; const std::string base;
@ -244,29 +246,30 @@ public:
:BlockingHttpRequest(_curl, _uri), :BlockingHttpRequest(_curl, _uri),
CommonExpatParser(ExpatNamespaceSeparator{'|'}) CommonExpatParser(ExpatNamespaceSeparator{'|'})
{ {
request.SetOption(CURLOPT_CUSTOMREQUEST, "PROPFIND"); auto &easy = request.GetEasy();
request.SetOption(CURLOPT_FOLLOWLOCATION, 1L);
request.SetOption(CURLOPT_MAXREDIRS, 1L); easy.SetOption(CURLOPT_CUSTOMREQUEST, "PROPFIND");
easy.SetOption(CURLOPT_FOLLOWLOCATION, 1L);
easy.SetOption(CURLOPT_MAXREDIRS, 1L);
/* this option eliminates the probe request when /* this option eliminates the probe request when
username/password are specified */ username/password are specified */
request.SetOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC); easy.SetOption(CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
request_headers.Append(FmtBuffer<40>("depth: {}", depth)); request_headers.Append(FmtBuffer<40>("depth: {}", depth));
request_headers.Append("content-type: text/xml"); request_headers.Append("content-type: text/xml");
request.SetOption(CURLOPT_HTTPHEADER, request_headers.Get()); easy.SetRequestHeaders(request_headers.Get());
request.SetOption(CURLOPT_POSTFIELDS, easy.SetRequestBody("<?xml version=\"1.0\"?>\n"
"<?xml version=\"1.0\"?>\n" "<a:propfind xmlns:a=\"DAV:\">"
"<a:propfind xmlns:a=\"DAV:\">" "<a:prop>"
"<a:prop>" "<a:resourcetype/>"
"<a:resourcetype/>" "<a:getcontenttype/>"
"<a:getcontenttype/>" "<a:getcontentlength/>"
"<a:getcontentlength/>" "<a:getlastmodified/>"
"<a:getlastmodified/>" "</a:prop>"
"</a:prop>" "</a:propfind>"sv);
"</a:propfind>");
} }
using BlockingHttpRequest::GetEasy; using BlockingHttpRequest::GetEasy;