From f13c8aa7ddc670af309d1a58f9455d4405279eec Mon Sep 17 00:00:00 2001 From: Michael Peter Christen Date: Thu, 12 Jun 2014 18:06:22 +0200 Subject: [PATCH] re-implementation of file push option in the context of POST http requests. The internal representation of post-arguments is String and therefore not appropriate for byte[] object as submitted by file pushes. Therefore all pushed files are encoded to base64 _after_ uploading with an http form (you do not need to do that encoding yourself) to hand-over the byte[] as string in the post argument. Servlets which read such files must decode the base64 data to get the original byte[] array. This is considered as a temporary solution for file uploads and a proper implementations would need to consider all attributes as handed over as Objects with either String or byte[] Object instances. This would be a major code change and is not done at this time here now. The feature was submitted to realize a feature as pushed with the next commit. --- .../http/servlets/YaCyDefaultServlet.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/source/net/yacy/http/servlets/YaCyDefaultServlet.java b/source/net/yacy/http/servlets/YaCyDefaultServlet.java index dd8098a5b..4d6067c27 100644 --- a/source/net/yacy/http/servlets/YaCyDefaultServlet.java +++ b/source/net/yacy/http/servlets/YaCyDefaultServlet.java @@ -39,6 +39,7 @@ import java.util.Iterator; import java.util.List; import java.util.concurrent.ConcurrentHashMap; import java.util.zip.GZIPInputStream; + import javax.servlet.ReadListener; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; @@ -49,8 +50,10 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; + import net.yacy.cora.date.GenericFormatter; import net.yacy.cora.document.analysis.Classification; +import net.yacy.cora.order.Base64Order; import net.yacy.cora.protocol.HeaderFramework; import net.yacy.cora.protocol.RequestHeader; import net.yacy.cora.util.ByteBuffer; @@ -73,18 +76,23 @@ import net.yacy.server.serverObjects; import net.yacy.server.serverSwitch; import net.yacy.server.servletProperties; import net.yacy.visualization.RasterPlotter; + import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileItemFactory; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; + import static org.eclipse.jetty.http.HttpHeader.CONTENT_RANGE; import static org.eclipse.jetty.http.HttpHeader.IF_MODIFIED_SINCE; import static org.eclipse.jetty.http.HttpHeader.IF_UNMODIFIED_SINCE; import static org.eclipse.jetty.http.HttpHeader.REQUEST_RANGE; import static org.eclipse.jetty.http.HttpMethod.HEAD; + import org.eclipse.jetty.http.MimeTypes; + import static org.eclipse.jetty.http.MimeTypes.Type.TEXT_HTML; import static org.eclipse.jetty.http.MimeTypes.Type.TEXT_HTML_UTF_8; + import org.eclipse.jetty.io.WriterOutputStream; import org.eclipse.jetty.server.InclusiveByteRange; import org.eclipse.jetty.util.MultiPartOutputStream; @@ -757,8 +765,7 @@ public class YaCyDefaultServlet extends HttpServlet { return m; } - protected void handleTemplate(String target, HttpServletRequest request, - HttpServletResponse response) throws IOException, ServletException { + protected void handleTemplate(String target, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { Switchboard sb = Switchboard.getSwitchboard(); String localeSelection = sb.getConfig("locale.language", "default"); @@ -771,6 +778,7 @@ public class YaCyDefaultServlet extends HttpServlet { Enumeration argNames = request.getParameterNames(); while (argNames.hasMoreElements()) { String argName = argNames.nextElement(); + // standard attributes are just pushed as string args.put(argName, request.getParameter(argName)); } //TODO: for SSI request, local parameters are added as attributes, put them back as parameter for the legacy request @@ -779,7 +787,7 @@ public class YaCyDefaultServlet extends HttpServlet { while (attNames.hasMoreElements()) { String argName = attNames.nextElement(); args.put(argName, request.getAttribute(argName).toString()); - } + } RequestHeader legacyRequestHeader = generateLegacyRequestHeader(request, target, targetExt); // add multipart-form fields to parameter if (ServletFileUpload.isMultipartContent(request)) { @@ -1002,6 +1010,17 @@ public class YaCyDefaultServlet extends HttpServlet { // use default encoding (given as header or ISO-8859-1) args.add(item.getFieldName(), item.getString()); } + } else { + try { + InputStream filecontent = item.getInputStream(); + byte[] bytes = FileUtils.read(filecontent); + filecontent.close(); + // the file is written in base64 encoded form to a string + String fieldName = item.getFieldName(); + args.put(fieldName, Base64Order.standardCoder.encode(bytes)); + } catch (IOException e) { + ConcurrentLog.logException(e); + } } } } catch (Exception ex) {