Skip to content

Commit b870fb0

Browse files
Fix URL encoding for thumbnail and cinf commands
- Add url_encode_path() function to properly encode file paths in URLs - Encode individual path components while preserving path separators - Handle both Windows backslashes and Linux forward slashes - Properly encode spaces and special characters in filenames - Improve HTTP error messages to show actual status codes Fixes CasparCG#1692
1 parent 37cc67a commit b870fb0

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/protocol/amcp/AMCPCommandsImpl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,13 +1463,13 @@ std::wstring thumbnail_list_command(command_context& ctx)
14631463
std::wstring thumbnail_retrieve_command(command_context& ctx)
14641464
{
14651465
return make_request(
1466-
ctx, "/thumbnail/" + http::url_encode(u8(ctx.parameters.at(0))), L"501 THUMBNAIL RETRIEVE FAILED\r\n");
1466+
ctx, "/thumbnail/" + http::url_encode_path(u8(ctx.parameters.at(0))), L"501 THUMBNAIL RETRIEVE FAILED\r\n");
14671467
}
14681468

14691469
std::wstring thumbnail_generate_command(command_context& ctx)
14701470
{
14711471
return make_request(
1472-
ctx, "/thumbnail/generate/" + http::url_encode(u8(ctx.parameters.at(0))), L"501 THUMBNAIL GENERATE FAILED\r\n");
1472+
ctx, "/thumbnail/generate/" + http::url_encode_path(u8(ctx.parameters.at(0))), L"501 THUMBNAIL GENERATE FAILED\r\n");
14731473
}
14741474

14751475
std::wstring thumbnail_generateall_command(command_context& ctx)
@@ -1481,7 +1481,7 @@ std::wstring thumbnail_generateall_command(command_context& ctx)
14811481

14821482
std::wstring cinf_command(command_context& ctx)
14831483
{
1484-
return make_request(ctx, "/cinf/" + http::url_encode(u8(ctx.parameters.at(0))), L"501 CINF FAILED\r\n");
1484+
return make_request(ctx, "/cinf/" + http::url_encode_path(u8(ctx.parameters.at(0))), L"501 CINF FAILED\r\n");
14851485
}
14861486

14871487
std::wstring cls_command(command_context& ctx) { return make_request(ctx, "/cls", L"501 CLS FAILED\r\n"); }

src/protocol/util/http_request.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,9 @@ HTTPResponse request(const std::string& host, const std::string& port, const std
6868
}
6969

7070
if (res.status_code < 200 || res.status_code >= 300) {
71-
// TODO
72-
CASPAR_THROW_EXCEPTION(io_error() << msg_info("Invalid Response"));
71+
std::stringstream error_msg;
72+
error_msg << "HTTP request failed with status " << res.status_code;
73+
CASPAR_THROW_EXCEPTION(io_error() << msg_info(error_msg.str()));
7374
}
7475

7576
// Read the response headers, which are terminated by a blank line.
@@ -122,4 +123,30 @@ std::string url_encode(const std::string& str)
122123
return escaped.str();
123124
}
124125

126+
std::string url_encode_path(const std::string& path)
127+
{
128+
std::stringstream result;
129+
std::string component;
130+
131+
for (auto c : path) {
132+
// Treat both forward slash and backslash as path separators
133+
// Always output forward slash for HTTP URLs
134+
if (c == '/' || c == '\\') {
135+
if (!component.empty()) {
136+
result << url_encode(component);
137+
component.clear();
138+
}
139+
result << '/';
140+
} else {
141+
component += c;
142+
}
143+
}
144+
145+
if (!component.empty()) {
146+
result << url_encode(component);
147+
}
148+
149+
return result.str();
150+
}
151+
125152
}} // namespace caspar::http

src/protocol/util/http_request.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ HTTPResponse request(const std::string& host, const std::string& port, const std
1818
// URL-encode a string for use in URL paths. Preserves forward slashes (/).
1919
// For encoding query parameters where '/' should be encoded, use a different function.
2020
std::string url_encode(const std::string& str);
21+
std::string url_encode_path(const std::string& path);
2122

2223
}} // namespace caspar::http

0 commit comments

Comments
 (0)