Permalink
Browse files

parse_url

git-svn-id: https://erlyaws.svn.sourceforge.net/svnroot/erlyaws/trunk/yaws@290 9fbdc01b-0d2c-0410-bfb7-fb27d70d8b52
  • Loading branch information...
1 parent 46ac7a2 commit 8da2b3a71ba690d14a9d21bc15bd6f8566aa4e9a @klacke committed Nov 20, 2002
Showing with 124 additions and 1 deletion.
  1. +6 −0 include/yaws_api.hrl
  2. +10 −0 man/yaws_api.5
  3. +30 −0 src/yaws.erl
  4. +78 −1 src/yaws_api.erl
View
@@ -54,3 +54,9 @@
+-record(url,
+ {scheme,
+ host,
+ port, %% undefined means not set
+ path = [],
+ querypart = []}).
View
@@ -308,6 +308,16 @@ Returns a list of reformated header values from a #header{}
record. The return list is suitable for retransmit.
+.TP
+\fBparse_url(Str)\fR
+Parse URL in a string, returns a #url record
+
+.TP
+\fVformat_url(UrlRecord)\fR
+Takes a #url record a formats the Url as a string
+
+
+
.SH RETURN VALUES from out/1
.PP
The out/1 function can return different values to control the behavior
View
@@ -662,3 +662,33 @@ funreverse([H|T], Fun, Ack) ->
funreverse(T, Fun, [Fun(H)|Ack]);
funreverse([], _Fun, Ack) ->
Ack.
+
+
+%% splits Str in two parts
+%% First part leading Upto SubStr and remaining part after
+split_at(Str, Substr) ->
+ split_at(Str, Substr,[]).
+
+split_at(Str, Substr, Ack) ->
+ case is_prefix(Substr, Str) of
+ {true, Tail} ->
+ {lists:reverse(Ack), Tail};
+ false ->
+ case Str of
+ [] ->
+ {lists:reverse(Ack), []};
+ [H|T] ->
+ split_at(T, Substr, [H|Ack])
+ end
+ end.
+
+
+%% is arg1 a prefix of arg2
+is_prefix([H|T1], [H|T2]) ->
+ is_prefix(T1, T2);
+is_prefix([], T) ->
+ {true, T};
+is_prefix(_,_) ->
+ false.
+
+
View
@@ -27,7 +27,7 @@
htmlize/1, htmlize_char/1, f/2, fl/1]).
-export([find_cookie_val/2, secs/0,
url_decode/1,
- url_encode/1]).
+ url_encode/1, parse_url/1, parse_url/2, format_url/1]).
-export([get_line/1, mime_type/1]).
-export([stream_chunk_deliver/2, stream_chunk_end/1]).
-export([new_cookie_session/1,
@@ -955,3 +955,80 @@ reformat_header(H) ->
set_content_type(MimeType) ->
yaws_server:make_content_type(MimeType).
+
+%% returns a #url{} record
+parse_url(Str) ->
+ parse_url(Str, strict).
+
+parse_url(Str, Strict) ->
+ case Str of
+ "http://" ++ Rest ->
+ parse_url(host, Strict, #url{scheme = http}, Rest, []);
+ "https://" ++ Rest ->
+ parse_url(host, Strict, #url{scheme = https}, Rest, []);
+ "ftp://" ++ Rest ->
+ parse_url(host, Strict, #url{scheme = ftp}, Rest, []);
+ "file://" ++ Rest ->
+ parse_url(host, Strict, #url{scheme = file}, Rest, []);
+ _ when Strict == sloppy ->
+ parse_url(host, Strict, #url{scheme = http}, Str, [])
+ end.
+
+
+parse_url(host, Strict, U, Str, Ack) ->
+ case Str of
+ [] ->
+ U#url{host = lists:reverse(Ack),
+ path = "/"
+ };
+ [$/|Tail] ->
+ U2 = U#url{host = lists:reverse(Ack)},
+ parse_url(path, Strict, U2, Tail,"/");
+ [$:|T] ->
+ U2 = U#url{host = lists:reverse(Ack)},
+ parse_url(port, Strict, U2, T,[]);
+ [H|T] ->
+ parse_url(host, Strict, U, T, [H|Ack])
+ end;
+parse_url(port, Strict, U, Str, Ack) ->
+ case Str of
+ [] ->
+ U#url{port = list_to_integer(lists:reverse(Ack)),
+ path = "/"};
+ [$/|T] ->
+ U2 = U#url{port = list_to_integer(lists:reverse(Ack))},
+ parse_url(path, Strict, U2, T,"/");
+ [H|T] ->
+ parse_url(port, Strict, U,T,[H|Ack])
+ end;
+parse_url(path, Strict, U, Str, Ack) ->
+ case Str of
+ [] ->
+ U#url{path = lists:reverse(Ack)};
+ [$?|T] ->
+ U#url{path = lists:reverse(Ack),
+ querypart = T};
+ [H|T] ->
+ parse_url(path, Strict, U, T, [H|Ack])
+ end.
+
+
+format_url(Url) when record(Url, url) ->
+ [
+ atom_to_list(Url#url.scheme), "://",
+ Url#url.host,
+ if
+ Url#url.port == undefined ->
+ [];
+ true ->
+ [$: | integer_to_list(Url#url.port)]
+ end,
+ Url#url.path,
+ if
+ Url#url.querypart == [] ->
+ [];
+ true ->
+ [$?|Url#url.querypart]
+ end
+ ].
+

0 comments on commit 8da2b3a

Please sign in to comment.