Пример #1
0
static Cfg *init_bearerbox(Cfg *cfg)
{
    CfgGroup *grp;
    Octstr *log, *val;
    long loglevel, store_dump_freq, value;
    int lf, m;
#ifdef HAVE_LIBSSL
    Octstr *ssl_server_cert_file;
    Octstr *ssl_server_key_file;
    int ssl_enabled = 0;
#endif /* HAVE_LIBSSL */
    Octstr *http_proxy_host = NULL;
    long http_proxy_port = -1;
    int http_proxy_ssl = 0;
    List *http_proxy_exceptions = NULL;
    Octstr *http_proxy_username = NULL;
    Octstr *http_proxy_password = NULL;
    Octstr *http_proxy_exceptions_regex = NULL;

    /* defaults: use localtime and markers for access-log */
    lf = m = 1;
	
    grp = cfg_get_single_group(cfg, octstr_imm("core"));

    log = cfg_get(grp, octstr_imm("log-file"));
    if (log != NULL) {
        if (cfg_get_integer(&loglevel, grp, octstr_imm("log-level")) == -1)
            loglevel = 0;
        log_open(octstr_get_cstr(log), loglevel, GW_NON_EXCL);
        octstr_destroy(log);
    }
    if ((val = cfg_get(grp, octstr_imm("syslog-level"))) != NULL) {
        long level;
        Octstr *facility;
        if ((facility = cfg_get(grp, octstr_imm("syslog-facility"))) != NULL) {
            log_set_syslog_facility(octstr_get_cstr(facility));
            octstr_destroy(facility);
        }
        if (octstr_compare(val, octstr_imm("none")) == 0) {
            log_set_syslog(NULL, 0);
        } else if (octstr_parse_long(&level, val, 0, 10) > 0) {
            log_set_syslog("bearerbox", level);
        }
        octstr_destroy(val);
    } else {
        log_set_syslog(NULL, 0);
    }

    if (check_config(cfg) == -1)
        panic(0, "Cannot start with corrupted configuration");

    /* determine which timezone we use for access logging */
    if ((log = cfg_get(grp, octstr_imm("access-log-time"))) != NULL) {
        lf = (octstr_case_compare(log, octstr_imm("gmt")) == 0) ? 0 : 1;
        octstr_destroy(log);
    }

    /* should predefined markers be used, ie. prefixing timestamp */
    cfg_get_bool(&m, grp, octstr_imm("access-log-clean"));

    /* custom access-log format  */
    if ((log = cfg_get(grp, octstr_imm("access-log-format"))) != NULL) {
        bb_alog_init(log);
        octstr_destroy(log);
    }

    /* open access-log file */
    if ((log = cfg_get(grp, octstr_imm("access-log"))) != NULL) {
        alog_open(octstr_get_cstr(log), lf, m ? 0 : 1);
        octstr_destroy(log);
    }

    if (cfg_get_integer(&store_dump_freq, grp,
                           octstr_imm("store-dump-freq")) == -1)
        store_dump_freq = -1;

    log = cfg_get(grp, octstr_imm("store-file"));
    /* initialize the store file */
    if (log != NULL) {
        warning(0, "'store-file' option deprecated, please use 'store-location' and 'store-type' instead.");
        val = octstr_create("file");
    } else {
        log = cfg_get(grp, octstr_imm("store-location"));
        val = cfg_get(grp, octstr_imm("store-type"));
    }
    if (store_init(val, log, store_dump_freq, msg_pack, msg_unpack_wrapper) == -1)
        panic(0, "Could not start with store init failed.");
    octstr_destroy(val);
    octstr_destroy(log);

    cfg_get_integer(&http_proxy_port, grp, octstr_imm("http-proxy-port"));
#ifdef HAVE_LIBSSL
    cfg_get_bool(&http_proxy_ssl, grp, octstr_imm("http-proxy-ssl"));
#endif /* HAVE_LIBSSL */

    http_proxy_host = cfg_get(grp, 
    	    	    	octstr_imm("http-proxy-host"));
    http_proxy_username = cfg_get(grp, 
    	    	    	    octstr_imm("http-proxy-username"));
    http_proxy_password = cfg_get(grp, 
    	    	    	    octstr_imm("http-proxy-password"));
    http_proxy_exceptions = cfg_get_list(grp,
    	    	    	    octstr_imm("http-proxy-exceptions"));
    http_proxy_exceptions_regex = cfg_get(grp,
    	    	    	    octstr_imm("http-proxy-exceptions-regex"));

    conn_config_ssl (grp);

    /*
     * Make sure we have "ssl-server-cert-file" and "ssl-server-key-file" specified
     * in the core group since we need it to run SSL-enabled internal box 
     * connections configured via "smsbox-port-ssl = yes" and "wapbox-port-ssl = yes".
     * Check only these, because for "admin-port-ssl" and "sendsms-port-ssl" for the 
     * SSL-enabled HTTP servers are probed within gw/bb_http.c:httpadmin_start()
     */
#ifdef HAVE_LIBSSL
    ssl_server_cert_file = cfg_get(grp, octstr_imm("ssl-server-cert-file"));
    ssl_server_key_file = cfg_get(grp, octstr_imm("ssl-server-key-file"));
    if (ssl_server_cert_file != NULL && ssl_server_key_file != NULL) {
       /* we are fine, at least files are specified in the configuration */
    } else {
        cfg_get_bool(&ssl_enabled, grp, octstr_imm("smsbox-port-ssl"));
        cfg_get_bool(&ssl_enabled, grp, octstr_imm("wapbox-port-ssl"));
        if (ssl_enabled) {
	       panic(0, "You MUST specify cert and key files within core group for SSL-enabled inter-box connections!");
        }
    }
    octstr_destroy(ssl_server_cert_file);
    octstr_destroy(ssl_server_key_file);
#endif /* HAVE_LIBSSL */

    /* if all seems to be OK by the first glimpse, real start-up */

    outgoing_sms = gwlist_create();
    incoming_sms = gwlist_create();
    outgoing_wdp = gwlist_create();
    incoming_wdp = gwlist_create();

    outgoing_sms_counter = counter_create();
    incoming_sms_counter = counter_create();
    incoming_dlr_counter = counter_create();
    outgoing_dlr_counter = counter_create();
    outgoing_wdp_counter = counter_create();
    incoming_wdp_counter = counter_create();

    status_mutex = mutex_create();

    outgoing_sms_load = load_create();
    /* add 60,300,-1 entries */
    load_add_interval(outgoing_sms_load, 60);
    load_add_interval(outgoing_sms_load, 300);
    load_add_interval(outgoing_sms_load, -1);
    incoming_sms_load = load_create();
    /* add 60,300,-1 entries */
    load_add_interval(incoming_sms_load, 60);
    load_add_interval(incoming_sms_load, 300);
    load_add_interval(incoming_sms_load, -1);
    incoming_dlr_load = load_create();
    /* add 60,300,-1 entries to dlr */
    load_add_interval(incoming_dlr_load, 60);
    load_add_interval(incoming_dlr_load, 300);
    load_add_interval(incoming_dlr_load, -1);
    outgoing_dlr_load = load_create();
    /* add 60,300,-1 entries to dlr */
    load_add_interval(outgoing_dlr_load, 60);
    load_add_interval(outgoing_dlr_load, 300);
    load_add_interval(outgoing_dlr_load, -1);

    setup_signal_handlers();
    
    /* http-admin is REQUIRED */
    httpadmin_start(cfg);

    if (cfg_get_integer(&max_incoming_sms_qlength, grp,
                           octstr_imm("maximum-queue-length")) == -1)
        max_incoming_sms_qlength = -1;
    else {
        warning(0, "Option 'maximum-queue-length' is deprecated! Please use"
                          " 'sms-incoming-queue-limit' instead!");
    }

    if (max_incoming_sms_qlength == -1 &&
        cfg_get_integer(&max_incoming_sms_qlength, grp,
                                  octstr_imm("sms-incoming-queue-limit")) == -1)
        max_incoming_sms_qlength = -1;
        
    if (cfg_get_integer(&max_outgoing_sms_qlength, grp,
                                  octstr_imm("sms-outgoing-queue-limit")) == -1)
        max_outgoing_sms_qlength = -1;

    if (max_outgoing_sms_qlength < 0)
        max_outgoing_sms_qlength = DEFAULT_OUTGOING_SMS_QLENGTH;

    if (cfg_get_integer(&value, grp, octstr_imm("http-timeout")) == 0)
        http_set_client_timeout(value);
#ifndef NO_SMS    
    {
        List *list;
	
        list = cfg_get_multi_group(cfg, octstr_imm("smsc"));
        if (list != NULL) {
           gwlist_destroy(list, NULL); 
           if (start_smsc(cfg) == -1) {
               panic(0, "Unable to start SMSCs.");
               return NULL;
           }
        }
    }
#endif
    
#ifndef NO_WAP
    grp = cfg_get_single_group(cfg, octstr_imm("core"));
    val = cfg_get(grp, octstr_imm("wdp-interface-name"));
    if (val != NULL && octstr_len(val) > 0)
        start_udp(cfg);
    octstr_destroy(val);

    if (cfg_get_single_group(cfg, octstr_imm("wapbox")) != NULL)
        start_wap(cfg);
#endif

    if (http_proxy_host != NULL && http_proxy_port > 0) {
    	http_use_proxy(http_proxy_host, http_proxy_port, http_proxy_ssl,
		       http_proxy_exceptions, http_proxy_username,
                       http_proxy_password, http_proxy_exceptions_regex);
    }

    octstr_destroy(http_proxy_host);
    octstr_destroy(http_proxy_username);
    octstr_destroy(http_proxy_password);
    octstr_destroy(http_proxy_exceptions_regex);
    gwlist_destroy(http_proxy_exceptions, octstr_destroy_item);

    return cfg;
}
Пример #2
0
/*
 * Read all reloadable configuration directives
 */
