Use content-disposition to set a filename to be used when downloading recorded files

This commit is contained in:
Andreas Öman 2011-03-02 23:47:57 +01:00
parent 2ea7764431
commit b80b3e0fbc
3 changed files with 29 additions and 7 deletions

View file

@ -152,7 +152,8 @@ void
http_send_header(http_connection_t *hc, int rc, const char *content,
int64_t contentlen,
const char *encoding, const char *location,
int maxage, const char *range)
int maxage, const char *range,
const char *disposition)
{
struct tm tm0, *tm;
htsbuf_queue_t hdrs;
@ -212,6 +213,9 @@ http_send_header(http_connection_t *hc, int rc, const char *content,
htsbuf_qprintf(&hdrs, "Accept-Ranges: %s\r\n", "bytes");
htsbuf_qprintf(&hdrs, "Content-Range: %s\r\n", range);
}
if(disposition != NULL)
htsbuf_qprintf(&hdrs, "Content-Disposition: %s\r\n", disposition);
htsbuf_qprintf(&hdrs, "\r\n");
@ -228,7 +232,7 @@ http_send_reply(http_connection_t *hc, int rc, const char *content,
const char *encoding, const char *location, int maxage)
{
http_send_header(hc, rc, content, hc->hc_reply.hq_size,
encoding, location, maxage, 0);
encoding, location, maxage, 0, NULL);
if(hc->hc_no_output)
return;

View file

@ -113,7 +113,8 @@ void http_redirect(http_connection_t *hc, const char *location);
void http_send_header(http_connection_t *hc, int rc, const char *content,
int64_t contentlen, const char *encoding,
const char *location, int maxage, const char *range);
const char *location, int maxage, const char *range,
const char *disposition);
typedef int (http_callback_t)(http_connection_t *hc,
const char *remain, void *opaque);

View file

@ -115,7 +115,7 @@ page_static_file(http_connection_t *hc, const char *remain, void *opaque)
return 404;
}
http_send_header(hc, 200, content, st.st_size, NULL, NULL, 10, 0);
http_send_header(hc, 200, content, st.st_size, NULL, NULL, 10, 0, NULL);
sendfile(hc->hc_fd, fd, NULL, st.st_size);
close(fd);
return 0;
@ -503,7 +503,8 @@ page_static_bundle(http_connection_t *hc, const char *remain, void *opaque)
if(!strcmp(fbe->filename, remain)) {
http_send_header(hc, 200, content, fbe->size,
fbe->original_size == -1 ? NULL : "gzip", NULL, 10, 0);
fbe->original_size == -1 ? NULL : "gzip", NULL, 10, 0,
NULL);
/* ignore return value */
n = write(hc->hc_fd, fbe->data, fbe->size);
return 0;
@ -519,12 +520,13 @@ page_static_bundle(http_connection_t *hc, const char *remain, void *opaque)
static int
page_dvrfile(http_connection_t *hc, const char *remain, void *opaque)
{
int fd;
int fd, i;
struct stat st;
const char *content = NULL, *postfix, *range;
dvr_entry_t *de;
char *fname;
char range_buf[255];
char disposition[256];
off_t content_len, file_start, file_end, chunk;
ssize_t r;
@ -584,9 +586,24 @@ page_dvrfile(http_connection_t *hc, const char *remain, void *opaque)
if(file_start > 0)
lseek(fd, file_start, SEEK_SET);
if(de->de_title != NULL) {
snprintf(disposition, sizeof(disposition),
"attachment; filename=%s.mkv", de->de_title);
i = 20;
while(disposition[i]) {
if(disposition[i] == ' ')
disposition[i] = '_';
i++;
}
} else {
disposition[0] = 0;
}
http_send_header(hc, range ? HTTP_STATUS_PARTIAL_CONTENT : HTTP_STATUS_OK,
content, content_len, NULL, NULL, 10,
range ? range_buf : NULL);
range ? range_buf : NULL,
disposition[0] ? disposition : NULL);
if(!hc->hc_no_output) {
while(content_len > 0) {