+#include <config.h>
+
#include <apt-pkg/strutl.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/error.h>
}
/*}}}*/
bool sendHead(int const client, int const httpcode, std::list<std::string> &headers) { /*{{{*/
- string response("HTTP/1.1 ");
+ std::string response("HTTP/1.1 ");
response.append(httpcodeToStr(httpcode));
headers.push_front(response);
return Success;
}
/*}}}*/
-void sendError(int const client, int const httpcode, string const &request, bool content) { /*{{{*/
+void sendError(int const client, int const httpcode, std::string const &request, bool content) { /*{{{*/
std::list<std::string> headers;
- string response("<html><head><title>");
+ std::string response("<html><head><title>");
response.append(httpcodeToStr(httpcode)).append("</title></head>");
response.append("<body><h1>").append(httpcodeToStr(httpcode)).append("</h1");
response.append("This error is a result of the request: <pre>");
sendData(client, response);
}
/*}}}*/
+void sendRedirect(int const client, int const httpcode, std::string const &uri, std::string const &request, bool content) { /*{{{*/
+ 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");
+ response.append("<p>You should be redirected to <em>").append(uri).append("</em></p>");
+ response.append("This page is a result of the request: <pre>");
+ response.append(request).append("</pre></body></html>");
+ addDataHeaders(headers, response);
+ std::string location("Location: ");
+ if (strncmp(uri.c_str(), "http://", 7) != 0)
+ location.append("http://").append(LookupTag(request, "Host")).append("/").append(uri);
+ else
+ location.append(uri);
+ headers.push_back(location);
+ sendHead(client, httpcode, headers);
+ if (content == true)
+ sendData(client, response);
+}
+ /*}}}*/
// sendDirectoryLisiting /*{{{*/
int filter_hidden_files(const struct dirent *a) {
if (a->d_name[0] == '.')
}
return strcasecmp((*a)->d_name, (*b)->d_name);
}
-void sendDirectoryListing(int const client, string const &dir, string const &request, bool content) {
+void sendDirectoryListing(int const client, std::string const &dir, std::string const &request, bool content) {
std::list<std::string> headers;
std::ostringstream listing;
std::string filename(dir);
filename.append("/").append(namelist[i]->d_name);
stat(filename.c_str(), &fs);
- listing << "<tr><td>" << ((S_ISDIR(fs.st_mode)) ? 'd' : 'f') << "</td>"
- << "<td><a href=\"" << namelist[i]->d_name << "\">" << namelist[i]->d_name << "</a></td>";
- if (S_ISDIR(fs.st_mode))
- listing << "<td>-</td>";
- else
- listing << "<td>" << SizeToStr(fs.st_size) << "B</td>";
+ if (S_ISDIR(fs.st_mode)) {
+ listing << "<tr><td>d</td>"
+ << "<td><a href=\"" << namelist[i]->d_name << "/\">" << namelist[i]->d_name << "</a></td>"
+ << "<td>-</td>";
+ } else {
+ listing << "<tr><td>f</td>"
+ << "<td><a href=\"" << namelist[i]->d_name << "\">" << namelist[i]->d_name << "</a></td>"
+ << "<td>" << SizeToStr(fs.st_size) << "B</td>";
+ }
listing << "<td>" << TimeRFC1123(fs.st_mtime) << "</td></tr>" << std::endl;
}
listing << "</table></body></html>" << std::endl;
}
size_t const filestart = m->find(' ', 5);
- string filename = m->substr(5, filestart - 5);
+ std::string filename = m->substr(5, filestart - 5);
if (filename.empty() == true)
filename = ".";
+ else
+ filename = DeQuoteString(filename);
if (simulate_broken_server == true) {
- string data("ni ni ni\n");
+ std::string data("ni ni ni\n");
addDataHeaders(headers, data);
sendHead(client, 200, headers);
sendData(client, data);
sendFile(client, data);
}
else if (DirectoryExists(filename) == true) {
- sendDirectoryListing(client, filename, *m, sendContent);
+ if (filename == "." || filename[filename.length()-1] == '/')
+ sendDirectoryListing(client, filename, *m, sendContent);
+ else
+ sendRedirect(client, 301, filename.append("/"), *m, sendContent);
}
else
sendError(client, 404, *m, false);