libcoverart  1.0.0
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
HTTPFetch.h
Go to the documentation of this file.
1 /* --------------------------------------------------------------------------
2 
3  libcoverart - Client library to access CoverArtArchive
4 
5  Copyright (C) 2012 Andrew Hawkins
6 
7  This file is part of libcoverart.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  libcoverart is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with this library. If not, see <http://www.gnu.org/licenses/>.
21 
22  $Id$
23 
24 ----------------------------------------------------------------------------*/
25 
26 #ifndef _COVERARTARCHIVE_HTTP_FETCH_
27 #define _COVERARTARCHIVE_HTTP_FETCH_
28 
29 #include <string>
30 #include <vector>
31 
32 namespace CoverArtArchive
33 {
34  class CHTTPFetchPrivate;
35 
36  class CExceptionBase: public std::exception
37  {
38  public:
39  CExceptionBase(const std::string& ErrorMessage, const std::string& Exception)
40  : m_ErrorMessage(ErrorMessage),
41  m_Exception(Exception)
42  {
43  m_FullMessage=m_Exception + ": " + m_ErrorMessage;
44  }
45 
46  virtual ~CExceptionBase() throw() {};
47 
48  virtual const char* what() const throw()
49  {
50  return m_FullMessage.c_str();
51  }
52 
53  private:
54  std::string m_ErrorMessage;
55  std::string m_Exception;
56  std::string m_FullMessage;
57  };
58 
64  {
65  public:
66  CConnectionError(const std::string& ErrorMessage)
67  : CExceptionBase(ErrorMessage,"Connection error")
68  {
69  }
70  };
71 
77  {
78  public:
79  CTimeoutError(const std::string& ErrorMessage)
80  : CExceptionBase(ErrorMessage,"Timeout error")
81  {
82  }
83  };
84 
90  {
91  public:
92  CAuthenticationError(const std::string& ErrorMessage)
93  : CExceptionBase(ErrorMessage,"Authentication error")
94  {
95  }
96  };
97 
103  {
104  public:
105  CFetchError(const std::string& ErrorMessage)
106  : CExceptionBase(ErrorMessage,"Fetch error")
107  {
108  }
109  };
110 
116  {
117  public:
118  CRequestError(const std::string& ErrorMessage)
119  : CExceptionBase(ErrorMessage,"Request error")
120  {
121  }
122  };
123 
129  {
130  public:
131  CResourceNotFoundError(const std::string& ErrorMessage)
132  : CExceptionBase(ErrorMessage,"Resource not found error")
133  {
134  }
135  };
136 
141  class CRedirect: public CExceptionBase
142  {
143  public:
144  CRedirect(const std::string& Location)
145  : CExceptionBase(Location,"Redirect"),
146  m_Location(Location)
147  {
148  }
149 
150  virtual ~CRedirect() throw() {}
151 
152  const char *Location() const throw()
153  {
154  return m_Location.c_str();
155  }
156 
157  private:
158  std::string m_Location;
159  };
160 
168  {
169  public:
179  CHTTPFetch(const std::string& UserAgent);
180  ~CHTTPFetch();
181 
190  void SetUserName(const std::string& UserName);
191 
200  void SetPassword(const std::string& Password);
201 
210  void SetProxyHost(const std::string& ProxyHost);
211 
220  void SetProxyPort(int ProxyPort);
221 
230  void SetProxyUserName(const std::string& ProxyUserName);
231 
240  void SetProxyPassword(const std::string& ProxyPassword);
241 
261  int Fetch(const std::string& URL, bool FollowRedirects=true);
262 
271  std::vector<unsigned char> Data() const;
272 
281  int Result() const;
282 
291  int Status() const;
292 
301  std::string ErrorMessage() const;
302 
303  private:
304  CHTTPFetchPrivate * const m_d;
305 
306  int DoRequest(const std::string& URL);
307 
308  static int httpAuth(void *userdata, const char *realm, int attempts, char *username, char *password);
309  static int proxyAuth(void *userdata, const char *realm, int attempts, char *username, char *password);
310  static int httpResponseReader(void *userdata, const char *buf, size_t len);
311  };
312 }
313 
314 #endif
Definition: HTTPFetch.h:36
Object for make HTTP requests.
Definition: HTTPFetch.h:167
void SetPassword(const std::string &Password)
Set the password to use.
Exception thrown when an error occurs connecting to web service.
Definition: HTTPFetch.h:63
Exception thrown when the requested resource is not found.
Definition: HTTPFetch.h:128
void SetProxyHost(const std::string &ProxyHost)
Set the proxy server to use.
std::vector< unsigned char > Data() const
Get the data receieved.
void SetProxyPassword(const std::string &ProxyPassword)
Set the proxy password to use.
Exception thrown when an invalid request is made.
Definition: HTTPFetch.h:115
CAuthenticationError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:92
void SetUserName(const std::string &UserName)
Set the user name to use.
int Result() const
libneon result code from the request
void SetProxyPort(int ProxyPort)
Set the proxy port to use.
CResourceNotFoundError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:131
int Status() const
Status.
CFetchError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:105
const char * Location() const
Definition: HTTPFetch.h:152
CRedirect(const std::string &Location)
Definition: HTTPFetch.h:144
Exception thrown when an error occurs fetching data.
Definition: HTTPFetch.h:102
Exception thrown when an authentication error occurs.
Definition: HTTPFetch.h:89
CTimeoutError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:79
Exception thrown when a connection to the web service times out.
Definition: HTTPFetch.h:76
int Fetch(const std::string &URL, bool FollowRedirects=true)
Make a request to the server.
std::string ErrorMessage() const
Return the error message from the request.
virtual const char * what() const
Definition: HTTPFetch.h:48
CRequestError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:118
virtual ~CExceptionBase()
Definition: HTTPFetch.h:46
virtual ~CRedirect()
Definition: HTTPFetch.h:150
CHTTPFetch(const std::string &UserAgent)
Constructor.
Exception thrown when a redirect is returned.
Definition: HTTPFetch.h:141
CExceptionBase(const std::string &ErrorMessage, const std::string &Exception)
Definition: HTTPFetch.h:39
CConnectionError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:66
void SetProxyUserName(const std::string &ProxyUserName)
Set the proxy user name to use.