Prepare a HttpResponse destined to the client in response to the request

request is fully formed request object with a reference to the session if one already exists.

truncated_uri is the ending of the full request URI, truncated from the route leading to this Action.

Property definitions

nitcorn :: reactor $ Action :: answer
	# Prepare a `HttpResponse` destined to the client in response to the `request`
	#
	# `request` is fully formed request object with a reference to the session
	# if one already exists.
	#
	# `truncated_uri` is the ending of the full request URI, truncated from the route
	# leading to this `Action`.
	fun answer(request: HttpRequest, truncated_uri: String): HttpResponse is abstract
lib/nitcorn/reactor.nit:100,2--107,82

github $ HookAction :: answer
	# Parse hook request then call `listener.apply_event`.
	redef fun answer(request, uri) do
		# get event type
		var kind = request.header.get_or_null("X-GitHub-Event")
		if kind == null then return new HttpResponse(403)
		# parse event
		var event = listener.event_factory(kind, request.body)
		if event == null then return new HttpResponse(403)
		listener.apply_event(event)
		return new HttpResponse(200)
	end
lib/github/hooks.nit:136,2--146,4

nitcorn $ FileServer :: answer
	redef fun answer(request, turi)
	do
		var response

		var local_file = root.join_path(turi.strip_start_slashes)
		local_file = local_file.simplify_path

		# Is it reachable?
		#
		# This make sure that the requested file is within the root folder.
		if (local_file + "/").has_prefix(root) then
			# Does it exists?
			var file_stat = local_file.file_stat
			if file_stat != null then
				if file_stat.is_dir then
					# If we target a directory without an ending `/`,
					# redirect to the directory ending with `/`.
					var uri = request.uri
					if not uri.is_empty and uri.chars.last != '/' then
						return answer_redirection(request.uri + "/")
					end

					# Show index file instead of the directory listing
					# only if `index.html` or `index.htm` is available
					var index_file = local_file.join_path("index.html")
					if index_file.file_exists then
						local_file = index_file
					else
						index_file = local_file.join_path("index.htm")
						if index_file.file_exists then local_file = index_file
					end
				end

				file_stat = local_file.file_stat
				if show_directory_listing and file_stat != null and file_stat.is_dir then
					response = answer_directory_listing(request, turi, local_file)
				else if file_stat != null and not file_stat.is_dir then # It's a single file
					response = answer_file(local_file)
				else response = answer_default
			else response = answer_default
		else response = new HttpResponse(403)

		if response.status_code != 200 then
			var tmpl = error_page(response.status_code)
			if header != null and tmpl isa ErrorTemplate then tmpl.header = header
			response.body = tmpl
		end

		return response
	end
lib/nitcorn/file_server.nit:83,2--132,4

nitcorn $ ProxyAction :: answer
	redef fun answer(http_request, turi) do

		var request = new CurlHTTPRequest("http://{self.target}{turi}")

		var curl_headers = new HeaderMap

		for k, v in http_request.header do
			if k == "User-Agent" then
				request.user_agent = v
			else
				curl_headers[k] = v
			end
		end

		var curl_response = request.execute
		var response: HttpResponse

		if curl_response isa CurlResponseSuccess then
			response = new HttpResponse(curl_response.status_code)
			response.body = curl_response.body_str
		else if curl_response isa CurlResponseFailed then
			response = new HttpResponse(500)

			# CURLE_COULDNT_RESOLVE_HOST
			if curl_response.error_code == 6 then
				response.status_code = 502 # Bad Gateway
			# CURLE_URL_MALFORMAT
			else if curl_response.error_code == 3 then
				response.status_code = 520 # Unknown Error
			# CURLE_COULDNT_CONNECT
			else if curl_response.error_code == 7 then
				response.status_code = 502 # Bad Gateway
			end
		else abort

		return response

	end
lib/nitcorn/proxy.nit:33,2--70,4

nitcorn $ RestfulAction :: answer
	redef fun answer(request, truncated_uri) do return new HttpResponse(400)
lib/nitcorn/restful.nit:67,2--73

nitcorn $ HTCPCPAction :: answer
	redef fun answer(http_request, turi) do
		var message: String
		var method = http_request.method
		var response: HttpResponse

		if is_teapot then
			response = new HttpResponse(418)
			response.body = "I'm a teapot!\n"
			response.header["Content-Type"] = "text"
			return response
		end

		if method == "POST" or method == "BREW" then
			if brewing then
				message = "Pot Busy"
				response = new HttpResponse(400)
			else
				message = "Brewing a new pot of coffee\n"
				brewing = true
				response = new HttpResponse(200)
			end
		else if method == "WHEN" then
			if brewing then
				message = "Stopped adding milk, your coffee is ready!\n"
				brewing = false
				response = new HttpResponse(200)
			else
				message = "There is no coffee brewing!\n"
				response = new HttpResponse(405)
			end
		else if method == "PROPFIND" or method == "GET" then
			if brewing then
				message = "The pot is busy\n"
			else
				message = "The pot is ready to brew more coffee\n"
			end
			response = new HttpResponse(200)
		else
			message = "Unknown method: {method}"
			brewing = false
			response = new HttpResponse(405)
		end

		response.header["Content-Type"] = "text"
		response.body = message

		return response
	end
lib/nitcorn/examples/src/htcpcp_server.nit:34,2--81,4

nitcorn $ ParamAction :: answer
	redef fun answer(http_request, turi)
	do
		var response = new HttpResponse(200)
		var name = http_request.param("name")
		if name == null then
			response.body = "No name..."
		else
			response.body = "Hello {name}"
		end
		return response
	end
lib/nitcorn/examples/src/nitcorn_hello_world.nit:57,2--67,4

nitcorn $ MyAction :: answer
	# Catch all other request
	redef fun answer(request, turi)
	do
		var resp = new HttpResponse(404)
		resp.body = "Fallback answer"
		return resp
	end
lib/nitcorn/examples/src/restful_annot.nit:89,2--95,4

popcorn :: popcorn $ App :: answer
	# Handle request from nitcorn
	redef fun answer(req, uri) do
		uri = uri.simplify_path
		var res = new HttpResponse(404)
		for route, handler in pre_handlers do
			handler.handle(route, uri, req, res)
		end
		for route, handler in handlers do
			handler.handle(route, uri, req, res)
			if res.sent then break
		end
		if not res.sent then
			res.send(error_tpl(res.status_code, res.status_message), 404)
		end
		for route, handler in post_handlers do
			handler.handle(route, uri, req, res)
		end
		res.session = req.session
		return res
	end
lib/popcorn/popcorn.nit:49,2--68,4