#!/usr/bin/python3
import sys
import requests
import re
from urllib.parse import urlparse, parse_qs
url = input()
test_no = int(url.split("/")[-1])
if test_no == 1:
res = requests.get(url)
assert res.status_code == 401
assert "X-Hint" in res.headers
assert re.match(r"^Heslo je [a-zA-Z.]+$", res.headers["X-Hint"])
password = res.headers["X-Hint"].replace("Heslo je ", "")
auth = ("some_random_username", password)
res = requests.get(url, auth=auth)
assert res.status_code == 403
assert "Only root can do this." in res.text
auth = ("root", auth[1])
res = requests.get(url, auth=auth)
assert res.status_code == 200
assert re.match(r"^Klíč je [0-9a-f]{16}\.$", res.text.strip())
key = res.text.replace("Klíč je ", "").strip()[:-1]
print(key)
elif test_no == 2:
res = requests.get(url, allow_redirects=False)
assert res.status_code == 301
assert res.headers["Location"] == url + "/"
url = res.headers["Location"]
res = requests.get(url, allow_redirects=False)
assert res.status_code == 307
assert res.headers["Location"] == url + "form.cgi?task=37-2-4&what=key"
url = res.headers["Location"]
res = requests.get(url, allow_redirects=False)
assert res.status_code == 405
assert res.headers["Allow"] == "POST"
res = requests.post(url, allow_redirects=False)
assert res.status_code == 415
assert "Forms must be submitted as application/x-www-form-urlencoded" in res.text
res = requests.post(urlparse(url)._replace(query="").geturl(), allow_redirects=False, data=parse_qs(urlparse(url).query))
assert res.status_code == 303
res = requests.get(res.headers["Location"], allow_redirects=False)
assert res.status_code == 200
assert re.match(r"^[0-9a-f]{16}$", res.text.strip())
key = res.text.strip()
print(key)
elif test_no == 3:
url = url + "/"
res = requests.get(url)
assert res.status_code == 405
res = requests.put(url)
assert res.status_code == 415
assert "You must submit a text file." in res.text
res = requests.put(url + "", data="Brůžek Batůžek".encode('utf-8'), headers={"Content-Type": "text/plain"})
assert res.status_code == 200
assert re.match(r"^Jméno uloženo. Klíč je [0-9a-f]{16}.$", res.text.strip())
key = res.text.replace("Jméno uloženo. Klíč je ", "").strip()[:-1]
print(key)
elif test_no == 4:
url = url + "/"
res = requests.get(url)
assert res.status_code == 300
assert "
GIF picture" in res.text
assert "XML" in res.text
assert "PNG picture" in res.text
res = requests.get(url + "minotauros.gif")
assert res.status_code == 451
assert "$1" in res.text
assert "X-Coin" in res.headers
assert res.headers["X-Coin"] == "$1"
png_url = url + "minotauros.png"
res = requests.get(png_url, allow_redirects=False)
assert res.status_code == 307
assert "The resource has moved. Guess where." in res.text
png_url = res.headers["Location"]
res = requests.get(png_url, allow_redirects=False)
assert res.status_code == 302
assert "Just moved the image to a new directory, sorry." in res.text
png_url = res.headers["Location"]
res = requests.get(png_url, allow_redirects=False)
assert res.status_code == 302
res = requests.get(url + "minotauros.xml")
assert res.status_code == 200
assert "" in res.text
matches = re.search(r"([0-9a-f]{16})", res.text)
assert matches
key = matches[1]
print(key)
elif test_no == 5:
url = url + "/"
res = requests.get(url)
assert res.status_code == 200
assert res.headers["Content-Type"] == "application/x-pigzip"
assert res.text.strip() == "42"
res = requests.get(url, headers={"Accept": "*/*;q=1.0, application/x-pigzip;q=0.9"})
assert res.status_code == 413
assert "Due to limited dimensions of your space-time continuum, we do not serve infinite files." in res.text
res = requests.get(url, headers={"Range": "bytes=0-999999", "Accept": "application/x-ksp-key"})
assert res.status_code == 413
assert "Due to global crisis, we do not send more than 1KB at a time."
res = requests.get(url, headers={"Range": "bytes=0-1024", "Accept": "application/x-ksp-key"})
assert res.status_code == 206
matches = re.search(r"(?m)^klic=([0-9a-f]{16})$", res.text)
assert matches
key = matches[1]
print(key)
elif test_no == 6:
url = url + "/"
res = requests.get(url)
assert res.status_code == 200
assert "Pro tuto úlohu nebyl žádný klíč nastaven." in res.text
res = requests.get(url, headers={"Cache-Control": "no-cache"})
assert res.status_code == 200
assert re.match(r"^Klíč je [0-9a-f]{16}.$", res.text.strip())
key = res.text.replace("Klíč je ", "").strip()[:-1]
print(key)
elif test_no == 7:
url = url + "/"
res = requests.get(url)
assert res.status_code == 402
assert "The access to this resource is paid. Please insert coin." in res.text
res = requests.get(url, headers={"X-Coin": "bagr"})
assert res.status_code == 402
assert "This currency is not supported. Please pay in US dollars." in res.text
res = requests.get(url, headers={"X-Coin": "$1"})
assert res.status_code == 200
assert re.match(r"^Klíč je [0-9a-f]{16}.$", res.text.strip())
key = res.text.replace("Klíč je ", "").strip()[:-1]
res = requests.get(url, headers={"X-Coin": "$1000"})
assert res.status_code == 200
assert "Děkujeme za vaši štědrost" in res.text
print(key)
else:
print("Unknown test", file=sys.stderr)
exit(1)