]> git.mxchange.org Git - simgear.git/blob - simgear/io/HTTPRequest.cxx
Tweaks to HTTP code, in preparation for using it for metar - especially, test code...
[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 extern const int DEFAULT_HTTP_PORT;
23
24 Request::Request(const string& url, const string method) :
25     _method(method),
26     _url(url)
27 {
28     
29 }
30
31 Request::~Request()
32 {
33     
34 }
35
36 void Request::setUrl(const string& url)
37 {
38     _url = url;
39 }
40
41 string_list Request::requestHeaders() const
42 {
43     string_list r;
44     return r;
45 }
46
47 string Request::header(const std::string& name) const
48 {
49     return string();
50 }
51
52 void Request::responseStart(const string& r)
53 {
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];
58 }
59
60 void Request::responseHeader(const string& key, const string& value)
61 {
62     _responseHeaders[key] = value;
63 }
64
65 void Request::responseHeadersComplete()
66 {
67     // no op
68 }
69
70 void Request::gotBodyData(const char* s, int n)
71 {
72
73 }
74
75 void Request::responseComplete()
76 {
77     
78 }
79     
80 string Request::scheme() const
81 {
82     int firstColon = url().find(":");
83     if (firstColon > 0) {
84         return url().substr(0, firstColon);
85     }
86     
87     return ""; // couldn't parse scheme
88 }
89     
90 string Request::path() const
91 {
92     string u(url());
93     int schemeEnd = u.find("://");
94     if (schemeEnd < 0) {
95         return ""; // couldn't parse scheme
96     }
97     
98     int hostEnd = u.find('/', schemeEnd + 3);
99     if (hostEnd < 0) { 
100         return ""; // couldn't parse host
101     }
102     
103     int query = u.find('?', hostEnd + 1);
104     if (query < 0) {
105         // all remainder of URL is path
106         return u.substr(hostEnd);
107     }
108     
109     return u.substr(hostEnd, query - hostEnd);
110 }
111
112 string Request::host() const
113 {
114     string hp(hostAndPort());
115     int colonPos = hp.find(':');
116     if (colonPos >= 0) {
117         return hp.substr(0, colonPos); // trim off the colon and port
118     } else {
119         return hp; // no port specifier
120     }
121 }
122
123 unsigned short Request::port() const
124 {
125     string hp(hostAndPort());
126     int colonPos = hp.find(':');
127     if (colonPos >= 0) {
128         return (unsigned short) strutils::to_int(hp.substr(colonPos + 1));
129     } else {
130         return DEFAULT_HTTP_PORT;
131     }
132 }
133
134 string Request::hostAndPort() const
135 {
136     string u(url());
137     int schemeEnd = u.find("://");
138     if (schemeEnd < 0) {
139         return ""; // couldn't parse scheme
140     }
141     
142     int hostEnd = u.find('/', schemeEnd + 3);
143     if (hostEnd < 0) { // all remainder of URL is host
144         return u.substr(schemeEnd + 3);
145     }
146     
147     return u.substr(schemeEnd + 3, hostEnd - (schemeEnd + 3));
148 }
149
150 unsigned int Request::contentLength() const
151 {
152     HeaderDict::const_iterator it = _responseHeaders.find("content-length");
153     if (it == _responseHeaders.end()) {
154         return 0;
155     }
156     
157     return (unsigned int) strutils::to_int(it->second);
158 }
159
160 } // of namespace HTTP
161
162 } // of namespace simgear