From fcc8e0aae86c26967dec6072b7ea2b8c29dba5f7 Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 4 Apr 2018 19:10:25 +0200 Subject: [PATCH 1/9] Giving a filename to wget is now optional. wget will try to determine the file name by: 1. Stripping all trailing slashes 2. Take everything right of the last remaining slash, meaning either the full domain name or the file or foldername on the server. --- .../lua/rom/programs/http/wget.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index d644866256..4f6b3447d5 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -1,11 +1,11 @@ local function printUsage() print( "Usage:" ) - print( "wget " ) + print( "wget [filename]" ) end local tArgs = { ... } -if #tArgs < 2 then +if #tArgs < 1 then printUsage() return end @@ -15,7 +15,18 @@ if not http then printError( "Set http_enable to true in ComputerCraft.cfg" ) return end - + +local function getFilename( sUrl ) + while sUrl:sub(#sUrl) == "/" do + sUrl = sUrl:sub(1,#sUrl-1) --Remove any trailing slashes + end + local pos = sUrl:find("/") + while sUrl:find("/", pos + 1) do + pos = sUrl:find("/", pos + 1) --Find the last / + end + return sUrl:sub(pos+1) +end + local function get( sUrl ) write( "Connecting to " .. sUrl .. "... " ) @@ -43,7 +54,7 @@ end -- Determine file to download local sUrl = tArgs[1] -local sFile = tArgs[2] +local sFile = tArgs[2] or getFilename(sUrl) local sPath = shell.resolve( sFile ) if fs.exists( sPath ) then print( "File already exists" ) From 0ad84b319124310d2f3a111407f5c401ababdd2d Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 4 Apr 2018 19:16:18 +0200 Subject: [PATCH 2/9] Adjusted codestyle --- .../computercraft/lua/rom/programs/http/wget.lua | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index 4f6b3447d5..a31885f99f 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -17,14 +17,14 @@ if not http then end local function getFilename( sUrl ) - while sUrl:sub(#sUrl) == "/" do - sUrl = sUrl:sub(1,#sUrl-1) --Remove any trailing slashes + while sUrl:sub( #sUrl ) == "/" do + sUrl = sUrl:sub( 1 , #sUrl - 1 ) -- Remove any trailing slashes end - local pos = sUrl:find("/") - while sUrl:find("/", pos + 1) do - pos = sUrl:find("/", pos + 1) --Find the last / + local pos = sUrl:find( "/" ) + while sUrl:find( "/" , pos + 1 ) do + pos = sUrl:find( "/" , pos + 1 ) -- Find the last / end - return sUrl:sub(pos+1) + return sUrl:sub( pos + 1 ) end local function get( sUrl ) @@ -54,7 +54,7 @@ end -- Determine file to download local sUrl = tArgs[1] -local sFile = tArgs[2] or getFilename(sUrl) +local sFile = tArgs[2] or getFilename( sUrl ) local sPath = shell.resolve( sFile ) if fs.exists( sPath ) then print( "File already exists" ) From f1cec14e7294e92db9326b94682581347c36ad9d Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 4 Apr 2018 20:09:41 +0200 Subject: [PATCH 3/9] Use gsub instead of loops and also strip anchors and url parameters --- .../computercraft/lua/rom/programs/http/wget.lua | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index a31885f99f..5119282c8b 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -17,14 +17,8 @@ if not http then end local function getFilename( sUrl ) - while sUrl:sub( #sUrl ) == "/" do - sUrl = sUrl:sub( 1 , #sUrl - 1 ) -- Remove any trailing slashes - end - local pos = sUrl:find( "/" ) - while sUrl:find( "/" , pos + 1 ) do - pos = sUrl:find( "/" , pos + 1 ) -- Find the last / - end - return sUrl:sub( pos + 1 ) + sUrl = sUrl:gsub("[#].*", ""):gsub("/+$", "") + return sUrl:match("/([^/]+)$") end local function get( sUrl ) @@ -54,7 +48,7 @@ end -- Determine file to download local sUrl = tArgs[1] -local sFile = tArgs[2] or getFilename( sUrl ) +local sFile = tArgs[2] or getFilename(sUrl) local sPath = shell.resolve( sFile ) if fs.exists( sPath ) then print( "File already exists" ) From 39ecc90fd28ef218a6cc04615ce1da378a592cbb Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 4 Apr 2018 20:12:53 +0200 Subject: [PATCH 4/9] Again adjusting the codestyle --- .../assets/computercraft/lua/rom/programs/http/wget.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index 5119282c8b..a0403f92f2 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -17,8 +17,8 @@ if not http then end local function getFilename( sUrl ) - sUrl = sUrl:gsub("[#].*", ""):gsub("/+$", "") - return sUrl:match("/([^/]+)$") + sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" ) + return sUrl:match( "/([^/]+)$" ) end local function get( sUrl ) From a5bd04e58e8941e9976a487a3c2559a94dba6332 Mon Sep 17 00:00:00 2001 From: Luca Date: Wed, 4 Apr 2018 21:27:42 +0200 Subject: [PATCH 5/9] getFilename() needs to return something, because shell.resolve() is called before checking if the URL is valid --- .../assets/computercraft/lua/rom/programs/http/wget.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index a0403f92f2..804d38dc3a 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -18,7 +18,7 @@ end local function getFilename( sUrl ) sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" ) - return sUrl:match( "/([^/]+)$" ) + return sUrl:match( "/([^/]+)$" ) or " " end local function get( sUrl ) From 4e8d7aa1930eb381801926454b5bbd0933ac7db2 Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 5 Apr 2018 10:51:07 +0200 Subject: [PATCH 6/9] Check if the URL is valid before trying to determine the filename --- .../lua/rom/programs/http/wget.lua | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index 804d38dc3a..7715a99991 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -18,21 +18,12 @@ end local function getFilename( sUrl ) sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" ) - return sUrl:match( "/([^/]+)$" ) or " " + return sUrl:match( "/([^/]+)$" ) end local function get( sUrl ) write( "Connecting to " .. sUrl .. "... " ) - local ok, err = http.checkURL( sUrl ) - if not ok then - print( "Failed." ) - if err then - printError( err ) - end - return nil - end - local response = http.get( sUrl , nil , true ) if not response then print( "Failed." ) @@ -45,10 +36,20 @@ local function get( sUrl ) response.close() return sResponse end - + -- Determine file to download local sUrl = tArgs[1] -local sFile = tArgs[2] or getFilename(sUrl) + +--Check if the URL is valid +local ok, err = http.checkURL( sUrl ) +if not ok then + if err then + printError( err ) + end + return nil +end + +local sFile = tArgs[2] or getFilename( sUrl ) local sPath = shell.resolve( sFile ) if fs.exists( sPath ) then print( "File already exists" ) From 781141a1222726218c7d1d2e0506014b53033d03 Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 5 Apr 2018 10:53:04 +0200 Subject: [PATCH 7/9] Replace return nil with return because we are exiting from the program and not from a function now. --- .../assets/computercraft/lua/rom/programs/http/wget.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index 7715a99991..3364bdab4c 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -46,7 +46,7 @@ if not ok then if err then printError( err ) end - return nil + return end local sFile = tArgs[2] or getFilename( sUrl ) From 67448f10e8b4965f259acc7f9f88b83c996b029d Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 5 Apr 2018 16:21:15 +0200 Subject: [PATCH 8/9] err can't be nil --- .../assets/computercraft/lua/rom/programs/http/wget.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua index 3364bdab4c..2cacd6c546 100644 --- a/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua +++ b/src/main/resources/assets/computercraft/lua/rom/programs/http/wget.lua @@ -43,9 +43,7 @@ local sUrl = tArgs[1] --Check if the URL is valid local ok, err = http.checkURL( sUrl ) if not ok then - if err then - printError( err ) - end + printError( err or "Invalid URL." ) return end From 7a4dac31059d33a7225d70d2538ee34ed81d9273 Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 5 Apr 2018 16:29:11 +0200 Subject: [PATCH 9/9] Adjust wget help file --- src/main/resources/assets/computercraft/lua/rom/help/wget.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/resources/assets/computercraft/lua/rom/help/wget.txt b/src/main/resources/assets/computercraft/lua/rom/help/wget.txt index f4d2a81b28..428ed5bab6 100644 --- a/src/main/resources/assets/computercraft/lua/rom/help/wget.txt +++ b/src/main/resources/assets/computercraft/lua/rom/help/wget.txt @@ -1,5 +1,7 @@ wget is a program for downloading files from the internet. This is useful for downloading programs created by other players. +If no filename is specified wget will try to determine the filename from the URL by stripping any anchors, parameters and trailing slashes and then taking everything remaining after the last slash. The HTTP API must be enabled in ComputerCraft.cfg to use this program. - ex: "wget http://pastebin.com/raw/CxaWmPrX test" will download the file from the URL http://pastebin.com/raw/CxaWmPrX, and save it as "test". +"wget http://example.org/test.lua/?foo=bar#qzu" will download the file from the URL http://example.org/test.lua/?foo=bar#qzu and save it as "test.lua" +"wget http://example.org/" will download the file from the URL http://example.org and save it as "example.org"