1 | // |
2 | // HTTPSessionFactory.cpp |
3 | // |
4 | // Library: Net |
5 | // Package: HTTPClient |
6 | // Module: HTTPSessionFactory |
7 | // |
8 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/Net/HTTPSessionFactory.h" |
16 | #include "Poco/Net/HTTPSessionInstantiator.h" |
17 | #include "Poco/Exception.h" |
18 | |
19 | |
20 | using Poco::SingletonHolder; |
21 | using Poco::FastMutex; |
22 | using Poco::NotFoundException; |
23 | using Poco::ExistsException; |
24 | |
25 | |
26 | namespace Poco { |
27 | namespace Net { |
28 | |
29 | |
30 | HTTPSessionFactory::HTTPSessionFactory(): |
31 | _proxyPort(0) |
32 | { |
33 | } |
34 | |
35 | |
36 | HTTPSessionFactory::HTTPSessionFactory(const std::string& proxyHost, Poco::UInt16 proxyPort): |
37 | _proxyHost(proxyHost), |
38 | _proxyPort(proxyPort) |
39 | { |
40 | } |
41 | |
42 | |
43 | HTTPSessionFactory::~HTTPSessionFactory() |
44 | { |
45 | for (Instantiators::iterator it = _instantiators.begin(); it != _instantiators.end(); ++it) |
46 | { |
47 | delete it->second.pIn; |
48 | } |
49 | } |
50 | |
51 | |
52 | void HTTPSessionFactory::registerProtocol(const std::string& protocol, HTTPSessionInstantiator* pSessionInstantiator) |
53 | { |
54 | poco_assert_dbg(pSessionInstantiator); |
55 | |
56 | FastMutex::ScopedLock lock(_mutex); |
57 | std::pair<Instantiators::iterator, bool> tmp = _instantiators.insert(make_pair(protocol, InstantiatorInfo(pSessionInstantiator))); |
58 | if (!tmp.second) |
59 | { |
60 | ++tmp.first->second.cnt; |
61 | delete pSessionInstantiator; |
62 | } |
63 | } |
64 | |
65 | |
66 | void HTTPSessionFactory::unregisterProtocol(const std::string& protocol) |
67 | { |
68 | FastMutex::ScopedLock lock(_mutex); |
69 | |
70 | Instantiators::iterator it = _instantiators.find(protocol); |
71 | if (it != _instantiators.end()) |
72 | { |
73 | if (it->second.cnt == 1) |
74 | { |
75 | delete it->second.pIn; |
76 | _instantiators.erase(it); |
77 | } |
78 | else --it->second.cnt; |
79 | } |
80 | else throw NotFoundException("No HTTPSessionInstantiator registered for" , protocol); |
81 | } |
82 | |
83 | |
84 | bool HTTPSessionFactory::supportsProtocol(const std::string& protocol) |
85 | { |
86 | FastMutex::ScopedLock lock(_mutex); |
87 | |
88 | Instantiators::iterator it = _instantiators.find(protocol); |
89 | return it != _instantiators.end(); |
90 | } |
91 | |
92 | |
93 | HTTPClientSession* HTTPSessionFactory::createClientSession(const Poco::URI& uri) |
94 | { |
95 | FastMutex::ScopedLock lock(_mutex); |
96 | |
97 | if (uri.isRelative()) throw Poco::UnknownURISchemeException("Relative URIs are not supported by HTTPSessionFactory." ); |
98 | |
99 | Instantiators::iterator it = _instantiators.find(uri.getScheme()); |
100 | if (it != _instantiators.end()) |
101 | { |
102 | it->second.pIn->setProxy(_proxyHost, _proxyPort); |
103 | it->second.pIn->setProxyCredentials(_proxyUsername, _proxyPassword); |
104 | return it->second.pIn->createClientSession(uri); |
105 | } |
106 | else throw Poco::UnknownURISchemeException(uri.getScheme()); |
107 | } |
108 | |
109 | |
110 | void HTTPSessionFactory::setProxy(const std::string& host, Poco::UInt16 port) |
111 | { |
112 | FastMutex::ScopedLock lock(_mutex); |
113 | |
114 | _proxyHost = host; |
115 | _proxyPort = port; |
116 | } |
117 | |
118 | |
119 | void HTTPSessionFactory::setProxyCredentials(const std::string& username, const std::string& password) |
120 | { |
121 | FastMutex::ScopedLock lock(_mutex); |
122 | |
123 | _proxyUsername = username; |
124 | _proxyPassword = password; |
125 | } |
126 | |
127 | |
128 | namespace |
129 | { |
130 | static SingletonHolder<HTTPSessionFactory> singleton; |
131 | } |
132 | |
133 | |
134 | HTTPSessionFactory& HTTPSessionFactory::defaultFactory() |
135 | { |
136 | return *singleton.get(); |
137 | } |
138 | |
139 | |
140 | HTTPSessionFactory::InstantiatorInfo::InstantiatorInfo(HTTPSessionInstantiator* pInst): pIn(pInst), cnt(1) |
141 | { |
142 | poco_check_ptr (pIn); |
143 | } |
144 | |
145 | |
146 | } } // namespace Poco::Net |
147 | |