Results 1 to 11 of 11

Thread: qUncompress with QHttp gzipped response

  1. #1
    Join Date
    Nov 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default qUncompress with QHttp gzipped response

    Hello, I'm new to the forums and I joined because I'm having a problem with a Qt4 function, qUncompress. In the documentation it says that if the string has been compressed with zlib or an external application, you should prepend 4 bytes containing the expected lenght of the uncompressed string... How would I do that? I tried converting the .size() unsigned int value to a byte array (using some method I found on the internet) but it failed. Could anyone please help me?
    Thanks in advance.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qUncompress with QHttp gzipped response

    Some rough code would be:

    QByteArray ba;

    ba.append(size >> 24);
    ba.append(size >> 16);
    ba.append(size >> 8);
    ba.append(size);

    uncompressed.prepend(ba);

  3. #3
    Join Date
    Nov 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qUncompress with QHttp gzipped response

    Quote Originally Posted by fatjuicymole View Post
    Some rough code would be:

    QByteArray ba;

    ba.append(size >> 24);
    ba.append(size >> 16);
    ba.append(size >> 8);
    ba.append(size);

    uncompressed.prepend(ba);
    Tried that, it gives me the following error in the Application Output:
    Qt Code:
    1. qUncompress: Z_DATA_ERROR: Input data is corrupted
    To copy to clipboard, switch view to plain text mode 

    This is my code:
    Qt Code:
    1. QByteArray responseBytes = currentClient->readAll();
    2. QString responseHTML;
    3.  
    4. if (responseHeaders.hasKey("Content-Encoding") && responseHeaders.value("Content-Encoding") == "gzip")
    5. {
    6. QByteArray inflatedResponse;
    7.  
    8. unsigned int byteSize = responseBytes.size();
    9. QByteArray bytes;
    10.  
    11. bytes.append(byteSize >> 24);
    12. bytes.append(byteSize >> 16);
    13. bytes.append(byteSize >> 8);
    14. bytes.append(byteSize);
    15.  
    16. responseBytes.prepend(bytes);
    17.  
    18. inflatedResponse = qUncompress(responseBytes);
    19.  
    20. responseHTML = QString(inflatedResponse);
    21.  
    22. }
    23. else
    24. {
    25. responseHTML = QString(responseBytes);
    26. }
    To copy to clipboard, switch view to plain text mode 

    I have no idea why it does not work. I'm really desperate

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qUncompress with QHttp gzipped response

    You seem to be prepending the size of the compressed file (responseBytes.size()), rather than the expected uncompressed size. That seems meaningless to me - if quncompress wants that size, it can just call size() itself.

  5. #5
    Join Date
    Nov 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qUncompress with QHttp gzipped response

    Hmm, you're right. I forgot it's the expected size that it asks for. So now, my question is another: how do I get the expected size from a gzipped string?

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qUncompress with QHttp gzipped response

    Considering it's just compressed data, you can't. You have to get it from somewhere else. Whoever sent you the data must know the uncompressed size, and they should send it to you.

  7. #7
    Join Date
    Nov 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qUncompress with QHttp gzipped response

    Quote Originally Posted by fatjuicymole View Post
    Considering it's just compressed data, you can't. You have to get it from somewhere else. Whoever sent you the data must know the uncompressed size, and they should send it to you.
    Hmm 0.o
    I'm just trying to decompress a string that contains HTML code of a webpage, and it's the server which compresses it into a gzip stream... I mean, browsers do that as well, so there must be a way. Maybe I have to use native zlib functions...?

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qUncompress with QHttp gzipped response

    Did you check HTTP Response Headers? I'd be suprised if the decompressed size isn't stored in there.

    Another way around the problem is simply to tell the server you don't support compressed HTML.

  9. #9
    Join Date
    Nov 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qUncompress with QHttp gzipped response

    HTTP headers do not send any info on uncompressed size, sadly
    I have no idea how to solve this problem... I even tried using some zlib methods I found over the web, but I failed. Could anybody help me? I'd be willing to pay for help

  10. #10
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qUncompress with QHttp gzipped response

    Are you supporting gzip for a reason, or just because the server is sending data to you in that format?

    I'm thinking the easiest way to go is to set your "Accept-Encoding" header to exclude compress and gzip, then the server will not compress it in the first place.

  11. #11
    Join Date
    Nov 2009
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: qUncompress with QHttp gzipped response

    I'm supporting it because I need my application to be as quick as possible, actually. It's not really needed, but when it comes to downloading html webpages gzip compression can save a lot of bandwidth/time.
    And also, it's something I want to undestand for my personal knowledge. It's the first time I'm messing with HTTP compression without some easy module or class (it wasn't too hard in vb.net)

Similar Threads

  1. qUncompress and gzipped files
    By roxton in forum Qt Programming
    Replies: 3
    Last Post: 17th April 2011, 09:41
  2. QHttp Response content
    By ct in forum Qt Programming
    Replies: 9
    Last Post: 11th July 2010, 11:06
  3. QHTTP does not see tunneled TCP HTTP OK authentication response
    By SailingDreams in forum Qt Programming
    Replies: 6
    Last Post: 23rd May 2009, 09:39
  4. From QHttp to QHttp over SSL
    By Nyphel in forum Newbie
    Replies: 1
    Last Post: 3rd July 2007, 10:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.