#include <time.h>
#include <stdlib.h>
#include <dirent.h>
+#include <signal.h>
char const * const httpcodeToStr(int const httpcode) { /*{{{*/
switch (httpcode) {
case 203: return "203 Non-Authoritative Information";
case 204: return "204 No Content";
case 205: return "205 Reset Content";
- case 206: return "206 Partial Conent";
+ case 206: return "206 Partial Content";
// Redirections 3xx
case 300: return "300 Multiple Choices";
case 301: return "301 Moved Permanently";
case 404: return "404 Not Found";
case 405: return "405 Method Not Allowed";
case 406: return "406 Not Acceptable";
- case 407: return "Proxy Authentication Required";
- case 408: return "Request Time-out";
- case 409: return "Conflict";
- case 410: return "Gone";
- case 411: return "Length Required";
- case 412: return "Precondition Failed";
- case 413: return "Request Entity Too Large";
- case 414: return "Request-URI Too Large";
- case 415: return "Unsupported Media Type";
- case 416: return "Requested range not satisfiable";
- case 417: return "Expectation Failed";
+ case 407: return "407 Proxy Authentication Required";
+ case 408: return "408 Request Time-out";
+ case 409: return "409 Conflict";
+ case 410: return "410 Gone";
+ case 411: return "411 Length Required";
+ case 412: return "412 Precondition Failed";
+ case 413: return "413 Request Entity Too Large";
+ case 414: return "414 Request-URI Too Large";
+ case 415: return "415 Unsupported Media Type";
+ case 416: return "416 Requested range not satisfiable";
+ case 417: return "417 Expectation Failed";
// Server error 5xx
- case 500: return "Internal Server Error";
- case 501: return "Not Implemented";
- case 502: return "Bad Gateway";
- case 503: return "Service Unavailable";
- case 504: return "Gateway Time-out";
- case 505: return "HTTP Version not supported";
+ case 500: return "500 Internal Server Error";
+ case 501: return "501 Not Implemented";
+ case 502: return "502 Bad Gateway";
+ case 503: return "503 Service Unavailable";
+ case 504: return "504 Gateway Time-out";
+ case 505: return "505 HTTP Version not supported";
}
return NULL;
}
std::string lastmodified("Last-Modified: ");
lastmodified.append(TimeRFC1123(data.ModificationTime()));
headers.push_back(lastmodified);
+
+ std::string const fileext = flExtension(data.Name());
+ if (fileext.empty() == false && fileext != data.Name()) {
+ std::string confcontenttype("aptwebserver::ContentType::");
+ confcontenttype.append(fileext);
+ std::string const contenttype = _config->Find(confcontenttype);
+ if (contenttype.empty() == false) {
+ std::string header("Content-Type: ");
+ header.append(contenttype);
+ headers.push_back(header);
+ }
+ }
}
/*}}}*/
void addDataHeaders(std::list<std::string> &headers, std::string &data) {/*{{{*/
date.append(TimeRFC1123(time(NULL)));
headers.push_back(date);
+ headers.push_back("Accept-Ranges: bytes");
+
std::clog << ">>> RESPONSE >>>" << std::endl;
bool Success = true;
for (std::list<std::string>::const_iterator h = headers.begin();
Success == true && h != headers.end(); ++h) {
Success &= FileFd::Write(client, h->c_str(), h->size());
- Success &= FileFd::Write(client, "\r\n", 2);
+ if (Success == true)
+ Success &= FileFd::Write(client, "\r\n", 2);
std::clog << *h << std::endl;
}
- Success &= FileFd::Write(client, "\r\n", 2);
+ if (Success == true)
+ Success &= FileFd::Write(client, "\r\n", 2);
std::clog << "<<<<<<<<<<<<<<<<" << std::endl;
return Success;
}
while ((Success &= data.Read(buffer, sizeof(buffer), &actual)) == true) {
if (actual == 0)
break;
- Success &= FileFd::Write(client, buffer, actual);
+ if (Success == true)
+ Success &= FileFd::Write(client, buffer, actual);
}
- Success &= FileFd::Write(client, "\r\n", 2);
+ if (Success == true)
+ Success &= FileFd::Write(client, "\r\n", 2);
return Success;
}
/*}}}*/
bool sendData(int const client, std::string const &data) { /*{{{*/
bool Success = true;
Success &= FileFd::Write(client, data.c_str(), data.size());
- Success &= FileFd::Write(client, "\r\n", 2);
+ if (Success == true)
+ Success &= FileFd::Write(client, "\r\n", 2);
return Success;
}
/*}}}*/
-void sendError(int const client, int const httpcode, std::string const &request, bool content) { /*{{{*/
+void sendError(int const client, int const httpcode, std::string const &request, bool content, std::string const &error = "") { /*{{{*/
std::list<std::string> headers;
std::string response("<html><head><title>");
response.append(httpcodeToStr(httpcode)).append("</title></head>");
response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1>");
+ if (error.empty() == false)
+ response.append("<p><em>Error</em>: ").append(error).append("</p>");
response.append("This error is a result of the request: <pre>");
response.append(request).append("</pre></body></html>");
addDataHeaders(headers, response);
sendData(client, response);
}
/*}}}*/
+bool parseFirstLine(int const client, std::string const &request, std::string &filename, bool &sendContent, bool &closeConnection) { /*{{{*/
+ if (strncmp(request.c_str(), "HEAD ", 5) == 0)
+ sendContent = false;
+ if (strncmp(request.c_str(), "GET ", 4) != 0)
+ {
+ sendError(client, 501, request, true);
+ return false;
+ }
+
+ size_t const lineend = request.find('\n');
+ size_t filestart = request.find(' ');
+ for (; request[filestart] == ' '; ++filestart);
+ size_t fileend = request.rfind(' ', lineend);
+ if (lineend == std::string::npos || filestart == std::string::npos ||
+ fileend == std::string::npos || filestart == fileend) {
+ sendError(client, 500, request, sendContent, "Filename can't be extracted");
+ return false;
+ }
+
+ size_t httpstart = fileend;
+ for (; request[httpstart] == ' '; ++httpstart);
+ if (strncmp(request.c_str() + httpstart, "HTTP/1.1\r", 9) == 0)
+ closeConnection = strcasecmp(LookupTag(request, "Connection", "Keep-Alive").c_str(), "Keep-Alive") != 0;
+ else if (strncmp(request.c_str() + httpstart, "HTTP/1.0\r", 9) == 0)
+ closeConnection = strcasecmp(LookupTag(request, "Connection", "Keep-Alive").c_str(), "close") == 0;
+ else {
+ sendError(client, 500, request, sendContent, "Not an HTTP/1.{0,1} request");
+ return false;
+ }
+
+ filename = request.substr(filestart, fileend - filestart);
+ if (filename.find(' ') != std::string::npos) {
+ sendError(client, 500, request, sendContent, "Filename contains an unencoded space");
+ return false;
+ }
+ filename = DeQuoteString(filename);
+
+ // this is not a secure server, but at least prevent the obvious …
+ if (filename.empty() == true || filename[0] != '/' ||
+ strncmp(filename.c_str(), "//", 2) == 0 ||
+ filename.find_first_of("\r\n\t\f\v") != std::string::npos ||
+ filename.find("/../") != std::string::npos) {
+ sendError(client, 400, request, sendContent, "Filename contains illegal character (sequence)");
+ return false;
+ }
+
+ // nuke the first character which is a / as we assured above
+ filename.erase(0, 1);
+ if (filename.empty() == true)
+ filename = ".";
+ return true;
+}
+ /*}}}*/
int main(int const argc, const char * argv[])
{
CommandLine::Args Args[] = {
}
// create socket, bind and listen to it {{{
+ // ignore SIGPIPE, this can happen on write() if the socket closes connection
+ signal(SIGPIPE, SIG_IGN);
int sock = socket(AF_INET6, SOCK_STREAM, 0);
if(sock < 0 ) {
_error->Errno("aptwerbserver", "Couldn't create socket");
<< " on socket " << sock << std::endl;
while (ReadMessages(client, messages)) {
+ bool closeConnection = false;
for (std::vector<std::string>::const_iterator m = messages.begin();
- m != messages.end(); ++m) {
+ m != messages.end() && closeConnection == false; ++m) {
std::clog << ">>> REQUEST >>>>" << std::endl << *m
<< std::endl << "<<<<<<<<<<<<<<<<" << std::endl;
std::list<std::string> headers;
+ std::string filename;
bool sendContent = true;
- if (strncmp(m->c_str(), "HEAD ", 5) == 0)
- sendContent = false;
- if (strncmp(m->c_str(), "GET ", 4) != 0)
- sendError(client, 501, *m, true);
+ if (parseFirstLine(client, *m, filename, sendContent, closeConnection) == false)
+ continue;
std::string host = LookupTag(*m, "Host", "");
if (host.empty() == true) {
- // RFC 2616 §14.23 Host
- sendError(client, 400, *m, sendContent);
+ // RFC 2616 §14.23 requires Host
+ sendError(client, 400, *m, sendContent, "Host header is required");
continue;
}
- size_t const filestart = m->find(' ', 5);
- std::string filename = m->substr(5, filestart - 5);
- if (filename.empty() == true)
- filename = ".";
- else
- filename = DeQuoteString(filename);
-
if (simulate_broken_server == true) {
std::string data("ni ni ni\n");
addDataHeaders(headers, data);
continue;
}
}
+ condition = LookupTag(*m, "If-Range", "");
+ bool ignoreRange = false;
+ if (condition.empty() == false) {
+ time_t cache;
+ if (RFC1123StrToTime(condition.c_str(), cache) == false ||
+ cache < data.ModificationTime())
+ ignoreRange = true;
+ }
+ condition = LookupTag(*m, "Range", "");
+ if (ignoreRange == false && condition.empty() == false &&
+ strncmp(condition.c_str(), "bytes=", 6) == 0) {
+ size_t end = condition.find(',');
+ // FIXME: support multiple byte-ranges
+ if (end == std::string::npos) {
+ size_t start = 6;
+ unsigned long long filestart = strtoull(condition.c_str() + start, NULL, 10);
+ // FIXME: no fileend support
+ size_t dash = condition.find('-') + 1;
+ unsigned long long fileend = strtoull(condition.c_str() + dash, NULL, 10);
+ unsigned long long filesize = data.FileSize();
+ if (fileend == 0 || fileend == filesize) {
+ if (filesize > filestart) {
+ data.Skip(filestart);
+ std::ostringstream contentlength;
+ contentlength << "Content-Length: " << (filesize - filestart);
+ headers.push_back(contentlength.str());
+ std::ostringstream contentrange;
+ contentrange << "Content-Range: bytes " << filestart << "-"
+ << filesize - 1 << "/" << filesize;
+ headers.push_back(contentrange.str());
+ sendHead(client, 206, headers);
+ if (sendContent == true)
+ sendFile(client, data);
+ continue;
+ } else {
+ headers.push_back("Content-Length: 0");
+ std::ostringstream contentrange;
+ contentrange << "Content-Range: bytes 0-0/" << filesize;
+ headers.push_back(contentrange.str());
+ sendHead(client, 416, headers);
+ continue;
+ }
+ }
+ }
+ }
+
addFileHeaders(headers, data);
sendHead(client, 200, headers);
if (sendContent == true)
}
_error->DumpErrors(std::cerr);
messages.clear();
+ if (closeConnection == true)
+ break;
}
std::clog << "CLOSE client " << client