how to get absolute uri ?

15 views
Skip to first unread message

Benoit Chesneau

unread,
Jul 17, 2009, 9:31:30 AM7/17/09
to MochiWeb
Im' trying to get absolute url inside code, but I didn't find a way to
do it currently, i can only have raw path. Any idee how it could be
done ?

- benoît

Ben Hood

unread,
Jul 17, 2009, 10:45:42 AM7/17/09
to moch...@googlegroups.com
Benoit,

I don't whether this is exactly what you want or whether there is a
better way of doing this:

Host = mochiweb_headers:get_value("host", Headers),
URL = "http://" ++ Host ++ "/" ++ RelPath ++ "/"

HTH,

Ben

Benoit Chesneau

unread,
Jul 18, 2009, 2:03:56 AM7/18/09
to moch...@googlegroups.com
2009/7/17 Ben Hood <0x6e...@gmail.com>:

Thanks, someone on couchdb channel also give me link to have Host when
no Host header is provided. Here is the extracted method from couchdb
:

absolute_uri(Req, Path) ->
Host = case Req:get_header_value("Host") of
undefined ->
{ok, {Address, Port}} = inet:sockname(Req:get(socket)),
inet_parse:ntoa(Address) ++ ":" ++ integer_to_list(Port);
Value -> Value
end,
"http://" ++ Host ++ Path.

Maybe it could be provided by mochiweb api anyway ?

- benoît

Ben Hood

unread,
Jul 18, 2009, 6:56:05 AM7/18/09
to moch...@googlegroups.com
Benoit,

On 18 Jul 2009, at 07:03, Benoit Chesneau <bche...@gmail.com> wrote:

> Thanks, someone on couchdb channel also give me link to have Host when
> no Host header is provided. Here is the extracted method from couchdb
> :
>
> absolute_uri(Req, Path) ->
> Host = case Req:get_header_value("Host") of
> undefined ->
> {ok, {Address, Port}} = inet:sockname(Req:get(socket)),
> inet_parse:ntoa(Address) ++ ":" ++ integer_to_list(Port);
> Value -> Value
> end,
> "http://" ++ Host ++ Path.
>
> Maybe it could be provided by mochiweb api anyway ?

There's probably one or two ways to do this, depending on the calling
context.

If you a change to the API, I would have thought a patch is the best
way to go.

Ben

Steve Davis

unread,
Jul 18, 2009, 12:35:04 PM7/18/09
to MochiWeb
Why not... inet:gethostname() ?

Reply all
Reply to author
Forward
0 new messages