Example #1
0
 public HttpContact(IChannel channel, Request request, Response response = null)
 {
     _channel = channel;
     _request = request;
     _response = response;
     IsAutoSendResponse = true;
 }
        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);
        }
Example #3
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);
        }
Example #4
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);
            }
        }
Example #5
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);
        }