# HG changeset patch
# User Asier Lostalé <asier.lostale@openbravo.com>
# Date 1523963277 -7200
#      Tue Apr 17 13:07:57 2018 +0200
# Node ID 0f2501cd6e4e2ea0239103fddf94f2a61e4c5632
# Parent  ec0926402d321001afc536ab9d883f1f10e4129e
fixed 38390: request timeout is checked after reading the whole request content

  * Now request is read in chunks of 4096 characters (this size is the same as the
    one that was already being used in Tomcat's CoyoteReader.readLine) instead of
    line by line.
  * After each chunk time out is checked.
  * Additionally, prevent generating debugging potentially big String if it's not
    going to be used.

diff -r ec0926402d32 -r 0f2501cd6e4e src/org/openbravo/mobile/core/process/WebServiceAuthenticatedServlet.java
--- a/src/org/openbravo/mobile/core/process/WebServiceAuthenticatedServlet.java	Tue Apr 17 12:49:56 2018 +0200
+++ b/src/org/openbravo/mobile/core/process/WebServiceAuthenticatedServlet.java	Tue Apr 17 13:07:57 2018 +0200
@@ -1,6 +1,6 @@
 /*
  ************************************************************************************
- * Copyright (C) 2012-2014 Openbravo S.L.U.
+ * Copyright (C) 2012-2018 Openbravo S.L.U.
  * Licensed under the Openbravo Commercial License version 1.0
  * You may obtain a copy of the License at http://www.openbravo.com/legal/obcl.html
  * or in the legal folder of this module distribution.
@@ -42,6 +42,9 @@
   private static final Logger log = Logger.getLogger(WebServiceAuthenticatedServlet.class);
   private static final long serialVersionUID = 1L;
 
+  // Using same buffer as Tomcat's CoyoteReader.readLine
+  private static final int BUFFER_LENGTH = 4096;
+
   @Override
   public void service(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
@@ -157,21 +160,27 @@
     if (reader == null) {
       return "";
     }
-    String line;
-    final StringBuilder sb = new StringBuilder();
-    while ((line = reader.readLine()) != null) {
+
+    StringBuilder sb = new StringBuilder();
+    char[] buffer = new char[BUFFER_LENGTH];
+    int nRead = 0;
+    while (nRead != -1) {
       if (timeToTimeout != null && System.currentTimeMillis() > timeToTimeout) {
         throw new RequestTimeoutException("Timeout reached. Process: " + this.getClass()
             + ". Timeout: " + timeout);
       }
-      if (sb.length() > 0) {
-        sb.append("\n");
+
+      nRead = reader.read(buffer);
+      if (nRead > 0) {
+        sb.append(buffer, 0, nRead);
       }
-      sb.append(line);
     }
-    log.debug("REQUEST CONTENT>>>>");
-    log.debug(sb.toString());
-    return sb.toString();
+
+    String content = sb.toString();
+    if (log.isDebugEnabled()) {
+      log.debug("Request content: " + content);
+    }
+    return content;
   }
 
   protected Object getContentAsJSON(String content) throws JSONException {
