Added "nino" subtree merged in contrib/library/network/server/nino

This commit is contained in:
Jocelyn Fiat
2012-06-13 22:45:50 +02:00
91 changed files with 3553 additions and 0 deletions

View File

@@ -0,0 +1,172 @@
class
GET_REQUEST_HANDLER
inherit
HTTP_REQUEST_HANDLER
HTTP_SERVER_SHARED_CONFIGURATION
undefine
default_create
end
SHARED_URI_CONTENTS_TYPES
undefine
default_create
end
HTTP_CONSTANTS
undefine
default_create
end
create
make
feature {NONE} -- Initialization
make (a_socket: TCP_STREAM_SOCKET)
do
default_create
socket := a_socket
end
feature -- Access
socket: TCP_STREAM_SOCKET
feature -- Execution
process
-- process the request and create an answer
local
fname: STRING_8
f: RAW_FILE
ctype, extension: detachable STRING_8
do
answer.reset
if script_name.is_equal ("/") then
process_default
answer.set_content_type ("text/html")
else
create fname.make_from_string (Document_root)
fname.append (script_name)
debug
print ("URI filename: " + fname)
end
create f.make (real_filename (fname))
if f.exists then
extension := Ct_table.extension (script_name)
ctype := Ct_table.content_types.item (extension)
if f.is_directory then
process_directory (f)
else
if ctype = Void then
process_raw_file (f)
answer.set_content_type ("text/html")
else
if ctype.is_equal ("text/html") then
process_text_file (f)
else
process_raw_file (f)
end
answer.set_content_type (ctype)
end
end
else
answer.set_status_code (Not_found)
answer.set_reason_phrase (Not_found_message)
answer.set_reply_text ("Not found on this server")
end
end
if attached answer.reply_text as t then
answer.set_content_length (t.count)
else
answer.set_content_length (0)
end
--| Output the result
socket.put_string (answer.reply_header + answer.reply_text)
end
process_default
-- Return a default response
local
html: STRING_8
do
answer.set_reply_text ("")
html := " <html> <head> <title> NINO HTTPD </title> " + " </head> " + " <body> " + " <h1> Welcome to NINO HTTPD! </h1> " + " <p> Default page " + " </p> " + " </body> " + " </html> "
answer.append_reply_text (html)
end
process_text_file (f: FILE)
-- send a text file reply
require
valid_f: f /= Void
do
f.open_read
from
answer.set_reply_text ("")
f.read_line
until
f.end_of_file
loop
answer.append_reply_text (f.last_string)
answer.append_reply_text (Crlf)
f.read_line
end
f.close
end
process_raw_file (f: FILE)
-- send a raw file reply
require
valid_f: f /= Void
do
f.open_read
from
answer.set_reply_text ("")
until
f.end_of_file
loop
f.read_stream_thread_aware (1024)
answer.append_reply_text (f.last_string)
end
f.close
end
process_directory (f: FILE)
--read the directory
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
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 := script_name
if path[path.count] = '/' then
path.remove_tail (1)
end
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

View File

@@ -0,0 +1,115 @@
note
description: "Summary description for {HEAD_REQUEST_HANDLER}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
HEAD_REQUEST_HANDLER
inherit
SHARED_DOCUMENT_ROOT
SHARED_URI_CONTENTS_TYPES
HTTP_REQUEST_HANDLER
HTTP_CONSTANTS
feature
process
-- process the request and create an answer
local
fname: STRING
f: RAW_FILE
ctype, extension: STRING
do
fname := document_root_cell.item.twin
fname.append (request_uri)
debug
print ("URI name: " + fname )
end
create f.make (fname)
create answer.make
if f.exists then
extension := ct_table.extension (request_uri)
ctype := ct_table.content_types.item (extension)
-- TODO: This code could be improved to avoid string
-- comparisons
if ctype = Void then
process_default
answer.set_content_type ("text/html")
else
if ctype.is_equal ("text/html") then
process_text_file (f)
else
process_raw_file (f)
end
answer.set_content_type (ctype)
end
else
answer.set_status_code (not_found)
answer.set_reason_phrase (not_found_message)
answer.set_reply_text ("Not found on this server%N%R")
end
end
process_default
--
local
html : STRING
do
answer.set_reply_text ("")
html := " <html> <head> <title> Micro HTTPD </title> " +
" </head> " +
" <body> " +
" <h1> Welcome to Micro HTTPD! </h1> "+
" <p> Default page " +
" </p> " +
" </body> " +
" </html> "
answer.append_reply_text (html)
end
process_text_file (f: FILE)
-- send a text file reply
require
valid_f: f /= Void
do
f.open_read
from
answer.set_reply_text ("")
f.read_line
until f.end_of_file
loop
answer.append_reply_text (f.last_string)
answer.append_reply_text (crlf)
f.read_line
end
f.close
end
process_raw_file (f: FILE)
-- send a raw file reply
require
valid_f: f /= Void
do
-- this is not quite right....
f.open_read
from
answer.set_reply_text ("")
until f.end_of_file
loop
f.read_stream (1024)
answer.append_reply_text (f.last_string)
end
f.close
end
end

View File

@@ -0,0 +1,53 @@
note
description: "Summary description for {POST_REQUEST_HANDLER}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
POST_REQUEST_HANDLER
inherit
GET_REQUEST_HANDLER
redefine
process
end
create
make
feature -- Execution
process
-- process the request and create an answer
local
l_data: STRING
s: detachable STRING
n: INTEGER
sock: like socket
do
from
n := 1_024
sock := socket
if sock.socket_ok then
sock.read_stream_thread_aware (n)
s := sock.last_string
else
s := Void
end
create l_data.make_empty
until
s = Void or else s.count < n
loop
l_data.append_string (s)
if sock.socket_ok then
sock.read_stream_thread_aware (n)
s := sock.last_string
else
s := Void
end
end
Precursor
end
end