diff --git a/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp b/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp index 9bcaa28077f4633c981cc212ebae29935cf3011d..5f8a4afeeda97e67c4e34b2f381f5b62cd27e52c 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 e0bc3230c0a09fa394838addd7ee49ffaf28fed6..ade3f596e1e80055a4e1d9ca0a2b242cf72998c7 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 a967c63f994a9000204c553182a0d202a1d3f56a..9032f1adca608215c41de937b9d9d6846157e17b 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 857e37611e080e36f89ee4905d0ac25850d7e643..289519e209c0a4d613e8c00a63cdd2e2cb020da2 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 {