URIStreamOpener.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // URIStreamOpener.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/URIStreamOpener.cpp#1 $
  5. //
  6. // Library: Foundation
  7. // Package: URI
  8. // Module: URIStreamOpener
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/URIStreamOpener.h"
  16. #include "Poco/URIStreamFactory.h"
  17. #include "Poco/FileStreamFactory.h"
  18. #include "Poco/URI.h"
  19. #include "Poco/Path.h"
  20. #include "Poco/SingletonHolder.h"
  21. #include "Poco/Exception.h"
  22. namespace Poco {
  23. URIStreamOpener::URIStreamOpener()
  24. {
  25. registerStreamFactory("file", new FileStreamFactory);
  26. }
  27. URIStreamOpener::~URIStreamOpener()
  28. {
  29. for (FactoryMap::iterator it = _map.begin(); it != _map.end(); ++it)
  30. delete it->second;
  31. }
  32. std::istream* URIStreamOpener::open(const URI& uri) const
  33. {
  34. FastMutex::ScopedLock lock(_mutex);
  35. std::string scheme;
  36. if (uri.isRelative())
  37. scheme = "file";
  38. else
  39. scheme = uri.getScheme();
  40. return openURI(scheme, uri);
  41. }
  42. std::istream* URIStreamOpener::open(const std::string& pathOrURI) const
  43. {
  44. FastMutex::ScopedLock lock(_mutex);
  45. try
  46. {
  47. URI uri(pathOrURI);
  48. std::string scheme(uri.getScheme());
  49. FactoryMap::const_iterator it = _map.find(scheme);
  50. if (it != _map.end())
  51. return openURI(scheme, uri);
  52. }
  53. catch (Exception&)
  54. {
  55. }
  56. Path path(pathOrURI, Path::PATH_GUESS);
  57. return openFile(path);
  58. }
  59. std::istream* URIStreamOpener::open(const std::string& basePathOrURI, const std::string& pathOrURI) const
  60. {
  61. FastMutex::ScopedLock lock(_mutex);
  62. try
  63. {
  64. URI uri(basePathOrURI);
  65. std::string scheme(uri.getScheme());
  66. FactoryMap::const_iterator it = _map.find(scheme);
  67. if (it != _map.end())
  68. {
  69. uri.resolve(pathOrURI);
  70. return openURI(scheme, uri);
  71. }
  72. }
  73. catch (Exception&)
  74. {
  75. }
  76. Path base(basePathOrURI, Path::PATH_GUESS);
  77. Path path(pathOrURI, Path::PATH_GUESS);
  78. base.resolve(path);
  79. return openFile(base);
  80. }
  81. void URIStreamOpener::registerStreamFactory(const std::string& scheme, URIStreamFactory* pFactory)
  82. {
  83. poco_check_ptr (pFactory);
  84. FastMutex::ScopedLock lock(_mutex);
  85. if (_map.find(scheme) == _map.end())
  86. {
  87. _map[scheme] = pFactory;
  88. }
  89. else throw ExistsException("An URIStreamFactory for the given scheme has already been registered", scheme);
  90. }
  91. void URIStreamOpener::unregisterStreamFactory(const std::string& scheme)
  92. {
  93. FastMutex::ScopedLock lock(_mutex);
  94. FactoryMap::iterator it = _map.find(scheme);
  95. if (it != _map.end())
  96. {
  97. URIStreamFactory* pFactory = it->second;
  98. _map.erase(it);
  99. delete pFactory;
  100. }
  101. else throw NotFoundException("No URIStreamFactory has been registered for the given scheme", scheme);
  102. }
  103. bool URIStreamOpener::supportsScheme(const std::string& scheme)
  104. {
  105. FastMutex::ScopedLock lock(_mutex);
  106. return _map.find(scheme) != _map.end();
  107. }
  108. namespace
  109. {
  110. static SingletonHolder<URIStreamOpener> sh;
  111. }
  112. URIStreamOpener& URIStreamOpener::defaultOpener()
  113. {
  114. return *sh.get();
  115. }
  116. std::istream* URIStreamOpener::openFile(const Path& path) const
  117. {
  118. FileStreamFactory factory;
  119. return factory.open(path);
  120. }
  121. std::istream* URIStreamOpener::openURI(const std::string& scheme, const URI& uri) const
  122. {
  123. std::string actualScheme(scheme);
  124. URI actualURI(uri);
  125. int redirects = 0;
  126. while (redirects < MAX_REDIRECTS)
  127. {
  128. try
  129. {
  130. FactoryMap::const_iterator it = _map.find(actualScheme);
  131. if (it != _map.end())
  132. return it->second->open(actualURI);
  133. else if (redirects > 0)
  134. throw UnknownURISchemeException(actualURI.toString() + std::string("; redirected from ") + uri.toString());
  135. else
  136. throw UnknownURISchemeException(actualURI.toString());
  137. }
  138. catch (URIRedirection& redir)
  139. {
  140. actualURI = redir.uri();
  141. actualScheme = actualURI.getScheme();
  142. ++redirects;
  143. }
  144. }
  145. throw IOException("Too many redirects while opening URI", uri.toString());
  146. }
  147. } // namespace Poco