libmusicbrainz5  5.1.0
HTTPFetch.h
Go to the documentation of this file.
1 /* --------------------------------------------------------------------------
2 
3  libmusicbrainz5 - Client library to access MusicBrainz
4 
5  Copyright (C) 2012 Andrew Hawkins
6 
7  This file is part of libmusicbrainz5.
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  libmusicbrainz5 is distributed in the hope that it will be useful,
15  but 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 _MUSICBRAINZ5_HTTP_FETCH_
27 #define _MUSICBRAINZ5_HTTP_FETCH_
28 
29 #include <string>
30 #include <vector>
31 
32 namespace MusicBrainz5
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 
144  {
145  public:
157  CHTTPFetch(const std::string& UserAgent, const std::string& Host, int Port=80);
158  ~CHTTPFetch();
159 
168  void SetUserName(const std::string& UserName);
169 
178  void SetPassword(const std::string& Password);
179 
188  void SetProxyHost(const std::string& ProxyHost);
189 
198  void SetProxyPort(int ProxyPort);
199 
208  void SetProxyUserName(const std::string& ProxyUserName);
209 
218  void SetProxyPassword(const std::string& ProxyPassword);
219 
238  int Fetch(const std::string& URL, const std::string& Request="GET");
239 
248  std::vector<unsigned char> Data() const;
249 
258  int Result() const;
259 
268  int Status() const;
269 
278  std::string ErrorMessage() const;
279 
280  private:
281  CHTTPFetchPrivate * const m_d;
282 
283  static int httpAuth(void *userdata, const char *realm, int attempts, char *username, char *password);
284  static int proxyAuth(void *userdata, const char *realm, int attempts, char *username, char *password);
285  static int httpResponseReader(void *userdata, const char *buf, size_t len);
286  };
287 }
288 
289 #endif
MusicBrainz5::CHTTPFetch::SetProxyHost
void SetProxyHost(const std::string &ProxyHost)
Set the proxy server to use.
MusicBrainz5::CHTTPFetch::SetUserName
void SetUserName(const std::string &UserName)
Set the user name to use.
MusicBrainz5::CHTTPFetch::ErrorMessage
std::string ErrorMessage() const
Return the error message from the request.
MusicBrainz5::CTimeoutError::CTimeoutError
CTimeoutError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:79
MusicBrainz5::CExceptionBase::~CExceptionBase
virtual ~CExceptionBase()
Definition: HTTPFetch.h:46
MusicBrainz5::CResourceNotFoundError
Exception thrown when the requested resource is not found.
Definition: HTTPFetch.h:128
MusicBrainz5::CHTTPFetch::SetProxyPassword
void SetProxyPassword(const std::string &ProxyPassword)
Set the proxy password to use.
MusicBrainz5::CFetchError
Exception thrown when an error occurs fetching data.
Definition: HTTPFetch.h:102
MusicBrainz5::CTimeoutError
Exception thrown when a connection to the web service times out.
Definition: HTTPFetch.h:76
MusicBrainz5::CExceptionBase::CExceptionBase
CExceptionBase(const std::string &ErrorMessage, const std::string &Exception)
Definition: HTTPFetch.h:39
MusicBrainz5::CHTTPFetch
Object for make HTTP requests.
Definition: HTTPFetch.h:143
MusicBrainz5::CHTTPFetch::Status
int Status() const
Status.
MusicBrainz5::CAuthenticationError::CAuthenticationError
CAuthenticationError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:92
MusicBrainz5::CAuthenticationError
Exception thrown when an authentication error occurs.
Definition: HTTPFetch.h:89
MusicBrainz5::CHTTPFetch::CHTTPFetch
CHTTPFetch(const std::string &UserAgent, const std::string &Host, int Port=80)
Constructor.
MusicBrainz5::CHTTPFetch::Data
std::vector< unsigned char > Data() const
Get the data receieved.
MusicBrainz5::CHTTPFetch::Fetch
int Fetch(const std::string &URL, const std::string &Request="GET")
Make a request to the server.
MusicBrainz5::CHTTPFetch::SetProxyUserName
void SetProxyUserName(const std::string &ProxyUserName)
Set the proxy user name to use.
MusicBrainz5::CExceptionBase::what
virtual const char * what() const
Definition: HTTPFetch.h:48
MusicBrainz5::CFetchError::CFetchError
CFetchError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:105
MusicBrainz5::CHTTPFetch::~CHTTPFetch
~CHTTPFetch()
MusicBrainz5::CExceptionBase
Definition: HTTPFetch.h:36
MusicBrainz5::CHTTPFetch::SetPassword
void SetPassword(const std::string &Password)
Set the password to use.
MusicBrainz5
Definition: Alias.h:36
MusicBrainz5::CConnectionError
Exception thrown when an error occurs connecting to web service.
Definition: HTTPFetch.h:63
MusicBrainz5::CHTTPFetch::Result
int Result() const
libneon result code from the request
MusicBrainz5::CHTTPFetch::SetProxyPort
void SetProxyPort(int ProxyPort)
Set the proxy port to use.
MusicBrainz5::CResourceNotFoundError::CResourceNotFoundError
CResourceNotFoundError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:131
MusicBrainz5::CRequestError
Exception thrown when an invalid request is made.
Definition: HTTPFetch.h:115
MusicBrainz5::CRequestError::CRequestError
CRequestError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:118
MusicBrainz5::CConnectionError::CConnectionError
CConnectionError(const std::string &ErrorMessage)
Definition: HTTPFetch.h:66