[mythtv] Qt based replacement for wget

Holger Buchfink avalanche at beyondmonkey.com
Wed Apr 20 00:02:25 UTC 2005


Attached is a blocking http class done in Qt that basically does what wget
does. It can do PHP session-ids and enforce delays between requests. Might
be useful for Myth in the future, so I'm going to leave it here with the
engine running and the doors wide open.

It's been a while since I made it - I have a short term memory and probably
won't be able to answer any questions about it. It should all be in the Qt
Docs.

Holger
-------------- next part --------------

#include "httpgogetter.h"



void Blocker::block()
{
    blocked = true;
    while(blocked)
    {
        app->processEvents();
        usleep(5000);
    }    
}




HttpGoGetter::HttpGoGetter(Blocker *blocker, int cooldowninms, int timeoutinms)
{
    this->blocker = blocker;
    this->timeoutinms = timeoutinms;
    this->cooldowninms = cooldowninms;

    timeouttimer = new QTimer( this );
    cooldowntimer = new QTimer( this );
    coolingdown = false;

    QObject::connect(&http, SIGNAL(done(bool)), 
                     this, SLOT(done(bool)) );    

    QObject::connect(&http, 
        SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)), 
                     this, 
        SLOT(responseHeaderReceived(const QHttpResponseHeader &))); 

    QObject::connect(cooldowntimer, SIGNAL(timeout()), 
                     this, SLOT(cooleddown()));    
    QObject::connect(timeouttimer, SIGNAL(timeout()), 
                     this, SLOT(timeout()));    

}
HttpGoGetter::~HttpGoGetter()
{
}

bool HttpGoGetter::goGetIt(const QString &url)
{
    //if (coolingdown)
    //    cout << "coolingdown \n";
    while(coolingdown)
    {
        usleep(50000);
        blocker->processEvents();
    }
    //cout << "requesting... \n";

    QUrl qurl(url);

    QHttpRequestHeader header( "GET", qurl.encodedPathAndQuery() );
    header.setValue( "Host", qurl.host() );
    
    if (cookie != "")
    { 
        header.setValue("Cookie", cookie);
        //cout << "using cookie: " << cookie << "\n";
    }
    http.readAll();
    http.setHost( qurl.host() );

    timeouttimer->start(timeoutinms);
    http.request( header );

    //cout << "header: " << header.toString() << "\n";

    blocker->block();

    return !error;
}

void HttpGoGetter::responseHeaderReceived(const QHttpResponseHeader & resp)
{
    //cout << "resonseheader: " << resp.toString() << "\n";

    QString sidkey = "set-cookie";

    if (resp.hasKey(sidkey))
    {
        QRegExp rx("PHPSESSID=(.+);");
        rx.setMinimal(true);
        rx.setCaseSensitive(false);
        if (rx.search(resp.value(sidkey)) >= 0)
        {
            cookie = "PHPSESSID=" + rx.cap(1);
            //cout << "found cookie:" << cookie << "\n";
        }
    }
}

void HttpGoGetter::done(bool error)
{
    timeouttimer->stop();
    this->error = error;
    //cout << "done: " << error << "\n";

    data = http.readAll();
    blocker->resume();

    cooldowntimer->start(cooldowninms);
    coolingdown = true;
}

void HttpGoGetter::timeout()
{
    cout << "http timeout \n";
    timeouttimer->stop();
    http.abort();
}

void HttpGoGetter::cooleddown()
{
    //cout << "cooleddown \n";
    coolingdown = false;
    cooldowntimer->stop();
}



//----------------------------------------------------------------------------



-------------- next part --------------

#ifndef httpgogetterH
#define httpgogetterH

#include <unistd.h>
#include <iostream>
using namespace std;

#include <qapplication.h>
#include <qstring.h>
#include <qhttp.h>
#include <qnetwork.h>
#include <qurloperator.h>
#include <qtimer.h>
#include <qregexp.h>

class Blocker : public QObject
{
    Q_OBJECT
  public:
    Blocker(QApplication *app) { this->app = app; }

    void block();
    void processEvents() { app->processEvents(); }

  public slots:    
    void resume() { blocked = false; }

  private:
    bool blocked;
    QApplication *app;
};




class HttpGoGetter : public QObject
{
      Q_OBJECT
  public:
      HttpGoGetter(Blocker *blocker, int cooldowninms = 2000, 
                   int timeoutinms = 30000);
      ~HttpGoGetter();

      bool goGetIt(const QString &url);
      char* getData() { return data.data(); }
      bool requestOk() { return !error; }
      void clearSessionID() { cookie = ""; }

  public slots:
      void responseHeaderReceived(const QHttpResponseHeader & resp);
      void done(bool error);
      void timeout();
      void cooleddown();

  signals:

  private:
      QHttp http;
      QByteArray data;
      Blocker *blocker;
      QString cookie;
      bool error;

      int timeoutinms;
      QTimer *timeouttimer;

      int cooldowninms;
      bool coolingdown;
      QTimer *cooldowntimer;
};


#endif


More information about the mythtv-dev mailing list