GError *
peer_dump(const gchar *target, struct sqlx_name_s *name, gboolean chunked,
		peer_dump_cb callback, gpointer cb_arg)
{
	struct gridd_client_s *client;
	GByteArray *encoded;
	GError *err = NULL;

	gboolean on_reply(gpointer ctx, MESSAGE reply) {
		GError *err2 = NULL;
		gsize bsize = 0;
		gint64 remaining = -1;
		(void) ctx;

		err2 = metautils_message_extract_strint64(reply, "remaining", &remaining);
		g_clear_error(&err2);

		void *b = metautils_message_get_BODY(reply, &bsize);
		if (b && bsize) {
			GByteArray *dump = g_byte_array_new();
			g_byte_array_append(dump, b, bsize);
			err2 = callback(dump, remaining, cb_arg);
		}
		if (err2 != NULL) {
			GRID_ERROR("Failed to use result of dump: (%d) %s",
					err2->code, err2->message);
			g_clear_error(&err2);
			return FALSE;
		}
		return TRUE;
	}
GError *
metautils_message_extract_body_encoded(MESSAGE msg, gboolean mandatory,
		GSList **result, body_decoder_f decoder)
{
	EXTRA_ASSERT(result != NULL);
	EXTRA_ASSERT(decoder != NULL);

	gsize bsize = 0;
	void *b = metautils_message_get_BODY(msg, &bsize);
	if (!b) {
		if (mandatory)
			return NEWERROR(CODE_BAD_REQUEST, "Missing body");
		return NULL;
    }

	GError *err = NULL;
	int rc = decoder(result, b, bsize, &err);
	if (rc <= 0) {
		EXTRA_ASSERT(err != NULL);
		err->code = CODE_BAD_REQUEST;
		g_prefix_error(&err, "Invalid body: ");
		return err;
	}

	return NULL;
}
static gboolean
meta1_dispatch_v2_USERCREATE(struct gridd_reply_ctx_s *reply,
		struct meta1_backend_s *m1, gpointer ignored UNUSED)
{
	struct oio_url_s *url = metautils_message_extract_url (reply->request);
	reply->subject("%s|%s", oio_url_get(url, OIOURL_WHOLE), oio_url_get(url, OIOURL_HEXID));

	gsize length = 0;
	void *body = metautils_message_get_BODY(reply->request, &length);
	gchar **properties = NULL;
	GError *err = KV_decode_buffer(body, length, &properties);
	if (NULL != err) {
		reply->send_error(0, err);
		return TRUE;
	}

	err = meta1_backend_user_create(m1, url, properties);
	g_strfreev(properties);

	if (NULL != err)
		reply->send_error(0, err);
	else
		reply->send_reply(CODE_FINAL_OK, "Created");

	oio_url_clean (url);
	return TRUE;
}
static gboolean
meta1_dispatch_v2_PROPDEL(struct gridd_reply_ctx_s *reply,
		struct meta1_backend_s *m1, gpointer ignored UNUSED)
{
	struct oio_url_s *url = metautils_message_extract_url (reply->request);
	reply->subject("%s|%s", oio_url_get(url, OIOURL_WHOLE), oio_url_get(url, OIOURL_HEXID));

	gchar **keys = NULL;
	gsize length = 0;
	void *body = metautils_message_get_BODY(reply->request, &length);
	GError *err = STRV_decode_buffer(body, length, &keys);

	if (NULL != err)
		reply->send_error(CODE_BAD_REQUEST, err);
	else {
		err = meta1_backend_del_container_properties(m1, url, keys);
		if (err)
			reply->send_error(0, err);
		else
			reply->send_reply(CODE_FINAL_OK, "OK");
		g_strfreev (keys);
	}

	oio_url_clean (url);
	return TRUE;
}
static gboolean
meta1_dispatch_v2_SRV_UNLINK(struct gridd_reply_ctx_s *reply,
		struct meta1_backend_s *m1, gpointer ignored UNUSED)
{
	gchar *srvtype = metautils_message_extract_string_copy (reply->request, NAME_MSGKEY_TYPENAME);
	struct oio_url_s *url = metautils_message_extract_url (reply->request);
	reply->subject("%s|%s|%s", oio_url_get(url, OIOURL_WHOLE), oio_url_get(url, OIOURL_HEXID), srvtype);

	if (!srvtype)
		reply->send_error(CODE_BAD_REQUEST, NEWERROR(CODE_BAD_REQUEST, "Missing srvtype"));
	else {
		gsize length = 0;
		void *body = metautils_message_get_BODY(reply->request, &length);
		gchar **urlv = NULL;
		GError *err = STRV_decode_buffer(body, length, &urlv);
		if (NULL != err)
			reply->send_error(CODE_BAD_REQUEST, err);
		else {
			err = meta1_backend_services_unlink(m1, url, srvtype, urlv);
			if (NULL != err)
				reply->send_error(0, err);
			else
				reply->send_reply(CODE_FINAL_OK, "OK");
			g_strfreev (urlv);
		}
	}

	oio_url_clean (url);
	g_free0 (srvtype);
	return TRUE;
}
static gboolean
_cb_exec_and_concat (GByteArray *tmp, MESSAGE reply)
{
	gsize bsize = 0;
	void *b = metautils_message_get_BODY(reply, &bsize);
	if (b && bsize)
		g_byte_array_append(tmp, b, bsize);
	return TRUE;
}
GError *
metautils_message_extract_body_strv(MESSAGE msg, gchar ***result)
{
	gsize bsize = 0;
	void *b = metautils_message_get_BODY(msg, &bsize);
	if (!b) {
		*result = g_malloc0(sizeof(gchar*));
		return NULL;
	}

	*result = metautils_decode_lines(b, ((gchar*)b)+bsize);
	return NULL;
}
GError *
metautils_message_extract_body_gba(MESSAGE msg, GByteArray **result)
{
	EXTRA_ASSERT(result != NULL);

	gsize bsize = 0;
	*result = NULL;
	void *b = metautils_message_get_BODY(msg, &bsize);
	if (!b)
		return NEWERROR(CODE_BAD_REQUEST, "No body");

	*result = g_byte_array_new();
	if (bsize > 0)
		g_byte_array_append(*result, b, bsize);
	return NULL;
}
int
meta2_filter_action_create_container(struct gridd_filter_ctx_s *ctx,
		struct gridd_reply_ctx_s *reply)
{
	struct m2v2_create_params_s params = {NULL,NULL,NULL,0};
	struct meta2_backend_s *m2b = meta2_filter_ctx_get_backend(ctx);
	struct oio_url_s *url = meta2_filter_ctx_get_url(ctx);
	GError *err = NULL;
	int retry = 2;

	params.storage_policy = meta2_filter_ctx_get_param(ctx, NAME_MSGKEY_STGPOLICY);
	params.version_policy = meta2_filter_ctx_get_param(ctx, NAME_MSGKEY_VERPOLICY);
	params.local = (meta2_filter_ctx_get_param(ctx, NAME_MSGKEY_LOCAL) != NULL);

	gsize len = 0;
	void *buf = metautils_message_get_BODY(reply->request, &len);
	err = KV_decode_buffer(buf, len, &params.properties);
	if (err) {
		meta2_filter_ctx_set_error(ctx, err);
		return FILTER_KO;
	}

	while (!err) {
		if (!(err = meta2_backend_create_container(m2b, url, &params)))
			break;
		if (err != NULL && err->code == CODE_REDIRECT && retry-- > 0 &&
			!g_strcmp0(err->message, meta2_backend_get_local_addr(m2b))) {
			GRID_WARN(
					"Redirecting on myself!?! Retrying request immediately");
			g_clear_error(&err);
			hc_decache_reference_service(m2b->resolver, url,
										 NAME_SRVTYPE_META2);
		}
	}

	g_strfreev(params.properties);
	params.properties = NULL;
	if (!err)
		return FILTER_OK;
	meta2_filter_ctx_set_error(ctx, err);
	return FILTER_KO;
}
Beispiel #10
0
static gboolean
_on_reply(gpointer u, MESSAGE reply)
{
	(void) u;
	size_t bsize = 0;
	void *b = metautils_message_get_BODY(reply, &bsize);
	if (!b || !bsize)
		return TRUE;

	asn_codec_ctx_t ctx;
	memset(&ctx, 0, sizeof(ctx));
	ctx.max_stack_size = ASN1C_MAX_STACK;

	struct TableSequence *ts = NULL;
	asn_dec_rval_t rv = ber_decode(&ctx, &asn_DEF_TableSequence, (void**)&ts, b, bsize);
	if (rv.code != RC_OK) {
		g_printerr("Invalid reply from SQLX: bad body, decoding error\n");
		return FALSE;
	}

	if (flag_json) {
		g_print("{ \"result\" : [\n");
		for (int i=0; i < ts->list.count ; i++)
			_dump_table_json(ts->list.array[i]);
		g_print(" ]\n}");
	}
	else if (flag_xml) {
		g_print("<result>\n");
		for (int i=0; i < ts->list.count ; i++)
			_dump_table_xml(ts->list.array[i]);
		g_print("</result>");
	}
	else {
		for (int i=0; i < ts->list.count ; i++)
			_dump_table_text(ts->list.array[i]);
	}

	if (ts)
		asn_DEF_TableSequence.free_struct(&asn_DEF_TableSequence, ts, FALSE);
	return TRUE;
}
GError *
metautils_message_extract_body_string(MESSAGE msg, gchar **result)
{
	gsize bsize = 0;
	void *b = metautils_message_get_BODY(msg, &bsize);
	if (!b)
		return NEWERROR(CODE_BAD_REQUEST, "No body");

	if (!b || !bsize) {
		*result = g_malloc0(sizeof(void*));
		return NULL;
	}

	register gchar *c, *last;
	for (c=b,last=b+bsize; c < last ;c++) {
		if (!g_ascii_isprint(*c))
			return NEWERROR(CODE_BAD_REQUEST,
					"Body contains non printable characters");
	}

	*result = g_strndup((gchar*)b, bsize);
	return NULL;
}
gboolean
metautils_message_has_BODY (MESSAGE m)
{ return NULL != metautils_message_get_BODY(m,NULL); }