]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPRequest.hxx
hla: Use HLADataElementIndices for HLAInteractionClass.
[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      * Query the size of the request body. -1 (the default value) means no
49      * request body
50      */
51     virtual int requestBodyLength() const;
52     
53     /**
54      * Retrieve the body data bytes. Will be passed the maximum body bytes
55      * to return in the buffer, and should update count with the actual number
56      * of bytes written. 
57      */
58     virtual void getBodyData(char* s, int& count) const;
59   
60     /**
61      * retrieve the request body content type. Default is text/plain
62      */
63     virtual std::string requestBodyType() const;
64   
65     /**
66      * running total of body bytes received so far. Can be used
67      * to generate a completion percentage, if the response length is
68      * known. 
69      */
70     unsigned int responseBytesReceived() const
71         { return _receivedBodyBytes; }
72         
73     enum HTTPVersion {
74         HTTP_VERSION_UNKNOWN = 0,
75         HTTP_0_x, // 0.9 or similar
76         HTTP_1_0,
77         HTTP_1_1
78     };
79     
80     HTTPVersion responseVersion() const
81         { return _responseVersion; }
82     
83     static HTTPVersion decodeVersion(const std::string& v);
84     
85     bool closeAfterComplete() const;
86 protected:
87     Request(const std::string& url, const std::string method = "GET");
88
89     virtual void responseStart(const std::string& r);
90     virtual void responseHeader(const std::string& key, const std::string& value);
91     virtual void responseHeadersComplete();
92     virtual void responseComplete();
93     virtual void failed();
94     virtual void gotBodyData(const char* s, int n);
95 private:
96     friend class Client;
97     friend class Connection;
98     
99     void processBodyBytes(const char* s, int n);
100     void setFailure(int code, const std::string& reason);
101
102     std::string _method;
103     std::string _url;
104     HTTPVersion _responseVersion;
105     int _responseStatus;
106     std::string _responseReason;
107     unsigned int _responseLength;
108     unsigned int _receivedBodyBytes;
109     bool _willClose;
110     
111     typedef std::map<std::string, std::string> HeaderDict;
112     HeaderDict _responseHeaders; 
113 };
114
115 typedef SGSharedPtr<Request> Request_ptr;
116
117 } // of namespace HTTP
118
119 } // of namespace simgear
120
121 #endif // of SG_HTTP_REQUEST_HXX
122