private Uri CreateRemoteUri(MappingElement mapping, IDictionary <string, string> tokens) { Uri remoteUri; string uri = ReplaceTokens(mapping.TargetURI, tokens); remoteUri = new Uri(uri); return(remoteUri); }
void IHttpHandler.ProcessRequest(HttpContext context) { MappingElement mapping = null; Uri outgoingUri; HttpWebResponse response; //Check Configuration if (Configuration == null) { throw new Exception("Unable to load Configuration"); } // Search for a matching mapping foreach (MappingElement map in Configuration.Mappings) { if (MatchUri(context.Request.Url, map.SourceURI, map.SourceRegexMatching)) { mapping = map; break; } } // Return 404 if can't find mapping if (mapping == null) { Utility.Return404(context); return; } // Create destination mapping, this includes GET query as well outgoingUri = CreateRemoteUri(mapping, GenerateTokensFromRequest(context.Request)); HttpWebRequest outgoing = (HttpWebRequest)WebRequest.Create(outgoingUri); // Credentials // TODO: Impliment Credential pass-through //request.Credentials = CredentialCache.DefaultCredentials; // Headers Utility.CopyHeaders(context.Request, outgoing); // Copy POST Data if (mapping.SourceIncludePost && (context.Request.RequestType == "POST")) { outgoing.ContentLength = context.Request.ContentLength; Utility.CopyStream(context.Request.InputStream, outgoing.GetRequestStream()); } try { response = (HttpWebResponse)outgoing.GetResponse(); } catch (WebException ex) { Utility.Return500(context, ex); return; } Stream receiveStream = response.GetResponseStream(); //Copy some headers, not too many since I'm not against hiding internal details ;) //TODO: copy cookies? context.Response.ContentType = response.ContentType; // Copy Content Encoding //context.Response.ContentEncoding = Encoding. //context.Response. // Do any parsing of HTML (or anything with URLs) here if (!string.IsNullOrEmpty(mapping.RewriteContent) && ((response.ContentType.ToLower().IndexOf("html") >= 0) || (response.ContentType.ToLower().IndexOf("javascript") >= 0)) ) { string sResp = Utility.ConvertStream(receiveStream); sResp = RewriteContent(sResp, mapping.RewriteContent); context.Response.Write(sResp); } else { // Output without rewriting, this will offer the best performance and lowest memory useage. Utility.CopyStream(receiveStream, context.Response.OutputStream); } response.Close(); context.Response.End(); }