You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by ca...@apache.org on 2008/01/17 19:18:32 UTC
svn commit: r612908 - in
/xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors:
Socket/UnixHTTPURLInputStream.cpp WinSock/BinHTTPURLInputStream.cpp
Author: cargilld
Date: Thu Jan 17 10:18:23 2008
New Revision: 612908
URL: http://svn.apache.org/viewvc?rev=612908&view=rev
Log:
support for http redirection for socket and winsock netaccessors
Modified:
xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp
xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp
Modified: xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp
URL: http://svn.apache.org/viewvc/xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp?rev=612908&r1=612907&r2=612908&view=diff
==============================================================================
--- xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp (original)
+++ xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/Socket/UnixHTTPURLInputStream.cpp Thu Jan 17 10:18:23 2008
@@ -150,6 +150,11 @@
chSpace, chDigit_2, chDigit_0, chDigit_0, chSpace, chNull
};
+ const char LOCATION[] =
+ {
+ chLatin_L, chLatin_o, chLatin_c, chLatin_a, chLatin_t, chLatin_i, chLatin_o, chLatin_n, chColon, chSpace, chNull
+ };
+
unsigned int charsEaten;
unsigned int transSize;
XMLTransService::Codes failReason;
@@ -172,7 +177,7 @@
//
char* hostNameAsCharStar = XMLString::transcode(hostName, fMemoryManager);
- ArrayJanitor<char> janBuf1(hostNameAsCharStar, fMemoryManager);
+ ArrayJanitor<char> janHostNameAsCharStar(hostNameAsCharStar, fMemoryManager);
//
// Convert all the parts of the urlSource object to ASCII so they can be
@@ -184,13 +189,15 @@
(
(transSize+1) * sizeof(char)
);//new char[transSize+1];
- ArrayJanitor<char> janBuf2(hostNameAsASCII, fMemoryManager);
+ ArrayJanitor<char> janHostNameAsASCII(hostNameAsASCII, fMemoryManager);
XMLTranscoder* trans = XMLPlatformUtils::fgTransService->makeNewTranscoderFor("ISO8859-1", failReason, blockSize, fMemoryManager);
+ Janitor<XMLTranscoder> janTranscoder(trans);
+
trans->transcodeTo(hostName, transSize, (unsigned char *) hostNameAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
char* pathAsASCII = 0;
- ArrayJanitor<char> janBuf3(pathAsASCII, fMemoryManager);
+ ArrayJanitor<char> janPathAsASCII(pathAsASCII, fMemoryManager);
if (path)
{
transSize = XMLString::stringLen(path)+1;
@@ -198,12 +205,12 @@
(
(transSize+1) * sizeof(char)
);//new char[transSize+1];
- janBuf3.reset(pathAsASCII, fMemoryManager);
+ janPathAsASCII.reset(pathAsASCII, fMemoryManager);
trans->transcodeTo(path, transSize, (unsigned char *) pathAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
}
char* fragmentAsASCII = 0;
- ArrayJanitor<char> janBuf4(fragmentAsASCII, fMemoryManager);
+ ArrayJanitor<char> janfragmentAsASCII(fragmentAsASCII, fMemoryManager);
if (fragment)
{
transSize = XMLString::stringLen(fragment)+1;
@@ -211,12 +218,12 @@
(
(transSize+1) * sizeof(char)
);//new char[transSize+1];
- janBuf4.reset(fragmentAsASCII, fMemoryManager);
+ janfragmentAsASCII.reset(fragmentAsASCII, fMemoryManager);
trans->transcodeTo(fragment, transSize, (unsigned char *) fragmentAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
}
char* queryAsASCII = 0;
- ArrayJanitor<char> janBuf5(queryAsASCII, fMemoryManager);
+ ArrayJanitor<char> janqueryAsASCII(queryAsASCII, fMemoryManager);
if (query)
{
transSize = XMLString::stringLen(query)+1;
@@ -224,7 +231,7 @@
(
(transSize+1) * sizeof(char)
);//new char[transSize+1];
- janBuf5.reset(queryAsASCII, fMemoryManager);
+ janqueryAsASCII.reset(queryAsASCII, fMemoryManager);
trans->transcodeTo(query, transSize, (unsigned char *) queryAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
}
@@ -240,10 +247,11 @@
(
(transSize+1) * sizeof(char)
);//new char[transSize+1];
- ArrayJanitor<char> janBuf6(portAsASCII, fMemoryManager);
+ ArrayJanitor<char> janportAsASCII(portAsASCII, fMemoryManager);
trans->transcodeTo(portBuffer, transSize, (unsigned char *) portAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
- delete trans;
+ const XMLCh* username = urlSource.getUser();
+ const XMLCh* password = urlSource.getPassword();
//
// Set up a socket.
@@ -251,197 +259,326 @@
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,
+ bool sawRedirect;
+ bool lookUpHost = true;
+ int redirectCount = 0;
+ int s;
+ SocketJanitor janSock(0);
+
+ do {
+ sawRedirect = false;
+
+ // Use the hostName in the local code page ....
+ if (lookUpHost &&
+ ((hostEntPtr = gethostbyname(hostNameAsCharStar)) == NULL))
+ {
+ unsigned long numAddress = inet_addr(hostNameAsCharStar);
+ if (numAddress < 0)
+ {
+ ThrowXMLwithMemMgr1(NetAccessorException,
XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager);
- }
- if ((hostEntPtr =
+ }
+ if ((hostEntPtr =
gethostbyaddr((char *) &numAddress,
sizeof(unsigned long), AF_INET)) == NULL)
- {
- ThrowXMLwithMemMgr1(NetAccessorException,
+ {
+ ThrowXMLwithMemMgr1(NetAccessorException,
XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager);
+ }
}
- }
+ lookUpHost = false;
- 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,
+ 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);
+
+ if (janSock.get())
+ janSock.release();
+ s = socket(hostEntPtr->h_addrtype, SOCK_STREAM, 0);
+ if (s < 0)
+ {
+ ThrowXMLwithMemMgr1(NetAccessorException,
XMLExcepts::NetAcc_CreateSocket, urlSource.getURLText(), fMemoryManager);
- }
- SocketJanitor janSock(&s);
+ }
+ janSock.reset(&s);
- if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) < 0)
- {
- ThrowXMLwithMemMgr1(NetAccessorException,
+ 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);
+ // 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);
+ }
- 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);
- strcat(fBuffer, (char*)authData);
- strcat(fBuffer, CRLF);
+ if (queryAsASCII != 0)
+ {
+ size_t n = strlen(fBuffer);
+ fBuffer[n] = chQuestion;
+ fBuffer[n+1] = chNull;
+ strcat(fBuffer, queryAsASCII);
}
- }
- if(httpInfo!=0 && httpInfo->fHeaders!=0)
- strncat(fBuffer,httpInfo->fHeaders,httpInfo->fHeadersLen);
+ if (fragmentAsASCII != 0)
+ {
+ strcat(fBuffer, fragmentAsASCII);
+ }
+ strcat(fBuffer, HTTP10);
- strcat(fBuffer, CRLF);
+ strcat(fBuffer, HOST);
+ strcat(fBuffer, hostNameAsASCII);
+ if (portNumber != 80)
+ {
+ strcat(fBuffer,COLON);
+ strcat(fBuffer,portAsASCII);
+ }
+ strcat(fBuffer, CRLF);
+
+ 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);
+ strcat(fBuffer, (char*)authData);
+ strcat(fBuffer, CRLF);
+ }
+ }
- // Send the http request
- int lent = strlen(fBuffer);
- int aLent = 0;
+ if(httpInfo!=0 && httpInfo->fHeaders!=0)
+ strncat(fBuffer,httpInfo->fHeaders,httpInfo->fHeadersLen);
- if ((aLent = write(s, (void *) fBuffer, lent)) != lent)
- {
- ThrowXMLwithMemMgr1(NetAccessorException,
- XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager);
- }
+ strcat(fBuffer, CRLF);
- if(httpInfo!=0 && httpInfo->fPayload!=0) {
+ // Send the http request
+ int lent = strlen(fBuffer);
int aLent = 0;
- if ((aLent = write(s, (void *) httpInfo->fPayload, httpInfo->fPayloadLen)) != httpInfo->fPayloadLen)
+
+ 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);
- }
+ //
+ // 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;
+ 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);
+ // 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 += 2;
- *(fBufferPos-1) = 0;
+ fBufferPos += 4;
+ *(fBufferPos-2) = 0;
}
else
- fBufferPos = fBufferEnd;
- }
+ {
+ 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);
- }
+ // 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);
- }
+ 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);
- }
+ if (memcmp(p, resp200, strlen(resp200)))
+ {
+ // a 3xx response means there was a HTTP redirect
+ if (p[1] = chDigit_3) {
+ sawRedirect = true;
+ redirectCount++;
+
+ p = strstr(fBuffer, LOCATION);
+ if (p == 0)
+ {
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager);
+ }
+ p += 10; // Length of string "Location: "
+
+ char* endP = strstr(p, CRLF);
+ if (endP == 0)
+ {
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager);
+ }
+ endP[0] = chNull;
+
+ XMLURL newURL(fMemoryManager);
+ XMLCh* newURLString = XMLString::transcode(p, fMemoryManager);
+ ArrayJanitor<XMLCh> janNewURLString(newURLString, fMemoryManager);
+
+ // The location string is either of the form:
+ // local.xsd (ie. a relative URL)
+ // http://host/path (ie. an absolute path)
+ char* colonP = strstr(p, COLON);
+ if (colonP == 0) {
+ // if no colon assume relative url
+ newURL.setURL(urlSource, newURLString);
+ }
+ else {
+ // if colon then either a schema is specified or
+ // a port is specified
+ newURL.setURL(newURLString);
+
+ if (newURL.getProtocol() != XMLURL::HTTP) {
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, newURL.getURLText(), fMemoryManager);
+ }
+
+ const XMLCh* newHostName = newURL.getHost();
+ if (XMLString::compareIStringASCII(hostName, newHostName) != 0) {
+ lookUpHost = true;
+ janHostNameAsCharStar.release();
+ hostNameAsCharStar = XMLString::transcode(newHostName, fMemoryManager);
+ janHostNameAsCharStar.reset(hostNameAsCharStar, fMemoryManager);
+
+ janHostNameAsASCII.release();
+ transSize = XMLString::stringLen(newHostName)+1;
+ hostNameAsASCII = (char*) fMemoryManager->allocate
+ (
+ (transSize+1) * sizeof(char)
+ );//new char[transSize+1];
+ janHostNameAsASCII.reset(hostNameAsASCII, fMemoryManager);
+ trans->transcodeTo(newHostName, transSize, (unsigned char *) hostNameAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
+ }
+ }
+
+ path = newURL.getPath();
+ if (path) {
+ janPathAsASCII.release();
+ transSize = XMLString::stringLen(path)+1;
+ pathAsASCII = (char*) fMemoryManager->allocate
+ (
+ (transSize+1) * sizeof(char)
+ );//new char[transSize+1];
+ janPathAsASCII.reset(pathAsASCII, fMemoryManager);
+ trans->transcodeTo(path, transSize, (unsigned char *) pathAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
+ }
+
+ fragment = newURL.getFragment();
+ if (fragment) {
+ janfragmentAsASCII.release();
+ transSize = XMLString::stringLen(fragment)+1;
+ fragmentAsASCII = (char*) fMemoryManager->allocate
+ (
+ (transSize+1) * sizeof(char)
+ );//new char[transSize+1];
+ janfragmentAsASCII.reset(fragmentAsASCII, fMemoryManager);
+ trans->transcodeTo(fragment, transSize, (unsigned char *) fragmentAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
+ }
+
+ query = newURL.getQuery();
+ if (query) {
+ janqueryAsASCII.release();
+ transSize = XMLString::stringLen(query)+1;
+ queryAsASCII = (char*) fMemoryManager->allocate
+ (
+ (transSize+1) * sizeof(char)
+ );//new char[transSize+1];
+ janqueryAsASCII.reset(queryAsASCII, fMemoryManager);
+ trans->transcodeTo(query, transSize, (unsigned char *) queryAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
+ }
+
+
+ portNumber = (unsigned short) newURL.getPortNum();
+ //
+ // Convert port number integer to unicode so we can transcode it to ASCII
+ //
+
+ janportAsASCII.release();
+ XMLString::binToText((unsigned int) portNumber, portBuffer, bufSize, 10, fMemoryManager);
+ transSize = XMLString::stringLen(portBuffer)+1;
+ portAsASCII = (char*) fMemoryManager->allocate
+ (
+ (transSize+1) * sizeof(char)
+ );//new char[transSize+1];
+ janportAsASCII.reset(portAsASCII, fMemoryManager);
+ trans->transcodeTo(portBuffer, transSize, (unsigned char *) portAsASCII, transSize, charsEaten, XMLTranscoder::UnRep_Throw);
+
+ username = newURL.getUser();
+ password = newURL.getPassword();
+ }
+ else {
+ // Most likely a 404 Not Found error.
+ // Should recognize and handle the forwarding responses.
+ //
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlSource.getURLText(), fMemoryManager);
+ }
+ }
+
+ }
+ while (sawRedirect && redirectCount < 6);
- fSocket = *janSock.release();
+ fSocket = *janSock.release();
}
Modified: xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp
URL: http://svn.apache.org/viewvc/xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp?rev=612908&r1=612907&r2=612908&view=diff
==============================================================================
--- xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp (original)
+++ xerces/c/branches/xerces-2/src/xercesc/util/NetAccessors/WinSock/BinHTTPURLInputStream.cpp Thu Jan 17 10:18:23 2008
@@ -249,27 +249,29 @@
// and transcode them back to ASCII.
//
const XMLCh* hostName = urlSource.getHost();
- char* hostNameAsCharStar = XMLString::transcode(hostName, urlSource.getMemoryManager());
- ArrayJanitor<char> janBuf1(hostNameAsCharStar, urlSource.getMemoryManager());
+ char* hostNameAsCharStar = XMLString::transcode(hostName, fMemoryManager);
+ ArrayJanitor<char> janHostName(hostNameAsCharStar, fMemoryManager);
const XMLCh* path = urlSource.getPath();
- char* pathAsCharStar = XMLString::transcode(path, urlSource.getMemoryManager());
- ArrayJanitor<char> janBuf2(pathAsCharStar, urlSource.getMemoryManager());
+ char* pathAsCharStar = XMLString::transcode(path, fMemoryManager);
+ ArrayJanitor<char> janPath(pathAsCharStar, fMemoryManager);
const XMLCh* fragment = urlSource.getFragment();
char* fragmentAsCharStar = 0;
if (fragment)
- fragmentAsCharStar = XMLString::transcode(fragment, urlSource.getMemoryManager());
- ArrayJanitor<char> janBuf3(fragmentAsCharStar, urlSource.getMemoryManager());
+ fragmentAsCharStar = XMLString::transcode(fragment, fMemoryManager);
+ ArrayJanitor<char> janFragment(fragmentAsCharStar, fMemoryManager);
const XMLCh* query = urlSource.getQuery();
char* queryAsCharStar = 0;
if (query)
- queryAsCharStar = XMLString::transcode(query, urlSource.getMemoryManager());
- ArrayJanitor<char> janBuf4(queryAsCharStar, urlSource.getMemoryManager());
+ queryAsCharStar = XMLString::transcode(query, fMemoryManager);
+ ArrayJanitor<char> janQuery(queryAsCharStar, fMemoryManager);
unsigned short portNumber = (unsigned short) urlSource.getPortNum();
+ const XMLCh* username = urlSource.getUser();
+ const XMLCh* password = urlSource.getPassword();
//
// Set up a socket.
//
@@ -277,222 +279,311 @@
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,
+ bool sawRedirect;
+ int redirectCount = 0;
+ SOCKET s;
+ SocketJanitor janSock(0);
+ bool lookUpHost = true;
+
+ do {
+
+ if (lookUpHost &&
+ ((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 =
+ }
+ if ((hostEntPtr =
gethostbyaddr((const char *) &numAddress,
sizeof(unsigned long), AF_INET)) == NULL)
- {
- // Call WSAGetLastError() to get the error number.
- ThrowXMLwithMemMgr1(NetAccessorException,
+ {
+ // Call WSAGetLastError() to get the error number.
+ ThrowXMLwithMemMgr1(NetAccessorException,
XMLExcepts::NetAcc_TargetResolution, hostName, fMemoryManager);
+ }
}
- }
+ lookUpHost = false;
- memcpy((void *) &sa.sin_addr,
+ memcpy((void *) &sa.sin_addr,
(const void *) hostEntPtr->h_addr, hostEntPtr->h_length);
- sa.sin_family = hostEntPtr->h_addrtype;
- sa.sin_port = htons(portNumber);
+ 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,
+ if (janSock.get())
+ janSock.release();
+ 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);
+ }
+ janSock.reset(&s);
- if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) == SOCKET_ERROR)
- {
+ if (connect(s, (struct sockaddr *) &sa, sizeof(sa)) == SOCKET_ERROR)
+ {
// Call WSAGetLastError() to get the error number.
- ThrowXMLwithMemMgr1(NetAccessorException,
+ 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;
+ // Set a flag so we know that the headers have not been read yet.
+ bool fHeaderRead = false;
+ sawRedirect = 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);
- }
+ strcat(fBuffer, pathAsCharStar);
- if (fragmentAsCharStar != 0)
- {
- strcat(fBuffer, fragmentAsCharStar);
- }
- strcat(fBuffer, " HTTP/1.0\r\n");
+ 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)
+ strcat(fBuffer, "Host: ");
+ strcat(fBuffer, hostNameAsCharStar);
+ if (portNumber != 80)
{
- // 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");
+ strcat(fBuffer, ":");
+ int i = strlen(fBuffer);
+ _itoa(portNumber, fBuffer+i, 10);
+ }
+ strcat(fBuffer, "\r\n");
+
+ 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");
+ if(httpInfo!=0 && httpInfo->fHeaders!=0)
+ strncat(fBuffer,httpInfo->fHeaders,httpInfo->fHeadersLen);
- // 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);
- }
+ strcat(fBuffer, "\r\n");
- if(httpInfo!=0 && httpInfo->fPayload!=0) {
+ // Send the http request
+ int lent = strlen(fBuffer);
int aLent = 0;
- if ((aLent = send(s, httpInfo->fPayload, httpInfo->fPayloadLen, 0)) != httpInfo->fPayloadLen)
+ if ((aLent = send(s, fBuffer, lent, 0)) != lent)
{
// Call WSAGetLastError() to get the error number.
ThrowXMLwithMemMgr1(NetAccessorException,
- XMLExcepts::NetAcc_WriteSocket, urlSource.getURLText(), fMemoryManager);
+ 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;
+ 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);
+ }
+ }
- 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)
+ // 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)
{
- fBufferPos += 4;
- *(fBufferPos-2) = 0;
- fHeaderRead = true;
+ // Call WSAGetLastError() to get the error number.
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager);
}
- else
- {
- fBufferPos = strstr(fBuffer, "\n\n");
+
+ 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 += 2;
- *(fBufferPos-1) = 0;
+ fBufferPos += 4;
+ *(fBufferPos-2) = 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)
+ fBufferPos = strstr(fBuffer, "\n\n");
+ if (fBufferPos != 0)
{
- // Call WSAGetLastError() to get the error number.
- ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager);
+ 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;
}
- 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);
}
- } 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);
+ }
- p = strchr(p, ' ');
- if (p == 0)
- {
- ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager);
- }
+ int httpResponse = atoi(p);
+ if (httpResponse != 200)
+ {
+ // a 3xx response means there was a HTTP redirect
+ if (httpResponse >= 300 && httpResponse < 400) {
+ sawRedirect = true;
+ redirectCount++;
- 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);
- }
+ p = strstr(fBuffer, "Location: ");
+ if (p == 0)
+ {
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager);
+ }
+ p += 10; // Length of string "Location: "
+
+ char* endP = strstr(p, "\r\n");
+ if (endP == 0)
+ {
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::NetAcc_ReadSocket, urlSource.getURLText(), fMemoryManager);
+ }
+ endP[0] = chNull;
+
+ XMLURL newURL(fMemoryManager);
+ XMLCh* newURLString = XMLString::transcode(p, fMemoryManager);
+ ArrayJanitor<XMLCh> janNewURLString(newURLString, fMemoryManager);
+
+ // The location string is either of the form:
+ // local.xsd (ie. a relative URL)
+ // http://host/path (ie. an absolute path)
+ char* colonP = strstr(p, ":");
+ if (colonP == 0) {
+ // if no colon assume relative url
+ newURL.setURL(urlSource, newURLString);
+ }
+ else {
+ // if colon then either a schema is specified or
+ // a port is specified
+ newURL.setURL(newURLString);
+
+ if (newURL.getProtocol() != XMLURL::HTTP) {
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, newURL.getURLText(), fMemoryManager);
+ }
+
+ const XMLCh* newHostName = newURL.getHost();
+ if (XMLString::compareIStringASCII(hostName, newHostName) != 0) {
+ lookUpHost = true;
+ janHostName.release();
+ hostNameAsCharStar = XMLString::transcode(newHostName, fMemoryManager);
+ janHostName.reset(hostNameAsCharStar);
+ }
+ }
+
+ path = newURL.getPath();
+ janPath.release();
+ pathAsCharStar = XMLString::transcode(path, fMemoryManager);
+ janPath.reset(pathAsCharStar);
+
+ fragment = newURL.getFragment();
+ janFragment.release();
+ fragmentAsCharStar = 0;
+ if (fragment)
+ fragmentAsCharStar = XMLString::transcode(fragment, fMemoryManager);
+ janFragment.reset(fragmentAsCharStar);
+
+ query = newURL.getQuery();
+ janQuery.release();
+ queryAsCharStar = 0;
+ if (query)
+ queryAsCharStar = XMLString::transcode(query, fMemoryManager);
+ janQuery.reset(queryAsCharStar);
+
+ portNumber = (unsigned short) newURL.getPortNum();
+ username = newURL.getUser();
+ password = newURL.getPassword();
+ }
+ else {
+ // Most likely a 404 Not Found error.
+ // Should recognize and handle the forwarding responses.
+ //
+ ThrowXMLwithMemMgr1(NetAccessorException, XMLExcepts::File_CouldNotOpenFile, urlSource.getURLText(), fMemoryManager);
+ }
+ }
+
+ }
+ while(sawRedirect && redirectCount <6);
+
fSocketHandle = (unsigned int) *janSock.release();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org