reduce verbosity
[gnash.git] / libbase / StreamProvider.cpp
blobdd1442c3dc63d6f99a178a60dcb30b5ee56d1a75
1 // StreamProvider.cpp: ActionScript file: or http: stream reader, for Gnash.
2 //
3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software
4 // 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"
31 #include <cstdio>
32 #include <map>
33 #include <string>
34 #include <vector>
35 #include <boost/shared_ptr.hpp>
37 namespace gnash {
39 StreamProvider::StreamProvider(const URL& orig, const URL& base,
40 std::auto_ptr<NamingPolicy> np)
42 _namingPolicy(np),
43 _base(base),
44 _original(orig)
48 bool
49 StreamProvider::allow(const URL& url) const
51 return URLAccessManager::allow(url, _original);
54 std::auto_ptr<IOChannel>
55 StreamProvider::getStream(const URL& url, bool namedCacheFile) const
58 std::auto_ptr<IOChannel> stream;
60 if (url.protocol() == "file") {
62 std::string path = url.path();
63 if (path == "-") {
64 // TODO: only allow this as the *very first* call ?
65 // Rationale is a movie might request load of
66 // standard input, being a security issue.
67 // Note also that the FB gui will use stdin
68 // for key events.
70 FILE *newin = fdopen(dup(0), "rb");
72 // Close on destruction.
73 stream = makeFileChannel(newin, true);
74 return stream;
76 else {
77 // check security here !!
78 if (!allow(url)) return stream;
80 FILE *newin = std::fopen(path.c_str(), "rb");
81 if (!newin) {
82 return stream;
84 // Close on destruction
85 stream = makeFileChannel(newin, true);
86 return stream;
89 else {
90 if (allow(url)) {
91 stream = NetworkAdapter::makeStream(url.str(),
92 namedCacheFile ? namingPolicy()(url) : "");
95 // Will return 0 auto_ptr if not allowed.
96 return stream;
100 std::auto_ptr<IOChannel>
101 StreamProvider::getStream(const URL& url, const std::string& postdata,
102 const NetworkAdapter::RequestHeaders& headers, bool namedCacheFile)
103 const
106 if (url.protocol() == "file") {
107 if (!headers.empty()) {
108 log_error("Request Headers discarded while getting stream "
109 "from file: uri");
111 return getStream(url, postdata);
114 if (allow(url) ) {
115 return NetworkAdapter::makeStream(url.str(), postdata, headers,
116 namedCacheFile ? namingPolicy()(url) : "");
119 return std::auto_ptr<IOChannel>();
123 std::auto_ptr<IOChannel>
124 StreamProvider::getStream(const URL& url, const std::string& postdata,
125 bool namedCacheFile) const
128 std::auto_ptr<IOChannel> stream;
130 if (url.protocol() == "file") {
131 if (!postdata.empty()) {
132 log_error(_("POST data discarded while getting a stream "
133 "from file: uri"));
135 std::string path = url.path();
136 if (path == "-") {
137 FILE *newin = fdopen(dup(0), "rb");
138 stream = makeFileChannel(newin, false);
139 return stream;
141 else {
142 if (!allow(url)) return stream;
144 FILE *newin = std::fopen(path.c_str(), "rb");
145 if (!newin) {
146 return stream;
148 stream = makeFileChannel(newin, false);
149 return stream;
152 else {
153 if (allow(url)) {
154 stream = NetworkAdapter::makeStream(url.str(), postdata,
155 namedCacheFile ? namingPolicy()(url) : "");
157 // Will return 0 auto_ptr if not allowed.
158 return stream;
164 } // namespace gnash