package ocsigenserver

  1. Overview
  2. Docs
A full-featured and extensible Web server

Install

dune-project
 Dependency

Authors

Maintainers

Sources

8.0.0.tar.gz
md5=be7a28e3a79cc028d35c88eca538ee8b
sha512=08cdaab31864aeba743ad0c03077a6eccf1089e7b7c4078045dab9409d409144b70cc546df7881b64b780689eee642ca2391be84ab2a6a8ffee0fbed2ad5ac98

doc/src/rewritemod/rewritemod.ml.html

Source file rewritemod.ml

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
(* Ocsigen
 * http://www.ocsigen.org
 * Module rewritemod.ml
 * Copyright (C) 2008 Vincent Balat
 *
 * 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.
 *)

(* Rewrite URLs in the configuration file *)

module Pcre = Re.Pcre

(* IMPORTANT WARNING

   It is really basic for now:
    - rewrites only subpaths (and doees not change get parameters)
    - changes only ri_sub_path and ri_sub_path_string
   not ri_full_path, nor ri_full_path_string, nor ri_url_string, nor ri_url

   This is probably NOT what we want... *)

let section = Logs.Src.create "ocsigen:ext:rewritemod"

exception Not_concerned

(* The table of rewrites for each virtual server *)
type assockind = Regexp of Pcre.regexp * string * bool

let find_rewrite (Regexp (regexp, dest, fullrewrite)) suburl =
  ( (match Ocsigen_base.Lib.Netstring_pcre.string_match regexp suburl 0 with
    | None -> raise Not_concerned
    | Some _ ->
        (* Matching regexp found! *)
        Ocsigen_base.Lib.Netstring_pcre.global_replace regexp dest suburl)
  , fullrewrite )

(* The function that will generate the pages from the request *)
let gen regexp continue = function
  | Ocsigen.Extensions.Req_found _ ->
      Lwt.return Ocsigen.Extensions.Ext_do_nothing
  | Ocsigen.Extensions.Req_not_found (err, ri) ->
      let try_block () =
        Logs.info ~src:section (fun fmt -> fmt "Is it a rewrite?");
        let redir, full_rewrite =
          let ri = ri.Ocsigen.Extensions.request_info in
          find_rewrite regexp
            (match Ocsigen.Request.query ri with
            | None -> Ocsigen.Request.sub_path_string ri
            | Some g -> Ocsigen.Request.sub_path_string ri ^ "?" ^ g)
        in
        Logs.info ~src:section (fun fmt -> fmt "YES! rewrite to: %s" redir);
        if continue
        then
          Lwt.return
          @@ Ocsigen.Extensions.Ext_continue_with
               ( { ri with
                   Ocsigen.Extensions.request_info =
                     Ocsigen.Request.update ~full_rewrite
                       ~uri:(Uri.of_string redir)
                       ri.Ocsigen.Extensions.request_info }
               , Ocsigen_cookie_map.empty
               , err )
        else
          Lwt.return
          @@ Ocsigen.Extensions.Ext_retry_with
               ( { ri with
                   Ocsigen.Extensions.request_info =
                     Ocsigen.Request.update ~full_rewrite
                       ~uri:(Uri.of_string redir)
                       ri.Ocsigen.Extensions.request_info }
               , Ocsigen_cookie_map.empty )
      and catch_block = function
        | Ocsigen.Extensions.Not_concerned ->
            Lwt.return (Ocsigen.Extensions.Ext_next err)
        | e -> Lwt.fail e
      in
      Lwt.catch try_block catch_block

let parse_config element =
  let regexp = ref "" in
  let dest = ref None in
  let fullrewrite = ref false in
  let continue = ref false in
  Ocsigen.Extensions.(
    Configuration.process_element ~in_tag:"host"
      ~other_elements:(fun t _ _ -> raise (Bad_config_tag_for_extension t))
      ~elements:
        [ Configuration.element ~name:"rewrite"
            ~attributes:
              [ Configuration.attribute ~name:"regexp" ~obligatory:true (fun s ->
                  regexp := s)
              ; Configuration.attribute ~name:"url" (fun s -> dest := Some s)
              ; Configuration.attribute ~name:"dest" (fun s -> dest := Some s)
              ; Configuration.attribute ~name:"fullrewrite" (fun s ->
                  fullrewrite := s = "fullrewrite" || s = "true")
              ; Configuration.attribute ~name:"continue" (fun s ->
                  continue := s = "continue" || s = "true") ]
            () ]
      element);
  match !dest with
  | None ->
      raise
        (Ocsigen.Extensions.Error_in_config_file
           "url attribute expected for <rewrite>")
  | Some dest ->
      gen
        (Regexp
           ( Ocsigen_base.Lib.Netstring_pcre.regexp ("^" ^ !regexp ^ "$")
           , dest
           , !fullrewrite ))
        !continue

(** Registration of the extension *)
let () =
  Ocsigen.Extensions.register ~name:"rewritemod"
    ~fun_site:(fun _ _ _ _ _ _ -> parse_config)
    ()

let run ?(continue = false) ?(full_rewrite = false) ~regexp dest () _ _ _ =
  gen
    (Regexp
       ( Ocsigen_base.Lib.Netstring_pcre.regexp ("^" ^ regexp ^ "$")
       , dest
       , full_rewrite ))
    continue