<commit_before>package utils;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

/**
 * Created by liverliu on 14/8/4.
 */
public class HttpUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class);
    private HttpUtil() {

    }

    private static PoolingHttpClientConnectionManager connectionManager;

    private static CloseableHttpClient getConnection() {
        if(connectionManager == null) {
            synchronized (HttpUtil.class) {
                if(connectionManager == null) {
                    connectionManager = new PoolingHttpClientConnectionManager();
                    connectionManager.setMaxTotal(50);
                    connectionManager.setDefaultMaxPerRoute(25);
                }
            }
        }
        return HttpClients.custom()
                .setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).
                        setConnectionManager(connectionManager).build();
    }

    public static String post(String url, Map<String, String> paramMap) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, paramMap, config);
    }

    public static String post(String url, String body) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, body, config);
    }
    
    public static String get(String url) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return get(url,config);
    }

    public static String post(String url, HttpServletRequest request) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, request, config);
    }

    public static String post(String url, HttpServletRequest request, String body) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, request, config, body);
    }

    public static String post(String url, HttpServletRequest request, RequestConfig config) {
        CloseableHttpClient httpClient = getConnection();
        StringBuilder sb = new StringBuilder();
        int retryCount = 0;
        while(retryCount < 3) {
            try {
                retryCount++;
                //url
                HttpPost post = new HttpPost(url+request.getServletPath());
                //header
                Enumeration<String> headers = request.getHeaderNames();
                while(headers.hasMoreElements()) {
                    String name = headers.nextElement();
                    if(name.toLowerCase().equals("content-length")) {
                        continue;
                    }
                    if(name.toLowerCase().equals("host")) {
                        post.addHeader(name, "music.163.com");
                        continue;
                    }
                    post.addHeader(name, request.getHeader(name));
                }
                //config
                post.setConfig(config);
                //body
                post.setEntity(new InputStreamEntity(request.getInputStream()));
                CloseableHttpResponse response = httpClient.execute(post);
                parseResult(sb, response);
                response.close();
                post.releaseConnection();
                LOGGER.debug(sb.toString());
                break;
            } catch (Exception ex) {
                LOGGER.error("Post Error!", ex);
            }
        }
        return sb.toString();
    }

    public static String post(String url, HttpServletRequest request, RequestConfig config, String body) {
        CloseableHttpClient httpClient = getConnection();
        StringBuilder sb = new StringBuilder();
        int retryCount = 0;
        while(retryCount < 3) {
            try {
                retryCount++;
                //url
                HttpPost post = new HttpPost(url+request.getServletPath());
                //header
                Enumeration<String> headers = request.getHeaderNames();
                while(headers.hasMoreElements()) {
                    String name = headers.nextElement();
                    if(name.toLowerCase().equals("content-length")) {
                        continue;
                    }
                    if(name.toLowerCase().equals("host")) {
                        post.addHeader(name, "music.163.com");
                        continue;
                    }
                    post.addHeader(name, request.getHeader(name));
                }
                //config
                post.setConfig(config);
                //body
                post.setEntity(new StringEntity(body));
                CloseableHttpResponse response = httpClient.execute(post);
                parseResult(sb, response);
                response.close();
                post.releaseConnection();
                LOGGER.debug(sb.toString());
                break;
            } catch (Exception ex) {
                LOGGER.error("Post Error!", ex);
            }
        }
        return sb.toString();
    }

    public static String post(String url, Map<String, String> paramMap, RequestConfig config){
        CloseableHttpClient httpClient = getConnection();
        StringBuilder sb = new StringBuilder();<commit_msg>Use "keep-alive" in post<commit_after>package utils;

import org.apache.http.HttpHeaders;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.cookie.ClientCookie;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

/**
 * Created by liverliu on 14/8/4.
 */
public class HttpUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpUtil.class);
    private HttpUtil() {

    }

    private static PoolingHttpClientConnectionManager connectionManager;

    private static CloseableHttpClient getConnection() {
        if(connectionManager == null) {
            synchronized (HttpUtil.class) {
                if(connectionManager == null) {
                    connectionManager = new PoolingHttpClientConnectionManager();
                    connectionManager.setMaxTotal(50);
                    connectionManager.setDefaultMaxPerRoute(25);
                }
            }
        }
        return HttpClients.custom()
                .setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).
                        setConnectionManager(connectionManager).build();
    }

    public static String post(String url, Map<String, String> paramMap) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, paramMap, config);
    }

    public static String post(String url, String body) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, body, config);
    }
    
    public static String get(String url) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return get(url,config);
    }

    public static String post(String url, HttpServletRequest request) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, request, config);
    }

    public static String post(String url, HttpServletRequest request, String body) {
        RequestConfig config = RequestConfig.custom()
                .setConnectionRequestTimeout(2000)
                .setConnectTimeout(3000)
                .setSocketTimeout(4000)
                .build();
        return post(url, request, config, body);
    }

    public static String post(String url, HttpServletRequest request, RequestConfig config) {
        CloseableHttpClient httpClient = getConnection();
        StringBuilder sb = new StringBuilder();
        int retryCount = 0;
        while(retryCount < 3) {
            try {
                retryCount++;
                //url
                HttpPost post = new HttpPost(url+request.getServletPath());
                //header
                addHeader(post, request);
                //config
                post.setConfig(config);
                //body
                post.setEntity(new InputStreamEntity(request.getInputStream()));
                CloseableHttpResponse response = httpClient.execute(post);
                parseResult(sb, response);
                response.close();
                post.releaseConnection();
                LOGGER.debug(sb.toString());
                break;
            } catch (Exception ex) {
                LOGGER.error("Post Error!", ex);
            }
        }
        return sb.toString();
    }

    public static String post(String url, HttpServletRequest request, RequestConfig config, String body) {
        CloseableHttpClient httpClient = getConnection();
        StringBuilder sb = new StringBuilder();
        int retryCount = 0;
        while(retryCount < 3) {
            try {
                retryCount++;
                //url
                HttpPost post = new HttpPost(url+request.getServletPath());
                //header
                addHeader(post, request);
                //config
                post.setConfig(config);
                //body
                post.setEntity(new StringEntity(body));
                CloseableHttpResponse response = httpClient.execute(post);
                parseResult(sb, response);
                response.close();
                post.releaseConnection();
                LOGGER.debug(sb.toString());
                break;
            } catch (Exception ex) {
                LOGGER.error("Post Error!", ex);
            }
        }
        return sb.toString();
    }

    private static void addHeader(HttpPost post, HttpServletRequest request) {
        Enumeration<String> headers = request.getHeaderNames();
        while(headers.hasMoreElements()) {
            String name = headers.nextElement();
            if(name.toLowerCase().equals("content-length")) {
                continue;
            }
            if(name.toLowerCase().equals("host")) {
                post.addHeader(name, "music.163.com");
                continue;
            }
            if(name.toLowerCase().equals("connection")) {
                post.addHeader(name, "keep-alive");
                continue;
            }
            post.addHeader(name, request.getHeader(name));
        }
    }

    public static String post(String url, Map<String, String> paramMap, RequestConfig config){
        CloseableHttpClient httpClient = getConnection();
        StringBuilder sb = new StringBuilder();
{'fraction_non_alphanumeric': 0.09589518433340302, 'fraction_numerical': 0.01094152902641299, 'mean_word_length': 2.0473561265661497, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 10, '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': '1765147', 'n_tokens_mistral': 3676, 'n_tokens_neox': 3435, 'n_words': 698}