This repository was archived by the owner on Dec 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmhttp.lua
More file actions
126 lines (107 loc) · 4.15 KB
/
mhttp.lua
File metadata and controls
126 lines (107 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
--[[
Malte0621's ExternalHttp Module (V2).
(coroutine.wrap(function() end) is also helpful to prevent crashes during the request.)
(Requires json.lua)
--> Note
The game crashes when too many requests are sent WITH response headers enabled.
To disable response headers use:
mhttp_return_headers = false
AND To enable it again use:
mhttp_return_headers = true
--> Usage
#include "mhttp.lua"
local resp = http.PostAsyncA("http://localhost/",{msg = "hello"}) -- PostAsyncA urlencodes for you. While PostAsync does not and requires manual urlencoding.
-- local resp = http.GetAsync("http://localhost/")
--> API
--------------------------
"mhttp.lua"
--------------------------
http = {
GetAsync = function(<url:string>,<headers:table>) -> {Success = <success:bool>, (Error = <error:string>), Body = <body:string>, Headers = <headers:table>, StatusCode = <statuscode:int>}
PostAsyncA = function(<url:string>,<data:table>,<headers:table>) -> {Success = <success:bool>, (Error = <error:string>), Body = <body:string>, Headers = <headers:table>, StatusCode = <statuscode:int>}
PutAsync = function(<url:string>,<headers:table>) -> {Success = <success:bool>, (Error = <error:string>), Body = <body:string>, Headers = <headers:table>, StatusCode = <statuscode:int>}
DeleteAsync = function(<url:string>,<headers:table>) -> {Success = <success:bool>, (Error = <error:string>), Body = <body:string>, Headers = <headers:table>, StatusCode = <statuscode:int>}
OptionsAsync = function(<url:string>,<headers:table>) -> {Success = <success:bool>, (Error = <error:string>), Body = <body:string>, Headers = <headers:table>, StatusCode = <statuscode:int>}
PostAsync = function(<url:string>,<data:string>,<headers:table>) -> {Success = <success:bool>, (Error = <error:string>), Body = <body:string>, Headers = <headers:table>, StatusCode = <statuscode:int>}
UrlEncode = function(<str:string>) -> <output:string>
UrlDecode = function(<str:string>) -> <output:string>
}
## headers example ##
{["key"] = "value"}
--------------------------
]]
#include "json.lua"
mhttp_return_headers = true
local UrlEncode = function(str)
str = string.gsub (str, "([^0-9a-zA-Z !'()*._~-])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
return str
end;
local UrlDecode = function(str)
str = string.gsub (str, "+", " ")
str = string.gsub (str, "%%(%x%x)", function(h) return string.char(tonumber(h,16)) end)
return str
end;
local unpack = table.unpack or unpack
local function formatHeaders(headers)
local tmp = {}
for i,v in pairs(headers) do
table.insert(tmp,{[i] = v})
end
return tmp
end
local function parseResponse(resp)
if resp["Headers"] then
resp["Headers"] = json.decode(resp["Headers"])
end
return resp
end
local http_get = http_get
local http_put = http_put
local http_delete = http_delete
local http_options = http_options
local http_post = http_post
http_get, http_put, http_delete, http_options, http_post = nil
http = {
UrlEncode = UrlEncode;
UrlDecode = UrlDecode;
GetAsync = function(url,headers)
if not headers then headers = {} end
headers = formatHeaders(headers)
return parseResponse(http_get(url,unpack(headers)))
end;
PutAsync = function(url,headers)
if not headers then headers = {} end
headers = formatHeaders(headers)
return parseResponse(http_put(url,unpack(headers)))
end;
DeleteAsync = function(url,headers)
if not headers then headers = {} end
headers = formatHeaders(headers)
return parseResponse(http_delete(url,unpack(headers)))
end;
OptionsAsync = function(url,headers)
if not headers then headers = {} end
headers = formatHeaders(headers)
return parseResponse(http_options(url,unpack(headers)))
end;
PostAsync = function(url,body,headers)
if not headers then headers = {} end
headers = formatHeaders(headers)
return parseResponse(http_post(url,body,unpack(headers)))
end;
PostAsyncA = function(url,body,headers)
if not headers then headers = {} end
headers = formatHeaders(headers)
local data = ""
for k, v in pairs(body) do
data = data .. ("&%s=%s"):format(
UrlEncode(k),
UrlEncode(v)
)
end
data = data:sub(2)
return parseResponse(http_post(url,data,unpack(headers)))
end;
}