Added process directory

This commit is contained in:
Javier Velilla
2011-05-21 12:46:45 -03:00
parent dfa30471e3
commit af852498be

View File

@@ -1,112 +1,145 @@
class GET_REQUEST_HANDLER class
GET_REQUEST_HANDLER
inherit
inherit
SHARED_DOCUMENT_ROOT SHARED_DOCUMENT_ROOT
SHARED_URI_CONTENTS_TYPES SHARED_URI_CONTENTS_TYPES
HTTP_REQUEST_HANDLER HTTP_REQUEST_HANDLER
HTTP_CONSTANTS HTTP_CONSTANTS
feature create
default_create
process
-- process the request and create an answer feature
local
fname: STRING process
f: RAW_FILE -- process the request and create an answer
ctype, extension: STRING local
do fname: STRING_8
create answer.make f: RAW_FILE
if request_uri.is_equal ("/") then ctype, extension: STRING_8
process_default do
answer.set_content_type ("text/html") create answer.make
else if request_uri.is_equal ("/") then
fname := document_root_cell.item.twin process_default
fname.append (request_uri) answer.set_content_type ("text/html")
debug else
print ("URI name: " + fname ) fname := Document_root_cell.item.twin
end fname.append (request_uri)
create f.make (fname) debug
create answer.make print ("URI name: " + fname)
if f.exists then end
extension := ct_table.extension (request_uri) create f.make (fname)
ctype := ct_table.content_types.item (extension) create answer.make
if ctype = Void then if f.exists then
process_raw_file (f) extension := Ct_table.extension (request_uri)
answer.set_content_type ("text/html") ctype := Ct_table.content_types.item (extension)
else if f.is_directory then
if ctype.is_equal ("text/html") then process_directory (f)
process_text_file (f) else
else if ctype = Void then
process_raw_file (f) process_raw_file (f)
end answer.set_content_type ("text/html")
answer.set_content_type (ctype) else
end if ctype.is_equal ("text/html") then
else process_text_file (f)
answer.set_status_code (not_found) else
answer.set_reason_phrase (not_found_message) process_raw_file (f)
answer.set_reply_text ("Not found on this server") end
end answer.set_content_type (ctype)
end end
answer.set_content_length (answer.reply_text.count.out) end
end else
answer.set_status_code (Not_found)
process_default answer.set_reason_phrase (Not_found_message)
-- Return a defaul response answer.set_reply_text ("Not found on this server")
local end
html : STRING end
do answer.set_content_length (answer.reply_text.count.out)
answer.set_reply_text ("") end
html := " <html> <head> <title> NINO HTTPD </title> " +
" </head> " + process_default
" <body> " + -- Return a defaul response
" <h1> Welcome to NINO HTTPD! </h1> "+ local
" <p> Default page " + html: STRING_8
do
" </p> " + answer.set_reply_text ("")
" </body> " + html := " <html> <head> <title> NINO HTTPD </title> " + " </head> " + " <body> " + " <h1> Welcome to NINO HTTPD! </h1> " + " <p> Default page " + " </p> " + " </body> " + " </html> "
" </html> " answer.append_reply_text (html)
answer.append_reply_text (html) end
end
process_text_file (f: FILE)
-- send a text file reply
process_text_file (f: FILE) require
-- send a text file reply valid_f: f /= Void
require do
valid_f: f /= Void f.open_read
do from
f.open_read answer.set_reply_text ("")
from f.read_line
answer.set_reply_text ("") until
f.read_line f.end_of_file
until f.end_of_file loop
loop answer.append_reply_text (f.last_string)
answer.append_reply_text (f.last_string) answer.append_reply_text (Crlf)
answer.append_reply_text (crlf) f.read_line
f.read_line end
end f.close
f.close end
end
process_raw_file (f: FILE)
process_raw_file (f: FILE) -- send a raw file reply
-- send a raw file reply require
require valid_f: f /= Void
valid_f: f /= Void do
do f.open_read
-- this is not quite right.... from
f.open_read answer.set_reply_text ("")
from until
answer.set_reply_text ("") f.end_of_file
until f.end_of_file loop
loop f.read_stream_thread_aware (1024)
f.read_stream_thread_aware (1024) answer.append_reply_text (f.last_string)
answer.append_reply_text (f.last_string) end
end f.close
f.close end
end
process_directory (f: FILE)
--read the directory
end require
is_directory: f.is_directory
local
l_dir: DIRECTORY
files: ARRAYED_LIST [STRING_8]
html1: STRING_8
html2: STRING_8
htmldir: STRING_8
path: STRING_8
index: INTEGER_32
do
answer.set_reply_text ("")
html1 := " <html> <head> <title> NINO HTTPD </title> " + " </head> " + " <body> " + " <h1> Welcome to NINO HTTPD! </h1> " + " <p> Default page "
html2 := " </p> " + " </body> " + " </html> "
path := f.name.twin
index := path.last_index_of ('/', path.count)
path.remove_substring (1, index)
create l_dir.make_open_read (f.name)
files := l_dir.linear_representation
from
files.start
htmldir := "<ul>"
until
files.after
loop
htmldir := htmldir + "<li><a href=%"./" + path + "/" + files.item_for_iteration + "%">" + files.item_for_iteration + "</a> </li>%N"
files.forth
end
htmldir := htmldir + "</ul>"
answer.append_reply_text (html1 + htmldir + html2)
end
end -- class GET_REQUEST_HANDLER