Пример #1
0
        /// <summary>
        /// 引数(<paramref name="request"/>)に紐づくレスポンスデータ情報を取得する
        /// </summary>
        /// <param name="request">HTTPリクエスト</param>
        /// <exception cref="ArgumentNullException">
        /// 引数の <paramref name="request"/> がNULLの場合に発生
        /// </exception>
        /// <exception cref="RegexMatchTimeoutException">
        /// 正規表現のマッチング処理にてタイムアウトが発生した場合に発生
        /// </exception>
        /// <returns>レスポンスデータ情報</returns>
        private HttpResponseData GetTargetResponseData(HttpListenerRequest request)
        {
            // NULLチェック
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // favicon.iconのリクエストかチェック
            if (request.HttpMethod.Equals(
                    HttpMethod.Get.ToString(), StringComparison.OrdinalIgnoreCase) &&
                request.RawUrl.Equals(
                    LocalHttpServerResponseSetting.FaviconUrl, StringComparison.OrdinalIgnoreCase))
            {
                // favicon.iconのリクエストの場合は、それfavicon.icon用のレスポンスデータ情報を返却
                return(Setting?.FaviconResponse ?? LocalHttpServerCommon.EmptyDataResponse);
            }

            // HttpMethod、URLに紐づくレスポンスデータ情報を取得
            LocalHttpServerResponceProcess responceProcess
                = Setting?.Find(request.HttpMethod, request.RawUrl);

            // 取得したレスポンスデータ情報を返却
            // 取得できなかった場合はデフォルト用のレスポンスデータ情報を返却
            HttpResponseData defaultProcess
                = Setting?.DefaultResponse ?? LocalHttpServerCommon.EmptyDataResponse;

            return(responceProcess?.ResponseData ?? defaultProcess);
        }
        /// <summary>
        /// このクラスのインスタンスの複製を生成する
        /// </summary>
        /// <returns>
        /// このクラスのインスタンスのコピーである新しいインスタンス
        /// </returns>
        public LocalHttpServerResponseSetting DeepCopy()
        {
            // Httpレスポンスの処理のクラスの配列を複製
            LocalHttpServerResponceProcess[] copyList = null;
            if (ResponceProcesses != null)
            {
                copyList = new LocalHttpServerResponceProcess[ResponceProcesses.Count];
                int index = 0;
                foreach (LocalHttpServerResponceProcess responceProcess in ResponceProcesses)
                {
                    copyList[index] = responceProcess.DeepCopy();
                    index++;
                }
            }

            // このクラスのインスタンスを生成し返却
            return(new LocalHttpServerResponseSetting(
                       defaultProcess: DefaultResponse.DeepCopy(),
                       faviconProcess: FaviconResponse.DeepCopy(),
                       responceProcesses: copyList));
        }
        /// <summary>
        /// <see cref="HttpMethod"/> とURLに紐づくHttpレスポンスの処理のクラスの配列から、
        /// 引数の条件に合致するデータを取得する
        /// </summary>
        /// <param name="method">検索対象とするHttpメソッド</param>
        /// <param name="processName">検索対象とする処理名:URLのパスの部分の名称(相対パス)</param>
        /// <exception cref="ArgumentNullException">
        /// 引数の <paramref name="method"/> 又は、<paramref name="processName"/> がNULLの場合に発生
        /// </exception>
        /// <exception cref="RegexMatchTimeoutException">
        /// 正規表現のマッチング処理にてタイムアウトが発生した場合に発生
        /// </exception>
        /// <returns>
        /// 引数に合致するHttpレスポンスの処理のクラス
        /// 条件に合致するデータが存在しない場合はNULLを返却する
        /// 複数取得できる場合は先頭の一つのみ取得する
        /// </returns>
        public LocalHttpServerResponceProcess Find(string method, string processName)
        {
            // NULLチェック
            if (method == null)
            {
                throw new ArgumentNullException(nameof(method));
            }
            else if (processName == null)
            {
                throw new ArgumentNullException(nameof(processName));
            }

            // 引数に合致するHttpレスポンスの処理のクラスを取得
            // 取得できなかった場合は、NULL
            LocalHttpServerResponceProcess responceProcess =
                ResponceProcesses?.FirstOrDefault(
                    (x) => x.MethodString.Equals(method, StringComparison.Ordinal) &&
                    x.ProcessNameRegex.IsMatch(processName));

            // 取得したHttpレスポンスの処理のクラスを返却
            return(responceProcess);
        }