Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
curl.cc
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file utility/curl.cc
11 /// @brief Implements a OO interface to CURL wrapping and hiding the ugly C callbacks.
12 /// @author Mike Tyka (mike.tyka@gmail.com)
13 
14 #ifdef WITHCURL
15 
16 #include <utility/curl.hh>
18 
19 
20 namespace utility {
21 
22 // must include -lcurl during linking if you're using -dwithcurl
23 
24  CurlGet::CurlGet()
25  {
26  }
27 
28  // This is the writer call back function used by curl
29  int
30  CurlGet::writer(char *data, size_t size, size_t nmemb, std::string *buffer)
31  {
32  // What we will return
33  int result = 0;
34 
35  // Is there anything in the buffer?
36  if (buffer != NULL)
37  {
38  // Append the data to the buffer
39  buffer->append(data, size * nmemb);
40 
41  // How much did we write?
42  result = size * nmemb;
43  }
44 
45  return result;
46  }
47 
48  char *
49  CurlGet::getErrorBuffer()
50  {
51  return &errorBuffer[0];
52  }
53 
54  std::string *
55  CurlGet::getbuffer()
56  {
57  return &buffer;
58  }
59 
60  std::string
61  CurlGet::get( const std::string &url )
62  {
63  // Our curl objects
64  CURL *curl;
65  CURLcode result;
66 
67  // Create our curl handle
68  curl = curl_easy_init();
69 
70  if (curl)
71  {
72  // Now set up all of the curl options
73  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, getErrorBuffer() );
74  curl_easy_setopt(curl, CURLOPT_URL, url.c_str() );
75  curl_easy_setopt(curl, CURLOPT_HEADER, 0);
76  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
77  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, this->writer);
78  curl_easy_setopt(curl, CURLOPT_WRITEDATA, getbuffer() );
79 
80  // Attempt to retrieve the remote page
81  result = curl_easy_perform(curl);
82 
83  // Always cleanup
84  curl_easy_cleanup(curl);
85 
86  // Did we succeed?
87  if (result == CURLE_OK)
88  {
89  return buffer;
90  }
91  else
92  {
93  std::stringstream ss;
94  ss << "Error: [" << result << "] - " << errorBuffer;
95  throw std::string( ss.str() );
96  }
97  }
98  }
99 
100 
101  CurlPost::CurlPost()
102  {
103  }
104 
105  // This is the writer call back function used by curl
106  int
107  CurlPost::writer(char *data, size_t size, size_t nmemb, std::string *buffer)
108  {
109  // What we will return
110  int result = 0;
111  //std::cout << "Buffer: " << *buffer << std::endl;
112  // Is there anything in the buffer?
113  if (buffer != NULL)
114  {
115  // Append the data to the buffer
116  buffer->append(data, size * nmemb);
117 
118  // How much did we write?
119  result = size * nmemb;
120  }
121 
122  return result;
123  }
124 
125  char *
126  CurlPost::getErrorBuffer()
127  {
128  return &errorBuffer[0];
129  }
130 
131  std::string *
132  CurlPost::getreadbuffer()
133  {
134  return &readbuffer;
135  }
136 
137  std::string *
138  CurlPost::getwritebuffer()
139  {
140  return &writebuffer;
141  }
142 
143  std::string
144  CurlPost::post( const std::string &url, const std::string &data, const std::string &fields )
145  {
146  // Our curl objects
147  CURL *curl;
148  CURLcode result;
149 
150  // Create our curl handle
151  curl = curl_easy_init();
152 
153  if (curl)
154  {
155  // fill read buffer
156  readbuffer = data;
157 
158  // Now set up all of the curl options
159  curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, getErrorBuffer() );
160  curl_easy_setopt(curl, CURLOPT_URL, url.c_str() );
161  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, fields.c_str() );
162  curl_easy_setopt(curl, CURLOPT_HEADER, 0);
163  curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
164  curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, this->writer);
165  curl_easy_setopt(curl, CURLOPT_WRITEDATA, getwritebuffer() );
166 
167  // Attempt to retrieve the remote page
168  result = curl_easy_perform(curl);
169 
170  // Always cleanup
171  curl_easy_cleanup(curl);
172 
173  // Did we succeed?
174  if (result == CURLE_OK)
175  {
176  return writebuffer;
177  }
178  else
179  {
180  std::stringstream ss;
181  ss << "Error: [" << result << "] - " << errorBuffer;
182  throw std::string( ss.str() );
183  }
184  }
185  }
186 
187 
188 }
189 
190 #endif
191 
dictionary size
Definition: amino_acids.py:44
common derived classes for thrown exceptions
Implements a OO interface to CURL wrapping and hiding the ugly C callbacks.