Exemplo n.º 1
0
 public FakeHttpServer(string[] listnedAddresses)
 {
     asyncServer     = new HttpAsyncServer(listnedAddresses, (context) => OnServerReceived(context));
     receiveMessages = new List <string>();
     asyncServer.RunServer();
     receivers = new List <ConditionalProducer>();
 }
Exemplo n.º 2
0
        private void RequestReceived(HttpAsyncServer sender, HttpListenerContext context)
        {
            var entity = new HttpEntity(context.Request, context.Response, context.User, _logHttpRequests,
                                        _advertiseAsAddress, _advertiseAsPort);

            _requestsMultiHandler.Handle(new IncomingHttpRequestMessage(this, entity, _requestsMultiHandler));
        }
Exemplo n.º 3
0
        public void HttpAsyncServer_should_able_to_run_and_to_stop()
        {
            HttpAsyncServer server = new HttpAsyncServer(new string[] { "http://localhost:3030/" }, context => { });

            server.RunServer();
            server.stop();
        }
Exemplo n.º 4
0
 public FakeHttpServer(string[] listnedAddresses)
 {
     asyncServer    = new HttpAsyncServer(listnedAddresses, (context) => onServerRecived(context));
     reciveMessages = new List <string>();
     asyncServer.RunServer();
     recivers = new List <ConditionalProducer>();
     deffaultAnswer.Response(DEFAULT_RESPONSE_BODY);
 }
Exemplo n.º 5
0
        private void RequestReceived(HttpAsyncServer sender, HttpListenerContext context)
        {
            var allMatches     = AllMatches(context.Request.Url);
            var allowedMethods = allMatches.Select(m => m.ControllerAction.HttpMethod).ToArray();

            if (allMatches.Count == 0)
            {
                NotFound(context);
                return;
            }
            var match = allMatches.LastOrDefault(m => m.ControllerAction.HttpMethod == context.Request.HttpMethod);

            if (match == null)
            {
                MethodNotAllowed(context, allowedMethods);
                return;
            }

            ICodec requestCodec;

            if (!TrySelectRequestCodec(context.Request.HttpMethod,
                                       context.Request.ContentType,
                                       match.ControllerAction.SupportedRequestCodecs,
                                       out requestCodec))
            {
                BadCodec(context, "Content-Type MUST be set for POST PUT and DELETE");
                return;
            }
            ICodec responseCodec;

            if (!TrySelectResponseCodec(context.Request.QueryString,
                                        context.Request.AcceptTypes,
                                        match.ControllerAction.SupportedResponseCodecs,
                                        match.ControllerAction.DefaultResponseCodec,
                                        out responseCodec))
            {
                BadCodec(context, "Requested uri is not available in requested format");
                return;
            }

            var entity = CreateEntity(DateTime.UtcNow,
                                      context,
                                      requestCodec,
                                      responseCodec,
                                      allowedMethods,
                                      satisfied =>
            {
                lock (_pendingLock)
                    _pending.Remove(satisfied);
            });

            lock (_pendingLock)
                _pending.Add(entity);

            match.RequestHandler(entity, match.TemplateMatch);
        }
Exemplo n.º 6
0
        public HttpService(IPublisher inputBus, string[] prefixes)
        {
            Ensure.NotNull(inputBus, "inputBus");
            Ensure.NotNull(prefixes, "prefixes");

            _inputBus        = inputBus;
            _publishEnvelope = new PublishEnvelope(inputBus);

            _pending = new SortedSet <HttpEntity>(new HttpEntityReceivedComparer());
            _actions = new Dictionary <ControllerAction, Action <HttpEntity, UriTemplateMatch> >();

            _httpPipe = new HttpMessagePipe();

            _server = new HttpAsyncServer(prefixes);
            _server.RequestReceived += RequestReceived;
        }
Exemplo n.º 7
0
        public HttpService(ServiceAccessibility accessibility, IPublisher inputBus, IUriRouter uriRouter,
                           MultiQueuedHandler multiQueuedHandler, params string[] prefixes)
        {
            Ensure.NotNull(inputBus, "inputBus");
            Ensure.NotNull(uriRouter, "uriRouter");
            Ensure.NotNull(prefixes, "prefixes");

            _accessibility   = accessibility;
            _inputBus        = inputBus;
            _uriRouter       = uriRouter;
            _publishEnvelope = new PublishEnvelope(inputBus);

            _requestsMultiHandler = multiQueuedHandler;

            _server = new HttpAsyncServer(prefixes);
            _server.RequestReceived += RequestReceived;
        }
Exemplo n.º 8
0
        public HttpService(ServiceAccessibility accessibility, IPublisher inputBus, IUriRouter uriRouter,
                           MultiQueuedHandler multiQueuedHandler, bool logHttpRequests, IPAddress advertiseAsAddress, int advertiseAsPort, params string[] prefixes)
        {
            Ensure.NotNull(inputBus, "inputBus");
            Ensure.NotNull(uriRouter, "uriRouter");
            Ensure.NotNull(prefixes, "prefixes");

            _accessibility   = accessibility;
            _inputBus        = inputBus;
            _uriRouter       = uriRouter;
            _publishEnvelope = new PublishEnvelope(inputBus);

            _requestsMultiHandler = multiQueuedHandler;
            _logHttpRequests      = logHttpRequests;

            _server = new HttpAsyncServer(prefixes);
            _server.RequestReceived += RequestReceived;

            _advertiseAsAddress = advertiseAsAddress;
            _advertiseAsPort    = advertiseAsPort;
        }
Exemplo n.º 9
0
        private void RequestReceived(HttpAsyncServer sender, HttpListenerContext context)
        {
            try
            {
                var allMatches     = AllMatches(context.Request.Url);
                var allowedMethods = allMatches.Select(m => m.ControllerAction.HttpMethod).ToArray();

                if (allMatches.Count == 0)
                {
                    NotFound(context);
                    return;
                }

                //add options to the list of allowed request methods
                allowedMethods = allowedMethods.Union(new[] { HttpMethod.Options }).ToArray();
                if (context.Request.HttpMethod.Equals(HttpMethod.Options))
                {
                    RespondWithOptions(context, allowedMethods);
                    return;
                }

                var match = allMatches.LastOrDefault(m => m.ControllerAction.HttpMethod == context.Request.HttpMethod);
                if (match == null)
                {
                    MethodNotAllowed(context, allowedMethods);
                    return;
                }

                ICodec requestCodec;
                if (
                    !TrySelectRequestCodec(
                        context.Request.HttpMethod, context.Request.ContentType,
                        match.ControllerAction.SupportedRequestCodecs, out requestCodec))
                {
                    BadCodec(context, "Content-Type MUST be set for POST PUT and DELETE");
                    return;
                }
                ICodec responseCodec;
                if (
                    !TrySelectResponseCodec(
                        context.Request.QueryString, context.Request.AcceptTypes,
                        match.ControllerAction.SupportedResponseCodecs, match.ControllerAction.DefaultResponseCodec,
                        out responseCodec))
                {
                    BadCodec(context, "Requested uri is not available in requested format");
                    return;
                }

                var entity = CreateEntity(
                    DateTime.UtcNow, context, requestCodec, responseCodec, allowedMethods, satisfied =>
                {
                    lock (_pendingLock)
                        _pending.Remove(satisfied);
                });
                lock (_pendingLock)
                    _pending.Add(entity);

                match.RequestHandler(entity, match.TemplateMatch);
            }
            catch (Exception exception)
            {
                _log.ErrorException(exception, "Unhandled exception while processing http request");
                InternalServerError(context);
            }
        }