From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/mime/mime.cc | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 src/mime/mime.cc (limited to 'src/mime/mime.cc') diff --git a/src/mime/mime.cc b/src/mime/mime.cc new file mode 100644 index 0000000..a7a1884 --- /dev/null +++ b/src/mime/mime.cc @@ -0,0 +1,188 @@ +/************************************************************************* + * + * HTCd - Copyright (C) 1998-2006 Henrik Rydberg + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include + +const charset par_left("([{"),par_right("}])"); + +inline int is_field(const sref& s) { + sref p=s.left_first(':'); + while(cset_spaces.test(p.back())) p=p.popb(); + return p.nempty()&&p.find_of(cset_spaces)<0; +} + +inline int getquoted(const sref& s,char c) { + int i,quote=0,par=0; + for(i=0;ifirst,charset_tag)) cont_type_charset=p->second; + else if(eqn(p->first,name_tag)) cont_type_name=p->second; + else if(eqn(p->first,boundary_tag)) cont_type_boundary=p->second; + } + } + else if(eqn(name,cont_id_tag)) cont_id=val; + else if(eqn(name,cont_desc_tag)) cont_desc=val; + else if(eqn(name,cont_disp_tag)) { + cont_disp=val; + for(rrpile::const_iterator p=opts.begin();p!=opts.end();p++) { + if(eqn(p->first,name_tag)) cont_disp_name=p->second; + else if(eqn(p->first,file_tag)) cont_disp_file=p->second; + } + } + else if(eqn(name,cont_trans_enc_tag)) cont_trans_enc=val; + else if(eqn(name,cont_length_tag)) cont_length=val; + else return 0; + return 1; +} + +/////////////////////////////////////////////////////////////////////////// + +void Mime::putHead(mstream& out) const +{ + if(putval(out,mime_version_tag,mime_version)) putend(out); + if(putval(out,cont_type_tag,cont_type)) { + putopt(out,charset_tag,cont_type_charset); + putopt(out,name_tag,cont_type_name); + putopt(out,boundary_tag,cont_type_boundary); + putend(out); + } + if(putval(out,cont_id_tag,cont_id)) putend(out); + if(putval(out,cont_desc_tag,cont_desc)) putend(out); + if(putval(out,cont_disp_tag,cont_disp)) { + putopt(out,name_tag,cont_disp_name); + putopt(out,file_tag,cont_disp_file); + putend(out); + } + if(putval(out,cont_trans_enc_tag,cont_trans_enc)) putend(out); + if(putval(out,cont_length_tag,cont_length)) putend(out); +} + + +/////////////////////////////////////////////////////////////////////////// + +int Mime::Multipart(rpile& list) const +{ + if(cont_type_boundary.empty()) return 0; + mstring bound=sref("--")+cont_type_boundary; + sref rest=body.past_first(bound).past_nl(); + while(rest.nempty()) { + int pos=rest.find(bound); + if(pos>=0) { + sref first=rest.left(pos); + if(first.back()=='\n') { + first=first.popb(); + if(first.back()=='\r') first=first.popb(); + } + list.push_back(first); + rest=rest.right(pos).adv(bound.size()); + if(*rest=='-') rest.clear(); else rest=rest.past_nl(); + } + else { + list.push_back(rest); + rest.clear(); + } + } + return !list.empty(); +} + +void Mime::Pick(const sref& mime) +{ + rpile list; + if(Multipart(list)) { + if(eqn(cont_type,mime_multialt)) { + int pos=0; + for(int i=0;i