Exemple #1
0
 internal void HandleRequest(FastCGIRequest request)
 {
     if (OnRequest != null)
     {
         try {
             OnRequest(request);
         }
         catch (Exception) {
             request.End();
         }
     }
     else
     {
         request.End();
     }
 }
Exemple #2
0
        private void OnRecordReceived(RecordType recordType, int requestId, ArraySegment <byte> content)
        {
            switch (recordType)
            {
            case RecordType.BeginRequest: {
                var buf       = content.Array;
                var p         = content.Offset;
                var role      = (buf[p] << 8) | buf[p + 1];
                var flags     = buf[p + 2];
                var keepAlive = (flags & FCGI_KEEP_CONN) != 0;
                var request   = new FastCGIRequest(requestId, this, keepAlive);
                _requests[requestId] = request;
            }
            break;

            case RecordType.AbortRequest:
            case RecordType.EndRequest: {
                _requests.Remove(requestId);
            }
            break;

            case RecordType.GetValues: {
                var buf = new byte[256];
                int pos = 0;
                WriteNameValue(buf, ref pos, "FCGI_MAX_CONNS", "32");
                WriteNameValue(buf, ref pos, "FCGI_MAX_REQS", "32");
                WriteNameValue(buf, ref pos, "FCGI_MPXS_CONNS", "0");

                SendRecord(RecordType.GetValuesResult, 0, new ArraySegment <byte>(buf, 0, pos));
            }
            break;

            case RecordType.Params: {
                FastCGIRequest request = null;
                if (_requests.TryGetValue(requestId, out request))
                {
                    if (content.Count > 0)
                    {
                        var buf = content.Array;
                        int len = content.Count;
                        int pos = content.Offset;

                        while (pos < len)
                        {
                            var nameLength  = ReadVarLength(buf, ref pos);
                            var valueLength = ReadVarLength(buf, ref pos);

                            if (pos + nameLength + valueLength > len)
                            {
                                throw new EndOfStreamException();
                            }

                            var name  = Encoding.UTF8.GetString(buf, pos, nameLength); pos += nameLength;
                            var value = Encoding.UTF8.GetString(buf, pos, valueLength); pos += valueLength;

                            request._parameters[name] = value;
                        }
                    }
                }
            }
            break;

            case RecordType.Stdin: {
                FastCGIRequest request = null;
                if (_requests.TryGetValue(requestId, out request))
                {
                    // Finished requests are indicated by an empty stdin record
                    if (content.Count == 0)
                    {
                        _owner.HandleRequest(request);
                    }
                    else
                    {
                        request._body.Add(content);
                    }
                }
            }
            break;
            }
        }