123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120(* Ocsigen
* http://www.ocsigen.org
* Module outputfilter.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.
*)(* This module enables rewritting the server output *)letgenfilter=function|Ocsigen_extensions.Req_not_found(code,_)->Lwt.return(Ocsigen_extensions.Ext_nextcode)|Ocsigen_extensions.Req_found(_ri,res)->Lwt.return@@Ocsigen_extensions.Ext_found(fun()->Lwt.return@@matchfilterwith|`Rewrite(header,regexp,dest)->(tryletl=List.map(Ocsigen_lib.Netstring_pcre.global_replaceregexpdest)(Ocsigen_response.header_multiresheader)anda=Ocsigen_response.remove_headerresheaderinOcsigen_response.add_header_multiaheaderlwithNot_found->res)|`Add(header,dest,replace)->(matchreplacewith|None->(matchOcsigen_response.headerresheaderwith|Some_->res|None->Ocsigen_response.add_headerresheaderdest)|Somefalse->Ocsigen_response.add_headerresheaderdest|Sometrue->Ocsigen_response.replace_headerresheaderdest))letgen_codecode=function|Ocsigen_extensions.Req_not_found(code,_)->Lwt.return(Ocsigen_extensions.Ext_nextcode)|Ocsigen_extensions.Req_found(_ri,res)->Lwt.return@@Ocsigen_extensions.Ext_found(fun()->Lwt.return(Ocsigen_response.set_statusrescode))letparse_configconfig_elem=letheader=refNoneinletregexp=refNoneinletdest=refNoneinletreplace=refNoneinletcode=refNoneinOcsigen_extensions.(Configuration.process_element~in_tag:"host"~other_elements:(funt__->raise(Bad_config_tag_for_extensiont))~elements:[Configuration.element~name:"outputfilter"~attributes:[Configuration.attribute~name:"header"(funs->header:=Somes);Configuration.attribute~name:"regexp"(funs->regexp:=Some(Ocsigen_lib.Netstring_pcre.regexps));Configuration.attribute~name:"dest"(funs->dest:=Somes);Configuration.attribute~name:"replace"(funs->tryreplace:=Some(bool_of_strings)withInvalid_argument_->badconfig"Wrong value for attribute replace of <outputfilter/>: %s. It should be true or false"s)]();Configuration.element~name:"sethttpcode"~attributes:[Configuration.attribute~name:"code"(funs->trymatchCohttp.Code.status_of_code(int_of_strings)with|#Cohttp.Code.statusasstatus->code:=Somestatus|`Code_->failwith"Invalid code"withFailure_->badconfig"Invalid code attribute in <sethttpcode>")]()]config_elem);match!codewith|None->(match!header,!regexp,!dest,!replacewith|_,Some_,_,Some_->Ocsigen_extensions.badconfig"Wrong attributes for <outputfilter/>: attributes regexp and replace can't be set simultaneously"|Someh,Somer,Somed,None->gen(`Rewrite(Ocsigen_header.Name.of_stringh,r,d))|Someh,None,Somed,rep->gen(`Add(Ocsigen_header.Name.of_stringh,d,rep))|_->Ocsigen_extensions.badconfig"Wrong attributes for <outputfilter header=... dest=... (regexp=... / replace=...)/>")|Somecode->gen_codecodelet()=Ocsigen_extensions.register~name:"outputfilter"~fun_site:(fun______->parse_config)()letrun~mode()___=matchmodewith|`Codec->gen_codec|`Rewrite(header,regexp,dest)->gen(`Rewrite(header,Re.Pcre.regexp("^"^regexp^"$"),dest))|`Addf->gen(`Addf)