1 ------------------------------------------------------------
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>
7 timestamp: Wed 2016-04-20 18:39:07 +1200
9 cachemgr.cgi: use dynamic MemBuf for internal content generation
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-\
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
29 #include "tests/STUB.h"
31 void cbdataRegisterWithCacheManager(void) STUB
33 +void *cbdataInternalAlloc(cbdata_type type, const char *, int sz) {
34 + return xcalloc(1, sz);
36 +void *cbdataInternalFree(void *p, const char *, int) {
41 void *cbdataInternalAllocDbg(cbdata_type type, const char *, int) STUB_RETVAL(NULL)
42 void *cbdataInternalFreeDbg(void *p, const char *, int) STUB_RETVAL(NULL)
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
50 #define STUB_API "stub_mem.cc"
52 +#include "tests/STUB.h"
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
61 stub_debug.cc: $(top_srcdir)/src/tests/stub_debug.cc
62 cp $(top_srcdir)/src/tests/stub_debug.cc .
64 +MemBuf.cc: $(top_srcdir)/src/MemBuf.cc
65 + cp $(top_srcdir)/src/MemBuf.cc $@
67 time.cc: $(top_srcdir)/src/time.cc
68 cp $(top_srcdir)/src/time.cc .
70 +stub_cbdata.cc: $(top_srcdir)/src/tests/stub_cbdata.cc
71 + cp $(top_srcdir)/src/tests/stub_cbdata.cc $@
73 +stub_mem.cc: $(top_srcdir)/src/tests/stub_mem.cc
74 + cp $(top_srcdir)/src/tests/stub_mem.cc $@
76 # stock tools for unit tests - library independent versions of dlink_list
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
84 ## ##### helper-mux #####
87 libexec_PROGRAMS = cachemgr$(CGIEXT)
89 cachemgr__CGIEXT__SOURCES = cachemgr.cc \
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
102 #include "getfullhostname.h"
103 #include "html_quote.h"
104 #include "ip/Address.h"
114 -munge_menu_line(const char *buf, cachemgr_request * req)
116 +munge_menu_line(MemBuf &out, const char *buf, cachemgr_request * req)
120 @@ -469,15 +470,14 @@
124 - static char html[2 * 1024];
126 - if (strlen(buf) < 1)
132 - buf_copy = x = xstrdup(buf);
134 + const char bufLen = strlen(buf);
135 + if (bufLen < 1 || *buf != ' ') {
136 + out.append(buf, bufLen);
140 + buf_copy = x = xstrndup(buf, bufLen);
142 a = xstrtok(&x, '\t');
144 @@ -489,59 +489,56 @@
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);
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);
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);
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",
167 + out.Printf("<LI type=\"square\"><A HREF=\"%s\"><font color=\"#FF0000\">%s</font></A>\n",
170 /* public entry or unknown type of protection */
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);
183 -munge_other_line(const char *buf, cachemgr_request * req)
185 +munge_other_line(MemBuf &out, const char *buf, cachemgr_request *)
187 static const char *ttags[] = {"td", "th"};
189 - static char html[4096];
190 static int table_line_num = 0;
191 static int next_is_header = 0;
197 /* does it look like a table? */
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));
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);
219 ttag = ttags[is_header];
222 - l += snprintf(html + l, sizeof(html) - l, "<tr>");
223 + out.append("<tr>", 4);
225 /* substitute '\t' */
226 buf_copy = x = xstrdup(buf);
227 @@ -568,18 +565,17 @@
231 - l += snprintf(html + l, sizeof(html) - l, "<%s colspan=\"%d\" align=\"%s\">%s</%s>",
233 - is_header ? "center" : is_number(cell) ? "right" : "left",
234 - html_quote(cell), ttag);
235 + out.Printf("<%s colspan=\"%d\" align=\"%s\">%s</%s>",
237 + is_header ? "center" : is_number(cell) ? "right" : "left",
238 + html_quote(cell), ttag);
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");
251 @@ -736,14 +732,18 @@
252 /* yes, fall through, we do not want to loose the first line */
256 /* interpret [and reformat] cache response */
261 - fputs(munge_menu_line(buf, req), stdout);
262 + munge_menu_line(out, buf, req);
264 - fputs(munge_other_line(buf, req), stdout);
265 + munge_other_line(out, buf, req);
268 + fputs(out.buf, stdout);
273 /* forward: no modifications allowed */