<commit_before>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Http.Headers;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Http
{
    internal sealed partial class HttpConnection : IDisposable
    {
        private const int InitialReadBufferSize =
#if DEBUG
            10;
#else
            4096;
#endif
        private const int InitialWriteBufferSize = InitialReadBufferSize;

        private static readonly byte[] s_contentLength0NewlineAsciiBytes = Encoding.ASCII.GetBytes("Content-Length: 0\r\n");
        private static readonly byte[] s_spaceHttp11NewlineAsciiBytes = Encoding.ASCII.GetBytes(" HTTP/1.1\r\n");
        private static readonly byte[] s_hostKeyAndSeparator = Encoding.ASCII.GetBytes(HttpKnownHeaderNames.Host + ": ");

        private readonly HttpConnectionPool _pool;
        private readonly HttpConnectionKey _key;
        private readonly Stream _stream;
        private readonly TransportContext _transportContext;
        private readonly bool _usingProxy;
        private readonly byte[] _idnHostAsciiBytes;

        private ValueStringBuilder _sb; // mutable struct, do not make this readonly

        private HttpRequestMessage _currentRequest;
        private readonly byte[] _writeBuffer;
        private int _writeOffset;

        private Task<int> _readAheadTask;
        private byte[] _readBuffer;
        private int _readOffset;
        private int _readLength;

        private bool _connectionClose; // Connection: close was seen on last response

        private bool _disposed;

        public HttpConnection(
            HttpConnectionPool pool,
            HttpConnectionKey key,
            string requestIdnHost,
            Stream stream, 
            TransportContext transportContext, 
            bool usingProxy)
        {
            Debug.Assert(pool != null);
            Debug.Assert(stream != null);

            _pool = pool;
            _key = key;
            _stream = stream;
            _transportContext = transportContext;
            _usingProxy = usingProxy;
            if (requestIdnHost != null)
            {
                _idnHostAsciiBytes = Encoding.ASCII.GetBytes(requestIdnHost);
            }

            const int DefaultCapacity = 16;
            _sb = new ValueStringBuilder(DefaultCapacity);

            _writeBuffer = new byte[InitialWriteBufferSize];
            _writeOffset = 0;

            _readBuffer = new byte[InitialReadBufferSize];
            _readLength = 0;
            _readOffset = 0;

            _connectionClose = false;
            _disposed = false;

            if (NetEventSource.IsEnabled) Trace("Connection created.");
        }
<commit_msg>Add some more tracing to ManagedHandler<commit_after>﻿// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Net.Http.Headers;
using System.Net.Security;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace System.Net.Http
{
    internal sealed partial class HttpConnection : IDisposable
    {
        private const int InitialReadBufferSize =
#if DEBUG
            10;
#else
            4096;
#endif
        private const int InitialWriteBufferSize = InitialReadBufferSize;

        private static readonly byte[] s_contentLength0NewlineAsciiBytes = Encoding.ASCII.GetBytes("Content-Length: 0\r\n");
        private static readonly byte[] s_spaceHttp11NewlineAsciiBytes = Encoding.ASCII.GetBytes(" HTTP/1.1\r\n");
        private static readonly byte[] s_hostKeyAndSeparator = Encoding.ASCII.GetBytes(HttpKnownHeaderNames.Host + ": ");

        private readonly HttpConnectionPool _pool;
        private readonly HttpConnectionKey _key;
        private readonly Stream _stream;
        private readonly TransportContext _transportContext;
        private readonly bool _usingProxy;
        private readonly byte[] _idnHostAsciiBytes;

        private ValueStringBuilder _sb; // mutable struct, do not make this readonly

        private HttpRequestMessage _currentRequest;
        private readonly byte[] _writeBuffer;
        private int _writeOffset;

        private Task<int> _readAheadTask;
        private byte[] _readBuffer;
        private int _readOffset;
        private int _readLength;

        private bool _connectionClose; // Connection: close was seen on last response

        private bool _disposed;

        public HttpConnection(
            HttpConnectionPool pool,
            HttpConnectionKey key,
            string requestIdnHost,
            Stream stream, 
            TransportContext transportContext, 
            bool usingProxy)
        {
            Debug.Assert(pool != null);
            Debug.Assert(stream != null);

            _pool = pool;
            _key = key;
            _stream = stream;
            _transportContext = transportContext;
            _usingProxy = usingProxy;
            if (requestIdnHost != null)
            {
                _idnHostAsciiBytes = Encoding.ASCII.GetBytes(requestIdnHost);
            }

            const int DefaultCapacity = 16;
            _sb = new ValueStringBuilder(DefaultCapacity);

            _writeBuffer = new byte[InitialWriteBufferSize];
            _writeOffset = 0;

            _readBuffer = new byte[InitialReadBufferSize];
            _readLength = 0;
            _readOffset = 0;

            _connectionClose = false;
            _disposed = false;

            if (NetEventSource.IsEnabled)
            {
                if (_stream is SslStream sslStream)
                {
                    Trace(
                        $"Secure connection created to {key.Host}:{key.Port}. " +
                        $"SslProtocol:{sslStream.SslProtocol}, " +
                        $"CipherAlgorithm:{sslStream.CipherAlgorithm}, CipherStrength:{sslStream.CipherStrength}, " +
                        $"HashAlgorithm:{sslStream.HashAlgorithm}, HashStrength:{sslStream.HashStrength}, " +
                        $"KeyExchangeAlgorithm:{sslStream.KeyExchangeAlgorithm}, KeyExchangeStrength:{sslStream.KeyExchangeStrength}, " +
                        $"LocalCert:{sslStream.LocalCertificate}, RemoteCert:{sslStream.RemoteCertificate}");
                }
                else
                {
                    Trace($"Connection created to {key.Host}:{key.Port}.");
                }
            }
        }

{'fraction_non_alphanumeric': 0.0707723227103072, 'fraction_numerical': 0.004880849842090152, 'mean_word_length': 2.2693571093383387, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7487467', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1670, 'n_words': 478}