From 44057a75ef001bfd1919e2169ec3f7f355d14c20 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 16 Apr 2009 23:04:35 +0000 Subject: Hey, the list code is better, also, I fixed some stuff in socket (the isBlocking function was backward), and fastcgi is actually working now! Also added comparison functions to FString. --- src/fastcgi.cpp | 123 ++++++++++++++++++++++++++++++++++++++++++++--------- src/fastcgi.h | 4 ++ src/fbasicstring.h | 32 ++++++++++++++ src/list.h | 90 ++++++++++++++++++++++++++++----------- src/socket.cpp | 4 +- 5 files changed, 206 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/fastcgi.cpp b/src/fastcgi.cpp index aba3b71..9a602de 100644 --- a/src/fastcgi.cpp +++ b/src/fastcgi.cpp @@ -50,13 +50,17 @@ bool Bu::FastCgi::isEmbedded() void Bu::FastCgi::read( Bu::Socket &s, Bu::FastCgi::Record &r ) { - s.read( &r, sizeof(Record) ); + int iRead = s.read( &r, sizeof(Record) ); + if( iRead != sizeof(Record) ) + throw Bu::SocketException("Hey, the size %d is wrong for Record. (%s)", + iRead, strerror( errno ) ); r.uRequestId = ntohs( r.uRequestId ); r.uContentLength = ntohs( r.uContentLength ); } void Bu::FastCgi::write( Bu::Socket &s, Bu::FastCgi::Record r ) { + sio << "Out -> " << r << sio.nl; r.uRequestId = htons( r.uRequestId ); r.uContentLength = htons( r.uContentLength ); s.write( &r, sizeof(Record) ); @@ -111,8 +115,30 @@ void Bu::FastCgi::readPair( Bu::Socket &s, StrHash &hParams, uint16_t &uRead ) } } +bool Bu::FastCgi::hasChannel( int iChan ) +{ + if( aChannel.getSize() < iChan ) + return false; + if( aChannel[iChan-1] == NULL ) + return false; + return true; +} + +Bu::Formatter &Bu::operator<<( Bu::Formatter &f, const Bu::FastCgi::Record &r ) +{ + f << "[Ver=" << (uint32_t)r.uVersion << + ", Type=" << (uint32_t)r.uType << + ", Req=" << (uint32_t)r.uRequestId << + ", clen=" << (uint32_t)r.uContentLength << + ", plen=" << (uint32_t)r.uPaddingLength << + ", resv=" << (uint32_t)r.uReserved << + "]"; + return f; +} + void Bu::FastCgi::run() { + sio << "sizeof(Bu::FastCgi::Record) = " << sizeof(Record) << sio.nl; bRunning = true; while( bRunning ) { @@ -122,21 +148,49 @@ void Bu::FastCgi::run() Bu::Socket s( iSock ); s.setBlocking( true ); + sio << "Got connection, blocking? " << s.isBlocking() << sio.nl; try { for(;;) { Record r; - read( s, r ); - while( aChannel.getSize() < r.uRequestId ) - aChannel.append( NULL ); + memset( &r, 0, sizeof(r) ); +// try +// { + read( s, r ); +// } +// catch( Bu::ExceptionBase &e ) +// { +// sio << "Error: " << e.what() << ", " << s.isOpen() << +// sio.nl; +// continue; +// } Channel *pChan = NULL; if( r.uRequestId > 0 ) - pChan = aChannel[r.uRequestId-1]; + { + if( !hasChannel( r.uRequestId ) && + r.uType != typeBeginRequest ) + { + sio << "Error, stream data without stream." << sio.nl; + sio << r << sio.nl; + if( r.uContentLength > 0 ) + { + char *buf = new char[r.uContentLength]; + sio << " (read " << s.read( buf, r.uContentLength ) + << "/" << r.uContentLength << "):" << sio.nl; + sio.write( buf, r.uContentLength ); + sio << sio.nl << sio.nl; + } + } + while( aChannel.getSize() < r.uRequestId ) + aChannel.append( NULL ); + if( r.uRequestId > 0 ) + pChan = aChannel[r.uRequestId-1]; + } sio << "Record (id=" << r.uRequestId << ", len=" << r.uContentLength << ", pad=" << - (int)r.uPaddingLength << "): "; + (unsigned int)r.uPaddingLength << "): "; fflush( stdout ); switch( (RequestType)r.uType ) @@ -180,9 +234,16 @@ void Bu::FastCgi::run() else { char *buf = new char[r.uContentLength]; - sio << " (read " << s.read( buf, r.uContentLength ) - << ")"; - pChan->sStdIn.append( buf, r.uContentLength ); + int iTotal = 0; + do + { + size_t iRead = s.read( + buf, r.uContentLength-iTotal ); + iTotal += iRead; + sio << " (read " << iRead << " " << iTotal + << "/" << r.uContentLength << ")"; + pChan->sStdIn.append( buf, iRead ); + } while( iTotal < r.uContentLength ); delete[] buf; } break; @@ -219,6 +280,7 @@ void Bu::FastCgi::run() { if( pChan->uFlags == chflgAllDone ) { + sio << "All done, generating output." << sio.nl; Bu::MemBuf mStdOut, mStdErr; int iRet = request( pChan->hParams, pChan->sStdIn, @@ -229,27 +291,47 @@ void Bu::FastCgi::run() Bu::FString &sStdErr = mStdErr.getString(); Record rOut; + memset( &rOut, 0, sizeof(rOut) ); rOut.uVersion = 1; rOut.uRequestId = r.uRequestId; rOut.uPaddingLength = 0; + rOut.uType = typeStdOut; if( sStdOut.getSize() > 0 ) { - rOut.uType = typeStdOut; - rOut.uContentLength = sStdOut.getSize(); - write( s, rOut ); - s.write( sStdOut ); + for( int iPos = 0; iPos < sStdOut.getSize(); + iPos += 65528 ) + { + int iSize = sStdOut.getSize()-iPos; + if( iSize > 65528 ) + iSize = 65528; + rOut.uContentLength = iSize; + write( s, rOut ); + int iAmnt = s.write( + sStdOut.getStr()+iPos, iSize ); + sio << "Wrote " << iAmnt << + " of " << iSize << sio.nl; + } } - rOut.uType = typeStdOut; rOut.uContentLength = 0; write( s, rOut ); + + rOut.uType = typeStdErr; if( sStdErr.getSize() > 0 ) { - rOut.uType = typeStdErr; - rOut.uContentLength = sStdErr.getSize(); - write( s, rOut ); - s.write( sStdOut ); + for( int iPos = 0; iPos < sStdErr.getSize(); + iPos += 65528 ) + { + int iSize = sStdErr.getSize()-iPos; + if( iSize > 65528 ) + iSize = 65528; + rOut.uContentLength = iSize; + write( s, rOut ); + int iAmnt = s.write( + sStdErr.getStr()+iPos, iSize ); + sio << "Wrote " << iAmnt << + " of " << iSize << sio.nl; + } } - rOut.uType = typeStdErr; rOut.uContentLength = 0; write( s, rOut ); @@ -270,7 +352,8 @@ void Bu::FastCgi::run() } catch( Bu::SocketException &e ) { - //sio << "Bu::SocketException: " << e.what() << sio.nl; + sio << "Bu::SocketException: " << e.what() << sio.nl << + "\tSocket open: " << s.isOpen() << sio.nl; } } } diff --git a/src/fastcgi.h b/src/fastcgi.h index 83e9b83..67ec82f 100644 --- a/src/fastcgi.h +++ b/src/fastcgi.h @@ -110,11 +110,15 @@ namespace Bu void write( Bu::Socket &s, Record r ); void write( Bu::Socket &s, EndRequestBody b ); + bool hasChannel( int iChan ); + private: Bu::ServerSocket *pSrv; bool bRunning; Bu::Array aChannel; }; + + Bu::Formatter &operator<<( Bu::Formatter &f, const Bu::FastCgi::Record &r ); }; #endif diff --git a/src/fbasicstring.h b/src/fbasicstring.h index 853625c..57e798e 100644 --- a/src/fbasicstring.h +++ b/src/fbasicstring.h @@ -1297,6 +1297,38 @@ namespace Bu return !(*this == pData); } + bool operator<(const MyType &pData ) const + { + flatten(); + pData.flatten(); + + const chr *a = pData.pFirst->pData; + chr *b = pFirst->pData; + for( long j = 0; j < nLength; j++, a++, b++ ) + { + if( *a != *b ) + return *a < *b; + } + + return false; + } + + bool operator>(const MyType &pData ) const + { + flatten(); + pData.flatten(); + + const chr *a = pData.pFirst->pData; + chr *b = pFirst->pData; + for( long j = 0; j < nLength; j++, a++, b++ ) + { + if( *a != *b ) + return *a > *b; + } + + return false; + } + /** * Indexing operator *@param nIndex (long) The index of the character you want. diff --git a/src/list.h b/src/list.h index f7a6d2f..93116e7 100644 --- a/src/list.h +++ b/src/list.h @@ -45,6 +45,9 @@ namespace Bu typedef class List MyType; public: + struct const_iterator; + struct iterator; + List() : pFirst( NULL ), pLast( NULL ), @@ -201,6 +204,32 @@ namespace Bu } } + void insert( MyType::iterator &i, const value &v ) + { + Link *pAfter = i.pLink; + if( pAfter == NULL ) + { + append( v ); + return; + } + Link *pPrev = pAfter->pPrev; + if( pPrev == NULL ) + { + prepend( v ); + return; + } + + Link *pNew = la.allocate( 1 ); + pNew->pValue = va.allocate( 1 ); + va.construct( pNew->pValue, v ); + nSize++; + + pNew->pNext = pAfter; + pNew->pPrev = pPrev; + pAfter->pPrev = pNew; + pPrev->pNext = pNew; + } + /** * Insert a new item in sort order by searching for the first item that * is larger and inserting this before it, or at the end if none are @@ -250,7 +279,6 @@ namespace Bu } } - struct const_iterator; /** * An iterator to iterate through your list. */ @@ -260,24 +288,30 @@ namespace Bu friend class List; private: Link *pLink; - MyType &rList; + MyType *pList; bool bOffFront; - iterator( MyType &rList ) : + iterator( MyType *pList ) : pLink( NULL ), - rList( rList ) + pList( pList ) { } - iterator( Link *pLink, MyType &rList ) : + iterator( Link *pLink, MyType *pList ) : pLink( pLink ), - rList( rList ) + pList( pList ) { } public: + iterator() : + pLink( NULL ), + pList( NULL ) + { + } + iterator( const iterator &i ) : pLink( i.pLink ), - rList( i.rList ) + pList( i.pList ) { } @@ -342,7 +376,7 @@ namespace Bu iterator &operator++() { if( pLink == NULL ) - pLink = (bOffFront)?(rList.pFirst):(NULL); + pLink = (bOffFront)?(pList->pFirst):(NULL); else pLink = pLink->pNext; bOffFront = false; @@ -352,7 +386,7 @@ namespace Bu iterator &operator--() { if( pLink == NULL ) - pLink = (bOffFront)?(NULL):(rList.pLast); + pLink = (bOffFront)?(NULL):(pList->pLast); else pLink = pLink->pPrev; bOffFront = true; @@ -362,7 +396,7 @@ namespace Bu iterator &operator++( int ) { if( pLink == NULL ) - pLink = (bOffFront)?(rList.pFirst):(NULL); + pLink = (bOffFront)?(pList->pFirst):(NULL); else pLink = pLink->pNext; bOffFront = false; @@ -372,7 +406,7 @@ namespace Bu iterator &operator--( int ) { if( pLink == NULL ) - pLink = (bOffFront)?(NULL):(rList.pLast); + pLink = (bOffFront)?(NULL):(pList->pLast); else pLink = pLink->pPrev; bOffFront = true; @@ -409,24 +443,30 @@ namespace Bu friend class List; private: Link *pLink; - const MyType &rList; + const MyType *pList; bool bOffFront; - const_iterator( const MyType &rList ) : + const_iterator( const MyType *pList ) : pLink( NULL ), - rList( rList ) + pList( pList ) { } - const_iterator( Link *pLink, const MyType &rList ) : + const_iterator( Link *pLink, const MyType *pList ) : pLink( pLink ), - rList( rList ) + pList( pList ) { } public: + const_iterator() : + pLink( NULL ), + pList( NULL ) + { + } + const_iterator( const iterator &i ) : pLink( i.pLink ), - rList( i.rList ) + pList( i.pList ) { } @@ -463,7 +503,7 @@ namespace Bu const_iterator &operator++() { if( pLink == NULL ) - pLink = (bOffFront)?(rList.pFirst):(NULL); + pLink = (bOffFront)?(pList->pFirst):(NULL); else pLink = pLink->pNext; bOffFront = false; @@ -473,7 +513,7 @@ namespace Bu const_iterator &operator--() { if( pLink == NULL ) - pLink = (bOffFront)?(NULL):(rList.pLast); + pLink = (bOffFront)?(NULL):(pList->pLast); else pLink = pLink->pPrev; bOffFront = true; @@ -483,7 +523,7 @@ namespace Bu const_iterator &operator++( int ) { if( pLink == NULL ) - pLink = (bOffFront)?(rList.pFirst):(NULL); + pLink = (bOffFront)?(pList->pFirst):(NULL); else pLink = pLink->pNext; bOffFront = false; @@ -493,7 +533,7 @@ namespace Bu const_iterator &operator--( int ) { if( pLink == NULL ) - pLink = (bOffFront)?(NULL):(rList.pLast); + pLink = (bOffFront)?(NULL):(pList->pLast); else pLink = pLink->pPrev; bOffFront = true; @@ -529,7 +569,7 @@ namespace Bu */ iterator begin() { - return iterator( pFirst, *this ); + return iterator( pFirst, this ); } /** @@ -538,7 +578,7 @@ namespace Bu */ const_iterator begin() const { - return const_iterator( pFirst, *this ); + return const_iterator( pFirst, this ); } /** @@ -548,7 +588,7 @@ namespace Bu */ const iterator end() { - return iterator( NULL, *this ); + return iterator( NULL, this ); } /** @@ -558,7 +598,7 @@ namespace Bu */ const const_iterator end() const { - return const_iterator( NULL, *this ); + return const_iterator( NULL, this ); } /** diff --git a/src/socket.cpp b/src/socket.cpp index a660b30..c01c63a 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -369,7 +369,7 @@ bool Bu::Socket::isSeekable() bool Bu::Socket::isBlocking() { #ifndef WIN32 - return ((fcntl( nSocket, F_GETFL, 0 ) & O_NONBLOCK) == O_NONBLOCK); + return ((fcntl( nSocket, F_GETFL, 0 ) & O_NONBLOCK) != O_NONBLOCK); #else return false; #endif @@ -380,7 +380,7 @@ void Bu::Socket::setBlocking( bool bBlocking ) #ifndef WIN32 if( bBlocking ) { - fcntl( nSocket, F_SETFL, fcntl( nSocket, F_GETFL, 0 ) & ~O_NONBLOCK ); + fcntl( nSocket, F_SETFL, fcntl( nSocket, F_GETFL, 0 ) & (~O_NONBLOCK) ); } else { -- cgit v1.2.3