MPD: lib/curl/Request.hxx Source File

MPD 0.20.15

Request.hxx
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2008-2017 Max Kellermann <max.kellermann@gmail.com>
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the
14 * distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 * OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifndef CURL_REQUEST_HXX
31 #define CURL_REQUEST_HXX
32
33 #include "Easy.hxx"
34 #include "event/DeferredMonitor.hxx"
35
36 #include <map>
37 #include <string>
38 #include <exception>
39
40 struct StringView;
41 class CurlGlobal;
42 class CurlResponseHandler;
43
44 class CurlRequest final : DeferredMonitor {
45 CurlGlobal &global;
46
47 CurlResponseHandler &handler;
48
50 CurlEasy easy;
51
52 enum class State {
53 HEADERS,
54 BODY,
55 CLOSED,
56 } state = State::HEADERS;
57
58 std::multimap<std::string, std::string> headers;
59
68 std::exception_ptr postponed_error;
69
71 char error_buffer[CURL_ERROR_SIZE];
72
73 bool registered = false;
74
75 public:
79 CurlRequest(CurlGlobal &_global, const char *url,
80 CurlResponseHandler &_handler);
81 ~CurlRequest();
82
83 CurlRequest(const CurlRequest &) = delete;
84 CurlRequest &operator=(const CurlRequest &) = delete;
85
92 void Start();
93
99 void Stop();
100
101 CURL *Get() {
102 return easy.Get();
103 }
104
105 template<typename T>
106 void SetOption(CURLoption option, T value) {
107 easy.SetOption(option, value);
108 }
109
114 struct Pause {};
115
116 void Resume();
117
121 void Done(CURLcode result);
122
123 private:
128 void FreeEasy();
129
130 void FinishHeaders();
131 void FinishBody();
132
133 size_t DataReceived(const void *ptr, size_t size);
134
135 void HeaderFunction(StringView s);
136
138 static size_t _HeaderFunction(void *ptr, size_t size, size_t nmemb,
139 void *stream);
140
142 static size_t WriteFunction(void *ptr, size_t size, size_t nmemb,
143 void *stream);
144
145 /* virtual methods from class DeferredMonitor */
146 void RunDeferred() override;
147 };
148
149 #endif
CurlRequest::CurlRequest
CurlRequest(CurlGlobal &_global, const char *url, CurlResponseHandler &_handler)
To start sending the request, call Start().
CurlRequest::SetOption
void SetOption(CURLoption option, T value)
Definition: Request.hxx:106
CurlRequest::~CurlRequest
~CurlRequest()
CurlEasy::Get
CURL * Get()
Definition: Easy.hxx:79
CurlEasy
An OO wrapper for a "CURL*" (a libCURL "easy" handle).
Definition: Easy.hxx:42
DeferredMonitor
Defer execution of an event into an EventLoop.
Definition: DeferredMonitor.hxx:32
CurlRequest::Start
void Start()
Register this request via CurlGlobal::Add(), which starts the request.
CurlResponseHandler
Definition: Handler.hxx:39
CurlRequest::Pause
CurlResponseHandler::OnData() shall throw this to pause the stream.
Definition: Request.hxx:114
CurlRequest::Resume
void Resume()
CurlRequest
Definition: Request.hxx:44
StringView
Definition: StringView.hxx:37
CurlRequest::Stop
void Stop()
Unregister this request via CurlGlobal::Remove().
CurlEasy::SetOption
void SetOption(CURLoption option, T value)
Definition: Easy.hxx:84
DeferredMonitor.hxx
CurlRequest::Get
CURL * Get()
Definition: Request.hxx:101
CurlRequest::operator=
CurlRequest & operator=(const CurlRequest &)=delete
CurlGlobal
Manager for the global CURLM object.
Definition: Global.hxx:43
Easy.hxx
CurlRequest::Done
void Done(CURLcode result)
A HTTP request is finished.