[prev in list] [next in list] [prev in thread] [next in thread] 

List:       thttpd
Subject:    [THTTPD]
From:       kevin graham <kgraham () dotnetdotcom ! org>
Date:       2001-03-06 21:25:13
[Download RAW message or body]

Here's another patch I made that I figured would be worthy of general
consumption. On our image servers, we've got a few vhosts defined, but a
fair number of requests are still directed with an ip addr or have no
Host: header at all..

To spare having to create symlinks to accomodate this, I wanted to have
thttpd fall back to the 'default' document tree if a vhost-specific
directory didn't exist..

Everything is wrapped nicely in #define's, so this is a net 0 effect if
applied and disabled via config.h

enjoy.

..kg..

(Andrew -- I've directly cc'ed you incase the attachment gets mangled
again. Let me know if there's any problems).

default_vhost.patch - Enables a fallback for non-existant vhosts
===================================================

Author:         Kevin Graham <kgraham@dotnetdotcom.org>

Applies to:     thttpd 2.20b (untested, but should apply against 2.20
                fine)

Requires:       No special requirements

Apply with:     patch -d thttpd-2.20b < default_vhost.patch

Changes:        In the case that a directory for a virtual host does not
                exist under the document root, this will cause thttpd to
                default to the normal document root. This should save some
                ugly symlink trees in the case that you're using vhosts,
                but still have a fair number of requests coming in w/o
                proper Host: headers.

                The only caveat is that it adds an extra stat() (lstat,
                actually) in the course of request processing. This
                shouldn't impact performance (since presumably this will
                work its way into a prominent spot in the kernel's inode
                cache), but I figured its worth mentioning.


["default_vhost.patch" (TEXT/PLAIN)]

diff -rc thttpd-2.20b-orig/config.h thttpd-2.20b/config.h
*** thttpd-2.20b-orig/config.h	Tue Sep 26 14:31:16 2000
--- thttpd-2.20b/config.h	Tue Mar  6 13:17:44 2001
***************
*** 181,186 ****
--- 181,202 ----
  #define ALWAYS_VHOST
  #endif
  
+ /* CONFIGURE: Check to make sure a vhost-specific directory actually exists
+ ** before using it. By default, thttpd will do this blindly; with this defined,
+ ** a missing directory will cause it to default to the default document root.
+ */
+ #define CHECK_VHOST
+ 
+ /* CONFIGURE: Used in conjunction with CHECK_VHOST. If a vhost subdirectory
+ ** does not exist, then an http status 500 is returned. If this is left
+ ** undefined, then the normal behavior of returning a 404 is preserved. This
+ ** should probably be left undefined (I haven't thoroughly checked the
+ ** ramifications of map_vhost(*http_conn) returning non-true).
+ */
+ #ifdef notdef
+ #define STRICT_VHOST
+ #endif
+ 
  /* CONFIGURE: If you're using the vhost feature and you have a LOT of
  ** virtual hostnames (like, hundreds or thousands), you will want to
  ** enable this feature.  It avoids a problem with most Unix filesystems,
Only in thttpd-2.20b: default_vhost.patch
diff -rc thttpd-2.20b-orig/libhttpd.c thttpd-2.20b/libhttpd.c
*** thttpd-2.20b-orig/libhttpd.c	Wed Sep 27 11:13:24 2000
--- thttpd-2.20b/libhttpd.c	Tue Mar  6 13:20:11 2001
***************
*** 1166,1171 ****
--- 1166,1175 ----
      {
      httpd_sockaddr sa;
      int sz;
+ #ifdef CHECK_VHOST
+     struct stat dirstat;
+     char* temphostdir;
+ #endif CHECK_VHOST
      static char* tempfilename;
      static int maxtempfilename = 0;
      char* cp1;
***************
*** 1197,1202 ****
--- 1201,1210 ----
  
      if ( hc->tildemapped )
  	return 1;
+    
+ #ifdef CHECK_VHOST 
+     temphostdir = hc->hostdir;
+ #endif CHECK_VHOST
  
      /* Figure out the host directory. */
  #ifdef VHOST_DIRLEVELS
***************
*** 1217,1222 ****
--- 1225,1241 ----
      httpd_realloc_str( &hc->hostdir, &hc->maxhostdir, strlen( hc->hostname ) );
      (void) strcpy( hc->hostdir, hc->hostname );
  #endif /* VHOST_DIRLEVELS */
+ 
+ #ifdef CHECK_VHOST
+     if (0 != lstat(hc->hostdir, &dirstat)) {
+ #ifdef STRICT_VHOST
+        return -1;
+ #else
+        hc->hostdir = temphostdir;
+        return 1;
+ #endif
+     }
+ #endif CHECK_VHOST
  
      /* Prepend hostdir to the filename. */
      len = strlen( hc->expnfilename );


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic