Revision 88b5da7b transports/janus_http.c

View differences:

transports/janus_http.c
104 104
static gboolean http_janus_api_enabled = FALSE;
105 105
static gboolean http_admin_api_enabled = FALSE;
106 106

  
107
/* JSON serialization options */
108
static size_t json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER;
109

  
107 110

  
108 111
/* Incoming HTTP message */
109 112
typedef struct janus_http_msg {
......
551 554
		janus_config_print(config);
552 555

  
553 556
		/* Handle configuration */
554
		
555
		/* ... starting with the base paths */
556
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "base_path");
557
		janus_config_item *item = janus_config_get_item_drilldown(config, "general", "json");
558
		if(item && item->value) {
559
			/* Check how we need to format/serialize the JSON output */
560
			if(!strcasecmp(item->value, "indented")) {
561
				/* Default: indented, we use three spaces for that */
562
				json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER;
563
			} else if(!strcasecmp(item->value, "plain")) {
564
				/* Not indented and no new lines, but still readable */
565
				json_format = JSON_INDENT(0) | JSON_PRESERVE_ORDER;
566
			} else if(!strcasecmp(item->value, "compact")) {
567
				/* Compact, so no spaces between separators */
568
				json_format = JSON_COMPACT | JSON_PRESERVE_ORDER;
569
			} else {
570
				JANUS_LOG(LOG_WARN, "Unsupported JSON format option '%s', using default (indented)\n", item->value);
571
				json_format = JSON_INDENT(3) | JSON_PRESERVE_ORDER;
572
			}
573
		}
574

  
575
		/* Check the base paths */
576
		item = janus_config_get_item_drilldown(config, "general", "base_path");
557 577
		if(item && item->value) {
558 578
			if(item->value[0] != '/') {
559 579
				JANUS_LOG(LOG_FATAL, "Invalid base path %s (it should start with a /, e.g., /janus\n", item->value);
......
1298 1318
		if(event != NULL) {
1299 1319
			if(max_events == 1) {
1300 1320
				/* Return just this message and leave */
1301
				gchar *event_text = json_dumps(event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1321
				gchar *event_text = json_dumps(event, json_format);
1302 1322
				json_decref(event);
1303 1323
				ret = janus_http_return_success(msg, event_text);
1304 1324
			} else {
......
1314 1334
					events++;
1315 1335
				}
1316 1336
				/* Return the array of messages and leave */
1317
				gchar *list_text = json_dumps(list, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1337
				gchar *list_text = json_dumps(list, json_format);
1318 1338
				json_decref(list);
1319 1339
				ret = janus_http_return_success(msg, list_text);
1320 1340
			}
......
1366 1386
	if(!msg->response) {
1367 1387
		ret = MHD_NO;
1368 1388
	} else {
1369
		char *response_text = json_dumps(msg->response, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1389
		char *response_text = json_dumps(msg->response, json_format);
1370 1390
		json_decref(msg->response);
1371 1391
		msg->response = NULL;
1372 1392
		ret = janus_http_return_success(msg, response_text);
......
1613 1633
	if(!msg->response) {
1614 1634
		ret = MHD_NO;
1615 1635
	} else {
1616
		char *response_text = json_dumps(msg->response, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1636
		char *response_text = json_dumps(msg->response, json_format);
1617 1637
		json_decref(msg->response);
1618 1638
		msg->response = NULL;
1619 1639
		ret = janus_http_return_success(msg, response_text);
......
1740 1760
		}
1741 1761
		/* FIXME Improve the Janus protocol keep-alive mechanism in JavaScript */
1742 1762
	}
1743
	char *payload_text = json_dumps(list ? list : event, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1763
	char *payload_text = json_dumps(list ? list : event, json_format);
1744 1764
	json_decref(list ? list : event);
1745 1765
	/* Finish the request by sending the response */
1746 1766
	JANUS_LOG(LOG_VERB, "We have a message to serve...\n\t%s\n", payload_text);
......
1798 1818
	json_object_set_new(error_data, "code", json_integer(error));
1799 1819
	json_object_set_new(error_data, "reason", json_string(error_string));
1800 1820
	json_object_set_new(reply, "error", error_data);
1801
	gchar *reply_text = json_dumps(reply, JSON_INDENT(3) | JSON_PRESERVE_ORDER);
1821
	gchar *reply_text = json_dumps(reply, json_format);
1802 1822
	json_decref(reply);
1803 1823
	/* Use janus_http_return_error to send the error response */
1804 1824
	return janus_http_return_success(msg, reply_text);

Also available in: Unified diff