Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 163 lines (132 sloc) 5.714 kB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18 (** Accept:
19
20 Module for handling HTTP request Accept* header values.
21
22 Method:
23 1) We provide a regexp for splitting the first value,
24 eg. '/' for Accept: text/html, '-' for Accept-Language: en-GB.
25 2) We call the parser routine to break up the input string (make)
26 into a (currently abstract) internal type.
27 3) Various routines are provided to handle the "q" values, either
28 by ranking them according to decreasing "q" and accessing them
29 sequentially (q_rank, q_nth) or by providing a list of values
30 (generated by make_el) and calling q_preferred to return the
31 preferred value.
32 4) Routines are provided to extract the values from request headers
33 and to replace existing values in request headers with new ones.
34 5) Currently, the values are managed as string lists, so for example,
35 "text/html;level=1;q=0.5" is handled as (["text";"html"],[["level";"1"]],Some 0.5).
36 Remember you can convert back and forward with make_el and string_of_el.
37
38 *)
39 open HttpServerTypes
40
41 (** Concrete type of elements in the list of values.
42 Does for the internal representation and for user-generated values.
43 For example:
44 "text/html;level=1;q=0.5"
45 is represented as:
46 (["text"; "html"], [["level"; "1"]], Some 0.5)
47 The quality value is ignored in user-generated instances of this type.
48 *)
49 type el = string list * string list list * float option
50
51 (** Abstract type for parsed headers.
52 Stores enough information to be able to reconstruct the
53 original string from internal information, including the comment.
54 *)
55 type t (*= string option * char option * el list*)
56
57 (** Some precompiled regexp values for use with the input parser.
58 regexp3 constructs a fresh regexp from a given character.
59 regexp2sl is a precompiled regexp for '/' (ie. Accept: headers).
60 regexp2mi is a precompiled regexp for '-' (ie. Accept-Language: headers).
61 For other header types, None leaves the value intact.
62 *)
63 val regexp3 : char option -> (char * Str.regexp) option
64 val regexp3sl : (char * Str.regexp) option
65 val regexp3mi : (char * Str.regexp) option
66
67 (** Build an element from a string.
68 Can use the precompiled regexps above.
69 See "type el" above.
70 *)
71 val make_el : (char * Str.regexp) option -> string -> el
72
73 (** Build an element from a supplied character.
74 Note that the regexp has to be compiled.
75 *)
76 val make_el_ch : char option -> string -> el
77
78 (** The main input parser routine.
79 Takes the regexp for the first element character ('/' for Accept:,
80 '-' for Accept-Language:).
81 Returns an abstract type with the internal representation.
82 *)
83 val make : (char * Str.regexp) option -> string -> t
84
85 (** Simple accessor function, the number of elements that have been parsed.
86 *)
87 val size : t -> int
88
89 (** Return the quality value.
90 No value returns 1.0.
91 If the parser found an error (eg. a malformed quality value) then
92 this will be fixed at 0.0.
93 *)
94 val q_value : el -> float
95
96 (** Sort the internal list of element in decreasing quality value.
97 *)
98 val q_rank : t -> t
99
100 (** Return the n'th element from the parsed list.
101 Returns None if n is out of range.
102 *)
103 val q_nth : t -> int -> el option
104
105 (** Given an element return the element in the parsed
106 data which has the highest quality value.
107 *)
108 val q_max : t -> el -> float * el option
109
110 (** Given a list of elements acceptable to the server, search the
111 values provided by the client for the best matching quality.
112 Note that if the best matching quality is 0.0 then None is returned.
113 According to the RFC, the response should be 406 Unnacceptable in this
114 circumstance.
115 *)
116 val q_preferred : t -> el list -> el option
117
118 (** Return a string representation of an element.
119 *)
120 val string_of_el : string -> el -> string
121
122 (** Reconstruct a parsed string.
123 Spacing may be different and the quality value may be rounded
124 differently from the original.
125 Otherwise, this string is suitable for embedding in Accept: headers.
126 *)
127 val to_string : t -> string
128
129 (** Same as above but returns empty string for None. *)
130 val to_string_opt : t option -> string
131
132 (** Find the Accept: header in a request_header and parse the string.
133 *)
134 val get_accept : request -> t option
135
136 (** Set the Accept: header line in a request.
137 *)
138 val set_accept : request -> t option -> request
139
140 (** Find the Accept-Charset: header in a request_header and parse the string.
141 *)
142 val get_accept_charset : request -> t option
143
144 (** Set the Accept-Charset: header line in a request.
145 *)
146 val set_accept_charset : request -> t option -> request
147
148 (** Find the Accept-Encoding: header in a request_header and parse the string.
149 *)
150 val get_accept_encoding : request -> t option
151
152 (** Set the Accept-Encoding: header line in a request.
153 *)
154 val set_accept_encoding : request -> t option -> request
155
156 (** Find the Accept-Language: header in a request_header and parse the string.
157 *)
158 val get_accept_language : request -> t option
159
160 (** Set the Accept-Language: header line in a request.
161 *)
162 val set_accept_language : request -> t option -> request
Something went wrong with that request. Please try again.