Updated April 5, 2023
Definition of Lua HTTP
- It is an application-level protocol used to load and fetches the World Wide Web application link on the browser Lua programming language.
- It is a hypertext transfer text protocol used for transfer information on the internet using Lua technology.
- It is a generic protocol to provide high-level communication between server and client of the web browser using Lua source code.
- It is a stateless protocol for flexible data transportation, retrieving HTTP data, and throwing the Error of the Lua code.
- It is useful for the webserver for the request to the webserver and response to the web browser.
Syntax:
- It has many modules and methods to communicate with clients and servers.
- Some modules information with explanations is below.
- The syntaxes are below.
- The client-side request syntax is below.
require "http.request";
- Example: local lua_variable_name = require ” http.request “
new_from_uri("uri link")
- Example: local new_variable_name = variable_name.new_from_uri(“http://lua.org/demo”)
- The http. the request is a hight level interface for client-side requests.
- The http. the request is using to client-side request to retrieve the server information.
- The new_from_uri is fetching the given link immediately and working the procedure.
- The new_from_uri is connected with the http. request object to evaluate the URI.
The server-side syntax is below.
require "http.server"
Example: local lua_server = require “http.server”
require "http. headers"
Example: local lua_headers = require “http.headers”
- The http. the server is creating the object and used to encapsulate the server socket.
- The http. the server is sending the information to the client-side.
- The http. headers are the index of the field name.
- The http. the header is sensitive data and retrieves it by calling the key value.
The header syntax is below.
headers:append(name, value, never_index)
headers:each()
: status
- The headers: append used to create and associate the header name, value, and Boolean authorization.
- The headers: each() is useful for iteration of the header value.
- The header “: status” for keep value in string format.
The util syntax is below.
require "http.util"
Example: local lua_http_variable = require “http.util”;
The compact socket syntax is below.
require "http.compat.socket"
Example: local lua_http_variable = require “http.compat.socket”;
This is to supply the compatibility of the request.
How HTTP works in Lua?
Step 1: Lua programming IDE Environmental Setup.
The Lua text editor, Lua compiler, and Lua interpreter installed in your virtual machine as per the version of Lua software.
Step 2: The environmental setup.
Install helping with the package manager of Lua modules (luarocks).
Website name:- https://luarocks.org/
- This manager is automatically installing dependencies.
- The following command is just entered in the Lua software.
$ luarocks install http.
Step 3: The following dependencies you should install in the Lua software.
-
- The basic dependencies are luaossl, basexx, lpeg, lpeg_pattern, binary heap, fifp, and queues(Microsoft windows does not support).
- The document compression dependencies are lzlib and Lua-zlib.
- The cookies dependency is lua-psl.
- The testing dependencies are luacheck, busted, and luacov.
Step 4: Create a variable with the request.
local lua_http_variable= require('http.request');
Step 5: Create a variable with Lua new_from_uri(” uri name “).
local new_http_variable= lua_http_variable.new_from_uri("http://lua.org/demo")
Step 6: The headers and steam uses in the Lua HTTP.
for lua_field, lua_value in headers:each()
do
print(lua_field, lua_value)
end
local body_http_variable = assert(stream:get_body_as_string())
Step 7: Return the required data.
print(body_http_variable)
- Combine the working procedure.
local lua_http_variable= require "http.request"
local new_http_variable= lua_http_variable .new_from_uri ("http://lua.org/demo")
local headers, stream= assert ( new_http_variable:go());
for lua_field, lua_value in headers:each()
do
print (lua_field, lua_value)
end
local body_http_variable = assert (stream:get_body_as_string())
print (body_http_variable)
Examples
Let us discuss the examples.
Example #1: Client-side server example.
Code:
local lua_http_variable= require "http.request";
local new_http_variable= lua_http_variable .new_from_uri ("http://lua.org/demo")
local headers, stream= assert ( new_http_variable:go());
for lua_field, lua_value in headers:each()
do
print (lua_field, lua_value)
end
local body_http_variable = assert (stream:get_body_as_string())
print (body_http_variable)
Output 1: Enter the $ cat filename.Lua command in Lua command prompt.
Output 2: Enter the $ lua filename.Lua command in Lua command prompt.
Example #2: Client-side server with “if” loop example.
Code:
local lua_http_variable= require "http.request";
local new_http_variable= lua_http_variable .new_from_uri ("http://lua.org/demo")
local headers, stream= assert( new_http_variable:go());
local body_http_variable = assert (stream:get_body_as_string())
if headers : get ":status" ~= "200"
then
error (body_http_variable)
end
print (body_http_variable)
Output:
Output 1: Enter the $ cat filename.Lua command in Lua command prompt.
Output 2: Enter the $ lua filename.Lua command in Lua command prompt.
Example #3: Server-side example.
Code:
local lua_server = require "http.server";
local lua_header = require "http.headers";
local lua_http_server = lua_server.listen {
host: "localhost";
port: 8005;
onstream: function(sa, sb)
local reqst_header = sb:get_headers(),
local reqst_method = sb:get(":method")
local lua_path = reqst_header:get(":path") or "",
local recv_path = headers.new()
recv_path:append(":status", "200")
recv_path:append("content-type", "text/plain")
sb:write_headers(recv_path, reqst_method == "HEAD")
if reqst_method ~= "HEAD" then
sb:write_chunk(("Received message \n":format(lua_path), true)
end
end
}
lua_http_server.listen();
lua_http_server.loop();
Output 1: This output shows the port of the server.
Output 2: This output shows the message of the server.
Example #4: Util example.
Part 1: use the query-args(string) with http.util example.
local lua_http_vrblr = require "http.util";
for names, values in lua_http_vrblr.query.args("Lua = language & Lua = http")
do
print("names, values")
end
Output:
Part 2: use the query-args(string) with http.util example.
local lua_http_util = require "http.util"
print(lua_http_util.split_authority("lua.org", "https"))
print(lua_http_util.split_authority("localhost:8005", "http"))
Output:
Example #5: Socket example.
Code:
local lua_http_socket = require "http.compat.socket"
local bd, dc = assert(lua_http_socket.request("http://google.com"))
print(dc, #bd);
Output:
Conclusion
- It has many ways to control the server and client-side data or information.
- It is making the Lua application easy, interactive, and protected on the server or internet.
- It is helped the client to get the server-side information easily with protection.
Recommended Articles
We hope that this EDUCBA information on “Lua HTTP” was beneficial to you. You can view EDUCBA’s recommended articles for more information.