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,5 @@
Eiffel Web Nino is and HTTPD server. It's a work in progress, so maybe it will be refactored.
The goal of is to provide a simple web server for development (like Java, Python and Ruby provide)

View File

@@ -0,0 +1,79 @@
note
description: "Summary description for {HTTP_SERVER_CONFIGURATION}."
date: "$Date$"
revision: "$Revision$"
class
HTTP_SERVER_CONFIGURATION
create
make
feature {NONE} -- Initialization
make
do
http_server_port := 80
max_tcp_clients := 100
socket_accept_timeout := 1_000
socket_connect_timeout := 5_000
document_root := "htdocs"
force_single_threaded := False
end
feature -- Access
Server_details : STRING = "Server : NINO Eiffel Server"
document_root: STRING assign set_document_root
http_server_port: INTEGER assign set_http_server_port
max_tcp_clients: INTEGER assign set_max_tcp_clients
socket_accept_timeout: INTEGER assign set_socket_accept_timeout
socket_connect_timeout: INTEGER assign set_socket_connect_timeout
force_single_threaded: BOOLEAN assign set_force_single_threaded
is_verbose: BOOLEAN assign set_is_verbose
-- Display verbose message to the output?
feature -- Element change
set_http_server_port (v: like http_server_port)
do
http_server_port := v
end
set_document_root (v: like document_root)
do
document_root := v
end
set_max_tcp_clients (v: like max_tcp_clients)
do
max_tcp_clients := v
end
set_socket_accept_timeout (v: like socket_accept_timeout)
do
socket_accept_timeout := v
end
set_socket_connect_timeout (v: like socket_connect_timeout)
do
socket_connect_timeout := v
end
set_force_single_threaded (v: like force_single_threaded)
do
force_single_threaded := v
end
set_is_verbose (b: BOOLEAN)
-- Set `is_verbose' to `b'
do
is_verbose := b
end
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,199 @@
note
description: "Summary description for {HTTP_CONNECTION_HANDLER}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
HTTP_CONNECTION_HANDLER
inherit
HTTP_HANDLER
redefine
make
end
feature {NONE} -- Initialization
make (a_server: like server)
-- Creates a {HTTP_CONNECTION_HANDLER}, assigns the main_server and sets the current_request_message to empty.
--
-- `a_server': The main server object
do
Precursor (a_server)
reset
end
reset
do
has_error := False
create method.make_empty
create uri.make_empty
create request_header.make_empty
create request_header_map.make (10)
remote_info := Void
end
feature -- Execution
receive_message_and_send_reply (client_socket: TCP_STREAM_SOCKET)
local
l_remote_info: detachable like remote_info
do
create l_remote_info
if attached client_socket.peer_address as l_addr then
l_remote_info.addr := l_addr.host_address.host_address
l_remote_info.hostname := l_addr.host_address.host_name
l_remote_info.port := l_addr.port
remote_info := l_remote_info
end
analyze_request_message (client_socket)
if has_error then
check catch_bad_incoming_connection: False end
if is_verbose then
log ("ERROR: invalid HTTP incoming request")
end
else
process_request (Current, client_socket)
end
reset
end
feature -- Request processing
process_request (a_handler: HTTP_CONNECTION_HANDLER; a_socket: TCP_STREAM_SOCKET)
-- Process request ...
require
no_error: not has_error
a_handler_attached: a_handler /= Void
a_uri_attached: a_handler.uri /= Void
a_method_attached: a_handler.method /= Void
a_header_map_attached: a_handler.request_header_map /= Void
a_header_text_attached: a_handler.request_header /= Void
a_socket_attached: a_socket /= Void
deferred
end
feature -- Access
request_header: STRING
-- Header' source
request_header_map : HASH_TABLE [STRING,STRING]
-- Contains key:value of the header
has_error: BOOLEAN
-- Error occurred during `analyze_request_message'
method: STRING
-- http verb
uri: STRING
-- http endpoint
version: detachable STRING
-- http_version
--| unused for now
remote_info: detachable TUPLE [addr: STRING; hostname: STRING; port: INTEGER]
-- Information related to remote client
feature -- Parsing
analyze_request_message (a_socket: TCP_STREAM_SOCKET)
-- Analyze message extracted from `a_socket' as HTTP request
require
input_readable: a_socket /= Void and then a_socket.is_open_read
local
end_of_stream : BOOLEAN
pos,n : INTEGER
line : detachable STRING
k, val: STRING
txt: STRING
l_is_verbose: BOOLEAN
do
create txt.make (64)
request_header := txt
if attached next_line (a_socket) as l_request_line and then not l_request_line.is_empty then
txt.append (l_request_line)
txt.append_character ('%N')
analyze_request_line (l_request_line)
else
has_error := True
end
l_is_verbose := is_verbose
if not has_error or l_is_verbose then
-- if `is_verbose' we can try to print the request, even if it is a bad HTTP request
from
line := next_line (a_socket)
until
line = Void or end_of_stream
loop
n := line.count
if l_is_verbose then
log (line)
end
pos := line.index_of (':',1)
if pos > 0 then
k := line.substring (1, pos-1)
if line [pos+1].is_space then
pos := pos + 1
end
if line [n] = '%R' then
n := n - 1
end
val := line.substring (pos + 1, n)
request_header_map.put (val, k)
end
txt.append (line)
txt.append_character ('%N')
if line.is_empty or else line [1] = '%R' then
end_of_stream := True
else
line := next_line (a_socket)
end
end
end
end
analyze_request_line (line: STRING)
-- Analyze `line' as a HTTP request line
require
valid_line: line /= Void and then not line.is_empty
local
pos, next_pos: INTEGER
do
if is_verbose then
log ("%N## Parse HTTP request line ##")
log (line)
end
pos := line.index_of (' ', 1)
method := line.substring (1, pos - 1)
next_pos := line.index_of (' ', pos + 1)
uri := line.substring (pos + 1, next_pos - 1)
version := line.substring (next_pos + 1, line.count)
has_error := method.is_empty
end
next_line (a_socket: TCP_STREAM_SOCKET): detachable STRING
-- Next line fetched from `a_socket' is available.
require
is_readable: a_socket.is_open_read
do
if a_socket.socket_ok then
a_socket.read_line_thread_aware
Result := a_socket.last_string
end
end
invariant
request_header_attached: request_header /= Void
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,149 @@
note
description: "Summary description for {HTTP_CONSTANTS}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
HTTP_CONSTANTS
feature
http_version_1_1: STRING = "HTTP/1.1"
http_version_1_0: STRING = "HTTP/1.0"
crlf: STRING = "%/13/%/10/"
feature -- Status codes
-- 1xx Informational -Request received, continuing process
Continue : STRING = "100"
Switching_Protocols : STRING = "101"
-- 2xx Success - The action was successfully received, understood, and accepted
Ok: STRING = "200"
Created : STRING = "201"
Accepted : STRING = "202"
Non_Authoritative_Information : STRING = "203"
No_Content : STRING = "204"
Reset_Content : STRING = "205"
Parcial_Content : STRING = "206"
-- 3xx Redirection - Further Action must be taken in order to complete the request
Multiple_Choices : STRING = "300"
Moved_Permanently: STRING = "301"
Found : STRING = "302"
See_Other : STRING = "303"
Not_Modified : STRING = "304"
Use_Proxy : STRING = "305"
Temporary_Redirect : STRING = "307"
--4xx Client Error - The request contains bad syntax or cannot be fulfilled
Bad_Request : STRING = "400"
Unauthorized : STRING = "401"
Payment_Required : STRING = "402"
Forbidden : STRING = "403"
Not_Found : STRING = "404"
Method_Not_Allowed : STRING = "405"
Not_Acceptable : STRING = "406"
Proxy_Authentication_Required : STRING = "407"
Request_Time_out : STRING = "408"
Conflict : STRING = "409"
Gone : STRING = "410"
Length_Required : STRING = "411"
Precondition_Failed : STRING = "412"
Request_Entity_Too_Large : STRING = "413"
Request_URI_Too_Large : STRING = "414"
Unsupported_Media_Type : STRING = "415"
Requested_range_not_satisfiable : STRING = "416"
Expectation_Failed : STRING = "417"
--5xx Server Error - The server failed to fulfill an apparently valid request
server_error: STRING = "500"
Internal_Server_Error : STRING = "500"
Not_Implemented : STRING = "501"
Bad_Gateway : STRING = "502"
Service_Unavailable : STRING = "503"
Gateway_Time_out : STRING = "504"
HTTP_Version_not_supported : STRING = "505"
-- messages
ok_message: STRING = "OK"
continue_message : STRING = "Continue"
not_found_message: STRING = "URI not found"
not_implemented_message: STRING = "Not Implemented"
feature -- content types
text_html: STRING = "text/html"
feature -- General Header Fields
-- There are a few header fields which have general applicability for both request and response messages,
-- but which do not apply to the entity being transferred.
-- These header fields apply only to the message being transmitted.
Cache_control : STRING = "Cache-Control"
Connection : STRING = "Connection"
Date : STRING = "Date"
Pragma : STRING = "PRAGMA"
Trailer : STRING = "Trailer"
Transfer_encoding : STRING = "Transfer-Encoding"
Upgrade : STRING = "Upgrade"
Via : STRING = "Via"
Warning : STRING = "Warning"
feature -- Request Header
Accept : STRING = "Accept"
Accept_charset : STRING = "Accept-Charset"
Accept_encoding : STRING = "Accept-Encoding"
Accept_language : STRING = "Accept-Language"
Authorization : STRING = "Authorization"
Expect : STRING = "Expect"
From_header : STRING = "From"
Host : STRING = "Host"
If_match : STRING = "If-Match"
If_modified_since : STRING = "If-Modified-Since"
If_none_match : STRING = "If-None-Match"
If_range : STRING = "If-Range"
If_unmodified_since : STRING = "If-Unmodified-Since"
Max_forwards : STRING = "Max-Forwards"
Proxy_authorization : STRING = "Proxy-Authorization"
Range : STRING = "Range"
Referer : STRING = "Referrer"
TE : STRING = "TE"
User_agent : STRING = "User-Agent"
feature -- Entity Header
Allow : STRING = "Allow"
Content_encoding : STRING = "Content-Encoding"
Content_language : STRING = "Content-Language"
Content_length : STRING = "Content-Length"
Content_location : STRING = "Content-Location"
Content_MD5 : STRING = "Content-MD5"
Content_range : STRING = "Content-Range"
Content_type : STRING = "Content-Type"
Expires : STRING = "Expires"
Last_modified : STRING = "Last-Modified"
feature -- Http Method
Options : STRING = "OPTIONS"
Get : STRING = "GET"
Head : STRING = "HEAD"
Post : STRING = "POST"
Put : STRING = "PUT"
Delete : STRING = "DELETE"
Trace : STRING = "TRACE"
Connect : STRING = "CONNECT"
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,60 @@
note
description: "[
Provides features to encode and decode messages
]"
legal: "See notice at end of class."
status: "Community Preview 1.0"
date: "$Date: 2009-09-01 19:15:37 -0300 (mar 01 de sep de 2009) $"
revision: "$Revision: 80577 $"
class
HTTP_ENCODING_FACILITIES
create
make
feature -- Initialization
make
do
end
feature -- Conversion
encode_natural(a_i: NATURAL; a_is_fragmented: BOOLEAN): NATURAL
-- Leftshift of the natural (don't use numbers >= 2^31) and subsequent append of the flag bit.
-- Use decode_natural and decode_flag for decoding.
require
no_too_big: a_i < 2147483648
do
Result := (a_i |<< 1) + a_is_fragmented.to_integer.as_natural_32
end
change_flag(a_i: NATURAL; a_new_flag: BOOLEAN): NATURAL
-- Changes the flag to "new_flag" and doesn't change the encoded natural.
do
Result := (a_i & 0xFFFFFFFE) + a_new_flag.to_integer.as_natural_32
end
decode_natural_and_flag (a_i: NATURAL): TUPLE [NATURAL, BOOLEAN]
-- Convenience feature which combines both decodings (natural and flag)
do
Result := [decode_natural (a_i), decode_flag (a_i)]
end
decode_natural (a_i: NATURAL): NATURAL
-- The natural that was encoded in {ENCODING_FACILITIES}.encode_natural.
do
Result := (a_i |>> 1)
end
decode_flag (a_i: NATURAL): BOOLEAN
--`Result': the flag that was encoded in encode_natural
do
Result := (a_i.bit_and (1) = 1)
end
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,220 @@
note
description: "Summary description for {HTTP_CONNECTION_HANDLER}."
date: "$Date$"
revision: "$Revision$"
deferred class
HTTP_HANDLER
inherit
ANY
HTTP_CONSTANTS
feature {NONE} -- Initialization
make (a_server: like server)
-- Creates a {HTTP_HANDLER}, assigns the server and initialize various values
--
-- `a_server': The main server object
require
a_server_attached: a_server /= Void
do
server := a_server
is_stop_requested := False
ensure
server_set: a_server ~ server
end
feature -- Output
log (a_message: READABLE_STRING_8)
-- Log `a_message'
do
io.put_string (a_message)
io.put_new_line
end
feature -- Inherited Features
execute
-- <Precursor>
-- Creates a socket and connects to the http server.
local
l_listening_socket: detachable TCP_STREAM_SOCKET
l_http_port: INTEGER
do
launched := False
port := 0
is_stop_requested := False
l_http_port := http_server_port
create l_listening_socket.make_server_by_port (l_http_port)
if not l_listening_socket.is_bound then
if is_verbose then
log ("Socket could not be bound on port " + l_http_port.out)
end
else
l_http_port := l_listening_socket.port
from
l_listening_socket.listen (max_tcp_clients)
if is_verbose then
log ("%NHTTP Connection Server ready on port " + l_http_port.out +" : http://localhost:" + l_http_port.out + "/")
end
on_launched (l_http_port)
until
is_stop_requested
loop
l_listening_socket.accept
if not is_stop_requested then
if attached l_listening_socket.accepted as l_thread_http_socket then
process_connection (l_thread_http_socket)
end
end
is_stop_requested := stop_requested_on_server
end
l_listening_socket.cleanup
check
socket_is_closed: l_listening_socket.is_closed
end
end
if launched then
on_stopped
end
if is_verbose then
log ("HTTP Connection Server ends.")
end
rescue
log ("HTTP Connection Server shutdown due to exception. Please relaunch manually.")
if l_listening_socket /= Void then
l_listening_socket.cleanup
check
socket_is_closed: l_listening_socket.is_closed
end
end
if launched then
on_stopped
end
is_stop_requested := True
retry
end
process_connection (a_socket: TCP_STREAM_SOCKET)
-- Process incoming connection
do
if is_verbose then
log ("Incoming connection...(socket:" + a_socket.descriptor.out + ")")
end
--| FIXME jfiat [2011/11/03] : should use a Pool of Threads/Handler to process this connection
--| also handle permanent connection...?
receive_message_and_send_reply (a_socket)
a_socket.cleanup
if is_verbose then
log ("connection completed...")
end
ensure
socket_closed: a_socket.is_closed
end
feature -- Event
on_launched (a_port: INTEGER)
-- Server launched using port `a_port'
require
not_launched: not launched
do
launched := True
port := a_port
ensure
launched: launched
end
on_stopped
-- Server stopped
require
launched: launched
do
launched := False
ensure
stopped: not launched
end
feature -- Access
is_stop_requested: BOOLEAN
-- Set true to stop accept loop
launched: BOOLEAN
-- Server launched and listening on `port'
port: INTEGER
-- Listening port.
--| 0: not launched
feature -- Access: configuration
is_verbose: BOOLEAN
-- Is verbose for output messages.
do
Result := server_configuration.is_verbose
end
force_single_threaded: BOOLEAN
do
Result := server_configuration.force_single_threaded
end
http_server_port: INTEGER
do
Result := server_configuration.http_server_port
end
max_tcp_clients: INTEGER
do
Result := server_configuration.max_tcp_clients
end
feature {NONE} -- Access: server
server: HTTP_SERVER
-- The main server object
stop_requested_on_server: BOOLEAN
-- Stop requested on `server' object
do
Result := server.stop_requested
end
feature {NONE} -- Access: configuration
server_configuration: HTTP_SERVER_CONFIGURATION
-- The main server's configuration
do
Result := server.configuration
end
feature -- Status setting
shutdown
-- Stops the thread
do
is_stop_requested := True
end
feature -- Execution
receive_message_and_send_reply (client_socket: TCP_STREAM_SOCKET)
require
socket_attached: client_socket /= Void
-- socket_valid: client_socket.is_open_read and then client_socket.is_open_write
a_http_socket: not client_socket.is_closed
deferred
end
invariant
server_attached: server /= Void
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,56 @@
note
description: "Summary description for {HTTP_SERVER}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
HTTP_SERVER
create
make
feature -- Initialization
make (cfg: like configuration)
do
configuration := cfg
end
setup (a_http_handler: HTTP_HANDLER)
require
a_http_handler_valid: a_http_handler /= Void
do
if configuration.is_verbose then
log ("%N%N%N")
log ("Starting Web Application Server (port="+ configuration.http_server_port.out +"):%N")
end
stop_requested := False
a_http_handler.execute
end
shutdown_server
do
stop_requested := True
end
feature -- Access
configuration: HTTP_SERVER_CONFIGURATION
-- Configuration of the server
stop_requested: BOOLEAN
-- Stops the server
feature -- Output
log (a_message: READABLE_STRING_8)
-- Log `a_message'
do
io.put_string (a_message)
end
;note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,118 @@
deferred class HTTP_REQUEST_HANDLER
inherit
ANY
redefine
default_create
end
feature {NONE} -- Initialization
default_create
do
Precursor
create request_uri.make_empty
create script_name.make_empty
create query_string.make_empty
create answer
create headers.make (0)
end
feature -- Access
request_uri: STRING
-- requested url
script_name: STRING
-- Script name
query_string: STRING
-- Query string
data: detachable 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"
answer: HTTP_RESPONSE
-- reply to this request
feature -- Execution
process
-- process the request and create an answer
require
valid_uri: request_uri /= Void
deferred
end
feature -- Recycle
reset
-- reinit the fields
do
request_uri.wipe_out
script_name.wipe_out
query_string.wipe_out
data := Void
answer.reset
end
feature -- Element change
set_uri (new_uri: STRING)
-- set new URI
require
valid_uri: new_uri /= Void
local
p: INTEGER
do
request_uri := new_uri
p := new_uri.index_of ('?', 1)
if p > 0 then
script_name := new_uri.substring (1, p - 1)
query_string := new_uri.substring (p + 1, new_uri.count)
else
script_name := new_uri.string
query_string := ""
end
end
set_data (new_data: STRING)
-- set new data
do
data := new_data
end
set_headers ( a_header : HASH_TABLE [STRING, STRING] )
do
headers := a_header
end
feature {NONE} -- Implementation
real_filename (fn: STRING): STRING
-- Real filename from url-path `fn'
--| Find a better design for this piece of code
--| Eventually in a spec/$ISE_PLATFORM/ specific cluster
do
if {PLATFORM}.is_windows then
create Result.make_from_string (fn)
Result.replace_substring_all ("/", "\")
if Result[Result.count] = '\' then
Result.remove_tail (1)
end
else
Result := fn
if Result[Result.count] = '/' then
Result := Result.substring (1, Result.count - 1)
end
end
end
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,147 @@
class HTTP_RESPONSE
inherit
HTTP_CONSTANTS
redefine
default_create
end
create
default_create
feature -- creation
default_create
do
Precursor
set_defaults
end
set_defaults
-- Set default values for the reply
do
status_code := ok
create content_length_data.make_empty
reason_phrase := ok_message
content_type_data := text_html
set_reply_text (Void)
end
feature -- Recycle
reset
do
set_defaults
end
feature -- response header fields
status_code: STRING
-- status
content_length_data : STRING
-- length
reason_phrase: STRING
-- message, if any
content_type_data: STRING
-- type of content in this reply (eg. text/html)
feature -- Element change
set_content_length (new_content_length: INTEGER)
require
positive_or_zero: new_content_length >= 0
do
content_length_data := new_content_length.out
end
set_status_code (new_status_code: STRING)
require
not_void: new_status_code /= Void
do
status_code := new_status_code
end
set_reason_phrase (new_reason_phrase: STRING)
require
not_void: new_reason_phrase /= Void
do
reason_phrase := new_reason_phrase
end
set_content_type (new_content_type: STRING)
require
not_void: new_content_type /= Void
do
content_type_data := new_content_type
end
feature -- Access: send reply
reply_header: STRING
-- header
do
Result := http_version_1_1.twin
Result.extend (' ')
Result.append (status_code)
Result.extend (' ')
Result.append (reason_phrase)
Result.append (crlf)
Result.append ({HTTP_SERVER_CONFIGURATION}.Server_details)
Result.append (crlf)
Result.append (Content_type + ": ")
Result.append (content_type_data)
Result.append (crlf)
Result.append (Content_length + ": ")
Result.append (content_length_data)
Result.append (crlf)
Result.append (crlf)
-- TODO: could add the size of data being sent here and
-- then keep the connection alive
end
reply_header_continue: STRING
-- header
do
Result := http_version_1_1.twin
Result.extend (' ')
Result.append (status_code)
Result.extend (' ')
Result.append (continue_message)
Result.append (crlf)
Result.append (crlf)
-- TODO: could add the size of data being sent here and
-- then keep the connection alive
end
reply_text: STRING
-- reply text
feature -- Change element: send reply
set_reply_text (new_text: detachable STRING)
-- text could be Void
do
if new_text = Void then
create reply_text.make_empty
else
reply_text := new_text
end
end
append_reply_text (more_text: STRING)
-- add more text to the reply
require
reply_text /= Void
more_text /= Void
do
reply_text.append (more_text)
end
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,18 @@
note
description: "Summary description for {SHARED_URI_CONTENTS_TYPES}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
SHARED_URI_CONTENTS_TYPES
feature
ct_table: URI_CONTENTS_TYPES
once
create Result.make
end
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,59 @@
note
description: "Summary description for {TCP_STREAM_SOCKET}."
date: "$Date$"
revision: "$Revision$"
class
TCP_STREAM_SOCKET
inherit
NETWORK_STREAM_SOCKET
create
make_server_by_port
create {NETWORK_STREAM_SOCKET}
make_from_descriptor_and_address
feature -- Basic operation
send_message (a_msg: STRING)
local
a_package : PACKET
a_data : MANAGED_POINTER
c_string : C_STRING
do
create c_string.make (a_msg)
create a_data.make_from_pointer (c_string.item, a_msg.count + 1)
create a_package.make_from_managed_pointer (a_data)
send (a_package, 1)
end
feature -- Output
put_readable_string_8 (s: READABLE_STRING_8)
-- Write readable string `s' to socket.
local
ext: C_STRING
do
create ext.make (s)
put_managed_pointer (ext.managed_data, 0, s.count)
end
feature -- Status report
try_ready_for_reading: BOOLEAN
-- Is data available for reading from the socket right now?
require
socket_exists: exists
local
retval: INTEGER
do
retval := c_select_poll_with_timeout (descriptor, True, 0)
Result := (retval > 0)
end
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end

View File

@@ -0,0 +1,91 @@
class URI_CONTENTS_TYPES
create
make
feature
content_types: HASH_TABLE [STRING, STRING]
extension (uri: STRING): STRING
-- extract extendion from a URI
local
i: INTEGER
do
-- going from the end find the position of the "."
from
i := uri.count
until
i = 0 or else uri.item (i) = '.'
loop
i := i - 1
end
Result := uri.substring (i+1, uri.count)
end
feature {NONE}
make
do
create content_types.make (30)
content_types.put ("text/html", "html")
content_types.put ("text/html", "htm")
content_types.put ("image/gif", "gif")
content_types.put ("image/jpeg", "jpeg")
content_types.put ("image/png", "jpg")
content_types.put ("image/png", "png")
end
feature -- Access: Encoding
urlencode (s: STRING): STRING
-- URL encode `s'
do
Result := s.string
Result.replace_substring_all ("#", "%%23")
Result.replace_substring_all (" ", "%%20")
Result.replace_substring_all ("%T", "%%09")
Result.replace_substring_all ("%N", "%%0A")
Result.replace_substring_all ("/", "%%2F")
Result.replace_substring_all ("&", "%%26")
Result.replace_substring_all ("<", "%%3C")
Result.replace_substring_all ("=", "%%3D")
Result.replace_substring_all (">", "%%3E")
Result.replace_substring_all ("%"", "%%22")
Result.replace_substring_all ("%'", "%%27")
end
urldecode (s: STRING): STRING
-- URL decode `s'
do
Result := s.string
Result.replace_substring_all ("%%23", "#")
Result.replace_substring_all ("%%20", " ")
Result.replace_substring_all ("%%09", "%T")
Result.replace_substring_all ("%%0A", "%N")
Result.replace_substring_all ("%%2F", "/")
Result.replace_substring_all ("%%26", "&")
Result.replace_substring_all ("%%3C", "<")
Result.replace_substring_all ("%%3D", "=")
Result.replace_substring_all ("%%3E", ">")
Result.replace_substring_all ("%%22", "%"")
Result.replace_substring_all ("%%27", "%'")
end
stripslashes (s: STRING): STRING
do
Result := s.string
Result.replace_substring_all ("\%"", "%"")
Result.replace_substring_all ("\'", "'")
Result.replace_substring_all ("\/", "/")
Result.replace_substring_all ("\\", "\")
end
note
copyright: "2011-2011, Javier Velilla and others"
license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
end