File: curl-helper.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (137 lines) | stat: -rw-r--r-- 2,972 bytes parent folder | download | duplicates (2)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "curl-helper.hpp"
#include "log-helper.hpp"

#include <QDir>
#include <QFileInfo>
#include <curl/curl.h>

namespace advss {

#if defined(WIN32)
constexpr auto curl_library_name = "libcurl.dll";
#elif __APPLE__
constexpr auto curl_library_name = "libcurl.4.dylib";
#else
constexpr auto curl_library_name = "libcurl.so.4";
#endif

CurlHelper::CurlHelper()
{
	if (LoadLib()) {
		_curl = _init();
		_initialized = true;
	}
}

CurlHelper::~CurlHelper()
{
	if (_lib) {
		if (_cleanup) {
			_cleanup(_curl);
		}
		delete _lib;
		_lib = nullptr;
	}
}

CurlHelper &CurlHelper::GetInstance()
{
	static CurlHelper curl;
	return curl;
}

bool CurlHelper::Initialized()
{
	return GetInstance()._initialized;
}

curl_slist *CurlHelper::SlistAppend(curl_slist *list, const char *string)
{
	auto &curl = GetInstance();
	if (!curl._initialized) {
		return nullptr;
	}
	return curl._slistAppend(list, string);
}

CURLcode CurlHelper::Perform()
{
	auto &curl = GetInstance();
	if (!curl._initialized) {
		return CURLE_FAILED_INIT;
	}
	return curl._perform(curl._curl);
}

char *CurlHelper::GetError(CURLcode code)
{
	auto &curl = GetInstance();
	if (!curl._initialized) {
		return (char *)"CURL initialization failed";
	}

	return curl._error(code);
}

bool CurlHelper::LoadLib()
{
	_lib = new QLibrary(curl_library_name, nullptr);
	if (Resolve()) {
		blog(LOG_INFO, "found curl library");
		return true;
	} else {
		delete _lib;
		_lib = nullptr;
		blog(LOG_WARNING, "couldn't find the curl library in PATH");
	}
	QStringList locations;
	locations << QDir::currentPath();
#if defined(__linux__) || defined(__APPLE__)
	locations << "/usr/lib";
	locations << "/usr/local/lib";
	locations << "/usr/lib/x86_64-linux-gnu";
	locations << "/usr/local/opt/curl/lib";
#endif
	for (QString path : locations) {
		blog(LOG_INFO, "trying '%s'", path.toUtf8().constData());
		QFileInfo libPath(
			QDir(path).absoluteFilePath(curl_library_name));

		if (libPath.exists() && libPath.isFile()) {
			QString libFilePath = libPath.absoluteFilePath();
			blog(LOG_INFO, "found curl library at '%s'",
			     libFilePath.toUtf8().constData());

			_lib = new QLibrary(libFilePath, nullptr);
			if (Resolve()) {
				return true;
			} else {
				delete _lib;
				_lib = nullptr;
			}
		}
	}
	blog(LOG_WARNING, "can't find the curl library");
	return false;
}

bool CurlHelper::Resolve()
{
	_init = (initFunction)_lib->resolve("curl_easy_init");
	_setopt = (setOptFunction)_lib->resolve("curl_easy_setopt");
	_slistAppend = (slistAppendFunction)_lib->resolve("curl_slist_append");
	_perform = (performFunction)_lib->resolve("curl_easy_perform");
	_cleanup = (cleanupFunction)_lib->resolve("curl_easy_cleanup");
	_error = (errorFunction)_lib->resolve("curl_easy_strerror");

	if (_init && _setopt && _slistAppend && _perform && _cleanup &&
	    _error) {
		blog(LOG_INFO, "curl loaded successfully");
		return true;
	}

	blog(LOG_INFO, "curl symbols not resolved");
	return false;
}

} // namespace advss