]> git.saurik.com Git - apt.git/blob - test/interactive-helper/aptwebserver.cc
98bbde21b84a78fe1674d0b78ab3b63965f42ebb
[apt.git] / test / interactive-helper / aptwebserver.cc
1 #include <config.h>
2
3 #include <apt-pkg/strutl.h>
4 #include <apt-pkg/fileutl.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/cmndline.h>
7 #include <apt-pkg/configuration.h>
8 #include <apt-pkg/init.h>
9
10 #include <vector>
11 #include <string>
12 #include <list>
13 #include <sstream>
14
15 #include <sys/socket.h>
16 #include <sys/stat.h>
17 #include <netinet/in.h>
18 #include <unistd.h>
19 #include <errno.h>
20 #include <time.h>
21 #include <stdlib.h>
22 #include <signal.h>
23
24 char const * const httpcodeToStr(int const httpcode) /*{{{*/
25 {
26 switch (httpcode)
27 {
28 // Informational 1xx
29 case 100: return "100 Continue";
30 case 101: return "101 Switching Protocols";
31 // Successful 2xx
32 case 200: return "200 OK";
33 case 201: return "201 Created";
34 case 202: return "202 Accepted";
35 case 203: return "203 Non-Authoritative Information";
36 case 204: return "204 No Content";
37 case 205: return "205 Reset Content";
38 case 206: return "206 Partial Content";
39 // Redirections 3xx
40 case 300: return "300 Multiple Choices";
41 case 301: return "301 Moved Permanently";
42 case 302: return "302 Found";
43 case 303: return "303 See Other";
44 case 304: return "304 Not Modified";
45 case 305: return "304 Use Proxy";
46 case 307: return "307 Temporary Redirect";
47 // Client errors 4xx
48 case 400: return "400 Bad Request";
49 case 401: return "401 Unauthorized";
50 case 402: return "402 Payment Required";
51 case 403: return "403 Forbidden";
52 case 404: return "404 Not Found";
53 case 405: return "405 Method Not Allowed";
54 case 406: return "406 Not Acceptable";
55 case 407: return "407 Proxy Authentication Required";
56 case 408: return "408 Request Time-out";
57 case 409: return "409 Conflict";
58 case 410: return "410 Gone";
59 case 411: return "411 Length Required";
60 case 412: return "412 Precondition Failed";
61 case 413: return "413 Request Entity Too Large";
62 case 414: return "414 Request-URI Too Large";
63 case 415: return "415 Unsupported Media Type";
64 case 416: return "416 Requested range not satisfiable";
65 case 417: return "417 Expectation Failed";
66 case 418: return "418 I'm a teapot";
67 // Server error 5xx
68 case 500: return "500 Internal Server Error";
69 case 501: return "501 Not Implemented";
70 case 502: return "502 Bad Gateway";
71 case 503: return "503 Service Unavailable";
72 case 504: return "504 Gateway Time-out";
73 case 505: return "505 HTTP Version not supported";
74 }
75 return NULL;
76 }
77 /*}}}*/
78 void addFileHeaders(std::list<std::string> &headers, FileFd &data) /*{{{*/
79 {
80 std::ostringstream contentlength;
81 contentlength << "Content-Length: " << data.FileSize();
82 headers.push_back(contentlength.str());
83
84 std::string lastmodified("Last-Modified: ");
85 lastmodified.append(TimeRFC1123(data.ModificationTime()));
86 headers.push_back(lastmodified);
87 }
88 /*}}}*/
89 void addDataHeaders(std::list<std::string> &headers, std::string &data) /*{{{*/
90 {
91 std::ostringstream contentlength;
92 contentlength << "Content-Length: " << data.size();
93 headers.push_back(contentlength.str());
94 }
95 /*}}}*/
96 bool sendHead(int const client, int const httpcode, std::list<std::string> &headers)/*{{{*/
97 {
98 std::string response("HTTP/1.1 ");
99 response.append(httpcodeToStr(httpcode));
100 headers.push_front(response);
101
102 headers.push_back("Server: APT webserver");
103
104 std::string date("Date: ");
105 date.append(TimeRFC1123(time(NULL)));
106 headers.push_back(date);
107
108 std::clog << ">>> RESPONSE >>>" << std::endl;
109 bool Success = true;
110 for (std::list<std::string>::const_iterator h = headers.begin();
111 Success == true && h != headers.end(); ++h)
112 {
113 Success &= FileFd::Write(client, h->c_str(), h->size());
114 if (Success == true)
115 Success &= FileFd::Write(client, "\r\n", 2);
116 std::clog << *h << std::endl;
117 }
118 if (Success == true)
119 Success &= FileFd::Write(client, "\r\n", 2);
120 std::clog << "<<<<<<<<<<<<<<<<" << std::endl;
121 return Success;
122 }
123 /*}}}*/
124 bool sendFile(int const client, FileFd &data) /*{{{*/
125 {
126 bool Success = true;
127 char buffer[500];
128 unsigned long long actual = 0;
129 while ((Success &= data.Read(buffer, sizeof(buffer), &actual)) == true)
130 {
131 if (actual == 0)
132 break;
133 if (Success == true)
134 Success &= FileFd::Write(client, buffer, actual);
135 }
136 if (Success == true)
137 Success &= FileFd::Write(client, "\r\n", 2);
138 return Success;
139 }
140 /*}}}*/
141 bool sendData(int const client, std::string const &data) /*{{{*/
142 {
143 bool Success = true;
144 Success &= FileFd::Write(client, data.c_str(), data.size());
145 if (Success == true)
146 Success &= FileFd::Write(client, "\r\n", 2);
147 return Success;
148 }
149 /*}}}*/
150 void sendError(int const client, int const httpcode, std::string const &request,/*{{{*/
151 bool content, std::string const &error = "")
152 {
153 std::list<std::string> headers;
154 std::string response("<html><head><title>");
155 response.append(httpcodeToStr(httpcode)).append("</title></head>");
156 response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1>");
157 if (error.empty() == false)
158 response.append("<p><em>Error</em>: ").append(error).append("</p>");
159 response.append("This error is a result of the request: <pre>");
160 response.append(request).append("</pre></body></html>");
161 addDataHeaders(headers, response);
162 sendHead(client, httpcode, headers);
163 if (content == true)
164 sendData(client, response);
165 }
166 /*}}}*/
167 bool parseFirstLine(int const client, std::string const &request, /*{{{*/
168 std::string &filename, bool &sendContent,
169 bool &closeConnection)
170 {
171 if (strncmp(request.c_str(), "HEAD ", 5) == 0)
172 sendContent = false;
173 if (strncmp(request.c_str(), "GET ", 4) != 0)
174 {
175 sendError(client, 501, request, true);
176 return false;
177 }
178
179 size_t const lineend = request.find('\n');
180 size_t filestart = request.find(' ');
181 for (; request[filestart] == ' '; ++filestart);
182 size_t fileend = request.rfind(' ', lineend);
183 if (lineend == std::string::npos || filestart == std::string::npos ||
184 fileend == std::string::npos || filestart == fileend)
185 {
186 sendError(client, 500, request, sendContent, "Filename can't be extracted");
187 return false;
188 }
189
190 size_t httpstart = fileend;
191 for (; request[httpstart] == ' '; ++httpstart);
192 if (strncmp(request.c_str() + httpstart, "HTTP/1.1\r", 9) == 0)
193 closeConnection = strcasecmp(LookupTag(request, "Connection", "Keep-Alive").c_str(), "Keep-Alive") != 0;
194 else if (strncmp(request.c_str() + httpstart, "HTTP/1.0\r", 9) == 0)
195 closeConnection = strcasecmp(LookupTag(request, "Connection", "Keep-Alive").c_str(), "close") == 0;
196 else
197 {
198 sendError(client, 500, request, sendContent, "Not a HTTP/1.{0,1} request");
199 return false;
200 }
201
202 filename = request.substr(filestart, fileend - filestart);
203 if (filename.find(' ') != std::string::npos)
204 {
205 sendError(client, 500, request, sendContent, "Filename contains an unencoded space");
206 return false;
207 }
208 filename = DeQuoteString(filename);
209
210 // this is not a secure server, but at least prevent the obvious …
211 if (filename.empty() == true || filename[0] != '/' ||
212 strncmp(filename.c_str(), "//", 2) == 0 ||
213 filename.find_first_of("\r\n\t\f\v") != std::string::npos ||
214 filename.find("/../") != std::string::npos)
215 {
216 sendError(client, 400, request, sendContent, "Filename contains illegal character (sequence)");
217 return false;
218 }
219
220 // nuke the first character which is a / as we assured above
221 filename.erase(0, 1);
222 if (filename.empty() == true)
223 filename = ".";
224 return true;
225 }
226 /*}}}*/
227 int main(int const argc, const char * argv[])
228 {
229 CommandLine::Args Args[] = {
230 {0, "port", "aptwebserver::port", CommandLine::HasArg},
231 {'c',"config-file",0,CommandLine::ConfigFile},
232 {'o',"option",0,CommandLine::ArbItem},
233 {0,0,0,0}
234 };
235
236 CommandLine CmdL(Args, _config);
237 if(CmdL.Parse(argc,argv) == false)
238 {
239 _error->DumpErrors();
240 exit(1);
241 }
242
243 // create socket, bind and listen to it {{{
244 // ignore SIGPIPE, this can happen on write() if the socket closes connection
245 signal(SIGPIPE, SIG_IGN);
246 int sock = socket(AF_INET6, SOCK_STREAM, 0);
247 if(sock < 0)
248 {
249 _error->Errno("aptwerbserver", "Couldn't create socket");
250 _error->DumpErrors(std::cerr);
251 return 1;
252 }
253
254 int const port = _config->FindI("aptwebserver::port", 8080);
255
256 // ensure that we accept all connections: v4 or v6
257 int const iponly = 0;
258 setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &iponly, sizeof(iponly));
259 // to not linger on an address
260 int const enable = 1;
261 setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(enable));
262
263 struct sockaddr_in6 locAddr;
264 memset(&locAddr, 0, sizeof(locAddr));
265 locAddr.sin6_family = AF_INET6;
266 locAddr.sin6_port = htons(port);
267 locAddr.sin6_addr = in6addr_any;
268
269 if (bind(sock, (struct sockaddr*) &locAddr, sizeof(locAddr)) < 0)
270 {
271 _error->Errno("aptwerbserver", "Couldn't bind");
272 _error->DumpErrors(std::cerr);
273 return 2;
274 }
275
276 std::clog << "Serving ANY file on port: " << port << std::endl;
277
278 listen(sock, 1);
279 /*}}}*/
280
281 std::vector<std::string> messages;
282 int client;
283 while ((client = accept(sock, NULL, NULL)) != -1)
284 {
285 std::clog << "ACCEPT client " << client
286 << " on socket " << sock << std::endl;
287
288 while (ReadMessages(client, messages))
289 {
290 bool closeConnection = false;
291 for (std::vector<std::string>::const_iterator m = messages.begin();
292 m != messages.end() && closeConnection == false; ++m) {
293 std::clog << ">>> REQUEST >>>>" << std::endl << *m
294 << std::endl << "<<<<<<<<<<<<<<<<" << std::endl;
295 std::list<std::string> headers;
296 std::string filename;
297 bool sendContent = true;
298 if (parseFirstLine(client, *m, filename, sendContent, closeConnection) == false)
299 continue;
300
301 std::string host = LookupTag(*m, "Host", "");
302 if (host.empty() == true)
303 {
304 // RFC 2616 §14.23 requires Host
305 sendError(client, 400, *m, sendContent, "Host header is required");
306 continue;
307 }
308
309 if (RealFileExists(filename) == true)
310 {
311 FileFd data(filename, FileFd::ReadOnly);
312 std::string condition = LookupTag(*m, "If-Modified-Since", "");
313 if (condition.empty() == false)
314 {
315 time_t cache;
316 if (RFC1123StrToTime(condition.c_str(), cache) == true &&
317 cache >= data.ModificationTime())
318 {
319 sendHead(client, 304, headers);
320 continue;
321 }
322 }
323
324 addFileHeaders(headers, data);
325 sendHead(client, 200, headers);
326 if (sendContent == true)
327 sendFile(client, data);
328 }
329 else
330 sendError(client, 404, *m, sendContent);
331 }
332 _error->DumpErrors(std::cerr);
333 messages.clear();
334 if (closeConnection == true)
335 break;
336 }
337
338 std::clog << "CLOSE client " << client
339 << " on socket " << sock << std::endl;
340 close(client);
341 }
342 return 0;
343 }