# HG changeset patch # User Sebastien Jodogne # Date 1606456643 -3600 # Node ID a85e74235a78a24b8bb3568ecf69a050656dc4de # Parent 17d209a3f39712b21297e45450b5ccaa58f084c5 fix parsing of multipart boundaries, to resolve issue #190 in STOW-RS of DICOMweb plugin diff -r 17d209a3f397 -r a85e74235a78 OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp --- a/OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp Wed Nov 25 21:42:50 2020 +0100 +++ b/OrthancFramework/Sources/HttpServer/MultipartStreamReader.cpp Fri Nov 27 06:57:23 2020 +0100 @@ -297,6 +297,17 @@ } + static void RemoveSurroundingQuotes(std::string& value) + { + if (value.size() >= 2 && + value[0] == '"' && + value[value.size() - 1] == '"') + { + value = value.substr(1, value.size() - 2); + } + } + + bool MultipartStreamReader::ParseMultipartContentType(std::string& contentType, std::string& subType, std::string& boundary, @@ -331,6 +342,10 @@ if (boost::iequals("boundary", Toolbox::StripSpaces(items[0]))) { boundary = Toolbox::StripSpaces(items[1]); + + // https://bugs.orthanc-server.com/show_bug.cgi?id=190 + RemoveSurroundingQuotes(boundary); + valid = !boundary.empty(); } else if (boost::iequals("type", Toolbox::StripSpaces(items[0]))) @@ -340,12 +355,7 @@ // https://bitbucket.org/sjodogne/orthanc/issues/54/decide-what-to-do-wrt-quoting-of-multipart // https://tools.ietf.org/html/rfc7231#section-3.1.1.1 - if (subType.size() >= 2 && - subType[0] == '"' && - subType[subType.size() - 1] == '"') - { - subType = subType.substr(1, subType.size() - 2); - } + RemoveSurroundingQuotes(subType); } } } diff -r 17d209a3f397 -r a85e74235a78 OrthancFramework/UnitTestsSources/RestApiTests.cpp --- a/OrthancFramework/UnitTestsSources/RestApiTests.cpp Wed Nov 25 21:42:50 2020 +0100 +++ b/OrthancFramework/UnitTestsSources/RestApiTests.cpp Fri Nov 27 06:57:23 2020 +0100 @@ -894,6 +894,36 @@ } +TEST(MultipartStreamReader, Issue190) +{ + // https://bugs.orthanc-server.com/show_bug.cgi?id=190 + // https://hg.orthanc-server.com/orthanc-dicomweb/rev/6dc2f79b5579 + + std::map headers; + headers["content-type"] = "multipart/related; type=application/dicom; boundary=0f3cf5c0-70e0-41ef-baef-c6f9f65ec3e1"; + + { + std::string tmp, contentType, subType, boundary; + ASSERT_TRUE(Orthanc::MultipartStreamReader::GetMainContentType(tmp, headers)); + ASSERT_TRUE(Orthanc::MultipartStreamReader::ParseMultipartContentType(contentType, subType, boundary, tmp)); + ASSERT_EQ("multipart/related", contentType); + ASSERT_EQ("application/dicom", subType); + ASSERT_EQ("0f3cf5c0-70e0-41ef-baef-c6f9f65ec3e1", boundary); + } + + headers["content-type"] = "multipart/related; type=\"application/dicom\"; boundary=\"0f3cf5c0-70e0-41ef-baef-c6f9f65ec3e1\""; + + { + std::string tmp, contentType, subType, boundary; + ASSERT_TRUE(Orthanc::MultipartStreamReader::GetMainContentType(tmp, headers)); + ASSERT_TRUE(Orthanc::MultipartStreamReader::ParseMultipartContentType(contentType, subType, boundary, tmp)); + ASSERT_EQ("multipart/related", contentType); + ASSERT_EQ("application/dicom", subType); + ASSERT_EQ("0f3cf5c0-70e0-41ef-baef-c6f9f65ec3e1", boundary); + } +} + + TEST(WebServiceParameters, Url) { WebServiceParameters w;