lib/curl: use std::span

This commit is contained in:
Max Kellermann 2022-06-27 17:27:37 +02:00 committed by Max Kellermann
parent 062df65b1e
commit 1da09f5b1b
12 changed files with 32 additions and 33 deletions

View File

@ -132,7 +132,7 @@ private:
/* virtual methods from CurlResponseHandler */ /* virtual methods from CurlResponseHandler */
void OnHeaders(unsigned status, Curl::Headers &&headers) override; void OnHeaders(unsigned status, Curl::Headers &&headers) override;
void OnData(ConstBuffer<void> data) override; void OnData(std::span<const std::byte> data) override;
void OnEnd() override; void OnEnd() override;
void OnError(std::exception_ptr e) noexcept override; void OnError(std::exception_ptr e) noexcept override;
@ -296,21 +296,21 @@ CurlInputStream::OnHeaders(unsigned status,
} }
void void
CurlInputStream::OnData(ConstBuffer<void> data) CurlInputStream::OnData(std::span<const std::byte> data)
{ {
assert(data.size > 0); assert(!data.empty());
const std::scoped_lock<Mutex> protect(mutex); const std::scoped_lock<Mutex> protect(mutex);
if (IsSeekPending()) if (IsSeekPending())
SeekDone(); SeekDone();
if (data.size > GetBufferSpace()) { if (data.size() > GetBufferSpace()) {
AsyncInputStream::Pause(); AsyncInputStream::Pause();
throw CurlResponseHandler::Pause{}; throw CurlResponseHandler::Pause{};
} }
AppendToBuffer(data.data, data.size); AppendToBuffer(data.data(), data.size());
} }
void void

View File

@ -168,7 +168,7 @@ CurlResponseHandlerAdapter::DataReceived(const void *ptr,
try { try {
FinishHeaders(); FinishHeaders();
handler.OnData({ptr, received_size}); handler.OnData({(const std::byte *)ptr, received_size});
return received_size; return received_size;
} catch (CurlResponseHandler::Pause) { } catch (CurlResponseHandler::Pause) {
return CURL_WRITEFUNC_PAUSE; return CURL_WRITEFUNC_PAUSE;

View File

@ -41,7 +41,7 @@ DelegateCurlResponseHandler::OnHeaders(unsigned status, Curl::Headers &&headers)
} }
void void
DelegateCurlResponseHandler::OnData(ConstBuffer<void> data) DelegateCurlResponseHandler::OnData(std::span<const std::byte> data)
{ {
parser->OnData(data); parser->OnData(data);
} }

View File

@ -64,6 +64,6 @@ protected:
public: public:
void OnHeaders(unsigned status, Curl::Headers &&headers) final; void OnHeaders(unsigned status, Curl::Headers &&headers) final;
void OnData(ConstBuffer<void> data) final; void OnData(std::span<const std::byte> data) final;
void OnEnd() final; void OnEnd() final;
}; };

View File

