PR feedback
This commit is contained in:
@@ -54,7 +54,7 @@
|
|||||||
http_body/2,
|
http_body/2,
|
||||||
http_redirect/2,
|
http_redirect/2,
|
||||||
http_query/3,
|
http_query/3,
|
||||||
urldecode//1
|
url_decode//1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
:- use_module(library(sockets)).
|
:- use_module(library(sockets)).
|
||||||
@@ -67,13 +67,6 @@
|
|||||||
:- use_module(library(time)).
|
:- use_module(library(time)).
|
||||||
:- use_module(library(crypto)).
|
:- use_module(library(crypto)).
|
||||||
|
|
||||||
% TODO
|
|
||||||
% - HTTP Error Codes
|
|
||||||
% - Improve code quality
|
|
||||||
% - Comments
|
|
||||||
% - Remove !
|
|
||||||
% - URL Encode
|
|
||||||
|
|
||||||
% Server initialization
|
% Server initialization
|
||||||
http_listen(Port, Handlers) :-
|
http_listen(Port, Handlers) :-
|
||||||
must_be(integer, Port),
|
must_be(integer, Port),
|
||||||
@@ -84,12 +77,12 @@ http_listen(Port, Handlers) :-
|
|||||||
|
|
||||||
% Server loop
|
% Server loop
|
||||||
accept_loop(Socket, Handlers) :-
|
accept_loop(Socket, Handlers) :-
|
||||||
setup_call_cleanup(socket_server_accept(Socket, Client, Stream, [type(binary)]),
|
setup_call_cleanup(socket_server_accept(Socket, _Client, Stream, [type(binary)]),
|
||||||
(
|
(
|
||||||
read_header_lines(Stream, Lines),
|
read_header_lines(Stream, Lines),
|
||||||
[Request|Headers] = Lines,
|
[Request|Headers] = Lines,
|
||||||
(
|
(
|
||||||
(phrase(parse_request(Version, Method, Path, Queries), Request), maplist(map_parse_header, Headers, HeadersKV)) -> (
|
(phrase(parse_request(_Version, Method, Path, Queries), Request), maplist(map_parse_header, Headers, HeadersKV)) -> (
|
||||||
(
|
(
|
||||||
member("content-length"-ContentLength, HeadersKV) ->
|
member("content-length"-ContentLength, HeadersKV) ->
|
||||||
(number_chars(ContentLengthN, ContentLength), get_bytes(Stream, ContentLengthN, Body))
|
(number_chars(ContentLengthN, ContentLength), get_bytes(Stream, ContentLengthN, Body))
|
||||||
@@ -237,12 +230,12 @@ parse_path(Path, []) -->
|
|||||||
parse_queries([Key-Value|Queries]) -->
|
parse_queries([Key-Value|Queries]) -->
|
||||||
string_without("=", Key0),
|
string_without("=", Key0),
|
||||||
{
|
{
|
||||||
phrase(urldecode(Key), Key0)
|
phrase(url_decode(Key), Key0)
|
||||||
},
|
},
|
||||||
"=",
|
"=",
|
||||||
string_without("&", Value0),
|
string_without("&", Value0),
|
||||||
{
|
{
|
||||||
phrase(urldecode(Value), Value0)
|
phrase(url_decode(Value), Value0)
|
||||||
},
|
},
|
||||||
"&",
|
"&",
|
||||||
parse_queries(Queries).
|
parse_queries(Queries).
|
||||||
@@ -250,12 +243,12 @@ parse_queries([Key-Value|Queries]) -->
|
|||||||
parse_queries([Key-Value]) -->
|
parse_queries([Key-Value]) -->
|
||||||
string_without("=", Key0),
|
string_without("=", Key0),
|
||||||
{
|
{
|
||||||
phrase(urldecode(Key), Key0)
|
phrase(url_decode(Key), Key0)
|
||||||
},
|
},
|
||||||
"=",
|
"=",
|
||||||
string_without(" ", Value0),
|
string_without(" ", Value0),
|
||||||
{
|
{
|
||||||
phrase(urldecode(Value), Value0)
|
phrase(url_decode(Value), Value0)
|
||||||
}.
|
}.
|
||||||
|
|
||||||
map_parse_header(Header, HeaderKV) :-
|
map_parse_header(Header, HeaderKV) :-
|
||||||
@@ -347,13 +340,13 @@ char_lower(Char, Lower) :-
|
|||||||
; Char = Lower).
|
; Char = Lower).
|
||||||
|
|
||||||
% Decodes a UTF-8 URL Encoded string: RFC-1738
|
% Decodes a UTF-8 URL Encoded string: RFC-1738
|
||||||
urldecode([Char|Chars]) -->
|
url_decode([Char|Chars]) -->
|
||||||
[Char],
|
[Char],
|
||||||
{
|
{
|
||||||
Char \= '%'
|
Char \= '%'
|
||||||
},
|
},
|
||||||
urldecode(Chars).
|
url_decode(Chars).
|
||||||
urldecode([Char|Chars]) -->
|
url_decode([Char|Chars]) -->
|
||||||
"%",
|
"%",
|
||||||
[A],
|
[A],
|
||||||
[B],
|
[B],
|
||||||
@@ -364,8 +357,8 @@ urldecode([Char|Chars]) -->
|
|||||||
chars_utf8bytes(Chars0, Bytes),
|
chars_utf8bytes(Chars0, Bytes),
|
||||||
Chars0 = [Char]
|
Chars0 = [Char]
|
||||||
},
|
},
|
||||||
urldecode(Chars).
|
url_decode(Chars).
|
||||||
urldecode([Char|Chars]) -->
|
url_decode([Char|Chars]) -->
|
||||||
"%",
|
"%",
|
||||||
[A, B],
|
[A, B],
|
||||||
"%",
|
"%",
|
||||||
@@ -377,8 +370,8 @@ urldecode([Char|Chars]) -->
|
|||||||
chars_utf8bytes(Chars0, Bytes),
|
chars_utf8bytes(Chars0, Bytes),
|
||||||
Chars0 = [Char]
|
Chars0 = [Char]
|
||||||
},
|
},
|
||||||
urldecode(Chars).
|
url_decode(Chars).
|
||||||
urldecode([Char|Chars]) -->
|
url_decode([Char|Chars]) -->
|
||||||
"%",
|
"%",
|
||||||
[A, B],
|
[A, B],
|
||||||
"%",
|
"%",
|
||||||
@@ -392,8 +385,8 @@ urldecode([Char|Chars]) -->
|
|||||||
chars_utf8bytes(Chars0, Bytes),
|
chars_utf8bytes(Chars0, Bytes),
|
||||||
Chars0 = [Char]
|
Chars0 = [Char]
|
||||||
},
|
},
|
||||||
urldecode(Chars).
|
url_decode(Chars).
|
||||||
urldecode([Char|Chars]) -->
|
url_decode([Char|Chars]) -->
|
||||||
"%",
|
"%",
|
||||||
[A, B],
|
[A, B],
|
||||||
"%",
|
"%",
|
||||||
@@ -407,6 +400,6 @@ urldecode([Char|Chars]) -->
|
|||||||
chars_utf8bytes(Chars0, Bytes),
|
chars_utf8bytes(Chars0, Bytes),
|
||||||
Chars0 = [Char]
|
Chars0 = [Char]
|
||||||
},
|
},
|
||||||
urldecode(Chars).
|
url_decode(Chars).
|
||||||
|
|
||||||
urldecode([]) --> [].
|
url_decode([]) --> [].
|
||||||
|
|||||||
Reference in New Issue
Block a user