RESP mini-server (Redis) — C
Topics: wire-protocol parsing, RESP, in-memory store
Problem
Parse one client command in the RESP wire format, run it against an in-memory store, and write
the RESP reply. (Connection framing is out of scope — see message-framing / line-protocol.)
typedef struct { /* fixed-size key/val store */ } Store;
void store_init(Store *s);
size_t resp_handle(Store *s, const unsigned char *req, size_t reqlen, char *out, size_t outcap);
Clients send a command as an array of bulk strings: *2\r\n$3\r\nGET\r\n$3\r\nfoo\r\n. Replies:
+OK\r\n, $<len>\r\n<v>\r\n (or null $-1\r\n), :<n>\r\n, -ERR <msg>\r\n.
Commands (case-insensitive): PING (→ +PONG, or echo its arg), SET k v, GET k, INCR k
(error on a non-integer value), DEL k... (→ count removed). Unknown/malformed → -ERR.
The fixed-size store and its helpers (store_init/find/set/get/del) are provided; you implement
resp_handle — walk the array (each $<len> bulk string), then dispatch and format the reply.
Run
cc -std=c11 -O2 -Wall tests.c -o /tmp/resp && /tmp/resp
Sign in to submit your solution.