~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to proxy/StatPages.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
  StatPages.cc
 
27
 
 
28
 
 
29
 ****************************************************************************/
 
30
 
 
31
#include "ink_config.h"
 
32
#include "ProxyConfig.h"
 
33
#include "StatPages.h"
 
34
#include "HdrUtils.h"
 
35
#include "MatcherUtils.h"
 
36
 
 
37
#define MAX_STAT_PAGES      32
 
38
 
 
39
 
 
40
// Globals
 
41
StatPagesManager statPagesManager;
 
42
 
 
43
static struct
 
44
{
 
45
  char *module;
 
46
  StatPagesFunc func;
 
47
} stat_pages[MAX_STAT_PAGES];
 
48
 
 
49
static volatile int n_stat_pages = 0;
 
50
 
 
51
void
 
52
StatPagesManager::init()
 
53
{
 
54
  REC_EstablishStaticConfigInt32(m_enabled, "proxy.config.http_ui_enabled");
 
55
}
 
56
 
 
57
void
 
58
StatPagesManager::register_http(const char *module, StatPagesFunc func)
 
59
{
 
60
  ink_release_assert(n_stat_pages < MAX_STAT_PAGES);
 
61
 
 
62
  stat_pages[n_stat_pages].module = (char *) xmalloc(strlen(module) + 3);
 
63
  snprintf(stat_pages[n_stat_pages].module, strlen(module) + 3, "{%s}", module);
 
64
  stat_pages[n_stat_pages++].func = func;
 
65
}
 
66
 
 
67
Action *
 
68
StatPagesManager::handle_http(Continuation * cont, HTTPHdr * header, int client_ip)
 
69
{
 
70
  NOWARN_UNUSED(client_ip);
 
71
  URL *url = header->url_get();
 
72
 
 
73
  if (((m_enabled == 1 || m_enabled == 3) && is_cache_inspector_page(url)) ||
 
74
      ((m_enabled == 2 || m_enabled == 3) && is_stat_page(url) && !is_cache_inspector_page(url))) {
 
75
    int host_len;
 
76
    char host[1024];
 
77
    const char *h;
 
78
    int i;
 
79
 
 
80
    h = url->host_get(&host_len);
 
81
    ink_strncpy(host, h, host_len >= 1023 ? 1024 : host_len + 1);
 
82
    host_len = unescapifyStr(host);
 
83
 
 
84
    for (i = 0; i < n_stat_pages; i++) {
 
85
      if (ptr_len_cmp(host, host_len, stat_pages[i].module) == 0) {
 
86
        return stat_pages[i].func(cont, header);
 
87
      }
 
88
    }
 
89
  }
 
90
 
 
91
  cont->handleEvent(STAT_PAGE_FAILURE, 0);
 
92
  return ACTION_RESULT_DONE;
 
93
}
 
94
 
 
95
bool StatPagesManager::is_stat_page(URL * url)
 
96
{
 
97
  int length;
 
98
  const char *h = url->host_get(&length);
 
99
  char host[1024];
 
100
 
 
101
  if (h == NULL || length < 2)
 
102
    return false;
 
103
 
 
104
  ink_strncpy(host, h, length >= 1023 ? 1024 : length + 1);
 
105
  length = unescapifyStr(host);
 
106
 
 
107
  if ((host[0] == '{') && (host[length - 1] == '}'))
 
108
    return true;
 
109
 
 
110
  return false;
 
111
}
 
112
 
 
113
bool StatPagesManager::is_cache_inspector_page(URL * url)
 
114
{
 
115
  int length;
 
116
  const char *h = url->host_get(&length);
 
117
  char host[1024];
 
118
 
 
119
  if (h == NULL || length < 2)
 
120
    return false;
 
121
 
 
122
  ink_strncpy(host, h, length >= 1023 ? 1024 : length + 1);
 
123
  host[length] = '\0';
 
124
  length = unescapifyStr(host);
 
125
 
 
126
  if (strncmp(host, "{cache}", length) == 0)
 
127
    return true;
 
128
  else
 
129
    return false;
 
130
 
 
131
}
 
132
 
 
133
void
 
134
BaseStatPagesHandler::resp_clear()
 
135
{
 
136
  if (response) {
 
137
    xfree(response);
 
138
  }
 
139
 
 
140
  response = NULL;
 
141
  response_size = 0;
 
142
  response_length = 0;
 
143
}
 
