Example #1
0
        private object CallMethod(Delegate method, CallRequest request)
        {
            MethodInfo mi = method.Method;

            ParameterInfo[] pis  = mi.GetParameters();
            object[]        args = new object[pis.Length];

            // Enumerate all parameters and fill out objects array
            for (int i = 0; i < pis.Length; ++i)
            {
                args[i] = DemarhasllObject(pis[i].Name, pis[i].ParameterType, request.Parameters);
            }
            // Arguments are ready
            return(method.DynamicInvoke(args));
        }
Example #2
0
        private void ProcessRequest(Stream socketStream)
        {
            CallRequest req = new CallRequest();

            try
            {
                req.Read(socketStream);
            }
            catch (CallRequest.RequestException ex)
            {
                FormatError("HTTP/1.0", _defaultFormatter(), socketStream, ex.Code, ex.Message);
                return;
            }
            catch (Exception ex)
            {
                FormatError("HTTP/1.0", _defaultFormatter(), socketStream, 408, ex.Message);
                return;
            }

            // Check our method
            if (Core.State != CoreState.Running)
            {
                FormatError(req.Version, _defaultFormatter(), socketStream, 403, "Not ready");
                return;
            }

            // Check secure string
            int keyEnd = req.URL.IndexOf('/', 1);

            if (keyEnd < 0)
            {
                FormatError(req.Version, _defaultFormatter(), socketStream, 404, "Secure key not found");
                return;
            }
            string key = req.URL.Substring(1, keyEnd - 1);

            if (key != _protectionKey)
            {
                FormatError(req.Version, _defaultFormatter(), socketStream, 403, "Invalid secure key");
                return;
            }

            // Try to find formatter
            int frmEnd = req.URL.IndexOf('/', keyEnd + 1);

            if (frmEnd < 0)
            {
                FormatError(req.Version, _defaultFormatter(), socketStream, 404, "Output format not found");
                return;
            }
            string formatName = req.URL.Substring(keyEnd + 1, frmEnd - keyEnd - 1);

            // Try to find method.
            if (!_formatters.ContainsKey(formatName))
            {
                FormatError(req.Version, _defaultFormatter(), socketStream, 404, "Output format not found");
                return;
            }
            IOutputFormatter formatter = (_formatters[formatName] as CreateFormatter)();

            string method = req.URL.Substring(frmEnd + 1);

            if (!_calls.ContainsKey(method))
            {
                FormatError(req.Version, formatter, socketStream, 404, "Method not found");
                return;
            }

            // Get method and check parameters
            Delegate md;

            lock (_calls)
            {
                md = _calls[method] as Delegate;
            }
            object result;

            try
            {
                result = CallMethod(md, req);
                FormatResult(req.Version, formatter, socketStream, result);
            }
            catch (Exception ex)
            {
                FormatError(req.Version, formatter, socketStream, 500, ex.Message);
            }
        }