PageRenderTime 87ms CodeModel.GetById 81ms app.highlight 3ms RepoModel.GetById 0ms app.codeStats 1ms

/library/server/fcgi/implementation/windows/fcgi_imp.e

http://github.com/jocelyn/EiffelWebReloaded
Specman e | 267 lines | 83 code | 30 blank | 154 comment | 2 complexity | b70e64ea6df94e597028fc9075623dcb MD5 | raw file
  1note
  2	description: "Implementation for the FCGI_I interface"
  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_IMP
  9
 10inherit
 11	FCGI_I
 12
 13feature {NONE} -- Initialization
 14
 15	make
 16			-- Initialize FCGI interface
 17		do
 18			create fcgi
 19		end
 20
 21	fcgi: FCGI_C_API
 22			-- FastCGI C API primitives
 23
 24feature -- Access
 25
 26	fcgi_environ: POINTER
 27		do
 28			Result := fcgi.environ
 29		end
 30
 31--	updated_environ_variables: HASH_TABLE [STRING, STRING]
 32--		local
 33----			n, l_size,
 34--			i: INTEGER
 35--			p, v, null: POINTER
 36--		do
 37----			update_eif_environ
 38----			Result := starting_environment_variables
 39--
 40----			p := environ_strings_pointer ($n)
 41----			from
 42----				i := 1
 43----				l_size := 0
 44----				create Result.make (n)
 45----			until
 46----				i > n
 47----			loop
 48----				create s.make_from_c (p.plus (l_size))
 49----				l_size := l_size + s.count + 1
 50----				if attached separated_variables (s) as t then
 51----					Result.force (t.value, t.key)
 52----				end
 53----				i := i + 1
 54----			end
 55--
 56--			p := fcgi.environ
 57--			create Result.make (50)
 58--			if p /= null then
 59--				from
 60--					i := 0
 61--					v := fcgi_i_th_environ (i,p)
 62--				until
 63--					v = null
 64--				loop
 65--					if attached separated_variables (create {STRING}.make_from_c (v)) as t then
 66--						Result.force (t.value, t.key)
 67--					end
 68--					i := i + 1
 69--					v := fcgi_i_th_environ (i,p)
 70--				end
 71--			end
 72--		end
 73--
 74--	fcgi_i_th_environ (i: INTEGER; p: POINTER): POINTER
 75--			-- Environment variable at `i'-th position of `p'.
 76--		require
 77--			i_valid: i >=0
 78--		external
 79--			"C inline use <string.h>"
 80--		alias
 81--			"[
 82--				return ((char **)$p)[$i];
 83--			]"
 84--		end
 85
 86feature -- FCGI connection
 87
 88	fcgi_listen: INTEGER
 89			-- Listen to the FCGI input stream
 90			-- Return 0 for successful calls, -1 otherwise.
 91		do
 92			Result := fcgi.accept
 93		end
 94
 95--	update_eif_environ
 96--		external
 97--			"C inline use <string.h>"
 98--		alias
 99--			"[
