You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by jb...@apache.org on 2005/05/31 23:45:08 UTC

svn commit: r179286 [2/2] - in /xerces/c/branches/jberry/3.0-unstable: ./ m4/ src/ src/xercesc/util/ src/xercesc/util/NetAccessors/Curl/

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp
URL: http://svn.apache.org/viewcvs/xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp?rev=179286&view=auto
==============================================================================
--- xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp (added)
+++ xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp Tue May 31 14:45:06 2005
@@ -0,0 +1,71 @@
+/*
+ * Copyright 1999-2000,2004 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * $Id$
+ */
+
+
+#if !defined(CURLNETACCESSOR_HPP)
+#define CURLNETACCESSOR_HPP
+
+
+#include <xercesc/util/XercesDefs.hpp>
+#include <xercesc/util/XMLURL.hpp>
+#include <xercesc/util/BinInputStream.hpp>
+#include <xercesc/util/XMLNetAccessor.hpp>
+
+XERCES_CPP_NAMESPACE_BEGIN
+
+//
+// This class is the wrapper for the socket based code which
+// provides the ability to fetch a resource specified using
+// a HTTP or FTP URL.
+//
+
+class XMLUTIL_EXPORT CurlNetAccessor : public XMLNetAccessor
+{
+public :
+    CurlNetAccessor();
+    ~CurlNetAccessor();
+    
+    virtual BinInputStream* makeNew(const XMLURL&  urlSource, const XMLNetHTTPInfo* httpInfo=0);
+    virtual const XMLCh* getId() const;
+
+    virtual void initCurl(void);
+    virtual void cleanupCurl(void);
+
+private :
+	static int fgCurlInitCount;
+    static const XMLCh fgMyName[];
+
+    CurlNetAccessor(const CurlNetAccessor&);
+    CurlNetAccessor& operator=(const CurlNetAccessor&);
+
+}; // CurlNetAccessor
+
+
+inline const XMLCh* CurlNetAccessor::getId() const
+{
+    return fgMyName;
+}
+
+
+XERCES_CPP_NAMESPACE_END
+
+#endif // CURLNETACCESSOR_HPP
+
+

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.loT
URL: http://svn.apache.org/viewcvs/xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.loT?rev=179286&view=auto
==============================================================================
--- xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.loT (added)
+++ xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlNetAccessor.loT Tue May 31 14:45:06 2005
@@ -0,0 +1,7 @@
+# xercesc/util/NetAccessors/Curl/CurlNetAccessor.lo - a libtool object file
+# Generated by ltmain.sh - GNU libtool 1.5.14 (1.1220.2.195 2005/02/12 12:12:33)
+#
+# Please DO NOT delete this file!
+# It is necessary for linking the library.
+
+# Name of the PIC object.

