Esempio n. 1
0
        private void ProcessRequest(IHttpService httpService, HttpEntity httpEntity)
        {
            var request = httpEntity.Request;

            try {
                var allMatches = httpService.GetAllUriMatches(request.Url);
                if (allMatches.Count == 0)
                {
                    NotFound(httpEntity);
                    return;
                }

                var allowedMethods = GetAllowedMethods(allMatches);

                if (request.HttpMethod.Equals(HttpMethod.Options, StringComparison.OrdinalIgnoreCase))
                {
                    RespondWithOptions(httpEntity, allowedMethods);
                    return;
                }

                var match = allMatches.LastOrDefault(
                    m => m.ControllerAction.HttpMethod.Equals(request.HttpMethod, StringComparison.OrdinalIgnoreCase));
                if (match == null)
                {
                    MethodNotAllowed(httpEntity, allowedMethods);
                    return;
                }

                ICodec requestCodec           = null;
                var    supportedRequestCodecs = match.ControllerAction.SupportedRequestCodecs;
                if (supportedRequestCodecs != null && supportedRequestCodecs.Length > 0)
                {
                    requestCodec = SelectRequestCodec(request.HttpMethod, request.ContentType, supportedRequestCodecs);
                    if (requestCodec == null)
                    {
                        BadContentType(httpEntity, "Invalid or missing Content-Type");
                        return;
                    }
                }

                ICodec responseCodec = SelectResponseCodec(request,
                                                           request.AcceptTypes,
                                                           match.ControllerAction.SupportedResponseCodecs,
                                                           match.ControllerAction.DefaultResponseCodec);
                if (responseCodec == null)
                {
                    BadCodec(httpEntity, "Requested URI is not available in requested format");
                    return;
                }


                try {
                    var manager =
                        httpEntity.CreateManager(requestCodec, responseCodec, allowedMethods, satisfied => { });
                    var reqParams = match.RequestHandler(manager, match.TemplateMatch);
                    if (!reqParams.IsDone)
                    {
                        _pending.Add(Tuple.Create(DateTime.UtcNow + reqParams.Timeout, manager));
                    }
                } catch (Exception exc) {
                    Log.ErrorException(exc, "Error while handling HTTP request '{url}'.", request.Url);
                    InternalServerError(httpEntity);
                }
            } catch (Exception exc) {
                Log.ErrorException(exc, "Unhandled exception while processing HTTP request at {url}.",
                                   httpEntity.RequestedUrl);
                InternalServerError(httpEntity);
            }

            PurgeTimedOutRequests();
        }