# Root directory, were all huge files are located set root /tmp/examples/ # Name of current server set server [ns_info server] # get query parameter for "file" and "create" set file [ns_queryget file] set create [ns_queryget create] # Create test files if { $create == 1 } { if { [catch { file mkdir $root set fd [open $root/test1.dat w] puts -nonewline $fd [string repeat "test" 1000000] close $fd } errmsg] } { ns_log error "writer.tcl: $errmsg" } } append data "

Writer Example

" append data \ "Writer threads can be used to spool potentially huge files
" \ "to the client using asynchronous I/O. One writer thread can serve
" \ "many clients concurrently with little resource consumptions.
" \ "By using writer threads, connections thread are used for only short
" \ "time periods. Furthermore, this helps against slow read attacks.

" set writerthreads [ns_config ns/server/$server/module/nssock writerthreads] append data \ "The current server (named $server) " \ "is configured with $writerthreads writer thread(s).
" \ "You can change the number of writer threads in the used configuration file
" \ "[ns_info config].

The configuration file contains entries like the following:

" \ "

   ns_section ns/server/$server/module/nssock\n   ns_param writerthreads $writerthreads\n" \
    "
" # Show all files if { $file eq "" } { set files [lsort [glob -nocomplain $root/*.dat]] if { [llength $files] == 0 } { append data \ "No test files exist in the configured TMP folder $root.

" \ "Do you want to create them? Yes" } else { append data "List of available files in $root:

" } append data \ "When downloading huge files via writer threads, the size of the NaviServer thread
" \ "will stay small.

Back to example page.
" ns_return 200 text/html $data return } ns_log notice "check exist [ns_normalizepath $root/$file]" # Check if it exists set path [ns_normalizepath $root/$file] if { ![file exists $path] } { ns_returnnotfound return } # Let the writer to handle headers and size set rc [ns_writer submitfile -headers $path] ns_log notice "call submitfile $path -> $rc" if { $rc } { ns_returnnotfound } ns_log Notice "file $file has been submitted: $rc" # # Local variables: # mode: tcl # tcl-indent-level: 4 # indent-tabs-mode: nil # End: