public static void WriteEmptyResponse(this HttpMessageWriter writer, StatusCodes statusCode)
 {
     writer.WriteStatusLine(statusCode);
     writer.WriteAuxiliaryHeaders();
     writer.WriteContentLength(0);
     writer.WriteCRLF();
 }
Beispiel #2
0
        public void ProcessIncomingRequest(Connection c)
        {
            if (c.HttpReader.HasUpgarde(Upgrades.Websocket))
            {
                // c.HttpReader.SecWebSocketProtocol

                var    key = c.HttpReader.SecWebSocketKey.ToString();
                byte[] accept;
                lock (sha1)
                    accept = sha1.ComputeHash(Encoding.ASCII.GetBytes(key + guid));

                using (var writer = new HttpMessageWriter())
                {
                    writer.WriteStatusLine(StatusCodes.SwitchingProtocols);
                    writer.WriteConnectionUpgrade();
                    writer.WriteUpgradeWebsocket();
                    writer.WriteSecWebSocketAccept(accept);
                    writer.WriteSecWebSocketProtocol(sip);
                    writer.WriteContentLength(0);
                    writer.WriteCRLF();

                    SendResponse(c, writer, InvalidAgentIndex);
                }

                c.UpgradeToWebsocket();
            }
            else
            {
                base.ProcessIncomingRequest(c);
            }
        }
 public static void WriteOptionsResponse(this HttpMessageWriter writer)
 {
     writer.WriteStatusLine(StatusCodes.OK);
     writer.WriteAuxiliaryHeaders();
     writer.WriteAllow(Methods.Get, Methods.Options);
     writer.WriteContentLength(0);
     writer.WriteCRLF();
 }
 public static void WriteResponse(this HttpMessageWriter writer, StatusCodes statusCodes, ContentType contentType, byte[] content)
 {
     writer.WriteStatusLine(statusCodes);
     writer.WriteAuxiliaryHeaders();
     writer.WriteContentType(contentType);
     writer.WriteContentLength(content.Length);
     writer.WriteCRLF();
     writer.Write(content);
 }
 public static void WriteNotFinishedResponse(this HttpMessageWriter writer, StatusCodes statusCode, ContentType contentType)
 {
     writer.WriteStatusLine(statusCode);
     writer.WriteAuxiliaryHeaders();
     if (contentType != ContentType.None)
     {
         writer.WriteContentType(contentType);
     }
     else
     {
         writer.WriteContentLength(0);
     }
 }
Beispiel #6
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));
                    }
                }
            }
        }
Beispiel #7
0
        private static void WriteHttpHeader(ServerAsyncEventArgs e)
        {
            using (var writer = new HttpMessageWriter())
            {
                writer.WriteStatusLine(StatusCodes.OK);
                writer.WriteContentLength(e.Count);
                writer.WriteAccessControlHeaders();
                writer.WriteCRLF();

                e.OffsetOffset -= writer.Count;
                e.Count        += writer.Count;
                Buffer.BlockCopy(writer.Buffer, writer.Offset, e.Buffer, e.Offset, writer.Count);
            }
        }
Beispiel #8
0
        private void SendJsonMessage(Session session)
        {
            using (var writer = new HttpMessageWriter())
            {
                //byte[] json = Encoding.UTF8.GetBytes(string.Format(@"{{id:{0}}}", session.Id));
                byte[] json = Encoding.UTF8.GetBytes(string.Format(@"{0}", session.Id));

                writer.WriteStatusLine(StatusCodes.OK);
                //writer.Write(Encoding.UTF8.GetBytes("Content-Type: application/json\r\n"));
                writer.WriteContentLength(json.Length);
                writer.WriteAccessControlHeaders();
                writer.WriteCRLF();
                writer.Write(json);

                SendWriter(session, writer);
            }
        }
Beispiel #9
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);
            }
        }