1 #include "HTTPRequest.hxx"
3 #include <simgear/misc/strutils.hxx>
4 #include <simgear/compiler.h>
5 #include <simgear/debug/logstream.hxx>
22 extern const int DEFAULT_HTTP_PORT;
24 Request::Request(const string& url, const string method) :
36 void Request::setUrl(const string& url)
41 string_list Request::requestHeaders() const
47 string Request::header(const std::string& name) const
52 void Request::responseStart(const string& r)
54 const int maxSplit = 2; // HTTP/1.1 nnn status code
55 string_list parts = strutils::split(r, NULL, maxSplit);
56 _responseStatus = strutils::to_int(parts[1]);
57 _responseReason = parts[2];
60 void Request::responseHeader(const string& key, const string& value)
62 _responseHeaders[key] = value;
65 void Request::responseHeadersComplete()
70 void Request::gotBodyData(const char* s, int n)
75 void Request::responseComplete()
80 string Request::scheme() const
82 int firstColon = url().find(":");
84 return url().substr(0, firstColon);
87 return ""; // couldn't parse scheme
90 string Request::path() const
93 int schemeEnd = u.find("://");
95 return ""; // couldn't parse scheme
98 int hostEnd = u.find('/', schemeEnd + 3);
100 return ""; // couldn't parse host
103 int query = u.find('?', hostEnd + 1);
105 // all remainder of URL is path
106 return u.substr(hostEnd);
109 return u.substr(hostEnd, query - hostEnd);
112 string Request::host() const
114 string hp(hostAndPort());
115 int colonPos = hp.find(':');
117 return hp.substr(0, colonPos); // trim off the colon and port
119 return hp; // no port specifier
123 unsigned short Request::port() const
125 string hp(hostAndPort());
126 int colonPos = hp.find(':');
128 return (unsigned short) strutils::to_int(hp.substr(colonPos + 1));
130 return DEFAULT_HTTP_PORT;
134 string Request::hostAndPort() const
137 int schemeEnd = u.find("://");
139 return ""; // couldn't parse scheme
142 int hostEnd = u.find('/', schemeEnd + 3);
143 if (hostEnd < 0) { // all remainder of URL is host
144 return u.substr(schemeEnd + 3);
147 return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
150 unsigned int Request::contentLength() const
152 HeaderDict::const_iterator it = _responseHeaders.find("content-length");
153 if (it == _responseHeaders.end()) {
157 return (unsigned int) strutils::to_int(it->second);
160 } // of namespace HTTP
162 } // of namespace simgear