Fix InternetLyricsFetcher
[ncmpcpp.git] / src / curl_handle.cpp
blob7be15675bfafadc7bdc74f8324e2e2e9a759f9ab
1 /***************************************************************************
2 * Copyright (C) 2008-2017 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "curl_handle.h"
23 #include <cstdlib>
25 namespace
27 size_t write_data(char *buffer, size_t size, size_t nmemb, void *data)
29 size_t result = size*nmemb;
30 static_cast<std::string *>(data)->append(buffer, result);
31 return result;
35 CURLcode Curl::perform(std::string &data, const std::string &URL, const std::string &referer, bool follow_redirect, unsigned timeout)
37 CURLcode result;
38 CURL *c = curl_easy_init();
39 curl_easy_setopt(c, CURLOPT_URL, URL.c_str());
40 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_data);
41 curl_easy_setopt(c, CURLOPT_WRITEDATA, &data);
42 curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, timeout);
43 curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1);
44 // Workaround last.fm SSL certificate problems.
45 curl_easy_setopt(c, CURLOPT_SSL_VERIFYHOST, 0);
46 curl_easy_setopt(c, CURLOPT_USERAGENT, "ncmpcpp " VERSION);
47 if (follow_redirect)
48 curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
49 if (!referer.empty())
50 curl_easy_setopt(c, CURLOPT_REFERER, referer.c_str());
51 result = curl_easy_perform(c);
52 curl_easy_cleanup(c);
53 return result;
56 std::string Curl::escape(const std::string &s)
58 char *cs = curl_easy_escape(0, s.c_str(), s.length());
59 std::string result(cs);
60 curl_free(cs);
61 return result;