Added: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp
URL: http://svn.apache.org/viewcvs/xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp?rev=179286&view=auto
==============================================================================
--- xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp (added)
+++ xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp Tue May 31 14:45:06 2005
@@ -0,0 +1,245 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/time.h>
+
+#include <xercesc/util/XercesDefs.hpp>
+#include <xercesc/util/XMLNetAccessor.hpp>
+#include <xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp>
+#include <xercesc/util/XMLString.hpp>
+#include <xercesc/util/XMLExceptMsgs.hpp>
+#include <xercesc/util/Janitor.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
+#include <xercesc/util/TransService.hpp>
+#include <xercesc/util/TranscodingException.hpp>
+#include <xercesc/util/PlatformUtils.hpp>
+
+XERCES_CPP_NAMESPACE_BEGIN
+
+
+CurlURLInputStream::CurlURLInputStream(const XMLURL& urlSource, const XMLNetHTTPInfo* httpInfo/*=0*/)
+      : fMulti(0)
+      , fEasy(0)
+      , fMemoryManager(urlSource.getMemoryManager())
+      , fURLSource(urlSource)
+      , fURL(0)
+      , fTotalBytesRead(0)
+      , fWritePtr(0)
+      , fBytesRead(0)
+      , fBytesToRead(0)
+      , fDataAvailable(false)
+      , fBufferHeadPtr(fBuffer)
+      , fBufferTailPtr(fBuffer)
+{
+	// Allocate the curl multi handle
+	fMulti = curl_multi_init();
+	
+	// Allocate the curl easy handle
+	fEasy = curl_easy_init();
+	
+	// Get the text of the URL we're going to use
+	fURL.reset(XMLString::transcode(fURLSource.getURLText(), fMemoryManager), fMemoryManager);
+
+	//printf("Curl trying to fetch %s\n", fURL.get());
+
+	// Set URL option
+	curl_easy_setopt(fEasy, CURLOPT_URL, fURL.get());
+	curl_easy_setopt(fEasy, CURLOPT_WRITEDATA, this);						// Pass this pointer to write function
+	curl_easy_setopt(fEasy, CURLOPT_WRITEFUNCTION, staticWriteCallback);	// Our static write function
+	
+	// Add easy handle to the multi stack
+	curl_multi_add_handle(fMulti, fEasy);
+}
+
+
+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);
+}
+
+
+size_t
+CurlURLInputStream::staticWriteCallback(char *buffer,
+                                      size_t size,
+                                      size_t nitems,
+                                      void *outstream)
+{
+	return ((CurlURLInputStream*)outstream)->writeCallback(buffer, size, nitems);
+}
+
+
+
+size_t
+CurlURLInputStream::writeCallback(char *buffer,
+                                      size_t size,
+                                      size_t nitems)
+{
+	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);
+	fWritePtr		+= consume;
+	fBytesRead		+= consume;
+	fTotalBytesRead	+= consume;
+	fBytesToRead	-= consume;
+
+	//printf("write callback consuming %d bytes\n", consume);
+
+	// If bytes remain, rebuffer as many as possible into our holding buffer
+	buffer			+= consume;
+	totalConsumed	+= consume;
+	cnt				-= consume;
+	if (cnt > 0)
+	{
+		XMLSize_t bufAvail = sizeof(fBuffer) - (fBufferHeadPtr - fBuffer);
+		consume = (cnt > bufAvail) ? bufAvail : cnt;
+		memcpy(fBufferHeadPtr, buffer, consume);
+		fBufferHeadPtr	+= consume;
+		buffer			+= consume;
+		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
+	// is a logic error somewhere here.
+	return totalConsumed;
+}
+
+
+
+unsigned int
+CurlURLInputStream::readBytes(XMLByte* const    toFill
+                                     , const unsigned int    maxToRead)
+{
+	fBytesRead = 0;
+	fBytesToRead = maxToRead;
+	fWritePtr = toFill;
+	
+	for (bool tryAgain = true; fBytesToRead > 0 && (tryAgain || fBytesRead == 0); )
+	{
+		// First, any buffered data we have available
+		XMLSize_t bufCnt = fBufferHeadPtr - fBufferTailPtr;
+		bufCnt = (bufCnt > fBytesToRead) ? fBytesToRead : bufCnt;
+		if (bufCnt > 0)
+		{
+			memcpy(fWritePtr, fBufferTailPtr, bufCnt);
+			fWritePtr		+= bufCnt;
+			fBytesRead		+= bufCnt;
+			fTotalBytesRead	+= bufCnt;
+			fBytesToRead	-= bufCnt;
+			
+			fBufferTailPtr	+= bufCnt;
+			if (fBufferTailPtr == fBufferHeadPtr)
+				fBufferHeadPtr = fBufferTailPtr = fBuffer;
+				
+			//printf("consuming rebuffered %d bytes\n", bufCnt);
+
+			tryAgain = true;
+			continue;
+		}
+	
+		int runningHandles = 0;
+		CURLMcode curlResult = curl_multi_perform(fMulti, &runningHandles);
+		tryAgain = (curlResult == CURLM_CALL_MULTI_PERFORM);
+		
+		// Process messages from curl
+		int msgsInQueue = 0;
+		for (CURLMsg* msg = NULL; (msg = curl_multi_info_read(fMulti, &msgsInQueue)) != NULL; )
+		{
+			//printf("msg %d, %d from curl\n", msg->msg, msg->data.result);
+
+			if (msg->msg != CURLMSG_DONE)
+				continue;
+				
+			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);
+                break;
+                
+            case CURLE_COULDNT_CONNECT:
+                ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ConnSocket, fURLSource.getURLText(), fMemoryManager);
+            	
+            case CURLE_RECV_ERROR:
+                ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, fURLSource.getURLText(), fMemoryManager);
+                break;
+
+            default:
+                ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_InternalError, fURLSource.getURLText(), fMemoryManager);
+				break;
+			}
+		}
+		
+		// If nothing is running any longer, bail out
+		if (runningHandles == 0)
+			break;
+		
+		// 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 (!tryAgain && fBytesRead == 0)
+		{
+			fd_set readSet[16];
+			fd_set writeSet[16];
+			fd_set exceptSet[16];
+			int fdcnt = 16;
+			
+			// As curl for the file descriptors to wait on
+			(void) curl_multi_fdset(fMulti, readSet, writeSet, exceptSet, &fdcnt);
+			
+			// Wait on the file descriptors
+			timeval tv;
+			tv.tv_sec  = 2;
+			tv.tv_usec = 0;
+			(void) select(fdcnt, readSet, writeSet, exceptSet, &tv);
+		}
+	}
+	
+	return fBytesRead;
+}
+
+
+XERCES_CPP_NAMESPACE_END
+

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.cpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp
URL: http://svn.apache.org/viewcvs/xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp?rev=179286&view=auto
==============================================================================
--- xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp (added)
+++ xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp Tue May 31 14:45:06 2005
@@ -0,0 +1,120 @@
+/*
+ * Copyright 1999-2000,2004 The Apache Software Foundation.
+ * 
+ * Licensed 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.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/*
+ * $Id$
+ */
+
+#if !defined(CURLURLINPUTSTREAM_HPP)
+#define CURLURLINPUTSTREAM_HPP
+
+#include <curl.h>
+#include <multi.h>
+#include <easy.h>
+
+#include <xercesc/util/XMLURL.hpp>
+#include <xercesc/util/XMLExceptMsgs.hpp>
+#include <xercesc/util/Janitor.hpp>
+#include <xercesc/util/BinInputStream.hpp>
+#include <xercesc/util/XMLNetAccessor.hpp>
+
+XERCES_CPP_NAMESPACE_BEGIN
+
+//
+// This class implements the BinInputStream interface specified by the XML
+// parser.
+//
+
+class XMLUTIL_EXPORT CurlURLInputStream : public BinInputStream
+{
+public :
+    CurlURLInputStream(const XMLURL&  urlSource, const XMLNetHTTPInfo* httpInfo=0);
+    ~CurlURLInputStream();
+
+    unsigned int curPos() const;
+    unsigned int readBytes
+    (
+                XMLByte* const  toFill
+        , const unsigned int    maxToRead
+    );
+
+
+private :
+    // -----------------------------------------------------------------------
+    //  Unimplemented constructors and operators
+    // -----------------------------------------------------------------------
+    CurlURLInputStream(const CurlURLInputStream&);
+    CurlURLInputStream& operator=(const CurlURLInputStream&);
+    
+    static size_t staticWriteCallback(char *buffer,
+                                      size_t size,
+                                      size_t nitems,
+                                      void *outstream);
+    size_t writeCallback(			  char *buffer,
+                                      size_t size,
+                                      size_t nitems);
+
+
+    // -----------------------------------------------------------------------
+    //  Private data members
+    //
+    //  fSocket
+    //      The socket representing the connection to the remote file.
+    //  fBytesProcessed
+    //      Its a rolling count of the number of bytes processed off this
+    //      input stream.
+    //  fBuffer
+    //      Holds the http header, plus the first part of the actual
+    //      data.  Filled at the time the stream is opened, data goes
+    //      out to user in response to readBytes().
+    //  fBufferPos, fBufferEnd
+    //      Pointers into fBuffer, showing start and end+1 of content
+    //      that readBytes must return.
+    // -----------------------------------------------------------------------
+	
+    CURLM*				fMulti;
+    CURL*				fEasy;
+    
+    MemoryManager*      fMemoryManager;
+    
+    XMLURL				fURLSource;
+    ArrayJanitor<char>	fURL;
+    
+    unsigned long       fTotalBytesRead;
+    XMLByte*			fWritePtr;
+    unsigned long		fBytesRead;
+    unsigned long		fBytesToRead;
+    bool				fDataAvailable;
+    
+    // Overflow buffer for when curl writes more data to us
+    // than we've asked for.
+    XMLByte				fBuffer[CURL_MAX_WRITE_SIZE];
+    XMLByte*			fBufferHeadPtr;
+    XMLByte*			fBufferTailPtr;
+    
+}; // CurlURLInputStream
+
+
+inline unsigned int
+CurlURLInputStream::curPos() const
+{
+    return fTotalBytesRead;
+}
+
+XERCES_CPP_NAMESPACE_END
+
+#endif // CURLURLINPUTSTREAM_HPP
+

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.hpp
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.loT
URL: http://svn.apache.org/viewcvs/xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.loT?rev=179286&view=auto
==============================================================================
--- xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.loT (added)
+++ xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/NetAccessors/Curl/CurlURLInputStream.loT Tue May 31 14:45:06 2005
@@ -0,0 +1,7 @@
+# xercesc/util/NetAccessors/Curl/CurlURLInputStream.lo - a libtool object file
+# Generated by ltmain.sh - GNU libtool 1.5.14 (1.1220.2.195 2005/02/12 12:12:33)
+#
+# Please DO NOT delete this file!
+# It is necessary for linking the library.
+
+# Name of the PIC object.

