From ecb3d8adb387920342907a8ed917d3ed0c405e21 Mon Sep 17 00:00:00 2001 From: Emil Mikulic Date: Sat, 13 Dec 2003 08:45:06 +0000 Subject: [PATCH] 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(). --- trunk/darkhttpd.c | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/trunk/darkhttpd.c b/trunk/darkhttpd.c index ee517f7..decf1fa 100644 --- a/trunk/darkhttpd.c +++ b/trunk/darkhttpd.c @@ -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, - "%s\n" - "

%s

\n" - "
\n",
-     conn->uri, conn->uri);
+    append(listing, "\n\n ");
+    append(listing, conn->uri);
+    append(listing, "\n\n\n

"); + append(listing, conn->uri); + append(listing, "

\n
\n");
+
+    spaces = xmalloc(maxlen);
+    memset(spaces, ' ', maxlen);
 
     for (i=0; i%s",
-            list[i]->name, list[i]->name);
+        append(listing, "name);
+        append(listing, "\">");
+        append(listing, list[i]->name);
+        append(listing, "");
 
         if (list[i]->is_dir)
             append(listing, "/\n");
         else
         {
-            int j;
-            for (j=strlen(list[i]->name); jname));
             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,
      "
\n" "
\n" - "Generated by %s on %s\n" - "\n", - pkgname, date); + "Generated by "); + append(listing, pkgname); + append(listing, " on "); + append(listing, date); + append(listing, "\n\n\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);