33size_t findUserinfoAt(
const wxString& aUrl )
35 size_t protoSep = aUrl.find(
"://" );
37 if( protoSep == wxString::npos )
38 return wxString::npos;
40 size_t hostStart = protoSep + 3;
41 size_t pathStart = aUrl.find_first_of(
"/?#", hostStart );
42 size_t searchEnd = ( pathStart == wxString::npos ) ? aUrl.length() : pathStart;
44 if( searchEnd <= hostStart )
45 return wxString::npos;
47 size_t at = aUrl.rfind(
'@', searchEnd - 1 );
49 return ( at != wxString::npos && at >= hostStart ) ? at : wxString::npos;
54wxString decodePercent(
const wxString& aText )
56 const std::string
text = aText.utf8_string();
59 for(
size_t ii = 0; ii <
text.size(); ++ii )
61 if(
text[ii] ==
'%' && ii + 2 <
text.size() && isxdigit( (
unsigned char)
text[ii + 1] )
62 && isxdigit( (
unsigned char)
text[ii + 2] ) )
64 decoded +=
static_cast<char>( std::stoi(
text.substr( ii + 1, 2 ),
nullptr, 16 ) );
81int parsePort(
const wxString& aPort )
88 if( !aPort.ToLong( &port ) || port <= 0 || port > 65535 )
91 return static_cast<int>( port );
95bool isScpIpv6(
const wxString& aText,
size_t aBracket )
99 for(
size_t ii = aBracket + 1; ii < aText.length(); ++ii )
101 wxUniChar c = aText[ii];
109 if( c !=
':' && !wxIsxdigit( c ) )
117bool scpHasUser(
const wxString& aText,
size_t aStart )
119 for(
size_t ii = aStart; ii < aText.length(); ++ii )
121 if( aText[ii] ==
'@' )
124 if( aText[ii] ==
':' )
133bool parseScpAddress(
const wxString& aURL, wxString& aUser, wxString& aHost, wxString& aPort )
150 size_t user = 0, userLen = 0, host = 0, hostLen = 0, port = 0, portLen = 0;
152 bool havePath =
false;
154 for(
size_t ii = 0; ii < aURL.length() && !havePath; ++ii )
156 wxUniChar c = aURL[ii];
161 if( c ==
'@' || c ==
':' )
166 if( isScpIpv6( aURL, ii ) )
171 else if( bracket++ > 1 )
176 else if( scpHasUser( aURL, ii ) )
199 state = c ==
'[' ? IPV6 : HOST;
207 state = bracket ? PORT_START : PATH_START;
238 state = bracket ? PORT_START : PATH_START;
265 case PATH_START: havePath =
true;
break;
269 if( !havePath || hostLen == 0 )
272 aUser = aURL.Mid( user, userLen );
273 aHost = aURL.Mid( host, hostLen );
274 aPort = aURL.Mid( port, portLen );
277 if( aHost.StartsWith(
"[" ) && aHost.EndsWith(
"]" ) )
278 aHost = aHost.Mid( 1, aHost.length() - 2 );
295 size_t schemeEnd = aURL.find(
"://" );
296 size_t hostStart = 0;
299 if( schemeEnd != wxString::npos )
301 wxString scheme = aURL.Left( schemeEnd ).Lower();
303 if( scheme ==
"https" || scheme ==
"http" )
305 else if( scheme ==
"ssh" || scheme ==
"ssh+git" || scheme ==
"git+ssh" )
310 hostStart = schemeEnd + 3;
311 hostEnd = aURL.find_first_of(
"/?#", hostStart );
312 size_t at = findUserinfoAt( aURL );
314 if( at != wxString::npos )
316 wxString credentials = aURL.Mid( hostStart, at - hostStart );
317 size_t colon = credentials.find(
':' );
318 wxString user = credentials.Left( colon );
319 result.username = decodePercent( user );
321 if( colon != wxString::npos )
322 result.password = decodePercent( credentials.Mid( colon + 1 ) );
327 userPrefix = user +
"@";
329 result.url = aURL.Left( hostStart ) + userPrefix + aURL.Mid( at + 1 );
336 if( aURL.length() >= 2 && wxIsalpha( aURL[0] ) && aURL[1] ==
':' )
339 hostEnd = aURL.find(
':' );
340 size_t bracket = aURL.find(
'[' );
342 if( bracket != wxString::npos && bracket < hostEnd )
344 size_t endBracket = aURL.find(
']', bracket );
346 if( endBracket != wxString::npos )
347 hostEnd = aURL.find(
':', endBracket + 1 );
350 size_t slash = aURL.find_first_of(
"/\\" );
352 if( hostEnd == wxString::npos || hostEnd == 0 || ( slash != wxString::npos && slash < hostEnd ) )
361 if( parseScpAddress( aURL,
result.username,
result.hostname, port ) )
362 result.port = parsePort( port );
367 wxString host = aURL.Mid( hostStart, hostEnd == wxString::npos ? wxString::npos : hostEnd - hostStart );
370 if( host.StartsWith(
"[" ) )
372 result.hostname = host.AfterFirst(
'[' ).BeforeFirst(
']' );
373 port = host.AfterFirst(
']' ).AfterFirst(
':' );
377 result.hostname = host.BeforeFirst(
':' );
378 port = host.AfterFirst(
':' );
383 result.port = parsePort( port );
394 return wxEmptyString;
396 const wxString scheme = aUrl.Contains( wxS(
"://" ) ) ? aUrl.BeforeFirst(
':' ).Lower() : wxString( wxS(
"ssh" ) );
397 const int defaultPort = scheme == wxS(
"https" ) ? 443 : scheme == wxS(
"http" ) ? 80 : 22;
398 const wxString host = parsed.
hostname.Lower();
399 wxString server = host.Contains( wxS(
":" ) ) ? wxS(
"[" ) + host + wxS(
"]" ) : host;
401 if( parsed.
port > 0 && parsed.
port != defaultPort )
402 server << wxS(
":" ) << parsed.
port;
413 return wxEmptyString;
415 return aUrl.BeforeFirst(
':' ).Lower() + wxS(
"://" ) + server;
422 size_t lineStart = 0;
424 result.reserve( aText.length() );
428 size_t lineEnd = aText.find_first_of( wxS(
"\r\n" ), lineStart );
430 if( lineEnd == wxString::npos )
431 lineEnd = aText.length();
435 wxString line = aText.Mid( lineStart, lineEnd - lineStart );
436 const size_t sep = line.find( wxS(
"://" ) );
437 const size_t at = line.rfind(
'@' );
439 if( sep != wxString::npos && at != wxString::npos && at >= sep + 3 )
440 line.erase( sep + 3, at - sep - 2 );
444 if( lineEnd == aText.length() )
448 lineStart = lineEnd + 1;
455 wxString
text = wxString::FromUTF8( aData, aLength );
458 if(
text.IsEmpty() && aLength > 0 )
459 text = wxString::From8BitData( aData, aLength );
467 static thread_local const wxRegEx escapes( wxS(
"\x1B\\[[0-?]*[ -/]*[@-~]" ), wxRE_ADVANCED );
469 wxString
text = aText;
472 text.Replace( wxString( wxUniChar( 0 ) ), wxEmptyString );
476 escapes.ReplaceAll( &
text, wxEmptyString );
480 for( wxUniChar c :
text )
482 const wxUint32 code = c.GetValue();
483 const bool control = code < 0x20 || code == 0x7F || ( code >= 0x80 && code <= 0x9F );
484 const bool bidi = ( code >= 0x202A && code <= 0x202E ) || ( code >= 0x2066 && code <= 0x2069 );
487 printable << wxS(
" " );
488 else if( ( !control && !bidi ) || code ==
'\n' || code ==
'\r' )
@ USER
The main config directory (e.g. ~/.config/kicad/)
wxString PrintableServerText(const wxString &aText)
Text a server sent, made safe to show by removing terminal escapes, control and bidi characters,...
wxString DecodeServerText(const char *aData, size_t aLength)
Bytes a server sent, as UTF-8 or, when they are not valid UTF-8, one character for each byte.
wxString CredentialServer(const wxString &aUrl)
The host and port aUrl reaches, as host[:port] without the scheme's default port.
wxString CredentialRealm(const wxString &aUrl)
The server an HTTP(S) credential for aUrl serves, as scheme://host[:port] with no user,...
REMOTE_URL ParseRemoteURL(const wxString &aURL)
Split a remote address the way libgit2 reads it, so a host key is checked against the host it dials.
wxString StripEmbeddedUserinfo(const wxString &aText)
Remove the user and password from every URL quoted in a message, since the remote's can carry a passw...
A transport address with any embedded credentials extracted.
int port
0 when the address names none.
static std::string decoded(const std::string &aBase64)
wxString result
Test unit parsing edge cases and error handling.