<commit_before>﻿namespace SDammann.WebApi.Versioning {
    using System;
    using System.Globalization;
    using System.Net.Http;
    using System.Web.Http;
    using System.Web.Http.Dispatcher;
    using System.Web.Http.Routing;
    using System.Linq;
    using System.Web.Http.Controllers;


    /// <summary>
    ///   Represents an <see cref="IHttpControllerSelector" /> implementation that supports versioning and selects an controller based on versioning by convention (namespace.Api.Version1.xxxController). The controller to invoke is determined by the "version" key in the routing dictionary.
    /// </summary>
    public sealed class RouteVersionedControllerSelector : VersionedControllerSelector {
        private const string VersionKey = "version";

        /// <summary>
        ///   Initializes a new instance of the <see cref="RouteVersionedControllerSelector" /> class.
        /// </summary>
        /// <param name="configuration"> The configuration. </param>
        public RouteVersionedControllerSelector (HttpConfiguration configuration) : base(configuration) {
        }

        protected override ControllerIdentification GetControllerIdentificationFromRequest (HttpRequestMessage request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

            IHttpRouteData routeData = request.GetRouteData();
            if (routeData == null) {
                return default(ControllerIdentification);
            }

            // Look up controller in route data
            string controllerName;
            var subRoute = routeData.GetSubRoutes().FirstOrDefault();
            if (subRoute == null)
                controllerName = this.GetControllerNameFromRequest(request);
            else
                controllerName = getControllerNameFromSubRouteData(subRoute);

            // Also try the version if possible
            object apiVersionObj;
            string apiVersion = null;
            int version;

            if (subRoute != null && subRoute.Values.TryGetValue(VersionKey, out apiVersionObj)
                && !String.IsNullOrWhiteSpace(apiVersionObj as string))
            {
                apiVersion = apiVersionObj as string;
            }
            else if (routeData.Values.TryGetValue(VersionKey, out apiVersionObj) &&
                     !String.IsNullOrWhiteSpace(apiVersionObj as string))
            {
                apiVersion = apiVersionObj as string;
            }

            return new ControllerIdentification(controllerName, apiVersion);
        }

        private string getControllerNameFromSubRouteData(IHttpRouteData pRouteData)
        {
            var descriptors = pRouteData.Route.DataTokens["actions"] as HttpActionDescriptor[];
            if (descriptors == null || descriptors.Length == 0)
                return string.Empty;

            return descriptors[0].ControllerDescriptor.ControllerName;
        }
    }
}<commit_msg>Fix nullref<commit_after>﻿namespace SDammann.WebApi.Versioning {
    using System;
    using System.Globalization;
    using System.Net.Http;
    using System.Web.Http;
    using System.Web.Http.Dispatcher;
    using System.Web.Http.Routing;
    using System.Linq;
    using System.Web.Http.Controllers;


    /// <summary>
    ///   Represents an <see cref="IHttpControllerSelector" /> implementation that supports versioning and selects an controller based on versioning by convention (namespace.Api.Version1.xxxController). The controller to invoke is determined by the "version" key in the routing dictionary.
    /// </summary>
    public sealed class RouteVersionedControllerSelector : VersionedControllerSelector {
        private const string VersionKey = "version";

        /// <summary>
        ///   Initializes a new instance of the <see cref="RouteVersionedControllerSelector" /> class.
        /// </summary>
        /// <param name="configuration"> The configuration. </param>
        public RouteVersionedControllerSelector (HttpConfiguration configuration) : base(configuration) {
        }

        protected override ControllerIdentification GetControllerIdentificationFromRequest (HttpRequestMessage request) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

            IHttpRouteData routeData = request.GetRouteData();
            if (routeData == null) {
                return default(ControllerIdentification);
            }

            // Look up controller in route data
            string controllerName;
            var subRoute = (routeData.GetSubRoutes() ?? Enumerable.Empty<IHttpRouteData>()).FirstOrDefault();
            if (subRoute == null)
                controllerName = this.GetControllerNameFromRequest(request);
            else
                controllerName = getControllerNameFromSubRouteData(subRoute);

            // Also try the version if possible
            object apiVersionObj;
            string apiVersion = null;
            int version;

            if (subRoute != null && subRoute.Values.TryGetValue(VersionKey, out apiVersionObj)
                && !String.IsNullOrWhiteSpace(apiVersionObj as string))
            {
                apiVersion = apiVersionObj as string;
            }
            else if (routeData.Values.TryGetValue(VersionKey, out apiVersionObj) &&
                     !String.IsNullOrWhiteSpace(apiVersionObj as string))
            {
                apiVersion = apiVersionObj as string;
            }

            return new ControllerIdentification(controllerName, apiVersion);
        }

        private string getControllerNameFromSubRouteData(IHttpRouteData pRouteData)
        {
            var descriptors = pRouteData.Route.DataTokens["actions"] as HttpActionDescriptor[];
            if (descriptors == null || descriptors.Length == 0)
                return string.Empty;

            return descriptors[0].ControllerDescriptor.ControllerName;
        }
    }
}
{'fraction_non_alphanumeric': 0.08003300330033003, 'fraction_numerical': 0.0009900990099009901, 'mean_word_length': 2.2463845741831814, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7660416', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1366, 'n_words': 366}