Esempio n. 1
0
        /// <summary>
        ///     Writes HTTP message to wrapped stream
        /// </summary>
        /// <param name="header">HTTP message header</param>
        /// <param name="body">HTTP message body</param>
        /// <param name="bodyLength">expected length of HTTP message body</param>
        public void Write(HttpMessageHeader header, Stream body = null, Nullable <Int64> bodyLength = null)
        {
            Contract.Requires <ArgumentNullException>(header != null, "header");

            var writer = new StreamWriter(OutputStream, Encoding.ASCII);

            writer.WriteLine(header.StartLine);

            foreach (String headerLine in header.Headers.Lines)
            {
                writer.WriteLine(headerLine);
            }

            writer.WriteLine();
            writer.Flush();

            if (body == null)
            {
                return;
            }

            if (WriteBody(header, body, bodyLength.GetValueOrDefault(0)))
            {
                writer.WriteLine();
                writer.Flush();
            }
        }
Esempio n. 2
0
        public static void WriteHttpTrace(this StringBuilder stringBuilder, HttpMessageHeader messageHeader)
        {
            if (messageHeader == null)
            {
                return;
            }

            stringBuilder
            .AppendFormat("StartLine: {0}", messageHeader.StartLine)
            .AppendLine();

            var headers = messageHeader.Headers.Lines.ToList();

            if (headers.Count == 0)
            {
                return;
            }

            stringBuilder.AppendLine("Headers:");

            foreach (var header in headers)
            {
                stringBuilder
                .AppendFormat("    {0}", header)
                .AppendLine();
            }
        }
Esempio n. 3
0
        public String ShouldWriteHttpMessage(HttpMessageHeader header, Stream body, long?bodyLength)
        {
            var outputStream = new MemoryStream();

            var httpWriter = new HttpMessageWriter(outputStream);

            httpWriter.Write(header, body, bodyLength);

            return(Encoding.ASCII.GetString(outputStream.ToArray()));
        }
Esempio n. 4
0
 protected override bool WriteBody(HttpMessageHeader header, Stream body, long bodyLength)
 {
     if (!IsRedirect(header as HttpResponseHeader))
     {
         return(base.WriteBody(header, body, bodyLength));
     }
     else
     {
         this.OnLog(LogLevel.Debug, "Skipping redirect response body");
         return(false);
     }
 }
Esempio n. 5
0
        public static string GetHttpTrace(HttpMessageHeader header)
        {
            if (header == null)
            {
                return(string.Empty);
            }

            var sb = new StringBuilder();

            WriteHttpTrace(sb, header);

            return(sb.ToString());
        }
Esempio n. 6
0
 protected override bool WriteBody(HttpMessageHeader header, Stream body, Int64 bodyLength)
 {
     if (!IsRedirect(header as HttpResponseHeader))
     {
         return(base.WriteBody(header, body, bodyLength));
     }
     else
     {
         if (Logger.IsDebugEnabled)
         {
             Logger.Debug("Skipping redirect response body");
         }
         return(false);
     }
 }
Esempio n. 7
0
 /// <summary>
 ///     Writes messag body to <seealso cref="OutputStream"/>
 /// </summary>
 /// <param name="header">HTTP message header</param>
 /// <param name="body">HTTP message body</param>
 /// <param name="bodyLength">expected length of HTTP message body</param>
 /// <returns>True when body content was written to output stream and false otherwise</returns>
 protected virtual bool WriteBody(HttpMessageHeader header, Stream body, long bodyLength)
 {
     if (header.Chunked)
     {
         CopyChunkedMessageBody(body);
     }
     else if (header.EntityHeaders.ContentLength.HasValue && header.EntityHeaders.ContentLength.Value > 0)
     {
         CopyPlainMessageBody(body, header.EntityHeaders.ContentLength.Value);
     }
     else if (bodyLength > 0)
     {
         body.CopyTo(OutputStream);
     }
     else
     {
         OnLog(LogLevel.Debug, "Message body is empty");
         return(false);
     }
     return(true);
 }
Esempio n. 8
0
 /// <summary>
 ///     Writes messag body to <seealso cref="OutputStream"/>
 /// </summary>
 /// <param name="header">HTTP message header</param>
 /// <param name="body">HTTP message body</param>
 /// <param name="bodyLength">expected length of HTTP message body</param>
 /// <returns>True when body content was written to output stream and false otherwise</returns>
 protected virtual bool WriteBody(HttpMessageHeader header, Stream body, Int64 bodyLength)
 {
     if (header.Chunked)
     {
         CopyChunkedMessageBody(body);
     }
     else if (header.EntityHeaders.ContentLength.HasValue && header.EntityHeaders.ContentLength.Value > 0)
     {
         CopyPlainMessageBody(body, header.EntityHeaders.ContentLength.Value);
     }
     else if (bodyLength > 0)
     {
         body.CopyTo(OutputStream);
     }
     else
     {
         if (Logger.IsDebugEnabled)
         {
             Logger.Debug("Message body is empty");
         }
         return(false);
     }
     return(true);
 }
 public MessageWriterTestCaseBuilder(HttpMessageHeader header, String body)
 {
     _messageBody       = body;
     _httpMessageHeader = header;
 }