From 5df79c53745fde5d6c3340a2979b1429cd5892c1 Mon Sep 17 00:00:00 2001 From: Henrik Rydberg Date: Sat, 8 Oct 2011 20:30:28 +0200 Subject: Initial import of htcd system 1.0 Signed-off-by: Henrik Rydberg --- src/http/http.h | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/http/mime.cc | 92 +++++++++++++++++++++++++++++++++++ src/http/mime.h | 99 ++++++++++++++++++++++++++++++++++++++ src/http/req.cc | 100 ++++++++++++++++++++++++++++++++++++++ src/http/resp.cc | 79 ++++++++++++++++++++++++++++++ src/http/test.cc | 28 +++++++++++ 6 files changed, 542 insertions(+) create mode 100644 src/http/http.h create mode 100644 src/http/mime.cc create mode 100644 src/http/mime.h create mode 100644 src/http/req.cc create mode 100644 src/http/resp.cc create mode 100644 src/http/test.cc (limited to 'src/http') 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 @@ +/************************************************************************* + * + * 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 + */ + +#ifndef HTTPH +#define HTTPH + +#include + +/////////////////////////////////////////////////////////////////// + +const mstring http_mime_default="application/octet-stream"; + +const mstring HTTP_V10="HTTP/1.0"; +const mstring HTTP_V11="HTTP/1.1"; + +/////////////////////////////////////////////////////////////////// +// Request types +const mstring HTTP_OPTIONS="OPTIONS"; +const mstring HTTP_GET="GET"; +const mstring HTTP_HEAD="HEAD"; +const mstring HTTP_POST="POST"; +const mstring HTTP_PUT="PUT"; +const mstring HTTP_DELETE="DELETE"; +const mstring HTTP_TRACE="TRACE"; +const mstring HTTP_CONNECT="CONNECT"; + +class httpReq : public HTTP { +public: + httpReq(); + httpReq(const sref& msg) { Init(msg); } + + mstring method,uri,arg,version; + mstring auth,expect,from,host; + mstring if_match,if_mod,if_none,if_range,if_unmod; + mstring max_forwards,proxy,range; + mstring referer,te,user_agent; + sspile cookies; + + void putHead(mstream& out) const; +protected: + int Special(sref line); + int Handle(const sref& name, + const sref& rawval, + const sref& val, + const rrpile& opts); +}; + +/////////////////////////////////////////////////////////////////// + +/////////////////////////////////////////////////////////////////// +// Informational 1xx +const mstring HTTP_100="100 Continue"; +const mstring HTTP_101="101 Switching Protocols"; +// Successful 2xx +const mstring HTTP_200="200 OK"; +const mstring HTTP_201="201 Created"; +const mstring HTTP_202="202 Accepted"; +const mstring HTTP_203="203 Non-Authoritative Information"; +const mstring HTTP_204="204 No Content"; +const mstring HTTP_205="205 Reset Content"; +const mstring HTTP_206="206 Partial Content"; +// Redirection 3xx +const mstring HTTP_300="300 Multiple Choices"; +const mstring HTTP_301="301 Moved Permanently"; +const mstring HTTP_302="302 Found"; +const mstring HTTP_303="303 See Other"; +const mstring HTTP_304="304 Not Modified"; +const mstring HTTP_305="305 Use Proxy"; +const mstring HTTP_306="306 (Unused)"; +const mstring HTTP_307="307 Temporary Redirect"; +// Client Error 4xx +const mstring HTTP_400="400 Bad Request"; +const mstring HTTP_401="401 Unauthorized"; +const mstring HTTP_402="402 Payment Required"; +const mstring HTTP_403="403 Forbidden"; +const mstring HTTP_404="404 Not Found"; +const mstring HTTP_405="405 Method Not Allowed"; +const mstring HTTP_406="406 Not Acceptable"; +const mstring HTTP_407="407 Proxy Authentication Required"; +const mstring HTTP_408="408 Request Timeout"; +const mstring HTTP_409="409 Conflict"; +const mstring HTTP_410="410 Gone"; +const mstring HTTP_411="411 Length Required"; +const mstring HTTP_412="412 Precondition Failed"; +const mstring HTTP_413="413 Request Entity Too Large"; +const mstring HTTP_414="414 Request-URI Too Long"; +const mstring HTTP_415="415 Unsupported Media Type"; +const mstring HTTP_416="416 Requested Range Not Satisfiable"; +const mstring HTTP_417="417 Expectation Failed"; +// Server Error 5xx +const mstring HTTP_500="500 Internal Server Error"; +const mstring HTTP_501="501 Not Implemented"; +const mstring HTTP_502="502 Bad Gateway"; +const mstring HTTP_503="503 Service Unavailable"; +const mstring HTTP_504="504 Gateway Timeout"; +const mstring HTTP_505="505 HTTP Version Not Supported"; + +struct cookie_t { + mstring name,val,options; + cookie_t() {} + cookie_t(const sref& n,const sref& v) : name(n),val(v) {} + cookie_t(const sref& n,const sref& v,const sref& o) : + name(n),val(v),options(o) {} +}; + +class httpResp : public HTTP { +public: + httpResp(); + httpResp(const sref& msg) { Init(msg); } + + mstring version,status; + mstring age,etag,location; + mstring proxy_authent,retry,server,vary,authent; + msvec set_cookies; + + void putHead(mstream& out) const; +protected: + int Special(sref line); + int Handle(const sref& name, + const sref& rawval, + const sref& val, + const rrpile& opts); +}; + +/////////////////////////////////////////////////////////////////// + +#endif + 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 @@ +/************************************************************************* + * + * 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 + +HTTP::HTTP() +{ + connect="close"; + pragma="no-cache"; + date=httpTime(idate=Now()); +} + +int HTTP::Special(sref line) +{ + return 1; +} + +int HTTP::Handle(const sref& name, + const sref& rawval, + const sref& val, + const rrpile& opts) +{ + if(Mime::Handle(name,rawval,val,opts)) return 1; + else if(eqn(name,cache_tag)) cache=val; + else if(eqn(name,connect_tag)) connect=val; + else if(eqn(name,date_tag)) date=val; + else if(eqn(name,pragma_tag)) pragma=val; + else if(eqn(name,trailer_tag)) trailer=val; + else if(eqn(name,trans_enc_tag)) trans_enc=val; + else if(eqn(name,upgrade_tag)) upgrade=val; + else if(eqn(name,via_tag)) via=val; + else if(eqn(name,warning_tag)) warning=val; + else if(eqn(name,allow_tag)) allow=val; + else if(eqn(name,cont_enc_tag)) cont_enc=val; + else if(eqn(name,cont_lang_tag)) cont_lang=val; + else if(eqn(name,cont_location_tag)) cont_location=val; + else if(eqn(name,cont_md5_tag)) cont_md5=val; + else if(eqn(name,cont_range_tag)) cont_range=val; + else if(eqn(name,expires_tag)) expires=val; + else if(eqn(name,lastmod_tag)) lastmod=val; + else if(eqn(name,accept_tag)) accept=val; + else if(eqn(name,accept_charset_tag)) accept_charset=val; + else if(eqn(name,accept_enc_tag)) accept_enc=val; + else if(eqn(name,accept_lang_tag)) accept_lang=val; + else if(eqn(name,accept_range_tag)) accept_range=val; + else return 0; + return 1; +} + +void HTTP::putHead(mstream& out) const +{ + if(putval(out,cache_tag,cache)) putend(out); + if(putval(out,connect_tag,connect)) putend(out); + if(putval(out,date_tag,date)) putend(out); + if(putval(out,pragma_tag,pragma)) putend(out); + if(putval(out,trailer_tag,trailer)) putend(out); + if(putval(out,trans_enc_tag,trans_enc)) putend(out); + if(putval(out,upgrade_tag,upgrade)) putend(out); + if(putval(out,via_tag,via)) putend(out); + if(putval(out,warning_tag,warning)) putend(out); + if(putval(out,allow_tag,allow)) putend(out); + if(putval(out,cont_enc_tag,cont_enc)) putend(out); + if(putval(out,cont_lang_tag,cont_lang)) putend(out); + if(putval(out,cont_location_tag,cont_location)) putend(out); + if(putval(out,cont_md5_tag,cont_md5)) putend(out); + if(putval(out,cont_range_tag,cont_range)) putend(out); + if(putval(out,expires_tag,expires)) putend(out); + if(putval(out,lastmod_tag,lastmod)) putend(out); + if(putval(out,accept_tag,accept)) putend(out); + if(putval(out,accept_charset_tag,accept_charset)) putend(out); + if(putval(out,accept_enc_tag,accept_enc)) putend(out); + if(putval(out,accept_lang_tag,accept_lang)) putend(out); + if(putval(out,accept_range_tag,accept_range)) putend(out); + Mime::putHead(out); +} + 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 @@ +/************************************************************************* + * + * 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 + */ + +#ifndef MIMEHTTPH +#define MIMEHTTPH + +#include +#include + +// Entity Headers +const mstring allow_tag="Allow"; +const mstring accept_tag="Accept"; +const mstring accept_charset_tag="Accept-Charset"; +const mstring accept_enc_tag="Accept-Encoding"; +const mstring accept_lang_tag="Accept-Language"; +const mstring accept_range_tag="Accept-Ranges"; +const mstring cont_enc_tag="Content-Encoding"; +const mstring cont_lang_tag="Content-Language"; +const mstring cont_location_tag="Content-Location"; +const mstring cont_md5_tag="Content-MD5"; +const mstring cont_range_tag="Content-Range"; +const mstring expires_tag="Expires"; +const mstring lastmod_tag="Last-Modified"; +// General headers +const mstring cache_tag="Cache-Control"; +const mstring connect_tag="Connection"; +const mstring date_tag="Date"; +const mstring pragma_tag="Pragma"; +const mstring trailer_tag="Trailer"; +const mstring trans_enc_tag="Transfer-Encoding"; +const mstring upgrade_tag="Upgrade"; +const mstring via_tag="Via"; +const mstring warning_tag="Warning"; +// Request headers +const mstring auth_tag="Authorization"; +const mstring expect_tag="Expect"; +const mstring host_tag="Host"; +const mstring if_match_tag="If-Match"; +const mstring if_mod_tag="If-Modified-Since"; +const mstring if_none_tag="If-None-Match"; +const mstring if_range_tag="If-Range"; +const mstring if_unmod_tag="If-Unmodified-Since"; +const mstring max_forwards_tag="Max-Forwards"; +const mstring proxy_tag="Proxy-Authorization"; +const mstring range_tag="Range"; +const mstring referer_tag="Referer"; +const mstring te_tag="TE"; +const mstring user_agent_tag="User-Agent"; +const mstring cookie_tag="Cookie"; +// Response headers +const mstring age_tag="Age"; +const mstring etag_tag="ETag"; +const mstring location_tag="Location"; +const mstring proxy_authent_tag="Proxy-Authenticate"; +const mstring retry_tag="Retry-After"; +const mstring server_tag="Server"; +const mstring vary_tag="Vary"; +const mstring authent_tag="WWW-Authenticate"; +const mstring set_cookie_tag="Set-Cookie"; + +class HTTP : public Mime { +public: + HTTP(); + HTTP(const sref& msg) { Init(msg); } + + mstring cache,connect,date,pragma; + mstring trailer,trans_enc,upgrade,via,warning; + mstring allow,cont_enc,cont_lang,cont_location; + mstring cont_md5,cont_range,expires,lastmod; + mstring accept,accept_charset,accept_enc,accept_lang,accept_range; + + time_t idate; + void putHead(mstream& out) const; +protected: + int Special(sref line); + int Handle(const sref& name, + const sref& rawval, + const sref& val, + const rrpile& opts); +}; + +#endif + 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 @@ +/************************************************************************* + * + * 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 +#include + +httpReq::httpReq() +{ +} + +int httpReq::Special(sref line) +{ + sref first,rest=line; + Split(first,rest); + method=first; + Split(first,rest); + uri=first; + arg.clear(); + Split(first,rest); + version=first; + int pos=uri.find('?'); + if(pos>=0) { arg=uri.right(pos+1); uri.resize(pos); } + return 1; +} + +int httpReq::Handle(const sref& name, + const sref& rawval, + const sref& val, + const rrpile& opts) +{ + if(HTTP::Handle(name,rawval,val,opts)) return 1; + else if(eqn(name,auth_tag)) auth=val; + else if(eqn(name,expect_tag)) expect=val; + else if(eqn(name,from_tag)) from=val; + else if(eqn(name,host_tag)) host=val; + else if(eqn(name,if_match_tag)) if_match=val; + else if(eqn(name,if_mod_tag)) if_mod=val; + else if(eqn(name,if_none_tag)) if_none=val; + else if(eqn(name,if_range_tag)) if_range=val; + else if(eqn(name,if_unmod_tag)) if_unmod=val; + else if(eqn(name,max_forwards_tag)) max_forwards=val; + else if(eqn(name,proxy_tag)) proxy=val; + else if(eqn(name,range_tag)) range=val; + else if(eqn(name,referer_tag)) referer=val; + else if(eqn(name,te_tag)) te=val; + else if(eqn(name,user_agent_tag)) user_agent=val; + else if(eqn(name,cookie_tag)) { + for(rrpile::const_iterator p=opts.begin();p!=opts.end();p++) + cookies.push_back(sstring(p->first,p->second)); + } + else return 0; + return 1; +} + +void httpReq::putHead(mstream& out) const +{ + out<first,p->second); + else putopt(out,p->first,p->second); + } + putend(out); + } +} 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 @@ +/************************************************************************* + * + * 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 + +httpResp::httpResp() +{ + server="HTCd/1.2 MT (Unix)"; + version="HTTP/1.1"; + status=HTTP_200; + mime_version.clear(); + //accept="*/*"; + //this is non-standard in response headers - now... +} + +int httpResp::Special(sref line) +{ + version=First(line); + status=Rest(line); + return 1; +} + +int httpResp::Handle(const sref& name, + const sref& rawval, + const sref& val, + const rrpile& opts) +{ + if(HTTP::Handle(name,rawval,val,opts)) return 1; + else if(eqn(name,age_tag)) age=val; + else if(eqn(name,etag_tag)) etag=val; + else if(eqn(name,location_tag)) location=val; + else if(eqn(name,proxy_authent_tag)) proxy_authent=val; + else if(eqn(name,retry_tag)) retry=val; + else if(eqn(name,server_tag)) server=val; + else if(eqn(name,vary_tag)) vary=val; + else if(eqn(name,authent_tag)) authent=val; + else if(eqn(name,set_cookie_tag)) { + for(rrpile::const_iterator p=opts.begin();p!=opts.end();p++) + set_cookies.push_back(cookie_t(p->first,p->second)); + } + else return 0; + return 1; +} + +void httpResp::putHead(mstream& out) const +{ + out<::const_iterator p=set_cookies.begin();p!=set_cookies.end();p++) { + out<name,p->val); + if(p->options.nempty()) out<<"; "<options; + putend(out); + } +} 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 @@ +/************************************************************************* + * + * 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 +#include + +static fp_stream mout(stdout),merr(stderr); + +main(int argc,char* argv[]) +{ + return 0; +} -- cgit v1.2.3