@@ -18,6 +18,8 @@ static dictionary *config;
18
18
static int readonly = 0 ;
19
19
static int cycle_log = 0 ;
20
20
21
+ #define _min (a , b ) (((a) < (b)) ? (a) : (b))
22
+
21
23
static const char * get_prefix (const char * path ) {
22
24
const char * result = strrchr (path , '/' );
23
25
return result ? result + 1 : 0 ;
@@ -124,26 +126,33 @@ static int list_keys(FCGX_Request *req, db_table *pl, const char *key) {
124
126
int total = 0 , result ;
125
127
char body [4096 ];
126
128
char * b = body ;
127
- size_t len = sizeof (body );
129
+ size_t len = sizeof (body )- 1 ;
128
130
129
131
do {
130
132
result = cb_find_prefix (& pl -> trie , key , strlen (key ), matches , BATCH , total );
131
133
if (result > 1 ) {
132
134
int i ;
133
135
for (i = 0 ; i != result ; ++ i ) {
134
- const char * k , * v ;
135
136
size_t bytes ;
136
137
db_entry entry ;
137
138
cb_get_kv (matches [i ], & entry , sizeof (db_entry ));
138
- k = (const char * )matches [i ];
139
- v = (const char * )entry .data ;
140
- bytes = snprintf (b , len , "%s: %s\n" , k , v );
139
+ bytes = snprintf (b , len , "%s: " , (const char * )matches [i ]);
140
+ len -= bytes ;
141
+ b += bytes ;
142
+ bytes = _min (len , entry .size );
143
+ memcpy (b , entry .data , bytes );
141
144
len -= bytes ;
142
145
b += bytes ;
146
+ if (len > 0 ) {
147
+ b [0 ] = '\n' ;
148
+ ++ b ;
149
+ -- len ;
150
+ }
143
151
}
144
152
total += result ;
145
153
}
146
154
} while (result == BATCH );
155
+ b [0 ]= 0 ;
147
156
printf ("found %d matches for prefix %s\n" , total , key );
148
157
if (total > 0 ) {
149
158
http_response (req -> out , 200 , "OK" , body , strlen (body ));
0 commit comments