From 10558cc9007f87939b97fb36c87a05be4b99660d Mon Sep 17 00:00:00 2001 From: Jaroslav Kysela Date: Mon, 4 Aug 2014 21:18:49 +0200 Subject: [PATCH] http: enhance redirect and playlists mux override --- src/http.c | 29 ++++++++++++++++++++++++++--- src/http.h | 3 ++- src/webui/simpleui.c | 2 +- src/webui/webui.c | 38 ++++++++++++++++++++++++++++---------- 4 files changed, 57 insertions(+), 15 deletions(-) diff --git a/src/http.c b/src/http.c index 0973ebba..435cf49b 100644 --- a/src/http.c +++ b/src/http.c @@ -355,10 +355,31 @@ http_output_content(http_connection_t *hc, const char *content) * Send an HTTP REDIRECT */ void -http_redirect(http_connection_t *hc, const char *location) +http_redirect(http_connection_t *hc, const char *location, + http_arg_list_t *req_args) { + const char *loc = location; htsbuf_queue_flush(&hc->hc_reply); + if (req_args) { + http_arg_t *ra; + htsbuf_queue_t hq; + int first = 1; + htsbuf_queue_init(&hq, 0); + htsbuf_append(&hq, location, strlen(location)); + htsbuf_append(&hq, "?", 1); + TAILQ_FOREACH(ra, req_args, link) { + if (!first) + htsbuf_append(&hq, "&", 1); + first = 0; + htsbuf_append_and_escape_url(&hq, ra->key); + htsbuf_append(&hq, "=", 1); + htsbuf_append_and_escape_url(&hq, ra->val); + } + loc = htsbuf_to_string(&hq); + htsbuf_queue_flush(&hq); + } + htsbuf_qprintf(&hc->hc_reply, "\r\n" "\r\n" @@ -366,9 +387,11 @@ http_redirect(http_connection_t *hc, const char *location) "\r\n" "Please follow %s\r\n" "\r\n", - location, location); + loc, loc); - http_send_reply(hc, HTTP_STATUS_FOUND, "text/html", NULL, location, 0); + http_send_reply(hc, HTTP_STATUS_FOUND, "text/html", NULL, loc, 0); + if (loc != location) + free((void *)loc); } /** diff --git a/src/http.h b/src/http.h index ee52cfac..73791649 100644 --- a/src/http.h +++ b/src/http.h @@ -167,7 +167,8 @@ void http_output_html(http_connection_t *hc); void http_output_content(http_connection_t *hc, const char *content); -void http_redirect(http_connection_t *hc, const char *location); +void http_redirect(http_connection_t *hc, const char *location, + struct http_arg_list *req_args); void http_send_header(http_connection_t *hc, int rc, const char *content, int64_t contentlen, const char *encoding, diff --git a/src/webui/simpleui.c b/src/webui/simpleui.c index 4bea56e2..9bd698d9 100644 --- a/src/webui/simpleui.c +++ b/src/webui/simpleui.c @@ -307,7 +307,7 @@ page_pvrinfo(http_connection_t *hc, const char *remain, void *opaque) if(de == NULL) { pthread_mutex_unlock(&global_lock); - http_redirect(hc, "/simple.html"); + http_redirect(hc, "/simple.html", &hc->hc_req_args); return 0; } diff --git a/src/webui/webui.c b/src/webui/webui.c index 8e3f38d8..05d5fbbb 100644 --- a/src/webui/webui.c +++ b/src/webui/webui.c @@ -141,9 +141,9 @@ static int page_root(http_connection_t *hc, const char *remain, void *opaque) { if(is_client_simple(hc)) { - http_redirect(hc, "simple.html"); + http_redirect(hc, "simple.html", &hc->hc_req_args); } else { - http_redirect(hc, "extjs.html"); + http_redirect(hc, "extjs.html", &hc->hc_req_args); } return 0; } @@ -154,7 +154,7 @@ page_root2(http_connection_t *hc, const char *remain, void *opaque) if (!tvheadend_webroot) return 1; char *tmp = malloc(strlen(tvheadend_webroot) + 2); sprintf(tmp, "%s/", tvheadend_webroot); - http_redirect(hc, tmp); + http_redirect(hc, tmp, &hc->hc_req_args); free(tmp); return 0; } @@ -374,7 +374,7 @@ http_channel_playlist(http_connection_t *hc, channel_t *channel) htsbuf_qprintf(hq, "#EXTM3U\n"); htsbuf_qprintf(hq, "#EXTINF:-1,%s\n", channel_get_name(channel)); - htsbuf_qprintf(hq, "http://%s%s?ticket=%s", host, buf, + htsbuf_qprintf(hq, "http://%s%s?ticket=%s", host, buf, access_ticket_create(buf)); #if ENABLE_LIBAV @@ -415,16 +415,22 @@ http_tag_playlist(http_connection_t *hc, channel_tag_t *tag) char buf[255]; channel_tag_mapping_t *ctm; const char *host; + muxer_container_type_t mc; hq = &hc->hc_reply; host = http_arg_get(&hc->hc_args, "Host"); + mc = muxer_container_txt2type(http_arg_get(&hc->hc_req_args, "mux")); + if(mc == MC_UNKNOWN) + mc = dvr_config_find_by_name_default("")->dvr_mc; + htsbuf_qprintf(hq, "#EXTM3U\n"); LIST_FOREACH(ctm, &tag->ct_ctms, ctm_tag_link) { snprintf(buf, sizeof(buf), "/stream/channelid/%d", channel_get_id(ctm->ctm_channel)); htsbuf_qprintf(hq, "#EXTINF:-1,%s\n", channel_get_name(ctm->ctm_channel)); - htsbuf_qprintf(hq, "http://%s%s?ticket=%s\n", host, buf, + htsbuf_qprintf(hq, "http://%s%s?ticket=%s", host, buf, access_ticket_create(buf)); + htsbuf_qprintf(hq, "&mux=%s\n", muxer_container_type2txt(mc)); } http_output_content(hc, "audio/x-mpegurl"); @@ -443,10 +449,15 @@ http_tag_list_playlist(http_connection_t *hc) char buf[255]; channel_tag_t *ct; const char *host; + muxer_container_type_t mc; hq = &hc->hc_reply; host = http_arg_get(&hc->hc_args, "Host"); + mc = muxer_container_txt2type(http_arg_get(&hc->hc_req_args, "mux")); + if(mc == MC_UNKNOWN) + mc = dvr_config_find_by_name_default("")->dvr_mc; + htsbuf_qprintf(hq, "#EXTM3U\n"); TAILQ_FOREACH(ct, &channel_tags, ct_link) { if(!ct->ct_enabled || ct->ct_internal) @@ -454,8 +465,9 @@ http_tag_list_playlist(http_connection_t *hc) snprintf(buf, sizeof(buf), "/playlist/tagid/%d", ct->ct_identifier); htsbuf_qprintf(hq, "#EXTINF:-1,%s\n", ct->ct_name); - htsbuf_qprintf(hq, "http://%s%s?ticket=%s\n", host, buf, + htsbuf_qprintf(hq, "http://%s%s?ticket=%s", host, buf, access_ticket_create(buf)); + htsbuf_qprintf(hq, "&mux=%s\n", muxer_container_type2txt(mc)); } http_output_content(hc, "audio/x-mpegurl"); @@ -486,10 +498,15 @@ http_channel_list_playlist(http_connection_t *hc) channel_t **chlist; const char *host; int idx = 0, count = 0; + muxer_container_type_t mc; hq = &hc->hc_reply; host = http_arg_get(&hc->hc_args, "Host"); + mc = muxer_container_txt2type(http_arg_get(&hc->hc_req_args, "mux")); + if(mc == MC_UNKNOWN) + mc = dvr_config_find_by_name_default("")->dvr_mc; + CHANNEL_FOREACH(ch) count++; @@ -508,8 +525,9 @@ http_channel_list_playlist(http_connection_t *hc) snprintf(buf, sizeof(buf), "/stream/channelid/%d", channel_get_id(ch)); htsbuf_qprintf(hq, "#EXTINF:-1,%s\n", channel_get_name(ch)); - htsbuf_qprintf(hq, "http://%s%s?ticket=%s\n", host, buf, + htsbuf_qprintf(hq, "http://%s%s?ticket=%s", host, buf, access_ticket_create(buf)); + htsbuf_qprintf(hq, "&mux=%s\n", muxer_container_type2txt(mc)); } free(chlist); @@ -557,7 +575,7 @@ http_dvr_list_playlist(http_connection_t *hc) htsbuf_qprintf(hq, "#EXT-X-PROGRAM-DATE-TIME:%s\n", buf); snprintf(buf, sizeof(buf), "/dvrfile/%d", de->de_id); - htsbuf_qprintf(hq, "http://%s%s?ticket=%s\n", host, buf, + htsbuf_qprintf(hq, "http://%s%s?ticket=%s\n", host, buf, access_ticket_create(buf)); } @@ -624,7 +642,7 @@ page_http_playlist(http_connection_t *hc, const char *remain, void *opaque) channel_tag_t *tag = NULL; if(!remain) { - http_redirect(hc, "/playlist/channels"); + http_redirect(hc, "/playlist/channels", &hc->hc_req_args); return HTTP_STATUS_FOUND; } @@ -1231,7 +1249,7 @@ webui_static_content(const char *http_path, const char *source) static int favicon(http_connection_t *hc, const char *remain, void *opaque) { - http_redirect(hc, "static/htslogo.png"); + http_redirect(hc, "static/htslogo.png", NULL); return 0; }