static void config_reload(int reload) {
    Cfg *cfg;
    CfgGroup *grp;
    List *groups;
    long map_url_max;
    Octstr *s;
    long i;
    long new_value;
    int new_bool;
    Octstr *http_proxy_host;
    Octstr *http_interface_name;
    long http_proxy_port;
    int http_proxy_ssl = 0;
    List *http_proxy_exceptions;
    Octstr *http_proxy_username;
    Octstr *http_proxy_password;
    Octstr *http_proxy_exceptions_regex;
    int warn_map_url = 0;

    /* XXX TO-DO: if(reload) implement wapbox.suspend/mutex.lock */
    
    if (reload)
        debug("config_reload", 0, "Reloading configuration");

    /* 
     * NOTE: we could lstat config file and only reload if it was modified, 
     * but as we have a include directive, we don't know every file's
     * timestamp at this point
     */

    cfg = cfg_create(config_filename);

    if (cfg_read(cfg) == -1) {
        warning(0, "Couldn't %sload configuration from `%s'.", 
                   (reload ? "re" : ""), octstr_get_cstr(config_filename));
        return;
    }

    grp = cfg_get_single_group(cfg, octstr_imm("core"));

    http_proxy_host = cfg_get(grp, octstr_imm("http-proxy-host"));
    http_proxy_port =  -1;
    cfg_get_integer(&http_proxy_port, grp, octstr_imm("http-proxy-port"));
#ifdef HAVE_LIBSSL
    cfg_get_bool(&http_proxy_ssl, grp, octstr_imm("http-proxy-ssl"));
#endif /* HAVE_LIBSSL */
    http_proxy_username = cfg_get(grp, octstr_imm("http-proxy-username"));
    http_proxy_password = cfg_get(grp, octstr_imm("http-proxy-password"));
    http_proxy_exceptions = cfg_get_list(grp, octstr_imm("http-proxy-exceptions"));
    http_proxy_exceptions_regex = cfg_get(grp, octstr_imm("http-proxy-exceptions-regex"));
    if (http_proxy_host != NULL && http_proxy_port > 0) {
        http_use_proxy(http_proxy_host, http_proxy_port, http_proxy_ssl,
                       http_proxy_exceptions, http_proxy_username, 
                       http_proxy_password, http_proxy_exceptions_regex);
    }
    octstr_destroy(http_proxy_host);
    octstr_destroy(http_proxy_username);
    octstr_destroy(http_proxy_password);
    octstr_destroy(http_proxy_exceptions_regex);
    gwlist_destroy(http_proxy_exceptions, octstr_destroy_item);

    grp = cfg_get_single_group(cfg, octstr_imm("wapbox"));
    if (grp == NULL) {
        warning(0, "No 'wapbox' group in configuration.");
        return;
    }
    
    if (cfg_get_integer(&new_value, grp, octstr_imm("log-level")) != -1) {
        reload_int(reload, octstr_imm("log level"), &logfilelevel, &new_value);
        logfilelevel = new_value;
        log_set_log_level(new_value);
    }

    /* Configure interface name for http requests */
    http_interface_name = cfg_get(grp, octstr_imm("http-interface-name"));
    if (http_interface_name != NULL) {
        http_set_interface(http_interface_name);
        octstr_destroy(http_interface_name);
    }

    /* 
     * users may define 'smart-errors' to have WML decks returned with
     * error information instead of signaling using the HTTP reply codes
     */
    cfg_get_bool(&new_bool, grp, octstr_imm("smart-errors"));
    reload_bool(reload, octstr_imm("smart error messaging"), &wsp_smart_errors, &new_bool);

    /* decide if our XML parser within WML compiler is strict or relaxed */
    cfg_get_bool(&new_bool, grp, octstr_imm("wml-strict"));
    reload_bool(reload, octstr_imm("XML within WML has to be strict"), 
                &wml_xml_strict, &new_bool);
    if (!wml_xml_strict)
        warning(0, "'wml-strict' config directive has been set to no, "
                   "this may make you vulnerable against XML bogus input.");

    if (cfg_get_bool(&new_bool, grp, octstr_imm("concatenation")) == 1)
        reload_bool(reload, octstr_imm("concatenation"), &concatenation, &new_bool);
    else
        concatenation = 1;

    if (cfg_get_integer(&new_value, grp, octstr_imm("max-messages")) != -1) {
        max_messages = new_value;
        reload_int(reload, octstr_imm("max messages"), &max_messages, &new_value);
    }

    /* configure URL mappings */
    map_url_max = -1;
    cfg_get_integer(&map_url_max, grp, octstr_imm("map-url-max"));
    if (map_url_max > 0)
        warn_map_url = 1;

    if (reload) { /* clear old map */
        wap_map_destroy();
        wap_map_user_destroy();
    }
	
    if ((device_home = cfg_get(grp, octstr_imm("device-home"))) != NULL) {
        wap_map_url_config_device_home(octstr_get_cstr(device_home));
    }
    if ((s = cfg_get(grp, octstr_imm("map-url"))) != NULL) {
        warn_map_url = 1;
        wap_map_url_config(octstr_get_cstr(s));
        octstr_destroy(s);
    }
    debug("wap", 0, "map_url_max = %ld", map_url_max);

    for (i = 0; i <= map_url_max; i++) {
        Octstr *name;
        name = octstr_format("map-url-%d", i);
        if ((s = cfg_get(grp, name)) != NULL)
            wap_map_url_config(octstr_get_cstr(s));
        octstr_destroy(name);
    }

    /* warn the user that he/she should use the new wap-url-map groups */
    if (warn_map_url)
        warning(0, "'map-url' config directive and related are deprecated, "
                   "please use wap-url-map group");

    /* configure wap-url-map */
    groups = cfg_get_multi_group(cfg, octstr_imm("wap-url-map"));
    while (groups && (grp = gwlist_extract_first(groups)) != NULL) {
        Octstr *name, *url, *map_url, *send_msisdn_query;
        Octstr *send_msisdn_header, *send_msisdn_format;
        int accept_cookies;

        name = cfg_get(grp, octstr_imm("name"));
        url = cfg_get(grp, octstr_imm("url"));
        map_url = cfg_get(grp, octstr_imm("map-url"));
        send_msisdn_query = cfg_get(grp, octstr_imm("send-msisdn-query"));
        send_msisdn_header = cfg_get(grp, octstr_imm("send-msisdn-header"));
        send_msisdn_format = cfg_get(grp, octstr_imm("send-msisdn-format"));
        accept_cookies = -1;
        cfg_get_bool(&accept_cookies, grp, octstr_imm("accept-cookies"));

        wap_map_add_url(name, url, map_url, send_msisdn_query, send_msisdn_header,
                        send_msisdn_format, accept_cookies);

        info(0, "Added wap-url-map <%s> with url <%s>, map-url <%s>, "
                "send-msisdn-query <%s>, send-msisdn-header <%s>, "
                "send-msisdn-format <%s>, accept-cookies <%s>", 
             octstr_get_cstr(name), octstr_get_cstr(url), 
             octstr_get_cstr(map_url), octstr_get_cstr(send_msisdn_query), 
             octstr_get_cstr(send_msisdn_header), 
             octstr_get_cstr(send_msisdn_format), (accept_cookies ? "yes" : "no"));
    }
    gwlist_destroy(groups, NULL);

    /* configure wap-user-map */
    groups = cfg_get_multi_group(cfg, octstr_imm("wap-user-map"));
    while (groups && (grp = gwlist_extract_first(groups)) != NULL) {
        Octstr *name, *user, *pass, *msisdn;

        name = cfg_get(grp, octstr_imm("name"));
        user = cfg_get(grp, octstr_imm("user"));
        pass = cfg_get(grp, octstr_imm("pass"));
        msisdn = cfg_get(grp, octstr_imm("msisdn"));
           
        wap_map_add_user(name, user, pass, msisdn);

        info(0,"Added wap-user-map <%s> with credentials <%s:%s> "
               "and MSISDN <%s>", octstr_get_cstr(name),
             octstr_get_cstr(user), octstr_get_cstr(pass),
             octstr_get_cstr(msisdn));
    }
    gwlist_destroy(groups, NULL);

    cfg_destroy(cfg);
    /* XXX TO-DO: if(reload) implement wapbox.resume/mutex.unlock */
}
Пример #3
0
/******************************************************************************
 ...
******************************************************************************/
uerr_t proz_http_get_url_info(connection_t * connection)
{
  uerr_t err;
  int remote_port_len;
  char *user, *passwd, *www_auth = NULL, *proxy_auth = NULL,
      *referer = NULL, *location = NULL, *pragma_no_cache = NULL;
  char *request, *remote_port;
  netrc_entry *netrc_ent;

  memset(&connection->hs, 0, sizeof(connection->hs));

  if (http_use_proxy(connection))
  {
    connection_show_message(connection, _("Connecting to %s"),
			    connection->http_proxy->proxy_url.host);

    err = connect_to_server(&connection->data_sock,
			    connection->http_proxy->proxy_url.host,
			    connection->http_proxy->proxy_url.port,
			    &connection->xfer_timeout);
    if (err != NOCONERROR)
    {
      connection_show_message(connection, _("Error connecting to %s"),
			      connection->http_proxy->proxy_url.host);
      return err;
    }
  } else
  {
    connection_show_message(connection, _("Connecting to %s"),
			    connection->u.host);
    err = connect_to_server(&connection->data_sock, connection->u.host,
			    connection->u.port, &connection->xfer_timeout);
    if (err != NOCONERROR)
    {
      connection_show_message(connection, _("Error connecting to %s"),
			      connection->u.host);
      return err;
    }
  }

  user = connection->u.user;
  passwd = connection->u.passwd;

  /* Use .netrc if asked to do so. */
  if (connection->use_netrc == TRUE)
  {
    netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host);

    if (netrc_ent != NULL)
    {
      user = netrc_ent->account;
      passwd = netrc_ent->password;
    }
  }

  user = user ? user : "";
  passwd = passwd ? passwd : "";

  if (strlen(user) || strlen(passwd))
  {
    /* Construct the necessary header. */
    www_auth = get_basic_auth_str(user, passwd, "Authorization");
    proz_debug(_("Authenticating as user %s password %s"), user, passwd);
    proz_debug(_("Authentification string=%s"), www_auth);
  } else
    www_auth = 0;

  if (http_use_proxy(connection))
  {
    if (strlen(connection->http_proxy->username)
	|| strlen(connection->http_proxy->passwd))
      proxy_auth =
	  get_basic_auth_str(connection->http_proxy->username,
			     connection->http_proxy->passwd,
			     "Proxy-Authorization");
  }

  if (connection->u.port == 80)
  {
    remote_port = NULL;
    remote_port_len = 0;
  } else
  {
    remote_port = (char *) alloca(64);
    remote_port_len = sprintf(remote_port, ":%d", connection->u.port);
  }


  if (connection->u.referer)
  {
    referer = (char *) alloca(13 + strlen(connection->u.referer));
    sprintf(referer, "Referer: %s\r\n", connection->u.referer);
  }

  /* If we go through a proxy the request for the URL is different */
  if (http_use_proxy(connection))
  {
    location = (char *) alloca(strlen(connection->u.url) + 1);
    strcpy(location, connection->u.url);
  } else
  {
    location = (char *) alloca(strlen(connection->u.path) + 1);
    strcpy(location, connection->u.path);
  }

  /*Use no-cache directive for proxy servers? */
  if (http_use_proxy(connection)
      && (connection->http_no_cache || connection->attempts > 0))
  {
    pragma_no_cache = (char *) alloca(21);
    sprintf(pragma_no_cache, "Pragma: no-cache\r\n");
  }

  request = (char *) alloca(strlen(location)
			    + strlen(connection->user_agent)
			    + strlen(connection->u.host) + remote_port_len
			    + (referer ? strlen(referer) : 0)
			    + (www_auth ? strlen(www_auth) : 0)
			    + (proxy_auth ? strlen(proxy_auth) : 0) + 64
			    +
			    (pragma_no_cache ? strlen(pragma_no_cache) :
			     0));

  sprintf(request,
	  "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s%s\r\n",
	  location, connection->user_agent, connection->u.host,
	  remote_port ? remote_port : "",
	  referer ? referer : "",
	  www_auth ? www_auth : "", proxy_auth ? proxy_auth : "",
	  pragma_no_cache ? pragma_no_cache : "");

  proz_debug("HTTP request = %s", request);

  connection_show_message(connection, _("Sending HTTP request"));
  err = http_fetch_headers(connection, &connection->hs, request);

  close_sock(&connection->data_sock);

  if (err == HOK)
  {
    connection->main_file_size = connection->hs.contlen;
    if (connection->hs.accept_ranges == 1)
      connection->resume_support = TRUE;
    else if (connection->hs.accept_ranges == -1)
      connection->resume_support = FALSE;
  }


  connection->file_type = REGULAR_FILE;
  return err;
}
Пример #4
0
uerr_t ftp_get_url_info_from_http_proxy(connection_t * connection)
{

  uerr_t err;
  int remote_port_len;
  char *user, *passwd, *www_auth = NULL, *proxy_auth =
      NULL, *pragma_no_cache = NULL;

  char *request, *remote_port;
  netrc_entry *netrc_ent;

  memset(&connection->hs, 0, sizeof(connection->hs));

  err = connect_to_server(&connection->data_sock,
			  connection->ftp_proxy->proxy_url.host,
			  connection->ftp_proxy->proxy_url.port,
			  &connection->xfer_timeout);

  if (err != NOCONERROR)
  {
    connection_show_message(connection, _("Error connecting to %s"),
			    connection->ftp_proxy->proxy_url.host);
    return err;
  }

  user = connection->u.user;
  passwd = connection->u.passwd;

  /* Use .netrc if asked to do so. */
  if (connection->use_netrc == TRUE)
  {
    netrc_ent = search_netrc(libprozrtinfo.netrc_list, connection->u.host);

    if (netrc_ent != NULL)
    {
      user = netrc_ent->account;
      passwd = netrc_ent->password;
    }
  }

  user = user ? user : "";
  passwd = passwd ? passwd : "";

  if (strlen(user) || strlen(passwd))
  {
    /* Construct the necessary header. */
    www_auth = get_basic_auth_str(user, passwd, "Authorization");
    proz_debug(_("Authenticating as user %s password %s"), user, passwd);
    proz_debug(_("Authentification string=%s"), www_auth);
  } else
    www_auth = 0;

  if (strlen(connection->ftp_proxy->username)
      || strlen(connection->ftp_proxy->passwd))
    proxy_auth =
	get_basic_auth_str(connection->ftp_proxy->username,
			   connection->ftp_proxy->passwd,
			   "Proxy-Authorization");

  remote_port = (char *) alloca(64);
  remote_port_len = sprintf(remote_port, ":%d", connection->u.port);

  if (http_use_proxy(connection)
      && (connection->http_no_cache || connection->attempts > 0))
  {
    pragma_no_cache = (char *) alloca(21);
    sprintf(pragma_no_cache, "Pragma: no-cache\r\n");
  }

  /*Referrer TAG should not be needed in FTP through HTTP proxy..right */


  request = (char *) alloca(strlen(connection->u.url)
			    + strlen(connection->user_agent)
			    + strlen(connection->u.host) + remote_port_len
			    + (www_auth ? strlen(www_auth) : 0)
			    + (proxy_auth ? strlen(proxy_auth) : 0)
			    + 64
			    +
			    (pragma_no_cache ? strlen(pragma_no_cache) :
			     0));

  sprintf(request,
	  "GET %s HTTP/1.0\r\nUser-Agent: %s\r\nHost: %s%s\r\nAccept: */*\r\n%s%s%s\r\n",
	  connection->u.url, connection->user_agent, connection->u.host,
	  remote_port,
	  www_auth ? www_auth : "", proxy_auth ? proxy_auth : "",
	  pragma_no_cache ? pragma_no_cache : "");

  proz_debug("HTTP request = %s", request);


  err = http_fetch_headers(connection, &connection->hs, request);

  close_sock(&connection->data_sock);

  /*Convert the error code to the equivalent FTP one if possible */

  if (err == HOK)
  {
    connection->main_file_size = connection->hs.contlen;
    if (connection->hs.accept_ranges == 1)
      connection->resume_support = TRUE;
    else if (connection->hs.accept_ranges == -1)
      connection->resume_support = FALSE;
    return FTPOK;
  }


  if (err == HAUTHREQ)
    return FTPLOGREFUSED;
  else if (err == HTTPNSFOD)
    return FTPNSFOD;


  /*  connection->file_type = REGULAR_FILE; */
  return FTPERR;

}
Пример #5
0
int main(int argc, char **argv) 
{
    int i, opt, num_threads;
    Octstr *proxy;
    List *exceptions;
    long proxy_port;
    int proxy_ssl = 0;
    Octstr *proxy_username;
    Octstr *proxy_password;
    Octstr *exceptions_regex;
    char *p;
    long threads[MAX_THREADS];
    time_t start, end;
    double run_time;
    FILE *fp;
    int ssl = 0;
    
    gwlib_init();
    
    proxy = NULL;
    proxy_port = -1;
    exceptions = gwlist_create();
    proxy_username = NULL;
    proxy_password = NULL;
    exceptions_regex = NULL;
    num_threads = 1;
    file = 0;
    fp = NULL;
    
    while ((opt = getopt(argc, argv, "hv:qr:p:P:Se:t:i:a:u:sc:H:B:m:f")) != EOF) {
	switch (opt) {
	case 'v':
	    log_set_output_level(atoi(optarg));
	    break;
	
	case 'q':
	    verbose = 0;
	    break;
	
	case 'r':
	    max_requests = atoi(optarg);
	    break;
	
	case 't':
	    num_threads = atoi(optarg);
	    if (num_threads > MAX_THREADS)
		num_threads = MAX_THREADS;
	    break;

	case 'i':
	    interval = atof(optarg);
	    break;

    case 'u':
        file = 1;
        fp = fopen(optarg, "a");
        if (fp == NULL)
            panic(0, "Cannot open message text file %s", optarg);
        msg_text = octstr_read_file(optarg);
        if (msg_text == NULL)
            panic(0, "Cannot read message text");
        debug("", 0, "message text is");
        octstr_dump(msg_text, 0);
        octstr_url_encode(msg_text);
        fclose(fp);
        break;
	
	case 'h':
	    help();
	    exit(0);
	
	case 'p':
	    proxy = octstr_create(optarg);
	    break;
	
	case 'P':
	    proxy_port = atoi(optarg);
	    break;

	case 'S':
        proxy_ssl = 1;
        break;
	
	case 'e':
	    p = strtok(optarg, ":");
	    while (p != NULL) {
		gwlist_append(exceptions, octstr_create(p));
		p = strtok(NULL, ":");
	    }
	    break;

   case 'E':
       exceptions_regex = octstr_create(optarg);
       break;

	case 'a':
	    p = strtok(optarg, ":");
	    if (p != NULL) {
		auth_username = octstr_create(p);
		p = strtok(NULL, "");
		if (p != NULL)
		    auth_password = octstr_create(p);
	    }
	    break;

    case 's':
        ssl = 1;
        break;

    case 'c':
	    octstr_destroy(ssl_client_certkey_file);
	    ssl_client_certkey_file = octstr_create(optarg);
        break;

    case 'H':
        fp = fopen(optarg, "a");
        if (fp == NULL)
            panic(0, "Cannot open header text file %s", optarg);
        extra_headers = octstr_read_file(optarg);
        if (extra_headers == NULL)
            panic(0, "Cannot read header text");
        debug("", 0, "headers are");
        octstr_dump(extra_headers, 0);
        split_headers(extra_headers, &split);
        fclose(fp);
        break;

    case 'B':
        content_file = octstr_create(optarg);
        break;

	case 'm':
	    method_name = octstr_create(optarg);
	    break;

    case 'f':
        follow_redirect = 0;
        break;

    case '?':
	default:
	    error(0, "Invalid option %c", opt);
	    help();
	    panic(0, "Stopping.");
	}
    }
    
    if (optind == argc) {
        help();
        exit(0);
    }

#ifdef HAVE_LIBSSL
    /*
     * check if we are doing a SSL-enabled client version here
     * load the required cert and key file
     */
    if (ssl || proxy_ssl) {
        if (ssl_client_certkey_file != NULL) {
            use_global_client_certkey_file(ssl_client_certkey_file);
        } else {
            panic(0, "client certkey file need to be given!");
        }
    }
#endif

    if (method_name != NULL) {
        method = http_name2method(method_name);
    }
    
    if (proxy != NULL && proxy_port > 0) {
        http_use_proxy(proxy, proxy_port, proxy_ssl, exceptions,
        proxy_username, proxy_password, exceptions_regex);
    }
    octstr_destroy(proxy);
    octstr_destroy(proxy_username);
    octstr_destroy(proxy_password);
    octstr_destroy(exceptions_regex);
    gwlist_destroy(exceptions, octstr_destroy_item);
    
    urls = argv + optind;
    num_urls = argc - optind;
    
    time(&start);
    if (num_threads == 1)
        client_thread(http_caller_create());
    else {
        for (i = 0; i < num_threads; ++i)
            threads[i] = gwthread_create(client_thread, http_caller_create());
        for (i = 0; i < num_threads; ++i)
            gwthread_join(threads[i]);
    }
    time(&end);
    
    run_time = difftime(end, start);
    info(0, "%ld requests in %f seconds, %f requests/s.",
         (max_requests * num_threads), run_time, (max_requests * num_threads) / run_time);
    
    octstr_destroy(ssl_client_certkey_file);
    octstr_destroy(auth_username);
    octstr_destroy(auth_password);
    octstr_destroy(extra_headers);
    octstr_destroy(content_file);
    gwlist_destroy(split, octstr_destroy_item);
    
    gwlib_shutdown();
    
    return 0;
}