aboutsummaryrefslogtreecommitdiff
path: root/src/old/httpget.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2009-02-11 05:29:41 +0000
committerMike Buland <eichlan@xagasoft.com>2009-02-11 05:29:41 +0000
commitf4b191f0ea396b58465bfba40749977780a3af58 (patch)
tree891490e91ab3b67524be67b2b71c85d84fd2f92a /src/old/httpget.cpp
parent292ae9453e7fdb2f1023ed9dfc99cbcd751f8b90 (diff)
downloadlibbu++-f4b191f0ea396b58465bfba40749977780a3af58.tar.gz
libbu++-f4b191f0ea396b58465bfba40749977780a3af58.tar.bz2
libbu++-f4b191f0ea396b58465bfba40749977780a3af58.tar.xz
libbu++-f4b191f0ea396b58465bfba40749977780a3af58.zip
Just removing some things that are cluttering up the source tree.
Diffstat (limited to 'src/old/httpget.cpp')
-rw-r--r--src/old/httpget.cpp263
1 files changed, 0 insertions, 263 deletions
diff --git a/src/old/httpget.cpp b/src/old/httpget.cpp
deleted file mode 100644
index ee1f29c..0000000
--- a/src/old/httpget.cpp
+++ /dev/null
@@ -1,263 +0,0 @@
1#include "httpget.h"
2#include "exceptions.h"
3#include "connection.h"
4#include <stdio.h>
5
6char HttpGet::hexcode[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
7
8HttpGet::HttpGet() :
9 nPort( 80 ),
10 sUserAgent("libbu++; HttpGet")
11{
12}
13
14HttpGet::HttpGet( const std::string &url ) :
15 nPort( 80 )
16{
17 setURL( url );
18}
19
20HttpGet::~HttpGet()
21{
22}
23
24void HttpGet::setURL( const std::string &url )
25{
26 int len = url.size();
27 //printf("Full URL: %s\n", url.c_str() );
28 int pos = url.find("://");
29 sProto.assign( url, 0, pos );
30 //printf("Protocol: %s\n", sProto.c_str() );
31
32 int pos2 = url.find("/", pos+3 );
33 if( pos2 >= 0 )
34 {
35 sHost.assign( url, pos+3, pos2-pos-3 );
36 }
37 else
38 {
39 sHost.assign( url, pos+3, std::string::npos );
40 }
41
42 int pos3 = sHost.find(":");
43 if( pos3 >= 0 )
44 {
45 nPort = strtol( sHost.c_str()+pos3+1, NULL, 10 );
46 sHost.erase( pos3 );
47 }
48 //printf("Hostname: %s\n", sHost.c_str() );
49 //printf("Port: %d\n", nPort );
50
51 pos3 = url.find("?", pos2+1 );
52 if( pos3 >= 0 )
53 {
54 sPath.assign( url, pos2, pos3-pos2 );
55 //printf("Path: %s\n", sPath.c_str() );
56 for(;;)
57 {
58 int end = pos3+1;
59 for(; url[end] != '=' && url[end] != '&' && end < len; end++ );
60 std::string sKey, sValue;
61 sKey.assign( url, pos3+1, end-pos3-1 );
62 if( url[end] == '=' )
63 {
64 pos3 = end;
65 for( end++; url[end] != '&' && end < len; end++ );
66 sValue.assign( url, pos3+1, end-pos3-1 );
67 pos3 = end;
68 }
69 else
70 {
71 }
72 lParams.push_back( StringPair( sKey, sValue ) );
73 //printf("Param: %s = %s\n", sKey.c_str(), sValue.c_str() );
74 if( end+1 >= len ) break;
75 }
76 }
77 else
78 {
79 sPath.assign( url, pos2, std::string::npos );
80 //printf("Path: %s\n", sPath.c_str() );
81 }
82
83 //printf("\n");
84}
85
86void HttpGet::addParam( const std::string &key, const std::string &value )
87{
88 lParams.push_back( StringPair( key, value ) );
89}
90
91std::string HttpGet::escape( const std::string &src )
92{
93 std::string escaped("");
94 for( std::string::const_iterator i = src.begin(); i != src.end(); i++ )
95 {
96 unsigned char j = *i;
97 if( (j >= '0' && j <= '9') ||
98 (j >= 'a' && j <= 'z') ||
99 (j >= 'A' && j <= 'Z') ||
100 j == '$' ||
101 j == '-' ||
102 j == '_' ||
103 j == '.' ||
104 j == '+' ||
105 j == '!' ||
106 j == '*' ||
107 j == '\'' ||
108 j == '(' ||
109 j == ')' )
110 {
111 escaped += j;
112 }
113 else
114 {
115 escaped += "%";
116 escaped += hexcode[j>>4];
117 escaped += hexcode[j&0x0F];
118 }
119 }
120
121 return escaped;
122}
123
124SBuffer *HttpGet::get()
125{
126 std::string sData;
127 sData = "GET " + sPath;
128 if( !lParams.empty() )
129 {
130 sData += "?";
131 for( std::list<StringPair>::iterator i = lParams.begin();
132 i != lParams.end(); i++ )
133 {
134 if( i != lParams.begin() )
135 sData += "&";
136
137 if( (*i).second == "" )
138 {
139 sData += escape( (*i).first );
140 }
141 else
142 {
143 sData += escape( (*i).first );
144 sData += "=";
145 sData += escape( (*i).second );
146 }
147 }
148 }
149
150 sData += " HTTP/1.1\r\n"
151 "User-Agent: " + sUserAgent + "\r\n"
152 "Connection: close\r\n"
153 "Host: " + sHost + "\r\n"
154 "Content-type: application/x-www-form-urlencoded\r\n\r\n";
155
156 //printf("Connection content:\n\n%s\n\n", sData.c_str() );
157
158 Connection con;
159 //printf("Opening connection...\n");
160 con.open( sHost.c_str(), nPort );
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 struct timeval tv;
174 tv.tv_sec = 4;
175 tv.tv_usec = 0;
176
177 //printf("Selecting on socket, can we read, write, etc?\n");
178 retval = select( nSocket+1, &rfds, &wfds, &efds, &tv );
179 /*printf("About to write: sock=%d, r=%d, w=%d, e=%d, ret=%d\n",
180 nSocket,
181 FD_ISSET( nSocket, &rfds ),
182 FD_ISSET( nSocket, &wfds ),
183 FD_ISSET( nSocket, &efds ),
184 retval
185 );*/
186
187 if( retval == 0 )
188 {
189 //printf("Timeout on connection.\n");
190 con.close();
191 throw ExceptionBase("Connection Timeout on open.\n");
192 }
193
194 }
195 con.appendOutput( sData.c_str(), sData.size() );
196 //printf("Writing to socket...\n");
197 con.writeOutput();
198 //printf("Data written...\n");
199 int nSec = 5;
200 int nUSec = 0;
201 int nLastAmnt = con.getInputAmnt();
202 try
203 {
204 double dTotTime = 0.0;
205 //printf("About to read input...\n");
206 while( con.readInput( nSec, nUSec, &nSec, &nUSec ) )
207 {
208 if( nLastAmnt == con.getInputAmnt() )
209 {
210 if( nSec <= 0 && nUSec <= 0 )
211 {
212 //printf("out of time, closing up.\n");
213 con.close();
214 throw ExceptionBase("Connection Timeout.\n");
215 }
216 if( nSec == 5 && nUSec == 0 )
217 {
218 //printf("No new data, breaking.\n");
219 break;
220 }
221 }
222 else
223 {
224 dTotTime += (5.0-(nSec+nUSec/1000000.0));
225 printf("\rRead %db at %.2fkb/sec",
226 con.getInputAmnt(),
227 ((double)(con.getInputAmnt())/1024.0) / dTotTime
228 );
229 fflush( stdout );
230 nSec = 5;
231 nUSec = 0;
232 nLastAmnt = con.getInputAmnt();
233 }
234 }
235 }
236 catch( ConnectionException &e )
237 {
238 //con.close();
239 if( strcmp( e.what(), "Connection closed" ) )
240 printf("\nConnectionException: %s\n", e.what() );
241 }
242
243 int total = con.getInputAmnt();
244 const char *dat = con.getInput();
245 //printf("\n===> Final size %d\n", total );
246 for( int i = 0; i < total; i++ )
247 {
248 if( !memcmp( dat+i, "\r\n\r\n", 4 ) )
249 {
250 SBuffer *buf = new SBuffer;
251 buf->write( dat+i+4, total-i-4 );
252 buf->setPos( 0 );
253 con.close();
254 return buf;
255 }
256 }
257 con.close();
258
259 //printf("\n\n%s\n\n", dat );
260
261 throw ExceptionBase("Something went wrong, incomplete response? fix this.\n");
262}
263