From 694c0a193c572a910ab1d0c20b3fc3e58d9da8db Mon Sep 17 00:00:00 2001 From: Jocelyn Fiat Date: Wed, 27 Jul 2011 15:11:07 +0200 Subject: [PATCH] removed implementation from APPLICATION , RESPONSE and REQUEST classes --- library/server/ewsgi/src/gw_application.e | 13 +---- .../src/implementation/gw_application_imp.e | 18 +++++- .../in_memory/gw_in_memory_response.e | 13 ++--- .../server/ewsgi/src/response/gw_response.e | 51 +++-------------- .../ewsgi/src/response/gw_response_imp.e | 56 ++++++++++++++++++- 5 files changed, 85 insertions(+), 66 deletions(-) diff --git a/library/server/ewsgi/src/gw_application.e b/library/server/ewsgi/src/gw_application.e index be0b0c01..45f8936e 100644 --- a/library/server/ewsgi/src/gw_application.e +++ b/library/server/ewsgi/src/gw_application.e @@ -20,7 +20,7 @@ feature -- Process request if not rescued then pre_execute (env) req := new_request (env, a_input) - res := new_response (a_output) + res := new_response (req, a_output) execute (req, res) post_execute (req, res) else @@ -56,13 +56,6 @@ feature {NONE} -- Execution rescue_execute (req: detachable GW_REQUEST; res: detachable GW_RESPONSE; a_exception: detachable EXCEPTION) -- Operation processed on rescue of `execute' do - if - req /= Void and res /= Void - and a_exception /= Void and then attached a_exception.exception_trace as l_trace - then - res.write_header ({HTTP_STATUS_CODE}.internal_server_error, Void) - res.write_string ("
" + l_trace + "
") - end post_execute (req, res) end @@ -76,8 +69,8 @@ feature -- Factory deferred end - new_response (a_output: GW_OUTPUT_STREAM): GW_RESPONSE - -- New Response based on `a_output' + new_response (req: GW_REQUEST; a_output: GW_OUTPUT_STREAM): GW_RESPONSE + -- New Response based on `req' and `a_output' --| note: you can redefine this function to create your own --| descendant of GW_RESPONSE , or even to reuse/recycle existing --| instance of GW_RESPONSE diff --git a/library/server/ewsgi/src/implementation/gw_application_imp.e b/library/server/ewsgi/src/implementation/gw_application_imp.e index 7c80b53e..a32fe8f3 100644 --- a/library/server/ewsgi/src/implementation/gw_application_imp.e +++ b/library/server/ewsgi/src/implementation/gw_application_imp.e @@ -11,7 +11,8 @@ deferred class inherit GW_APPLICATION redefine - process + process, + rescue_execute end feature -- Access @@ -28,6 +29,19 @@ feature -- Execution Precursor (env, a_input, a_output) end + rescue_execute (req: detachable GW_REQUEST; res: detachable GW_RESPONSE; a_exception: detachable EXCEPTION) + -- Operation processed on rescue of `execute' + do + if + req /= Void and res /= Void + and a_exception /= Void and then attached a_exception.exception_trace as l_trace + then + res.write_header ({HTTP_STATUS_CODE}.internal_server_error, Void) + res.write_string ("
" + l_trace + "
") + end + Precursor (req, res, a_exception) + end + feature -- Factory new_request (env: GW_ENVIRONMENT; a_input: GW_INPUT_STREAM): GW_REQUEST @@ -36,7 +50,7 @@ feature -- Factory Result.execution_variables.set_variable (request_count.out, "REQUEST_COUNT") end - new_response (a_output: GW_OUTPUT_STREAM): GW_RESPONSE + new_response (req: GW_REQUEST; a_output: GW_OUTPUT_STREAM): GW_RESPONSE do create {GW_RESPONSE_IMP} Result.make (a_output) end diff --git a/library/server/ewsgi/src/implementation/in_memory/gw_in_memory_response.e b/library/server/ewsgi/src/implementation/in_memory/gw_in_memory_response.e index bfea2c51..9a5bcd4e 100644 --- a/library/server/ewsgi/src/implementation/in_memory/gw_in_memory_response.e +++ b/library/server/ewsgi/src/implementation/in_memory/gw_in_memory_response.e @@ -28,15 +28,7 @@ feature {NONE} -- Initialization body: STRING_8 -feature -- Status setting - - set_status_code (c: INTEGER) - -- Set the status code of the response - do - header.put_status (c) - end - -feature -- Output operation +feature {NONE} -- Status output write (s: STRING) -- Send the content of `s' @@ -44,6 +36,8 @@ feature -- Output operation body.append (s) end +feature -- Output operation + write_file_content (fn: STRING) -- Send the content of file `fn' local @@ -73,6 +67,7 @@ feature {GW_APPLICATION} -- Commit commit (a_output: GW_OUTPUT_STREAM) do + a_output.put_status (status_code) header.send_to (a_output) write (body) Precursor (a_output) diff --git a/library/server/ewsgi/src/response/gw_response.e b/library/server/ewsgi/src/response/gw_response.e index ce5e85d0..e77f8376 100644 --- a/library/server/ewsgi/src/response/gw_response.e +++ b/library/server/ewsgi/src/response/gw_response.e @@ -11,9 +11,9 @@ feature {GW_APPLICATION} -- Commit commit (a_output_stream: GW_OUTPUT_STREAM) -- Commit the current response - do - --| To be redefined as needed, to flush, or what you need... - a_output_stream.flush + deferred + ensure + status_set: is_status_set end feature {NONE} -- Core output operation @@ -27,8 +27,8 @@ feature {NONE} -- Core output operation feature -- Status setting is_status_set: BOOLEAN - do - Result := status_code /= 0 + -- Is status set? + deferred end set_status_code (a_code: INTEGER) @@ -36,23 +36,15 @@ feature -- Status setting -- Should be done before sending any data back to the client require status_not_set: not is_status_set - do - status_code := a_code - write_status (a_code) + deferred ensure + status_code_set: status_code = a_code status_set: is_status_set end status_code: INTEGER -- Response status - -feature {NONE} -- Status output - - write_status (a_code: INTEGER) - -- Send status line for `a_code' deferred - ensure - status_set: is_status_set end feature -- Output operation @@ -61,8 +53,7 @@ feature -- Output operation -- Send the string `s' require status_set: is_status_set - do - write (s) + deferred end write_file_content (fn: STRING) @@ -74,35 +65,11 @@ feature -- Output operation feature -- Header output operation - write_header_object (h: GW_HEADER) - -- Send `header' to `output'. - require - status_set: is_status_set - deferred - end - write_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [key: STRING; value: STRING]]) -- Send headers with status `a_status', and headers from `a_headers' require status_not_set: not is_status_set - local - h: GW_HEADER - i,n: INTEGER - do - set_status_code (a_status_code) - create h.make - if a_headers /= Void then - from - i := a_headers.lower - n := a_headers.upper - until - i > n - loop - h.put_header_key_value (a_headers[i].key, a_headers[i].value) - i := i + 1 - end - end - write_header_object (h) + deferred ensure status_set: is_status_set end diff --git a/library/server/ewsgi/src/response/gw_response_imp.e b/library/server/ewsgi/src/response/gw_response_imp.e index b6a74dfc..480d6a48 100644 --- a/library/server/ewsgi/src/response/gw_response_imp.e +++ b/library/server/ewsgi/src/response/gw_response_imp.e @@ -20,6 +20,14 @@ feature {NONE} -- Initialization output := a_output end +feature {GW_APPLICATION} -- Commit + + commit (a_output_stream: GW_OUTPUT_STREAM) + -- Commit the current response + do + a_output_stream.flush + end + feature {NONE} -- Core output operation write (s: STRING) @@ -28,28 +36,70 @@ feature {NONE} -- Core output operation output.put_string (s) end -feature {NONE} -- Status output +feature -- Status setting - write_status (a_code: INTEGER) - -- Send status line for `a_code' + is_status_set: BOOLEAN do + Result := status_code /= 0 + end + + set_status_code (a_code: INTEGER) + -- Set response status code + -- Should be done before sending any data back to the client + do + status_code := a_code output.put_status_line (a_code) end + status_code: INTEGER + -- Response status + feature -- Output operation + write_string (s: STRING) + -- Send the string `s' + do + write (s) + end + write_file_content (fn: STRING) -- Send the content of file `fn' do output.put_file_content (fn) end +feature -- Header output operation + write_header_object (h: GW_HEADER) -- Send `header' to `output'. + require + status_set: is_status_set do h.send_to (Current) end + write_header (a_status_code: INTEGER; a_headers: detachable ARRAY [TUPLE [key: STRING; value: STRING]]) + -- Send headers with status `a_status', and headers from `a_headers' + local + h: GW_HEADER + i,n: INTEGER + do + set_status_code (a_status_code) + create h.make + if a_headers /= Void then + from + i := a_headers.lower + n := a_headers.upper + until + i > n + loop + h.put_header_key_value (a_headers[i].key, a_headers[i].value) + i := i + 1 + end + end + write_header_object (h) + end + feature {NONE} -- Implementation: Access output: GW_OUTPUT_STREAM