Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions album-songs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion albums.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
35 changes: 33 additions & 2 deletions ampache-handshake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 2 additions & 4 deletions ampache-http.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
Binary file modified tiled_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions token
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0d90a504f85426c1a36b3356e1ba4f8a