How to get the "query string" from a QUrl? - http

I have a QUrl and I need to extract the path+file+params. Basically everything but the hostname - what would be requested via HTTP.
I looked through the Qt 4.6 docs but I couldn't find anything that looked like it would do this.
What method(s) would I call?

You can clear the scheme with setScheme. After that the url will be relative so it shouldn't return the hostname anymore when converting it to a string.
QUrl someUrl("http://stackoverflow.com/foo/bar?spam=eggs");
someUrl.setScheme("");
someUrl.toString();
Or, you can give the toString() method some extra parameters:
QUrl someUrl("http://stackoverflow.com/foo/bar?spam=eggs");
someUrl.toString(QUrl::RemoveScheme);

Related

Special characters in QUrl will be transformed to punycode

I'm having a textfield where the user can add an URL with I'm having a textfield where the user can add an URL with QUrl::fromUserInput() and it then will be put into a list.
If I use special characters in the URL like "http://blöd.de/" it will be shown as "http://blöd.de/" but if I only type in "ö" it will get converted to the punycode "http://xn--nda/".
I tried every QUrl::FormattingOptions and every QUrl::ParsingMode
qDebug() << QUrl::fromUserInput("blöd.de"); // results in: http://blöd.de
qDebug() << QUrl::fromUserInput("ö"); // results in: http://xn--nda
Does somebody have an idea how I can convert this punycode back to the special character? And why is it only not converted when I have a top level domain?
The reason some urls are shown with Unicode characters and others with punnycode is to prevent homograph attacks.
One way to decide how to behave for a specific url is by the mean of a TLD whitelist.
In Qt you can see and edit the whitelist using QUrl::idnWhitelist() and QUrl::setIdnWhitelist(const QStringList &list).
In your example .de is in the whitelist, but .ö is not. That is why you can see a difference in behaviour.

Is it better to use a "?" or a ";" in a URL?

In my application, I redirect an HTTP request and also pass a parameter. Example:
http://localhost:9000/home;signup=error
Is it better to use a ; or shall I use a ? i.e. shall I do http://localhost:9000/home;signup=error or http://localhost:9000/home?signup=error?
Are the above two different from each other semantically?
The ? is a reserved character; I have read that this is both valid and invalid, but I have used it for 'slugs' when templating.
Should you choose to use it, percent-encode the query string using %3F which is not human readable, but will produce the ?. (An encoder is recommended)
Perhaps you will find a more suitable solution for your redirects by adding an .htaccess file to your project.

How to remove the "?" character from g-wan URIs

I have checked cache.c <- totally clueless what it is doing or how to have pretty permalinks to servlet calls.
Update: OK, I know what the above does, but the problem is you have to call the above script first before you can access it as permalink. Is there any way I can access permalinks without using "?" at all (in the first place)?
I have also checked on this link: Anatomy of G-WAN URI servlets
I would like to have http://example.com:8080/servlet/arg1/arg2, without "?", and would like the above link to reference "servlet" to servlet.c.
Basically, like this pretty URL for this question
https://stackoverflow.com/questions/27084626/how-to-remove-in-g-wan-url-completely
See...no "?" within the URL.
Is this possible?
I have also checked
u8 *query_char = (u8*)get_env(argv, QUERY_CHAR);
*query_char = '!'; // use "/!hello.c" instead of "/?hello.c"
I know I can't do
*query_char = '';
you can re-write url with handler there is a simple rewrite example

ASP.NET: Get *real* raw URL

In ASP.NET, is there any way to get the real raw URL?
For example, if a user browse to "http://example.com/mypage.aspx/%2F", I would like to be able to get "http://example.com/mypage.aspx/%2F" rather than "http://example.com/mypage.aspx//".
I would of course like a clean way to do it, but I can live with a hacky approach using reflection or accessing obscure properties.
At the moment, I try to use the uri in the Authorization-header (which works), but I cannot rely on that always being there.
EDIT:
What I really want to do is to be able to distinguish between "http://example.com/mypage.aspx/%2F" and "http://example.com/mypage.aspx/%2F%2F".
It looks like ASP.NET first converts "%2F%2F" into "//" and then converts the slashes into a single slash.
So just re-encoding it is not going to work.
I wasn't able to test this because it only works in IIS and not the ASP.NET Development Server that is part of Visual Studio, but try:
Request.ServerVariables[ "HTTP_URL" ]
The following code works for me:
IServiceProvider serviceProvider = (IServiceProvider)HttpContext.Current;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)serviceProvider.GetService(typeof(HttpWorkerRequest));
string realUrl = workerRequest.GetServerVariable("HTTP_URL");
Note that this only works when running on the IIS and not under f.x. ASP.NET Development Server!
Thanks to Lucero for the answer in another thread and Zhaph for pointing me to the thread.
See also:
Get the exact url the user typed into the browser
Server.HtmlEncode(Request.RawUrl);
The raw URL is defined as the part of the URL following the domain information. In the URL string http://www.contoso.com/articles/recent.aspx, the raw URL is /articles/recent.aspx. The raw URL includes the query string, if present.
see also:link text
I can't test here, but this might be what you need:
Request.Url.AbsoluteUri
Request.RawUrl will return the application relative path(including querystring info) while Request.Url will return the complete path(including querystring info).
For more information, see "Making sense of ASP.NET paths".
Well, you could just encode it back to the url-encoded version.
Get the url from the request and urlencode only the query string part and then concatenate them

How do you convert a url to a virtual path in asp.net without manual string parsing?

I've seen similar questions and answers regarding conversions from virtual to absolute and url, but how can I convert a url to a virtual path without manual string parsing?
Example:
I want "http://myserver/home.aspx" converted to: "~/home.aspx"
I realize the above example would be an easy string parsing routine, but I'm looking for a proper solution that will scale to the changing of the url format.
You can get most of it from the Uri class:
new Uri("http://myserver.com/home.aspx").AbsolutePath
Then you just have to prepend the ~
Though, that will might break if you host in a subdirectory - I don't think there's a way to do it specifically in the context of the application you're running.
EDIT: This might do it:
VirtualPathUtility.ToAppRelative(new Uri("http://myserver.com/home.aspx").AbsolutePath);
VirtualPathUtility.ToAppRelative Method (String) seems to be what you are looking for (http://msdn.microsoft.com/en-us/library/ms150163.aspx)
If the virtual path for the application is "myapp" and the virtual path "/myApp/sub/default.asp" is passed into the ToAppRelative method, the resulting application-relative path is "~/sub/default.aspx".

Resources