/root/doris/be/src/util/url_coding.cpp
Line | Count | Source (jump to first uncovered line) |
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 "util/url_coding.h" |
19 | | |
20 | | #include <curl/curl.h> |
21 | | #include <libbase64.h> |
22 | | |
23 | | #include <sstream> |
24 | | |
25 | | namespace doris { |
26 | | |
27 | 0 | inline unsigned char to_hex(unsigned char x) { |
28 | 0 | return x + (x > 9 ? ('A' - 10) : '0'); |
29 | 0 | } |
30 | | |
31 | | // Adapted from http://dlib.net/dlib/server/server_http.cpp.html |
32 | 0 | void url_encode(const std::string_view& in, std::string* out) { |
33 | 0 | std::ostringstream os; |
34 | 0 | for (auto c : in) { |
35 | | // impl as https://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html |
36 | 0 | if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || |
37 | 0 | c == '.' || c == '-' || c == '*' || c == '_') { // allowed |
38 | 0 | os << c; |
39 | 0 | } else if (c == ' ') { |
40 | 0 | os << '+'; |
41 | 0 | } else { |
42 | 0 | os << '%' << to_hex(c >> 4) << to_hex(c % 16); |
43 | 0 | } |
44 | 0 | } |
45 | |
|
46 | 0 | *out = os.str(); |
47 | 0 | } |
48 | | |
49 | | // Adapted from |
50 | | // http://www.boost.org/doc/libs/1_40_0/doc/html/boost_asio/ |
51 | | // example/http/server3/request_handler.cpp |
52 | | // See http://www.boost.org/LICENSE_1_0.txt for license for this method. |
53 | 0 | bool url_decode(const std::string& in, std::string* out) { |
54 | 0 | out->clear(); |
55 | 0 | out->reserve(in.size()); |
56 | |
|
57 | 0 | for (size_t i = 0; i < in.size(); ++i) { |
58 | 0 | if (in[i] == '%') { |
59 | 0 | if (i + 3 <= in.size()) { |
60 | 0 | int value = 0; |
61 | 0 | std::istringstream is(in.substr(i + 1, 2)); |
62 | |
|
63 | 0 | if (is >> std::hex >> value) { |
64 | 0 | (*out) += static_cast<char>(value); |
65 | 0 | i += 2; |
66 | 0 | } else { |
67 | 0 | return false; |
68 | 0 | } |
69 | 0 | } else { |
70 | 0 | return false; |
71 | 0 | } |
72 | 0 | } else if (in[i] == '+') { |
73 | 0 | (*out) += ' '; |
74 | 0 | } else { |
75 | 0 | (*out) += in[i]; |
76 | 0 | } |
77 | 0 | } |
78 | | |
79 | 0 | return true; |
80 | 0 | } |
81 | | |
82 | 3 | void base64_encode(const std::string& in, std::string* out) { |
83 | 3 | out->resize(size_t(in.length() * (4.0 / 3) + 1)); |
84 | 3 | auto len = base64_encode(reinterpret_cast<const unsigned char*>(in.c_str()), in.length(), |
85 | 3 | (unsigned char*)out->c_str()); |
86 | 3 | out->resize(len); |
87 | 3 | } |
88 | | |
89 | 40 | size_t base64_encode(const unsigned char* data, size_t length, unsigned char* encoded_data) { |
90 | 40 | size_t encode_len = 0; |
91 | | #if defined(__aarch64__) || defined(_M_ARM64) |
92 | | do_base64_encode(reinterpret_cast<const char*>(data), length, |
93 | | reinterpret_cast<char*>(encoded_data), &encode_len, BASE64_FORCE_NEON64); |
94 | | #else |
95 | 40 | do_base64_encode(reinterpret_cast<const char*>(data), length, |
96 | 40 | reinterpret_cast<char*>(encoded_data), &encode_len, 0); |
97 | 40 | #endif |
98 | 40 | return encode_len; |
99 | 40 | } |
100 | | |
101 | 67 | int64_t base64_decode(const char* data, size_t length, char* decoded_data) { |
102 | 67 | size_t decode_len = 0; |
103 | | #if defined(__aarch64__) || defined(_M_ARM64) |
104 | | auto ret = do_base64_decode(reinterpret_cast<const char*>(data), length, decoded_data, |
105 | | &decode_len, BASE64_FORCE_NEON64); |
106 | | #else |
107 | 67 | auto ret = do_base64_decode(reinterpret_cast<const char*>(data), length, decoded_data, |
108 | 67 | &decode_len, 0); |
109 | 67 | #endif |
110 | 67 | return ret > 0 ? decode_len : -1; |
111 | 67 | } |
112 | | |
113 | 24 | bool base64_decode(const std::string& in, std::string* out) { |
114 | 24 | out->resize(in.length()); |
115 | | |
116 | 24 | int64_t len = base64_decode(in.c_str(), in.length(), out->data()); |
117 | 24 | if (len < 0) { |
118 | 2 | return false; |
119 | 2 | } |
120 | 22 | out->resize(len); |
121 | 22 | return true; |
122 | 24 | } |
123 | | |
124 | 0 | void escape_for_html(const std::string& in, std::stringstream* out) { |
125 | 0 | for (const auto& c : in) { |
126 | 0 | switch (c) { |
127 | 0 | case '<': |
128 | 0 | (*out) << "<"; |
129 | 0 | break; |
130 | | |
131 | 0 | case '>': |
132 | 0 | (*out) << ">"; |
133 | 0 | break; |
134 | | |
135 | 0 | case '&': |
136 | 0 | (*out) << "&"; |
137 | 0 | break; |
138 | | |
139 | 0 | default: |
140 | 0 | (*out) << c; |
141 | 0 | } |
142 | 0 | } |
143 | 0 | } |
144 | | |
145 | 0 | std::string escape_for_html_to_string(const std::string& in) { |
146 | 0 | std::stringstream str; |
147 | 0 | escape_for_html(in, &str); |
148 | 0 | return str.str(); |
149 | 0 | } |
150 | | } // namespace doris |