[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] simplify getinfo using reply lines



commit 9b196f15638ea490d1d0892cf320c5afc66a1eef
Author: Taylor Yu <catalyst@xxxxxxxxxxxxxx>
Date:   Thu Aug 22 17:54:33 2019 -0500

    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.
---
 src/feature/control/control_getinfo.c | 45 +++++++++++++----------------------
 1 file changed, 17 insertions(+), 28 deletions(-)

diff --git a/src/feature/control/control_getinfo.c b/src/feature/control/control_getinfo.c
index 979fa4480..20dc4d082 100644
--- a/src/feature/control/control_getinfo.c
+++ b/src/feature/control/control_getinfo.c
@@ -50,6 +50,7 @@
 #include "feature/stats/geoip_stats.h"
 #include "feature/stats/predict_ports.h"
 #include "lib/version/torversion.h"
+#include "lib/encoding/kvline.h"
 
 #include "core/or/entry_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 *unrecognized = smartlist_new();
   char *ans = NULL;
-  int i;
 
   SMARTLIST_FOREACH_BEGIN(questions, const char *, q) {
     const char *errmsg = NULL;
@@ -1644,43 +1644,32 @@ handle_control_getinfo(control_connection_t *conn,
       goto done;
     }
     if (!ans) {
-      if (errmsg) /* use provided error message */
-        smartlist_add_strdup(unrecognized, errmsg);
-      else /* use default error message */
-        smartlist_add_asprintf(unrecognized, "Unrecognized key \"%s\"", q);
+      if (errmsg) {
+        /* use provided error message */
+        control_reply_add_str(unrecognized, 552, errmsg);
+      } else {
+        /* use default error message */
+        control_reply_add_printf(unrecognized, 552,
+                                 "Unrecognized key \"%s\"", q);
+      }
     } else {
-      smartlist_add_strdup(answers, q);
-      smartlist_add(answers, ans);
+      control_reply_add_1kv(answers, 250, KV_RAW, q, ans);
     }
   } SMARTLIST_FOREACH_END(q);
 
-  if (smartlist_len(unrecognized)) {
-    /* 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_reply_add_done(answers);
 
-    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;
   }
 
-  for (i = 0; i < smartlist_len(answers); i += 2) {
-    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);
+  control_write_reply_lines(conn, answers);
 
  done:
-  SMARTLIST_FOREACH(answers, char *, cp, tor_free(cp));
-  smartlist_free(answers);
-  SMARTLIST_FOREACH(unrecognized, char *, cp, tor_free(cp));
-  smartlist_free(unrecognized);
+  control_reply_free(answers);
+  control_reply_free(unrecognized);
 
   return 0;
 }



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits