Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 #include "ink_config.h"
00032 #include "ProxyConfig.h"
00033 #include "StatPages.h"
00034 #include "HdrUtils.h"
00035 #include "MatcherUtils.h"
00036 
00037 #define MAX_STAT_PAGES      32
00038 
00039 
00040 
00041 StatPagesManager statPagesManager;
00042 
00043 static struct
00044 {
00045   char *module;
00046   StatPagesFunc func;
00047 } stat_pages[MAX_STAT_PAGES];
00048 
00049 static volatile int n_stat_pages = 0;
00050 
00051 void
00052 StatPagesManager::init()
00053 {
00054   REC_EstablishStaticConfigInt32(m_enabled, "proxy.config.http_ui_enabled");
00055 }
00056 
00057 void
00058 StatPagesManager::register_http(const char *module, StatPagesFunc func)
00059 {
00060   ink_release_assert(n_stat_pages < MAX_STAT_PAGES);
00061 
00062   stat_pages[n_stat_pages].module = (char *)ats_malloc(strlen(module) + 3);
00063   snprintf(stat_pages[n_stat_pages].module, strlen(module) + 3, "{%s}", module);
00064   stat_pages[n_stat_pages++].func = func;
00065 }
00066 
00067 Action *
00068 StatPagesManager::handle_http(Continuation * cont, HTTPHdr * header)
00069 {
00070   URL *url = header->url_get();
00071 
00072   if (((m_enabled == 1 || m_enabled == 3) && is_cache_inspector_page(url)) ||
00073       ((m_enabled == 2 || m_enabled == 3) && is_stat_page(url) && !is_cache_inspector_page(url))) {
00074     int host_len;
00075     char host[MAXDNAME + 1];
00076     const char *h;
00077     int i;
00078 
00079     h = url->host_get(&host_len);
00080     if (host_len > MAXDNAME)
00081       host_len = MAXDNAME;
00082     memcpy(host, h, host_len);
00083     host[host_len] = '\0';
00084     host_len = unescapifyStr(host);
00085 
00086     for (i = 0; i < n_stat_pages; i++) {
00087       if (ptr_len_cmp(host, host_len, stat_pages[i].module) == 0) {
00088         return stat_pages[i].func(cont, header);
00089       }
00090     }
00091   }
00092 
00093   cont->handleEvent(STAT_PAGE_FAILURE, 0);
00094   return ACTION_RESULT_DONE;
00095 }
00096 
00097 bool
00098 StatPagesManager::is_stat_page(URL * url)
00099 {
00100   
00101   if (m_enabled <= 0)
00102     return false;
00103 
00104   int length;
00105   const char *h = url->host_get(&length);
00106   char host[MAXDNAME + 1];
00107 
00108   if (h == NULL || length < 2 || length > MAXDNAME)
00109     return false;
00110 
00111   memcpy(host, h, length);
00112   host[length] = '\0';
00113   length = unescapifyStr(host);
00114 
00115   if ((host[0] == '{') && (host[length - 1] == '}'))
00116     return true;
00117 
00118   return false;
00119 }
00120 
00121 bool
00122 StatPagesManager::is_cache_inspector_page(URL * url)
00123 {
00124   int length;
00125   const char *h = url->host_get(&length);
00126   char host[MAXDNAME + 1];
00127 
00128   if (h == NULL || length < 2 || length > MAXDNAME)
00129     return false;
00130 
00131   memcpy(host, h, length);
00132   host[length] = '\0';
00133   length = unescapifyStr(host);
00134 
00135   if (strncmp(host, "{cache}", length) == 0)
00136     return true;
00137   else
00138     return false;
00139 
00140 }
00141 
00142 void
00143 BaseStatPagesHandler::resp_clear()
00144 {
00145   ats_free(response);
00146   response = NULL;
00147   response_size = 0;
00148   response_length = 0;
00149 }
00150 
00151 void
00152 BaseStatPagesHandler::resp_add(const char *fmt, ...)
00153 {
00154   va_list args;
00155   char buf[16384];
00156   int length;
00157   int size;
00158 
00159   va_start(args, fmt);
00160   length = vsnprintf(buf, 16384, fmt, args);
00161   va_end(args);
00162 
00163   size = response_size;
00164   if (size == 0) {
00165     size = 1024;
00166   }
00167   while ((response_length + length + 1) > size) {
00168     size *= 2;
00169   }
00170 
00171   if (size != response_size) {
00172     if (!response) {
00173       response = (char *)ats_malloc(size);
00174     } else {
00175       response = (char *)ats_realloc(response, size);
00176     }
00177     response_size = size;
00178   }
00179 
00180   memcpy(&response[response_length], buf, length + 1);
00181   response_length += length;
00182 }
00183 
00184 void
00185 BaseStatPagesHandler::resp_add_sep()
00186 {
00187   resp_add("<hr width=\"100%%\">\n");
00188 }
00189 
00190 void
00191 BaseStatPagesHandler::resp_begin(const char *title)
00192 {
00193   resp_clear();
00194   resp_add("<html>\n"
00195            "<head><title>%s</title></head>\n"
00196            "<body text=\"#000000\" bgcolor=\"#ffffff\" link=\"#0000ee\" vlink=\"#551a8b\" alink=\"#ff0000\">\n", title);
00197 }
00198 
00199 void
00200 BaseStatPagesHandler::resp_end()
00201 {
00202   resp_add("</body>\n" "</html>\n");
00203 }
00204 
00205 void
00206 BaseStatPagesHandler::resp_begin_numbered()
00207 {
00208   resp_add("<ol>\n");
00209 }
00210 
00211 void
00212 BaseStatPagesHandler::resp_end_numbered()
00213 {
00214   resp_add("</ol>\n");
00215 }
00216 
00217 void
00218 BaseStatPagesHandler::resp_begin_unnumbered()
00219 {
00220   resp_add("<ul>\n");
00221 }
00222 
00223 void
00224 BaseStatPagesHandler::resp_end_unnumbered()
00225 {
00226   resp_add("</ul>\n");
00227 }
00228 
00229 void
00230 BaseStatPagesHandler::resp_begin_item()
00231 {
00232   resp_add("<li>\n");
00233 }
00234 
00235 void
00236 BaseStatPagesHandler::resp_end_item()
00237 {
00238   resp_add("</li>\n");
00239 }
00240 
00241 void
00242 BaseStatPagesHandler::resp_begin_table(int border, int columns, int percent)
00243 {
00244   resp_add("<table border=%d cols=%d width=\"%d%%\">\n", border, columns, percent);
00245 }
00246 
00247 void
00248 BaseStatPagesHandler::resp_end_table()
00249 {
00250   resp_add("</table>\n");
00251 }
00252 
00253 void
00254 BaseStatPagesHandler::resp_begin_row()
00255 {
00256   resp_add("<tr>\n");
00257 }
00258 
00259 void
00260 BaseStatPagesHandler::resp_end_row()
00261 {
00262   resp_add("</tr>\n");
00263 }
00264 
00265 void
00266 BaseStatPagesHandler::resp_begin_column(int percent, const char *align)
00267 {
00268   if (percent == -1) {
00269     resp_add("<td %s%s>\n", align ? "align=" : "", align ? align : "");
00270   } else {
00271     resp_add("<td width=\"%d%%\" %s%s>\n", percent, align ? "align=" : "", align ? align : "");
00272   }
00273 }
00274 
00275 void
00276 BaseStatPagesHandler::resp_end_column()
00277 {
00278   resp_add("</td>\n");
00279 }