Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, June 2008
QUrl::path() leading slash bug?


Message 1 in thread

Hi.

 

I have a problem with QUrl.

See the following code:

 

      QUrl url1;

      QUrl url2;

 

      url1.setPath("test.txt");

      url2.setPath("/test.txt");

 

      url1.setHost("some.net");

      url2.setHost("some.net");

 

      url1.setPort(1234);

      url2.setPort(1234);

 

      url1.setScheme("http");

      url2.setScheme("http");

 

      qDebug() << url1.toString(); // "http://some.net:1234/test.txt";

      qDebug() << url2.toString(); // "http://some.net:1234/test.txt";

 

      qDebug() << url1.path(); // "test.txt"

      qDebug() << url2.path(); // "/test.txt"

 

Why path() remains different while toString() is the same? I want QUrl to be equivalent if toString() is the same.

Ideas?


Message 2 in thread

Anton Serdyukov wrote:
>      qDebug() << url1.toString(); // "http://some.net:1234/test.txt";
>
>      qDebug() << url2.toString(); // "http://some.net:1234/test.txt";
>
> 
>
>      qDebug() << url1.path(); // "test.txt"
>
>      qDebug() << url2.path(); // "/test.txt"
>
> 
>
>Why path() remains different while toString() is the same? I want QUrl
> to be equivalent if toString() is the same.

How would you write the first URL, without the leading path?

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 3 in thread

This is a user input.
User can enter just "test.txt", or "/test.txt", or full URL.
If user hasn't entered some URL part (host, port or user info), it is set with some default values.

I would like QUrl to work in all these cases identically...

> -----Original Message-----
> From: Thiago Macieira [mailto:thiago.macieira@xxxxxxxxxxxxx]
> Sent: Tuesday, June 17, 2008 2:32 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: QUrl::path() leading slash bug?
> 
> Anton Serdyukov wrote:
> >      qDebug() << url1.toString(); // "http://some.net:1234/test.txt";
> >
> >      qDebug() << url2.toString(); // "http://some.net:1234/test.txt";
> >
> >
> >
> >      qDebug() << url1.path(); // "test.txt"
> >
> >      qDebug() << url2.path(); // "/test.txt"
> >
> >
> >
> >Why path() remains different while toString() is the same? I want QUrl
> > to be equivalent if toString() is the same.
> 
> How would you write the first URL, without the leading path?
> 
> --
> Thiago JosÐ Macieira - thiago.macieira AT trolltech.com
> Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway

--
 [ signature omitted ] 

Message 4 in thread

On Tuesday 17 June 2008 09:41:36 Anton Serdyukov wrote:
> This is a user input.
> User can enter just "test.txt", or "/test.txt", or full URL.
> If user hasn't entered some URL part (host, port or user info), it is set
> with some default values.
>
> I would like QUrl to work in all these cases identically...

The problem is that "test.txt" and "/test.txt" are different things. The 
former is a relative URL, the latter is an absolute URL. Both have no schema 
and no authority.

QUrl handles them all. However, some of them cannot be represented in textual 
format. The case here is the only one I can remember (authority + relative 
path).

You'll have to decide if your program accepts relative URLs at all. If it 
doesn't, then prepend the slash to the path.

-- 
 [ signature omitted ] 

Attachment: signature.asc
Description: This is a digitally signed message part.


Message 5 in thread

On Tuesday 17 June 2008 09:53, Thiago Macieira wrote:
> The problem is that "test.txt" and "/test.txt" are different things. The
> former is a relative URL, the latter is an absolute URL. Both have no
> schema and no authority.
>
> QUrl handles them all. However, some of them cannot be represented in
> textual format.

If a QUrl cannot be represented in a textual format, then toString() should 
return a null-string, shouldn't it?

-Rainer

--
 [ signature omitted ] 

Message 6 in thread

