public void ProcessRequest(HttpContext context) { DirectProvider provider = this.GetProvider(context, this.ProviderName); string data = string.Empty; string type = "text/javascript"; // TotalBytes hangs sometimes, since it accesses the InputStream, which tries to complete loading in another thread // when not everything in the stream. // Use ContentLength instead... // This used to check for string.IsNullOrEmpty(context.Request["extAction"])) but in order to eliminate that from the hanging // candidates we've removed it, since we do not need form post at this stage. // When we do need it, making it more targetted may be an idea, such as content.Request.Form["extAction"] etc. if (context.Request.ContentLength == 0) { data = provider.ToString(); } else { DirectExecutionResponse resp = DirectProcessor.Execute(provider, context.Request, this.GetConverters()); data = resp.Data; if (resp.IsUpload) { type = "text/html"; } } context.Response.ContentType = type; context.Response.Write(data); }
public void ProcessRequest(HttpContext context) { DirectProvider provider = this.GetProvider(context, this.ProviderName); string data = string.Empty; string type = "text/javascript"; if (context.Request.TotalBytes == 0 && string.IsNullOrEmpty(context.Request["extAction"])) { data = provider.ToString(); } else { DirectExecutionResponse resp = DirectProcessor.Execute(provider, context.Request, this.GetConverters()); data = resp.Data; if (resp.IsUpload) { type = "text/html"; } } context.Response.ContentType = type; context.Response.Write(data); }
/// <summary> /// Processes an incoming request. /// </summary> /// <param name="provider">The provider that triggered the request.</param> /// <param name="httpRequest">The http information.</param> /// <returns>The result from the client method.</returns> internal static DirectExecutionResponse Execute(DirectProvider provider, HttpRequest httpRequest, IEnumerable<JsonConverter> converters) { List<DirectResponse> responses = new List<DirectResponse>(); if (!String.IsNullOrEmpty(httpRequest[DirectRequest.RequestFormAction])) { DirectRequest request = new DirectRequest() { Action = httpRequest[DirectRequest.RequestFormAction] ?? string.Empty, Method = httpRequest[DirectRequest.RequestFormMethod] ?? string.Empty, Type = httpRequest[DirectRequest.RequestFormType] ?? string.Empty, IsUpload = Convert.ToBoolean(httpRequest[DirectRequest.RequestFormUpload]), TransactionId = Convert.ToInt32(httpRequest[DirectRequest.RequestFormTransactionId]), Data = new object[] { httpRequest } }; responses.Add(DirectProcessor.ProcessRequest(provider, request)); } else { UTF8Encoding encoding = new UTF8Encoding(); string json = encoding.GetString(httpRequest.BinaryRead(httpRequest.TotalBytes)); /************************************************************************************** skygreen:解决bug:Self referencing loop 参考:http://stackoverflow.com/questions/7397207/json-net-error-self-referencing-loop-detected-for-type **************************************************************************************/ if (substr_count(json,"data")>1) { List<DirectRequest> requests = JsonConvert.DeserializeObject<List<DirectRequest>>(json); if (requests.Count > 0) { JArray raw = JArray.Parse(json); int i = 0; foreach (DirectRequest request in requests) { request.RequestData = (JObject)raw[i]; responses.Add(DirectProcessor.ProcessRequest(provider, request)); ++i; } } else { DirectRequest request = JsonConvert.DeserializeObject<DirectRequest>(json); request.RequestData = JObject.Parse(json); responses.Add(DirectProcessor.ProcessRequest(provider, request)); } } else { DirectRequest request = JsonConvert.DeserializeObject<DirectRequest>(json); request.RequestData = JObject.Parse(json); responses.Add(DirectProcessor.ProcessRequest(provider, request)); } } DirectExecutionResponse response = new DirectExecutionResponse(); JsonSerializerSettings outputSettings = new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore, ReferenceLoopHandling =ReferenceLoopHandling.Ignore }; foreach (JsonConverter converter in converters) { outputSettings.Converters.Add(converter); } if (responses.Count > 1 || !responses[0].IsUpload) { response.Data = JsonConvert.SerializeObject(responses, Formatting.None, outputSettings); } else { response.IsUpload = true; string outputJson = JsonConvert.SerializeObject(responses[0], Formatting.None, outputSettings); response.Data = String.Format("<html><body><textarea>{0}</textarea></body></html>", outputJson.Replace(""", "\\"")); } return response; }
/// <summary> /// Processes an incoming request. /// </summary> /// <param name="provider">The provider that triggered the request.</param> /// <param name="httpRequest">The http information.</param> /// <returns>The result from the client method.</returns> internal static DirectExecutionResponse Execute(DirectProvider provider, HttpRequest httpRequest, IEnumerable<JsonConverter> converters) { List<DirectResponse> responses = new List<DirectResponse>(); if (!String.IsNullOrEmpty(httpRequest[DirectRequest.RequestFormAction])) { DirectRequest request = new DirectRequest() { Action = httpRequest[DirectRequest.RequestFormAction] ?? string.Empty, Method = httpRequest[DirectRequest.RequestFormMethod] ?? string.Empty, Type = httpRequest[DirectRequest.RequestFormType] ?? string.Empty, IsUpload = Convert.ToBoolean(httpRequest[DirectRequest.RequestFormUpload]), TransactionId = Convert.ToInt32(httpRequest[DirectRequest.RequestFormTransactionId]), Data = new object[] { httpRequest } }; responses.Add(DirectProcessor.ProcessRequest(provider, request)); } else { string json = null; using (var reader = new StreamReader(httpRequest.InputStream, Encoding.UTF8)) { json = reader.ReadToEnd(); } try { // Force into an array shape if (!json.StartsWith("[")) { json = String.Format("[{0}]", json); } // Get raw array data JArray raw = JArray.Parse(json); // And also deserialize the requests List<DirectRequest> requests = JsonConvert.DeserializeObject<List<DirectRequest>>(json); int i = 0; foreach (DirectRequest request in requests) { request.RequestData = (JObject)raw[i]; responses.Add(DirectProcessor.ProcessRequest(provider, request)); ++i; } } catch (Exception ex) { responses.Add(new DirectResponse(String.Format("An exception occurred when attempting to decode the requests: {0}", ex))); } } DirectExecutionResponse response = new DirectExecutionResponse(); JsonSerializerSettings outputSettings = new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() }; foreach (JsonConverter converter in converters) { outputSettings.Converters.Add(converter); } // Updated this to guard against having no responses at all. // Was before I added the above else, but good practice anyway... if (responses.Count > 1 || ((responses.Count > 0) && !responses[0].IsUpload)) { response.Data = JsonConvert.SerializeObject(responses, Formatting.None, outputSettings); } else if (responses.Count > 0) { response.IsUpload = true; string outputJson = JsonConvert.SerializeObject(responses[0], Formatting.None, outputSettings); response.Data = String.Format("<html><body><textarea>{0}</textarea></body></html>", outputJson.Replace(""", "\\"")); } return response; }
/// <summary> /// Processes an incoming request. /// </summary> /// <param name="provider">The provider that triggered the request.</param> /// <param name="httpRequest">The http information.</param> /// <returns>The result from the client method.</returns> internal static DirectExecutionResponse Execute(DirectProvider provider, HttpRequest httpRequest, IEnumerable <JsonConverter> converters) { List <DirectResponse> responses = new List <DirectResponse>(); if (!String.IsNullOrEmpty(httpRequest[DirectRequest.RequestFormAction])) { DirectRequest request = new DirectRequest() { Action = httpRequest[DirectRequest.RequestFormAction] ?? string.Empty, Method = httpRequest[DirectRequest.RequestFormMethod] ?? string.Empty, Type = httpRequest[DirectRequest.RequestFormType] ?? string.Empty, IsUpload = Convert.ToBoolean(httpRequest[DirectRequest.RequestFormUpload]), TransactionId = Convert.ToInt32(httpRequest[DirectRequest.RequestFormTransactionId]), Data = new object[] { httpRequest } }; responses.Add(DirectProcessor.ProcessRequest(provider, request)); } else { UTF8Encoding encoding = new UTF8Encoding(); string json = encoding.GetString(httpRequest.BinaryRead(httpRequest.TotalBytes)); List <DirectRequest> requests = JsonConvert.DeserializeObject <List <DirectRequest> >(json); if (requests.Count > 0) { JArray raw = JArray.Parse(json); int i = 0; foreach (DirectRequest request in requests) { request.RequestData = (JObject)raw[i]; responses.Add(DirectProcessor.ProcessRequest(provider, request)); ++i; } } else { DirectRequest request = JsonConvert.DeserializeObject <DirectRequest>(json); request.RequestData = JObject.Parse(json); responses.Add(DirectProcessor.ProcessRequest(provider, request)); } } DirectExecutionResponse response = new DirectExecutionResponse(); JsonSerializerSettings outputSettings = new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore }; foreach (JsonConverter converter in converters) { outputSettings.Converters.Add(converter); } if (responses.Count > 1 || !responses[0].IsUpload) { response.Data = JsonConvert.SerializeObject(responses, Formatting.None, outputSettings); } else { response.IsUpload = true; string outputJson = JsonConvert.SerializeObject(responses[0], Formatting.None, outputSettings); response.Data = String.Format("<html><body><textarea>{0}</textarea></body></html>", outputJson.Replace(""", "\\"")); } return(response); }
/// <summary> /// Processes an incoming request. /// </summary> /// <param name="provider">The provider that triggered the request.</param> /// <param name="httpRequest">The http information.</param> /// <returns>The result from the client method.</returns> internal static DirectExecutionResponse Execute(DirectProvider provider, HttpRequest httpRequest, IEnumerable <JsonConverter> converters) { List <DirectResponse> responses = new List <DirectResponse>(); if (!String.IsNullOrEmpty(httpRequest[DirectRequest.RequestFormAction])) { DirectRequest request = new DirectRequest() { Action = httpRequest[DirectRequest.RequestFormAction] ?? string.Empty, Method = httpRequest[DirectRequest.RequestFormMethod] ?? string.Empty, Type = httpRequest[DirectRequest.RequestFormType] ?? string.Empty, IsUpload = Convert.ToBoolean(httpRequest[DirectRequest.RequestFormUpload]), TransactionId = Convert.ToInt32(httpRequest[DirectRequest.RequestFormTransactionId]), Data = new object[] { httpRequest } }; responses.Add(DirectProcessor.ProcessRequest(provider, request)); } else { string json = null; using (var reader = new StreamReader(httpRequest.InputStream, Encoding.UTF8)) { json = reader.ReadToEnd(); } try { // Force into an array shape if (!json.StartsWith("[")) { json = String.Format("[{0}]", json); } // Get raw array data JArray raw = JArray.Parse(json); // And also deserialize the requests List <DirectRequest> requests = JsonConvert.DeserializeObject <List <DirectRequest> >(json); int i = 0; foreach (DirectRequest request in requests) { request.RequestData = (JObject)raw[i]; responses.Add(DirectProcessor.ProcessRequest(provider, request)); ++i; } } catch (Exception ex) { responses.Add(new DirectResponse(String.Format("An exception occurred when attempting to decode the requests: {0}", ex))); } } DirectExecutionResponse response = new DirectExecutionResponse(); JsonSerializerSettings outputSettings = new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() }; foreach (JsonConverter converter in converters) { outputSettings.Converters.Add(converter); } // Updated this to guard against having no responses at all. // Was before I added the above else, but good practice anyway... if (responses.Count > 1 || ((responses.Count > 0) && !responses[0].IsUpload)) { response.Data = JsonConvert.SerializeObject(responses, Formatting.None, outputSettings); } else if (responses.Count > 0) { response.IsUpload = true; string outputJson = JsonConvert.SerializeObject(responses[0], Formatting.None, outputSettings); response.Data = String.Format("<html><body><textarea>{0}</textarea></body></html>", outputJson.Replace(""", "\\"")); } return(response); }