<commit_before>import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*Created by aidan on 10/26/14.
 */
public class HeaderEditor {

    public static int parseLength(String header){
        int returnVar = 0;
        Pattern pattern = Pattern.compile("Content-Length:(.)*");
        Matcher matcher = pattern.matcher(header);
        if(!matcher.find()){
            //it was a GET, or non-payload HTTP packet
            return -1;
        }
        String length = matcher.group();
        length = length.replaceFirst("Content-Length: ","");
        length = length.trim();
        try {
            returnVar = Integer.parseInt(length);
        } catch(NumberFormatException e){
            System.err.println("Error while parsing '"+length+"' to an integer!");
        }
        return returnVar;
    }

    public static int parseLength(byte[] request){
        String strRequest = new String(request);
        return parseLength(strRequest);
    }

    public static int parseLength(String[] header){
        StringBuilder sb = new StringBuilder();
        for(String s: header)sb.append(s);
        return parseLength(sb.toString());
    }

    /**
     * Grabs the host from the HTTP request
     * @param request HTTP request as a string
     * @return the hostname as a string
     *
     * Complexity: 2
     */
    public static String parseHost(byte[] request){
        String httpHeader = new String(request);
        Pattern pattern = Pattern.compile("Host:(.)*");
        Matcher matcher = pattern.matcher(httpHeader);
        if(!matcher.find()){
            System.err.println("Malformed HTTP request! No Host specified!");
            return null;
        }
        String returnVar = matcher.group();
        returnVar = returnVar.trim();
        returnVar = returnVar.replaceFirst("Host: ","");
        returnVar = returnVar.replaceFirst(":(.)*", "");//get rid of the explicit port.
        returnVar = returnVar.trim();
        return returnVar;
    }

    public static byte[] convertConnection(byte[] request){
        String returnVar = new String(request);
        returnVar = returnVar.replaceFirst("Connection: keep-alive","Connection: close");
        return returnVar.getBytes();
    }
}<commit_msg>added method to find location of header's end.<commit_after>import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*Created by aidan on 10/26/14.
 */
public class HeaderEditor {

    private enum TERMINATION_STAGE {reset,r1,n1,r2}

    public static int parseLength(String header){
        if(header == null) return -1;
        int returnVar = 0;
        Pattern pattern = Pattern.compile("Content-Length:(.)*");
        Matcher matcher = pattern.matcher(header);
        if(!matcher.find()){
            //it was a GET, or non-payload HTTP packet
            return -1;
        }
        String length = matcher.group();
        length = length.replaceFirst("Content-Length: ","");
        length = length.trim();
        try {
            returnVar = Integer.parseInt(length);
        } catch(NumberFormatException e){
            System.err.println("Error while parsing '"+length+"' to an integer!");
        }
        return returnVar;
    }

    public static int parseLength(byte[] request){
        String strRequest = new String(request);
        return parseLength(strRequest);
    }

    public static int parseLength(String[] header){
        StringBuilder sb = new StringBuilder();
        for(String s: header)sb.append(s);
        return parseLength(sb.toString());
    }

    /**
     * Grabs the host from the HTTP request
     * @param request HTTP request as a string
     * @return the hostname as a string
     *
     * Complexity: 2
     */
    public static String parseHost(byte[] request){
        if(request == null) return null;
        String httpHeader = new String(request);
        Pattern pattern = Pattern.compile("Host:(.)*");
        Matcher matcher = pattern.matcher(httpHeader);
        if(!matcher.find()){
            System.err.println("Malformed HTTP request! No Host specified!");
            return null;
        }
        String returnVar = matcher.group();
        returnVar = returnVar.trim();
        returnVar = returnVar.replaceFirst("Host: ","");
        returnVar = returnVar.replaceFirst(":(.)*", "");//get rid of the explicit port.
        returnVar = returnVar.trim();
        return returnVar;
    }

    public static byte[] convertConnection(byte[] request){
        if(request == null) return null;
        String returnVar = new String(request);
        returnVar = returnVar.replaceFirst("Connection: keep-alive","Connection: close");
        return returnVar.getBytes();
    }

    public static int getHeaderEnd(byte[] header){//0x0D0A == \r\n
        int returnVar = -1;
        TERMINATION_STAGE termination_stage = TERMINATION_STAGE.reset;
        for(int i = 0; i< header.length; i++) {//0D0A
            byte b = header[i];
            switch (termination_stage){
                case reset:
                    if (b == 0x0D) termination_stage = TERMINATION_STAGE.r1;
                    break;
                case r1:
                    if(b == 0x0A) termination_stage = TERMINATION_STAGE.n1;
                    if(b == 0x0D) termination_stage = TERMINATION_STAGE.r1;
                    if(b != 0x0D && b != 0x0A) termination_stage = TERMINATION_STAGE.reset;
                    break;
                case n1:
                    if(b == 0x0D) termination_stage = TERMINATION_STAGE.r2;
                    if(b == 0x0A) termination_stage = TERMINATION_STAGE.r1;
                    if(b != 0x0D && b != 0x0A) termination_stage = TERMINATION_STAGE.reset;
                    break;
                case r2:
                    if(b == 0x0A) return i;
                    if(b == 0x0D) termination_stage = TERMINATION_STAGE.r1;
                    else termination_stage = TERMINATION_STAGE.reset;
                    break;
            }
        }
        return returnVar;
    }
}
{'fraction_non_alphanumeric': 0.1150324621275179, 'fraction_numerical': 0.00998834692858332, 'mean_word_length': 2.124284971398856, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 3, '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': '1273232', 'n_tokens_mistral': 1700, 'n_tokens_neox': 1616, 'n_words': 431}