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

Commit

Permalink
WIP: Fix for issue #9
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewerr committed Jun 14, 2021
1 parent 9a3a07b commit 81ae6eb
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 15 deletions.
7 changes: 6 additions & 1 deletion config/meow.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# MeowMeow main config file
# This file contains webserve listening config
# This file contains webserver listening config
# To configure request handling logic see routes.conf

## DocDir
Expand Down Expand Up @@ -30,3 +30,8 @@ LogLevel 4
## KeepAlive
# Sets time in ms after which opened connection with Keep-Alive flag will be killed.
KeepAlive 10000 #ms

## AllowLegacyHttp
# Allows older version of HTTP protocol, i.e. HTTP/1.0. Not reccomended to use it. "No" by default.
AllowLegacyHttp No

18 changes: 11 additions & 7 deletions src/access.erl
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,17 @@ get_rules_checked(Request, {Type, Pattern, List}, Rules, T)->
true -> get_rules(Request, T, Rules)
end;
host ->
Host=string:trim(maps:get("Host",Request#request.header)),
logging:debug("Host=`~s`,Pattern=`~s`",[Host, Pattern]),
StatHost = util:check_wildcard(Host, Pattern),
logging:debug("StatHost=~p",[StatHost]),
if StatHost -> get_rules(Request, T, Rules ++ get_rules(Request,List,[]));
true -> get_rules(Request, T, Rules)
end
IsKey = maps:is_key("Host", Request#request.header),
if IsKey ->
Host=string:trim(maps:get("Host",Request#request.header)),
logging:debug("Host=`~s`,Pattern=`~s`",[Host, Pattern]),
StatHost = util:check_wildcard(Host, Pattern),
logging:debug("StatHost=~p",[StatHost]),
if StatHost -> get_rules(Request, T, Rules ++ get_rules(Request,List,[]));
true -> get_rules(Request, T, Rules)
end;
true -> logging:warn("The Host header is required by config, but client did not provide it, so ignoring all Host rules.")
end
end.

get_rules(Request) ->
Expand Down
4 changes: 2 additions & 2 deletions src/config.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
-author("p01ar").
-record(sockaddr_in4, {family = inet, port = 8888, addr = {0, 0, 0, 0}}).
-define(CHUNK_SIZE, 2048).
-define(version, "MeowMeow/1.02-prebeta-r10").
-define(version, "MeowMeow/1.02-prebeta-r11").
-define(accessfile, "/etc/MeowMeow/routes.conf").
-define(max_request_length, 10000).
-define(mime_types_file, "/etc/MeowMeow/mime.types").
-define(docdir, configuration:get("DocDir",string)).
-define(chunk_size, 1400).
-define(timeout, 10000).
-define(configfile, "/etc/MeowMeow/meow.conf").
-define(defconf, #{"DocDir"=>"/var/www/","LogLevel" => "0", "ListenHost" => "127.0.0.1", "KeepAlive"=> "10000", "ListenPort"=>"80"}). %% Default configuration
-define(defconf, #{"DocDir"=>"/var/www/","LogLevel" => "0", "ListenHost" => "127.0.0.1", "KeepAlive"=> "10000", "ListenPort"=>"80", "AllowLegacyHttp"=>"No"}). %% Default configuration
9 changes: 9 additions & 0 deletions src/configuration.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ get(VarName, string) ->
[{table, Map}] = ets:lookup(config_storage, table),
maps:get(VarName, Map);

get(VarName, bool) ->
[{table, Map}] = ets:lookup(config_storage, table),
StrValue = string:trim(string:lowercase(maps:get(VarName, Map))),
if StrValue == "no" -> false;
StrValue == "yes" -> true;
true -> logging:err("Can not parse config: ~s option requires Yes/No values, but got ~p", []),
{error, parse_error}
end;

get(VarName, int) ->
[{table, Map}] = ets:lookup(config_storage, table),
%%io:fwrite("VarName=~s,Map=~p,tp=int~n", [VarName, Map]),
Expand Down
25 changes: 23 additions & 2 deletions src/handle.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@
-include("request.hrl").
-include("response.hrl").

%% Checks that request is good
is_good(Request) ->
AllowLegacy = configuration:get("AllowLegacyHttp", bool),
if not AllowLegacy ->
{Major, Minor} = util:get_http_ver_pair(Request#request.http_ver),
((Major == 1) and (Minor >= 1)) or (Major > 1);
true -> true
end.

get_ua(Request) ->
IsKey = maps:is_key("User-Agent", Request#request.header),
if IsKey -> maps:get("User-Agent", Request#request.header);
true -> "<<Unknown UA>>"
end.

log_response(Request, Code) ->
logging:info("~p.~p.~p.~p ~s ~s -- ~p ~s", util:tup2list(Request#request.src_addr) ++ [Request#request.method, Request#request.route, Code, get_desc(integer_to_list(Code))]).

Expand Down Expand Up @@ -215,6 +225,18 @@ handle(Sock, Upstream, RequestLines) ->
Upstream ! {send, abort(400)},
ok;
{ok, Request} ->
IsGood = is_good(Request),
if IsGood ->
handle_by_method(Request, Upstream, Sock);
true->
log_response(Request, 400),
Upstream ! {send, abort(400)},
Upstream ! close,
ok
end
end.

handle_by_method(Request, Upstream, Sock) ->
case Request#request.method of
<<"GET">>->
Response = #response{socket = Sock, code = 200, request = Request, upstream = Upstream},
Expand All @@ -237,8 +259,7 @@ handle(Sock, Upstream, RequestLines) ->
Upstream ! {send, abort(405)},
Upstream ! close,
ok
end
end.
end.

handler(Sock, Upstream, RequestLines) ->
receive
Expand Down
3 changes: 2 additions & 1 deletion src/parse_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,14 @@ parse_request(SrcAddr, Lines) ->
{ok, Map} ->
Method = maps:get(method, Map),
BinRoute = maps:get(route, Map),
HttpVer = maps:get(http_ver, Map),
logging:debug("BinRoute=~p", [BinRoute]),
SRoute = binary:split(BinRoute,<<"?">>),
case SRoute of
[CRoute] -> Route = CRoute, Params = "";
[CRoute, CParams] -> Route = CRoute, Params = CParams
end,
{ok, #request{src_addr = SrcAddr, route = Route, header = Map, method = Method, params = Params}}
{ok, #request{src_addr = SrcAddr, http_ver = HttpVer, route = Route, header = Map, method = Method, params = Params}}
end.


Expand Down
2 changes: 1 addition & 1 deletion src/request.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
%%%-------------------------------------------------------------------
-author("p01ar").

-record(request, {src_addr, route, method, header, body = "", params = ""}).
-record(request, {src_addr, route, method, header, http_ver = "HTTP/0.9", body = "", params = ""}).
7 changes: 6 additions & 1 deletion src/util.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-module(util).
-export([addr2str/1, str2addr/1, get_time/0, wildcard2regex/1, check_wildcard/2, tup2list/1, sget/2, sget2/2, pretty_addr/1]).
-export([addr2str/1, get_http_ver_pair/1, str2addr/1, get_time/0, wildcard2regex/1, check_wildcard/2, tup2list/1, sget/2, sget2/2, pretty_addr/1]).
-include("config.hrl").

%% This part of code converts wildcards to regex
Expand Down Expand Up @@ -90,6 +90,11 @@ str2addr(Str) ->
true -> logging:err("Failed to parse IP ~s",[Str])
end.

get_http_ver_pair(Str) ->
Ver = lists:nth(string:split(Str,"/"),2),
[VerMajor, VerMinor] = string:split(Ver, "."),
{str2int(VerMajor),str2int(VerMinor)}.

addr2str(Addr)->
lists:flatten(io_lib:format("~p.~p.~p.~p",Addr)).

Expand Down

0 comments on commit 81ae6eb

Please sign in to comment.