I clearly understand the difference.
The only one thing I was mistaken is that toString() can lose some information about url.
So, my program deals with absolute URLs only. 
But I want user to be able to enter URL in one of the following forms:
"test.txt"
"/test.txt"
"http://server.net/test.txt";
Missing parts are filled with some default values (host, port, user info, and schema).
The path is the only mandatory part.
Despite of the user input the path should be treated as absolute path.
It's obvious that I need to perform some manipulation on user's input.
Could you suggest me the most elegant and right way to do it.

> The problem is that "test.txt" and "/test.txt" are different things.
> The
> former is a relative URL, the latter is an absolute URL. Both have no
> schema
> and no authority.
> 
> QUrl handles them all. However, some of them cannot be represented in
> textual
> format. The case here is the only one I can remember (authority +
> relative
> path).
> 
> You'll have to decide if your program accepts relative URLs at all. If
> it
> doesn't, then prepend the slash to the path.
> 
> --
> Thiago JosÐ Macieira - thiago.macieira AT trolltech.com
> Trolltech ASA - Sandakerveien 116, NO-0402 Oslo, Norway

--
 [ signature omitted ] 

Message 7 in thread

On Wed, Jun 18, 2008 at 4:18 AM, Anton Serdyukov <uksus70@xxxxxxxxx> wrote:
> Could you suggest me the most elegant and right way to do it.

if (!input.at(0) == '/') {
  input.prepend('/');
}

?
-- 
 [ signature omitted ] 

Message 8 in thread

No, this would make things worse. ("http:..." => "/http:...")

I'd try something like this:

QUrl baseUrl("http://some.net:1234/";);
QUrl inputUrl("/test.txt");
if(inputUrl.isRelative())
	inputUrl = baseUrl.resolved(inputUrl);

Cheers,
Peter 

> -----Ursprüngliche Nachricht-----
> Von: Robin Helgelin [mailto:lobbin@xxxxxxxxx] 
> Gesendet: Mittwoch, 18. Juni 2008 10:55
> An: qt-interest@xxxxxxxxxxxxx
> Betreff: Re: QUrl::path() leading slash bug?
> 
> On Wed, Jun 18, 2008 at 4:18 AM, Anton Serdyukov 
> <uksus70@xxxxxxxxx> wrote:
> > Could you suggest me the most elegant and right way to do it.
> 
> if (!input.at(0) == '/') {
>   input.prepend('/');
> }

--
 [ signature omitted ] 

Message 9 in thread

Thanks for advice, but this is not as elegant as I want.
Furthermore, user can enter "http://server.net/test.txt";.
In this case your method will not work.

> -----Original Message-----
> From: Robin Helgelin [mailto:lobbin@xxxxxxxxx]
> Sent: Wednesday, June 18, 2008 4:55 PM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Re: QUrl::path() leading slash bug?
> 
> On Wed, Jun 18, 2008 at 4:18 AM, Anton Serdyukov <uksus70@xxxxxxxxx>
> wrote:
> > Could you suggest me the most elegant and right way to do it.
> 
> if (!input.at(0) == '/') {
>   input.prepend('/');
> }
> 
> ?
> --
>  regards,
>  Robin
> 
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/

--
 [ signature omitted ] 

Message 10 in thread

On Wednesday 18 June 2008 11:44, Anton Serdyukov wrote:
> Thanks for advice, but this is not as elegant as I want.
> Furthermore, user can enter "http://server.net/test.txt";.
> In this case your method will not work.

Maybe you could use QUrl::resolved if the user enters an relative URL (test 
with QUrl::isRelative).

-Rainer

--
 [ signature omitted ] 

Message 11 in thread

On Tuesday 17 June 2008 08:25:24 Anton Serdyukov wrote:
> Why path() remains different while toString() is the same? I want QUrl to
> be equivalent if toString() is the same.
>
> Ideas?

why can't you start understanding that urls are not strings?
QUrl has a different storage format then QString. When converting between 
those formats, it is either split or built together. assuming that an Url is 
a String sounds like bad code.



-- 
 [ signature omitted ]