Espen

ComputerCraft HttpTest v1.4

Feb 14th, 2012
880
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.66 KB | None | 0 0
  1. --[[
  2. HttpTest by Espen v1.4
  3. An example code to show how to use the ComputerCraft HTTP functions.
  4.  
  5. Thanks to Advert for making me aware of the redundant call to http.get after http.request!
  6. 1.4: Changed the else-condition in getHttpBody() to explicitly check for an http_failure, instead of just assuming one if the event isn't an http_success. *derp*
  7. --]]
  8.  
  9. --[[ Setting up variables ]]
  10. local fileWriteOK
  11. local url
  12. local filename
  13. local tArgs = { ... }
  14.  
  15.  
  16. local function getHttpBody( url )
  17.     http.request( url )
  18.    
  19.     while true do
  20.         local event, url, hBody = os.pullEvent()
  21.         if event == "http_success" then
  22.             print( "HTTP SUCCESS\nURL = "..url )
  23.             return hBody
  24.         elseif event == "http_failure" then
  25.             error( "HTTP FAILURE\nURL = "..url )    -- If the error is not catched, this will exit the program.
  26.             return nil    -- In case this function is called via pcall.
  27.         end
  28.     end
  29. end
  30.  
  31. local function processHttpBody( hBody, filename )
  32.     local hFile
  33.    
  34.     if hBody then
  35.             local body = hBody.readAll()    -- Read the whole body.
  36.             hFile = io.open( filename, "w" )    -- Open the provided filename for writing.
  37.             hFile:write( body )     -- Write the body to the file.
  38.             hFile:close()   -- Save the changes and close the file.
  39.     else
  40.         print( "Sorry, no body to process." )
  41.     end
  42.    
  43.     hBody.close()   -- Do not know for sure if this is really necessary, but just in case.
  44. end
  45.  
  46. local function checkOverwrite( filename )
  47.     term.setCursorBlink( false )
  48.     print("\nWarning: File already exists. Overwrite? (Y/N)")
  49.    
  50.     while true do
  51.             event, choice = os.pullEvent( "char" )  -- Only listen for "char" events.
  52.             if string.lower( choice ) == "y" then return true end
  53.             if string.lower( choice ) == "n" then return false end
  54.     end
  55. end
  56.  
  57. local function printUsage()
  58.     print("Usage:")
  59.     print("httptest <URL> <OUTPUT-FILENAME>")
  60.     print("Example:")
  61.     print("httptest http://www.google.com/ webout")
  62.     print("\nThe response body will then be output into the file 'webout', line for line.")
  63. end
  64.  
  65.  
  66. --[[ ===== Execution Entry ===== ]]
  67.  
  68. --[[ Processing arguments ]]
  69. if tArgs[1] then url = tArgs[1] end
  70. if tArgs[2] then filename = tArgs[2] end
  71.  
  72. --[[ Making sure all necessary arguments were provided by the user ]]
  73. if not url or not filename then
  74.     printUsage()
  75. elseif not fs.exists( filename ) or checkOverwrite( filename ) then
  76.     processHttpBody( getHttpBody( url ), filename )    --If getHttpBody successfully returns a body, we continue with processing the body and saving it to a file.
  77. end
Advertisement
Add Comment
Please, Sign In to add comment