util/FormatString: return AllocatedString
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
#include "HttpdClient.hxx"
|
||||
#include "HttpdInternal.hxx"
|
||||
#include "util/ASCII.hxx"
|
||||
#include "util/AllocatedString.hxx"
|
||||
#include "Page.hxx"
|
||||
#include "IcyMetaDataServer.hxx"
|
||||
#include "net/SocketError.hxx"
|
||||
@@ -141,7 +142,8 @@ HttpdClient::HandleLine(const char *line)
|
||||
bool
|
||||
HttpdClient::SendResponse()
|
||||
{
|
||||
char buffer[1024], *allocated = nullptr;
|
||||
char buffer[1024];
|
||||
AllocatedString<> allocated = nullptr;
|
||||
const char *response;
|
||||
|
||||
assert(state == RESPONSE);
|
||||
@@ -162,11 +164,12 @@ HttpdClient::SendResponse()
|
||||
response = buffer;
|
||||
|
||||
} else if (metadata_requested) {
|
||||
response = allocated =
|
||||
allocated =
|
||||
icy_server_metadata_header(httpd.name, httpd.genre,
|
||||
httpd.website,
|
||||
httpd.content_type,
|
||||
metaint);
|
||||
response = allocated.c_str();
|
||||
} else { /* revert to a normal HTTP request */
|
||||
snprintf(buffer, sizeof(buffer),
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
@@ -180,7 +183,6 @@ HttpdClient::SendResponse()
|
||||
}
|
||||
|
||||
ssize_t nbytes = SocketMonitor::Write(response, strlen(response));
|
||||
delete[] allocated;
|
||||
if (gcc_unlikely(nbytes < 0)) {
|
||||
const SocketErrorMessage msg;
|
||||
FormatWarning(httpd_output_domain,
|
||||
|
@@ -22,48 +22,49 @@
|
||||
#include "Page.hxx"
|
||||
#include "tag/Tag.hxx"
|
||||
#include "util/FormatString.hxx"
|
||||
#include "util/AllocatedString.hxx"
|
||||
#include "util/StringUtil.hxx"
|
||||
#include "util/Macros.hxx"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
char*
|
||||
AllocatedString<>
|
||||
icy_server_metadata_header(const char *name,
|
||||
const char *genre, const char *url,
|
||||
const char *content_type, int metaint)
|
||||
{
|
||||
return FormatNew("ICY 200 OK\r\n"
|
||||
"icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
|
||||
"icy-notice2:MPD - The music player daemon<BR>\r\n"
|
||||
"icy-name: %s\r\n" /* TODO */
|
||||
"icy-genre: %s\r\n" /* TODO */
|
||||
"icy-url: %s\r\n" /* TODO */
|
||||
"icy-pub:1\r\n"
|
||||
"icy-metaint:%d\r\n"
|
||||
/* TODO "icy-br:%d\r\n" */
|
||||
"Content-Type: %s\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Pragma: no-cache\r\n"
|
||||
"Cache-Control: no-cache, no-store\r\n"
|
||||
"\r\n",
|
||||
name,
|
||||
genre,
|
||||
url,
|
||||
metaint,
|
||||
/* bitrate, */
|
||||
content_type);
|
||||
return FormatString("ICY 200 OK\r\n"
|
||||
"icy-notice1:<BR>This stream requires an audio player!<BR>\r\n" /* TODO */
|
||||
"icy-notice2:MPD - The music player daemon<BR>\r\n"
|
||||
"icy-name: %s\r\n" /* TODO */
|
||||
"icy-genre: %s\r\n" /* TODO */
|
||||
"icy-url: %s\r\n" /* TODO */
|
||||
"icy-pub:1\r\n"
|
||||
"icy-metaint:%d\r\n"
|
||||
/* TODO "icy-br:%d\r\n" */
|
||||
"Content-Type: %s\r\n"
|
||||
"Connection: close\r\n"
|
||||
"Pragma: no-cache\r\n"
|
||||
"Cache-Control: no-cache, no-store\r\n"
|
||||
"\r\n",
|
||||
name,
|
||||
genre,
|
||||
url,
|
||||
metaint,
|
||||
/* bitrate, */
|
||||
content_type);
|
||||
}
|
||||
|
||||
static char *
|
||||
static AllocatedString<>
|
||||
icy_server_metadata_string(const char *stream_title, const char* stream_url)
|
||||
{
|
||||
// The leading n is a placeholder for the length information
|
||||
char *icy_metadata = FormatNew("nStreamTitle='%s';"
|
||||
"StreamUrl='%s';",
|
||||
stream_title,
|
||||
stream_url);
|
||||
auto icy_metadata = FormatString("nStreamTitle='%s';"
|
||||
"StreamUrl='%s';",
|
||||
stream_title,
|
||||
stream_url);
|
||||
|
||||
size_t meta_length = strlen(icy_metadata);
|
||||
size_t meta_length = strlen(icy_metadata.c_str());
|
||||
|
||||
meta_length--; // subtract placeholder
|
||||
|
||||
@@ -71,10 +72,8 @@ icy_server_metadata_string(const char *stream_title, const char* stream_url)
|
||||
|
||||
icy_metadata[0] = meta_length;
|
||||
|
||||
if (meta_length > 255) {
|
||||
delete[] icy_metadata;
|
||||
if (meta_length > 255)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return icy_metadata;
|
||||
}
|
||||
@@ -105,14 +104,10 @@ icy_server_metadata_page(const Tag &tag, const TagType *types)
|
||||
p = CopyString(p, " - ", end - p);
|
||||
}
|
||||
|
||||
char *icy_string = icy_server_metadata_string(stream_title, "");
|
||||
const auto icy_string = icy_server_metadata_string(stream_title, "");
|
||||
|
||||
if (icy_string == nullptr)
|
||||
if (icy_string.IsNull())
|
||||
return nullptr;
|
||||
|
||||
Page *icy_metadata = Page::Copy(icy_string, (icy_string[0] * 16) + 1);
|
||||
|
||||
delete[] icy_string;
|
||||
|
||||
return icy_metadata;
|
||||
return Page::Copy(icy_string.c_str(), (icy_string[0] * 16) + 1);
|
||||
}
|
||||
|
@@ -24,11 +24,9 @@
|
||||
|
||||
struct Tag;
|
||||
class Page;
|
||||
template<typename T> class AllocatedString;
|
||||
|
||||
/**
|
||||
* Free the return value with delete[].
|
||||
*/
|
||||
char*
|
||||
AllocatedString<char>
|
||||
icy_server_metadata_header(const char *name,
|
||||
const char *genre, const char *url,
|
||||
const char *content_type, int metaint);
|
||||
|
Reference in New Issue
Block a user