Modified: xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/PlatformUtils.cpp
URL: http://svn.apache.org/viewcvs/xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/PlatformUtils.cpp?rev=179286&r1=179285&r2=179286&view=diff
==============================================================================
--- xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/PlatformUtils.cpp (original)
+++ xerces/c/branches/jberry/3.0-unstable/src/xercesc/util/PlatformUtils.cpp Tue May 31 14:45:06 2005
@@ -76,6 +76,9 @@
 #endif
 
 #include <xercesc/util/XMLNetAccessor.hpp>
+#if XERCES_USE_NETACCESSOR_CURL
+#	include <xercesc/util/NetAccessors/Curl/CurlNetAccessor.hpp>
+#endif
 #if XERCES_USE_NETACCESSOR_SOCKET
 #	include <xercesc/util/NetAccessors/Socket/SocketNetAccessor.hpp>
 #endif
@@ -380,7 +383,9 @@
 {
 	XMLNetAccessor* na = 0;
 	
-	#if defined (XERCES_USE_NETACCESSOR_SOCKET)
+	#if defined (XERCES_USE_NETACCESSOR_CURL)
+		na = new CurlNetAccessor();
+	#elif defined (XERCES_USE_NETACCESSOR_SOCKET)
 		na = new SocketNetAccessor();
 	#elif defined (XERCES_USE_NETACCESSOR_LIBWWW)
 		na = new LibWWWNetAccessor();



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org