private static string GetDomain(IConfiguration settings) { var domain = new Uri(settings.GetSiteRoot(false)).DnsSafeHost; if (domain.IndexOf(':') >= 0) { domain = domain.Substring(0, domain.IndexOf(':')); } return domain; }
public static void TransformRobocode() { var w = new Uri("http://robocode.sourceforge.net/docs/robocode/allclasses-noframe.html").ToWebString(); var zip = new ZIPFile(); var o = 0; while (o >= 0) { const string pregfix = "<A HREF=\""; var i = w.IndexOf(pregfix, o); if (i >= 0) { var j = w.IndexOf("\"", i + pregfix.Length); if (j >= 0) { var type = w.Substring(i + pregfix.Length, j - (i + pregfix.Length)); const string suffix = ".html"; if (type.EndsWith(suffix)) { o = j + 1; type = type.Substring(0, type.Length - suffix.Length).Replace("/", "."); Console.WriteLine(type); zip.Add(type.Replace(".", "/") + ".cs", new DefinitionProvider( type // "robocode.BattleRules" // //"java.net.InetSocketAddress" // //"java.net.ServerSocket" // //"java.nio.channels.ServerSocketChannel" , k => k.ToWebString()).GetString() ); } else o = -1; } else o = -1; } else o = -1; } using (var ww = new BinaryWriter(File.OpenWrite("Robocode.zip"))) { zip.WriteTo(ww); } }
public bool BeforeRequest(Session session, Rule rule) { Console.WriteLine(String.Format("Request ({0}) cached due to the rule: {1}", session.hostname, rule.Name)); // TODO: // Cache-Control - Expires, MaxAge (private) // Age? // var querystring = new Uri(session.fullUrl).Query; var startindex = querystring.IndexOf("&url="); var length = querystring.IndexOf("&ei", startindex) - startindex; var url = querystring.Substring(startindex, length); session.fullUrl = Uri.UnescapeDataString(url); return false; }
protected void Page_Load(object sender, EventArgs e) { if (String.IsNullOrEmpty(Request.QueryString["provider"])) { throw new ArgumentNullException("provider"); } // build callback url string returnUrl = new Uri(Request.Url, "ExternalLoginResult.aspx").AbsoluteUri; // pop-up indicator for JavaScript code if (!String.IsNullOrEmpty(Request.QueryString["popup"])) { if (returnUrl.IndexOf("?") != -1) { returnUrl += "&"; } else { returnUrl += "?"; } returnUrl += "popup=true"; } // redirect to authorization page of the specified provider OAuthWeb.RedirectToAuthorization(Request.QueryString["provider"], returnUrl); }
/// <summary> /// 取得媒體ID /// </summary> /// <param name="Url">網址</param> /// <returns>媒體ID</returns> private string GetMediaId(string Url) { string result = new Uri(Url).Segments.Last<string>(); try { result = result.Substring(0, result.IndexOf("_")); } catch { } return result; }
public static void CreateImageFolder(this string itemName) { var path = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath; var mainPath = path.Substring(0, path.IndexOf("bin", 0)); var imgPath = string.Format(@"{0}\Content\images\item\{1}", mainPath, itemName); if (!Directory.Exists(imgPath)) Directory.CreateDirectory(imgPath); }
private HttpWebResponse Download (SafeUri uri, int nb_retries) { Hyena.ThreadAssist.AssertNotInMainThread (); while (true) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create (uri.AbsoluteUri); request.UserAgent = Banshee.Web.Browser.UserAgent; request.KeepAlive = false; request.Timeout = 5 * 1000; request.AllowAutoRedirect = true; // Parse out and set credentials, if any string user_info = new Uri (uri.AbsoluteUri).UserInfo; if (!String.IsNullOrEmpty (user_info)) { string username = String.Empty; string password = String.Empty; int cIndex = user_info.IndexOf (":"); if (cIndex != -1) { username = user_info.Substring (0, cIndex); password = user_info.Substring (cIndex + 1); } else { username = user_info; } request.Credentials = new NetworkCredential (username, password); } var response = (HttpWebResponse)request.GetResponse (); if (response.StatusCode == HttpStatusCode.GatewayTimeout) { throw new WebException ("", WebExceptionStatus.Timeout); } return response; } catch (WebException e) { if (e.Status == WebExceptionStatus.Timeout && nb_retries > 0) { nb_retries--; Log.InformationFormat ("Playlist download from {0} timed out, retrying in 1 second...", uri.AbsoluteUri); System.Threading.Thread.Sleep (1000); } else { throw; } } } }
private static string GetTrippinWebRoot(string serviceName) { var codeBase = new Uri(typeof(TrippinServiceFixture).Assembly.CodeBase).LocalPath; var parentPathLength = codeBase.IndexOf(EigenString) + EigenString.Length; return Path.Combine(codeBase.Substring(0, parentPathLength), serviceName); }
private async Task<List<ImageLink>> GetImgurImageOrAlbumFromUrl(Post post, string outputPath, CancellationToken token) { token.ThrowIfCancellationRequested(); var links = new List<ImageLink>(); OutputLine($"\tGetting links from post: {post.Title}", true); var url = new Uri(post.Url.ToString()).GetLeftPart(UriPartial.Path).Replace(".gifv", ".gif"); url = url.StartsWith("http://") ? url : "http://" + url.Substring(url.IndexOf(post.Url.DnsSafeHost, StringComparison.Ordinal)); var name = Path.GetInvalidFileNameChars().Aggregate(post.AuthorName, (current, c) => current.Replace(c, '-')); var filepath = outputPath + "\\"; if (_allAuthorsPosts) filepath += post.AuthorName; else filepath += post.SubredditName + "\\" + name; var filename = filepath + $"\\{name}_{post.SubredditName}_{post.Id}"; var extention = GetExtention(url); if (!string.IsNullOrEmpty(extention)) { OutputLine($"\tAdding Link {url}", true); links.Add(new ImageLink(post, url, filename)); return links; } string htmlString = await GetHtml(url, token); if (string.IsNullOrWhiteSpace(htmlString)) return links; var caroselAlbum = htmlString.Contains(@"data-layout=""h"""); if (caroselAlbum) { htmlString = await GetHtml(url + "/all", token); if (string.IsNullOrWhiteSpace(htmlString)) return links; } var gridAlbum = htmlString.Contains(@"data-layout=""g"""); if (caroselAlbum && !gridAlbum) return links; var regPattern = new Regex(@"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>", RegexOptions.IgnoreCase); var matchImageLinks = regPattern.Matches(htmlString); OutputLine($"\tFound {matchImageLinks.Count} image(s) from link.", true); foreach (Match m in matchImageLinks) { token.ThrowIfCancellationRequested(); var imgurl = m.Groups[1].Value.Replace(".gifv", ".gif"); if (!imgurl.Contains("imgur.com")) continue; if (gridAlbum) imgurl = imgurl.Remove(imgurl.LastIndexOf('.') - 1, 1); var domain = new Uri(imgurl).DnsSafeHost; imgurl = imgurl.StartsWith("http://") ? imgurl : "http://" + imgurl.Substring(imgurl.IndexOf(domain, StringComparison.Ordinal)); links.Add(new ImageLink(post, imgurl, filename)); } return links; }
/// <summary> /// fully resolves any relative pathing inside the URL, and other URL oddities /// </summary> string ResolveUrl(string url) { // resolve any relative pathing try { url = new Uri(url).AbsoluteUri; } catch (UriFormatException ex) { throw new ArgumentException("'" + url + "' does not appear to be a valid URL.", ex); } // remove any anchor tags from the end of URLs if (url.IndexOf("#") > -1) { string jump = Regex.Match(url, "/[^/]*?(?<jump>#[^/?.]+$)").Groups["jump"].Value; if (jump != "") url = url.Replace(jump, ""); } return url; }
public WEBDAV(string url, Dictionary<string, string> options) { Uri u = new Uri(url); if (!string.IsNullOrEmpty(u.UserInfo)) { m_userInfo = new System.Net.NetworkCredential(); if (u.UserInfo.IndexOf(":") >= 0) { m_userInfo.UserName = u.UserInfo.Substring(0, u.UserInfo.IndexOf(":")); m_userInfo.Password = u.UserInfo.Substring(u.UserInfo.IndexOf(":") + 1); } else { m_userInfo.UserName = u.UserInfo; if (options.ContainsKey("ftp-password")) m_userInfo.Password = options["ftp-password"]; } } else { if (options.ContainsKey("ftp-username")) { m_userInfo = new System.Net.NetworkCredential(); m_userInfo.UserName = options["ftp-username"]; if (options.ContainsKey("ftp-password")) m_userInfo.Password = options["ftp-password"]; } } m_useIntegratedAuthentication = Utility.Utility.ParseBoolOption(options, "integrated-authentication"); m_forceDigestAuthentication = Utility.Utility.ParseBoolOption(options, "force-digest-authentication"); m_useSSL = Utility.Utility.ParseBoolOption(options, "use-ssl"); m_url = (m_useSSL ? "https" : "http") + url.Substring(u.Scheme.Length); if (!m_url.EndsWith("/")) m_url += "/"; m_path = new Uri(m_url).PathAndQuery; if (m_path.IndexOf("?") > 0) m_path = m_path.Substring(0, m_path.IndexOf("?")); m_path = System.Web.HttpUtility.UrlDecode(m_path); m_rawurl = (m_useSSL ? "https://" : "http://") + u.Host + m_path; int port = u.Port; if (port <= 0) port = m_useSSL ? 443 : 80; m_rawurlPort = (m_useSSL ? "https://" : "http://") + u.Host + ":" + port + m_path; m_sanitizedUrl = (m_useSSL ? "https://" : "http://") + u.Host + m_path; m_reverseProtocolUrl = (m_useSSL ? "http://" : "https://") + u.Host + m_path; options.TryGetValue("debug-propfind-file", out m_debugPropfindFile); }
private static string GetRootSolutionDirectoryPath() { var codebase = new Uri(typeof(NestDeployer).Assembly.CodeBase).AbsolutePath; return codebase.Substring(0, codebase.IndexOf(kRootSolutionName) + kRootSolutionName.Length); }
/// <summary>Infuses the http handler (usually an aspx page) with the content page associated with the url if it implements the <see cref="IContentTemplate"/> interface.</summary> protected virtual void Application_AcquireRequestState(object sender, EventArgs e) { //TODO: Add ForceConsistentUrls property? Right now there is only this #if to turn it off by default. #if SAFE_URL_HANDLING if (webContext.CurrentPath == null || webContext.CurrentPath.IsEmpty()) return; HttpContext httpContext = ((HttpApplication)sender).Context; // jamestharpe: webContext.Request causes Obsolete warning. Uri requestBaseUrl = new Uri(string.Format("{0}{1}{2}", httpContext.Request.Url.Scheme, Uri.SchemeDelimiter, httpContext.Request.Url.Authority)); string rawUrl = new Uri(requestBaseUrl, httpContext.Request.RawUrl).ToString(), preferredUrl = GetPreferredUrl(webContext.CurrentPage, requestBaseUrl); if (!rawUrl.Equals(preferredUrl, StringComparison.InvariantCulture)) { int queryStringIndex = rawUrl.IndexOf('?'); if (queryStringIndex < 0) // Not equal to SafeUrl - redirect RedirectPermanent(httpContext.Response, preferredUrl); else if (queryStringIndex != preferredUrl.Length) { // There was a query string - this could have caused the difference string queryString = rawUrl.Substring(queryStringIndex), destination = preferredUrl + queryString; RedirectPermanent(httpContext.Response, destination); } } else { var adapter = adapters.ResolveAdapter<RequestAdapter>(webContext.CurrentPage); adapter.InjectCurrentPage(webContext.CurrentPath, webContext.HttpContext.Handler); } #endif }
public bool Parse (SafeUri uri) { ThreadAssist.AssertNotInMainThread (); lock (this) { elements = null; HttpWebResponse response = null; Stream stream = null; Stream web_stream = null; bool partial_read = false; long saved_position = 0; if (uri.Scheme == "file") { stream = Banshee.IO.File.OpenRead (uri); } else if (uri.Scheme == "http") { HttpWebRequest request = (HttpWebRequest)WebRequest.Create (uri.AbsoluteUri); request.UserAgent = Banshee.Web.Browser.UserAgent; request.KeepAlive = false; request.Timeout = 5 * 1000; request.AllowAutoRedirect = true; // Parse out and set credentials, if any string user_info = new Uri (uri.AbsoluteUri).UserInfo; if (!String.IsNullOrEmpty (user_info)) { string username = String.Empty; string password = String.Empty; int cIndex = user_info.IndexOf (":"); if (cIndex != -1) { username = user_info.Substring (0, cIndex); password = user_info.Substring (cIndex + 1); } else { username = user_info; } request.Credentials = new NetworkCredential (username, password); } response = (HttpWebResponse)request.GetResponse (); web_stream = response.GetResponseStream (); try { stream = new MemoryStream (); byte [] buffer = new byte[4096]; int read; // If we haven't read the whole stream and if // it turns out to be a playlist, we'll read the rest read = web_stream.Read (buffer, 0, buffer.Length); stream.Write (buffer, 0, read); if ((read = web_stream.Read (buffer, 0, buffer.Length)) > 0) { partial_read = true; stream.Write (buffer, 0, read); saved_position = stream.Position; } stream.Position = 0; } finally { if (!partial_read) { web_stream.Close (); response.Close (); } } } else { Hyena.Log.DebugFormat ("Not able to parse playlist at {0}", uri); return false; } PlaylistFormatDescription matching_format = null; foreach (PlaylistFormatDescription format in playlist_formats) { stream.Position = 0; if (format.MagicHandler (new StreamReader (stream))) { matching_format = format; break; } } if (matching_format == null) { return false; } if (partial_read) { try { stream.Position = saved_position; Banshee.IO.StreamAssist.Save (web_stream, stream, false); } finally { web_stream.Close (); response.Close (); } } stream.Position = 0; IPlaylistFormat playlist = (IPlaylistFormat)Activator.CreateInstance (matching_format.Type); playlist.BaseUri = BaseUri; playlist.Load (stream, false); stream.Dispose (); elements = playlist.Elements; Title = playlist.Title ?? Path.GetFileNameWithoutExtension (uri.LocalPath); return true; } }