示例#1
0
文件: gce.c 项目: collectd/collectd
char *gce_scope(char const *email) /* {{{ */
{
  char url[1024];

  snprintf(url, sizeof(url), GCE_SCOPE_URL_FORMAT,
           (email != NULL) ? email : GCE_DEFAULT_SERVICE_ACCOUNT);

  return read_url(url);
} /* }}} char *gce_scope */
示例#2
0
int main(int argc, char** argv) {
  if (argc != 2) {
    std::cerr << "usage: main <config-file>" << std::endl;
    return 1;
  }

  std::string url = read_url(argv[1]);
  if (!url.size()) {
    std::cerr << "error: none or empty url" << std::endl;
    return 1;
  }

  std::cout << "retrieving XML from \"" << url << "\" ..." << std::endl;
  std::ostringstream data;
  try {
    curlpp::Cleanup cleanup;
    curlpp::Easy request;
    request.setOpt<curlpp::options::WriteStream>(&data);
    request.setOpt<curlpp::options::Url>(url);
    request.perform();
  }
  catch (std::exception& e) {
    std::cerr << e.what() << std::endl;
    return 1;
  }

  std::cout << "parsing XML document ..." << std::endl << std::endl;
  tinyxml2::XMLDocument doc;
  tinyxml2::XMLError error = doc.Parse(data.str().c_str());
  if (error != tinyxml2::XML_SUCCESS) {
    std::cerr << "tinyxml2 error: " << error << std::endl;
    return 1;
  }

  data << "\n\nTags:\n\n";
  dump_xml(data, doc.RootElement());

  #ifdef HAVE_QT5
    QApplication app(argc, argv);
    QWidget window;

    QHBoxLayout layout(&window);
    window.setLayout(&layout);

    QLabel label(data.str().c_str());
    layout.addWidget(&label);

    window.show();
    return app.exec();
  #else
    std::cout << data.str() << std::endl;
    return 0;
  #endif
}
示例#3
0
文件: gce.c 项目: collectd/collectd
int gce_access_token(char const *email, char *buffer,
                     size_t buffer_size) /* {{{ */
{
  char url[1024];
  char *json;
  cdtime_t now = cdtime();

  pthread_mutex_lock(&token_lock);

  if (email == NULL)
    email = GCE_DEFAULT_SERVICE_ACCOUNT;

  if ((token_email != NULL) && (strcmp(email, token_email) == 0) &&
      (token_valid_until > now)) {
    sstrncpy(buffer, token, buffer_size);
    pthread_mutex_unlock(&token_lock);
    return 0;
  }

  snprintf(url, sizeof(url), GCE_TOKEN_URL_FORMAT, email);
  json = read_url(url);
  if (json == NULL) {
    pthread_mutex_unlock(&token_lock);
    return -1;
  }

  char tmp[256];
  cdtime_t expires_in = 0;
  int status = oauth_parse_json_token(json, tmp, sizeof(tmp), &expires_in);
  sfree(json);
  if (status != 0) {
    pthread_mutex_unlock(&token_lock);
    return status;
  }

  sfree(token);
  token = strdup(tmp);

  sfree(token_email);
  token_email = strdup(email);

  /* let tokens expire a bit early */
  expires_in = (expires_in * 95) / 100;
  token_valid_until = now + expires_in;

  sstrncpy(buffer, token, buffer_size);
  pthread_mutex_unlock(&token_lock);
  return 0;
} /* }}} char *gce_token */
示例#4
0
文件: gce.c 项目: collectd/collectd
char *gce_zone(void) /* {{{ */
{
  return read_url(GCE_ZONE_URL);
} /* }}} char *gce_instance_id */
示例#5
0
文件: gce.c 项目: collectd/collectd
char *gce_instance_id(void) /* {{{ */
{
  return read_url(GCE_INSTANCE_ID_URL);
} /* }}} char *gce_instance_id */
示例#6
0
文件: gce.c 项目: collectd/collectd
char *gce_project_id(void) /* {{{ */
{
  return read_url(GCE_PROJECT_ID_URL);
} /* }}} char *gce_project_id */
示例#7
0
static void test__search_input_by_name( const char *param )
{
	gchar         *input_names[ ] = { "name1", "name2", "name3", NULL };
	input_field_t input1          = { .name = "name2", .value = "value2" };
	input_field_t input2          = { .name = "name3", .value = "value3" };
	input_field_t input4          = { .name = "name4", .value = "value4" };
	int found;

	found = search_input_by_name( &input1, input_names );
	printf( "1: %d\n", found );
	found = search_input_by_name( &input2, input_names );
	printf( "2: %d\n", found );
	found = search_input_by_name( &input4, input_names );
	printf( "3: %d\n", found );
}



