aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/lib
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2016-12-09 09:40:11 -0800
committerGravatar Craig Tiller <ctiller@google.com>2016-12-09 09:40:11 -0800
commitb4aa70ea94687f41c731e57f9782c51c5dacf779 (patch)
tree618a24561fe188b69d36619c43e14f1831bc9014 /src/core/lib
parent75f8013ba65f5008660c8483800f325d0c1b80a9 (diff)
Offer grpc_slice_to_c_string to simplify some code
Diffstat (limited to 'src/core/lib')
-rw-r--r--src/core/lib/channel/compress_filter.c4
-rw-r--r--src/core/lib/security/credentials/plugin/plugin_credentials.c2
-rw-r--r--src/core/lib/security/transport/client_auth_filter.c10
-rw-r--r--src/core/lib/slice/slice.c7
-rw-r--r--src/core/lib/surface/call.c6
-rw-r--r--src/core/lib/surface/call_log_batch.c2
-rw-r--r--src/core/lib/transport/metadata.c28
-rw-r--r--src/core/lib/transport/metadata_batch.c4
-rw-r--r--src/core/lib/transport/service_config.c2
-rw-r--r--src/core/lib/transport/transport.c4
10 files changed, 38 insertions, 31 deletions
diff --git a/src/core/lib/channel/compress_filter.c b/src/core/lib/channel/compress_filter.c
index 330688c29c..3347b5facb 100644
--- a/src/core/lib/channel/compress_filter.c
+++ b/src/core/lib/channel/compress_filter.c
@@ -115,7 +115,7 @@ static grpc_error *process_send_initial_metadata(
initial_metadata->idx.named.grpc_internal_encoding_request->md;
if (!grpc_compression_algorithm_parse(GRPC_MDVALUE(md),
&calld->compression_algorithm)) {
- char *val = grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_ASCII);
+ char *val = grpc_slice_to_c_string(GRPC_MDVALUE(md));
gpr_log(GPR_ERROR,
"Invalid compression algorithm: '%s' (unknown). Ignoring.", val);
gpr_free(val);
@@ -123,7 +123,7 @@ static grpc_error *process_send_initial_metadata(
}
if (!GPR_BITGET(channeld->enabled_algorithms_bitset,
calld->compression_algorithm)) {
- char *val = grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_ASCII);
+ char *val = grpc_slice_to_c_string(GRPC_MDVALUE(md));
gpr_log(GPR_ERROR,
"Invalid compression algorithm: '%s' (previously disabled). "
"Ignoring.",
diff --git a/src/core/lib/security/credentials/plugin/plugin_credentials.c b/src/core/lib/security/credentials/plugin/plugin_credentials.c
index 66e501cffc..f98c9cd8ef 100644
--- a/src/core/lib/security/credentials/plugin/plugin_credentials.c
+++ b/src/core/lib/security/credentials/plugin/plugin_credentials.c
@@ -79,7 +79,7 @@ static void plugin_md_request_metadata_ready(void *request,
grpc_credentials_md *md_array = NULL;
for (i = 0; i < num_md; i++) {
if (!grpc_header_key_is_legal(md[i].key)) {
- char *key = grpc_dump_slice(md[i].key, GPR_DUMP_ASCII);
+ char *key = grpc_slice_to_c_string(md[i].key);
gpr_log(GPR_ERROR, "Plugin added invalid metadata key: %s", key);
gpr_free(key);
seen_illegal_header = true;
diff --git a/src/core/lib/security/transport/client_auth_filter.c b/src/core/lib/security/transport/client_auth_filter.c
index d8540b9625..8852412d61 100644
--- a/src/core/lib/security/transport/client_auth_filter.c
+++ b/src/core/lib/security/transport/client_auth_filter.c
@@ -134,7 +134,7 @@ static void on_credentials_metadata(grpc_exec_ctx *exec_ctx, void *user_data,
grpc_error *error = GRPC_ERROR_NONE;
for (i = 0; i < num_md; i++) {
if (!grpc_header_key_is_legal(md_elems[i].key)) {
- char *str = grpc_dump_slice(md_elems[i].key, GPR_DUMP_ASCII);
+ char *str = grpc_slice_to_c_string(md_elems[i].key);
gpr_log(GPR_ERROR, "attempt to send invalid metadata key: %s", str);
gpr_free(str);
} else if (!grpc_is_binary_header(md_elems[i].key) &&
@@ -162,7 +162,7 @@ static void on_credentials_metadata(grpc_exec_ctx *exec_ctx, void *user_data,
void build_auth_metadata_context(grpc_security_connector *sc,
grpc_auth_context *auth_context,
call_data *calld) {
- char *service = grpc_dump_slice(calld->method, GPR_DUMP_ASCII);
+ char *service = grpc_slice_to_c_string(calld->method);
char *last_slash = strrchr(service, '/');
char *method_name = NULL;
char *service_url = NULL;
@@ -178,7 +178,7 @@ void build_auth_metadata_context(grpc_security_connector *sc,
method_name = gpr_strdup(last_slash + 1);
}
if (method_name == NULL) method_name = gpr_strdup("");
- char *host = grpc_dump_slice(calld->host, GPR_DUMP_ASCII);
+ char *host = grpc_slice_to_c_string(calld->host);
gpr_asprintf(&service_url, "%s://%s%s",
sc->url_scheme == NULL ? "" : sc->url_scheme, host, service);
calld->auth_md_context.service_url = service_url;
@@ -237,7 +237,7 @@ static void on_host_checked(grpc_exec_ctx *exec_ctx, void *user_data,
send_security_metadata(exec_ctx, elem, &calld->op);
} else {
char *error_msg;
- char *host = grpc_dump_slice(calld->host, GPR_DUMP_ASCII);
+ char *host = grpc_slice_to_c_string(calld->host);
gpr_asprintf(&error_msg, "Invalid host %s set in :authority metadata.",
host);
gpr_free(host);
@@ -297,7 +297,7 @@ static void auth_start_transport_op(grpc_exec_ctx *exec_ctx,
}
}
if (calld->have_host) {
- char *call_host = grpc_dump_slice(calld->host, GPR_DUMP_ASCII);
+ char *call_host = grpc_slice_to_c_string(calld->host);
calld->op = *op; /* Copy op (originates from the caller's stack). */
grpc_channel_security_connector_check_call_host(
exec_ctx, chand->security_connector, call_host, chand->auth_context,
diff --git a/src/core/lib/slice/slice.c b/src/core/lib/slice/slice.c
index e77be6fc14..1cddf062cd 100644
--- a/src/core/lib/slice/slice.c
+++ b/src/core/lib/slice/slice.c
@@ -41,6 +41,13 @@
#include "src/core/lib/iomgr/exec_ctx.h"
+char *grpc_slice_to_c_string(grpc_slice slice) {
+ char *out = gpr_malloc(GRPC_SLICE_LENGTH(slice) + 1);
+ memcpy(out, GRPC_SLICE_START_PTR(slice), GRPC_SLICE_LENGTH(slice));
+ out[GRPC_SLICE_LENGTH(slice)] = 0;
+ return out;
+}
+
grpc_slice grpc_empty_slice(void) {
grpc_slice out;
out.refcount = NULL;
diff --git a/src/core/lib/surface/call.c b/src/core/lib/surface/call.c
index 439d17b3db..366dd98279 100644
--- a/src/core/lib/surface/call.c
+++ b/src/core/lib/surface/call.c
@@ -532,7 +532,7 @@ static void set_encodings_accepted_by_peer(grpc_exec_ctx *exec_ctx,
GPR_BITSET(&call->encodings_accepted_by_peer, algorithm);
} else {
char *accept_encoding_entry_str =
- grpc_dump_slice(accept_encoding_entry_slice, GPR_DUMP_ASCII);
+ grpc_slice_to_c_string(accept_encoding_entry_slice);
gpr_log(GPR_ERROR,
"Invalid entry in accept encoding metadata: '%s'. Ignoring.",
accept_encoding_entry_str);
@@ -599,7 +599,7 @@ static int prepare_application_metadata(
grpc_linked_mdelem *l = (grpc_linked_mdelem *)&md->internal_data;
GPR_ASSERT(sizeof(grpc_linked_mdelem) == sizeof(md->internal_data));
if (!grpc_header_key_is_legal(md->key)) {
- char *str = grpc_dump_slice(md->key, GPR_DUMP_ASCII);
+ char *str = grpc_slice_to_c_string(md->key);
gpr_log(GPR_ERROR, "attempt to send invalid metadata key: %s", str);
gpr_free(str);
break;
@@ -860,7 +860,7 @@ static grpc_compression_algorithm decode_compression(grpc_mdelem md) {
grpc_compression_algorithm algorithm =
grpc_compression_algorithm_from_slice(GRPC_MDVALUE(md));
if (algorithm == GRPC_COMPRESS_ALGORITHMS_COUNT) {
- char *md_c_str = grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_ASCII);
+ char *md_c_str = grpc_slice_to_c_string(GRPC_MDVALUE(md));
gpr_log(GPR_ERROR,
"Invalid incoming compression algorithm: '%s'. Interpreting "
"incoming data as uncompressed.",
diff --git a/src/core/lib/surface/call_log_batch.c b/src/core/lib/surface/call_log_batch.c
index 169e967341..7fc58e19c2 100644
--- a/src/core/lib/surface/call_log_batch.c
+++ b/src/core/lib/surface/call_log_batch.c
@@ -46,7 +46,7 @@ static void add_metadata(gpr_strvec *b, const grpc_metadata *md, size_t count) {
}
for (i = 0; i < count; i++) {
gpr_strvec_add(b, gpr_strdup("\nkey="));
- gpr_strvec_add(b, grpc_dump_slice(md[i].key, GPR_DUMP_ASCII));
+ gpr_strvec_add(b, grpc_slice_to_c_string(md[i].key));
gpr_strvec_add(b, gpr_strdup(" value="));
gpr_strvec_add(b,
diff --git a/src/core/lib/transport/metadata.c b/src/core/lib/transport/metadata.c
index 06e703c9d9..489c20cbc8 100644
--- a/src/core/lib/transport/metadata.c
+++ b/src/core/lib/transport/metadata.c
@@ -161,8 +161,8 @@ static int is_mdelem_static(grpc_mdelem e) {
static void ref_md_locked(mdtab_shard *shard,
interned_metadata *md DEBUG_ARGS) {
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- char *key_str = grpc_dump_slice(md->key, GPR_DUMP_ASCII);
- char *value_str = grpc_dump_slice(md->value, GPR_DUMP_ASCII);
+ char *key_str = grpc_slice_to_c_string(md->key);
+ char *value_str = grpc_slice_to_c_string(md->value);
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
"ELM REF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
gpr_atm_no_barrier_load(&md->refcnt),
@@ -261,8 +261,8 @@ grpc_mdelem grpc_mdelem_create(
allocated->value = grpc_slice_ref_internal(value);
gpr_atm_rel_store(&allocated->refcnt, 1);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- char *key_str = grpc_dump_slice(allocated->key, GPR_DUMP_ASCII);
- char *value_str = grpc_dump_slice(allocated->value, GPR_DUMP_ASCII);
+ char *key_str = grpc_slice_to_c_string(allocated->key);
+ char *value_str = grpc_slice_to_c_string(allocated->value);
gpr_log(GPR_DEBUG, "ELM ALLOC:%p:%zu: '%s' = '%s'", (void *)allocated,
gpr_atm_no_barrier_load(&allocated->refcnt), key_str, value_str);
gpr_free(key_str);
@@ -312,8 +312,8 @@ grpc_mdelem grpc_mdelem_create(
shard->elems[idx] = md;
gpr_mu_init(&md->mu_user_data);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- char *key_str = grpc_dump_slice(md->key, GPR_DUMP_ASCII);
- char *value_str = grpc_dump_slice(md->value, GPR_DUMP_ASCII);
+ char *key_str = grpc_slice_to_c_string(md->key);
+ char *value_str = grpc_slice_to_c_string(md->value);
gpr_log(GPR_DEBUG, "ELM NEW:%p:%zu: '%s' = '%s'", (void *)md,
gpr_atm_no_barrier_load(&md->refcnt), key_str, value_str);
gpr_free(key_str);
@@ -374,8 +374,8 @@ grpc_mdelem grpc_mdelem_ref(grpc_mdelem gmd DEBUG_ARGS) {
case GRPC_MDELEM_STORAGE_INTERNED: {
interned_metadata *md = (interned_metadata *)GRPC_MDELEM_DATA(gmd);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- char *key_str = grpc_dump_slice(md->key, GPR_DUMP_ASCII);
- char *value_str = grpc_dump_slice(md->value, GPR_DUMP_ASCII);
+ char *key_str = grpc_slice_to_c_string(md->key);
+ char *value_str = grpc_slice_to_c_string(md->value);
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
"ELM REF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
gpr_atm_no_barrier_load(&md->refcnt),
@@ -394,8 +394,8 @@ grpc_mdelem grpc_mdelem_ref(grpc_mdelem gmd DEBUG_ARGS) {
case GRPC_MDELEM_STORAGE_ALLOCATED: {
allocated_metadata *md = (allocated_metadata *)GRPC_MDELEM_DATA(gmd);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- char *key_str = grpc_dump_slice(md->key, GPR_DUMP_ASCII);
- char *value_str = grpc_dump_slice(md->value, GPR_DUMP_ASCII);
+ char *key_str = grpc_slice_to_c_string(md->key);
+ char *value_str = grpc_slice_to_c_string(md->value);
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
"ELM REF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
gpr_atm_no_barrier_load(&md->refcnt),
@@ -422,8 +422,8 @@ void grpc_mdelem_unref(grpc_exec_ctx *exec_ctx, grpc_mdelem gmd DEBUG_ARGS) {
case GRPC_MDELEM_STORAGE_INTERNED: {
interned_metadata *md = (interned_metadata *)GRPC_MDELEM_DATA(gmd);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- char *key_str = grpc_dump_slice(md->key, GPR_DUMP_ASCII);
- char *value_str = grpc_dump_slice(md->value, GPR_DUMP_ASCII);
+ char *key_str = grpc_slice_to_c_string(md->key);
+ char *value_str = grpc_slice_to_c_string(md->value);
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
"ELM UNREF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
gpr_atm_no_barrier_load(&md->refcnt),
@@ -446,8 +446,8 @@ void grpc_mdelem_unref(grpc_exec_ctx *exec_ctx, grpc_mdelem gmd DEBUG_ARGS) {
case GRPC_MDELEM_STORAGE_ALLOCATED: {
allocated_metadata *md = (allocated_metadata *)GRPC_MDELEM_DATA(gmd);
#ifdef GRPC_METADATA_REFCOUNT_DEBUG
- char *key_str = grpc_dump_slice(md->key, GPR_DUMP_ASCII);
- char *value_str = grpc_dump_slice(md->value, GPR_DUMP_ASCII);
+ char *key_str = grpc_slice_to_c_string(md->key);
+ char *value_str = grpc_slice_to_c_string(md->value);
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG,
"ELM UNREF:%p:%zu->%zu: '%s' = '%s'", (void *)md,
gpr_atm_no_barrier_load(&md->refcnt),
diff --git a/src/core/lib/transport/metadata_batch.c b/src/core/lib/transport/metadata_batch.c
index 07b0dd3725..18b0f161ab 100644
--- a/src/core/lib/transport/metadata_batch.c
+++ b/src/core/lib/transport/metadata_batch.c
@@ -99,8 +99,8 @@ void grpc_metadata_batch_destroy(grpc_exec_ctx *exec_ctx,
}
grpc_error *grpc_attach_md_to_error(grpc_error *src, grpc_mdelem md) {
- char *k = grpc_dump_slice(GRPC_MDKEY(md), GPR_DUMP_ASCII);
- char *v = grpc_dump_slice(GRPC_MDVALUE(md), GPR_DUMP_ASCII);
+ char *k = grpc_slice_to_c_string(GRPC_MDKEY(md));
+ char *v = grpc_slice_to_c_string(GRPC_MDVALUE(md));
grpc_error *out = grpc_error_set_str(
grpc_error_set_str(src, GRPC_ERROR_STR_KEY, k), GRPC_ERROR_STR_VALUE, v);
gpr_free(k);
diff --git a/src/core/lib/transport/service_config.c b/src/core/lib/transport/service_config.c
index 8073a46105..12da2a88fe 100644
--- a/src/core/lib/transport/service_config.c
+++ b/src/core/lib/transport/service_config.c
@@ -237,7 +237,7 @@ void* grpc_method_config_table_get(grpc_exec_ctx* exec_ctx,
// If we didn't find a match for the path, try looking for a wildcard
// entry (i.e., change "/service/method" to "/service/*").
if (value == NULL) {
- char* path_str = grpc_dump_slice(path, GPR_DUMP_ASCII);
+ char* path_str = grpc_slice_to_c_string(path);
const char* sep = strrchr(path_str, '/') + 1;
const size_t len = (size_t)(sep - path_str);
char* buf = gpr_malloc(len + 2); // '*' and NUL
diff --git a/src/core/lib/transport/transport.c b/src/core/lib/transport/transport.c
index 9bc278c133..9ccd7daf45 100644
--- a/src/core/lib/transport/transport.c
+++ b/src/core/lib/transport/transport.c
@@ -224,7 +224,7 @@ void grpc_transport_stream_op_add_cancellation_with_message(
}
grpc_error *error;
if (optional_message != NULL) {
- char *msg = grpc_dump_slice(*optional_message, GPR_DUMP_ASCII);
+ char *msg = grpc_slice_to_c_string(*optional_message);
error = grpc_error_set_str(GRPC_ERROR_CREATE(msg),
GRPC_ERROR_STR_GRPC_MESSAGE, msg);
gpr_free(msg);
@@ -250,7 +250,7 @@ void grpc_transport_stream_op_add_close(grpc_exec_ctx *exec_ctx,
}
grpc_error *error;
if (optional_message != NULL) {
- char *msg = grpc_dump_slice(*optional_message, GPR_DUMP_ASCII);
+ char *msg = grpc_slice_to_c_string(*optional_message);
error = grpc_error_set_str(GRPC_ERROR_CREATE(msg),
GRPC_ERROR_STR_GRPC_MESSAGE, msg);
gpr_free(msg);