~ubuntu-branches/ubuntu/oneiric/ocsigen/oneiric

« back to all changes in this revision

Viewing changes to extensions/redirectmod.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stephane Glondu
  • Date: 2009-07-02 10:02:08 UTC
  • mfrom: (1.1.9 upstream) (4.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090702100208-n158b1sqwzn0asil
Tags: 1.2.0-2
Fix build on non-native architectures

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
open Simplexmlparser
39
39
 
40
40
 
41
 
exception Not_concerned
42
 
 
43
41
 
44
42
(*****************************************************************************)
45
43
(* The table of redirections for each virtual server                         *)
46
44
type assockind =
47
 
  | Regexp of Netstring_pcre.regexp * string * bool (* temporary *)
48
 
 
49
 
 
50
 
 
51
 
 
52
 
(*****************************************************************************)
53
 
let rec parse_global_config = function
54
 
  | [] -> ()
55
 
  | _ -> raise (Error_in_config_file
56
 
                  ("Unexpected content inside redirectmod config"))
57
 
 
58
 
let _ = parse_global_config (Ocsigen_extensions.get_config ())
59
 
 
60
 
 
61
 
 
62
 
 
63
 
 
64
 
(*****************************************************************************)
65
 
(* Finding redirections *)
66
 
 
67
 
let find_redirection (Regexp (regexp, dest, temp)) path =
68
 
  match Netstring_pcre.string_match regexp path 0 with
69
 
  | None -> raise Not_concerned
70
 
  | Some _ -> (* Matching regexp found! *)
71
 
      (Netstring_pcre.global_replace regexp dest path, temp)
72
 
 
73
 
 
74
 
 
75
 
 
76
 
 
77
 
 
78
 
 
79
 
(*****************************************************************************)
80
 
(** Function to be called at the beginning of the initialisation phase
81
 
    of the server (actually each time the config file is reloaded) *)
82
 
let start_init () =
83
 
  ()
84
 
 
85
 
(** Function to be called at the end of the initialisation phase *)
86
 
let end_init () =
87
 
  ()
 
45
  | Regexp of Netstring_pcre.regexp * string
 
46
      * Ocsigen_lib.yesnomaybe (* full url *) 
 
47
      * bool (* temporary *)
88
48
 
89
49
 
90
50
 
91
51
(*****************************************************************************)
92
52
(** The function that will generate the pages from the request. *)
93
 
let gen dir charset = function
94
 
| Ocsigen_extensions.Req_found (_, r) -> Lwt.return (Ocsigen_extensions.Ext_found r)
95
 
| Ocsigen_extensions.Req_not_found (err, ri) ->
96
 
  catch
97
 
    (* Is it a redirection? *)
98
 
    (fun () ->
99
 
      Ocsigen_messages.debug2 "--Redirectmod: Is it a redirection?";
100
 
      let (redir, temp) =
101
 
        find_redirection dir
102
 
          (match ri.ri_get_params_string with
103
 
          | None -> ri.ri_sub_path_string
104
 
          | Some g -> ri.ri_sub_path_string ^ "?" ^ g)
105
 
      in
106
 
      Ocsigen_messages.debug (fun () ->
107
 
        "--Redirectmod: YES! "^
108
 
        (if temp then "Temporary " else "Permanent ")^
109
 
        "redirection to: "^redir);
110
 
      let empty_result = Ocsigen_http_frame.empty_result () in
111
 
      return
112
 
        (Ext_found
113
 
           (fun () ->
114
 
              Lwt.return
115
 
                {empty_result with
116
 
                   Ocsigen_http_frame.res_location = Some redir;
117
 
                   Ocsigen_http_frame.res_code= if temp then 302 else 301}))
118
 
    )
119
 
    (function
120
 
      | Not_concerned -> return (Ext_next err)
121
 
      | e -> fail e)
 
53
let gen dir = function
 
54
  | Ocsigen_extensions.Req_found _ -> 
 
55
      Lwt.return Ocsigen_extensions.Ext_do_nothing
 
56
  | Ocsigen_extensions.Req_not_found (err, ri) ->
 
57
      catch
 
58
        (* Is it a redirection? *)
 
59
        (fun () ->
 
60
           Ocsigen_messages.debug2 "--Redirectmod: Is it a redirection?";
 
61
           let Regexp (regexp, dest, full, temp) = dir in
 
62
           let redir =
 
63
             let fi full =
 
64
               Ocsigen_extensions.find_redirection
 
65
                 regexp
 
66
                 full
 
67
                 dest
 
68
                 ri.request_info.ri_ssl
 
69
                 ri.request_info.ri_host
 
70
                 ri.request_info.ri_server_port
 
71
                 ri.request_info.ri_get_params_string
 
72
                 ri.request_info.ri_sub_path_string
 
73
                 ri.request_info.ri_full_path_string
 
74
             in
 
75
             match full with
 
76
               | Ocsigen_lib.Yes -> fi true
 
77
               | Ocsigen_lib.No -> fi false
 
78
               | Ocsigen_lib.Maybe -> 
 
79
                   try fi false 
 
80
                   with Ocsigen_extensions.Not_concerned -> fi true
 
81
           in
 
82
           Ocsigen_messages.debug
 
83
             (fun () ->
 
84
                "--Redirectmod: YES! "^
 
85
                  (if temp then "Temporary " else "Permanent ")^
 
86
                  "redirection to: "^redir);
 
87
           let empty_result = Ocsigen_http_frame.empty_result () in
 
88
           return
 
89
             (Ext_found
 
90
                (fun () ->
 
91
                   Lwt.return
 
92
                     {empty_result with
 
93
                        Ocsigen_http_frame.res_location = Some redir;
 
94
                        Ocsigen_http_frame.res_code= 
 
95
                         if temp then 302 else 301}))
 
96
        )
 
97
        (function
 
98
           | Ocsigen_extensions.Not_concerned -> return (Ext_next err)
 
99
           | e -> fail e)
122
100
 
123
101
 
124
102
 
125
103
 
126
104
(*****************************************************************************)
127
 
(** Configuration for each site.
128
 
    These tags are inside <site ...>...</site> in the config file.
129
 
 
130
 
   For example:
131
 
   <site dir="">
132
 
     <redirect regexp="" dest="" />
133
 
   </extension>
134
 
 
135
 
 *)
136
 
 
137
 
let parse_config path charset _ parse_site = function
 
105
 
 
106
let parse_config = function
138
107
  | Element ("redirect", atts, []) ->
139
 
      let dir = match atts with
140
 
      | [] ->
141
 
          raise (Error_in_config_file
142
 
                   "regexp attribute expected for <redirect>")
143
 
      | [("regexp", s);("dest",t)] ->
144
 
          Regexp ((Netstring_pcre.regexp ("^"^s^"$")), t, false)
145
 
      | [("temporary", "temporary");("regexp", s);("dest",t)] ->
146
 
          Regexp ((Netstring_pcre.regexp ("^"^s^"$")), t, true)
147
 
      | _ -> raise (Error_in_config_file "Wrong attribute for <redirect>")
148
 
      in
149
 
      gen dir charset
 
108
      let rec parse_attrs ((r, f, d, temp) as res) = function
 
109
        | [] -> res
 
110
        | ("regexp", regexp)::l when r = None -> (* deprecated *)
 
111
            parse_attrs
 
112
              (Some (Netstring_pcre.regexp ("^"^regexp^"$")), Ocsigen_lib.Maybe,
 
113
               d, temp)
 
114
              l
 
115
        | ("fullurl", regexp)::l when r = None ->
 
116
            parse_attrs
 
117
              (Some (Netstring_pcre.regexp ("^"^regexp^"$")), Ocsigen_lib.Yes,
 
118
               d, temp)
 
119
              l
 
120
        | ("suburl", regexp)::l when r = None ->
 
121
            parse_attrs
 
122
              (Some (Netstring_pcre.regexp ("^"^regexp^"$")), Ocsigen_lib.No,
 
123
               d, temp)
 
124
              l
 
125
        | ("dest", dest)::l when d = None ->
 
126
            parse_attrs
 
127
              (r, f, Some dest, temp)
 
128
              l
 
129
        | ("temporary", "temporary")::l ->
 
130
            parse_attrs
 
131
              (r, f, d, false)
 
132
              l
 
133
        | _ -> raise (Error_in_config_file "Wrong attribute for <redirect>")
 
134
        in
 
135
        let dir =
 
136
          match parse_attrs (None, Ocsigen_lib.Yes, None, true) atts with
 
137
          | (None, _, _, _) -> 
 
138
              raise (Error_in_config_file
 
139
                       "Missing attribute regexp for <redirect>")
 
140
          | (_, _, None, _) -> 
 
141
              raise (Error_in_config_file
 
142
                       "Missing attribute dest for <redirect>>")
 
143
          | (Some r, full, Some d, temp) ->
 
144
              Regexp (r, d, full, temp)
 
145
        in
 
146
        gen dir
 
147
  | Element ("redirect" as s, _, _) -> badconfig "Bad syntax for tag %s" s
 
148
 
150
149
  | Element (t, _, _) ->
151
150
      raise (Bad_config_tag_for_extension t)
152
151
  | _ -> raise (Error_in_config_file "(redirectmod extension) Bad data")
154
153
 
155
154
 
156
155
 
157
 
 
158
 
(*****************************************************************************)
159
 
(** A function that will be called for each virtual host,
160
 
   generating two functions:
161
 
    - one that will be called to generate the pages
162
 
    - one to parse the configuration file. *)
163
 
let virtual_host_creator hostpattern = (gen, parse_config)
164
 
   (* hostpattern has type Ocsigen_extensions.virtual_hosts
165
 
      and represents the name of the virtual host *)
166
 
 
167
 
 
168
156
(*****************************************************************************)
169
157
(** Registration of the extension *)
170
 
let _ = register_extension (* takes a quadruple *)
171
 
    (fun hostpattern -> parse_config)
172
 
    (fun hostpattern -> parse_config)
173
 
    start_init
174
 
    end_init
175
 
    raise
176
 
 
 
158
let () = register_extension
 
159
  ~name:"redirectmod"
 
160
  ~fun_site:(fun _ _ _ _ -> parse_config)
 
161
  ~user_fun_site:(fun _ _ _ _ _ -> parse_config)
 
162
  ()