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" 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..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 @@ -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,19 +15,15 @@ if not http then printError( "Set http_enable to true in ComputerCraft.cfg" ) return end - + +local function getFilename( sUrl ) + sUrl = sUrl:gsub( "[#?].*" , "" ):gsub( "/+$" , "" ) + 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." ) @@ -40,10 +36,18 @@ local function get( sUrl ) response.close() return sResponse end - + -- Determine file to download local sUrl = tArgs[1] -local sFile = tArgs[2] + +--Check if the URL is valid +local ok, err = http.checkURL( sUrl ) +if not ok then + printError( err or "Invalid URL." ) + return +end + +local sFile = tArgs[2] or getFilename( sUrl ) local sPath = shell.resolve( sFile ) if fs.exists( sPath ) then print( "File already exists" )