moonbridge
changeset 33:59f485dc48ea v0.3.0
Removed method request:send_text_status_response(...); Added helloworld.lua
author | jbe |
---|---|
date | Tue Feb 17 13:15:08 2015 +0100 (2015-02-17) |
parents | fd542c33020b |
children | de20248b53c2 |
files | example_application.lua helloworld.lua moonbridge_http.lua reference.txt |
line diff
1.1 --- a/example_application.lua Tue Feb 17 04:10:38 2015 +0100 1.2 +++ b/example_application.lua Tue Feb 17 13:15:08 2015 +0100 1.3 @@ -1,5 +1,7 @@ 1.4 -- Moonbridge example application 1.5 -- invoke with ./moonbridge example_application.lua 1.6 +-- 1.7 +-- see helloworld.lua for a simpler example 1.8 1.9 local http = require "moonbridge_http" 1.10 1.11 @@ -61,13 +63,11 @@ 1.12 }, 1.13 function(request) 1.14 1.15 - local function file_not_found() 1.16 - request:send_status("404 Not Found") 1.17 - request:send_header("Content-Type", "text/html; chatset=UTF-8") 1.18 - request:send_data("<html>\n") 1.19 - request:send_data("<head><title>404 Not Found</title></head>\n") 1.20 - request:send_data("<body><h1>404 Not Found</h1></body>\n") 1.21 - request:send_data("</html>\n") 1.22 + local function error_response(status) 1.23 + request:send_status(status) 1.24 + request:send_header("Content-Type", "text/html") 1.25 + request:send_data("<html>\n<head><title>", status, "</title></head>\n<body><h1>", status, "</h1></body>\n</html>\n") 1.26 + request:finish() 1.27 end 1.28 1.29 if request.method == "GET" or request.method == "HEAD" then 1.30 @@ -89,7 +89,7 @@ 1.31 end 1.32 request:send_data(document) 1.33 else 1.34 - file_not_found() 1.35 + error_response("404 Not Found") 1.36 end 1.37 1.38 end 1.39 @@ -137,12 +137,12 @@ 1.40 request:send_data("</body>\n</html>\n") 1.41 1.42 else 1.43 - file_not_found() 1.44 + error_response("404 Not Found") 1.45 1.46 end 1.47 1.48 else 1.49 - request:send_text_status_response("405 Method not allowed") 1.50 + error_response("405 Method not allowed") 1.51 1.52 end 1.53
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/helloworld.lua Tue Feb 17 13:15:08 2015 +0100 2.3 @@ -0,0 +1,33 @@ 2.4 +-- minimal example application for Moonbridge 2.5 +-- invoke with ./moonbridge helloworld.lua 2.6 +-- 2.7 +-- see example_application.lua for a more elaborated example 2.8 + 2.9 +local http = require "moonbridge_http" 2.10 + 2.11 +listen{ 2.12 + { proto = "tcp4", port = 8080, localhost = true }, 2.13 + { proto = "tcp6", port = 8080, localhost = true }, 2.14 + connect = http.generate_handler( 2.15 + function(request) 2.16 + local function error_response(status) 2.17 + request:send_status(status) 2.18 + request:send_header("Content-Type", "text/html") 2.19 + request:send_data("<html>\n<head><title>", status, "</title></head>\n<body><h1>", status, "</h1></body>\n</html>\n") 2.20 + end 2.21 + if request.method == "GET" or request.method == "HEAD" then 2.22 + if request.path == "" then 2.23 + request:send_status("200 OK") 2.24 + request:send_header("Content-Type", "text/html; charset=UTF-8") 2.25 + request:send_data("<html>\n<head><title>Hello World Application</title></head>\n<body>Hello World!</body>\n</html>\n") 2.26 + else 2.27 + error_response("404 Not Found") 2.28 + end 2.29 + else 2.30 + error_response("405 Method not allowed") 2.31 + end 2.32 + return true 2.33 + end 2.34 + ) 2.35 +} 2.36 +
3.1 --- a/moonbridge_http.lua Tue Feb 17 04:10:38 2015 +0100 3.2 +++ b/moonbridge_http.lua Tue Feb 17 13:15:08 2015 +0100 3.3 @@ -753,17 +753,6 @@ 3.4 read_body_bytes(request_body_content_length, callback) 3.5 end 3.6 input_state = "finished" 3.7 - end, 3.8 - -- helper function to send simple status responses, 3.9 - -- e.g. to send errors for malformed requests, etc.: 3.10 - send_text_status_response = function(self, status, text) 3.11 - request:send_status(status) 3.12 - request:send_header("Content-Type", "text/plain") 3.13 - request:send_data(status, "\n") 3.14 - if text then 3.15 - request:send_data("\n", text, "\n") 3.16 - end 3.17 - request:finish() 3.18 end 3.19 } 3.20 -- initialize tables for GET params in request object: 3.21 @@ -787,9 +776,8 @@ 3.22 end 3.23 end 3.24 }) 3.25 - -- function identical to request:send_text_status_response(...) 3.26 - -- but enforcing a connection close after sending the response, and 3.27 - -- returning the boolean value of the "survive" variable: 3.28 + -- sends a minimalistic error response and enforces closing of the 3.29 + -- connection and returns the boolean value "survive" 3.30 local function error_response(...) 3.31 request:send_status(status) 3.32 request:send_header("Content-Type", "text/plain")
4.1 --- a/reference.txt Tue Feb 17 04:10:38 2015 +0100 4.2 +++ b/reference.txt Tue Feb 17 13:15:08 2015 +0100 4.3 @@ -361,16 +361,6 @@ 4.4 sent. 4.5 4.6 4.7 -### request:send_text_status_response(status_string, text) 4.8 - 4.9 -Sends a HTTP status plus a response body of content-type "text/plain" and 4.10 -finishes the request using request:finish(). The status_string has to be 4.11 -provided in the same format as expected by request:send_status(...) and will 4.12 -be included in the response body as plain text. The additional second "text" 4.13 -argument will be appended to the reponse body (separated by an empty line) if 4.14 -given. 4.15 - 4.16 - 4.17 ### request.socket 4.18 4.19 The underlaying socket. Can be used to force a TCP RST, etc.