Kaydet (Commit) 805b57cd authored tarafından Stephan Bergmann's avatar Stephan Bergmann

Various INetURLObject::getData() fixes

Change-Id: I5eafd6250bbde7227dcc0447a4280b7741c320de
üst 75e0fe06
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <sal/types.h> #include <sal/types.h>
#include "cppunit/TestFixture.h" #include "cppunit/TestFixture.h"
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx> #include <tools/urlobj.hxx>
#define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() ) #define OUSTR_TO_STDSTR( oustr ) std::string( OUStringToOString( oustr, RTL_TEXTENCODING_ASCII_US ).getStr() )
...@@ -243,6 +244,94 @@ namespace tools_urlobj ...@@ -243,6 +244,94 @@ namespace tools_urlobj
} }
} }
void urlobjTest_data() {
INetURLObject url;
SvMemoryStream * strm;
unsigned char const * buf;
url = INetURLObject("data:");
//TODO: CPPUNIT_ASSERT(url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm == 0);
url = INetURLObject("data:,");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm != 0);
CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
delete strm;
url = INetURLObject("data:,,%C3%A4%90");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm != 0);
CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
buf = static_cast<unsigned char const *>(strm->GetData());
CPPUNIT_ASSERT_EQUAL(0x2C, int(buf[0]));
CPPUNIT_ASSERT_EQUAL(0xC3, int(buf[1]));
CPPUNIT_ASSERT_EQUAL(0xA4, int(buf[2]));
CPPUNIT_ASSERT_EQUAL(0x90, int(buf[3]));
delete strm;
url = INetURLObject("data:base64,");
//TODO: CPPUNIT_ASSERT(url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm == 0);
url = INetURLObject("data:;base64,");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm != 0);
CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
delete strm;
url = INetURLObject("data:;bAsE64,");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm != 0);
CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(0), strm->GetSize());
delete strm;
url = INetURLObject("data:;base64,YWJjCg==");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm != 0);
CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
buf = static_cast<unsigned char const *>(strm->GetData());
CPPUNIT_ASSERT_EQUAL(0x61, int(buf[0]));
CPPUNIT_ASSERT_EQUAL(0x62, int(buf[1]));
CPPUNIT_ASSERT_EQUAL(0x63, int(buf[2]));
CPPUNIT_ASSERT_EQUAL(0x0A, int(buf[3]));
delete strm;
url = INetURLObject("data:;base64,YWJjCg=");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm == 0);
url = INetURLObject("data:;base64,YWJ$Cg==");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm == 0);
url = INetURLObject("data:text/plain;param=%22;base64,%22,YQ==");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm != 0);
CPPUNIT_ASSERT_EQUAL(sal_uIntPtr(4), strm->GetSize());
buf = static_cast<unsigned char const *>(strm->GetData());
CPPUNIT_ASSERT_EQUAL(0x59, int(buf[0]));
CPPUNIT_ASSERT_EQUAL(0x51, int(buf[1]));
CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[2]));
CPPUNIT_ASSERT_EQUAL(0x3D, int(buf[3]));
delete strm;
url = INetURLObject("http://example.com");
CPPUNIT_ASSERT(!url.HasError());
strm = url.getData();
CPPUNIT_ASSERT(strm == 0);
}
// Change the following lines only, if you add, remove or rename // Change the following lines only, if you add, remove or rename
// member functions of the current class, // member functions of the current class,
// because these macros are need by auto register mechanism. // because these macros are need by auto register mechanism.
...@@ -256,6 +345,7 @@ namespace tools_urlobj ...@@ -256,6 +345,7 @@ namespace tools_urlobj
CPPUNIT_TEST( urlobjTest_006 ); CPPUNIT_TEST( urlobjTest_006 );
CPPUNIT_TEST( urlobjCmisTest ); CPPUNIT_TEST( urlobjCmisTest );
CPPUNIT_TEST( urlobjTest_emptyPath ); CPPUNIT_TEST( urlobjTest_emptyPath );
CPPUNIT_TEST( urlobjTest_data );
CPPUNIT_TEST_SUITE_END( ); CPPUNIT_TEST_SUITE_END( );
}; // class createPool }; // class createPool
......
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 . * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/ */
#include <sal/config.h>
#include <boost/checked_delete.hpp>
#include <o3tl/heap_ptr.hxx>
#include <tools/urlobj.hxx> #include <tools/urlobj.hxx>
#include <tools/debug.hxx> #include <tools/debug.hxx>
#include <tools/inetmime.hxx> #include <tools/inetmime.hxx>
...@@ -588,6 +592,21 @@ void INetURLObject::setInvalid() ...@@ -588,6 +592,21 @@ void INetURLObject::setInvalid()
m_aFragment.clear(); m_aFragment.clear();
} }
namespace {
SvMemoryStream * memoryStream(void const * data, sal_Int32 length) {
o3tl::heap_ptr<char, boost::checked_array_deleter<char> > b(
new char[length]);
memcpy(b.get(), data, length);
o3tl::heap_ptr<SvMemoryStream> s(
new SvMemoryStream(b.get(), length, STREAM_READ));
s->ObjectOwnsMemory(true);
b.release();
return s.release();
}
}
SvMemoryStream* INetURLObject::getData() SvMemoryStream* INetURLObject::getData()
{ {
if( GetProtocol() != INET_PROT_DATA ) if( GetProtocol() != INET_PROT_DATA )
...@@ -596,34 +615,32 @@ SvMemoryStream* INetURLObject::getData() ...@@ -596,34 +615,32 @@ SvMemoryStream* INetURLObject::getData()
} }
OUString sURLPath = GetURLPath( DECODE_WITH_CHARSET, RTL_TEXTENCODING_ISO_8859_1 ); OUString sURLPath = GetURLPath( DECODE_WITH_CHARSET, RTL_TEXTENCODING_ISO_8859_1 );
OUString sType, sSubType; sal_Unicode const * pSkippedMediatype = INetMIME::scanContentType( sURLPath.getStr(), sURLPath.getStr() + sURLPath.getLength(), NULL, NULL, NULL );
OUString sBase64Enc(";base64,"); sal_Int32 nCharactersSkipped = pSkippedMediatype == NULL
? 0 : pSkippedMediatype-sURLPath.getStr();
INetContentTypeParameterList params; if (sURLPath.match(",", nCharactersSkipped))
sal_Unicode const * pSkippedMediatype = INetMIME::scanContentType( sURLPath.getStr(), sURLPath.getStr() + sURLPath.getLength(), &sType, &sSubType, &params ); {
sal_Int32 nCharactersSkipped = pSkippedMediatype-sURLPath.getStr(); nCharactersSkipped += strlen(",");
sal_Int32 nCommaIndex = sURLPath.indexOf( ",", nCharactersSkipped ); OString sURLEncodedData(
sal_Int32 nBase64Index = sURLPath.indexOf( sBase64Enc, nCharactersSkipped ); sURLPath.getStr() + nCharactersSkipped,
SvMemoryStream* aStream=NULL; sURLPath.getLength() - nCharactersSkipped,
RTL_TEXTENCODING_ISO_8859_1, OUSTRING_TO_OSTRING_CVTFLAGS);
if( nBase64Index >= 0 && nBase64Index < nCommaIndex ) return memoryStream(
sURLEncodedData.getStr(), sURLEncodedData.getLength());
}
else if (sURLPath.matchIgnoreAsciiCase(";base64,", nCharactersSkipped))
{ {
// base64 decoding nCharactersSkipped += strlen(";base64,");
OUString sBase64Data = sURLPath.copy( nBase64Index + sBase64Enc.getLength() ); OUString sBase64Data = sURLPath.copy( nCharactersSkipped );
css::uno::Sequence< sal_Int8 > aDecodedData; css::uno::Sequence< sal_Int8 > aDecodedData;
::sax::Converter::decodeBase64( aDecodedData, sBase64Data ); if (sax::Converter::decodeBase64SomeChars(aDecodedData, sBase64Data)
if( aDecodedData.hasElements() ) == sBase64Data.getLength())
{ {
aStream = new SvMemoryStream( aDecodedData.getArray(), aDecodedData.getLength(), STREAM_READ ); return memoryStream(
aDecodedData.getArray(), aDecodedData.getLength());
} }
} }
else return NULL;
{
// URL decoding
OUString sURLEncodedData = sURLPath.copy( nCommaIndex+1 );
aStream = new SvMemoryStream( const_cast< sal_Char * >(OUStringToOString(sURLEncodedData, RTL_TEXTENCODING_UTF8).getStr()), sURLEncodedData.getLength(), STREAM_READ);
}
return aStream;
} }
namespace unnamed_tools_urlobj { namespace unnamed_tools_urlobj {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment