public IAsyncResult BeginGetString(string url, int cacheDuration, AsyncCallback cb, object state) { return AspectF.Define .Log(Services.Get<ILogger>(), "BeginGetString Url: {0} cache: {1}", url, cacheDuration) .Return<IAsyncResult>(() => { // See if the response from the URL is already cached on server string cachedContent = Services.Get<ICache>().Get(CACHE_KEY + url) as string; if (!string.IsNullOrEmpty(cachedContent)) { this.CacheResponse(Context, cacheDuration); return new AsmxHandlerSyncResult(cachedContent); } HttpWebRequest request = this.CreateHttpWebRequest(url); // As we will stream the response, don't want to automatically decompress the content request.AutomaticDecompression = DecompressionMethods.None; GetStringState myState = new GetStringState(state); myState.Request = request; myState.Url = url; myState.CacheDuration = cacheDuration; return request.BeginGetResponse(cb, myState); }); }
public IAsyncResult BeginGetString(string url, int cacheDuration, AsyncCallback cb, object state) { return(AspectF.Define .Log(Services.Get <ILogger>(), "BeginGetString Url: {0} cache: {1}", url, cacheDuration) .Return <IAsyncResult>(() => { // See if the response from the URL is already cached on server string cachedContent = Services.Get <ICache>().Get(CACHE_KEY + url) as string; if (!string.IsNullOrEmpty(cachedContent)) { this.CacheResponse(Context, cacheDuration); return new AsmxHandlerSyncResult(cachedContent); } HttpWebRequest request = this.CreateHttpWebRequest(url); // As we will stream the response, don't want to automatically decompress the content request.AutomaticDecompression = DecompressionMethods.None; GetStringState myState = new GetStringState(state); myState.Request = request; myState.Url = url; myState.CacheDuration = cacheDuration; return request.BeginGetResponse(cb, myState); })); }
public string EndGetString(IAsyncResult result) { GetStringState state = result.AsyncState as GetStringState; HttpWebRequest request = state.Request; using (HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse) { using (StreamReader reader = new StreamReader(response.GetResponseStream())) { string content = reader.ReadToEnd(); state.Context.Cache.Insert(state.Url, content, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(state.CacheDuration), CacheItemPriority.Normal, null); // produce cache headers for response caching CacheResponse(state.Context, state.CacheDuration); return(content); } } }
public IAsyncResult BeginGetString(string url, int cacheDuration, AsyncCallback cb, object state) { // See if the response from the URL is already cached on server string cachedContent = Context.Cache[CACHE_KEY + url] as string; if (!string.IsNullOrEmpty(cachedContent)) { this.CacheResponse(Context, cacheDuration); return new AsmxHandlerSyncResult(cachedContent); } HttpWebRequest request = this.CreateHttpWebRequest(url); // As we will stream the response, don't want to automatically decompress the content request.AutomaticDecompression = DecompressionMethods.None; GetStringState myState = new GetStringState(state); myState.Request = request; myState.Url = url; myState.CacheDuration = cacheDuration; return request.BeginGetResponse(cb, myState); }
public IAsyncResult BeginGetString(string url, int cacheDuration, AsyncCallback cb, object state) { // See if the response from the URL is already cached on server string cachedContent = Context.Cache[CACHE_KEY + url] as string; if (!string.IsNullOrEmpty(cachedContent)) { CacheResponse(Context, cacheDuration); return(new AsmxHandlerSyncResult(cachedContent)); } HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; GetStringState myState = new GetStringState(state); myState.Request = request; myState.Url = url; myState.CacheDuration = cacheDuration; return(request.BeginGetResponse(cb, myState)); }
public string EndGetString(IAsyncResult result) { GetStringState state = result.AsyncState as GetStringState; return(AspectF.Define .MustBeNonNull(state) .Log(Services.Get <ILogger>(), "EndGetString Url: {0} cache: {1}", state.Url, state.CacheDuration) .Return <string>(() => { MemoryStream responseBuffer = new MemoryStream(); HttpWebRequest request = state.Request; using (HttpWebResponse response = request.EndGetResponse(result) as HttpWebResponse) { using (Stream stream = response.GetResponseStream()) { // produce cache headers for response caching this.CacheResponse(state.Context, state.CacheDuration); string contentLength = response.GetResponseHeader("Content-Length") ?? "-1"; state.Context.Response.AppendHeader("Content-Length", contentLength); string contentEncoding = response.GetResponseHeader("Content-Encoding") ?? ""; state.Context.Response.AppendHeader("Content-Encoding", contentEncoding); state.Context.Response.ContentType = response.ContentType; const int BUFFER_SIZE = 4 * 1024; byte[] buffer = new byte[BUFFER_SIZE]; int dataReceived; while ((dataReceived = stream.Read(buffer, 0, BUFFER_SIZE)) > 0) { if (!state.Context.Response.IsClientConnected) { return string.Empty; } // Transmit to client (browser) immediately byte[] outBuffer = new byte[dataReceived]; Array.Copy(buffer, outBuffer, dataReceived); state.Context.Response.BinaryWrite(outBuffer); //state.Context.Response.Flush(); // Store in buffer so that we can cache the whole stuff responseBuffer.Write(buffer, 0, dataReceived); } responseBuffer.Position = 0; // If the content is compressed, decompress it Stream contentStream = contentEncoding == "gzip" ? (new GZipStream(responseBuffer, CompressionMode.Decompress) as Stream) : (contentEncoding == "deflate" ? (new DeflateStream(responseBuffer, CompressionMode.Decompress) as Stream) : (responseBuffer as Stream)); // Cache the decompressed content so that we can return it next time using (StreamReader reader = new StreamReader(contentStream, true)) { string content = reader.ReadToEnd(); Services.Get <ICache>().Add(CACHE_KEY + state.Url, content); } state.Context.Response.Flush(); return null; } } })); }