<commit_before>/*
 * Copyright (c) 2007 Digital Bazaar, Inc.  All rights reserved.
 */
#include "HttpHeader.h"
#include "StringTools.h"
#include "Convert.h"

using namespace std;
using namespace db::net::http;
using namespace db::util;

// define CRLF
const char HttpHeader::CRLF[] = "\r\n";

HttpHeader::HttpHeader()
{
}

HttpHeader::~HttpHeader()
{
}

void HttpHeader::setHeader(const string& header, long long value)
{
   setHeader(header, Convert::integerToString(value));
}

void HttpHeader::setHeader(const string& header, const string& value)
{
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // trim and set value
   string v = value;
   mHeaders[bic] = StringTools::trim(v);
}

void HttpHeader::addHeader(const string& header, const string& value)
{
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // get existing value
   string existing = "";
   getHeader(bic, existing);
   
   // trim and append new value
   string v = value;
   setHeader(bic, existing + ", " + StringTools::trim(v));
}

void HttpHeader::removeHeader(const string& header)
{
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // erase it
   mHeaders.erase(bic);
}

void HttpHeader::clearHeaders()
{
   mHeaders.clear();
}

bool HttpHeader::getHeader(const string& header, long long& value)
{
   bool rval = false;
   
   string str;
   if(getHeader(header, str))
   {
      rval = Convert::stringToInteger(str, value);
   }
   
   return rval;
}

bool HttpHeader::getHeader(const string& header, string& value)
{
   bool rval = false;
   
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // find header entry
   map<string, string>::iterator i = mHeaders.find(bic);
   if(i != mHeaders.end())
   {
      // get value
      value = i->second;
      rval = true;
   }
   
   return rval;
}

bool HttpHeader::parse(const string& str)
{
   // parse start line
   bool rval = parseStartLine(str);
   
   // clear headers
   clearHeaders();
   
   // parse lines according to CRLF (and skip start-line)
   string line;
   string::size_type lineStart = str.find(CRLF);<commit_msg>Added note for fixing slow string parsing.<commit_after>/*
 * Copyright (c) 2007 Digital Bazaar, Inc.  All rights reserved.
 */
#include "HttpHeader.h"
#include "StringTools.h"
#include "Convert.h"

using namespace std;
using namespace db::net::http;
using namespace db::util;

// define CRLF
const char HttpHeader::CRLF[] = "\r\n";

HttpHeader::HttpHeader()
{
}

HttpHeader::~HttpHeader()
{
}

void HttpHeader::setHeader(const string& header, long long value)
{
   setHeader(header, Convert::integerToString(value));
}

void HttpHeader::setHeader(const string& header, const string& value)
{
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // trim and set value
   string v = value;
   mHeaders[bic] = StringTools::trim(v);
}

void HttpHeader::addHeader(const string& header, const string& value)
{
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // get existing value
   string existing = "";
   getHeader(bic, existing);
   
   // trim and append new value
   string v = value;
   setHeader(bic, existing + ", " + StringTools::trim(v));
}

void HttpHeader::removeHeader(const string& header)
{
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // erase it
   mHeaders.erase(bic);
}

void HttpHeader::clearHeaders()
{
   mHeaders.clear();
}

bool HttpHeader::getHeader(const string& header, long long& value)
{
   bool rval = false;
   
   string str;
   if(getHeader(header, str))
   {
      rval = Convert::stringToInteger(str, value);
   }
   
   return rval;
}

bool HttpHeader::getHeader(const string& header, string& value)
{
   bool rval = false;
   
   // bicapitalize and trim header
   string bic = header;
   biCapitalize(StringTools::trim(bic));
   
   // find header entry
   map<string, string>::iterator i = mHeaders.find(bic);
   if(i != mHeaders.end())
   {
      // get value
      value = i->second;
      rval = true;
   }
   
   return rval;
}

bool HttpHeader::parse(const string& str)
{
   // FIXME: super slow parsing needs to be fixed
   // FIXME: parseStartLine = 500 conn/sec fewer
   // FIXME: remaining parse code = 500 conn/sec fewer
   
   // parse start line
   bool rval = parseStartLine(str);
   
   // clear headers
   clearHeaders();
   
   // parse lines according to CRLF (and skip start-line)
   string line;
   string::size_type lineStart = str.find(CRLF);
{'fraction_non_alphanumeric': 0.12775793233872662, 'fraction_numerical': 0.0029417944946417314, 'mean_word_length': 3.358974358974359, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '4122365', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1533, 'n_words': 417}