1 // StreamProvider.cpp: ActionScript file: or http: stream reader, for Gnash.
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
4 // Free Software Foundation, Inc
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.
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"
25 #include "NetworkAdapter.h"
26 #include "URLAccessManager.h"
28 #include "rc.h" // for rcfile
29 #include "NamingPolicy.h"
30 #include "IOChannel.h"
33 #include <cstring> // for strerror
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
))
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();
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
77 log_error(_("Could not stdin (filename -): %2%"),
78 std::strerror(errno
));
81 FILE *newin
= fdopen(fd
, "rb");
83 // Close on destruction.
84 stream
= makeFileChannel(newin
, true);
88 // check security here !!
89 if (!allow(url
)) return stream
;
91 FILE *newin
= std::fopen(path
.c_str(), "rb");
93 log_error(_("Could not open file %1%: %2%"),
94 path
, std::strerror(errno
));
97 // Close on destruction
98 stream
= makeFileChannel(newin
, true);
104 stream
= NetworkAdapter::makeStream(url
.str(),
105 namedCacheFile
? namingPolicy()(url
) : "");
108 // Will return 0 unique_ptr if not allowed.
113 std::unique_ptr
<IOChannel
>
114 StreamProvider::getStream(const URL
& url
, const std::string
& postdata
,
115 const NetworkAdapter::RequestHeaders
& headers
, bool namedCacheFile
)
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
);
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 "
147 std::string path
= url
.path();
149 FILE *newin
= fdopen(dup(0), "rb");
150 stream
= makeFileChannel(newin
, false);
154 if (!allow(url
)) return stream
;
156 FILE *newin
= std::fopen(path
.c_str(), "rb");
158 log_error(_("Could not open file %1%: %2%"),
159 path
, std::strerror(errno
));
162 stream
= makeFileChannel(newin
, false);
168 stream
= NetworkAdapter::makeStream(url
.str(), postdata
,
169 namedCacheFile
? namingPolicy()(url
) : "");
171 // Will return 0 unique_ptr if not allowed.