Modernize lyrics screen
[ncmpcpp.git] / src / curl_handle.cpp
blobde1295dbb0dcaa50a6a2655354197c04bee71985
1 /***************************************************************************
2 * Copyright (C) 2008-2016 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 #ifdef HAVE_CURL_CURL_H
25 #include <cstdlib>
27 namespace
29 size_t write_data(char *buffer, size_t size, size_t nmemb, void *data)
31 size_t result = size*nmemb;
32 static_cast<std::string *>(data)->append(buffer, result);
33 return result;
37 CURLcode Curl::perform(std::string &data, const std::string &URL, const std::string &referer, bool follow_redirect, unsigned timeout)
39 CURLcode result;
40 CURL *c = curl_easy_init();
41 curl_easy_setopt(c, CURLOPT_URL, URL.c_str());
42 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_data);
43 curl_easy_setopt(c, CURLOPT_WRITEDATA, &data);
44 curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, timeout);
45 curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1);
46 // Workaround last.fm SSL certificate problems.
47 curl_easy_setopt(c, CURLOPT_SSL_VERIFYHOST, 0);
48 curl_easy_setopt(c, CURLOPT_USERAGENT, "ncmpcpp " VERSION);
49 if (follow_redirect)
50 curl_easy_setopt(c, CURLOPT_FOLLOWLOCATION, 1L);
51 if (!referer.empty())
52 curl_easy_setopt(c, CURLOPT_REFERER, referer.c_str());
53 result = curl_easy_perform(c);
54 curl_easy_cleanup(c);
55 return result;
58 std::string Curl::escape(const std::string &s)
60 char *cs = curl_easy_escape(0, s.c_str(), s.length());
61 std::string result(cs);
62 curl_free(cs);
63 return result;
66 #endif // HAVE_CURL_CURL_H