xref: /aosp_15_r20/external/ot-br-posix/src/web/web-service/web_server.hpp (revision 4a64e381480ef79f0532b2421e44e6ee336b8e0d)
1 /*
2  *  Copyright (c) 2017, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements the web server of border router
32  */
33 
34 #ifndef OTBR_WEB_WEB_SERVICE_WEB_SERVER_
35 #define OTBR_WEB_WEB_SERVICE_WEB_SERVER_
36 
37 #include "openthread-br/config.h"
38 
39 #include <algorithm>
40 #include <fstream>
41 #include <iostream>
42 #include <string>
43 #include <vector>
44 
45 #include <net/if.h>
46 #include <syslog.h>
47 
48 #include <boost/asio/ip/tcp.hpp>
49 
50 #include "web/web-service/wpan_service.hpp"
51 
52 namespace SimpleWeb {
53 template <class T> class Server;
54 typedef boost::asio::ip::tcp::socket HTTP;
55 } // namespace SimpleWeb
56 
57 namespace otbr {
58 namespace Web {
59 
60 typedef SimpleWeb::Server<SimpleWeb::HTTP> HttpServer;
61 
62 /**
63  * This class implements the http server.
64  */
65 class WebServer
66 {
67 public:
68     /**
69      * This method is constructor to initialize the WebServer.
70      */
71     WebServer(void);
72 
73     /**
74      * This method is destructor to free the WebServer.
75      */
76     ~WebServer(void);
77 
78     /**
79      * This method starts the Web Server.
80      *
81      * @param[in] aIfName      The pointer to the Thread interface name.
82      * @param[in] aListenAddr  The http server listen address, can be nullptr for any address.
83      * @param[in] aPort        The port of http server.
84      */
85     void StartWebServer(const char *aIfName, const char *aListenAddr, uint16_t aPort);
86 
87     /**
88      * This method stops the Web Server.
89      */
90     void StopWebServer(void);
91 
92 private:
93     typedef std::string (*HttpRequestCallback)(const std::string &aRequest, void *aUserData);
94     static std::string HandleJoinNetworkRequest(const std::string &aJoinRequest, void *aUserData);
95     static std::string HandleGetQRCodeRequest(const std::string &aGetQRCodeRequest, void *aUserData);
96     static std::string HandleFormNetworkRequest(const std::string &aFormRequest, void *aUserData);
97     static std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest, void *aUserData);
98     static std::string HandleDeletePrefixRequest(const std::string &aDeletePrefixRequest, void *aUserData);
99     static std::string HandleGetStatusRequest(const std::string &aGetStatusRequest, void *aUserData);
100     static std::string HandleGetAvailableNetworkResponse(const std::string &aGetAvailableNetworkRequest,
101                                                          void              *aUserData);
102     static std::string HandleCommission(const std::string &aCommissionRequest, void *aUserData);
103 
104     std::string HandleJoinNetworkRequest(const std::string &aJoinRequest);
105     std::string HandleGetQRCodeRequest(const std::string &aGetQRCodeRequest);
106     std::string HandleFormNetworkRequest(const std::string &aFormRequest);
107     std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest);
108     std::string HandleDeletePrefixRequest(const std::string &aDeletePrefixRequest);
109     std::string HandleGetStatusRequest(const std::string &aGetStatusRequest);
110     std::string HandleGetAvailableNetworkResponse(const std::string &aGetAvailableNetworkRequest);
111     std::string HandleCommission(const std::string &aCommissionRequest);
112 
113     void HandleHttpRequest(const char *aUrl, const char *aMethod, HttpRequestCallback aCallback);
114     void ResponseGetQRCode(void);
115     void ResponseJoinNetwork(void);
116     void ResponseFormNetwork(void);
117     void ResponseAddOnMeshPrefix(void);
118     void ResponseDeleteOnMeshPrefix(void);
119     void ResponseGetStatus(void);
120     void ResponseGetAvailableNetwork(void);
121     void DefaultHttpResponse(void);
122     void ResponseCommission(void);
123 
124     void Init(void);
125 
126     HttpServer            *mServer;
127     otbr::Web::WpanService mWpanService;
128 };
129 
130 } // namespace Web
131 } // namespace otbr
132 
133 #endif // OTBR_WEB_WEB_SERVICE_WEB_SERVER_
134