aboutsummaryrefslogtreecommitdiff
path: root/src/httpget.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-12-29 20:08:10 +0000
committerMike Buland <eichlan@xagasoft.com>2006-12-29 20:08:10 +0000
commit6e94731fd92d86e74f8c2fa9f679d0e2911fe9c3 (patch)
treefdac3418c7b72abaf8ac77deb9f83bc494e90def /src/httpget.cpp
parent9c0a471d6aa6822bff9470f0b942e0389d3986d2 (diff)
downloadlibbu++-6e94731fd92d86e74f8c2fa9f679d0e2911fe9c3.tar.gz
libbu++-6e94731fd92d86e74f8c2fa9f679d0e2911fe9c3.tar.bz2
libbu++-6e94731fd92d86e74f8c2fa9f679d0e2911fe9c3.tar.xz
libbu++-6e94731fd92d86e74f8c2fa9f679d0e2911fe9c3.zip
Part of the Connection class timed reads were reporting things improperly.
HttpGet is also much better now, and will timeout if it needs to.
Diffstat (limited to 'src/httpget.cpp')
-rw-r--r--src/httpget.cpp80
1 files changed, 67 insertions, 13 deletions
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 ) )