From bcbe6220310b9161ccc0ab3b64c5152aedbc9da4 Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Fri, 2 Jun 2023 11:40:37 +0200 Subject: [PATCH] message: implement header_write --- message/header.ha | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/message/header.ha b/message/header.ha index a323305..c54bc3c 100644 --- a/message/header.ha +++ b/message/header.ha @@ -340,6 +340,50 @@ export fn header_set(head: *header, key: str, val: str) void = { assert(vals[0] == "text/x-hare"); }; +// Writes a MIME [[header]] to an [[io::handle]]. +export fn write_header(sink: io::handle, head: *header) (size | io::error) = { + let z = 0z; + for (let i = len(head.fields); i > 0; i -= 1) { + const field = head.fields[i - 1]; + const raw = header_field_raw(field)!; + z += io::writeall(sink, raw)?; + }; + z += fmt::fprint(sink, "\r\n")?; + return z; +}; + +@test fn write_header() void = { + const head = new_header(); + defer header_finish(&head); + + const dkim = "DKIM-Signature: a=rsa-sha256; bh=uI/rVH7mLBSWkJVvQYKz3TbpdI2BLZWTIMKcuo0KHOI=; c=simple/simple; d=example.org; h=Subject:To:From; s=default; t=1577562184; v=1; b=;\r\n"; + header_add(&head, "DKIM-Signature", "a=rsa-sha256; bh=uI/rVH7mLBSWkJVvQYKz3TbpdI2BLZWTIMKcuo0KHOI=; c=simple/simple; d=example.org; h=Subject:To:From; s=default; t=1577562184; v=1; b=;"); + header_add_raw(&head, strings::toutf8(dkim)); + header_add(&head, "Content-Type", "text/html"); + header_add(&head, "Content-Type", "text/plain"); + header_add(&head, "FROM", "Harriet "); + header_add(&head, "to", "Drew DeVault "); + + const sink = strio::dynamic(); + defer io::close(&sink)!; + write_header(&sink, &head)!; + const result = strio::string(&sink); + + const expect = + "To: Drew DeVault \r\n" + "From: Harriet \r\n" + "Content-Type: text/plain\r\n" + "Content-Type: text/html\r\n" + // Should not fold or canonicalize + "DKIM-Signature: a=rsa-sha256; bh=uI/rVH7mLBSWkJVvQYKz3TbpdI2BLZWTIMKcuo0KHOI=; c=simple/simple; d=example.org; h=Subject:To:From; s=default; t=1577562184; v=1; b=;\r\n" + // Should fold & canonicalize + "Dkim-Signature: a=rsa-sha256;\r\n" + " bh=uI/rVH7mLBSWkJVvQYKz3TbpdI2BLZWTIMKcuo0KHOI=; c=simple/simple;\r\n" + " d=example.org; h=Subject:To:From; s=default; t=1577562184; v=1; b=;\r\n" + "\r\n"; + assert(result == expect); +}; + def PREFERRED_HEADER_LEN = 76z; def MAX_HEADER_LEN = 998z; -- 2.45.2