libmusicbrainz5  5.0.1
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
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 v2 of the GNU Lesser General Public
11  License as published by the Free Software Foundation.
12 
13  libmusicbrainz5 is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this library. If not, see <http://www.gnu.org/licenses/>.
20 
21  $Id$
22 
23 ----------------------------------------------------------------------------*/
24 
25 #ifndef _MUSICBRAINZ5_HTTP_FETCH_
26 #define _MUSICBRAINZ5_HTTP_FETCH_
27 
28 #include <string>
29 #include <vector>
30 
31 namespace MusicBrainz5
32 {
33  class CHTTPFetchPrivate;
34 
35  class CExceptionBase: public std::exception
36  {
37  public:
38  CExceptionBase(const std::string& ErrorMessage, const std::string& Exception)
39  : m_ErrorMessage(ErrorMessage),
40  m_Exception(Exception)
41  {
42  m_FullMessage=m_Exception + ": " + m_ErrorMessage;
43  }
44 
45  virtual ~CExceptionBase() throw() {};
46 
47  virtual const char* what() const throw()
48  {
49  return m_FullMessage.c_str();
50  }
51 
52  private:
53  std::string m_ErrorMessage;
54  std::string m_Exception;
55  std::string m_FullMessage;
56  };
57 
63  {
64  public:
65  CConnectionError(const std::string& ErrorMessage)
66  : CExceptionBase(ErrorMessage,"Connection error")
67  {
68  }
69  };
70 
76  {
77  public:
78  CTimeoutError(const std::string& ErrorMessage)
79  : CExceptionBase(ErrorMessage,"Timeout error")
80  {
81  }
82  };
83 
89  {
90  public:
91  CAuthenticationError(const std::string& ErrorMessage)
92  : CExceptionBase(ErrorMessage,"Authentication error")
93  {
94  }
95  };
96 
102  {
103  public:
104  CFetchError(const std::string& ErrorMessage)
105  : CExceptionBase(ErrorMessage,"Fetch error")
106  {
107  }
108  };
109 
115  {
116  public:
117  CRequestError(const std::string& ErrorMessage)
118  : CExceptionBase(ErrorMessage,"Request error")
119  {
120  }
121  };
122 
128  {
129  public:
130  CResourceNotFoundError(const std::string& ErrorMessage)
131  : CExceptionBase(ErrorMessage,"Resource not found error")
132  {
133  }
134  };
135 
143  {
144  public:
156  CHTTPFetch(const std::string& UserAgent, const std::string& Host, int Port=80);
157  ~CHTTPFetch();
158 
167  void SetUserName(const std::string& UserName);
168 
177  void SetPassword(const std::string& Password);
178 
187  void SetProxyHost(const std::string& ProxyHost);
188 
197  void SetProxyPort(int ProxyPort);
198 
207  void SetProxyUserName(const std::string& ProxyUserName);
208 
217  void SetProxyPassword(const std::string& ProxyPassword);
218 
237  int Fetch(const std::string& URL, const std::string& Request="GET");
238 
247  std::vector<unsigned char> Data() const;
248 
257  int Result() const;
258 
267  int Status() const;
268 
277  std::string ErrorMessage() const;
278 
279  private:
280  CHTTPFetchPrivate * const m_d;
281 
282  static int httpAuth(void *userdata, const char *realm, int attempts, char *username, char *password);
283  static int proxyAuth(void *userdata, const char *realm, int attempts, char *username, char *password);
284  static int httpResponseReader(void *userdata, const char *buf, size_t len);
285  };
286 }
287 
288 #endif