capricorn 2.0.8 → 2.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/erlang/lib/capricorn/ebin/capricorn.app +2 -1
  2. data/erlang/lib/capricorn/include/capricorn.hrl +1 -0
  3. data/erlang/lib/capricorn/src/cap_cluster_gems.erl +6 -1
  4. data/erlang/lib/capricorn/src/cap_console_dispatcher.erl +214 -0
  5. data/erlang/lib/capricorn/src/cap_machine_apps.erl +25 -1
  6. data/erlang/lib/capricorn/src/cap_sup.erl +14 -0
  7. data/erlang/lib/ejson/Makefile +24 -0
  8. data/erlang/lib/ejson/ebin/ejson.app +9 -0
  9. data/erlang/lib/ejson/include/ejson.hrl +40 -0
  10. data/erlang/lib/ejson/rebar.config +3 -0
  11. data/erlang/lib/ejson/src/ejson.erl +22 -0
  12. data/erlang/lib/ejson/src/ejson_decode.erl +337 -0
  13. data/erlang/lib/ejson/src/ejson_encode.erl +124 -0
  14. data/erlang/lib/ejson/test/arrays.escript +47 -0
  15. data/erlang/lib/ejson/test/compound.escript +56 -0
  16. data/erlang/lib/ejson/test/literals.escript +30 -0
  17. data/erlang/lib/ejson/test/numbers.escript +70 -0
  18. data/erlang/lib/ejson/test/objects.escript +51 -0
  19. data/erlang/lib/ejson/test/strings.escript +49 -0
  20. data/erlang/lib/ejson/test/timing.escript +43 -0
  21. data/erlang/lib/ejson/test/timing.json +382 -0
  22. data/erlang/lib/ejson/vendor/mochijson2.erl +621 -0
  23. data/erlang/lib/ejson/vendor/rfc4627.erl +625 -0
  24. data/erlang/lib/misultin/LICENSE.txt +41 -0
  25. data/erlang/lib/misultin/Makefile +26 -0
  26. data/erlang/lib/misultin/README.txt +120 -0
  27. data/erlang/lib/misultin/ebin/misultin.app +9 -0
  28. data/erlang/lib/misultin/examples/misultin_compress.erl +43 -0
  29. data/erlang/lib/misultin/examples/misultin_echo.erl +58 -0
  30. data/erlang/lib/misultin/examples/misultin_file.erl +43 -0
  31. data/erlang/lib/misultin/examples/misultin_gen_server.erl +158 -0
  32. data/erlang/lib/misultin/examples/misultin_get_variable.erl +55 -0
  33. data/erlang/lib/misultin/examples/misultin_hello_world.erl +43 -0
  34. data/erlang/lib/misultin/examples/misultin_rest.erl +68 -0
  35. data/erlang/lib/misultin/examples/misultin_ssl.erl +51 -0
  36. data/erlang/lib/misultin/examples/misultin_stream.erl +55 -0
  37. data/erlang/lib/misultin/examples/misultin_websocket_event_example.erl +103 -0
  38. data/erlang/lib/misultin/examples/misultin_websocket_example.erl +95 -0
  39. data/erlang/lib/misultin/include/misultin.hrl +95 -0
  40. data/erlang/lib/misultin/make.bat +55 -0
  41. data/erlang/lib/misultin/priv/README.txt +12 -0
  42. data/erlang/lib/misultin/priv/test_certificate.pem +21 -0
  43. data/erlang/lib/misultin/priv/test_privkey.pem +18 -0
  44. data/erlang/lib/misultin/rebar.config +3 -0
  45. data/erlang/lib/misultin/src/misultin.app.src +9 -0
  46. data/erlang/lib/misultin/src/misultin.erl +338 -0
  47. data/erlang/lib/misultin/src/misultin_http.erl +488 -0
  48. data/erlang/lib/misultin/src/misultin_req.erl +280 -0
  49. data/erlang/lib/misultin/src/misultin_socket.erl +193 -0
  50. data/erlang/lib/misultin/src/misultin_utility.erl +357 -0
  51. data/erlang/lib/misultin/src/misultin_websocket.erl +252 -0
  52. data/erlang/lib/misultin/src/misultin_ws.erl +78 -0
  53. data/erlang/rebar.config +2 -0
  54. data/erlang/rel/overlay/etc/capricorn/app.config +4 -0
  55. data/erlang/rel/reltool.config +5 -0
  56. data/lib/capricorn/recipes/apache-debian.rb +1 -1
  57. data/lib/capricorn/recipes/centos-plesk.rb +1 -1
  58. data/lib/capricorn/recipes/debian-plesk95.rb +1 -2
  59. data/lib/capricorn/recipes/macports.rb +1 -1
  60. data/lib/capricorn/version.rb +1 -1
  61. metadata +51 -4
