Request.cxx 5.61 KB
Newer Older
1
/*
2
 * Copyright 2008-2018 Max Kellermann <max.kellermann@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * 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.
 */

#include "config.h"
#include "Request.hxx"
#include "Global.hxx"
#include "Handler.hxx"
34
#include "event/Call.hxx"
35
#include "util/RuntimeError.hxx"
36
#include "util/StringStrip.hxx"
37 38 39 40 41 42
#include "util/StringView.hxx"
#include "util/CharUtil.hxx"

#include <curl/curl.h>

#include <algorithm>
43
#include <cassert>
44 45 46

#include <string.h>

47
CurlRequest::CurlRequest(CurlGlobal &_global,
48
			 CurlResponseHandler &_handler)
49 50 51
	:global(_global), handler(_handler),
	 postpone_error_event(global.GetEventLoop(),
			      BIND_THIS_METHOD(OnPostponeError))
52 53 54
{
	error_buffer[0] = 0;

55 56 57 58
	easy.SetPrivate((void *)this);
	easy.SetUserAgent("Music Player Daemon " VERSION);
	easy.SetHeaderFunction(_HeaderFunction, this);
	easy.SetWriteFunction(WriteFunction, this);
59
	easy.SetOption(CURLOPT_NETRC, 1L);
60 61 62 63
	easy.SetErrorBuffer(error_buffer);
	easy.SetNoProgress();
	easy.SetNoSignal();
	easy.SetConnectTimeout(10);
64
	easy.SetOption(CURLOPT_HTTPAUTH, (long) CURLAUTH_ANY);
65 66
}

67
CurlRequest::~CurlRequest() noexcept
68 69 70 71
{
	FreeEasy();
}

72
void
73
CurlRequest::Start()
74 75 76
{
	assert(!registered);

77
	global.Add(*this);
78 79 80
	registered = true;
}

81 82 83 84 85 86 87 88
void
CurlRequest::StartIndirect()
{
	BlockingCall(global.GetEventLoop(), [this](){
			Start();
		});
}

89
void
90
CurlRequest::Stop() noexcept
91
{
92 93
	if (!registered)
		return;
94

95
	global.Remove(*this);
96 97 98
	registered = false;
}

99 100 101 102 103 104 105 106
void
CurlRequest::StopIndirect()
{
	BlockingCall(global.GetEventLoop(), [this](){
			Stop();
		});
}

107
void
108
CurlRequest::FreeEasy() noexcept
109 110 111 112
{
	if (!easy)
		return;

113
	Stop();
114 115 116 117
	easy = nullptr;
}

void
118
CurlRequest::Resume() noexcept
119
{
120 121
	assert(registered);

122
	easy.Unpause();
123 124 125 126

	global.InvalidateSockets();
}

127
void
128 129 130
CurlRequest::FinishHeaders()
{
	if (state != State::HEADERS)
131
		return;
132 133 134 135

	state = State::BODY;

	long status = 0;
136
	easy.GetInfo(CURLINFO_RESPONSE_CODE, &status);
137

138
	handler.OnHeaders(status, std::move(headers));
139 140 141 142 143
}

void
CurlRequest::FinishBody()
{
144
	FinishHeaders();
145 146 147 148 149 150 151 152 153

	if (state != State::BODY)
		return;

	state = State::CLOSED;
	handler.OnEnd();
}

void
154
CurlRequest::Done(CURLcode result) noexcept
155
{
156
	Stop();
157 158 159 160 161 162 163 164 165 166

	try {
		if (result != CURLE_OK) {
			StripRight(error_buffer);
			const char *msg = error_buffer;
			if (*msg == 0)
				msg = curl_easy_strerror(result);
			throw FormatRuntimeError("CURL failed: %s", msg);
		}

167 168 169 170 171
		FinishBody();
	} catch (...) {
		state = State::CLOSED;
		handler.OnError(std::current_exception());
	}
172 173
}

174 175
gcc_pure
static bool
176
IsResponseBoundaryHeader(StringView s) noexcept
177
{
178
	return s.size > 5 && (s.StartsWith("HTTP/") ||
179 180
			      /* the proprietary "ICY 200 OK" is
				 emitted by Shoutcast */
181
			      s.StartsWith("ICY 2"));
182 183
}

184
inline void
185
CurlRequest::HeaderFunction(StringView s) noexcept
186 187 188 189
{
	if (state > State::HEADERS)
		return;

190
	if (IsResponseBoundaryHeader(s)) {
191 192 193 194 195 196 197 198 199 200 201 202 203 204
		/* this is the boundary to a new response, for example
		   after a redirect */
		headers.clear();
		return;
	}

	const char *header = s.data;
	const char *end = StripRight(header, header + s.size);

	const char *value = s.Find(':');
	if (value == nullptr)
		return;

	std::string name(header, value);
205 206
	std::transform(name.begin(), name.end(), name.begin(),
		       static_cast<char(*)(char)>(ToLowerASCII));
207 208 209 210 211 212 213 214 215 216 217 218 219 220

	/* skip the colon */

	++value;

	/* strip the value */

	value = StripLeft(value, end);
	end = StripRight(value, end);

	headers.emplace(std::move(name), std::string(value, end));
}

size_t
221
CurlRequest::_HeaderFunction(char *ptr, size_t size, size_t nmemb,
222
			     void *stream) noexcept
223 224 225 226 227
{
	CurlRequest &c = *(CurlRequest *)stream;

	size *= nmemb;

228
	c.HeaderFunction({ptr, size});
229 230 231 232
	return size;
}

inline size_t
233
CurlRequest::DataReceived(const void *ptr, size_t received_size) noexcept
234 235 236 237
{
	assert(received_size > 0);

	try {
238
		FinishHeaders();
239 240 241 242 243 244
		handler.OnData({ptr, received_size});
		return received_size;
	} catch (Pause) {
		return CURL_WRITEFUNC_PAUSE;
	} catch (...) {
		state = State::CLOSED;
245 246 247
		/* move the CurlResponseHandler::OnError() call into a
		   "safe" stack frame */
		postponed_error = std::current_exception();
248
		postpone_error_event.Schedule();
249
		return CURL_WRITEFUNC_PAUSE;
250 251 252 253 254
	}

}

size_t
255
CurlRequest::WriteFunction(char *ptr, size_t size, size_t nmemb,
256
			   void *stream) noexcept
257 258 259 260 261 262 263 264 265
{
	CurlRequest &c = *(CurlRequest *)stream;

	size *= nmemb;
	if (size == 0)
		return 0;

	return c.DataReceived(ptr, size);
}
266 267

void
268
CurlRequest::OnPostponeError() noexcept
269 270 271 272 273
{
	assert(postponed_error);

	handler.OnError(postponed_error);
}