static bool checkBasicResult(const XMLURL& testURL , const BasicTestEntry& testInfo) { // // Check each part to insure that its what its supposed to be. Since // any of them can be a null pointer, we have a little helper function // that spits out the actual testing code for each one. // if (!checkAField(testURL.getURLText(), testInfo.fullText, L"Full Text")) return false; if (!checkAField(testURL.getFragment(), testInfo.fragment, L"Fragment")) return false; if (!checkAField(testURL.getHost(), testInfo.host, L"Host")) return false; if (testURL.getPortNum() != testInfo.portNum) { XERCES_STD_QUALIFIER wcout << L"Expected port number: " << testInfo.portNum << L" but got: " << testURL.getPortNum() << XERCES_STD_QUALIFIER endl; return false; } if (!checkAField(testURL.getPath(), testInfo.path, L"Path")) return false; if (!checkAField(testURL.getPassword(), testInfo.password, L"Password")) return false; if (!checkAField(testURL.getQuery(), testInfo.query, L"Query")) return false; if (!checkAField(testURL.getUser(), testInfo.user, L"User")) return false; return true; }
void BinHTTPInputStreamCommon::createHTTPRequest(const XMLURL &urlSource, const XMLNetHTTPInfo *httpInfo, CharBuffer &buffer) { static const char *GET = "GET "; static const char *PUT = "PUT "; static const char *POST = "POST "; static const char *HTTP10 = " HTTP/1.0\r\n"; static const char *HOST = "Host: "; static const char *AUTHORIZATION = "Authorization: Basic "; static const char *COLON = ":"; XMLTransService::Codes failReason; const XMLSize_t blockSize = 2048; XMLTranscoder* trans = XMLPlatformUtils::fgTransService->makeNewTranscoderFor("ISO8859-1", failReason, blockSize, fMemoryManager); Janitor<XMLTranscoder> janTrans(trans); TranscodeToStr hostName(urlSource.getHost(), trans, fMemoryManager); TranscodeToStr path(urlSource.getPath(), trans, fMemoryManager); TranscodeToStr fragment(urlSource.getFragment(), trans, fMemoryManager); TranscodeToStr query(urlSource.getQuery(), trans, fMemoryManager); // 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) { switch(httpInfo->fHTTPMethod) { case XMLNetHTTPInfo::GET: buffer.append(GET); break; case XMLNetHTTPInfo::PUT: buffer.append(PUT); break; case XMLNetHTTPInfo::POST: buffer.append(POST); break; } } else { buffer.append(GET); } if(path.str() != 0) { buffer.append((char*)path.str()); } else { buffer.append("/"); } if(query.str() != 0) { buffer.append("?"); buffer.append((char*)query.str()); } if(fragment.str() != 0) { buffer.append((char*)fragment.str()); } buffer.append(HTTP10); buffer.append(HOST); buffer.append((char*)hostName.str()); if(urlSource.getPortNum() != 80) { buffer.append(COLON); buffer.appendDecimalNumber(urlSource.getPortNum()); } buffer.append(CRLF); const XMLCh *username = urlSource.getUser(); const XMLCh *password = urlSource.getPassword(); if(username && password) { XMLBuffer userPassBuf(256, fMemoryManager); userPassBuf.append(username); userPassBuf.append(chColon); userPassBuf.append(password); TranscodeToStr userPass(userPassBuf.getRawBuffer(), trans, fMemoryManager); XMLSize_t len; XMLByte* encodedData = Base64::encode(userPass.str(), userPass.length(), &len, fMemoryManager); ArrayJanitor<XMLByte> janBuf2(encodedData, fMemoryManager); if(encodedData) { // HTTP doesn't want the 0x0A separating the data in chunks of 76 chars per line XMLByte* authData = (XMLByte*)fMemoryManager->allocate((len+1)*sizeof(XMLByte)); ArrayJanitor<XMLByte> janBuf(authData, fMemoryManager); XMLByte *cursor = authData; for(XMLSize_t i = 0; i < len; ++i) if(encodedData[i] != chLF) *cursor++ = encodedData[i]; *cursor++ = 0; buffer.append(AUTHORIZATION); buffer.append((char*)authData); buffer.append(CRLF); } } if(httpInfo && httpInfo->fHeaders) buffer.append(httpInfo->fHeaders, httpInfo->fHeadersLen); buffer.append(CRLF); }
BinHTTPURLInputStream::BinHTTPURLInputStream(const XMLURL& urlSource, const XMLNetHTTPInfo* httpInfo /*=0*/) : fSocketHandle(0) , fBytesProcessed(0) { if(!fInitialized) { if (!fInitMutex) { XMLMutex* tmpMutex = new XMLMutex(XMLPlatformUtils::fgMemoryManager); if (XMLPlatformUtils::compareAndSwap((void**)&fInitMutex, tmpMutex, 0)) { // Someone beat us to it, so let's clean up ours delete tmpMutex; } } XMLMutexLock lock(fInitMutex); if (!fInitialized) { Initialize(urlSource.getMemoryManager()); } } fMemoryManager = urlSource.getMemoryManager(); // // Pull all of the parts of the URL out of th urlSource object, and transcode them // and transcode them back to ASCII. // const XMLCh* hostName = urlSource.getHost(); char* hostNameAsCharStar = XMLString::transcode(hostName, urlSource.getMemoryManager()); ArrayJanitor<char> janBuf1(hostNameAsCharStar, urlSource.getMemoryManager()); const XMLCh* path = urlSource.getPath(); char* pathAsCharStar = XMLString::transcode(path, urlSource.getMemoryManager()); ArrayJanitor<char> janBuf2(pathAsCharStar, urlSource.getMemoryManager()); const XMLCh* fragment = urlSource.getFragment(); char* fragmentAsCharStar = 0; if (fragment) fragmentAsCharStar = XMLString::transcode(fragment, urlSource.getMemoryManager()); ArrayJanitor<char> janBuf3(fragmentAsCharStar, urlSource.getMemoryManager()); const XMLCh* query = urlSource.getQuery(); char* queryAsCharStar = 0; if (query) queryAsCharStar = XMLString::transcode(query, urlSource.getMemoryManager()); ArrayJanitor<char> janBuf4(queryAsCharStar, urlSource.getMemoryManager()); unsigned short portNumber = (unsigned short) urlSource.getPortNum(); // // Set up a socket. // struct hostent* hostEntPtr = 0; struct sockaddr_in sa; if ((hostEntPtr = gethostbyname(hostNameAsCharStar)) == NULL) { unsigned long numAddress = inet_addr(hostNameAsCharStar); if (numAddress == INADDR_NONE) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager); } if ((hostEntPtr = gethostbyaddr((const char *) &numAddress, sizeof(unsigned long), AF_INET)) == NULL) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager); } } memcpy((void *) &sa.sin_addr, (const void *) hostEntPtr->h_addr, hostEntPtr->h_length); sa.sin_family = hostEntPtr->h_addrtype; sa.sin_port = htons(portNumber); SOCKET s = socket(hostEntPtr->h_addrtype, SOCK_STREAM, 0); if (s == INVALID_SOCKET) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_CreateSocket, urlSource.getURLText(), fMemoryManager); } SocketJanitor janSock(&s); if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) == SOCKET_ERROR) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ConnSocket, urlSource.getURLText(), fMemoryManager); } // Set a flag so we know that the headers have not been read yet. bool fHeaderRead = false; // 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. memset(fBuffer, 0, sizeof(fBuffer)); 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; } } strcat(fBuffer, pathAsCharStar); if (queryAsCharStar != 0) { // Tack on a ? before the fragment strcat(fBuffer,"?"); strcat(fBuffer, queryAsCharStar); } if (fragmentAsCharStar != 0) { strcat(fBuffer, fragmentAsCharStar); } strcat(fBuffer, " HTTP/1.0\r\n"); strcat(fBuffer, "Host: "); strcat(fBuffer, hostNameAsCharStar); if (portNumber != 80) { strcat(fBuffer, ":"); int i = strlen(fBuffer); _itoa(portNumber, fBuffer+i, 10); } strcat(fBuffer, "\r\n"); const XMLCh* username = urlSource.getUser(); const XMLCh* password = urlSource.getPassword(); if (username && password) { XMLBuffer userPass(256, fMemoryManager); userPass.append(username); userPass.append(chColon); userPass.append(password); char* userPassAsCharStar = XMLString::transcode(userPass.getRawBuffer(), fMemoryManager); ArrayJanitor<char> janBuf(userPassAsCharStar, fMemoryManager); unsigned int len; XMLByte* encodedData = Base64::encode((XMLByte *)userPassAsCharStar, strlen(userPassAsCharStar), &len, fMemoryManager); ArrayJanitor<XMLByte> janBuf2(encodedData, fMemoryManager); if (encodedData) { // HTTP doesn't want the 0x0A separating the data in chunks of 76 chars per line XMLByte* authData = (XMLByte*)fMemoryManager->allocate((len+1)*sizeof(XMLByte)); ArrayJanitor<XMLByte> janBuf(authData, fMemoryManager); XMLByte* cursor=authData; for(unsigned int i=0;i<len;i++) if(encodedData[i]!=chLF) *cursor++=encodedData[i]; *cursor++=0; strcat(fBuffer, "Authorization: Basic "); strcat(fBuffer, (char*)authData); strcat(fBuffer, "\r\n"); } } if(httpInfo!=0 && httpInfo->fHeaders!=0) strncat(fBuffer,httpInfo->fHeaders,httpInfo->fHeadersLen); strcat(fBuffer, "\r\n"); // Send the http request int lent = strlen(fBuffer); int aLent = 0; if ((aLent = send(s, fBuffer, lent, 0)) != lent) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager); } if(httpInfo!=0 && httpInfo->fPayload!=0) { int aLent = 0; if ((aLent = send(s, httpInfo->fPayload, httpInfo->fPayloadLen, 0)) != httpInfo->fPayloadLen) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager); } } // // get the response, check the http header for errors from the server. // memset(fBuffer, 0, sizeof(fBuffer)); aLent = recv(s, fBuffer, sizeof(fBuffer)-1, 0); if (aLent == SOCKET_ERROR || aLent == 0) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } fBufferEnd = fBuffer+aLent; *fBufferEnd = 0; do { // 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, "\r\n\r\n"); if (fBufferPos != 0) { fBufferPos += 4; *(fBufferPos-2) = 0; fHeaderRead = true; } else { fBufferPos = strstr(fBuffer, "\n\n"); if (fBufferPos != 0) { fBufferPos += 2; *(fBufferPos-1) = 0; fHeaderRead = true; } else { // // Header is not yet read, do another recv() to get more data... aLent = recv(s, fBufferEnd, (sizeof(fBuffer) - 1) - (fBufferEnd - fBuffer), 0); if (aLent == SOCKET_ERROR || aLent == 0) { // Call WSAGetLastError() to get the error number. ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } fBufferEnd = fBufferEnd + aLent; *fBufferEnd = 0; } } } while(fHeaderRead == false); // 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, ' '); if (p == 0) { ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager); } int httpResponse = atoi(p); if (httpResponse != 200) { // Most likely a 404 Not Found error. // Should recognize and handle the forwarding responses. // ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlSource.getURLText(), fMemoryManager); } fSocketHandle = (unsigned int) *janSock.release(); }
XERCES_CPP_NAMESPACE_BEGIN CurlURLInputStream::CurlURLInputStream(const XMLURL& urlSource, const XMLNetHTTPInfo* httpInfo/*=0*/) : fMulti(0) , fEasy(0) , fMemoryManager(urlSource.getMemoryManager()) , fURLSource(urlSource) , fTotalBytesRead(0) , fWritePtr(0) , fBytesRead(0) , fBytesToRead(0) , fDataAvailable(false) , fBufferHeadPtr(fBuffer) , fBufferTailPtr(fBuffer) , fPayload(0) , fPayloadLen(0) , fContentType(0) { // 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()); // 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); // Add username and password if authentication is required const XMLCh *username = urlSource.getUser(); const XMLCh *password = urlSource.getPassword(); if(username && password) { XMLBuffer userPassBuf(256, fMemoryManager); userPassBuf.append(username); userPassBuf.append(chColon); userPassBuf.append(password); TranscodeToStr userPass(userPassBuf.getRawBuffer(), "ISO8859-1", fMemoryManager); curl_easy_setopt(fEasy, CURLOPT_HTTPAUTH, (long)CURLAUTH_ANY); curl_easy_setopt(fEasy, CURLOPT_USERPWD, (char*)userPass.str()); } if(httpInfo) { // Set the correct HTTP method switch(httpInfo->fHTTPMethod) { case XMLNetHTTPInfo::GET: break; case XMLNetHTTPInfo::PUT: curl_easy_setopt(fEasy, CURLOPT_UPLOAD, (long)1); break; case XMLNetHTTPInfo::POST: curl_easy_setopt(fEasy, CURLOPT_POST, (long)1); break; } // Add custom headers if(httpInfo->fHeaders) { struct curl_slist *headersList = 0; const char *headersBuf = httpInfo->fHeaders; const char *headersBufEnd = httpInfo->fHeaders + httpInfo->fHeadersLen; const char *headerStart = headersBuf; while(headersBuf < headersBufEnd) { if(*headersBuf == '\r' && (headersBuf + 1) < headersBufEnd && *(headersBuf + 1) == '\n') { XMLSize_t length = headersBuf - headerStart; ArrayJanitor<char> header((char*)fMemoryManager->allocate((length + 1) * sizeof(char)), fMemoryManager); memcpy(header.get(), headerStart, length); header.get()[length] = 0; headersList = curl_slist_append(headersList, header.get()); headersBuf += 2; headerStart = headersBuf; continue; } ++headersBuf; } curl_easy_setopt(fEasy, CURLOPT_HTTPHEADER, headersList); curl_slist_free_all(headersList); } // Set up the payload if(httpInfo->fPayload) { fPayload = httpInfo->fPayload; fPayloadLen = httpInfo->fPayloadLen; curl_easy_setopt(fEasy, CURLOPT_READDATA, this); curl_easy_setopt(fEasy, CURLOPT_READFUNCTION, staticReadCallback); curl_easy_setopt(fEasy, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fPayloadLen); } } // Add easy handle to the multi stack curl_multi_add_handle(fMulti, fEasy); // Start reading, to get the content type while(fBufferHeadPtr == fBuffer) { int runningHandles = 0; readMore(&runningHandles); if(runningHandles == 0) break; } // Find the content type char *contentType8 = 0; curl_easy_getinfo(fEasy, CURLINFO_CONTENT_TYPE, &contentType8); if(contentType8) fContentType = TranscodeFromStr((XMLByte*)contentType8, XMLString::stringLen(contentType8), "ISO8859-1", fMemoryManager).adopt(); }