Optimize generate_dir_listing():

. Split appendf() calls into a number of append() calls.
. To insert spaces, instead of sitting in a loop, use a spaces[] buffer
  and insert a chunk of it with appendl().
This commit is contained in:
Emil Mikulic 2003-12-13 08:45:06 +00:00
parent 4ce4eabdbd
commit ecb3d8adb3

View File

@ -1641,7 +1641,7 @@ static void cleanup_sorted_dirlist(struct dlent **list, const ssize_t size)
*/
static void generate_dir_listing(struct connection *conn, const char *path)
{
char date[DATE_LEN];
char date[DATE_LEN], *spaces;
struct dlent **list;
ssize_t listsize;
size_t maxlen = 0;
@ -1662,39 +1662,45 @@ static void generate_dir_listing(struct connection *conn, const char *path)
if (maxlen < tmp) maxlen = tmp;
}
appendf(listing,
"<html><head><title>%s</title></head><body>\n"
"<h1>%s</h1>\n"
"<tt><pre>\n",
conn->uri, conn->uri);
append(listing, "<html>\n<head>\n <title>");
append(listing, conn->uri);
append(listing, "</title>\n</head>\n<body>\n<h1>");
append(listing, conn->uri);
append(listing, "</h1>\n<tt><pre>\n");
spaces = xmalloc(maxlen);
memset(spaces, ' ', maxlen);
for (i=0; i<listsize; i++)
{
appendf(listing, "<a href=\"%s\">%s</a>",
list[i]->name, list[i]->name);
append(listing, "<a href=\"");
append(listing, list[i]->name);
append(listing, "\">");
append(listing, list[i]->name);
append(listing, "</a>");
if (list[i]->is_dir)
append(listing, "/\n");
else
{
int j;
for (j=strlen(list[i]->name); j<maxlen; j++)
append(listing, " ");
appendl(listing, spaces, maxlen-strlen(list[i]->name));
appendf(listing, "%10d\n", list[i]->size);
}
}
cleanup_sorted_dirlist(list, listsize);
safefree(list);
safefree(spaces);
(void)rfc1123_date(date, time(NULL));
appendf(listing,
append(listing,
"</pre></tt>\n"
"<hr>\n"
"Generated by %s on %s\n"
"</body></html>\n",
pkgname, date);
"Generated by ");
append(listing, pkgname);
append(listing, " on ");
append(listing, date);
append(listing, "\n</body>\n</html>\n");
conn->reply = listing->str;
conn->reply_length = listing->length;
@ -1705,7 +1711,7 @@ static void generate_dir_listing(struct connection *conn, const char *path)
"Date: %s\r\n"
"Server: %s\r\n"
"%s" /* keep-alive */
"Content-Length: %d\r\n"
"Content-Length: %u\r\n"
"Content-Type: text/html\r\n"
"\r\n",
date, pkgname, keep_alive(conn), conn->reply_length);