Debian Codegraph @

src/downloadmanagers/default.cpp

raw
1 // Default download manager, used when no other plugin matches the user agent
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 #ifdef HAVE_CONFIG_H
9 #include "e2config.h"
10 #endif
11
12 #include "../DownloadManager.hpp"
13 #include "../OptionContainer.hpp"
14 #include "../Logger.hpp"
15
16 #include <string.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <unistd.h>
20
21 // GLOBALS
22
23 extern OptionContainer o;
24
25 // DECLARATIONS
26
27 class dminstance : public DMPlugin {
28 public:
29 dminstance(ConfigVar &definition)
30 : DMPlugin(definition) {};
31
32 int in(DataBuffer *d, Socket *sock, Socket *peersock, HTTPHeader *requestheader,
33 HTTPHeader *docheader, bool wantall, int *headersent, bool *toobig);
34
35 // default plugin is as basic as you can get - no initialisation, and uses the default
36 // set of matching mechanisms. uncomment and implement these to override default behaviour.
37 //int init(void* args);
38 //bool willHandle(HTTPHeader *requestheader, HTTPHeader *docheader);
39 };
40
41 // IMPLEMENTATION
42
43 // class factory code *MUST* be included in every plugin
44
45 DMPlugin *defaultdmcreate(ConfigVar &definition) {
46 DEBUG_trace("Creating default DM");
47 return new dminstance(definition);
48 }
49
50 // end of Class factory
51
52 // uncomment these if you wish to replace the default inherited functions
53 // < 0 = error
54 // = 0 = ok
55 // > 0 = warning
56
57 //int dminstance::init(void* args) {
58 // return 0;
59 //}
60 //int dminstance::quit(void) {
61 // return 0;
62 //}
63
64 // download body for this request
65 int dminstance::in(DataBuffer *d, Socket *sock, Socket *peersock, class HTTPHeader *requestheader,
66 class HTTPHeader *docheader, bool wantall, int *headersent, bool *toobig) {
67
68 //DataBuffer *d = where to stick the data back into
69 //Socket *sock = where to read from
70 //Socket *peersock = browser to send stuff to for keeping it alive
71 //HTTPHeader *requestheader = header client used to request
72 //HTTPHeader *docheader = header used for sending first line of reply
73 //bool wantall = to determine if just content filter or a full scan
74 //int *headersent = to use to send the first line of header if needed
75 // or to mark the header has already been sent
76 //bool *toobig = flag to modify to say if it could not all be downloaded
77
78 DEBUG_trace("Inside default download manager plugin icap=", d->icap);
79
80 // To access settings for the plugin use the following example:
81 // std::cerr << "cvtest:" << cv["dummy"] << std::endl;
82
83 // int rc = 0;
84 d->got_all = false;
85 d->bytes_toget = docheader->contentLength();
86 if (!d->icap) {
87 DEBUG_dwload("tranencodeing is ", docheader->transferEncoding());
88 d->chunked = docheader->transferEncoding().contains("chunked");
89 }
90
91 // if using non-persistent connections, some servers will not report
92 // a content-length. in these situations, just download everything.
93 d->geteverything = false;
94 if ((d->bytes_toget < 0) || (d->chunked))
95 d->geteverything = true;
96
97 d->swappedtodisk = false;
98 d->doneinitialdelay = false;
99
100 struct timeval themdays;
101 struct timeval nowadays;
102 gettimeofday(&themdays, NULL);
103
104 // buffer size for streaming downloads
105 off_t blocksize = 32768;
106 // set to a sensible minimum
107 if (!wantall && (blocksize > o.content.max_content_filter_size))
108 blocksize = o.content.max_content_filter_size;
109 else if (wantall && (blocksize > o.content.max_content_ramcache_scan_size))
110 blocksize = o.content.max_content_ramcache_scan_size;
111
112 DEBUG_dwload("blocksize: ", blocksize);
113
114 while ((d->bytes_toget > 0) || d->geteverything) {
115 DEBUG_dwload("toget:", d->bytes_toget, " geteverything ", d->geteverything);
116 // send x-header keep-alive here
117 if (o.content.trickle_delay > 0) {
118 gettimeofday(&nowadays, NULL);
119 if (d->doneinitialdelay ? nowadays.tv_sec - themdays.tv_sec > o.content.trickle_delay :
120 nowadays.tv_sec - themdays.tv_sec > o.content.initial_trickle_delay) {
121 themdays.tv_sec = nowadays.tv_sec;
122 d->doneinitialdelay = true;
123 if ((*headersent) < 1) {
124 DEBUG_dwload("sending first line of header first");
125 if (!d->icap) {
126 docheader->out(NULL, peersock, __E2HEADER_SENDFIRSTLINE);
127 (*headersent) = 1;
128 }
129 }
130 DEBUG_dwload("trickle delay - sending X-E2KeepAlive: on");
131 if (!d->icap)
132 peersock->writeString("X-E2GKeepAlive: on\r\n");
133 }
134 }
135 int read_res;
136 int rc;
137 int bsize = blocksize;
138 if((!d->geteverything) && (d->bytes_toget < bsize))
139 bsize = d->bytes_toget;
140 DEBUG_dwload("bsize is ", bsize);
141
142 rc = d->readInFromSocket(sock,bsize,wantall, read_res);
143 if(read_res & DB_TOBIG)
144 *toobig = true;
145 if (rc <= 0) break;
146 }
147
148 if (!(*toobig) && !d->swappedtodisk) { // won't deflate stuff swapped to disk
149 if (d->decompress.contains("deflate")) {
150 DEBUG_dwload("zlib format");
151 d->zlibinflate(false); // incoming stream was zlib compressed
152 } else if (d->decompress.contains("gzip")) {
153 DEBUG_dwload("gzip format");
154 d->zlibinflate(true); // incoming stream was gzip compressed
155 }
156 }
157 d->bytesalreadysent = 0;
158 DEBUG_trace("Leaving default download manager plugin");
159 return 0;
160 }