]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPRequest.cxx
68d69d3cc270c2f41f5f55c3cdef83045d4e7aaa
[simgear.git] / simgear / io / HTTPRequest.cxx
1 #include "HTTPRequest.hxx"
2
3 #include <simgear/misc/strutils.hxx>
4 #include <simgear/compiler.h>
5 #include <simgear/debug/logstream.hxx>
6
7 using std::string;
8 using std::map;
9
10 namespace simgear
11 {
12
13 namespace HTTP
14 {
15
16 extern const int DEFAULT_HTTP_PORT;
17
18 Request::Request(const string& url, const string method) :
19     _method(method),
20     _url(url)
21 {
22     
23 }
24
25 Request::~Request()
26 {
27     
28 }
29
30 void Request::setUrl(const string& url)
31 {
32     _url = url;
33 }
34
35 string_list Request::requestHeaders() const
36 {
37     string_list r;
38     return r;
39 }
40
41 string Request::header(const std::string& name) const
42 {
43     return string();
44 }
45
46 void Request::responseStart(const string& r)
47 {
48     const int maxSplit = 2; // HTTP/1.1 nnn reason-string
49     string_list parts = strutils::split(r, NULL, maxSplit);
50     if (parts.size() != 3) {
51         SG_LOG(SG_IO, SG_WARN, "HTTP::Request: malformed response start:" << r);
52         _responseStatus = 400;
53         _responseReason = "bad HTTP response header";
54         return;
55     }
56     
57     _responseStatus = strutils::to_int(parts[1]);
58     _responseReason = parts[2];
59 }
60
61 void Request::responseHeader(const string& key, const string& value)
62 {
63     _responseHeaders[key] = value;
64 }
65
66 void Request::responseHeadersComplete()
67 {
68     // no op
69 }
70
71 void Request::gotBodyData(const char* s, int n)
72 {
73
74 }
75
76 void Request::responseComplete()
77 {
78     
79 }
80     
81 string Request::scheme() const
82 {
83     int firstColon = url().find(":");
84     if (firstColon > 0) {
85         return url().substr(0, firstColon);
86     }
87     
88     return ""; // couldn't parse scheme
89 }
90     
91 string Request::path() const
92 {
93     string u(url());
94     int schemeEnd = u.find("://");
95     if (schemeEnd < 0) {
96         return ""; // couldn't parse scheme
97     }
98     
99     int hostEnd = u.find('/', schemeEnd + 3);
100     if (hostEnd < 0) { 
101         return ""; // couldn't parse host
102     }
103     
104     int query = u.find('?', hostEnd + 1);
105     if (query < 0) {
106         // all remainder of URL is path
107         return u.substr(hostEnd);
108     }
109     
110     return u.substr(hostEnd, query - hostEnd);
111 }
112
113 string Request::host() const
114 {
115     string hp(hostAndPort());
116     int colonPos = hp.find(':');
117     if (colonPos >= 0) {
118         return hp.substr(0, colonPos); // trim off the colon and port
119     } else {
120         return hp; // no port specifier
121     }
122 }
123
124 unsigned short Request::port() const
125 {
126     string hp(hostAndPort());
127     int colonPos = hp.find(':');
128     if (colonPos >= 0) {
129         return (unsigned short) strutils::to_int(hp.substr(colonPos + 1));
130     } else {
131         return DEFAULT_HTTP_PORT;
132     }
133 }
134
135 string Request::hostAndPort() const
136 {
137     string u(url());
138     int schemeEnd = u.find("://");
139     if (schemeEnd < 0) {
140         return ""; // couldn't parse scheme
141     }
142     
143     int hostEnd = u.find('/', schemeEnd + 3);
144     if (hostEnd < 0) { // all remainder of URL is host
145         return u.substr(schemeEnd + 3);
146     }
147     
148     return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
149 }
150
151 unsigned int Request::contentLength() const
152 {
153     HeaderDict::const_iterator it = _responseHeaders.find("content-length");
154     if (it == _responseHeaders.end()) {
155         return 0;
156     }
157     
158     return (unsigned int) strutils::to_int(it->second);
159 }
160
161 } // of namespace HTTP
162
163 } // of namespace simgear