/// <summary> /// Create a response for this request. /// </summary> /// <returns>Response</returns> /// <remarks> /// <para> /// If you override this method you have to copy the PipelineIndexKey header like this: /// <code> /// var pipeline = Headers[PipelineIndexKey]; /// if (pipeline != null) /// { /// response.Headers[PipelineIndexKey] = pipeline; /// } /// </code> /// </para> /// </remarks> public override IHttpResponse CreateResponse() { var response = new HttpResponse(200, "OK", HttpVersion); var pipeline = Headers[PipelineIndexKey]; if (pipeline != null) { response.Headers[PipelineIndexKey] = pipeline; } return response; }
private void OnDecoderFailure(ITcpChannel channel, Exception error) { var pos = error.Message.IndexOfAny(new[] {'\r', '\n'}); var descr = pos == -1 ? error.Message : error.Message.Substring(0, pos); var response = new HttpResponse(HttpStatusCode.BadRequest, descr, "HTTP/1.1"); var counter = (int)channel.Data.GetOrAdd(HttpMessage.PipelineIndexKey, x => 1); response.Headers[HttpMessage.PipelineIndexKey] = counter.ToString(); channel.Send(response); channel.Close(); }
public void basic_response() { var frame = new HttpResponse(404, "Failed to find it dude", "HTTP/1.1"); var expected = "HTTP/1.1 404 Failed to find it dude\r\n"; var buffer = new SocketBufferFake(); var encoder = new HttpMessageEncoder(); encoder.Prepare(frame); encoder.Send(buffer); var actual = Encoding.ASCII.GetString(buffer.Buffer, 0, buffer.Count); actual.Substring(0,expected.Length).Should().Be(expected); }
public void response_with_body() { var frame = new HttpResponse(HttpStatusCode.NotFound, "Failed to find it dude", "HTTP/1.1"); frame.AddHeader("X-Requested-With", "XHttpRequest"); frame.ContentType = "text/plain"; frame.Body = new MemoryStream(Encoding.ASCII.GetBytes("hello queue a")); var expected = string.Format("HTTP/1.1 404 Failed to find it dude\r\nServer: griffinframework.net\r\nDate: {0}\r\nContent-Type: text/plain\r\nX-Requested-With: XHttpRequest\r\nContent-Length: 13\r\n\r\nhello queue a", DateTime.UtcNow.ToString("R")); var buffer = new SocketBufferFake(); var encoder = new HttpMessageEncoder(); encoder.Prepare(frame); encoder.Send(buffer); var actual = Encoding.ASCII.GetString(buffer.Buffer, 0, buffer.Count); actual.Should().Be(expected); }