<commit_before>package org.craft.atom.protocol.http.model;

import static org.craft.atom.protocol.http.model.HttpConstants.S_CR;
import static org.craft.atom.protocol.http.model.HttpConstants.S_LF;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * HTTP messages consist of requests from client to server and responses
 * from server to client.
 * <pre>
 *     HTTP-message   = Request | Response     ; HTTP/1.1 messages
 * </pre>
 * <p>
 * HTTP messages use the generic message format of RFC 822 for
 * transferring entities (the payload of the message). Both types
 * of message consist of a start-line, zero or more header fields
 * (also known as "headers"), an empty line (i.e., a line with nothing
 * preceding the CRLF) indicating the end of the header fields,
 * and possibly a message-body.
 * </p>
 * <pre>
 *      generic-message = start-line
 *                        *(message-header CRLF)
 *                        CRLF
 *                        [ message-body ]
 *      start-line      = Request-Line | Status-Line
 * </pre>
 *  
 * @author mindwind
 * @version 1.0, Feb 1, 2013
 */
public abstract class HttpMessage implements Serializable {

	private static final long serialVersionUID = -8373186983205172162L;
	
	protected List<HttpHeader> headers = new ArrayList<HttpHeader>();
	protected HttpEntity entity;
	
	// ~ ------------------------------------------------------------------------------------------------------------

	public HttpMessage() {
		super();
	}

	public HttpMessage(List<HttpHeader> headers) {
		this.headers = headers;
	}
	
	public HttpMessage(List<HttpHeader> headers, HttpEntity entity) {
		this.headers = headers;
		this.entity = entity;
	}
	
	// ~ ------------------------------------------------------------------------------------------------------------
	
	
	/**
	 * Add a new header to http message, if the header exists replace it.
	 * 
	 * @param header
	 */
	public void addHeader(HttpHeader header) {
		if (header == null || header.getName() == null) {
			throw new IllegalArgumentException("header or header name is null!");
		}
		
		headers.add(header);
	}
	
	/**
     * Gets the first header with the given name.
     *
     * <p>Header name comparison is case insensitive.
     *
     * @param name the name of the header to get
     * @return the first header or <code>null</code>
     */
	public HttpHeader getFirstHeader(String name) {<commit_msg>add - remove header method<commit_after>package org.craft.atom.protocol.http.model;

import static org.craft.atom.protocol.http.model.HttpConstants.S_CR;
import static org.craft.atom.protocol.http.model.HttpConstants.S_LF;

import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * HTTP messages consist of requests from client to server and responses
 * from server to client.
 * <pre>
 *     HTTP-message   = Request | Response     ; HTTP/1.1 messages
 * </pre>
 * <p>
 * HTTP messages use the generic message format of RFC 822 for
 * transferring entities (the payload of the message). Both types
 * of message consist of a start-line, zero or more header fields
 * (also known as "headers"), an empty line (i.e., a line with nothing
 * preceding the CRLF) indicating the end of the header fields,
 * and possibly a message-body.
 * </p>
 * <pre>
 *      generic-message = start-line
 *                        *(message-header CRLF)
 *                        CRLF
 *                        [ message-body ]
 *      start-line      = Request-Line | Status-Line
 * </pre>
 *  
 * @author mindwind
 * @version 1.0, Feb 1, 2013
 */
public abstract class HttpMessage implements Serializable {

	private static final long serialVersionUID = -8373186983205172162L;
	
	protected List<HttpHeader> headers = new ArrayList<HttpHeader>();
	protected HttpEntity entity;
	
	// ~ ------------------------------------------------------------------------------------------------------------

	public HttpMessage() {
		super();
	}

	public HttpMessage(List<HttpHeader> headers) {
		this.headers = headers;
	}
	
	public HttpMessage(List<HttpHeader> headers, HttpEntity entity) {
		this.headers = headers;
		this.entity = entity;
	}
	
	// ~ ------------------------------------------------------------------------------------------------------------
	
	
	/**
	 * Add a new header to http message, if the header exists replace it.
	 * 
	 * @param header
	 */
	public void addHeader(HttpHeader header) {
		if (header == null || header.getName() == null) {
			throw new IllegalArgumentException("header or header name is null!");
		}
		
		headers.add(header);
	}
	
	/**
	 * Remove the first header with given name.
	 * 
	 * @param name the name of the header
	 */
	public void removeFirstHeader(String name) {
		removeHeaders0(name, true);
	}
	
	/**
	 * Remove all the headers with the given name.
	 * 
	 * @param name the name of the header
	 */
	public void removeHeaders(String name) {
		removeHeaders0(name, false);
	}
	
	private void removeHeaders0(String name, boolean interrupt) {
		if (name == null) {
			throw new IllegalArgumentException("name is null!");
		}
		
		for (int i = 0; i < headers.size(); i++) {
            HttpHeader header = headers.get(i);
            if (header.getName().equalsIgnoreCase(name)) {
            	headers.remove(i);
            	if (interrupt) {
            		break;
            	}
            }
        }
	}
	
	/**
     * Get the first header with the given name.
     *
     * <p>Header name comparison is case insensitive.
     *
     * @param name the name of the header
     * @return the first header or <code>null</code>
     */
	public HttpHeader getFirstHeader(String name) {
{'fraction_non_alphanumeric': 0.17299068644360124, 'fraction_numerical': 0.011383235598482234, 'mean_word_length': 2.1261455525606467, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 29, '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': '2375475', 'n_tokens_mistral': 1675, 'n_tokens_neox': 1521, 'n_words': 512}