Exemple #1
0
        protected override void ProcessRequest()
        {
            if (!CaselessString.Equals(Request.RequestType, "POST"))
            {
                throw new JsonRpcException(string.Format("HTTP {0} is not supported for RPC execution. Use HTTP POST only.", Request.RequestType));
            }

            //
            // Sets the "Cache-Control" header value to "no-cache".
            // NOTE: It does not send the common HTTP 1.0 request directive
            // "Pragma" with the value "no-cache".
            //

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //
            // Response will be plain text, though it would have been nice to
            // be more specific, like text/json.
            //

            Response.ContentType = "text/plain";

            //
            // Delegate rest of the work to JsonRpcServer.
            //

            JsonRpcDispatcher dispatcher = new JsonRpcDispatcher(Service);

            if (HttpRequestSecurity.IsLocal(Request))
                dispatcher.SetLocalExecution();

            using (TextReader reader = GetRequestReader())
                dispatcher.Process(reader, Response.Output);
        }
        protected override void ProcessRequest()
        {
            //
            // Sets the "Cache-Control" header value to "no-cache".
            // NOTE: It does not send the common HTTP 1.0 request directive
            // "Pragma" with the value "no-cache".
            //

            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //
            // Response will be plain text, though it would have been nice to 
            // be more specific, like text/json.
            //

            Response.ContentType = "text/plain";

            //
            // Delegate rest of the work to JsonRpcServer.
            //

            JsonRpcDispatcher dispatcher = new JsonRpcDispatcher(TargetService);
            
            if (HttpRequestSecurity.IsLocal(Request))
                dispatcher.SetLocalExecution();

            using (StreamReader reader = new StreamReader(Request.InputStream, Request.ContentEncoding))
                dispatcher.Process(reader, Response.Output);
        }
        public JsonRpcDispatchScope(JsonRpcDispatcher dispatcher, HttpContext context) 
        {
            Debug.Assert(dispatcher != null);
            Debug.Assert(context != null);

            //
            // Setup for local execution if client request is from the same
            // machine. The dispatcher uses this to determine whether to 
            // emit a detailed stack trace or not in the event of an error.
            //

            if (HttpRequestSecurity.IsLocal(context.Request))
                dispatcher.SetLocalExecution();

            //
            // Initialize the import and export contexts, which are pooled
            // per-application instance.
            //

            IDictionary appVars = AppVars.Get(context.ApplicationInstance);

            ExportContext expctx = (ExportContext) appVars[typeof(ExportContext)];

            if (expctx == null)
            {
                expctx = new ExportContext();
                appVars.Add(typeof(ExportContext), expctx);
            }

            dispatcher.ExportContext = expctx;

            ImportContext impctx = (ImportContext) appVars[typeof(ImportContext)];

            if (impctx == null)
            {
                impctx = new ImportContext();
                appVars.Add(typeof(ImportContext), impctx);
            }

            dispatcher.ImportContext = impctx;

            _dispatcher = dispatcher;
        }
        protected override void ProcessRequest()
        {
            string httpMethod = Request.RequestType;

            if (!CaselessString.Equals(httpMethod, "GET") &&
                !CaselessString.Equals(httpMethod, "HEAD"))
            {
                throw new JsonRpcException(string.Format("HTTP {0} is not supported for RPC execution. Use HTTP GET or HEAD only.", httpMethod));
            }

            string callback = Mask.NullString(Request.QueryString["jsonp"]);
            bool padded = callback.Length > 0;
            
            //
            // The response type depends on whether JSONP (JSON with 
            // Padding) is in effect or not.
            //

            Response.ContentType = padded ? "text/javascript" : "application/json";
            
            //
            // Validate that the JSONP callback method conforms to the 
            // allowed syntax. If not, issue a client-side exception
            // that will certainly help to bring problem to light, even if
            // a little too aggressively.
            //
            
            if (padded)
            {
                if (!_jsonpex.IsMatch(callback))
                {
                    Response.Write("throw new Error('Invalid JSONP callback parameter value.');");
                    Response.End();
                }
            }
            
            //
            // Convert the query string into a call object.
            //

            JsonWriter writer = new JsonTextWriter();
            
            writer.WriteStartObject();
            
            writer.WriteMember("id");
            writer.WriteNumber(-1);
            
            writer.WriteMember("method");
            
            string methodName = Mask.NullString(Request.PathInfo);
            
            if (methodName.Length == 0)
            {
                writer.WriteNull();
            }
            else
            {
                //
                // If the method name contains periods then we replace it
                // with dashes to mean the one and same thing. This is
                // done to provide dashes as an alternative to some periods
                // since some web servers may block requests (for security
                // reasons) if a path component of the URL contains more
                // than one period.
                //
                
                writer.WriteString(methodName.Substring(1).Replace('-', '.'));
            }
            
            writer.WriteMember("params");
            NameValueCollection query = new NameValueCollection(Request.QueryString);
            query.Remove(string.Empty);
            JsonConvert.Export(Request.QueryString, writer);
            
            writer.WriteEndObject();
            
            //
            // Delegate rest of the work to JsonRpcDispatcher.
            //

            JsonRpcDispatcher dispatcher = new JsonRpcDispatcher(Service);
            
            dispatcher.RequireIdempotency = true;
            
            if (HttpRequestSecurity.IsLocal(Request))
                dispatcher.SetLocalExecution();
                        
            if (padded)
            {
                //
                // For JSONP, see details here:
                // http://bob.pythonmac.org/archives/2005/12/05/remote-json-jsonp/
                //

                Response.Write(callback);
                Response.Write('(');
            }

            dispatcher.Process(new StringReader(writer.ToString()), Response.Output);
            
            if (padded)
                Response.Write(')');
        }
        protected override void ProcessRequest()
        {
            string httpMethod = Request.RequestType;

            if (!CaselessString.Equals(httpMethod, "GET") &&
                !CaselessString.Equals(httpMethod, "HEAD"))
            {
                throw new JsonRpcException(string.Format("HTTP {0} is not supported for RPC execution. Use HTTP GET or HEAD only.", httpMethod));
            }

            //
            // Response will be plain text, though it would have been nice to
            // be more specific, like text/json.
            //

            Response.ContentType = "text/plain";

            //
            // Convert the query string into a call object.
            //

            JsonWriter writer = new JsonTextWriter();

            writer.WriteStartObject();

            writer.WriteMember("id");
            writer.WriteNumber(-1);

            writer.WriteMember("method");

            string methodName = Mask.NullString(Request.PathInfo);

            if (methodName.Length == 0)
            {
                writer.WriteNull();
            }
            else
            {
                //
                // If the method name contains periods then we replace it
                // with dashes to mean the one and same thing. This is
                // done to provide dashes as an alternative to some periods
                // since some web servers may block requests (for security
                // reasons) if a path component of the URL contains more
                // than one period.
                //

                writer.WriteString(methodName.Substring(1).Replace('-', '.'));
            }

            writer.WriteMember("params");
            NameValueCollection query = new NameValueCollection(Request.QueryString);
            query.Remove(string.Empty);
            JsonConvert.Export(Request.QueryString, writer);

            writer.WriteEndObject();

            //
            // Delegate rest of the work to JsonRpcDispatcher.
            //

            JsonRpcDispatcher dispatcher = new JsonRpcDispatcher(Service);

            dispatcher.RequireIdempotency = true;

            if (HttpRequestSecurity.IsLocal(Request))
                dispatcher.SetLocalExecution();

            dispatcher.Process(new StringReader(writer.ToString()), Response.Output);
        }
        protected override void ProcessRequest()
        {
            string httpMethod = Request.RequestType;

            if (!CaselessString.Equals(httpMethod, "GET") &&
                !CaselessString.Equals(httpMethod, "HEAD"))
            {
                throw new JsonRpcException(string.Format("HTTP {0} is not supported for RPC execution. Use HTTP GET or HEAD only.", httpMethod));
            }

            //
            // Response will be plain text, though it would have been nice to 
            // be more specific, like text/json.
            //

            Response.ContentType = "text/plain";
            
            //
            // Convert the query string into a call object.
            //

            JsonWriter writer = new JsonTextWriter();
            
            writer.WriteStartObject();
            
            writer.WriteMember("id");
            writer.WriteNumber(0);
            
            writer.WriteMember("method");
            string methodName = Mask.NullString(Request.PathInfo);
            if (methodName.Length == 0)
                writer.WriteNull();
            else
                writer.WriteString(methodName.Substring(1));
            
            writer.WriteMember("params");
            writer.WriteStartObject();

            NameValueCollection query = Request.QueryString;
            
            if (query.HasKeys())
            {
                foreach (string name in query)
                {
                    if (Mask.NullString(name).Length == 0)
                        continue;
                
                    writer.WriteMember(name);

                    string[] values = query.GetValues(name);                    
                    
                    if (values.Length == 0)
                        writer.WriteNull();
                    else if (values.Length == 1)
                        writer.WriteString(values[0]);
                    else 
                        writer.WriteArray(values);
                }
            }
            
            writer.WriteEndObject();
            
            writer.WriteEndObject();
            
            //
            // Delegate rest of the work to JsonRpcDispatcher.
            //

            JsonRpcDispatcher dispatcher = new JsonRpcDispatcher(Service);
            
            if (HttpRequestSecurity.IsLocal(Request))
                dispatcher.SetLocalExecution();
            
            dispatcher.Process(new StringReader(writer.ToString()), Response.Output);
        }