summaryrefslogtreecommitdiff
path: root/src/net/thrserv.cc
blob: 0d91d12b1ecb6f097b3dae5fb44243a807f76789 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*************************************************************************
 *
 * 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 <net/thrserv.h>
#include <errno.h>

const mstring cron_tag="\\run.cron";
const mstring FATAL="FATAL ERROR";
const mstring UNKNOWN="UNKNOWN";
const mstring REJECTED="REJECTED";

const int MAXHEAD=16384;
const int MAXBODY=2<<21;
const int MAXURI=4096;
const int MAXARG=4096;
const int SAFEZONE=4096;

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

thrServ::thrServ(htcServ& server):
  pmutex(MUTEX_INIT),
  pcond(MTCOND_INIT),
  keepalive(0),running(0),termok(0),
  first(time(0)),last(time(0)),
  serv(&server),
  sock(0),
  user(server.user,server.htcd.docroot)
{
}

thrServ::~thrServ()
{
  delete sock;
}

void thrServ::reset(isocket* s,int keep)
{
  keepalive=keep;
  running=1;
  termok=0;
  first=time(0);
  last=time(0);
  sock=s;
  while(!tid);
  if(MCONTINUE(pcond)) throw((int)0);
}

void thrServ::Log(const sref& msg)
{
  MLOCK(serv->htcd.mutex);
  if(sock) serv->htcd.errlog<<"["<<time(0)<<"]["<<tid<<"]["
                            <<inet_ntoa(sock->address())<<":"<<sock->port()<<"]["
                            <<msg<<"]\n";  
  else serv->htcd.errlog<<"["<<time(0)<<"]["<<tid<<"]["<<msg<<"]\n";  
  serv->htcd.errlog.flush();
}

void thrServ::enter()
{
  tid=pthread_self();
  MSUSPEND(pmutex,pcond)
  while(serv->htcd.running&&!termok) {
    session();
    delete sock;
    sock=0;
    if(serv->htcd.running&&!termok) {
      MSUSPEND(pmutex,pcond)
    }
  }
}

void thrServ::session() try
{
  int keep=1;
  while(sock&&keep&&running) {
    int test=sock->test();
    if(test>0) { last=time(0); keep=handle(); }
    else if(test<0) keep=0;
  }
}
catch(const merror_t& e) {
  Log(e.desc);
}
catch(...) {
  Log(FATAL);
}

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

thrCron::thrCron(htcServ& server):
  tid(0),running(0),serv(&server),user(server.user,server.htcd.docroot)
{
}

void thrCron::Log(const sref& msg)
{
  MLOCK(serv->htcd.mutex);
  serv->htcd.errlog<<"["<<time(0)<<"][CRON]["<<msg<<"]\n";  
  serv->htcd.errlog.flush();
}

void thrCron::enter() try
{
  tid=pthread_self();
  tcpReq tcp; tcp.port=0;
  httpReq req;
  mstream devnull;
  htcd_req R(serv->htcd,user.toks,devnull,tcp,req);
  Env env(serv->htcd.ops,R,XSYSTEM);
  env.req->running=&running;
  running=1;
  mstream m;
  env.parse(m,cron_tag);
}
catch(const merror_t& e) {
  Log(e.desc);
}
catch(...) {
  Log(FATAL);
}


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

int thrServ::handle()
{
  tcpReq tcp;
  tcp.port=sock->port();
  if(!Lookup(tcp.node,sock->address())) {
    Log(REJECTED);
    return 0;
  }    

  //////////////////////////////////////////////////////////////////
  
  mstring buffer(MAXHEAD+MAXBODY+SAFEZONE);
  int nhead=sock->gethead(buffer.data(),MAXHEAD);
  if(nhead==0) return 0;
  else if(nhead<0) return 0;

  sref message(buffer.data(),nhead);

#if 0
  static fp_stream merr(stderr);
  merr<<"QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ\n";
  merr<<message<<"\n";
  merr<<"=====================================\n";
#endif

  httpReq req(message);
  
  //////////////////////////////////////////////////////////////////

  int keep=keepalive;
  if(!eqn(req.connect,keep_tag)) keep=0;

  mswrite cache;
  htcd_req R(serv->htcd,user.toks,cache,tcp,req);
  
  //////////////////////////////////////////////////////////////////

  if(nhead>=MAXHEAD) return SendStatus(*sock,R,keep,HTTP_413);
  
  if(eqn(req.method,HTTP_TRACE)) {
    R.resp.cont_type="message/http";
    return SendStatus(*sock,R,keep,HTTP_200,buffer.left(nhead));
  }

  if(req.expect.nempty()) {
    if(eqn(req.expect,expect_continue_tag)) SendStatus(*sock,R,keep,HTTP_100);
    else return SendStatus(*sock,R,keep,HTTP_417);
  }

  if(req.trans_enc.nempty()||req.cont_enc.nempty()) return SendStatus(*sock,R,0,HTTP_501);

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

  int ilength=atoi(req.cont_length);
  if(ilength>MAXBODY) return SendStatus(*sock,R,0,HTTP_413);
  else if(ilength>0) {
    int nbody=sock->getbody(buffer.data()+nhead,ilength);
    if(nbody<0) return 0;
    else {
      sref sbody(buffer.data()+nhead,nbody);
#if 0
      static fp_stream merr(stderr);
      merr<<"PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP\n";
      merr<<sbody<<"\n";
      merr<<"=====================================\n";
#endif
      R.AddBody(sbody);
      if(req.body.size()!=ilength) return 0;
    }
  }
  
  //////////////////////////////////////////////////////////////////
  
  if(eqn(req.method,HTTP_OPTIONS)) {
    R.resp.allow="HEAD GET PUT POST DELETE";
    return SendStatus(*sock,R,keep,HTTP_200);
  }

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

  if(req.cont_length.empty()&&(eqn(req.method,HTTP_PUT)||eqn(req.method,HTTP_POST)))
    return SendStatus(*sock,R,0,HTTP_411);

  if(eqn(req.method,HTTP_HEAD)||
     eqn(req.method,HTTP_GET)||
     eqn(req.method,HTTP_PUT)||
     eqn(req.method,HTTP_DELETE)||
     eqn(req.method,HTTP_POST)) {
    R.resp.cont_type=mime_texthtml;
    R.resp.cont_type_charset=mime_default;
    try {
      Env env(serv->htcd.ops,R,XSYSTEM);
      env.req->running=&running;
      mstream m;
      env.parse(m,serv->server);
      if(!env.req->is_running()) THROW("htc: "<<req.uri<<" ended prematurely; reload");
    }
    catch(const merror_t& e) {
      Log(e.desc);
      return SendStatus(*sock,R,0,HTTP_503,e.desc);
    }
  }
  else return SendStatus(*sock,R,keep,HTTP_501);

  if(!keepalive) keep=0;
  if(req.if_mod.nempty()&&R.resp.lastmod.nempty()) {
    time_t since=cvtTime(req.if_mod),lst=cvtTime(R.resp.lastmod);
    if(lst<=since) return SendStatus(*sock,R,keep,HTTP_304);
  }

  mstring status=R.resp.status;
  if(eqn(req.method,HTTP_HEAD)) return SendStatus(*sock,R,keep,status);
  else return SendStatus(*sock,R,keep,status,cache.str());

}