<commit_before>﻿/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Runtime.Serialization;

using DotCMIS.Enums;
using DotCMIS.Exceptions;
using DotCMIS.Util;

namespace DotCMIS.Binding
{
    public class HttpWebRequestResource : IDisposable
    {
        private static object ResourceLock = new object();
        private static HashSet<HttpWebRequest> ResourceSet = new HashSet<HttpWebRequest>(); 

        private HttpWebRequest Request;

        public static void AbortAll()
        {
            lock (ResourceLock) {
                foreach (HttpWebRequest request in ResourceSet) {
                    request.Abort ();
                }
            }
        }

        public HttpWebRequestResource()
        {
            Request = null;
        }

        public void StartResource(HttpWebRequest request)
        {
            lock (ResourceLock) {
                if (Request != null) {
                    ResourceSet.Remove (Request);
                }
                Request = request;
                ResourceSet.Add (Request);
            }
        }

        public void Dispose()
        {
            if (Request != null) {
                lock (ResourceLock) {
                    ResourceSet.Remove (Request);
                }
            }
        }
    }

}

namespace DotCMIS.Binding.Impl
{
    internal static class HttpUtils
    {
        public delegate void Output(Stream stream);

        public static Response InvokeGET(UrlBuilder url, BindingSession session)
        {
            return Invoke(url, "GET", null, null, session, null, null, null);
        }

        public static Response InvokeGET(UrlBuilder url, BindingSession session, long? offset, long? length)
        {
            return Invoke(url, "GET", null, null, session, offset, length, null);
        }

        public static Response InvokePOST(UrlBuilder url, String contentType, Output writer, BindingSession session)
        {
            return Invoke(url, "POST", contentType, writer, session, null, null, null);
        }

        public static Response InvokePUT(UrlBuilder url, String contentType, IDictionary<string, string> headers, Output writer, BindingSession session)
        {
            return Invoke(url, "PUT", contentType, writer, session, null, null, headers);
        }

        public static Response InvokeDELETE(UrlBuilder url, BindingSession session)
        {
            return Invoke(url, "DELETE", null, null, session, null, null, null);
        }

