PageRenderTime 165ms CodeModel.GetById 60ms app.highlight 48ms RepoModel.GetById 53ms app.codeStats 0ms

/include/ewgi.hrl

http://github.com/skarab/ewgi
Erlang | 240 lines | 149 code | 34 blank | 57 comment | 0 complexity | 8ea41c5603499e2fc4f0c04d8f3e1ece MD5 | raw file
  1-ifndef(_EWGI_HRL).
  2-define(_EWGI_HRL, 1).
  3
  4% ``The contents of this file are subject to the Mozilla Public License
  5% Version 1.1 (the "License"); you may not use this file except in
  6% compliance with the License. You may obtain a copy of the License at
  7% http://www.mozilla.org/MPL/
  8% 
  9% Software distributed under the License is distributed on an "AS IS"
 10% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 11% License for the specific language governing rights and limitations
 12% under the License.
 13% 
 14% The Original Code is the EWGI reference implementation.
 15% 
 16% The Initial Developer of the Original Code is S.G. Consulting
 17% srl. Portions created by S.G. Consulting s.r.l. are Copyright (C)
 18% 2007 S.G. Consulting srl. All Rights Reserved.
 19% 
 20% Contributor(s): Filippo Pacini <filippo.pacini@gmail.com>
 21%                 Hunter Morris <huntermorris@gmail.com>
 22
 23-define(DEFAULT_CHUNKSIZE, 4096).
 24
 25-type ewgi_propval() :: atom() | integer() | string() | binary().
 26-type ewgi_prop() :: {ewgi_propval(), ewgi_propval()}.
 27-type ewgi_proplist() :: [ewgi_prop()].
 28
 29%% @type bag() = gb_tree()
 30-ifdef(HAS_GB_TREE_SPEC).
 31-type bag() :: gb_tree().
 32-else.
 33-type bag() :: {non_neg_integer(), {any(), any(), any(), any()} | 'nil'}.
 34-endif.
 35
 36%%% Note: Dialyzer currently doesn't support recursive types. When it does, this should change:
 37%%%-type ewgi_ri_callback() :: fun(('eof' | {data, binary()}) -> iolist() | ewgi_ri_callback()).
 38%% @type ewgi_ri_callback() = function()
 39-type ewgi_ri_callback() :: fun(('eof' | {data, binary()}) -> iolist() | function()) | iolist().
 40
 41%% @type ewgi_read_input() = function()
 42-type ewgi_read_input() :: fun((ewgi_ri_callback(), integer()) -> ewgi_ri_callback()).
 43
 44%% @type ewgi_write_error() = function()
 45-type ewgi_write_error() :: fun((any()) -> 'ok').
 46
 47%% @type ewgi_version() = {integer(), integer()}
 48-type ewgi_version() :: {integer(), integer()}.
 49
 50%% @type ewgi_spec() = {'ewgi_spec', function(), function(), string(),
 51%%                      ewgi_version(), bag()}
 52
 53-type ewgi_spec() :: {'ewgi_spec', ewgi_read_input(),
 54                      ewgi_write_error(), string(), ewgi_version(),
 55                      bag()}.
 56
 57-define(IS_EWGI_SPEC(R), ((element(1, R) =:= 'ewgi_spec')
 58                          and (size(R) =:= 6))).
 59-define(GET_EWGI_READ_INPUT(R), element(2, R)).
 60-define(SET_EWGI_READ_INPUT(A, R), setelement(2, R, A)).
 61-define(GET_EWGI_WRITE_ERROR(R), element(3, R)).
 62-define(SET_EWGI_WRITE_ERROR(A, R), setelement(3, R, A)).
 63-define(GET_EWGI_URL_SCHEME(R), element(4, R)).
 64-define(SET_EWGI_URL_SCHEME(A, R), setelement(4, R, A)).
 65-define(GET_EWGI_VERSION(R), element(5, R)).
 66-define(SET_EWGI_VERSION(A, R), setelement(5, R, A)).
 67-define(GET_EWGI_DATA(R), element(6, R)).
 68-define(SET_EWGI_DATA(A, R), setelement(6, R, A)).
 69
 70%% @type ewgi_header_val() = string() | 'undefined'
 71-type ewgi_header_val() :: string() | 'undefined'.
 72
 73%% @type ewgi_header_key() = string()
 74-type ewgi_header_key() :: string().
 75
 76%% @type ewgi_http_headers() = {'ewgi_http_headers',
 77%%                              ewgi_header_val(),
 78%%                              ewgi_header_val(),
 79%%                              ewgi_header_val(),
 80%%                              ewgi_header_val(),
 81%%                              ewgi_header_val(),
 82%%                              ewgi_header_val(),
 83%%                              bag()}
 84
 85-type ewgi_http_headers() :: {'ewgi_http_headers', ewgi_header_val(),
 86                              ewgi_header_val(), ewgi_header_val(),
 87                              ewgi_header_val(), ewgi_header_val(),
 88                              ewgi_header_val(), bag()}.
 89
 90-define(IS_HTTP_HEADERS(R), ((element(1, R) =:= 'ewgi_http_headers')
 91                             and (size(R) =:= 8))).
 92-define(GET_HTTP_ACCEPT(R), element(2, R)).
 93-define(SET_HTTP_ACCEPT(A, R), setelement(2, R, A)).
 94-define(GET_HTTP_COOKIE(R), element(3, R)).
 95-define(SET_HTTP_COOKIE(A, R), setelement(3, R, A)).
 96-define(GET_HTTP_HOST(R), element(4, R)).
 97-define(SET_HTTP_HOST(A, R), setelement(4, R, A)).
 98-define(GET_HTTP_IF_MODIFIED_SINCE(R), element(5, R)).
 99-define(SET_HTTP_IF_MODIFIED_SINCE(A, R), setelement(5, R, A)).
