Now custom httpHeader can be created

and filled with cookies and so on.

This header one can set into serverObjects

Check CookieTest.html and CookieTest.java for details.

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1334 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
(no author) 19 years ago
parent d69a182736
commit 001513cc1f

@ -10,6 +10,12 @@
</script> </script>
Here is a cookie test page. Here is a cookie test page.
<hr />
<br />
<form action="">
<input type="hidden" name="act" value="clear_cookie" />
<input type="submit" name="set_action" value="Just clean it" />
</form>
<hr /> <hr />
<br /> <br />
@ -26,24 +32,28 @@ Cookies at this browser:<br />
<script type="text/javascript"> <script type="text/javascript">
document.write(document.cookie.split(';').join(';<br />')) document.write(document.cookie.split(';').join(';<br />'))
</script> </script>
#(cookiesout)#
::
<br /> <br />
<hr /> <hr />
Cookies coming to server:<br />
#(/cookiesout)#
#{cookiesout}#
#[string]#
#{/cookiesout}#
#(cookiesin)# #(cookiesin)#
Cookies coming to server:
:: ::
<br />
<hr />
Cookies server sent:<br />
#(/cookiesin)# #(/cookiesin)#
#{cookiesin}# #{cookiesin}#
#[name]# = #[value]# #[name]# = #[value]#
#{/cookiesin}# #{/cookiesin}#
<br />
<hr />
#(cookiesout)#
Cookies server sent:
::
#(/cookiesout)#
#{cookiesout}#
#[name]# = #[value]#
#{/cookiesout}#
<br> <br>
<hr> <hr>

@ -82,21 +82,26 @@ public class CookieTest {
final serverObjects prop = new serverObjects(); final serverObjects prop = new serverObjects();
if(post.containsKey("act")&&post.get("act").equals("clear_cookie")) if(post.containsKey("act")&&post.get("act").equals("clear_cookie"))
{ {
httpHeader outgoingHeader=new httpHeader();
Iterator it = header.entrySet().iterator(); Iterator it = header.entrySet().iterator();
while(it.hasNext()) while(it.hasNext())
{ {
java.util.Map.Entry e = (Entry) it.next(); java.util.Map.Entry e = (Entry) it.next();
if(e.getKey().equals("Cookie")); if(e.getKey().equals("Cookie"))
prop.setCoockie( {
(String)e.getKey(), String coockie[]=e.getValue().toString().split(";");
(String)e.getValue(), for(int i=0;i<coockie.length;i++)
"expires=Thu, 01-Jan-99 00:00:01 GMT" {
); String nameValue[]=coockie[i].split("=");
outgoingHeader.setCoockie(nameValue[0].trim(),nameValue.length>1?(nameValue[1].trim()):"","Thu, 01-Jan-99 00:00:01 GMT");
}
}
}
}
prop.setOutgoingHeader(outgoingHeader);
prop.put("coockiesout",0); prop.put("coockiesout",0);
//header. //header.
@ -105,18 +110,29 @@ public class CookieTest {
{ {
String CoockieName = post.get("cookie_name").toString().trim(); String CoockieName = post.get("cookie_name").toString().trim();
String CoockieValue = post.get("cookie_value").toString().trim(); String CoockieValue = post.get("cookie_value").toString().trim();
prop.setCoockie(CoockieName,CoockieValue); httpHeader outgoingHeader=new httpHeader();
prop.put("cookiesout",1);
prop.put("cookiesout_0_name",CoockieName); outgoingHeader.setCoockie(CoockieName,CoockieValue);
prop.put("cookiesout_0_value",CoockieValue); prop.setOutgoingHeader(outgoingHeader);
prop.put("cookiesin",1);
prop.put("cookiesin_0_name",CoockieName);
prop.put("cookiesin_0_value",CoockieValue);
//header. //header.
} }
Iterator it = header.entrySet().iterator(); Iterator it = header.entrySet().iterator();
while(it.hasNext()) while(it.hasNext())
{ {
java.util.Map.Entry e = (Entry) it.next(); java.util.Map.Entry e = (Entry) it.next();
System.out.println(""+e.getKey()+" : "+e.getValue()); System.out.println(""+e.getKey()+" : "+e.getValue());
if(e.getKey().equals("Cookie"))
{
String a;
prop.put("cookiesout",1);
prop.put("cookiesout_0_string",e.getValue().toString().replaceAll(";",";<br />"));
}
} }
return prop; return prop;

@ -70,10 +70,12 @@ import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.Vector;
import de.anomic.server.serverCore; import de.anomic.server.serverCore;
import de.anomic.server.logging.serverLog; import de.anomic.server.logging.serverLog;
public final class httpHeader extends TreeMap implements Map { public final class httpHeader extends TreeMap implements Map {
@ -718,4 +720,141 @@ public final class httpHeader extends TreeMap implements Map {
String dstHostSocket = (String) header.get(httpHeader.HOST); String dstHostSocket = (String) header.get(httpHeader.HOST);
prop.setProperty(CONNECTION_PROP_HOST,(httpd.isThisHostName(dstHostSocket)?virtualHost:dstHostSocket)); prop.setProperty(CONNECTION_PROP_HOST,(httpd.isThisHostName(dstHostSocket)?virtualHost:dstHostSocket));
} }
/*
* Patch BEGIN:
* Name: Header Property Patch
* Date: Fri. 13.01.2006
* Description: Makes possible to send header properties such as coockies back to the client.
* Part 1 of 5
* Questions: sergej.z@list.ru
*/
/**
* Holds header properties
*/
//Since properties such as coockies can be multiple, we cannot use HashMap here. We have to use Vector.
private Vector coockies=new Vector();
/**
*
* Implementation of Map.Entry. Structure that hold two values - exactly what we need!
*/
class Entry implements Map.Entry
{
private Object Key;
private Object Value;
Entry(Object Key,String Value){this.Key=Key;this.Value=Value;}
public Object getKey() {return Key;}
public Object getValue() {return Value;}
public Object setValue(Object Value) {return(this.Value=Value);}
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
* @param path: Path the coockie belongs to. Default - "/". Can be <b>null</b>.
* @param domain: Domain this cookie belongs to. Default - domain name. Can be <b>null</b>.
* @param secure: If true coockie will be send only over safe connection such as https
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires, String path, String domain, boolean secure)
{
/*
* TODO:Here every value can be validated for correctness if needed
* For example semicolon should be not in any of the values
* However an exception in this case would be an overhead IMHO.
*/
String coockieString=name+"="+value+";";
if(expires!=null)
coockieString+=" expires="+expires+";";
if(path!=null)
coockieString+=" path="+path+";";
if(domain!=null)
coockieString+=" domain="+domain+";";
if(secure)
coockieString+=" secure;";
coockies.add(new Entry("Set-Cookie",coockieString));
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
* @param path: Path the coockie belongs to. Default - "/". Can be <b>null</b>.
* @param domain: Domain this cookie belongs to. Default - domain name. Can be <b>null</b>.
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not.
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires, String path, String domain)
{
setCoockie( name, value, expires, path, domain, false);
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
* @param path: Path the coockie belongs to. Default - "/". Can be <b>null</b>.
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not.
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires, String path)
{
setCoockie( name, value, expires, path, null, false);
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not.
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires)
{
setCoockie( name, value, expires, null, null, false);
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not. This coockie never expires
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value )
{
setCoockie( name, value, null, null, null, false);
}
/**
* Returns an iterator within all properties can be reached.
* Is used mainly by httpd.
* @return iterator to read all request properties.
*
* Example:
*
* Iterator it=serverObjects.getRequestProperties();
* while(it.hasNext())
* {
* java.util.Map.Entry e=(java.util.Map.Entry)it.next();
* String propertyName=e.getKey();
* String propertyValue=e.getValue();
* }
*/
public Iterator getCookies()
{
return coockies.iterator();
}
/*
* Patch END:
* Name: Header Property Patch
*/
} }

@ -1174,11 +1174,11 @@ public final class httpd implements serverHandler {
long contentLength, long contentLength,
Date moddate, Date moddate,
Date expires, Date expires,
serverObjects requestProperties, httpHeader headers,
String contentEnc, String contentEnc,
String transferEnc String transferEnc
) throws IOException { ) throws IOException {
sendRespondHeader(conProp,respond,httpVersion,httpStatusCode,httpStatusText,contentType,contentLength,moddate,expires,requestProperties,contentEnc,transferEnc,true); sendRespondHeader(conProp,respond,httpVersion,httpStatusCode,httpStatusText,contentType,contentLength,moddate,expires,headers,contentEnc,transferEnc,true);
} }
public static final void sendRespondHeader( public static final void sendRespondHeader(
@ -1191,13 +1191,13 @@ public final class httpd implements serverHandler {
long contentLength, long contentLength,
Date moddate, Date moddate,
Date expires, Date expires,
serverObjects requestProperties, httpHeader headers,
String contentEnc, String contentEnc,
String transferEnc, String transferEnc,
boolean nocache boolean nocache
) throws IOException { ) throws IOException {
httpHeader headers = new httpHeader(); if(headers==null)headers = new httpHeader();
Date now = new Date(System.currentTimeMillis()); Date now = new Date(System.currentTimeMillis());
headers.put(httpHeader.SERVER, "AnomicHTTPD (www.anomic.de)"); headers.put(httpHeader.SERVER, "AnomicHTTPD (www.anomic.de)");
@ -1216,7 +1216,7 @@ public final class httpd implements serverHandler {
if (contentEnc != null) headers.put(httpHeader.CONTENT_ENCODING, contentEnc); if (contentEnc != null) headers.put(httpHeader.CONTENT_ENCODING, contentEnc);
if (transferEnc != null) headers.put(httpHeader.TRANSFER_ENCODING, transferEnc); if (transferEnc != null) headers.put(httpHeader.TRANSFER_ENCODING, transferEnc);
sendRespondHeader(conProp, respond, httpVersion, httpStatusCode, httpStatusText, headers,requestProperties); sendRespondHeader(conProp, respond, httpVersion, httpStatusCode, httpStatusText, headers);
} }
public static final void sendRespondHeader( public static final void sendRespondHeader(
@ -1228,25 +1228,14 @@ public final class httpd implements serverHandler {
) throws IOException { ) throws IOException {
sendRespondHeader(conProp,respond,httpVersion,httpStatusCode,null,header); sendRespondHeader(conProp,respond,httpVersion,httpStatusCode,null,header);
} }
/*This we need because the interface has changed*/
public static final void sendRespondHeader(
Properties conProp,
OutputStream respond,
String httpVersion,
int httpStatusCode,
String httpStatusText,
httpHeader header
) throws IOException {
sendRespondHeader(conProp,respond,httpVersion,httpStatusCode,null,header,null);
}
public static final void sendRespondHeader( public static final void sendRespondHeader(
Properties conProp, Properties conProp,
OutputStream respond, OutputStream respond,
String httpVersion, String httpVersion,
int httpStatusCode, int httpStatusCode,
String httpStatusText, String httpStatusText,
httpHeader header, httpHeader header
serverObjects requestProperties
) throws IOException { ) throws IOException {
if (respond == null) throw new NullPointerException("The outputstream must not be null."); if (respond == null) throw new NullPointerException("The outputstream must not be null.");
@ -1294,9 +1283,13 @@ public final class httpd implements serverHandler {
.append(Integer.toString(httpStatusCode)).append(" ") .append(Integer.toString(httpStatusCode)).append(" ")
.append(httpStatusText).append("\r\n"); .append(httpStatusText).append("\r\n");
//read custom headers //read custom headers
/*
if (requestProperties != null) if (requestProperties != null)
{ {
Iterator it=requestProperties.getRequestProperties(); httpHeader outgoingHeader=requestProperties.getOutgoingHeader();
if (outgoingHeader!=null)
{*/
Iterator it=header.getCookies();
while(it.hasNext()) while(it.hasNext())
{ {
//Append user properties to the main String //Append user properties to the main String
@ -1304,7 +1297,10 @@ public final class httpd implements serverHandler {
java.util.Map.Entry e=(java.util.Map.Entry)it.next(); java.util.Map.Entry e=(java.util.Map.Entry)it.next();
headerStringBuffer.append(e.getKey()).append(": ").append(e.getValue()).append("\r\n"); headerStringBuffer.append(e.getKey()).append(": ").append(e.getValue()).append("\r\n");
} }
}
/*
}
}*/
// write header // write header
Iterator i = header.keySet().iterator(); Iterator i = header.keySet().iterator();

@ -671,7 +671,7 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http
} }
// write the array to the client // write the array to the client
httpd.sendRespondHeader(this.connectionProperties, out, httpVersion, 200, null, mimeType, result.length, targetDate, null, tp, (zipContent)?"gzip":null, null, nocache); httpd.sendRespondHeader(this.connectionProperties, out, httpVersion, 200, null, mimeType, result.length, targetDate, null, tp.getOutgoingHeader(), (zipContent)?"gzip":null, null, nocache);
if (! method.equals(httpHeader.METHOD_HEAD)) { if (! method.equals(httpHeader.METHOD_HEAD)) {
Thread.sleep(200); // this solved the message problem (!!) Thread.sleep(200); // this solved the message problem (!!)
serverFileUtils.write(result, out); serverFileUtils.write(result, out);

@ -69,9 +69,21 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.Vector; import java.util.Vector;
import de.anomic.http.httpHeader;
public final class serverObjects extends Hashtable implements Cloneable { public final class serverObjects extends Hashtable implements Cloneable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private httpHeader outgoingHeader;
public void setOutgoingHeader(httpHeader outgoingHeader)
{
this.outgoingHeader=outgoingHeader;
}
public httpHeader getOutgoingHeader()
{
return outgoingHeader;
}
public serverObjects() { public serverObjects() {
super(); super();
@ -211,158 +223,4 @@ public final class serverObjects extends Hashtable implements Cloneable {
return super.clone(); return super.clone();
} }
/*
* Patch BEGIN:
* Name: Header Property Patch
* Date: Fri. 13.01.2006
* Description: Makes possible to send header properties such as coockies back to the client.
* Part 1 of 5
* Questions: sergej.z@list.ru
*/
/**
* Holds header properties
*/
//Since properties such as coockies can be multiple, we cannot use HashMap here. We have to use Vector.
private Vector requestProperty=new Vector();
/**
*
* Implementation of Map.Entry. Structure that hold two values - exactly what we need!
*/
class Entry implements Map.Entry
{
private Object Key;
private Object Value;
Entry(Object Key,String Value){this.Key=Key;this.Value=Value;}
public Object getKey() {return Key;}
public Object getValue() {return Value;}
public Object setValue(Object Value) {return(this.Value=Value);}
}
/**
* Set a header property <b>name</b> with <b>value</b>
* @param name : name of the property. Ex. Location
* @param value : value of the property
*
* We can achieve redirection using property Location
* setRequestProperty("Location", "http://www.yacy.net");
* Coockies can be convinently defined with setCookie method
*/
public void setRequestProperty(String name, String value)
{
/*
* TODO: Maybe we should check here if the property name is in RFC2616
* And check for the validity of the value as well...
* */
requestProperty.add(new Entry(name,value));
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
* @param path: Path the coockie belongs to. Default - "/". Can be <b>null</b>.
* @param domain: Domain this cookie belongs to. Default - domain name. Can be <b>null</b>.
* @param secure: If true coockie will be send only over safe connection such as https
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires, String path, String domain, boolean secure)
{
/*
* TODO:Here every value can be validated for correctness if needed
* For example semicolon should be not in any of the values
* However an exception in this case would be an overhead IMHO.
*/
String coockieString=name+"="+value+";";
if(expires!=null)
coockieString+=" expires="+expires+";";
if(path!=null)
coockieString+=" path="+path+";";
if(domain!=null)
coockieString+=" domain="+domain+";";
if(secure)
coockieString+=" secure;";
requestProperty.add(new Entry("Set-Cookie",coockieString));
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
* @param path: Path the coockie belongs to. Default - "/". Can be <b>null</b>.
* @param domain: Domain this cookie belongs to. Default - domain name. Can be <b>null</b>.
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not.
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires, String path, String domain)
{
setCoockie( name, value, expires, path, domain, false);
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
* @param path: Path the coockie belongs to. Default - "/". Can be <b>null</b>.
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not.
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires, String path)
{
setCoockie( name, value, expires, path, null, false);
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
* @param expires: when should this coockie be autmatically deleted. If <b>null</b> - coockie will stay forever
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not.
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value, String expires)
{
setCoockie( name, value, expires, null, null, false);
}
/**
* Sets Cookie on the client machine.
*
* @param name: Coockie name
* @param value: Coockie value
*
* Note: this coockie will be sent over each connection independend if it is safe connection or not. This coockie never expires
* Further documentation at <a href="http://docs.sun.com/source/816-6408-10/cookies.htm">docs.sun.com</a>
*/
public void setCoockie(String name, String value )
{
setCoockie( name, value, null, null, null, false);
}
/**
* Returns an iterator within all properties can be reached.
* Is used mainly by httpd.
* @return iterator to read all request properties.
*
* Example:
*
* Iterator it=serverObjects.getRequestProperties();
* while(it.hasNext())
* {
* java.util.Map.Entry e=(java.util.Map.Entry)it.next();
* String propertyName=e.getKey();
* String propertyValue=e.getValue();
* }
*/
public Iterator getRequestProperties()
{
return requestProperty.iterator();
}
/*
* Patch END:
* Name: Header Property Patch
*/
} }
Loading…
Cancel
Save