00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "Parse.h"
00026 #include "ServerHandler.h"
00027 #include <libhttpd.h>
00028 #include "Deliver.h"
00029
00030
00031
00032 Deliver::Deliver(TcpSocket& s,const std::string& h,const std::string& p,const std::string& m,const std::string& u)
00033 :m_socket(s)
00034 ,m_host(h)
00035 ,m_protocol(p)
00036 ,m_method(m)
00037 ,m_url(u)
00038 {
00039 if (m_url.substr(m_url.size() - 1) == "/")
00040 {
00041 m_url += "index.html";
00042 }
00043 }
00044
00045
00046 Deliver::~Deliver()
00047 {
00048 }
00049
00050
00051 void Deliver::Run()
00052 {
00053
00054
00055
00056 ServerHandler& h = static_cast<ServerHandler&>(m_socket.MasterHandler());
00057 std::string url = m_protocol + "://" + m_host + m_url;
00058 std::string host;
00059 int port;
00060 std::string url_ut;
00061 std::string file;
00062 std::string ext;
00063 url_this(url, host, port, url_ut, file, ext);
00064 std::string mime_type = h.GetMimetype(ext);
00065 Database *db = h.GetDatabase();
00066 Query q(*db);
00067 std::string safe_host = db -> safestr(m_host);
00068 std::string safe_protocol = db -> safestr(m_protocol);
00069 std::string safe_method = db -> safestr(m_method);
00070 std::string safe_url = db -> safestr(m_url);
00071 char sql[1000];
00072 sprintf(sql,"select * from url where host='%s' and protocol='%s' and method='%s' and url='%s'",
00073 safe_host.c_str(),
00074 safe_protocol.c_str(),
00075 safe_method.c_str(),
00076 safe_url.c_str());
00077 db::Url xx(db,sql);
00078 if (!xx.num)
00079 {
00080 xx.host = m_host;
00081 xx.protocol = m_protocol;
00082 xx.method = m_method;
00083 xx.url = m_url;
00084 xx.mime_type = mime_type;
00085 }
00086 xx.access_count++;
00087 xx.save();
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102 db::Regel regel(*db,xx.regel);
00103 if (regel.num)
00104 {
00105 switch (regel.typ)
00106 {
00107 case 1:
00108 {
00109 std::string filename = regel.path + m_url;
00110 FILE *fil = fopen(filename.c_str(),"rb");
00111 if (fil)
00112 {
00113 char buf[4096];
00114 size_t n;
00115 m_socket.Send("HTTP/1.0 200 OK\n");
00116 m_socket.Send("Server: " + h.GetString("server/identity") + "\n");
00117 if (mime_type.size())
00118 {
00119 std::string str = "Content-type: " + mime_type;
00120 m_socket.Send(str + "\n");
00121 }
00122 m_socket.Send("Connection: close\n");
00123 m_socket.Send("\n");
00124 n = fread(buf, 1, 4096, fil);
00125 while (n > 0)
00126 {
00127
00128 m_socket.SendBuf(buf, n);
00129
00130 n = fread(buf, 1, 4096, fil);
00131 }
00132 fclose(fil);
00133 m_socket.SetCloseAndDelete();
00134 }
00135 else
00136 {
00137 m_socket.Send("HTTP/1.0 404 Not Found\n");
00138 m_socket.Send("Server: " + h.GetString("server/identity") + "\n");
00139 m_socket.Send("Content-type: text/html\n");
00140 m_socket.Send("\n<html><body><h1>404 Not Found</h1></html>\n");
00141 m_socket.SetCloseAndDelete();
00142 }
00143 }
00144 break;
00145 case 2:
00146 break;
00147 default:
00148 m_socket.Send("HTTP/1.0 404 Not Found\n");
00149 m_socket.Send("Server: " + h.GetString("server/identity") + "\n");
00150 m_socket.Send("Content-type: text/html\n");
00151 m_socket.Send("\n<html><body><h1>404 Not Found - Resource not available</h1></html>\n");
00152 m_socket.SetCloseAndDelete();
00153 }
00154 }
00155 else
00156 {
00157 m_socket.Send("HTTP/1.0 404 Not Found\n");
00158 m_socket.Send("Server: " + h.GetString("server/identity") + "\n");
00159 m_socket.Send("Content-type: text/html\n");
00160 m_socket.Send("\n<html><body><h1>404 Not Found - Resource not available</h1></html>\n");
00161 m_socket.SetCloseAndDelete();
00162 }
00163 }
00164
00165
00166 void Deliver::url_this(const std::string& url_in,std::string& host,int& port,std::string& url,std::string& file,std::string& ext)
00167 {
00168 Parse pa(url_in,"/");
00169 pa.getword();
00170 host = pa.getword();
00171 if (strstr(host.c_str(),":"))
00172 {
00173 Parse pa(host,":");
00174 pa.getword(host);
00175 port = pa.getvalue();
00176 }
00177 else
00178 {
00179 port = 80;
00180 }
00181 url = "/" + pa.getrest();
00182 {
00183 Parse pa(url,"/");
00184 std::string tmp = pa.getword();
00185 while (tmp.size())
00186 {
00187 file = tmp;
00188 tmp = pa.getword();
00189 }
00190 }
00191 {
00192 Parse pa(file,".");
00193 std::string tmp = pa.getword();
00194 while (tmp.size())
00195 {
00196 ext = tmp;
00197 tmp = pa.getword();
00198 }
00199 }
00200 }
00201
00202