+/**
+ * Escapes a string so it can be used as a JSON string value.
+ * Does not support unicode specifiers in the form of \uXXXX.
+ * @param buf The string to be escaped
+ * @return The escaped string (return value == buf)
+ */
+char *FCGI_EscapeJSON(char *buf)
+{
+ int length, i;
+ length = strlen(buf);
+
+ //Escape special characters. Must count down to escape properly
+ for (i = length - 1; i >= 0; i--) {
+ if (buf[i] < 0x20) { //Control characters
+ buf[i] = ' ';
+ } else if (buf[i] == '"') {
+ if (i-1 >= 0 && buf[i-1] == '\\')
+ i--;
+ else
+ buf[i] = '\'';
+ } else if (buf[i] == '\\') {
+ if (i-1 >= 0 && buf[i-1] == '\'')
+ i--;
+ else
+ buf[i] = ' ';
+ }
+ }
+ return buf;
+}
+
+/**
+ * To be used when the input parameters are rejected. The return data
+ * will also have debugging information provided.
+ * @param context The context to work in
+ * @param status The status the return data should have.
+ * @param description A short description of why the input was rejected.
+ */
+void FCGI_RejectJSONEx(FCGIContext *context, StatusCodes status, const char *description)
+{
+ if (description == NULL)
+ description = "Unknown";
+
+ Log(LOGINFO, "%s: Rejected query with: %d: %s", context->current_module, status, description);
+ FCGI_BeginJSON(context, status);
+ FCGI_JSONPair("description", description);
+ FCGI_JSONLong("responsenumber", context->response_number);
+ FCGI_JSONPair("params", getenv("QUERY_STRING"));
+ FCGI_JSONPair("host", getenv("SERVER_HOSTNAME"));
+ FCGI_JSONPair("user", getenv("REMOTE_USER"));
+ FCGI_JSONPair("ip", getenv("REMOTE_ADDR"));
+ FCGI_EndJSON();
+}
+
+/**
+ * Generates a response to the client as described by the format parameter and
+ * extra arguments (exactly like printf). To be used when none of the other
+ * predefined functions will work exactly as needed. Extra care should be taken
+ * to ensure the correctness of the output.
+ * @param format The format string
+ * @param ... Any extra arguments as required by the format string.
+ */
+void FCGI_PrintRaw(const char *format, ...)
+{
+ va_list list;
+ va_start(list, format);
+ vprintf(format, list);
+ va_end(list);
+}
+
+
+/**
+ * Write binary data
+ * See fwrite
+ */
+void FCGI_WriteBinary(void * data, size_t size, size_t num_elem)
+{
+ Log(LOGDEBUG,"Writing!");
+ FCGI_fwrite(data, size, num_elem, FCGI_stdout);
+}
+