@@ -0,0 +1,41 @@
1
+ ==========================================================================================================
2
+ MISULTIN - An Erlang library for building fast lightweight HTTP servers.
3
+ <http://code.google.com/p/misultin/>
4
+
5
+ >-|-|-(°>
6
+
7
+ Copyright (C) 2010, Roberto Ostinelli <roberto@ostinelli.net>, Joe Armstrong, Sean Hinde,
8
+ Bob Ippolito <bob@mochimedia.com> for Mochi Media, Inc.
9
+
10
+ All rights reserved.
11
+
12
+ Code portions from Joe Armstrong have been originally taken under MIT license at the address:
13
+ <http://armstrongonsoftware.blogspot.com/2009/12/comet-is-dead-long-live-websockets.html>
14
+
15
+ Code portions from Sean Hinde have been originally taken under BSD license from Trapexit at the address:
16
+ <http://www.trapexit.org/A_fast_web_server_demonstrating_some_undocumented_Erlang_features>
17
+
18
+ Code portions from Bob Ippolito have been originally taken under MIT license from MOCHIWEB:
19
+ <http://code.google.com/p/mochiweb/>
20
+ ==========================================================================================================
21
+
22
+ BSD License
23
+
24
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided
25
+ that the following conditions are met:
26
+
27
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the
28
+ following disclaimer.
29
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
30
+ the following disclaimer in the documentation and/or other materials provided with the distribution.
31
+ * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
32
+ products derived from this software without specific prior written permission.
33
+
34
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
35
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
36
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
37
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
38
+ TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
40
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41
+ POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,26 @@
1
+ EBIN_DIR := ebin
2
+ SRC_DIR := src
3
+ EXAMPLES_DIR := examples
4
+ INCLUDE_DIR := include
5
+ ERLC := erlc
6
+ ERLC_FLAGS := -W -I $(INCLUDE_DIR) -o $(EBIN_DIR)
7
+
8
+ all:
9
+ @mkdir -p $(EBIN_DIR)
10
+ $(ERLC) $(ERLC_FLAGS) $(SRC_DIR)/*.erl
11
+ @cp $(SRC_DIR)/misultin.app.src $(EBIN_DIR)/misultin.app
12
+
13
+ clean:
14
+ @rm -rf $(EBIN_DIR)/*
15
+ @rm -f erl_crash.dump
16
+
17
+ debug:
18
+ @mkdir -p $(EBIN_DIR)
19
+ $(ERLC) -D log_debug $(ERLC_FLAGS) $(SRC_DIR)/*.erl
20
+ @cp $(SRC_DIR)/misultin.app.src $(EBIN_DIR)/misultin.app
21
+
22
+ example:
23
+ @mkdir -p $(EBIN_DIR)
24
+ $(ERLC) $(ERLC_FLAGS) $(SRC_DIR)/*.erl
25
+ @cp $(SRC_DIR)/misultin.app.src $(EBIN_DIR)/misultin.app
26
+ $(ERLC) $(ERLC_FLAGS) $(EXAMPLES_DIR)/*.erl
@@ -0,0 +1,120 @@
1
+ ==========================================================================================================
2
+ MISULTIN - An Erlang library for building fast lightweight HTTP servers.
3
+ <http://code.google.com/p/misultin/>
4
+
5
+ >-|-|-(°>
6
+
7
+
8
+ INSTALL INSTRUCTIONS
9
+ ==========================================================================================================
10
+
11
+ 1. Compile
12
+
13
+ Run the appropriate script:
14
+
15
+ * OSX | Linux users: make
16
+ * Windows users: make.bat. Note that Erlang bin directory (by default, C:\Program Files\erl5.7.2\bin\) must be in your path for the script to work.
17
+
18
+ This script will compile the .erl files in the Misultin src directory and save the compiled files into the ebin directory.
19
+
20
+ 2. (Optional) Copy Misultin files
21
+
22
+ This optional step will allow Misultin to be called from modules running from any directory on your file system.
23
+
24
+ Locate the directory where Erlang is installed on your system. Under OSX and Linux, it should be something similar to /usr/local/lib/erlang/ or /usr/lib/erlang/. Under Windows, Erlang is installed by default in something similar to C:\Program Files\erl5.7.2\ (version changes may apply).
25
+
26
+ Browse into the lib directory under this Erlang root and copy the root misultin-0.x directory into this directory. You should now have Misultin under something similar to:
27
+
28
+ * OSX | Linux users: /usr/local/lib/erlang/lib/misultin-0.x/
29
+ * Windows users: C:\Program Files\erl5.7.2\lib\misultin-0.x\
30
+
31
+ 3. Test
32
+
33
+ If you did proceed with step 2, CD to the Misultin examples directory and start an Erlang shell.
34
+
35
+ If you did not proceed with step 2, copy the file misultin_hello_world.erl from the Misultin examples directory to the directory where the .beam files compiled in step 1 are located, then CD to this directory and start an Erlang shell.
36
+
37
+ In the shell, compile and run misultin_hello_world by issuing:
38
+
39
+ (one@rob.loc)1>c(misultin_hello_world).
40
+ {ok,misultin_hello_world}
41
+ (one@rob.loc)2>misultin_hello_world:start(8080).
42
+ {ok,<0.50.0>}
43
+
44
+ Open your favourite browser and point it to http://localhost:8080/, you should read "Hello World." printed on your page.
45
+
46
+ You may now stop the misultin_hello_world HTTP server:
47
+
48
+ (one@rob.loc)3>misultin_hello_world:stop().
49
+
50
+ 4. Congratulations!
51
+
52
+ You're ready to go.
53
+
54
+ >-|-|-(°>
55
+
56
+
57
+ DOCUMENTATION
58
+ ==========================================================================================================
59
+
60
+ API Documentation is available online on the Misultin's wiki: http://code.google.com/p/misultin/wiki/
61
+
62
+
63
+ CHANGELOG
64
+ ==========================================================================================================
65
+
66
+ 0.6.1: - added support to websocket protocol hixie draft 76 [thanks to sergio veiga]
67
+ - added support to multiple websocket draft protocols [for backwards compatibility]
68
+ - added ws_autoexit option which allows to get an event on websocket controlling processes [issue track #15, suggestion of esente]
69
+ - added headers also in misultin websockets [thanks to jlirochon]
70
+ - made it basho's rebar friendly [thanks to mrinalwadhwa]
71
+
72
+ 0.6: - added HTTP compression option
73
+ - refactoring of the main server loop, so that it is now isolated from the HTTP functionality
74
+ - removed unnecessary compilation warnings
75
+ - replaced proplists:get_value with much faster utility function
76
+
77
+ 0.5: - added SSL support
78
+ - refactoring of the acceptor loop
79
+
80
+ 0.4: - added preliminary websocket support
81
+
82
+ 0.3.4: - added Req support to return the socket handling the request
83
+ - bug correction on Content-Length: 0 header causing timeout on POST requests [issue track #12,
84
+ thanks to gdamjan]
85
+
86
+ 0.3.3: - added echoing of the Connection header [issue track #7, thanks to thijsterlouw]
87
+ - bug correction on acceptor respawning [issue track #10, thanks to thijsterlouw]
88
+
89
+ 0.3.2: - optimized error handling [issue track #5, thanks to Max Lapshin]
90
+
91
+ 0.3.1: - added flow control using inet options {active, once} [issue track #4, thanks to Max Lapshin]
92
+ - added support to standard http headers response
93
+ - added http 400 bad request error in socket handling
94
+ - bug correction: removed erroneous sending of response timeout on listening open connections
95
+ - added stream_support optimization option
96
+
97
+ 0.3: - reengineering of the listener process, using active instead of passive mode in request parsing,
98
+ except for BODY where passive is still used [thanks to Lev Walkin]
99
+ - added better support for request timeout
100
+
101
+ 0.2.2: - added .app file [thanks to Essien Ita Essien]
102
+ - simplified get_options [thanks to Essien Ita Essien]
103
+ - added ip address option [thanks to Essien Ita Essien]
104
+ - added ipv6 support
105
+ - added recv_timeout option
106
+ - bug correction: requests peer address and port are now not reset on open connections multiple
107
+ requests
108
+
109
+ 0.2.1: - added support for Content-Type that specifies charset in POST data [thanks to Tuncer Ayaz]
110
+ - added support for iolist in misultin_req:ok/1,2 and misultin_req:respond/2,3
111
+ - code optimized taking out unnecessary binary conversion and lists:flatten [thanks to Feng Yu]
112
+
113
+ 0.2: - added trap exit for acceptor failure
114
+ - added backlog option
115
+ - added fallback if no connection header is present [issue track #1, thanks to Ciconia]
116
+ - added limit for parsing headers to avoid malicious attacks [thanks to Mazen Harake]
117
+ - minor bug corrections
118
+
119
+ 0.1: - initial release.
120
+
@@ -0,0 +1,9 @@
1
+ {application, misultin,
2
+ [
3
+ {description, "Lightweight HTTP(s) and Websockets Server Library"},
4
+ {vsn, "0.6.1"},
5
+ {modules, [misultin, misultin_req, misultin_socket, misultin_http, misultin_utility, misultin_websocket, misultin_ws]},
6
+ {registered, [misultin]},
7
+ {env, []},
8
+ {applications, [kernel, stdlib]}
9
+ ]}.
@@ -0,0 +1,43 @@
1
+ % ==========================================================================================================
2
+ % MISULTIN - Example: Hello World Compressed.
3
+ %
4
+ % >-|-|-(°>
5
+ %
6
+ % Copyright (C) 2009, Roberto Ostinelli <roberto@ostinelli.net>
7
+ % All rights reserved.
8
+ %
9
+ % BSD License
10
+ %
11
+ % Redistribution and use in source and binary forms, with or without modification, are permitted provided
12
+ % that the following conditions are met:
13
+ %
14
+ % * Redistributions of source code must retain the above copyright notice, this list of conditions and the
15
+ % following disclaimer.
16
+ % * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
17
+ % the following disclaimer in the documentation and/or other materials provided with the distribution.
18
+ % * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
19
+ % products derived from this software without specific prior written permission.
20
+ %
21
+ % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
22
+ % WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
+ % PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24
+ % ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25
+ % TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
+ % HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
+ % NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ % POSSIBILITY OF SUCH DAMAGE.
29
+ % ==========================================================================================================
30
+ -module(misultin_compress).
31
+ -export([start/1, stop/0]).
32
+
33
+ % start misultin http server
34
+ start(Port) ->
35
+ misultin:start_link([{port, Port}, {compress, true}, {loop, fun(Req) -> handle_http(Req) end}]).
36
+
37
+ % stop misultin
38
+ stop() ->
39
+ misultin:stop().
40
+
41
+ % callback on request received
42
+ handle_http(Req) ->
43
+ Req:ok("Hello World Compressed.").
@@ -0,0 +1,58 @@
1
+ % ==========================================================================================================
2
+ % MISULTIN - Example: Echoes inputted GET variables into an XML.
3
+ %
4
+ % >-|-|-(°>
5
+ %
6
+ % Copyright (C) 2009, Roberto Ostinelli <roberto@ostinelli.net>
7
+ % All rights reserved.
8
+ %
9
+ % BSD License
10
+ %
11
+ % Redistribution and use in source and binary forms, with or without modification, are permitted provided
12
+ % that the following conditions are met:
13
+ %
14
+ % * Redistributions of source code must retain the above copyright notice, this list of conditions and the
15
+ % following disclaimer.
16
+ % * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
17
+ % the following disclaimer in the documentation and/or other materials provided with the distribution.
18
+ % * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
19
+ % products derived from this software without specific prior written permission.
20
+ %
21
+ % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
22
+ % WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
+ % PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24
+ % ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25
+ % TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
+ % HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
+ % NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ % POSSIBILITY OF SUCH DAMAGE.
29
+ % ==========================================================================================================
30
+ -module(misultin_echo).
31
+ -export([start/1, stop/0]).
32
+
33
+ % start misultin http server
34
+ start(Port) ->
35
+ misultin:start_link([{port, Port}, {loop, fun(Req) -> handle_http(Req) end}]).
36
+
37
+ % stop misultin
38
+ stop() ->
39
+ misultin:stop().
40
+
41
+ % callback on request received
42
+ handle_http(Req) ->
43
+ % get params depending on method
44
+ Method = Req:get(method),
45
+ case Method of
46
+ 'GET' ->
47
+ Args = Req:parse_qs();
48
+ 'POST' ->
49
+ Args = Req:parse_post()
50
+ end,
51
+ % build an XML with all parameters and values
52
+ BuildXml = fun({Param, Value}, Acc) ->
53
+ [lists:flatten(io_lib:format("<param><name>~s</name><value>~s</value></param>", [Param, Value]))|Acc]
54
+ end,
55
+ Xml = lists:flatten(lists:reverse(lists:foldl(BuildXml, [], Args))),
56
+ % output
57
+ Req:ok([{"Content-Type", "text/xml"}], "<misultin_test><method>~s</method>~s</misultin_test>", [Method, Xml]).
58
+
@@ -0,0 +1,43 @@
1
+ % ==========================================================================================================
2
+ % MISULTIN - Example: Sends file as attachment.
3
+ %
4
+ % >-|-|-(°>
5
+ %
6
+ % Copyright (C) 2009, Roberto Ostinelli <roberto@ostinelli.net>
7
+ % All rights reserved.
8
+ %
9
+ % BSD License
10
+ %
11
+ % Redistribution and use in source and binary forms, with or without modification, are permitted provided
12
+ % that the following conditions are met:
13
+ %
14
+ % * Redistributions of source code must retain the above copyright notice, this list of conditions and the
15
+ % following disclaimer.
16
+ % * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
17
+ % the following disclaimer in the documentation and/or other materials provided with the distribution.
18
+ % * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
19
+ % products derived from this software without specific prior written permission.
20
+ %
21
+ % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
22
+ % WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
+ % PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24
+ % ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25
+ % TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
+ % HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
+ % NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ % POSSIBILITY OF SUCH DAMAGE.
29
+ % ==========================================================================================================
30
+ -module(misultin_file).
31
+ -export([start/1, stop/0]).
32
+
33
+ % start misultin http server
34
+ start(Port) ->
35
+ misultin:start_link([{port, Port}, {loop, fun(Req) -> handle_http(Req) end}]).
36
+
37
+ % stop misultin
38
+ stop() ->
39
+ misultin:stop().
40
+
41
+ % callback on request received
42
+ handle_http(Req) ->
43
+ Req:file(attachment, "1.png").
@@ -0,0 +1,158 @@
1
+ % ==========================================================================================================
2
+ % MISULTIN - Example: Running Misultin from a gen_server.
3
+ %
4
+ % >-|-|-(°>
5
+ %
6
+ % Copyright (C) 2009, Roberto Ostinelli <roberto@ostinelli.net>
7
+ % All rights reserved.
8
+ %
9
+ % BSD License
10
+ %
11
+ % Redistribution and use in source and binary forms, with or without modification, are permitted provided
12
+ % that the following conditions are met:
13
+ %
14
+ % * Redistributions of source code must retain the above copyright notice, this list of conditions and the
15
+ % following disclaimer.
16
+ % * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
17
+ % the following disclaimer in the documentation and/or other materials provided with the distribution.
18
+ % * Neither the name of the authors nor the names of its contributors may be used to endorse or promote
19
+ % products derived from this software without specific prior written permission.
20
+ %
21
+ % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
22
+ % WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
+ % PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24
+ % ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
25
+ % TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26
+ % HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27
+ % NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
+ % POSSIBILITY OF SUCH DAMAGE.
29
+ % ==========================================================================================================
30
+ -module(misultin_gen_server).
31
+ -behaviour(gen_server).
32
+
33
+ % gen_server callbacks
34
+ -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
35
+
36
+ % API
37
+ -export([start_link/1, stop/0]).
38
+
39
+ % records
40
+ -record(state, {
41
+ port
42
+ }).
43
+
44
+ % macros
45
+ -define(SERVER, ?MODULE).
46
+
47
+
48
+ % ============================ \/ API ======================================================================
49
+
50
+ % Function: {ok,Pid} | ignore | {error, Error}
51
+ % Description: Starts the server.
52
+ start_link(Port) ->
53
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [Port], []).
54
+
55
+ % Function: -> ok
56
+ % Description: Manually stops the server.
57
+ stop() ->
58
+ gen_server:cast(?SERVER, stop).
59
+
60
+ % ============================ /\ API ======================================================================
61
+
62
+
63
+ % ============================ \/ GEN_SERVER CALLBACKS =====================================================
64
+
65
+ % ----------------------------------------------------------------------------------------------------------
66
+ % Function: -> {ok, State} | {ok, State, Timeout} | ignore | {stop, Reason}
67
+ % Description: Initiates the server.
68
+ % ----------------------------------------------------------------------------------------------------------
69
+ init([Port]) ->
70
+ % trap_exit -> this gen_server needs to be supervised
71
+ process_flag(trap_exit, true),
72
+ % start misultin & set monitor
73
+ misultin:start_link([{port, Port}, {loop, fun(Req) -> handle_http(Req) end}]),
74
+ erlang:monitor(process, misultin),
75
+ {ok, #state{port = Port}}.
76
+
77
+ % ----------------------------------------------------------------------------------------------------------
78
+ % Function: handle_call(Request, From, State) -> {reply, Reply, State} | {reply, Reply, State, Timeout} |
79
+ % {noreply, State} | {noreply, State, Timeout} |
80
+ % {stop, Reason, Reply, State} | {stop, Reason, State}
81
+ % Description: Handling call messages.
82
+ % ----------------------------------------------------------------------------------------------------------
83
+
84
+ % handle_call generic fallback
85
+ handle_call(_Request, _From, State) ->
86
+ {reply, undefined, State}.
87
+
88
+ % ----------------------------------------------------------------------------------------------------------
89
+ % Function: handle_cast(Msg, State) -> {noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}
90
+ % Description: Handling cast messages.
91
+ % ----------------------------------------------------------------------------------------------------------
92
+
93
+ % manual shutdown
94
+ handle_cast(stop, State) ->
95
+ {stop, normal, State};
96
+
97
+ % handle_cast generic fallback (ignore)
98
+ handle_cast(_Msg, State) ->
99
+ {noreply, State}.
100
+
101
+ % ----------------------------------------------------------------------------------------------------------
102
+ % Function: handle_info(Info, State) -> {noreply, State} | {noreply, State, Timeout} | {stop, Reason, State}
103
+ % Description: Handling all non call/cast messages.
104
+ % ----------------------------------------------------------------------------------------------------------
105
+
106
+ % handle info when misultin server goes down -> take down misultin_gen_server too [the supervisor will take everything up again]
107
+ handle_info({'DOWN', _, _, {misultin, _}, _}, State) ->
108
+ {stop, normal, State};
109
+
110
+ % handle_info generic fallback (ignore)
111
+ handle_info(_Info, State) ->
112
+ {noreply, State}.
113
+
114
+ % ----------------------------------------------------------------------------------------------------------
115
+ % Function: terminate(Reason, State) -> void()
116
+ % Description: This function is called by a gen_server when it is about to terminate. When it returns,
117
+ % the gen_server terminates with Reason. The return value is ignored.
118
+ % ----------------------------------------------------------------------------------------------------------
119
+ terminate(_Reason, _State) ->
120
+ % stop misultin
121
+ misultin:stop(),
122
+ terminated.
123
+
124
+ % ----------------------------------------------------------------------------------------------------------
125
+ % Func: code_change(OldVsn, State, Extra) -> {ok, NewState}
126
+ % Description: Convert process state when code is changed.
127
+ % ----------------------------------------------------------------------------------------------------------
128
+ code_change(_OldVsn, State, _Extra) ->
129
+ {ok, State}.
130
+
131
+ % ============================ /\ GEN_SERVER CALLBACKS =====================================================
132
+
133
+
134
+ % ============================ \/ INTERNAL FUNCTIONS =======================================================
135
+
136
+ % ---------------------------- \/ misultin requests --------------------------------------------------------
137
+
138
+ handle_http(Req) ->
139
+ % get params depending on method
140
+ Method = Req:get(method),
141
+ case Method of
142
+ 'GET' ->
143
+ Args = Req:parse_qs();
144
+ 'POST' ->
145
+ Args = Req:parse_post()
146
+ end,
147
+ % build an XML with all parameters and values
148
+ BuildXml = fun({Param, Value}, Acc) ->
149
+ [lists:flatten(io_lib:format("<param><name>~s</name><value>~s</value></param>", [Param, Value]))|Acc]
150
+ end,
151
+ Xml = lists:flatten(lists:reverse(lists:foldl(BuildXml, [], Args))),
152
+ % output
153
+ Req:ok([{"Content-Type", "text/xml"}], "<misultin_test><method>~s</method>~s</misultin_test>", [Method, Xml]).
154
+
155
+ % ---------------------------- /\ misultin requests --------------------------------------------------------
156
+
157
+ % ============================ /\ INTERNAL FUNCTIONS =======================================================
158
+