PageRenderTime 77ms CodeModel.GetById 73ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/library/server/fcgi/interface/fcgi_i.e

http://github.com/jocelyn/EiffelWebReloaded
Specman e | 236 lines | 176 code | 37 blank | 23 comment | 5 complexity | d8265c441fe20fb50d67c5f3cd6070c2 MD5 | raw file
  1note
  2	description: "Abstract interface to FCGI C library"
  3	legal: "See notice at end of class."
  4	status: "See notice at end of class."
  5	date: "$Date$"
  6	revision: "$Revision$"
  7
  8deferred class FCGI_I
  9
 10inherit
 11	STRING_HANDLER
 12
 13feature {NONE} -- Initialization
 14
 15	make
 16			-- Initialize FCGI interface
 17		deferred
 18		end
 19
 20feature -- Access
 21
 22	updated_environ_variables: HASH_TABLE [STRING, STRING]
 23		local
 24			i: INTEGER
 25			p, v, null: POINTER
 26		do
 27			p := fcgi_environ
 28			create Result.make (50)
 29			if p /= null then
 30				from
 31					i := 0
 32					v := fcgi_i_th_environ (i,p)
 33				until
 34					v = null
 35				loop
 36					if attached separated_variables (create {STRING}.make_from_c (v)) as t then
 37						Result.force (t.value, t.key)
 38					end
 39					i := i + 1
 40					v := fcgi_i_th_environ (i,p)
 41				end
 42			end
 43		end
 44
 45feature -- FCGI interface
 46
 47	fcgi_listen: INTEGER
 48			-- Listen to the FCGI input stream
 49			-- Return 0 for successful calls, -1 otherwise.
 50		deferred
 51		end
 52
 53	fcgi_environ: POINTER
 54			-- Get the (char**) environ variable from the DLL.
 55		deferred
 56		end
 57
 58	fcgi_finish
 59			-- Finish current request from HTTP server started from
 60			-- the most recent call to `fcgi_accept'.
 61		deferred
 62		end
 63
 64	set_fcgi_exit_status (v: INTEGER)
 65		deferred
 66		end
 67
 68feature -- Status
 69
 70	is_interactive: BOOLEAN
 71			-- Is execution interactive? (for debugging)
 72		do
 73		end
 74
 75feature -- Input
 76
 77	fill_string_from_stdin (s: STRING; n: INTEGER)
 78			-- Read up to `n' bytes from stdin and store in string `s'
 79		local
 80			new_count: INTEGER
 81			str_area: ANY
 82		do
 83			s.grow (n)
 84			str_area := s.area
 85			new_count := fill_pointer_from_stdin ($str_area, n)
 86			s.set_count (new_count)
 87		end
 88
 89	read_from_stdin (n: INTEGER)
 90			-- Read up to n bytes from stdin and store in input buffer
 91		require
 92			small_enough: n <= buffer_capacity
 93		local
 94			l_c_str: C_STRING
 95			l_count: INTEGER
 96		do
 97			last_read_is_empty_ref.set_item (False)
 98			l_c_str := c_buffer
 99			l_count := fill_pointer_from_stdin (l_c_str.item, n)
100			last_read_count_ref.set_item (l_count)
101			if l_count <= 0 then
102				last_read_is_empty_ref.set_item (True)
103			end
104		end
105
106	fill_pointer_from_stdin (p: POINTER; n: INTEGER): INTEGER
107			-- Read up to `n' bytes from stdin and store in pointer `p'
108			-- and return number of bytes read.
109		deferred
110		end
111
112	copy_from_stdin (n: INTEGER; f: FILE)
113			-- Read up to n bytes from stdin and write to given file
114		require
115--			small_enough: n <= buffer_capacity
116			file_exists: f /= Void
117			file_open: f.is_open_write or f.is_open_append
118		deferred
119		end
120
121feature -- Output
122
123	put_string (a_str: STRING)
124			-- Put `a_str' on the FastCGI stdout.
125		require
126			a_str_not_void: a_str /= Void
127		deferred
128		end
129
130feature -- Implementation		
131
132	buffer_contents: STRING
133		local
134			n: like last_read_count
135		do
136			n := last_read_count
137			create Result.make (n)
138			Result.set_count (n)
139			c_buffer.read_substring_into (Result, 1, n)
140		end
141
142	buffer_capacity: INTEGER
143		do
144			Result := c_buffer.capacity
145		end
146
147--RFO	last_string: STRING
148--RFO		once
149--RFO			create Result.make (K_input_bufsize)
150--RFO		end
151
152	last_read_count: INTEGER
153		do
154			Result := last_read_count_ref.item
155		end
156
157	last_read_is_empty: BOOLEAN
158		do
159			Result := last_read_is_empty_ref.item
160		end
161
162feature {NONE} -- Shared buffer
163
164	c_buffer: C_STRING
165			-- Buffer for Eiffel to C and C to Eiffel string conversions.
166		once
167			create Result.make_empty (K_input_bufsize)
168		ensure
169			c_buffer_not_void: Result /= Void
170		end
171
172
173feature {NONE} -- Constants
174
175	last_read_count_ref: INTEGER_REF
176		once
177			create Result
178		end
179
180	last_read_is_empty_ref: BOOLEAN_REF
181		once
182			create Result
183		end
184
185	K_input_bufsize: INTEGER = 1024_000
186
187feature {NONE} -- Implementation: Environment
188
189	fcgi_i_th_environ (i: INTEGER; p: POINTER): POINTER
190			-- Environment variable at `i'-th position of `p'.
191		require
192			i_valid: i >=0
193		external
194			"C inline use <string.h>"
195		alias
196			"return ((char **)$p)[$i];"
197		end
198
199	separated_variables (a_var: STRING): detachable TUPLE [value: STRING; key: STRING]
200			-- Given an environment variable `a_var' in form of "key=value",
201			-- return separated key and value.
202			-- Return Void if `a_var' is in incorrect format.
203		require
204			a_var_attached: a_var /= Void
205		local
206			i, j: INTEGER
207			done: BOOLEAN
208		do
209			j := a_var.count
210			from
211				i := 1
212			until
213				i > j or done
214			loop
215				if a_var.item (i) = '=' then
216					done := True
217				else
218					i := i + 1
219				end
220			end
221			if i > 1 and then i < j then
222				Result := [a_var.substring (i + 1, j), a_var.substring (1, i - 1)]
223			end
224		end
225
226note
227	copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
228	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
229	source: "[
230			Eiffel Software
231			5949 Hollister Ave., Goleta, CA 93117 USA
232			Telephone 805-685-1006, Fax 805-685-6869
233			Website http://www.eiffel.com
234			Customer support http://support.eiffel.com
235		]"
236end