コード例 #1
0
        private static UnicornResponse Execute(UnicornRequest request)
        {
            if(!_isAdminMode && !request.IsAuthenticated)
                return UnicornResponse.CreateUnauthorized();

            if(!_handlers.ContainsKey(request.Verb))
                return UnicornResponse.CreateNotFound();

            return _handlers[request.Verb].Handle(request);
        }
コード例 #2
0
        private static UnicornResponse Execute(UnicornRequest request)
        {
            if (!_isAdminMode && !request.IsAuthenticated)
            {
                return(UnicornResponse.CreateUnauthorized());
            }

            if (!_handlers.ContainsKey(request.Verb))
            {
                return(UnicornResponse.CreateNotFound());
            }

            return(_handlers[request.Verb].Handle(request));
        }
コード例 #3
0
        private static async void StartServer()
        {
            var listener = new TcpListener(new IPEndPoint(IPAddress.Loopback, 2222));

            listener.Start();
            Console.WriteLine("Server is running on port 2222");
            while (true)
            {
                var connection = await listener.AcceptTcpClientAsync();

                int t = 0;
                while (t < 3)
                {
                    if (connection.Available != 0)
                    {
                        break;
                    }

                    Thread.Sleep(100);
                    t++;
                }
                var stream = connection.GetStream();
                var buffer = new byte[connection.Available];
                await stream.ReadAsync(buffer, 0, connection.Available);

                var request  = Encoding.UTF8.GetString(buffer);
                var response = Execute(UnicornRequest.Parse(request));

                do
                {
                    var renderResponse = response.RenderResponse();
                    var rData          = Encoding.UTF8.GetBytes(renderResponse);
                    await stream.WriteAsync(rData, 0, rData.Length);

                    await stream.FlushAsync();

                    Thread.Sleep(_random.Next(100, 500));
                } while (!response.IsComplete);
                connection.Close();
                connection.Dispose();
            }
        }
コード例 #4
0
        public static UnicornRequest Parse(string request)
        {
            var parts = request.Split(new[] {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries);
            var querystring = Regex.Match(parts[0], "(?<=\\?)[^\\s]+").Value;
            var r = new UnicornRequest();
            foreach (var queryPart in querystring.Split('&'))
            {
                var keyValue = queryPart.Split('=');
                r.Query.Add(keyValue[0], keyValue[1]);
            }

            for (int i = 1; i < parts.Length; i++)
            {
                var header = parts[i].Split(':');
                var key = header[0].Trim();
                var value = header[1].Trim();

                r.Headers.Add(key, value);
            }

            return r;
        }
コード例 #5
0
        public static UnicornRequest Parse(string request)
        {
            var parts       = request.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            var querystring = Regex.Match(parts[0], "(?<=\\?)[^\\s]+").Value;
            var r           = new UnicornRequest();

            foreach (var queryPart in querystring.Split('&'))
            {
                var keyValue = queryPart.Split('=');
                r.Query.Add(keyValue[0], keyValue[1]);
            }

            for (int i = 1; i < parts.Length; i++)
            {
                var header = parts[i].Split(':');
                var key    = header[0].Trim();
                var value  = header[1].Trim();

                r.Headers.Add(key, value);
            }

            return(r);
        }