static void test__search_form_by_action_and_inputs( const char *param )
{
	input_field_t input1 = { "in1", "ivalue1" };
	input_field_t input2 = { "in2", "ivalue2" };
	input_field_t input3 = { "in3", "ivalue3" };
	GSList        *input;

	form_field_t form1 = { "form1", "value1", "action1", NULL };

	const gchar *search1[ ] = { "in1", NULL };
	const gchar *search2[ ] = { "in4", NULL };
	const gchar *search3[ ] = { "in2", NULL };
	struct form_search_t search_form1 = { "action1", search1 };
	struct form_search_t search_form2 = { "act", search2 };
	struct form_search_t search_form3 = { "action4", search3 };

	int found;

	input = g_slist_append( NULL, &input1 );
	input = g_slist_append( input, &input2 );
	input = g_slist_append( input, &input3 );

	form1.input_fields = input;
	found = search_form_by_action_and_inputs( &form1, &search_form1 );
	printf( "1: %d\n", found );
	found = search_form_by_action_and_inputs( &form1, &search_form2 );
	printf( "2: %d\n", found );
	found = search_form_by_action_and_inputs( &form1, &search_form3 );
	printf( "3: %d\n", found );
}



static void test__find_form( const char *param )
{
	FILE *filehd;
	char *html_buf;
	int    ctr;
	form_field_t *form1;
	form_field_t *form2;
	const char *input_names[ ] = { "bgresponse", "submit_access", NULL };

	html_buf = malloc( 75000 );

	filehd = fopen( "../../data/oauth2-confirm.html", "r" );
	if( ! filehd )
	{
		printf( "Error: cannot find test data\n" );
		exit( EXIT_FAILURE );
	}
	if( fread( html_buf, 75000, 1, filehd ) )
	{
		printf( "Error: cannot read test data\n" );
		exit( EXIT_FAILURE );
	}

	ctr = 1;
	form1 = find_form( html_buf, "https://accounts.google.com/", input_names );
	print_form( form1, &ctr );
	form2 = find_form( html_buf, "https://accounts.google.com/o/oauth2/nonexistent", input_names );
	if( form2 != NULL )
	{
		printf( "2: found\n" );
	}
	else
	{
		printf( "2: not found\n" );
	}
}



static void test__receive_curl_response( const char *param )
{
	struct curl_write_buffer_t write_buffer = { NULL, 0 };
	const char                 *testdata = "01234567891011121314151617181920";
	size_t                     processed;

	processed = receive_curl_response( testdata, 5, 3, &write_buffer );
	printf( "1: %2d \"%s\"\n", (int) processed, write_buffer.data );

	processed = receive_curl_response( &testdata[ 15 ], 10, 1, &write_buffer );
	printf( "2: %2d \"%s\"\n", (int) processed, write_buffer.data );

	processed = receive_curl_response( &testdata[ 25 ], 1, 3, &write_buffer );
	printf( "3: %2d \"%s\"\n", (int) processed, write_buffer.data );
}



static void test__read_url( const char *param )
{
	CURL  *curl;
	gchar *html;

	/* Skip test if no internet access is available. */
	if( test_check_internet( ) == FALSE )
	{
		exit( 77 );
	}

	curl = curl_easy_init( );
	html = read_url( curl, "http://www.microsoft.com" );
	curl_easy_cleanup( curl );
	if( html != NULL )
	{
		printf( "%s\n", html );
		g_free( html );
	}
}
示例#8
0
/* Allocate an entry from POOL and read it from [*BUF, END).  The
   buffer may be modified in place while parsing.  Return the new
   entry in *NEW_ENTRY.  Advance *BUF to point at the end of the entry
   record.
   The entries file format should be provided in ENTRIES_FORMAT. */
