|
| 1 | +note |
| 2 | + description: "Summary description for {APP_EMBEDDED_WEB_SERVICE}." |
| 3 | + author: "" |
| 4 | + date: "$Date$" |
| 5 | + revision: "$Revision$" |
| 6 | + |
| 7 | +class |
| 8 | + APP_EMBEDDED_WEB_SERVICE |
| 9 | + |
| 10 | +inherit |
| 11 | + EMBEDDED_WEB_SERVICE |
| 12 | + redefine |
| 13 | + make |
| 14 | + end |
| 15 | + |
| 16 | +create |
| 17 | + make |
| 18 | + |
| 19 | +feature {NONE} -- Initialization |
| 20 | + |
| 21 | + make |
| 22 | + do |
| 23 | + Precursor |
| 24 | + create request_exit_operation_actions |
| 25 | + local_connection_restriction_enabled := True |
| 26 | + end |
| 27 | + |
| 28 | +feature -- Execution |
| 29 | + |
| 30 | + request_exit_operation_actions: ACTION_SEQUENCE [TUPLE] |
| 31 | + |
| 32 | + execute (req: WSF_REQUEST; res: WSF_RESPONSE) |
| 33 | + -- Execute the request |
| 34 | + -- See `req.input' for input stream |
| 35 | + -- `req.meta_variables' for the CGI meta variable |
| 36 | + -- and `res' for output buffer |
| 37 | + local |
| 38 | + router: WSF_ROUTER |
| 39 | + sess: detachable WSF_ROUTER_SESSION |
| 40 | + m: WSF_HTML_PAGE_RESPONSE |
| 41 | + b: STRING |
| 42 | + fs: WSF_FILE_SYSTEM_HANDLER |
| 43 | + do |
| 44 | + create router.make (3) |
| 45 | + router.handle ("/test/{var}", create {WSF_URI_TEMPLATE_AGENT_HANDLER}.make (agent handle_test)) |
| 46 | + router.handle ("/env", create {WSF_URI_AGENT_HANDLER}.make (agent handle_env)) |
| 47 | + router.handle ("/exit", create {WSF_URI_TEMPLATE_AGENT_HANDLER}.make (agent handle_exit)) |
| 48 | + create fs.make_with_path ((create {EXECUTION_ENVIRONMENT}).current_working_path.extended ("files")) |
| 49 | + router.handle ("/files", fs) |
| 50 | + create sess |
| 51 | + router.dispatch (req, res, sess) |
| 52 | + if not sess.dispatched then |
| 53 | + create m.make |
| 54 | + create b.make_from_string ("<h1>Hello Eiffel desktop user</h1>") |
| 55 | + b.append ("<li><a href=%"" + req.script_url ("/test/start") + "%">test</a></li>") |
| 56 | + b.append ("<li><a href=%"" + req.script_url ("/env") + "%">env</a></li>") |
| 57 | + b.append ("<li><a href=%"" + req.script_url ("/files") + "%">files</a></li>") |
| 58 | + b.append ("<li><a href=%"" + req.script_url ("/exit") + "%">exit</a></li>") |
| 59 | + m.set_body (b) |
| 60 | + res.send (m) |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + handle_test (req: WSF_REQUEST; res: WSF_RESPONSE) |
| 65 | + local |
| 66 | + m: WSF_HTML_PAGE_RESPONSE |
| 67 | + b: STRING |
| 68 | + l_name: READABLE_STRING_32 |
| 69 | + do |
| 70 | + if attached {WSF_STRING} req.item ("var") as p_name then |
| 71 | + l_name := p_name.value |
| 72 | + else |
| 73 | + l_name := {STRING_32} "Embedded web service and web_browser in vision2 application" |
| 74 | + end |
| 75 | + create m.make |
| 76 | + create b.make_from_string ("<h1>This is a test about "+ m.html_encoded_string (l_name) +"</h1>") |
| 77 | + b.append ("<li><a href=%"" + req.script_url ("/") + "%">back to home</a></li>") |
| 78 | + if l_name.is_case_insensitive_equal_general ("start") then |
| 79 | + b.append ("<li><a href=%"" + req.script_url ("/test/js") + "%">test javascript+ajax</a></li>") |
| 80 | + elseif l_name.is_case_insensitive_equal_general ("js") then |
| 81 | + b.append ("[ |
| 82 | + <div id="myDiv"><h2>Let AJAX change this text</h2> |
| 83 | + <button type="button" onclick="loadXMLDoc()">Change Content</button> |
| 84 | + </div> |
| 85 | + ]") |
| 86 | + m.add_javascript_content ("[ |
| 87 | + function loadXMLDoc() |
| 88 | + { |
| 89 | + var xmlhttp; |
| 90 | + if (window.XMLHttpRequest) |
| 91 | + {// code for IE7+, Firefox, Chrome, Opera, Safari |
| 92 | + xmlhttp=new XMLHttpRequest(); |
| 93 | + } |
| 94 | + else |
| 95 | + {// code for IE6, IE5 |
| 96 | + xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); |
| 97 | + } |
| 98 | + xmlhttp.onreadystatechange=function() |
| 99 | + { |
| 100 | + if (xmlhttp.readyState==4 && xmlhttp.status==200) |
| 101 | + { |
| 102 | + document.getElementById("myDiv").innerHTML=xmlhttp.responseText; |
| 103 | + } |
| 104 | + } |
| 105 | + xmlhttp.open("GET","/test/ajax.txt",true); |
| 106 | + xmlhttp.send(); |
| 107 | + } |
| 108 | + ]") |
| 109 | + elseif l_name.is_case_insensitive_equal_general ("ajax.txt") then |
| 110 | + b := "This is AJAX response ... from " + req.absolute_script_url ("") |
| 111 | + end |
| 112 | + m.set_body (b) |
| 113 | + res.send (m) |
| 114 | + end |
| 115 | + |
| 116 | + handle_env (req: WSF_REQUEST; res: WSF_RESPONSE) |
| 117 | + local |
| 118 | + s: STRING_8 |
| 119 | + p: WSF_PAGE_RESPONSE |
| 120 | + v: STRING_8 |
| 121 | + do |
| 122 | + create s.make (2048) |
| 123 | + s.append ("**DEBUG**%N") |
| 124 | + req.set_raw_input_data_recorded (True) |
| 125 | + |
| 126 | + append_iterable_to ("Meta variables:", req.meta_variables, s) |
| 127 | + s.append_character ('%N') |
| 128 | + |
| 129 | + append_iterable_to ("Path parameters", req.path_parameters, s) |
| 130 | + s.append_character ('%N') |
| 131 | + |
| 132 | + append_iterable_to ("Query parameters", req.query_parameters, s) |
| 133 | + s.append_character ('%N') |
| 134 | + |
| 135 | + append_iterable_to ("Form parameters", req.form_parameters, s) |
| 136 | + s.append_character ('%N') |
| 137 | + |
| 138 | + if attached req.content_type as l_type then |
| 139 | + s.append ("Content: type=" + l_type.debug_output) |
| 140 | + s.append (" length=") |
| 141 | + s.append_natural_64 (req.content_length_value) |
| 142 | + s.append_character ('%N') |
| 143 | + create v.make (req.content_length_value.to_integer_32) |
| 144 | + req.read_input_data_into (v) |
| 145 | + across |
| 146 | + v.split ('%N') as v_cursor |
| 147 | + loop |
| 148 | + s.append (" |") |
| 149 | + s.append (v_cursor.item) |
| 150 | + s.append_character ('%N') |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + create p.make_with_body (s) |
| 155 | + p.header.put_content_type_text_plain |
| 156 | + res.send (p) |
| 157 | + end |
| 158 | + |
| 159 | + handle_exit (req: WSF_REQUEST; res: WSF_RESPONSE) |
| 160 | + local |
| 161 | + m: WSF_HTML_PAGE_RESPONSE |
| 162 | + b: STRING |
| 163 | + do |
| 164 | + create m.make |
| 165 | + create b.make_from_string ("<h1>Embedded server is about to shutdown</h1>") |
| 166 | + b.append ("<li><a href=%"" + req.script_url ("/") + "%">back to home</a></li>") |
| 167 | + m.set_body (b) |
| 168 | + res.send (m) |
| 169 | + if attached {WGI_NINO_CONNECTOR} req.wgi_connector as nino then |
| 170 | + nino.server.shutdown_server |
| 171 | + end |
| 172 | + request_exit_operation_actions.call (Void) |
| 173 | + end |
| 174 | + |
| 175 | +feature {NONE} -- Implementation |
| 176 | + |
| 177 | + append_iterable_to (a_title: READABLE_STRING_8; it: detachable ITERABLE [WSF_VALUE]; s: STRING_8) |
| 178 | + local |
| 179 | + n: INTEGER |
| 180 | + t: READABLE_STRING_8 |
| 181 | + v: READABLE_STRING_8 |
| 182 | + do |
| 183 | + s.append (a_title) |
| 184 | + s.append_character (':') |
| 185 | + if it /= Void then |
| 186 | + across it as c loop |
| 187 | + n := n + 1 |
| 188 | + end |
| 189 | + if n = 0 then |
| 190 | + s.append (" empty") |
| 191 | + s.append_character ('%N') |
| 192 | + else |
| 193 | + s.append_character ('%N') |
| 194 | + across |
| 195 | + it as c |
| 196 | + loop |
| 197 | + s.append (" - ") |
| 198 | + s.append (c.item.url_encoded_name) |
| 199 | + t := c.item.generating_type |
| 200 | + if t.same_string ("WSF_STRING") then |
| 201 | + else |
| 202 | + s.append_character (' ') |
| 203 | + s.append_character ('{') |
| 204 | + s.append (t) |
| 205 | + s.append_character ('}') |
| 206 | + end |
| 207 | + s.append_character ('=') |
| 208 | + v := c.item.string_representation.as_string_8 |
| 209 | + if v.has ('%N') then |
| 210 | + s.append_character ('%N') |
| 211 | + across |
| 212 | + v.split ('%N') as v_cursor |
| 213 | + loop |
| 214 | + s.append (" |") |
| 215 | + s.append (v_cursor.item) |
| 216 | + s.append_character ('%N') |
| 217 | + end |
| 218 | + else |
| 219 | + s.append (v) |
| 220 | + s.append_character ('%N') |
| 221 | + end |
| 222 | + end |
| 223 | + end |
| 224 | + else |
| 225 | + s.append (" none") |
| 226 | + s.append_character ('%N') |
| 227 | + end |
| 228 | + end |
| 229 | + |
| 230 | +end |
0 commit comments