示例#1
0
    private static void ProcessRequest(bool isFcgiDebug, FcgiSynchronizer sync, Socket s)
    {
      using (s)
      using (var stream = new NetworkStream(s))
      {
        Log("accepted..");

        for (var i = 0; i < 20; ++i)
        {
          if (stream.DataAvailable)
            break;
          System.Threading.Thread.Sleep(TimeSpan.FromMilliseconds(30));
          if (i == 19)
          {
            Log("cancel");
            return;
          }
        }

        var parameters = new Dictionary<string, string>();
        for (; ; )
        {
          var req = stream.ReadRequest();
          if (isFcgiDebug)
            Log(string.Format("{0}:{1}, {2}:{3}:{4}", req.Version, req.Type, req.RequestId, req.Content.Hex(), req.Padding.Hex()));

          if (req.Type == FcgiType.Params)
          {
            var _parameters = ParseParameters(req.Content);
            if (_parameters.Count > 0)
            {
              parameters = _parameters;
              if (isFcgiDebug)
              {
                Log("parameters:");
                foreach (var pair in parameters)
                  Log(string.Format("  {0}:{1}", pair.Key, pair.Value));
              }
            }
          }
          JsonData json = null;
          if (req.Type == FcgiType.Stdin && req.Content != null && req.Content.Length > 0)
          {
            if (isFcgiDebug)
              Log("Stdin text: " + System.Text.Encoding.UTF8.GetString(req.Content));
            var data = ParseQuery(System.Text.Encoding.UTF8.GetString(req.Content));
            var values = data.ContainsKey("values") ? data["values"] : null;
            if (isFcgiDebug)
              Log("  " + values);
            //var jsSerializer = new System.Net.Json.JsonTextParser();
            var jsSerializer = Newtonsoft.Json.JsonSerializer.Create();
            json = new JsonData(jsSerializer.Deserialize(new Newtonsoft.Json.JsonTextReader(new System.IO.StringReader(values))));

          }
          if (req.Type == FcgiType.Stdin && req.Padding.Length == 0)
          {
            var res_text = string.Format(@"Content-type: text/html

{0}
", sync.ProcessRequest(parameters, json));
            var res_data = System.Text.Encoding.UTF8.GetBytes(res_text);
            var res_pad = new byte[8 - res_data.Length % 8];
            var res = new FcgiRequest { Version = 1, Type = FcgiType.Stdout, RequestId = req.RequestId, Content = res_data, Padding = res_pad };
            stream.Write(res);
            stream.Write(new FcgiRequest { Version = 1, Type = FcgiType.Stdout, RequestId = req.RequestId });
            stream.Write(new FcgiRequest { Version = 1, Type = FcgiType.EndRequest, RequestId = req.RequestId, Content = new byte[8] });
            break;
          }
        }
      }
    }
示例#2
0
    public static void Write(this System.IO.Stream stream, FcgiRequest req)
    {
      var mstream = new System.IO.MemoryStream();
      mstream.WriteByte(req.Version);
      mstream.WriteByte((byte)req.Type);
      mstream.WriteByte((byte)(req.RequestId / 256));
      mstream.WriteByte((byte)(req.RequestId % 256));
      mstream.WriteByte((byte)(req.Content.Length / 256));
      mstream.WriteByte((byte)(req.Content.Length % 256));
      mstream.WriteByte((byte)(req.Padding.Length));
      mstream.WriteByte(0);
      mstream.Write(req.Content, 0, req.Content.Length);
      mstream.Write(req.Padding, 0, req.Padding.Length);
      var message = mstream.ToArray();
      stream.Write(message, 0, message.Length);

      //Program.Log(string.Format("  {0}:{1}, {2}:{3}:{4}", req.Version, req.Type, req.RequestId, req.Content.Hex(), req.Padding.Hex()));
    }