123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143(* 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 *)letsection=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:[`Yes|`No|`Maybe];r_temp:bool}letcreate_redirection?(full=`Yes)?(temporary=false)~regexpr_dest=letr_regexp=Pcre.regexp("^"^regexp^"$")in{r_regexp;r_dest;r_full=full;r_temp=temporary}letattempt_redir{r_regexp;r_dest;r_full;r_temp}_errri()=Lwt_log.ign_info~section"Is it a redirection?";letredir=letfindfull=Ocsigen_extensions.find_redirectionr_regexpfullr_destriinmatchr_fullwith|`Yes->findtrue|`No->findfalse|`Maybe->tryfindfalsewithOcsigen_extensions.Not_concerned->findtrueinLwt_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.faileletparse_configconfig_elem=letregexp=refNoneanddest=ref""andmode=ref`Yesandtemporary=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:"regexp"(funs->regexp:=Some("^"^s^"$");mode:=`Maybe);Configuration.attribute~name:"fullurl"(funs->regexp:=Somes;mode:=`Yes);Configuration.attribute~name:"suburl"(funs->regexp:=Somes;mode:=`No);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:!mode~regexp~temporary:!temporary!dest)let()=Ocsigen_extensions.register~name:"redirectmod"~fun_site:(fun______->parse_config)()letredirection=Ocsigen_server.Site.Config.key()letextension=Ocsigen_server.Site.create_extension(fun{Ocsigen_server.Site.Config.accessor}->matchaccessorredirectionwith|Someredirection->genredirection|None->failwith"Redirectmod.redirection not set")