Exemple #1
0
        internal async Task <RequestAnalysis> AnalyzeRequest(HttpRequest request)
        {
            string absolutePath = request.Path;//request.Url.AbsolutePath;

            if (absolutePath.StartsWith(_recurringTaskHandlerAbsolutePath, StringComparison.Ordinal))
            {
                var analysis = new RequestAnalysis();
                analysis.IsRecurringTask = true;
                int parameterStartingIndex = _recurringTaskHandlerAbsolutePath.Length;

                if (absolutePath.Length > parameterStartingIndex)
                {
                    // AbsolutePath format:
                    // task       -> ~/__RevaleeRecurring.axd/{identifier}/{occurrence}
                    // heartbeat  -> ~/__RevaleeRecurring.axd/{heartbeatId}

                    int taskParameterDelimiterIndex = absolutePath.IndexOf('/', parameterStartingIndex);

                    if (taskParameterDelimiterIndex < 0)
                    {
                        // no task parameter delimiter

                        if ((absolutePath.Length - parameterStartingIndex) == 32)
                        {
                            Guid heartbeatId;

                            if (Guid.TryParseExact(absolutePath.Substring(parameterStartingIndex), "N", out heartbeatId))
                            {
                                if (heartbeatId.Equals(_id))
                                {
                                    await OnActivate();
                                }
                            }
                        }
                    }
                    else
                    {
                        // task parameter delimiter present

                        if ((absolutePath.Length - taskParameterDelimiterIndex) > 1)
                        {
                            if (long.TryParse(absolutePath.Substring(taskParameterDelimiterIndex + 1), NumberStyles.None, CultureInfo.InvariantCulture, out analysis.Occurrence))
                            {
                                analysis.TaskIdentifier = absolutePath.Substring(parameterStartingIndex, taskParameterDelimiterIndex - parameterStartingIndex);
                            }
                        }
                    }
                }

                // If the TaskIdentifier is not set the default will be string.Empty, which will be discarded by the HttpModule

                return(analysis);
            }

            return(new RequestAnalysis());
        }
        public Task Invoke(HttpContext context)
        {
            try
            {
                TaskManifest _manifest = LoadedManifest(context).Result;

                if (context?.Request != null && _manifest != null)
                {
                    var             request  = context.Request;
                    RequestAnalysis analysis = _manifest.AnalyzeRequest(context.Request).Result;

                    if (analysis.IsRecurringTask)
                    {
                        ConfiguredTask taskConfig;
                        HttpStatusCode statusCode;

                        if (_manifest.TryGetTask(analysis.TaskIdentifier, out taskConfig))
                        {
                            if (request.Method == "POST")
                            {
                                if (_revalee.ValidateCallback(context))
                                {
                                    if (taskConfig.SetLastOccurrence(analysis.Occurrence))
                                    {
                                        _manifest.Reschedule(taskConfig).Wait();
                                        context.Items.Add(RouteKeys.IN_PROCESS_CONTEXT_KEY, BuildCallbackDetails(request, taskConfig.Identifier));
                                        context.Request.Path = new PathString(taskConfig.Url.AbsolutePath);

                                        goto Continue;
                                    }
                                    else
                                    {
                                        statusCode = HttpStatusCode.Accepted;
                                    }
                                }
                                else
                                {
                                    statusCode = HttpStatusCode.Unauthorized;
                                }
                            }
                            else
                            {
                                if (request.Method == "GET" || request.Method == "HEAD")
                                {
                                    if (request.Headers["Expect"] == "100-continue")
                                    {
                                        context.Response.StatusCode = (int)HttpStatusCode.Continue;
                                        goto Continue;
                                    }
                                    else
                                    {
                                        statusCode = HttpStatusCode.MethodNotAllowed;
                                    }
                                }
                                else
                                {
                                    statusCode = HttpStatusCode.NotImplemented;
                                }
                            }
                        }
                        else
                        {
                            statusCode = HttpStatusCode.NoContent;
                        }

                        context.Response.StatusCode = (int)statusCode;
                        return(context.Response.WriteAsync(""));
                    }
                }
            }
            catch (Exception ex)
            {
            }

Continue:
            return(_next(context));
        }