File: staticmod.ml

package info (click to toggle)
ocsigen 1.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,560 kB
  • sloc: ml: 35,873; makefile: 1,450; sh: 772; ansic: 29
file content (259 lines) | stat: -rw-r--r-- 9,673 bytes parent folder | download | duplicates (2)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
(* Ocsigen
 * http://www.ocsigen.org
 * Module staticmod.ml
 * Copyright (C) 2005 Vincent Balat
 * Laboratoire PPS - CNRS Universit Paris Diderot
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, with linking exception;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *)
(*****************************************************************************)
(*****************************************************************************)
(* Ocsigen module to load static pages                                       *)
(*****************************************************************************)
(*****************************************************************************)

open Lwt
open Ocsigen_lib
open Ocsigen_extensions

exception Not_concerned

let bad_config s = raise (Error_in_config_file s)


(*****************************************************************************)
(* Structures describing the static pages a each virtual server *)

(* A static site is either an entire directory served unconditionnaly,
   or a more elaborate redirection based on regexpes and http error
   codes. See the web documentation of staticmod for detail *)
type static_site_kind =
  | Dir of string (* Serves an entire directory *)
  | Regexp of regexp_site
and regexp_site = {
  source_regexp: Netstring_pcre.regexp;
  dest: Ocsigen_extensions.ud_string;
  http_status_filter: Netstring_pcre.regexp option;
  root_checks: Ocsigen_extensions.ud_string option;
}



(*****************************************************************************)
(* Finding files *)

(* Does the http status code returned for the page match the given filter ? *)
let http_status_match status_filter status =
  match status_filter with
  | None -> true
  | Some r ->
      Netstring_pcre.string_match r (string_of_int status) 0 <> None


(* Checks that the path specified in a userconf is correct.
   Currently, we check that the path does not contain ".." *)
let correct_user_local_file =
  let regexp = Netstring_pcre.regexp "(/\\.\\./)|(/\\.\\.$)" in
  fun path ->
    try ignore(Netstring_pcre.search_forward regexp path 0); false
    with Not_found -> true


(* Find the local file corresponding to [path] in the static site [dir],
   with [err] as the current http status (in case [dir] is a filter).
   Raises [Not_Concerned] if [dir] does not match, or returns
   - a boolean indicating that [dir] is an error handler
   - the local file
   If the parameter [usermode] is true, we check that the path
   is valid.
*)
let find_static_page ~request ~usermode ~dir ~err ~pathstring =
  let status_filter, filename, root = match dir with
    | Dir d ->
        (false,
         Filename.concat d pathstring,
         (match usermode with
            | None -> Some d
            | Some { localfiles_root = r } -> Some r
         ))
    | Regexp { source_regexp = source; dest = dest;
               http_status_filter = status_filter;
               root_checks = rc }
          when http_status_match status_filter err ->
        let status_filter = status_filter <> None
        and file =
          match Netstring_pcre.string_match source pathstring 0 with
            | None -> raise Not_concerned
            | Some _ ->
                Ocsigen_extensions.replace_user_dir source dest pathstring
        and root_checks =
         (match rc, usermode with
            | None, Some { localfiles_root = r } ->
                Some r
            | Some _, Some _ ->
                raise (Ocsigen_extensions.Error_in_user_config_file
                         "Staticmod: cannot specify option 'root' in \
                           user configuration files")
            | None, None -> None
            | Some rc, None ->
                Some (Ocsigen_extensions.replace_user_dir source rc pathstring)
         )
        in
        status_filter, file, root_checks
    | _ -> raise Not_concerned
  in
  if usermode = None || correct_user_local_file filename then
    (status_filter,
     Ocsigen_LocalFiles.resolve ?no_check_for:root ~request ~filename)
  else
    raise (Ocsigen_extensions.Error_in_user_config_file
             "Staticmod: cannot use '..' in user paths")



