Support class to populate user authentication data on a connection.
Support class to populate user authentication data on a connection.

Instances of an HttpAuthMethod are not thread-safe, as some implementations may need to maintain per-connection state information.

示例#1
0
 private static string GetAuthMethodName(HttpAuthMethod authMethod)
 {
     return authMethod.GetType().Name;
 }
示例#2
0
        // No explicit connections are maintained.
        /// <exception cref="NGit.Errors.TransportException"></exception>
        /// <exception cref="System.NotSupportedException"></exception>
        private HttpURLConnection Connect(string service)
        {
            Uri u;

            try
            {
                StringBuilder b = new StringBuilder();
                b.Append(baseUrl);
                if (b[b.Length - 1] != '/')
                {
                    b.Append('/');
                }
                b.Append(Constants.INFO_REFS);
                if (useSmartHttp)
                {
                    b.Append(b.IndexOf("?") < 0 ? '?' : '&');
                    //$NON-NLS-1$
                    b.Append("service=");
                    //$NON-NLS-1$
                    b.Append(service);
                }
                u = new Uri(b.ToString());
            }
            catch (UriFormatException e)
            {
                throw new NotSupportedException(MessageFormat.Format(JGitText.Get().invalidURL, uri
                                                                     ), e);
            }
            try
            {
                int authAttempts = 1;
                for (; ;)
                {
                    HttpURLConnection conn = HttpOpen(u);
                    if (useSmartHttp)
                    {
                        string exp = "application/x-" + service + "-advertisement";
                        //$NON-NLS-1$ //$NON-NLS-2$
                        conn.SetRequestProperty(HttpSupport.HDR_ACCEPT, exp + ", */*");
                    }
                    else
                    {
                        //$NON-NLS-1$
                        conn.SetRequestProperty(HttpSupport.HDR_ACCEPT, "*/*");
                    }
                    //$NON-NLS-1$
                    int status = HttpSupport.Response(conn);
                    switch (status)
                    {
                    case HttpURLConnection.HTTP_OK:
                    {
                        return(conn);
                    }

                    case HttpURLConnection.HTTP_NOT_FOUND:
                    {
                        throw new NoRemoteRepositoryException(uri, MessageFormat.Format(JGitText.Get().uriNotFound
                                                                                        , u));
                    }

                    case HttpURLConnection.HTTP_UNAUTHORIZED:
                    {
                        authMethod = HttpAuthMethod.ScanResponse(conn);
                        if (authMethod == HttpAuthMethod.NONE)
                        {
                            throw new TransportException(uri, MessageFormat.Format(JGitText.Get().authenticationNotSupported
                                                                                   , uri));
                        }
                        if (1 < authAttempts || !authMethod.Authorize(uri, GetCredentialsProvider()))
                        {
                            throw new TransportException(uri, JGitText.Get().notAuthorized);
                        }
                        authAttempts++;
                        continue;
                        goto case HttpURLConnection.HTTP_FORBIDDEN;
                    }

                    case HttpURLConnection.HTTP_FORBIDDEN:
                    {
                        throw new TransportException(uri, MessageFormat.Format(JGitText.Get().serviceNotPermitted
                                                                               , service));
                    }

                    default:
                    {
                        string err = status + " " + conn.GetResponseMessage();
                        //$NON-NLS-1$
                        throw new TransportException(uri, err);
                    }
                    }
                }
            }
            catch (NotSupportedException e)
            {
                throw;
            }
            catch (TransportException e)
            {
                throw;
            }
            catch (IOException e)
            {
                throw new TransportException(uri, MessageFormat.Format(JGitText.Get().cannotOpenService
                                                                       , service), e);
            }
        }