<commit_before>#include "httpserver.hpp"

//Added for the json-example:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using namespace std;
//Added for the json-example:
using namespace boost::property_tree;

int main() {
    //HTTP-server at port 8080 using 4 threads
    HTTPServer httpserver(8080, 4);
    
    //Add resources using regular expressions for path and method
    //POST-example for the path /string, responds the posted string
    httpserver.resources["^/string/?$"]["^POST$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        //Retrieve string from istream (*request.content)
        stringstream ss;
        *request.content >> ss.rdbuf();
        string content=move(ss.str());
        
        response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
    };
    
    //POST-example for the path /json, responds firstName+" "+lastName from the posted json
    //Responds with an appropriate error message if the posted json is not correct, or if firstName or lastName is missing
    //Example posted json:
    //{
    //  "firstName": "John",
    //  "lastName": "Smith",
    //  "age": 25
    //}
    httpserver.resources["^/json/?$"]["^POST$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        ptree pt;
        try {
            read_json(*request.content, pt);

            string name=pt.get<string>("firstName")+" "+pt.get<string>("lastName");

            response << "HTTP/1.1 200 OK\r\nContent-Length: " << name.length() << "\r\n\r\n" << name;
        }
        catch(exception& e) {
            response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
        }
    };
    
    //GET-example for the path /
    httpserver.resources["^/$"]["^GET$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        response <<  "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nRoot resource";
    };
    
    //GET-example for the path /test, responds with the matched string in path (test) 
    httpserver.resources["^/(test)/?$"]["^GET$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        response << "HTTP/1.1 200 OK\r\nContent-Length: 4\r\n\r\n" << path_match[1];<commit_msg>updated comments<commit_after>#include "httpserver.hpp"

//Added for the json-example:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using namespace std;
//Added for the json-example:
using namespace boost::property_tree;

int main() {
    //HTTP-server at port 8080 using 4 threads
    HTTPServer httpserver(8080, 1);
    
    //Add resources using regular expressions for path and method
    //POST-example for the path /string, responds the posted string
    httpserver.resources["^/string/?$"]["^POST$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        //Retrieve string from istream (*request.content)
        stringstream ss;
        *request.content >> ss.rdbuf();
        string content=move(ss.str());
        
        response << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
    };
    
    //POST-example for the path /json, responds firstName+" "+lastName from the posted json
    //Responds with an appropriate error message if the posted json is not correct, or if firstName or lastName is missing
    //Example posted json:
    //{
    //  "firstName": "John",
    //  "lastName": "Smith",
    //  "age": 25
    //}
    httpserver.resources["^/json/?$"]["^POST$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        ptree pt;
        try {
            read_json(*request.content, pt);

            string name=pt.get<string>("firstName")+" "+pt.get<string>("lastName");

            response << "HTTP/1.1 200 OK\r\nContent-Length: " << name.length() << "\r\n\r\n" << name;
        }
        catch(exception& e) {
            response << "HTTP/1.1 400 Bad Request\r\nContent-Length: " << strlen(e.what()) << "\r\n\r\n" << e.what();
        }
    };
    
    //GET-example for the path /
    httpserver.resources["^/$"]["^GET$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        response <<  "HTTP/1.1 200 OK\r\nContent-Length: 13\r\n\r\nRoot resource";
    };
    
    //GET-example for the path /match/[number], responds with the matched string in path (number) 
    httpserver.resources["^/match/([0-9]*)/?$"]["^GET$"]=[](ostream& response, const Request& request, const smatch& path_match) {
        string number=path_match[1];
        response << "HTTP/1.1 200 OK\r\nContent-Length: " << number.length() << "\r\n\r\n" << number;
{'fraction_non_alphanumeric': 0.16663173338922657, 'fraction_numerical': 0.017396772165164536, 'mean_word_length': 2.8176, 'pattern_counts': {'":': 6, '<': 75, '<?xml version=': 0, '>': 15, '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': '2379829', 'n_tokens_mistral': 1564, 'n_tokens_neox': 1497, 'n_words': 393}