Exemplo n.º 1
0
 public HttpResponseBody(
     Stream rawResponseStream,
     TcpSettings tcpSettings,
     HttpSettings httpSettings,
     IHttpResponse response)
     : this(rawResponseStream, tcpSettings, httpSettings, response, _staticLogger)
 {
 }
Exemplo n.º 2
0
 /// <summary>
 /// Validate the provided TcpSettings
 /// </summary>
 public static void RequireValidTcpSettings(TcpSettings tcpSettings)
 {
     if (tcpSettings.ReadWriteBufferSize <= 0 ||
         tcpSettings.ReadWriteBufferSize >= (8 * 1024 * 1024))
     {
         throw new ArgumentOutOfRangeException(nameof(tcpSettings.ReadWriteBufferSize));
     }
 }
Exemplo n.º 3
0
 static HttpChunkedRequestBody CreateChunkedRequestBody(
     RollbackableStream requestStream,
     HttpRequestHeader requestHeader,
     TcpSettings tcpSettings)
 {
     return(new HttpChunkedRequestBody(
                requestStream, requestHeader, tcpSettings));
 }
Exemplo n.º 4
0
 static void ApplyTcpSettings(ITcpClient client, TcpSettings tcpSettings)
 {
     client.ReceiveTimeout = (int)tcpSettings.IdleTimeout.TotalMilliseconds;
     client.SendTimeout    = (int)tcpSettings.IdleTimeout.TotalMilliseconds;
     client.GetStream().ReadTimeout  = (int)tcpSettings.IdleTimeout.TotalMilliseconds;
     client.GetStream().WriteTimeout = (int)tcpSettings.IdleTimeout.TotalMilliseconds;
     client.SendBufferSize    = tcpSettings.ReadWriteBufferSize;
     client.ReceiveBufferSize = tcpSettings.ReadWriteBufferSize;
 }
Exemplo n.º 5
0
 static void RegisterSettings(
     ContainerBuilder containerBuilder,
     TcpSettings tcpSettings,
     HttpSettings httpSettings)
 {
     containerBuilder
     .Register(x => tcpSettings).AsSelf().SingleInstance();
     containerBuilder
     .Register(x => httpSettings).AsSelf().SingleInstance();
 }
 public HttpChunkedResponseEncoder(
     Stream target,
     TcpSettings tcpSettings,
     HttpSettings httpSettings)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     Validation.RequireValidHttpSettings(httpSettings);
     _buffer       = new MemoryStream(httpSettings.MaxBodyChunkSize);
     _maxChunkSize = httpSettings.MaxBodyChunkSize;
     _target       = target ?? throw new ArgumentNullException(nameof(target));
     _tcpSettings  = tcpSettings;
 }
Exemplo n.º 7
0
        public RollbackableStream(Stream tcpStream, TcpSettings tcpSettings)
        {
            Validation.RequireValidTcpSettings(tcpSettings);

            _original = tcpStream
                        ?? throw new ArgumentNullException(nameof(tcpStream));

            // Allocate read-ahead buffer:
            // It should be large enough, at least equal to the TCP read buffer,
            // otherwise rollback calls will fail.
            _readAheadBuffer = new BinaryStack(tcpSettings.ReadWriteBufferSize);
        }
Exemplo n.º 8
0
 public HttpRequest(
     Stream requestStream,
     TcpSettings tcpSettings,
     IHttpRequestBodyFactory requestBodyFactory)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     _requestStream = new RollbackableStream(
         requestStream ?? throw new ArgumentNullException(nameof(requestStream)),
         tcpSettings);
     _tcpSettings        = tcpSettings;
     _requestBodyFactory = requestBodyFactory
                           ?? throw new ArgumentNullException(nameof(requestBodyFactory));
 }
Exemplo n.º 9
0
 public HttpChunkedRequestBody(
     RollbackableStream requestStream,
     HttpRequestHeader header,
     TcpSettings tcpSettings)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     _requestStream = requestStream
                      ?? throw new ArgumentNullException(nameof(requestStream));
     _header = header
               ?? throw new ArgumentNullException(nameof(header));
     _readBuffer    = new byte[tcpSettings.ReadWriteBufferSize];
     _trailerReader = new HttpChunkedTrailerReader(
         _requestStream,
         tcpSettings.ReadWriteBufferSize);
 }
Exemplo n.º 10
0
 public HttpResponse(
     IHttpRequest request,
     Stream rawResponseStream,
     TcpSettings tcpSettings,
     HttpSettings httpSettings)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     _request           = request;
     _rawResponseStream = rawResponseStream
                          ?? throw new ArgumentNullException(nameof(rawResponseStream));
     _tcpSettings       = tcpSettings;
     _header            = new HttpResponseHeader();
     _header.StatusCode = 200;
     _body = new HttpResponseBody(_rawResponseStream,
                                  tcpSettings, httpSettings, this);
 }
