<commit_before>/*
 * Copyright (c) 2011, <NAME>. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the WebSocket++ Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

#ifndef HTTP_PARSER_HPP
#define HTTP_PARSER_HPP

#include <iostream>
#include <map>
#include <string>
#include <sstream>

#include "constants.hpp"

namespace websocketpp {
namespace http {
namespace parser {

    namespace state {
        enum value {
            METHOD,
            RESOURCE,
            VERSION,
            HEADERS
        };
    }

typedef std::map<std::string,std::string> header_list;


class parser {
public:
    // consumes bytes from the stream and returns true if enough bytes have
    // been read
    bool consume (std::istream&) {
        throw "Not Implemented";
    }
    
    void set_version(const std::string& version) {
        // TODO: validation?
        m_version = version;
    }
    
    const std::string& version() const {
        return m_version;
    }
    
    std::string header(const std::string& key) const {
        header_list::const_iterator h = m_headers.find(key);
        
        if (h == m_headers.end()) {
            return "";
        } else {
            return h->second;
        }
    }
    
    // multiple calls to add header will result in values aggregating.
    // use replace_header if you do not want this behavior.
    void add_header(const std::string &key,const std::string &val) {
        // TODO: prevent use of reserved headers?
        if (this->header(key) == "") {
            m_headers[key] = val;
        } else {
            m_headers[key] += ", " + val;
        }
    }
    
    void replace_header(const std::string &key,const std::string &val) {
        m_headers[key] = val;
    }
    
    void remove_header(const std::string &key) {
        m_headers.erase(key);
    }
protected:
    bool parse_headers(std::istream& s) {
        std::string header;
        std::string::size_type end;
        
        // get headers
        while (std::getline(s, header) && header != "\r") {
            if (header[header.size()-1] != '\r') {
                continue; // ignore malformed header lines?
            } else {
                header.erase(header.end()-1);
            }
            
            end = header.find(": ",0);
            
            if (end != std::string::npos) {         
                add_header(header.substr(0,end),header.substr(end+2));
            }
        }
        
        return true;
    }
    
    std::string raw_headers() {
        std::stringstream raw;
        
        header_list::iterator it;
        for (it = m_headers.begin(); it != m_headers.end(); it++) {
            raw << it->first << ": " << it->second << "\r\n";
        }
        
        return raw.str();
    }
    
private:
    std::string m_version;
    header_list m_headers;
};

class request : public parser {
public: 
    // parse a complete header (ie \r\n\r\n MUST be in the input stream)<commit_msg>Handle case-insensitive HTTP header keys (obey RFC2616)<commit_after>/*
 * Copyright (c) 2011, <NAME>. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the WebSocket++ Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */

#ifndef HTTP_PARSER_HPP
#define HTTP_PARSER_HPP

#include <iostream>
#include <map>
#include <string>
#include <sstream>

#include "constants.hpp"

namespace websocketpp {
namespace http {
namespace parser {

    namespace state {
        enum value {
            METHOD,
            RESOURCE,
            VERSION,
            HEADERS
        };
    }

typedef std::map<std::string,std::string> header_list;


class parser {
public:
    // consumes bytes from the stream and returns true if enough bytes have
    // been read
    bool consume (std::istream&) {
        throw "Not Implemented";
    }
    
    void set_version(const std::string& version) {
        // TODO: validation?
        m_version = version;
    }
    
    const std::string& version() const {
        return m_version;
    }
    
    std::string header(const std::string& key) const {
        header_list::const_iterator h = m_headers.find(camel_case(key));
        
        if (h == m_headers.end()) {
            return "";
        } else {
            return h->second;
        }
    }
    
    // multiple calls to add header will result in values aggregating.
    // use replace_header if you do not want this behavior.
    void add_header(const std::string &key,const std::string &val) {
        // TODO: prevent use of reserved headers?
        const std::string camel_case_key = camel_case(key);
        if (this->header(camel_case_key) == "") {
            m_headers[camel_case_key] = val;
        } else {
            m_headers[camel_case_key] += ", " + val;
        }
    }
    
    void replace_header(const std::string &key,const std::string &val) {
        m_headers[camel_case(key)] = val;
    }
    
    void remove_header(const std::string &key) {
        m_headers.erase(camel_case(key));
    }
protected:
    bool parse_headers(std::istream& s) {
        std::string header;
        std::string::size_type end;
        
        // get headers
        while (std::getline(s, header) && header != "\r") {
            if (header[header.size()-1] != '\r') {
                continue; // ignore malformed header lines?
            } else {
                header.erase(header.end()-1);
            }
            
            end = header.find(": ",0);
            
            if (end != std::string::npos) {         
                add_header(header.substr(0,end),header.substr(end+2));
            }
        }
        
        return true;
    }
    
    std::string raw_headers() {
        std::stringstream raw;
        
        header_list::iterator it;
        for (it = m_headers.begin(); it != m_headers.end(); it++) {
            raw << it->first << ": " << it->second << "\r\n";
        }
        
        return raw.str();
    }
    
private:
    std::string m_version;
    header_list m_headers;

    static std::string camel_case(const std::string& str) {
      std::string camel;
      camel.reserve(str.size());
      bool upper = true;
      for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
        char c = *it;
        if (upper) {
          if (c >= 'a' && c <= 'z') {
            c = c + 'A' - 'a';
          }
          upper = false;
        } else if (c >= 'A' && c <= 'Z') {
          c = c + 'a' - 'A';
        }
        camel.push_back(c);
        if (c == '-') {
          upper = true;
        }
      }
      return camel;
    }
};

class request : public parser {
public: 
    // parse a complete header (ie \r\n\r\n MUST be in the input stream)
{'fraction_non_alphanumeric': 0.10289794204115918, 'fraction_numerical': 0.0023099538009239817, 'mean_word_length': 2.2124789207419897, 'pattern_counts': {'":': 4, '<': 33, '<?xml version=': 0, '>': 25, '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': '3667391', 'n_tokens_mistral': 2746, 'n_tokens_neox': 2600, 'n_words': 912}