Guest User

Untitled

a guest
Mar 1st, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
TCL 1.10 KB | None | 0 0
  1. proc on404 {method resource} {
  2.     echo "<h1>404 Sorry</h1>"
  3. }
  4.  
  5. proc webcall {method resource} {
  6.     global mime_types
  7.  
  8.     set dir_list [split $resource /]
  9.  
  10.     foreach dir $dir_list {
  11.         if {[string compare $dir ".."]==0} {
  12.             echo "Nope Nope Nope Nope"
  13.             return
  14.         }
  15.     }
  16.  
  17.     unset dir_list
  18.  
  19.     if {[string compare $resource "/"]==0} {
  20.         set resource "./static/index.html"
  21.     } else {
  22.         set resource "./static$resource"
  23.     }
  24.  
  25.     if {[file exists $resource] && [file isfile $resource]} {
  26.         set fd [open $resource r]
  27.         set data [read $fd]
  28.        
  29.         set extension_find [split $resource "."]
  30.  
  31.         if {[dict exists $mime_types [lindex $extension_find end]]} {
  32.             response header "HTTP/1.1 200 OK\r\nContent-Type: [dict get $mime_types [lindex $extension_find end]]\r\nConncetion: close\r\nContent-Length: [string length $data]\r\n\r\n"
  33.         } else {
  34.             response header "HTTP/1.1 200 OK\r\nContent-Type: application/octet-stream\r\nConncetion: close\r\nContent-Length: [string length $data]\r\n\r\n"
  35.         }  
  36.         echo $data
  37.  
  38.         close $fd
  39.         unset data
  40.         unset extension_find
  41.         unset resource
  42.     } else {
  43.         on404 $method $resource
  44.     }
  45. }
Add Comment
Please, Sign In to add comment