Skip to content

Commit 17ad8c7

Browse files
committed
Merge branch 'develop' of https://github.com/toniebox-reverse-engineering/teddycloud into develop
2 parents 5ed9ac7 + 10463bf commit 17ad8c7

File tree

4 files changed

+48
-61
lines changed

4 files changed

+48
-61
lines changed

src/handler.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "handler.h"
2+
#include "server_helpers.h"
23

34
req_cbr_t getCloudCbr(HttpConnection *connection, const char_t *uri, const char_t *queryString, cloudapi_t api, cbr_ctx_t *ctx, client_ctx_t *client_ctx)
45
{
@@ -94,9 +95,7 @@ void cbrCloudBodyPassthrough(void *src_ctx, HttpClientContext *cloud_ctx, const
9495
getContentPathFromCharRUID(ruid, &ctx->tonieInfo.contentPath, ctx->client_ctx->settings);
9596
ctx->tonieInfo = getTonieInfo(ctx->tonieInfo.contentPath, ctx->client_ctx->settings);
9697

97-
char *tmpPath = osAllocMem(osStrlen(ctx->tonieInfo.contentPath) + 4 + 1);
98-
osStrcpy(tmpPath, ctx->tonieInfo.contentPath);
99-
osStrcat(tmpPath, ".tmp");
98+
char *tmpPath = custom_asprintf("%s.tmp", ctx->tonieInfo.contentPath);
10099

101100
char *dir = strdup(ctx->tonieInfo.contentPath);
102101
dir[osStrlen(dir) - 8] = '\0';
@@ -120,9 +119,8 @@ void cbrCloudBodyPassthrough(void *src_ctx, HttpClientContext *cloud_ctx, const
120119
if (error == ERROR_END_OF_STREAM)
121120
{
122121
fsCloseFile(ctx->file);
123-
char *tmpPath = osAllocMem(osStrlen(ctx->tonieInfo.contentPath) + 4 + 1);
124-
osStrcpy(tmpPath, ctx->tonieInfo.contentPath);
125-
osStrcat(tmpPath, ".tmp");
122+
char *tmpPath = custom_asprintf("%s.tmp", ctx->tonieInfo.contentPath);
123+
126124
fsDeleteFile(ctx->tonieInfo.contentPath);
127125
fsRenameFile(tmpPath, ctx->tonieInfo.contentPath);
128126
if (fsFileExists(ctx->tonieInfo.contentPath))
@@ -194,12 +192,11 @@ char *strupr(char input[])
194192

195193
void getContentPathFromCharRUID(char ruid[17], char **pcontentPath, settings_t *settings)
196194
{
197-
*pcontentPath = osAllocMem(256);
198195
char filePath[18];
199196
osSprintf(filePath, "%.8s/%.8s", ruid, &ruid[8]);
200197
strupr(filePath);
201198

202-
osSprintf(*pcontentPath, "%s/%s", settings->internal.contentdirfull, filePath);
199+
*pcontentPath = custom_asprintf("%s/%s", settings->internal.contentdirfull, filePath);
203200
}
204201

205202
void getContentPathFromUID(uint64_t uid, char **pcontentPath, settings_t *settings)
@@ -242,10 +239,7 @@ tonie_info_t getTonieInfo(const char *contentPath, settings_t *settings)
242239
tonieInfo.contentConfig.cache = false;
243240
tonieInfo.contentConfig._updated = false;
244241
tonieInfo.contentConfig._stream = false;
245-
tonieInfo.contentConfig._streamFile = osAllocMem(osStrlen(contentPath) + 7 + 1);
246-
247-
osStrcpy(tonieInfo.contentConfig._streamFile, contentPath);
248-
osStrcat(tonieInfo.contentConfig._streamFile, ".stream");
242+
tonieInfo.contentConfig._streamFile = custom_asprintf("%s.stream", contentPath);
249243

250244
if (osStrstr(contentPath, ".json") == NULL &&
251245
osStrstr(contentPath, settings->internal.contentdirfull) == contentPath &&

src/handler_api.c

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -856,62 +856,63 @@ error_t handleApiContent(HttpConnection *connection, const char_t *uri, const ch
856856
bool skipFileHeader = !strcmp(ogg, "true");
857857
size_t startOffset = skipFileHeader ? 4096 : 0;
858858

859-
char *new_uri = (char *)osAllocMem(osStrlen(uri) + osStrlen(rootPath) + 1);
860-
if (new_uri == NULL)
861-
{
862-
return ERROR_OUT_OF_MEMORY;
863-
}
859+
char *file_path = custom_asprintf("%s%s", rootPath, &uri[8]);
864860

865-
osStrcpy(new_uri, rootPath);
866-
osStrcat(new_uri, &uri[8]);
867-
TRACE_DEBUG("Request for '%s', ogg: %s\r\n", new_uri, ogg);
861+
TRACE_DEBUG("Request for '%s', ogg: %s\r\n", file_path, ogg);
868862

869863
error_t error;
870864
size_t n;
871865
uint32_t length;
872866
FsFile *file;
873867

874868
// Retrieve the size of the specified file
875-
error = fsGetFileSize(new_uri, &length);
869+
error = fsGetFileSize(file_path, &length);
876870

877871
bool_t isStream = false;
878-
tonie_info_t tafInfo = getTonieInfo(new_uri, client_ctx->settings);
872+
tonie_info_t tafInfo = getTonieInfo(file_path, client_ctx->settings);
873+
879874
if (tafInfo.valid && tafInfo.stream)
880875
{
881876
isStream = true;
882877
length = CONTENT_LENGTH_MAX;
883878
connection->response.noCache = true;
884879
}
885-
// osFreeMem(filePathAbsolute);
880+
886881
freeTonieInfo(&tafInfo);
887-
// The specified URI cannot be found?
882+
888883
if (error || length < startOffset)
889884
{
890-
TRACE_ERROR("File does not exist '%s'\r\n", new_uri);
885+
TRACE_ERROR("File does not exist '%s'\r\n", file_path);
891886
return ERROR_NOT_FOUND;
892887
}
893888

894889
/* in case of skipped headers, also reduce the file length */
895890
length -= startOffset;
896891

897892
// Open the file for reading
898-
file = fsOpenFile(new_uri, FS_FILE_MODE_READ);
893+
file = fsOpenFile(file_path, FS_FILE_MODE_READ);
894+
free(file_path);
895+
899896
// Failed to open the file?
900897
if (file == NULL)
898+
{
901899
return ERROR_NOT_FOUND;
900+
}
901+
902+
char *range_hdr = NULL;
902903

903904
// Format HTTP response header
904905
// TODO add status 416 on invalid ranges
905906
if (!isStream && connection->request.Range.start > 0)
906907
{
907908
connection->request.Range.size = length;
908909
if (connection->request.Range.end >= connection->request.Range.size || connection->request.Range.end == 0)
910+
{
909911
connection->request.Range.end = connection->request.Range.size - 1;
912+
}
910913

911-
if (connection->response.contentRange == NULL)
912-
connection->response.contentRange = osAllocMem(255);
913-
914-
osSprintf((char *)connection->response.contentRange, "bytes %" PRIu32 "-%" PRIu32 "/%" PRIu32, connection->request.Range.start, connection->request.Range.end, connection->request.Range.size);
914+
range_hdr = custom_asprintf("bytes %" PRIu32 "-%" PRIu32 "/%" PRIu32, connection->request.Range.start, connection->request.Range.end, connection->request.Range.size);
915+
connection->response.contentRange = range_hdr;
915916
connection->response.statusCode = 206;
916917
connection->response.contentLength = connection->request.Range.end - connection->request.Range.start + 1;
917918
TRACE_DEBUG("Added response range %s\r\n", connection->response.contentRange);
@@ -924,14 +925,16 @@ error_t handleApiContent(HttpConnection *connection, const char_t *uri, const ch
924925
connection->response.contentType = "audio/ogg";
925926
connection->response.chunkedEncoding = FALSE;
926927

927-
// Send the header to the client
928928
error = httpWriteHeader(connection);
929-
// Any error to report?
929+
930+
if (range_hdr)
931+
{
932+
osFreeMem(range_hdr);
933+
}
934+
930935
if (error)
931936
{
932-
// Close the file
933937
fsCloseFile(file);
934-
// Return status code
935938
return error;
936939
}
937940

@@ -986,8 +989,6 @@ error_t handleApiContent(HttpConnection *connection, const char_t *uri, const ch
986989
}
987990
}
988991

989-
free(new_uri);
990-
991992
return error;
992993
}
993994

src/server.c

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,7 @@ error_t httpServerRequestCallback(HttpConnection *connection, const char_t *uri)
278278
uri = "/web/index.html";
279279
}
280280

281-
char_t *newUri = osAllocMem(osStrlen(client_ctx->settings->core.wwwdir) + osStrlen(uri) + 1);
282-
osStrcpy(newUri, client_ctx->settings->core.wwwdir);
283-
// osStrcat(newUri, "/");
284-
osStrcat(newUri, uri);
281+
char_t *newUri = custom_asprintf("%s%s", client_ctx->settings->core.wwwdir, uri);
285282

286283
error = httpSendResponse(connection, newUri);
287284
free(newUri);
@@ -291,12 +288,8 @@ error_t httpServerRequestCallback(HttpConnection *connection, const char_t *uri)
291288
error_t httpServerUriNotFoundCallback(HttpConnection *connection, const char_t *uri)
292289
{
293290
error_t error = NO_ERROR;
294-
char *fnf = "404.html";
295291

296-
char_t *newUri = osAllocMem(osStrlen(fnf) + osStrlen(get_settings()->core.wwwdir) + 2);
297-
osStrcpy(newUri, get_settings()->core.wwwdir);
298-
osStrcat(newUri, "/");
299-
osStrcat(newUri, fnf);
292+
char_t *newUri = custom_asprintf("%s/404.html", get_settings()->core.wwwdir);
300293

301294
error = httpSendResponse(connection, newUri);
302295
free(newUri);

src/settings.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "tls_adapter.h"
1212

1313
#include "fs_port.h"
14+
#include "server_helpers.h"
1415

1516
#define OVERLAY_CONFIG_PREFIX "overlay."
1617
static settings_t Settings_Overlay[MAX_OVERLAYS];
@@ -276,8 +277,7 @@ settings_t *get_settings_cn(const char *commonName)
276277
{
277278
char *boxId = settings_sanitize_box_id((const char *)commonName);
278279
char *boxPrefix = "teddyCloud Box ";
279-
char *boxName = osAllocMem(osStrlen(boxPrefix) + osStrlen(commonName) + 1);
280-
osSprintf(boxName, "%s%s", boxPrefix, commonName);
280+
char *boxName = custom_asprintf("%s%s", boxPrefix, commonName);
281281

282282
settings_set_string_id("commonName", boxId, i);
283283
settings_set_string_id("internal.overlayUniqueId", boxId, i);
@@ -491,7 +491,7 @@ void settings_save_ovl(bool overlay)
491491
for (size_t i = 0; i < MAX_OVERLAYS; i++)
492492
{
493493
int pos = 0;
494-
char buffer[256]; // Buffer to hold the file content
494+
char *buffer = NULL;
495495

496496
if (i == 0 && overlay)
497497
{
@@ -517,41 +517,40 @@ void settings_save_ovl(bool overlay)
517517
pos++;
518518
continue; // Only write overlay settings if they were overlayed
519519
}
520-
overlayPrefix = osAllocMem(8 + osStrlen(Settings_Overlay[i].internal.overlayUniqueId) + 1 + 1); // overlay.[NAME].
521-
osStrcpy(overlayPrefix, "overlay.");
522-
osStrcat(overlayPrefix, Settings_Overlay[i].internal.overlayUniqueId);
523-
osStrcat(overlayPrefix, ".");
520+
overlayPrefix = custom_asprintf("overlay.%s.", Settings_Overlay[i].internal.overlayUniqueId);
524521
}
525522
else
526523
{
527-
overlayPrefix = osAllocMem(1);
528-
osStrcpy(overlayPrefix, "");
524+
overlayPrefix = custom_asprintf("");
529525
}
530526

531527
switch (opt->type)
532528
{
533529
case TYPE_BOOL:
534-
sprintf(buffer, "%s%s=%s\n", overlayPrefix, opt->option_name, *((bool *)opt->ptr) ? "true" : "false");
530+
buffer = custom_asprintf("%s%s=%s\n", overlayPrefix, opt->option_name, *((bool *)opt->ptr) ? "true" : "false");
535531
break;
536532
case TYPE_SIGNED:
537-
sprintf(buffer, "%s%s=%d\n", overlayPrefix, opt->option_name, *((int32_t *)opt->ptr));
533+
buffer = custom_asprintf("%s%s=%d\n", overlayPrefix, opt->option_name, *((int32_t *)opt->ptr));
538534
break;
539535
case TYPE_UNSIGNED:
540536
case TYPE_HEX:
541-
sprintf(buffer, "%s%s=%u\n", overlayPrefix, opt->option_name, *((uint32_t *)opt->ptr));
537+
buffer = custom_asprintf("%s%s=%u\n", overlayPrefix, opt->option_name, *((uint32_t *)opt->ptr));
542538
break;
543539
case TYPE_FLOAT:
544-
sprintf(buffer, "%s%s=%f\n", overlayPrefix, opt->option_name, *((float *)opt->ptr));
540+
buffer = custom_asprintf("%s%s=%f\n", overlayPrefix, opt->option_name, *((float *)opt->ptr));
545541
break;
546542
case TYPE_STRING:
547-
sprintf(buffer, "%s%s=%s\n", overlayPrefix, opt->option_name, *((char **)opt->ptr));
543+
buffer = custom_asprintf("%s%s=%s\n", overlayPrefix, opt->option_name, *((char **)opt->ptr));
548544
break;
549545
default:
550-
buffer[0] = '\0';
546+
buffer = custom_asprintf("");
551547
break;
552548
}
553-
if (osStrlen(buffer) > 0)
549+
if (buffer && osStrlen(buffer) > 0)
550+
{
554551
fsWriteFile(file, buffer, osStrlen(buffer));
552+
osFreeMem(buffer);
553+
}
555554
osFreeMem(overlayPrefix);
556555
}
557556
pos++;

0 commit comments

Comments
 (0)