1 /*
2 * Copyright 2003-2021 The Music Player Daemon Project
3 * http://www.musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "IcyMetaDataServer.hxx"
21 #include "tag/Tag.hxx"
22 #include "util/FormatString.hxx"
23 #include "util/AllocatedString.hxx"
24 #include "util/TruncateString.hxx"
25
26 #include <iterator>
27
28 #include <string.h>
29
30 AllocatedString
icy_server_metadata_header(const char * name,const char * genre,const char * url,const char * content_type,int metaint)31 icy_server_metadata_header(const char *name,
32 const char *genre, const char *url,
33 const char *content_type, int metaint) noexcept
34 {
35 return FormatString("HTTP/1.1 200 OK\r\n"
36 "icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
37 "icy-notice2:MPD - The music player daemon<BR>\r\n"
38 "icy-name: %s\r\n" /* TODO */
39 "icy-genre: %s\r\n" /* TODO */
40 "icy-url: %s\r\n" /* TODO */
41 "icy-pub:1\r\n"
42 "icy-metaint:%d\r\n"
43 /* TODO "icy-br:%d\r\n" */
44 "Content-Type: %s\r\n"
45 "Connection: close\r\n"
46 "Pragma: no-cache\r\n"
47 "Cache-Control: no-cache, no-store\r\n"
48 "Access-Control-Allow-Origin: *\r\n"
49 "\r\n",
50 name,
51 genre,
52 url,
53 metaint,
54 /* bitrate, */
55 content_type);
56 }
57
58 static AllocatedString
icy_server_metadata_string(const char * stream_title,const char * stream_url)59 icy_server_metadata_string(const char *stream_title,
60 const char* stream_url) noexcept
61 {
62 // The leading n is a placeholder for the length information
63 auto icy_metadata = FormatString("nStreamTitle='%s';"
64 "StreamUrl='%s';"
65 /* pad 15 spaces just in case
66 the length needs to be
67 rounded up */
68 " ",
69 stream_title,
70 stream_url);
71
72 size_t meta_length = strlen(icy_metadata.c_str());
73
74 meta_length--; // subtract placeholder
75
76 meta_length = meta_length / 16;
77
78 icy_metadata[0] = meta_length;
79
80 if (meta_length > 255)
81 return nullptr;
82
83 return icy_metadata;
84 }
85
86 PagePtr
icy_server_metadata_page(const Tag & tag,const TagType * types)87 icy_server_metadata_page(const Tag &tag, const TagType *types) noexcept
88 {
89 const char *tag_items[TAG_NUM_OF_ITEM_TYPES];
90
91 int last_item = -1;
92 while (*types != TAG_NUM_OF_ITEM_TYPES) {
93 const char *tag_item = tag.GetValue(*types++);
94 if (tag_item)
95 tag_items[++last_item] = tag_item;
96 }
97
98 int item = 0;
99
100 // Length + Metadata - "StreamTitle='';StreamUrl='';" = 4081 - 28
101 char stream_title[(1 + 255 - 28) * 16];
102 char *p = stream_title, *const end = stream_title + std::size(stream_title);
103 stream_title[0] = '\0';
104
105 while (p < end && item <= last_item) {
106 p = CopyTruncateString(p, tag_items[item++], end - p);
107
108 if (item <= last_item)
109 p = CopyTruncateString(p, " - ", end - p);
110 }
111
112 const auto icy_string = icy_server_metadata_string(stream_title, "");
113
114 if (icy_string == nullptr)
115 return nullptr;
116
117 return std::make_shared<Page>(ConstBuffer<std::byte>{(const std::byte *)icy_string.c_str(), uint8_t(icy_string[0]) * 16U + 1U});
118 }
119