PageRenderTime 55ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/server/ewsgi/connectors/standalone/src/wgi_standalone_connector.e

http://github.com/EiffelWebFramework/EWF
Specman e | 296 lines | 209 code | 57 blank | 30 comment | 5 complexity | d64c197b92d61ab0cc28c2c20bbc7b38 MD5 | raw file
  1. note
  2. description: "[
  3. Standalone Web Server connector.
  4. ]"
  5. date: "$Date$"
  6. revision: "$Revision$"
  7. class
  8. WGI_STANDALONE_CONNECTOR [G -> WGI_EXECUTION create make end]
  9. inherit
  10. WGI_CONNECTOR
  11. create
  12. make,
  13. make_with_base
  14. feature {NONE} -- Initialization
  15. make
  16. -- Create current standalone connector.
  17. local
  18. fac: like request_handler_factory
  19. do
  20. -- Callbacks
  21. create on_launched_actions
  22. -- Server
  23. create <NONE> fac
  24. request_handler_factory := fac
  25. create server.make (fac)
  26. create <NONE> observer
  27. configuration := server_configuration (server)
  28. controller := server_controller (server)
  29. initialize_server (server)
  30. end
  31. make_with_base (a_base: detachable separate READABLE_STRING_8)
  32. -- Create current standalone connector with base url `a_base'
  33. require
  34. a_base_starts_with_slash: (a_base /= Void and then not a_base.is_empty) implies is_valid_base (a_base)
  35. do
  36. make
  37. set_base (a_base)
  38. end
  39. feature {NONE} -- Separate helper
  40. initialize_server (a_server: like server)
  41. do
  42. a_server.set_observer (observer)
  43. end
  44. update_factory (conn: detachable separate WGI_STANDALONE_CONNECTOR [G]; fac: separate WGI_HTTPD_REQUEST_HANDLER_FACTORY [G]; a_conf: like configuration)
  45. do
  46. fac.update_with (conn, a_conf)
  47. end
  48. server_configuration (a_server: like server): like configuration
  49. do
  50. Result := a_server.configuration
  51. end
  52. feature -- Access
  53. name: STRING_8
  54. -- Name of Current connector
  55. once
  56. Result := "httpd"
  57. end
  58. version: STRING_8
  59. -- Version of Current connector
  60. once
  61. Result := "1.0"
  62. end
  63. feature -- Access
  64. server: separate HTTPD_SERVER
  65. -- HTTPd server object.
  66. request_handler_factory: separate WGI_HTTPD_REQUEST_HANDLER_FACTORY [G]
  67. -- Factory for request handlers.
  68. controller: separate HTTPD_CONTROLLER
  69. -- Controller used to shutdown server.
  70. observer: separate WGI_STANDALONE_SERVER_OBSERVER
  71. -- Observer providing information related to port number, and server status.
  72. configuration: separate HTTPD_CONFIGURATION
  73. -- Server configuration.
  74. feature -- Access
  75. base: detachable READABLE_STRING_8
  76. -- Root url base
  77. feature -- Status report
  78. launched: BOOLEAN
  79. -- Server launched and listening on `port'
  80. port: INTEGER
  81. -- Listening port.
  82. --| 0: not launched
  83. feature -- Callbacks
  84. on_launched_actions: ACTION_SEQUENCE [TUPLE [WGI_STANDALONE_CONNECTOR [WGI_EXECUTION]]]
  85. -- Actions triggered when launched.
  86. -- WARNING: only supported for now with SCOOP concurrency mode. [2016-10-07]
  87. feature -- Event
  88. on_launched (a_port: INTEGER)
  89. -- Server launched
  90. do
  91. launched := True
  92. port := a_port
  93. on_launched_actions.call ([Current])
  94. end
  95. feature -- Element change
  96. set_base (v: detachable separate READABLE_STRING_8)
  97. -- Set base url `base' to `v'.
  98. require
  99. b_starts_with_slash: (v /= Void and then not v.is_empty) implies is_valid_base (v)
  100. do
  101. if v = Void then
  102. base := Void
  103. else
  104. create {STRING_8} base.make_from_separate (v)
  105. end
  106. ensure
  107. valid_base: (attached base as l_base and then not l_base.is_empty) implies l_base.starts_with ("/")
  108. end
  109. is_valid_base (v: separate READABLE_STRING_8): BOOLEAN
  110. do
  111. Result := not v.is_whitespace and then v[1] = '/'
  112. end
  113. set_port_number (a_port_number: INTEGER)
  114. -- Set port number to `a_port_number'.
  115. require
  116. a_port_number_positive_or_zero: a_port_number >= 0
  117. do
  118. set_port_on_configuration (a_port_number, configuration)
  119. end
  120. set_socket_recv_timeout (a_nb_seconds: INTEGER)
  121. require
  122. a_nb_seconds_positive_or_zero: a_nb_seconds >= 0
  123. do
  124. set_socket_recv_timeout_on_configuration (a_nb_seconds, configuration)
  125. end
  126. set_max_concurrent_connections (nb: INTEGER)
  127. -- Set maximum concurrent connections to `nb'.
  128. require
  129. nb_positive_or_zero: nb >= 0
  130. do
  131. set_max_concurrent_connections_on_configuration (nb, configuration)
  132. end
  133. set_is_verbose (b: BOOLEAN)
  134. -- Set verbose mode.
  135. do
  136. set_is_verbose_on_configuration (b, configuration)
  137. end
  138. set_is_secure (b: BOOLEAN)
  139. -- Set is_secure connection mode.
  140. -- i.e: using SSL.
  141. do
  142. set_is_secure_on_configuration (b, configuration)
  143. end
  144. feature -- Server
  145. launch_on_port (a_port_number: INTEGER)
  146. -- Launch server listening on port `a_port_number'.
  147. do
  148. set_port_number (a_port_number)
  149. launch
  150. end
  151. launch
  152. -- Launch web server listening.
  153. do
  154. launched := False
  155. port := 0
  156. launch_server (server)
  157. on_server_started (observer)
  158. end
  159. shutdown_server
  160. -- Shutdown web server listening.
  161. do
  162. if launched then
  163. -- FIXME: prevent multiple calls (otherwise it hangs) [2015-03-27]
  164. separate_shutdown_server_on_controller (controller)
  165. end
  166. end
  167. feature -- Events
  168. on_server_started (obs: like observer)
  169. -- Server started and listeing on port `obs.port'.
  170. require
  171. obs.started -- SCOOP wait condition.
  172. do
  173. -- FIXME: this works only with SCOOP concurrency mode. [2016-10-07]
  174. if obs.port > 0 then
  175. on_launched (obs.port)
  176. end
  177. end
  178. feature {NONE} -- Implementation
  179. server_controller (a_server: like server): separate HTTPD_CONTROLLER
  180. do
  181. Result := a_server.controller
  182. end
  183. apply_configuration (a_configuration: like configuration)
  184. local
  185. v: BOOLEAN
  186. do
  187. v := a_configuration.is_verbose
  188. if v then
  189. if attached base as l_base and then not l_base.is_whitespace then
  190. io.error.put_string ("Base=" + l_base + "%N")
  191. end
  192. end
  193. update_factory (Current, request_handler_factory, a_configuration)
  194. end
  195. launch_server (a_server: like server)
  196. do
  197. apply_configuration (a_server.configuration)
  198. a_server.launch
  199. end
  200. separate_server_terminated (a_server: like server): BOOLEAN
  201. do
  202. Result := a_server.is_terminated
  203. end
  204. separate_shutdown_server_on_controller (a_controller: separate HTTPD_CONTROLLER)
  205. do
  206. a_controller.shutdown
  207. end
  208. feature {NONE} -- Implementation: element change
  209. set_port_on_configuration (a_port_number: INTEGER; cfg: like configuration)
  210. do
  211. cfg.set_http_server_port (a_port_number)
  212. end
  213. set_socket_recv_timeout_on_configuration (a_nb_seconds: INTEGER; cfg: like configuration)
  214. do
  215. cfg.set_socket_recv_timeout (a_nb_seconds)
  216. end
  217. set_max_concurrent_connections_on_configuration (nb: INTEGER; cfg: like configuration)
  218. do
  219. cfg.set_max_concurrent_connections (nb)
  220. end
  221. set_is_verbose_on_configuration (b: BOOLEAN; cfg: like configuration)
  222. do
  223. cfg.set_is_verbose (b)
  224. end
  225. set_is_secure_on_configuration (b: BOOLEAN; cfg: like configuration)
  226. do
  227. cfg.set_is_secure (b)
  228. end
  229. note
  230. copyright: "2011-2017, Jocelyn Fiat, Javier Velilla, Eiffel Software and others"
  231. license: "Eiffel Forum License v2 (see http://www.eiffel.com/licensing/forum.txt)"
  232. source: "[
  233. Eiffel Software
  234. 5949 Hollister Ave., Goleta, CA 93117 USA
  235. Telephone 805-685-1006, Fax 805-685-6869
  236. Website http://www.eiffel.com
  237. Customer support http://support.eiffel.com
  238. ]"
  239. end