Skip to content
Snippets Groups Projects
Commit f413e174 authored by Boris Kolpackov's avatar Boris Kolpackov
Browse files

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
parent 5c788bbf
No related branches found
No related tags found
No related merge requests found
......@@ -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
......@@ -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);
......
......@@ -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 {
......
......@@ -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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment