include "bufio.m"; include "rfc822.m"; rfc822 := load RFC822 RFC822->PATH; Content, Rfclex: import rfc822; Word, QString: con ...; Maxrequest: con 16*1024; # more than enough for anything sensible init: fn(b: Bufio); Rfclex: adt { tok: int; wordval: string; mk: fn(a: array of byte): ref Rfclex; getc: fn(p: self ref Rfclex): int; ungetc: fn(p: self ref Rfclex); lex: fn(p: self ref Rfclex): int; unlex: fn(p: self ref Rfclex); skipws: fn(p: self ref Rfclex): int; line: fn(p: self ref Rfclex): string; }; readheaders: fn(fd: ref Bufio->Iobuf, limit: int): array of (string, array of byte); parseparams: fn(ps: ref Rfclex): list of (string, string); parsecontent: fn(ps: ref Rfclex, multipart: int, head: list of ref Content): list of ref Content; mimefields: fn(ps: ref Rfclex): list of (string, list of (string, string)); quotable: fn(s: string): int; quote: fn(s: string): string; sec2date: fn(secs: int): string; date2sec: fn(s: string): int; now: fn(): int; time: fn(): string; Content: adt{ generic: string; specific: string; params: list of (string, string); mk: fn(specific: string, generic: string, params: list of (string, string)): ref Content; check: fn(c: self ref Content, oks: list of ref Content): int; text: fn(c: self ref Content): string; }; suffixclass: fn(name: string): (ref Content, ref Content); dataclass: fn(a: array of byte): (ref Content, ref Content);
Init must be called before any other operation in the module. It must be given an instance of the Bufio module (see bufio(2)).
Readheaders reads a set of RFC822 header lines from fd, ended by an empty line. It returns an array of tuples (fieldname, value), one per header line. The string fieldname is the header line's field name, in lower case. The value gives the rest of the line, after removing any initial white space and appending any continuation lines, uninterpreted, as an array of bytes (not a string). Limit is the maximum allowed size of the header in bytes; usually that is Maxrequest. Readheaders returns the tuple (nil, nil) on end of file or if the header size limit is exceeded.
Rfclex takes a header line's value and produces a sequence of tokens. It provides the following operations:
Several functions take an Rfclex referring to a header line's value, parse it, and return a higher-level representation of its value.
Parseparams parses a sequence of parameter settings of the form (;attribute=value)* and returns a corresponding list of tuples (attribute, value). It returns nil if no parameters are found.
Parsecontent parses the values of fields such as Content-Type and Accept. The syntax is (loosely) a sequence of comma-separated elements of the form type, type/*, or type/subtype followed by an optional sequence of parameters of the form (;attribute=value )*. The type/subtype form is allowed only if multipart is true (non-zero). It returns a corresponding list of Content values followed by the initial list head.
Mimefields parses a sequence of comma-separated elements of the form word(.BI;attr=val )* as used for instance in the rule transfer-coding. It returns a corresponding list of tuples (word, l) where l is an optional list of tuples (attr, val).
When producing an RFC822 header line, words must be quoted when they contain certain special characters. Quotable returns true iff the string s contains any of those characters. Quote returns the value of s with quotes added as required.
RFC822 headers have a particular syntax for dates, different from that of daytime(2). The function sec2date returns a string in RFC822 date format representing the time secs (in seconds from the Epoch). Date2sec takes a string in RFC822 date format and returns the time in seconds from the Epoch. Now returns the current time in seconds from the epoch (it is equivalent to Daytime->now() from daytime(2)). Time returns the current time in RFC822's date format.
The Multipurpose Internet Mail Extensions (see RFC2045-7) include syntax for describing different types of media, content, and content encodings. Content values represent those descriptions. Its fields and operations are as follows:
Given the name of a file, suffixclass returns a tuple (type, enc) where type gives the MIME Content-Type of name (or nil, if its type is not known), and enc gives the MIME Content-Encoding of name (or nil, if it is not encoded).
RFC822(2 ) | Rev: Tue Mar 31 02:42:39 GMT 2015 |