From b1e57f8f6ae8bd523fd2559ad4977d8ede61d431 Mon Sep 17 00:00:00 2001 From: Julien BLACHE Date: Thu, 9 Sep 2010 18:03:44 +0200 Subject: [PATCH] Factor out common code in RSP --- src/httpd_rsp.c | 91 +++++++++++++++---------------------------------- 1 file changed, 27 insertions(+), 64 deletions(-) diff --git a/src/httpd_rsp.c b/src/httpd_rsp.c index ec5affe1..116739f7 100644 --- a/src/httpd_rsp.c +++ b/src/httpd_rsp.c @@ -265,9 +265,31 @@ rsp_send_error(struct evhttp_request *req, char *errmsg) } static void -rsp_reply_info(struct evhttp_request *req, char **uri, struct evkeyvalq *query) +rsp_send_reply(struct evhttp_request *req, mxml_node_t *reply) { struct evbuffer *evbuf; + + evbuf = mxml_to_evbuf(reply); + mxmlDelete(reply); + + if (!evbuf) + { + rsp_send_error(req, "Could not finalize reply"); + + return; + } + + evhttp_add_header(req->output_headers, "Content-Type", "text/xml; charset=utf-8"); + evhttp_add_header(req->output_headers, "Connection", "close"); + httpd_send_reply(req, HTTP_OK, "OK", evbuf); + + evbuffer_free(evbuf); +} + + +static void +rsp_reply_info(struct evhttp_request *req, char **uri, struct evkeyvalq *query) +{ mxml_node_t *reply; mxml_node_t *status; mxml_node_t *info; @@ -316,21 +338,7 @@ rsp_reply_info(struct evhttp_request *req, char **uri, struct evkeyvalq *query) node = mxmlNewElement(info, "name"); mxmlNewText(node, 0, library); - evbuf = mxml_to_evbuf(reply); - mxmlDelete(reply); - - if (!evbuf) - { - rsp_send_error(req, "Could not finalize reply"); - - return; - } - - evhttp_add_header(req->output_headers, "Content-Type", "text/xml; charset=utf-8"); - evhttp_add_header(req->output_headers, "Connection", "close"); - httpd_send_reply(req, HTTP_OK, "OK", evbuf); - - evbuffer_free(evbuf); + rsp_send_reply(req, reply); } static void @@ -338,7 +346,6 @@ rsp_reply_db(struct evhttp_request *req, char **uri, struct evkeyvalq *query) { struct query_params qp; struct db_playlist_info dbpli; - struct evbuffer *evbuf; char **strval; mxml_node_t *reply; mxml_node_t *status; @@ -422,21 +429,7 @@ rsp_reply_db(struct evhttp_request *req, char **uri, struct evkeyvalq *query) db_query_end(&qp); - evbuf = mxml_to_evbuf(reply); - mxmlDelete(reply); - - if (!evbuf) - { - rsp_send_error(req, "Could not finalize reply"); - - return; - } - - evhttp_add_header(req->output_headers, "Content-Type", "text/xml; charset=utf-8"); - evhttp_add_header(req->output_headers, "Connection", "close"); - httpd_send_reply(req, HTTP_OK, "OK", evbuf); - - evbuffer_free(evbuf); + rsp_send_reply(req, reply); } static void @@ -444,7 +437,6 @@ rsp_reply_playlist(struct evhttp_request *req, char **uri, struct evkeyvalq *que { struct query_params qp; struct db_media_file_info dbmfi; - struct evbuffer *evbuf; const char *param; char **strval; mxml_node_t *reply; @@ -617,28 +609,13 @@ rsp_reply_playlist(struct evhttp_request *req, char **uri, struct evkeyvalq *que db_query_end(&qp); - evbuf = mxml_to_evbuf(reply); - mxmlDelete(reply); - - if (!evbuf) - { - rsp_send_error(req, "Could not finalize reply"); - - return; - } - - evhttp_add_header(req->output_headers, "Content-Type", "text/xml; charset=utf-8"); - evhttp_add_header(req->output_headers, "Connection", "close"); - httpd_send_reply(req, HTTP_OK, "OK", evbuf); - - evbuffer_free(evbuf); + rsp_send_reply(req, reply); } static void rsp_reply_browse(struct evhttp_request *req, char **uri, struct evkeyvalq *query) { struct query_params qp; - struct evbuffer *evbuf; char *browse_item; mxml_node_t *reply; mxml_node_t *status; @@ -747,21 +724,7 @@ rsp_reply_browse(struct evhttp_request *req, char **uri, struct evkeyvalq *query db_query_end(&qp); - evbuf = mxml_to_evbuf(reply); - mxmlDelete(reply); - - if (!evbuf) - { - rsp_send_error(req, "Could not finalize reply"); - - return; - } - - evhttp_add_header(req->output_headers, "Content-Type", "text/xml; charset=utf-8"); - evhttp_add_header(req->output_headers, "Connection", "close"); - httpd_send_reply(req, HTTP_OK, "OK", evbuf); - - evbuffer_free(evbuf); + rsp_send_reply(req, reply); } static void