Esempio n. 1
0
    public static void Execute(bool isFcgiDebug, int port, Dictionary<string, Func<string, int, JsonData, HtmlResult<HElement>>> handlers)
    {
      var sync = new FcgiSynchronizer(handlers);

      var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
      socket.Bind(new IPEndPoint(IPAddress.Loopback, port));
      socket.Listen(10);

      Console.WriteLine(string.Format("listen {0}..", port));
      for (var i = 0; ; i++)
      {
        var s = socket.Accept();
        //new System.Threading.Thread(_s =>
        //  {
        ProcessRequest(i == 0 || isFcgiDebug, sync, (Socket)s);
        //}).Start(s);
      }
    }
Esempio n. 2
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;
          }
        }
      }
    }