binhttpurlinputstream.cpp

来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 552 行 · 第 1/2 页

CPP
552
字号
/* * 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. *//* * $Log: BinHTTPURLInputStream.cpp,v $ * Revision 1.9  2004/09/08 13:56:36  peiyongz * Apache License Version 2.0 * * Revision 1.8  2004/05/11 13:39:35  amassari * The net accessor input source now can be used to get data using PUT or POST, as well as GET * * Revision 1.7  2003/12/17 13:58:02  cargilld * Platform update for memory management so that the static memory manager (one * used to call Initialize) is only for static data. * * Revision 1.6  2003/12/17 00:18:37  cargilld * Update to memory management so that the static memory manager (one used to call Initialize) is only for static data. * * Revision 1.5  2003/12/02 14:12:20  amassari * Make the code compilable on Windows when UNICODE is defined (bug#16055) * * Revision 1.4  2003/05/17 05:54:18  knoaman * Update NetAccessors to use the memory manager. * * Revision 1.3  2002/12/06 16:43:55  tng * Fix the error messages thrown from net accessor module. * * Revision 1.2  2002/11/04 15:11:39  tng * C++ Namespace Support. * * Revision 1.1.1.1  2002/02/01 22:22:23  peiyongz * sane_include * * Revision 1.12  2001/12/13 21:16:52  peiyongz * Fix: Invalid Argument to FreeLibrary (Hint: 0x0000000) * * Revision 1.11  2001/10/25 16:26:39  tng * [Bug 4213] BinHTTPURLInputStream initialisation not thread safe.  By Mark Weaver. * * Revision 1.10  2001/10/25 16:10:46  tng * [Bug 4121] BinHTTPUrlInputStream needds to read entire HTTP header. By John Clayton. * * Revision 1.9  2001/10/24 20:17:54  tng * [Bug 3813] BinHTTPURLInputStream has weak HTTP request capabilities.  By Kevin Philips. * * Revision 1.8  2001/10/24 20:03:03  tng * [Bug 2305] Include <stdlib.h> to BinHTTPURLInputStream.cpp.  By Peter A. Volchek. * * Revision 1.7  2001/09/04 17:52:57  peiyongz * Bugzilla# 3170: patch from Kevin Philips to handle Query in XMLURL. * * Revision 1.6  2001/01/22 16:43:38  tng * Loads winsock dynamically.  Fixed by Curt Arnold. * Winsock2 is not initialized unless an http URL is used.    If an http * URL is used and the Winsock 2 DLL is not installed, then an NetAccessor * initialization exception is thrown. * * Revision 1.5  2000/07/21 03:22:44  andyh * Improved (but still weak) http access by the parser. * Windows only.  UNIX will follow, probably tomorrow. * * Revision 1.4  2000/05/15 22:31:29  andyh * Replace #include<memory.h> with <string.h> everywhere. * * Revision 1.3  2000/03/24 00:32:15  rahulj * Connect to the port specified in the URL, rather than the default one. * * Revision 1.2  2000/03/22 00:21:10  rahulj * Now we throw exceptions when errors occur. * Simplified the code, based on the assumption that * the calling function will make sure that the buffer into * which the data has to be read is large enough. * * Revision 1.1  2000/03/17 02:37:54  rahulj * First cut at adding HTTP capability via native sockets. * Still need to add: *   error handling capability, ports other than 80, *   escaped URL's * Will add options in project file only when I am done with these * above changes. * */#define INCL_WINSOCK_API_TYPEDEFS 1#include <winsock2.h>#include <windows.h>#include <tchar.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <xercesc/util/PlatformUtils.hpp>#include <xercesc/util/XMLNetAccessor.hpp>#include <xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.hpp>#include <xercesc/util/XMLString.hpp>#include <xercesc/util/XMLExceptMsgs.hpp>#include <xercesc/util/Janitor.hpp>#include <xercesc/util/XMLUniDefs.hpp>XERCES_CPP_NAMESPACE_BEGINHMODULE gWinsockLib = NULL;LPFN_GETHOSTBYNAME gWSgethostbyname = NULL;LPFN_INET_ADDR gWSinet_addr = NULL;LPFN_GETHOSTBYADDR gWSgethostbyaddr = NULL;LPFN_HTONS gWShtons = NULL;LPFN_SOCKET gWSsocket = NULL;LPFN_CONNECT gWSconnect = NULL;LPFN_SEND gWSsend = NULL;LPFN_RECV gWSrecv = NULL;LPFN_SHUTDOWN gWSshutdown = NULL;LPFN_CLOSESOCKET gWSclosesocket = NULL;LPFN_WSACLEANUP gWSACleanup = NULL;bool BinHTTPURLInputStream::fInitialized = false;XMLMutex* BinHTTPURLInputStream::fInitMutex = 0;void BinHTTPURLInputStream::Initialize(MemoryManager* const manager) {    //    // Initialize the WinSock library here.    //    WORD        wVersionRequested;    WSADATA     wsaData;	LPFN_WSASTARTUP startup = NULL;	if(gWinsockLib == NULL) {		gWinsockLib = LoadLibrary(_T("WSOCK32"));		if(gWinsockLib == NULL) {			ThrowXMLwithMemMgr(NetAccessorException, XMLExcepts::NetAcc_InitFailed, manager);		}		else {			startup = (LPFN_WSASTARTUP) GetProcAddress(gWinsockLib,"WSAStartup");			gWSACleanup = (LPFN_WSACLEANUP) GetProcAddress(gWinsockLib,"WSACleanup");			gWSgethostbyname = (LPFN_GETHOSTBYNAME) GetProcAddress(gWinsockLib,"gethostbyname");			gWSinet_addr = (LPFN_INET_ADDR) GetProcAddress(gWinsockLib,"inet_addr");			gWSgethostbyaddr = (LPFN_GETHOSTBYADDR) GetProcAddress(gWinsockLib,"gethostbyaddr");			gWShtons = (LPFN_HTONS) GetProcAddress(gWinsockLib,"htons");			gWSsocket = (LPFN_SOCKET) GetProcAddress(gWinsockLib,"socket");			gWSconnect = (LPFN_CONNECT) GetProcAddress(gWinsockLib,"connect");			gWSsend = (LPFN_SEND) GetProcAddress(gWinsockLib,"send");			gWSrecv = (LPFN_RECV) GetProcAddress(gWinsockLib,"recv");			gWSshutdown = (LPFN_SHUTDOWN) GetProcAddress(gWinsockLib,"shutdown");			gWSclosesocket = (LPFN_CLOSESOCKET) GetProcAddress(gWinsockLib,"closesocket");			if(startup == NULL ||				gWSACleanup == NULL ||				gWSgethostbyname == NULL ||				gWSinet_addr == NULL ||				gWSgethostbyaddr == NULL ||				gWShtons == NULL ||				gWSsocket == NULL ||				gWSconnect == NULL ||				gWSsend == NULL ||				gWSrecv == NULL ||				gWSshutdown == NULL ||				gWSclosesocket == NULL)			{				gWSACleanup = NULL;				Cleanup();				ThrowXMLwithMemMgr(NetAccessorException, XMLExcepts::NetAcc_InitFailed, manager);			}		}	}    wVersionRequested = MAKEWORD( 2, 2 );    int err = (*startup)(wVersionRequested, &wsaData);    if (err != 0)    {        // Call WSAGetLastError() to get the last error.        ThrowXMLwithMemMgr(NetAccessorException, XMLExcepts::NetAcc_InitFailed, manager);    }    fInitialized = true;}void BinHTTPURLInputStream::Cleanup() {	if(fInitialized)	{		if(gWSACleanup) (*gWSACleanup)();		gWSACleanup = NULL;		FreeLibrary(gWinsockLib);		gWinsockLib = NULL;		gWSgethostbyname = NULL;		gWSinet_addr = NULL;		gWSgethostbyaddr = NULL;		gWShtons = NULL;		gWSsocket = NULL;		gWSconnect = NULL;		gWSsend = NULL;		gWSrecv = NULL;		gWSshutdown = NULL;		gWSclosesocket = NULL;      fInitialized = false;      delete fInitMutex;      fInitMutex = 0;	}}hostent* BinHTTPURLInputStream::gethostbyname(const char* name){	return (*gWSgethostbyname)(name);}unsigned long BinHTTPURLInputStream::inet_addr(const char* cp){	return (*gWSinet_addr)(cp);}hostent* BinHTTPURLInputStream::gethostbyaddr(const char* addr,int len,int type){	return (*gWSgethostbyaddr)(addr,len,type);}unsigned short BinHTTPURLInputStream::htons(unsigned short hostshort){	return (*gWShtons)(hostshort);}unsigned short BinHTTPURLInputStream::socket(int af,int type,int protocol){	return (*gWSsocket)(af,type,protocol);}int BinHTTPURLInputStream::connect(unsigned short s,const sockaddr* name,int namelen){	return (*gWSconnect)(s,name,namelen);}int BinHTTPURLInputStream::send(unsigned short s,const char* buf,int len,int flags){	return (*gWSsend)(s,buf,len,flags);}int BinHTTPURLInputStream::recv(unsigned short s,char* buf,int len,int flags){	return (*gWSrecv)(s,buf,len,flags);}int BinHTTPURLInputStream::shutdown(unsigned int s,int how){	return (*gWSshutdown)(s,how);}int BinHTTPURLInputStream::closesocket(unsigned int socket){	return (*gWSclosesocket)(socket);}BinHTTPURLInputStream::BinHTTPURLInputStream(const XMLURL& urlSource, const XMLNetHTTPInfo* httpInfo /*=0*/)      : fSocketHandle(0)      , fBytesProcessed(0){    if(!fInitialized)    {        if (!fInitMutex)        {            XMLMutex* tmpMutex = new XMLMutex();            if (XMLPlatformUtils::compareAndSwap((void**)&fInitMutex, tmpMutex, 0))            {                // Someone beat us to it, so let's clean up ours

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?