Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String that came from Request.Url.ToString() misteriously changes to another string when manipulating/comparing the first characters

Tags:

string

c#

asp.net

I'm aware that there are easier ways to do this and believe me, I've tried them. I'm of course open to any suggestions =). You don't need to read the whole code, just the part that says where the problem lies. Also, I'm debbugging perl style so you guys can see. Oh and did I mention that on my development environment everything works as intended?

Here's the code:

string GetPortalAlias()
{
    String myURL2 = Request.Url.ToString();
    URLLabel.Text = "Original Request.Url.ToString() returned: \"" + myURL2 + "\"";
    string myURL = string.Copy(myURL2);
    URLLabel.Text = "Copying it to myURL, it's now: \"" + myURL + "\"";
    myURL = myURL.ToLower().Trim();
    URLLabel.Text += "<br>Trimming and ToLower myURL.<br>The new url is \"" + myURL + "\"" + "<br>";
    myURL = myURL.Replace(":80", "");
    URLLabel.Text += "Replacing the \":80\".<br> The new url is\"" + myURL + "\"<br>";


    //***HERE LIES THE PROBLEM***
    myURL = myURL.Replace("http://", "");
    URLLabel.Text += "Replacing the \"http://\".<br> The new url is\"" + myURL + "\"<br>";
    //***PROBLEM ENDS***


    myURL = myURL.Remove(myURL.IndexOf("/"));
    URLLabel.Text += "Removing everything after the \"/\"." + "<br> The new url is \"" + myURL + "\"<br>";
    URLLabel.Text += "<br>GetPortalAlias Returning \"" + myURL + "\"";
    return myURL;
}

Believe it or not, the output produced in the webpage is this:

output

Copying it to myURL, it's now: "http://sar.smg.com.ar/Default.aspx?TabID=912"
Trimming and ToLower myURL.
The new url is "http://sar.smg.com.ar/default.aspx?tabid=912"
Replacing the ":80".
The new url is"http://sar.smg.com.ar/default.aspx?tabid=912"
Replacing the "http://".
The new url is"intranetqa/default.aspx?tabid=912"
Removing everything after the "/".
The new url is "intranetqa"

GetPortalAlias Returning "intranetqa" 

So... for some reason whenever it reaches the replace section it mysteriously mutates to start with "intranetqa" instead of "sar.smg.com.ar". "intranetqa" is our default hostname. CHANGING OR TAKING AWAY ANY CHARACTER OF HTTP:// IN ANY WAY MUTATES THE STRING.

I do a string.copy because I'm aware that if two strings are equal the compiler stores them in the same place therefore I wanted to prevent errors. Taking those lines away and use Request.Url.ToString() tomyURL directly does nothing at all. They were just a test to see if that worked.

Here's a list of the things I've tried:

  • All combinations of string / String, none worked.
  • I've tried Request.Host.Url and it just gave me "intranetqa".
  • I've used Request.Url.AbsoluteUri and that's why I have the replace :80 line.
  • USING THE .tochararray FUNCTION GIVES ME BACK THE INTRANETQA THING
  • myURL = myURL.Substring(6) gives back the intranetqa thing.
  • string.Contains("sar.smg.com.ar") gives back false.

I believe the trick lies around here:

  • Uri uriAddress1 = Request.Url; and "The parts are <br>" + "Part 1: " + uriAddress1.Segments[0] + "<br>Part 2: " + uriAddress1.Segments[1]; Gives Part1 : "/" and Part 2: "Default.aspx". Trying to access part 3 (index 2) gives an exception. The request.url does not have the first part, but when I call the ToString() method, it does have like a "fake" first part
like image 902
Gaspa79 Avatar asked Oct 16 '12 14:10

Gaspa79


2 Answers

Between your browser and the server are a reverse proxy and an output re-writer. These may be the same component, or separate components.

The URL your server actually sees is always of the form http://intranetqa/default.aspx?tabid=912 (after the reverse proxy/URL re-writer has intercepted the request).

The output your server produces is actually like:

Copying it to myURL, it's now: "http://intranetqa/Default.aspx?TabID=912"
Trimming and ToLower myURL.
The new url is "http://intranetqa/default.aspx?tabid=912"
Replacing the ":80".
The new url is"http://intranetqa/default.aspx?tabid=912"
Replacing the "http://".
The new url is"intranetqa/default.aspx?tabid=912"
Removing everything after the "/".
The new url is "intranetqa"

GetPortalAlias Returning "intranetqa" 

The output re-writer is inspecting the output from your server and doing a replace of http://intranetqa with http://sar.smg.com.ar. Once you strip the http:// off of the front of these strings, it's no longer a match and so replacement no longer occurs.

If you want to know what the original requesting URL/host are, hopefully the reverse proxy either is, or can be configured to, adding an extra header to the request with the original URL.

like image 93
Damien_The_Unbeliever Avatar answered Sep 28 '22 04:09

Damien_The_Unbeliever


You can try something like this

Uri uriAddress1 = new Uri("http://www.contoso.com/title/index.htm");
Console.WriteLine("The parts are {0}, {1}, {2}", uriAddress1.Segments[0], uriAddress1.Segments[1], uriAddress1.Segments[2]);

Uri.Segments Property

This is better way to handle URIs and their segments.

like image 20
jams Avatar answered Sep 28 '22 04:09

jams