From b6aeb90d675617716b2cb417cbf9dfde48a330c7 Mon Sep 17 00:00:00 2001 From: gamax92 Date: Tue, 23 Jun 2015 13:38:52 -0600 Subject: [PATCH] More fixes trim the url string when doing a request Check the protocol to make sure it's valid or supported Add in another patch to http to actually preform http requests when asked, the hack however prevents you from doing HTTP on 443, but it's much better Add in the SDL2 scancode to cprint debugging Ignore Escape key Artificially generate various characters for various keys Also default to 0 in key_up Add in a secondary list for the artificial characters: Backspaces Tabs Enter Delete --- src/component/internet.lua | 22 ++++++++++++++++++++-- src/component/keyboard_sdl2.lua | 24 ++++++++++++++---------- src/support/http_patch.lua | 1 + src/support/sdl_to_lwjgl.lua | 7 +++++++ 4 files changed, 42 insertions(+), 12 deletions(-) diff --git a/src/component/internet.lua b/src/component/internet.lua index 07eb9c7..8086ad3 100644 --- a/src/component/internet.lua +++ b/src/component/internet.lua @@ -7,8 +7,8 @@ if not okay then end require("support.http_patch") local url = require("socket.url") -local okay, http = pcall(require, "ssl.https") -if not okay then +local httpsok, http = pcall(require, "ssl.https") +if not httpsok then cprint("Cannot use HTTPS: " .. http) http = require("socket.http") end @@ -17,6 +17,11 @@ component.connect("filesystem",gen_uuid(),nil,"lua/component/internet",true) local obj = {} +local function string_trim(s) + local from = s:match"^%s*()" + return from > #s and "" or s:match(".*%S", from) +end + local function checkUri(address, port) local parsed = url.parse(address) if parsed ~= nil and parsed.host ~= nil and (parsed.port ~= nil or port > -1) then @@ -117,6 +122,19 @@ function obj.request(url, postData) -- Starts an HTTP request. If this returns t return nil, "http requests are unavailable" end -- TODO: Check for too many connections + url = string_trim(url) + -- TODO: Use url.parse + local protocol = url:match("(.-):") + if protocol == "http" then + elseif protocol == "https" then + if not httpsok then + return nil, "unsupported protocol" + end + elseif protocol == "ftp" or protocol == "mailto" then + return nil, "unsupported protocol" + else + return nil, "invalid address" + end if type(postData) ~= "string" then postData = nil end diff --git a/src/component/keyboard_sdl2.lua b/src/component/keyboard_sdl2.lua index 8d9484f..c2fb7b1 100644 --- a/src/component/keyboard_sdl2.lua +++ b/src/component/keyboard_sdl2.lua @@ -5,7 +5,7 @@ local lua_utf8 = require("utf8") local SDL = elsa.SDL -- Conversion table for SDL2 keys to LWJGL key codes -local keys = require("support.sdl_to_lwjgl") +local keys,codes = elsa.filesystem.load("support/sdl_to_lwjgl.lua")() local code2char = {} @@ -24,15 +24,16 @@ end function elsa.keydown(event) local keyevent = ffi.cast("SDL_KeyboardEvent", event) local key = keyevent.keysym.scancode - cprint("keydown",keys[key]) - table.insert(kbdcodes,{type="key_down",addr=address,code=keys[key] or 0}) + local lwjgl = keys[key] + cprint("keydown",keyevent.keysym.scancode,lwjgl) -- TODO: Lovely SDL Hacks - if keys[key] == 15 then - setLatest(9) - elseif keys[key] == 28 or keys[key] == 156 then - setLatest(13) + if lwjgl ~= 1 then -- Escape + table.insert(kbdcodes,{type="key_down",addr=address,code=lwjgl or 0}) end - if keys[key] == 210 then + if lwjgl ~= nil and codes[lwjgl] ~= nil then + setLatest(codes[lwjgl]) + end + if lwjgl == 210 then if SDL.hasClipboardText() > 0 then table.insert(machine.signals,{"clipboard",address,ffi.string(SDL.getClipboardText())}) end @@ -42,8 +43,11 @@ end function elsa.keyup(event) local keyevent = ffi.cast("SDL_KeyboardEvent", event) local key = keyevent.keysym.scancode - cprint("keydown",keys[key]) - table.insert(kbdcodes,{type="key_up",addr=address,code=keys[key],char=code2char[keys[key]]}) + local lwjgl = keys[key] + cprint("keydown",keyevent.keysym.scancode,lwjgl) + if key ~= 41 then -- Escape + table.insert(kbdcodes,{type="key_up",addr=address,code=lwjgl or 0,char=code2char[lwjgl]}) + end end -- keyboard component diff --git a/src/support/http_patch.lua b/src/support/http_patch.lua index 090768b..cde4b45 100644 --- a/src/support/http_patch.lua +++ b/src/support/http_patch.lua @@ -7,6 +7,7 @@ cprint("http_patch start") -- Patch data local patches = { {[[if headers[name] then headers[name] = headers[name] .. ", " .. value]],[[if headers[name] then if type(headers[name]) == "string" then headers[name] = {headers[name]} end headers[name][#headers[name]+1] = value]]}, + {[[local nreqt = adjustrequest(reqt)]],[[local nreqt = adjustrequest(reqt) if nreqt.scheme == "http" then nreqt.create = nil if nreqt.port == "443" then nreqt.port = "80" end end]]}, } package.loaded["socket.http"] = nil local path = package.searchpath("socket.http",package.path) diff --git a/src/support/sdl_to_lwjgl.lua b/src/support/sdl_to_lwjgl.lua index 21863e5..84363df 100644 --- a/src/support/sdl_to_lwjgl.lua +++ b/src/support/sdl_to_lwjgl.lua @@ -36,6 +36,7 @@ return { [38]=10, [39]=11, [40]=28, +[41]=1, [42]=14, [43]=15, [44]=57, @@ -96,4 +97,10 @@ return { [228]=157, [229]=54, [230]=184, +},{ +[14]=8, +[15]=9, +[28]=13, +[156]=13, +[211]=127, }