From df91c3370537639701b6f128bb5a9c5354f266d3 Mon Sep 17 00:00:00 2001 From: jcorporation Date: Mon, 18 Jun 2018 20:55:54 +0100 Subject: [PATCH] Implement jsonrpc api --- htdocs/js/mpd.js | 138 +++++++------- src/mpd_client.c | 465 +++++++++++++---------------------------------- src/mpd_client.h | 1 - src/mympd.c | 40 ++-- 4 files changed, 197 insertions(+), 447 deletions(-) diff --git a/htdocs/js/mpd.js b/htdocs/js/mpd.js index 8f66ea2..4349b0a 100644 --- a/htdocs/js/mpd.js +++ b/htdocs/js/mpd.js @@ -120,9 +120,8 @@ app.route=function() { app.prepare(); if (app.current.app == 'Playback') { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_CURRENT_SONG"}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_CURRENT_SONG"})}).done(function( obj ) { songChange(obj.data.title, obj.data.artist, obj.data.album, obj.data.uri, obj.data.currentsongid); }); } @@ -142,34 +141,30 @@ app.route=function() { getQueue(); } else if (app.current.app == 'Browse' && app.current.tab == 'Playlists') { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_PLAYLISTS," + app.current.page + ","+app.current.filter}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_PLAYLISTS","data": { "offset": app.current.page, "filter": app.current.filter}})}).done(function( obj ) { parsePlaylists(obj); doSetFilterLetter('#browsePlaylistsFilter'); }); } else if (app.current.app == 'Browse' && app.current.tab == 'Database' && app.current.view == 'Artist') { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_ARTISTS," + app.current.page + ","+app.current.filter}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_ARTISTS","data": { "offset": app.current.page, "filter": app.current.filter}})}).done(function( obj ) { parseListDBtags(obj); doSetFilterLetter('#BrowseDatabaseFilter'); }); } else if (app.current.app == 'Browse' && app.current.tab == 'Database' && app.current.view == 'Album') { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_ARTISTALBUMS," + app.current.page + ","+app.current.filter + "," + app.current.search}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_ARTISTALBUMS","data": { "offset": app.current.page, "filter": app.current.filter, "albumartist": app.current.search}})}).done(function( obj ) { parseListDBtags(obj); doSetFilterLetter('#BrowseDatabaseFilter'); }); } else if (app.current.app == 'Browse' && app.current.tab == 'Filesystem') { $('#BrowseBreadcrumb').empty().append(''); - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_FILESYSTEM," + app.current.page + ","+(app.current.search ? app.current.search : "/")+","+app.current.filter}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_FILESYSTEM","data": { "offset": app.current.page,"path":(app.current.search ? app.current.search : "/"),"filter": app.current.filter}})}).done(function( obj ) { parseFilesystem(obj); doSetFilterLetter('#BrowseFilesystemFilter'); }); @@ -178,7 +173,7 @@ app.route=function() { if (app.current.search) { add_all_songs.off(); // remove previous binds add_all_songs.on('click', function() { - sendAPI('MPD_API_ADD_TRACK,'+app.current.search); + sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": { "uri": app.current.search}})); }); add_all_songs.removeAttr('disabled').removeClass('disabled'); } else { @@ -219,9 +214,8 @@ app.route=function() { } }); if (app.current.search.length >= 2) { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_SEARCH," + app.current.filter + ","+app.current.page+","+app.current.search}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_SEARCH", "data": { "mpdtag": app.current.filter,"offset":app.current.page,"searchstr":app.current.search}})}).done(function( obj ) { parseSearch(obj); }); } else { @@ -239,14 +233,12 @@ app.route=function() { }; $(document).ready(function(){ - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_SETTINGS"}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_SETTINGS"})}).done(function( obj ) { parseSettings(obj); }); - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_OUTPUTNAMES"}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_OUTPUTNAMES"})}).done(function( obj ) { parseOutputnames(obj); }); @@ -256,7 +248,7 @@ $(document).ready(function(){ volumeBar=$('#volumebar').slider(); volumeBar.slider('setValue',0); volumeBar.slider('on','slideStop', function(value){ - sendAPI("MPD_API_SET_VOLUME,"+value); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_VOLUME","data": {"volume":value}})); }); progressBar=$('#progressbar').slider(); @@ -265,22 +257,20 @@ $(document).ready(function(){ progressBar.slider('on','slideStop', function(value){ if(current_song && current_song.currentSongId >= 0) { var seekVal = Math.ceil(current_song.totalTime*(value/100)); - sendAPI("MPD_API_SET_SEEK,"+current_song.currentSongId+","+seekVal); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_SEEK", "data": {"songid":current_song.currentSongId,"seek":seekVal}})); } }); $('#about').on('shown.bs.modal', function () { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_STATS"}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_STATS"})}).done(function( obj ) { parseStats(obj); }); }) $('#settings').on('shown.bs.modal', function () { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_SETTINGS"}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_SETTINGS"})}).done(function( obj ) { parseSettings(obj); }); }) @@ -511,9 +501,8 @@ function parseState(obj) { //Get current song on queue change for http streams if (last_state == undefined || obj.data.queue_version != last_state.data.queue_version) { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_CURRENT_SONG"}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_CURRENT_SONG"})}).done(function( obj ) { songChange(obj.data.title, obj.data.artist, obj.data.album, obj.data.uri, obj.data.currentsongid); }); } @@ -531,16 +520,14 @@ function parseState(obj) { function getQueue() { if (app.current.search.length >= 2) { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_SEARCH_QUEUE," + app.current.filter + ","+app.current.page+"," + app.current.search}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_SEARCH_QUEUE", "data": {"mpdtag":app.current.filter, "offset":app.current.page,"searchstr": app.current.search}})}).done(function( obj ) { parseQueue(obj); }); } else { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", - data: "MPD_API_GET_QUEUE," + app.current.page}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_QUEUE", "data": {"offset": app.current.page}})}).done(function( obj ) { parseQueue(obj); }); } @@ -622,7 +609,7 @@ function parseQueue(obj) { $('#QueueList > tbody > tr').on({ click: function() { $('#queueList > tbody > tr').removeClass('active'); - sendAPI('MPD_API_PLAY_TRACK,'+$(this).attr('trackid')); + sendAPI(JSON.stringify({"cmd":"MPD_API_PLAY_TRACK","data": {"track":$(this).attr('trackid')}})); $(this).addClass('active'); }, }); @@ -699,7 +686,7 @@ function parseFilesystem(obj) { '' + glyphicon + '') .find('a').click(function(e) { e.stopPropagation(); - sendAPI(onClickAction + "," + decodeURI($(this).parents("tr").attr("uri"))); + sendAPI(JSON.stringify({"cmd":onClickAction,"data":{ "uri":decodeURI($(this).parents("tr").attr("uri"))}})); showNotification('"' + $('td:nth-last-child(3)', $(this).parents('tr')).text() + '" added','','','success'); }); } @@ -730,11 +717,11 @@ function parseFilesystem(obj) { app.goto('Browse','Filesystem',undefined,app.current.page+'/'+app.current.filter+'/'+app.current.search); break; case 'song': - sendAPI("MPD_API_ADD_TRACK," + decodeURI($(this).attr("uri"))); + sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": {"uri": decodeURI($(this).attr("uri"))}})); showNotification('"' + $('td:nth-last-child(5)', this).text() + '" added','','','success'); break; case 'plist': - sendAPI("MPD_API_ADD_PLAYLIST," + decodeURI($(this).attr("uri"))); + sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_PLAYLIST", "data": {"plist": decodeURI($(this).attr("uri"))}})); showNotification('"' + $('td:nth-last-child(3)', this).text() + '" added','','','success'); break; } @@ -792,7 +779,7 @@ function parsePlaylists(obj) { }; $('#'+app.current.app+app.current.tab+'List > tbody > tr').on({ click: function() { - sendAPI('MPD_API_ADD_PLAYLIST,' + decodeURI($(this).attr('uri'))); + sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_PLAYLIST", "data": { "plist": decodeURI($(this).attr('uri'))}})); showNotification('"' + $('td:nth-last-child(3)', this).text() + '" added','','','success'); } }); @@ -856,9 +843,8 @@ function parseListDBtags(obj) { ' '+ ''; $('#BrowseDatabaseAlbumCards').append(card); - $.ajax({url: "/jsonrpc", contentType:"application/json", method: "POST", - data: JSON.stringify({"cmd":"MPD_API_GET_ARTISTALBUMTITLES", "data": { "albumartist": obj.searchstr, "album": obj.data[item].value}})}).done(function( data ) { - var obj = JSON.parse(data); + $.ajax({url: "/api", contentType:"application/json", method: "POST", + data: JSON.stringify({"cmd":"MPD_API_GET_ARTISTALBUMTITLES", "data": { "albumartist": obj.searchstr, "album": obj.data[item].value}})}).done(function( obj ) { parseListTitles(obj); }); } @@ -881,14 +867,14 @@ function parseListTitles(obj) { $('#'+genId(obj.album)+' > img').on({ click: function() { - sendAPI('MPD_API_ADD_TRACK,' + decodeURI($(this).attr('uri'))); + sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": { "track": decodeURI($(this).attr('uri'))}})); showNotification('"'+decodeURI($(this).attr('data-album'))+'" added','','','success'); } }); $('#tbl'+genId(obj.album)+' > tbody > tr').on({ click: function() { - sendAPI('MPD_API_ADD_TRACK,' + decodeURI($(this).attr('uri'))); + sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK", "data": { "track": decodeURI($(this).attr('uri'))}})); showNotification('"' + $('td:nth-last-child(1)', this).text() + '" added','','','success'); } }); @@ -982,24 +968,24 @@ function updatePlayIcon(obj) { } function sendAPI(cmd) { - $.ajax({url: "/api", mimeType: "text/plain", contentType:"text/plain", method: "POST", data: cmd}); + $.ajax({url: "/api", contentType:"application/json", method: "POST", data: cmd}); } function updateDB(event) { - sendAPI('MPD_API_UPDATE_DB'); + sendAPI(JSON.stringify({"cmd":"MPD_API_UPDATE_DB"})); showNotification('Updating MPD Database...','','','success'); event.preventDefault(); } function clickPlay() { if( playstate != 'play') - sendAPI('MPD_API_SET_PLAY'); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_PLAY"})); else - sendAPI('MPD_API_SET_PAUSE'); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_PAUSE"})); } function clickStop() { - sendAPI('MPD_API_SET_STOP'); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_STOP"})); } function setLocalStream(mpdhost,streamport) { @@ -1015,16 +1001,16 @@ function setLocalStream(mpdhost,streamport) { function delQueueSong(tr,event) { event.stopPropagation(); if ( $('#btntrashmodeup').hasClass('btn-success') ) { - sendAPI('MPD_API_RM_RANGE,0,' + (tr.index() + 1)); + sendAPI(JSON.stringify({"cmd":"MPD_API_RM_RANGE", "data": {"start":0, "end": (tr.index() + 1)}})); } else if ( $('#btntrashmodesingle').hasClass('btn-success') ) { - sendAPI('MPD_API_RM_TRACK,' + tr.attr('trackid')); + sendAPI(JSON.stringify({"cmd":"MPD_API_RM_TRACK", "data": { "track": tr.attr('trackid')}})); } else if ( $('#btntrashmodedown').hasClass('btn-success') ) { - sendAPI('MPD_API_RM_RANGE,' + tr.index() + ',-1'); + sendAPI(JSON.stringify({"cmd":"MPD_API_RM_RANGE", "data": {"start": tr.index(), "end":-1}})); }; } function delPlaylist(tr) { - sendAPI("MPD_API_RM_PLAYLIST," + decodeURI(tr.attr("uri"))); + sendAPI(JSON.stringify({"cmd":"MPD_API_RM_PLAYLIST","data": {"plist": decodeURI(tr.attr("uri"))}})); tr.remove(); } @@ -1137,23 +1123,23 @@ function confirmSettings() { } } if (formOK == true) { - sendAPI("MPD_API_TOGGLE_CONSUME," + ($('#btnconsume').hasClass('btn-success') ? 1 : 0)); - sendAPI("MPD_API_TOGGLE_RANDOM," + ($('#btnrandom').hasClass('btn-success') ? 1 : 0)); - sendAPI("MPD_API_TOGGLE_SINGLE," + ($('#btnsingle').hasClass('btn-success') ? 1 : 0)); - sendAPI("MPD_API_TOGGLE_REPEAT," + ($('#btnrepeat').hasClass('btn-success') ? 1 : 0)); - sendAPI("MPD_API_SET_REPLAYGAIN," + $('#selectReplaygain').val()); + sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_CONSUME","data": {"state": ($('#btnconsume').hasClass('btn-success') ? 1 : 0)}})); + sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_RANDOM","data":{"state": ($('#btnrandom').hasClass('btn-success') ? 1 : 0)}})); + sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_SINGLE","data":{"state": ($('#btnsingle').hasClass('btn-success') ? 1 : 0)}})); + sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_REPEAT","data":{"state": ($('#btnrepeat').hasClass('btn-success') ? 1 : 0)}})); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_REPLAYGAIN","data":{"mode": $('#selectReplaygain').val()}})); if (!$('#inputCrossfade').is(':disabled')) - sendAPI("MPD_API_SET_CROSSFADE," + $('#inputCrossfade').val()); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_CROSSFADE","data":{"state": $('#inputCrossfade').val()}})); if (!$('#inputMixrampdb').is(':disabled')) - sendAPI("MPD_API_SET_MIXRAMPDB," + $('#inputMixrampdb').val()); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_MIXRAMPDB","data":{"state": $('#inputMixrampdb').val()}})); if (!$('#inputMixrampdelay').is(':disabled')) - sendAPI("MPD_API_SET_MIXRAMPDELAY," + $('#inputMixrampdelay').val()); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_MIXRAMPDELAY","data":{"state": $('#inputMixrampdelay').val()}})); $('#settings').modal('hide'); } } function toggleoutput(button, id) { - sendAPI("MPD_API_TOGGLE_OUTPUT,"+id+"," + ($(button).hasClass('btn-success') ? 0 : 1)); + sendAPI(JSON.stringify({"cmd":"MPD_API_TOGGLE_OUTPUT", "data": {"output": id, "state": ($(button).hasClass('btn-success') ? 0 : 1)}})); } $('#trashmodebtns > button').on('click', function(e) { @@ -1206,7 +1192,7 @@ $('#search2').submit(function () { function addAllFromSearch() { if (app.current.search.length >= 2) { - sendAPI('MPD_API_SEARCH_ADD,' + app.current.filter + ',' + app.current.search); + sendAPI(JSON.stringify({"cmd":"MPD_API_SEARCH_ADD","data":{"filter": app.current.filter,"searchstr": + app.current.search}})); var rowCount = $('#SearchList >tbody >tr').length; showNotification('Added '+rowCount+' songs from search','','','success'); } @@ -1271,7 +1257,7 @@ function gotoPage(x,element,event) { function addStream() { if($('#streamurl').val().length > 0) { - sendAPI('MPD_API_ADD_TRACK,'+$('#streamurl').val()); + sendAPI(JSON.stringify({"cmd":"MPD_API_ADD_TRACK","data":{"uri":$('#streamurl').val()}})); } $('#streamurl').val(""); $('#addstream').modal('hide'); @@ -1279,7 +1265,7 @@ function addStream() { function saveQueue() { if($('#playlistname').val().length > 0) { - sendAPI('MPD_API_SAVE_QUEUE,'+$('#playlistname').val()); + sendAPI(JSON.stringify({"cmd":"MPD_API_SAVE_QUEUE","data":{"plist":$('#playlistname').val()}})); } $('#savequeue').modal('hide'); } @@ -1361,10 +1347,10 @@ $(document).keydown(function(e){ } switch (e.which) { case 37: //left - sendAPI('MPD_API_SET_PREV'); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_PREV"})); break; case 39: //right - sendAPI('MPD_API_SET_NEXT'); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_NEXT"})); break; case 32: //space clickPlay(); @@ -1416,7 +1402,7 @@ function chVolume (increment) { if (newValue<0) { newValue=0; } else if (newValue > 100) { newValue=100; } volumeBar.slider('setValue',newValue); - sendAPI("MPD_API_SET_VOLUME,"+newValue); + sendAPI(JSON.stringify({"cmd":"MPD_API_SET_VOLUME", "data": {"volume":newValue}})); } function beautifyDuration(x) { diff --git a/src/mpd_client.c b/src/mpd_client.c index 2b329ed..1a25e97 100644 --- a/src/mpd_client.c +++ b/src/mpd_client.c @@ -42,14 +42,6 @@ const char * mpd_cmd_strs[] = { MPD_CMDS(GEN_STR) }; -char * get_arg1 (const char *p) { - return strchr(p, ',') + 1; -} - -char * get_arg2 (const char *p) { - return get_arg1(get_arg1(p)); -} - static inline enum mpd_cmd_ids get_cmd_id(const char *cmd) { for(int i = 0; i < sizeof(mpd_cmd_strs)/sizeof(mpd_cmd_strs[0]); i++) @@ -59,16 +51,14 @@ static inline enum mpd_cmd_ids get_cmd_id(const char *cmd) return -1; } -void callback_mympd_jsonrpc(struct mg_connection *nc, const struct mg_str msg) +void callback_mympd(struct mg_connection *nc, const struct mg_str msg) { size_t n = 0; char *cmd; - int int_buf; - int je; + unsigned int uint_buf1, uint_buf2; + int je, int_buf; float float_buf; - char *p_charbuf1; - char *p_charbuf2; - char *p_charbuf3; + char *p_charbuf1, *p_charbuf2; #ifdef DEBUG fprintf(stdout,"Got request: %s\n",msg.p); @@ -88,48 +78,6 @@ void callback_mympd_jsonrpc(struct mg_connection *nc, const struct mg_str msg) free(p_charbuf1); free(p_charbuf2); break; - } - free(cmd); - - if(mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) - { - #ifdef DEBUG - fprintf(stdout,"Error: %s\n",mpd_connection_get_error_message(mpd.conn)); - #endif - n = snprintf(mpd.buf, MAX_SIZE, "{\"type\":\"error\", \"data\": \"%s\"}", - mpd_connection_get_error_message(mpd.conn)); - - /* Try to recover error */ - if (!mpd_connection_clear_error(mpd.conn)) - mpd.conn_state = MPD_FAILURE; - } - - if(n > 0) { - #ifdef DEBUG - fprintf(stdout,"Send http response:\n %s\n",mpd.buf); - #endif - mg_send_http_chunk(nc, mpd.buf, n); - } -} - -void callback_mympd(struct mg_connection *nc, const struct mg_str msg) -{ - enum mpd_cmd_ids cmd_id = get_cmd_id(msg.p); - size_t n = 0; - unsigned int uint_buf, uint_buf_2; - int int_buf; - float float_buf; - char *p_charbuf = NULL, *token; - char *p_charbuf2 = NULL; - char *searchstr = NULL; - #ifdef DEBUG - fprintf(stdout,"Got request: %s\n",msg.p); - #endif - if(cmd_id == -1) - return; - - switch(cmd_id) - { case MPD_API_WELCOME: n = mympd_put_welcome(mpd.buf); case MPD_API_UPDATE_DB: @@ -154,343 +102,184 @@ void callback_mympd(struct mg_connection *nc, const struct mg_str msg) mpd_run_clear(mpd.conn); break; case MPD_API_RM_TRACK: - if(sscanf(msg.p, "MPD_API_RM_TRACK,%u", &uint_buf)) - mpd_run_delete_id(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1); + if (je == 1) + mpd_run_delete_id(mpd.conn, uint_buf1); break; case MPD_API_RM_RANGE: - if(sscanf(msg.p, "MPD_API_RM_RANGE,%u,%u", &uint_buf, &uint_buf_2)) - mpd_run_delete_range(mpd.conn, uint_buf, uint_buf_2); + je = json_scanf(msg.p, msg.len, "{ data: { start:%u, end:%u } }", &uint_buf1, &uint_buf2); + if (je == 2) + mpd_run_delete_range(mpd.conn, uint_buf1, uint_buf2); break; case MPD_API_MOVE_TRACK: - if (sscanf(msg.p, "MPD_API_MOVE_TRACK,%u,%u", &uint_buf, &uint_buf_2) == 2) + je = json_scanf(msg.p, msg.len, "{ data: { track:%u, pos:%u } }", &uint_buf1, &uint_buf2); + if (je == 2) { - uint_buf -= 1; - uint_buf_2 -= 1; - mpd_run_move(mpd.conn, uint_buf, uint_buf_2); + uint_buf1 -= 1; + uint_buf2 -= 1; + mpd_run_move(mpd.conn, uint_buf1, uint_buf2); } break; case MPD_API_PLAY_TRACK: - if(sscanf(msg.p, "MPD_API_PLAY_TRACK,%u", &uint_buf)) - mpd_run_play_id(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { track:%u } }", &uint_buf1); + if (je == 1) + mpd_run_play_id(mpd.conn, uint_buf1); break; case MPD_API_TOGGLE_RANDOM: - if(sscanf(msg.p, "MPD_API_TOGGLE_RANDOM,%u", &uint_buf)) - mpd_run_random(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1); + if (je == 1) + mpd_run_random(mpd.conn, uint_buf1); break; case MPD_API_TOGGLE_REPEAT: - if(sscanf(msg.p, "MPD_API_TOGGLE_REPEAT,%u", &uint_buf)) - mpd_run_repeat(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1); + if (je == 1) + mpd_run_repeat(mpd.conn, uint_buf1); break; case MPD_API_TOGGLE_CONSUME: - if(sscanf(msg.p, "MPD_API_TOGGLE_CONSUME,%u", &uint_buf)) - mpd_run_consume(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1); + if (je == 1) + mpd_run_consume(mpd.conn, uint_buf1); break; case MPD_API_TOGGLE_SINGLE: - if(sscanf(msg.p, "MPD_API_TOGGLE_SINGLE,%u", &uint_buf)) - mpd_run_single(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1); + if (je == 1) + mpd_run_single(mpd.conn, uint_buf1); break; case MPD_API_SET_CROSSFADE: - if(sscanf(msg.p, "MPD_API_SET_CROSSFADE,%u", &uint_buf)) - mpd_run_crossfade(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { state:%u } }", &uint_buf1); + if (je == 1) + mpd_run_crossfade(mpd.conn, uint_buf1); break; case MPD_API_SET_MIXRAMPDB: - if(sscanf(msg.p, "MPD_API_SET_MIXRAMPDB,%f", &float_buf)) + je = json_scanf(msg.p, msg.len, "{ data: { state:%f } }", &float_buf); + if (je == 1) mpd_run_mixrampdb(mpd.conn, float_buf); break; case MPD_API_SET_MIXRAMPDELAY: - if(sscanf(msg.p, "MPD_API_SET_MIXRAMPDELAY,%f", &float_buf)) + je = json_scanf(msg.p, msg.len, "{ data: { state:%f } }", &float_buf); + if (je == 1) mpd_run_mixrampdelay(mpd.conn, float_buf); break; case MPD_API_GET_OUTPUTNAMES: n = mympd_put_outputnames(mpd.buf); break; case MPD_API_TOGGLE_OUTPUT: - if (sscanf(msg.p, "MPD_API_TOGGLE_OUTPUT,%u,%u", &uint_buf, &uint_buf_2)) { - if (uint_buf_2) - mpd_run_enable_output(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { output:%u, state:%u } }", &uint_buf1, &uint_buf2); + if (je == 2) + { + if (uint_buf2) + mpd_run_enable_output(mpd.conn, uint_buf1); else - mpd_run_disable_output(mpd.conn, uint_buf); + mpd_run_disable_output(mpd.conn, uint_buf1); } break; case MPD_API_SET_VOLUME: - if(sscanf(msg.p, "MPD_API_SET_VOLUME,%ud", &uint_buf) && uint_buf <= 100) - mpd_run_set_volume(mpd.conn, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { volume:%u } }", &uint_buf1); + if (je == 1) + mpd_run_set_volume(mpd.conn, uint_buf1); break; case MPD_API_SET_SEEK: - if(sscanf(msg.p, "MPD_API_SET_SEEK,%u,%u", &uint_buf, &uint_buf_2)) - mpd_run_seek_id(mpd.conn, uint_buf, uint_buf_2); + je = json_scanf(msg.p, msg.len, "{ data: { songid:%u, seek:%u } }", &uint_buf1, &uint_buf2); + if (je == 2) + mpd_run_seek_id(mpd.conn, uint_buf1, uint_buf2); break; case MPD_API_GET_QUEUE: - if(sscanf(msg.p, "MPD_API_GET_QUEUE,%u", &uint_buf)) - n = mympd_put_queue(mpd.buf, uint_buf); + je = json_scanf(msg.p, msg.len, "{ data: { offset:%u } }", &uint_buf1); + if (je == 1) + n = mympd_put_queue(mpd.buf, uint_buf1); break; case MPD_API_GET_CURRENT_SONG: n = mympd_put_current_song(mpd.buf); break; - case MPD_API_GET_ARTISTS: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_GET_ARTISTS")) - goto out_artist; - uint_buf = strtoul(strtok(NULL, ","), NULL, 10); - - if((token = strtok(NULL, ",")) == NULL) { - goto out_artist; - } else { - p_charbuf2 = strdup(token); - } - n = mympd_put_db_tag(mpd.buf, uint_buf, "AlbumArtist","","",p_charbuf2); - free(p_charbuf2); -out_artist: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q } }", &uint_buf1, &p_charbuf1); + if (je == 2) + n = mympd_put_db_tag(mpd.buf, uint_buf1, "AlbumArtist","","",p_charbuf1); + free(p_charbuf1); break; case MPD_API_GET_ARTISTALBUMS: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_GET_ARTISTALBUMS")) - goto out_artistalbum; - uint_buf = strtoul(strtok(NULL, ","), NULL, 10); - - if((token = strtok(NULL, ",")) == NULL) { - goto out_artistalbum; - } else { - p_charbuf2 = strdup(token); - } - - if((token = strtok(NULL, ",")) == NULL) { - free(p_charbuf2); - goto out_artistalbum; - } else { - searchstr = strdup(token); - } - n = mympd_put_db_tag(mpd.buf, uint_buf, "Album", "AlbumArtist", searchstr, p_charbuf2); - free(searchstr); + je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q, albumartist:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2); + if (je == 3) + n = mympd_put_db_tag(mpd.buf, uint_buf1, "Album", "AlbumArtist", p_charbuf2, p_charbuf1); + free(p_charbuf1); free(p_charbuf2); -out_artistalbum: - free(p_charbuf); - break; - case MPD_API_GET_ARTISTALBUMTITLES: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_GET_ARTISTALBUMTITLES")) - goto out_artistalbumtitle; - - if((token = strtok(NULL, ",")) == NULL) { - goto out_artistalbumtitle; - } else { - searchstr = strdup(token); - } - n = mympd_put_songs_in_album(mpd.buf, searchstr, token+strlen(token)+1); - free(searchstr); -out_artistalbumtitle: - free(p_charbuf); break; case MPD_API_GET_PLAYLISTS: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_GET_PLAYLISTS")) - goto out_artist; - uint_buf = strtoul(strtok(NULL, ","), NULL, 10); - - if((token = strtok(NULL, ",")) == NULL) { - goto out_playlists; - } else { - p_charbuf2 = strdup(token); - } - n = mympd_put_playlists(mpd.buf, uint_buf, p_charbuf2); - free(p_charbuf2); -out_playlists: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q } }", &uint_buf1, &p_charbuf1); + if (je == 2) + n = mympd_put_playlists(mpd.buf, uint_buf1, p_charbuf1); + free(p_charbuf1); break; case MPD_API_GET_FILESYSTEM: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_GET_FILESYSTEM")) - goto out_browse; - - uint_buf = strtoul(strtok(NULL, ","), NULL, 10); - - if((token = strtok(NULL, ",")) == NULL) { - goto out_browse; - } else { - p_charbuf2 = strdup(token); - } - - if((token = strtok(NULL, ",")) == NULL) { - free(p_charbuf2); - goto out_browse; - } else { - searchstr = strdup(token); - } - n = mympd_put_browse(mpd.buf, p_charbuf2, uint_buf, searchstr); - free(searchstr); + je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, filter:%Q, path:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2); + if (je == 3) + n = mympd_put_browse(mpd.buf, p_charbuf2, uint_buf1, p_charbuf1); + free(p_charbuf1); free(p_charbuf2); -out_browse: - free(p_charbuf); - break; - break; case MPD_API_ADD_TRACK: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_TRACK")) - goto out_add_track; - - if((token = strtok(NULL, ",")) == NULL) - goto out_add_track; - - free(p_charbuf); - p_charbuf = strdup(msg.p); - mpd_run_add(mpd.conn, get_arg1(p_charbuf)); -out_add_track: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1); + if (je == 1) + mpd_run_add(mpd.conn, p_charbuf1); + free(p_charbuf1); break; case MPD_API_ADD_PLAY_TRACK: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_PLAY_TRACK")) - goto out_play_track; - - if((token = strtok(NULL, ",")) == NULL) - goto out_play_track; - - free(p_charbuf); - p_charbuf = strdup(msg.p); - int_buf = mpd_run_add_id(mpd.conn, get_arg1(p_charbuf)); - if(int_buf != -1) - mpd_run_play_id(mpd.conn, int_buf); -out_play_track: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { uri:%Q } }", &p_charbuf1); + if (je == 1) { + int_buf = mpd_run_add_id(mpd.conn, p_charbuf1); + if(int_buf != -1) + mpd_run_play_id(mpd.conn, int_buf); + } + free(p_charbuf1); break; case MPD_API_ADD_PLAYLIST: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_ADD_PLAYLIST")) - goto out_playlist; - - if((token = strtok(NULL, ",")) == NULL) - goto out_playlist; - - free(p_charbuf); - p_charbuf = strdup(msg.p); - mpd_run_load(mpd.conn, get_arg1(p_charbuf)); -out_playlist: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q } }", &p_charbuf1); + if (je == 1) + mpd_run_load(mpd.conn, p_charbuf1); + free(p_charbuf1); break; case MPD_API_SAVE_QUEUE: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_SAVE_QUEUE")) - goto out_save_queue; - - if((token = strtok(NULL, ",")) == NULL) - goto out_save_queue; - - free(p_charbuf); - p_charbuf = strdup(msg.p); - mpd_run_save(mpd.conn, get_arg1(p_charbuf)); -out_save_queue: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q } }", &p_charbuf1); + if (je == 1) + mpd_run_save(mpd.conn, p_charbuf1); + free(p_charbuf1); break; case MPD_API_SEARCH_QUEUE: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEARCH_QUEUE")) - goto out_search_queue; - if((token = strtok(NULL, ",")) == NULL) { - goto out_search_queue; - } else { - p_charbuf2 = strdup(token); - } - - uint_buf = strtoul(strtok(NULL, ","), NULL, 10); - - if((token = strtok(NULL, ",")) == NULL) { - free(p_charbuf2); - goto out_search_queue; - } else { - searchstr = strdup(token); - } - n = mympd_search_queue(mpd.buf, p_charbuf2, uint_buf, searchstr); - free(searchstr); + je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, mpdtag:%Q, searchstr:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2); + if (je == 3) + n = mympd_search_queue(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2); + free(p_charbuf1); free(p_charbuf2); -out_search_queue: - free(p_charbuf); break; case MPD_API_SEARCH_ADD: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEARCH_ADD")) - goto out_search_add; - if((token = strtok(NULL, ",")) == NULL) { - goto out_search_add; - } else { - p_charbuf2 = strdup(token); - } - - if((token = strtok(NULL, ",")) == NULL) { - free(p_charbuf2); - goto out_search_add; - } else { - searchstr = strdup(token); - } - n = mympd_search_add(mpd.buf, p_charbuf2, searchstr); - free(searchstr); + je = json_scanf(msg.p, msg.len, "{ data: { mpdtag:%Q, searchstr:%Q } }", &p_charbuf1, &p_charbuf2); + if (je == 2) + n = mympd_search_add(mpd.buf, p_charbuf1, p_charbuf2); + free(p_charbuf1); free(p_charbuf2); -out_search_add: - free(p_charbuf); break; case MPD_API_SEARCH: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEARCH")) - goto out_search; - if((token = strtok(NULL, ",")) == NULL) { - goto out_search; - } else { - p_charbuf2 = strdup(token); - } - - uint_buf = strtoul(strtok(NULL, ","), NULL, 10); - - if((token = strtok(NULL, ",")) == NULL) { - free(p_charbuf2); - goto out_search; - } else { - searchstr = strdup(token); - } - n = mympd_search(mpd.buf, p_charbuf2, uint_buf, searchstr); - free(searchstr); + je = json_scanf(msg.p, msg.len, "{ data: { offset:%u, mpdtag:%Q, searchstr:%Q } }", &uint_buf1, &p_charbuf1, &p_charbuf2); + if (je == 3) + n = mympd_search(mpd.buf, p_charbuf1, uint_buf1, p_charbuf2); + free(p_charbuf1); free(p_charbuf2); -out_search: - free(p_charbuf); break; case MPD_API_SEND_SHUFFLE: mpd_run_shuffle(mpd.conn); break; case MPD_API_SEND_MESSAGE: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_SEND_MESSAGE")) - goto out_send_message; - - if((token = strtok(NULL, ",")) == NULL) - goto out_send_message; - - free(p_charbuf); - p_charbuf = strdup(get_arg1(msg.p)); - - if ( strtok(p_charbuf, ",") == NULL ) - goto out_send_message; - - if ( (token = strtok(NULL, ",")) == NULL ) - goto out_send_message; - - mpd_run_send_message(mpd.conn, p_charbuf, token); -out_send_message: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { channel:%Q, text:%Q } }", &p_charbuf1, &p_charbuf2); + if (je == 2) + mpd_run_send_message(mpd.conn, p_charbuf1, p_charbuf2); + free(p_charbuf1); + free(p_charbuf2); break; case MPD_API_RM_PLAYLIST: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_RM_PLAYLIST")) - goto out_rm_playlist; - - if((token = strtok(NULL, ",")) == NULL) - goto out_rm_playlist; - - free(p_charbuf); - p_charbuf = strdup(msg.p); - mpd_run_rm(mpd.conn, get_arg1(p_charbuf)); -out_rm_playlist: - free(p_charbuf); + je = json_scanf(msg.p, msg.len, "{ data: { plist:%Q } }", &p_charbuf1); + if (je == 1) + mpd_run_rm(mpd.conn, p_charbuf1); + free(p_charbuf1); break; case MPD_API_GET_SETTINGS: n = mympd_put_settings(mpd.buf); @@ -499,25 +288,20 @@ out_rm_playlist: n = mympd_get_stats(mpd.buf); break; case MPD_API_SET_REPLAYGAIN: - p_charbuf = strdup(msg.p); - if(strcmp(strtok(p_charbuf, ","), "MPD_API_SET_REPLAYGAIN")) - goto out_set_replaygain; - - if((token = strtok(NULL, ",")) == NULL) - goto out_set_replaygain; - - free(p_charbuf); - p_charbuf = strdup(msg.p); - mpd_send_command(mpd.conn, "replay_gain_mode", get_arg1(p_charbuf), NULL); - struct mpd_pair *pair; - while ((pair = mpd_recv_pair(mpd.conn)) != NULL) { - mpd_return_pair(mpd.conn, pair); - } -out_set_replaygain: - free(p_charbuf); - break; + je = json_scanf(msg.p, msg.len, "{ data: { mode:%Q } }", &p_charbuf1); + if (je == 1) + { + mpd_send_command(mpd.conn, "replay_gain_mode", p_charbuf1, NULL); + struct mpd_pair *pair; + while ((pair = mpd_recv_pair(mpd.conn)) != NULL) { + mpd_return_pair(mpd.conn, pair); + } + } + free(p_charbuf1); + break; } - + free(cmd); + if(mpd.conn_state == MPD_CONNECTED && mpd_connection_get_error(mpd.conn) != MPD_ERROR_SUCCESS) { #ifdef DEBUG @@ -532,10 +316,9 @@ out_set_replaygain: } if(n > 0) { - if(is_websocket(nc)) { #ifdef DEBUG - fprintf(stdout,"Send response over websocket:\n %s\n",mpd.buf); + fprintf(stdout,"Send websocket response:\n %s\n",mpd.buf); #endif mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, mpd.buf, n); } @@ -543,9 +326,9 @@ out_set_replaygain: #ifdef DEBUG fprintf(stdout,"Send http response:\n %s\n",mpd.buf); #endif - mg_send_http_chunk(nc, mpd.buf, n); + mg_send_http_chunk(nc, mpd.buf, n); } - } + } } int mympd_close_handler(struct mg_connection *c) diff --git a/src/mpd_client.h b/src/mpd_client.h index e212bf1..6086f01 100644 --- a/src/mpd_client.h +++ b/src/mpd_client.h @@ -133,7 +133,6 @@ struct t_mpd_client_session { }; void mympd_poll(struct mg_mgr *s); -void callback_mympd_jsonrpc(struct mg_connection *nc, const struct mg_str msg); void callback_mympd(struct mg_connection *nc, const struct mg_str msg); int mympd_close_handler(struct mg_connection *c); int mympd_put_state(char *buffer, int *current_song_id, int *next_song_id, unsigned *queue_version); diff --git a/src/mympd.c b/src/mympd.c index 27fb3ee..2fb81c2 100644 --- a/src/mympd.c +++ b/src/mympd.c @@ -44,21 +44,16 @@ static void signal_handler(int sig_num) { } static void handle_api(struct mg_connection *nc, struct http_message *hm) { - mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); - char buf[1000] = {0}; - memcpy(buf, hm->body.p,sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len); - struct mg_str d = {buf, strlen(buf)}; - callback_mympd(nc, d); - mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */ -} - -static void handle_jsonrpc(struct mg_connection *nc, struct http_message *hm) { - mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); - char buf[1000] = {0}; - memcpy(buf, hm->body.p,sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len); - struct mg_str d = {buf, strlen(buf)}; - callback_mympd_jsonrpc(nc, d); - mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */ + if(!is_websocket(nc)) { + mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-Type: application/json\r\n\r\n"); + } + char buf[1000] = {0}; + memcpy(buf, hm->body.p,sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len); + struct mg_str d = {buf, strlen(buf)}; + callback_mympd(nc, d); + if(!is_websocket(nc)) { + mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */ + } } static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { @@ -67,20 +62,10 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { #ifdef DEBUG fprintf(stdout,"New Websocket connection\n"); #endif - struct mg_str d = {(char *) "MPD_API_WELCOME", 15 }; + struct mg_str d = {(char *) "{\"cmd\":\"MPD_API_WELCOME\"}", 25 }; callback_mympd(nc, d); break; } - case MG_EV_WEBSOCKET_FRAME: { - struct websocket_message *wm = (struct websocket_message *) ev_data; - wm->data[wm->size]='\0'; - struct mg_str d = {(char *) wm->data, wm->size}; - #ifdef DEBUG - fprintf(stdout,"Websocket request: %s\n",wm->data); - #endif - callback_mympd(nc, d); - break; - } case MG_EV_HTTP_REQUEST: { struct http_message *hm = (struct http_message *) ev_data; #ifdef DEBUG @@ -89,9 +74,6 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { if (mg_vcmp(&hm->uri, "/api") == 0) { handle_api(nc, hm); } - else if (mg_vcmp(&hm->uri, "/jsonrpc") == 0) { - handle_jsonrpc(nc, hm); - } else { mg_serve_http(nc, hm, s_http_server_opts); }