From f413e1746401477f79bf094d4401cd99a5fdedce Mon Sep 17 00:00:00 2001 From: Boris Kolpackov <borisk@apache.org> Date: Wed, 21 Apr 2010 14:20:51 +0000 Subject: [PATCH] Check that we have non-NULL host before trying to connect (XERCESC-1920). git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@936317 13f79535-47bb-0310-9956-ffa450edef68 --- .../NetAccessors/Curl/CurlURLInputStream.cpp | 48 ++++++++++--------- .../URLAccessCFBinInputStream.cpp | 7 ++- .../Socket/UnixHTTPURLInputStream.cpp | 16 ++++++- .../WinSock/BinHTTPURLInputStream.cpp | 16 ++++++- 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp b/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp index 9bcaa2807..5f8a4afee 100644 --- a/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp +++ b/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp @@ -5,9 +5,9 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -71,10 +71,10 @@ CurlURLInputStream::CurlURLInputStream(const XMLURL& urlSource, const XMLNetHTTP { // Allocate the curl multi handle fMulti = curl_multi_init(); - + // Allocate the curl easy handle fEasy = curl_easy_init(); - + // Set URL option TranscodeToStr url(fURLSource.getURLText(), "ISO8859-1", fMemoryManager); curl_easy_setopt(fEasy, CURLOPT_URL, (char*)url.str()); @@ -82,7 +82,7 @@ CurlURLInputStream::CurlURLInputStream(const XMLURL& urlSource, const XMLNetHTTP // Set up a way to recieve the data curl_easy_setopt(fEasy, CURLOPT_WRITEDATA, this); // Pass this pointer to write function curl_easy_setopt(fEasy, CURLOPT_WRITEFUNCTION, staticWriteCallback); // Our static write function - + // Do redirects curl_easy_setopt(fEasy, CURLOPT_FOLLOWLOCATION, (long)1); curl_easy_setopt(fEasy, CURLOPT_MAXREDIRS, (long)6); @@ -178,10 +178,10 @@ CurlURLInputStream::~CurlURLInputStream() { // Remove the easy handle from the multi stack curl_multi_remove_handle(fMulti, fEasy); - + // Cleanup the easy handle curl_easy_cleanup(fEasy); - + // Cleanup the multi handle curl_multi_cleanup(fMulti); @@ -214,7 +214,7 @@ CurlURLInputStream::writeCallback(char *buffer, { XMLSize_t cnt = size * nitems; XMLSize_t totalConsumed = 0; - + // Consume as many bytes as possible immediately into the buffer XMLSize_t consume = (cnt > fBytesToRead) ? fBytesToRead : cnt; memcpy(fWritePtr, buffer, consume); @@ -239,7 +239,7 @@ CurlURLInputStream::writeCallback(char *buffer, totalConsumed += consume; //printf("write callback rebuffering %d bytes\n", consume); } - + // Return the total amount we've consumed. If we don't consume all the bytes // then an error will be generated. Since our buffer size is equal to the // maximum size that curl will write, this should never happen unless there @@ -267,7 +267,7 @@ bool CurlURLInputStream::readMore(int *runningHandles) { // Ask the curl to do some work CURLMcode curlResult = curl_multi_perform(fMulti, runningHandles); - + // Process messages from curl int msgsInQueue = 0; for (CURLMsg* msg = NULL; (msg = curl_multi_info_read(fMulti, &msgsInQueue)) != NULL; ) @@ -276,26 +276,31 @@ bool CurlURLInputStream::readMore(int *runningHandles) if (msg->msg != CURLMSG_DONE) return true; - + switch (msg->data.result) { case CURLE_OK: // We completed successfully. runningHandles should have dropped to zero, so we'll bail out below... break; - + case CURLE_UNSUPPORTED_PROTOCOL: ThrowXMLwithMemMgr(MalformedURLException, XMLExcepts::URL_UnsupportedProto, fMemoryManager); break; case CURLE_COULDNT_RESOLVE_HOST: case CURLE_COULDNT_RESOLVE_PROXY: - ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, fURLSource.getHost(), fMemoryManager); + { + if (fURLSource.getHost()) + ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, fURLSource.getHost(), fMemoryManager); + else + ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, fURLSource.getURLText(), fMemoryManager); break; - + } + case CURLE_COULDNT_CONNECT: ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ConnSocket, fURLSource.getURLText(), fMemoryManager); break; - + case CURLE_RECV_ERROR: ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, fURLSource.getURLText(), fMemoryManager); break; @@ -309,7 +314,7 @@ bool CurlURLInputStream::readMore(int *runningHandles) // If nothing is running any longer, bail out if(*runningHandles == 0) return false; - + // If there is no further data to read, and we haven't // read any yet on this invocation, call select to wait for data if (curlResult != CURLM_CALL_MULTI_PERFORM && fBytesRead == 0) @@ -343,7 +348,7 @@ CurlURLInputStream::readBytes(XMLByte* const toFill fBytesRead = 0; fBytesToRead = maxToRead; fWritePtr = toFill; - + for (bool tryAgain = true; fBytesToRead > 0 && (tryAgain || fBytesRead == 0); ) { // First, any buffered data we have available @@ -356,21 +361,21 @@ CurlURLInputStream::readBytes(XMLByte* const toFill fBytesRead += bufCnt; fTotalBytesRead += bufCnt; fBytesToRead -= bufCnt; - + fBufferTailPtr += bufCnt; if (fBufferTailPtr == fBufferHeadPtr) fBufferHeadPtr = fBufferTailPtr = fBuffer; - + //printf("consuming %d buffered bytes\n", bufCnt); tryAgain = true; continue; } - + // Ask the curl to do some work int runningHandles = 0; tryAgain = readMore(&runningHandles); - + // If nothing is running any longer, bail out if (runningHandles == 0) break; @@ -385,4 +390,3 @@ const XMLCh *CurlURLInputStream::getContentType() const } XERCES_CPP_NAMESPACE_END - diff --git a/src/xercesc/util/NetAccessors/MacOSURLAccessCF/URLAccessCFBinInputStream.cpp b/src/xercesc/util/NetAccessors/MacOSURLAccessCF/URLAccessCFBinInputStream.cpp index e0bc3230c..ade3f596e 100644 --- a/src/xercesc/util/NetAccessors/MacOSURLAccessCF/URLAccessCFBinInputStream.cpp +++ b/src/xercesc/util/NetAccessors/MacOSURLAccessCF/URLAccessCFBinInputStream.cpp @@ -100,8 +100,13 @@ URLAccessCFBinInputStream::URLAccessCFBinInputStream(const XMLURL& urlSource) break; case kCFURLRemoteHostUnavailableError: - ThrowXML1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, urlSource.getHost()); + { + if (urlSource.getHost()) + ThrowXML1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, urlSource.getHost()); + else + ThrowXML1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlText); break; + } case kCFURLUnknownError: ThrowXML1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlText); diff --git a/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp b/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp index a967c63f9..9032f1adc 100644 --- a/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp +++ b/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp @@ -104,6 +104,13 @@ UnixHTTPURLInputStream::UnixHTTPURLInputStream(const XMLURL& urlSource, const XM MemoryManager *memoryManager = urlSource.getMemoryManager(); const XMLCh* hostName = urlSource.getHost(); + + if (hostName == 0) + ThrowXMLwithMemMgr1(NetAccessorException, + XMLExcepts::File_CouldNotOpenFile, + urlSource.getURLText(), + memoryManager); + char* hostNameAsCharStar = XMLString::transcode(hostName, memoryManager); ArrayJanitor<char> janHostNameAsCharStar(hostNameAsCharStar, memoryManager); @@ -217,9 +224,16 @@ UnixHTTPURLInputStream::UnixHTTPURLInputStream(const XMLURL& urlSource, const XM } url = newURL; + hostName = newURL.getHost(); + + if (hostName == 0) + ThrowXMLwithMemMgr1(NetAccessorException, + XMLExcepts::File_CouldNotOpenFile, + newURL.getURLText(), + memoryManager); janHostNameAsCharStar.release(); - hostNameAsCharStar = XMLString::transcode(newURL.getHost(), memoryManager); + hostNameAsCharStar = XMLString::transcode(hostName, memoryManager); janHostNameAsCharStar.reset(hostNameAsCharStar, memoryManager); } else { diff --git a/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp b/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp index 857e37611..289519e20 100644 --- a/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp +++ b/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp @@ -312,6 +312,13 @@ BinHTTPURLInputStream::BinHTTPURLInputStream(const XMLURL& urlSource, const XMLN // and transcode them back to ASCII. // const XMLCh* hostName = urlSource.getHost(); + + if (hostName == 0) + ThrowXMLwithMemMgr1(NetAccessorException, + XMLExcepts::File_CouldNotOpenFile, + urlSource.getURLText(), + memoryManager); + char* hostNameAsCharStar = XMLString::transcode(hostName, memoryManager); ArrayJanitor<char> janHostNameAsCharStar(hostNameAsCharStar, memoryManager); @@ -437,9 +444,16 @@ BinHTTPURLInputStream::BinHTTPURLInputStream(const XMLURL& urlSource, const XMLN } url = newURL; + hostName = newURL.getHost(); + + if (hostName == 0) + ThrowXMLwithMemMgr1(NetAccessorException, + XMLExcepts::File_CouldNotOpenFile, + newURL.getURLText(), + memoryManager); janHostNameAsCharStar.release(); - hostNameAsCharStar = XMLString::transcode(newURL.getHost(), memoryManager); + hostNameAsCharStar = XMLString::transcode(hostName, memoryManager); janHostNameAsCharStar.reset(hostNameAsCharStar, memoryManager); } else { -- GitLab