KiCad PCB EDA Suite
Loading...
Searching...
No Matches
remote_provider_utils.cpp
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
21
22#include <wx/intl.h>
23#include <wx/uri.h>
24
25
26wxString UrlEncode( const wxString& aValue )
27{
28 wxString encoded;
29 const wxScopedCharBuffer utf8 = aValue.ToUTF8();
30 const char* p = utf8.data();
31 size_t len = utf8.length();
32
33 for( size_t i = 0; i < len; ++i )
34 {
35 unsigned char byte = static_cast<unsigned char>( p[i] );
36
37 if( ( byte >= 'A' && byte <= 'Z' ) || ( byte >= 'a' && byte <= 'z' )
38 || ( byte >= '0' && byte <= '9' ) || byte == '-' || byte == '_'
39 || byte == '.' || byte == '~' )
40 {
41 encoded.Append( static_cast<char>( byte ) );
42 }
43 else
44 {
45 encoded.Append( wxString::Format( wxS( "%%%02X" ), byte ) );
46 }
47 }
48
49 return encoded;
50}
51
52
53wxString RemoteProviderJsonString( const nlohmann::json& aObject, const char* aKey )
54{
55 auto it = aObject.find( aKey );
56
57 if( it != aObject.end() && it->is_string() )
58 return wxString::FromUTF8( it->get_ref<const std::string&>().c_str() );
59
60 return wxString();
61}
62
63
64bool IsLoopbackHost( const wxString& aHost )
65{
66 wxString host = aHost.Lower();
67
68 if( host.StartsWith( wxS( "[" ) ) && host.EndsWith( wxS( "]" ) ) )
69 {
70 host.RemoveLast();
71 host.Remove( 0, 1 );
72 }
73
74 return host == wxS( "localhost" ) || host == wxS( "127.0.0.1" ) || host == wxS( "::1" );
75}
76
77
78bool ValidateRemoteUrlSecurity( const wxString& aUrl, bool aAllowInsecureLocalhost,
79 wxString& aError, const wxString& aLabel )
80{
81 if( aUrl.IsEmpty() )
82 return true;
83
84 wxURI uri( aUrl );
85 wxString scheme = uri.GetScheme().Lower();
86
87 if( scheme == wxS( "https" ) )
88 return true;
89
90 if( scheme == wxS( "http" ) && aAllowInsecureLocalhost && IsLoopbackHost( uri.GetServer() ) )
91 return true;
92
93 aError = wxString::Format(
94 _( "%s must use HTTPS unless allow_insecure_localhost is enabled for a loopback URL." ),
95 aLabel );
96 return false;
97}
98
99
100wxString NormalizedUrlOrigin( const wxString& aUrl )
101{
102 if( aUrl.IsEmpty() )
103 return wxString();
104
105 wxURI uri( aUrl );
106
107 if( !uri.HasScheme() || !uri.HasServer() )
108 return wxString();
109
110 wxString scheme = uri.GetScheme().Lower();
111 wxString host = uri.GetServer().Lower();
112 wxString port = uri.GetPort();
113
114 if( port.IsEmpty() )
115 {
116 if( scheme == wxS( "https" ) )
117 port = wxS( "443" );
118 else if( scheme == wxS( "http" ) )
119 port = wxS( "80" );
120 }
121
122 return scheme + wxS( "://" ) + host + wxS( ":" ) + port;
123}
124
125
126void COLLECTING_JSON_ERROR_HANDLER::error( const nlohmann::json::json_pointer& aPointer,
127 const nlohmann::json& aInstance,
128 const std::string& aMessage )
129{
130 wxUnusedVar( aInstance );
131 m_errors.emplace_back( wxString::Format( wxS( "%s: %s" ),
132 wxString::FromUTF8( aPointer.to_string().c_str() ),
133 wxString::FromUTF8( aMessage.c_str() ) ) );
134}
135
136
138{
139 if( m_errors.empty() )
140 return wxString();
141
142 return m_errors.front();
143}
void error(const nlohmann::json::json_pointer &aPointer, const nlohmann::json &aInstance, const std::string &aMessage) override
#define _(s)
wxString NormalizedUrlOrigin(const wxString &aUrl)
Return a normalized scheme://host:port origin string for aUrl.
wxString UrlEncode(const wxString &aValue)
Percent-encode a string for use in URL query parameters (RFC 3986 unreserved characters are passed th...
wxString RemoteProviderJsonString(const nlohmann::json &aObject, const char *aKey)
Extract an optional string value from a JSON object, returning an empty wxString when the key is abse...
bool IsLoopbackHost(const wxString &aHost)
Return true when aHost resolves to a loopback address (localhost, 127.0.0.1, or ::1).
bool ValidateRemoteUrlSecurity(const wxString &aUrl, bool aAllowInsecureLocalhost, wxString &aError, const wxString &aLabel)
Validate that aUrl uses HTTPS, or HTTP on a loopback address when aAllowInsecureLocalhost is true.