1
  
2
  
3
  
4
  
5
  
6
  
7
  
8
  
9
  
10
  
11
  
12
  
13
  
14
  
15
  
16
  
17
  
18
  
19
  
20
  
21
  
22
  
23
  
24
  
25
  
26
  
27
  
28
  
29
  
30
  
31
  
32
  
33
  
34
  
35
  
36
  
37
  
38
  
39
  
40
  
41
  
42
  
43
  
44
  
45
  
46
  
47
  
48
  
49
  
50
  
51
  
52
  
53
  
54
  
55
  
56
  
57
  
58
  
59
  
60
  
61
  
62
  
63
  
64
  
65
  
66
  
67
  
68
  
69
  
70
  
71
  
72
  
73
  
74
  
75
  
76
  
77
  
78
  
79
  
80
  
81
  
82
  
83
  
84
  
85
  
86
  
87
  
88
  
89
  
90
  
91
  
92
  
93
  
94
  
95
  
96
  
97
  
98
  
99
  
100
  
101
  
102
  
103
  
104
  
105
  
106
  
107
  
108
  
109
  
110
  
111
  
112
  
113
  
114
  
115
  
116
  
117
  
118
  
119
  
120
  
121
  
122
  
123
  
124
  
125
  
126
  
127
  
128
  
129
  
130
  
131
  
132
  
133
  
134
  
135
  
136
  
137
  
138
  
139
  
140
  
141
  
142
  
143
  
144
  
145
  
146
  
147
  
148
  
149
  
150
  
151
  
152
  
153
  
154
  
155
  
156
  
157
  
 
 
constant days = ({ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }); 
constant months = ({ "Jan", "Feb", "Mar", "Apr", "May", "Jun", 
                     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }); 
 
constant prefix = ({ "bytes", "kB", "MB", "GB", "TB", "HB"}); 
 
string sizetostring( int size ) 
{ 
  int s; 
  if(size<0) 
    return "--------"; 
  s=size*10;  
  size = 0; 
  while( s > 10240 || size==0) 
  { 
    s /= 1024; 
    size ++; 
  } 
  return sprintf("%d.%01d %s", s/10, s%10, prefix[ size ]); 
} 
 
 
// The server root. 
#ifdef __NT__ 
#define BASE "H:\\html\\per.hedbor.org\\" 
#else 
#define BASE "/tmp/" 
#endif 
 
#define PORT 4002 
 
string http_date(int t) 
{ 
  mapping l = gmtime(t); 
  return(sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", 
                 days[l->wday], l->mday, months[l->mon], 1900+l->year, 
                 l->hour, l->min, l->sec)); 
} 
 
 
class request_program  
{ 
  inherit HTTPAccept.prog; 
} 
 
mixed handle(object o) 
{ 
  string url = o->not_query; 
  string f = combine_path(BASE, url[1..]); 
  array|object s; 
  if(f[-1] == '/' && (s = file_stat(f+"index.html"))) { 
    url += "index.html"; 
    f += "index.html"; 
  } else 
    s = file_stat(f); 
  if(!s || s[1] == -1 || search(f, BASE) == -1) 
  { 
    string nofile = "<title>No such file</title>" 
      "<h1>No such file or directory: "+f+"</h1>"; 
    o->reply_with_cache("HTTP/1.0 404 Unknown file\r\n" 
                        "Content-type: text/html\r\n" 
                        "Content-Length: "+sizeof(nofile)+"\r\n" 
                        "MIME-Version: 1.0\r\n" 
                        "Server: Neo-FastSpeed\r\n" 
                        "Connection: Keep-Alive\r\n" 
                        "\r\n" 
                        +nofile, 60); 
  } 
  else if(s[1] == -2)  
  { 
    if(f[-1] == '/' || f[-1]=='\\') 
    { 
      string head = ("HTTP/1.1 200 Ok\r\n" 
                     "Content-type: text/html\r\n" 
                     "Last-Modified: "+http_date(s[4])+"\r\n" 
                     "MIME-Version: 1.0\r\n" 
                     "Server: Neo-FastSpeed\r\n" 
                     "Connection: Keep-Alive\r\n"); 
      string res = ("<title>Directory listing of "+url+"</title>" 
                    "<h1 align=center>Directory listing of "+ 
                    url+"</h1>\n<pre>"); 
      if(url != "/") 
        res += "<b><a href=../>Parent Directory...</a></b>\n\n"; 
      string file2; 
      foreach(sort(get_dir(f)||({})), string file) { 
        int size = Stdio.file_size(f+file); 
        int islink; 
#if constant(Stdio.readlink) 
        catch { Stdio.readlink(f+file); islink = 1; }; 
#endif 
      string ext = (file / ".") [-1]; 
        string ctype = (size >= 0 ? MIME.ext_to_media_type(ext) || "text/plain" : 
                        "Directory"); 
        if(size < -1) 
          file += "/"; 
        if(islink) 
          file2 = "@"+file; 
        else 
          file2 = file; 
        res += sprintf("<a href=%s>%-30s</a> %15s %s\n", 
                       file, file2, sizetostring(size), 
                       ctype); 
      } 
      return o->reply_with_cache(head + 
                                 "Content-Length: "+sizeof(res)+ 
                                 "\r\n\r\n"+res, 30); 
    } else { 
      o->reply_with_cache("HTTP/1.0 302  Redirect\r\n" 
                          "Content-type: text/plain\r\n" 
                          "MIME-Version: 1.0\r\n" 
                          "Server: Neo-FastSpeed\r\n" 
                          "Connection: Keep-Alive\r\n" 
                          "Content-Length: 0\r\n" 
                          "Location: "+url+"/"+ 
                          (o->query ? "?"+o->query : "")+ 
                          "\r\n\r\n", 60); 
    } 
  } else { 
    string ext = (f / ".") [-1]; 
    string ctype = MIME.ext_to_media_type(ext) || "text/plain"; 
    o->reply_with_cache("HTTP/1.1 200 Ok\r\n" 
                        "Content-type: "+ctype+"\r\n" 
                        "Content-Length: "+s[1]+"\r\n" 
                        "Last-Modified: "+http_date(s[4])+"\r\n" 
                        "MIME-Version: 1.0\r\n" 
                        "Server: Neo-FastSpeed\r\n" 
                        "Connection: Keep-Alive\r\n" 
                        "\r\n"+ 
                        Stdio.read_bytes(f), 20); 
    destruct(o); 
  } 
} 
 
object port; 
object l; 
 
int main(int argc, array (string) argv) 
{ 
#if constant(thread_set_concurrency) 
  thread_set_concurrency(100); 
#endif 
  port = Stdio.Port(); 
  if(!port->bind(PORT)) 
  { 
    werror("Bind failed.\n"); 
    return 1; 
  } 
   
  l = HTTPAccept.Loop( port, request_program, handle, 0, 1024*1024, 0,  
                       0); 
  werror("WWW-server listening to port "+PORT+".\n"); 
  return -1; 
}