Exemplo n.º 11
0
 public TcpClientHandler(
     ISslService sslService,
     ITcpSessionFactory tcpSessionFactory,
     IWatchDog tcpWatchDog,
     TcpSettings tcpSettings)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     _sslService = sslService
                   ?? throw new ArgumentNullException(nameof(sslService));
     _tcpSessionFactory = tcpSessionFactory
                          ?? throw new ArgumentNullException(nameof(tcpSessionFactory));
     _tcpWatchDog = tcpWatchDog
                    ?? throw new ArgumentNullException(nameof(tcpWatchDog));
     _tcpWatchDog.MaxSessionDuration = tcpSettings.IdleTimeout;
     _tcpSettings = tcpSettings;
 }
Exemplo n.º 12
0
 internal HttpResponseBody(
     Stream rawResponseStream,
     TcpSettings tcpSettings,
     HttpSettings httpSettings,
     IHttpResponse response,
     ILogger logger)
 {
     Validation.RequireValidHttpSettings(httpSettings);
     Validation.RequireValidTcpSettings(tcpSettings);
     _tcpSettings = tcpSettings;
     _logger      = logger
                    ?? throw new ArgumentNullException(nameof(logger));
     _rawResponseStream = rawResponseStream
                          ?? throw new ArgumentNullException(nameof(rawResponseStream));
     _response = response
                 ?? throw new ArgumentNullException(nameof(response));
     _httpSettings    = httpSettings;
     _buffer          = new MemoryStream(httpSettings.MaxBodyChunkSize);
     _buffer.Position = 0;
 }
Exemplo n.º 13
0
 public HttpSession(
     Stream connection,
     TcpSettings tcpSettings,
     IContent content,
     IContentSettingsReadOnly contentSettings,
     IHttpKeepAliveService keepAliveService,
     IHttpRequestInternal request,
     IHttpResponseInternal response)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     _tcpSettings = tcpSettings;
     _connection  = connection
                    ?? throw new ArgumentNullException(nameof(connection));
     _content = content
                ?? throw new ArgumentNullException(nameof(content));
     _contentSettings = contentSettings
                        ?? throw new ArgumentNullException(nameof(contentSettings));
     _keepAliveService = keepAliveService
                         ?? throw new ArgumentNullException(nameof(keepAliveService));
     _request = request
                ?? throw new ArgumentNullException(nameof(request));
     _response = response
                 ?? throw new ArgumentNullException(nameof(response));
 }
Exemplo n.º 14
0
 public void SetTcpSettings(TcpSettings tcpSettings)
 {
     Validation.RequireValidTcpSettings(tcpSettings);
     _tcpSettings = tcpSettings;
 }
Exemplo n.º 15
0
        public ReadOnlyStream Create(
            TcpSettings tcpSettings,
            HttpRequestHeader requestHeader,
            RollbackableStream requestStream)
        {
            // Message body length https://tools.ietf.org/html/rfc7230#section-3.3.3
            // 1.
            // If a Transfer - Encoding header field is present and the chunked
            // transfer coding(Section 4.1) is the final encoding, the message
            // body length is determined by reading and decoding the chunked
            // data until the transfer coding indicates the data is complete.
            if (requestHeader.ContainsKey(HttpKeys.TransferEncoding))
            {
                var encodings     = Split(requestHeader.Get(HttpKeys.TransferEncoding, false).Last());
                var finalEncoding = encodings[encodings.Length - 1];
                if (HeaderValueEquals(finalEncoding, HttpKeys.ChunkedValue))
                {
                    // If a message is received with both a Transfer - Encoding and a
                    // Content - Length header field, the Transfer - Encoding overrides the
                    // Content - Length.Such a message might indicate an attempt to
                    // perform request smuggling(Section 9.5) or response splitting
                    // (Section 9.4) and ought to be handled as an error.A sender MUST
                    // remove the received Content - Length field prior to forwarding such
                    // a message downstream.
                    if (requestHeader.ContainsKey(HttpKeys.ContentLength))
                    {
                        requestHeader.Remove(HttpKeys.ContentLength);
                    }

                    return(CreateChunkedRequestBody(requestStream, requestHeader, tcpSettings));
                }
                // If a Transfer - Encoding header field
                // is present in a request and the chunked transfer coding is not
                // the final encoding, the message body length cannot be determined
                // reliably; the server MUST respond with the 400(Bad Request)
                // status code and then close the connection.
                else
                {
                    ThrowForLastTransferEncodingIsNotChunked();
                }
            }
            // If a message is received without Transfer - Encoding and with
            // either multiple Content - Length header fields having differing
            // field - values or a single Content-Length header field having an
            // invalid value, then the message framing is invalid and the
            // recipient MUST treat it as an unrecoverable error.  If this is a
            // request message, the server MUST respond with a 400(Bad Request)
            // status code and then close the connection.
            //
            // If a valid Content-Length header field is present without
            // Transfer - Encoding, its decimal value defines the expected message
            // body length in octets.
            else if (requestHeader.ContainsKey(HttpKeys.ContentLength))
            {
                return(CreateFixedLengthRequestBody(requestStream, requestHeader.GetContentLength()));
            }
            // If this is a request message and none of the above are true, then
            // the message body length is zero (no message body is present).
            else
            {
                return(CreateEmptyRequestBody());
            }

            // Woudln't reach here,
            // But just to make the compiler happy.
            return(null);
        }