/* * mimefilter.aikido * * Aikido Language System, * export version: 1.00 * Copyright (c) 2002 Sun Microsystems, Inc. * * Sun Public License Notice * * The contents of this file are subject to the Sun Public License Version 1.0 (the "License"). You * may not use this file except in compliance with the License. A copy of the License is available * at http://www.opensource.org/licenses/sunpublic.php * * The Original Code is Aikido. * The Initial Developer of the Original Code is David Allison on behalf of Sun Microsystems, Inc. * Copyright (C) Sun Microsystems, Inc. 2000-2003. All Rights Reserved. * * * Contributor(s): dallison * * Version: 1.4 * Created by dallison on 4/19/2002 * Last modified by dallison on 03/07/29 */ class MimeFilter (instream, outstream, outdata = true, indata = false) { import properties import ctype protected var headersSent = false protected var inheaders = new Properties() protected var outheaders = new Properties() function toupper (s) { return System.transform (s, ctype.toupper) //return s } public function putHeader (name, value) { outheaders.put (toupper(name), value) } public function getHeader (name) { return inheaders[toupper(name)] } public function writeHeaders(stream) { if (headersSent) { return } foreach header outheaders { [header.first, ": ", header.second, "\r\n"] -> stream } "\r\n" -> stream headersSent = true ; } public function readHeaders (stream) { var line = "" inheaders.clear() // read the headers for (;;) { stream -> line // read one line if (sizeof (line) == 0 || line[0] == '\r') { break } if (line[sizeof(line) - 1] == '\n') { line >>= 1 } var ex = line["([^:]+):(.*)"] if (sizeof (ex) != 3) { break } var prop = line[ex[1].start:ex[1].end] var val = line[ex[2].start:ex[2].end] inheaders.put (toupper (prop), val) } } public operator[] (i, j = -1) { if (j != -1) { throw "range not allowed" } return inheaders[i] } public operator -> (data, isout) { if (isout) { if (!headersSent) { writeHeaders (outstream) outheaders.clear() } if (outdata) { data -> outstream // output rest of data } } else { readHeaders (instream) if (indata) { instream -> data } } } }