Charles.Forsyth20061220965a0ca19 years agoHistory
327 lines · 7.7 KB · Text
RawDownload
.TH RFC822 2.SH NAMErfc822 \- RFC822 mail format helpers.SH SYNOPSIS.EXinclude "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);.EE.SH DESCRIPTION.B RFC822provides types and functions to help read and parse RFC822 e-mail headers(following the more streamlined rules of RFC2822, which has replaced RFC822),including some MIME extensions.Currently the focus is on operations that support HTTP and other W3C protocols,rather than mail reading..PP.B Initmust be called before any other operation in the module.It must be given an instance of the.B Bufiomodule (see.IR bufio (2))..PP.B Readheadersreads a set of RFC822 header lines from.IR fd ,ended by an empty line.It returns an array of tuples.BI ( fieldname,\ value ),one per header line.The string.I fieldnameis the header line's field name, in lower case.The.I valuegives 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)..I Limitis the maximum allowed size of the header in bytes;usually that is.BR Maxrequest ..B Readheadersreturns the tuple.B (nil,\ nil)on end of file or if the header size limit is exceeded..PP.B Rfclextakes a header line's.I valueand produces a sequence of tokens.It provides the following operations:.TP.BI Rfclex.mk( a )Return a reference to a new.B Rfclexvalue that will produce tokens from the array of byte.IR a ,which is normally the.I valueof one of the header lines returned by.BR readheaders ..TP.IB rfl .getc()Return the next character from the input stream, in the Latin-1 (ISO 8859-1) character set.Return a negative value.RB ( Bufio->EOF )on end-of-file..TP.IB rfl .ungetc()Put back the last character read, which will be returned again by the next call to.IR rfl .getc ..TP.IB rfl .lex()Return the next token from the input stream, ignoring any leading white space.Most tokens are single characters representing themselves.The token value is also assigned to.IB rfl .tok .There are two special token values:.BR Word ,representing an unquoted word in the RFC2822 grammar; and.BR QString ,representing a quoted string.In both cases.IB rfl .wordvalwill contain the text of the word or string (without quotes)..TP.IB rfl .unlex()Put back the last token read; the next call to.IB rfl .lexwill return it again..TP.IB rfl .skipws()Skip over any white space at the current position;return the initial character of the next token (which is not consumed)..TP.IB rfl .line()Return a string giving the remainder of the input line..PPSeveral functions take an.B Rfclexreferring to a header line's value, parse it, and return a higher-level representation of its value..PP.B Parseparamsparses a sequence of.I parametersettings of the form (\c.BI ; attribute = value\c)*and returns a corresponding list of tuples.BI ( attribute,\ value ) .It returns nil if no parameters are found..PP.B Parsecontentparses the values of fields such as.B Content-Typeand.BR Accept .The syntax is (loosely) a sequence of comma-separated elements of the form.IR type ,.IB type /* ,or.IB type / subtypefollowed by an optional sequence of parameters of the form (\c. BI ; attribute = value)*.The.IB type  / subtypeform is allowed only if.I multipartis true (non-zero).It returns a corresponding list of.B Contentvalues followed by the initial list.IR head ..PP.B Mimefieldsparses a sequence of comma-separated elements of the form.IR word (\c.BI ; attr = val)*as used for instance in the rule.IR transfer-coding .It returns a corresponding list of tuples.BI ( word,\ l )where.I lis an optional list of tuples.BI ( attr,\ val ) ..PPWhen producing an RFC822 header line, words must be quoted when they contain certainspecial characters..B Quotablereturns true iff the string.I scontains any of those characters..B Quotereturns the value of.I swith quotes added as required..PPRFC822 headers have a particular syntax for dates, different from that of.IR daytime (2).The function.B sec2datereturns a string in RFC822 date format representing the time.I secs(in seconds from the Epoch)..B Date2sectakes a string in RFC822 date format and returns the time in seconds from the Epoch..B Nowreturns the current time in seconds from the epoch(it is equivalent to.B Daytime->now()from.IR daytime (2))..B Timereturns the current time in RFC822's date format..PPThe Multipurpose Internet Mail Extensions (see RFC2045-7) include syntaxfor describing different types of media, content, and content encodings..B Contentvalues represent those descriptions.Its fields and operations are as follows:.TP.B genericGeneral class of content (eg,.BR application ,.BR image ,.BR text ,etc).TP.B specificOptional particular type within that class(eg,.B octet-streamwithin.BR application ,or.B plainwithin.BR text ).TP.B paramsOptional list of.BI ( attr,\ value )pairs giving parameters to the content type or encoding.The particular attribute.B qhas a floating-point.I valuethat specifies the relative quality (utility) of a particular type or encoding to a client..TP.BI mk( generic,\ specific,\ params )Return a reference to a new.B Contentvalue, initialised with the given parameters..TP.IB c .check( ok )Return true if.I cis acceptable content/media/encoding according to the list of allowable forms in.IR ok ..I Cis always acceptable if.I okis nil (ie, there are no restrictions).Otherwise, at least one of the.B Contentvalues in.I okmust match.IR c .That is, an.I okvalue must have the same generic and specific types as.IR c ,or specify.RB ` * 'in one or both positions (to match any value in.IR c ).Any.I paramsare ignored..TP.IB c .text()Return the RFC822 syntax for.IR c :.IB generic / specific ; a\f5=\fIv\fR\&...where the component words are quoted if necessary..PPGiven the.I nameof a file,.B suffixclassreturns a tuple.BI ( type,\ enc )where.I typegives the MIME.B Content-Typeof.I name(or nil, if its type is not known), and.I encgives the MIME.B Content-Encodingof.I name(or nil, if it is not encoded)..SH FILES.TF /lib/mimetype.TP/lib/mimetypemap between file suffix and MIME content types.SH SOURCE.B /appl/lib/rfc822.b