Skip to content

Commit

Permalink
updating handling of http responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Vorreuter committed Aug 17, 2009
1 parent cd4d6f2 commit 8de6757
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 15 deletions.
6 changes: 4 additions & 2 deletions Makefile
Expand Up @@ -24,12 +24,14 @@ rel: compile
erl -pa ebin -noshell -run excavator build_rel -s init stop

package: clean
@mkdir $(PKGNAME)-$(VERSION)/ && cp -rf ebin include Makefile public README.markdown src support t $(PKGNAME)-$(VERSION)
@mkdir $(PKGNAME)-$(VERSION)/ && cp -rf ebin excavator include Makefile public README.markdown src support t $(PKGNAME)-$(VERSION)
@COPYFILE_DISABLE=true tar zcf $(PKGNAME)-$(VERSION).tgz $(PKGNAME)-$(VERSION)
@rm -rf $(PKGNAME)-$(VERSION)/

install:
@mkdir -p $(prefix)/$(LIBDIR)/$(PKGNAME)-$(VERSION)/{ebin,include}
@mkdir -p $(prefix)/$(ROOTDIR)/bin
for i in ebin/*.beam include/*.hrl ebin/*.app; do install $$i $(prefix)/$(LIBDIR)/$(PKGNAME)-$(VERSION)/$$i ; done
cp *.boot $(prefix)/$(ROOTDIR)/bin/
cp *.boot $(prefix)/$(ROOTDIR)/bin/
@mkdir -p $(prefix)/etc/init.d
cp excavator $(prefix)/etc/init.d/
Binary file removed dependencies/dynamic_compile-0.1.tgz
Binary file not shown.
Binary file removed dependencies/mochiweb-0.2.tgz
Binary file not shown.
Binary file removed dependencies/mochixpath-0.1.tgz
Binary file not shown.
7 changes: 6 additions & 1 deletion src/ex_consumer.erl
Expand Up @@ -317,10 +317,15 @@ assert_true(_K, [], list_of_strings) -> true;
assert_true(_K, V, list_of_strings) when is_list(V) ->
case lists:usort([ex_util:typeof(I) || I <- V]) of
[string] -> true;
_ -> false
_ ->
case ex_util:typeof(V) of
string -> true;
_ -> false
end
end;

assert_true(_K, [], list_of_nodes) -> true;
assert_true(_K, {A,B,C}, list_of_nodes) when is_binary(A), is_list(B), is_list(C) -> true;
assert_true(_K, V, list_of_nodes) when is_list(V) ->
case lists:usort([ex_util:typeof(I) || I <- V]) of
[node] -> true;
Expand Down
35 changes: 34 additions & 1 deletion src/ex_eval.erl
Expand Up @@ -43,8 +43,18 @@ expand(State, {http, Method, Url, Headers}) when Method==options;Method==get;Met
expand(State, {http, Method, Url, Headers, []});
expand(State, {http, Method, Url, Headers, Body}) ->
Url1 = flatten_url(State, Url),
ex_web:request(Method, Url1, Headers, Body);
ex_web:request(Method, Url1, expand(State, Headers), expand(State, Body));

%% HTTP Cookie Value
expand(State, {cookie, Key, HttpRespKey}) ->
Cookies = get_cookies(expand(State, HttpRespKey)),
case proplists:get_value(Key, Cookies) of
{cookie, _, Fields} ->
proplists:get_value(Key, Fields);
_ ->
undefined
end;

%% XPath
expand(State, {xpath, Source, XPath}) ->
ex_xpath:run(expand(State, XPath), expand(State, Source));
Expand Down Expand Up @@ -106,4 +116,27 @@ flatten_url(State, Url) ->
case ex_util:typeof(Url) of
string -> Url;
list -> lists:concat([expand(State, I) || I <- Url])
end.

get_cookies({http_response, _, Headers, _}) ->
case proplists:get_all_values("set-cookie", Headers) of
[] ->
[];
Values ->
dict:to_list(lists:foldl(
fun(CookieString, Dict) ->
Fields =
[begin
case string:tokens(Field, "=") of
[Key] ->
{string:strip(Key), ""};
[Key, Val] ->
{string:strip(Key), string:strip(Val)};
[Key | Vals] ->
{string:strip(Key), string:strip(string:join(Vals, "="))}
end
end || Field <- string:tokens(CookieString, ";")],
[{Name,_}|_] = Fields,
dict:store(Name, {cookie, CookieString, Fields}, Dict)
end, dict:new(), Values))
end.
2 changes: 1 addition & 1 deletion src/ex_pp.erl
Expand Up @@ -121,7 +121,7 @@ build_instr({call, _, {atom, _, Instr}, Args})
Instr==onfail; Instr==commit; Instr==add;
Instr==gassign; Instr==gadd ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,Instr}, to_cons(Args)]};

build_instr(Term) ->
{tuple, ?L, [{atom,?L,instr}, {atom,?L,term}, to_cons([Term])]}.

Expand Down
19 changes: 9 additions & 10 deletions src/ex_web.erl
Expand Up @@ -21,17 +21,14 @@
%% FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
%% OTHER DEALINGS IN THE SOFTWARE.
-module(ex_web).
-export([
request/4
]).
-export([request/4]).

-define(HEADERS, [
{"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
{"Accept-Language","en-us,en;q=0.5"},
%{"Accept-Encoding","gzip,deflate"},
{"Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
{"Connection","keep-alive"},
{"User-Agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5"}
{"Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"},
{"Accept-Language","en-us,en;q=0.5"},
{"Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7"},
{"Connection","keep-alive"},
{"User-Agent","Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.0.5) Gecko/2008120121 Firefox/3.0.5"}
]).

request(Method, Url, [], Body) -> request(Method, Url, ?HEADERS, Body);
Expand All @@ -45,7 +42,9 @@ request(Method, Url, Headers, [])

request(Method, Url, Headers, Body)
when Method == post; Method == put ->
case http:request(Method, {Url, Headers, "text/html", Body}, [], []) of
ContentType = proplists:get_value("Content-Type", Headers, "text/html"),
io:format("http:request(~p, ~p, ~p, ~p)~n", [Method, {Url, Headers, ContentType, Body}, [], []]),
case http:request(Method, {Url, Headers, ContentType, Body}, [], []) of
{ok, {{_,RspStatus,_}, RspHeaders, RspBody}} -> {http_response, RspStatus, RspHeaders, RspBody};
{error, Reason} -> exit({?MODULE, ?LINE, Reason})
end.

0 comments on commit 8de6757

Please sign in to comment.