473
void * handleClient(void * voidclient) /*{{{*/
475
int client = *((int*)(voidclient));
476
std::clog << "ACCEPT client " << client << std::endl;
477
std::vector<std::string> messages;
478
while (ReadMessages(client, messages))
480
bool closeConnection = false;
481
for (std::vector<std::string>::const_iterator m = messages.begin();
482
m != messages.end() && closeConnection == false; ++m) {
483
std::clog << ">>> REQUEST from " << client << " >>>" << std::endl << *m
484
<< std::endl << "<<<<<<<<<<<<<<<<" << std::endl;
485
std::list<std::string> headers;
486
std::string filename;
488
bool sendContent = true;
489
if (parseFirstLine(client, *m, filename, params, sendContent, closeConnection) == false)
492
// special webserver command request
493
if (filename.length() > 1 && filename[0] == '_')
495
std::vector<std::string> parts = VectorizeString(filename, '/');
496
if (parts[0] == "_config")
498
handleOnTheFlyReconfiguration(client, *m, parts);
503
// string replacements in the requested filename
504
::Configuration::Item const *Replaces = _config->Tree("aptwebserver::redirect::replace");
505
if (Replaces != NULL)
507
std::string redirect = "/" + filename;
508
for (::Configuration::Item *I = Replaces->Child; I != NULL; I = I->Next)
509
redirect = SubstVar(redirect, I->Tag, I->Value);
511
if (redirect != filename)
513
sendRedirect(client, 301, redirect, *m, sendContent);
518
::Configuration::Item const *Overwrite = _config->Tree("aptwebserver::overwrite");
519
if (Overwrite != NULL)
521
for (::Configuration::Item *I = Overwrite->Child; I != NULL; I = I->Next)
523
regex_t *pattern = new regex_t;
524
int const res = regcomp(pattern, I->Tag.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
528
regerror(res, pattern, error, sizeof(error));
529
sendError(client, 500, *m, sendContent, error);
532
if (regexec(pattern, filename.c_str(), 0, 0, 0) == 0)
534
filename = _config->Find("aptwebserver::overwrite::" + I->Tag + "::filename", filename);
535
if (filename[0] == '/')
544
// deal with the request
545
if (RealFileExists(filename) == true)
547
FileFd data(filename, FileFd::ReadOnly);
548
std::string condition = LookupTag(*m, "If-Modified-Since", "");
549
if (_config->FindB("aptwebserver::support::modified-since", true) == true && condition.empty() == false)
552
if (RFC1123StrToTime(condition.c_str(), cache) == true &&
553
cache >= data.ModificationTime())
555
sendHead(client, 304, headers);
560
if (_config->FindB("aptwebserver::support::range", true) == true)
561
condition = LookupTag(*m, "Range", "");
564
if (condition.empty() == false && strncmp(condition.c_str(), "bytes=", 6) == 0)
568
if (_config->FindB("aptwebserver::support::if-range", true) == true)
569
ifrange = LookupTag(*m, "If-Range", "");
570
bool validrange = (ifrange.empty() == true ||
571
(RFC1123StrToTime(ifrange.c_str(), cache) == true &&
572
cache <= data.ModificationTime()));
574
// FIXME: support multiple byte-ranges (APT clients do not do this)
575
if (condition.find(',') == std::string::npos)
578
unsigned long long filestart = strtoull(condition.c_str() + start, NULL, 10);
579
// FIXME: no support for last-byte-pos being not the end of the file (APT clients do not do this)
580
size_t dash = condition.find('-') + 1;
581
unsigned long long fileend = strtoull(condition.c_str() + dash, NULL, 10);
582
unsigned long long filesize = data.FileSize();
583
if ((fileend == 0 || (fileend == filesize && fileend >= filestart)) &&
586
if (filesize > filestart)
588
data.Skip(filestart);
589
std::ostringstream contentlength;
590
contentlength << "Content-Length: " << (filesize - filestart);
591
headers.push_back(contentlength.str());
592
std::ostringstream contentrange;
593
contentrange << "Content-Range: bytes " << filestart << "-"
594
<< filesize - 1 << "/" << filesize;
595
headers.push_back(contentrange.str());
596
sendHead(client, 206, headers);
597
if (sendContent == true)
598
sendFile(client, data);
603
headers.push_back("Content-Length: 0");
604
std::ostringstream contentrange;
605
contentrange << "Content-Range: bytes */" << filesize;
606
headers.push_back(contentrange.str());
607
sendHead(client, 416, headers);
614
addFileHeaders(headers, data);
615
sendHead(client, 200, headers);
616
if (sendContent == true)
617
sendFile(client, data);
619
else if (DirectoryExists(filename) == true)
621
if (filename[filename.length()-1] == '/')
622
sendDirectoryListing(client, filename, *m, sendContent);
624
sendRedirect(client, 301, filename.append("/"), *m, sendContent);
627
sendError(client, 404, *m, sendContent);
629
_error->DumpErrors(std::cerr);
631
if (closeConnection == true)
635
std::clog << "CLOSE client " << client << std::endl;
430
640
int main(int const argc, const char * argv[])
432
642
CommandLine::Args Args[] = {
515
728
std::clog << "Serving ANY file on port: " << port << std::endl;
730
int const slaves = _config->FindB("aptwebserver::slaves", SOMAXCONN);
731
listen(sock, slaves);
520
734
_config->CndSet("aptwebserver::response-header::Server", "APT webserver");
521
735
_config->CndSet("aptwebserver::response-header::Accept-Ranges", "bytes");
523
std::vector<std::string> messages;
525
while ((client = accept(sock, NULL, NULL)) != -1)
736
_config->CndSet("aptwebserver::directoryindex", "index.html");
738
std::list<int> accepted_clients;
527
std::clog << "ACCEPT client " << client
528
<< " on socket " << sock << std::endl;
530
while (ReadMessages(client, messages))
532
bool closeConnection = false;
533
for (std::vector<std::string>::const_iterator m = messages.begin();
534
m != messages.end() && closeConnection == false; ++m) {
535
std::clog << ">>> REQUEST >>>>" << std::endl << *m
536
<< std::endl << "<<<<<<<<<<<<<<<<" << std::endl;
537
std::list<std::string> headers;
538
std::string filename;
539
bool sendContent = true;
540
if (parseFirstLine(client, *m, filename, sendContent, closeConnection) == false)
543
// special webserver command request
544
if (filename.length() > 1 && filename[0] == '_')
546
std::vector<std::string> parts = VectorizeString(filename, '/');
547
if (parts[0] == "_config")
549
handleOnTheFlyReconfiguration(client, *m, parts);
554
// string replacements in the requested filename
555
::Configuration::Item const *Replaces = _config->Tree("aptwebserver::redirect::replace");
556
if (Replaces != NULL)
558
std::string redirect = "/" + filename;
559
for (::Configuration::Item *I = Replaces->Child; I != NULL; I = I->Next)
560
redirect = SubstVar(redirect, I->Tag, I->Value);
562
if (redirect != filename)
564
sendRedirect(client, 301, redirect, *m, sendContent);
569
::Configuration::Item const *Overwrite = _config->Tree("aptwebserver::overwrite");
570
if (Overwrite != NULL)
572
for (::Configuration::Item *I = Overwrite->Child; I != NULL; I = I->Next)
574
regex_t *pattern = new regex_t;
575
int const res = regcomp(pattern, I->Tag.c_str(), REG_EXTENDED | REG_ICASE | REG_NOSUB);
579
regerror(res, pattern, error, sizeof(error));
580
sendError(client, 500, *m, sendContent, error);
583
if (regexec(pattern, filename.c_str(), 0, 0, 0) == 0)
585
filename = _config->Find("aptwebserver::overwrite::" + I->Tag + "::filename", filename);
586
if (filename[0] == '/')
595
// deal with the request
596
if (RealFileExists(filename) == true)
598
FileFd data(filename, FileFd::ReadOnly);
599
std::string condition = LookupTag(*m, "If-Modified-Since", "");
600
if (condition.empty() == false)
603
if (RFC1123StrToTime(condition.c_str(), cache) == true &&
604
cache >= data.ModificationTime())
606
sendHead(client, 304, headers);
611
if (_config->FindB("aptwebserver::support::range", true) == true)
612
condition = LookupTag(*m, "Range", "");
615
if (condition.empty() == false && strncmp(condition.c_str(), "bytes=", 6) == 0)
619
if (_config->FindB("aptwebserver::support::if-range", true) == true)
620
ifrange = LookupTag(*m, "If-Range", "");
621
bool validrange = (ifrange.empty() == true ||
622
(RFC1123StrToTime(ifrange.c_str(), cache) == true &&
623
cache <= data.ModificationTime()));
625
// FIXME: support multiple byte-ranges (APT clients do not do this)
626
if (condition.find(',') == std::string::npos)
629
unsigned long long filestart = strtoull(condition.c_str() + start, NULL, 10);
630
// FIXME: no support for last-byte-pos being not the end of the file (APT clients do not do this)
631
size_t dash = condition.find('-') + 1;
632
unsigned long long fileend = strtoull(condition.c_str() + dash, NULL, 10);
633
unsigned long long filesize = data.FileSize();
634
if ((fileend == 0 || (fileend == filesize && fileend >= filestart)) &&
637
if (filesize > filestart)
639
data.Skip(filestart);
640
std::ostringstream contentlength;
641
contentlength << "Content-Length: " << (filesize - filestart);
642
headers.push_back(contentlength.str());
643
std::ostringstream contentrange;
644
contentrange << "Content-Range: bytes " << filestart << "-"
645
<< filesize - 1 << "/" << filesize;
646
headers.push_back(contentrange.str());
647
sendHead(client, 206, headers);
648
if (sendContent == true)
649
sendFile(client, data);
654
headers.push_back("Content-Length: 0");
655
std::ostringstream contentrange;
656
contentrange << "Content-Range: bytes */" << filesize;
657
headers.push_back(contentrange.str());
658
sendHead(client, 416, headers);
665
addFileHeaders(headers, data);
666
sendHead(client, 200, headers);
667
if (sendContent == true)
668
sendFile(client, data);
670
else if (DirectoryExists(filename) == true)
672
if (filename == "." || filename[filename.length()-1] == '/')
673
sendDirectoryListing(client, filename, *m, sendContent);
675
sendRedirect(client, 301, filename.append("/"), *m, sendContent);
678
sendError(client, 404, *m, sendContent);
680
_error->DumpErrors(std::cerr);
682
if (closeConnection == true)
686
std::clog << "CLOSE client " << client
687
<< " on socket " << sock << std::endl;
742
int client = accept(sock, NULL, NULL);
747
_error->Errno("accept", "Couldn't accept client on socket %d", sock);
748
_error->DumpErrors(std::cerr);
753
if (pthread_attr_init(&attr) != 0 || pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0)
755
_error->Errno("pthread_attr", "Couldn't set detach attribute for a fresh thread to handle client %d on socket %d", client, sock);
756
_error->DumpErrors(std::cerr);
762
// thats rather dirty, but we need to store the client socket somewhere safe
763
accepted_clients.push_front(client);
764
if (pthread_create(&tid, &attr, &handleClient, &(*accepted_clients.begin())) != 0)
766
_error->Errno("pthread_create", "Couldn't create a fresh thread to handle client %d on socket %d", client, sock);
767
_error->DumpErrors(std::cerr);