Difference between revisions of "Ns returnfile"

From AOLserver Wiki
Jump to navigation Jump to search
(Added a clearer explanation of the fastpath caching issue)
Line 1: Line 1:
ns_returnfile 200 "application/zip" $filepath
+
ns_returnfile 200 "application/zip" $filepath
 
  ns_returnfile 200 [ns_guesstype $file] $filepath
 
  ns_returnfile 200 [ns_guesstype $file] $filepath
  
 
==== Caveat programmor ====
 
==== Caveat programmor ====
  
ns_returnfile uses [[fastpath]] internally, so care should be taken when using it to return dynamically generated files.  A better practice if dealing with dynamic data in the filesystem would probably be to use [[ns_return]] instead.  For example:
+
ns_returnfile uses [[fastpath]] caching internally, and fastpath caching may fail to distinguish between two files on the same filesystem which have the same inode, mtime, and size (serving one file in lieu of the other)This can happen if a file is generated, returned to the user, and then deleted.  For example, in the following sample code the second call to ns_returnfile will return /var/tmp/myfile rather than /var/tmp/myotherfile:
 +
 
 +
set file [open "/var/tmp/myfile" "w"]
 +
puts $file "ABC123"
 +
close $file
 +
ns_returnfile 200 text/plain "/var/tmp/myfile"
 +
ns_unlink -nocomplain "/var/tmp/myfile"
 +
 +
set file [open "/var/tmp/myotherfile" "w"]
 +
puts $file "XYZ987"
 +
close $file
 +
ns_returnfile 200 text/plain "/var/tmp/myotherfile"
 +
ns_unlink -nocomplain "/var/tmp/myotherfile"
 +
 
 +
When returning "dynamic" file data like this, a safer practice would be to use [[ns_return]] or ns_returnfp instead.  For example:
  
 
  set fd [open $myfile]
 
  set fd [open $myfile]

Revision as of 23:05, 19 August 2008

ns_returnfile 200 "application/zip" $filepath
ns_returnfile 200 [ns_guesstype $file] $filepath

Caveat programmor

ns_returnfile uses fastpath caching internally, and fastpath caching may fail to distinguish between two files on the same filesystem which have the same inode, mtime, and size (serving one file in lieu of the other). This can happen if a file is generated, returned to the user, and then deleted. For example, in the following sample code the second call to ns_returnfile will return /var/tmp/myfile rather than /var/tmp/myotherfile:

set file [open "/var/tmp/myfile" "w"]
puts $file "ABC123"
close $file
ns_returnfile 200 text/plain "/var/tmp/myfile"
ns_unlink -nocomplain "/var/tmp/myfile"

set file [open "/var/tmp/myotherfile" "w"]
puts $file "XYZ987"
close $file
ns_returnfile 200 text/plain "/var/tmp/myotherfile"
ns_unlink -nocomplain "/var/tmp/myotherfile"

When returning "dynamic" file data like this, a safer practice would be to use ns_return or ns_returnfp instead. For example:

set fd [open $myfile]
ns_unlink $myfile; # So that nothing else can access the file from the filesystem
ns_return 200 [ns_guesstype $myfile] [read $fd]
close $fd

Want to name the file returned?

ns_set update [ns_conn outputheaders] content-disposition "attachment; filename=1.abc"