let gen ~usermode dir = function
  | Ocsigen_extensions.Req_found (_, r) ->
      Lwt.return (Ocsigen_extensions.Ext_do_nothing)
  | Ocsigen_extensions.Req_not_found (err, ri) ->
      catch
        (fun () ->
           Ocsigen_messages.debug2 "--Staticmod: Is it a static file?";
           let status_filter, page =
             find_static_page ~request:ri ~usermode ~dir ~err
             ~pathstring:(Ocsigen_lib.string_of_url_path ~encode:false
                            ri.request_info.ri_sub_path) in
           Ocsigen_LocalFiles.content ri page
           >>= fun answer ->
           let answer' =
             if status_filter = false then
               answer
             else
               (* The page is an error handler, we propagate
                  the original error code *)
               {answer with Ocsigen_http_frame.res_code = err }
           in Lwt.return (Ext_found (fun () -> Lwt.return answer'))
        )

        (function
           | Ocsigen_LocalFiles.Failed_403 -> return (Ext_next 403)
               (* XXX We should try to leave an information about this
                  error for later *)
           | Ocsigen_LocalFiles.NotReadableDirectory ->
               return (Ext_next err)
           | NoSuchUser | Not_concerned
           | Ocsigen_LocalFiles.Failed_404 -> return (Ext_next err)
           | e -> fail e
        )


(*****************************************************************************)
(** Parsing of config file *)
open Simplexmlparser

(* In userconf modes, paths must be relative to the root of the userconf config *)
let rewrite_local_path userconf path =
  match userconf with
    | None -> path
    | Some { Ocsigen_extensions.localfiles_root = root } ->
        root ^ "/" ^ path

type options = {
  opt_dir: string option;
  opt_regexp: Netstring_pcre.regexp option;
  opt_code: Netstring_pcre.regexp option;
  opt_dest: Ocsigen_extensions.ud_string option;
  opt_root_checks: Ocsigen_extensions.ud_string option;
}

let parse_config userconf : parse_config_aux = fun _ _ _ ->
  let rec parse_attrs l opt =
    match l with
      | [] -> opt

      | ("dir", d)::l when opt.opt_dir = None ->
          parse_attrs l
            { opt with opt_dir = Some (rewrite_local_path userconf d)}

      | ("regexp", s)::l when opt.opt_regexp = None ->
          let s = try Netstring_pcre.regexp ("^"^s^"$")
           with Pcre.Error (Pcre.BadPattern _) ->
             bad_config ("Bad regexp \""^s^"\" in <static regexp=\"...\" />")
          in
          parse_attrs l { opt with opt_regexp = Some s }

      | ("code", c)::l when opt.opt_code = None ->
          let c = try Netstring_pcre.regexp ("^"^c^"$")
           with Pcre.Error (Pcre.BadPattern _) ->
             bad_config ("Bad regexp \""^c^"\" in <static code=\"...\" />")
          in
          parse_attrs l { opt with opt_code = Some c }

      | ("dest", s)::l when opt.opt_dest = None ->
          parse_attrs l
            { opt with opt_dest =
                Some (parse_user_dir (rewrite_local_path userconf s)) }

      | ("root", s) :: l when opt.opt_root_checks = None ->
          parse_attrs l
            { opt with opt_root_checks = Some (parse_user_dir s) }

      | _ -> bad_config "Wrong attribute for <static>"
  in
  function
    | Element ("static", atts, []) ->
        let opt =
          parse_attrs atts {
            opt_dir = None;
            opt_regexp = None;
            opt_code = None;
            opt_dest = None;
            opt_root_checks = None;
          }
        in
        let kind =
          match opt.opt_dir, opt.opt_regexp, opt.opt_code, opt.opt_dest, opt.opt_root_checks with
          | (None, None, None, _, _) ->
              raise (Error_in_config_file
                       "Missing attribute dir, regexp, or code for <static>")

          | (Some d, None, None, None, None) ->
              Dir (remove_end_slash d)

          | (None, Some r, code, Some t, rc) ->
              Regexp { source_regexp = r;
                       dest = t;
                       http_status_filter = code;
                       root_checks = rc;
                     }

          | (None, None, (Some _ as code), Some t, None) ->
              Regexp { dest = t; http_status_filter = code; root_checks = None;
                       source_regexp = Netstring_pcre.regexp "^.*$" }

          | _ -> raise (Error_in_config_file "Wrong attributes for <static>")
        in
        gen ~usermode:userconf kind
    | Element (t, _, _) -> raise (Bad_config_tag_for_extension t)
    | _ -> bad_config "(staticmod extension) Bad data"



(*****************************************************************************)
(** extension registration *)
let () = register_extension
  ~name:"staticmod"
  ~fun_site:(fun _ -> parse_config None)
  ~user_fun_site:(fun path _ -> parse_config (Some path))
  ()