1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <sys/types.h>
  4 #include <sys/stat.h>
  5 #include <fcntl.h>
  6 #include <unistd.h>
  7 #include "rhttpd.h"
  8 #include "handler.h"
  9 #include "http_request.h"
 10 #include "http_host.h"
 11 #include "rh_string.h"
 12 #include "config.h"
 13 #include "client.h"
 14 #include "server.h"
 15 #include "log.h"
 16 
 17 #define TEST(_call)     if (0 != _call) return HANDLER_FAILURE
 18 
 19 DECLARE_HANDLER_FUNCTION (vhost,exec)
 20 {
 21         rh_buffer_t             *filename;
 22         size_t                  size;
 23         http_header_entry_t     *key_host;
 24 
 25         const char              *hostname;
 26         size_t                  nhostname;
 27 
 28         const config_value_t    *config;
 29         http_request_t  *request;
 30 
 31 #ifdef RHTTPD_DUMP_HANDLER_CALL
 32         DEBUGLOG("handler(%p)", (void*)handler);
 33 #endif
 34 
 35         request = handler->request;
 36 
 37         size = CONST_LEN(RHTTPD_WWW_ROOT); /* server->config .. */
 38 
 39 #ifdef RHTTPD_HTDOCS
 40         size += CONST_LEN(RHTTPD_HTDOCS);
 41 #endif
 42 
 43         config = CONFIG_POP_VALUE (request->client->server->config,
 44                         VIRTUAL_DOCUMENT_ROOT);
 45         
 46         if (NULL ==config)
 47                 return HANDLER_FAILURE;
 48 #if 0   
 49         printf ("%s(): CONFIG[%.*s]\n", __FUNCTION__,
 50                                 config->value.used,
 51                                 config->value.data );
 52 #endif
 53         config_push_value (config);
 54 
 55 
 56 
 57         key_host = http_header_key_get (&request->header_in,
 58                         HTTP_HEADER_KEY_HOST );
 59 
 60         if (key_host && key_host->found && key_host->data) {
 61                 http_host_t     *host = key_host->data;
 62 
 63                 hostname = host->host.data;
 64                 nhostname = host->host.used;
 65         
 66         } else {
 67 #if 0
 68                 config = CONFIG_POP_VALUE (request->client->server->config,
 69                                 SERVER_NAME);
 70                 
 71                 if (NULL ==config)
 72                         return HANDLER_FAILURE;
 73 #endif
 74 
 75                 /* server->config("default hostname") */
 76                 hostname = "localhost";
 77                 nhostname = CONST_LEN("localhost");
 78         }
 79 
 80         size += nhostname + request->header_in.uri.path.used + 1;
 81 
 82         /* make physical filename */
 83         
 84         filename = &request->header_in.uri.filename;
 85 
 86         TEST( rh_buffer_reserve (filename, size) );
 87 
 88         /* root directory */
 89         TEST( rh_buffer_append (filename, CONST_STR_LEN(RHTTPD_WWW_ROOT) ) );
 90 
 91         /* vhost directory */
 92         TEST( rh_buffer_append (filename, hostname, nhostname) );
 93 
 94         /* htdocs */
 95 #ifdef RHTTPD_HTDOCS
 96         /* server->config() */
 97         TEST( rh_buffer_append (filename, CONST_STR_LEN(RHTTPD_HTDOCS) ) );
 98 #endif
 99 
100         /* requested path */
101         TEST( rh_buffer_append (filename,
102                         request->header_in.uri.path.data,
103                         request->header_in.uri.path.used ) );
104         
105         filename->data[filename->used] = 0;
106         
107         return HANDLER_SUCCESS;
108 }
109 
110 DECLARE_HANDLER_BEGIN (vhost, REWRITE)  
111         .DECLARE_HANDLER_NULL   (vhost, setup),
112         .DECLARE_HANDLER_NULL   (vhost, init),
113         .DECLARE_HANDLER_NULL   (vhost, init_global),
114         .DECLARE_HANDLER_NULL   (vhost, free),
115         .DECLARE_HANDLER_NULL   (vhost, free_global),
116         .DECLARE_HANDLER_SYMBOL (vhost, exec),
117 DECLARE_HANDLER_END


syntax highlighted by Code2HTML, v. 0.9.1