File: Request.hxx

package info (click to toggle)
mpdscribble 0.25-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 968 kB
  • sloc: cpp: 3,803; sh: 164; makefile: 17
file content (62 lines) | stat: -rw-r--r-- 1,167 bytes parent folder | download
1
2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#ifndef CURL_REQUEST_HXX
#define CURL_REQUEST_HXX

#include "Easy.hxx"

#include <string>

class CurlGlobal;
class HttpResponseHandler;

/**
 * A non-blocking HTTP request integrated via #CurlGlobal into the
 * #EventLoop.
 */
class CurlRequest final
{
	CurlGlobal &global;

	HttpResponseHandler &handler;

	/** the CURL easy handle */
	CurlEasy curl;

	/** the POST request body */
	std::string request_body;

	/** the response body */
	std::string response_body;

	/** error message provided by libcurl */
	char error[CURL_ERROR_SIZE];

public:
	CurlRequest(CurlGlobal &global,
		    const char *url, std::string &&_request_body,
		    HttpResponseHandler &_handler);
	~CurlRequest() noexcept;

	CURL *Get() noexcept {
		return curl.Get();
	}

	/**
	 * A HTTP request is finished: invoke its callback and free it.
	 */
	void Done(CURLcode result) noexcept;

private:
	void CheckResponse(CURLcode result);

	/**
	 * Called by curl when new data is available.
	 */
	static size_t WriteFunction(char *ptr, size_t size, size_t nmemb,
				    void *stream) noexcept;

};

#endif