Import HTTPD Eiffel
This commit is contained in:
112
request/get_request_handler.e
Normal file
112
request/get_request_handler.e
Normal file
@@ -0,0 +1,112 @@
|
||||
class GET_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
|
||||
create answer.make
|
||||
if request_uri.is_equal ("/") then
|
||||
process_default
|
||||
answer.set_content_type ("text/html")
|
||||
else
|
||||
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)
|
||||
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
|
||||
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
|
||||
answer.set_content_length (answer.reply_text.count.out)
|
||||
end
|
||||
|
||||
process_default
|
||||
-- Return a defaul response
|
||||
local
|
||||
html : STRING
|
||||
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
|
||||
-- this is not quite right....
|
||||
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
|
||||
|
||||
|
||||
end
|
||||
115
request/head_request_handler.e
Normal file
115
request/head_request_handler.e
Normal 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 is
|
||||
-- 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 is
|
||||
--
|
||||
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) is
|
||||
-- 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) is
|
||||
-- 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
|
||||
53
request/http_request_handler.e
Normal file
53
request/http_request_handler.e
Normal file
@@ -0,0 +1,53 @@
|
||||
deferred class HTTP_REQUEST_HANDLER
|
||||
feature
|
||||
|
||||
set_uri (new_uri: STRING)
|
||||
-- set new URI
|
||||
require
|
||||
valid_uri: new_uri /= Void
|
||||
do
|
||||
request_uri := new_uri
|
||||
end
|
||||
|
||||
request_uri: STRING
|
||||
-- requested url
|
||||
|
||||
set_data (new_data: STRING)
|
||||
-- set new data
|
||||
do
|
||||
data := new_data
|
||||
end
|
||||
|
||||
data: STRING
|
||||
-- the entire request message
|
||||
|
||||
|
||||
headers : HASH_TABLE [STRING, STRING]
|
||||
-- Provides access to the request's HTTP headers, for example:
|
||||
-- headers["Content-Type"] is "text/plain"
|
||||
|
||||
|
||||
set_headers ( a_header : HASH_TABLE [STRING, STRING] )
|
||||
do
|
||||
headers := a_header
|
||||
end
|
||||
|
||||
process
|
||||
-- process the request and create an answer
|
||||
require
|
||||
valid_uri: request_uri /= Void
|
||||
deferred
|
||||
end
|
||||
|
||||
answer: HTTP_RESPONSE
|
||||
-- reply to this request
|
||||
|
||||
reset
|
||||
-- reinit the fields
|
||||
do
|
||||
request_uri := Void
|
||||
data := Void
|
||||
answer := Void
|
||||
end
|
||||
|
||||
end
|
||||
115
request/post_request_handler.e
Normal file
115
request/post_request_handler.e
Normal file
@@ -0,0 +1,115 @@
|
||||
note
|
||||
description: "Summary description for {POST_REQUEST_HANDLER}."
|
||||
author: ""
|
||||
date: "$Date$"
|
||||
revision: "$Revision$"
|
||||
|
||||
class
|
||||
POST_REQUEST_HANDLER
|
||||
|
||||
inherit
|
||||
|
||||
SHARED_DOCUMENT_ROOT
|
||||
|
||||
SHARED_URI_CONTENTS_TYPES
|
||||
|
||||
HTTP_REQUEST_HANDLER
|
||||
|
||||
HTTP_CONSTANTS
|
||||
|
||||
feature
|
||||
|
||||
|
||||
process is
|
||||
-- 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 is
|
||||
--
|
||||
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) is
|
||||
-- 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) is
|
||||
-- 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
|
||||
Reference in New Issue
Block a user