Skip to content

Commit 2f86b10

Browse files
Fix #49: add support for more HTTP methods (#50)
1 parent a4c236b commit 2f86b10

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

amx/server/syscalls.lua

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,33 +2763,61 @@ function SetPlayerChatBubble(amx, player, text, color, dist, exptime)
27632763
end
27642764

27652765

2766-
-- In MTA we can't set the type of the request.
2767-
-- When we add 'data' it becomes POST request.
2768-
-- TODO: arguments "index" and "type" here only for compatibility.
2766+
-- Now we have all of RESTful types of requests. Our function is better!
2767+
-- The SAMP documentation said about 'url' - "The URL you want to request. (Without 'http://')"
2768+
-- I made a check. The state without a protocol is called as 'default'.
2769+
-- HTTP and HTTPS you can put into URL if you want. It works fine.
2770+
-- TODO: An "index" argument only for compatibility.
27692771
function HTTP(amx, index, type, url, data, callback)
2770-
if type == 3 then
2771-
notImplemented('HTTP', 'A HEAD type have not support right now')
2772+
2773+
local protomatch = pregMatch(url,'^(\\w+):\\/\\/')
2774+
local proto = protomatch[1] or 'default'
2775+
-- if somebody will try to put here ftp:// ssh:// etc...
2776+
if proto ~= 'http' and proto ~= 'https' and proto ~= 'default' then
2777+
print('Current protocol is not supporting')
2778+
return 0
27722779
end
2773-
local request = fetchRemote ("http://".. url,
2774-
function(responseData, error)
2780+
local typesToText = {
2781+
'GET',
2782+
'POST',
2783+
'HEAD',
2784+
[-4] = 'PUT',
2785+
[-5] = 'PATCH',
2786+
[-6] = 'DELETE',
2787+
[-7] = 'COPY',
2788+
[-8] = 'OPTIONS',
2789+
[-9] = 'LINK',
2790+
[-10] = 'UNLINK',
2791+
[-11] = 'PURGE',
2792+
[-12] = 'LOCK',
2793+
[-13] = 'UNLOCK',
2794+
[-14] = 'PROPFIND',
2795+
[-15] = 'VIEW'
2796+
}
2797+
local sendOptions = {
2798+
queueName = "amx." .. getResourceName(amx.res) .. "." .. amx.name,
2799+
postData = data,
2800+
method = typesToText[tonumber(type)],
2801+
}
2802+
local successRemote = fetchRemote(url, sendOptions,
2803+
function (responseData, responseInfo)
2804+
local error = responseInfo.statusCode
27752805
if error == 0 then
27762806
procCallInternal(amx, callback, index, 200, responseData)
2777-
return 1;
27782807
elseif error >= 1 and error <= 89 then
27792808
procCallInternal(amx, callback, index, 3, responseData)
2780-
return 0;
27812809
elseif error == 1006 or error == 1005 then
27822810
procCallInternal(amx, callback, index, 1, responseData)
2783-
return 0;
27842811
elseif error == 1007 then
27852812
procCallInternal(amx, callback, index, 5, responseData)
2786-
return 0;
27872813
else
27882814
procCallInternal(amx, callback, index, error, responseData)
2789-
return 0;
27902815
end
2791-
end, data, false)
2792-
return 0;
2816+
end)
2817+
if not successRemote then
2818+
return 0
2819+
end
2820+
return 1
27932821
end
27942822

27952823
function Create3DTextLabel(amx, text, r, g, b, a, x, y, z, dist, vw, los)

0 commit comments

Comments
 (0)