Coverage Report

Created: 2024-11-21 14:31

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