From 915005e218b5d00939b548de65ce6354f7acb487 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 28 Jul 2023 21:18:56 -0700 Subject: Completely redesigned Server and Client. Like, seriously, they're almost completely different. --- src/experimental/fastcgi.cpp | 81 ++++++++++++++++++++++---------------------- 1 file changed, 40 insertions(+), 41 deletions(-) (limited to 'src/experimental/fastcgi.cpp') diff --git a/src/experimental/fastcgi.cpp b/src/experimental/fastcgi.cpp index ac946b0..7e91bd1 100644 --- a/src/experimental/fastcgi.cpp +++ b/src/experimental/fastcgi.cpp @@ -24,14 +24,14 @@ Bu::FastCgi::FastCgi() : pSrv( NULL ), bRunning( true ) { - pSrv = new Bu::TcpServerSocket( (Bu::TcpServerSocket::socket_t)STDIN_FILENO, false ); + pSrv = new Bu::ServerSocketTcp( (Bu::ServerSocketTcp::socket_t)STDIN_FILENO, false ); } Bu::FastCgi::FastCgi( int iPort ) : pSrv( NULL ), bRunning( true ) { - pSrv = new Bu::TcpServerSocket( iPort ); + pSrv = new Bu::ServerSocketTcp( iPort ); } Bu::FastCgi::~FastCgi() @@ -64,63 +64,63 @@ bool Bu::FastCgi::isEmbedded() #endif } -void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::Record &r ) +void Bu::FastCgi::read( Bu::Socket *pSock, Bu::FastCgi::Record &r ) { - int iRead = s.read( &r, sizeof(Record) ); + int iRead = pSock->read( &r, sizeof(Record) ); if( iRead != sizeof(Record) ) - throw Bu::TcpSocketException("Hey, the size %d is wrong for Record. (%s)", + throw Bu::ExceptionBase("Hey, the size %d is wrong for Record. (%pSock)", iRead, strerror( errno ) ); r.uRequestId = ntohs( r.uRequestId ); r.uContentLength = ntohs( r.uContentLength ); } -void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::Record r ) +void Bu::FastCgi::write( Bu::Socket *pSock, Bu::FastCgi::Record r ) { // sio << "Out -> " << r << sio.nl; r.uRequestId = htons( r.uRequestId ); r.uContentLength = htons( r.uContentLength ); - s.write( &r, sizeof(Record) ); + pSock->write( &r, sizeof(Record) ); } -void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::BeginRequestBody &b ) +void Bu::FastCgi::read( Bu::Socket *pSock, Bu::FastCgi::BeginRequestBody &b ) { - s.read( &b, sizeof(BeginRequestBody) ); + pSock->read( &b, sizeof(BeginRequestBody) ); b.uRole = ntohs( b.uRole ); } -void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::EndRequestBody b ) +void Bu::FastCgi::write( Bu::Socket *pSock, Bu::FastCgi::EndRequestBody b ) { b.uStatus = htonl( b.uStatus ); - s.write( &b, sizeof(b) ); + pSock->write( &b, sizeof(b) ); } -uint32_t Bu::FastCgi::readLen( Bu::TcpSocket &s, uint16_t &uRead ) +uint32_t Bu::FastCgi::readLen( Bu::Socket *pSock, uint16_t &uRead ) { uint8_t uByte[4]; - s.read( uByte, 1 ); + pSock->read( uByte, 1 ); uRead++; if( uByte[0] >> 7 == 0 ) return uByte[0]; - s.read( uByte+1, 3 ); + pSock->read( uByte+1, 3 ); uRead += 3; return ((uByte[0]&0x7f)<<24)|(uByte[1]<<16)|(uByte[2]<<8)|(uByte[3]); } -void Bu::FastCgi::readPair( Bu::TcpSocket &s, StrHash &hParams, uint16_t &uRead ) +void Bu::FastCgi::readPair( Bu::Socket *pSock, StrHash &hParams, uint16_t &uRead ) { - uint32_t uName = readLen( s, uRead ); - uint32_t uValue = readLen( s, uRead ); + uint32_t uName = readLen( pSock, uRead ); + uint32_t uValue = readLen( pSock, uRead ); uRead += uName + uValue; unsigned char *sName = new unsigned char[uName]; - s.read( sName, uName ); + pSock->read( sName, uName ); Bu::String fsName( (char *)sName, uName ); delete[] sName; if( uValue > 0 ) { unsigned char *sValue = new unsigned char[uValue]; - s.read( sValue, uValue ); + pSock->read( sValue, uValue ); Bu::String fsValue( (char *)sValue, uValue ); hParams.insert( fsName, fsValue ); delete[] sValue; @@ -158,13 +158,12 @@ void Bu::FastCgi::run() bRunning = true; while( bRunning ) { - int iSock = pSrv->accept( 5 ); - if( iSock < 0 ) + Bu::Socket *pSock = pSrv->accept( 5 ); + if( pSock == NULL ) continue; - Bu::TcpSocket s( iSock ); - s.setBlocking( true ); -// sio << "Got connection, blocking? " << s.isBlocking() << sio.nl; + pSock->setBlocking( true ); +// sio << "Got connection, blocking? " << pSock->isBlocking() << sio.nl; try { for(;;) @@ -173,11 +172,11 @@ void Bu::FastCgi::run() memset( &r, 0, sizeof(r) ); // try // { - read( s, r ); + read( pSock, r ); // } // catch( Bu::ExceptionBase &e ) // { -// sio << "Error: " << e.what() << ", " << s.isOpen() << +// sio << "Error: " << e.what() << ", " << pSock->isOpen() << // sio.nl; // continue; // } @@ -192,7 +191,7 @@ void Bu::FastCgi::run() if( r.uContentLength > 0 ) { char *buf = new char[r.uContentLength]; - sio << " (read " << s.read( buf, r.uContentLength ) + sio << " (read " << pSock->read( buf, r.uContentLength ) << "/" << r.uContentLength << "):" << sio.nl; sio.write( buf, r.uContentLength ); sio << sio.nl << sio.nl; @@ -215,7 +214,7 @@ void Bu::FastCgi::run() // sio << "Begin Request."; { BeginRequestBody b; - read( s, b ); + read( pSock, b ); if( pChan != NULL ) { sio << "Error!!!" << sio.nl; @@ -236,7 +235,7 @@ void Bu::FastCgi::run() uint16_t uUsed = 0; while( uUsed < r.uContentLength ) { - readPair( s, pChan->hParams, uUsed ); + readPair( pSock, pChan->hParams, uUsed ); } } break; @@ -253,7 +252,7 @@ void Bu::FastCgi::run() int iTotal = 0; do { - size_t iRead = s.read( + size_t iRead = pSock->read( buf, r.uContentLength-iTotal ); iTotal += iRead; // sio << " (read " << iRead << " " << iTotal @@ -273,7 +272,7 @@ void Bu::FastCgi::run() else { char *buf = new char[r.uContentLength]; - s.read( buf, r.uContentLength ); + pSock->read( buf, r.uContentLength ); pChan->sData.append( buf, r.uContentLength ); delete[] buf; } @@ -323,12 +322,12 @@ void Bu::FastCgi::run() if( iSize > 65528 ) iSize = 65528; rOut.uContentLength = iSize; - write( s, rOut ); - s.write( sStdOut.getStr()+iPos, iSize ); + write( pSock, rOut ); + pSock->write( sStdOut.getStr()+iPos, iSize ); } } rOut.uContentLength = 0; - write( s, rOut ); + write( pSock, rOut ); rOut.uType = typeStdErr; if( sStdErr.getSize() > 0 ) @@ -340,21 +339,21 @@ void Bu::FastCgi::run() if( iSize > 65528 ) iSize = 65528; rOut.uContentLength = iSize; - write( s, rOut ); - s.write( sStdErr.getStr()+iPos, iSize ); + write( pSock, rOut ); + pSock->write( sStdErr.getStr()+iPos, iSize ); } } rOut.uContentLength = 0; - write( s, rOut ); + write( pSock, rOut ); rOut.uType = typeEndRequest; rOut.uContentLength = 8; - write( s, rOut ); + write( pSock, rOut ); EndRequestBody b; memset( &b, 0, sizeof(b) ); b.uStatus = iRet; - write( s, b ); + write( pSock, b ); delete pChan; aChannel[r.uRequestId-1] = NULL; @@ -362,10 +361,10 @@ void Bu::FastCgi::run() } } } - catch( Bu::TcpSocketException &e ) + catch( Bu::ExceptionBase &e ) { // sio << "Bu::SocketException: " << e.what() << sio.nl << -// "\tSocket open: " << s.isOpen() << sio.nl; +// "\tSocket open: " << pSock->isOpen() << sio.nl; } } } -- cgit v1.2.3