123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143(* 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 *)(* 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... *)letsection=Lwt_log.Section.make"ocsigen:ext:rewritemod"exceptionNot_concerned(* The table of rewrites for each virtual server *)typeassockind=RegexpofPcre.regexp*string*boolletfind_rewrite(Regexp(regexp,dest,fullrewrite))suburl=(matchOcsigen_lib.Netstring_pcre.string_matchregexpsuburl0with|None->raiseNot_concerned|Some_->(* Matching regexp found! *)Ocsigen_lib.Netstring_pcre.global_replaceregexpdestsuburl),fullrewrite(* The function that will generate the pages from the request *)letgenregexpcontinue=function|Ocsigen_extensions.Req_found_->Lwt.returnOcsigen_extensions.Ext_do_nothing|Ocsigen_extensions.Req_not_found(err,ri)->lettry_block()=Lwt_log.ign_info~section"Is it a rewrite?";letredir,full_rewrite=letri=ri.Ocsigen_extensions.request_infoinfind_rewriteregexp(matchOcsigen_request.queryriwith|None->Ocsigen_request.sub_path_stringri|Someg->Ocsigen_request.sub_path_stringri^"?"^g)inLwt_log.ign_info_f~section"YES! rewrite to: %s"redir;ifcontinuethenLwt.return@@Ocsigen_extensions.Ext_continue_with({riwithOcsigen_extensions.request_info=Ocsigen_request.update~full_rewrite~uri:(Uri.of_stringredir)ri.Ocsigen_extensions.request_info},Ocsigen_cookie_map.empty,err)elseLwt.return@@Ocsigen_extensions.Ext_retry_with({riwithOcsigen_extensions.request_info=Ocsigen_request.update~full_rewrite~uri:(Uri.of_stringredir)ri.Ocsigen_extensions.request_info},Ocsigen_cookie_map.empty)andcatch_block=function|Ocsigen_extensions.Not_concerned->Lwt.return(Ocsigen_extensions.Ext_nexterr)|e->Lwt.faileinLwt.catchtry_blockcatch_blockletparse_configelement=letregexp=ref""inletdest=refNoneinletfullrewrite=reffalseinletcontinue=reffalseinOcsigen_extensions.(Configuration.process_element~in_tag:"host"~other_elements:(funt__->raise(Bad_config_tag_for_extensiont))~elements:[Configuration.element~name:"rewrite"~attributes:[Configuration.attribute~name:"regexp"~obligatory:true(funs->regexp:=s);Configuration.attribute~name:"url"(funs->dest:=Somes);Configuration.attribute~name:"dest"(funs->dest:=Somes);Configuration.attribute~name:"fullrewrite"(funs->fullrewrite:=(s="fullrewrite"||s="true"));Configuration.attribute~name:"continue"(funs->continue:=(s="continue"||s="true"));]()]element);match!destwith|None->raise(Ocsigen_extensions.Error_in_config_file"url attribute expected for <rewrite>")|Somedest->gen(Regexp((Ocsigen_lib.Netstring_pcre.regexp("^"^!regexp^"$")),dest,!fullrewrite))!continue(** Registration of the extension *)let()=Ocsigen_extensions.register~name:"rewritemod"~fun_site:(fun______->parse_config)()