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.h
27 
28 
29  ****************************************************************************/
30 
31 #pragma once
32 #include "P_EventSystem.h"
33 
34 #include "HTTP.h"
35 
36 //              SPECIAL URLs
37 //
38 //
39 // 1. Access from Browsers
40 //
41 //    By special URLS:
42 //
43 //      http://{module}/component/sub-component/request-type?arguments
44 //
45 //    Note how the hostname is the module to be queried with "{}" surrounding.
46 //
47 //    Running Example:
48 //
49 //      http://{http}/groups/dump?comp.compilers
50 //
51 // 2. What sort of things should be available?
52 //
53 //    A. The type of data should default to HTML or match the
54 //       extension type e.g.:
55 //
56 //         http://{http}/groups/use_graph.gif?august
57 //
58 //    B. Each protocol/subsystem should have their own information.
59 //       For example
60 
61 #define STAT_PAGE_SUCCESS STAT_PAGES_EVENTS_START + 0
62 #define STAT_PAGE_FAILURE STAT_PAGES_EVENTS_START + 1
63 
64 typedef Action *(*StatPagesFunc)(Continuation *cont, HTTPHdr *header);
65 
66 struct StatPageData {
67   char *data = nullptr;
68   char *type = nullptr;
69   int length = 0;
70 
StatPageDataStatPageData71   StatPageData() {}
StatPageDataStatPageData72   StatPageData(char *adata) : data(adata) { length = strlen(adata); }
StatPageDataStatPageData73   StatPageData(char *adata, int alength) : data(adata), length(alength) {}
74 };
75 
76 struct StatPagesManager {
77   void init();
78 
79   inkcoreapi void register_http(const char *hostname, StatPagesFunc func);
80 
81   // Private
82   Action *handle_http(Continuation *cont, HTTPHdr *header);
83   bool is_stat_page(URL *url);
84   bool is_cache_inspector_page(URL *url);
85   int m_enabled;
86   ink_mutex stat_pages_mutex;
87 };
88 
89 inkcoreapi extern StatPagesManager statPagesManager;
90 
91 // Stole Pete's code for formatting the page and slapped it here
92 //   for easy reuse
93 class BaseStatPagesHandler : public Continuation
94 {
95 public:
BaseStatPagesHandler(ProxyMutex * amutex)96   BaseStatPagesHandler(ProxyMutex *amutex) : Continuation(amutex), response(nullptr), response_size(0), response_length(0){};
~BaseStatPagesHandler()97   ~BaseStatPagesHandler() override { resp_clear(); };
98 
99 protected:
100   inkcoreapi void resp_clear();
101   inkcoreapi void resp_add(const char *fmt, ...);
102   inkcoreapi void resp_add_sep();
103   inkcoreapi void resp_begin(const char *title);
104   inkcoreapi void resp_end();
105   void resp_begin_numbered();
106   void resp_end_numbered();
107   inkcoreapi void resp_begin_unnumbered();
108   inkcoreapi void resp_end_unnumbered();
109   inkcoreapi void resp_begin_item();
110   void resp_end_item();
111   inkcoreapi void resp_begin_table(int border, int columns, int percent);
112   inkcoreapi void resp_end_table();
113   inkcoreapi void resp_begin_row();
114   inkcoreapi void resp_end_row();
115   inkcoreapi void resp_begin_column(int percent = -1, const char *align = nullptr);
116   inkcoreapi void resp_end_column();
117 
118   char *response;
119   int response_size;
120   int response_length;
121 };
122