NaviServer - programmable web server
4.99  5.0

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

ns_serverrootproc(n) 5.0.0a 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 ?arg ...?

Register a script which returns the location of the server root directory (directory path).

When the optional arguments are provided, these are added to the command script to be executed. Therefore, two invocation styles are supported: either providing a script, or a command with additional 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 get -nocase [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, virtual host