internal ApiBatchResponse ProcessBatchRequest(HttpContextBase context, ApiBatchRequest apiBatchRequest) { if (context.Request == null) { throw new InvalidOperationException("Request is empty"); } if (context.Request.Url == null) { throw new InvalidOperationException("Url is empty"); } using (var writer = new StringWriter()) { var path = apiBatchRequest.RelativeUrl; if (context.Request.ApplicationPath != null && path.StartsWith(context.Request.ApplicationPath)) { path = path.Substring(context.Request.ApplicationPath.Length); } var uri = new Uri(context.Request.Url, "/" + path.TrimStart('/')); var workerRequest = new ApiWorkerRequest(Uri.UnescapeDataString(uri.AbsolutePath.TrimStart('/')), uri.Query.TrimStart('?'), writer, context, new ContentType(apiBatchRequest.BodyContentType)); workerRequest.HttpVerb = apiBatchRequest.Method; if (!string.IsNullOrEmpty(apiBatchRequest.Body)) { var contentType = new ContentType(apiBatchRequest.BodyContentType); var encoding = Encoding.GetEncoding(contentType.CharSet); workerRequest.EntityBody = encoding.GetBytes(apiBatchRequest.Body); } var workContext = new HttpContext(workerRequest) { Handler = this }; var newContext = new HttpContextWrapper(workContext); //Make a faked request var routeData = RouteTable.Routes.GetRouteData(newContext); if (routeData != null) { //Construct new context Container.BeginLifetimeScope().Resolve <IApiHttpHandler>(new TypedParameter(typeof(RouteData), routeData)).Process(newContext); newContext.Response.Flush(); //Form response var response = new ApiBatchResponse(apiBatchRequest) { Data = writer.GetStringBuilder().ToString(), Headers = new ItemDictionary <string, string>() }; foreach (var responseHeaderKey in workerRequest.ResponseHeaders.AllKeys) { response.Headers.Add(responseHeaderKey, workerRequest.ResponseHeaders[responseHeaderKey]); } response.Status = workerRequest.HttpStatus; response.Name = apiBatchRequest.Name; return(response); } } return(null); }
internal ApiBatchResponse ProcessBatchRequest(HttpContextBase context, ApiBatchRequest apiBatchRequest) { if (context.Request == null) throw new InvalidOperationException("Request is empty"); if (context.Request.Url == null) throw new InvalidOperationException("Url is empty"); using (var writer = new StringWriter()) { var path = apiBatchRequest.RelativeUrl; if (context.Request.ApplicationPath != null && path.StartsWith(context.Request.ApplicationPath)) { path = path.Substring(context.Request.ApplicationPath.Length); } var uri = new Uri(context.Request.Url,"/"+path.TrimStart('/')); var workerRequest = new ApiWorkerRequest(Uri.UnescapeDataString(uri.AbsolutePath.TrimStart('/')),uri.Query.TrimStart('?'), writer, context, new ContentType(apiBatchRequest.BodyContentType)); workerRequest.HttpVerb = apiBatchRequest.Method; if (!string.IsNullOrEmpty(apiBatchRequest.Body)) { var contentType = new ContentType(apiBatchRequest.BodyContentType); var encoding = Encoding.GetEncoding(contentType.CharSet); workerRequest.EntityBody = encoding.GetBytes(apiBatchRequest.Body); } var workContext = new HttpContext(workerRequest); var newContext = new HttpContextWrapper(workContext); //Make a faked request var routeData = RouteTable.Routes.GetRouteData(newContext); if (routeData != null) { //Construct new context Container.Resolve<IApiHttpHandler>(new DependencyOverride(typeof(RouteData), routeData)).Process(newContext); newContext.Response.Flush(); //Form response var response = new ApiBatchResponse(apiBatchRequest) { Data = writer.GetStringBuilder().ToString(), Headers = new ItemDictionary<string, string>() }; foreach (var responseHeaderKey in workerRequest.ResponseHeaders.AllKeys) { response.Headers.Add(responseHeaderKey, workerRequest.ResponseHeaders[responseHeaderKey]); } response.Status = workerRequest.HttpStatus; response.Name = apiBatchRequest.Name; return response; } } return null; }