summaryrefslogtreecommitdiff
path: root/src/serv/main.cc
blob: f02f10c8afe3f4a1731b3f1beb059c2b38bb9479 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*************************************************************************
 *
 * 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>
#include <hdb/buffer.h>

const mstring RCURI="/sys/htcd.rc";
const mstring TREEURI="/sys/sitemap.hdb";
const mstring TEMPURI="/sys/temp.hdb";

const mstring LOGURI="/sys/htcd.log";
const mstring ERRURI="/sys/htcd.err";

///////////////////////////////////////////////////

static void OpenTREE(dbmap& db,const sref& path)
{
  mstring TREEMAP=path+TREEURI;
  db.Open(TREEMAP,1);
  if(db.Tables()==0) for(int i=0;i<tree_tabs;i++) db.InsertTable(i,tree_tab[i]);
}

static void OpenTEMP(dbmap& db,const sref& path)
{
  mstring TEMPMAP=path+TEMPURI;
  db.Open(TEMPMAP,1);
  if(db.Tables()==0) for(int i=0;i<temp_tabs;i++) db.InsertTable(i,temp_tab[i]);
}

htcd_user::htcd_user(tokmap& p,const mstring& path) : htc_user(p)
{
  OpenTREE(DBRef(toks,TREE_tag,ms_empty,tok_t::SYSONLY)->db,path);
  OpenTEMP(DBRef(toks,TEMP_tag,ms_empty,tok_t::SYSONLY)->db,path);
}

///////////////////////////////////////////////////

void SetCookies(tokmap& map,const httpReq& req);
void envAddForm(tokmap& map,const httpReq& req);

int htcd_req::RootAllowed()
{
  if(htcd()->bindroot.empty()) return 1;
  sref first,rest=htcd()->bindroot;
  while(Split(first,rest)) if(first==tcp->node.name) return 1;
  return 0;
}

htcd_req::htcd_req(HTCd& g,tokmap& p,mstream& out,
                   tcpReq& trec,httpReq& hrec) : htc_req(g,p)
{
  output=&out;
  tcp=&trec;
  req=&hrec;
  SetCookies(toks,hrec);
}

void htcd_req::AddBody(const sref& body)
{
  req->body=body;
  envAddForm(toks,*req);
}

///////////////////////////////////////////////////

void envAddDirect(tokmap& map);
void envAddReq(tokmap& map);
void envAddConst(tokmap& map);
void envAddAuth(tokmap& map);
void envAddTemp(tokmap& map);

HTCd::HTCd(const mstring& path):
  logger(fopen((path+LOGURI).c_str(),"a+")),
  errlog(fopen((path+ERRURI).c_str(),"a+")),
  HTC(path)
{
  envAddDirect(toks);
  envAddReq(toks);
  envAddConst(toks);
  envAddAuth(toks);
  envAddTemp(toks);
  
  memset(&mutex,0,sizeof(mutex));
  errlog<<"["<<time(0)<<"][MAIN][HTCd started]\n";
  errlog.flush();
}

HTCd::~HTCd()
{
  fclose(errlog.fp);
  fclose(logger.fp);
}

void Setup(tokmap& user,HTCd& htcd)
{
  tcpReq tcp; tcp.port=0;
  httpReq hreq; hreq.method="GET";
  mstream m1;
  htcd_req req(htcd,htcd.toks,m1,tcp,hreq);
  Env env(htcd.ops,req,XSYSTEM);
  mstring buffer;
  LoadBuffer(buffer,htcd.docroot+RCURI);
  Env e(env,user,htcd.docroot+RCURI);
  mstream m2;
  e.parse(m2,buffer);
}