/* * Returns true if the url has a query string that * begins with precisely the string "xml". */ public static bool useXml(string url) { if (StringTools.IsTrivial(url)) { return(false); } int qmark = url.IndexOf('?'); if (qmark < 0) { return(false); } string query = url.Substring(qmark + 1); if (query.Length < 3) { return(false); } query = query.Substring(0, 3); return(query == "xml"); }
/// <summary> /// Adds an open tag with the given tag name to the /// given StringBuilder builder. /// /// Then shuts the tag at the end with /> /// /// Adds the attributes list if non-trivial. /// /// The attributes list should be a space-separated /// string of the form: /// /// attribute-name='attribute-value' /// /// Adds a newline if requested. /// /// Does nothing if the tag name is trivial. /// </summary> /// <param name="builder">The builder to collect the markup</param> /// <param name="tagname">The tag name</param> /// <param name="attributes">Optional attributes</param> /// <param name="newline">If true append newline</param> public static void OpenShutTag (StringBuilder builder, string tagname, string attributes, bool newline) { if (StringTools.IsTrivial(tagname)) { return; } builder.Append("<"); builder.Append(tagname); if (!StringTools.IsTrivial(attributes)) { builder.Append(" "); builder.Append(attributes); } builder.Append(" />"); if (newline) { builder.Append("\n"); } }
/// <summary> /// Returns true if the tildeFilePath corresponds to a file that /// exists and is a text file. /// /// In that case, the filePath and info parameters are initialized. /// </summary> /// <param name="server">The server to convert to real file paths</param> /// <param name="tildeFilePath">The tilde file path</param> /// <param name="filePath">The real file path</param> /// <param name="info">The FileInfo object</param> public static bool TildeFilePathExistsAndIsText (HttpServerUtility server, string tildeFilePath, ref string filePath, ref FileInfo info) { bool error = StringTools.IsTrivial(tildeFilePath); if (!error) { error = !tildeFilePath.StartsWith("~/"); } if (!error) { int category = FileTools.GetFileCategory(tildeFilePath); error = category != FileTools.TEXT; } if (!error) { try { filePath = server.MapPath(tildeFilePath); info = new FileInfo(filePath); long bytes = info.Length; } catch { error = true; } } return(!error); }
/// <summary> /// Create the HTML page markup given: /// /// The language abbreviation for the html tag. /// /// The page title. /// /// Markup for the head. /// /// Attributes for the body. /// /// Markup for the body. /// /// In the head, use <meta charset='utf-8' />. /// /// If lang is trivial, use "en". /// /// If any other parameter is trivial, add nothing for that /// parameter. /// </summary> public static string MakePageMarkup (string lang, string title, string headMarkup, string bodyAttributes, string bodyMarkup) { StringBuilder builder = new StringBuilder(); builder.Append(htmlDoctype); if (StringTools.IsTrivial(lang)) { builder.Append(open_html); } else { builder.Append("\n<html lang='"); builder.Append(lang); builder.Append("'>\n"); } // head builder.Append(open_head); builder.Append(standard_meta); if (!StringTools.IsTrivial(title)) { builder.Append(open_title); builder.Append(title); builder.Append(shut_title); } if (!StringTools.IsTrivial(headMarkup)) { builder.Append(headMarkup); } builder.Append(shut_head); // body if (StringTools.IsTrivial(bodyAttributes)) { builder.Append(open_body); } else { OpenTag(builder, "body", bodyAttributes); } if (!StringTools.IsTrivial(bodyMarkup)) { builder.Append(bodyMarkup); } builder.Append(shut_body); builder.Append(shut_html); return(builder.ToString()); }
/* * Functions to build simple HTML tables. */ /// <summary> /// Append the start of an HTML table with the given border, /// padding, and optional attributes /// to the given StringBuilder builder. /// </summary> /// <param name="builder">The builder to collect the markup</param> /// <param name="border">The border size if positive</param> /// <param name="padding">The cellpadding size if positive</param> /// <param name="attributes">Optional attributes</param> public static void AppendTableStart (StringBuilder builder, int border, int padding, string attributes) { builder.Append("\n<table"); if (border > 0) { builder.Append(" border='"); builder.Append(border); builder.Append("'"); } if (padding > 0) { builder.Append(" cellpadding='"); builder.Append(padding); builder.Append("'"); } if (!StringTools.IsTrivial(attributes)) { builder.Append(" "); builder.Append(attributes); } builder.Append(">\n"); }
/// <summary> /// Returns the markup from the search /// of either the public directories /// or all directories. /// </summary> /// <param name="page"> /// The page calling this method</param> /// <param name="pattern"> /// The search pattern</param> /// <param name="isRegex"> /// Is the pattern a regular expression?</param> /// <param name="ignoreCase"> /// Ignore case in the search?</param> /// <param name="statistics"> /// Include file statistics markup?</param> /// <param name="download"> /// Include download button markup?</param> /// <param name="onlyPublic"> /// Whether or not to restrict to public directories</param> public static string SearchSiteMarkup (Page page, string pattern, bool isRegex, bool ignoreCase, bool statistics, bool download, bool onlyPublic) { StringBuilder builder = new StringBuilder(); string rootPath = FileTools.GetRoot(page); List <string> directoryList = SourceTools.MakeDirectoryList(rootPath, onlyPublic); List <string> tildeDirectoryList = FileTools.GetTildePaths(rootPath, directoryList); foreach (string tildeDirectoryPath in tildeDirectoryList) { string markup = SearchDirectoryMarkup(page, tildeDirectoryPath, pattern, isRegex, ignoreCase, statistics, download, onlyPublic); if (!StringTools.IsTrivial(markup)) { builder.Append(markup); } } return(builder.ToString()); }
/// Returns the script code to reference the Javascript /// file at the given path. /// /// If the path is a tilde path, use the context to /// create the correct url via MakeMergedPath. /// /// Otherwise, assume the path is a correct url as is. /// Further, in this case, one may pass null for the /// context parameter. /// /// If an error occurs returns the empty string. public static string MakeJavascriptReference (HttpContext context, string path) { if (StringTools.IsTrivial(path)) { return(""); } if (path.StartsWith(SourceTools.tildeStart)) { if (context == null) { return(""); } path = MakeMergedPath(context, path); } StringBuilder builder = new StringBuilder(); builder.Append(HTML_Tools.script_file_1); builder.Append(path); builder.Append(HTML_Tools.script_file_2); return(builder.ToString()); }
/// <summary> /// Returns the style sheet link code to reference /// the given css path with the given media setting. /// /// If the path is a tilde path, use the context to /// create the correct url via MakeMergedPath. /// /// Otherwise, assume the path is a correct url as is. /// Further, in this case, one may pass null for the /// context parameter. /// /// If the media string is null or empty, it is ignored. /// /// If an error occurs returns the empty string. /// </summary> public static string MakeStyleSheetLink (HttpContext context, string path, string media) { if (StringTools.IsTrivial(path)) { return(""); } if (path.StartsWith(SourceTools.tildeStart)) { if (context == null) { return(""); } path = MakeMergedPath(context, path); } StringBuilder builder = new StringBuilder(); builder.Append(HTML_Tools.open_css_link); builder.Append(path); if (!StringTools.IsTrivial(media)) { builder.Append(HTML_Tools.media_css); builder.Append(media); } builder.Append(HTML_Tools.shut_css_link); return(builder.ToString()); }
/// <summary> /// Uses context.Request.ApplicationPath /// /// Returns context.Request.ApplicationPath with a trailing /// slash appended if the path does not end in a slash. /// /// If an error occurs returns the empty string. /// </summary> public static string GetApplicationPathWithSlash(HttpContext context) { if (context == null) { return(""); } string s = context.Request.ApplicationPath; if (StringTools.IsTrivial(s)) { return(""); } int n = s.Length - 1; char slash = FileTools.slash; if (s[n] == slash) { return(s); } else { return(s + slash); } }
/* * Returns the HttpWebResponse from executing a * WebRequest using the given url. * * May return null if a serious error occurs. * * May return a response with a status code that * is other than 200 for OK. * * * This static method may be used standalone if * you wish to process a web response in C# code * on the server. */ public static HttpWebResponse GetResponse(string url) { HttpWebResponse response = null; if (StringTools.IsTrivial(url)) { return(response); } try { Uri uri = new Uri(url); HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest; request.Date = DateTime.Now; response = request.GetResponse() as HttpWebResponse; } catch { } return(response); }
/// <summary> /// Returns the response category as one of three values /// FileTools.TEXT /// FileTools.IMAGE /// FileTools.OTHER /// /// First tests the response object. /// /// If response.ContentType determines that the value is /// TEXT or IMAGE then that value is returned. /// /// Otherwise, uri.FileExtension is tested using /// FileTools.GetFileCategory /// /// If that determines the answer then that is returned. /// /// Otherwise, FileTools.OTHER is returned. /// /// Assumption: The response is the result of requesting /// the uri. /// </summary> /// <param name="uri">The uri to request</param> /// <param name="response">The response to the request</param> public static int ResponseCategory (UriPlus uri, HttpWebResponse response) { int category = FileTools.OTHER; // First test response.ContentType if (response != null) { string contentType = response.ContentType; if (!StringTools.IsTrivial(contentType)) { if (contentType.StartsWith("text")) { category = FileTools.TEXT; } else if (contentType.StartsWith("image")) { category = FileTools.IMAGE; } else if (contentType.Contains("javascript")) { category = FileTools.TEXT; } else if (contentType.Contains("json")) { category = FileTools.TEXT; } else if (contentType.Contains("htm")) { category = FileTools.TEXT; } else if (contentType.StartsWith("application/xml")) { category = FileTools.TEXT; } } } // Only test uri.FileExtension if category did not change if (category == FileTools.OTHER) { if (uri != null) { string extension = uri.FileExtension; if (!StringTools.IsTrivial(extension)) { category = FileTools.GetFileCategory(extension); } } } return(category); }
/// <summary> /// Returns x sandwiched between open and shut pre tags /// with no automatic HTML encoding. /// /// Returns an empty string if x is trivial. /// </summary> /// <param name="x">The text to sandwich</param> public static string pre(string x) { if (StringTools.IsTrivial(x)) { return(""); } return(open_pre + x + shut_pre); }
/// <summary> /// Returns x sandwiched between open and shut code tags /// with no automatic HTML encoding. /// /// Returns an empty string if x is trivial. /// </summary> /// <param name="x">The text to sandwich</param> public static string code(string x) { if (StringTools.IsTrivial(x)) { return(""); } return(open_code + x + shut_code); }
/// <summary> /// Returns the response category as one of three values /// FileTools.TEXT /// FileTools.IMAGE /// FileTools.OTHER /// /// Tests the response object. /// /// If response.ContentType determines that the value is /// TEXT or IMAGE then that value is returned. /// /// Otherwise, OTHER is returned. /// /// /// Assumption: The response is the result of requesting /// some uri. /// </summary> /// <param name="response">The response to the request</param> public static int ResponseCategory(HttpWebResponse response) { int category = FileTools.OTHER; if (response == null) { return(category); } string contentType = response.ContentType; if (!StringTools.IsTrivial(contentType)) { if (contentType.StartsWith("text")) { category = FileTools.TEXT; } else if (contentType.StartsWith("image")) { category = FileTools.IMAGE; } else if (contentType.Contains("htm")) { category = FileTools.TEXT; } else if (contentType.Contains("xml")) { category = FileTools.TEXT; } else if (contentType.Contains("javascript")) { category = FileTools.TEXT; } else if (contentType.Contains("json")) { category = FileTools.TEXT; } else if (contentType.StartsWith("application")) { string[] types = GetApplicationMediaTypesThatAreText(); foreach (string t in types) { if (contentType.StartsWith(t)) { category = FileTools.TEXT; break; } } } } return(category); }
/// <summary> /// Returns the markup for the list of servable files /// in the same directory /// as the context web directory. /// /// Each file name will link to the file so that it /// may be served. /// /// The optional comment heads the list of file names. /// If omitted, a default comment is constructed. /// </summary> /// <param name="context">Web site HttpContext object</param> /// <param name="comment">Optional comment to head list</param> public static string MakeServableListMarkup (HttpContext context, string comment) { if (context == null) { return(""); } List <string> servablelist = HttpContextTools.MakeServableList(context); int count = servablelist.Count; if (count == 0) { return(""); } StringBuilder builder = new StringBuilder(); builder.Append(HTML_Tools.open_p); if (StringTools.IsTrivial(comment)) { string tildePath = HttpContextTools.GetTildeDirectoryPath(context); builder.Append("Servable files in "); builder.Append(tildePath); builder.Append(" ["); builder.Append(count); builder.Append("]:"); } else { builder.Append(comment); } builder.Append(HTML_Tools.shut_p); foreach (string name in servablelist) { builder.Append(HTML_Tools.open_div); builder.Append(HTML_Tools.open_anchor); builder.Append(name); builder.Append(HTML_Tools.mid_anchor_blank); builder.Append(HTML_Tools.open_code); builder.Append(name); builder.Append(HTML_Tools.shut_code); builder.Append(HTML_Tools.shut_anchor); builder.Append(HTML_Tools.shut_div); } return(builder.ToString()); }
/// <summary> /// Returns x sandwiched between open and shut code tags. /// /// If encode is true, first HTML encode x. /// /// Returns an empty string if x is trivial. /// </summary> /// <param name="x">The text to sandwich</param> /// <param name="encode">If true HTML encode x</param> public static string code(string x, bool encode) { if (StringTools.IsTrivial(x)) { return(""); } x = encode ? StringTools.HtmlEncode(x) : x; return(open_code + x + shut_code); }
/// <summary> /// Adds feedback header markup constructed as follows /// to the given StringBuilder builder. /// /// If the feedback header is non-trivial then the /// header is inserted in a p-tag with CSS class /// 'feedback'. /// /// Otherwise, does nothing. /// /// See the const string FeedbackCSS. /// </summary> /// <param name="builder">The builder to collect the markup</param> /// <param name="header">The feedback header</param> public static void FeedbackHeader (StringBuilder builder, string header) { if (StringTools.IsTrivial(header)) { return; } builder.Append(open_p_feedback); builder.Append(header); builder.Append(shut_p); }
/// <summary> /// Assuming that the given webpath is a string with /// names separated by slash characters and possibly /// a leading tilde, returns the substring after the /// final slash character. /// /// This method assumes that the filename is the last /// portion of the string after the final slash. /// /// Hence, extra stuff such as a tail, a query string, /// or a fragment identifier must already be stripped. /// /// If webpath is null returns the empty string. /// /// If webpath does not contain slash returns webpath. /// </summary> public static string GetFileName(string webpath) { if (StringTools.IsTrivial(webpath)) { return(""); } int spot = webpath.LastIndexOf(FileTools.slash); return((spot >= 0) ? webpath.Substring(spot + 1) : webpath); }
public TildePathInfo(HttpContext context, string tildeFilePath) { if (context == null) { return; } if (StringTools.IsTrivial(tildeFilePath)) { return; } CommonCode(context.Server, tildeFilePath); }
public TildePathInfo(HttpServerUtility server, string tildeFilePath) { if (server == null) { return; } if (StringTools.IsTrivial(tildeFilePath)) { return; } CommonCode(server, tildeFilePath); }
public TildePathInfo(Page page, string tildeFilePath) { if (page == null) { return; } if (StringTools.IsTrivial(tildeFilePath)) { return; } CommonCode(page.Server, tildeFilePath); }
public static string ToString(List <Range> list) { if (StringTools.IsTrivial(list)) { return(""); } StringBuilder builder = new StringBuilder(); foreach (Range range in list) { builder.Append(range.ToString()); } return(builder.ToString()); }
/// <summary> /// Returns a list of Range objects representing /// the matches of the pattern in the content /// using plain text matching. /// /// Matching begins at the start index. /// /// Ignores case if ignoreCase is true. /// </summary> /// <param name="content"> /// The content to search</param> /// <param name="pattern"> /// The search pattern</param> /// <param name="start"> /// The search start index</param> /// <param name="ignoreCase"> /// If true ignore case</param> public static List <Range> PlainSearch (string content, string pattern, int start, bool ignoreCase) { List <Range> list = new List <Range>(); if (StringTools.IsTrivial(content)) { return(list); } if (StringTools.IsTrivial(pattern)) { return(list); } StringComparison compare = ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; int contentLength = content.Length; int patternLength = pattern.Length; if (start < 0) { start = 0; } while (start < contentLength) { int index = content.IndexOf(pattern, start, compare); if (index < 0) { break; } list.Add(new Range(index, patternLength)); start = index + patternLength; } return(list); }
/// <summary> /// Returns a list of Range objects representing /// the matches of the pattern in the content /// using regular expression matching. /// /// Matching begins at the start index. /// /// Ignores case if ignoreCase is true. /// </summary> /// <param name="content"> /// The content to search</param> /// <param name="pattern"> /// The search pattern</param> /// <param name="start"> /// The search start index</param> /// <param name="ignoreCase"> /// If true ignore case</param> public static List <Range> RegexSearch (string content, string pattern, int start, bool ignoreCase) { List <Range> list = new List <Range>(); if (StringTools.IsTrivial(content)) { return(list); } if (StringTools.IsTrivial(pattern)) { return(list); } RegexOptions options = RegexOptions.Multiline; if (ignoreCase) { options |= RegexOptions.IgnoreCase; } Regex regex = new Regex(pattern, options); if (start < 0) { start = 0; } if (start < content.Length) { Match match = regex.Match(content, start); while (match.Success) { list.Add(new Range(match.Index, match.Length)); match = match.NextMatch(); } } return(list); }
/// <summary> /// Adds a shut tag with the given tag name to the /// given StringBuilder builder. /// /// Adds a newline if requested. /// /// Does nothing if the tag name is trivial. /// </summary> /// <param name="builder">The builder to collect the markup</param> /// <param name="tagname">The tag name</param> /// <param name="newline">If true append newline</param> public static void ShutTag (StringBuilder builder, string tagname, bool newline) { if (StringTools.IsTrivial(tagname)) { return; } builder.Append("</"); builder.Append(tagname); builder.Append(">"); if (newline) { builder.Append("\n"); } }
/// <summary> /// Returns true if response.ContentType contains "htm". /// /// This should pick out just the media types: /// text/html /// application/xhtml+xml /// /// We do not pass a uri since we will only consider a /// response to be HTML if its server asserts that. /// </summary> /// <param name="response">The response to a request</param> public static bool IsHtml(HttpWebResponse response) { if (response != null) { string contentType = response.ContentType; if (!StringTools.IsTrivial(contentType)) { if (contentType.Contains("htm")) { return(true); } } } return(false); }
/* * Provide error content to the HttpContext Response object. * * The embeddedUrl is the url that was being processed. * * The message is any additional information to provide. */ void ProcessRequestError (HttpContext context, string embeddedUrl, string message) { context.Response.Write("<!DOCTYPE html>\n"); context.Response.Write("<html>\n"); context.Response.Write("<head><title>SimpleProxy</title></head>\n"); context.Response.Write("<body><h1>SimpleProxy Error</h1>\n"); if (StringTools.IsTrivial(embeddedUrl)) { context.Response.Write("<h2>Needs an embedded Url in the query string</h2>\n"); context.Response.Write("<pre style='color: blue; font-size: 150%; margin-left: 5%'><b>"); context.Response.Write("?url=\"...\""); context.Response.Write("</b></pre>\n"); context.Response.Write("<h2>where ... is the url embedded between the delimiters.</h2>"); context.Response.Write("<h2>The delimiters may be single quote, double quote, or vertical bar.</h2>"); context.Response.Write("<h2>Documentation is <a href="); context.Response.Write("'http://net4.ccs.neu.edu/home/rasala/simpleproxy/'"); context.Response.Write(" target='_blank' style='text-decoration: none'>here</a>.</h2>\n"); } else { context.Response.Write("<h2>Embedded Url:</h2>\n"); embeddedUrl = StringTools.HtmlEncode(embeddedUrl); context.Response.Write("<pre style='color: blue; font-size: 150%; margin-left: 5%'><b>"); context.Response.Write(embeddedUrl); context.Response.Write("</b></pre>\n"); } if (!StringTools.IsTrivial(message)) { context.Response.Write("<div>"); context.Response.Write(message); context.Response.Write("</div>\n"); } context.Response.Write("</body>\n"); context.Response.Write("</html>\n"); }
/// <summary> /// Adds x sandwiched between open and shut code tags /// to the given StringBuilder builder. /// /// If encode is true, first HTML encode x. /// /// Does nothing if x is trivial. /// </summary> /// <param name="builder">The builder to collect the markup</param> /// <param name="x">The text to sandwich</param> /// <param name="encode">If true HTML encode x</param> public static void code (StringBuilder builder, string x, bool encode) { if (StringTools.IsTrivial(x)) { return; } builder.Append(open_code); if (encode) { x = StringTools.HtmlEncode(x); } builder.Append(x); builder.Append(shut_code); }
/* * Functions to build a generic HTML tag. */ /// <summary> /// Adds an open tag with the given tag name to the /// given StringBuilder builder. /// /// Adds the attributes list if non-trivial. /// /// The attributes list should be a space-separated /// string of the form: /// /// attribute-name='attribute-value' /// /// Does nothing if the tag name is trivial. /// </summary> /// <param name="builder">The builder to collect the markup</param> /// <param name="tagname">The tag name</param> /// <param name="attributes">Optional attributes</param> public static void OpenTag (StringBuilder builder, string tagname, string attributes) { if (StringTools.IsTrivial(tagname)) { return; } builder.Append("<"); builder.Append(tagname); if (!StringTools.IsTrivial(attributes)) { builder.Append(" "); builder.Append(attributes); } builder.Append(">"); }
public TildePathInfoComparer(string compare_mode) { if (StringTools.IsTrivial(compare_mode)) { return; } compare_mode = compare_mode.ToLower(); if (compare_mode == "size") { _compare_mode = "size"; } if (compare_mode == "date") { _compare_mode = "date"; } }