static void soup_msg_cb()

in chime/chime-connection.c [763:815]


static void soup_msg_cb(SoupSession *soup_sess, SoupMessage *msg, gpointer _cmsg)
{
	struct chime_msg *cmsg = _cmsg;
	ChimeConnection *cxn = cmsg->cxn;
	ChimeConnectionPrivate *priv = CHIME_CONNECTION_GET_PRIVATE (cxn);
	JsonParser *parser = NULL;
	JsonNode *node = NULL;

	if (priv->msgs_queued)
		g_queue_remove(priv->msgs_queued, cmsg);

	/* Special case for renew_cb itself, which mustn't recurse! */
	if (priv->state != CHIME_STATE_DISCONNECTED &&
	    cmsg->cb != renew_cb && cmsg->cb != register_cb &&
	    (msg->status_code == 401 /*||
	     (msg->status_code == 7 && !g_queue_is_empty(priv->msgs_pending_auth))*/)) {
		g_object_ref(msg);
		gboolean already_renewing = !g_queue_is_empty(priv->msgs_pending_auth);
		g_queue_push_tail(priv->msgs_pending_auth, cmsg);
		if (!already_renewing) {
#if 0 /* Not working; we can catch statue_code==7 above but it's also breaking
	 the websocket connection too. */
			while (!g_queue_is_empty(priv->msgs_queued)) {
				cmsg = g_queue_pop_head(priv->msgs_queued);
				soup_session_cancel_message(priv->soup_sess, cmsg->msg, 401);
				// They should requeue themselves
			}
#endif
			chime_renew_token(cxn);
		}
		g_object_unref(cxn);
		return;
	}

	const gchar *content_type = soup_message_headers_get_content_type(msg->response_headers, NULL);
	if (!g_strcmp0(content_type, "application/json") && msg->response_body->data) {
		GError *error = NULL;

		parser = json_parser_new();
		if (!json_parser_load_from_data(parser, msg->response_body->data, msg->response_body->length, &error)) {
			g_warning("Error loading data: %s", error->message);
			g_error_free(error);
		} else {
			node = json_parser_get_root(parser);
		}
	}

	if (cmsg->cb)
		cmsg->cb(cmsg->cxn, msg, node, cmsg->cb_data);
	g_clear_object(&parser);
	g_free(cmsg);
	g_object_unref(cxn);
}