12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697(* Ocsigen
* http://www.ocsigen.org
* Module redirectmod.ml
* Copyright (C) 2007 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.
*)(* Define page redirections in the configuration file *)modulePcre=Re.Pcreletsection=Lwt_log.Section.make"ocsigen:ext:redirectmod"(* The table of redirections for each virtual server *)typeredirection={r_regexp:Pcre.regexp;r_dest:string;r_full:bool;r_temp:bool}letcreate_redirection?(full_url=true)?(temporary=false)~regexpr_dest=letr_regexp=Pcre.regexp("^"^regexp^"$")in{r_regexp;r_dest;r_full=full_url;r_temp=temporary}letattempt_redir{r_regexp;r_dest;r_full;r_temp}_errri()=Lwt_log.ign_info~section"Is it a redirection?";letredir=Ocsigen_extensions.find_redirectionr_regexpr_fullr_destriinLwt_log.ign_info_f~section"YES! %s redirection to: %s"(ifr_tempthen"Temporary "else"Permanent ")redir;Lwt.return@@Ocsigen_extensions.Ext_found(fun()->Lwt.return@@Ocsigen_response.make@@letheaders=Cohttp.Header.(init_with"Location"redir)andstatus=ifr_tempthen`Foundelse`Moved_permanentlyinCohttp.Response.make~status~headers())(** The function that will generate the pages from the request *)letgendir=function|Ocsigen_extensions.Req_found_->Lwt.returnOcsigen_extensions.Ext_do_nothing|Ocsigen_extensions.Req_not_found(err,{Ocsigen_extensions.request_info;_})->(Lwt.catch(attempt_redirdirerrrequest_info)@@function|Ocsigen_extensions.Not_concerned->Lwt.return(Ocsigen_extensions.Ext_nexterr)|e->Lwt.faile)letparse_configconfig_elem=letregexp=refNoneanddest=ref""andmode=reftrueandtemporary=reffalseinOcsigen_extensions.(Configuration.process_element~in_tag:"host"~other_elements:(funt__->raise(Bad_config_tag_for_extensiont))~elements:[Configuration.element~name:"redirect"~attributes:[Configuration.attribute~name:"fullurl"(funs->regexp:=Somes;mode:=true);Configuration.attribute~name:"suburl"(funs->regexp:=Somes;mode:=false);Configuration.attribute~name:"dest"~obligatory:true(funs->dest:=s);Configuration.attribute~name:"temporary"(function|"temporary"->temporary:=true|_->())]()]config_elem);match!regexpwith|None->Ocsigen_extensions.badconfig"Missing attribute regexp for <redirect>"|Someregexp->gen(create_redirection~full_url:!mode~regexp~temporary:!temporary!dest)let()=Ocsigen_extensions.register~name:"redirectmod"~fun_site:(fun______->parse_config)()letrun~redirection()___=genredirection