Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-34453

QHttpNetworkReply::parseHeader could be stronger in somecase

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • Not Evaluated
    • None
    • 4.6.2, 4.7.1, 4.8.1
    • Network
    • None
    • qt4.6.2 on Windows 7 vs2008

    Description

      here is what happened when i get data from a stock-trade website using qt

      the header i'm looking forward is

      correct header looked forward
      ......
      SomeHeadName:SomeValue
      Content-Encoding:gzip
      ......
      

      the parsing result should be:

      SomeHead          =  SomeValue
      Content-Encoding  =  gzip
      

      but
      here is what i received from the careless website

      the header received in reality
      ......
      SomeHeadNameSomeValue      !!seperator ":" is missed here!!
      Content-Encoding:gzip
      ......
      

      and QHttpNetworkReply::parseHeader parses the header as

      ......
      SomeHeadSomeValueContent-Encoding  =  gzip
      ......
      

      because qt found no "Content-Encoding=gzip" in the parse result,

      so I got wrong data from QNetworkReply::readAll() because of the response not treated as a gzip file

      on the other side, this website can be parsed correctly by IE and Chrome

      here is what i did to make HttpNetworkReply::parseHeader work better

      void QHttpNetworkReplyPrivate::parseHeader(const QByteArray &header)
      {
          // see rfc2616, sec 4 for information about HTTP/1.1 headers.
          // allows relaxed parsing here, accepts both CRLF & LF line endings
          const QByteArrayMatcher lf("\n");
          const QByteArrayMatcher colon(":");
      +    const QByteArrayMatcher space(" ");	//added by tieliu 20130106
          int i = 0;
          while (i < header.count()) {
              int j = colon.indexIn(header, i); // field-name
      
      +       //added by tieliu 20130106
      +       //use space or \n if no : found in a line
      +       int jspace = space.indexIn(header, i);
      +       int jlf = lf.indexIn(header, i);
      +       if( jlf >= 0 && j > jlf )
      +       {
      +           if( jspace >= 0 && jspace < jlf )
      +               j = jspace;
      +           else
      +               j = jlf;
      +       }
      +       //end of added lines
      
              if (j == -1)
                  break;
              const QByteArray field = header.mid(i, j - i).trimmed();
      +       if( j < jlf )	//added by tieliu 20120106( add if(j < jlf) )
                 j++;
              // any number of LWS is allowed before and after the value
              QByteArray value;
              do {
                  i = lf.indexIn(header, j);
                  if (i == -1)
                      break;
                  if (!value.isEmpty())
                      value += ' ';
                  // check if we have CRLF or only LF
                  bool hasCR = (i && header[i-1] == '\r');
      -           int length = i -(hasCR ? 1: 0) - j;
      +           int length = i -( ( hasCR && i>j ) ? 1: 0) - j;	// ( && i>j ) by tieliu 20120116
                  value += header.mid(j, length).trimmed();
                  j = ++i;
              } while (i < header.count() && (header.at(i) == ' ' || header.at(i) == '\t'));
              if (i == -1)
                  break; // something is wrong
      
              fields.append(qMakePair(field, value));
          }
      }
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            shkearns Shane Kearns
            fengliutie Fengliutie
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes