mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-09-21 05:26:20 +02:00
simplify getinfo using reply lines
Simplify handle_control_getinfo() by using the new reply lines abstraction. Previously, this function explicitly checked for whether it should generate a MidReplyLine, a DataReplyLine, or an EndReplyLine. control_write_reply_lines() now abstracts this check. Part of #30984.
This commit is contained in:
parent
a08f43ba04
commit
9b196f1563
@ -50,6 +50,7 @@
|
|||||||
#include "feature/stats/geoip_stats.h"
|
#include "feature/stats/geoip_stats.h"
|
||||||
#include "feature/stats/predict_ports.h"
|
#include "feature/stats/predict_ports.h"
|
||||||
#include "lib/version/torversion.h"
|
#include "lib/version/torversion.h"
|
||||||
|
#include "lib/encoding/kvline.h"
|
||||||
|
|
||||||
#include "core/or/entry_connection_st.h"
|
#include "core/or/entry_connection_st.h"
|
||||||
#include "core/or/or_connection_st.h"
|
#include "core/or/or_connection_st.h"
|
||||||
@ -1632,7 +1633,6 @@ handle_control_getinfo(control_connection_t *conn,
|
|||||||
smartlist_t *answers = smartlist_new();
|
smartlist_t *answers = smartlist_new();
|
||||||
smartlist_t *unrecognized = smartlist_new();
|
smartlist_t *unrecognized = smartlist_new();
|
||||||
char *ans = NULL;
|
char *ans = NULL;
|
||||||
int i;
|
|
||||||
|
|
||||||
SMARTLIST_FOREACH_BEGIN(questions, const char *, q) {
|
SMARTLIST_FOREACH_BEGIN(questions, const char *, q) {
|
||||||
const char *errmsg = NULL;
|
const char *errmsg = NULL;
|
||||||
@ -1644,43 +1644,32 @@ handle_control_getinfo(control_connection_t *conn,
|
|||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
if (!ans) {
|
if (!ans) {
|
||||||
if (errmsg) /* use provided error message */
|
if (errmsg) {
|
||||||
smartlist_add_strdup(unrecognized, errmsg);
|
/* use provided error message */
|
||||||
else /* use default error message */
|
control_reply_add_str(unrecognized, 552, errmsg);
|
||||||
smartlist_add_asprintf(unrecognized, "Unrecognized key \"%s\"", q);
|
} else {
|
||||||
|
/* use default error message */
|
||||||
|
control_reply_add_printf(unrecognized, 552,
|
||||||
|
"Unrecognized key \"%s\"", q);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
smartlist_add_strdup(answers, q);
|
control_reply_add_1kv(answers, 250, KV_RAW, q, ans);
|
||||||
smartlist_add(answers, ans);
|
|
||||||
}
|
}
|
||||||
} SMARTLIST_FOREACH_END(q);
|
} SMARTLIST_FOREACH_END(q);
|
||||||
|
|
||||||
if (smartlist_len(unrecognized)) {
|
control_reply_add_done(answers);
|
||||||
/* control-spec section 2.3, mid-reply '-' or end of reply ' ' */
|
|
||||||
for (i=0; i < smartlist_len(unrecognized)-1; ++i)
|
|
||||||
control_write_midreply(conn, 552,
|
|
||||||
(char *)smartlist_get(unrecognized, i));
|
|
||||||
|
|
||||||
control_write_endreply(conn, 552, (char *)smartlist_get(unrecognized, i));
|
if (smartlist_len(unrecognized)) {
|
||||||
|
control_write_reply_lines(conn, unrecognized);
|
||||||
|
/* If there were any unrecognized queries, don't write real answers */
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 0; i < smartlist_len(answers); i += 2) {
|
control_write_reply_lines(conn, answers);
|
||||||
char *k = smartlist_get(answers, i);
|
|
||||||
char *v = smartlist_get(answers, i+1);
|
|
||||||
if (!strchr(v, '\n') && !strchr(v, '\r')) {
|
|
||||||
control_printf_midreply(conn, 250, "%s=%s", k, v);
|
|
||||||
} else {
|
|
||||||
control_printf_datareply(conn, 250, "%s=", k);
|
|
||||||
control_write_data(conn, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
send_control_done(conn);
|
|
||||||
|
|
||||||
done:
|
done:
|
||||||
SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
|
control_reply_free(answers);
|
||||||
smartlist_free(answers);
|
control_reply_free(unrecognized);
|
||||||
SMARTLIST_FOREACH(unrecognized, char *, cp, tor_free(cp));
|
|
||||||
smartlist_free(unrecognized);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user