2005-11-29 jrandom

* Further Syndie UI cleanup
    * Bundled our patched MultiPartRequest code from jetty (APL2 licensed),
      since it hasn't been applied to the jetty CVS yet [1].  Its packaged
      into syndie.jar and renamed to net.i2p.syndie.web.MultiPartRequest, but
      will be removed as soon as its integrated into Jetty.  This patch allows
      posting content in various character sets.
      [1] http://article.gmane.org/gmane.comp.java.jetty.general/6031
    * Upgraded new installs to the latest stable jetty (5.1.6), though this
      isn't pushed as part of the update yet, as there aren't any critical
      bugs.
This commit is contained in:
jrandom
2005-11-29 16:58:01 +00:00
committed by zzz
parent c66e3256aa
commit d8071296eb
6 changed files with 475 additions and 42 deletions

View File

@ -3,27 +3,27 @@
<target name="all" depends="build" />
<target name="fetchJettylib" >
<available property="jetty.available" file="jetty-5.1.2.zip" />
<available property="jetty.available" file="jetty-5.1.6.zip" />
<ant target="doFetchJettylib" />
</target>
<target name="doFetchJettylib" unless="jetty.available" >
<echo message="The libraries contained within the fetched file are from Jetty's 5.1.2" />
<echo message="The libraries contained within the fetched file are from Jetty's 5.1.6" />
<echo message="distribution (http://jetty.mortbay.org/). These are not " />
<echo message="necessary for using I2P, but are used by some applications on top of I2P," />
<echo message="such as the routerconsole." />
<get src="http://mesh.dl.sourceforge.net/sourceforge/jetty/jetty-5.1.2.zip" verbose="true" dest="jetty-5.1.2.zip" />
<get src="http://mesh.dl.sourceforge.net/sourceforge/jetty/jetty-5.1.6.zip" verbose="true" dest="jetty-5.1.6.zip" />
<ant target="doExtract" />
</target>
<target name="doExtract">
<unzip src="jetty-5.1.2.zip" dest="." />
<unzip src="jetty-5.1.6.zip" dest="." />
<mkdir dir="jettylib" />
<copy todir="jettylib">
<fileset dir="jetty-5.1.2/lib">
<fileset dir="jetty-5.1.6/lib">
<include name="*.jar" />
</fileset>
</copy>
<copy todir="jettylib">
<fileset dir="jetty-5.1.2/ext">
<fileset dir="jetty-5.1.6/ext">
<include name="ant.jar" />
<include name="commons-el.jar" />
<include name="commons-logging.jar" />
@ -34,7 +34,7 @@
<include name="xercesImpl.jar" />
</fileset>
</copy>
<delete dir="jetty-5.1.2" />
<delete dir="jetty-5.1.6" />
</target>
<target name="build" depends="fetchJettylib" />
<target name="builddep" />

View File

