35 #include <boost/date_time.hpp>
36 #include <boost/lexical_cast.hpp>
38 using namespace boost::filesystem;
40 using namespace tfc::utils;
42 using boost::lexical_cast;
44 namespace MIME = tfc::RegistreTipusMIME;
56 inline string mtime(
const time_t & t) {
60 string mida(
const size_t s) {
62 const size_t mb = s / (1<<20);
63 const size_t kb = s % (1<<20);
64 return lexical_cast<
string>(mb)+
"."+lexical_cast<string>(kb)[0]+
" MiB";
66 else if (s > (1<<10)) {
67 const size_t kb = s / (1<<10);
68 const size_t b = s % (1<<10);
69 return lexical_cast<
string>(kb)+
"."+lexical_cast<string>(b)[0]+
" KiB";
71 return lexical_cast<
string>(s)+
" B";
78 string AutoIndex::html()
const {
79 if (!cache_.empty()) {
85 const bool barra_final = ( !url_.ruta().empty() &&
'/' ==
utils::final(url_.ruta()));
86 const string base_dir = url_.ruta() + ( barra_final ?
"" :
"/");
97 ss <<
"<!DOCTYPE html>\n"
99 "<head><title>Índex per a " << base_dir <<
"</title></head>\n"
101 "<h1>Índex per a " << base_dir <<
"</h1>\n"
115 paths.push_back(
path(
".."));
116 copy(directory_iterator(directori()), directory_iterator(), back_inserter(paths));
117 sort(paths.begin(), paths.end());
121 const string str_dir = string(
AMPLE_MIDA-5,
' ')+
"<DIR>";
123 for (
auto it = paths.begin(); it != paths.end(); ++it) {
128 const string fn = it->filename().string();
129 const bool es_dir = is_directory(*it);
130 const string str_mida = (es_dir ? str_dir :
mida(file_size(*it)));
131 const string data =
mtime(last_write_time(*it));
132 const string extra = (es_dir ?
"/" :
"");
134 const string url = base_dir + fn;
139 <<
" <a href=\"" << url <<
"\">"
140 << fn << extra <<
"</a>"
146 "<address>" << Servidor::signatura()
147 <<
". Servidor " << host_.address() <<
", port " << host_.port()
155 void AutoIndex::envia(boost::asio::ip::tcp::socket & socket)
const
156 throw (runtime_error)
158 this->envia(socket, html());
163 const string content_length = lexical_cast<
string>(html().length());
164 capsaleres.push_back( make_pair(
"Content-Length", content_length) );
167 capsaleres.push_back( make_pair(
"Content-Type",
"text/html; charset=UTF-8") );