static svn_error_t *
read_entry(svn_wc_entry_t **new_entry,
           char **buf, const char *end,
           int entries_format,
           apr_pool_t *pool)
{
    svn_wc_entry_t *entry = alloc_entry(pool);
    const char *name;

#define MAYBE_DONE if (**buf == '\f') goto done

    /* Find the name and set up the entry under that name. */
    SVN_ERR(read_path(&name, buf, end, pool));
    entry->name = name ? name : SVN_WC_ENTRY_THIS_DIR;

    /* Set up kind. */
    {
        const char *kindstr;
        SVN_ERR(read_val(&kindstr, buf, end));
        if (kindstr)
        {
            if (strcmp(kindstr, ENTRIES_VALUE_FILE) == 0)
                entry->kind = svn_node_file;
            else if (strcmp(kindstr, ENTRIES_VALUE_DIR) == 0)
                entry->kind = svn_node_dir;
            else
                return svn_error_createf
                       (SVN_ERR_NODE_UNKNOWN_KIND, NULL,
                        _("Entry '%s' has invalid node kind"),
                        (name ? name : SVN_WC_ENTRY_THIS_DIR));
        }
        else
            entry->kind = svn_node_none;
    }
    MAYBE_DONE;

    /* Attempt to set revision (resolve_to_defaults may do it later, too) */
    SVN_ERR(read_revnum(&entry->revision, buf, end, pool));
    MAYBE_DONE;

    /* Attempt to set up url path (again, see resolve_to_defaults). */
    SVN_ERR(read_url(&entry->url, buf, end, entries_format, pool));
    MAYBE_DONE;

    /* Set up repository root.  Make sure it is a prefix of url. */
    SVN_ERR(read_url(&entry->repos, buf, end, entries_format, pool));
    if (entry->repos && entry->url
            && ! svn_uri__is_ancestor(entry->repos, entry->url))
        return svn_error_createf(SVN_ERR_WC_CORRUPT, NULL,
                                 _("Entry for '%s' has invalid repository "
                                   "root"),
                                 name ? name : SVN_WC_ENTRY_THIS_DIR);
    MAYBE_DONE;

    /* Look for a schedule attribute on this entry. */
    {
        const char *schedulestr;
        SVN_ERR(read_val(&schedulestr, buf, end));
        entry->schedule = svn_wc_schedule_normal;
        if (schedulestr)
        {
            if (strcmp(schedulestr, ENTRIES_VALUE_ADD) == 0)
                entry->schedule = svn_wc_schedule_add;
            else if (strcmp(schedulestr, ENTRIES_VALUE_DELETE) == 0)
                entry->schedule = svn_wc_schedule_delete;
            else if (strcmp(schedulestr, ENTRIES_VALUE_REPLACE) == 0)
                entry->schedule = svn_wc_schedule_replace;
            else
                return svn_error_createf(
                           SVN_ERR_ENTRY_ATTRIBUTE_INVALID, NULL,
                           _("Entry '%s' has invalid 'schedule' value"),
                           name ? name : SVN_WC_ENTRY_THIS_DIR);
        }
    }
    MAYBE_DONE;

    /* Attempt to set up text timestamp. */
    SVN_ERR(read_time(&entry->text_time, buf, end, pool));
    MAYBE_DONE;

    /* Checksum. */
    SVN_ERR(read_str(&entry->checksum, buf, end, pool));
    MAYBE_DONE;

    /* Setup last-committed values. */
    SVN_ERR(read_time(&entry->cmt_date, buf, end, pool));
    MAYBE_DONE;

    SVN_ERR(read_revnum(&entry->cmt_rev, buf, end, pool));
    MAYBE_DONE;

    SVN_ERR(read_str(&entry->cmt_author, buf, end, pool));
    MAYBE_DONE;

    /* has-props, has-prop-mods, cachable-props, present-props are all
       deprecated. Read any values that may be in the 'entries' file, but
       discard them, and just put default values into the entry. */
    {
        const char *unused_value;

        /* has-props flag. */
        SVN_ERR(read_val(&unused_value, buf, end));
        entry->has_props = FALSE;
        MAYBE_DONE;

        /* has-prop-mods flag. */
        SVN_ERR(read_val(&unused_value, buf, end));
        entry->has_prop_mods = FALSE;
        MAYBE_DONE;

        /* Use the empty string for cachable_props, indicating that we no
           longer attempt to cache any properties. An empty string for
           present_props means that no cachable props are present. */

        /* cachable-props string. */
        SVN_ERR(read_val(&unused_value, buf, end));
        entry->cachable_props = "";
        MAYBE_DONE;

        /* present-props string. */
        SVN_ERR(read_val(&unused_value, buf, end));
        entry->present_props = "";
        MAYBE_DONE;
    }

    /* Is this entry in a state of mental torment (conflict)? */
    {
        SVN_ERR(read_path(&entry->prejfile, buf, end, pool));
        MAYBE_DONE;
        SVN_ERR(read_path(&entry->conflict_old, buf, end, pool));
        MAYBE_DONE;
        SVN_ERR(read_path(&entry->conflict_new, buf, end, pool));
        MAYBE_DONE;
        SVN_ERR(read_path(&entry->conflict_wrk, buf, end, pool));
        MAYBE_DONE;
    }

    /* Is this entry copied? */
    SVN_ERR(read_bool(&entry->copied, ENTRIES_BOOL_COPIED, buf, end));
    MAYBE_DONE;

    SVN_ERR(read_url(&entry->copyfrom_url, buf, end, entries_format, pool));
    MAYBE_DONE;
    SVN_ERR(read_revnum(&entry->copyfrom_rev, buf, end, pool));
    MAYBE_DONE;

    /* Is this entry deleted? */
    SVN_ERR(read_bool(&entry->deleted, ENTRIES_BOOL_DELETED, buf, end));
    MAYBE_DONE;

    /* Is this entry absent? */
    SVN_ERR(read_bool(&entry->absent, ENTRIES_BOOL_ABSENT, buf, end));
    MAYBE_DONE;

    /* Is this entry incomplete? */
    SVN_ERR(read_bool(&entry->incomplete, ENTRIES_BOOL_INCOMPLETE, buf, end));
    MAYBE_DONE;

    /* UUID. */
    SVN_ERR(read_str(&entry->uuid, buf, end, pool));
    MAYBE_DONE;

    /* Lock token. */
    SVN_ERR(read_str(&entry->lock_token, buf, end, pool));
    MAYBE_DONE;

    /* Lock owner. */
    SVN_ERR(read_str(&entry->lock_owner, buf, end, pool));
    MAYBE_DONE;

    /* Lock comment. */
    SVN_ERR(read_str(&entry->lock_comment, buf, end, pool));
    MAYBE_DONE;

    /* Lock creation date. */
    SVN_ERR(read_time(&entry->lock_creation_date, buf, end, pool));
    MAYBE_DONE;

    /* Changelist. */
    SVN_ERR(read_str(&entry->changelist, buf, end, pool));
    MAYBE_DONE;

    /* Keep entry in working copy after deletion? */
    SVN_ERR(read_bool(&entry->keep_local, ENTRIES_BOOL_KEEP_LOCAL, buf, end));
    MAYBE_DONE;

    /* Translated size */
    {
        const char *val;

        /* read_val() returns NULL on an empty (e.g. default) entry line,
           and entry has already been initialized accordingly already */
        SVN_ERR(read_val(&val, buf, end));
        if (val)
            entry->working_size = (apr_off_t)apr_strtoi64(val, NULL, 0);
    }
    MAYBE_DONE;

    /* Depth. */
    {
        const char *result;
        SVN_ERR(read_val(&result, buf, end));
        if (result)
        {
            svn_boolean_t invalid;
            svn_boolean_t is_this_dir;

            entry->depth = svn_depth_from_word(result);

            /* Verify the depth value:
               THIS_DIR should not have an excluded value and SUB_DIR should only
               have excluded value. Remember that infinity value is not stored and
               should not show up here. Otherwise, something bad may have
               happened. However, infinity value itself will always be okay. */
            is_this_dir = !name;
            /* '!=': XOR */
            invalid = is_this_dir != (entry->depth != svn_depth_exclude);
            if (entry->depth != svn_depth_infinity && invalid)
                return svn_error_createf(
                           SVN_ERR_ENTRY_ATTRIBUTE_INVALID, NULL,
                           _("Entry '%s' has invalid 'depth' value"),
                           name ? name : SVN_WC_ENTRY_THIS_DIR);
        }
        else
            entry->depth = svn_depth_infinity;

    }
    MAYBE_DONE;

    /* Tree conflict data. */
    SVN_ERR(read_str(&entry->tree_conflict_data, buf, end, pool));
    MAYBE_DONE;

    /* File external URL and revision. */
    {
        const char *str;
        SVN_ERR(read_str(&str, buf, end, pool));
        SVN_ERR(svn_wc__unserialize_file_external(&entry->file_external_path,
                &entry->file_external_peg_rev,
                &entry->file_external_rev,
                str,
                pool));
    }
    MAYBE_DONE;

done:
    *new_entry = entry;
    return SVN_NO_ERROR;
}