Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
reading the post request body and sending it in a get attribute to servlets
Michael Peter Christen2f0629d42354
diff --git a/source/net/yacy/http/servlets/YaCyDefaultServlet.java b/source/net/yacy/http/servlets/YaCyDefaultServlet.javaindex 7c4b2204d..de9ecfcce 100644--- a/source/net/yacy/http/servlets/YaCyDefaultServlet.java+++ b/source/net/yacy/http/servlets/YaCyDefaultServlet.java@@ -34,10 +34,14 @@ import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.net.URL;import java.nio.charset.StandardCharsets;-import java.util.Enumeration;-import java.util.Iterator;-import java.util.List;-import java.util.Locale;+import java.util.Collections;+import java.util.Enumeration;+import java.util.HashMap;+import java.util.Iterator;+import java.util.LinkedHashSet;+import java.util.List;+import java.util.Locale;+import java.util.Map;import java.util.concurrent.ConcurrentHashMap;import javax.servlet.RequestDispatcher;@@ -46,8 +50,9 @@ import javax.servlet.ServletException;import javax.servlet.UnavailableException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;-import javax.servlet.http.HttpServletRequest;-import javax.servlet.http.HttpServletResponse;+import javax.servlet.http.HttpServletRequest;+import javax.servlet.http.HttpServletRequestWrapper;+import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileItemFactory;@@ -360,10 +365,107 @@ public class YaCyDefaultServlet extends HttpServlet {/* ------------------------------------------------------------ */@Override- protected void doPost(final HttpServletRequest request, final HttpServletResponse response)- throws ServletException, IOException {- this.doGet(request, response);+ protected void doPost(final HttpServletRequest request, final HttpServletResponse response)+ throws ServletException, IOException {+ HttpServletRequest requestForGet = request;+ if (this.shouldWrapBody(request)) {+ final String body = this.readRequestBody(request);+ if (body != null && !body.isEmpty()) {+ requestForGet = new BodyParameterRequestWrapper(request, body);+ }+ }+ this.doGet(requestForGet, response);+ }++ private boolean shouldWrapBody(final HttpServletRequest request) {+ final String method = request.getMethod();+ if (method == null || !HttpMethod.POST.asString().equalsIgnoreCase(method)) {+ return false;+ }+ final String contentType = request.getContentType();+ if (contentType == null) {+ return false;+ }+ final String normalizedType = contentType.toLowerCase(Locale.ROOT);+ if (normalizedType.contains("multipart/form-data")) {+ return false;+ }+ if (normalizedType.contains("application/x-www-form-urlencoded")) {+ return false;+ }+ return request.getContentLengthLong() != 0 || request.getHeader("Transfer-Encoding") != null;+ }++ private String readRequestBody(final HttpServletRequest request) {+ try (InputStream inputStream = request.getInputStream();+ ByteArrayOutputStream buffer = new ByteArrayOutputStream()) {+ final byte[] tmp = new byte[4096];+ int read;+ while ((read = inputStream.read(tmp)) != -1) {+ buffer.write(tmp, 0, read);+ }+ return buffer.toString(StandardCharsets.UTF_8.name());+ } catch (final IOException e) {+ ConcurrentLog.warn("FILEHANDLER", "Failed to read POST body: " + e.getMessage());+ return null;+ }}++ private static final class BodyParameterRequestWrapper extends HttpServletRequestWrapper {++ private static final String BODY_PARAMETER_NAME = "BODY";+ private final Map<String, String[]> additionalParameters = new HashMap<>();++ BodyParameterRequestWrapper(final HttpServletRequest request, final String bodyContent) {+ super(request);+ this.additionalParameters.put(BODY_PARAMETER_NAME, new String[]{bodyContent});+ }++ @Override+ public String getParameter(final String name) {+ final String[] values = this.getParameterValues(name);+ return values == null || values.length == 0 ? null : values[0];+ }++ @Override+ public String[] getParameterValues(final String name) {+ if (name == null) {+ return null;+ }+ final String[] override = this.additionalParameters.get(name);+ if (override != null) {+ return override.clone();+ }+ final String[] base = super.getParameterValues(name);+ return base == null ? null : base.clone();+ }++ @Override+ public Enumeration<String> getParameterNames() {+ final LinkedHashSet<String> names = new LinkedHashSet<>();+ final Enumeration<String> upstream = super.getParameterNames();+ while (upstream.hasMoreElements()) {+ names.add(upstream.nextElement());+ }+ names.addAll(this.additionalParameters.keySet());+ return Collections.enumeration(names);+ }++ @Override+ public Map<String, String[]> getParameterMap() {+ final Map<String, String[]> base = new HashMap<>();+ final Map<String, String[]> upstream = super.getParameterMap();+ if (upstream != null) {+ for (final Map.Entry<String, String[]> entry : upstream.entrySet()) {+ base.put(entry.getKey(), entry.getValue() == null ? null : entry.getValue().clone());+ }+ }+ for (final Map.Entry<String, String[]> entry : this.additionalParameters.entrySet()) {+ base.put(entry.getKey(), entry.getValue().clone());+ }+ return base;+ }+ }/* ------------------------------------------------------------ *//* (non-Javadoc)@@ -1220,10 +1322,10 @@ public class YaCyDefaultServlet extends HttpServlet {* @param request* @param args found fields/values are added to the map*/- protected void parseMultipart(final HttpServletRequest request, final serverObjects args) throws IOException {-- // reject too large uploads- //if (request.getContentLength() > SIZE_FILE_THRESHOLD) throw new IOException("FileUploadException: uploaded file too large = " + request.getContentLength());+ protected void parseMultipart(final HttpServletRequest request, final serverObjects args) throws IOException {++ // reject too large uploads+ //if (request.getContentLength() > SIZE_FILE_THRESHOLD) throw new IOException("FileUploadException: uploaded file too large = " + request.getContentLength());// check if we have enough memoryif (!MemoryControl.request(request.getContentLength() * 3, false)) {@@ -1263,8 +1365,9 @@ public class YaCyDefaultServlet extends HttpServlet {}}- } catch (final Exception ex) {- ConcurrentLog.info("FILEHANDLER", ex.getMessage());- }- }- }+ } catch (final Exception ex) {+ ConcurrentLog.info("FILEHANDLER", ex.getMessage());+ }+ }++}