summaryrefslogtreecommitdiff
path: root/libadhocutil/curlHandle.h
blob: ba1df7896350eccabd405329c6ed1af0be44ca71 (plain)
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
63
#ifndef ADHOCUTIL_CURLHANDLE_H
#define ADHOCUTIL_CURLHANDLE_H

#include "c++11Helpers.h"
#include "visibility.h"
#include <curl/curl.h> // IWYU pragma: export
#include <memory>
#include <string>

namespace AdHoc::Net {

	/// libcurl handle wrapper.
	/** Wraps a libcurl CURL * object in a C++ friendly manner. */
	class DLL_PUBLIC CurlHandle {
	public:
		/**
		 * Create a new CurlHandle.
		 * @param url Set the required CURLOPT_URL property to the given url.
		 */
		explicit CurlHandle(const std::string & url);
		/// Standard move/copy support
		SPECIAL_MEMBERS_DEFAULT_MOVE_NO_COPY(CurlHandle);
		virtual ~CurlHandle();

		/** Set option wrapper. */
		template<typename T>
		void
		setopt(CURLoption opt, const T & val)
		{
			curl_easy_setopt(curl_handle, opt, val);
		}
		/** Get info for long values */
		void getinfo(CURLINFO info, long & val) const;
		/** Get info for int values (avoids ambiguous call errors for ease of use) */
		void getinfo(CURLINFO info, int & val) const;
		/** Get info for double values */
		void getinfo(CURLINFO info, double & val) const;
		/** Get info for char * values */
		void getinfo(CURLINFO info, char *& val) const;
		/** Append the given HTTP header */
		void appendHeader(const char *);
		/** Append the given HTTP post content */
		void appendPost(const char *, const char *);
		/** Perform the CURL transfer. */
		void perform();

		/** Get the underlying CURL * handle. @warning Make changes at your own risk. */
		// NOLINTNEXTLINE(hicpp-explicit-conversions)
		operator CURL *() const;

	protected:
		/// @cond
		void checkCurlCode(CURLcode res) const;

		CURL * curl_handle;
		curl_slist * curl_headers;
		curl_httppost *postS, *postE;
		/// @endcond
	};
	using CurlHandlePtr = std::shared_ptr<CurlHandle>;
}

#endif