Debian Codegraph @

src/DownloadManager.cpp

raw
1 // Implements dm_plugin_load and base DMPlugin methods
2
3 // For all support, instructions and copyright go to:
4 // http://e2guardian.org/
5 // Released under the GPL v2, with the OpenSSL exception described in the README file.
6
7 // INCLUDES
8
9 #ifdef HAVE_CONFIG_H
10 #include "e2config.h"
11 #endif
12 #include "DownloadManager.hpp"
13 #include "ConfigVar.hpp"
14 #include "OptionContainer.hpp"
15 #include "ConnectionHandler.hpp"
16 #include "RegExp.hpp"
17 #include "Logger.hpp"
18
19 #include <iostream>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24
25 // GLOBALS
26
27 extern OptionContainer o;
28
29 extern dmcreate_t defaultdmcreate;
30
31 // find the class factory functions for any DM plugins we've been configured to build
32
33 extern dmcreate_t fancydmcreate;
34
35 extern dmcreate_t trickledmcreate;
36
37 // IMPLEMENTATION
38
39 //
40 // DMPlugin
41 //
42
43 // constructor
44 DMPlugin::DMPlugin(ConfigVar &definition)
45 : alwaysmatchua(false), cv(definition)
46 {
47 }
48
49 // default initialisation procedure
50 int DMPlugin::init(void *args)
51 {
52 return 0;
53 }
54
55 // default method for sending the client a download link
56 bool DMPlugin::sendLink(Socket &peersock, String &linkurl, String &prettyurl)
57 {
58 // 1220 "<p>Scan complete.</p><p>Click here to download: "
59 String message(o.language_list.getTranslation(1220));
60 message += "<a href=\"" + linkurl + "\">" + prettyurl + "</a></p></body></html>\n";
61 return peersock.writeString(message.toCharArray());
62 }
63 #ifdef NOTDEF
64 // default method for deciding whether we will handle a request
65 bool DMPlugin::willHandle(HTTPHeader *requestheader, HTTPHeader *docheader)
66 {
67 // match user agent first (quick)
68 RegResult Rre;
69 if (!(alwaysmatchua || ua_match.match(requestheader->userAgent().toCharArray(),Rre)))
70 return false;
71
72 // then check standard lists (mimetypes & extensions)
73
74 // mimetypes
75 String mimetype("");
76 bool matchedmime = false;
77 if (mimelistenabled) {
78 mimetype = docheader->getContentType();
79 DEBUG_dwload("mimetype: ", mimetype);
80 String lc;
81 if (mimetypelist.findInList(mimetype.toCharArray(), lc) == NULL) {
82 if (!extensionlistenabled)
83 return false;
84 } else
85 matchedmime = true;
86 } else {
87 DEBUG_dwload("NO mimelistenabled!");
88 }
89
90
91 if (extensionlistenabled && !matchedmime) {
92 // determine the extension
93 String path(requestheader->decode(requestheader->getUrl()));
94 path.removeWhiteSpace();
95 path.toLower();
96 path.removePTP();
97 path = path.after("/");
98 path.hexDecode();
99 path.realPath();
100 String disposition(docheader->disposition());
101 String extension;
102 if (disposition.length() > 2) {
103 extension = disposition;
104 while (extension.contains(".")) {
105 extension = extension.after(".");
106 }
107 extension = "." + extension;
108 } else {
109 if (!path.contains("?")) {
110 extension = path;
111 } else {
112 if (mimetype.length() == 0)
113 mimetype = docheader->getContentType();
114 if (mimetype.contains("application/")) {
115 extension = path;
116 if (extension.contains("?")) {
117 extension = extension.before("?");
118 }
119 }
120 }
121 }
122 DEBUG_dwload("extension: ", extension);
123 // check the extension list
124 String lc;
125 if (!extension.contains(".") || (extensionlist.findEndsWith(extension.toCharArray(), lc) == NULL))
126 return matchedmime;
127 } else {
128 DEBUG_dwload("NO extensionlistenabled!");
129 }
130
131
132 return true;
133 }
134 #endif
135
136 #ifdef NOTDEF
137 // read in all the lists of various things we wish to handle
138 bool DMPlugin::readStandardLists()
139 {
140 mimetypelist.reset(); // incase this is a reload
141 extensionlist.reset();
142
143 String filename(cv["managedmimetypelist"]);
144 if (filename.length() > 0) {
145 if (!mimetypelist.readItemList(filename.toCharArray(),"", false, 0)) {
146 E2LOGGER_error("Error opening managedmimetypelist");
147 return false;
148 }
149 mimetypelist.doSort(false);
150 mimelistenabled = true;
151 } else {
152 mimelistenabled = false;
153 }
154
155 filename = cv["managedextensionlist"];
156 if (filename.length() > 0) {
157 if (!extensionlist.readItemList(filename.toCharArray(), "", false, 0)) {
158 E2LOGGER_error("Error opening managedextensionlist");
159 return false;
160 }
161 extensionlist.doSort(false);
162 extensionlistenabled = true;
163 } else {
164 extensionlistenabled = false;
165 }
166
167 return true;
168 }
169 #endif
170
171 // take in a DM plugin configuration file, find the DMPlugin descendent matching the value of plugname, and store its class factory funcs for later use
172 DMPlugin *dm_plugin_load(const char *pluginConfigPath)
173 {
174 ConfigVar cv;
175
176 if (cv.readVar(pluginConfigPath, "=") > 0) {
177 E2LOGGER_error("Unable to load plugin config: ", pluginConfigPath);
178 return NULL;
179 }
180
181 String plugname(cv["plugname"]);
182
183 if (plugname.length() < 1) {
184 E2LOGGER_error("Unable read plugin config plugname variable: ", pluginConfigPath);
185 return NULL;
186 }
187
188 if (plugname == "default") {
189 DEBUG_dwload("Enabling default DM plugin");
190 return defaultdmcreate(cv);
191 }
192
193 #ifdef FANCY_READY // Plug-in not working yet
194 if (plugname == "fancy") {
195 DEBUG_dwload("Enabling fancy DM plugin");
196 return fancydmcreate(cv);
197 }
198 #endif
199
200 if (plugname == "trickle") {
201 DEBUG_dwload("Enabling trickle DM plugin");
202 return trickledmcreate(cv);
203 }
204
205 E2LOGGER_error("Unable to load plugin: ", plugname);
206 return NULL;
207 }