]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPRequest.hxx
Compile SGReaderWriterOptions.cxx under Windows
[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     
34     virtual string_list requestHeaders() const;
35     virtual std::string header(const std::string& name) const;
36
37     virtual int responseCode() const
38         { return _responseStatus; }
39         
40     virtual std::string responseReason() const
41         { return _responseReason; }
42         
43     void setResponseLength(unsigned int l);    
44     virtual unsigned int responseLength() const;
45     
46     /**
47      * running total of body bytes received so far. Can be used
48      * to generate a completion percentage, if the response length is
49      * known. 
50      */
51     unsigned int responseBytesReceived() const
52         { return _receivedBodyBytes; }
53         
54     enum HTTPVersion {
55         HTTP_VERSION_UNKNOWN = 0,
56         HTTP_0_x, // 0.9 or similar
57         HTTP_1_0,
58         HTTP_1_1
59     };
60     
61     HTTPVersion responseVersion() const
62         { return _responseVersion; }
63     
64     static HTTPVersion decodeVersion(const std::string& v);
65     
66     bool closeAfterComplete() const;
67 protected:
68     Request(const std::string& url, const std::string method = "GET");
69
70     virtual void responseStart(const std::string& r);
71     virtual void responseHeader(const std::string& key, const std::string& value);
72     virtual void responseHeadersComplete();
73     virtual void responseComplete();
74     virtual void failed();
75     virtual void gotBodyData(const char* s, int n);
76 private:
77     friend class Client;
78     friend class Connection;
79     
80     void processBodyBytes(const char* s, int n);
81     void setFailure(int code, const std::string& reason);
82
83     std::string _method;
84     std::string _url;
85     HTTPVersion _responseVersion;
86     int _responseStatus;
87     std::string _responseReason;
88     unsigned int _responseLength;
89     unsigned int _receivedBodyBytes;
90     bool _willClose;
91     
92     typedef std::map<std::string, std::string> HeaderDict;
93     HeaderDict _responseHeaders; 
94 };
95
96 typedef SGSharedPtr<Request> Request_ptr;
97
98 } // of namespace HTTP
99
100 } // of namespace simgear
101
102 #endif // of SG_HTTP_REQUEST_HXX
103