summaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
Diffstat (limited to 'src/http')
-rw-r--r--src/http/http.h144
-rw-r--r--src/http/mime.cc92
-rw-r--r--src/http/mime.h99
-rw-r--r--src/http/req.cc100
-rw-r--r--src/http/resp.cc79
-rw-r--r--src/http/test.cc28
6 files changed, 542 insertions, 0 deletions
diff --git a/src/http/http.h b/src/http/http.h
new file mode 100644
index 0000000..8bd4d23
--- /dev/null
+++ b/src/http/http.h
@@ -0,0 +1,144 @@
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#ifndef HTTPH
21#define HTTPH
22
23#include <http/mime.h>
24
25///////////////////////////////////////////////////////////////////
26
27const mstring http_mime_default="application/octet-stream";
28
29const mstring HTTP_V10="HTTP/1.0";
30const mstring HTTP_V11="HTTP/1.1";
31
32///////////////////////////////////////////////////////////////////
33// Request types
34const mstring HTTP_OPTIONS="OPTIONS";
35const mstring HTTP_GET="GET";
36const mstring HTTP_HEAD="HEAD";
37const mstring HTTP_POST="POST";
38const mstring HTTP_PUT="PUT";
39const mstring HTTP_DELETE="DELETE";
40const mstring HTTP_TRACE="TRACE";
41const mstring HTTP_CONNECT="CONNECT";
42
43class httpReq : public HTTP {
44public:
45 httpReq();
46 httpReq(const sref& msg) { Init(msg); }
47
48 mstring method,uri,arg,version;
49 mstring auth,expect,from,host;
50 mstring if_match,if_mod,if_none,if_range,if_unmod;
51 mstring max_forwards,proxy,range;
52 mstring referer,te,user_agent;
53 sspile cookies;
54
55 void putHead(mstream& out) const;
56protected:
57 int Special(sref line);
58 int Handle(const sref& name,
59 const sref& rawval,
60 const sref& val,
61 const rrpile& opts);
62};
63
64///////////////////////////////////////////////////////////////////
65
66///////////////////////////////////////////////////////////////////
67// Informational 1xx
68const mstring HTTP_100="100 Continue";
69const mstring HTTP_101="101 Switching Protocols";
70// Successful 2xx
71const mstring HTTP_200="200 OK";
72const mstring HTTP_201="201 Created";
73const mstring HTTP_202="202 Accepted";
74const mstring HTTP_203="203 Non-Authoritative Information";
75const mstring HTTP_204="204 No Content";
76const mstring HTTP_205="205 Reset Content";
77const mstring HTTP_206="206 Partial Content";
78// Redirection 3xx
79const mstring HTTP_300="300 Multiple Choices";
80const mstring HTTP_301="301 Moved Permanently";
81const mstring HTTP_302="302 Found";
82const mstring HTTP_303="303 See Other";
83const mstring HTTP_304="304 Not Modified";
84const mstring HTTP_305="305 Use Proxy";
85const mstring HTTP_306="306 (Unused)";
86const mstring HTTP_307="307 Temporary Redirect";
87// Client Error 4xx
88const mstring HTTP_400="400 Bad Request";
89const mstring HTTP_401="401 Unauthorized";
90const mstring HTTP_402="402 Payment Required";
91const mstring HTTP_403="403 Forbidden";
92const mstring HTTP_404="404 Not Found";
93const mstring HTTP_405="405 Method Not Allowed";
94const mstring HTTP_406="406 Not Acceptable";
95const mstring HTTP_407="407 Proxy Authentication Required";
96const mstring HTTP_408="408 Request Timeout";
97const mstring HTTP_409="409 Conflict";
98const mstring HTTP_410="410 Gone";
99const mstring HTTP_411="411 Length Required";
100const mstring HTTP_412="412 Precondition Failed";
101const mstring HTTP_413="413 Request Entity Too Large";
102const mstring HTTP_414="414 Request-URI Too Long";
103const mstring HTTP_415="415 Unsupported Media Type";
104const mstring HTTP_416="416 Requested Range Not Satisfiable";
105const mstring HTTP_417="417 Expectation Failed";
106// Server Error 5xx
107const mstring HTTP_500="500 Internal Server Error";
108const mstring HTTP_501="501 Not Implemented";
109const mstring HTTP_502="502 Bad Gateway";
110const mstring HTTP_503="503 Service Unavailable";
111const mstring HTTP_504="504 Gateway Timeout";
112const mstring HTTP_505="505 HTTP Version Not Supported";
113
114struct cookie_t {
115 mstring name,val,options;
116 cookie_t() {}
117 cookie_t(const sref& n,const sref& v) : name(n),val(v) {}
118 cookie_t(const sref& n,const sref& v,const sref& o) :
119 name(n),val(v),options(o) {}
120};
121
122class httpResp : public HTTP {
123public:
124 httpResp();
125 httpResp(const sref& msg) { Init(msg); }
126
127 mstring version,status;
128 mstring age,etag,location;
129 mstring proxy_authent,retry,server,vary,authent;
130 msvec<cookie_t> set_cookies;
131
132 void putHead(mstream& out) const;
133protected:
134 int Special(sref line);
135 int Handle(const sref& name,
136 const sref& rawval,
137 const sref& val,
138 const rrpile& opts);
139};
140
141///////////////////////////////////////////////////////////////////
142
143#endif
144
diff --git a/src/http/mime.cc b/src/http/mime.cc
new file mode 100644
index 0000000..f6b3412
--- /dev/null
+++ b/src/http/mime.cc
@@ -0,0 +1,92 @@
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 <http/mime.h>
21
22HTTP::HTTP()
23{
24 connect="close";
25 pragma="no-cache";
26 date=httpTime(idate=Now());
27}
28
29int HTTP::Special(sref line)
30{
31 return 1;
32}
33
34int HTTP::Handle(const sref& name,
35 const sref& rawval,
36 const sref& val,
37 const rrpile& opts)
38{
39 if(Mime::Handle(name,rawval,val,opts)) return 1;
40 else if(eqn(name,cache_tag)) cache=val;
41 else if(eqn(name,connect_tag)) connect=val;
42 else if(eqn(name,date_tag)) date=val;
43 else if(eqn(name,pragma_tag)) pragma=val;
44 else if(eqn(name,trailer_tag)) trailer=val;
45 else if(eqn(name,trans_enc_tag)) trans_enc=val;
46 else if(eqn(name,upgrade_tag)) upgrade=val;
47 else if(eqn(name,via_tag)) via=val;
48 else if(eqn(name,warning_tag)) warning=val;
49 else if(eqn(name,allow_tag)) allow=val;
50 else if(eqn(name,cont_enc_tag)) cont_enc=val;
51 else if(eqn(name,cont_lang_tag)) cont_lang=val;
52 else if(eqn(name,cont_location_tag)) cont_location=val;
53 else if(eqn(name,cont_md5_tag)) cont_md5=val;
54 else if(eqn(name,cont_range_tag)) cont_range=val;
55 else if(eqn(name,expires_tag)) expires=val;
56 else if(eqn(name,lastmod_tag)) lastmod=val;
57 else if(eqn(name,accept_tag)) accept=val;
58 else if(eqn(name,accept_charset_tag)) accept_charset=val;
59 else if(eqn(name,accept_enc_tag)) accept_enc=val;
60 else if(eqn(name,accept_lang_tag)) accept_lang=val;
61 else if(eqn(name,accept_range_tag)) accept_range=val;
62 else return 0;
63 return 1;
64}
65
66void HTTP::putHead(mstream& out) const
67{
68 if(putval(out,cache_tag,cache)) putend(out);
69 if(putval(out,connect_tag,connect)) putend(out);
70 if(putval(out,date_tag,date)) putend(out);
71 if(putval(out,pragma_tag,pragma)) putend(out);
72 if(putval(out,trailer_tag,trailer)) putend(out);
73 if(putval(out,trans_enc_tag,trans_enc)) putend(out);
74 if(putval(out,upgrade_tag,upgrade)) putend(out);
75 if(putval(out,via_tag,via)) putend(out);
76 if(putval(out,warning_tag,warning)) putend(out);
77 if(putval(out,allow_tag,allow)) putend(out);
78 if(putval(out,cont_enc_tag,cont_enc)) putend(out);
79 if(putval(out,cont_lang_tag,cont_lang)) putend(out);
80 if(putval(out,cont_location_tag,cont_location)) putend(out);
81 if(putval(out,cont_md5_tag,cont_md5)) putend(out);
82 if(putval(out,cont_range_tag,cont_range)) putend(out);
83 if(putval(out,expires_tag,expires)) putend(out);
84 if(putval(out,lastmod_tag,lastmod)) putend(out);
85 if(putval(out,accept_tag,accept)) putend(out);
86 if(putval(out,accept_charset_tag,accept_charset)) putend(out);
87 if(putval(out,accept_enc_tag,accept_enc)) putend(out);
88 if(putval(out,accept_lang_tag,accept_lang)) putend(out);
89 if(putval(out,accept_range_tag,accept_range)) putend(out);
90 Mime::putHead(out);
91}
92
diff --git a/src/http/mime.h b/src/http/mime.h
new file mode 100644
index 0000000..85200cc
--- /dev/null
+++ b/src/http/mime.h
@@ -0,0 +1,99 @@
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#ifndef MIMEHTTPH
21#define MIMEHTTPH
22
23#include <mime/mime.h>
24#include <mt/dates.h>
25
26// Entity Headers
27const mstring allow_tag="Allow";
28const mstring accept_tag="Accept";
29const mstring accept_charset_tag="Accept-Charset";
30const mstring accept_enc_tag="Accept-Encoding";
31const mstring accept_lang_tag="Accept-Language";
32const mstring accept_range_tag="Accept-Ranges";
33const mstring cont_enc_tag="Content-Encoding";
34const mstring cont_lang_tag="Content-Language";
35const mstring cont_location_tag="Content-Location";
36const mstring cont_md5_tag="Content-MD5";
37const mstring cont_range_tag="Content-Range";
38const mstring expires_tag="Expires";
39const mstring lastmod_tag="Last-Modified";
40// General headers
41const mstring cache_tag="Cache-Control";
42const mstring connect_tag="Connection";
43const mstring date_tag="Date";
44const mstring pragma_tag="Pragma";
45const mstring trailer_tag="Trailer";
46const mstring trans_enc_tag="Transfer-Encoding";
47const mstring upgrade_tag="Upgrade";
48const mstring via_tag="Via";
49const mstring warning_tag="Warning";
50// Request headers
51const mstring auth_tag="Authorization";
52const mstring expect_tag="Expect";
53const mstring host_tag="Host";
54const mstring if_match_tag="If-Match";
55const mstring if_mod_tag="If-Modified-Since";
56const mstring if_none_tag="If-None-Match";
57const mstring if_range_tag="If-Range";
58const mstring if_unmod_tag="If-Unmodified-Since";
59const mstring max_forwards_tag="Max-Forwards";
60const mstring proxy_tag="Proxy-Authorization";
61const mstring range_tag="Range";
62const mstring referer_tag="Referer";
63const mstring te_tag="TE";
64const mstring user_agent_tag="User-Agent";
65const mstring cookie_tag="Cookie";
66// Response headers
67const mstring age_tag="Age";
68const mstring etag_tag="ETag";
69const mstring location_tag="Location";
70const mstring proxy_authent_tag="Proxy-Authenticate";
71const mstring retry_tag="Retry-After";
72const mstring server_tag="Server";
73const mstring vary_tag="Vary";
74const mstring authent_tag="WWW-Authenticate";
75const mstring set_cookie_tag="Set-Cookie";
76
77class HTTP : public Mime {
78public:
79 HTTP();
80 HTTP(const sref& msg) { Init(msg); }
81
82 mstring cache,connect,date,pragma;
83 mstring trailer,trans_enc,upgrade,via,warning;
84 mstring allow,cont_enc,cont_lang,cont_location;
85 mstring cont_md5,cont_range,expires,lastmod;
86 mstring accept,accept_charset,accept_enc,accept_lang,accept_range;
87
88 time_t idate;
89 void putHead(mstream& out) const;
90protected:
91 int Special(sref line);
92 int Handle(const sref& name,
93 const sref& rawval,
94 const sref& val,
95 const rrpile& opts);
96};
97
98#endif
99
diff --git a/src/http/req.cc b/src/http/req.cc
new file mode 100644
index 0000000..acb2105
--- /dev/null
+++ b/src/http/req.cc
@@ -0,0 +1,100 @@
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 <http/http.h>
21#include <mime/mailbox.h>
22
23httpReq::httpReq()
24{
25}
26
27int httpReq::Special(sref line)
28{
29 sref first,rest=line;
30 Split(first,rest);
31 method=first;
32 Split(first,rest);
33 uri=first;
34 arg.clear();
35 Split(first,rest);
36 version=first;
37 int pos=uri.find('?');
38 if(pos>=0) { arg=uri.right(pos+1); uri.resize(pos); }
39 return 1;
40}
41
42int httpReq::Handle(const sref& name,
43 const sref& rawval,
44 const sref& val,
45 const rrpile& opts)
46{
47 if(HTTP::Handle(name,rawval,val,opts)) return 1;
48 else if(eqn(name,auth_tag)) auth=val;
49 else if(eqn(name,expect_tag)) expect=val;
50 else if(eqn(name,from_tag)) from=val;
51 else if(eqn(name,host_tag)) host=val;
52 else if(eqn(name,if_match_tag)) if_match=val;
53 else if(eqn(name,if_mod_tag)) if_mod=val;
54 else if(eqn(name,if_none_tag)) if_none=val;
55 else if(eqn(name,if_range_tag)) if_range=val;
56 else if(eqn(name,if_unmod_tag)) if_unmod=val;
57 else if(eqn(name,max_forwards_tag)) max_forwards=val;
58 else if(eqn(name,proxy_tag)) proxy=val;
59 else if(eqn(name,range_tag)) range=val;
60 else if(eqn(name,referer_tag)) referer=val;
61 else if(eqn(name,te_tag)) te=val;
62 else if(eqn(name,user_agent_tag)) user_agent=val;
63 else if(eqn(name,cookie_tag)) {
64 for(rrpile::const_iterator p=opts.begin();p!=opts.end();p++)
65 cookies.push_back(sstring(p->first,p->second));
66 }
67 else return 0;
68 return 1;
69}
70
71void httpReq::putHead(mstream& out) const
72{
73 out<<method<<" "<<uri;
74 if(arg.nempty()) out<<"?"<<arg;
75 out<<" "<<version<<CRLF;
76 if(putval(out,auth_tag,auth)) putend(out);
77 if(putval(out,expect_tag,expect)) putend(out);
78 if(putval(out,from_tag,from)) putend(out);
79 if(putval(out,host_tag,host)) putend(out);
80 if(putval(out,if_match_tag,if_match)) putend(out);
81 if(putval(out,if_mod_tag,if_mod)) putend(out);
82 if(putval(out,if_none_tag,if_none)) putend(out);
83 if(putval(out,if_range_tag,if_range)) putend(out);
84 if(putval(out,if_unmod_tag,if_unmod)) putend(out);
85 if(putval(out,max_forwards_tag,max_forwards)) putend(out);
86 if(putval(out,proxy_tag,proxy)) putend(out);
87 if(putval(out,range_tag,range)) putend(out);
88 if(putval(out,referer_tag,referer)) putend(out);
89 if(putval(out,te_tag,te)) putend(out);
90 if(putval(out,user_agent_tag,user_agent)) putend(out);
91 HTTP::putHead(out);
92 if(!cookies.empty()) {
93 out<<cookie_tag<<": ";
94 for(sspile::const_iterator p=cookies.begin();p!=cookies.end();p++) {
95 if(p==cookies.begin()) putopt1(out,p->first,p->second);
96 else putopt(out,p->first,p->second);
97 }
98 putend(out);
99 }
100}
diff --git a/src/http/resp.cc b/src/http/resp.cc
new file mode 100644
index 0000000..aa7ed7b
--- /dev/null
+++ b/src/http/resp.cc
@@ -0,0 +1,79 @@
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 <http/http.h>
21
22httpResp::httpResp()
23{
24 server="HTCd/1.2 MT (Unix)";
25 version="HTTP/1.1";
26 status=HTTP_200;
27 mime_version.clear();
28 //accept="*/*";
29 //this is non-standard in response headers - now...
30}
31
32int httpResp::Special(sref line)
33{
34 version=First(line);
35 status=Rest(line);
36 return 1;
37}
38
39int httpResp::Handle(const sref& name,
40 const sref& rawval,
41 const sref& val,
42 const rrpile& opts)
43{
44 if(HTTP::Handle(name,rawval,val,opts)) return 1;
45 else if(eqn(name,age_tag)) age=val;
46 else if(eqn(name,etag_tag)) etag=val;
47 else if(eqn(name,location_tag)) location=val;
48 else if(eqn(name,proxy_authent_tag)) proxy_authent=val;
49 else if(eqn(name,retry_tag)) retry=val;
50 else if(eqn(name,server_tag)) server=val;
51 else if(eqn(name,vary_tag)) vary=val;
52 else if(eqn(name,authent_tag)) authent=val;
53 else if(eqn(name,set_cookie_tag)) {
54 for(rrpile::const_iterator p=opts.begin();p!=opts.end();p++)
55 set_cookies.push_back(cookie_t(p->first,p->second));
56 }
57 else return 0;
58 return 1;
59}
60
61void httpResp::putHead(mstream& out) const
62{
63 out<<version<<" "<<status<<CRLF;
64 if(putval(out,age_tag,age)) putend(out);
65 if(putval(out,etag_tag,etag)) putend(out);
66 if(putval(out,location_tag,location)) putend(out);
67 if(putval(out,proxy_authent_tag,proxy_authent)) putend(out);
68 if(putval(out,retry_tag,retry)) putend(out);
69 if(putval(out,server_tag,server)) putend(out);
70 if(putval(out,vary_tag,vary)) putend(out);
71 if(putval(out,authent_tag,authent)) putend(out);
72 HTTP::putHead(out);
73 for(msvec<cookie_t>::const_iterator p=set_cookies.begin();p!=set_cookies.end();p++) {
74 out<<set_cookie_tag<<": ";
75 putopt1(out,p->name,p->val);
76 if(p->options.nempty()) out<<"; "<<p->options;
77 putend(out);
78 }
79}
diff --git a/src/http/test.cc b/src/http/test.cc
new file mode 100644
index 0000000..59ac5c8
--- /dev/null
+++ b/src/http/test.cc
@@ -0,0 +1,28 @@
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 <http/http.h>
21#include <sys/stat.h>
22
23static fp_stream mout(stdout),merr(stderr);
24
25main(int argc,char* argv[])
26{
27 return 0;
28}