Results 1 to 3 of 3

Thread: QNetworkAccessManager with Accept-Encoding gzip

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QNetworkAccessManager with Accept-Encoding gzip

    I subclass QNetworkAccessManager (This class was introduced in Qt 4.4.) to load remote xml html or image to compose documents.

    the code is avaiable on:

    http://fop-miniscribus.googlecode.co...rk/FillCache.h
    http://fop-miniscribus.googlecode.co.../FillCache.cpp

    I start request on this way:

    Qt Code:
    1. void NetCacheSwap::start_Get( const QUrl url )
    2. {
    3. CacheInfo havingram = take_Url(url);
    4. if (havingram.pending) {
    5. /* request ist start */
    6. return;
    7. }
    8. if (havingram.valid) {
    9. /* request and data live on swap cache */
    10. emit incomming(url);
    11. return;
    12. }
    13. CacheInfo newrequest;
    14. QNetworkRequest need(url);
    15. need.setRawHeader( "User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); "
    16. "en-US; rv:1.9.0.1) Gecko/2008070206 Firefox/3.0.1" );
    17. need.setRawHeader( "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7" );
    18. need.setRawHeader( "Accept-Encoding", "gzip,deflate,qcompress" ); ////////gzip,deflate,qcompress
    19. need.setRawHeader( "Connection", "keep-alive" );
    20. if (cookie_host[url.host()].size() > 0) {
    21. qDebug() << "### > send cookie ->" << cookie_host[url.host()].toAscii();
    22. need.setRawHeader( "Cookie",cookie_host[url.host()].toAscii());
    23. }
    24. QNetworkReply *reps = get(need);
    25. /* mark it as pending request! */
    26. newrequest.pending = true;
    27. cache_ram.insert(fastmd5(url.toString()),newrequest);
    28. connect(reps, SIGNAL(downloadProgress (qint64,qint64)),
    29. SLOT(progress(qint64,qint64)));
    30. connect(reps, SIGNAL(error(QNetworkReply::NetworkError)),
    31. SLOT(url_error(QNetworkReply::NetworkError)));
    32. connect(reps, SIGNAL(finished()),SLOT(incomming_cache()));
    33. }
    To copy to clipboard, switch view to plain text mode 

    if incomming chunk is and header is content-encoding == gzip i open on this way:

    i decompress on file .....
    Qt Code:
    1. QByteArray NetCacheSwap::deflate( const QByteArray chunk )
    2. {
    3. QChar letter('A' + (qrand() % 26));
    4. /* must not go on file solution gunzip buffer ? go cache from net location */
    5. const QString tmpfiler = QString("%1/http_%2.gz").arg(location).arg(letter);
    6. QByteArray input;
    7. QFile file(tmpfiler);
    8. if ( file.open(QIODevice::WriteOnly) ) {
    9. file.write(chunk);
    10. file.close();
    11. gzFile filegunzip;
    12. filegunzip = gzopen(tmpfiler.toUtf8().data(),"rb");
    13. if(!filegunzip) {
    14. qDebug() << "### Unable to work on tmp file ... ";
    15. return QByteArray();
    16. }
    17. char buffer[1024];
    18. while(int readBytes =gzread(filegunzip, buffer, 1024))
    19. {
    20. input.append(QByteArray(buffer, readBytes));
    21. }
    22. gzclose(filegunzip);
    23. file.remove();
    24. }
    25. return input;
    26. }
    To copy to clipboard, switch view to plain text mode 

    How i can deflate / decompress QByteArray data direct on buffer? is this possibel?


    i search "content-encoding" on network qt lib i found only

    bool QHttpNetworkReplyPrivate::isGzipped()
    {
    QByteArray encoding = headerField("content-encoding");
    return encoding.toLower() == "gzip";
    }

    and isGzipped search is NULL on other or webkit .. ?

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager with Accept-Encoding gzip

    is this not possibel to deflate on buffer?

  3. #3
    Join Date
    Feb 2013
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QNetworkAccessManager with Accept-Encoding gzip

    Theres a solution here with code that decompresses gzip data using zlib:

    http://stackoverflow.com/questions/2...ress-gzip-data

    Qt Code:
    1. QByteArray gUncompress(const QByteArray &data)
    2. {
    3. if (data.size() <= 4) {
    4. qWarning("gUncompress: Input data is truncated");
    5. return QByteArray();
    6. }
    7.  
    8. QByteArray result;
    9.  
    10. int ret;
    11. z_stream strm;
    12. static const int CHUNK_SIZE = 1024;
    13. char out[CHUNK_SIZE];
    14.  
    15. /* allocate inflate state */
    16. strm.zalloc = Z_NULL;
    17. strm.zfree = Z_NULL;
    18. strm.opaque = Z_NULL;
    19. strm.avail_in = data.size();
    20. strm.next_in = (Bytef*)(data.data());
    21.  
    22. ret = inflateInit2(&strm, 15 + 32); // gzip decoding
    23. if (ret != Z_OK)
    24. return QByteArray();
    25.  
    26. // run inflate()
    27. do {
    28. strm.avail_out = CHUNK_SIZE;
    29. strm.next_out = (Bytef*)(out);
    30.  
    31. ret = inflate(&strm, Z_NO_FLUSH);
    32. Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered
    33.  
    34. switch (ret) {
    35. case Z_NEED_DICT:
    36. ret = Z_DATA_ERROR; // and fall through
    37. case Z_DATA_ERROR:
    38. case Z_MEM_ERROR:
    39. (void)inflateEnd(&strm);
    40. return QByteArray();
    41. }
    42.  
    43. result.append(out, CHUNK_SIZE - strm.avail_out);
    44. } while (strm.avail_out == 0);
    45.  
    46. // clean up and return
    47. inflateEnd(&strm);
    48. return result;
    49. }
    To copy to clipboard, switch view to plain text mode 

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.