note description : "Basic Service that build a generic front end for the most used search engines." date : "$Date$" revision : "$Revision$" class APPLICATION inherit WSF_DEFAULT_SERVICE redefine initialize end create make_and_launch feature {NONE} -- Initialization initialize -- Initialize current service. do set_service_option ("port", 9090) end feature -- Basic operations execute (req: WSF_REQUEST; res: WSF_RESPONSE) -- Execute the incomming request local l_message: STRING do -- To send a response we need to setup, the status code and -- the response headers. if req.is_get_request_method then if req.path_info.same_string ("/") then res.put_header ({HTTP_STATUS_CODE}.ok, <<["Content-Type", "text/html"], ["Content-Length", web_page.count.out]>>) res.put_string (web_page) else send_resouce_not_found (req, res) end elseif req.is_post_request_method then if req.path_info.same_string ("/search") then if attached {WSF_STRING} req.form_parameter ("query") as l_query then if attached {WSF_STRING} req.form_parameter ("engine") as l_engine then if attached {STRING} map.at (l_engine.value) as l_engine_url then l_engine_url.append (l_query.value) send_redirect (req, res, l_engine_url) -- res.redirect_now (l_engine_url) else send_bad_request (req, res, " search engine: " + l_engine.value + " not supported,
try with Google or Bing") end else send_bad_request (req, res, " search engine not selected") end else send_bad_request (req, res, " form_parameter query is not present") end else send_resouce_not_found (req, res) end else create l_message.make_from_string (message_template) l_message.replace_substring_all ("$title", "Method Not Allowed") l_message.replace_substring_all ("$status", "Method Not Allowed 405") -- Method not allowed res.put_header ({HTTP_STATUS_CODE}.method_not_allowed, <<["Content-Type", "text/html"], ["Content-Length", l_message.count.out]>>) res.put_string (l_message) end end feature -- Engine Map map : STRING_TABLE[STRING] do create Result.make (2) Result.put ("http://www.google.com/search?q=", "Google") Result.put ("http://www.bing.com/search?q=", "Bing") end feature -- Redirect send_redirect (req: WSF_REQUEST; res: WSF_RESPONSE; a_location: READABLE_STRING_32) -- Redirect to `a_location' local h: HTTP_HEADER do create h.make h.put_content_type_text_html h.put_current_date h.put_location (a_location) res.set_status_code ({HTTP_STATUS_CODE}.see_other) res.put_header_text (h.string) end feature -- Bad Request send_bad_request (req: WSF_REQUEST; res: WSF_RESPONSE; description: STRING) local l_message: STRING do create l_message.make_from_string (message_template) l_message.replace_substring_all ("$title", "Bad Request") l_message.replace_substring_all ("$status", "Bad Request" + description) res.put_header ({HTTP_STATUS_CODE}.bad_request, <<["Content-Type", "text/html"], ["Content-Length", l_message.count.out]>>) res.put_string (l_message) end feature -- Resource not found send_resouce_not_found (req: WSF_REQUEST; res: WSF_RESPONSE) local l_message: STRING do create l_message.make_from_string (message_template) l_message.replace_substring_all ("$title", "Resource not found") l_message.replace_substring_all ("$status", "Resource " + req.request_uri + " not found 404") res.put_header ({HTTP_STATUS_CODE}.not_found, <<["Content-Type", "text/html"], ["Content-Length", l_message.count.out]>>) res.put_string (l_message) end feature -- Home Page web_page: STRING = "[ Generic Search Engine

Generic Search Engine

Search:

]" feature -- Generic Message message_template: STRING="[ $title

This page is an example of $status

]" end