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