summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2015-02-22 10:21:41 +0100
committerPřemysl Janouch <p.janouch@gmail.com>2015-02-22 10:24:42 +0100
commit0f2a66e7b0a00af827c26db0a063c01bec65d0a6 (patch)
tree4c33c84349b354876a74102cccf851590c651396
parent1b53c219e9c0653b775899d3e410e1ce52d4a3bd (diff)
downloadjson-rpc-shell-0f2a66e7b0a00af827c26db0a063c01bec65d0a6.tar.gz
json-rpc-shell-0f2a66e7b0a00af827c26db0a063c01bec65d0a6.tar.xz
json-rpc-shell-0f2a66e7b0a00af827c26db0a063c01bec65d0a6.zip
Expect UTF-8 charset in Content-Type
-rw-r--r--json-rpc-shell.c47
1 files changed, 43 insertions, 4 deletions
diff --git a/json-rpc-shell.c b/json-rpc-shell.c
index 71f99c9..93269e1 100644
--- a/json-rpc-shell.c
+++ b/json-rpc-shell.c
@@ -387,6 +387,48 @@ write_callback (char *ptr, size_t size, size_t nmemb, void *user_data)
goto fail; \
BLOCK_END
+static bool
+try_advance (const char **p, const char *text)
+{
+ size_t len = strlen (text);
+ if (strncmp (*p, text, len))
+ return false;
+
+ *p += len;
+ return true;
+}
+
+static bool
+validate_content_type (const char *type)
+{
+ const char *content_types[] =
+ {
+ "application/json-rpc", // obsolete
+ "application/json"
+ };
+ const char *tails[] =
+ {
+ "; charset=utf-8",
+ "; charset=UTF-8",
+ ""
+ };
+
+ bool found = false;
+ for (size_t i = 0; i < N_ELEMENTS (content_types); i++)
+ if ((found = try_advance (&type, content_types[i])))
+ break;
+ if (!found)
+ return false;
+
+ for (size_t i = 0; i < N_ELEMENTS (tails); i++)
+ if ((found = try_advance (&type, tails[i])))
+ break;
+ if (!found)
+ return false;
+
+ return !*type;
+}
+
static void
make_json_rpc_call (struct app_context *ctx,
const char *method, json_t *id, json_t *params)
@@ -435,13 +477,10 @@ make_json_rpc_call (struct app_context *ctx,
bool success = false;
if (id)
{
- // TODO: maybe also accept "...; charset=UTF-8"
if (!type)
print_warning ("missing `Content-Type' header");
- else if (strcmp (type, "application/json")
- && strcmp (type, "application/json-rpc")) // obsolete
+ else if (!validate_content_type (type))
print_warning ("unexpected `Content-Type' header: %s", type);
-
success = parse_response (ctx, &buf);
}
else