00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pandabase.h"
00020 #include "string_utils.h"
00021 #include "queuedConnectionManager.h"
00022 #include "queuedConnectionReader.h"
00023 #include "connectionWriter.h"
00024 #include "connection.h"
00025 #include "pointerTo.h"
00026 #include "datagram.h"
00027
00028 string proxy_server;
00029 int proxy_port;
00030 string data_server;
00031 string url;
00032
00033 class HTTPClient {
00034 public:
00035 HTTPClient();
00036 bool connect(const string &server, int port);
00037 bool send(const string &data);
00038 bool read(string &result);
00039 bool test_connection();
00040 void close();
00041 bool is_closed() const;
00042
00043 QueuedConnectionManager _manager;
00044 QueuedConnectionReader _reader;
00045 ConnectionWriter _writer;
00046 PT(Connection) _connection;
00047 };
00048
00049 HTTPClient::
00050 HTTPClient() :
00051 _reader(&_manager, 0),
00052 _writer(&_manager, 0)
00053 {
00054 _reader.set_raw_mode(true);
00055 _writer.set_raw_mode(true);
00056 }
00057
00058 bool HTTPClient::
00059 connect(const string &server, int port) {
00060 test_connection();
00061 nassertr(_connection.is_null(), false);
00062
00063 _connection = _manager.open_TCP_client_connection(server, port, 1000);
00064 if (_connection.is_null()) {
00065 return false;
00066 }
00067
00068 _reader.add_connection(_connection);
00069 return true;
00070 }
00071
00072 bool HTTPClient::
00073 send(const string &data) {
00074 test_connection();
00075 Datagram dg(data);
00076 return _writer.send(dg, _connection);
00077 }
00078
00079 bool HTTPClient::
00080 read(string &result) {
00081 test_connection();
00082 bool any_data = false;
00083
00084 Datagram dg;
00085 while (_reader.data_available()) {
00086 if (_reader.get_data(dg)) {
00087 result += dg.get_message();
00088 any_data = true;
00089 }
00090 }
00091
00092 return any_data;
00093 }
00094
00095 bool HTTPClient::
00096 test_connection() {
00097 bool okflag = true;
00098 while (_manager.reset_connection_available()) {
00099 PT(Connection) c;
00100 if (_manager.get_reset_connection(c)) {
00101 cerr << "lost connection: " << (void *)c << "\n";
00102 _manager.close_connection(_connection);
00103 _connection = NULL;
00104 okflag = false;
00105 }
00106 }
00107
00108 return okflag;
00109 }
00110
00111 void HTTPClient::
00112 close() {
00113 test_connection();
00114 if (!_connection.is_null()) {
00115 _manager.close_connection(_connection);
00116 _connection = NULL;
00117 }
00118 }
00119
00120 bool HTTPClient::
00121 is_closed() const {
00122 return (_connection.is_null());
00123 }
00124
00125 int
00126 main(int argc, char *argv[]) {
00127 bool okflag = false;
00128
00129 if (argc == 4 || argc == 5) {
00130 if (string_to_int(argv[2], proxy_port)) {
00131 proxy_server = argv[1];
00132 data_server = argv[3];
00133 if (argc >= 5) {
00134 url = argv[4];
00135 }
00136 okflag = true;
00137 }
00138 }
00139
00140 if (!okflag) {
00141 cerr << "test_proxy proxy_server proxy_port data_server [url]\n";
00142 exit(1);
00143 }
00144
00145 HTTPClient client;
00146 if (!client.connect(proxy_server, proxy_port)) {
00147 cerr << "Unable to connect to " << proxy_server << ":" << proxy_port << "\n";
00148 exit(1);
00149 }
00150
00151 ostringstream request_strm;
00152
00153 if (!url.empty()) {
00154
00155 request_strm
00156 << "CONNECT " << data_server << " HTTP/1.0\n"
00157 << "\n"
00158 << "GET " << url << " HTTP/1.0\n"
00159 << "\n";
00160
00161
00162
00163
00164
00165
00166 } else {
00167
00168 request_strm
00169 << "CONNECT " << data_server << " HTTP/1.0\n"
00170 << "\n";
00171 }
00172
00173 string request = request_strm.str();
00174 cerr << request;
00175
00176 if (!client.send(request)) {
00177 cerr << "Error transmitting to proxy server.\n";
00178 exit(1);
00179 }
00180
00181 cerr << "waiting.\n";
00182
00183 while (!client.is_closed()) {
00184 string result;
00185 while (client.read(result)) {
00186 cerr << result;
00187 result = "";
00188 }
00189 }
00190
00191 cerr << "terminating.\n";
00192 client.close();
00193
00194 return (0);
00195 }
00196