<commit_before>// Code based off of echo Server boost: 
// http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/
//     echo/blocking_tcp_echo_server.cpp

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>

#include "parser/ParserProcessor.h"

static const char *server_domain = "http://localhost";

using boost::asio::ip::tcp;

typedef boost::shared_ptr<tcp::socket> socket_ptr;

void session(socket_ptr sock)
{
    try
    {
        // Send Hello World message
        std::string hello_str("HTTP/1.0 200 OK\nContent-Type: text/html\n\n"
                                      "<html><body>Hello, world!</body></html>");
        boost::asio::write(*sock, boost::asio::buffer(hello_str.c_str(),
                                                      hello_str.size()));
        
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception in thread: " << e.what() << "\n";
    }
}

void server(boost::asio::io_service& io_service, unsigned short port)
{
    // Accept incoming connections
    tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
    while(true)
    {
        socket_ptr sock(new tcp::socket(io_service));
        a.accept(*sock);
        boost::thread t(boost::bind(session, sock));
    }
}

int main(int argc, char* argv[])
{
    try
    {
        if (argc != 2) {
            printf("Usage: ./webserver <config file>\n");
            return 1;
        }

        // Parse the given configuration file
        NginxConfigParser config_parser;
        NginxConfig config;
        config_parser.Parse(argv[1], &config);

        // Get the port from the parsed configuration file
        ParserProcessor parser_processor = ParserProcessor(config);
        unsigned short port = parser_processor.get_port();

        // Print server address
        fprintf(stderr, "Starting server at %s:%d\n",server_domain, port);

        // Launch echo server
        boost::asio::io_service io_service;
        server(io_service, port);
    }
    catch (std::exception& e)<commit_msg>Echo server attempt changed stuff in session and main<commit_after>// Code based off of echo Server boost: 
// http://www.boost.org/doc/libs/1_55_0/doc/html/boost_asio/example/cpp03/
//     echo/blocking_tcp_echo_server.cpp

#include <cstdlib>
#include <iostream>
#include <boost/bind.hpp>
#include <boost/smart_ptr.hpp>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>

#include "parser/ParserProcessor.h"

static const char *server_domain = "http://localhost";

using boost::asio::ip::tcp;

const int max_length = 1024;

typedef boost::shared_ptr<tcp::socket> socket_ptr;


void session(socket_ptr sock)
{
    try
    {
        for (;;)
        {
         char data[max_length];

         boost::system::error_code error;
         size_t length = sock->read_some(boost::asio::buffer(data), error);
         if (error == boost::asio::error::eof)
             break; // Connection closed cleanly by peer.
         else if (error)
             throw boost::system::system_error(error); // Some other error.
         std::string hello_str("HTTP/1.0 200 OK\nContent-Type: text/html\n\n"
                                    "<html><body>%s</body></html>",data);
         boost::asio::write(*sock, boost::asio::buffer(hello_str.c_str(), 
                                                        hello_str.size()));
        }
    }
    catch (std::exception& e)
    {
        std::cerr << "Exception in thread: " << e.what() << "\n";
    }
}

void server(boost::asio::io_service& io_service, unsigned short port)
{
    // Accept incoming connections
    tcp::acceptor a(io_service, tcp::endpoint(tcp::v4(), port));
    while(true)
    {
        socket_ptr sock(new tcp::socket(io_service));
        a.accept(*sock);
        boost::thread t(boost::bind(session, sock));
    }
}

int main(int argc, char* argv[])
{
    try
    {
        if (argc != 2) {
            printf("Usage: ./webserver <config file>\n");
            return 1;
        }

        // Parse the given configuration file
        NginxConfigParser config_parser;
        NginxConfig config;
        config_parser.Parse(argv[1], &config);

        // Get the port from the parsed configuration file
        ParserProcessor parser_processor = ParserProcessor(config);
        unsigned short port = parser_processor.get_port();

        // Print server address
        fprintf(stderr, "Starting server at %s:%d\n",server_domain, port);

        // Launch echo server
        boost::asio::io_service io_service;
        using namespace std;
        server(io_service, port);
    }
    catch (std::exception& e)
{'fraction_non_alphanumeric': 0.1375796178343949, 'fraction_numerical': 0.007218683651804671, 'mean_word_length': 2.471628592483419, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5786271', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1397, 'n_words': 319}