combined logfile format and redirect dirs

Gerrit Pape <[email protected]> Wed, 10 Mar 2004 13:00:07 +0000
Newsgroups gmane.comp.web.fnord
Message-ID <[email protected]>
Hi, I've seen common logfile format (COLF) implemented in cvs.  The
following patch extends this to combined logfile format[0], and also
adds code to redirect requests to directories without a trailing slash.
It seems to work fine for me.

Regards, Gerrit.

[0] http://httpd.apache.org/docs/mod/mod_log_config.html

Index: httpd.c
===================================================================
RCS file: /cvs/httpd/httpd.c,v
retrieving revision 1.84
diff -u -r1.84 httpd.c
--- httpd.c	2 Mar 2004 21:31:18 -0000	1.84
+++ httpd.c	10 Mar 2004 13:29:37 -0000
@@ -24,6 +24,10 @@
 #include "byte.h"
 #include "scan.h"
 
+#define REDIRECTDIR
+#define COLF
+#define COLF_COMBINED
+
 /* uncomment the following line to enable support for CGI */
 // #define CGI
 
@@ -80,7 +84,7 @@
 
 /* uncomment the following line to make fnord support connection
  * keep-alive */
-#define KEEPALIVE
+// #define KEEPALIVE
 
 /* the following is the time in seconds that fnord should wait for a
  * valid HTTP request */
@@ -146,7 +150,9 @@
 unsigned long post_mlen;
 unsigned long post_len=0;
 #endif
-
+#ifdef REDIRECTDIR
+char *protourl;
+#endif
 #if _FILE_OFFSET_BITS == 64
 static unsigned long long rangestart, rangeend;	/* for ranged queries */
 #define scan_range scan_ulonglong
@@ -214,12 +220,21 @@
   case HEAD: buffer_puts(buffer_2,"HEAD "); break;
   default: buffer_puts(buffer_2,"? "); break;
   }
-  buffer_puts(buffer_2,url);
+  if (url) buffer_puts(buffer_2,url); else buffer_puts(buffer_2,"(null)");
   buffer_puts(buffer_2,httpversion?" HTTP/1.1\" ":" HTTP/1.0\" ");
   buffer_putulong(buffer_2,retcode);
   buffer_putspace(buffer_2);
-  buffer_putulong(buffer_2,len);
-
+  buffer_putulong(buffer_2,(method==HEAD)?0:len);
+#ifdef COLF_COMBINED
+  buffer_puts(buffer_2," \"");
+  if (!refer) refer="-";
+  sanitize(refer);
+  buffer_puts(buffer_2,refer);
+  buffer_puts(buffer_2,"\" \"");
+  sanitize(ua);
+  buffer_puts(buffer_2,ua);
+  buffer_puts(buffer_2,"\"");
+#endif
 #else
   buffer_puts(buffer_2,remote_ip?remote_ip:"0.0.0.0");
   buffer_putspace(buffer_2);
@@ -846,8 +861,14 @@
   }
   if ((fd=open(url,O_RDONLY))>=0) {
     if (fstat(fd,&st)) goto bad;
+#ifdef REDIRECTDIR
+    /* redirect directories */
+    if (S_ISDIR(st.st_mode))
+      { retcode=301; goto bad; }
+#else
     /* no directories */
     if (S_ISDIR(st.st_mode)) goto bad;
+#endif
     /* see if the peer accepts MIME type */
     /* see if the document has been changed */
     ims=parsedate(header(buf,buflen,"If-Modified-Since"));
@@ -1031,7 +1052,33 @@
   }
 }
 #endif
+#ifdef REDIRECTDIR
+static void handledirslash(const char *origurl) {
+  char *p=(char*)origurl;
+  int i;
 
+  while (*p=='/') ++p;
+#ifdef NORMALIZE_HOST
+  {
+    unsigned int l=str_len(host);
+    char *e=host+l;
+    if (*--e=='0' && *--e=='8' && *--e==':') *e=0;
+  }
+#endif
+  redirectboilerplate();
+  buffer_puts(buffer_1,protourl);
+  buffer_puts(buffer_1,host);
+  buffer_puts(buffer_1,"/");
+  if (*p) {
+    buffer_puts(buffer_1,p);
+    buffer_puts(buffer_1,"/");
+  }
+  buffer_puts(buffer_1,"\r\n\r\n");
+  dolog(0);
+  buffer_flush(buffer_1);
+  exit(0);
+}
+#endif
 #ifdef INDEX_CGI
 static int handleindexcgi(const char *testurl,const char* origurl) {
   unsigned int ul,ol=str_len(origurl);
@@ -1251,6 +1298,9 @@
 #endif
   signal(SIGPIPE,SIG_IGN);
   get_ucspi_env();
+#ifdef REDIRECTDIR
+  if (! (protourl=getenv("PROTOURL"))) protourl="http://";
+#endif
 
 #ifdef KEEPALIVE
 handlenext:
@@ -1653,6 +1703,9 @@
     handledirlist(origurl);
 #endif
     badrequest(404,"Not Found","<title>Not Found</title>No such file or directory.");
+#ifdef REDIRECTDIR
+  case 301: handledirslash(origurl);
+#endif
   case 406: badrequest(406,"Not Acceptable","<title>Not Acceptable</title>Nothing acceptable found.");
   case 416: badrequest(416,"Requested Range Not Satisfiable","");
   case 304: badrequest(304,"Not Changed","");
-- 
Open projects at http://smarden.org/pape/.