Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
1.02-prebeta-16421
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewerr committed Apr 16, 2021
1 parent d131920 commit 23a5bd0
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ The directives currently supported by server:
* `Disallow` - sends `HTTP/1.1 403 Forbidden` to client
* `Set-Header <<HEADER>> <<VALUE>>` - sets response header `<<HEADER>>` to `<<VALUE>>`
* `ExecFCGI <<FILE>> <<FCGI_HOST>> <<FCGI_PORT>> <<FCGI_TIMEOUT>>` - asks FastCGI running on `<<FCGI_HOST>>:<<FCGI_PORT>>` to execute `<<FILE>>` with timeout of `<<FCGI_TIMEOUT>>` ms

* `Set-Status <<CODE>>` sets status code for a response.
* `Send-File <<PATH>>` sends file as a response.
## Credits
* erl_fastcgi - Copyright 2017, Marcelo Gornstein <[email protected]> (Apache-2.0 license).<br> Changes introduced(file: `src/erl_fastcgi.erl`):
* Added logging integrated with MeowMeow webserver
Expand Down
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{relx, [{release, {'MeowMeow', "1.02-prebeta-5421"}, [app]},
{relx, [{release, {'MeowMeow', "1.02-prebeta-16421"}, [app]},
{dev_mode, true},
{include_erts, false},
{extended_start_script, true}
Expand Down
5 changes: 3 additions & 2 deletions src/access.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ parse_line(Dev, {ok, Line}) ->
case Cmd of
pass -> {ok, []};
{"Section", true} -> {ok, []};
{"Route", [Name]} -> {ok, [{Name, parse_section({ok, Dev}, [], Name)}]};
{"Route", [Name]} -> {ok, [{route, Name, parse_section({ok, Dev}, [], Name)}]};
{"Host", [Name]} -> {ok, [{host, Name, parse_section({ok, Dev}, [], Name)}]};
{"End", _} -> finish;
{Key, Value} -> {ok, [{Key, Value}]};
Any -> logging:err("get_cmd/1 returned unexpected result ~p @ access:parse_line/2", [Any])
Expand Down Expand Up @@ -57,7 +58,7 @@ reload() ->

get_rules(_, [], Rules) -> Rules;
get_rules(Route, Array, Rules) ->
[{Pattern, List} | T] = Array,
[{route, Pattern, List} | T] = Array,
Stat = util:check_wildcard(Route, Pattern),
if Stat -> get_rules(Route, T, Rules ++ List);
true -> get_rules(Route, T, Rules)
Expand Down
2 changes: 1 addition & 1 deletion src/app.app.src
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{application, app, [
{description, "A tiny webserver written in erlang"},
{vsn, "1.02-prebeta-5421"},
{vsn, "1.02-prebeta-16421"},
{modules, []},
{registered, [app_sup]},
{applications, [
Expand Down
2 changes: 1 addition & 1 deletion src/config.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-author("p01ar").
-record(sockaddr_in4, {family = inet, port = 8888, addr = {0, 0, 0, 0}}).
-define(CHUNK_SIZE, 2048).
-define(version, "MeowMeow/1.02-prebeta-5421").
-define(version, "MeowMeow/1.02-prebeta-16421").
-define(accessfile, "/etc/MeowMeow/routes.conf").
-define(max_request_length, 10000).
-define(mime_types_file, "/etc/MeowMeow/mime.types").
Expand Down
1 change: 1 addition & 0 deletions src/handle.erl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ wrap_fname_stat(FName) ->
{error, Err} -> Err;
{ok, FInfo} -> {FName, FInfo}
end.

get_filename(XRoute) ->
Route = binary:bin_to_list(XRoute, {1, string:length(XRoute) - 1}),
SafeFName = filelib:safe_relative_path(Route, ?docdir),
Expand Down
1 change: 1 addition & 0 deletions src/response.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ get_desc(Code) ->
"428" => "Precondition Required",
"429" => "Too Many Requests",
"431" => "Request Header Fields Too Large",
"451" => "Unavailable For Legal Reasons",
"500" => "Internal Server Error",
"501" => "Not Implemented",
"502" => "Bad Gateway",
Expand Down
18 changes: 18 additions & 0 deletions src/rules.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ rule_set_header(Arg, Response) ->
[Header|Value] = string:split(Arg, " "),
Response#response{headers = update_headers(Response, #{Header => Value})}.

rule_set_code(Arg, Response)->
{Code, []} = string:to_integer(Arg),
Response#response{code = Code}.

rule_fcgi_exec(Arg, Response) ->
StrTime = util:get_time(),
Expand All @@ -68,13 +71,28 @@ rule_fcgi_exec(Arg, Response) ->
{aborted, 502}
end.

rule_send_file(Arg, RawResponse) ->
case handle:stat_file(file:read_file_info(Arg)) of
{FSize,ok} -> Response = RawResponse#response{headers = update_headers(RawResponse,
#{"Content-Length" => erlang:integer_to_list(FSize)})},
io_proxy:tcp_send(Response#response.socket,
response:response_headers(Response#response.headers,
Response#response.code)),
handle:send_file(Arg, Response#response.socket, ?chunk_size),
Response#response{is_finished=true};
Any -> logging:err("Bad stat for ~s: ~p",[Arg, Any]),
{aborted, 500}
end.

register_basic() ->
logging:info("Registering basic rules"),
register_rule("Abort", fun(Args, Resp) -> rule_abort(Args, Resp) end),
register_rule("No-Content", fun(Args, Resp) -> rule_no_content(Args, Resp) end),
register_rule("Disallow", fun(Args, Resp) -> rule_disallow(Args, Resp) end),
register_rule("Set-Header", fun(Args, Resp) -> rule_set_header(Args, Resp) end),
register_rule("ExecFCGI", fun(Args, Resp) -> rule_fcgi_exec(Args, Resp) end),
register_rule("Set-Code", fun(Args, Resp) -> rule_set_code(Args, Resp) end),
register_rule("Send-File", fun(Args, Resp) -> rule_send_file(Args, Resp) end),
ok.


Expand Down
12 changes: 11 additions & 1 deletion src/server.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(server).
-export([run/1, run_synchronized/1, start/0, stop/0]).
-export([run/1, run_synchronized/1, start/0, stop/0, start_async/0]).
-import(socket, [create_socket/1, socket_recv/2, socket_accept/3, socket_send/2, socket_recv_all/2]).
-import(handle, [handle_http11/1, abort/1]).
-import(parse_http, [http2map/1]).
Expand Down Expand Up @@ -82,11 +82,21 @@ run_synchronized(Port) ->
listen_synchronized(Port).

start() ->
io:fwrite("
______ ___ ______ ___
___ |/ /_____________ ____ |/ /_____________ __
__ /|_/ /_ _ \\ __ \\_ | /| / /_ /|_/ /_ _ \\ __ \\_ | /| / /
_ / / / / __/ /_/ /_ |/ |/ /_ / / / / __/ /_/ /_ |/ |/ /
/_/ /_/ \\___/\\____/____/|__/ /_/ /_/ \\___/\\____/____/|__/
Version ~s~n", [?version]),
configuration:load(),
rules:init_rules(),
rules:register_basic(),
access:load_access(?accessfile),
run_synchronized(configuration:get("ListenPort", int)).

start_async() ->
spawn(fun() -> start() end).
stop() ->
init:stop().

0 comments on commit 23a5bd0

Please sign in to comment.