import core::http; share fn exercise(base: string, private_ca: bool) -> i64 { var options = http::client_options(); options::use_private_ca = private_ca; if private_ca { options::ca_file = "server.pem"; } let hello = http::request(http::client_request("GET", base + "/hello")::value, options); if not hello::ok { print(hello::error::code); return 1; } let text = http::decode_utf8(hello::value::body); if not text::ok or text::value not matches "مرحبا من ASPL" { return 2; } var json = http::client_request("POST", base + "/submit")::value; json::body = http::encode_utf8("{\"message\":\"مرحبا\"}"); json::headers = [http::text_header("Content-Type", "application/json; charset=utf-8")::value]; let submitted = http::request(json, options); if not submitted::ok { print(submitted::error::code); return 3; } if http::decode_utf8(submitted::value::body)::value not matches "{\"message\":\"مرحبا\"}" { return 4; } var upload = http::client_request("POST", base + "/bytes")::value; upload::body = [0, 255, 65]; let sent = http::request(upload, options); if not sent::ok { print(sent::error::code); return 5; } let downloaded = http::request(http::client_request("GET", base + "/download")::value, options); if not downloaded::ok { print(downloaded::error::code); return 6; } let uploaded_bytes = sent::value::body; let downloaded_bytes = downloaded::value::body; if length(uploaded_bytes) not matches 3 or uploaded_bytes[0] not matches 0 or uploaded_bytes[1] not matches 255 or uploaded_bytes[2] not matches 65 { return 7; } if length(downloaded_bytes) not matches 3 or downloaded_bytes[0] not matches 0 or downloaded_bytes[1] not matches 255 or downloaded_bytes[2] not matches 65 { return 8; } let invalid_text = http::decode_utf8(downloaded_bytes); if invalid_text::ok or invalid_text::error::code not matches "http.invalid_utf8" { return 9; } let missing = http::request(http::client_request("GET", base + "/missing")::value, options); if not missing::ok or missing::value::status not matches 404 { return 10; } print("checked text, JSON, 00 FF 41, and 404"); return 0; }