public static void WriteEmptyResponse(this HttpMessageWriter writer, StatusCodes statusCode) { writer.WriteStatusLine(statusCode); writer.WriteAuxiliaryHeaders(); writer.WriteContentLength(0); writer.WriteCRLF(); }
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 int CopyTo(Func<int, ArraySegment<byte>> getBuffer) //{ // lock (tuples) // { // int length = CountLength(); // var segment = getBuffer(length); // if (segment.Count >= length) // { // int offset = 0; // BlockCopy(openPresence, segment, ref offset, openPresence.Length); // for (int i = 0; i < tuples.Count; i++) // { // Buffer.BlockCopy(tuples[i].Data.Array, tuples[i].Data.Offset, // segment.Array, segment.Offset + offset, tuples[i].Data.Count); // offset += tuples[i].Data.Count; // } // BlockCopy(closePresence, segment, ref offset, closePresence.Length); // return length; // } // return 0; // } //} public void WriteLenghtAndContent(HttpMessageWriter writer) { lock (tuples) { int length = CountLength(); writer.WriteContentLength(length); writer.WriteCRLF(); WriteContent(writer, length); } }
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); } }
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)); } } } }
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); } }
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); } }
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); } }