@ -0,0 +1,424 @@
// see below for license info
package net.i2p.syndie.web;
import org.mortbay.servlet.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import javax.servlet.http.HttpServletRequest;
import org.mortbay.http.HttpFields;
import org.mortbay.util.LineInput;
import org.mortbay.util.MultiMap;
import org.mortbay.util.StringUtil;
/* ------------------------------------------------------------ */
/**
* Hacked version of Jetty's MultiPartRequest handler, applying a tiny patch for
* charset handling [1]. These changes are public domain, and will hopefully
* be integrated into Jetty so we can drop this file altogether. Of course,
* until then, this file is APL2 licensed.
*
* Original code is up at [2]
*
* [1] http://article.gmane.org/gmane.comp.java.jetty.general/6031
* [2] http://cvs.sourceforge.net/viewcvs.py/jetty/Jetty/src/org/mortbay/servlet/
* (rev 1.15)
*
*/
public class MultiPartRequest
{
/* ------------------------------------------------------------ */
HttpServletRequest _request;
LineInput _in;
String _boundary;
String _encoding;
byte[] _byteBoundary;
MultiMap _partMap = new MultiMap(10);
int _char=-2;
boolean _lastPart=false;
/* ------------------------------------------------------------ */
/** Constructor.
* @param request The request containing a multipart/form-data
* request
* @exception IOException IOException
*/
public MultiPartRequest(HttpServletRequest request)
throws IOException
{
_request=request;
String content_type = request.getHeader(HttpFields.__ContentType);
if (!content_type.startsWith("multipart/form-data"))
throw new IOException("Not multipart/form-data request");
//if(log.isDebugEnabled())log.debug("Multipart content type = "+content_type);
_encoding = request.getCharacterEncoding();
if (_encoding != null)
_in = new LineInput(request.getInputStream(), 2048, _encoding);
else
_in = new LineInput(request.getInputStream());
// Extract boundary string
_boundary="--"+
value(content_type.substring(content_type.indexOf("boundary=")));
//if(log.isDebugEnabled())log.debug("Boundary="+_boundary);
_byteBoundary= (_boundary+"--").getBytes(StringUtil.__ISO_8859_1);
loadAllParts();
}
/* ------------------------------------------------------------ */
/** Get the part names.
* @return an array of part names
*/
public String[] getPartNames()
{
Set s = _partMap.keySet();
return (String[]) s.toArray(new String[s.size()]);
}
/* ------------------------------------------------------------ */
/** Check if a named part is present
* @param name The part
* @return true if it was included
*/
public boolean contains(String name)
{
Part part = (Part)_partMap.get(name);
return (part!=null);
}
/* ------------------------------------------------------------ */
/** Get the data of a part as a string.
* @param name The part name
* @return The part data
*/
public String getString(String name)
{
List part = (List)_partMap.getValues(name);
if (part==null)
return null;
if (_encoding != null)
{
try
{
return new String(((Part)part.get(0))._data, _encoding);
}
catch (UnsupportedEncodingException uee)
{
//if (log.isDebugEnabled()) log.debug("Invalid character set: " + uee);
return null;
}
}
else
{
return new String(((Part)part.get(0))._data);
}
}
/* ------------------------------------------------------------ */
/**
* @param name The part name
* @return The parts data
*/
public String[] getStrings(String name)
{
List parts = (List)_partMap.getValues(name);
if (parts==null)
return null;
String[] strings = new String[parts.size()];
if (_encoding == null) {
for (int i=0; i<strings.length; i++) {
strings[i] = new String(((Part)parts.get(i))._data);
}
} else {
try
{
for (int i=0; i<strings.length; i++)
strings[i] = new String(((Part)parts.get(i))._data, _encoding);
}
catch (UnsupportedEncodingException uee)
{
//if (log.isDebugEnabled()) log.debug("Invalid character set: " + uee);
return null;
}
}
return strings;
}
/* ------------------------------------------------------------ */
/** Get the data of a part as a stream.
* @param name The part name
* @return Stream providing the part data
*/
public InputStream getInputStream(String name)
{
List part = (List)_partMap.getValues(name);
if (part==null)
return null;
return new ByteArrayInputStream(((Part)part.get(0))._data);
}
/* ------------------------------------------------------------ */
public InputStream[] getInputStreams(String name)
{
List parts = (List)_partMap.getValues(name);
if (parts==null)
return null;
InputStream[] streams = new InputStream[parts.size()];
for (int i=0; i<streams.length; i++) {
streams[i] = new ByteArrayInputStream(((Part)parts.get(i))._data);
}
return streams;
}
/* ------------------------------------------------------------ */
/** Get the MIME parameters associated with a part.
* @param name The part name
* @return Hashtable of parameters
*/
public Hashtable getParams(String name)
{
List part = (List)_partMap.getValues(name);
if (part==null)
return null;
return ((Part)part.get(0))._headers;
}
/* ------------------------------------------------------------ */
public Hashtable[] getMultipleParams(String name)
{
List parts = (List)_partMap.getValues(name);
if (parts==null)
return null;
Hashtable[] params = new Hashtable[parts.size()];
for (int i=0; i<params.length; i++) {
params[i] = ((Part)parts.get(i))._headers;
}
return params;
}
/* ------------------------------------------------------------ */
/** Get any file name associated with a part.
* @param name The part name
* @return The filename
*/
public String getFilename(String name)
{
List part = (List)_partMap.getValues(name);
if (part==null)
return null;
return ((Part)part.get(0))._filename;
}
/* ------------------------------------------------------------ */
public String[] getFilenames(String name)
{
List parts = (List)_partMap.getValues(name);
if (parts==null)
return null;
String[] filenames = new String[parts.size()];
for (int i=0; i<filenames.length; i++) {
filenames[i] = ((Part)parts.get(i))._filename;
}
return filenames;
}
/* ------------------------------------------------------------ */
private void loadAllParts()
throws IOException
{
// Get first boundary
String line = _in.readLine();
if (!line.equals(_boundary))
{
//log.warn(line);
throw new IOException("Missing initial multi part boundary");
}
// Read each part
while (!_lastPart)
{
// Read Part headers
Part part = new Part();
String content_disposition=null;
while ((line=_in.readLine())!=null)
{
// If blank line, end of part headers
if (line.length()==0)
break;
//if(log.isDebugEnabled())log.debug("LINE="+line);
// place part header key and value in map
int c = line.indexOf(':',0);
if (c>0)
{
String key = line.substring(0,c).trim().toLowerCase();
String value = line.substring(c+1,line.length()).trim();
String ev = (String) part._headers.get(key);
part._headers.put(key,(ev!=null)?(ev+';'+value):value);
//if(log.isDebugEnabled())log.debug(key+": "+value);
if (key.equals("content-disposition"))
content_disposition=value;
}
}
// Extract content-disposition
boolean form_data=false;
if (content_disposition==null)
{
throw new IOException("Missing content-disposition");
}
StringTokenizer tok =
new StringTokenizer(content_disposition,";");
while (tok.hasMoreTokens())
{
String t = tok.nextToken().trim();
String tl = t.toLowerCase();
if (t.startsWith("form-data"))
form_data=true;
else if (tl.startsWith("name="))
part._name=value(t);
else if (tl.startsWith("filename="))
part._filename=value(t);
}
// Check disposition
if (!form_data)
{
//log.warn("Non form-data part in multipart/form-data");
continue;
}
if (part._name==null || part._name.length()==0)
{
//log.warn("Part with no name in multipart/form-data");
continue;
}
//if(log.isDebugEnabled())log.debug("name="+part._name);
//if(log.isDebugEnabled())log.debug("filename="+part._filename);
_partMap.add(part._name,part);
part._data=readBytes();
}
}
/* ------------------------------------------------------------ */
private byte[] readBytes()
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c;
boolean cr=false;
boolean lf=false;
// loop for all lines`
while (true)
{
int b=0;
while ((c=(_char!=-2)?_char:_in.read())!=-1)
{
_char=-2;
// look for CR and/or LF
if (c==13 || c==10)
{
if (c==13) _char=_in.read();
break;
}
// look for boundary
if (b>=0 && b<_byteBoundary.length && c==_byteBoundary[b])
b++;
else
{
// this is not a boundary
if (cr) baos.write(13);
if (lf) baos.write(10);
cr=lf=false;
if (b>0)
baos.write(_byteBoundary,0,b);
b=-1;
baos.write(c);
}
}
// check partial boundary
if ((b>0 && b<_byteBoundary.length-2) ||
(b==_byteBoundary.length-1))
{
if (cr) baos.write(13);
if (lf) baos.write(10);
cr=lf=false;
baos.write(_byteBoundary,0,b);
b=-1;
}
// boundary match
if (b>0 || c==-1)
{
if (b==_byteBoundary.length)
_lastPart=true;
if (_char==10) _char=-2;
break;
}
// handle CR LF
if (cr) baos.write(13);
if (lf) baos.write(10);
cr=(c==13);
lf=(c==10 || _char==10);
if (_char==10) _char=-2;
}
//if(log.isTraceEnabled())log.trace(baos.toString());
return baos.toByteArray();
}
/* ------------------------------------------------------------ */
private String value(String nameEqualsValue)
{
String value =
nameEqualsValue.substring(nameEqualsValue.indexOf('=')+1).trim();
int i=value.indexOf(';');
if (i>0)
value=value.substring(0,i);
if (value.startsWith("\""))
{
value=value.substring(1,value.indexOf('"',1));
}
else
{
i=value.indexOf(' ');
if (i>0)
value=value.substring(0,i);
}
return value;
}
/* ------------------------------------------------------------ */
private class Part
{
String _name=null;
String _filename=null;
Hashtable _headers= new Hashtable(10);
byte[] _data=null;
}
};

