1 #include "HTTPRequest.hxx"
3 #include <simgear/misc/strutils.hxx>
4 #include <simgear/compiler.h>
5 #include <simgear/debug/logstream.hxx>
22 Request::Request(const string& url, const string method) :
34 string_list Request::requestHeaders() const
40 string Request::header(const std::string& name) const
45 void Request::responseStart(const string& r)
47 const int maxSplit = 2; // HTTP/1.1 nnn status code
48 string_list parts = strutils::split(r, NULL, maxSplit);
49 _responseStatus = strutils::to_int(parts[1]);
50 _responseReason = parts[2];
53 void Request::responseHeader(const string& key, const string& value)
55 _responseHeaders[key] = value;
58 void Request::responseHeadersComplete()
63 void Request::gotBodyData(const char* s, int n)
68 void Request::responseComplete()
73 string Request::scheme() const
75 int firstColon = url().find(":");
77 return url().substr(0, firstColon);
80 return ""; // couldn't parse scheme
83 string Request::path() const
86 int schemeEnd = u.find("://");
88 return ""; // couldn't parse scheme
91 int hostEnd = u.find('/', schemeEnd + 3);
93 return ""; // couldn't parse host
96 int query = u.find('?', hostEnd + 1);
98 // all remainder of URL is path
99 return u.substr(hostEnd);
102 return u.substr(hostEnd, query - hostEnd);
105 string Request::host() const
108 int schemeEnd = u.find("://");
110 return ""; // couldn't parse scheme
113 int hostEnd = u.find('/', schemeEnd + 3);
114 if (hostEnd < 0) { // all remainder of URL is host
115 return u.substr(schemeEnd + 3);
118 return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
121 int Request::contentLength() const
123 HeaderDict::const_iterator it = _responseHeaders.find("content-length");
124 if (it == _responseHeaders.end()) {
128 return strutils::to_int(it->second);
131 } // of namespace HTTP
133 } // of namespace simgear