]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPRequest.hxx
Merge commit 'refs/merge-requests/22' of git://gitorious.org/fg/simgear into merge...
[simgear.git] / simgear / io / HTTPRequest.hxx
1 #ifndef SG_HTTP_REQUEST_HXX
2 #define SG_HTTP_REQUEST_HXX
3
4 #include <map>
5
6 #include <simgear/structure/SGReferenced.hxx>
7 #include <simgear/structure/SGSharedPtr.hxx>
8 #include <simgear/math/sg_types.hxx>
9
10 namespace simgear
11 {
12
13 namespace HTTP
14 {
15
16 class Request : public SGReferenced
17 {
18 public:
19     virtual ~Request();
20     
21     virtual void setUrl(const std::string& url);
22     
23     virtual std::string method() const
24         { return _method; }
25     virtual std::string url() const
26         { return _url; }
27     
28     virtual std::string scheme() const;
29     virtual std::string path() const;
30     virtual std::string host() const;
31     virtual std::string hostAndPort() const;
32     virtual unsigned short port() const;
33     virtual std::string query() const;
34     
35     virtual string_list requestHeaders() const;
36     virtual std::string header(const std::string& name) const;
37
38     virtual int responseCode() const
39         { return _responseStatus; }
40         
41     virtual std::string responseReason() const
42         { return _responseReason; }
43         
44     void setResponseLength(unsigned int l);    
45     virtual unsigned int responseLength() const;
46     
47     /**
48      * running total of body bytes received so far. Can be used
49      * to generate a completion percentage, if the response length is
50      * known. 
51      */
52     unsigned int responseBytesReceived() const
53         { return _receivedBodyBytes; }
54         
55     enum HTTPVersion {
56         HTTP_VERSION_UNKNOWN = 0,
57         HTTP_0_x, // 0.9 or similar
58         HTTP_1_0,
59         HTTP_1_1
60     };
61     
62     HTTPVersion responseVersion() const
63         { return _responseVersion; }
64     
65     static HTTPVersion decodeVersion(const std::string& v);
66     
67     bool closeAfterComplete() const;
68 protected:
69     Request(const std::string& url, const std::string method = "GET");
70
71     virtual void responseStart(const std::string& r);
72     virtual void responseHeader(const std::string& key, const std::string& value);
73     virtual void responseHeadersComplete();
74     virtual void responseComplete();
75     virtual void failed();
76     virtual void gotBodyData(const char* s, int n);
77 private:
78     friend class Client;
79     friend class Connection;
80     
81     void processBodyBytes(const char* s, int n);
82     void setFailure(int code, const std::string& reason);
83
84     std::string _method;
85     std::string _url;
86     HTTPVersion _responseVersion;
87     int _responseStatus;
88     std::string _responseReason;
89     unsigned int _responseLength;
90     unsigned int _receivedBodyBytes;
91     bool _willClose;
92     
93     typedef std::map<std::string, std::string> HeaderDict;
94     HeaderDict _responseHeaders; 
95 };
96
97 typedef SGSharedPtr<Request> Request_ptr;
98
99 } // of namespace HTTP
100
101 } // of namespace simgear
102
103 #endif // of SG_HTTP_REQUEST_HXX
104