1  
//
1  
//
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/http
7  
// Official repository: https://github.com/cppalliance/http
8  
//
8  
//
9  

9  

10  
#include <boost/http/server/escape_html.hpp>
10  
#include <boost/http/server/escape_html.hpp>
11  

11  

12  
namespace boost {
12  
namespace boost {
13  
namespace http {
13  
namespace http {
14  

14  

15  
std::string
15  
std::string
16  
escape_html( core::string_view s )
16  
escape_html( core::string_view s )
17  
{
17  
{
18  
    std::string result;
18  
    std::string result;
19  
    result.reserve( s.size() );
19  
    result.reserve( s.size() );
20  

20  

21  
    for( char c : s )
21  
    for( char c : s )
22  
    {
22  
    {
23  
        switch( c )
23  
        switch( c )
24  
        {
24  
        {
25  
        case '&':
25  
        case '&':
26  
            result.append( "&amp;" );
26  
            result.append( "&amp;" );
27  
            break;
27  
            break;
28  
        case '<':
28  
        case '<':
29  
            result.append( "&lt;" );
29  
            result.append( "&lt;" );
30  
            break;
30  
            break;
31  
        case '>':
31  
        case '>':
32  
            result.append( "&gt;" );
32  
            result.append( "&gt;" );
33  
            break;
33  
            break;
34  
        case '"':
34  
        case '"':
35  
            result.append( "&quot;" );
35  
            result.append( "&quot;" );
36  
            break;
36  
            break;
37  
        case '\'':
37  
        case '\'':
38  
            result.append( "&#39;" );
38  
            result.append( "&#39;" );
39  
            break;
39  
            break;
40  
        default:
40  
        default:
41  
            result.push_back( c );
41  
            result.push_back( c );
42  
            break;
42  
            break;
43  
        }
43  
        }
44  
    }
44  
    }
45  

45  

46  
    return result;
46  
    return result;
47  
}
47  
}
48  

48  

49  
} // http
49  
} // http
50  
} // boost
50  
} // boost