Properly escapes an authorization string. The string can be quoted if
desired.

@param auth  authorization string, as UTF-8 encoded bytes
@param quote true to wrap escaped authorization in quotes
@return escaped authorization string
11168
public static byte[] escape(byte[] auth, boolean quote) {
        int escapeCount = 0;

        for (int i = 0; i < auth.length; i++) {
            if (auth[i] == '"' || auth[i] == '\\') {
                escapeCount++;
            }
        }

        if (escapeCount > 0 || quote) {
            byte[] escapedAuth = new byte[auth.length + escapeCount + (quote ? 2 : 0)];
            int index = quote ? 1 : 0;
            for (int i = 0; i < auth.length; i++) {
                if (auth[i] == '"' || auth[i] == '\\') {
                    escapedAuth[index++] = '\\';
                }
                escapedAuth[index++] = auth[i];
            }

            if (quote) {
                escapedAuth[0] = '"';
                escapedAuth[escapedAuth.length - 1] = '"';
            }

            auth = escapedAuth;
        }
        return auth;
    }
77629
def encode(self, string):
        
        if isinstance(string, unicode):
            string = string.encode("utf-8")
        return quote(string, safe="~")
808203
private String readQuote( char quote ) {
        StringBuilder builder = cachesBuilder;
        builder.setLength( 0 );
        readQuote( quote, builder );
        String str = builder.toString();
        builder.setLength( 0 );
        return str;
    }
405626
private String fixupAuthority(String uriAuthority, String charset) throws URIException {
        // Lowercase the host part of the uriAuthority; don't destroy any
        // userinfo capitalizations.  Make sure no illegal characters in
        // domainlabel substring of the uri authority.
        if (uriAuthority != null) {
            // Get rid of any trailing escaped spaces:
            // http://www.archive.org%20.  Rare but happens.
            // TODO: reevaluate: do IE or firefox do such mid-URI space-removal?
            // if not, we shouldn't either. 
            while(uriAuthority.endsWith(ESCAPED_SPACE)) {
                uriAuthority = uriAuthority.substring(0,uriAuthority.length()-3);
            }

            // lowercase & IDN-punycode only the domain portion
            int atIndex = uriAuthority.indexOf(COMMERCIAL_AT);
            int portColonIndex = uriAuthority.indexOf(COLON,(atIndex<0)?0:atIndex);
            if(atIndex<0 && portColonIndex<0) {
                // most common case: neither userinfo nor port
                return fixupDomainlabel(uriAuthority);
            } else if (atIndex<0 && portColonIndex>-1) {
                // next most common: port but no userinfo
                String domain = fixupDomainlabel(uriAuthority.substring(0,portColonIndex));
                String port = uriAuthority.substring(portColonIndex);
                return domain + port;
            } else if (atIndex>-1 && portColonIndex<0) {
                // uncommon: userinfo, no port
                String userinfo = ensureMinimalEscaping(uriAuthority.substring(0,atIndex+1),charset);
                String domain = fixupDomainlabel(uriAuthority.substring(atIndex+1));
                return userinfo + domain;
            } else {
                // uncommon: userinfo, port
                String userinfo = ensureMinimalEscaping(uriAuthority.substring(0,atIndex+1),charset);
                String domain = fixupDomainlabel(uriAuthority.substring(atIndex+1,portColonIndex));
                String port = uriAuthority.substring(portColonIndex);
                return userinfo + domain + port;
            }
        }
        return uriAuthority;
    }
882314
public static String usingAuthentication(String url) {
        if (authenticationTypes.BASIC.toString().equals(getInstance().authenticationType)) {
            return url.replace("://", "://" + getLogin() + ":" + getPassword() + "@");
        }
        return url;

    }
926356
def auth_string(self):
        '''
        
        '''

        username_token = '{username}:{token}'.format(username=self.username, token=self.token)
        b64encoded_string = b64encode(username_token)
        auth_string = 'Token {b64}'.format(b64=b64encoded_string)

        return auth_string
602295
private String escapeQuotes(String value)
   {
      StringBuilder sb = new StringBuilder();
      int length = value.length();
      char c;

      sb.append('"');
      for (int index = 0; index < length; index++)
      {
         c = value.charAt(index);
         sb.append(c);

         if (c == '"')
         {
            sb.append('"');
         }
      }
      sb.append('"');

      return (sb.toString());
   }
1035314
public String login(String token, Authentication authentication) {
		if (null == token) {
			return login(authentication);
		}
		tokens.put(token, new TokenContainer(authentication));
		return token;
	}
246681
def escape(s, quote=True):
    
    assert not isinstance(s, bytes), 'Pass a unicode string'
    if quote:
        return s.translate(_escape_map_full)
    return s.translate(_escape_map)
