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 */ 66 class WebServer 67 { 68 public: 69 /** 70 * This method is constructor to initialize the WebServer. 71 * 72 */ 73 WebServer(void); 74 75 /** 76 * This method is destructor to free the WebServer. 77 * 78 */ 79 ~WebServer(void); 80 81 /** 82 * This method starts the Web Server. 83 * 84 * @param[in] aIfName The pointer to the Thread interface name. 85 * @param[in] aListenAddr The http server listen address, can be nullptr for any address. 86 * @param[in] aPort The port of http server. 87 * 88 */ 89 void StartWebServer(const char *aIfName, const char *aListenAddr, uint16_t aPort); 90 91 /** 92 * This method stops the Web Server. 93 * 94 */ 95 void StopWebServer(void); 96 97 private: 98 typedef std::string (*HttpRequestCallback)(const std::string &aRequest, void *aUserData); 99 static std::string HandleJoinNetworkRequest(const std::string &aJoinRequest, void *aUserData); 100 static std::string HandleGetQRCodeRequest(const std::string &aGetQRCodeRequest, void *aUserData); 101 static std::string HandleFormNetworkRequest(const std::string &aFormRequest, void *aUserData); 102 static std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest, void *aUserData); 103 static std::string HandleDeletePrefixRequest(const std::string &aDeletePrefixRequest, void *aUserData); 104 static std::string HandleGetStatusRequest(const std::string &aGetStatusRequest, void *aUserData); 105 static std::string HandleGetAvailableNetworkResponse(const std::string &aGetAvailableNetworkRequest, 106 void *aUserData); 107 static std::string HandleCommission(const std::string &aCommissionRequest, void *aUserData); 108 109 std::string HandleJoinNetworkRequest(const std::string &aJoinRequest); 110 std::string HandleGetQRCodeRequest(const std::string &aGetQRCodeRequest); 111 std::string HandleFormNetworkRequest(const std::string &aFormRequest); 112 std::string HandleAddPrefixRequest(const std::string &aAddPrefixRequest); 113 std::string HandleDeletePrefixRequest(const std::string &aDeletePrefixRequest); 114 std::string HandleGetStatusRequest(const std::string &aGetStatusRequest); 115 std::string HandleGetAvailableNetworkResponse(const std::string &aGetAvailableNetworkRequest); 116 std::string HandleCommission(const std::string &aCommissionRequest); 117 118 void HandleHttpRequest(const char *aUrl, const char *aMethod, HttpRequestCallback aCallback); 119 void ResponseGetQRCode(void); 120 void ResponseJoinNetwork(void); 121 void ResponseFormNetwork(void); 122 void ResponseAddOnMeshPrefix(void); 123 void ResponseDeleteOnMeshPrefix(void); 124 void ResponseGetStatus(void); 125 void ResponseGetAvailableNetwork(void); 126 void DefaultHttpResponse(void); 127 void ResponseCommission(void); 128 129 void Init(void); 130 131 HttpServer *mServer; 132 otbr::Web::WpanService mWpanService; 133 }; 134 135 } // namespace Web 136 } // namespace otbr 137 138 #endif // OTBR_WEB_WEB_SERVICE_WEB_SERVER_ 139