Update with current status
[gnash.git] / libbase / StreamProvider.cpp
blob15799c04c73e4f3ead8a0361a2d57eaee4cf606f
1 // StreamProvider.cpp: ActionScript file: or http: stream reader, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "GnashFileUtilities.h"
22 #include "StreamProvider.h"
23 #include "URL.h"
24 #include "tu_file.h"
25 #include "NetworkAdapter.h"
26 #include "URLAccessManager.h"
27 #include "log.h"
28 #include "rc.h" // for rcfile
29 #include "NamingPolicy.h"
30 #include "IOChannel.h"
32 #include <cerrno>
33 #include <cstring> // for strerror
34 #include <cstdio>
35 #include <map>
36 #include <string>
37 #include <vector>
39 namespace gnash {
41 StreamProvider::StreamProvider(URL orig, URL base,
42 std::unique_ptr<NamingPolicy> np)
44 _namingPolicy(std::move(np)),
45 _base(std::move(base)),
46 _original(std::move(orig))
50 bool
51 StreamProvider::allow(const URL& url) const
53 return URLAccessManager::allow(url, _original);
56 struct dummy : public IOChannel
60 std::unique_ptr<IOChannel>
61 StreamProvider::getStream(const URL& url, bool namedCacheFile) const
63 std::unique_ptr<IOChannel> stream;
65 if (url.protocol() == "file") {
67 std::string path = url.path();
68 if (path == "-") {
69 // TODO: only allow this as the *very first* call ?
70 // Rationale is a movie might request load of
71 // standard input, being a security issue.
72 // Note also that the FB gui will use stdin
73 // for key events.
75 int fd = dup(0);
76 if (0 > fd) {
77 log_error(_("Could not stdin (filename -): %2%"),
78 std::strerror(errno));
79 return nullptr;
81 FILE *newin = fdopen(fd, "rb");
83 // Close on destruction.
84 stream = makeFileChannel(newin, true);
85 return stream;
87 else {
88 // check security here !!
89 if (!allow(url)) return stream;
91 FILE *newin = std::fopen(path.c_str(), "rb");
92 if (!newin) {
93 log_error(_("Could not open file %1%: %2%"),
94 path, std::strerror(errno));
95 return stream;
97 // Close on destruction
98 stream = makeFileChannel(newin, true);
99 return stream;
102 else {
103 if (allow(url)) {
104 stream = NetworkAdapter::makeStream(url.str(),
105 namedCacheFile ? namingPolicy()(url) : "");
108 // Will return 0 unique_ptr if not allowed.
109 return stream;
113 std::unique_ptr<IOChannel>
114 StreamProvider::getStream(const URL& url, const std::string& postdata,
115 const NetworkAdapter::RequestHeaders& headers, bool namedCacheFile)
116 const
119 if (url.protocol() == "file") {
120 if (!headers.empty()) {
121 log_error(_("Request Headers discarded while getting stream from file: uri"));
123 return getStream(url, postdata);
126 if (allow(url) ) {
127 return NetworkAdapter::makeStream(url.str(), postdata, headers,
128 namedCacheFile ? namingPolicy()(url) : "");
131 return std::unique_ptr<IOChannel>();
135 std::unique_ptr<IOChannel>
136 StreamProvider::getStream(const URL& url, const std::string& postdata,
137 bool namedCacheFile) const
140 std::unique_ptr<IOChannel> stream;
142 if (url.protocol() == "file") {
143 if (!postdata.empty()) {
144 log_error(_("POST data discarded while getting a stream "
145 "from file: uri"));
147 std::string path = url.path();
148 if (path == "-") {
149 FILE *newin = fdopen(dup(0), "rb");
150 stream = makeFileChannel(newin, false);
151 return stream;
153 else {
154 if (!allow(url)) return stream;
156 FILE *newin = std::fopen(path.c_str(), "rb");
157 if (!newin) {
158 log_error(_("Could not open file %1%: %2%"),
159 path, std::strerror(errno));
160 return stream;
162 stream = makeFileChannel(newin, false);
163 return stream;
166 else {
167 if (allow(url)) {
168 stream = NetworkAdapter::makeStream(url.str(), postdata,
169 namedCacheFile ? namingPolicy()(url) : "");
171 // Will return 0 unique_ptr if not allowed.
172 return stream;
178 } // namespace gnash