]>
Commit | Line | Data |
---|---|---|
198c3926 MF |
1 | ------------------------------------------------------------ |
2 | revno: 13233 | |
3 | revision-id: squid3@treenet.co.nz-20160420063907-hcnf4qmcg5hbjc11 | |
4 | parent: squid3@treenet.co.nz-20160330141410-t6p2dhzr8ri36fap | |
5 | committer: Amos Jeffries <squid3@treenet.co.nz> | |
6 | branch nick: 3.4 | |
7 | timestamp: Wed 2016-04-20 18:39:07 +1200 | |
8 | message: | |
9 | cachemgr.cgi: use dynamic MemBuf for internal content generation | |
10 | ||
11 | Using a fixed size buffer limits how big content lines can be. Modern | |
12 | HTTP is fast reaching the point where such limits are problematic. | |
13 | Also fixes incorrect uses of snprintf() by removing them. | |
14 | ------------------------------------------------------------ | |
15 | # Bazaar merge directive format 2 (Bazaar 0.90) | |
16 | # revision_id: squid3@treenet.co.nz-20160420063907-hcnf4qmcg5hbjc11 | |
17 | # target_branch: http://bzr.squid-cache.org/bzr/squid3/3.4 | |
18 | # testament_sha1: 161e86814f6f14d74557a3fa169b37b6601c08a1 | |
19 | # timestamp: 2016-04-20 06:50:57 +0000 | |
20 | # source_branch: http://bzr.squid-cache.org/bzr/squid3/3.4 | |
21 | # base_revision_id: squid3@treenet.co.nz-20160330141410-\ | |
22 | # t6p2dhzr8ri36fap | |
23 | # | |
24 | # Begin patch | |
25 | === modified file 'src/tests/stub_cbdata.cc' | |
26 | --- src/tests/stub_cbdata.cc 2012-11-01 10:31:28 +0000 | |
27 | +++ src/tests/stub_cbdata.cc 2016-04-20 06:39:07 +0000 | |
28 | @@ -5,7 +5,13 @@ | |
29 | #include "tests/STUB.h" | |
30 | ||
31 | void cbdataRegisterWithCacheManager(void) STUB | |
32 | - | |
33 | +void *cbdataInternalAlloc(cbdata_type type, const char *, int sz) { | |
34 | + return xcalloc(1, sz); | |
35 | +} | |
36 | +void *cbdataInternalFree(void *p, const char *, int) { | |
37 | + xfree(p); | |
38 | + return nullptr; | |
39 | +} | |
40 | #if USE_CBDATA_DEBUG | |
41 | void *cbdataInternalAllocDbg(cbdata_type type, const char *, int) STUB_RETVAL(NULL) | |
42 | void *cbdataInternalFreeDbg(void *p, const char *, int) STUB_RETVAL(NULL) | |
43 | ||
44 | === modified file 'src/tests/stub_mem.cc' | |
45 | --- src/tests/stub_mem.cc 2012-08-29 07:29:35 +0000 | |
46 | +++ src/tests/stub_mem.cc 2016-04-20 06:39:07 +0000 | |
47 | @@ -5,7 +5,7 @@ | |
48 | #include "squid.h" | |
49 | ||
50 | #define STUB_API "stub_mem.cc" | |
51 | -#include "STUB.h" | |
52 | +#include "tests/STUB.h" | |
53 | #include "Mem.h" | |
54 | ||
55 | void | |
56 | ||
57 | === modified file 'tools/Makefile.am' | |
58 | --- tools/Makefile.am 2014-04-06 04:37:08 +0000 | |
59 | +++ tools/Makefile.am 2016-04-20 06:39:07 +0000 | |
60 | @@ -35,15 +35,24 @@ | |
61 | stub_debug.cc: $(top_srcdir)/src/tests/stub_debug.cc | |
62 | cp $(top_srcdir)/src/tests/stub_debug.cc . | |
63 | ||
64 | +MemBuf.cc: $(top_srcdir)/src/MemBuf.cc | |
65 | + cp $(top_srcdir)/src/MemBuf.cc $@ | |
66 | + | |
67 | time.cc: $(top_srcdir)/src/time.cc | |
68 | cp $(top_srcdir)/src/time.cc . | |
69 | ||
70 | +stub_cbdata.cc: $(top_srcdir)/src/tests/stub_cbdata.cc | |
71 | + cp $(top_srcdir)/src/tests/stub_cbdata.cc $@ | |
72 | + | |
73 | +stub_mem.cc: $(top_srcdir)/src/tests/stub_mem.cc | |
74 | + cp $(top_srcdir)/src/tests/stub_mem.cc $@ | |
75 | + | |
76 | # stock tools for unit tests - library independent versions of dlink_list | |
77 | # etc. | |
78 | # globals.cc is needed by test_tools.cc. | |
79 | # Neither of these should be disted from here. | |
80 | TESTSOURCES= test_tools.cc | |
81 | -CLEANFILES += test_tools.cc stub_debug.cc time.cc | |
82 | +CLEANFILES += test_tools.cc MemBuf.cc stub_debug.cc time.cc stub_cbdata.cc stub_mem.cc | |
83 | ||
84 | ## ##### helper-mux ##### | |
85 | ||
86 | @@ -74,7 +83,10 @@ | |
87 | libexec_PROGRAMS = cachemgr$(CGIEXT) | |
88 | ||
89 | cachemgr__CGIEXT__SOURCES = cachemgr.cc \ | |
90 | + MemBuf.cc \ | |
91 | + stub_cbdata.cc \ | |
92 | stub_debug.cc \ | |
93 | + stub_mem.cc \ | |
94 | test_tools.cc \ | |
95 | time.cc | |
96 | ||
97 | ||
98 | === modified file 'tools/cachemgr.cc' | |
99 | --- tools/cachemgr.cc 2015-01-09 10:32:57 +0000 | |
100 | +++ tools/cachemgr.cc 2016-04-20 06:39:07 +0000 | |
101 | @@ -35,6 +35,7 @@ | |
102 | #include "getfullhostname.h" | |
103 | #include "html_quote.h" | |
104 | #include "ip/Address.h" | |
105 | +#include "MemBuf.h" | |
106 | #include "rfc1123.h" | |
107 | #include "rfc1738.h" | |
108 | #include "util.h" | |
109 | @@ -460,8 +461,8 @@ | |
110 | return url; | |
111 | } | |
112 | ||
113 | -static const char * | |
114 | -munge_menu_line(const char *buf, cachemgr_request * req) | |
115 | +static void | |
116 | +munge_menu_line(MemBuf &out, const char *buf, cachemgr_request * req) | |
117 | { | |
118 | char *x; | |
119 | const char *a; | |
120 | @@ -469,15 +470,14 @@ | |
121 | const char *p; | |
122 | char *a_url; | |
123 | char *buf_copy; | |
124 | - static char html[2 * 1024]; | |
125 | - | |
126 | - if (strlen(buf) < 1) | |
127 | - return buf; | |
128 | - | |
129 | - if (*buf != ' ') | |
130 | - return buf; | |
131 | - | |
132 | - buf_copy = x = xstrdup(buf); | |
133 | + | |
134 | + const char bufLen = strlen(buf); | |
135 | + if (bufLen < 1 || *buf != ' ') { | |
136 | + out.append(buf, bufLen); | |
137 | + return; | |
138 | + } | |
139 | + | |
140 | + buf_copy = x = xstrndup(buf, bufLen); | |
141 | ||
142 | a = xstrtok(&x, '\t'); | |
143 | ||
144 | @@ -489,59 +489,56 @@ | |
145 | ||
146 | /* no reason to give a url for a disabled action */ | |
147 | if (!strcmp(p, "disabled")) | |
148 | - snprintf(html, sizeof(html), "<LI type=\"circle\">%s (disabled)<A HREF=\"%s\">.</A>\n", d, a_url); | |
149 | + out.Printf("<LI type=\"circle\">%s (disabled)<A HREF=\"%s\">.</A>\n", d, a_url); | |
150 | else | |
151 | /* disable a hidden action (requires a password, but password is not in squid.conf) */ | |
152 | if (!strcmp(p, "hidden")) | |
153 | - snprintf(html, sizeof(html), "<LI type=\"circle\">%s (hidden)<A HREF=\"%s\">.</A>\n", d, a_url); | |
154 | + out.Printf("<LI type=\"circle\">%s (hidden)<A HREF=\"%s\">.</A>\n", d, a_url); | |
155 | else | |
156 | /* disable link if authentication is required and we have no password */ | |
157 | if (!strcmp(p, "protected") && !req->passwd) | |
158 | - snprintf(html, sizeof(html), "<LI type=\"circle\">%s (requires <a href=\"%s\">authentication</a>)<A HREF=\"%s\">.</A>\n", | |
159 | - d, menu_url(req, "authenticate"), a_url); | |
160 | + out.Printf("<LI type=\"circle\">%s (requires <a href=\"%s\">authentication</a>)<A HREF=\"%s\">.</A>\n", | |
161 | + d, menu_url(req, "authenticate"), a_url); | |
162 | else | |
163 | /* highlight protected but probably available entries */ | |
164 | if (!strcmp(p, "protected")) | |
165 | - snprintf(html, sizeof(html), "<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n", | |
166 | - a_url, d); | |
167 | + out.Printf("<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n", | |
168 | + a_url, d); | |
169 | ||
170 | /* public entry or unknown type of protection */ | |
171 | else | |
172 | - snprintf(html, sizeof(html), "<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url, d); | |
173 | + out.Printf("<LI type=\"disk\"><A HREF=\"%s\">%s</A>\n", a_url, d); | |
174 | ||
175 | xfree(a_url); | |
176 | ||
177 | xfree(buf_copy); | |
178 | - | |
179 | - return html; | |
180 | } | |
181 | ||
182 | -static const char * | |
183 | -munge_other_line(const char *buf, cachemgr_request * req) | |
184 | +static void | |
185 | +munge_other_line(MemBuf &out, const char *buf, cachemgr_request *) | |
186 | { | |
187 | static const char *ttags[] = {"td", "th"}; | |
188 | ||
189 | - static char html[4096]; | |
190 | static int table_line_num = 0; | |
191 | static int next_is_header = 0; | |
192 | int is_header = 0; | |
193 | const char *ttag; | |
194 | char *buf_copy; | |
195 | char *x, *p; | |
196 | - int l = 0; | |
197 | /* does it look like a table? */ | |
198 | ||
199 | if (!strchr(buf, '\t') || *buf == '\t') { | |
200 | /* nope, just text */ | |
201 | - snprintf(html, sizeof(html), "%s%s", | |
202 | - table_line_num ? "</table>\n<pre>" : "", html_quote(buf)); | |
203 | + if (table_line_num) | |
204 | + out.append("</table>\n<pre>", 14); | |
205 | + out.Printf("%s", html_quote(buf)); | |
206 | table_line_num = 0; | |
207 | - return html; | |
208 | + return; | |
209 | } | |
210 | ||
211 | /* start html table */ | |
212 | if (!table_line_num) { | |
213 | - l += snprintf(html + l, sizeof(html) - l, "</pre><table cellpadding=\"2\" cellspacing=\"1\">\n"); | |
214 | + out.append("</pre><table cellpadding=\"2\" cellspacing=\"1\">\n", 46); | |
215 | next_is_header = 0; | |
216 | } | |
217 | ||
218 | @@ -551,7 +548,7 @@ | |
219 | ttag = ttags[is_header]; | |
220 | ||
221 | /* record starts */ | |
222 | - l += snprintf(html + l, sizeof(html) - l, "<tr>"); | |
223 | + out.append("<tr>", 4); | |
224 | ||
225 | /* substitute '\t' */ | |
226 | buf_copy = x = xstrdup(buf); | |
227 | @@ -568,18 +565,17 @@ | |
228 | ++x; | |
229 | } | |
230 | ||
231 | - l += snprintf(html + l, sizeof(html) - l, "<%s colspan=\"%d\" align=\"%s\">%s</%s>", | |
232 | - ttag, column_span, | |
233 | - is_header ? "center" : is_number(cell) ? "right" : "left", | |
234 | - html_quote(cell), ttag); | |
235 | + out.Printf("<%s colspan=\"%d\" align=\"%s\">%s</%s>", | |
236 | + ttag, column_span, | |
237 | + is_header ? "center" : is_number(cell) ? "right" : "left", | |
238 | + html_quote(cell), ttag); | |
239 | } | |
240 | ||
241 | xfree(buf_copy); | |
242 | /* record ends */ | |
243 | - snprintf(html + l, sizeof(html) - l, "</tr>\n"); | |
244 | + out.append("</tr>\n", 6); | |
245 | next_is_header = is_header && strstr(buf, "\t\t"); | |
246 | ++table_line_num; | |
247 | - return html; | |
248 | } | |
249 | ||
250 | static const char * | |
251 | @@ -736,14 +732,18 @@ | |
252 | /* yes, fall through, we do not want to loose the first line */ | |
253 | ||
254 | case isBody: | |
255 | + { | |
256 | /* interpret [and reformat] cache response */ | |
257 | - | |
258 | + MemBuf out; | |
259 | + out.init(); | |
260 | if (parse_menu) | |
261 | - fputs(munge_menu_line(buf, req), stdout); | |
262 | + munge_menu_line(out, buf, req); | |
263 | else | |
264 | - fputs(munge_other_line(buf, req), stdout); | |
265 | + munge_other_line(out, buf, req); | |
266 | ||
267 | - break; | |
268 | + fputs(out.buf, stdout); | |
269 | + } | |
270 | + break; | |
271 | ||
272 | case isForward: | |
273 | /* forward: no modifications allowed */ | |
274 |