summaryrefslogtreecommitdiff
path: root/src/couch_inets/http_response.erl
blob: 27e807eb0290d2dea48139f69d152c27315d9724 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
%% ``The contents of this file are subject to the Erlang Public License,
%% Version 1.1, (the "License"); you may not use this file except in
%% compliance with the License. You should have received a copy of the
%% Erlang Public License along with this software. If not, it can be
%% retrieved via the world wide web at http://www.erlang.org/.
%% 
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and limitations
%% under the License.
%% 
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%% 
%%     $Id$

-module(http_response).

-include("http_internal.hrl").

-export([headers/2, header_list/1]).

%%-------------------------------------------------------------------------
%% headers(HeaderList, #http_response_h{}) -> #http_response_h{}
%%   HeaderList - ["HeaderField:Value"]     	
%%   HeaderField - string()
%%   Value - string()	
%%                                   
%% Description: Creates a http_response_h-record used internally to
%%              handle http-headers.
%%-------------------------------------------------------------------------
headers([], Headers) ->
    Headers;

headers([Header | Tail], Headers) ->  
    {Key, [$: | Value]} =
	lists:splitwith(fun($:) -> false; (_) -> true end, Header), 
    headers(Tail, headers(http_util:to_lower(string:strip(Key)), 
			  string:strip(Value), Headers)).

%%-------------------------------------------------------------------------
%% headers(#http_response_h{}) -> HeaderList
%%   HeaderList - [{"HeaderField", Value"}]     	
%%   HeaderField - string()
%%   Value - string()	
%%                                   
%% Description: Creates a list of key value tuples from the #http_response_h  
%%              record, to be returned to the application programmer. We
%%              do not wish to make the application programmer dependent on
%%              our records.
%%-------------------------------------------------------------------------
header_list(Headers) ->
    HeaderFields = record_info(fields, http_response_h) -- [other],
    HeaderList = lists:foldl(fun(Key, Acc) -> 
				     case key_value_tuple(Key, Headers) of
					 undefined ->
					     Acc;
					 Tuple ->
					     [Tuple | Acc]
				     end
			     end,
			     [], HeaderFields),
    lists:reverse(HeaderList) ++ Headers#http_response_h.other.
%%%========================================================================
%%% Internal functions
%%%========================================================================
headers("cache-control", Value, Headers) ->
    Headers#http_response_h{'cache-control'= Value};
headers("connection", Value, Headers) ->
    Headers#http_response_h{connection = Value};
headers("date", Value, Headers) ->
    Headers#http_response_h{date = Value};
headers("pragma", Value, Headers) ->
    Headers#http_response_h{pragma = Value};
headers("trailer", Value, Headers) ->
    Headers#http_response_h{trailer = Value};
headers("transfer-encoding", Value, Headers) ->
    Headers#http_response_h{'transfer-encoding' = Value};
headers("upgrade", Value, Headers) ->
    Headers#http_response_h{upgrade = Value};
headers("via", Value, Headers) ->
    Headers#http_response_h{via = Value};
headers("warning", Value, Headers) ->
    Headers#http_response_h{warning = Value};
headers("accept-ranges", Value, Headers) ->
    Headers#http_response_h{'accept-ranges' = Value};
headers("age", Value, Headers) ->
    Headers#http_response_h{age = Value};
headers("etag", Value, Headers) ->
    Headers#http_response_h{etag = Value};
headers("location", Value, Headers) ->
    Headers#http_response_h{location = Value};
headers("proxy-authenticate", Value, Headers) ->
    Headers#http_response_h{'proxy-authenticate' = Value};
headers("retry-after", Value, Headers) ->
    Headers#http_response_h{'retry-after' = Value};
headers("server", Value, Headers) ->
    Headers#http_response_h{server = Value};
headers("vary", Value, Headers) ->
    Headers#http_response_h{vary = Value};
headers("www-authenticate", Value, Headers) ->
    Headers#http_response_h{'www-authenticate' = Value};
headers("allow", Value, Headers) ->
    Headers#http_response_h{allow = Value};
headers("content-encoding", Value, Headers) ->
    Headers#http_response_h{'content-encoding' = Value};
headers("content-language", Value, Headers) ->
    Headers#http_response_h{'content-language' = Value};
headers("content-length", Value, Headers) ->
    Headers#http_response_h{'content-length' = Value};
headers("content-location", Value, Headers) ->
    Headers#http_response_h{'content-location' = Value};
headers("content-md5", Value, Headers) ->
    Headers#http_response_h{'content-md5' = Value};
headers("content-range", Value, Headers) ->
    Headers#http_response_h{'content-range' = Value};
headers("content-type", Value, Headers) ->
    Headers#http_response_h{'content-type' = Value};
headers("expires", Value, Headers) ->
    Headers#http_response_h{expires = Value};
headers("last-modified", Value, Headers) ->
    Headers#http_response_h{'last-modified' = Value};
headers(Key, Value, Headers) ->
    Headers#http_response_h{other=
			    [{Key, Value} | Headers#http_response_h.other]}.


key_value_tuple(Key = 'cache-control', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'cache-control');
key_value_tuple(Key = connection, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.connection);
key_value_tuple(Key = date, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.date);
key_value_tuple(Key = pragma, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.pragma);
key_value_tuple(Key = trailer, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.trailer);
key_value_tuple(Key ='transfer-encoding', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'transfer-encoding');
key_value_tuple(Key = upgrade, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.upgrade);
key_value_tuple(Key = via, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.via);
key_value_tuple(Key = warning, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.warning);
key_value_tuple(Key = 'accept-ranges', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'accept-ranges');
key_value_tuple(Key = age, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.age);
key_value_tuple(Key = etag, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.etag);
key_value_tuple(Key = location, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.location);
key_value_tuple(Key = 'proxy-authenticate', Headers) ->
    key_value_tuple(atom_to_list(Key),
		    Headers#http_response_h.'proxy-authenticate');
key_value_tuple(Key = 'retry-after', Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.'retry-after');
key_value_tuple(Key = server, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.server);
key_value_tuple(Key = vary, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.vary);
key_value_tuple(Key = 'www-authenticate', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'www-authenticate');
key_value_tuple(Key = allow, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.allow);
key_value_tuple(Key = 'content-encoding', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'content-encoding');
key_value_tuple(Key = 'content-language', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'content-language');
key_value_tuple(Key = 'content-length', Headers) ->
    case Headers#http_response_h.'content-length' of
	"-1" ->
	    undefined;
	_ -> 
	    key_value_tuple(atom_to_list(Key), 
			    Headers#http_response_h.'content-length')
    end;
key_value_tuple(Key = 'content-location', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'content-location');
key_value_tuple(Key = 'content-md5', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'content-md5');
key_value_tuple(Key = 'content-range', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'content-range');
key_value_tuple(Key = 'content-type', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'content-type');
key_value_tuple(Key = expires, Headers) ->
    key_value_tuple(atom_to_list(Key), Headers#http_response_h.expires);
key_value_tuple(Key = 'last-modified', Headers) ->
    key_value_tuple(atom_to_list(Key), 
		    Headers#http_response_h.'last-modified');
key_value_tuple(_, undefined) ->
    undefined;
key_value_tuple(Key, Value) ->
    {Key, Value}.