-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.erl
64 lines (54 loc) · 1.4 KB
/
http.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
%%%-------------------------------------------------------------------
%%% @author ben
%%% @copyright (C) 2017, <COMPANY>
%%% @doc
%%%
%%% @end
%%% Created : 19. feb 2017 10:34
%%%-------------------------------------------------------------------
%%%-------------------------------------------------------------------
%% C:\Users\ben\Desktop\Erlang tcp_udp_http
%% cd("C:/Users/ben/Desktop/Erlang tcp_udp_http").
-module(http).
-author("ben").
%% API
-export([parse_request/1]).
-compile(export_all).
parse_request(R0) ->
{Request, R1} = request_line(R0),
{Headers, R2} = headers(R1),
{Body, _} = message_body(R2),
{Request, Headers, Body}
.
request_line([$G, $E, $T, 32 |R0]) ->
{URI, R1} = request_uri(R0),
{Ver, R2} = http_version(R1),
[13,10|R3] = R2,
{{get, URI, Ver}, R3}.
request_uri([32|R0])->
{[], R0};
request_uri([C|R0]) ->
{Rest, R1} = request_uri(R0),
{[C|Rest], R1}.
http_version([$H, $T, $T, $P, $/, $1, $., $1 | R0]) ->
{v11, R0};
http_version([$H, $T, $T, $P, $/, $1, $., $0 | R0]) ->
{v10, R0}.
headers([13,10|R0]) ->
{[],R0};
headers(R0) ->
{Header, R1} = header(R0),
{Rest, R2} = headers(R1),
{[Header|Rest], R2}.
header([13,10|R0]) ->
{[], R0};
header([C|R0]) ->
{Rest, R1} = header(R0),
{[C|Rest], R1}.
message_body(R) ->
{R, []}.
%%reply section
ok(Body) ->
"HTTP/1.1 200 OK\r\n" ++ "\r\n" ++ Body.
get(URI) ->
"GET " ++ URI ++ " HTTP/1.1\r\n" ++ "\r\n".