diff --git a/album-songs.lua b/album-songs.lua index 5fb68bb..7f26a10 100644 --- a/album-songs.lua +++ b/album-songs.lua @@ -40,8 +40,7 @@ local res, code, response_headers, status, json_response, data = ampacheHttp.mak }) -- Check if the request was successful -if code == 200 then - +if code == 200 and data ~= nil then -- if the -j option is passed, just print the json file if is_json_output == true then print(json_response) diff --git a/albums.lua b/albums.lua index cead4b7..75e8c24 100644 --- a/albums.lua +++ b/albums.lua @@ -40,7 +40,7 @@ local res, code, response_headers, status, json_response, data = filterValue = filter_value }) -if code == 200 then +if code == 200 and data ~= nil then -- if the -j option is passed, just print the json file if is_json_output == true then print(json_response) diff --git a/ampache-handshake.lua b/ampache-handshake.lua index b606e0c..1ee7a90 100644 --- a/ampache-handshake.lua +++ b/ampache-handshake.lua @@ -16,6 +16,7 @@ local http = require("socket.http") local ltn12 = require("ltn12") local cjson = require("cjson") local sha2 = require("sha2") +local https = require("ssl.https") -- get the current Unix timestamp local function getTimestamp() @@ -26,8 +27,38 @@ local function calculateSha256(input) return sha2.sha256(input) end +local function fetchJson(url, maxRedirects) + maxRedirects = maxRedirects or 5 + + local responseBody = {} + local requester = url:match("^https://") and https or http + + local _, code, headers = requester.request{ + url = url, + sink = ltn12.sink.table(responseBody) + } + + if code == 301 or code == 302 or code == 307 or code == 308 then + if maxRedirects <= 0 then + error("Too many redirects (possible http/https loop)") + end + + local newUrl = headers.location + if not newUrl then + error("Redirect without Location header") + end + + return fetchJson(newUrl, maxRedirects - 1) + end + + if code ~= 200 then + error("HTTP request failed with status code " .. tostring(code)) + end + + return table.concat(responseBody) +end -- fetch JSON from the URL -local function fetchJson(url) +local function fetchJson2(url) local responseBody = {} local _, code, _, _ = http.request{ url = url, @@ -57,7 +88,7 @@ local function handshake(serverUrl, username, password) auth, username ) - return str2Json(fetchJson(url)) + return str2Json(fetchJson(url, 20)) end local function getAuthToken(serverUrl, username, password) diff --git a/ampache-http.lua b/ampache-http.lua index 8172435..71c4cb4 100644 --- a/ampache-http.lua +++ b/ampache-http.lua @@ -76,14 +76,12 @@ local function makeRequestFromUrl(url) if code == 200 then if response_body and #response_body > 0 then json_response = table.concat(response_body) - - local ok, decoded = pcall(cjson.decode, json_response) if ok then data = decoded -- The server can be returning an error json despite of the 200 response - if data.error ~= nil then + if data.error ~= nil then return nil, data.error.errorCode, {}, "Error Returned by server", json_response, data end @@ -102,7 +100,7 @@ end function authToken(serverUrl, username, password) local filename = "token" local token = nil - if ampache.isFileEmpty(filename) then + if ampache.isFileEmpty(filename) then token = handshake.getAuthToken(serverUrl, username, password) ampache.writeFile(filename, token) else diff --git a/tiled_image.jpg b/tiled_image.jpg index ebf9cb9..9cb0887 100644 Binary files a/tiled_image.jpg and b/tiled_image.jpg differ diff --git a/token b/token new file mode 100644 index 0000000..f9dab6a --- /dev/null +++ b/token @@ -0,0 +1 @@ +0d90a504f85426c1a36b3356e1ba4f8a \ No newline at end of file