예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpListenerReceiver"/> class.
        /// </summary>
        /// <param name="name">The name of the receiver.</param>
        /// <param name="prefixes">
        /// The URI prefixes handled by the <see cref="HttpListener"/>. See
        /// https://docs.microsoft.com/en-us/dotnet/api/system.net.httplistener for
        /// more information.
        /// </param>
        /// <param name="path">
        /// The path that requests must match in order to be handled. Any request whose
        /// path does not match this value will receive a 404 Not Found response.
        /// </param>
        /// <param name="httpResponseGenerator">
        /// An object that determines the http response that is returned to clients,
        /// depending on whether the message is acknowledged, rejected, or rolled back.
        /// </param>
        /// <param name="method">
        /// The http method that requests must have in order to be handled. Any request
        /// that does not have this method will receive a 405 Method Not Allowed response.
        /// </param>
        /// <param name="requiredHeaders">
        /// The HTTP headers that incoming requests are required to match in order to be handled.
        /// Any request that does not have match the required headers will receive a 4xx response.
        /// </param>
        public HttpListenerReceiver(string name, IReadOnlyList <string> prefixes, string path,
                                    IHttpResponseGenerator httpResponseGenerator, string method = DefaultMethod, RequiredHttpRequestHeaders?requiredHeaders = null)
            : base(name)
        {
            if (path is null)
            {
                throw new ArgumentNullException(nameof(path));
            }

            Prefixes = prefixes ?? throw new ArgumentNullException(nameof(prefixes));
            foreach (var prefix in Prefixes)
            {
                _listener.Prefixes.Add(prefix);
            }

            Path = path.Trim('/');
            var pathTokens  = new List <string>();
            var pathPattern = "^/?" + Regex.Replace(Path ?? "", "{([^}]+)}", m =>
            {
                var token = m.Groups[1].Value;
                pathTokens.Add(token);
                return($"(?<{token}>[^/]+)");
            }) + "/?$";

            _pathRegex  = new Regex(pathPattern, RegexOptions.IgnoreCase);
            _pathTokens = pathTokens;

            HttpResponseGenerator = httpResponseGenerator ?? throw new ArgumentNullException(nameof(httpResponseGenerator));
            Method          = method ?? throw new ArgumentNullException(nameof(method));
            RequiredHeaders = requiredHeaders;
        }
예제 #2
0
 internal HttpListenerReceiverMessage(HttpListenerContext context, IHttpResponseGenerator httpResponseGenerator, Regex pathRegex, IReadOnlyCollection <string> pathTokens)
     : base(() => GetPayload(context))
 {
     Context = context;
     HttpResponseGenerator = httpResponseGenerator;
     _pathRegex            = pathRegex;
     _pathTokens           = pathTokens;
 }
예제 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HttpListenerReceiver"/> class.
        /// </summary>
        /// <param name="name">The name of the receiver.</param>
        /// <param name="url">
        /// The url that the <see cref="HttpListener"/> should listen to. See
        /// https://docs.microsoft.com/en-us/dotnet/api/system.net.httplistener for
        /// more information.
        /// </param>
        /// <param name="httpResponseGenerator">
        /// An object that determines the http response that is returned to clients,
        /// depending on whether the message is acknowledged, rejected, or rolled back.
        /// </param>
        /// <param name="method">
        /// The http method that requests must have in order to be handled. Any request
        /// that does not have this method will receive a 405 Method Not Allowed response.
        /// </param>
        /// <param name="requiredHeaders">
        /// The HTTP headers that incoming requests are required to match in order to be handled.
        /// Any request that does not have match the required headers will receive a 4xx response.
        /// </param>
#pragma warning disable CA1054 // URI-like parameters should not be strings
        public HttpListenerReceiver(string name, string url,
#pragma warning restore CA1054 // URI-like parameters should not be strings
                                    IHttpResponseGenerator httpResponseGenerator, string method = DefaultMethod, RequiredHttpRequestHeaders?requiredHeaders = null)
            : this(name, GetPrefixes(url), GetPath(url), httpResponseGenerator, method, requiredHeaders)
        {
        }
예제 #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HttpListenerReceiver"/> class.
 /// </summary>
 /// <param name="name">The name of the receiver.</param>
 /// <param name="url">
 /// The url that the <see cref="HttpListener"/> should listen to. See
 /// https://docs.microsoft.com/en-us/dotnet/api/system.net.httplistener for
 /// more information.
 /// </param>
 /// <param name="httpResponseGenerator">
 /// An object that determines the http response that is returned to clients,
 /// depending on whether the message is acknowledged, rejected, or rolled back.
 /// </param>
 /// <param name="method">
 /// The http method that requests must have in order to be handled. Any request
 /// that does not have this method will receive a 405 Method Not Allowed response.
 /// </param>
 /// <param name="requiredHeaders">
 /// The HTTP headers that incoming requests are required to match in order to be handled.
 /// Any request that does not have match the required headers will receive a 4xx response.
 /// </param>
 public HttpListenerReceiver(string name, string url,
                             IHttpResponseGenerator httpResponseGenerator, string method = DefaultMethod, RequiredHttpRequestHeaders requiredHeaders = null)
     : this(name, GetPrefixes(url), GetPath(url), httpResponseGenerator, method, requiredHeaders)
 {
 }