001: // httpSSI.java
002: // -----------------------------
003: // (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
004: // first published 26.06.2007 on http://yacy.net
005: //
006: // This is a part of YaCy, a peer-to-peer based web search engine
007: //
008: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
009: // $LastChangedRevision: 1986 $
010: // $LastChangedBy: orbiter $
011: //
012: // LICENSE
013: //
014: // This program is free software; you can redistribute it and/or modify
015: // it under the terms of the GNU General Public License as published by
016: // the Free Software Foundation; either version 2 of the License, or
017: // (at your option) any later version.
018: //
019: // This program is distributed in the hope that it will be useful,
020: // but WITHOUT ANY WARRANTY; without even the implied warranty of
021: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
022: // GNU General Public License for more details.
023: //
024: // You should have received a copy of the GNU General Public License
025: // along with this program; if not, write to the Free Software
026: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
027:
028: package de.anomic.http;
029:
030: import java.io.IOException;
031: import java.io.OutputStream;
032: import java.util.Properties;
033:
034: import de.anomic.server.serverByteBuffer;
035:
036: public class httpSSI {
037:
038: public static void writeSSI(serverByteBuffer in, OutputStream out,
039: String authorization) throws IOException {
040: writeSSI(in, 0, out, authorization);
041: }
042:
043: public static void writeSSI(serverByteBuffer in, int off,
044: OutputStream out, String authorization) throws IOException {
045: int p = in.indexOf("<!--#".getBytes(), off);
046: if (p >= 0) {
047: int q = in.indexOf("-->".getBytes(), p + 10);
048: if (out instanceof httpChunkedOutputStream) {
049: ((httpChunkedOutputStream) out).write(in, off, p - off);
050: } else {
051: out.write(in.getBytes(off, p - off));
052: }
053: parseSSI(in, p, q + 3 - p, out, authorization);
054: writeSSI(in, q + 3, out, authorization);
055: } else /* p < 0 */{
056: if (out instanceof httpChunkedOutputStream) {
057: ((httpChunkedOutputStream) out).write(in, off, in
058: .length()
059: - off);
060: } else {
061: out.write(in.getBytes(off, in.length() - off));
062: }
063: }
064: }
065:
066: private static void parseSSI(serverByteBuffer in, int off, int len,
067: OutputStream out, String authorization) {
068: if (in.startsWith("<!--#include virtual=\"".getBytes(), off)) {
069: int q = in.indexOf("\"".getBytes(), off + 22);
070: if (q > 0) {
071: String path = in.toString(off + 22, q);
072: writeContent(path, out, authorization);
073: }
074: }
075: }
076:
077: private static void writeContent(String path, OutputStream out,
078: String authorization) {
079: // check if there are arguments in path string
080: String args = "";
081: int argpos = path.indexOf('?');
082: if (argpos > 0) {
083: args = path.substring(argpos + 1);
084: path = path.substring(0, argpos);
085: }
086:
087: // set up virtual connection properties to call httpdFileHander.doGet()
088: Properties conProp = new Properties();
089: httpHeader header = new httpHeader(httpd.reverseMappingCache);
090: conProp.setProperty(httpHeader.CONNECTION_PROP_METHOD,
091: httpHeader.METHOD_GET);
092: conProp.setProperty(httpHeader.CONNECTION_PROP_PATH, path);
093: conProp.setProperty(httpHeader.CONNECTION_PROP_ARGS, args);
094: conProp.setProperty(httpHeader.CONNECTION_PROP_HTTP_VER,
095: httpHeader.HTTP_VERSION_0_9);
096: conProp.setProperty("CLIENTIP", "127.0.0.1");
097: header.put(httpHeader.AUTHORIZATION, authorization);
098: httpdFileHandler.doGet(conProp, header, out);
099: }
100: }
|