/*
 * Copyright (c) 2010-2013 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */
package org.asynchttpclient.util;

import static java.nio.charset.StandardCharsets.ISO_8859_1;
import static org.asynchttpclient.util.AsyncHttpProviderUtils.getNonEmptyPath;
import static org.asynchttpclient.util.MiscUtils.isNonEmpty;

import java.nio.charset.Charset;

import org.asynchttpclient.ProxyServer;
import org.asynchttpclient.Realm;
import org.asynchttpclient.uri.Uri;

public final class AuthenticatorUtils {

    public static String computeBasicAuthentication(Realm realm) {
        return computeBasicAuthentication(realm.getPrincipal(), realm.getPassword(), realm.getCharset());
    }

    public static String computeBasicAuthentication(ProxyServer proxyServer) {
        return computeBasicAuthentication(proxyServer.getPrincipal(), proxyServer.getPassword(), proxyServer.getCharset());
    }

    private static String computeBasicAuthentication(String principal, String password, Charset charset) {
        String s = principal + ":" + password;
        return "Basic " + Base64.encode(s.getBytes(charset));
    }

    private static String computeRealmURI(Realm realm) {
        if (realm.isTargetProxy()) {
            return "/";
        } else {
            Uri uri = realm.getUri();
            if (realm.isUseAbsoluteURI()) {
                return realm.isOmitQuery() && MiscUtils.isNonEmpty(uri.getQuery()) ? uri.withNewQuery(null).toUrl() : uri.toUrl();
            } else {
                String path = getNonEmptyPath(uri);
                return realm.isOmitQuery() || !MiscUtils.isNonEmpty(uri.getQuery()) ? path : path + "?" + uri.getQuery();
            }
        }
    }

    public static String computeDigestAuthentication(Realm realm) {

        StringBuilder builder = new StringBuilder().append("Digest ");
        append(builder, "username", realm.getPrincipal(), true);
        append(builder, "realm", realm.getRealmName(), true);
        append(builder, "nonce", realm.getNonce(), true);
        append(builder, "uri", computeRealmURI(realm), true);
        append(builder, "algorithm", realm.getAlgorithm(), false);

        append(builder, "response", realm.getResponse(), true);
        if (isNonEmpty(realm.getOpaque()))
            append(builder, "opaque", realm.getOpaque(), true);
        append(builder, "qop", realm.getQop(), false);
        append(builder, "nc", realm.getNc(), false);
        append(builder, "cnonce", realm.getCnonce(), true);
        builder.setLength(builder.length() - 2); // remove tailing ", "

        return new String(builder.toString().getBytes(ISO_8859_1));
    }

    private static StringBuilder append(StringBuilder builder, String name, String value, boolean quoted) {
        builder.append(name).append('=');
        if (quoted)
            builder.append('"').append(value).append('"');
        else
            builder.append(value);

        return builder.append(", ");
    }
}

@@ -1785,90 +1785,8 @@
 ) %7B%0A
-        if (realm.isTargetProxy()) %7B%0A            return %22/%22;%0A        %7D else %7B%0A    
     
@@ -1811,28 +1811,24 @@
 m.getUri();%0A
-    
         if (
@@ -1859,36 +1859,32 @@
 ) %7B%0A            
- 
-   
 return realm.isO
@@ -1990,20 +1990,16 @@
         
-    
 %7D else %7B
@@ -1999,20 +1999,16 @@
  else %7B%0A
-    
         
@@ -2047,20 +2047,16 @@
 h(uri);%0A
-    
         
@@ -2169,22 +2169,8 @@
 ();%0A
-            %7D%0A
     

Use request uri and not "/" when processing DIGEST auth uri, close #801