diff options
Diffstat (limited to 'src/serv/form.cc')
| -rw-r--r-- | src/serv/form.cc | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/serv/form.cc b/src/serv/form.cc new file mode 100644 index 0000000..815f9c4 --- /dev/null +++ b/src/serv/form.cc | |||
| @@ -0,0 +1,102 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <serv/htcd.h> | ||
| 21 | |||
| 22 | const mstring tag="T."; | ||
| 23 | const mstring ncookie_tag="ncookie"; | ||
| 24 | const mstring multipart_tag="multipart"; | ||
| 25 | const mstring body_tag="T.body"; | ||
| 26 | |||
| 27 | static mstring Tag(const sref& in) | ||
| 28 | { | ||
| 29 | mstring name=in; | ||
| 30 | for(int i=0;i<name.size();i++) if(name[i]=='_') name[i]='.'; | ||
| 31 | if(tag.empty()||tag==name.left(tag.size())) return name; | ||
| 32 | name.insert(0,tag); | ||
| 33 | return name; | ||
| 34 | } | ||
| 35 | |||
| 36 | static void AddConst(tokmap& map,const sref& s,const sref& b) throw(merror_t) | ||
| 37 | { | ||
| 38 | tok_t* p=map.get_this(s); | ||
| 39 | if(p) { | ||
| 40 | mstring nb=p->body; nb.append(1,' '); nb+=b; | ||
| 41 | Const(map,s,nb,tok_t::PUBLIC,1); | ||
| 42 | } | ||
| 43 | else Const(map,s,b,tok_t::PUBLIC); | ||
| 44 | } | ||
| 45 | |||
| 46 | static void PushMultipart(tokmap& map,const Mime& root) throw(merror_t) | ||
| 47 | { | ||
| 48 | rpile part; | ||
| 49 | if(root.Multipart(part)) { | ||
| 50 | for(int i=0;i<part.size();i++) { | ||
| 51 | Mime msg(part[i]); | ||
| 52 | if(msg.cont_disp_name.nempty()) { | ||
| 53 | mstring name=Tag(msg.cont_disp_name); | ||
| 54 | if(msg.cont_disp_file.nempty()) { | ||
| 55 | reference_t* tok=Reference(map,name,msg.cont_disp_file,tok_t::PUBLIC); | ||
| 56 | swrite via; | ||
| 57 | if(eqn(msg.cont_trans_enc,trans_base64)) | ||
| 58 | decode64(via(tok->data),msg.body); | ||
| 59 | else if(eqn(msg.cont_trans_enc,trans_quote)) | ||
| 60 | decodeQP(via(tok->data),msg.body); | ||
| 61 | else | ||
| 62 | via(tok->data)<<msg.body; | ||
| 63 | } | ||
| 64 | else AddConst(map,name,msg.body); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | static void PushWWWForm(tokmap& map,const sref& in) throw(merror_t) | ||
| 71 | { | ||
| 72 | sspile tmp; | ||
| 73 | Unpack(tmp,in); | ||
| 74 | for(int i=0;i<tmp.size();i++) | ||
| 75 | AddConst(map,Tag(tmp[i].first),tmp[i].second); | ||
| 76 | } | ||
| 77 | |||
| 78 | void SetCookies(tokmap& map,const httpReq& req) | ||
| 79 | { | ||
| 80 | Const(map,ncookie_tag,itoa(req.cookies.size()),tok_t::PUBLIC); | ||
| 81 | int n=0; | ||
| 82 | for(sspile::const_iterator p=req.cookies.begin();p!=req.cookies.end();p++) { | ||
| 83 | mstring name,val; | ||
| 84 | name.convert("cookie%d.name",n); | ||
| 85 | val.convert("cookie%d.val",n); | ||
| 86 | Const(map,name,p->first,tok_t::PUBLIC); | ||
| 87 | Const(map,val,p->second,tok_t::PUBLIC); | ||
| 88 | n++; | ||
| 89 | } | ||
| 90 | if(req.arg.nempty()) PushWWWForm(map,req.arg); | ||
| 91 | } | ||
| 92 | |||
| 93 | void envAddForm(tokmap& map,const httpReq& req) | ||
| 94 | { | ||
| 95 | if(req.method==HTTP_POST) { | ||
| 96 | if(req.cont_type.find(multipart_tag,cset_lcase)>=0) PushMultipart(map,req); | ||
| 97 | else PushWWWForm(map,req.body); | ||
| 98 | } | ||
| 99 | else if(req.method==HTTP_PUT) { | ||
| 100 | Const(map,body_tag,req.body,tok_t::PUBLIC); | ||
| 101 | } | ||
| 102 | } | ||
