PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/tutorial/net/http_get_handler.e

http://github.com/tybor/Liberty
Specman e | 185 lines | 139 code | 14 blank | 32 comment | 12 complexity | f990f89945a56c4196f9544779eb9d44 MD5 | raw file
  1-- See the Copyright notice at the end of this file.
  2--
  3class HTTP_GET_HANDLER
  4
  5inherit
  6	HTTP_METHOD_HANDLER
  7
  8create {HTTP_CONNECTION}
  9	make
 10
 11feature {HTTP_CONNECTION}
 12	method: STRING
 13
 14	prepare_ok: BOOLEAN
 15
 16	prepare_answer is
 17		local
 18			f: TEXT_FILE_READ
 19		do
 20			f := file
 21			if f = Void then
 22				code := 404
 23			else
 24				check
 25					f.is_connected
 26				end
 27				fill_body(f)
 28				f.disconnect
 29				code := 200
 30			end
 31			prepare_ok := True
 32		end
 33
 34	add_header (header: STRING) is
 35		do
 36			std_output.put_string(once "HEADER: ")
 37			std_output.put_line(header)
 38		end
 39
 40	add_body (body: STRING) is
 41		do
 42			std_output.put_string(once "BODY: ")
 43			std_output.put_line(body)
 44		end
 45
 46	make (a_method: like method; a_uri, a_version: STRING) is
 47		require
 48			a_method.is_equal(once "GET") or else a_method.is_equal(once "POST")
 49		local
 50			i: INTEGER
 51		do
 52			method := a_method
 53			if uri = Void then
 54				uri := a_uri.twin
 55				version := a_version.twin
 56				create response_header.with_capacity(2)
 57				create response_body.make_empty
 58			else
 59				uri.copy(a_uri)
 60				version.copy(a_version)
 61				response_header.clear_count
 62				response_body.clear_count
 63			end
 64			-- remove any trailing part
 65			i := uri.first_index_of('#')
 66			if i > 0 then
 67				uri.shrink(1, i-1)
 68			end
 69			i := uri.first_index_of('?')
 70			if i > 0 then
 71				uri.shrink(1, i-1)
 72			end
 73			prepare_ok := False
 74		ensure
 75			version.is_equal(a_version)
 76		end
 77
 78	expect (events: EVENTS_SET) is
 79		local
 80			t: TIME_EVENTS
 81		do
 82			events.expect(t.timeout(0))
 83		end
 84
 85	is_ready (events: EVENTS_SET): BOOLEAN is
 86		do
 87			Result := True
 88		end
 89
 90feature {}
 91	file: TEXT_FILE_READ is
 92		local
 93			cwd, path: STRING; bd: BASIC_DIRECTORY; ft: FILE_TOOLS; i, o: INTEGER
 94		do
 95			cwd := once ""
 96			cwd.copy(bd.current_working_directory)
 97			path := once ""
 98			o := 1
 99			from
100				i := uri.first_index_of('/')
101				if i = 0 then
102					path.copy(uri)
103				end
104			until
105				i = 0
106			loop
107				o := i
108				i := uri.index_of('/', i + 1)
109				if i = 0 then
110					path.copy(uri)
111					path.shrink(o + 1, path.count)
112				else
113					path.copy(uri)
114					path.shrink(o + 1, i - 1)
115					if path.same_as(once "..") then
116						-- security risk, do not answer
117						i := 0
118						path.clear_count
119					end
120					bd.compute_subdirectory_with(cwd, path)
121					cwd.copy(bd.last_entry)
122				end
123			end
124			-- here should be the name of the file without its directory components
125			if not path.is_empty then
126				bd.compute_file_path_with(cwd, path)
127				if ft.file_exists(bd.last_entry) then
128					tfr.connect_to(bd.last_entry)
129					if tfr.is_connected then
130						Result := tfr
131					end
132				end
133			end
134		ensure
135			Result /= Void implies Result.is_connected
136		end
137
138	tfr: TEXT_FILE_READ is
139		once
140			create Result.make
141		end
142
143feature {}
144 	fill_body (f: TEXT_FILE_READ) is
145 		do
146 			from
147 			until
148 				f.end_of_input
149 			loop
150 				f.read_line
151 				response_body.append(f.last_string)
152				if not f.end_of_input then
153					response_body.append(once "%R%N")
154				end
155			end
156		end
157
158end -- class HTTP_GET_HANDLER
159--
160-- ------------------------------------------------------------------------------------------------------------
161-- Copyright notice below. Please read.
162--
163-- This file is part of the SmartEiffel standard library.
164-- Copyright(C) 1994-2002: INRIA - LORIA (INRIA Lorraine) - ESIAL U.H.P.       - University of Nancy 1 - FRANCE
165-- Copyright(C) 2003-2006: INRIA - LORIA (INRIA Lorraine) - I.U.T. Charlemagne - University of Nancy 2 - FRANCE
166--
167-- Authors: Dominique COLNET, Philippe RIBET, Cyril ADRIAN, Vincent CROIZIER, Frederic MERIZEN
168--
169-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
170-- documentation files (the "Software"), to deal in the Software without restriction, including without
171-- limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
172-- the Software, and to permit persons to whom the Software is furnished to do so, subject to the following
173-- conditions:
174--
175-- The above copyright notice and this permission notice shall be included in all copies or substantial
176-- portions of the Software.
177--
178-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
179-- LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
180-- EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181-- AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
182-- OR OTHER DEALINGS IN THE SOFTWARE.
183--
184-- http://SmartEiffel.loria.fr - SmartEiffel@loria.fr
185-- ------------------------------------------------------------------------------------------------------------