Ejemplo n.º 1
0
        private Task RequestReceived(HttpContext context)
        {
            var tcs    = new TaskCompletionSource <bool>();
            var entity = new HttpEntity(new CoreHttpRequestAdapter(context.Request),
                                        new CoreHttpResponseAdapter(context.Response), context.User, _logHttpRequests,
                                        _advertiseAsAddress, _advertiseAsPort, () => tcs.TrySetResult(true));

            entity.SetUser(context.User);
            _requestsMultiHandler.Handle(new AuthenticatedHttpRequestMessage(this, entity));
            return(tcs.Task);
        }
Ejemplo n.º 2
0
        private static bool TryMatch(HttpContext context, IUriRouter uriRouter, bool logHttpRequests, IPAddress advertiseAsAddress, int advertiseAsPort)
        {
            var tcs        = new TaskCompletionSource <bool>();
            var httpEntity = new HttpEntity(new CoreHttpRequestAdapter(context.Request),
                                            new CoreHttpResponseAdapter(context.Response), context.User, logHttpRequests,
                                            advertiseAsAddress, advertiseAsPort, () => tcs.TrySetResult(true));

            httpEntity.SetUser(context.User);

            var request = httpEntity.Request;

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

                var allowedMethods = GetAllowedMethods(allMatches);

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

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

                    ;
                }

                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(false);
                    }
                }

                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(false);
                }
                try {
                    var manager =
                        httpEntity.CreateManager(requestCodec, responseCodec, allowedMethods, satisfied => { });
                    context.Items.Add(manager.GetType(), manager);
                    context.Items.Add(match.GetType(), match);
                    context.Items.Add(tcs.GetType(), tcs);
                    return(true);
                } catch (Exception exc) {
                    Log.Error(exc, "Error while handling HTTP request '{url}'.", request.Url);
                    InternalServerError(httpEntity);
                }
            } catch (Exception exc) {
                Log.Error(exc, "Unhandled exception while processing HTTP request at {url}.",
                          httpEntity.RequestedUrl);
                InternalServerError(httpEntity);
            }
            return(false);
        }