Permalink
Browse files

No! Somdune is not a web server; CGI doesn't belong here

  • Loading branch information...
1 parent fc49f75 commit 7bcd72652b4236f6e3ec9fa5c56b3a28a352196a @jhs jhs committed Dec 9, 2010
Showing with 0 additions and 147 deletions.
  1. +0 −9 example/cgi.sh
  2. +0 −3 example/src/webdirs.erl
  3. +0 −93 src/somdune_cgi.erl
  4. +0 −42 src/somdune_net.erl
View
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-echo "Content-Type: text/html"
-echo ""
-
-echo "<html>"
-echo "<head><title>Very simple CGI</title></head>"
-echo "<body>This process $$ serving '$REQUEST_METHOD $PATH_TRANSLATED'</body>"
-echo "</html>"
View
@@ -95,9 +95,6 @@ route_request(Request)
?INFO("Reloading plugin", []),
_Pid = spawn(fun() -> reload() end),
{reply, {200, "Reloading"}, [{'Content-Type', <<"text/html">>}], <<"Policy reloaded. <a href='/'>Try now.</a>\r\n">>}
- ; "cgi-bin"
- -> ?INFO("Executing CGI", [])
- , {cgi, "cgi.sh"} % Absolute path, otherwise this runs from the CWD. The README says to run it from "example".
; _ ->
Dirs = string:tokens(Path, "/"),
case Dirs of
View
@@ -1,93 +0,0 @@
-%%% Copyright 2010 Relaxed, Inc.
-%%% Licensed under the Apache License, Version 2.0 (the "License");
-%%% you may not use this file except in compliance with the License.
-%%% You may obtain a copy of the License at
-%%%
-%%% http://www.apache.org/licenses/LICENSE-2.0
-%%%
-%%% Unless required by applicable law or agreed to in writing, software
-%%% distributed under the License is distributed on an "AS IS" BASIS,
-%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-%%% See the License for the specific language governing permissions and
-%%% limitations under the License.
-
-%% Dead-simple CGI environment variables based on a request.
-
--module(somdune_cgi).
--author('Jason Smith <jhs@couchone.com>').
-
--include("somdune.hrl").
--export([prep_request/1]).
-
-
-prep_request(Request)
- -> {ok, {env_for_request(Request), data_for_request(Request)}}
- .
-
-
-env_for_request(Req)
- % Clean up the various types in the structure.
- -> [{K, to_list(V)} || {K, V} <- env_for_request(Req, raw)]
- .
-
-env_for_request(Req, raw)
- -> Method = case Req#request.method
- of X when is_list(X) -> X
- ; X when is_atom(X) -> atom_to_list(X)
- end
- , {abs_path, Location} = Req#request.path
- , [FullPath | AfterQMark] = re:split(Location, "\\?")
- , [ {"REQUEST_METHOD" , Method}
- , {"GATEWAY_INTERFACE", "CGI/1.1"}
- , {"PATH_TRANSLATED" , FullPath}
- , {"HTTP_COOKIE" , somdune_net:header(Req, 'Cookie')}
- , {"QUERY_STRING" , binary_to_list(erlang:iolist_to_binary(AfterQMark))}
- , {"CONTENT_LENGTH" , somdune_net:header(Req, 'Content-Length')}
- ]
- .
-
-
-data_for_request(Req)
- -> case somdune_net:header(Req, 'Content-Length')
- of ""
- -> <<>>
- ; LenStr
- -> case somdune_net:header(Req, "Expect") % XXX String, not atom, not sure how future-proof that is.
- of "100-continue"
- -> somdune_net:tcp_send(Req#request.socket, <<"HTTP/1.1 100 Continue\r\n\r\n">>)
- ; _ -> ok
- end
- , data_for_request(Req#request.socket, [], list_to_integer(LenStr))
- end
- .
-
-data_for_request(Sock, Chunks, Remaining)
- %-> somdune_net:log_info("data_for_request ~p: ~p\n", [Remaining, Chunks])
- -> case Remaining
- of 0
- %-> somdune_net:log_info("Returning reversal of: ~p\n", [Chunks])
- -> erlang:iolist_to_binary(lists:reverse(Chunks))
- ; _
- -> somdune_net:tcp_setopts(Sock, [{packet, raw}, {active, once}])
- , receive
- {Type, Sock, Data} when Type == tcp orelse Type == ssl
- -> data_for_request(sock, [Data | Chunks], Remaining - size(Data))
- ; {Closed, _} when Closed == tcp_closed orelse Closed == ssl_closed
- -> data_for_request(sock, Chunks, 0)
- ; Else
- %-> somdune_net:log_info("WTF! ~p\n", [Else])
- -> exit({cgi_data_collection_error, Else})
- end
- end
- .
-
-
-to_list(Val)
- -> case Val
- of _ when is_atom(Val) -> atom_to_list(Val)
- ; _ when is_binary(Val) -> binary_to_list(Val)
- ; _ when is_list(Val) -> binary_to_list(erlang:iolist_to_binary(Val))
- end
- .
-
-% vim: sts=4 sw=4 noet
View
@@ -170,10 +170,6 @@ collectHttpHeaders(Sock, UntilTS, BalancerModule, Headers) ->
reply(Request, Status, RespHeaders);
{reply, Status, RespHeaders, Body} ->
reply(Request, Status, RespHeaders, Body);
- {cgi, CgiProgram} ->
- cgi(Request, CgiProgram, []);
- {cgi, CgiProgram, Env} ->
- cgi(Request, CgiProgram, Env);
noop ->
ok;
Else ->
@@ -230,44 +226,6 @@ reply(Request, Status, Headers, Body) ->
, poison_pill(Request, "postreply")
.
-
-cgi(Request, Program, BaseEnv)
- -> process_flag(trap_exit, true)
- , {ok, {CgiEnv, InputData}} = somdune_cgi:prep_request(Request)
- , Env = lists:keymerge(1, lists:keysort(1, BaseEnv), lists:keysort(1, CgiEnv)) % Prefer the given environment over the auto-generated one.
- , log_info("CGI for request: ~p", [{Env, InputData}])
- , Port = open_port({spawn_executable, Program}, [binary, stream, {args, ["first", "second"]}, {env, Env}]) % TODO: catch
- , case Port
- of Port when is_port(Port)
- -> Receiver = fun(Self, Chunks)
- %-> log_info("I AM WAITING FOR THE CGI SCRIPT NOW: ~p\n", [Chunks])
- -> receive
- {Port, {data, Data}} when is_port(Port)
- -> Self(Self, [Data | Chunks])
- ; {'EXIT', Port, _Reason} when is_port(Port)
- % Done. Return the chunks in order.
- -> {ok, erlang:iolist_to_binary(lists:reverse(Chunks))}
- ; {'EXIT', Pid, Reason} when is_pid(Pid)
- -> exit({linked_process_died, Pid, Reason})
- ; Else
- -> log_error("Woa: ~p", [Else])
- , exit({unknown_process_error, Else})
- after 3000
- -> log_error("Timeout waiting for CGI", [])
- , exit(timeout)
- end
- end
- , port_command(Port, InputData) % Send the POST or PUT data through.
- , case Receiver(Receiver, [])
- of {ok, Data}
- -> reply(Request, {200, "ok"}, null, Data)
- end
- ; Error
- -> exit({open_port_failed, Error, [{request, Request}]})
- end
- .
-
-
proxy(Req, Ip, Port) ->
proxy_raw(Req, request_to_binary(Req), Ip, Port).

0 comments on commit 7bcd726

Please sign in to comment.