|
| 1 | +/* |
| 2 | + Copyright The Overlaybd Authors |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#include <photon/net/http/server.h> |
| 18 | +#include <photon/net/socket.h> |
| 19 | +#include <photon/net/http/url.h> |
| 20 | +#include <map> |
| 21 | +#include <string> |
| 22 | +#include <string_view> |
| 23 | +#include "image_service.h" |
| 24 | +#include "image_file.h" |
| 25 | +#include "api_server.h" |
| 26 | + |
| 27 | +class ApiHandler : public photon::net::http::HTTPHandler { |
| 28 | +public: |
| 29 | + ImageService *imgservice; |
| 30 | + std::map<std::string, std::string> params; |
| 31 | + |
| 32 | + ApiHandler(ImageService *imgservice) : imgservice(imgservice) {} |
| 33 | + int handle_request(photon::net::http::Request& req, |
| 34 | + photon::net::http::Response& resp, |
| 35 | + std::string_view) override { |
| 36 | + auto target = req.target(); // string view, format: /snapshot?dev_id=${devID}&config=${config} |
| 37 | + std::string_view query(""); |
| 38 | + auto pos = target.find('?'); |
| 39 | + if (pos != std::string_view::npos) { |
| 40 | + query = target.substr(pos + 1); |
| 41 | + } |
| 42 | + // auto query = req.query(); |
| 43 | + LOG_INFO("Snapshot query: `", query); // string view, format: dev_id=${devID}&config=${config} |
| 44 | + parse_params(query); |
| 45 | + auto dev_id = params["dev_id"]; |
| 46 | + auto config_path = params["config"]; |
| 47 | + LOG_DEBUG("dev_id: `, config: `", dev_id, config_path); |
| 48 | + |
| 49 | + int code; |
| 50 | + std::string msg; |
| 51 | + ImageFile* img_file = nullptr; |
| 52 | + |
| 53 | + if (dev_id.empty() || config_path.empty()) { |
| 54 | + code = 400; |
| 55 | + msg = std::string(R"delimiter({ |
| 56 | + "success": false, |
| 57 | + "message": "Missing dev_id or config in snapshot request" |
| 58 | +})delimiter"); |
| 59 | + goto EXIT; |
| 60 | + } |
| 61 | + |
| 62 | + img_file = imgservice->find_image_file(dev_id); |
| 63 | + if (!img_file) { |
| 64 | + code = 404; |
| 65 | + msg = std::string(R"delimiter({ |
| 66 | + "success": false, |
| 67 | + "message": "Image file not found" |
| 68 | +})delimiter"); |
| 69 | + goto EXIT; |
| 70 | + } |
| 71 | + |
| 72 | + if (img_file->create_snapshot(config_path.c_str()) < 0) { |
| 73 | + code = 500; |
| 74 | + msg = std::string(R"delimiter({ |
| 75 | + "success": false, |
| 76 | + "message": "Failed to create snapshot`" |
| 77 | +})delimiter"); |
| 78 | + goto EXIT; |
| 79 | + } |
| 80 | + |
| 81 | + code = 200; |
| 82 | + msg = std::string(R"delimiter({ |
| 83 | + "success": true, |
| 84 | + "message": "Snapshot created successfully" |
| 85 | +})delimiter"); |
| 86 | + |
| 87 | +EXIT: |
| 88 | + resp.set_result(code); |
| 89 | + resp.headers.content_length(msg.size()); |
| 90 | + resp.keep_alive(true); |
| 91 | + auto ret_w = resp.write((void*)msg.c_str(), msg.size()); |
| 92 | + if (ret_w != (ssize_t)msg.size()) { |
| 93 | + LOG_ERRNO_RETURN(0, -1, "send body failed, target: `, `", req.target(), VALUE(ret_w)); |
| 94 | + } |
| 95 | + LOG_DEBUG("send body done"); |
| 96 | + return 0; |
| 97 | + } |
| 98 | + |
| 99 | + void parse_params(std::string_view query) { // format: dev_id=${devID}&config=${config}... |
| 100 | + if (query.empty()) |
| 101 | + return; |
| 102 | + |
| 103 | + size_t start = 0; |
| 104 | + while (start < query.length()) { |
| 105 | + auto end = query.find('&', start); |
| 106 | + if (end == std::string_view::npos) { // last one |
| 107 | + end = query.length(); |
| 108 | + } |
| 109 | + |
| 110 | + auto param = query.substr(start, end - start); |
| 111 | + auto eq_pos = param.find('='); |
| 112 | + if (eq_pos != std::string_view::npos) { |
| 113 | + auto key = param.substr(0, eq_pos); |
| 114 | + auto value = param.substr(eq_pos + 1); |
| 115 | + |
| 116 | + // url decode |
| 117 | + auto decoded_key = photon::net::http::url_unescape(key); |
| 118 | + auto decoded_value = photon::net::http::url_unescape(value); |
| 119 | + params[decoded_key] = decoded_value; |
| 120 | + } else { |
| 121 | + // key without value |
| 122 | + auto key = photon::net::http::url_unescape(param); |
| 123 | + params[key] = ""; |
| 124 | + } |
| 125 | + start = end + 1; |
| 126 | + } |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +struct ApiServer { |
| 131 | + photon::net::ISocketServer* tcpserver = nullptr; |
| 132 | + photon::net::http::HTTPServer* httpserver = nullptr; |
| 133 | + ApiHandler* handler = nullptr; |
| 134 | + |
| 135 | + ApiServer(ImageService *imgservice) : handler(new ApiHandler(imgservice)) {} |
| 136 | + |
| 137 | + ~ApiServer() { |
| 138 | + delete handler; |
| 139 | + if(tcpserver) { |
| 140 | + safe_delete(tcpserver); |
| 141 | + } |
| 142 | + if(httpserver) { |
| 143 | + safe_delete(httpserver); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + int init(const std::string &addr) { |
| 148 | + photon::net::http::URL url(addr); |
| 149 | + std::string host = url.host().data(); // the string pointed by data() doesn't end up with '\0' |
| 150 | + auto pos = host.find(":"); |
| 151 | + if (pos != host.npos) { |
| 152 | + host.resize(pos); |
| 153 | + } |
| 154 | + tcpserver = photon::net::new_tcp_socket_server(); |
| 155 | + tcpserver->setsockopt(SOL_SOCKET, SO_REUSEPORT, 1); |
| 156 | + if(tcpserver->bind(url.port(), photon::net::IPAddr(host.c_str())) < 0) |
| 157 | + LOG_ERRNO_RETURN(0, -1, "Failed to bind api server port `", url.port()); |
| 158 | + if(tcpserver->listen() < 0) |
| 159 | + LOG_ERRNO_RETURN(0, -1, "Failed to listen api server port `", url.port()); |
| 160 | + httpserver = photon::net::http::new_http_server(); |
| 161 | + httpserver->add_handler(handler, false, "/snapshot"); |
| 162 | + tcpserver->set_handler(httpserver->get_connection_handler()); |
| 163 | + tcpserver->start_loop(); |
| 164 | + LOG_DEBUG("Api server listening on `:`, path: `", host, url.port(), "/snapshot"); |
| 165 | + return 0; |
| 166 | + } |
| 167 | +}; |
| 168 | + |
| 169 | +int start_api_server(ApiServer *&api_server , ImageService *imgservice, const std::string &addr) { |
| 170 | + api_server = new ApiServer(imgservice); |
| 171 | + return api_server->init(addr); |
| 172 | +} |
| 173 | + |
| 174 | +int stop_api_server(ApiServer *api_server) { |
| 175 | + safe_delete(api_server); |
| 176 | +} |
0 commit comments