Logo
~Sockets~
~Examples~
~Contact~


Deliver Class Reference

#include <Deliver.h>

List of all members.


Public Member Functions

 Deliver (TcpSocket &, const std::string &host, const std::string &protocol, const std::string &method, const std::string &url)
 ~Deliver ()
void Run ()
void url_this (const std::string &url_in, std::string &host, int &port, std::string &url, std::string &file, std::string &ext)

Private Attributes

TcpSocket & m_socket
std::string m_host
std::string m_protocol
std::string m_method
std::string m_url

Detailed Description

File ......... Deliver.h Published .... 2004-07-13 Author ....... grymse@alhem.net

Definition at line 30 of file Deliver.h.


Constructor & Destructor Documentation

Deliver::Deliver ( TcpSocket &  s,
const std::string &  h,
const std::string &  p,
const std::string &  m,
const std::string &  u 
)

File ......... Deliver.cpp Published .... 2004-07-13 Author ....... grymse@alhem.net

Definition at line 32 of file Deliver.cpp.

References m_url.

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 }

Deliver::~Deliver (  ) 

Definition at line 46 of file Deliver.cpp.

00047 {
00048 }


Member Function Documentation

void Deliver::Run (  ) 

Definition at line 51 of file Deliver.cpp.

References ServerHandler::GetDatabase(), ServerHandler::GetMimetype(), ServerHandler::GetString(), m_host, m_method, m_protocol, m_socket, m_url, and url_this().

Referenced by ServerSocket::OnDetached().

00052 {
00053         // use Socket::MasterHandler because we're now in a detached socket
00054         // Handler() will return the local sockethandler driving the socket
00055         // MasterHandler() returns the original sockethandler
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         // quick'n dirty
00090 /*
00091 HTTP/1.1 200 OK
00092 Date: Wed, 07 Apr 2004 13:15:18 GMT
00093 Server: Apache/1.3.26 (Unix)
00094 Last-Modified: Mon, 22 Mar 2004 03:11:56 GMT
00095 ETag: "1e70c6-27cb-405e597c"
00096 Accept-Ranges: bytes
00097 Content-Length: 10187
00098 Connection: close
00099 Content-Type: text/html
00100 
00101 */
00102         db::Regel regel(*db,xx.regel);
00103         if (regel.num)
00104         {
00105                 switch (regel.typ)
00106                 {
00107                 case 1: // File
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 //printf("read %d bytes\n",n);
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: // Execute
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 }

void Deliver::url_this ( const std::string &  url_in,
std::string &  host,
int &  port,
std::string &  url,
std::string &  file,
std::string &  ext 
)

Definition at line 166 of file Deliver.cpp.

Referenced by Run().

00167 {
00168         Parse pa(url_in,"/");
00169         pa.getword(); // http
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 } // url_this


Member Data Documentation

TcpSocket& Deliver::m_socket [private]

Definition at line 45 of file Deliver.h.

Referenced by Run().

std::string Deliver::m_host [private]

Definition at line 46 of file Deliver.h.

Referenced by Run().

std::string Deliver::m_protocol [private]

Definition at line 47 of file Deliver.h.

Referenced by Run().

std::string Deliver::m_method [private]

Definition at line 48 of file Deliver.h.

Referenced by Run().

std::string Deliver::m_url [private]

Definition at line 49 of file Deliver.h.

Referenced by Deliver(), and Run().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2006 by Anders Hedström
Generated on Mon Aug 29 20:21:47 2005 for C++ Sockets by  doxygen 1.4.4