Exemplo n.º 1
0
        public BrewRequest(BaristaContext context, HttpRequest request)
        {
            m_context     = context ?? throw new ArgumentNullException(nameof(context));
            m_httpRequest = request ?? throw new ArgumentNullException(nameof(request));

            Method    = request.Method;
            m_headers = new Headers(context);
            foreach (var header in request.Headers)
            {
                m_headers.Append(header.Key, header.Value);
            }
        }
Exemplo n.º 2
0
        public void AppendList <T>(string name, IList <T> values)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (values == null)
            {
                throw new ArgumentNullException(nameof(values));
            }

            Headers.Append(name, values.Select(value => value.ToString()).ToArray());
        }
Exemplo n.º 3
0
        public void WriteTest()
        {
            const string additional_header_name = "Name-Length";

            var result = new StringBuilder(365);

            using (var writer = result.CreateWriter())
                GetStudents().Take(5)
                .AsCSV(';')
                .AddDefaultHeaders()
                .AddColumn(additional_header_name, s => s.Name.Length)
                .WriteTo(writer);

            using var reader = result.CreateReader();
            var header_line       = reader.ReadLine();
            var header_components = header_line?.Split(ValuesSeparator);

            Assert.That.Collection(header_components).IsEqualTo(Headers.Append(additional_header_name).ToArray());
        }
Exemplo n.º 4
0
        internal HttpApi.HTTP_FLAGS ComputeHeaders(bool endOfRequest = false, int bufferedBytes = 0)
        {
            Headers.IsReadOnly = false; // Temporarily allow modification.

            // 401
            if (StatusCode == (ushort)HttpStatusCode.Unauthorized)
            {
                RequestContext.Server.AuthenticationManager.SetAuthenticationChallenge(RequestContext);
            }

            var flags = HttpApi.HTTP_FLAGS.NONE;

            Debug.Assert(!ComputedHeaders, "HttpListenerResponse::ComputeHeaders()|ComputedHeaders is true.");
            _responseState = ResponseState.ComputedHeaders;

            // Gather everything from the request that affects the response:
            var requestVersion          = Request.ProtocolVersion;
            var requestConnectionString = Request.Headers[HttpKnownHeaderNames.Connection];
            var isHeadRequest           = Request.IsHeadMethod;
            var requestCloseSet         = Matches(Constants.Close, requestConnectionString);

            // Gather everything the app may have set on the response:
            // Http.Sys does not allow us to specify the response protocol version, assume this is a HTTP/1.1 response when making decisions.
            var responseConnectionString = Headers[HttpKnownHeaderNames.Connection];
            var transferEncodingString   = Headers[HttpKnownHeaderNames.TransferEncoding];
            var responseContentLength    = ContentLength;
            var responseCloseSet         = Matches(Constants.Close, responseConnectionString);
            var responseChunkedSet       = Matches(Constants.Chunked, transferEncodingString);
            var statusCanHaveBody        = CanSendResponseBody(_requestContext.Response.StatusCode);

            // Determine if the connection will be kept alive or closed.
            var keepConnectionAlive = true;

            if (requestVersion <= Constants.V1_0 || // Http.Sys does not support "Keep-Alive: true" or "Connection: Keep-Alive"
                (requestVersion == Constants.V1_1 && requestCloseSet) ||
                responseCloseSet)
            {
                keepConnectionAlive = false;
            }

            // Determine the body format. If the user asks to do something, let them, otherwise choose a good default for the scenario.
            if (responseContentLength.HasValue)
            {
                _boundaryType = BoundaryType.ContentLength;
                // ComputeLeftToWrite checks for HEAD requests when setting _leftToWrite
                _expectedBodyLength = responseContentLength.Value;
            }
            else if (responseChunkedSet)
            {
                // The application is performing it's own chunking.
                _boundaryType = BoundaryType.PassThrough;
            }
            else if (endOfRequest && !(isHeadRequest && statusCanHaveBody)) // HEAD requests should always end without a body. Assume a GET response would have a body.
            {
                if (bufferedBytes > 0)
                {
                    Headers[HttpKnownHeaderNames.ContentLength] = bufferedBytes.ToString(CultureInfo.InvariantCulture);
                }
                else if (statusCanHaveBody)
                {
                    Headers[HttpKnownHeaderNames.ContentLength] = Constants.Zero;
                }
                _boundaryType       = BoundaryType.ContentLength;
                _expectedBodyLength = bufferedBytes;
            }
            else if (keepConnectionAlive && requestVersion == Constants.V1_1)
            {
                _boundaryType = BoundaryType.Chunked;
                Headers[HttpKnownHeaderNames.TransferEncoding] = Constants.Chunked;
            }
            else
            {
                // The length cannot be determined, so we must close the connection
                keepConnectionAlive = false;
                _boundaryType       = BoundaryType.Close;
            }

            // Managed connection lifetime
            if (!keepConnectionAlive)
            {
                // All Http.Sys responses are v1.1, so use 1.1 response headers
                // Note that if we don't add this header, Http.Sys will often do it for us.
                if (!responseCloseSet)
                {
                    Headers.Append(HttpKnownHeaderNames.Connection, Constants.Close);
                }
                flags = HttpApi.HTTP_FLAGS.HTTP_SEND_RESPONSE_FLAG_DISCONNECT;
            }

            Headers.IsReadOnly = true; // Prohibit further modifications.
            return(flags);
        }
Exemplo n.º 5
0
 public void Append([NotNull] string name, [NotNull] object value)
 {
     Headers.Append(name, value.ToString());
 }