# HG changeset patch # User jbe # Date 1424175308 -3600 # Node ID 59f485dc48ea58416c83ebd7d8f79dce08d93ff1 # Parent fd542c33020bf9e0a1c701edeadc64e1afb13460 Removed method request:send_text_status_response(...); Added helloworld.lua diff -r fd542c33020b -r 59f485dc48ea example_application.lua --- a/example_application.lua Tue Feb 17 04:10:38 2015 +0100 +++ b/example_application.lua Tue Feb 17 13:15:08 2015 +0100 @@ -1,5 +1,7 @@ -- Moonbridge example application -- invoke with ./moonbridge example_application.lua +-- +-- see helloworld.lua for a simpler example local http = require "moonbridge_http" @@ -61,13 +63,11 @@ }, function(request) - local function file_not_found() - request:send_status("404 Not Found") - request:send_header("Content-Type", "text/html; chatset=UTF-8") - request:send_data("\n") - request:send_data("404 Not Found\n") - request:send_data("

404 Not Found

\n") - request:send_data("\n") + local function error_response(status) + request:send_status(status) + request:send_header("Content-Type", "text/html") + request:send_data("\n", status, "\n

", status, "

\n\n") + request:finish() end if request.method == "GET" or request.method == "HEAD" then @@ -89,7 +89,7 @@ end request:send_data(document) else - file_not_found() + error_response("404 Not Found") end end @@ -137,12 +137,12 @@ request:send_data("\n\n") else - file_not_found() + error_response("404 Not Found") end else - request:send_text_status_response("405 Method not allowed") + error_response("405 Method not allowed") end diff -r fd542c33020b -r 59f485dc48ea helloworld.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/helloworld.lua Tue Feb 17 13:15:08 2015 +0100 @@ -0,0 +1,33 @@ +-- minimal example application for Moonbridge +-- invoke with ./moonbridge helloworld.lua +-- +-- see example_application.lua for a more elaborated example + +local http = require "moonbridge_http" + +listen{ + { proto = "tcp4", port = 8080, localhost = true }, + { proto = "tcp6", port = 8080, localhost = true }, + connect = http.generate_handler( + function(request) + local function error_response(status) + request:send_status(status) + request:send_header("Content-Type", "text/html") + request:send_data("\n", status, "\n

", status, "

\n\n") + end + if request.method == "GET" or request.method == "HEAD" then + if request.path == "" then + request:send_status("200 OK") + request:send_header("Content-Type", "text/html; charset=UTF-8") + request:send_data("\nHello World Application\nHello World!\n\n") + else + error_response("404 Not Found") + end + else + error_response("405 Method not allowed") + end + return true + end + ) +} + diff -r fd542c33020b -r 59f485dc48ea moonbridge_http.lua --- a/moonbridge_http.lua Tue Feb 17 04:10:38 2015 +0100 +++ b/moonbridge_http.lua Tue Feb 17 13:15:08 2015 +0100 @@ -753,17 +753,6 @@ read_body_bytes(request_body_content_length, callback) end input_state = "finished" - end, - -- helper function to send simple status responses, - -- e.g. to send errors for malformed requests, etc.: - send_text_status_response = function(self, status, text) - request:send_status(status) - request:send_header("Content-Type", "text/plain") - request:send_data(status, "\n") - if text then - request:send_data("\n", text, "\n") - end - request:finish() end } -- initialize tables for GET params in request object: @@ -787,9 +776,8 @@ end end }) - -- function identical to request:send_text_status_response(...) - -- but enforcing a connection close after sending the response, and - -- returning the boolean value of the "survive" variable: + -- sends a minimalistic error response and enforces closing of the + -- connection and returns the boolean value "survive" local function error_response(...) request:send_status(status) request:send_header("Content-Type", "text/plain") diff -r fd542c33020b -r 59f485dc48ea reference.txt --- a/reference.txt Tue Feb 17 04:10:38 2015 +0100 +++ b/reference.txt Tue Feb 17 13:15:08 2015 +0100 @@ -361,16 +361,6 @@ sent. -### request:send_text_status_response(status_string, text) - -Sends a HTTP status plus a response body of content-type "text/plain" and -finishes the request using request:finish(). The status_string has to be -provided in the same format as expected by request:send_status(...) and will -be included in the response body as plain text. The additional second "text" -argument will be appended to the reponse body (separated by an empty line) if -given. - - ### request.socket The underlaying socket. Can be used to force a TCP RST, etc.