Support asynchronous name resolution in Http::Client
[libs/net.git] / source / http / client.h
1 #ifndef MSP_HTTP_CLIENT_H_
2 #define MSP_HTTP_CLIENT_H_
3
4 #include <string>
5 #include <sigc++/signal.h>
6 #include <msp/io/eventdispatcher.h>
7 #include <msp/net/resolve.h>
8 #include <msp/net/streamsocket.h>
9
10 namespace Msp {
11 namespace Http {
12
13 class client_busy: public std::logic_error
14 {
15 public:
16         client_busy(): std::logic_error(std::string()) { }
17         virtual ~client_busy() throw() { }
18 };
19
20 class Request;
21 class Response;
22
23 class Client
24 {
25 public:
26         sigc::signal<void, const Response &> signal_response_complete;
27         sigc::signal<void, const std::exception &> signal_socket_error;
28
29 private:
30         struct ResolveListener: public sigc::trackable
31         {
32                 Client &client;
33
34                 ResolveListener(Client &);
35
36                 void address_resolved(unsigned, const Net::SockAddr &);
37                 void resolve_failed(unsigned, const std::exception &);
38         };
39
40         Net::StreamSocket *sock;
41         IO::EventDispatcher *event_disp;
42         Net::Resolver *resolver;
43         ResolveListener *resolve_listener;
44         unsigned resolve_tag;
45         std::string user_agent;
46         Request *request;
47         Response *response;
48         std::string in_buf;
49
50         Client(const Client &);
51         Client &operator=(const Client &);
52 public:
53         Client();
54         ~Client();
55
56         void use_event_dispatcher(IO::EventDispatcher *);
57         void use_resolver(Net::Resolver *);
58         void set_user_agent(const std::string &);
59         void start_request(const Request &);
60         const Response *get_url(const std::string &);
61         void tick();
62         void wait_response();
63         void abort();
64         const Request *get_request() const { return request; }
65         const Response *get_response() const { return response; }
66 private:
67         void address_resolved(unsigned, const Net::SockAddr &);
68         void resolve_failed(unsigned, const std::exception &);
69         void connect_finished(const std::exception *);
70         void data_available();
71 };
72
73 } // namespace Http
74 } // namespace Msp
75
76 #endif