示例#1
0
文件: url.cpp 项目: bradclawsie/code
    // Construct a URL object from the string representation
    URL::URL(const std::string& url_str) { 
        if (url_str.empty()) throw std::invalid_argument("empty url string");

        const std::string sr = boost::trim_copy(url_str);

        try {
            token_rest scheme_rest = get_scheme(sr);
            scheme = scheme_rest.first;
            std::cout << scheme_rest.first << " : " << scheme_rest.second << std::endl;
            token_rest host_port_rest = get_host_port(scheme_rest.second);
            std::cout << host_port_rest.first << " : " << host_port_rest.second << std::endl;
            std::pair<std::string,uint16_t> host_port = split_host_port(host_port_rest.first);
            std::cout << host_port.first << " : " << host_port.second << std::endl;
            host = host_port.first;
            port = host_port.second;
            token_rest path_rest = get_path(host_port_rest.second);
            std::cout << path_rest.first << std::endl;
            path = path_rest.first;
            if (path_rest.second.empty()) return;
            token_rest query_rest = get_query(path_rest.second);
            std::cout << query_rest.first << " : " << query_rest.second << std::endl;
            if (!query_rest.first.empty()) {
                query_key_vals = make_query_map(query_rest.first);
            }
            std::string fragment = get_fragment(query_rest.second);
            std::cout << fragment << std::endl;
        } catch(const std::exception &e) {
            std::cerr << e.what() << std::endl;
            throw e;
        }

        // parsed successfully, we can copy over the original arg
        string_rep = url_str;
    }
示例#2
0
文件: main.c 项目: ejrh/snmp
static void initialise_config(Config *config)
{
    ConfigItem *item = config->item_list;
    
    while (item != NULL)
    {
        if (!split_host_port(item->host, DEFAULT_AGENT_PORT, &item->host_name, &item->port))
        {
            fprintf(stderr, "Warning: Agent host cannot be parsed: %s\n", item->host);
            item->port = 0;
            continue;
        }
        else if (item->port < 1 || item->port > 65535)
        {
            fprintf(stderr, "Agent port must be between 1 and 65535: %s\n", item->host);
            item->port = 0;
            continue;
        }
        
        item->next_time = 0;
        
        item = item->next;
    }
}
示例#3
0
文件: hub.c 项目: xiserge/Shakespeer
int hub_connect(const char *hubname, const char *nick, const char *email,
                const char *description, const char *speed, bool passive,
                const char *password, const char *encoding)
{
    return_val_if_fail(nick, -1);
    return_val_if_fail(hubname, -1);

    char *host = 0;
    int port = 0;
    if(split_host_port(hubname, &host, &port) != 0)
    {
        return -1;
    }
    else if(port < 0)
    {
        ui_send_status_message(NULL, hubname,
                               "Invalid port in hub address: %s", hubname);
        free(host);
        return -1;
    }
    else if(port == 0)
    {
        port = 411; /* default port */
    }

    ui_send_status_message(NULL, hubname, "Connecting to %s...", hubname);

    hub_connect_data_t *hcd = calloc(1, sizeof(hub_connect_data_t));
    hcd->nick = strdup(nick);
    hcd->email = xstrdup(email);
    hcd->description = xstrdup(description);
    hcd->speed = xstrdup(speed);
    hcd->address = strdup(hubname);
    hcd->passive = passive;
    hcd->password = xstrdup(password);
    hcd->encoding = xstrdup(encoding);
    hcd->port = port;

    struct in_addr xaddr;
    if(inet_aton(host, &xaddr))
    {
        /* host already given as an IP address */
        hub_connect_async(hcd, &xaddr);
        free(host);
    }
    else
    {
        int rc = evdns_resolve_ipv4(host, 0, hub_lookup_event, hcd);
        free(host);
        if(rc != DNS_ERR_NONE)
        {
            WARNING("Failed to lookup '%s': %s",
                    hubname, evdns_err_to_string(rc));
            ui_send_status_message(NULL, hubname, "Failed to lookup '%s': %s",
                                   hubname, evdns_err_to_string(rc));
            hcd_free(hcd);
            return -1;
        }
    }

    return 0;
}