Exemple #1
0
/*
 * Take a text string and try to extract a URL from it, and launch it.
 */
static int launch_url(char *s)
{
    int ret;
    if (is_url_scheme(s)) {
	/*
	 * Looks like it already has a URL scheme. Fine as it is.
	 */
	ret = launch_it(s);
    } else if (is_local_part(s)) {
	/*
	 * It's likely to be an email address, so we'll prefix
	 * `mailto:'.
	 */
	char *url = dupcat("mailto:", s, NULL);
	ret = launch_it(url);
	free(url);
    } else {
	/*
	 * No idea. We'll prefix `http://' on the
	 * assumption that it's a truncated URL of the form
	 * `www.thingy.com/wossname' or just `www.thingy.com'.
	 * 
	 * Exception: if the URL starts "//", we only prefix the
	 * `http:'.
	 */
	char *url;
	if (s[0] == '/' && s[1] == '/')
	    url = dupcat("http:", s, NULL);
	else
	    url = dupcat("http://", s, NULL);
	ret = launch_it(url);
	free(url);
    }
    return ret;
}
Exemple #2
0
//
// HTMLエスケープ
//
// include_url : URL中でもエスケープする( デフォルト = true )
//
const std::string MISC::html_escape( const std::string& str, const bool include_url )
{
    if( str.empty() ) return str;

    bool is_url = false;
    int scheme = SCHEME_NONE;
    std::string str_out;
    const size_t str_length = str.length();

    for( size_t pos = 0; pos < str_length; ++pos )
    {
        char tmpchar = str.c_str()[ pos ];

        // URL中はエスケープしない場合
        if( ! include_url )
        {
            // URLとして扱うかどうか
            // エスケープには影響がないので loose_url としておく
            if( scheme != SCHEME_NONE ) is_url = is_url_char( str.c_str() + pos, true );

            // URLスキームが含まれているか判別
            int len = 0;
            if( ! is_url ) scheme = is_url_scheme( str.c_str() + pos, &len );

            // URLスキームが含まれていた場合は文字数分進めてループに戻る
            if( len > 0 )
            {
                str_out += str.substr( pos, len );
                pos += len - 1; // あとで ++pos される分を引く
                continue;
            }
        }

        // include_url = false でURL中ならエスケープしない
        if( is_url ) str_out += tmpchar;
        else if( tmpchar == '&' )
        {
            const int bufsize = 64;
            char out_char[ bufsize ];
            int n_in, n_out;
            const int type = DBTREE::decode_char( str.c_str() + pos, n_in, out_char, n_out, false );
            if( type == DBTREE::NODE_NONE ) str_out += "&amp;";
            else str_out += tmpchar;
        }
        else if( tmpchar == '\"' ) str_out += "&quot;";
        else if( tmpchar == '<' ) str_out += "&lt;";
        else if( tmpchar == '>' ) str_out += "&gt;";
        else str_out += tmpchar;
    }

#ifdef _DEBUG
    if( str != str_out ){
        std::cout << "MISC::html_escape\nstr = " << str << std::endl
                  << "out = " << str_out << std::endl;
    }
#endif

    return str_out;
}
int process_arguments (int argc, char **argv) {
    int c;
    int option = 0;

    static struct option longopts[] = {
        MP_LONGOPTS_DEFAULT,
        MP_LONGOPTS_HOST,
        MP_LONGOPTS_PORT,
        {"url", required_argument, 0, 'u'},
        MP_LONGOPTS_WC,
        MP_LONGOPTS_END
    };

    if (argc < 2) {
        print_help();
        exit(STATE_OK);
    }

    /* Set default */

    while (1) {
        c = mp_getopt(&argc, &argv, MP_OPTSTR_DEFAULT"H:P:u:w:c:", longopts, &option);

        if (c == -1 || c == EOF)
            break;

        getopt_wc(c, optarg, &open_thresholds);

        switch (c) {
            case 'u':
                getopt_url(optarg, &url);
                break;
            /* Hostname opt */
            case 'H':
                getopt_host(optarg, &hostname);
                break;
            case 'P':
                getopt_port(optarg, &port);
                break;
        }
    }

    /* Check requirements */
    if (!is_url_scheme(url, "http") && !is_url_scheme(url, "https"))
        usage("Only http and https url allowed.");
    if (!url && !hostname)
        usage("Url or Hostname is mandatory.");
    if (url && hostname)
        usage("Only Url or Hostname allowed.");

    if (!url) {
        size_t len = strlen(hostname) + 40;
        char *u;
        u = mp_malloc(len);
        mp_snprintf(u, len , "http://%s:%d/server-status?auto", hostname, port);
        url = u;
    } else {
        char *u;
        u = (char *)(url + strlen(url) - 5);
        if (strcmp(u, "?auto") != 0) {
            u = mp_malloc(strlen(url) + 6);
            strcpy(u, url);
            strcat(u, "?auto");
            url = u;
        }
    }

    return(OK);
}