100-define(GET_HTTP_USER_AGENT(R), element(6, R)).
101-define(SET_HTTP_USER_AGENT(A, R), setelement(6, R, A)).
102-define(GET_HTTP_X_HTTP_METHOD_OVERRIDE(R), element(7, R)).
103-define(SET_HTTP_X_HTTP_METHOD_OVERRIDE(A, R), setelement(7, R, A)).
104-define(GET_HTTP_OTHER(R), element(8, R)).
105-define(SET_HTTP_OTHER(A, R), setelement(8, R, A)).
106
107%% @type ewgi_request_method() = 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' |
108%%       'DELETE' | 'TRACE' | 'CONNECT' | string()
109-type ewgi_request_method() :: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' |
110                               'DELETE' | 'TRACE' | 'CONNECT' | string().
111
112%% @type ewgi_val() = string() | 'undefined'
113-type ewgi_val() :: string() | 'undefined'.
114
115%% @type ewgi_request() :: {'ewgi_request', ewgi_val(), integer(), ewgi_val(),
116%%                          ewgi_spec(), ewgi_val(), ewgi_http_headers(),
117%%                          ewgi_val(), ewgi_val(), ewgi_val(), ewgi_val(),
118%%                          ewgi_val(), ewgi_val(), ewgi_val(), ewgi_val(),
119%%                          ewgi_request_method(), ewgi_val(), ewgi_val(),
120%%                          ewgi_val(), ewgi_val(), ewgi_val()}
121
122-type ewgi_request() :: {'ewgi_request', ewgi_val(),
123                         non_neg_integer(), ewgi_val(), ewgi_spec(),
124                         ewgi_val(), ewgi_http_headers(), ewgi_val(),
125                         ewgi_val(), ewgi_val(), ewgi_val(),
126                         ewgi_val(), ewgi_val(), ewgi_val(),
127                         ewgi_val(), ewgi_request_method(),
128                         ewgi_val(), ewgi_val(), ewgi_val(),
129                         ewgi_val(), ewgi_val()}.
130
131-define(IS_EWGI_REQUEST(R), ((element(1, R) =:= 'ewgi_request')
132                             and (size(R) =:= 21))).
133-define(GET_AUTH_TYPE(R), element(2, R)).
134-define(SET_AUTH_TYPE(A, R), setelement(2, R, A)).
135-define(GET_CONTENT_LENGTH(R), element(3, R)).
136-define(SET_CONTENT_LENGTH(A, R), setelement(3, R, A)).
137-define(GET_CONTENT_TYPE(R), element(4, R)).
138-define(SET_CONTENT_TYPE(A, R), setelement(4, R, A)).
139-define(GET_EWGI(R), element(5, R)).
140-define(SET_EWGI(A, R), setelement(5, R, A)).
141-define(GET_GATEWAY_INTERFACE(R), element(6, R)).
142-define(SET_GATEWAY_INTERFACE(A, R), setelement(6, R, A)).
143-define(GET_HTTP_HEADERS(R), element(7, R)).
144-define(SET_HTTP_HEADERS(A, R), setelement(7, R, A)).
145-define(GET_PATH_INFO(R), element(8, R)).
146-define(SET_PATH_INFO(A, R), setelement(8, R, A)).
147-define(GET_PATH_TRANSLATED(R), element(9, R)).
148-define(SET_PATH_TRANSLATED(A, R), setelement(9, R, A)).
149-define(GET_QUERY_STRING(R), element(10, R)).
150-define(SET_QUERY_STRING(A, R), setelement(10, R, A)).
151-define(GET_REMOTE_ADDR(R), element(11, R)).
152-define(SET_REMOTE_ADDR(A, R), setelement(11, R, A)).
153-define(GET_REMOTE_HOST(R), element(12, R)).
154-define(SET_REMOTE_HOST(A, R), setelement(12, R, A)).
155-define(GET_REMOTE_IDENT(R), element(13, R)).
156-define(SET_REMOTE_IDENT(A, R), setelement(13, R, A)).
157-define(GET_REMOTE_USER(R), element(14, R)).
158-define(SET_REMOTE_USER(A, R), setelement(14, R, A)).
159-define(GET_REMOTE_USER_DATA(R), element(15, R)).
160-define(SET_REMOTE_USER_DATA(A, R), setelement(15, R, A)).
161-define(GET_REQUEST_METHOD(R), element(16, R)).
162-define(SET_REQUEST_METHOD(A, R), setelement(16, R, A)).
163-define(GET_SCRIPT_NAME(R), element(17, R)).
164-define(SET_SCRIPT_NAME(A, R), setelement(17, R, A)).
165-define(GET_SERVER_NAME(R), element(18, R)).
166-define(SET_SERVER_NAME(A, R), setelement(18, R, A)).
167-define(GET_SERVER_PORT(R), element(19, R)).
168-define(SET_SERVER_PORT(A, R), setelement(19, R, A)).
169-define(GET_SERVER_PROTOCOL(R), element(20, R)).
170-define(SET_SERVER_PROTOCOL(A, R), setelement(20, R, A)).
171-define(GET_SERVER_SOFTWARE(R), element(21, R)).
172-define(SET_SERVER_SOFTWARE(A, R), setelement(21, R, A)).
173
174%%% Note: Dialyzer currently doesn't support recursive types. When it does, this should change:
175%%%-type stream() :: fun(() -> {} | {any(), stream()}).
176%% @type stream() = function()
177-type stream() :: fun(() -> {} | {any(), function()}).
178
179%% @type ewgi_status() = {integer(), string()}
180-type ewgi_status() :: {integer(), string()}.
181
182%% @type ewgi_message_body() = binary() | iolist() | stream()
183-type ewgi_message_body() :: binary() | iolist() | stream().
184
185%% @type ewgi_header_list() = [{ewgi_header_key(), ewgi_header_val()}]
186-type ewgi_header_list() :: [{ewgi_header_key(), ewgi_header_val()}].
187
188%% @type ewgi_response() = {'ewgi_response', ewgi_status(),
189%%                          [{ewgi_header_key(), ewgi_header_val()}],
190%%                           ewgi_message_body(), any()}
191
192-type ewgi_response() :: {'ewgi_response', ewgi_status(), ewgi_header_list(), ewgi_message_body(), any()}.
193
194-define(IS_EWGI_RESPONSE(R), ((element(1, R) =:= 'ewgi_response')
195                              and (size(R) =:= 5))).
196-define(GET_RESPONSE_STATUS(R), element(2, R)).
197-define(SET_RESPONSE_STATUS(A, R), setelement(2, R, A)).
198-define(GET_RESPONSE_HEADERS(R), element(3, R)).
199-define(SET_RESPONSE_HEADERS(A, R), setelement(3, R, A)).
200-define(GET_RESPONSE_MESSAGE_BODY(R), element(4, R)).
201-define(SET_RESPONSE_MESSAGE_BODY(A, R), setelement(4, R, A)).
202-define(GET_RESPONSE_ERROR(R), element(5, R)).
203-define(SET_RESPONSE_ERROR(A, R), setelement(5, R, A)).
204
205%% @type ewgi_context() = {'ewgi_context', ewgi_request(), ewgi_response()}
206
207-type ewgi_context() :: {'ewgi_context', ewgi_request(), ewgi_response()}.
208
209-define(IS_EWGI_CONTEXT(R), ((element(1, R) =:= 'ewgi_context')
210                             and ?IS_EWGI_REQUEST(element(2, R))
211                             and ?IS_EWGI_RESPONSE(element(3, R))
212                             and (size(R) =:= 3))).
213-define(GET_EWGI_REQUEST(R), element(2, R)).
214-define(SET_EWGI_REQUEST(A, R), setelement(2, R, A)).
215-define(GET_EWGI_RESPONSE(R), element(3, R)).
216-define(SET_EWGI_RESPONSE(A, R), setelement(3, R, A)).
217
218%% @type ewgi_app() = function()
219-type ewgi_app() :: fun((ewgi_context()) -> ewgi_context()).
220
221-ifndef(debug).
222-define(INSPECT_EWGI_RESPONSE(Ctx), Ctx).
223-else.
224-define(INSPECT_EWGI_RESPONSE(Ctx),
225	begin
226	    error_logger:info_msg("Inpecting the final ewgi_response()...~n"
227				  "Requested Url: ~p~n"
228				  "Status: ~p~n"
229				  "Headers: ~p~n"
230				  "Body: ~p~n",
231				  [ewgi_api:path_info(Ctx),
232				   ewgi_api:response_status(Ctx),
233				   ewgi_api:response_headers(Ctx),
234				   ewgi_api:response_message_body(Ctx)]),
235	    Ctx
236	end
237       ).
238-endif.
239
240-endif.