diff options
Diffstat (limited to 'src/experimental/fastcgi.cpp')
-rw-r--r-- | src/experimental/fastcgi.cpp | 81 |
1 files changed, 40 insertions, 41 deletions
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() : | |||
24 | pSrv( NULL ), | 24 | pSrv( NULL ), |
25 | bRunning( true ) | 25 | bRunning( true ) |
26 | { | 26 | { |
27 | pSrv = new Bu::TcpServerSocket( (Bu::TcpServerSocket::socket_t)STDIN_FILENO, false ); | 27 | pSrv = new Bu::ServerSocketTcp( (Bu::ServerSocketTcp::socket_t)STDIN_FILENO, false ); |
28 | } | 28 | } |
29 | 29 | ||
30 | Bu::FastCgi::FastCgi( int iPort ) : | 30 | Bu::FastCgi::FastCgi( int iPort ) : |
31 | pSrv( NULL ), | 31 | pSrv( NULL ), |
32 | bRunning( true ) | 32 | bRunning( true ) |
33 | { | 33 | { |
34 | pSrv = new Bu::TcpServerSocket( iPort ); | 34 | pSrv = new Bu::ServerSocketTcp( iPort ); |
35 | } | 35 | } |
36 | 36 | ||
37 | Bu::FastCgi::~FastCgi() | 37 | Bu::FastCgi::~FastCgi() |
@@ -64,63 +64,63 @@ bool Bu::FastCgi::isEmbedded() | |||
64 | #endif | 64 | #endif |
65 | } | 65 | } |
66 | 66 | ||
67 | void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::Record &r ) | 67 | void Bu::FastCgi::read( Bu::Socket *pSock, Bu::FastCgi::Record &r ) |
68 | { | 68 | { |
69 | int iRead = s.read( &r, sizeof(Record) ); | 69 | int iRead = pSock->read( &r, sizeof(Record) ); |
70 | if( iRead != sizeof(Record) ) | 70 | if( iRead != sizeof(Record) ) |
71 | throw Bu::TcpSocketException("Hey, the size %d is wrong for Record. (%s)", | 71 | throw Bu::ExceptionBase("Hey, the size %d is wrong for Record. (%pSock)", |
72 | iRead, strerror( errno ) ); | 72 | iRead, strerror( errno ) ); |
73 | r.uRequestId = ntohs( r.uRequestId ); | 73 | r.uRequestId = ntohs( r.uRequestId ); |
74 | r.uContentLength = ntohs( r.uContentLength ); | 74 | r.uContentLength = ntohs( r.uContentLength ); |
75 | } | 75 | } |
76 | 76 | ||
77 | void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::Record r ) | 77 | void Bu::FastCgi::write( Bu::Socket *pSock, Bu::FastCgi::Record r ) |
78 | { | 78 | { |
79 | // sio << "Out -> " << r << sio.nl; | 79 | // sio << "Out -> " << r << sio.nl; |
80 | r.uRequestId = htons( r.uRequestId ); | 80 | r.uRequestId = htons( r.uRequestId ); |
81 | r.uContentLength = htons( r.uContentLength ); | 81 | r.uContentLength = htons( r.uContentLength ); |
82 | s.write( &r, sizeof(Record) ); | 82 | pSock->write( &r, sizeof(Record) ); |
83 | } | 83 | } |
84 | 84 | ||
85 | void Bu::FastCgi::read( Bu::TcpSocket &s, Bu::FastCgi::BeginRequestBody &b ) | 85 | void Bu::FastCgi::read( Bu::Socket *pSock, Bu::FastCgi::BeginRequestBody &b ) |
86 | { | 86 | { |
87 | s.read( &b, sizeof(BeginRequestBody) ); | 87 | pSock->read( &b, sizeof(BeginRequestBody) ); |
88 | b.uRole = ntohs( b.uRole ); | 88 | b.uRole = ntohs( b.uRole ); |
89 | } | 89 | } |
90 | 90 | ||
91 | void Bu::FastCgi::write( Bu::TcpSocket &s, Bu::FastCgi::EndRequestBody b ) | 91 | void Bu::FastCgi::write( Bu::Socket *pSock, Bu::FastCgi::EndRequestBody b ) |
92 | { | 92 | { |
93 | b.uStatus = htonl( b.uStatus ); | 93 | b.uStatus = htonl( b.uStatus ); |
94 | s.write( &b, sizeof(b) ); | 94 | pSock->write( &b, sizeof(b) ); |
95 | } | 95 | } |
96 | 96 | ||
97 | uint32_t Bu::FastCgi::readLen( Bu::TcpSocket &s, uint16_t &uRead ) | 97 | uint32_t Bu::FastCgi::readLen( Bu::Socket *pSock, uint16_t &uRead ) |
98 | { | 98 | { |
99 | uint8_t uByte[4]; | 99 | uint8_t uByte[4]; |
100 | s.read( uByte, 1 ); | 100 | pSock->read( uByte, 1 ); |
101 | uRead++; | 101 | uRead++; |
102 | if( uByte[0] >> 7 == 0 ) | 102 | if( uByte[0] >> 7 == 0 ) |
103 | return uByte[0]; | 103 | return uByte[0]; |
104 | 104 | ||
105 | s.read( uByte+1, 3 ); | 105 | pSock->read( uByte+1, 3 ); |
106 | uRead += 3; | 106 | uRead += 3; |
107 | return ((uByte[0]&0x7f)<<24)|(uByte[1]<<16)|(uByte[2]<<8)|(uByte[3]); | 107 | return ((uByte[0]&0x7f)<<24)|(uByte[1]<<16)|(uByte[2]<<8)|(uByte[3]); |
108 | } | 108 | } |
109 | 109 | ||
110 | void Bu::FastCgi::readPair( Bu::TcpSocket &s, StrHash &hParams, uint16_t &uRead ) | 110 | void Bu::FastCgi::readPair( Bu::Socket *pSock, StrHash &hParams, uint16_t &uRead ) |
111 | { | 111 | { |
112 | uint32_t uName = readLen( s, uRead ); | 112 | uint32_t uName = readLen( pSock, uRead ); |
113 | uint32_t uValue = readLen( s, uRead ); | 113 | uint32_t uValue = readLen( pSock, uRead ); |
114 | uRead += uName + uValue; | 114 | uRead += uName + uValue; |
115 | unsigned char *sName = new unsigned char[uName]; | 115 | unsigned char *sName = new unsigned char[uName]; |
116 | s.read( sName, uName ); | 116 | pSock->read( sName, uName ); |
117 | Bu::String fsName( (char *)sName, uName ); | 117 | Bu::String fsName( (char *)sName, uName ); |
118 | delete[] sName; | 118 | delete[] sName; |
119 | 119 | ||
120 | if( uValue > 0 ) | 120 | if( uValue > 0 ) |
121 | { | 121 | { |
122 | unsigned char *sValue = new unsigned char[uValue]; | 122 | unsigned char *sValue = new unsigned char[uValue]; |
123 | s.read( sValue, uValue ); | 123 | pSock->read( sValue, uValue ); |
124 | Bu::String fsValue( (char *)sValue, uValue ); | 124 | Bu::String fsValue( (char *)sValue, uValue ); |
125 | hParams.insert( fsName, fsValue ); | 125 | hParams.insert( fsName, fsValue ); |
126 | delete[] sValue; | 126 | delete[] sValue; |
@@ -158,13 +158,12 @@ void Bu::FastCgi::run() | |||
158 | bRunning = true; | 158 | bRunning = true; |
159 | while( bRunning ) | 159 | while( bRunning ) |
160 | { | 160 | { |
161 | int iSock = pSrv->accept( 5 ); | 161 | Bu::Socket *pSock = pSrv->accept( 5 ); |
162 | if( iSock < 0 ) | 162 | if( pSock == NULL ) |
163 | continue; | 163 | continue; |
164 | 164 | ||
165 | Bu::TcpSocket s( iSock ); | 165 | pSock->setBlocking( true ); |
166 | s.setBlocking( true ); | 166 | // sio << "Got connection, blocking? " << pSock->isBlocking() << sio.nl; |
167 | // sio << "Got connection, blocking? " << s.isBlocking() << sio.nl; | ||
168 | try | 167 | try |
169 | { | 168 | { |
170 | for(;;) | 169 | for(;;) |
@@ -173,11 +172,11 @@ void Bu::FastCgi::run() | |||
173 | memset( &r, 0, sizeof(r) ); | 172 | memset( &r, 0, sizeof(r) ); |
174 | // try | 173 | // try |
175 | // { | 174 | // { |
176 | read( s, r ); | 175 | read( pSock, r ); |
177 | // } | 176 | // } |
178 | // catch( Bu::ExceptionBase &e ) | 177 | // catch( Bu::ExceptionBase &e ) |
179 | // { | 178 | // { |
180 | // sio << "Error: " << e.what() << ", " << s.isOpen() << | 179 | // sio << "Error: " << e.what() << ", " << pSock->isOpen() << |
181 | // sio.nl; | 180 | // sio.nl; |
182 | // continue; | 181 | // continue; |
183 | // } | 182 | // } |
@@ -192,7 +191,7 @@ void Bu::FastCgi::run() | |||
192 | if( r.uContentLength > 0 ) | 191 | if( r.uContentLength > 0 ) |
193 | { | 192 | { |
194 | char *buf = new char[r.uContentLength]; | 193 | char *buf = new char[r.uContentLength]; |
195 | sio << " (read " << s.read( buf, r.uContentLength ) | 194 | sio << " (read " << pSock->read( buf, r.uContentLength ) |
196 | << "/" << r.uContentLength << "):" << sio.nl; | 195 | << "/" << r.uContentLength << "):" << sio.nl; |
197 | sio.write( buf, r.uContentLength ); | 196 | sio.write( buf, r.uContentLength ); |
198 | sio << sio.nl << sio.nl; | 197 | sio << sio.nl << sio.nl; |
@@ -215,7 +214,7 @@ void Bu::FastCgi::run() | |||
215 | // sio << "Begin Request."; | 214 | // sio << "Begin Request."; |
216 | { | 215 | { |
217 | BeginRequestBody b; | 216 | BeginRequestBody b; |
218 | read( s, b ); | 217 | read( pSock, b ); |
219 | if( pChan != NULL ) | 218 | if( pChan != NULL ) |
220 | { | 219 | { |
221 | sio << "Error!!!" << sio.nl; | 220 | sio << "Error!!!" << sio.nl; |
@@ -236,7 +235,7 @@ void Bu::FastCgi::run() | |||
236 | uint16_t uUsed = 0; | 235 | uint16_t uUsed = 0; |
237 | while( uUsed < r.uContentLength ) | 236 | while( uUsed < r.uContentLength ) |
238 | { | 237 | { |
239 | readPair( s, pChan->hParams, uUsed ); | 238 | readPair( pSock, pChan->hParams, uUsed ); |
240 | } | 239 | } |
241 | } | 240 | } |
242 | break; | 241 | break; |
@@ -253,7 +252,7 @@ void Bu::FastCgi::run() | |||
253 | int iTotal = 0; | 252 | int iTotal = 0; |
254 | do | 253 | do |
255 | { | 254 | { |
256 | size_t iRead = s.read( | 255 | size_t iRead = pSock->read( |
257 | buf, r.uContentLength-iTotal ); | 256 | buf, r.uContentLength-iTotal ); |
258 | iTotal += iRead; | 257 | iTotal += iRead; |
259 | // sio << " (read " << iRead << " " << iTotal | 258 | // sio << " (read " << iRead << " " << iTotal |
@@ -273,7 +272,7 @@ void Bu::FastCgi::run() | |||
273 | else | 272 | else |
274 | { | 273 | { |
275 | char *buf = new char[r.uContentLength]; | 274 | char *buf = new char[r.uContentLength]; |
276 | s.read( buf, r.uContentLength ); | 275 | pSock->read( buf, r.uContentLength ); |
277 | pChan->sData.append( buf, r.uContentLength ); | 276 | pChan->sData.append( buf, r.uContentLength ); |
278 | delete[] buf; | 277 | delete[] buf; |
279 | } | 278 | } |
@@ -323,12 +322,12 @@ void Bu::FastCgi::run() | |||
323 | if( iSize > 65528 ) | 322 | if( iSize > 65528 ) |
324 | iSize = 65528; | 323 | iSize = 65528; |
325 | rOut.uContentLength = iSize; | 324 | rOut.uContentLength = iSize; |
326 | write( s, rOut ); | 325 | write( pSock, rOut ); |
327 | s.write( sStdOut.getStr()+iPos, iSize ); | 326 | pSock->write( sStdOut.getStr()+iPos, iSize ); |
328 | } | 327 | } |
329 | } | 328 | } |
330 | rOut.uContentLength = 0; | 329 | rOut.uContentLength = 0; |
331 | write( s, rOut ); | 330 | write( pSock, rOut ); |
332 | 331 | ||
333 | rOut.uType = typeStdErr; | 332 | rOut.uType = typeStdErr; |
334 | if( sStdErr.getSize() > 0 ) | 333 | if( sStdErr.getSize() > 0 ) |
@@ -340,21 +339,21 @@ void Bu::FastCgi::run() | |||
340 | if( iSize > 65528 ) | 339 | if( iSize > 65528 ) |
341 | iSize = 65528; | 340 | iSize = 65528; |
342 | rOut.uContentLength = iSize; | 341 | rOut.uContentLength = iSize; |
343 | write( s, rOut ); | 342 | write( pSock, rOut ); |
344 | s.write( sStdErr.getStr()+iPos, iSize ); | 343 | pSock->write( sStdErr.getStr()+iPos, iSize ); |
345 | } | 344 | } |
346 | } | 345 | } |
347 | rOut.uContentLength = 0; | 346 | rOut.uContentLength = 0; |
348 | write( s, rOut ); | 347 | write( pSock, rOut ); |
349 | 348 | ||
350 | rOut.uType = typeEndRequest; | 349 | rOut.uType = typeEndRequest; |
351 | rOut.uContentLength = 8; | 350 | rOut.uContentLength = 8; |
352 | write( s, rOut ); | 351 | write( pSock, rOut ); |
353 | 352 | ||
354 | EndRequestBody b; | 353 | EndRequestBody b; |
355 | memset( &b, 0, sizeof(b) ); | 354 | memset( &b, 0, sizeof(b) ); |
356 | b.uStatus = iRet; | 355 | b.uStatus = iRet; |
357 | write( s, b ); | 356 | write( pSock, b ); |
358 | 357 | ||
359 | delete pChan; | 358 | delete pChan; |
360 | aChannel[r.uRequestId-1] = NULL; | 359 | aChannel[r.uRequestId-1] = NULL; |
@@ -362,10 +361,10 @@ void Bu::FastCgi::run() | |||
362 | } | 361 | } |
363 | } | 362 | } |
364 | } | 363 | } |
365 | catch( Bu::TcpSocketException &e ) | 364 | catch( Bu::ExceptionBase &e ) |
366 | { | 365 | { |
367 | // sio << "Bu::SocketException: " << e.what() << sio.nl << | 366 | // sio << "Bu::SocketException: " << e.what() << sio.nl << |
368 | // "\tSocket open: " << s.isOpen() << sio.nl; | 367 | // "\tSocket open: " << pSock->isOpen() << sio.nl; |
369 | } | 368 | } |
370 | } | 369 | } |
371 | } | 370 | } |