TryLastChanceHostRedirection<TSettings extends ConfigurationSettingsBase> method

Future<bool> TryLastChanceHostRedirection<TSettings extends ConfigurationSettingsBase>(
  1. TSettings newSettings,
  2. String emailAddress,
  3. Uri redirectionUrl,
  4. OutParam<TSettings> settings,
)
Get an autodiscover SRV record in DNS and construct autodiscover URL. Name of the domain. The email address. Redirection Url. The settings.

Implementation

//        Uri GetRedirectionUrlFromDnsSrvRecord(String domainName)
//        {
//            this.TraceMessage(
//                TraceFlags.AutodiscoverConfiguration,
//                string.Format("Trying to get Autodiscover host from DNS SRV record for {0}.", domainName));
//
//            String hostname = this.dnsClient.FindAutodiscoverHostFromSrv(domainName);
//            if (!StringUtils.IsNullOrEmpty(hostname))
//            {
//                this.TraceMessage(
//                    TraceFlags.AutodiscoverConfiguration,
//                    string.Format("Autodiscover host {0} was returned.", hostname));
//
//                return new Uri(string.Format(AutodiscoverLegacyHttpsUrl, hostname));
//            }
//            else
//            {
//                this.TraceMessage(
//                    TraceFlags.AutodiscoverConfiguration,
//                    "No matching Autodiscover DNS SRV records were found.");
//
//                return null;
//            }
//        }

/// <summary>

/// </summary>
/// <typeparam name="TSettings">The type of the settings.</typeparam>
/// <param name="emailAddress">The email address.</param>
/// <param name="redirectionUrl">Redirection Url.</param>
/// <param name="settings">The settings.</param>
/* private */
Future<bool>
TryLastChanceHostRedirection<TSettings extends ConfigurationSettingsBase>(
  TSettings newSettings,
  String emailAddress,
  Uri redirectionUrl,
  OutParam<TSettings> settings,
) async {
  settings.param = null;

  List<String> redirectionEmailAddresses = <String>[];

  // Bug 60274: Performing a non-SSL HTTP GET to retrieve a redirection URL is potentially unsafe. We allow the caller
  // to specify delegate to be called to determine whether we are allowed to use the redirection URL.
  if (CallRedirectionUrlValidationCallback(redirectionUrl.toString())) {
    for (
      int currentHop = 0;
      currentHop < AutodiscoverService.AutodiscoverMaxRedirections;
      currentHop++
    ) {
      try {
        settings.param = _GetLegacyUserSettingsAtUrl<TSettings>(
          newSettings,
          emailAddress,
          redirectionUrl,
        );

        switch (settings.param!.ResponseType) {
          case AutodiscoverResponseType.Success:
            return true;
          case AutodiscoverResponseType.Error:
            throw AutodiscoverRemoteException(
              "Strings.AutodiscoverError",
              settings.param!.Error,
            );
          case AutodiscoverResponseType.RedirectAddress:
            // If this email address was already tried, we may have a loop
            // in SCP lookups. Disable consideration of SCP records.
            _DisableScpLookupIfDuplicateRedirection(
              settings.param!.RedirectTarget!,
              redirectionEmailAddresses,
            );

            settings.param = await InternalGetLegacyUserSettings<TSettings>(
              newSettings,
              settings.param!.RedirectTarget!,
              redirectionEmailAddresses,
              OutParam()..param = currentHop,
            );
            return true;
          case AutodiscoverResponseType.RedirectUrl:
            try {
              redirectionUrl = Uri.parse(settings.param!.RedirectTarget!);
            } catch (UriFormatException) {
              TraceMessage(
                enumerations.TraceFlags.AutodiscoverConfiguration,
                "Service returned invalid redirection URL ${settings.param!.RedirectTarget}",
              );
              return false;
            }
            break;
          default:
            String failureMessage =
                "Autodiscover call at $redirectionUrl failed with error ${settings.param!.ResponseType}, target ${settings.param!.RedirectTarget}";
            TraceMessage(
              enumerations.TraceFlags.AutodiscoverConfiguration,
              failureMessage,
            );
            return false;
        }
      } on WebException catch (ex) {
        IEwsHttpWebResponse response =
            HttpWebRequestFactory.CreateExceptionResponse(ex);
        if (_TryGetRedirectionResponse(
          response,
          OutParam()..param = redirectionUrl,
        )) {
          TraceMessage(
            enumerations.TraceFlags.AutodiscoverConfiguration,
            "Host returned a redirection to url $redirectionUrl",
          );
          continue;
        } else {
          ProcessHttpErrorResponse(response, ex);
        }

        TraceMessage(
          enumerations.TraceFlags.AutodiscoverConfiguration,
          "$_url failed: ${ex.runtimeType} (${ex.message})",
        );

        return false;
      } on XmlException catch (ex) {
        // If the response is malformed, it wasn't a valid Autodiscover endpoint.
        TraceMessage(
          enumerations.TraceFlags.AutodiscoverConfiguration,
          "$redirectionUrl failed: XML parsing error: $ex",
        );
        return false;
      } on IOException catch (ex) {
        TraceMessage(
          enumerations.TraceFlags.AutodiscoverConfiguration,
          "$redirectionUrl failed: I/O error: $ex",
        );
        return false;
      }
    }
  }

  return false;
}