File: http.cpp

package info (click to toggle)
kascade 1.0-beta10-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 964 kB
  • ctags: 815
  • sloc: cpp: 7,780; makefile: 39
file content (114 lines) | stat: -rw-r--r-- 2,244 bytes parent folder | download
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
#include <qapplication.h>
#include <qstatusbar.h>
#include <qtimer.h>
#include <qmessagebox.h>
#include <iostream>

#include "http.h"
#include "globals.h"
#include "browser.h"
#include "errors.h"
#include "preferences.h"

Http::Http() : QObject()
{
	QObject::connect( &socket, SIGNAL( connected() ), this, SLOT( connected() ) );
	QObject::connect( &socket, SIGNAL( connectionClosed() ), this, SLOT( closed() ) );
	QObject::connect( &socket, SIGNAL( readyRead() ), this, SLOT( readyRead() ) );

	QObject::connect( browser, SIGNAL( stopSignal() ), this, SLOT( abort() ) );
}

int Http::getFile( string address )
{
	browser->statusBar()->message( "Connecting.." );
	myapp->processEvents();

	host = address.substr( 7, address.find( "/", 7 )-7 );
	filename = address; 
	
#ifdef DEBUGOUTPUT
	cout << "getting " + address << endl;
#endif	

	QTimer::singleShot( config->timeoutValue(), this, SLOT( error() ) ); 

	socket.connectToHost( host.c_str(), 80 );
	
	socketClosed = FALSE;
	aborted = FALSE;

	sourcev = new vector<string>;
	
	while( !socketClosed )
		myapp->processEvents();

//	if( aborted )
//		cout << "grr aborted" << endl;

	if( sourcev->empty() || 
	    aborted )
		success = FALSE;	
	else
	{
		success = ( (*sourcev)[0].find( "200 OK" ) != -1 );
	
		do
			sourcev->erase( sourcev->begin(), sourcev->begin()+1 );
		while( (*sourcev)[0] != "" );
	}
	
	if( !success )
		delete sourcev;

	browser->statusBar()->message( "Ready.." );
	return success;
}

void Http::connected()
{
	//cout << "connected.." << endl;

	string s = "GET " + filename + " HTTP/1.0\r\n\r\n";
	socket.writeBlock( s.c_str(), s.length() );
}

void Http::readyRead()
{
	//cout << "incoming.." << endl;
	browser->statusBar()->message( "Reading.." );

	while( socket.canReadLine() )
	{
		string s = (const char *)( socket.readLine() );

		int i;
		while( ( i = s.find_first_of( "\n\r" ) ) != -1 )
			s.erase( i, 1 );

		sourcev->push_back( s );
		//cout << "$" + s << endl;
	}	
}

void Http::closed()
{
	//cout << "closed.." << endl;

	socketClosed = TRUE;
}	

void Http::error()
{
	socketClosed = TRUE;
}

void Http::abort()
{
//	cout << "kwad" << endl;

	QMessageBox::about( 0, "Aborted", "HTTP Transmission aborted by user" );

	aborted = TRUE;
	socketClosed = TRUE;
}