httpsessionfactory.cpp
来自「很好用的网络封装库,不熟悉网络编程的人也可以使用。使用风格良好的标准c++编写。」· C++ 代码 · 共 154 行
CPP
154 行
//
// HTTPSessionFactory.cpp
//
// $Id: //poco/1.3/Net/src/HTTPSessionFactory.cpp#1 $
//
// Library: Net
// Package: HTTPClient
// Module: HTTPSessionFactory
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/Net/HTTPSessionFactory.h"
#include "Poco/Net/HTTPSessionInstantiator.h"
#include "Poco/Exception.h"
using Poco::SingletonHolder;
using Poco::FastMutex;
using Poco::NotFoundException;
using Poco::ExistsException;
namespace Poco {
namespace Net {
HTTPSessionFactory::HTTPSessionFactory():
_proxyPort(0)
{
}
HTTPSessionFactory::HTTPSessionFactory(const std::string& proxyHost, Poco::UInt16 proxyPort):
_proxyHost(proxyHost),
_proxyPort(proxyPort)
{
}
HTTPSessionFactory::~HTTPSessionFactory()
{
for (Instantiators::iterator it = _instantiators.begin(); it != _instantiators.end(); ++it)
{
delete it->second.pIn;
}
}
void HTTPSessionFactory::registerProtocol(const std::string& protocol, HTTPSessionInstantiator* pSessionInstantiator)
{
poco_assert_dbg(pSessionInstantiator);
FastMutex::ScopedLock lock(_mutex);
std::pair<Instantiators::iterator, bool> tmp = _instantiators.insert(make_pair(protocol, InstantiatorInfo(pSessionInstantiator)));
if (!tmp.second)
{
++tmp.first->second.cnt;
delete pSessionInstantiator;
}
}
void HTTPSessionFactory::unregisterProtocol(const std::string& protocol)
{
FastMutex::ScopedLock lock(_mutex);
Instantiators::iterator it = _instantiators.find(protocol);
if (it != _instantiators.end())
{
if (it->second.cnt == 1)
{
delete it->second.pIn;
_instantiators.erase(it);
}
else --it->second.cnt;
}
else throw NotFoundException("No HTTPSessionInstantiator registered for", protocol);
}
bool HTTPSessionFactory::supportsProtocol(const std::string& protocol)
{
FastMutex::ScopedLock lock(_mutex);
Instantiators::iterator it = _instantiators.find(protocol);
return it != _instantiators.end();
}
HTTPClientSession* HTTPSessionFactory::createClientSession(const Poco::URI& uri)
{
FastMutex::ScopedLock lock(_mutex);
if (uri.isRelative()) throw Poco::UnknownURISchemeException("Relative URIs are not supported by HTTPSessionFactory.");
Instantiators::iterator it = _instantiators.find(uri.getScheme());
if (it != _instantiators.end())
{
it->second.pIn->setProxy(_proxyHost, _proxyPort);
return it->second.pIn->createClientSession(uri);
}
else throw Poco::UnknownURISchemeException(uri.getScheme());
}
void HTTPSessionFactory::setProxy(const std::string& host, Poco::UInt16 port)
{
FastMutex::ScopedLock lock(_mutex);
_proxyHost = host;
_proxyPort = port;
}
HTTPSessionFactory& HTTPSessionFactory::defaultFactory()
{
static SingletonHolder<HTTPSessionFactory> singleton;
return *singleton.get();
}
HTTPSessionFactory::InstantiatorInfo::InstantiatorInfo(HTTPSessionInstantiator* pInst): pIn(pInst), cnt(1)
{
poco_check_ptr (pIn);
}
} } // namespace Poco::Net
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?