]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPRequest.cxx
Make it compile with gcc-4.6
[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 #include <iostream>
11
12 using std::cout;
13 using std::cerr;
14 using std::endl;
15
16 namespace simgear
17 {
18
19 namespace HTTP
20 {
21
22 Request::Request(const string& url, const string method) :
23     _method(method),
24     _url(url)
25 {
26     
27 }
28
29 Request::~Request()
30 {
31     
32 }
33
34 string_list Request::requestHeaders() const
35 {
36     string_list r;
37     return r;
38 }
39
40 string Request::header(const std::string& name) const
41 {
42     return string();
43 }
44
45 void Request::responseStart(const string& r)
46 {
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];
51 }
52
53 void Request::responseHeader(const string& key, const string& value)
54 {
55     _responseHeaders[key] = value;
56 }
57
58 void Request::responseHeadersComplete()
59 {
60     // no op
61 }
62
63 void Request::gotBodyData(const char* s, int n)
64 {
65
66 }
67
68 void Request::responseComplete()
69 {
70     
71 }
72     
73 string Request::scheme() const
74 {
75     int firstColon = url().find(":");
76     if (firstColon > 0) {
77         return url().substr(0, firstColon);
78     }
79     
80     return ""; // couldn't parse scheme
81 }
82     
83 string Request::path() const
84 {
85     string u(url());
86     int schemeEnd = u.find("://");
87     if (schemeEnd < 0) {
88         return ""; // couldn't parse scheme
89     }
90     
91     int hostEnd = u.find('/', schemeEnd + 3);
92     if (hostEnd < 0) { 
93         return ""; // couldn't parse host
94     }
95     
96     int query = u.find('?', hostEnd + 1);
97     if (query < 0) {
98         // all remainder of URL is path
99         return u.substr(hostEnd);
100     }
101     
102     return u.substr(hostEnd, query - hostEnd);
103 }
104
105 string Request::host() const
106 {
107     string u(url());
108     int schemeEnd = u.find("://");
109     if (schemeEnd < 0) {
110         return ""; // couldn't parse scheme
111     }
112     
113     int hostEnd = u.find('/', schemeEnd + 3);
114     if (hostEnd < 0) { // all remainder of URL is host
115         return u.substr(schemeEnd + 3);
116     }
117     
118     return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
119 }
120
121 int Request::contentLength() const
122 {
123     HeaderDict::const_iterator it = _responseHeaders.find("content-length");
124     if (it == _responseHeaders.end()) {
125         return 0;
126     }
127     
128     return strutils::to_int(it->second);
129 }
130
131 } // of namespace HTTP
132
133 } // of namespace simgear