summaryrefslogtreecommitdiff
path: root/src/serv/form.cc
blob: 815f9c4ee471d41016c9c71ed4503bbd04e238ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*************************************************************************
 *
 * 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 <serv/htcd.h>

const mstring tag="T.";
const mstring ncookie_tag="ncookie";
const mstring multipart_tag="multipart";
const mstring body_tag="T.body";

static mstring Tag(const sref& in)
{
  mstring name=in;
  for(int i=0;i<name.size();i++) if(name[i]=='_') name[i]='.';
  if(tag.empty()||tag==name.left(tag.size())) return name;
  name.insert(0,tag);
  return name;
}

static void AddConst(tokmap& map,const sref& s,const sref& b) throw(merror_t)
{
  tok_t* p=map.get_this(s);
  if(p) {
    mstring nb=p->body; nb.append(1,' '); nb+=b;
    Const(map,s,nb,tok_t::PUBLIC,1);
  }
  else Const(map,s,b,tok_t::PUBLIC);
}

static void PushMultipart(tokmap& map,const Mime& root) throw(merror_t)
{
  rpile part;
  if(root.Multipart(part)) {
    for(int i=0;i<part.size();i++) {
      Mime msg(part[i]);
      if(msg.cont_disp_name.nempty()) {
        mstring name=Tag(msg.cont_disp_name);
        if(msg.cont_disp_file.nempty()) {
          reference_t* tok=Reference(map,name,msg.cont_disp_file,tok_t::PUBLIC);
          swrite via;
          if(eqn(msg.cont_trans_enc,trans_base64))
            decode64(via(tok->data),msg.body);
          else if(eqn(msg.cont_trans_enc,trans_quote))
            decodeQP(via(tok->data),msg.body);
          else
            via(tok->data)<<msg.body;
        }
        else AddConst(map,name,msg.body);
      }
    }
  }
}

static void PushWWWForm(tokmap& map,const sref& in) throw(merror_t)
{
  sspile tmp;
  Unpack(tmp,in);
  for(int i=0;i<tmp.size();i++)
    AddConst(map,Tag(tmp[i].first),tmp[i].second);
}

void SetCookies(tokmap& map,const httpReq& req)
{
  Const(map,ncookie_tag,itoa(req.cookies.size()),tok_t::PUBLIC);
  int n=0;
  for(sspile::const_iterator p=req.cookies.begin();p!=req.cookies.end();p++) {
    mstring name,val;
    name.convert("cookie%d.name",n);
    val.convert("cookie%d.val",n);
    Const(map,name,p->first,tok_t::PUBLIC);
    Const(map,val,p->second,tok_t::PUBLIC);
    n++;
  }
  if(req.arg.nempty()) PushWWWForm(map,req.arg);
}

void envAddForm(tokmap& map,const httpReq& req)
{
  if(req.method==HTTP_POST) {
    if(req.cont_type.find(multipart_tag,cset_lcase)>=0) PushMultipart(map,req);
    else PushWWWForm(map,req.body);
  }
  else if(req.method==HTTP_PUT) {
    Const(map,body_tag,req.body,tok_t::PUBLIC);
  }
}