NaviServer - programmable web server
4.99  5.0

[ Main Table Of Contents | Table Of Contents | Keyword Index ]

ns_serverrootproc(n) 4.99.30 naviserver "NaviServer Built-in Commands"

Name

ns_serverrootproc - Callback for determining server root

Table Of Contents

Synopsis

Description

This command can be used to set a callback for determining the root directory of the server based on, e.g., the host header field of the current connection.

COMMANDS

ns_serverrootproc script ?args?

The script registered with ns_serverrootproc is registered for a server. At the time of the registry, some client data might be passed as arguments.

EXAMPLES

Since the command ns_serverrootproc requires a defined server, it cannot be written plainly to the configuration file (defining potentially multiple servers). However, it is possible to use the parameter initcmd in the section tcl of the server (here default) as shown below.

 ns_section ns/server/default/tcl {
   ns_param initcmds {
     #
     # Use the Tcl namespace "::default" for the "default" server.
     #
     namespace eval ::default {
       proc serverroot {args} {
         #
         # Don't call [ns_server pagedir] here, since this
         # would lead to an infinite loop causing an exception.
         #
         set rootDir [ns_server serverdir]
         #
         # Accessing the host header field requires an
         # active connection.
         #
         if {[ns_conn isconnected]} {
           set rawHost [ns_set iget [ns_conn headers] host]
           ns_log notice "serverroot <$args>" \
              server  '[ns_info server]' \
              host    '$rawHost' \
              rootdir '$rootDir'
           #
           # Check, if there is a directory containing the
           # host in the name. One might want to normalize
           # the raw host header field.
           #
           if {$rawHost ne "" && [::file isdirectory $rootDir/$rawHost]} {
             set rootDir $rootDir/$rawHost
           }
           ns_log notice "... final rootdir '$rootDir'"
         }
         return $rootDir
       }
       ns_serverrootproc [namespace current]::serverroot
     }
   }
 }

See Also

ns_conn, ns_locationproc, ns_server, ns_set

Keywords

callback, global built-in, hosting, virtual