144
 
 
145
void
 
146
BaseStatPagesHandler::resp_add(const char *fmt, ...)
 
147
{
 
148
  va_list args;
 
149
  char buf[16384];
 
150
  int length;
 
151
  int size;
 
152
 
 
153
  va_start(args, fmt);
 
154
  length = vsnprintf(buf, 16384, fmt, args);
 
155
  va_end(args);
 
156
 
 
157
  size = response_size;
 
158
  if (size == 0) {
 
159
    size = 1024;
 
160
  }
 
161
  while ((response_length + length + 1) > size) {
 
162
    size *= 2;
 
163
  }
 
164
 
 
165
  if (size != response_size) {
 
166
    if (!response) {
 
167
      response = (char *) xmalloc(size);
 
168
    } else {
 
169
      response = (char *) xrealloc(response, size);
 
170
    }
 
171
    response_size = size;
 
172
  }
 
173
 
 
174
  memcpy(&response[response_length], buf, length + 1);
 
175
  response_length += length;
 
176
}
 
177
 
 
178
void
 
179
BaseStatPagesHandler::resp_add_sep()
 
180
{
 
181
  resp_add("<hr width=\"100%%\">\n");
 
182
}
 
183
 
 
184
void
 
185
BaseStatPagesHandler::resp_begin(const char *title)
 
186
{
 
187
  resp_clear();
 
188
  resp_add("<html>\n"
 
189
           "<head><title>%s</title></head>\n"
 
190
           "<body text=\"#000000\" bgcolor=\"#ffffff\" link=\"#0000ee\" vlink=\"#551a8b\" alink=\"#ff0000\">\n", title);
 
191
}
 
192
 
 
193
void
 
194
BaseStatPagesHandler::resp_end()
 
195
{
 
196
  resp_add("</body>\n" "</html>\n");
 
197
}
 
198
 
 
199
void
 
200
BaseStatPagesHandler::resp_begin_numbered()
 
201
{
 
202
  resp_add("<ol>\n");
 
203
}
 
204
 
 
205
void
 
206
BaseStatPagesHandler::resp_end_numbered()
 
207
{
 
208
  resp_add("</ol>\n");
 
209
}
 
210
 
 
211
void
 
212
BaseStatPagesHandler::resp_begin_unnumbered()
 
213
{
 
214
  resp_add("<ul>\n");
 
215
}
 
216
 
 
217
void
 
218
BaseStatPagesHandler::resp_end_unnumbered()
 
219
{
 
220
  resp_add("</ul>\n");
 
221
}
 
222
 
 
223
void
 
224
BaseStatPagesHandler::resp_begin_item()
 
225
{
 
226
  resp_add("<li>\n");
 
227
}
 
228
 
 
229
void
 
230
BaseStatPagesHandler::resp_end_item()
 
231
{
 
232
  resp_add("</li>\n");
 
233
}
 
234
 
 
235
void
 
236
BaseStatPagesHandler::resp_begin_table(int border, int columns, int percent)
 
237
{
 
238
  resp_add("<table border=%d cols=%d width=\"%d%%\">\n", border, columns, percent);
 
239
}
 
240
 
 
241
void
 
242
BaseStatPagesHandler::resp_end_table()
 
243
{
 
244
  resp_add("</table>\n");
 
245
}
 
246
 
 
247
void
 
248
BaseStatPagesHandler::resp_begin_row()
 
249
{
 
250
  resp_add("<tr>\n");
 
251
}
 
252
 
 
253
void
 
254
BaseStatPagesHandler::resp_end_row()
 
255
{
 
256
  resp_add("</tr>\n");
 
257
}
 
258
 
 
259
void
 
260
BaseStatPagesHandler::resp_begin_column(int percent, const char *align)
 
261
{
 
262
  if (percent == -1) {
 
263
    resp_add("<td %s%s>\n", align ? "align=" : "", align ? align : "");
 
264
  } else {
 
265
    resp_add("<td width=\"%d%%\" %s%s>\n", percent, align ? "align=" : "", align ? align : "");
 
266
  }
 
267
}
 
268
 
 
269
void
 
270
BaseStatPagesHandler::resp_end_column()
 
271
{
 
272
  resp_add("</td>\n");
 
273
}