Coverage Report

Created: 2026-03-12 17:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/service/http/web_page_handler.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <functional>
21
#include <map>
22
#include <mutex>
23
#include <sstream>
24
#include <string>
25
#include <utility>
26
27
#include "service/http/http_handler.h"
28
#include "service/http/http_handler_with_auth.h"
29
30
namespace doris {
31
32
class EvHttpServer;
33
class EasyJson;
34
class HttpRequest;
35
36
// This a handler for webpage request
37
// and this handler manage all the page handler
38
class WebPageHandler : public HttpHandlerWithAuth {
39
public:
40
    typedef std::map<std::string, std::string> ArgumentMap;
41
    typedef std::function<void(const ArgumentMap& args, std::stringstream* output)>
42
            PageHandlerCallback;
43
    typedef std::function<void(const ArgumentMap& args, EasyJson* output)>
44
            TemplatePageHandlerCallback;
45
46
    WebPageHandler(EvHttpServer* http_server, ExecEnv* exec_env);
47
    virtual ~WebPageHandler();
48
49
    void handle(HttpRequest* req) override;
50
51
    // Register a route 'path' to be rendered via template.
52
    // The appropriate template to use is determined by 'path'.
53
    // If 'is_on_nav_bar' is true, a link to the page will be placed on the navbar
54
    // in the header of styled pages. The link text is given by 'alias'.
55
    void register_template_page(const std::string& path, const std::string& alias,
56
                                const TemplatePageHandlerCallback& callback, bool is_on_nav_bar);
57
58
    // Register a route 'path'. See the register_template_page for details.
59
    void register_page(const std::string& path, const std::string& alias,
60
                       const PageHandlerCallback& callback, bool is_on_nav_bar);
61
62
private:
63
    void root_handler(const ArgumentMap& args, EasyJson* output);
64
65
    // Returns a mustache tag that renders the partial at path when
66
    // passed to mustache::RenderTemplate.
67
    std::string mustache_partial_tag(const std::string& path) const;
68
69
    // Returns whether or not a mustache template corresponding
70
    // to the given path can be found.
71
    bool mustache_template_available(const std::string& path) const;
72
73
    // Renders the main HTML template with the pre-rendered string 'content'
74
    // in the main body of the page, into 'output'.
75
    void render_main_template(const std::string& content, std::stringstream* output);
76
77
    // Renders the template corresponding to 'path' (if available), using
78
    // fields in 'ej'.
79
    void render(const std::string& path, const EasyJson& ej, bool use_style,
80
                std::stringstream* output);
81
82
    bool static_pages_available() const;
83
84
    // Container class for a list of path handler callbacks for a single URL.
85
    class PathHandler {
86
    public:
87
        PathHandler(bool is_styled, bool is_on_nav_bar, std::string alias,
88
                    PageHandlerCallback callback)
89
63
                : is_styled_(is_styled),
90
63
                  is_on_nav_bar_(is_on_nav_bar),
91
63
                  alias_(std::move(alias)),
92
63
                  callback_(std::move(callback)) {}
93
94
0
        bool is_styled() const { return is_styled_; }
95
0
        bool is_on_nav_bar() const { return is_on_nav_bar_; }
96
0
        const std::string& alias() const { return alias_; }
97
0
        const PageHandlerCallback& callback() const { return callback_; }
98
99
    private:
100
        // If true, the page appears is rendered styled.
101
        bool is_styled_;
102
103
        // If true, the page appears in the navigation bar.
104
        bool is_on_nav_bar_;
105
106
        // Alias used when displaying this link on the nav bar.
107
        std::string alias_;
108
109
        // Callback to render output for this page.
110
        PageHandlerCallback callback_;
111
    };
112
113
    std::string _www_path;
114
    EvHttpServer* _http_server;
115
    // Lock guarding the _path_handlers map
116
    std::mutex _map_lock;
117
    // Map of path to a PathHandler containing a list of handlers for that
118
    // path. More than one handler may register itself with a path so that many
119
    // components may contribute to a single page.
120
    typedef std::map<std::string, PathHandler*> PageHandlersMap;
121
    PageHandlersMap _page_map;
122
};
123
124
} // namespace doris