diff options
author | Taylor Yu <catalyst@torproject.org> | 2019-07-03 23:48:17 -0500 |
---|---|---|
committer | Taylor Yu <catalyst@torproject.org> | 2019-12-08 22:40:00 -0600 |
commit | c744d23c8dce73ded9146661001d910295f9bcbd (patch) | |
tree | d29ef15a918cb40bc6063961c8c9b3272b3e7f90 /src/feature/control/control_cmd.c | |
parent | 1a68a18093d38f9f5b7ed66c8d42e41a565febe0 (diff) | |
download | tor-c744d23c8dce73ded9146661001d910295f9bcbd.tar.gz tor-c744d23c8dce73ded9146661001d910295f9bcbd.zip |
simplify getconf by using reply lines
In handle_control_getconf(), use the new control reply line
abstraction to simplify output generation. Previously, this function
explicitly checked for whether it should generate a MidReplyLine or an
EndReplyLine. control_write_reply_lines() now abstracts this check.
Part of #30984.
Diffstat (limited to 'src/feature/control/control_cmd.c')
-rw-r--r-- | src/feature/control/control_cmd.c | 33 |
1 files changed, 9 insertions, 24 deletions
diff --git a/src/feature/control/control_cmd.c b/src/feature/control/control_cmd.c index 656ddf5ca1..d4fb17847d 100644 --- a/src/feature/control/control_cmd.c +++ b/src/feature/control/control_cmd.c @@ -289,26 +289,23 @@ handle_control_getconf(control_connection_t *conn, const smartlist_t *questions = args->args; smartlist_t *answers = smartlist_new(); smartlist_t *unrecognized = smartlist_new(); - char *msg = NULL; - size_t msg_len; const or_options_t *options = get_options(); - int i, len; SMARTLIST_FOREACH_BEGIN(questions, const char *, q) { if (!option_is_recognized(q)) { - smartlist_add(unrecognized, (char*) q); + control_reply_add_printf(unrecognized, 552, + "Unrecognized configuration key \"%s\"", q); } else { config_line_t *answer = option_get_assignment(options,q); if (!answer) { const char *name = option_get_canonical_name(q); - smartlist_add_asprintf(answers, "250-%s\r\n", name); + control_reply_add_1kv(answers, 250, KV_OMIT_VALS, name, ""); } while (answer) { config_line_t *next; - smartlist_add_asprintf(answers, "250-%s=%s\r\n", - answer->key, answer->value); - + control_reply_add_1kv(answers, 250, KV_RAW, answer->key, + answer->value); next = answer->next; tor_free(answer->key); tor_free(answer->value); @@ -318,20 +315,10 @@ handle_control_getconf(control_connection_t *conn, } } SMARTLIST_FOREACH_END(q); - if ((len = smartlist_len(unrecognized))) { - for (i=0; i < len-1; ++i) - control_printf_midreply(conn, 552, - "Unrecognized configuration key \"%s\"", - (char*)smartlist_get(unrecognized, i)); - control_printf_endreply(conn, 552, - "Unrecognized configuration key \"%s\"", - (char*)smartlist_get(unrecognized, len-1)); - } else if ((len = smartlist_len(answers))) { - char *tmp = smartlist_get(answers, len-1); - tor_assert(strlen(tmp)>4); - tmp[3] = ' '; - msg = smartlist_join_strings(answers, "", 0, &msg_len); - connection_buf_add(msg, msg_len, TO_CONN(conn)); + if (smartlist_len(unrecognized)) { + control_write_reply_lines(conn, unrecognized); + } else if (smartlist_len(answers)) { + control_write_reply_lines(conn, answers); } else { send_control_done(conn); } @@ -340,8 +327,6 @@ handle_control_getconf(control_connection_t *conn, smartlist_free(answers); smartlist_free(unrecognized); - tor_free(msg); - return 0; } |