private void EndGetContext(IAsyncResult result) { activeQuery++; Stopwatch t = new Stopwatch(); t.Start(); HttpListenerContext context = ((HttpListener)result.AsyncState).EndGetContext(result); if (getQuery) { Task.Factory.StartNew(() => { using (ImageTransformer image = new ImageTransformer(context)) { if (image.Validate()) { image.Transform(); } } t.Stop(); curTime += t.ElapsedMilliseconds; countQuery++; activeQuery--; Thread.Sleep(0); }); } else { context.Response.StatusCode = 409; context.Response.Close(); activeQuery--; } BeginGetContext(); }
protected override void HandleContext(HttpListenerContext listenerContext) { string url = listenerContext.Request.Url.ToString(); try { // Transform image string transformName = UrlPartsExtractor.ExtractTransformName(url); Image imageFromRequest = HttpImageExtractor.ExtractImage(listenerContext, imageMaxSizeConstraint, imageMaxWidthConstraint, imageMaxHeightConstraint); Image transformedImage = ImageTransformer.TransformImage(imageFromRequest, transformName); // Crop image CropArgs cropArgs = UrlPartsExtractor.ExtractCropArguments(url); Image croppedImage = ImageTransformer.CropImage(transformedImage, cropArgs.x, cropArgs.y, cropArgs.width, cropArgs.height); RespondWithOK(listenerContext.Response, croppedImage); } catch (Exception e) { if (e is NoTransformNameException || e is BadImageException || e is UnknownTransformException || e is BadCropArgumentsException || e is CropZeroSizeException) { RespondWithError(listenerContext.Response, BAD_REQUEST); } else if (e is EmptyIntersectionException) { RespondWithError(listenerContext.Response, NO_CONTENT); } else { RespondWithError(listenerContext.Response, INTERNAL_SERVER_ERROR); } return; } }