View File

@ -8,7 +8,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.mortbay.servlet.MultiPartRequest;
// temporarily, we use our overwride, until jetty applies our patches
//import org.mortbay.servlet.MultiPartRequest;
import net.i2p.I2PAppContext;
import net.i2p.client.naming.*;

View File

@ -247,12 +247,34 @@ public class ViewThreadedServlet extends BaseServlet {
}
}
out.write(" @ ");
out.write(": ");
out.write("<a href=\"");
out.write(getViewPostLink(req, node, user, false));
out.write("\" title=\"View post\">");
EntryContainer entry = archive.getEntry(node.getEntry());
if (entry == null) throw new RuntimeException("Unable to fetch the entry " + node.getEntry());
HeaderReceiver rec = new HeaderReceiver();
parser.parse(entry.getEntry().getText(), rec);
String subject = rec.getHeader(HTMLRenderer.HEADER_SUBJECT);
if (subject == null)
subject = "(no subject)";
out.write(trim(subject, 40));
//out.write("</a>\n</td><td class=\"threadRight\">\n");
out.write("</a>");
out.write(" (<a href=\"");
out.write(getViewThreadLink(req, node, user));
out.write("\" title=\"View all posts in the thread\">full thread</a>)\n");
out.write("</span><span class=\"threadInfoRight\">");
out.write(" <a href=\"");
out.write(getViewPostLink(req, new BlogURI(node.getMostRecentPostAuthor(), node.getMostRecentPostDate()), user));
out.write("\" title=\"View the most recent post\">latest - ");
long dayBegin = BlogManager.instance().getDayBegin();
long postId = node.getEntry().getEntryId();
long postId = node.getMostRecentPostDate();
if (postId >= dayBegin) {
out.write("<b>today</b>");
} else if (postId >= dayBegin - 24*60*60*1000) {
@ -261,39 +283,13 @@ public class ViewThreadedServlet extends BaseServlet {
int daysAgo = (int)((dayBegin - postId + 24*60*60*1000-1)/(24*60*60*1000));
out.write(daysAgo + " days ago");
}
out.write(": ");
EntryContainer entry = archive.getEntry(node.getEntry());
if (entry == null) throw new RuntimeException("Unable to fetch the entry " + node.getEntry());
HeaderReceiver rec = new HeaderReceiver();
parser.parse(entry.getEntry().getText(), rec);
String subject = rec.getHeader(HTMLRenderer.HEADER_SUBJECT);
if (subject == null)
subject = "";
out.write(trim(subject, 40));
//out.write("</a>\n</td><td class=\"threadRight\">\n");
out.write("</a></span><span class=\"threadInfoRight\">");
if (childCount > 0) {
out.write(" <a href=\"");
out.write(getViewPostLink(req, new BlogURI(node.getMostRecentPostAuthor(), node.getMostRecentPostDate()), user));
out.write("\" title=\"View the most recent post\">latest - ");
postId = node.getMostRecentPostDate();
if (postId >= dayBegin) {
out.write("<b>today</b>");
} else if (postId >= dayBegin - 24*60*60*1000) {
out.write("<b>yesterday</b>");
} else {
int daysAgo = (int)((dayBegin - postId + 24*60*60*1000-1)/(24*60*60*1000));
out.write(daysAgo + " days ago");
}
out.write("</a>\n");
}
out.write("</a>\n");
/*
out.write(" <a href=\"");
out.write(getViewThreadLink(req, node, user));
out.write("\" title=\"View all posts in the thread\">full thread</a>\n");
*/
out.write("</span>");
out.write("</td></tr>\n");

View File

@ -1,4 +1,16 @@
$Id: history.txt,v 1.335 2005/11/28 11:02:41 jrandom Exp $
$Id: history.txt,v 1.336 2005/11/29 07:46:34 jrandom Exp $
2005-11-29 jrandom
* Further Syndie UI cleanup
* Bundled our patched MultiPartRequest code from jetty (APL2 licensed),
since it hasn't been applied to the jetty CVS yet [1]. Its packaged
into syndie.jar and renamed to net.i2p.syndie.web.MultiPartRequest, but
will be removed as soon as its integrated into Jetty. This patch allows
posting content in various character sets.
[1] http://article.gmane.org/gmane.comp.java.jetty.general/6031
* Upgraded new installs to the latest stable jetty (5.1.6), though this
isn't pushed as part of the update yet, as there aren't any critical
bugs.
2005-11-29 jrandom
* Added back in the OSX jbigi, which was accidentally removed a few revs

View File

@ -15,9 +15,9 @@ import net.i2p.CoreVersion;
*
*/
public class RouterVersion {
public final static String ID = "$Revision: 1.302 $ $Date: 2005/11/26 13:26:23 $";
public final static String ID = "$Revision: 1.303 $ $Date: 2005/11/28 11:02:40 $";
public final static String VERSION = "0.6.1.6";
public final static long BUILD = 1;
public final static long BUILD = 3;
public static void main(String args[]) {
System.out.println("I2P Router version: " + VERSION + "-" + BUILD);
System.out.println("Router ID: " + RouterVersion.ID);