Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
http: enhance redirect and playlists mux override
  • Loading branch information
perexg committed Aug 4, 2014
1 parent 0362d50 commit 10558cc
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
29 changes: 26 additions & 3 deletions src/http.c
Expand Up @@ -355,20 +355,43 @@ 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,
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\r\n"
"<HTML><HEAD>\r\n"
"<TITLE>Redirect</TITLE>\r\n"
"</HEAD><BODY>\r\n"
"Please follow <a href=\"%s\">%s</a>\r\n"
"</BODY></HTML>\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);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/http.h
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/webui/simpleui.c
Expand Up @@ -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;
}

Expand Down
38 changes: 28 additions & 10 deletions src/webui/webui.c
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand All @@ -443,19 +449,25 @@ 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)
continue;

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");
Expand Down Expand Up @@ -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++;

Expand All @@ -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);
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 10558cc

Please sign in to comment.