/root/doris/be/src/http/http_channel.cpp
| 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 |  | #include "http/http_channel.h" | 
| 19 |  |  | 
| 20 |  | #include <absl/strings/str_split.h> | 
| 21 |  | #include <event2/buffer.h> | 
| 22 |  | #include <event2/bufferevent.h> | 
| 23 |  | #include <event2/http.h> | 
| 24 |  | #include <event2/http_struct.h> | 
| 25 |  |  | 
| 26 |  | #include <sstream> | 
| 27 |  | #include <string> | 
| 28 |  | #include <vector> | 
| 29 |  |  | 
| 30 |  | #include "common/logging.h" | 
| 31 |  | #include "common/status.h" | 
| 32 |  | #include "http/http_headers.h" | 
| 33 |  | #include "http/http_request.h" | 
| 34 |  | #include "http/http_status.h" | 
| 35 |  | #include "util/slice.h" | 
| 36 |  | #include "util/zlib.h" | 
| 37 |  |  | 
| 38 |  | namespace doris { | 
| 39 |  |  | 
| 40 |  | // Send Unauthorized status with basic challenge | 
| 41 | 1 | void HttpChannel::send_basic_challenge(HttpRequest* req, const std::string& realm) { | 
| 42 | 1 |     static std::string s_prompt_str = "Please provide your userid and password\n"; | 
| 43 | 1 |     std::stringstream ss; | 
| 44 | 1 |     ss << "Basic realm=\"" << realm << "\""; | 
| 45 | 1 |     req->add_output_header(HttpHeaders::WWW_AUTHENTICATE, ss.str().c_str()); | 
| 46 | 1 |     send_reply(req, HttpStatus::UNAUTHORIZED, s_prompt_str); | 
| 47 | 1 | } | 
| 48 |  |  | 
| 49 | 2 | void HttpChannel::send_error(HttpRequest* request, HttpStatus status) { | 
| 50 | 2 |     evhttp_send_error(request->get_evhttp_request(), status, default_reason(status).c_str()); | 
| 51 | 2 | } | 
| 52 |  |  | 
| 53 | 28 | void HttpChannel::send_reply(HttpRequest* request, HttpStatus status) { | 
| 54 | 28 |     evhttp_send_reply(request->get_evhttp_request(), status, default_reason(status).c_str(), | 
| 55 | 28 |                       nullptr); | 
| 56 | 28 | } | 
| 57 |  |  | 
| 58 | 24 | void HttpChannel::send_reply(HttpRequest* request, HttpStatus status, const std::string& content) { | 
| 59 | 24 |     auto* evb = evbuffer_new(); | 
| 60 | 24 |     std::string compressed_content; | 
| 61 | 24 |     if (compress_content(request->header(HttpHeaders::ACCEPT_ENCODING), content, | 
| 62 | 24 |                          &compressed_content)) { | 
| 63 | 0 |         request->add_output_header(HttpHeaders::CONTENT_ENCODING, "gzip"); | 
| 64 | 0 |         evbuffer_add(evb, compressed_content.c_str(), compressed_content.size()); | 
| 65 | 24 |     } else { | 
| 66 | 24 |         evbuffer_add(evb, content.c_str(), content.size()); | 
| 67 | 24 |     } | 
| 68 | 24 |     evhttp_send_reply(request->get_evhttp_request(), status, default_reason(status).c_str(), evb); | 
| 69 | 24 |     evbuffer_free(evb); | 
| 70 | 24 | } | 
| 71 |  |  | 
| 72 |  | void HttpChannel::send_file(HttpRequest* request, int fd, size_t off, size_t size, | 
| 73 | 1 |                             bufferevent_rate_limit_group* rate_limit_group) { | 
| 74 | 1 |     auto* evb = evbuffer_new(); | 
| 75 | 1 |     evbuffer_add_file(evb, fd, off, size); | 
| 76 | 1 |     auto* evhttp_request = request->get_evhttp_request(); | 
| 77 | 1 |     if (rate_limit_group) { | 
| 78 | 0 |         auto* evhttp_connection = evhttp_request_get_connection(evhttp_request); | 
| 79 | 0 |         auto* buffer_event = evhttp_connection_get_bufferevent(evhttp_connection); | 
| 80 | 0 |         bufferevent_add_to_rate_limit_group(buffer_event, rate_limit_group); | 
| 81 | 0 |     } | 
| 82 | 1 |     evhttp_send_reply(evhttp_request, HttpStatus::OK, default_reason(HttpStatus::OK).c_str(), evb); | 
| 83 | 1 |     evbuffer_free(evb); | 
| 84 | 1 | } | 
| 85 |  |  | 
| 86 |  | void HttpChannel::send_files(HttpRequest* request, const std::string& root_dir, | 
| 87 |  |                              std::vector<std::string> local_files, | 
| 88 | 0 |                              bufferevent_rate_limit_group* rate_limit_group) { | 
| 89 | 0 |     if (rate_limit_group) { | 
| 90 | 0 |         auto* evhttp_request = request->get_evhttp_request(); | 
| 91 | 0 |         auto* evhttp_connection = evhttp_request_get_connection(evhttp_request); | 
| 92 | 0 |         auto* buffer_event = evhttp_connection_get_bufferevent(evhttp_connection); | 
| 93 | 0 |         bufferevent_add_to_rate_limit_group(buffer_event, rate_limit_group); | 
| 94 | 0 |     } | 
| 95 |  | 
 | 
| 96 | 0 |     send_files(request, root_dir, std::move(local_files)); | 
| 97 | 0 | } | 
| 98 |  |  | 
| 99 |  | void HttpChannel::send_files(HttpRequest* request, const std::string& root_dir, | 
| 100 | 1 |                              std::vector<std::string> local_files) { | 
| 101 | 1 |     std::unique_ptr<evbuffer, decltype(&evbuffer_free)> evb(evbuffer_new(), &evbuffer_free); | 
| 102 | 35 |     for (const std::string& file : local_files) { | 
| 103 | 35 |         std::string file_path = fmt::format("{}/{}", root_dir, file); | 
| 104 | 35 |         int fd = open(file_path.c_str(), O_RDONLY); | 
| 105 | 35 |         if (fd < 0) { | 
| 106 | 0 |             std::string error_msg = "Failed to open file: " + file_path; | 
| 107 | 0 |             LOG(WARNING) << "http channel send files: " << error_msg; | 
| 108 | 0 |             HttpChannel::send_reply(request, HttpStatus::NOT_FOUND, error_msg); | 
| 109 | 0 |             return; | 
| 110 | 0 |         } | 
| 111 | 35 |         struct stat st; | 
| 112 | 35 |         auto res = fstat(fd, &st); | 
| 113 | 35 |         if (res < 0) { | 
| 114 | 0 |             close(fd); | 
| 115 | 0 |             std::string error_msg = "Failed to open file: " + file_path; | 
| 116 | 0 |             LOG(WARNING) << "http channel send files: " << error_msg; | 
| 117 | 0 |             HttpChannel::send_reply(request, HttpStatus::NOT_FOUND, error_msg); | 
| 118 | 0 |             return; | 
| 119 | 0 |         } | 
| 120 |  |  | 
| 121 | 35 |         int64_t file_size = st.st_size; | 
| 122 | 35 |         VLOG_DEBUG << "http channel send file " << file_path << ", size: " << file_size; | 
| 123 |  |  | 
| 124 | 35 |         evbuffer_add_printf(evb.get(), "File-Name: %s\r\n", file.c_str()); | 
| 125 | 35 |         evbuffer_add_printf(evb.get(), "Content-Length: %" PRIi64 "\r\n", file_size); | 
| 126 |  |  | 
| 127 | 35 |         evbuffer_add_printf(evb.get(), "\r\n"); | 
| 128 | 35 |         if (file_size > 0) { | 
| 129 | 33 |             evbuffer_add_file(evb.get(), fd, 0, file_size); | 
| 130 | 33 |         } | 
| 131 | 35 |     } | 
| 132 |  |  | 
| 133 | 1 |     evhttp_send_reply(request->get_evhttp_request(), HttpStatus::OK, | 
| 134 | 1 |                       default_reason(HttpStatus::OK).c_str(), evb.get()); | 
| 135 | 1 | } | 
| 136 |  |  | 
| 137 |  | bool HttpChannel::compress_content(const std::string& accept_encoding, const std::string& input, | 
| 138 | 29 |                                    std::string* output) { | 
| 139 |  |     // Don't bother compressing empty content. | 
| 140 | 29 |     if (input.empty()) { | 
| 141 | 2 |         return false; | 
| 142 | 2 |     } | 
| 143 |  |  | 
| 144 |  |     // Check if gzip compression is accepted by the caller. If so, compress the | 
| 145 |  |     // content and replace the prerendered output. | 
| 146 | 27 |     bool is_compressed = false; | 
| 147 | 27 |     std::vector<std::string> encodings = absl::StrSplit(accept_encoding, ","); | 
| 148 | 28 |     for (auto& encoding : encodings) { | 
| 149 | 28 |         absl::StripAsciiWhitespace(&encoding); | 
| 150 | 28 |         if (encoding == "gzip") { | 
| 151 | 2 |             std::ostringstream oss; | 
| 152 | 2 |             Status s = zlib::CompressLevel(Slice(input), 1, &oss); | 
| 153 | 2 |             if (s.ok()) { | 
| 154 | 2 |                 *output = oss.str(); | 
| 155 | 2 |                 is_compressed = true; | 
| 156 | 2 |             } else { | 
| 157 |  |                 LOG(WARNING) << "Could not compress output: " << s.to_string(); | 
| 158 | 0 |             } | 
| 159 | 2 |             break; | 
| 160 | 2 |         } | 
| 161 | 28 |     } | 
| 162 | 27 |     return is_compressed; | 
| 163 | 29 | } | 
| 164 |  |  | 
| 165 |  | } // namespace doris |