3 #include <apt-pkg/cmndline.h>
4 #include <apt-pkg/configuration.h>
5 #include <apt-pkg/error.h>
6 #include <apt-pkg/fileutl.h>
7 #include <apt-pkg/strutl.h>
13 #include <netinet/in.h>
20 #include <sys/socket.h>
34 static std::string
httpcodeToStr(int const httpcode
) /*{{{*/
39 case 100: return _config
->Find("aptwebserver::httpcode::100", "100 Continue");
40 case 101: return _config
->Find("aptwebserver::httpcode::101", "101 Switching Protocols");
42 case 200: return _config
->Find("aptwebserver::httpcode::200", "200 OK");
43 case 201: return _config
->Find("aptwebserver::httpcode::201", "201 Created");
44 case 202: return _config
->Find("aptwebserver::httpcode::202", "202 Accepted");
45 case 203: return _config
->Find("aptwebserver::httpcode::203", "203 Non-Authoritative Information");
46 case 204: return _config
->Find("aptwebserver::httpcode::204", "204 No Content");
47 case 205: return _config
->Find("aptwebserver::httpcode::205", "205 Reset Content");
48 case 206: return _config
->Find("aptwebserver::httpcode::206", "206 Partial Content");
50 case 300: return _config
->Find("aptwebserver::httpcode::300", "300 Multiple Choices");
51 case 301: return _config
->Find("aptwebserver::httpcode::301", "301 Moved Permanently");
52 case 302: return _config
->Find("aptwebserver::httpcode::302", "302 Found");
53 case 303: return _config
->Find("aptwebserver::httpcode::303", "303 See Other");
54 case 304: return _config
->Find("aptwebserver::httpcode::304", "304 Not Modified");
55 case 305: return _config
->Find("aptwebserver::httpcode::305", "305 Use Proxy");
56 case 307: return _config
->Find("aptwebserver::httpcode::307", "307 Temporary Redirect");
58 case 400: return _config
->Find("aptwebserver::httpcode::400", "400 Bad Request");
59 case 401: return _config
->Find("aptwebserver::httpcode::401", "401 Unauthorized");
60 case 402: return _config
->Find("aptwebserver::httpcode::402", "402 Payment Required");
61 case 403: return _config
->Find("aptwebserver::httpcode::403", "403 Forbidden");
62 case 404: return _config
->Find("aptwebserver::httpcode::404", "404 Not Found");
63 case 405: return _config
->Find("aptwebserver::httpcode::405", "405 Method Not Allowed");
64 case 406: return _config
->Find("aptwebserver::httpcode::406", "406 Not Acceptable");
65 case 407: return _config
->Find("aptwebserver::httpcode::407", "407 Proxy Authentication Required");
66 case 408: return _config
->Find("aptwebserver::httpcode::408", "408 Request Time-out");
67 case 409: return _config
->Find("aptwebserver::httpcode::409", "409 Conflict");
68 case 410: return _config
->Find("aptwebserver::httpcode::410", "410 Gone");
69 case 411: return _config
->Find("aptwebserver::httpcode::411", "411 Length Required");
70 case 412: return _config
->Find("aptwebserver::httpcode::412", "412 Precondition Failed");
71 case 413: return _config
->Find("aptwebserver::httpcode::413", "413 Request Entity Too Large");
72 case 414: return _config
->Find("aptwebserver::httpcode::414", "414 Request-URI Too Large");
73 case 415: return _config
->Find("aptwebserver::httpcode::415", "415 Unsupported Media Type");
74 case 416: return _config
->Find("aptwebserver::httpcode::416", "416 Requested range not satisfiable");
75 case 417: return _config
->Find("aptwebserver::httpcode::417", "417 Expectation Failed");
76 case 418: return _config
->Find("aptwebserver::httpcode::418", "418 I'm a teapot");
78 case 500: return _config
->Find("aptwebserver::httpcode::500", "500 Internal Server Error");
79 case 501: return _config
->Find("aptwebserver::httpcode::501", "501 Not Implemented");
80 case 502: return _config
->Find("aptwebserver::httpcode::502", "502 Bad Gateway");
81 case 503: return _config
->Find("aptwebserver::httpcode::503", "503 Service Unavailable");
82 case 504: return _config
->Find("aptwebserver::httpcode::504", "504 Gateway Time-out");
83 case 505: return _config
->Find("aptwebserver::httpcode::505", "505 HTTP Version not supported");
88 static bool chunkedTransferEncoding(std::list
<std::string
> const &headers
) {
89 if (std::find(headers
.begin(), headers
.end(), "Transfer-Encoding: chunked") != headers
.end())
91 if (_config
->FindB("aptwebserver::chunked-transfer-encoding", false) == true)
95 static void addFileHeaders(std::list
<std::string
> &headers
, FileFd
&data
)/*{{{*/
97 if (chunkedTransferEncoding(headers
) == false)
99 std::ostringstream contentlength
;
100 contentlength
<< "Content-Length: " << data
.FileSize();
101 headers
.push_back(contentlength
.str());
103 if (_config
->FindB("aptwebserver::support::last-modified", true) == true)
105 std::string
lastmodified("Last-Modified: ");
106 lastmodified
.append(TimeRFC1123(data
.ModificationTime(), false));
107 headers
.push_back(lastmodified
);
111 static void addDataHeaders(std::list
<std::string
> &headers
, std::string
&data
)/*{{{*/
113 if (chunkedTransferEncoding(headers
) == false)
115 std::ostringstream contentlength
;
116 contentlength
<< "Content-Length: " << data
.size();
117 headers
.push_back(contentlength
.str());
121 static bool sendHead(std::ostream
&log
, int const client
, int const httpcode
, std::list
<std::string
> &headers
)/*{{{*/
123 std::string
response("HTTP/1.1 ");
124 response
.append(httpcodeToStr(httpcode
));
125 headers
.push_front(response
);
126 _config
->Set("APTWebserver::Last-Status-Code", httpcode
);
128 std::stringstream buffer
;
129 auto const empties
= _config
->FindVector("aptwebserver::empty-response-header");
130 for (auto && e
: empties
)
131 buffer
<< e
<< ":" << std::endl
;
132 _config
->Dump(buffer
, "aptwebserver::response-header", "%t: %v%n", false);
133 std::vector
<std::string
> addheaders
= VectorizeString(buffer
.str(), '\n');
134 for (std::vector
<std::string
>::const_iterator h
= addheaders
.begin(); h
!= addheaders
.end(); ++h
)
135 headers
.push_back(*h
);
137 std::string
date("Date: ");
138 date
.append(TimeRFC1123(time(NULL
), false));
139 headers
.push_back(date
);
141 if (chunkedTransferEncoding(headers
) == true)
142 headers
.push_back("Transfer-Encoding: chunked");
144 log
<< ">>> RESPONSE to " << client
<< " >>>" << std::endl
;
146 for (std::list
<std::string
>::const_iterator h
= headers
.begin();
147 Success
== true && h
!= headers
.end(); ++h
)
149 Success
&= FileFd::Write(client
, h
->c_str(), h
->size());
151 Success
&= FileFd::Write(client
, "\r\n", 2);
152 log
<< *h
<< std::endl
;
155 Success
&= FileFd::Write(client
, "\r\n", 2);
156 log
<< "<<<<<<<<<<<<<<<<" << std::endl
;
160 static bool sendFile(int const client
, std::list
<std::string
> const &headers
, FileFd
&data
)/*{{{*/
163 bool const chunked
= chunkedTransferEncoding(headers
);
165 unsigned long long actual
= 0;
166 while ((Success
&= data
.Read(buffer
, sizeof(buffer
), &actual
)) == true)
174 strprintf(size
, "%llX\r\n", actual
);
175 Success
&= FileFd::Write(client
, size
.c_str(), size
.size());
176 Success
&= FileFd::Write(client
, buffer
, actual
);
177 Success
&= FileFd::Write(client
, "\r\n", strlen("\r\n"));
180 Success
&= FileFd::Write(client
, buffer
, actual
);
184 char const * const finish
= "0\r\n\r\n";
185 Success
&= FileFd::Write(client
, finish
, strlen(finish
));
187 if (Success
== false)
188 std::cerr
<< "SENDFILE:" << (chunked
? " CHUNKED" : "") << " READ/WRITE ERROR to " << client
<< std::endl
;
192 static bool sendData(int const client
, std::list
<std::string
> const &headers
, std::string
const &data
)/*{{{*/
194 if (chunkedTransferEncoding(headers
) == true)
196 unsigned long long const ullsize
= data
.length();
198 strprintf(size
, "%llX\r\n", ullsize
);
199 char const * const finish
= "\r\n0\r\n\r\n";
200 if (FileFd::Write(client
, size
.c_str(), size
.length()) == false ||
201 FileFd::Write(client
, data
.c_str(), ullsize
) == false ||
202 FileFd::Write(client
, finish
, strlen(finish
)) == false)
204 std::cerr
<< "SENDDATA: CHUNK WRITE ERROR to " << client
<< std::endl
;
208 else if (FileFd::Write(client
, data
.c_str(), data
.size()) == false)
210 std::cerr
<< "SENDDATA: WRITE ERROR to " << client
<< std::endl
;
216 static void sendError(std::ostream
&log
, int const client
, int const httpcode
, std::string
const &request
,/*{{{*/
217 bool const content
, std::string
const &error
, std::list
<std::string
> &headers
)
219 std::string
response("<!doctype html><html><head><title>");
220 response
.append(httpcodeToStr(httpcode
)).append("</title><meta charset=\"utf-8\" /></head>");
221 response
.append("<body><h1>").append(httpcodeToStr(httpcode
)).append("</h1>");
223 response
.append("<p><em>Error</em>: ");
225 response
.append("<p><em>Success</em>: ");
226 if (error
.empty() == false)
227 response
.append(error
);
229 response
.append(httpcodeToStr(httpcode
));
231 response
.append("</p>This error is a result of the request: <pre>");
233 response
.append("The successfully executed operation was requested by: <pre>");
234 response
.append(request
).append("</pre></body></html>");
237 if (_config
->FindB("aptwebserver::closeOnError", false) == true)
238 headers
.push_back("Connection: close");
240 addDataHeaders(headers
, response
);
241 sendHead(log
, client
, httpcode
, headers
);
243 sendData(client
, headers
, response
);
245 static void sendSuccess(std::ostream
&log
, int const client
, std::string
const &request
,
246 bool const content
, std::string
const &error
, std::list
<std::string
> &headers
)
248 sendError(log
, client
, 200, request
, content
, error
, headers
);
251 static void sendRedirect(std::ostream
&log
, int const client
, int const httpcode
,/*{{{*/
252 std::string
const &uri
, std::string
const &request
, bool content
)
254 std::list
<std::string
> headers
;
255 std::string
response("<!doctype html><html><head><title>");
256 response
.append(httpcodeToStr(httpcode
)).append("</title><meta charset=\"utf-8\" /></head>");
257 response
.append("<body><h1>").append(httpcodeToStr(httpcode
)).append("</h1");
258 response
.append("<p>You should be redirected to <em>").append(uri
).append("</em></p>");
259 response
.append("This page is a result of the request: <pre>");
260 response
.append(request
).append("</pre></body></html>");
261 addDataHeaders(headers
, response
);
262 std::string
location("Location: ");
263 if (strncmp(uri
.c_str(), "http://", 7) != 0 && strncmp(uri
.c_str(), "https://", 8) != 0)
265 std::string
const host
= LookupTag(request
, "Host");
266 unsigned int const httpsport
= _config
->FindI("aptwebserver::port::https", 4433);
267 std::string hosthttpsport
;
268 strprintf(hosthttpsport
, ":%u", httpsport
);
269 if (host
.find(hosthttpsport
) != std::string::npos
)
270 location
.append("https://");
272 location
.append("http://");
273 location
.append(host
).append("/");
274 if (strncmp("/home/", uri
.c_str(), strlen("/home/")) == 0 && uri
.find("/public_html/") != std::string::npos
)
276 std::string homeuri
= SubstVar(uri
, "/home/", "~");
277 homeuri
= SubstVar(homeuri
, "/public_html/", "/");
278 location
.append(homeuri
);
281 location
.append(uri
);
284 location
.append(uri
);
285 headers
.push_back(location
);
286 sendHead(log
, client
, httpcode
, headers
);
288 sendData(client
, headers
, response
);
291 static int filter_hidden_files(const struct dirent
*a
) /*{{{*/
293 if (a
->d_name
[0] == '.')
295 #ifdef _DIRENT_HAVE_D_TYPE
296 // if we have the d_type check that only files and dirs will be included
297 if (a
->d_type
!= DT_UNKNOWN
&&
298 a
->d_type
!= DT_REG
&&
299 a
->d_type
!= DT_LNK
&& // this includes links to regular files
305 static int grouped_alpha_case_sort(const struct dirent
**a
, const struct dirent
**b
) {
306 #ifdef _DIRENT_HAVE_D_TYPE
307 if ((*a
)->d_type
== DT_DIR
&& (*b
)->d_type
== DT_DIR
);
308 else if ((*a
)->d_type
== DT_DIR
&& (*b
)->d_type
== DT_REG
)
310 else if ((*b
)->d_type
== DT_DIR
&& (*a
)->d_type
== DT_REG
)
315 struct stat f_prop
; //File's property
316 stat((*a
)->d_name
, &f_prop
);
317 int const amode
= f_prop
.st_mode
;
318 stat((*b
)->d_name
, &f_prop
);
319 int const bmode
= f_prop
.st_mode
;
320 if (S_ISDIR(amode
) && S_ISDIR(bmode
));
321 else if (S_ISDIR(amode
))
323 else if (S_ISDIR(bmode
))
326 return strcasecmp((*a
)->d_name
, (*b
)->d_name
);
329 static void sendDirectoryListing(std::ostream
&log
, int const client
, std::string
const &dir
,/*{{{*/
330 std::string
const &request
, bool content
, std::list
<std::string
> &headers
)
332 struct dirent
**namelist
;
333 int const counter
= scandir(dir
.c_str(), &namelist
, filter_hidden_files
, grouped_alpha_case_sort
);
336 sendError(log
, client
, 500, request
, content
, "scandir failed", headers
);
340 std::ostringstream listing
;
341 listing
<< "<!doctype html><html><head><title>Index of " << dir
<< "</title><meta charset=\"utf-8\" />"
342 << "<style type=\"text/css\"><!-- td {padding: 0.02em 0.5em 0.02em 0.5em;}"
343 << "tr:nth-child(even){background-color:#dfdfdf;}"
344 << "h1, td:nth-child(3){text-align:center;}"
345 << "table {margin-left:auto;margin-right:auto;} --></style>"
346 << "</head>" << std::endl
347 << "<body><h1>Index of " << dir
<< "</h1>" << std::endl
348 << "<table><tr><th>#</th><th>Name</th><th>Size</th><th>Last-Modified</th></tr>" << std::endl
;
350 listing
<< "<tr><td>d</td><td><a href=\"..\">Parent Directory</a></td><td>-</td><td>-</td></tr>";
351 for (int i
= 0; i
< counter
; ++i
) {
353 std::string
filename(dir
);
354 filename
.append("/").append(namelist
[i
]->d_name
);
355 stat(filename
.c_str(), &fs
);
356 if (S_ISDIR(fs
.st_mode
))
358 listing
<< "<tr><td>d</td>"
359 << "<td><a href=\"" << namelist
[i
]->d_name
<< "/\">" << namelist
[i
]->d_name
<< "</a></td>"
364 listing
<< "<tr><td>f</td>"
365 << "<td><a href=\"" << namelist
[i
]->d_name
<< "\">" << namelist
[i
]->d_name
<< "</a></td>"
366 << "<td>" << SizeToStr(fs
.st_size
) << "B</td>";
368 listing
<< "<td>" << TimeRFC1123(fs
.st_mtime
, true) << "</td></tr>" << std::endl
;
370 listing
<< "</table></body></html>" << std::endl
;
372 std::string
response(listing
.str());
373 addDataHeaders(headers
, response
);
374 sendHead(log
, client
, 200, headers
);
376 sendData(client
, headers
, response
);
379 static bool parseFirstLine(std::ostream
&log
, int const client
, std::string
const &request
,/*{{{*/
380 std::string
&filename
, std::string
¶ms
, bool &sendContent
,
381 bool &closeConnection
, std::list
<std::string
> &headers
)
383 if (strncmp(request
.c_str(), "HEAD ", 5) == 0)
385 if (strncmp(request
.c_str(), "GET ", 4) != 0)
387 sendError(log
, client
, 501, request
, true, "", headers
);
391 size_t const lineend
= request
.find('\n');
392 size_t filestart
= request
.find(' ');
393 for (; request
[filestart
] == ' '; ++filestart
);
394 size_t fileend
= request
.rfind(' ', lineend
);
395 if (lineend
== std::string::npos
|| filestart
== std::string::npos
||
396 fileend
== std::string::npos
|| filestart
== fileend
)
398 sendError(log
, client
, 500, request
, sendContent
, "Filename can't be extracted", headers
);
402 size_t httpstart
= fileend
;
403 for (; request
[httpstart
] == ' '; ++httpstart
);
404 if (strncmp(request
.c_str() + httpstart
, "HTTP/1.1\r", 9) == 0)
405 closeConnection
= strcasecmp(LookupTag(request
, "Connection", "Keep-Alive").c_str(), "Keep-Alive") != 0;
406 else if (strncmp(request
.c_str() + httpstart
, "HTTP/1.0\r", 9) == 0)
407 closeConnection
= strcasecmp(LookupTag(request
, "Connection", "Keep-Alive").c_str(), "close") == 0;
410 sendError(log
, client
, 500, request
, sendContent
, "Not a HTTP/1.{0,1} request", headers
);
414 filename
= request
.substr(filestart
, fileend
- filestart
);
415 if (filename
.find(' ') != std::string::npos
)
417 sendError(log
, client
, 500, request
, sendContent
, "Filename contains an unencoded space", headers
);
421 std::string host
= LookupTag(request
, "Host", "");
422 if (host
.empty() == true)
424 // RFC 2616 §14.23 requires Host
425 sendError(log
, client
, 400, request
, sendContent
, "Host header is required", headers
);
428 host
= "http://" + host
;
430 // Proxies require absolute uris, so this is a simple proxy-fake option
431 std::string
const absolute
= _config
->Find("aptwebserver::request::absolute", "uri,path");
432 if (strncmp(host
.c_str(), filename
.c_str(), host
.length()) == 0 && APT::String::Startswith(filename
, "/_config/") == false)
434 if (absolute
.find("uri") == std::string::npos
)
436 sendError(log
, client
, 400, request
, sendContent
, "Request is absoluteURI, but configured to not accept that", headers
);
440 // strip the host from the request to make it an absolute path
441 filename
.erase(0, host
.length());
443 std::string
const authConf
= _config
->Find("aptwebserver::proxy-authorization", "");
444 std::string auth
= LookupTag(request
, "Proxy-Authorization", "");
445 if (authConf
.empty() != auth
.empty())
448 sendError(log
, client
, 407, request
, sendContent
, "Proxy requires authentication", headers
);
450 sendError(log
, client
, 407, request
, sendContent
, "Client wants to authenticate to proxy, but proxy doesn't need it", headers
);
453 if (authConf
.empty() == false)
455 char const * const basic
= "Basic ";
456 if (strncmp(auth
.c_str(), basic
, strlen(basic
)) == 0)
458 auth
.erase(0, strlen(basic
));
459 if (auth
!= authConf
)
461 sendError(log
, client
, 407, request
, sendContent
, "Proxy-Authentication doesn't match", headers
);
467 std::list
<std::string
> headers
;
468 headers
.push_back("Proxy-Authenticate: Basic");
469 sendError(log
, client
, 407, request
, sendContent
, "Unsupported Proxy-Authentication Scheme", headers
);
474 else if (absolute
.find("path") == std::string::npos
&& APT::String::Startswith(filename
, "/_config/") == false)
476 sendError(log
, client
, 400, request
, sendContent
, "Request is absolutePath, but configured to not accept that", headers
);
480 if (APT::String::Startswith(filename
, "/_config/") == false)
482 std::string
const authConf
= _config
->Find("aptwebserver::authorization", "");
483 std::string auth
= LookupTag(request
, "Authorization", "");
484 if (authConf
.empty() != auth
.empty())
487 sendError(log
, client
, 401, request
, sendContent
, "Server requires authentication", headers
);
489 sendError(log
, client
, 401, request
, sendContent
, "Client wants to authenticate to server, but server doesn't need it", headers
);
492 if (authConf
.empty() == false)
494 char const * const basic
= "Basic ";
495 if (strncmp(auth
.c_str(), basic
, strlen(basic
)) == 0)
497 auth
.erase(0, strlen(basic
));
498 if (auth
!= authConf
)
500 sendError(log
, client
, 401, request
, sendContent
, "Authentication doesn't match", headers
);
506 headers
.push_back("WWW-Authenticate: Basic");
507 sendError(log
, client
, 401, request
, sendContent
, "Unsupported Authentication Scheme", headers
);
513 size_t paramspos
= filename
.find('?');
514 if (paramspos
!= std::string::npos
)
516 params
= filename
.substr(paramspos
+ 1);
517 filename
.erase(paramspos
);
520 filename
= DeQuoteString(filename
);
522 // this is not a secure server, but at least prevent the obvious …
523 if (filename
.empty() == true || filename
[0] != '/' ||
524 strncmp(filename
.c_str(), "//", 2) == 0 ||
525 filename
.find_first_of("\r\n\t\f\v") != std::string::npos
||
526 filename
.find("/../") != std::string::npos
)
528 std::list
<std::string
> headers
;
529 sendError(log
, client
, 400, request
, sendContent
, "Filename contains illegal character (sequence)", headers
);
533 // nuke the first character which is a / as we assured above
534 filename
.erase(0, 1);
535 if (filename
.empty() == true)
537 // support ~user/ uris to refer to /home/user/public_html/ as a kind-of special directory
538 else if (filename
[0] == '~')
540 // /home/user is actually not entirely correct, but good enough for now
541 size_t dashpos
= filename
.find('/');
542 if (dashpos
!= std::string::npos
)
544 std::string home
= filename
.substr(1, filename
.find('/') - 1);
545 std::string pubhtml
= filename
.substr(filename
.find('/') + 1);
546 filename
= "/home/" + home
+ "/public_html/" + pubhtml
;
549 filename
= "/home/" + filename
.substr(1) + "/public_html/";
552 // if no filename is given, but a valid directory see if we can use an index or
553 // have to resort to a autogenerated directory listing later on
554 if (DirectoryExists(filename
) == true)
556 std::string
const directoryIndex
= _config
->Find("aptwebserver::directoryindex");
557 if (directoryIndex
.empty() == false && directoryIndex
== flNotDir(directoryIndex
) &&
558 RealFileExists(filename
+ directoryIndex
) == true)
559 filename
+= directoryIndex
;
565 static bool handleOnTheFlyReconfiguration(std::ostream
&log
, int const client
,/*{{{*/
566 std::string
const &request
, std::vector
<std::string
> parts
, std::list
<std::string
> &headers
)
568 size_t const pcount
= parts
.size();
569 for (size_t i
= 0; i
< pcount
; ++i
)
570 parts
[i
] = DeQuoteString(parts
[i
]);
571 if (pcount
== 4 && parts
[1] == "set")
573 _config
->Set(parts
[2], parts
[3]);
574 sendSuccess(log
, client
, request
, true, "Option '" + parts
[2] + "' was set to '" + parts
[3] + "'!", headers
);
577 else if (pcount
== 4 && parts
[1] == "find")
579 std::string response
= _config
->Find(parts
[2], parts
[3]);
580 addDataHeaders(headers
, response
);
581 sendHead(log
, client
, 200, headers
);
582 sendData(client
, headers
, response
);
585 else if (pcount
== 3 && parts
[1] == "find")
587 if (_config
->Exists(parts
[2]) == true)
589 std::string response
= _config
->Find(parts
[2]);
590 addDataHeaders(headers
, response
);
591 sendHead(log
, client
, 200, headers
);
592 sendData(client
, headers
, response
);
595 sendError(log
, client
, 404, request
, true, "Requested Configuration option doesn't exist", headers
);
598 else if (pcount
== 3 && parts
[1] == "clear")
600 _config
->Clear(parts
[2]);
601 sendSuccess(log
, client
, request
, true, "Option '" + parts
[2] + "' was cleared.", headers
);
605 sendError(log
, client
, 400, request
, true, "Unknown on-the-fly configuration request", headers
);
609 static void * handleClient(int const client
, size_t const id
) /*{{{*/
611 auto logfilepath
= _config
->FindFile("aptwebserver::logfiles");
612 if (logfilepath
.empty() == false)
613 strprintf(logfilepath
, "%s.client-%lu.log", logfilepath
.c_str(), id
);
615 logfilepath
= "/dev/null";
616 std::ofstream
logfile(logfilepath
);
617 basic_teeostream
<char> log(std::clog
, logfile
);
619 log
<< "ACCEPT client " << client
<< std::endl
;
620 bool closeConnection
= false;
621 while (closeConnection
== false)
623 std::vector
<std::string
> messages
;
624 if (ReadMessages(client
, messages
) == false)
627 std::list
<std::string
> headers
;
628 for (std::vector
<std::string
>::const_iterator m
= messages
.begin();
629 m
!= messages
.end() && closeConnection
== false; ++m
) {
630 // if we announced a closing in previous response, do the close now
631 if (std::find(headers
.begin(), headers
.end(), std::string("Connection: close")) != headers
.end())
633 closeConnection
= true;
638 log
<< ">>> REQUEST from " << client
<< " >>>" << std::endl
<< *m
639 << std::endl
<< "<<<<<<<<<<<<<<<<" << std::endl
;
640 std::string filename
;
642 bool sendContent
= true;
643 if (parseFirstLine(log
, client
, *m
, filename
, params
, sendContent
, closeConnection
, headers
) == false)
646 // special webserver command request
647 if (filename
.length() > 1 && filename
[0] == '_')
649 std::vector
<std::string
> parts
= VectorizeString(filename
, '/');
650 if (parts
[0] == "_config")
652 handleOnTheFlyReconfiguration(log
, client
, *m
, parts
, headers
);
657 // string replacements in the requested filename
658 ::Configuration::Item
const *Replaces
= _config
->Tree("aptwebserver::redirect::replace");
659 if (Replaces
!= NULL
)
661 std::string redirect
= "/" + filename
;
662 for (::Configuration::Item
*I
= Replaces
->Child
; I
!= NULL
; I
= I
->Next
)
663 redirect
= SubstVar(redirect
, I
->Tag
, I
->Value
);
664 if (redirect
.empty() == false && redirect
[0] == '/')
666 if (redirect
!= filename
)
668 sendRedirect(log
, client
, _config
->FindI("aptwebserver::redirect::httpcode", 301), redirect
, *m
, sendContent
);
673 ::Configuration::Item
const *Overwrite
= _config
->Tree("aptwebserver::overwrite");
674 if (Overwrite
!= NULL
)
676 for (::Configuration::Item
*I
= Overwrite
->Child
; I
!= NULL
; I
= I
->Next
)
678 regex_t
*pattern
= new regex_t
;
679 int const res
= regcomp(pattern
, I
->Tag
.c_str(), REG_EXTENDED
| REG_ICASE
| REG_NOSUB
);
683 regerror(res
, pattern
, error
, sizeof(error
));
684 sendError(log
, client
, 500, *m
, sendContent
, error
, headers
);
687 if (regexec(pattern
, filename
.c_str(), 0, 0, 0) == 0)
689 filename
= _config
->Find("aptwebserver::overwrite::" + I
->Tag
+ "::filename", filename
);
690 if (filename
[0] == '/')
699 // deal with the request
700 unsigned int const httpsport
= _config
->FindI("aptwebserver::port::https", 4433);
701 std::string hosthttpsport
;
702 strprintf(hosthttpsport
, ":%u", httpsport
);
703 if (_config
->FindB("aptwebserver::support::http", true) == false &&
704 LookupTag(*m
, "Host").find(hosthttpsport
) == std::string::npos
)
706 sendError(log
, client
, 400, *m
, sendContent
, "HTTP disabled, all requests must be HTTPS", headers
);
709 else if (RealFileExists(filename
) == true)
711 FileFd
data(filename
, FileFd::ReadOnly
);
712 std::string condition
= LookupTag(*m
, "If-Modified-Since", "");
713 if (_config
->FindB("aptwebserver::support::modified-since", true) == true && condition
.empty() == false)
716 if (RFC1123StrToTime(condition
.c_str(), cache
) == true &&
717 cache
>= data
.ModificationTime())
719 sendHead(log
, client
, 304, headers
);
724 if (_config
->FindB("aptwebserver::support::range", true) == true)
725 condition
= LookupTag(*m
, "Range", "");
728 if (condition
.empty() == false && strncmp(condition
.c_str(), "bytes=", 6) == 0)
730 std::string ranges
= ',' + _config
->Find("aptwebserver::response-header::Accept-Ranges") + ',';
731 ranges
.erase(std::remove(ranges
.begin(), ranges
.end(), ' '), ranges
.end());
732 if (ranges
.find(",bytes,") == std::string::npos
)
734 // we handle it as an error here because we are a test server - a real one should just ignore it
735 sendError(log
, client
, 400, *m
, sendContent
, "Client does range requests we don't support", headers
);
741 if (_config
->FindB("aptwebserver::support::if-range", true) == true)
742 ifrange
= LookupTag(*m
, "If-Range", "");
743 bool validrange
= (ifrange
.empty() == true ||
744 (RFC1123StrToTime(ifrange
.c_str(), cache
) == true &&
745 cache
<= data
.ModificationTime()));
747 // FIXME: support multiple byte-ranges (APT clients do not do this)
748 if (condition
.find(',') == std::string::npos
)
751 unsigned long long filestart
= strtoull(condition
.c_str() + start
, NULL
, 10);
752 // FIXME: no support for last-byte-pos being not the end of the file (APT clients do not do this)
753 size_t dash
= condition
.find('-') + 1;
754 unsigned long long fileend
= strtoull(condition
.c_str() + dash
, NULL
, 10);
755 unsigned long long filesize
= data
.FileSize();
756 if ((fileend
== 0 || (fileend
== filesize
&& fileend
>= filestart
)) &&
759 if (filesize
> filestart
)
761 data
.Skip(filestart
);
762 // make sure to send content-range before conent-length
763 // as regression test for LP: #1445239
764 std::ostringstream contentrange
;
765 contentrange
<< "Content-Range: bytes " << filestart
<< "-"
766 << filesize
- 1 << "/" << filesize
;
767 headers
.push_back(contentrange
.str());
768 std::ostringstream contentlength
;
769 contentlength
<< "Content-Length: " << (filesize
- filestart
);
770 headers
.push_back(contentlength
.str());
771 sendHead(log
, client
, 206, headers
);
772 if (sendContent
== true)
773 sendFile(client
, headers
, data
);
778 if (_config
->FindB("aptwebserver::support::content-range", true) == true)
780 std::ostringstream contentrange
;
781 contentrange
<< "Content-Range: bytes */" << filesize
;
782 headers
.push_back(contentrange
.str());
784 sendError(log
, client
, 416, *m
, sendContent
, "", headers
);
791 addFileHeaders(headers
, data
);
792 sendHead(log
, client
, 200, headers
);
793 if (sendContent
== true)
794 sendFile(client
, headers
, data
);
796 else if (DirectoryExists(filename
) == true)
798 if (filename
[filename
.length()-1] == '/')
799 sendDirectoryListing(log
, client
, filename
, *m
, sendContent
, headers
);
801 sendRedirect(log
, client
, 301, filename
.append("/"), *m
, sendContent
);
804 sendError(log
, client
, 404, *m
, sendContent
, "", headers
);
807 // if we announced a closing in the last response, do the close now
808 if (std::find(headers
.begin(), headers
.end(), std::string("Connection: close")) != headers
.end())
809 closeConnection
= true;
811 if (_error
->PendingError() == true)
813 _error
->DumpErrors(std::cerr
);
815 _error
->DumpErrors(std::cerr
);
817 log
<< "CLOSE client " << client
<< std::endl
;
822 int main(int const argc
, const char * argv
[])
824 CommandLine::Args Args
[] = {
825 {'p', "port", "aptwebserver::port", CommandLine::HasArg
},
826 {0, "request-absolute", "aptwebserver::request::absolute", CommandLine::HasArg
},
827 {0, "authorization", "aptwebserver::authorization", CommandLine::HasArg
},
828 {0, "proxy-authorization", "aptwebserver::proxy-authorization", CommandLine::HasArg
},
829 {0, "logfiles", "aptwebserver::logfiles", CommandLine::HasArg
},
830 {'c',"config-file",0,CommandLine::ConfigFile
},
831 {'o',"option",0,CommandLine::ArbItem
},
835 CommandLine
CmdL(Args
, _config
);
836 if(CmdL
.Parse(argc
,argv
) == false)
838 _error
->DumpErrors();
842 // create socket, bind and listen to it {{{
843 // ignore SIGPIPE, this can happen on write() if the socket closes connection
844 signal(SIGPIPE
, SIG_IGN
);
845 // we don't care for our slaves, so ignore their death
846 signal(SIGCHLD
, SIG_IGN
);
848 int sock
= socket(AF_INET6
, SOCK_STREAM
, 0);
851 _error
->Errno("aptwerbserver", "Couldn't create socket");
852 _error
->DumpErrors(std::cerr
);
856 int port
= _config
->FindI("aptwebserver::port", 8080);
858 // ensure that we accept all connections: v4 or v6
859 int const iponly
= 0;
860 setsockopt(sock
, IPPROTO_IPV6
, IPV6_V6ONLY
, &iponly
, sizeof(iponly
));
861 // to not linger on an address
862 int const enable
= 1;
863 setsockopt(sock
, SOL_SOCKET
, SO_REUSEADDR
, &enable
, sizeof(enable
));
865 struct sockaddr_in6 locAddr
;
866 memset(&locAddr
, 0, sizeof(locAddr
));
867 locAddr
.sin6_family
= AF_INET6
;
868 locAddr
.sin6_port
= htons(port
);
869 locAddr
.sin6_addr
= in6addr_any
;
871 if (bind(sock
, (struct sockaddr
*) &locAddr
, sizeof(locAddr
)) < 0)
873 _error
->Errno("aptwerbserver", "Couldn't bind");
874 _error
->DumpErrors(std::cerr
);
880 struct sockaddr_in6 addr
;
881 socklen_t addrlen
= sizeof(sockaddr_in6
);
882 if (getsockname(sock
, (struct sockaddr
*) &addr
, &addrlen
) != 0)
883 _error
->Errno("getsockname", "Could not get chosen port number");
885 port
= ntohs(addr
.sin6_port
);
887 std::string
const portfilename
= _config
->Find("aptwebserver::portfile", "");
888 if (portfilename
.empty() == false)
890 FileFd
portfile(portfilename
, FileFd::WriteOnly
| FileFd::Create
| FileFd::Empty
);
891 std::string portcontent
;
892 strprintf(portcontent
, "%d", port
);
893 portfile
.Write(portcontent
.c_str(), portcontent
.size());
896 _config
->Set("aptwebserver::port::http", port
);
899 if (_config
->FindB("aptwebserver::fork", false) == true)
901 std::string
const pidfilename
= _config
->Find("aptwebserver::pidfile", "aptwebserver.pid");
902 int const pidfilefd
= GetLock(pidfilename
);
903 if (pidfilefd
< 0 || pidfile
.OpenDescriptor(pidfilefd
, FileFd::WriteOnly
) == false)
905 _error
->Errno("aptwebserver", "Couldn't acquire lock on pidfile '%s'", pidfilename
.c_str());
906 _error
->DumpErrors(std::cerr
);
910 pid_t child
= fork();
913 _error
->Errno("aptwebserver", "Forking failed");
914 _error
->DumpErrors(std::cerr
);
919 // successfully forked: ready to serve!
920 std::string pidcontent
;
921 strprintf(pidcontent
, "%d", child
);
922 pidfile
.Write(pidcontent
.c_str(), pidcontent
.size());
924 if (_error
->PendingError() == true)
926 _error
->DumpErrors(std::cerr
);
929 std::cout
<< "Successfully forked as " << child
<< std::endl
;
934 std::clog
<< "Serving ANY file on port: " << port
<< std::endl
;
936 int const slaves
= _config
->FindI("aptwebserver::slaves", SOMAXCONN
);
937 std::cerr
<< "SLAVES: " << slaves
<< std::endl
;
938 listen(sock
, slaves
);
941 _config
->CndSet("aptwebserver::response-header::Server", "APT webserver");
942 _config
->CndSet("aptwebserver::response-header::Accept-Ranges", "bytes");
943 _config
->CndSet("aptwebserver::directoryindex", "index.html");
948 int client
= accept(sock
, NULL
, NULL
);
953 _error
->Errno("accept", "Couldn't accept client on socket %d", sock
);
954 _error
->DumpErrors(std::cerr
);
958 std::thread
t(handleClient
, client
, ++id
);