lib/curl/Headers: central type definition for the header map

This commit is contained in:
Max Kellermann
2022-02-04 11:11:38 +01:00
committed by Max Kellermann
parent 29e3a17f26
commit 19d2864c34
23 changed files with 90 additions and 75 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2021 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
* modification, are permitted provided that the following conditions
@@ -27,14 +27,13 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CURL_ADAPTER_HXX
#define CURL_ADAPTER_HXX
#pragma once
#include "Headers.hxx"
#include <curl/curl.h>
#include <cstddef>
#include <map>
#include <string>
struct StringView;
class CurlEasy;
@@ -45,7 +44,7 @@ class CurlResponseHandlerAdapter {
CurlResponseHandler &handler;
std::multimap<std::string, std::string> headers;
Curl::Headers headers;
/** error message provided by libcurl */
char error_buffer[CURL_ERROR_SIZE];
@@ -83,5 +82,3 @@ private:
std::size_t size, std::size_t nmemb,
void *stream) noexcept;
};
#endif

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
* modification, are permitted provided that the following conditions
@@ -34,8 +34,7 @@
#include <utility>
void
DelegateCurlResponseHandler::OnHeaders(unsigned status,
std::multimap<std::string, std::string> &&headers)
DelegateCurlResponseHandler::OnHeaders(unsigned status, Curl::Headers &&headers)
{
parser = MakeParser(status, std::move(headers));
assert(parser);

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
* modification, are permitted provided that the following conditions
@@ -27,8 +27,7 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CURL_DELEGATE_HXX
#define CURL_DELEGATE_HXX
#pragma once
#include "Handler.hxx"
@@ -53,7 +52,7 @@ protected:
* CurlResponseParser::OnError()).
*/
virtual std::unique_ptr<CurlResponseParser> MakeParser(unsigned status,
std::multimap<std::string, std::string> &&headers) = 0;
Curl::Headers &&headers) = 0;
/**
* The parser has finished parsing the response body. This
@@ -64,10 +63,7 @@ protected:
virtual void FinishParser(std::unique_ptr<CurlResponseParser> p) = 0;
public:
void OnHeaders(unsigned status,
std::multimap<std::string, std::string> &&headers) final;
void OnHeaders(unsigned status, Curl::Headers &&headers) final;
void OnData(ConstBuffer<void> data) final;
void OnEnd() final;
};
#endif

View File

@@ -31,8 +31,7 @@
#include "String.hxx"
std::string
EncodeForm(CURL *curl,
const std::multimap<std::string, std::string> &fields) noexcept
EncodeForm(CURL *curl, const Curl::Headers &fields) noexcept
{
std::string result;

View File

@@ -30,17 +30,17 @@
#ifndef CURL_FORM_HXX
#define CURL_FORM_HXX
#include "Headers.hxx"
#include <curl/curl.h>
#include <string>
#include <map>
/**
* Encode the given map of form fields to a
* "application/x-www-form-urlencoded" string.
*/
std::string
EncodeForm(CURL *curl,
const std::multimap<std::string, std::string> &fields) noexcept;
EncodeForm(CURL *curl, const Curl::Headers &fields) noexcept;
#endif

View File

@@ -27,14 +27,12 @@
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef CURL_HANDLER_HXX
#define CURL_HANDLER_HXX
#pragma once
#include "Headers.hxx"
#include "util/ConstBuffer.hxx"
#include <exception>
#include <string>
#include <map>
/**
* Asynchronous response handler for a #CurlRequest.
@@ -53,8 +51,7 @@ public:
/**
* Status line and headers have been received.
*/
virtual void OnHeaders(unsigned status,
std::multimap<std::string, std::string> &&headers) = 0;
virtual void OnHeaders(unsigned status, Curl::Headers &&headers) = 0;
/**
* Response body data has been received.
@@ -75,5 +72,3 @@ public:
*/
virtual void OnError(std::exception_ptr e) noexcept = 0;
};
#endif

42
src/lib/curl/Headers.hxx Normal file
View File

@@ -0,0 +1,42 @@
/*
* Copyright 2020-2021 CM4all GmbH
* All rights reserved.
*
* author: Max Kellermann <mk@cm4all.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <map>
#include <string>
namespace Curl {
using Headers = std::multimap<std::string, std::string>;
} // namespace Curl

View File

@@ -55,7 +55,7 @@ UPnPDeviceDirectory::Downloader::Destroy() noexcept
void
UPnPDeviceDirectory::Downloader::OnHeaders(unsigned status,
std::multimap<std::string, std::string> &&)
Curl::Headers &&)
{
if (status != 200) {
Destroy();

View File

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