Skip to content

Commit

Permalink
more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
toots committed Mar 7, 2023
1 parent 8897dd2 commit 5b90ac9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 30 deletions.
45 changes: 35 additions & 10 deletions src/libs/http.liq
Original file line number Diff line number Diff line change
Expand Up @@ -577,16 +577,6 @@ let settings.http.mime.extnames = ref([
("audio/x-wav", ".wav")
])

let http.mime = ()

# Find a file extension based on the mime-type
# @category Internet
def http.mime.extname(mime)
extnames = settings.http.mime.extnames()
extname = extnames[mime]
extname == "" ? null() : extname
end

let http.headers = ()

# Extract the content-disposition header
Expand Down Expand Up @@ -645,3 +635,38 @@ def http.headers.content_disposition(headers) =
({type = type, filename = filename, name = name, args = args}:{ type: string, filename?: string, name?: string, args: [(string*string?)]})
end, content_disposition)
end

# Try to get a filename from a request's headers.
# @category Internet
def http.headers.extname(headers) =
content_disposition = http.headers.content_disposition(headers)

mime =
try
list.find(fun (v) -> begin
let (header_name, _) = v
string.case(lower=true, header_name) == "content-type"
end, headers)
catch _ : [error.not_found] do
null()
end
mime = null.map(snd, mime)

extname =
if null.defined(content_disposition?.filename) then
extname = file.extension(null.get(content_disposition?.filename))
extname == "" ? null() : extname
else
null()
end

if null.defined(extname) then
extname
elsif null.defined(mime) then
extnames = settings.http.mime.extnames()
extname = extnames[null.get(mime)]
extname == "" ? null() : extname
else
null()
end
end
21 changes: 2 additions & 19 deletions src/libs/protocols.liq
Original file line number Diff line number Diff line change
Expand Up @@ -160,30 +160,13 @@ def protocol.http(proto,~rlog,~maxtime,arg) =

ret = http.head(timeout=timeout, uri)
code = ret.status_code ?? 999
headers = ret.headers
content_disposition = http.headers.content_disposition(headers)

mime =
if 200 <= code and code < 300 then
headers["content-type"]
else
log(level=3,"Failed to fetch mime-type for #{uri}.")
log(level=4,"Request response: #{ret}")
null()
end

extname =
if null.defined(content_disposition?.filename) then
file.extension(null.get(content_disposition?.filename))
else
null.map(http.mime.extname, mime)
end
extname = 200 <= code and code < 300 ? http.headers.extname(ret.headers) : null()

extname =
if null.defined(extname) then null.get(extname) else
log(level=3, "Failed to find a file extension for #{uri}.")
".osb"
end
end

output = file.temp("liq-process", extname)

Expand Down
2 changes: 1 addition & 1 deletion tests/harbor/http2.liq
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def f()
test.equals(content_disposition.args, [])

content_disposition = null.get(http.headers.content_disposition([
('Content-Disposition', "attachment; filename*=UTF-8''Na%C3%AFve%20file.txt; name=\"gni%20gno\"; foo=bla")
('coNtent-dispOsition', "attachment; filename*=UTF-8''Na%C3%AFve%20file.txt; name=\"gni%20gno\"; foo=bla")
]))
test.equals(content_disposition.type, "attachment")
test.equals(content_disposition?.filename, "Naïve file.txt")
Expand Down
23 changes: 23 additions & 0 deletions tests/harbor/http3.liq
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def f()
extname = http.headers.extname([
("Content-Disposition", 'attachment; filename="filename.jpg"')
])
test.equals(extname, ".jpg")

extname = http.headers.extname([
("Content-type", "audio/mpeg"),
("Content-Disposition", 'attachment; filename="filename.jpg"')
])
test.equals(extname, ".jpg")

settings.http.mime.extnames := [("foo", ".bla")]

extname = http.headers.extname([
("conTent-tyPe", "foo"),
])
test.equals(extname, ".bla")

test.pass()
end

test.check(f)

0 comments on commit 5b90ac9

Please sign in to comment.