1225904
private String escapeToken(String token) {
		String newToken = (String) escMap.get(token);
		if (newToken == null)
			return token;
		return newToken;
	}
895388
@GET
  @Produces(MediaType.APPLICATION_JSON)
  public String[] get(@HeaderParam("Authorization") @DefaultValue("no token") String auth) throws Exception {
    if(!this.isAuth(auth)) {
      throw new Exception("Unauthorized!");
    }
    return controller.getDisabled();
  }
716014
private static String escapeDoubleQuotes(String s, int beginIndex, int endIndex) {

        if (s == null || s.length() == 0 || s.indexOf('"') == -1) {
            return s;
        }

        StringBuilder b = new StringBuilder();
        for (int i = beginIndex; i < endIndex; i++) {
            char c = s.charAt(i);
            if (c == '\\' ) {
                b.append(c);
                //ignore the character after an escape, just append it
                if (++i>=endIndex) throw UndertowMessages.MESSAGES.invalidEscapeCharacter();
                b.append(s.charAt(i));
            } else if (c == '"')
                b.append('\\').append('"');
            else
                b.append(c);
        }

        return b.toString();
    }
1116752
def requote_uri(uri):
    
    safe_with_percent = "!#$%&'()*+,/:;=?@[]~"
    safe_without_percent = "!#$&'()*+,/:;=?@[]~"
    try:
        # Unquote only the unreserved characters
        # Then quote only illegal characters (do not quote reserved,
        # unreserved, or '%')
        return quote(unquote_unreserved(uri), safe=safe_with_percent)
    except InvalidURL:
        # We couldn't unquote the given URI, so let's try quoting it, but
        # there may be unquoted '%'s in the URI. We need to make sure they're
        # properly quoted so they do not cause issues elsewhere.
        return quote(uri, safe=safe_without_percent)
727415
@Sensitive
    protected String decodeBasicAuth(String data, String encoding) {
        String output = "";
        byte decodedByte[] = null;
        decodedByte = Base64Coder.base64DecodeString(data);
        if (decodedByte != null && decodedByte.length > 0) {
            boolean decoded = false;
            if (encoding != null) {
                try {
                    output = new String(decodedByte, encoding);
                    decoded = true;
                } catch (Exception e) {
                    // fall back not to use encoding..
                    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                        Tr.debug(tc, "An exception is caught using the encoder: " + encoding + ". The exception is: " + e.getMessage());
                    }
                }
            }
            if (!decoded) {
                output = new String(decodedByte);
            }
        }
        return output;
    }
632479
public static String encodeBasicAuth(final String username, final String password) {
        requireNonNull(username, "username");
        requireNonNull(password, "password");
        final String auth = username + ':' + password;
        byte[] encoded;
        try {
            encoded = Base64.getEncoder().encode(auth.getBytes(UTF_8));
        } catch (final IllegalArgumentException e) {
            throw new IllegalArgumentException("Failed to encode basic authentication token", e);
        }
        return BASIC_AUTH_PREFIX + new String(encoded, UTF_8);
    }
1144461
static String unescapeCommandAttributeValue(String s, QuoteStyle quoteStyle) {
    // NOTE: we don't just use String.replace since it internally allocates/compiles a regular
    // expression.  Instead we have a handrolled loop.
    int index = s.indexOf(quoteStyle == QuoteStyle.DOUBLE ? "\\\"" : "\\\'");
    if (index == -1) {
      return s;
    }
    StringBuilder buf = new StringBuilder(s.length());
    buf.append(s);
    boolean nextIsQ = buf.charAt(s.length() - 1) == quoteStyle.getQuoteChar();
    for (int i = s.length() - 2; i >= index; i--) {
      char c = buf.charAt(i);
      if (c == '\\' && nextIsQ) {
        buf.deleteCharAt(i);
      }
      nextIsQ = c == quoteStyle.getQuoteChar();
    }
    return buf.toString();
  }
92320
public static String quoteEscaped(final String str) {
        if (N.isNullOrEmpty(str)) {
            return str;
        }

        final StringBuilder sb = Objectory.createStringBuilder();
        final char[] chars = getCharsForReadOnly(str);

        try {
            char ch = 0;
            for (int i = 0, len = str.length(); i < len; i++) {
                ch = chars[i];

                if ((ch == _BACKSLASH) && (i < (len - 1))) {
                    sb.append(ch);
                    sb.append(str.charAt(++i));
                } else if ((ch == _QUOTATION_S) || (ch == _QUOTATION_D)) {
                    sb.append(_BACKSLASH);
                    sb.append(ch);
                } else {
                    sb.append(ch);
                }
            }

            return sb.toString();
        } finally {
            Objectory.recycle(sb);
        }
    }