Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Problem when Content-Length miss

2005-07-22
2013-05-02
  • Stefano Lenzi
    Stefano Lenzi
    2005-07-22

    I found a trouble when an HTTP answer doesn't contain Content-Length. The problem is that the Content-Length is created when you create the object HTTPResponse

    So to fix the trouble I have changed:

    org.cybergarage.http.HTTPResponse(){
       setContentType(HTML.CONTENT_TYPE);
       setServer(HTTPServer.getName());
       //setContent("");
    }

    and I have changed even the method:
    org.cybergarage.http.HTTPPacket.set(InputStream, boolean)

    I have just pasted a small piece of the whole metho just to allow you to discover the changes.

    if (isChunkedRequest == true) {
       try {
          String chunkSizeLine = reader.readLine();
          contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length()-2));
       }catch (Exception e) {};
    }else{
    //Stefano Lenzi changes start
       if(getHeader(HTTP.CONTENT_LENGTH)!=null){
          contentLen = getContentLength();
       }else{
          StringBuffer sb = new StringBuffer();
          int ch;
          while((ch=reader.read())!=-1){
             sb.append((char)ch);
          }
          setContent(sb.toString());
          return true;
       }
    //Stefano Lenzi changes end
    }

     
    • Stefano Lenzi
      Stefano Lenzi
      2005-07-22

      Using my solution I have found that the control point send invalid HTTP request. In fact we it execute the method org.cybergarage.xml.Parser.parse(URL) the HTTP have Connection: Keep-Alive but the Content-Length miss.

      That behaviour, as far as I know, doesn't follow the standard.

       
    • Stefano Lenzi
      Stefano Lenzi
      2005-07-22

      I have changed
      org.cybergarage.xml.Parser.parse(URL)
      in this way:

      public Node parse(URL locationURL) throws ParserException
      {
          try {
               HttpURLConnection urlCon = (HttpURLConnection)locationURL.openConnection();            
              urlCon.setRequestMethod("GET");
              urlCon.setRequestProperty(HTTP.CONTENT_LENGTH,"0");
              InputStream urlIn = urlCon.getInputStream();

              Node rootElem = parse(urlIn);
             
              urlIn.close();
              urlCon.disconnect();

              return rootElem;
             
          } catch (Exception e) {
              throw new ParserException(e);
          }
      }

       
    • Stefano Lenzi
      Stefano Lenzi
      2005-07-22

      Even the org.cybergarage.upnp.event.SubscriptionRequest.post() seems to be non-standard after my change. Beacuse the request doesn't have Content-Length header, but have Connection: close, but do not close the socket after have sent the request.

      So I have changed the Constructor in this way:
      org.cybergarage.upnp.event.SubscriptionRequest(){
          setContentLength(0);
      }

      org.cybergarage.upnp.event.SubscriptionRequest(HTTPRequest httpReq){
          this();
          set(httpReq);
      }

      so I set the Content-Length to 0 by default.