100--				#ifdef EIF_WINDOWS
101--					#ifndef GetEnvironmentStringsA
102--					 extern LPVOID WINAPI GetEnvironmentStringsA(void);
103--					#endif	
104--					
105--					eif_environ = (char**) GetEnvironmentStringsA();
106--				#endif
107--			]"
108--		end
109
110	fcgi_finish
111			-- Finish current request from HTTP server started from
112			-- the most recent call to `fcgi_accept'.
113		do
114			fcgi.finish
115		end
116
117	set_fcgi_exit_status (v: INTEGER)
118		do
119			fcgi.set_exit_status (-2)
120		end
121
122feature -- FCGI output
123
124	put_string (a_str: STRING)
125			-- Put `a_str' on the FastCGI stdout.
126		local
127			l_c_str: C_STRING
128		do
129			l_c_str := c_buffer
130			l_c_str.set_string (a_str)
131			fcgi.put_string (l_c_str.item, l_c_str.count)
132		end
133
134feature -- FCGI input
135
136	copy_from_stdin (n: INTEGER; f: FILE)
137			-- Read up to n bytes from stdin and write to given file
138		local
139			l_c_str: C_STRING
140			num, readsize, writecount: INTEGER
141			done: BOOLEAN
142			l_fcgi: like fcgi
143		do
144			--put_trace ("copy_from_stdin, n=" +n.out)
145			readsize := n.min (K_input_bufsize)
146			--put_trace ("copy_from_stdin, readsize=" +readsize.out)
147			l_c_str := c_buffer
148			from
149				l_fcgi := fcgi
150			until done or writecount >= n
151			loop
152				num := l_fcgi.read_content_into (l_c_str.item, readsize)
153				--put_trace ("copy_from_stdin, num=" +num.out)
154				if num  = 0 then
155					-- EOF
156					done := True
157				else
158					f.put_managed_pointer (c_buffer.managed_data, 0, num)
159					writecount := writecount + num
160				end
161			end
162		end
163
164feature {NONE} -- Implementation: FCGI Input
165
166	fill_pointer_from_stdin (p: POINTER; n: INTEGER): INTEGER
167			-- Read up to `n' bytes from stdin and store in pointer `p'
168			-- and return number of bytes read.
169		do
170			Result := fcgi.read_content_into (p, n)
171		end
172
173feature -- I/O Routines
174
175--RFO	read_stdin_into (a_str: STRING)
176--RFO			-- Read a string from the `stdin' into `a_str'.
177--RFO		require
178--RFO			a_str_not_void: a_str /= Void
179--RFO		local
180--RFO			l_c_str: C_STRING
181--RFO			n: INTEGER
182--RFO		do
183--RFO			l_c_str := c_buffer
184--RFO			n := fcgi.read_content_into (l_c_str.item, l_c_str.capacity)
185--RFO			a_str.set_count (n)
186--RFO			l_c_str.read_substring_into (a_str, 1, n)
187--RFO		end
188
189--RFO	read_string_into (a_str: STRING)
190--RFO		require
191--RFO			exists: a_str /= Void
192--RFO		local
193--RFO			l_c_str: C_STRING
194--RFO			p: POINTER
195--RFO		do
196--RFO			create l_c_str.make_empty (1024)
197--RFO			p := fcgi.gets (l_c_str.item)
198--RFO--			if p /= default_pointer and p = l_c_str.item then
199--RFO				a_str.resize (l_c_str.count)
200--RFO				l_c_str.read_string_into (a_str)
201--RFO--			else
202--RFO--				put_error_line ("Bad pointer from gets")
203--RFO--			end
204--RFO		end
205
206
207--RFO	read_line
208--RFO			-- Read up to the next end of line, or end of input
209--RFO			-- Leave result in last_string
210--RFO			-- Newline character is absent from result
211--RFO		do
212--RFO			if last_string = Void then
213--RFO				create Result.make (K_input_bufsize)
214--RFO			else
215--RFO				last_string.wipe_out
216--RFO			end
217--RFO--			if input_filename /= Void then
218--RFO--				io.read_line
219--RFO--				last_string.append (io.last_string)
220--RFO--			else
221--RFO				read_string_into (last_string)
222--RFO--			end
223--RFO		end
224
225
226
227feature {NONE} -- Implementation: environment
228
229--	environ_strings_pointer (p_nb: TYPED_POINTER [INTEGER]): POINTER
230--			-- Environment variable strings returned by `GetEnvironmentStringsA'
231--			-- `p_nb' return the count of environment variables.
232--		external
233--			"C inline use <string.h>"
234--		alias
235--			"[
236--				#ifdef EIF_WINDOWS
237--					#ifndef GetEnvironmentStringsA
238--					 extern LPVOID WINAPI GetEnvironmentStringsA(void);
239--					#endif	
240--					
241--					int cnt = 0;
242--					LPSTR vars = GetEnvironmentStringsA();
243--					char** p = (char**) vars;
244--					
245--					for (; *vars; vars++) {
246--					   while (*vars) { vars++; }
247--					   cnt++;
248--					}
249--					
250--					*$p_nb = cnt;
251--					return (EIF_POINTER) p;
252--				#endif
253--			]"
254--		end
255
256
257note
258	copyright: "Copyright (c) 1984-2011, Eiffel Software and others"
259	license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
260	source: "[
261			Eiffel Software
262			5949 Hollister Ave., Goleta, CA 93117 USA
263			Telephone 805-685-1006, Fax 805-685-6869
264			Website http://www.eiffel.com
265			Customer support http://support.eiffel.com
266		]"
267end