title: Bad Chunk Header (Apache Http / Jasper / JSP)
body: <p>I'm trying to make a Jasper client whose flow works like this: They are two applications. One calls the other and is displayed by an iframe.</p>
  <ol>
  <li><p>The main application sends credentials to the iframe application to get a session and execute a report execution.</p>
  </li>
  <li><p>The iframe application makes a REST call to get a session, and then another to request a report run. The result is a link that redirects to a loading screen.</p>
  </li>
  </ol>
  <p><strong>Source</strong></p>
  <pre><code>/**
   * Request Execution &quot;Servlet&quot;
   * 
   * @param request
   * @param response
   * @return
   * @throws IOException
   * @throws ServletException 
   */
  @Override
  @POST
  @Path(&quot;/execution&quot;)
  @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  @Produces(MediaType.TEXT_HTML)
  public String requestExection(@FormParam(&quot;username&quot;) String username,
          @FormParam(&quot;pass&quot;) String pass,
          @FormParam(&quot;type&quot;) String fileType,
          @FormParam(&quot;fileUri&quot;) String reportUnit,
          @FormParam(&quot;params&quot;) String params,
          @Context HttpServletResponse response) throws IOException, ServletException {
  
      JasperAuthentication session = new JasperAuthentication();
      CookieStore cookieStore = new BasicCookieStore();
      HttpContext httpContext = new BasicHttpContext();
      httpContext.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore);
      
      List&lt;NameValuePair&gt; urlParameters = new ArrayList&lt;&gt;();
      urlParameters.add(new BasicNameValuePair(&quot;j_username&quot;,username));
      urlParameters.add(new BasicNameValuePair(&quot;j_password&quot;,pass));   
      
      String url = &quot;&quot;;
      
      try {           
          boolean valid = session.openSession(urlParameters,response,httpContext);
          
          if (valid) {
              ReportObject repObj;
              Configuration config = PropertiesUtil.getProperties();
              repObj = requestJasperReport(params, fileType, reportUnit, response, httpContext);
  
              if (repObj != null) {
                  String strcookie = ServletUtils.getAllCookies(httpContext, &quot;;&quot;);
                  System.out.println(&quot;Cookie String: &quot; + strcookie);
                  Cookie cookie = new Cookie(&quot;context&quot;,
                          URLEncoder.encode(strcookie, StandardCharsets.UTF_8.toString()));
                  cookie.setPath(&quot;/&quot;);
                  cookie.setHttpOnly(true);
                  response.addCookie(cookie);
                  url = config.getString(&quot;client.host.url&quot;) + &quot;/report-viewer/visualizer/processing?type=&quot; + fileType
                          + &quot;&amp;ticket=&quot; + repObj.requestId + &quot;&amp;resource=&quot; + repObj.exports.get(0).id + &quot;&amp;date=&quot;+getExpiryPropFromJasper(strcookie);
              }
          }
      } catch (ConfigurationException e) {
          e.printStackTrace();
      }
      
      return &quot;{\&quot;report_url\&quot;:\&quot;&quot;+url+&quot;\&quot;}&quot;;
  }
  
  /**
   * Method for Request a Jasper Report to Server
   * 
   * @param strParams
   * @param fileType
   * @param response
   * @param client
   * @param httpContext
   * @return
   * @throws ClientProtocolException
   * @throws IOException
   * @throws ConfigurationException 
   */
  private ReportObject requestJasperReport(String strParams,
          String fileType,
          String reportUnit,
          HttpServletResponse response,
          HttpContext httpContext) throws IOException, ConfigurationException {
      Configuration config = PropertiesUtil.getProperties();
      StringBuilder sb = new StringBuilder();
      sb.append(config.getString(&quot;server.host.url&quot;))
          .append(config.getString(&quot;services.report.execution&quot;));
      HttpPost post = new HttpPost(sb.toString());
      Gson mGson = new Gson();
  
      Map&lt;String, String&gt; params = new HashMap&lt;&gt;();
      
      if (strParams != null) {
          String[] parameters = strParams.split(&quot;,&quot;);
  
          for (int i = 0; i &lt; parameters.length; i++) {
              String[] aux = parameters[i].split(&quot;:&quot;);
              params.put(aux[0], aux[1]);
          }
      }
      
      post.setHeader(&quot;Content-Type&quot;, &quot;application/xml;&quot;);
  
      post.setEntity(new StringEntity(ServletUtils.makeXMLRequest(reportUnit, params, fileType)));
  
      int coderes;
  
      Header[] headers = null;
      
      String content = &quot;&quot;;
      
      ReportObject repRes = null;
  
      try (CloseableHttpClient client = HttpClients.createDefault();
              CloseableHttpResponse chres = client.execute(post,httpContext)) {
          StatusLine status = chres.getStatusLine();
          coderes = status.getStatusCode();
          headers = chres.getAllHeaders();
          content = EntityUtils.toString(chres.getEntity());
      }
  
      if (coderes == 200 &amp;&amp; content != null &amp;&amp; content.length() &gt; 1) {
  
          repRes = mGson.fromJson(content, ReportObject.class);
          
          for (int i = 0; i &lt; headers.length; i++) {
              System.out.println(headers[i].getName() + &quot;=&quot; + headers[i].getValue());
              response.addHeader(headers[i].getName(), headers[i].getValue());
          }
          
      }
      return repRes;
  }
  
  /**
   * To get Expiration Date of a Cookie from Jasper Server
   * 
   * @param jaspercookie
   * @return
   */
  private String getExpiryPropFromJasper(String jaspercookie) {
      String[] expirity = jaspercookie.split(&quot;expiry=&quot;);
      return expirity[1].substring(0,expirity[1].length() - 2);
  }
  </code></pre>
  <ol start="3">
  <li><p>When the main application recieves the URL, he displays the loading page. The loading screen works as follows ... wait 10 seconds, and then redirect to another servlet where it requests the execution status of the report. If the report is ready, it redirects to the screen where it is downloaded, if it is not ready, then it redirects to the loading screen. (I'm showing you this because I'm suspicious of this)</p>
  <pre><code> var cont = 10;
  
   var intervalId = setInterval(countdown,1000);
  
   function countdown() {
   var ticket = $('#ticket').val();
   var type = $('#type').val();
   var resource = $('#resource').val();
   var tries = $('#tries').val();
   tries++;
   var url = window.location.protocol + &quot;//&quot; + window.location.host + &quot;/report- 
   viewer/visualizer/waitingroom? 
   type=&quot;+type+&quot;&amp;ticket=&quot;+ticket+&quot;&amp;resource=&quot;+resource+&quot;&amp;tries=&quot;+tries;
  
   if (cont&lt;=0) {
       clearInterval(intervalId);
       location.href = url;
   }
   cont--;
   }
  </code></pre>
  </li>
  </ol>
  <p>Here begins the problems. I'm getting this error message in my console when I try to request the previous step:</p>
  <pre><code>org.apache.http.MalformedChunkCodingException: Bad Chunk Header: {&quot;url&quot;:&quot;http://localhost:8080/report-viewer/visualizer/processing? . . .&quot;} at org.apache.http.impl.io.ChunckedInputStream..getChunkSize...
  </code></pre>
  <p>But before I get this, I notice that I get to this part with the javascript. (By the way, receiving 401, which means that my session seems to be invalid, even though the information I save in the cookie is exactly the same as the session cookie thrown by the login webservice. Maybe I miss something to keep that seesion alive) and then if I try to execute the sequence of calls again, then I got the error message.</p>
  <p>I could read that by default, apache has the property of maxKeepAliveRequests = 100. I'm suspecting that my javascript could be triggering this, since probably inside the loop (between javascript - JSP) it's opening connections (this are only suspicions).</p>
  <p>I made all the calls without having two applications connected and they worked fine, I was able to download the report. The issue happens when I try to connect both apps ...</p>
  <p>What can I do here ?</p>