- 
                Notifications
    
You must be signed in to change notification settings  - Fork 10
 
add nic option config for json response #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
        
          
                nic.lua
              
                Outdated
          
        
      | elseif type(msg) == "table" and type(msg.url) == "string" then | ||
| -- config object | ||
| url = msg.url | ||
| headers = msg.headers | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should check type of headers before using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also contents, if you send headers = {false} it will crash server.
Probably something like:
if type(msg.headers) == "table" and #msg.headers > 0 and #msg.headers < 50 then
  headers = {}
  for _, header in ipairs(msg.headers) do
    header = (tostring(header)):gmatch("%C+")()
    if header ~= "" then
      table.insert(headers, header)
    end
  end
endipairs is intended over pairs to drop hash keys.
| 
           Tested above with this crap: local msg = { headers =
{ 1, 2, 3, false, true, {"woo"}, function()quit()end, "Authorization: Ajadshk:dasjhghja", [[
And what
			about			!!!
this
crap????
]]
}
}
table.insert(msg.headers, msg.headers)
msg.headers.really = msg.headers[#msg.headers]
local headers
if type(msg.headers) == "table" and #msg.headers > 0 and #msg.headers < 50 then
  headers = {}
  for _, header in ipairs(msg.headers) do
    header = (tostring(header)):gmatch("%C+")()
    if header ~= "" then
      table.insert(headers, header)
    end
  end
end
http.fetch({
		url="http://127.0.0.1:10101/?hello",
		timeout=5,
		user_agent="Minetest Digilines Modem",
		extra_headers = headers
	},
	function(r) print(dump(r)) end,
	1
)Headers after running through filter was: {
	"1",
	"2",
	"3",
	"false",
	"true",
	"table: 0x555983663440",
	"function: 0x555983663490",
	"Authorization: Ajadshk:dasjhghja",
	"And what",
	"table: 0x5559836633f0"
} | 
    
| 
           Also for complete test, request I get for that is:  | 
    
| 
           i've removed the  Also: some fixed headers could be set in the future, for example if POST would be implemented with a json-object, the headers could be just `Content-Type: application/json``  | 
    
| 
          
 Though really the ideal solution would be to include  There is a PR open to add them, along with a heap of other helper functions: minetest-mods/mesecons#507, though this other one looks like it's more likely to get merged: minetest-mods/mesecons#557, and would allow for even more functionality. EDIT: then again, there is the huge benefit of the parsing being done outside of the luacontroller sandbox, so still 👍 for this PR :)  | 
    
| 
           I don't see any objections to this, so merging it.  | 
    
This PR adds the ability to pass a config object to the nic as well as json parsing
and header settings.The message can still be a plain url as text or an object in the form:
{ url = "http://x.y", parse_json = true )I've tested it and it works well but there may be some crash vectors, not sure 🤷
Example
Luacontroller
Console
{ completed = true, timeout = false, succeeded = true, data = { value = { id = 34, categories = { }, joke = "The opening scene of the movie "Saving Private Ryan" is loosely based on games of dodgeball Chuck Norris played in second grade." }, type = "success" }, code = 200 }