<commit_before>/*
 * Copyright (c) 2005-2006 Digital Bazaar, Inc.  All rights reserved.
 */
package com.db.net.http;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TimeZone;

import com.db.logging.Logger;
import com.db.logging.LoggerManager;

/**
 * An http (HyperText Transfer Protocol) header. An instance
 * of this class holds a http request or http response line along
 * with a collection of custom headers in key-value pair format.
 * 
 * @author <NAME>
 */
public abstract class HttpHeader
{
   /**
    * The version for this http header.
    */
   protected String mVersion;   

   /**
    * A map of the custom headers to their values.
    */
   protected HashMap mHeaders;
   
   /**
    * Stores the boundary for multipart http messages.
    */
   protected String mBoundary;
   
   /**
    * Standard line break, carriage return + line feed.
    */
   public static final String CRLF = "\r\n";   
   
   /**
    * Creates a new http header object.
    */
   public HttpHeader()
   {
      // set defaults
      setVersion("HTTP/1.1");
      
      // create headers map
      mHeaders = new HashMap();
      
      // no multipart boundary by default
      setBoundary(null);
   }
   
   /**
    * BiCapitalizes a header. 
    * 
    * @param header the header to biCapitalize.
    * 
    * @return the biCapitalized header.
    */
   protected String biCapitalizeHeader(String header)
   {
      StringBuffer sb = new StringBuffer(header.length());
      
      // make the header lowercase
      header = header.toLowerCase();
      
      // capitalize the first letter
      if(header.length() > 0)
      {
         char c = header.charAt(0);
         header = Character.toUpperCase(c) + header.substring(1);
      }
      
      // bicapitalize the rest of the header
      StringTokenizer st = new StringTokenizer(header, "-", true);
      while(st.hasMoreTokens())
      {
         String token = st.nextToken();
         if(token.length() > 1)
         {
            char c = token.charAt(0);
            c = Character.toUpperCase(c);
            sb.append(c);
            sb.append(token.substring(1, token.length()));
         }
         else
         {
            token.toUpperCase();
            sb.append(token);
         }
      }
      
      return sb.toString();
   }
   
   /**
    * Sets the boundary for multipart http messages.
    * 
    * @param boundary the boundary for multipart http messages.
    */
   protected void setBoundary(String boundary)
   {
      mBoundary = boundary;
   }
   
   /**
    * Joins an array of strings together using the passed delimiter starting
    * at the given begin index and stopping at the given end index.
    * 
    * @param array the string array to join.
    * @param beginIndex the index to start joining at.
    * @param endIndex the index to stop joining at (the value at this
    *                 index is not included). 
    * @param delimiter the delimiter to join the array with.
    * 
    * @return the joined string.
    */
   protected String joinArray(String[] array, int beginIndex, int endIndex,
                              String delimiter)
   {
      StringBuilder sb = new StringBuilder();
      
      for(int i = beginIndex; i < endIndex; i++)
      {
         sb.append(array[i]);
         
         if(i < (endIndex - 1))
         {
            sb.append(delimiter);
         }
      }
      
      return sb.toString();
   }
   
   /**
    * Parses the headers out of the passed string and stores them in
    * this object.
    * 
    * @param headers the string of headers to parse.
    * @param start the index to start parsing at.
    * @param stop the index to stop parsing at.
    */
   public void parseHeaders(String headers, int start, int stop)
   {
      if(start == 0 && headers.length() == stop)
      {
         parseHeaders(headers);
      }
      else
      {
         parseHeaders(headers.substring(start, stop));
      }
   }

