make sure to include boost posix_time where needed
[ncmpcpp.git] / src / curl_handle.cpp
blob663a931ff2dbd04808da8ca035397f212b624074
1 /***************************************************************************
2 * Copyright (C) 2008-2014 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>
26 #include <pthread.h>
28 namespace
30 size_t write_data(char *buffer, size_t size, size_t nmemb, void *data)
32 size_t result = size*nmemb;
33 static_cast<std::string *>(data)->append(buffer, result);
34 return result;
38 CURLcode Curl::perform(std::string &data, const std::string &URL, const std::string &referer, unsigned timeout)
40 CURLcode result;
41 CURL *c = curl_easy_init();
42 curl_easy_setopt(c, CURLOPT_URL, URL.c_str());
43 curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, write_data);
44 curl_easy_setopt(c, CURLOPT_WRITEDATA, &data);
45 curl_easy_setopt(c, CURLOPT_CONNECTTIMEOUT, timeout);
46 curl_easy_setopt(c, CURLOPT_NOSIGNAL, 1);
47 curl_easy_setopt(c, CURLOPT_USERAGENT, "ncmpcpp " VERSION);
48 if (!referer.empty())
49 curl_easy_setopt(c, CURLOPT_REFERER, referer.c_str());
50 result = curl_easy_perform(c);
51 curl_easy_cleanup(c);
52 return result;
55 std::string Curl::escape(const std::string &s)
57 char *cs = curl_easy_escape(0, s.c_str(), s.length());
58 std::string result(cs);
59 curl_free(cs);
60 return result;
63 #endif // HAVE_CURL_CURL_H