Plan 9 from Bell Labs’s /usr/web/sources/contrib/maht/inferno/appl/lib/mime.b

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


implement Mime;

include "mime.m";
include "sys.m";
	sys: Sys;

EOL : con "\r\n"; # End of Line
BOB: con "--"; # Begining of Boundary

Keyval.to_string(k : self ref Keyval) : string
{
	return k.key + "=\"" + k.value + "\"";
}

Disposition.to_string(d : self ref Disposition) : string
{
	a : ref Keyval;
	s := "Content-Disposition: " + d.d_type;
	for(ats := d.attributes; ats != nil; ats = tl ats) {
		a = hd ats;
		s += "; " + a.to_string();
	}
	return s;
}

Document.bytes(m : self ref Document) : array of byte
{
	sys = load Sys Sys->PATH;
	content_length := m.payload_size + (len array of byte BOB + len array of byte m.boundary + len array of byte EOL + len array of byte EOL) * (len m.parts) + len array of byte BOB + len array of byte m.boundary + len array of byte ("--" + EOL);

	bytes := array[content_length] of byte;

	bi := 0;
	for(ps := m.parts; ps != nil; ps = tl ps) {
		bytes[bi:] = array of byte BOB;
		bi += len array of byte BOB;
		bytes[bi:] = array of byte m.boundary;
		bi += len array of byte m.boundary;
		bytes[bi:] = array of byte EOL;
		bi += len array of byte EOL;
		bytes[bi:] = array of byte hd ps;
		bi += len array of byte hd ps;
		bytes[bi:] = array of byte EOL;
		bi += len array of byte EOL;
	}
	bytes[bi:] = array of byte BOB;
	bi += len array of byte BOB;
	bytes[bi:] = array of byte m.boundary;
	bi += len array of byte m.boundary;
	bytes[bi:] = array of byte ("--" + EOL);
	return bytes;
}

Document.add_part(m : self ref Document, disposition : ref Disposition, content_type : string, content : array of byte)
{
	header := disposition.to_string() + EOL;
	if(content_type != "")
		header += "Content-Type: " + content_type + EOL;
	header += EOL;
	bytes := array[len array of byte header + len content] of byte;
	bytes[0:] = array of byte header;
	bytes[len array of byte header:] = content;
	m.parts = bytes :: m.parts;
	m.payload_size += len bytes;
}

make_boundary() : string
{
	return "X-----------------MIME__-_BoUndary0x0r";
}

new_document() : ref Document
{
	d := ref Document;
	d.payload_size = 0;
	d.boundary = make_boundary();
	return d;
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].