speed enhancement for integrated http server:

- tuning hacks in template engine
- bypassing the template engine if no servlet present

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@5389 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 16 years ago
parent 7cd08bd5fb
commit db6b3bf5a3

@ -118,7 +118,6 @@ public class htmlFilterContentTransformer extends htmlFilterAbstractTransformer
final serverByteBuffer sbb = new serverByteBuffer(text); final serverByteBuffer sbb = new serverByteBuffer(text);
final serverByteBuffer[] sbbs = httpTemplate.splitQuotations(sbb); final serverByteBuffer[] sbbs = httpTemplate.splitQuotations(sbb);
//sbb = new serverByteBuffer();
for (int i = 0; i < sbbs.length; i++) { for (int i = 0; i < sbbs.length; i++) {
// TODO: avoid empty if statements // TODO: avoid empty if statements
if (sbbs[i].isWhitespace(true)) { if (sbbs[i].isWhitespace(true)) {

@ -128,6 +128,7 @@ public class htmlFilterInputStream extends InputStream implements htmlFilterEven
int c; int c;
while ((c = this.reader.read())!= -1) { while ((c = this.reader.read())!= -1) {
this.writer.write(c); this.writer.write(c);
if (this.charsetChanged) break; // thats enough
} }
// free writer // free writer

@ -49,7 +49,6 @@ package de.anomic.http;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -122,48 +121,46 @@ import de.anomic.server.logging.serverLog;
*/ */
public final class httpTemplate { public final class httpTemplate {
public static final byte hash = (byte)'#'; public final static byte hash = (byte)'#';
private static final byte[] hasha = {hash};
private static final byte dp = (byte)':'; public final static byte[] dpdpa = "::".getBytes();
public static final byte[] dpdpa = {dp, dp};
private static final byte lbr = (byte)'['; private final static byte lbr = (byte)'[';
private static final byte rbr = (byte)']'; private final static byte rbr = (byte)']';
private static final byte[] pOpen = {hash, lbr}; private final static byte[] pOpen = {hash, lbr};
private static final byte[] pClose = {rbr, hash}; private final static byte[] pClose = {rbr, hash};
private static final byte lcbr = (byte)'{'; private final static byte lcbr = (byte)'{';
private static final byte rcbr = (byte)'}'; private final static byte rcbr = (byte)'}';
private static final byte[] mOpen = {hash, lcbr}; private final static byte[] mOpen = {hash, lcbr};
private static final byte[] mClose = {rcbr, hash}; private final static byte[] mClose = {rcbr, hash};
private static final byte lrbr = (byte)'('; private final static byte lrbr = (byte)'(';
private static final byte rrbr = (byte)')'; private final static byte rrbr = (byte)')';
private static final byte[] aOpen = {hash, lrbr}; private final static byte[] aOpen = {hash, lrbr};
private static final byte[] aClose = {rrbr, hash}; private final static byte[] aClose = {rrbr, hash};
private static final byte ps = (byte)'%'; private final static byte ps = (byte)'%';
private static final byte[] iOpen = {hash, ps}; private final static byte[] iOpen = {hash, ps};
private static final byte[] iClose = {ps, hash}; private final static byte[] iClose = {ps, hash};
public static final byte[] slash = {(byte)'/'}; private final static byte[] slash = {(byte)'/'};
public static final Object[] meta_quotation = new Object[] { private final static Object[] meta_quotation = new Object[] {
new Object[] {pOpen, pClose}, new Object[] {pOpen, pClose},
new Object[] {mOpen, mClose}, new Object[] {mOpen, mClose},
new Object[] {aOpen, aClose}, new Object[] {aOpen, aClose},
new Object[] {iOpen, iClose} new Object[] {iOpen, iClose}
}; };
public static serverByteBuffer[] splitQuotations(final serverByteBuffer text) { public final static serverByteBuffer[] splitQuotations(final serverByteBuffer text) {
final List<serverByteBuffer> l = splitQuotation(text, 0); final List<serverByteBuffer> l = splitQuotation(text, 0);
final serverByteBuffer[] sbbs = new serverByteBuffer[l.size()]; final serverByteBuffer[] sbbs = new serverByteBuffer[l.size()];
for (int i = 0; i < l.size(); i++) sbbs[i] = l.get(i); for (int i = 0; i < l.size(); i++) sbbs[i] = l.get(i);
return sbbs; return sbbs;
} }
public static List<serverByteBuffer> splitQuotation(serverByteBuffer text, int qoff) { private final static List<serverByteBuffer> splitQuotation(serverByteBuffer text, int qoff) {
final ArrayList<serverByteBuffer> l = new ArrayList<serverByteBuffer>(); final ArrayList<serverByteBuffer> l = new ArrayList<serverByteBuffer>();
if (qoff >= meta_quotation.length) { if (qoff >= meta_quotation.length) {
if (text.length() > 0) l.add(text); if (text.length() > 0) l.add(text);
@ -205,20 +202,20 @@ public final class httpTemplate {
* transfer until a specified pattern is found; everything but the pattern is transfered so far * transfer until a specified pattern is found; everything but the pattern is transfered so far
* the function returns true, if the pattern is found * the function returns true, if the pattern is found
*/ */
private static boolean transferUntil(final PushbackInputStream i, final OutputStream o, final byte[] pattern) throws IOException { private final static boolean transferUntil(final PushbackInputStream i, final OutputStream o, final byte[] pattern) throws IOException {
int b, bb; int b, bb;
boolean equal; boolean equal;
while ((b = i.read()) > 0) { while ((b = i.read()) > 0) {
if ((b & 0xFF) == pattern[0]) { if ((b & 0xFF) == pattern[0]) {
// read the whole pattern // read the whole pattern
equal = true; equal = true;
for (int n = 1; n < pattern.length; n++) { lo: for (int n = 1; n < pattern.length; n++) {
if (((bb = i.read()) & 0xFF) != pattern[n]) { if (((bb = i.read()) & 0xFF) != pattern[n]) {
// push back all // push back all
i.unread(bb); i.unread(bb);
equal = false; equal = false;
for (int nn = n - 1; nn > 0; nn--) i.unread(pattern[nn]); for (int nn = n - 1; nn > 0; nn--) i.unread(pattern[nn]);
break; break lo;
} }
} }
if (equal) return true; if (equal) return true;
@ -228,46 +225,51 @@ public final class httpTemplate {
return false; return false;
} }
public static void writeTemplate(final InputStream in, final OutputStream out, final HashMap<String, String> pattern, final byte[] dflt) throws IOException { private final static boolean transferUntil(final PushbackInputStream i, final OutputStream o, final byte p) throws IOException {
int b;
while ((b = i.read()) > 0) {
if ((b & 0xFF) == p) return true;
o.write(b);
}
return false;
}
public final static void writeTemplate(final InputStream in, final OutputStream out, final HashMap<String, String> pattern, final byte[] dflt) throws IOException {
if (pattern == null) {
serverFileUtils.copy(in, out);
} else {
writeTemplate(in, out, pattern, dflt, new byte[0]); writeTemplate(in, out, pattern, dflt, new byte[0]);
} }
}
/** /**
* Reads a input stream, and writes the data with replaced templates on a output stream * Reads a input stream, and writes the data with replaced templates on a output stream
*/ */
public static byte[] writeTemplate(final InputStream in, final OutputStream out, final HashMap<String, String> pattern, final byte[] dflt, final byte[] prefix) throws IOException { private final static byte[] writeTemplate(final InputStream in, final OutputStream out, final HashMap<String, String> pattern, final byte[] dflt, final byte[] prefix) throws IOException {
final PushbackInputStream pis = new PushbackInputStream(in, 100); final PushbackInputStream pis = new PushbackInputStream(in, 100);
ByteArrayOutputStream keyStream; ByteArrayOutputStream keyStream = new ByteArrayOutputStream(512);
byte[] key; byte[] key;
byte[] multi_key; byte[] multi_key;
byte[] replacement; byte[] replacement;
int bb; int bb;
final serverByteBuffer structure=new serverByteBuffer(); final serverByteBuffer structure = new serverByteBuffer();
while (transferUntil(pis, out, hash)) {
while (transferUntil(pis, out, hasha)) {
bb = pis.read(); bb = pis.read();
keyStream = new ByteArrayOutputStream(512); keyStream.reset();
if( (bb & 0xFF) == lcbr ){ //multi // #{
if( transferUntil(pis, keyStream, mClose) ){ //close tag if ((bb & 0xFF) == lcbr) { //multi
if (transferUntil(pis, keyStream, mClose)) { //close tag
//multi_key = "_" + keyStream.toString(); //for _Key //multi_key = "_" + keyStream.toString(); //for _Key
bb = pis.read(); bb = pis.read();
if( (bb & 0xFF) != 10){ //kill newline if ((bb & 0xFF) != 10){ //kill newline
pis.unread(bb); pis.unread(bb);
} }
multi_key = keyStream.toByteArray(); //IMPORTANT: no prefix here multi_key = keyStream.toByteArray(); //IMPORTANT: no prefix here
keyStream.reset(); //reset stream keyStream.reset(); //reset stream
/* DEBUG - print key + value
try{
System.out.println("Key: "+prefix+multi_key+ "; Value: "+pattern.get(prefix+multi_key));
}catch(NullPointerException e){
System.out.println("Key: "+prefix+multi_key);
}
*/
//this needs multi_key without prefix //this needs multi_key without prefix
if( transferUntil(pis, keyStream, appendBytes(mOpen,slash,multi_key,mClose))){ if (transferUntil(pis, keyStream, appendBytes(mOpen,slash,multi_key,mClose))){
bb = pis.read(); bb = pis.read();
if((bb & 0xFF) != 10){ //kill newline if((bb & 0xFF) != 10){ //kill newline
pis.unread(bb); pis.unread(bb);
@ -282,42 +284,33 @@ public final class httpTemplate {
}catch(final NumberFormatException e){ }catch(final NumberFormatException e){
num=0; num=0;
} }
//System.out.println(multi_key + ": " + num); //DEBUG
} }
//Enumeration enx = pattern.keys(); while (enx.hasMoreElements()) System.out.println("KEY=" + enx.nextElement()); // DEBUG structure.append('<')
structure.append("<".getBytes("UTF-8"))
.append(multi_key) .append(multi_key)
.append(" type=\"multi\" num=\"".getBytes("UTF-8")) .append(" type=\"multi\" num=\"".getBytes())
.append(Integer.toString(num).getBytes("UTF-8")) .append(Integer.toString(num).getBytes())
.append("\">\n".getBytes("UTF-8")); .append("\">\n".getBytes());
for(int i=0;i < num;i++ ){ for(int i=0;i < num;i++) {
final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text)); final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(text));
//System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG //System.out.println("recursing with text(prefix="+ multi_key + "_" + i + "_" +"):"); //DEBUG
//System.out.println(text); //System.out.println(text);
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,multi_key,i))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,multi_key,i)));
}//for }//for
structure.append("</".getBytes("UTF-8")).append(multi_key).append(">\n".getBytes("UTF-8")); structure.append("</".getBytes()).append(multi_key).append(">\n".getBytes());
}else{//transferUntil } else {//transferUntil
serverLog.logSevere("TEMPLATE", "No Close Key found for #{"+new String(multi_key)+"}#"); //prefix here? serverLog.logSevere("TEMPLATE", "No Close Key found for #{"+new String(multi_key)+"}#"); //prefix here?
} }
} }
}else if( (bb & 0xFF) == lrbr ){ //alternatives
// #(
} else if ((bb & 0xFF) == lrbr) { //alternative
int others=0; int others=0;
final serverByteBuffer text= new serverByteBuffer(); final serverByteBuffer text= new serverByteBuffer();
PushbackInputStream pis2;
transferUntil(pis, keyStream, aClose); transferUntil(pis, keyStream, aClose);
key = keyStream.toByteArray(); //Caution: Key does not contain prefix key = keyStream.toByteArray(); //Caution: Key does not contain prefix
/* DEBUG - print key + value
try{
System.out.println("Key: "+prefix+key+ "; Value: "+pattern.get(prefix+key));
}catch(NullPointerException e){
System.out.println("Key: "+prefix+key);
}
*/
keyStream.reset(); //clear keyStream.reset(); //clear
boolean byName=false; boolean byName=false;
@ -338,43 +331,44 @@ public final class httpTemplate {
int currentPattern=0; int currentPattern=0;
boolean found=false; boolean found=false;
keyStream.reset(); //reset stream keyStream.reset(); //reset stream
if(byName){ PushbackInputStream pis2;
if (byName) {
//TODO: better Error Handling //TODO: better Error Handling
transferUntil(pis, keyStream,appendBytes("%%".getBytes("UTF-8"),patternName,null,null)); transferUntil(pis, keyStream, appendBytes("%%".getBytes(), patternName, null, null));
if(pis.available()==0){ if(pis.available()==0){
serverLog.logSevere("TEMPLATE", "No such Template: %%"+new String(patternName)); serverLog.logSevere("TEMPLATE", "No such Template: %%"+new String(patternName));
return structure.getBytes(); return structure.getBytes();
} }
keyStream.reset(); keyStream.reset();
transferUntil(pis, keyStream, "::".getBytes()); transferUntil(pis, keyStream, dpdpa);
pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toByteArray())); pis2 = new PushbackInputStream(new ByteArrayInputStream(keyStream.toByteArray()));
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
transferUntil(pis, keyStream, appendBytes("#(/".getBytes("UTF-8"),key,")#".getBytes("UTF-8"),null)); transferUntil(pis, keyStream, appendBytes("#(/".getBytes(),key,")#".getBytes("UTF-8"),null));
if(pis.available()==0){ if(pis.available()==0){
serverLog.logSevere("TEMPLATE", "No Close Key found for #("+new String(key)+")# (by Name)"); serverLog.logSevere("TEMPLATE", "No Close Key found for #("+new String(key)+")# (by Name)");
} }
}else{ } else {
while(!found){ while(!found){
bb=pis.read(); // performance problem? trace always points to this line
if ((bb & 0xFF) == hash){
bb=pis.read(); bb=pis.read();
if( (bb & 0xFF) == hash){ if ((bb & 0xFF) == lrbr){
bb=pis.read();
if( (bb & 0xFF) == lrbr){
transferUntil(pis, keyStream, aClose); transferUntil(pis, keyStream, aClose);
//reached the end. output last string. //reached the end. output last string.
if (java.util.Arrays.equals(keyStream.toByteArray(),appendBytes(slash, key, null,null))) { if (java.util.Arrays.equals(keyStream.toByteArray(),appendBytes(slash, key, null,null))) {
pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes()));
//this maybe the wrong, but its the last //this maybe the wrong, but its the last
structure.append("<".getBytes("UTF-8")).append(key).append(" type=\"alternative\" which=\"".getBytes("UTF-8")).append(Integer.toString(whichPattern).getBytes("UTF-8")).append("\" found=\"0\">\n".getBytes("UTF-8")); structure.append('<').append(key).append(" type=\"alternative\" which=\"".getBytes()).append(Integer.toString(whichPattern).getBytes("UTF-8")).append("\" found=\"0\">\n".getBytes());
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
structure.append("</".getBytes("UTF-8")).append(key).append(">\n".getBytes("UTF-8")); structure.append("</".getBytes()).append(key).append(">\n".getBytes());
found=true; found=true;
}else if(others >0 && keyStream.toString().startsWith("/")){ //close nested }else if(others >0 && keyStream.toString().startsWith("/")){ //close nested
others--; others--;
text.append("#(".getBytes("UTF-8")).append(keyStream.toByteArray()).append(")#".getBytes("UTF-8")); text.append(aOpen).append(keyStream.toByteArray()).append(")#".getBytes());
}else{ //nested } else { //nested
others++; others++;
text.append("#(".getBytes("UTF-8")).append(keyStream.toByteArray()).append(")#".getBytes("UTF-8")); text.append(aOpen).append(keyStream.toByteArray()).append(")#".getBytes());
} }
keyStream.reset(); //reset stream keyStream.reset(); //reset stream
continue; continue;
@ -382,16 +376,16 @@ public final class httpTemplate {
pis.unread(bb);//is processed in next loop pis.unread(bb);//is processed in next loop
bb = (hash);//will be added to text this loop bb = (hash);//will be added to text this loop
//text += "#"; //text += "#";
}else if( (bb & 0xFF) == ':' && others==0){//ignore :: in nested Expressions }else if ((bb & 0xFF) == ':' && others==0){//ignore :: in nested Expressions
bb=pis.read(); bb=pis.read();
if( (bb & 0xFF) == ':'){ if ((bb & 0xFF) == ':'){
if(currentPattern == whichPattern){ //found the pattern if(currentPattern == whichPattern){ //found the pattern
pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes())); pis2 = new PushbackInputStream(new ByteArrayInputStream(text.getBytes()));
structure.append("<".getBytes("UTF-8")).append(key).append(" type=\"alternative\" which=\"".getBytes("UTF-8")).append(Integer.toString(whichPattern).getBytes("UTF-8")).append("\" found=\"0\">\n".getBytes("UTF-8")); structure.append("<".getBytes()).append(key).append(" type=\"alternative\" which=\"".getBytes()).append(Integer.toString(whichPattern).getBytes("UTF-8")).append("\" found=\"0\">\n".getBytes());
structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key))); structure.append(writeTemplate(pis2, out, pattern, dflt, newPrefix(prefix,key)));
structure.append("</".getBytes("UTF-8")).append(key).append(">\n".getBytes("UTF-8")); structure.append("</".getBytes()).append(key).append(">\n".getBytes());
transferUntil(pis, keyStream, appendBytes("#(/".getBytes("UTF-8"),key,")#".getBytes("UTF-8"),null));//to #(/key)#. transferUntil(pis, keyStream, appendBytes("#(/".getBytes(),key,")#".getBytes("UTF-8"),null));//to #(/key)#.
found=true; found=true;
} }
@ -399,7 +393,7 @@ public final class httpTemplate {
text.clear(); text.clear();
continue; continue;
} }
text.append(":".getBytes("UTF-8")); text.append(":".getBytes());
} }
if(!found){ if(!found){
text.append((byte)bb);/* text.append((byte)bb);/*
@ -410,23 +404,17 @@ public final class httpTemplate {
} }
}//while }//while
}//if(byName) (else branch) }//if(byName) (else branch)
}else if( (bb & 0xFF) == lbr ){ //normal
// #[
} else if ((bb & 0xFF) == lbr) { //normal
if (transferUntil(pis, keyStream, pClose)) { if (transferUntil(pis, keyStream, pClose)) {
// pattern detected, write replacement // pattern detected, write replacement
key = keyStream.toByteArray(); key = keyStream.toByteArray();
final String patternKey = getPatternKey(prefix, key); final String patternKey = getPatternKey(prefix, key);
replacement = replacePattern(patternKey, pattern, dflt); //replace replacement = replacePattern(patternKey, pattern, dflt); //replace
structure.append("<".getBytes("UTF-8")).append(key).append(" type=\"normal\">\n".getBytes("UTF-8")); structure.append("<".getBytes()).append(key).append(" type=\"normal\">\n".getBytes());
structure.append(replacement); structure.append(replacement);
structure.append("</".getBytes("UTF-8")).append(key).append(">\n".getBytes("UTF-8")); structure.append("</".getBytes()).append(key).append(">\n".getBytes());
/* DEBUG
try{
System.out.println("Key: "+key+ "; Value: "+pattern.get(key));
}catch(NullPointerException e){
System.out.println("Key: "+key);
}
*/
serverFileUtils.copy(replacement, out); serverFileUtils.copy(replacement, out);
} else { } else {
@ -434,7 +422,9 @@ public final class httpTemplate {
serverFileUtils.copy(pis, out); serverFileUtils.copy(pis, out);
return structure.getBytes(); return structure.getBytes();
} }
}else if( (bb & 0xFF) == ps){ //include
// #%
} else if ((bb & 0xFF) == ps) { //include
final serverByteBuffer include = new serverByteBuffer(); final serverByteBuffer include = new serverByteBuffer();
keyStream.reset(); //reset stream keyStream.reset(); //reset stream
if(transferUntil(pis, keyStream, iClose)){ if(transferUntil(pis, keyStream, iClose)){
@ -453,8 +443,8 @@ public final class httpTemplate {
br = new BufferedReader( new InputStreamReader(new FileInputStream( httpdFileHandler.getLocalizedFile(new String(filename,"UTF-8"))),"UTF-8") ); //YaCy (with Locales) br = new BufferedReader( new InputStreamReader(new FileInputStream( httpdFileHandler.getLocalizedFile(new String(filename,"UTF-8"))),"UTF-8") ); //YaCy (with Locales)
//Read the Include //Read the Include
String line = ""; String line = "";
while( (line = br.readLine()) != null ){ while ((line = br.readLine()) != null) {
include.append(line.getBytes("UTF-8")).append(de.anomic.server.serverCore.CRLF_STRING.getBytes("UTF-8")); include.append(line.getBytes("UTF-8")).append(de.anomic.server.serverCore.CRLF_STRING.getBytes());
} }
} catch (final IOException e) { } catch (final IOException e) {
//file not found? //file not found?
@ -463,23 +453,22 @@ public final class httpTemplate {
if (br != null) try { br.close(); br=null; } catch (final Exception e) {} if (br != null) try { br.close(); br=null; } catch (final Exception e) {}
} }
final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(include.getBytes())); final PushbackInputStream pis2 = new PushbackInputStream(new ByteArrayInputStream(include.getBytes()));
structure.append("<fileinclude file=\"".getBytes("UTF-8")).append(filename).append(">\n".getBytes("UTF-8")); structure.append("<fileinclude file=\"".getBytes()).append(filename).append(">\n".getBytes());
structure.append(writeTemplate(pis2, out, pattern, dflt, prefix)); structure.append(writeTemplate(pis2, out, pattern, dflt, prefix));
structure.append("</fileinclude>\n".getBytes("UTF-8")); structure.append("</fileinclude>\n".getBytes());
} }
} }
}else{ //no match, but a single hash (output # + bb)
final byte[] tmp=new byte[2]; // # - no special character. This is simply a '#' without meaning
tmp[0]=hash; } else { //no match, but a single hash (output # + bb)
tmp[1]=(byte)bb; out.write(hash);
serverFileUtils.copy(tmp, out); out.write(bb);
} }
} }
//System.out.println(structure.toString()); //DEBUG
return structure.getBytes(); return structure.getBytes();
} }
public static byte[] replacePattern(final String key, final HashMap<String, String> pattern, final byte dflt[]) { private final static byte[] replacePattern(final String key, final HashMap<String, String> pattern, final byte dflt[]) {
byte[] replacement; byte[] replacement;
Object value; Object value;
if (pattern.containsKey(key)) { if (pattern.containsKey(key)) {
@ -503,41 +492,7 @@ public final class httpTemplate {
return replacement; return replacement;
} }
public static void main(final String[] args) { private final static byte[] newPrefix(final byte[] oldPrefix, final byte[] key) {
// arg1 = test input; arg2 = replacement for pattern 'test'; arg3 = default replacement
try {
final InputStream i = new ByteArrayInputStream(args[0].getBytes("UTF-8"));
final HashMap<String, String> h = new HashMap<String, String>();
h.put("test", args[1]);
writeTemplate(new PushbackInputStream(i, 100), System.out, h, args[2].getBytes("UTF-8"));
System.out.flush();
} catch (final Exception e) {
e.printStackTrace();
}
}
/*
* loads all Files from path into a filename->content HashMap
*/
public static HashMap<String, String> loadTemplates(File path) {
// reads all templates from a path
// we use only the folder from the given file path
final HashMap<String, String> result = new HashMap<String, String>();
if (path == null) return result;
if (!(path.isDirectory())) path = path.getParentFile();
if ((path == null) || (!(path.isDirectory()))) return result;
final String[] templates = path.list();
for (int i = 0; i < templates.length; i++) {
if (templates[i].endsWith(".template"))
try {
//System.out.println("TEMPLATE " + templates[i].substring(0, templates[i].length() - 9) + ": " + new String(buf, 0, c));
result.put(templates[i].substring(0, templates[i].length() - 9),
new String(serverFileUtils.read(new File(path, templates[i]))));
} catch (final Exception e) {}
}
return result;
}
public static byte[] newPrefix(final byte[] oldPrefix, final byte[] key) {
final serverByteBuffer newPrefix = new serverByteBuffer(); final serverByteBuffer newPrefix = new serverByteBuffer();
newPrefix.append(oldPrefix) newPrefix.append(oldPrefix)
.append(key) .append(key)
@ -551,30 +506,24 @@ public final class httpTemplate {
return result; return result;
} }
public static byte[] newPrefix(final byte[] oldPrefix, final byte[] multi_key, final int i) { private final static byte[] newPrefix(final byte[] oldPrefix, final byte[] multi_key, final int i) {
final serverByteBuffer newPrefix = new serverByteBuffer(); final serverByteBuffer newPrefix = new serverByteBuffer();
try {
newPrefix.append(oldPrefix) newPrefix.append(oldPrefix)
.append(multi_key) .append(multi_key)
.append("_".getBytes()) .append("_".getBytes())
.append(Integer.toString(i).getBytes("UTF-8")) .append(Integer.toString(i).getBytes())
.append("_".getBytes()); .append("_".getBytes());
} catch (final UnsupportedEncodingException e) {}
finally {
try { try {
newPrefix.close(); newPrefix.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
}
return newPrefix.getBytes(); return newPrefix.getBytes();
} }
public static String getPatternKey(final byte[] prefix, final byte[] key) { private final static String getPatternKey(final byte[] prefix, final byte[] key) {
final serverByteBuffer patternKey = new serverByteBuffer(); final serverByteBuffer patternKey = new serverByteBuffer();
patternKey.append(prefix) patternKey.append(prefix).append(key);
.append(key);
try { try {
return new String(patternKey.getBytes(),"UTF-8"); return new String(patternKey.getBytes(),"UTF-8");
} catch (final UnsupportedEncodingException e) { } catch (final UnsupportedEncodingException e) {
@ -588,7 +537,7 @@ public final class httpTemplate {
} }
} }
public static byte[] appendBytes(final byte[] b1, final byte[] b2, final byte[] b3, final byte[] b4) { private final static byte[] appendBytes(final byte[] b1, final byte[] b2, final byte[] b3, final byte[] b4) {
final serverByteBuffer byteArray = new serverByteBuffer(); final serverByteBuffer byteArray = new serverByteBuffer();
byteArray.append(b1) byteArray.append(b1)
.append(b2); .append(b2);
@ -603,4 +552,16 @@ public final class httpTemplate {
return result; return result;
} }
public static void main(final String[] args) {
// arg1 = test input; arg2 = replacement for pattern 'test'; arg3 = default replacement
try {
final InputStream i = new ByteArrayInputStream(args[0].getBytes("UTF-8"));
final HashMap<String, String> h = new HashMap<String, String>();
h.put("test", args[1]);
writeTemplate(new PushbackInputStream(i, 100), System.out, h, args[2].getBytes("UTF-8"));
System.out.flush();
} catch (final Exception e) {
e.printStackTrace();
}
}
} }

@ -489,7 +489,7 @@ public final class httpdFileHandler {
//File targetClass = rewriteClassFile(targetFile); //File targetClass = rewriteClassFile(targetFile);
//We need tp here //We need tp here
servletProperties tp = new servletProperties(); servletProperties templatePatterns = null;
Date targetDate; Date targetDate;
boolean nocache = false; boolean nocache = false;
@ -602,14 +602,14 @@ public final class httpdFileHandler {
final Object tmp = invokeServlet(targetClass, requestHeader, args); final Object tmp = invokeServlet(targetClass, requestHeader, args);
if (tmp == null) { if (tmp == null) {
// if no args given, then tp will be an empty Hashtable object (not null) // if no args given, then tp will be an empty Hashtable object (not null)
tp = new servletProperties(); templatePatterns = new servletProperties();
} else if (tmp instanceof servletProperties) { } else if (tmp instanceof servletProperties) {
tp = (servletProperties) tmp; templatePatterns = (servletProperties) tmp;
} else { } else {
tp = new servletProperties((serverObjects) tmp); templatePatterns = new servletProperties((serverObjects) tmp);
} }
// check if the servlets requests authentification // check if the servlets requests authentification
if (tp.containsKey(servletProperties.ACTION_AUTHENTICATE)) { if (templatePatterns.containsKey(servletProperties.ACTION_AUTHENTICATE)) {
// handle brute-force protection // handle brute-force protection
if (authorization != null) { if (authorization != null) {
serverLog.logInfo("HTTPD", "dynamic log-in for account 'admin' in http file handler for path '" + path + "' from host '" + clientIP + "'"); serverLog.logInfo("HTTPD", "dynamic log-in for account 'admin' in http file handler for path '" + path + "' from host '" + clientIP + "'");
@ -621,24 +621,24 @@ public final class httpdFileHandler {
} }
// send authentication request to browser // send authentication request to browser
final httpResponseHeader headers = getDefaultHeaders(path); final httpResponseHeader headers = getDefaultHeaders(path);
headers.put(httpRequestHeader.WWW_AUTHENTICATE,"Basic realm=\"" + tp.get(servletProperties.ACTION_AUTHENTICATE, "") + "\""); headers.put(httpRequestHeader.WWW_AUTHENTICATE,"Basic realm=\"" + templatePatterns.get(servletProperties.ACTION_AUTHENTICATE, "") + "\"");
httpd.sendRespondHeader(conProp,out,httpVersion,401,headers); httpd.sendRespondHeader(conProp,out,httpVersion,401,headers);
return; return;
} else if (tp.containsKey(servletProperties.ACTION_LOCATION)) { } else if (templatePatterns.containsKey(servletProperties.ACTION_LOCATION)) {
String location = tp.get(servletProperties.ACTION_LOCATION, ""); String location = templatePatterns.get(servletProperties.ACTION_LOCATION, "");
if (location.length() == 0) location = path; if (location.length() == 0) location = path;
final httpResponseHeader headers = getDefaultHeaders(path); final httpResponseHeader headers = getDefaultHeaders(path);
headers.setCookieVector(tp.getOutgoingHeader().getCookieVector()); //put the cookies into the new header TODO: can we put all headerlines, without trouble? headers.setCookieVector(templatePatterns.getOutgoingHeader().getCookieVector()); //put the cookies into the new header TODO: can we put all headerlines, without trouble?
headers.put(httpHeader.LOCATION,location); headers.put(httpHeader.LOCATION,location);
httpd.sendRespondHeader(conProp,out,httpVersion,302,headers); httpd.sendRespondHeader(conProp,out,httpVersion,302,headers);
return; return;
} }
// add the application version, the uptime and the client name to every rewrite table // add the application version, the uptime and the client name to every rewrite table
tp.put(servletProperties.PEER_STAT_VERSION, switchboard.getConfig("version", "")); templatePatterns.put(servletProperties.PEER_STAT_VERSION, switchboard.getConfig("version", ""));
tp.put(servletProperties.PEER_STAT_UPTIME, ((System.currentTimeMillis() - serverCore.startupTime) / 1000) / 60); // uptime in minutes templatePatterns.put(servletProperties.PEER_STAT_UPTIME, ((System.currentTimeMillis() - serverCore.startupTime) / 1000) / 60); // uptime in minutes
tp.putHTML(servletProperties.PEER_STAT_CLIENTNAME, switchboard.getConfig("peerName", "anomic")); templatePatterns.putHTML(servletProperties.PEER_STAT_CLIENTNAME, switchboard.getConfig("peerName", "anomic"));
tp.put(servletProperties.PEER_STAT_MYTIME, serverDate.formatShortSecond()); templatePatterns.put(servletProperties.PEER_STAT_MYTIME, serverDate.formatShortSecond());
//System.out.println("respond props: " + ((tp == null) ? "null" : tp.toString())); // debug //System.out.println("respond props: " + ((tp == null) ? "null" : tp.toString())); // debug
} catch (final InvocationTargetException e) { } catch (final InvocationTargetException e) {
if (e.getCause() instanceof InterruptedException) { if (e.getCause() instanceof InterruptedException) {
@ -726,8 +726,8 @@ public final class httpdFileHandler {
// send page in chunks and parse SSIs // send page in chunks and parse SSIs
final serverByteBuffer o = new serverByteBuffer(); final serverByteBuffer o = new serverByteBuffer();
// apply templates // apply templates
httpTemplate.writeTemplate(fis, o, tp, "-UNRESOLVED_PATTERN-".getBytes("UTF-8")); httpTemplate.writeTemplate(fis, o, templatePatterns, "-UNRESOLVED_PATTERN-".getBytes("UTF-8"));
httpd.sendRespondHeader(conProp, out, httpVersion, 200, null, mimeType, -1, targetDate, null, tp.getOutgoingHeader(), null, "chunked", nocache); httpd.sendRespondHeader(conProp, out, httpVersion, 200, null, mimeType, -1, targetDate, null, (templatePatterns == null) ? new httpResponseHeader() : templatePatterns.getOutgoingHeader(), null, "chunked", nocache);
// send the content in chunked parts, see RFC 2616 section 3.6.1 // send the content in chunked parts, see RFC 2616 section 3.6.1
final httpChunkedOutputStream chos = new httpChunkedOutputStream(out); final httpChunkedOutputStream chos = new httpChunkedOutputStream(out);
httpSSI.writeSSI(o, chos, authorization, clientIP); httpSSI.writeSSI(o, chos, authorization, clientIP);
@ -738,7 +738,7 @@ public final class httpdFileHandler {
final String contentEncoding = (zipContent) ? "gzip" : null; final String contentEncoding = (zipContent) ? "gzip" : null;
// apply templates // apply templates
final serverByteBuffer o1 = new serverByteBuffer(); final serverByteBuffer o1 = new serverByteBuffer();
httpTemplate.writeTemplate(fis, o1, tp, "-UNRESOLVED_PATTERN-".getBytes("UTF-8")); httpTemplate.writeTemplate(fis, o1, templatePatterns, "-UNRESOLVED_PATTERN-".getBytes("UTF-8"));
final serverByteBuffer o = new serverByteBuffer(); final serverByteBuffer o = new serverByteBuffer();
@ -757,13 +757,13 @@ public final class httpdFileHandler {
if (method.equals(httpHeader.METHOD_HEAD)) { if (method.equals(httpHeader.METHOD_HEAD)) {
httpd.sendRespondHeader(conProp, out, httpd.sendRespondHeader(conProp, out,
httpVersion, 200, null, mimeType, o.length(), httpVersion, 200, null, mimeType, o.length(),
targetDate, null, tp.getOutgoingHeader(), targetDate, null, (templatePatterns == null) ? new httpResponseHeader() : templatePatterns.getOutgoingHeader(),
contentEncoding, null, nocache); contentEncoding, null, nocache);
} else { } else {
final byte[] result = o.getBytes(); // this interrupts streaming (bad idea!) final byte[] result = o.getBytes(); // this interrupts streaming (bad idea!)
httpd.sendRespondHeader(conProp, out, httpd.sendRespondHeader(conProp, out,
httpVersion, 200, null, mimeType, result.length, httpVersion, 200, null, mimeType, result.length,
targetDate, null, tp.getOutgoingHeader(), targetDate, null, (templatePatterns == null) ? new httpResponseHeader() : templatePatterns.getOutgoingHeader(),
contentEncoding, null, nocache); contentEncoding, null, nocache);
serverFileUtils.copy(result, out); serverFileUtils.copy(result, out);
} }

Loading…
Cancel
Save