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