Ejemplo n.º 1
0
 public HttpContact(IChannel channel, Request request, Response response = null)
 {
     _channel = channel;
     _request = request;
     _response = response;
     IsAutoSendResponse = true;
 }
Ejemplo n.º 2
0
        public void Handle(IChannel channel, Request request)
        {
            var contact = new HttpContact(channel, request, new Response {Protocol = request.GetProtocol()});
            if(contact.GetRequest().GetProtocol() == "HTTP/1.1")
                contact.GetResponse().Protocol = "1.1";

            try
            {
                _action(contact, _rx.Split(request.GetPath()));
            }
            catch (Exception e)
            {
                contact.GetResponse().Status = 500;
                contact.GetResponse().SetContent(e.ToString());
            }

            if (contact.IsAutoSendResponse)
                channel.SendMessage(contact.GetResponse());
        }
Ejemplo n.º 3
0
 public bool IsMatch(Request request)
 {
     return _rx.IsMatch(request.GetPath());
 }
Ejemplo n.º 4
0
        public void Handle(IChannel channel, Request request)
        {
            var response = new Response();
            response.Protocol = request.GetProtocol();
            FileStream reader = null;
            try
            {
                reader = new FileStream(string.Format(_path, _rx.Split(request.GetPath())), FileMode.Open, FileAccess.Read);

                int index = request.GetPath().LastIndexOf(".");
                if(index != -1 && index+1 < request.GetPath().Length)
                {
                    try
                    {
                        string extension = request.GetPath().Substring(index+1);
                        response.ContentType = ContentTypeDictionary[extension];
                    }
                    catch (Exception)
                    {
                    }
                }

                if(response.ContentType.StartsWith("text/"))
                    response.SetContent(new StreamReader(reader).ReadToEnd());
                else
                    response.SetContent(reader);
            }
            catch (Exception)
            {
                response.Status = 404;
            }

            if (reader != null)
                reader.Close();

            channel.SendMessage(response);
        }
Ejemplo n.º 5
0
        private void UpgradeWebSocket(IChannel channel, Request request)
        {
            var response = new Response { Status = 101, Protocol="1.1"};

            var protocol = channel as IKeepProtocolChannel;
            if (protocol == null)
                return;

            var finder = GetWebSocketUriFinder(request);
            if (finder == null)
                return;

            var handler = channel as IKeepHandlerChannel;
            if (handler == null)
                return;

            handler.SetHandler((IChannelHandler)finder.GetHandler(request.GetPath()));
            response.GetHeader().AppendLine("Upgrade: websocket")
                .AppendLine("Connection: Upgrade")
                .AppendLine("Sec-WebSocket-Accept: " + GetWebSocketAcceptCode(request.GetHeader("Sec-WebSocket-Key")));
            channel.SendMessage(response);
            protocol.SetProtocol(WebSocketProtocol.Protocol);
            handler.GetHandler().Connected(channel);
        }
Ejemplo n.º 6
0
        private void Processing(IChannel channel, Request request)
        {
            //웹소켓 요청인가?
            if (request.GetHeader("connection") == "Upgrade")
            {
                if (request.GetHeader("upgrade") == "websocket")
                    UpgradeWebSocket(channel, request);
                return;
            }

            //JSON
            if (request.GetHeader("Accept") != null && request.GetHeader("Accept").IndexOf("application/json") >= 0)
            {
                UriFinder finder = GetJSONUriFinder(request);
                if (finder != null)
                {
                    var response = new Response();
                    response.SetContent(finder.GetHandler(request.GetPath()).ToString());
                    response.ContentType = "application/json";
                    response.Protocol = request.GetProtocol();
                    channel.SendMessage(response);
                    return;
                }
            }

            //여기서 여러가지 예외 처리를!
            IUriHandler handler = GetUriHandler(channel, request);
            if (handler != null)
            {
                handler.Handle(channel, request);
            }
            else
            {
                var response = new Response();
                response.Status = 401;
                response.Protocol = request.GetProtocol();
                channel.SendMessage(response);
            }
        }
Ejemplo n.º 7
0
 private UriFinder GetWebSocketUriFinder(Request request)
 {
     return _wsHandlers.FirstOrDefault(handler => handler.IsMatch(request.GetPath()));
 }
Ejemplo n.º 8
0
 private IUriHandler GetUriHandler(IChannel channel, Request request)
 {
     return _uriHandlers.FirstOrDefault(handler => handler.IsMatch(request));
 }
Ejemplo n.º 9
0
 private UriFinder GetJSONUriFinder(Request request)
 {
     return _jsonHandlers.FirstOrDefault(handler => handler.IsMatch(request.GetPath()));
 }
Ejemplo n.º 10
0
        private void UpgradeWebSocket(IChannel channel, Request request)
        {
            var response = new Response { Status = 101, Protocol="1.1"};

            var finder = GetWebSocketUriFinder(request);
            if (finder == null)
                return;

            channel.SetConfig("handler", finder.GetHandler(request.GetPath()));
            //((IChannelHandler)channel.GetConfig("handler")).GetHandler();
            response.GetHeader().AppendLine("Upgrade: websocket")
                .AppendLine("Connection: Upgrade")
                .AppendLine("Sec-WebSocket-Accept: " + GetWebSocketAcceptCode(request.GetHeader("Sec-WebSocket-Key")));
            channel.SendMessage(response);
            channel.SetConfig("encoder", Protocol.PacketEncoder.WebSocket.WebSocketEncoder.Encoder);
            channel.SetConfig("decoder", Protocol.PacketEncoder.WebSocket.WebSocketDecoder.Decoder);
            //protocol.SetProtocol(WebSocketProtocol.Protocol);
            ((IChannelHandler)channel.GetConfig("handler")).Connected(null/**/);
            //handler.GetHandler().Connected(channel);
        }