From b3763acee4eaaad59e9ff9998e0dfc46c1956615 Mon Sep 17 00:00:00 2001 From: Mihai Galos Date: Mon, 26 Sep 2022 17:52:49 +0200 Subject: [PATCH 1/2] PUT: Add initial support --- fhost.py | 4 +-- tests/test_client.py | 63 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/fhost.py b/fhost.py index 9c2b94b..5b7bf1e 100755 --- a/fhost.py +++ b/fhost.py @@ -330,9 +330,9 @@ def get(path): abort(404) -@app.route("/", methods=["GET", "POST"]) +@app.route("/", methods=["GET", "POST", "PUT"]) def fhost(): - if request.method == "POST": + if request.method == "POST" or request.method == "PUT": sf = None if "file" in request.files: diff --git a/tests/test_client.py b/tests/test_client.py index 40041ce..80de781 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -18,7 +18,7 @@ def client(): db_upgrade() yield client -def test_client(client): +def test_client_post(client): payloads = [ ({ "file" : (BytesIO(b"hello"), "hello.txt") }, 200, b"https://localhost/E.txt\n"), ({ "file" : (BytesIO(b"hello"), "hello.ignorethis") }, 200, b"https://localhost/E.txt\n"), @@ -79,3 +79,64 @@ def test_client(client): rv = client.get(p) assert rv.status_code == code +def test_client_put(client): + payloads = [ + ({ "file" : (BytesIO(b"hello"), "hello.txt") }, 200, b"https://localhost/E.txt\n"), + ({ "file" : (BytesIO(b"hello"), "hello.ignorethis") }, 200, b"https://localhost/E.txt\n"), + ({ "file" : (BytesIO(b"bye"), "bye.truncatethis") }, 200, b"https://localhost/Q.truncate\n"), + ({ "file" : (BytesIO(b"hi"), "hi.tar.gz") }, 200, b"https://localhost/h.tar.gz\n"), + ({ "file" : (BytesIO(b"lea!"), "lea!") }, 200, b"https://localhost/d.txt\n"), + ({ "file" : (BytesIO(b"why?"), "balls", "application/x-dosexec") }, 415, None), + ({ "shorten" : "https://0x0.st" }, 200, b"https://localhost/E\n"), + ({ "shorten" : "https://localhost" }, 400, None), + ({}, 400, None), + ] + + for p, s, r in payloads: + rv = client.put("/", buffered=True, + content_type="multipart/form-data", + data=p) + assert rv.status_code == s + if r: + assert rv.data == r + + f = File.query.get(2) + f.removed = True + db.session.add(f) + db.session.commit() + + rq = [ + (200, [ + "/", + "robots.txt", + "E.txt", + "E.txt/test", + "E.txt/test.py", + "d.txt", + "h.tar.gz", + ]), + (302, [ + "E", + "E/test", + "E/test.bin", + ]), + (404, [ + "test.bin", + "test.bin/test", + "test.bin/test.py", + "test", + "test/test", + "test.bin/test.py", + "E.bin", + ]), + (451, [ + "Q.truncate", + ]), + ] + + for code, paths in rq: + for p in paths: + app.logger.info(f"GET {p}") + rv = client.get(p) + assert rv.status_code == code + -- 2.47.0 From 1abe43f4c005252d38137796a71d6651a27a3722 Mon Sep 17 00:00:00 2001 From: Mihai Galos Date: Mon, 26 Sep 2022 18:13:28 +0200 Subject: [PATCH 2/2] Update index.html --- templates/index.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/templates/index.html b/templates/index.html index cef9de2..d646b9c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -9,6 +9,9 @@ You can also POST remote URLs: Or you can shorten URLs: curl -F'shorten=http://example.com/some/long/url' {{ fhost_url }} +Alternatively, you can use PUT: + curl -X PUT -T 'yourfile.png' {{ fhost_url }} + File URLs are valid for at least 30 days and up to a year (see below). Shortened URLs do not expire. {% set max_size = config["MAX_CONTENT_LENGTH"]|filesizeformat(True) %} -- 2.47.0