Пример #1
0
        public void ProcessRequest(HttpContext context)
        {
            var ret = new List <object>()
            {
                0, null
            };
            bool      ok   = false;
            bool      dret = false;
            JDEnvBase env  = null;

            context.Response.ContentType = "text/plain";
            try
            {
                env = JDEnvBase.createInstance();
                env.init(context);
                this.env = env;

                string path = context.Request.Path;
                Match  m    = Regex.Match(path, @"api/+([\w|.]+)$");
                //Match m = Regex.Match(path, @"api/(\w+)");
                if (!m.Success)
                {
                    throw new MyException(E_PARAM, "bad ac");
                }

                // 测试模式允许跨域
                string origin;
                if (env.isTestMode && (origin = _SERVER["HTTP_ORIGIN"]) != null)
                {
                    context.Response.AddHeader("Access-Control-Allow-Origin", origin);
                    context.Response.AddHeader("Access-Control-Allow-Credentials", "true");
                    context.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
                }
                if (context.Request.HttpMethod == "OPTIONS")
                {
                    throw new DirectReturn();
                }

                string ac = m.Groups[1].Value;
                try
                {
                    ret[1] = env.callSvc(ac);
                }
                catch (TargetInvocationException ex)
                {
                    Exception ex1 = ex;
                    while (ex1.InnerException != null && ex1 is TargetInvocationException)
                    {
                        ex1 = ex1.InnerException;
                    }
                    throw ex1;
                }
                ok = true;
            }
            catch (DirectReturn ex)
            {
                ok = true;
                if (ex.retVal != null)
                {
                    ret[1] = ex.retVal;
                }
                else
                {
                    dret = true;
                }
            }
            catch (MyException ex)
            {
                ret[0] = ex.Code;
                ret[1] = ex.Message;
                ret.Add(ex.DebugInfo);
            }
            catch (Exception ex)
            {
                ret[0] = ex is DbException ? E_DB : E_SERVER;
                ret[1] = GetErrInfo((int)ret[0]);
                if (env != null && env.isTestMode)
                {
                    ret.Add(ex.Message);
                    ret.Add(ex.StackTrace);
                }
            }
            if (env != null)
            {
                env.close(ok);
                if (env.debugInfo.Count > 0)
                {
                    ret.Add(env.debugInfo);
                }
            }

            if (dret)
            {
                return;
            }

            var s = jsonEncode(ret, env.isTestMode);

            context.Response.Write(s);
        }
Пример #2
0
        // TODO: asAdmin
        public object callSvc(string ac, JsObject param = null, JsObject postParam = null, CallSvcOpt opt = null)
        {
            Match  m          = Regex.Match(ac, @"(\w+)(?:\.(\w+))?$");
            string ac1        = null;
            string table      = null;
            string clsName    = null;
            string methodName = null;

            if (m.Groups[2].Length > 0)
            {
                table      = m.Groups[1].Value;
                ac1        = m.Groups[2].Value;
                clsName    = onCreateAC(table);
                methodName = "api_" + ac1;
            }
            else
            {
                clsName    = "Global";
                methodName = "api_" + m.Groups[1].Value;
            }

            JDApiBase api = null;
            Assembly  asm = JDEnvBase.getAsmembly();

            api = asm.CreateInstance("JDApi." + clsName) as JDApiBase;
            if (api == null)
            {
                if (table == null)
                {
                    throw new MyException(JDApiBase.E_PARAM, "bad ac=`" + ac + "` (no Global)");
                }

                int code = !this.api.hasPerm(JDApiBase.AUTH_LOGIN)? JDApiBase.E_NOAUTH : JDApiBase.E_FORBIDDEN;
                throw new MyException(code, string.Format("Operation is not allowed for current user on object `{0}`", table));
            }
            Type       t  = api.GetType();
            MethodInfo mi = t.GetMethod(methodName);

            if (mi == null)
            {
                throw new MyException(JDApiBase.E_PARAM, "bad ac=`" + ac + "` (no method)");
            }
            api.env = this;

            NameValueCollection[] bak = null;
            if (opt != null)
            {
                if (opt.backupEnv)
                {
                    bak = new NameValueCollection[] { this._GET, this._POST }
                }
                ;
                if (opt.isCleanCall)
                {
                    this._GET  = new NameValueCollection();
                    this._POST = new NameValueCollection();
                }
            }
            if (param != null)
            {
                foreach (var kv in param)
                {
                    this._GET[kv.Key] = kv.Value.ToString();
                }
            }
            if (postParam != null)
            {
                foreach (var kv in postParam)
                {
                    this._POST[kv.Key] = kv.Value.ToString();
                }
            }

            object ret = null;

            if (clsName == "Global")
            {
                ret = mi.Invoke(api, null);
            }
            else if (t.IsSubclassOf(typeof(AccessControl)))
            {
                AccessControl accessCtl = api as AccessControl;
                accessCtl.init(table, ac1);
                accessCtl.before();
                object rv = mi.Invoke(api, null);
                //ret[1] = t.InvokeMember(methodName, BindingFlags.InvokeMethod, null, api, null);
                accessCtl.after(ref rv);
                ret = rv;
            }
            else
            {
                throw new MyException(JDApiBase.E_SERVER, "misconfigured ac=`" + ac + "`");
            }
            if (ret == null)
            {
                ret = "OK";
            }
            if (bak != null)
            {
                this._GET  = bak[0] as NameValueCollection;
                this._POST = bak[1] as NameValueCollection;
            }
            return(ret);
        }