summaryrefslogtreecommitdiff
path: root/src/fastcgi.cpp
blob: 9aecbfb156233608d6ca6d673986fcf3d4ab0990 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (C) 2007-2008 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/fastcgi.h"

#include <arpa/inet.h>
#include <errno.h>

#include "bu/membuf.h"

#include "bu/sio.h"
using Bu::sio;
using Bu::Fmt;

Bu::FastCgi::FastCgi() :
	pSrv( NULL ),
	bRunning( true )
{
	pSrv = new Bu::ServerSocket( STDIN_FILENO, false );
}

Bu::FastCgi::FastCgi( int iPort ) :
	pSrv( NULL ),
	bRunning( true )
{
	pSrv = new Bu::ServerSocket( iPort );
}

Bu::FastCgi::~FastCgi()
{
}

bool Bu::FastCgi::isEmbedded()
{
	struct sockaddr name;
	socklen_t namelen = sizeof(name);
	if( getpeername( STDIN_FILENO, &name, &namelen ) != 0 &&
		errno == ENOTCONN )
	{
		sio << "errno = " << errno << " (" << strerror( errno ) << ")" <<
			sio.nl;
		sio << "Socket found" << sio.nl;
		return true;
	}
	else
	{
		sio << "errno = " << errno << " (" << strerror( errno ) << ")" <<
			sio.nl;
		sio << "No socket detected, running in standalone mode" << sio.nl;
		return false;
	}
}

void Bu::FastCgi::read( Bu::Socket &s, Bu::FastCgi::Record &r )
{
	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) );
}

void Bu::FastCgi::read( Bu::Socket &s, Bu::FastCgi::BeginRequestBody &b )
{
	s.read( &b, sizeof(BeginRequestBody) );
	b.uRole = ntohs( b.uRole );
}

void Bu::FastCgi::write( Bu::Socket &s, Bu::FastCgi::EndRequestBody b )
{
	b.uStatus = htonl( b.uStatus );
	s.write( &b, sizeof(b) );
}

uint32_t Bu::FastCgi::readLen( Bu::Socket &s, uint16_t &uRead )
{
	uint8_t uByte[4];
	s.read( uByte, 1 );
	uRead++;
	if( uByte[0] >> 7 == 0 )
		return uByte[0];
	
	s.read( uByte+1, 3 );
	uRead += 3;
	return ((uByte[0]&0x7f)<<24)|(uByte[1]<<16)|(uByte[2]<<8)|(uByte[3]);
}

void Bu::FastCgi::readPair( Bu::Socket &s, StrHash &hParams, uint16_t &uRead )
{
	uint32_t uName = readLen( s, uRead );
	uint32_t uValue = readLen( s, uRead );
	uRead += uName + uValue;
	unsigned char *sName = new unsigned char[uName];
	s.read( sName, uName );
	Bu::FString fsName( (char *)sName, uName );
	delete[] sName;

	if( uValue > 0 )
	{
		unsigned char *sValue = new unsigned char[uValue];
		s.read( sValue, uValue );
		Bu::FString fsValue( (char *)sValue, uValue );
		hParams.insert( fsName, fsValue );
		delete[] sValue;
	}
	else
	{
		hParams.insert( fsName, "" );
	}
}

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 )
	{
		int iSock = pSrv->accept( 5 );
		if( iSock < 0 )
			continue;

		Bu::Socket s( iSock );
		s.setBlocking( true );
//		sio << "Got connection, blocking?  " << s.isBlocking() << sio.nl;
		try
		{
			for(;;)
			{
				Record r;
				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 )
				{
					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=" << 
//					(unsigned int)r.uPaddingLength << "):  ";
//				fflush( stdout );
				
				switch( (RequestType)r.uType )
				{
					case typeBeginRequest:
//						sio << "Begin Request.";
						{
							BeginRequestBody b;
							read( s, b );
							if( pChan != NULL )
							{
								sio << "Error!!!" << sio.nl;
								return;
							}
							pChan = aChannel[r.uRequestId-1] = new Channel();
						}
						break;

					case typeParams:
//						sio << "Params.";
						if( r.uContentLength == 0 )
						{
							pChan->uFlags |= chflgParamsDone;
						}
						else
						{
							uint16_t uUsed = 0;
							while( uUsed < r.uContentLength )
							{
								readPair( s, pChan->hParams, uUsed );
							}
						}
						break;

					case typeStdIn:
//						sio << "StdIn.";
						if( r.uContentLength == 0 )
						{
							pChan->uFlags |= chflgStdInDone;
						}
						else
						{
							char *buf = new char[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;

					case typeData:
//						sio << "Data.";
						if( r.uContentLength == 0 )
						{
							pChan->uFlags |= chflgDataDone;
						}
						else
						{
							char *buf = new char[r.uContentLength];
							s.read( buf, r.uContentLength );
							pChan->sData.append( buf, r.uContentLength );
							delete[] buf;
						}
						break;

					case typeStdOut:
					case typeStdErr:
					case typeEndRequest:
					case typeAbortRequest:
					case typeGetValuesResult:
//						sio << "Scary.";
						// ??? we shouldn't get these.
						break;
					
				}

//				sio << sio.nl;

				if( pChan )
				{
					if( pChan->uFlags == chflgAllDone )
					{
//						sio << "All done, generating output." << sio.nl;
						Bu::MemBuf mStdOut, mStdErr;
						int iRet = onRequest(
							pChan->hParams, pChan->sStdIn,
							mStdOut, mStdErr
							);

						Bu::FString &sStdOut = mStdOut.getString();
						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 )
						{
							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.uContentLength = 0;
						write( s, rOut );

						rOut.uType = typeStdErr;
						if( sStdErr.getSize() > 0 )
						{
							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.uContentLength = 0;
						write( s, rOut );

						rOut.uType = typeEndRequest;
						rOut.uContentLength = 8;
						write( s, rOut );
						
						EndRequestBody b;
						memset( &b, 0, sizeof(b) );
						b.uStatus =  iRet;
						write( s, b );

						delete pChan;
						aChannel[r.uRequestId-1] = NULL;
					}
				}
			}
		}
		catch( Bu::SocketException &e )
		{
//			sio << "Bu::SocketException: " << e.what() << sio.nl <<
//				"\tSocket open:  " << s.isOpen() << sio.nl;
		}
	}
}