aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/connection.cpp12
-rw-r--r--src/httpget.cpp80
2 files changed, 75 insertions, 17 deletions
diff --git a/src/connection.cpp b/src/connection.cpp
index 0dff918..cb3c724 100644
--- a/src/connection.cpp
+++ b/src/connection.cpp
@@ -161,6 +161,7 @@ bool Connection::open( const char *sAddr, int nPort )
161 161
162 /* Create the socket. */ 162 /* Create the socket. */
163 nSocket = socket( PF_INET, SOCK_STREAM, 0 ); 163 nSocket = socket( PF_INET, SOCK_STREAM, 0 );
164
164 if( nSocket < 0 ) 165 if( nSocket < 0 )
165 { 166 {
166 bActive = false; 167 bActive = false;
@@ -240,7 +241,7 @@ int Connection::readInput()
240 241
241bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack ) 242bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack )
242{ 243{
243 fd_set rfds; 244 fd_set rfds, efds;
244 struct timeval tv, start, end; 245 struct timeval tv, start, end;
245 struct timezone tz; 246 struct timezone tz;
246 int retval; 247 int retval;
@@ -249,6 +250,8 @@ bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack
249 250
250 FD_ZERO(&rfds); 251 FD_ZERO(&rfds);
251 FD_SET(nSocket, &rfds); 252 FD_SET(nSocket, &rfds);
253 FD_ZERO(&efds);
254 FD_SET(nSocket, &efds);
252 255
253 tv.tv_sec = nSec; 256 tv.tv_sec = nSec;
254 tv.tv_usec = nUSec; 257 tv.tv_usec = nUSec;
@@ -266,10 +269,11 @@ bool Connection::readInput( int nSec, int nUSec, int *pnSecBack, int *pnUSecBack
266 { 269 {
267 //printf("retval=%d, nSocket=%d,%d, sec=%d, usec=%d\n", retval, nSocket, FD_ISSET( nSocket, &rfds ), tv.tv_sec, tv.tv_usec ); 270 //printf("retval=%d, nSocket=%d,%d, sec=%d, usec=%d\n", retval, nSocket, FD_ISSET( nSocket, &rfds ), tv.tv_sec, tv.tv_usec );
268 // None of them have data, but the connection is still active. 271 // None of them have data, but the connection is still active.
269 if( readInput() == 0 ) 272 if( FD_ISSET( nSocket, &rfds ) )
270 { 273 {
271 this->close(); 274 if( readInput() == 0 )
272 throw ConnectionException( excodeConnectionClosed, "Connection closed"); 275 {
276 throw ConnectionException( excodeConnectionClosed, "Connection closed"); }
273 } 277 }
274 } 278 }
275 279
diff --git a/src/httpget.cpp b/src/httpget.cpp
index dd0585a..529d814 100644
--- a/src/httpget.cpp
+++ b/src/httpget.cpp
@@ -1,5 +1,5 @@
1#include "httpget.h" 1#include "httpget.h"
2#include "exceptionbase.h" 2#include "exceptions.h"
3#include "connection.h" 3#include "connection.h"
4#include <stdio.h> 4#include <stdio.h>
5 5
@@ -24,10 +24,10 @@ HttpGet::~HttpGet()
24void HttpGet::setURL( const std::string &url ) 24void HttpGet::setURL( const std::string &url )
25{ 25{
26 int len = url.size(); 26 int len = url.size();
27 printf("Full URL: %s\n", url.c_str() ); 27 //printf("Full URL: %s\n", url.c_str() );
28 int pos = url.find("://"); 28 int pos = url.find("://");
29 sProto.assign( url, 0, pos ); 29 sProto.assign( url, 0, pos );
30 printf("Protocol: %s\n", sProto.c_str() ); 30 //printf("Protocol: %s\n", sProto.c_str() );
31 31
32 int pos2 = url.find("/", pos+3 ); 32 int pos2 = url.find("/", pos+3 );
33 if( pos2 >= 0 ) 33 if( pos2 >= 0 )
@@ -45,14 +45,14 @@ void HttpGet::setURL( const std::string &url )
45 nPort = strtol( sHost.c_str()+pos3+1, NULL, 10 ); 45 nPort = strtol( sHost.c_str()+pos3+1, NULL, 10 );
46 sHost.erase( pos3 ); 46 sHost.erase( pos3 );
47 } 47 }
48 printf("Hostname: %s\n", sHost.c_str() ); 48 //printf("Hostname: %s\n", sHost.c_str() );
49 printf("Port: %d\n", nPort ); 49 //printf("Port: %d\n", nPort );
50 50
51 pos3 = url.find("?", pos2+1 ); 51 pos3 = url.find("?", pos2+1 );
52 if( pos3 >= 0 ) 52 if( pos3 >= 0 )
53 { 53 {
54 sPath.assign( url, pos2, pos3-pos2 ); 54 sPath.assign( url, pos2, pos3-pos2 );
55 printf("Path: %s\n", sPath.c_str() ); 55 //printf("Path: %s\n", sPath.c_str() );
56 for(;;) 56 for(;;)
57 { 57 {
58 int end = pos3+1; 58 int end = pos3+1;
@@ -70,17 +70,17 @@ void HttpGet::setURL( const std::string &url )
70 { 70 {
71 } 71 }
72 lParams.push_back( StringPair( sKey, sValue ) ); 72 lParams.push_back( StringPair( sKey, sValue ) );
73 printf("Param: %s = %s\n", sKey.c_str(), sValue.c_str() ); 73 //printf("Param: %s = %s\n", sKey.c_str(), sValue.c_str() );
74 if( end+1 >= len ) break; 74 if( end+1 >= len ) break;
75 } 75 }
76 } 76 }
77 else 77 else
78 { 78 {
79 sPath.assign( url, pos2, std::string::npos ); 79 sPath.assign( url, pos2, std::string::npos );
80 printf("Path: %s\n", sPath.c_str() ); 80 //printf("Path: %s\n", sPath.c_str() );
81 } 81 }
82 82
83 printf("\n"); 83 //printf("\n");
84} 84}
85 85
86void HttpGet::addParam( const std::string &key, const std::string &value ) 86void HttpGet::addParam( const std::string &key, const std::string &value )
@@ -153,18 +153,72 @@ SBuffer *HttpGet::get()
153 "Host: " + sHost + "\r\n" 153 "Host: " + sHost + "\r\n"
154 "Content-type: application/x-www-form-urlencoded\r\n\r\n"; 154 "Content-type: application/x-www-form-urlencoded\r\n\r\n";
155 155
156 printf("Connection content:\n\n%s\n\n", sData.c_str() ); 156 //printf("Connection content:\n\n%s\n\n", sData.c_str() );
157 157
158 Connection con; 158 Connection con;
159 con.open( sHost.c_str(), nPort ); 159 con.open( sHost.c_str(), nPort );
160 printf("Connected, sending request.\n"); 160 //printf("Connected, sending request.\n");
161 {
162 int nSocket = con.getSocket();
163 fd_set rfds, wfds, efds;
164 int retval;
165
166 FD_ZERO(&rfds);
167 FD_SET(nSocket, &rfds);
168 FD_ZERO(&wfds);
169 FD_SET(nSocket, &wfds);
170 FD_ZERO(&efds);
171 FD_SET(nSocket, &efds);
172
173 retval = select( nSocket+1, &rfds, &wfds, &efds, NULL );
174 printf("About to write: sock=%d, r=%d, w=%d, e=%d, ret=%d\n",
175 nSocket,
176 FD_ISSET( nSocket, &rfds ),
177 FD_ISSET( nSocket, &wfds ),
178 FD_ISSET( nSocket, &efds ),
179 retval
180 );
181
182 }
161 con.appendOutput( sData.c_str(), sData.size() ); 183 con.appendOutput( sData.c_str(), sData.size() );
162 con.writeOutput(); 184 con.writeOutput();
163 while( con.readInput() ) 185 int nSec = 5;
164 printf("Read %db\n", con.getInputAmnt() ); 186 int nUSec = 0;
187 int nLastAmnt = con.getInputAmnt();
188 try
189 {
190 double dTotTime = 0.0;
191 while( con.readInput( nSec, nUSec, &nSec, &nUSec ) )
192 {
193 if( nLastAmnt == con.getInputAmnt() )
194 {
195 if( nSec <= 0 && nUSec <= 0 )
196 throw ExceptionBase("Connection Timeout.\n");
197 if( nSec == 5 && nUSec == 0 )
198 break;
199 }
200 else
201 {
202 dTotTime += (5.0-(nSec+nUSec/1000000.0));
203 printf("\rRead %db at %.2fkb/sec",
204 con.getInputAmnt(),
205 ((double)(con.getInputAmnt())/1024.0) / dTotTime
206 );
207 fflush( stdout );
208 nSec = 5;
209 nUSec = 0;
210 nLastAmnt = con.getInputAmnt();
211 }
212 }
213 }
214 catch( ConnectionException &e )
215 {
216 printf("ConnectionException: %s\n", e.what() );
217 }
165 218
166 int total = con.getInputAmnt(); 219 int total = con.getInputAmnt();
167 const char *dat = con.getInput(); 220 const char *dat = con.getInput();
221 //printf("\n===> Final size %d\n", total );
168 for( int i = 0; i < total; i++ ) 222 for( int i = 0; i < total; i++ )
169 { 223 {
170 if( !memcmp( dat+i, "\r\n\r\n", 4 ) ) 224 if( !memcmp( dat+i, "\r\n\r\n", 4 ) )