From: <eg...@us...> - 2007-06-17 11:18:04
|
Revision: 562 http://svn.sourceforge.net/opengate/?rev=562&view=rev Author: egore Date: 2007-06-17 04:18:04 -0700 (Sun, 17 Jun 2007) Log Message: ----------- Try to add a login against metaserver and fail with C++ (enough crap written for today) Added Paths: ----------- branches/ogsector/src/metaserver.cpp branches/ogsector/src/metaserver.h Added: branches/ogsector/src/metaserver.cpp =================================================================== --- branches/ogsector/src/metaserver.cpp (rev 0) +++ branches/ogsector/src/metaserver.cpp 2007-06-17 11:18:04 UTC (rev 562) @@ -0,0 +1,121 @@ +/*************************************************************************** + * Copyright (C) 2006-2007 by OpenGate development team * + * eg...@us... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "metaserver.h" + +namespace OpenGate{ + +MetaConnection::MetaConnection( asio::io_service & io_service, const std::string & hostName ) + : io_service_( io_service ), socket_( io_service ), resolver_( io_service ), + hostname_( hostName ) { + + log_ = LogManager::getSingletonPtr(); + + try { + asio::ip::tcp::resolver::query query(hostName.c_str(), "http"); + asio::ip::tcp::resolver::iterator endpoint_iterator = resolver_.resolve(query); + asio::ip::tcp::resolver::iterator end; + + // Try each endpoint until we successfully establish a connection. + asio::error error = asio::error::host_not_found; + while (error && endpoint_iterator != end) + { + socket_.close(); + socket_.connect(*endpoint_iterator++, asio::assign_error(error)); + } + if (error) + throw error; + + } catch ( asio::error & e) { + log_->fatal( e.what() ); + } catch (...) { + log_->fatal( "Unkown exception occured while resolving the metaserver" ); + } + +} + +#define SCRIPT_PATH "/script/schnittstelle/" + +void MetaConnection::login( const std::string & userName, const std::string & passwd ) { + try { + + // Form the request. We specify the "Connection: close" header so that the + // server will close the socket after transmitting the response. This will + // allow us to treat all data up until the EOF as the content. + asio::streambuf request; + std::ostream request_stream(&request); + request_stream << "GET " SCRIPT_PATH << "?action=login&username=" <<userName <<"&password=" << passwd << " HTTP/1.0\r\n"; + request_stream << "Host: " << hostname_ << "\r\n"; + request_stream << "Accept: */*\r\n"; + request_stream << "Connection: close\r\n\r\n"; + + // Send the request. + asio::write(socket, request); + + // Read the response status line. + asio::streambuf response; + asio::read_until(socket, response, boost::regex("\r\n")); + + // Check that response is OK. + std::istream response_stream(&response); + std::string http_version; + response_stream >> http_version; + unsigned int status_code; + response_stream >> status_code; + std::string status_message; + std::getline(response_stream, status_message); + if (!response_stream || http_version.substr(0, 5) != "HTTP/"){ + log_->fatal( "Invalid response from metaserver " ); + return; + } + if (status_code != 200){ + log_->fatal( std::string("Response returned with status code ") /* + status_code FIXME: I suck at CPP streams */ ); + return; + } + + // Read the response headers, which are terminated by a blank line. + asio::read_until(socket, response, boost::regex("\r\n\r\n")); + + // Process the response headers. + std::string header; + while (std::getline(response_stream, header) && header != "\r") + std::cout << header << "\n"; + std::cout << "\n"; + + // Write whatever content we already have to output. + if (response.size() > 0) + std::cout << &response; + + // Read until EOF, writing data to output as we go. + asio::error error = asio::error::eof; + while (asio::read(socket, response, + asio::transfer_at_least(1), + asio::assign_error(error))) + std::cout << &response; + if (error != asio::error::eof) + throw error; + } catch ( asio::error & e) { + log_->fatal( e.what() ); + } catch (...) { + log_->fatal( "Unkown exception occured while resolving the metaserver" ); + } +} + +} // namespace OpenGate Property changes on: branches/ogsector/src/metaserver.cpp ___________________________________________________________________ Name: svn:mime-type + text/x-c++src Name: svn:eol-style + native Added: branches/ogsector/src/metaserver.h =================================================================== --- branches/ogsector/src/metaserver.h (rev 0) +++ branches/ogsector/src/metaserver.h 2007-06-17 11:18:04 UTC (rev 562) @@ -0,0 +1,51 @@ +/*************************************************************************** + * Copyright (C) 2006-2007 by OpenGate development team * + * eg...@us... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _OPENGATE_METASERVER__H +#define _OPENGATE_METASERVER__H + +#include <asio.hpp> +#include <boost/bind.hpp> + +#include "LogManager.h" + +namespace OpenGate{ + +class MetaConnection { +public: + MetaConnection( asio::io_service & io_service, const std::string & hostName ); + + ~MetaConnection(); + + void login( const std::string & userName, const std::string & passwd ); +private: + + asio::io_service & io_service_; + asio::ip::tcp::socket socket_; + asio::ip::tcp::resolver resolver_; + std::string hostname_; + + LogManager *log_; + +}; + +} + +#endif //_OPENGATE_METASERVER__H Property changes on: branches/ogsector/src/metaserver.h ___________________________________________________________________ Name: svn:mime-type + text/x-c++hdr Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |