From 678a2d29a69ee7b3e88641d2f9038cda72f57c64 Mon Sep 17 00:00:00 2001 From: Alex Rousskov Date: Thu, 12 Aug 2021 12:23:43 +0000 Subject: [PATCH] Bug 4922: Improve ftp://... filename extraction (#879) Symptoms include: Unexpected ERR_FTP_NOT_FOUND/FTP_REPLY_CODE=550 outcome for GET ftp://... requests with URLs containing `;type=...`. Broken since commit 51b5dcf. --- src/clients/FtpGateway.cc | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/clients/FtpGateway.cc b/src/clients/FtpGateway.cc index 2f2e2cf683..a80c4363cf 100644 --- a/src/clients/FtpGateway.cc +++ b/src/clients/FtpGateway.cc @@ -1071,16 +1071,17 @@ Ftp::Gateway::checkAuth(const HttpHeader * req_hdr) void Ftp::Gateway::checkUrlpath() { - static SBuf str_type_eq("type="); - auto t = request->url.path().rfind(';'); - - if (t != SBuf::npos) { - auto filenameEnd = t-1; - if (request->url.path().substr(++t).cmp(str_type_eq, str_type_eq.length()) == 0) { - t += str_type_eq.length(); - typecode = (char)xtoupper(request->url.path()[t]); - request->url.path(request->url.path().substr(0,filenameEnd)); - } + // If typecode was specified, extract it and leave just the filename in + // url.path. Tolerate trailing garbage or missing typecode value. Roughly: + // [filename] ;type=[typecode char] [trailing garbage] + static const SBuf middle(";type="); + const auto typeSpecStart = request->url.path().find(middle); + if (typeSpecStart != SBuf::npos) { + const auto fullPath = request->url.path(); + const auto typecodePos = typeSpecStart + middle.length(); + typecode = (typecodePos < fullPath.length()) ? + static_cast(xtoupper(fullPath[typecodePos])) : '\0'; + request->url.path(fullPath.substr(0, typeSpecStart)); } int l = request->url.path().length(); -- 2.47.2