xref: /trafficserver/proxy/Show.h (revision 53c2282b)
1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /****************************************************************************
25 
26   Show.h
27 
28 
29  ****************************************************************************/
30 
31 #pragma once
32 
33 #include "StatPages.h"
34 
35 #define STREQ_PREFIX(_x, _s) (!strncasecmp(_x, _s, sizeof(_s) - 1))
36 
37 struct ShowCont;
38 typedef int (ShowCont::*ShowContEventHandler)(int event, Event *data);
39 struct ShowCont : public Continuation {
40 private:
41   char *buf, *start, *ebuf;
42 
43 public:
44   Action action;
45   char *sarg;
46 
47   int
showShowCont48   show(const char *s, ...)
49   {
50     va_list aap, va_scratch;
51     ptrdiff_t avail = ebuf - buf;
52     ptrdiff_t needed;
53 
54     va_start(aap, s);
55     va_copy(va_scratch, aap);
56     needed = vsnprintf(buf, avail, s, va_scratch);
57     va_end(va_scratch);
58 
59     if (needed >= avail) {
60       ptrdiff_t bufsz = ebuf - start;
61       ptrdiff_t used  = buf - start;
62 
63       Debug("cache_inspector", "needed %d bytes, reallocating to %d bytes", (int)needed, (int)bufsz + (int)needed);
64 
65       bufsz += ROUNDUP(needed, ats_pagesize());
66       start = (char *)ats_realloc(start, bufsz);
67       ebuf  = start + bufsz;
68       buf   = start + used;
69       avail = ebuf - buf;
70 
71       needed = vsnprintf(buf, avail, s, aap);
72       va_end(aap);
73 
74       if (needed >= avail) {
75         Debug("cache_inspector", "needed %d bytes, but had only %d", (int)needed, (int)avail + (int)needed);
76         return EVENT_DONE;
77       }
78     }
79 
80     buf += needed;
81     return EVENT_CONT;
82   }
83 
84 #define CHECK_SHOW(_x)  \
85   if (_x == EVENT_DONE) \
86     return complete_error(event, e);
87 
88   int
finishConnShowCont89   finishConn(int event, Event *e)
90   {
91     if (!action.cancelled) {
92       StatPageData data(start, buf - start);
93       action.continuation->handleEvent(STAT_PAGE_SUCCESS, &data);
94       start = nullptr;
95     } else {
96       ats_free(start);
97       start = nullptr;
98     }
99     return done(VIO::CLOSE, event, e);
100   }
101 
102   int
completeShowCont103   complete(int event, Event *e)
104   {
105     CHECK_SHOW(show("</BODY>\n</HTML>\n"));
106     return finishConn(event, e);
107   }
108 
109   int
completeJsonShowCont110   completeJson(int event, Event *e)
111   {
112     return finishConn(event, e);
113   }
114 
115   int
complete_errorShowCont116   complete_error(int event, Event *e)
117   {
118     ats_free(start);
119     start = nullptr;
120     if (!action.cancelled) {
121       action.continuation->handleEvent(STAT_PAGE_FAILURE, nullptr);
122     }
123     return done(VIO::ABORT, event, e);
124   }
125 
126   int
beginShowCont127   begin(const char *name)
128   {
129     return show("<HTML>\n<HEAD><TITLE>%s</TITLE>\n"
130                 "<BODY BGCOLOR=\"#ffffff\" FGCOLOR=\"#00ff00\">\n"
131                 "<H1>%s</H1>\n",
132                 name, name);
133   }
134 
135   int
showErrorShowCont136   showError(int event, Event *e)
137   {
138     return complete_error(event, e);
139   }
140 
141   virtual int
doneShowCont142   done(int /* e ATS_UNUSED */, int /* event ATS_UNUSED */, void * /* data ATS_UNUSED */)
143   {
144     delete this;
145     return EVENT_DONE;
146   }
147 
ShowContShowCont148   ShowCont(Continuation *c, HTTPHdr * /* h ATS_UNUSED */) : Continuation(nullptr), sarg(nullptr)
149   {
150     size_t sz = ats_pagesize();
151 
152     mutex  = c->mutex;
153     action = c;
154     buf    = (char *)ats_malloc(sz);
155     start  = buf;
156     ebuf   = buf + sz;
157   }
158 
~ShowContShowCont159   ~ShowCont() override
160   {
161     ats_free(sarg);
162     ats_free(start);
163   }
164 };
165