1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/****************************************************************************
** $Id: qt/examples/networkprotocol/http.cpp 2.3.1 edited 2001-01-26 $
**
** Implementation of Network Extension Library
**
** Created : 970521
**
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
**
** This file is part of an example program for Qt. This example
** program may be used, distributed and modified without limitation.
**
*****************************************************************************/
#include "http.h"
#ifndef QT_NO_NETWORKPROTOCOL_HTTP
#include "qurlinfo.h"
#include <stdlib.h>
#include <qstringlist.h>
#include <qregexp.h>
Http::Http()
: QNetworkProtocol(), connectionReady( FALSE )
{
commandSocket = new QSocket( this );
connect( commandSocket, SIGNAL( hostFound() ),
this, SLOT( hostFound() ) );
connect( commandSocket, SIGNAL( connected() ),
this, SLOT( connected() ) );
connect( commandSocket, SIGNAL( connectionClosed() ),
this, SLOT( closed() ) );
connect( commandSocket, SIGNAL( readyRead() ),
this, SLOT( readyRead() ) );
}
Http::~Http()
{
close();
delete commandSocket;
}
void Http::operationPut( QNetworkOperation * )
{
QString cmd = "POST ";
cmd += url()->encodedPathAndQuery();
cmd += "\r\n";
commandSocket->writeBlock( cmd.latin1(), cmd.length() );
}
void Http::operationGet( QNetworkOperation * )
{
QString cmd = "GET ";
cmd += url()->encodedPathAndQuery();
cmd += "\r\n";
commandSocket->writeBlock( cmd.latin1(), cmd.length() );
}
bool Http::checkConnection( QNetworkOperation * )
{
if ( !commandSocket->peerName().isEmpty() && connectionReady )
return TRUE;
if ( !commandSocket->peerName().isEmpty() )
return FALSE;
if ( commandSocket->state() == QSocket::Connecting )
return FALSE;
connectionReady = FALSE;
commandSocket->connectToHost( url()->host(),
url()->port() != -1 ? url()->port() : 80 );
return FALSE;
}
void Http::close()
{
if ( !commandSocket->peerName().isEmpty() ) {
commandSocket->writeBlock( "quit\r\n", strlen( "quit\r\n" ) );
commandSocket->close();
}
}
int Http::supportedOperations() const
{
return OpGet | OpPut;
}
void Http::hostFound()
{
if ( url() )
emit connectionStateChanged( ConHostFound, tr( "Host %1 found" ).arg( url()->host() ) );
else
emit connectionStateChanged( ConHostFound, tr( "Host found" ) );
}
void Http::connected()
{
if ( url() )
emit connectionStateChanged( ConConnected, tr( "Connected to host %1" ).arg( url()->host() ) );
else
emit connectionStateChanged( ConConnected, tr( "Connected to host" ) );
connectionReady = TRUE;
}
void Http::closed()
{
if ( url() )
emit connectionStateChanged( ConClosed, tr( "Connection to %1 closed" ).arg( url()->host() ) );
else
emit connectionStateChanged( ConClosed, tr( "Connection closed" ) );
connectionReady = FALSE;
emit finished( operationInProgress() );
}
void Http::readyRead()
{
QByteArray s;
s.resize( commandSocket->bytesAvailable() );
commandSocket->readBlock( s.data(), commandSocket->bytesAvailable() );
emit data( s, operationInProgress() );
}
#endif // QT_NO_NETWORKPROTOCOL_HTTP
|