<commit_before>/*
 * src/vala/dnsresolvd.vala
 * ============================================================================
 * DNS Resolver Daemon (dnsresolvd). Version 0.1
 * ============================================================================
 * A daemon that performs DNS lookups for the given hostname
 * passed in an HTTP request, with the focus on its implementation
 * using various programming languages. (libsoup-boosted impl.)
 * ============================================================================
 * Copyright (C) 2017-2018 <NAME>) Golubtsov
 *
 * (See the LICENSE file at the top of the source tree.)
 */

/** The main class of the daemon. */
class DnsResolvd : Soup.Server {
    /** Default constructor. */
    public DnsResolvd() {}
}

// The daemon entry point.
public static int main(string[] args) {
    int ret = Posix.EXIT_SUCCESS;

    int argc = args.length - 1;

    // Instantiating the daemon helper class.
    var aux = new AUX();

    var  daemon_name = args[0];
    uint port_number;

    string print_banner_opt = AUX.EMPTY_STRING;

    if (argc > 0) {
        port_number = int.parse(args[1]);

        if (argc > 1) {
            print_banner_opt = args[2];
        }
    } else {
        port_number = 0;
    }

//  stdout.printf(AUX.S_FMT,  AUX.V_BAR + AUX.SPACE + argc.to_string()
//              + AUX.SPACE + AUX.V_BAR + AUX.SPACE + daemon_name
//              + AUX.SPACE + AUX.V_BAR + AUX.SPACE + port_number.to_string()
//              + AUX.SPACE + AUX.V_BAR + AUX.SPACE + print_banner_opt
//              + AUX.SPACE + AUX.V_BAR + AUX.NEW_LINE);

    if (print_banner_opt == AUX.PRINT_BANNER_OPT) {
        aux.separator_draw(AUX.DMN_DESCRIPTION);

        stdout.puts(AUX.DMN_NAME + AUX.COMMA_SPACE_SEP
           + AUX.DMN_VERSION_S__ + AUX.SPACE + AUX.DMN_VERSION + AUX.NEW_LINE
           + AUX.DMN_DESCRIPTION                               + AUX.NEW_LINE
           + AUX.DMN_COPYRIGHT__ + AUX.SPACE + AUX.DMN_AUTHOR  + AUX.NEW_LINE);

        aux.separator_draw(AUX.DMN_DESCRIPTION);
    }

    // Opening the system logger.
    Posix.openlog((string) null,
                  (Posix.LOG_CONS | Posix.LOG_PID), Posix.LOG_DAEMON);

    // Checking for args presence.
    if (argc == 0) {
        ret = Posix.EXIT_FAILURE;

        stderr.printf(AUX.ERR_MUST_BE_ONE_TWO_ARGS
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name, argc);

        Posix.syslog(Posix.LOG_ERR,
                      AUX.ERR_MUST_BE_ONE_TWO_ARGS
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name, argc);

        stderr.printf(AUX.MSG_USAGE_TEMPLATE
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        aux.cleanups_fixate();

        return ret;
    }

    // Checking for port correctness.
    if ((port_number < AUX.MIN_PORT) || (port_number > AUX.MAX_PORT)) {
        ret = Posix.EXIT_FAILURE;

        stderr.printf(AUX.ERR_PORT_MUST_BE_POSITIVE_INT
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        Posix.syslog(Posix.LOG_ERR,
                      AUX.ERR_PORT_MUST_BE_POSITIVE_INT
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        stderr.printf(AUX.MSG_USAGE_TEMPLATE
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        aux.cleanups_fixate();

        return ret;
    }

    // Instantiating the main daemon class.
    var dmn = new DnsResolvd();

    // Creating the main loop instance.
    var loop = new MainLoop();

    if ((dmn == null) || (loop == null)) {
        ret = Posix.EXIT_FAILURE;

        stderr.printf(AUX.ERR_CANNOT_START_SERVER
                    + AUX.ERR_SRV_UNKNOWN_REASON
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        Posix.syslog(Posix.LOG_ERR,
                      AUX.ERR_CANNOT_START_SERVER
                    + AUX.ERR_SRV_UNKNOWN_REASON
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        aux.cleanups_fixate();

        return ret;
    }

    // Attaching Unix signal handlers to ensure daemon clean shutdown.
    Unix.signal_add(Posix.SIGINT,  () => { // <== SIGINT  handler (callback).
        aux.cleanups_fixate(loop);

        return new Unix.SignalSource(Posix.SIGINT ).REMOVE;
    });

    Unix.signal_add(Posix.SIGTERM, () => { // <== SIGTERM handler (callback).
        aux.cleanups_fixate(loop);

        return new Unix.SignalSource(Posix.SIGTERM).REMOVE;
    });

    /*
     * Attaching HTTP request handlers to process incoming requests
     * and producing the response.
     */
    dmn.add_handler(null, (dmn, msg, pth, qry) => { // <== Def. req. handler.
        var mtd      = msg.method;
        var req_body = msg.request_body;

        var hostname = (string) null; // The effective hostname to look up for.<commit_msg>libsoup/vala: Enrich doc-head commentary for callbacks.<commit_after>/*
 * src/vala/dnsresolvd.vala
 * ============================================================================
 * DNS Resolver Daemon (dnsresolvd). Version 0.1
 * ============================================================================
 * A daemon that performs DNS lookups for the given hostname
 * passed in an HTTP request, with the focus on its implementation
 * using various programming languages. (libsoup-boosted impl.)
 * ============================================================================
 * Copyright (C) 2017-2018 <NAME>
 *
 * (See the LICENSE file at the top of the source tree.)
 */

/** The main class of the daemon. */
class DnsResolvd : Soup.Server {
    /** Default constructor. */
    public DnsResolvd() {}
}

// The daemon entry point.
public static int main(string[] args) {
    int ret = Posix.EXIT_SUCCESS;

    int argc = args.length - 1;

    // Instantiating the daemon helper class.
    var aux = new AUX();

    var  daemon_name = args[0];
    uint port_number;

    string print_banner_opt = AUX.EMPTY_STRING;

    if (argc > 0) {
        port_number = int.parse(args[1]);

        if (argc > 1) {
            print_banner_opt = args[2];
        }
    } else {
        port_number = 0;
    }

//  stdout.printf(AUX.S_FMT,  AUX.V_BAR + AUX.SPACE + argc.to_string()
//              + AUX.SPACE + AUX.V_BAR + AUX.SPACE + daemon_name
//              + AUX.SPACE + AUX.V_BAR + AUX.SPACE + port_number.to_string()
//              + AUX.SPACE + AUX.V_BAR + AUX.SPACE + print_banner_opt
//              + AUX.SPACE + AUX.V_BAR + AUX.NEW_LINE);

    if (print_banner_opt == AUX.PRINT_BANNER_OPT) {
        aux.separator_draw(AUX.DMN_DESCRIPTION);

        stdout.puts(AUX.DMN_NAME + AUX.COMMA_SPACE_SEP
           + AUX.DMN_VERSION_S__ + AUX.SPACE + AUX.DMN_VERSION + AUX.NEW_LINE
           + AUX.DMN_DESCRIPTION                               + AUX.NEW_LINE
           + AUX.DMN_COPYRIGHT__ + AUX.SPACE + AUX.DMN_AUTHOR  + AUX.NEW_LINE);

        aux.separator_draw(AUX.DMN_DESCRIPTION);
    }

    // Opening the system logger.
    Posix.openlog((string) null,
                  (Posix.LOG_CONS | Posix.LOG_PID), Posix.LOG_DAEMON);

    // Checking for args presence.
    if (argc == 0) {
        ret = Posix.EXIT_FAILURE;

        stderr.printf(AUX.ERR_MUST_BE_ONE_TWO_ARGS
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name, argc);

        Posix.syslog(Posix.LOG_ERR,
                      AUX.ERR_MUST_BE_ONE_TWO_ARGS
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name, argc);

        stderr.printf(AUX.MSG_USAGE_TEMPLATE
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        aux.cleanups_fixate();

        return ret;
    }

    // Checking for port correctness.
    if ((port_number < AUX.MIN_PORT) || (port_number > AUX.MAX_PORT)) {
        ret = Posix.EXIT_FAILURE;

        stderr.printf(AUX.ERR_PORT_MUST_BE_POSITIVE_INT
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        Posix.syslog(Posix.LOG_ERR,
                      AUX.ERR_PORT_MUST_BE_POSITIVE_INT
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        stderr.printf(AUX.MSG_USAGE_TEMPLATE
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        aux.cleanups_fixate();

        return ret;
    }

    // Instantiating the main daemon class.
    var dmn = new DnsResolvd();

    // Creating the main loop instance.
    var loop = new MainLoop();

    if ((dmn == null) || (loop == null)) {
        ret = Posix.EXIT_FAILURE;

        stderr.printf(AUX.ERR_CANNOT_START_SERVER
                    + AUX.ERR_SRV_UNKNOWN_REASON
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        Posix.syslog(Posix.LOG_ERR,
                      AUX.ERR_CANNOT_START_SERVER
                    + AUX.ERR_SRV_UNKNOWN_REASON
                    + AUX.NEW_LINE + AUX.NEW_LINE, daemon_name);

        aux.cleanups_fixate();

        return ret;
    }

    // Attaching Unix signal handlers to ensure daemon clean shutdown.
    Unix.signal_add(Posix.SIGINT,  () => { // <== SIGINT  handler (callback).
        aux.cleanups_fixate(loop);

        return new Unix.SignalSource(Posix.SIGINT ).REMOVE;
    });

    Unix.signal_add(Posix.SIGTERM, () => { // <== SIGTERM handler (callback).
        aux.cleanups_fixate(loop);

        return new Unix.SignalSource(Posix.SIGTERM).REMOVE;
    });

    /*
     * Attaching HTTP request handlers to process incoming requests
     * and producing the response. ---+------+------+------+------+
     *                                |      |      |      |      |
     *                                v      v      v      v      v
     *
     * Default request handler.
     *
     * @param _pth The URI path for serving requests. Values <code>null</code>
     *             or &quot;/&quot; mean that this will be the default handler
     *             for all requests.
     * @param  dmn The daemon instance running.
     * @param  msg The HTTP message being processed.
     * @param  pth The path  component of the <code>msg</code> request URI.
     * @param  qry The query component of the <code>msg</code> request URI.
     */
    dmn.add_handler(null, (dmn, msg, pth, qry) => {
        var mtd      = msg.method;
        var req_body = msg.request_body;

        var hostname = (string) null; // The effective hostname to look up for.
{'fraction_non_alphanumeric': 0.1498046875, 'fraction_numerical': 0.003515625, 'mean_word_length': 2.083709725986149, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4705961', 'n_tokens_mistral': 3491, 'n_tokens_neox': 3293, 'n_words': 686}