        private static Response Invoke(UrlBuilder url, String method, String contentType, Output writer, BindingSession session,
                long? offset, long? length, IDictionary<string, string> headers)
        {
            Guid tag = Guid.NewGuid();
            string request = method + " " + url;
            Stopwatch watch = new Stopwatch();
            watch.Start();
            using (HttpWebRequestResource resource = new HttpWebRequestResource ())
            {
            try
            {
                // log before connect
                Trace.WriteLineIf(DotCMISDebug.DotCMISSwitch.TraceInfo, string.Format("[{0}] starting {1}", tag.ToString(), request));

                //Handles infrequent networking conditions
                int retry = 0;
                for(;;){

                    // create connection
                    HttpWebRequest conn = (HttpWebRequest)WebRequest.Create(url.Url);
                    conn.Method = method;
                    resource.StartResource(conn);

                    // device management
                    string deviceIdentifier = session.GetValue(SessionParameter.DeviceIdentifier) as String;
                    if(deviceIdentifier != null)
                    {
                        conn.Headers.Add("Device-ID", deviceIdentifier);
                    }

                    // user agent
                    string userAgent = session.GetValue(SessionParameter.UserAgent) as String;
                    conn.UserAgent = userAgent == null ? "Apache Chemistry DotCMIS" : userAgent;

                    // timeouts
                    int connectTimeout = session.GetValue(SessionParameter.ConnectTimeout, -2);
                    if (connectTimeout >= -1)
                    {
                        conn.Timeout = connectTimeout;
                    }

                    int readTimeout = session.GetValue(SessionParameter.ReadTimeout, -2);
                    if (readTimeout >= -1)<commit_msg>Tiny improvements on http connection creation<commit_after>﻿/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Runtime.Serialization;

using DotCMIS.Enums;
using DotCMIS.Exceptions;
using DotCMIS.Util;

namespace DotCMIS.Binding
{
    public class HttpWebRequestResource : IDisposable
    {
        private static object ResourceLock = new object();
        private static HashSet<HttpWebRequest> ResourceSet = new HashSet<HttpWebRequest>(); 

        private HttpWebRequest Request;

        public static void AbortAll()
        {
            lock (ResourceLock) {
                foreach (HttpWebRequest request in ResourceSet) {
                    request.Abort ();
                }
            }
        }

        public HttpWebRequestResource()
        {
            Request = null;
        }

        public void StartResource(HttpWebRequest request)
        {
            lock (ResourceLock) {
                if (Request != null) {
                    ResourceSet.Remove (Request);
                }
                Request = request;
                ResourceSet.Add (Request);
            }
        }

        public void Dispose()
        {
            if (Request != null) {
                lock (ResourceLock) {
                    ResourceSet.Remove (Request);
                }
            }
        }
    }

}

namespace DotCMIS.Binding.Impl
{
    internal static class HttpUtils
    {
        public delegate void Output(Stream stream);

        public static Response InvokeGET(UrlBuilder url, BindingSession session)
        {
            return Invoke(url, "GET", null, null, session, null, null, null);
        }

        public static Response InvokeGET(UrlBuilder url, BindingSession session, long? offset, long? length)
        {
            return Invoke(url, "GET", null, null, session, offset, length, null);
        }

        public static Response InvokePOST(UrlBuilder url, String contentType, Output writer, BindingSession session)
        {
            return Invoke(url, "POST", contentType, writer, session, null, null, null);
        }

        public static Response InvokePUT(UrlBuilder url, String contentType, IDictionary<string, string> headers, Output writer, BindingSession session)
        {
            return Invoke(url, "PUT", contentType, writer, session, null, null, headers);
        }

        public static Response InvokeDELETE(UrlBuilder url, BindingSession session)
        {
            return Invoke(url, "DELETE", null, null, session, null, null, null);
        }

        private static Response Invoke(UrlBuilder url, String method, String contentType, Output writer, BindingSession session,
                long? offset, long? length, IDictionary<string, string> headers)
        {
            Guid tag = Guid.NewGuid();
            string request = method + " " + url;
            Stopwatch watch = new Stopwatch();
            watch.Start();
            using (HttpWebRequestResource resource = new HttpWebRequestResource ())
            {
            try
            {
                // log before connect
                Trace.WriteLineIf(DotCMISDebug.DotCMISSwitch.TraceInfo, string.Format("[{0}] starting {1}", tag.ToString(), request));

                //Handles infrequent networking conditions
                int retry = 0;
                for(;;){

                    // create connection
                    HttpWebRequest conn = WebRequest.CreateHttp(url.Url);
                    conn.Method = method;
                    resource.StartResource(conn);

                    // device management
                    string deviceIdentifier = session.GetValue(SessionParameter.DeviceIdentifier) as String;
                    if(deviceIdentifier != null)
                    {
                        conn.Headers.Add("Device-ID", deviceIdentifier);
                    }

                    // user agent
                    string userAgent = session.GetValue(SessionParameter.UserAgent) as String;
                    conn.UserAgent = userAgent == null ? "Apache Chemistry DotCMIS" : userAgent;

                    // timeouts
                    int connectTimeout = session.GetValue(SessionParameter.ConnectTimeout, -2);
                    if (connectTimeout >= -1)
                    {
                        conn.Timeout = connectTimeout;
                    }

                    int readTimeout = session.GetValue(SessionParameter.ReadTimeout, -2);
                    if (readTimeout >= -1)
{'fraction_non_alphanumeric': 0.07901008249312558, 'fraction_numerical': 0.0020164986251145736, 'mean_word_length': 1.9205032119914347, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5488176', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2479, 'n_words': 828}