VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
ghttpclientprotocol.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file ghttpclientprotocol.h
19 ///
20 
21 #ifndef G_HTTP_CLIENT_PROTOCOL__H
22 #define G_HTTP_CLIENT_PROTOCOL__H
23 
24 #include "gdef.h"
25 #include "gnet.h"
26 #include "ghttpclientparser.h"
27 #include "gexception.h"
28 #include "gurl.h"
29 #include "gstrings.h"
30 #include <string>
31 
32 namespace GNet
33 {
34  class HttpClientProtocol ;
35 }
36 
37 /// \class GNet::HttpClientProtocol
38 /// A protocol driver for an http client. Supports "multipart" responses,
39 /// with each part is delivered separately as if a self-contained body. Also
40 /// supports digest authentication, with the authentication secrets being
41 /// supplied in the URL.
42 ///
44 {
45 public:
46  G_EXCEPTION( Auth , "http client protocol" ) ;
47  G_EXCEPTION( Fail , "http client protocol error" ) ;
48  G_EXCEPTION( Retry , "http client protocol" ) ;
49 
50  struct Callback /// A callback interface for GNet::HttpClientProtocol to send data and deliver content.
51  {
52  virtual void sendHttpRequest( const std::string & ) = 0 ;
53  ///< Called by the HttpClientProtocol when it needs to send
54  ///< a http request string.
55 
56  virtual void onHttpBody( const std::string & outer_content_type , const std::string & content_type ,
57  const char * body , size_t body_size ) = 0 ;
58  ///< Called on receipt of a complete response, or a multipart part.
59  ///< The first parameter is the empty string for a single-part
60  ///< response, and the outer content-type for a multipart part.
61 
62  virtual ~Callback() ;
63  ///< Destructor.
64  } ;
65 
66  HttpClientProtocol( Callback & , const G::Url & url ) ;
67  ///< Constructor.
68 
69  void start() ;
70  ///< Assembles a GET request (based on the constructor url) and asks the
71  ///< callback interface to send it.
72 
73  void apply( const char * , size_t ) ;
74  ///< To be called on receipt of data. Throws Auth if authentication is required
75  ///< but not supplied; throws Retry for a service-unavailble response; throws
76  ///< Fail on error.
77 
78  static std::string authorisation( const GNet::HttpClientParser & , const G::Url & ,
79  std::string get = std::string() , bool = false ) ;
80  ///< Returns an "Authorization" header for adding to a "GET" request.
81 
82 private:
84  void operator=( const HttpClientProtocol & ) ;
85  void processData() ;
86  void processBody() ;
87  void processPart() ;
88  static std::string hash( const std::string & ) ;
89  void sendGet() ;
90  void sendGetWithAuth() ;
91 
92 private:
93  Callback & m_callback ;
94  G::Url m_url ;
95  HttpClientParser m_parser ;
96  bool m_tried_auth ;
97 } ;
98 
99 #endif
void apply(const char *, size_t)
To be called on receipt of data.
HttpClientProtocol(Callback &, const G::Url &url)
Constructor.
A simple parser for URLs.
Definition: gurl.h:42
A callback interface for GNet::HttpClientProtocol to send data and deliver content.
A protocol driver for an http client.
A parser for HTTP responses received from a remote server.
void start()
Assembles a GET request (based on the constructor url) and asks the callback interface to send it...
virtual void onHttpBody(const std::string &outer_content_type, const std::string &content_type, const char *body, size_t body_size)=0
Called on receipt of a complete response, or a multipart part.
static std::string authorisation(const GNet::HttpClientParser &, const G::Url &, std::string get=std::string(), bool=false)
Returns an "Authorization" header for adding to a "GET" request.
virtual void sendHttpRequest(const std::string &)=0
Called by the HttpClientProtocol when it needs to send a http request string.