Пример #1
0
        void IHttpServerAgent.HandleRequest(BaseConnection c, HttpMessageReader httpReader, ArraySegment <byte> httpContent)
        {
            if (httpReader.Method != Methods.Get)
            {
                var writer = new HttpMessageWriter();
                writer.WriteResponse(StatusCodes.NotAcceptable);

                httpServer.SendResponse(c, writer);
            }
            else
            {
                if (httpReader.RequestUri.Equals(uri1))
                {
                    var writer = new HttpMessageWriter();

                    writer.WriteStatusLine(StatusCodes.MovedPermanently);
                    writer.WriteContentLength(0);
                    writer.WriteLocation(c.LocalEndPoint.Protocol == ServerProtocol.Tcp, httpReader.Host.Host, httpReader.Host.Port, uri2);
                    writer.WriteCRLF();

                    httpServer.SendResponse(c, writer);
                }
                else
                {
                    var rawFile = GetFile(httpReader);

                    if (rawFile.HasValue == false)
                    {
                        var writer = new HttpMessageWriter();
                        writer.WriteResponse(StatusCodes.NotFound);

                        httpServer.SendResponse(c, writer);
                    }
                    else
                    {
                        var file = rawFile.Value;

                        var writer = new HttpMessageWriter();
                        writer.WriteStatusLine(StatusCodes.OK);
                        writer.WriteContentType(file.ContentType);
                        writer.WriteContentLength(file.Content.Length);
                        writer.WriteCRLF();

                        httpServer.SendResponse(c, writer);

                        httpServer.SendResponse(c, new ArraySegment <byte>(file.Content));
                    }
                }
            }
        }
Пример #2
0
        public void ProcessIncomingRequest(BaseHttpConnection c)
        {
            bool handled = false;

            for (int i = 0; i < agents.Length; i++)
            {
                var result = agents[i].Value.IsHandled(c.HttpReader);
                if (result.IsHandled)
                {
                    bool isAuthorized = true;

                    if (agents[i].IsAuthorizationEnabled && result.IsAuthorizationRequred)
                    {
                        var writer = IsAuthorized(c, result.Realm, i);
                        if (writer != null)
                        {
                            isAuthorized = false;
                            SendResponse(c, writer, InvalidAgentIndex);
                        }
                    }

                    if (isAuthorized)
                    {
                        agents[i].Value.HandleRequest(c, c.HttpReader, c.Content);
                    }

                    handled = true;

                    break;
                }
            }

            if (handled == false)
            {
                var writer = new HttpMessageWriter();

                writer.WriteStatusLine(StatusCodes.TemporaryRedirect);
                writer.WriteLocation(c.LocalEndPoint.Protocol == ServerProtocol.Tcp, c.HttpReader.Host.Host, c.HttpReader.Host.Port, defaultUri);
                writer.WriteContentLength(0);
                writer.WriteCRLF();

                SendResponse(c, writer, InvalidAgentIndex);
            }
        }