i have a function that pulls URLs from various web resources. needless to say some are full valid URLS and some are relative as per the HTML of the page. below is my asp.net/ c# logic i derived for examining the URL and then generate a full usable URL from whats pulled from the site...
I have have not looked at this code in some time but i remember it was working well some months ago and now it needed many tweaks to get running - especially with relative paths and the regeneration of a FULL url from various relative variations.
is there a simpler way or method to accomplish this seemingly routing boilerplate task than what i have here?
NOTE:
origianlurl is the full url of the first searched page, and relativeUrl is a url found within the searched page (it can be a full www.site.com or a /contactus.html)
private string ResolveRelativePaths(string relativeUrl, string originatingUrl)
{
if (relativeUrl.StartsWith("http") || relativeUrl.StartsWith("www"))
return relativeUrl;
if (relativeUrl.StartsWith("/"))
{
//get main url something.com
Uri myURI = new Uri(originatingUrl);
//add the relative page to the end
return myURI.Host + relativeUrl;
}
string resolvedUrl = String.Empty;
string[] relativeUrlArray = relativeUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
string[] originatingUrlElements = originatingUrl.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
int indexOfFirstNonRelativePathElement = 0;
for (int i = 0; i <= relativeUrlArray.Length - 1; i++)
{
if (relativeUrlArray[i] != "..")
{
indexOfFirstNonRelativePathElement = i;
break;
}
}
int countOfOriginatingUrlElementsToUse = originatingUrlElements.Length - indexOfFirstNonRelativePathElement - 1;
//for (int i = 0; i <= countOfOriginatingUrlElementsToUse - 1; i++)
for (int i = 0; i <= countOfOriginatingUrlElementsToUse ; i++)
{
if (originatingUrlElements[i] == "http:" || originatingUrlElements[i] == "https:")
resolvedUrl += originatingUrlElements[i] + "//";
else
resolvedUrl += originatingUrlElements[i] + "/";
}
for (int i = 0; i <= relativeUrlArray.Length - 1; i++)
{
if (i >= indexOfFirstNonRelativePathElement)
{
resolvedUrl += relativeUrlArray[i];
if (i < relativeUrlArray.Length - 1)
resolvedUrl += "/";
}
}
return resolvedUrl;
}
The Uri class has a constructor that you can use for that exactly. Given a base uri, which is your originatingUrl and a string (the relative part) it generates the full url. As far as I can see there is not a single thing in your method that cannot be done using the Uri class (maybe a few instances). My guess is that you could rewrite it to 5-10 LOC.
Related
I'm trying to use the DocumentDB Rest API to create a new document and autogenerate the id for me.
The Create Document documentation says that id is a required property of the body, but the node.js SDK and the Azure Portal Document Explorer will both generate ID's when no id is provided in the input.
Here's the response from the server:
{ code: 'BadRequest',
message: 'Message: {"Errors":["The input content is invalid because the required properties - \'id; \' - are missing","The request payload is invalid. Ensure to provide a valid request pa
yload."]}\r\nActivityId: 6b718b3d-01bc-403f-82e6-266254aad952, Request URI: /apps/4c8d65d7-216b-46b4-abb7-52c1a0c7123f/services/0e58e0c6-ff02-4523-a94b-204abd0d2179/partitions/6bf7ec3e-d850
-440e-bbcb-50d949389f3e/replicas/131469737377192972p' }
I just noticed that the DocumentDB node.js SDK generates its own id's, this is not something handled by the database.
Here is the code that they use to generate unique id's, packaged for independent use:
function generateGuidId () {
var id = "";
for (var i = 0; i < 8; i++) {
id += getHexaDigit();
}
id += "-";
for (var i = 0; i < 4; i++) {
id += getHexaDigit();
}
id += "-";
for (var i = 0; i < 4; i++) {
id += getHexaDigit();
}
id += "-";
for (var i = 0; i < 4; i++) {
id += getHexaDigit();
}
id += "-";
for (var i = 0; i < 12; i++) {
id += getHexaDigit();
}
return id;
}
function getHexaDigit () {
return Math.floor(Math.random() * 16).toString(16);
}
DocumentDb supports two addressing modes SelfLink (a.ka. RID) and AltLink (a.k.a. named). SelfLink is server generated. 'id' property is an opportunity for named addressing (ex: /dbs/ToDoList/colls/items/docs/user1).
SDK just tries to fill them with UNIQ values in-case of unavailability. I suggest strongly to specify the name.
I am creating an PDF from HTML using ABC PDF 8.0, it works well on my local end but generate a blank page on IIS, I already down grade IE, and provide the all permission to folder. When I tried to generate the PDF through any external link like Google.com it works perfectly. more over my link is accessible and there is no error on the page. please find below the code for your reference.
var url="test.com"
if (XSettings.InstallLicense(abcPDFkey))
{
using (Doc theDoc = new Doc())
{
//apply a rotation transform
double w = theDoc.MediaBox.Width;
double h = theDoc.MediaBox.Height;
double l = theDoc.MediaBox.Left;
double b = theDoc.MediaBox.Bottom;
theDoc.Transform.Rotate(90, l, b);
theDoc.Transform.Translate(w, 0);
// To fix time out
theDoc.HtmlOptions.RetryCount = 1;
theDoc.HtmlOptions.Timeout = 25000;
// rotate our rectangle
theDoc.Rect.Width = h;
theDoc.Rect.Height = w;
theDoc.HtmlOptions.Engine = EngineType.Gecko;
theDoc.HtmlOptions.ImageQuality = 60;
int theID;
theID = theDoc.AddImageUrl(url);
while (true)
{
theDoc.FrameRect();
if (!theDoc.Chainable(theID))
break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
int NewtheID = theDoc.GetInfoInt(theDoc.Root, "Pages");
theDoc.SetInfo(NewtheID, "/Rotate", "90");
}
for (int i = 1; i <= theDoc.PageCount; i++)
{
theDoc.PageNumber = i;
theDoc.Flatten();
}
foreach (IndirectObject io in theDoc.ObjectSoup)
{
if (io is PixMap)
{
PixMap pm = (PixMap)io;
pm.Realize(); // eliminate indexed color images
pm.Resize(pm.Width / 6, pm.Height / 6);
}
}
theDoc.Save(System.Web.HttpContext.Current.Server.MapPath("PDFFileName"));
theDoc.Clear();
}
Please help, thanks
Ok, I figured out what was the issue.
First of all I define the relative path for all my Images, and secondly Our server have internal IP, I define the URL for internal IP instead of public domain. that fix my issue..
cheers !!
I want to check if an IP address is in a certain range, matching by "*" only. For example, "202.121.189.8" is in "202.121.189.*".
The scenario is that I have a list of banned IPs, some of them contains "*", so I wrote a function, it works fine so far:
static bool IsInRange(string ip, List<string> ipList)
{
if (ipList.Contains(ip))
{
return true;
}
var ipSets = ip.Split('.');
foreach (var item in ipList)
{
var itemSets = item.Split('.');
for (int i = 0; i < 4; i++)
{
if (itemSets[i] == "*")
{
bool isMatch = true;
for (int j = 0; j < i; j++)
{
if (ipSets[i - j - 1] != itemSets[i - j - 1])
{
isMatch = false;
}
}
if (isMatch)
{
return true;
}
}
}
}
return false;
}
Test code:
string ip = "202.121.189.8";
List<string> ipList = new List<string>() { "202.121.168.25", "202.121.189.*" };
Console.WriteLine(IsInRange(ip, ipList));
But I think what i wrote is very stupid, and I want to optimize it, does anyone have an idea how to simplify this function? not to use so many "for....if...".
A good idea would be to represent the banned subnets in a form of a pair: mask + base address. So your check will look like that:
banned = (ip & mask == baseaddress & mask);
For 11.22.33.* the base address will be 11*0x1000000 + 22*0x10000 + 33*0x100, mask will be 0xffffff00.
For single address 55.44.33.22 the address will be 55*0x1000000 + 44*0x10000 * 33*0x100 + 22, mask will be 0xffffffff.
You'll need to convert the address to a 32-bit int as a separate procedure.
After that all, your code will look like that:
int numip = ip2int(ip);
bool isIpBanned = banList.Any(item =>
numip & item.mask == item.baseaddress & item.mask);
By the way, this way you'll be able to represent even bans on smaller subsets.
int ip2int(string ip) // error checking omitted
{
var parts = ip.Split('.');
int result = 0;
foreach (var p in parts)
result = result * 0x100 + int.Parse(p);
}
class BanItem { public int baseaddres; public int mask; }
BanItem ip2banItem(string ip)
{
BanItem bi = new BanItem() { baseaddres = 0, mask = 0 };
var parts = ip.Split('.');
foreach (var p in parts)
{
bi.baseaddress *= 0x100;
bi.mask *= 0x100;
if (p != "*")
{
bi.mask += 0xff;
bi.baseaddress += int.Parse(p);
}
}
return bi;
}
banList = banIps.Select(ip2banItem).ToList();
I think you should keep a separate list for IP with * and those without asterick.
say IpList1 contains IP's without *
and
IpList2 --those contain * ..actually what we will be storing is the part before .* in this list. for e.g. 202.121.189.* would be stored as 202.121.189 only..
Thus for a given IP addrerss you just need to check for that IP address in IpList1,if it is not found over there then
for each Ip in IPList 2 you need to check whether it is a substring of input IP or not.
Thus no requirement of complex for and if loops.
Written In Java (Untested):
static boolean IsInRange(String ip, Vector<String> ipList) {
int indexOfStar = 0;
for (int i=0; i<ipList.size(); i++) {
if (ipList.contains("*")) {
indexOfStar = ipList.indexOf("*");
if ((ip.substring(0, indexOfStar)).equals(ipList.get(i).substring(0, indexOfStar))) {
return true;
}
}
}
return false;
}
I would use a space filling curve like in the xkcd comic: http://xkcd.com/195/. It's the function H(x,y) = (H(x),H(y)) and it reduces the 2 dimension to 1 dimension. It would also show that you are a real b*** coder.
I am using the IIS 7 Rewrite module to rewrite an incoming url like:
http://server/year/all
to
http://server/application/controller/year/all
Everything works fine, except when, while processing the rewritten request, I use MVC's UrlHelper.GenerateUrl() method:
UrlHelper.GenerateUrl(
"Assets",
"Css",
"Asset",
new RouteValueDictionary(new { site = site.Name, assetPath = assetPath }),
RouteTable.Routes,
controllerContext.RequestContext,
false);
Calling this method results in an HttpException:
System.Web.HttpException: Cannot use a leading .. to exit above the top directory.
at System.Web.Util.UrlPath.ReduceVirtualPath(String path)
at System.Web.Util.UrlPath.Reduce(String path)
at System.Web.VirtualPath.Combine(VirtualPath relativePath)
at System.Web.VirtualPathUtility.Combine(String basePath, String relativePath)
at System.Web.Mvc.PathHelpers.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath)
at System.Web.Mvc.PathHelpers.GenerateClientUrl(HttpContextBase httpContext, String contentPath)
at System.Web.Mvc.UrlHelper.GenerateUrl(String routeName, String actionName, String controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, Boolean includeImplicitMvcValues)
Looking at the RequestContext, it seems that all of the request paths are correct (ie, have the rewritten values). I can't seem to figure out why it's trying to exit out of the top level directory... There's nowhere we are using .... in a path.
I've also made sure the RewriteModule is in above the UrlRouting module in IIS.
While I can step into the framework methods, I can't examine any of the local variables (either in VS or WinDbg) because it's been compiler optimized.
Any thoughts?
This is a grotesque workaround involving private implementation details, but add this:
HttpContext.Current.Request.ServerVariables.Remove("IIS_WasUrlRewritten");
This avoids the internal check done in PathHelper.GenerateClientUrlInternal to see if the request was rewritten. It's quite likely that this will break some scenarios, as hinted at by this comment in the reference sources:
// Since the rawUrl represents what the user sees in his browser, it is what we want to use as the base
// of our absolute paths. For example, consider mysite.example.com/foo, which is internally
// rewritten to content.example.com/mysite/foo. When we want to generate a link to ~/bar, we want to
// base it from / instead of /foo, otherwise the user ends up seeing mysite.example.com/foo/bar,
// which is incorrect.
Working solution is to insert the line before Url.Content/UrlHelper.GenerateContentUrl (best place is in Application_BeginRequest):
System.Web.HttpContext.Current.Items.Add("IIS_WasUrlRewritten", "false");
My answer is the result of 2 above answers (Rick Schott and Thom). Both was quite right but that didn't help.
I learned source code at https://github.com/aspnet/AspNetWebStack/blob/master/src/ of two classes (System.Web.WebPages.Utils.UrlRewriterHelper.cs and System.Web.WebPages.Utils.UrlUtil.cs) that are in my stack trace:
System.Web.HttpException (0x80004005): Cannot use a leading .. to exit above the top directory.
at System.Web.Util.UrlPath.ReduceVirtualPath(String path)
at System.Web.Util.UrlPath.Reduce(String path)
at System.Web.VirtualPath.Combine(VirtualPath relativePath)
at System.Web.VirtualPathUtility.Combine(String basePath, String relativePath)
at System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath)
at System.Web.WebPages.UrlUtil.GenerateClientUrlInternal(HttpContextBase httpContext, String contentPath)
at System.Web.WebPages.UrlUtil.GenerateClientUrl(HttpContextBase httpContext, String basePath, String path, Object[] pathParts)
There is code in System.Web.WebPages.Utils.UrlUtil.cs - GenerateClientUrlInternal method:
if (!wasRequestRewritten)
{
return contentPath;
}
// Since the rawUrl represents what the user sees in his browser, it is what we want to use as the base
// of our absolute paths. For example, consider mysite.example.com/foo, which is internally
// rewritten to content.example.com/mysite/foo. When we want to generate a link to ~/bar, we want to
// base it from / instead of /foo, otherwise the user ends up seeing mysite.example.com/foo/bar,
// which is incorrect.
string relativeUrlToDestination = MakeRelative(httpContext.Request.Path, contentPath);
string absoluteUrlToDestination = MakeAbsolute(httpContext.Request.RawUrl, relativeUrlToDestination);
return absoluteUrlToDestination;
You could see strange lines with author's comment for url rewritten paths. Also, original client path is in HttpContext.Request.RawUrl but in Url it is rewritten.
Look forward at System.Web.WebPages.Utils.UrlRewriterHelper.cs:
if (httpContext.Items.Contains(UrlWasRewrittenServerVar))
{
return Object.Equals(httpContext.Items[UrlWasRewrittenServerVar], UrlWasRequestRewrittenTrueValue);
}
else
{
HttpWorkerRequest httpWorkerRequest = (HttpWorkerRequest)httpContext.GetService(typeof(HttpWorkerRequest));
bool requestWasRewritten = (httpWorkerRequest != null && httpWorkerRequest.GetServerVariable(UrlWasRewrittenServerVar) != null);
if (requestWasRewritten)
{
httpContext.Items.Add(UrlWasRewrittenServerVar, UrlWasRequestRewrittenTrueValue);
}
else
{
httpContext.Items.Add(UrlWasRewrittenServerVar, UrlWasRequestRewrittenFalseValue);
}
return requestWasRewritten;
}
If we write dummy value to HttpContext.Items[UrlWasRewrittenServerVar] with "false" value we make skipped httpWorkerRequest.GetServerVariable(UrlWasRewrittenServerVar) != null check.
So Url.Content is working now.
Not sure if it helps but here is the code throwing the exception:
internal static string ReduceVirtualPath(string path)
{
int length = path.Length;
int startIndex = 0;
while (true)
{
startIndex = path.IndexOf('.', startIndex);
if (startIndex < 0)
{
return path;
}
if (((startIndex == 0) || (path[startIndex - 1] == '/')) && ((((startIndex + 1) == length) || (path[startIndex + 1] == '/')) || ((path[startIndex + 1] == '.') && (((startIndex + 2) == length) || (path[startIndex + 2] == '/')))))
{
break;
}
startIndex++;
}
ArrayList list = new ArrayList();
StringBuilder builder = new StringBuilder();
startIndex = 0;
do
{
int num3 = startIndex;
startIndex = path.IndexOf('/', num3 + 1);
if (startIndex < 0)
{
startIndex = length;
}
if ((((startIndex - num3) <= 3) && ((startIndex < 1) || (path[startIndex - 1] == '.'))) && (((num3 + 1) >= length) || (path[num3 + 1] == '.')))
{
if ((startIndex - num3) == 3)
{
if (list.Count == 0)
{
throw new HttpException(SR.GetString("Cannot_exit_up_top_directory"));
}
if ((list.Count == 1) && IsAppRelativePath(path))
{
return ReduceVirtualPath(MakeVirtualPathAppAbsolute(path));
}
builder.Length = (int) list[list.Count - 1];
list.RemoveRange(list.Count - 1, 1);
}
}
else
{
list.Add(builder.Length);
builder.Append(path, num3, startIndex - num3);
}
}
while (startIndex != length);
string str = builder.ToString();
if (str.Length != 0)
{
return str;
}
if ((length > 0) && (path[0] == '/'))
{
return "/";
}
return ".";
}
I have a Webforms, AJAX-enabled web page which, when rendering large amounts of data, is extremely slow to load in IE (we're married to IE - no other browser options). In an attempt to determine the source of the slowness, I viewed the HTML source (about 2.5 MB) and copied all of it (except for the Ajax JavaScript calls) to a blank .html file. IE renders this file MUCH faster than when the rendering happens through .Net. This seems to indicate that the AJAX JavaScript is slowing down the display of the page. Does this sound plausible? Any recommendations on improving performance here?
I've already eliminated as many UpdatePanel controls as I can from the page, but it doesn't seem to help with render time.
Thanks for the help!
Update... In the HTML source, I noticed that at the bottom of the screen, a call to WebForm_InitCallback() appears. When I executed this call directly through javascript:alert(WebForm_InitCallback());, the CPU spikes for 12 seconds before it completes! This call is here because I implemented ICallbackEventHandler to try to accomplish some traditional-style AJAX handling. Looking at WebResource.axd, that WebForm_InitCallback() method iterates through the entire form and attaches some kind of events to EVERY SINGLE textbox, checkbox, radiobutton, etc. So I guess I really need to abandon ScriptManager and UpdatePanel altogether here. Poop.
Andy
I hate to say this, but can you take the Microsoft AJAX out of the equation? Try it with doing an XMLHTTP request and populate the data yourself. That way at least you could step through the js and figure out if it is time on the server, time turning the resulting XML or JSON into an object, or time spent populating your data on screen.
This is an old topic but I thought I should share what I recently did to fix long running script error in IE 7 caused by WebForm_InitCallback.
I had a page with over 2000 form elements and in IE 7 was causing a long running script warning / browser freeze for a client. We have other pages with many more form elements and paging or other options aren't options due to needing a quick turn around to improve performance.
I narrowed it down to WebForm_InitCallback, and even further to the following line:
element = theForm.elements[i];
By saving a reference to theForm.elements instead and using it to access the index, I found significant performance gains.
var elements = theForm.elements;
for (var i = 0; i < count; i++) {
element = elements[i];
....
}
I made a jsperf to test the difference since I didn't expect such impressive gains from not calling the refinement every time.
Beyond that, I found better performance by replacing the concatenation in WebForm_InitCallbackAddField to adding the strings to an array and joining it together after the for loop in WebForm_InitCallback completes and saving it back into __theFormPostData.
Here are the original two function that you'll see in the WebResource:
function WebForm_InitCallback() {
var count = theForm.elements.length;
var element;
for (var i = 0; i < count; i++) {
element = theForm.elements[i];
var tagName = element.tagName.toLowerCase();
if (tagName == "input") {
var type = element.type;
if ((__callbackTextTypes.test(type) || ((type == "checkbox" || type == "radio") && element.checked))
&& (element.id != "__EVENTVALIDATION")) {
WebForm_InitCallbackAddField(element.name, element.value);
}
}
else if (tagName == "select") {
var selectCount = element.options.length;
for (var j = 0; j < selectCount; j++) {
var selectChild = element.options[j];
if (selectChild.selected == true) {
WebForm_InitCallbackAddField(element.name, element.value);
}
}
}
else if (tagName == "textarea") {
WebForm_InitCallbackAddField(element.name, element.value);
}
}
}
function WebForm_InitCallbackAddField(name, value) {
var nameValue = new Object();
nameValue.name = name;
nameValue.value = value;
__theFormPostCollection[__theFormPostCollection.length] = nameValue;
__theFormPostData += WebForm_EncodeCallback(name) + "=" + WebForm_EncodeCallback(value) + "&";
}
And here is the javascript I added to my page to overwrite them. It's important that this code is inserted after the WebResource is added and before WebForm_InitCallback is called.
var __theFormPostDataArr = [];
if (typeof (WebForm_InitCallback) != "undefined") {
WebForm_InitCallback = function () {
var count = theForm.elements.length;
var element;
var elements = theForm.elements;
for (var i = 0; i < count; i++) {
element = elements[i];
var tagName = element.tagName.toLowerCase();
if (tagName == "input") {
var type = element.type;
if ((type == "text" || type == "hidden" || type == "password" ||
((type == "checkbox" || type == "radio") && element.checked)) &&
(element.id != "__EVENTVALIDATION")) {
WebForm_InitCallbackAddField(element.name, element.value);
}
}
else if (tagName == "select") {
var selectCount = element.options.length;
for (var j = 0; j < selectCount; j++) {
var selectChild = element.options[j];
if (selectChild.selected == true) {
WebForm_InitCallbackAddField(element.name, element.value);
}
}
}
else if (tagName == "textarea") {
WebForm_InitCallbackAddField(element.name, element.value);
}
}
__theFormPostData = __theFormPostDataArr.join('');
}
WebForm_InitCallbackAddField = function (name, value) {
__theFormPostDataArr = [];
var nameValue = new Object();
nameValue.name = name;
nameValue.value = value;
__theFormPostCollection[__theFormPostCollection.length] = nameValue;
__theFormPostDataArr[__theFormPostDataArr.length] = WebForm_EncodeCallback(name);
__theFormPostDataArr[__theFormPostDataArr.length] = "=";
__theFormPostDataArr[__theFormPostDataArr.length] = WebForm_EncodeCallback(value);
__theFormPostDataArr[__theFormPostDataArr.length] = "&";
}
}
Ultimately, it took the run time of WebForm_InitCallback from 27 seconds to 4 seconds on my IE 7 machine.