unixhttpurlinputstream.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 497 行 · 第 1/2 页
CPP
497 行
char* fragmentAsASCII = 0; ArrayJanitor<char> janBuf4(fragmentAsASCII, fMemoryManager); if (fragment) { transSize = XMLString::stringLen(fragment)+1; fragmentAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; trans->transcodeTo(fragment, transSize, (unsigned char *) fragmentAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); } char* queryAsASCII = 0; ArrayJanitor<char> janBuf5(queryAsASCII, fMemoryManager); if (query) { transSize = XMLString::stringLen(query)+1; queryAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; trans->transcodeTo(query, transSize, (unsigned char *) queryAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); } unsigned short portNumber = (unsigned short) urlSource.getPortNum(); // // Convert port number integer to unicode so we can transcode it to ASCII // XMLString::binToText((unsigned int) portNumber, portBuffer, bufSize, 10, fMemoryManager); transSize = XMLString::stringLen(portBuffer)+1; char* portAsASCII = (char*) fMemoryManager->allocate ( (transSize+1) * sizeof(char) );//new char[transSize+1]; trans->transcodeTo(portBuffer, transSize, (unsigned char *) portAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw); delete trans; // // Set up a socket. // struct hostent* hostEntPtr = 0; struct sockaddr_in sa; // Use the hostName in the local code page .... if ((hostEntPtr = gethostbyname(hostNameAsCharStar)) == NULL) { unsigned long numAddress = inet_addr(hostNameAsCharStar); if (numAddress < 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager); } if ((hostEntPtr = gethostbyaddr((char *) &numAddress, sizeof(unsigned long), AF_INET)) == NULL) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager); } } memset(&sa, '\0', sizeof(sockaddr_in)); // iSeries fix ?? memcpy((void *) &sa.sin_addr, (const void *) hostEntPtr->h_addr, hostEntPtr->h_length); sa.sin_family = hostEntPtr->h_addrtype; sa.sin_port = htons(portNumber); int s = socket(hostEntPtr->h_addrtype, SOCK_STREAM, 0); if (s < 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_CreateSocket, urlSource.getURLText(), fMemoryManager); } if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ConnSocket, urlSource.getURLText(), fMemoryManager); } // The port is open and ready to go. // Build up the http GET command to send to the server. // To do: We should really support http 1.1. This implementation // is weak. if(httpInfo==0) strcpy(fBuffer, GET); else switch(httpInfo->fHTTPMethod) { case XMLNetHTTPInfo::GET: strcpy(fBuffer, GET); break; case XMLNetHTTPInfo::PUT: strcpy(fBuffer, PUT); break; case XMLNetHTTPInfo::POST: strcpy(fBuffer, POST); break; } if (pathAsASCII != 0) { strcat(fBuffer, pathAsASCII); } if (queryAsASCII != 0) { size_t n = strlen(fBuffer); fBuffer[n] = chQuestion; fBuffer[n+1] = chNull; strcat(fBuffer, queryAsASCII); } if (fragmentAsASCII != 0) { strcat(fBuffer, fragmentAsASCII); } strcat(fBuffer, HTTP10); strcat(fBuffer, HOST); strcat(fBuffer, hostNameAsASCII); if (portNumber != 80) { strcat(fBuffer,COLON); strcat(fBuffer,portAsASCII); } strcat(fBuffer, CRLF); if(httpInfo!=0 && httpInfo->fHeaders!=0) strncat(fBuffer,httpInfo->fHeaders,httpInfo->fHeadersLen); strcat(fBuffer, CRLF); // Send the http request int lent = strlen(fBuffer); int aLent = 0; if ((aLent = write(s, (void *) fBuffer, lent)) != lent) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager); } if(httpInfo!=0 && httpInfo->fPayload!=0) { int aLent = 0; if ((aLent = write(s, (void *) httpInfo->fPayload, httpInfo->fPayloadLen)) != httpInfo->fPayloadLen) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager); } } // // get the response, check the http header for errors from the server. // aLent = read(s, (void *)fBuffer, sizeof(fBuffer)-1); if (aLent <= 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } fBufferEnd = fBuffer+aLent; *fBufferEnd = 0; // Find the break between the returned http header and any data. // (Delimited by a blank line) // Hang on to any data for use by the first read from this BinHTTPURLInputStream. // fBufferPos = strstr(fBuffer, CRLF2X); if (fBufferPos != 0) { fBufferPos += 4; *(fBufferPos-2) = 0; } else { fBufferPos = strstr(fBuffer, LF2X); if (fBufferPos != 0) { fBufferPos += 2; *(fBufferPos-1) = 0; } else fBufferPos = fBufferEnd; } // Make sure the header includes an HTTP 200 OK response. // char *p = strstr(fBuffer, HTTP); if (p == 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } p = strchr(p, chSpace); if (p == 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } if (memcmp(p, resp200, strlen(resp200))) { // Most likely a 404 Not Found error. // Should recognize and handle the forwarding responses. // ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlSource.getURLText(), fMemoryManager); } fSocket = s;}UnixHTTPURLInputStream::~UnixHTTPURLInputStream(){ shutdown(fSocket, 2); close(fSocket);}unsigned int UnixHTTPURLInputStream::readBytes(XMLByte* const toFill , const unsigned int maxToRead){ unsigned int len = fBufferEnd - fBufferPos; if (len > 0) { // If there's any data left over in the buffer into which we first // read from the server (to get the http header), return that. if (len > maxToRead) len = maxToRead; memcpy(toFill, fBufferPos, len); fBufferPos += len; } else { // There was no data in the local buffer. // Read some from the socket, straight into our caller's buffer. // len = read(fSocket, (void *) toFill, maxToRead); if (len == -1) { ThrowXMLwithMemMgr(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, fMemoryManager); } } fBytesProcessed += len; return len;}XERCES_CPP_NAMESPACE_END
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?