Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: fa9b2b271d
Fetching contributors…

Cannot retrieve contributors at this time

file 125 lines (111 sloc) 4.112 kb
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
-module (misultin_request_bridge).
-behaviour (simple_bridge_request).
-include_lib ("simple_bridge.hrl").
-export ([
    init/1,
    request_method/1, path/1, uri/1,
    peer_ip/1, peer_port/1,
    headers/1, header/2, cookies/1,
    query_params/1, post_params/1, request_body/1,
    socket/1, recv_from_socket/3
]).

init(Req) ->
    Req.

request_method(Req) ->
    Req:get(method).

path(Req) ->
    {abs_path,Path} = Req:get(uri),
    Path.

uri(Req) ->
    Req:get(uri).

peer_ip(Req) ->
    case Req:get(peer_addr) of
        {ok, IP} -> IP;
        IP -> IP
    end.

peer_port(Req) ->
    Req:get(peer_port).

header(connection, Req) ->
    misultin_utility:get_key_value('Connection', Req:get(headers));
header(accept, Req) ->
    misultin_utility:get_key_value('Accept', Req:get(headers));
header(host, Req) ->
    misultin_utility:get_key_value('Host', Req:get(headers));
header(if_modified_since, Req) ->
    misultin_utility:get_key_value('If-Modified-Since', Req:get(headers));
header(if_match, Req) ->
    misultin_utility:get_key_value('If-Match', Req:get(headers));
header(if_none_match, Req) ->
    misultin_utility:get_key_value('If-None-Match', Req:get(headers));
header(if_range, Req) ->
    misultin_utility:get_key_value('If-Range', Req:get(headers));
header(if_unmodified_since, Req) ->
    misultin_utility:get_key_value('If-Unmodified-Since', Req:get(headers));
header(range, Req) ->
    misultin_utility:get_key_value('Range', Req:get(headers));
header(referer, Req) ->
    misultin_utility:get_key_value('Referer', Req:get(headers));
header(user_agent, Req) ->
    misultin_utility:get_key_value('User-Agent', Req:get(headers));
header(accept_ranges, Req) ->
    misultin_utility:get_key_value('Accept-Ranges', Req:get(headers));
header(cookie, Req) ->
    misultin_utility:get_key_value('Cookie', Req:get(headers));
header(keep_alive, Req) ->
    misultin_utility:get_key_value('Keep-Alive', Req:get(headers));
header(location, Req) ->
    misultin_utility:get_key_value('Location', Req:get(headers));
header(content_length, Req) ->
    misultin_utility:get_key_value('Content-Length', Req:get(headers));
header(content_type, Req) ->
    misultin_utility:get_key_value('Content-Type', Req:get(headers));
header(content_encoding, Req) ->
    misultin_utility:get_key_value('Content-Encoding', Req:get(headers));
header(authorization, Req) ->
    misultin_utility:get_key_value('Authorization', Req:get(headers));
header(x_forwarded_for, Req) ->
    misultin_utility:get_key_value('X-Forwarded-For', Req:get(headers));
header(transfer_encoding, Req) ->
    misultin_utility:get_key_value('Transfer-Encoding', Req:get(headers));
header(accept_language, Req) ->
    misultin_utility:get_key_value('Accept-Language', Req:get(headers));
header(Header, Req) ->
    misultin_utility:get_key_value(Header, Req:get(headers)).

headers(Req) ->
    Headers1 = [ connection, accept, host, if_modified_since,
        if_match, if_none_match, if_range, if_unmodified_since,
        range, referer, user_agent, accept_language, accept_ranges, cookie,
        keep_alive, location, content_length, content_type,
        content_encoding, authorization, x_forwarded_for, transfer_encoding
    ],
    Headers2 = lists:map(fun(H) -> {H, header(H, Req)} end, Headers1),
    [{K, V} || {K, V} <- Headers2, V /= undefined].

cookies(Req) ->
    Headers = headers(Req),
    CookieData = proplists:get_value(cookie, Headers, ""),
    F = fun(Cookie) ->
        case string:tokens(Cookie, "=") of
            [] -> [];
            L ->
                X = string:strip(hd(L)),
                Y = string:join(tl(L), "="),
                {X, Y}
        end
    end,
    [F(X) || X <- string:tokens(CookieData, ";")].

query_params(Req) ->
    Req:parse_qs().

post_params(Req) ->
    Req:parse_post().

request_body(Req) ->
    Req:get(body).

socket(Req) ->
    Req:get(socket).

recv_from_socket(Length, Timeout, Req) ->
    Socket = socket(Req),
    case gen_tcp:recv(Socket, Length, Timeout) of
        {ok, Data} ->
            Data;
        _Other ->
            exit(normal)
    end.
Something went wrong with that request. Please try again.