@ -30,9 +30,10 @@
#pragma once #pragma once
#include "Headers.hxx" #include "Headers.hxx"
#include "util/ConstBuffer.hxx"
#include <cstddef>
#include <exception> #include <exception>
#include <span>
/** /**
* Asynchronous response handler for a #CurlRequest. * Asynchronous response handler for a #CurlRequest.
@ -58,7 +59,7 @@ public:
* *
* May throw #Pause (but nothing else). * May throw #Pause (but nothing else).
*/ */
virtual void OnData(ConstBuffer<void> data) = 0; virtual void OnData(std::span<const std::byte> data) = 0;
/** /**
* The response has ended. The method is allowed to delete the * The response has ended. The method is allowed to delete the

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2008-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2008-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,16 +27,15 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef CURL_PARSER_HXX #pragma once
#define CURL_PARSER_HXX
#include <span>
template<typename T> struct ConstBuffer; template<typename T> struct ConstBuffer;
class CurlResponseParser { class CurlResponseParser {
public: public:
virtual ~CurlResponseParser() = default; virtual ~CurlResponseParser() = default;
virtual void OnData(ConstBuffer<void> data) = 0; virtual void OnData(std::span<const std::byte> data) = 0;
virtual void OnEnd() = 0; virtual void OnEnd() = 0;
}; };
#endif

View File

@ -24,6 +24,7 @@
#include "event/Call.hxx" #include "event/Call.hxx"
#include "util/DeleteDisposer.hxx" #include "util/DeleteDisposer.hxx"
#include "util/ScopeExit.hxx" #include "util/ScopeExit.hxx"
#include "util/SpanCast.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include <upnptools.h> #include <upnptools.h>
@ -64,9 +65,9 @@ UPnPDeviceDirectory::Downloader::OnHeaders(unsigned status,
} }
void void
UPnPDeviceDirectory::Downloader::OnData(ConstBuffer<void> _data) UPnPDeviceDirectory::Downloader::OnData(std::span<const std::byte> src)
{ {
data.append((const char *)_data.data, _data.size); data.append(ToStringView(src));
} }
void void

View File

@ -114,7 +114,7 @@ class UPnPDeviceDirectory final : UpnpCallback {
/* virtual methods from CurlResponseHandler */ /* virtual methods from CurlResponseHandler */
void OnHeaders(unsigned status, Curl::Headers &&headers) override; void OnHeaders(unsigned status, Curl::Headers &&headers) override;
void OnData(ConstBuffer<void> data) override; void OnData(std::span<const std::byte> data) override;
void OnEnd() override; void OnEnd() override;
void OnError(std::exception_ptr e) noexcept override; void OnError(std::exception_ptr e) noexcept override;
}; };

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2008-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2008-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -31,9 +31,9 @@
#include "util/ConstBuffer.hxx" #include "util/ConstBuffer.hxx"
void void
YajlResponseParser::OnData(ConstBuffer<void> data) YajlResponseParser::OnData(std::span<const std::byte> data)
{ {
handle.Parse((const unsigned char *)data.data, data.size); handle.Parse((const unsigned char *)data.data(), data.size());
} }
void void

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2008-2018 Max Kellermann <max.kellermann@gmail.com> * Copyright 2008-2022 Max Kellermann <max.kellermann@gmail.com>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -27,8 +27,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE. * OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef YAJL_RESPONSE_PARSER_HXX #pragma once
#define YAJL_RESPONSE_PARSER_HXX
#include "Handle.hxx" #include "Handle.hxx"
#include "lib/curl/Parser.hxx" #include "lib/curl/Parser.hxx"
@ -45,8 +44,6 @@ public:
:handle(std::forward<Args>(args)...) {} :handle(std::forward<Args>(args)...) {}
/* virtual methods fro CurlResponseParser */ /* virtual methods fro CurlResponseParser */
void OnData(ConstBuffer<void> data) final; void OnData(std::span<const std::byte> data) final;
void OnEnd() override; void OnEnd() override;
}; };
#endif

View File

@ -37,6 +37,7 @@
#include "time/Parser.hxx" #include "time/Parser.hxx"
#include "util/ASCII.hxx" #include "util/ASCII.hxx"
#include "util/RuntimeError.hxx" #include "util/RuntimeError.hxx"
#include "util/SpanCast.hxx"
#include "util/StringCompare.hxx" #include "util/StringCompare.hxx"
#include "util/StringFormat.hxx" #include "util/StringFormat.hxx"
#include "util/StringView.hxx" #include "util/StringView.hxx"
@ -307,9 +308,9 @@ private:
throw std::runtime_error("Unexpected Content-Type from WebDAV server"); throw std::runtime_error("Unexpected Content-Type from WebDAV server");
} }
void OnData(ConstBuffer<void> _data) final { void OnData(std::span<const std::byte> _src) final {
const auto data = ConstBuffer<char>::FromVoid(_data); auto src = ToStringView(_src);
Parse(data.data, data.size); Parse(src.data(), src.size());
} }
void OnEnd() final { void OnEnd() final {

View File

@ -48,9 +48,9 @@ public:
i.first.c_str(), i.second.c_str()); i.first.c_str(), i.second.c_str());
} }
void OnData(ConstBuffer<void> data) override { void OnData(std::span<const std::byte> data) override {
try { try {
if (fwrite(data.data, data.size, 1, stdout) != 1) if (fwrite(data.data(), data.size(), 1, stdout) != 1)
throw std::runtime_error("Failed to write"); throw std::runtime_error("Failed to write");
} catch (...) { } catch (...) {
OnError(std::current_exception()); OnError(std::current_exception());