Ejemplo n.º 1
0
        /// <summary>
        /// Attempts to parse a name-value pair from the context's Input
        /// property. This is the last function in the pipeline, so it never
        /// invokes the specified next function.
        /// </summary>
        public static Task ParseSetting(ParseContext context, Func <Task> next)
        {
            const int SettingName   = 0;
            const int SettingValue  = 1;
            const int MaxSubstrings = 2;

            RequiresArgument.NotNull(context, "context");
            RequiresArgument.NotNull(next, "next");

            var arr = context
                      .Input
                      // We only want to split on the first colon, so the maximum
                      //number of resulting substrings is limited to two.
                      .Split(new[] { ':' }, MaxSubstrings)
                      .Select(x => x.Trim())
                      .ToArray();

            RequiresArgument.LengthEquals(arr, MaxSubstrings, "Settings must have a name and value. For example: MyFavoriteColor: Red");
            RequiresArgument.NotNullOrWhiteSpace(arr[SettingName], "Setting name");
            RequiresArgument.NotNullOrWhiteSpace(arr[SettingValue], "Setting value");

            // Using reflection to find parsed setting on the context's state
            // and set its value.
            var field = context.State.GetType().GetRuntimeField(arr[SettingName]);

            RequiresSettingExists(field, arr[SettingName]);
            field.SetValue(context.State, arr[SettingValue]);

            return(Task.CompletedTask);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns true when the HttpRequest has the specified method,
        /// e.g. GET, POST, etc., and path, ex: /api/students. The test is
        /// case insensitive.
        public static bool Matches(this HttpRequest request, string method, string path)
        {
            RequiresArgument.NotNullOrWhiteSpace(method, "method");
            RequiresArgument.NotNullOrWhiteSpace(path, "path");

            return(request.Method.ToUpperInvariant() == method.ToUpperInvariant() &&
                   request.Path.ToString().ToUpperInvariant() == path.ToUpperInvariant());
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Makes the response an OK (200) response and sets it's content.
        /// </summary>
        public static async Task Ok(this HttpResponse response, string content)
        {
            RequiresArgument.NotNullOrWhiteSpace(content, "message");

            response.StatusCode  = 200;
            response.ContentType = "text/plain";
            await response.WriteAsync(content);
        }