   /**
    * Parses the headers out of the passed string and stores them in
    * this object.
    * 
    * @param headers the string of headers to parse.
    */
   public void parseHeaders(String headers)
   {
      try
      {
         StringTokenizer st = new StringTokenizer(headers, CRLF);
         while(st.hasMoreTokens())
         {
            String token = st.nextToken();
            
            int index = token.indexOf(":");
            if(index != -1)
            {
               // trim the white space around the token
               token = token.trim();
               
               // get header and value
               String header = token.substring(0, index);
               String value = "";
               if(token.length() > index + 2)
               {
                  value = token.substring(index + 2, token.length());
               }
               
               addHeader(header, value);
               getLogger().debug(getClass(),
                  "http header='" + header + "',value='" + value + "'");
               
               // check for boundary
               if(header.equalsIgnoreCase("Content-Type"))
               {
                  index = value.toLowerCase().indexOf("boundary=");
                  if(index != -1)
                  {
                     String boundary = "";
                     
                     // boundary= is 9 characters 
                     if(value.length() > index + 9)
                     {
                        boundary = value.substring(index + 9, value.length());
                     }
                     
                     setBoundary("--" + boundary);
                     getLogger().debug(getClass(),
                        "http boundary='" + boundary + "'");
                  }
               }
            }
         }
      }
      catch(Throwable t)<commit_msg>Fixed bug where header values were the first letter was chopped off.<commit_after>/*
 * Copyright (c) 2005-2006 Digital Bazaar, Inc.  All rights reserved.
 */
package com.db.net.http;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TimeZone;

import com.db.logging.Logger;
import com.db.logging.LoggerManager;

/**
 * An http (HyperText Transfer Protocol) header. An instance
 * of this class holds a http request or http response line along
 * with a collection of custom headers in key-value pair format.
 * 
 * @author <NAME>
 */
public abstract class HttpHeader
{
   /**
    * The version for this http header.
    */
   protected String mVersion;   

   /**
    * A map of the custom headers to their values.
    */
   protected HashMap mHeaders;
   
   /**
    * Stores the boundary for multipart http messages.
    */
   protected String mBoundary;
   
   /**
    * Standard line break, carriage return + line feed.
    */
   public static final String CRLF = "\r\n";   
   
   /**
    * Creates a new http header object.
    */
   public HttpHeader()
   {
      // set defaults
      setVersion("HTTP/1.1");
      
      // create headers map
      mHeaders = new HashMap();
      
      // no multipart boundary by default
      setBoundary(null);
   }
   
   /**
    * BiCapitalizes a header. 
    * 
    * @param header the header to biCapitalize.
    * 
    * @return the biCapitalized header.
    */
   protected String biCapitalizeHeader(String header)
   {
      StringBuffer sb = new StringBuffer(header.length());
      
      // make the header lowercase
      header = header.toLowerCase();
      
      // capitalize the first letter
      if(header.length() > 0)
      {
         char c = header.charAt(0);
         header = Character.toUpperCase(c) + header.substring(1);
      }
      
      // bicapitalize the rest of the header
      StringTokenizer st = new StringTokenizer(header, "-", true);
      while(st.hasMoreTokens())
      {
         String token = st.nextToken();
         if(token.length() > 1)
         {
            char c = token.charAt(0);
            c = Character.toUpperCase(c);
            sb.append(c);
            sb.append(token.substring(1, token.length()));
         }
         else
         {
            token.toUpperCase();
            sb.append(token);
         }
      }
      
      return sb.toString();
   }
   
   /**
    * Sets the boundary for multipart http messages.
    * 
    * @param boundary the boundary for multipart http messages.
    */
   protected void setBoundary(String boundary)
   {
      mBoundary = boundary;
   }
   
   /**
    * Joins an array of strings together using the passed delimiter starting
    * at the given begin index and stopping at the given end index.
    * 
    * @param array the string array to join.
    * @param beginIndex the index to start joining at.
    * @param endIndex the index to stop joining at (the value at this
    *                 index is not included). 
    * @param delimiter the delimiter to join the array with.
    * 
    * @return the joined string.
    */
   protected String joinArray(String[] array, int beginIndex, int endIndex,
                              String delimiter)
   {
      StringBuilder sb = new StringBuilder();
      
      for(int i = beginIndex; i < endIndex; i++)
      {
         sb.append(array[i]);
         
         if(i < (endIndex - 1))
         {
            sb.append(delimiter);
         }
      }
      
      return sb.toString();
   }
   
   /**
    * Parses the headers out of the passed string and stores them in
    * this object.
    * 
    * @param headers the string of headers to parse.
    * @param start the index to start parsing at.
    * @param stop the index to stop parsing at.
    */
   public void parseHeaders(String headers, int start, int stop)
   {
      if(start == 0 && headers.length() == stop)
      {
         parseHeaders(headers);
      }
      else
      {
         parseHeaders(headers.substring(start, stop));
      }
   }

   /**
    * Parses the headers out of the passed string and stores them in
    * this object.
    * 
    * @param headers the string of headers to parse.
    */
   public void parseHeaders(String headers)
   {
      try
      {
         StringTokenizer st = new StringTokenizer(headers, CRLF);
         while(st.hasMoreTokens())
         {
            String token = st.nextToken();
            
            int index = token.indexOf(":");
            if(index != -1)
            {
               // trim the white space around the token
               token = token.trim();
               
               // get header and value
               String header = token.substring(0, index);
               String value = "";
               if(token.length() > index + 1)
               {
                  value = token.substring(index + 1, token.length());
               }
               
               addHeader(header, value);
               getLogger().debug(getClass(),
                  "http header='" + header + "',value='" + value + "'");
               
               // check for boundary
               if(header.equalsIgnoreCase("Content-Type"))
               {
                  index = value.toLowerCase().indexOf("boundary=");
                  if(index != -1)
                  {
                     String boundary = "";
                     
                     // boundary= is 9 characters 
                     if(value.length() > index + 9)
                     {
                        boundary = value.substring(index + 9, value.length());
                     }
                     
                     setBoundary("--" + boundary);
                     getLogger().debug(getClass(),
                        "http boundary='" + boundary + "'");
                  }
               }
            }
         }
      }
      catch(Throwable t)
{'fraction_non_alphanumeric': 0.10064204119069457, 'fraction_numerical': 0.004335862586508797, 'mean_word_length': 1.5287792536369387, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 13, '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': '4545237', 'n_tokens_mistral': 3230, 'n_tokens_neox': 3132, 'n_words': 960}