bug-myserver
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-myserver] Preliminary to gopher support :)


From: Domenico Chierico
Subject: Re: [bug-myserver] Preliminary to gopher support :)
Date: Sun, 01 Nov 2009 14:01:21 +0100

I've got all fix done, should be fine right now!

I'm waiting for feedback!! 

Cheers
Domenico Chierico

>From b692b00fb83592245135d8e8b11e0e17a91553b2 Mon Sep 17 00:00:00 2001
From: Domenico Chierico <address@hidden>
Date: Thu, 29 Oct 2009 22:46:06 +0100
Subject: [PATCH] Add support for gopher protocol

---
 myserver/configure.ac                             |    2 +
 myserver/include/protocol/Makefile.am             |    2 +-
 myserver/include/protocol/gopher/Makefile.am      |    4 +
 myserver/include/protocol/gopher/engine.h         |   64 ++++
 myserver/include/protocol/gopher/gopher.h         |  102 ++++++
 myserver/include/protocol/gopher/gopher_content.h |  266
+++++++++++++++
 myserver/src/protocol/Makefile.am                 |    2 +-
 myserver/src/protocol/gopher/Makefile.am          |    3 +
 myserver/src/protocol/gopher/engine.cpp           |  288
++++++++++++++++
 myserver/src/protocol/gopher/gopher.cpp           |  109 ++++++
 myserver/src/protocol/gopher/gopher_content.cpp   |  363
+++++++++++++++++++++
 myserver/src/server/server.cpp                    |    2 +
 12 files changed, 1205 insertions(+), 2 deletions(-)
 create mode 100644 myserver/include/protocol/gopher/Makefile.am
 create mode 100644 myserver/include/protocol/gopher/engine.h
 create mode 100644 myserver/include/protocol/gopher/gopher.h
 create mode 100644 myserver/include/protocol/gopher/gopher_content.h
 create mode 100644 myserver/src/protocol/gopher/Makefile.am
 create mode 100644 myserver/src/protocol/gopher/engine.cpp
 create mode 100644 myserver/src/protocol/gopher/gopher.cpp
 create mode 100644 myserver/src/protocol/gopher/gopher_content.cpp

diff --git a/myserver/configure.ac b/myserver/configure.ac
index 1d5c6af..0e6c6d0 100644
--- a/myserver/configure.ac
+++ b/myserver/configure.ac
@@ -438,6 +438,7 @@ AC_CONFIG_FILES([ po/Makefile.in
     include/protocol/https/Makefile
     include/protocol/http/Makefile
     include/protocol/http/env/Makefile
+    include/protocol/gopher/Makefile
     include/filter/Makefile
     include/filter/gzip/Makefile
     include/plugin/Makefile
@@ -494,6 +495,7 @@ AC_CONFIG_FILES([ po/Makefile.in
     src/protocol/https/Makefile
     src/protocol/http/Makefile
     src/protocol/http/env/Makefile
+    src/protocol/gopher/Makefile
     src/filter/Makefile
     src/filter/gzip/Makefile
     src/plugin/Makefile
diff --git a/myserver/include/protocol/Makefile.am
b/myserver/include/protocol/Makefile.am
index aa23116..41dce02 100644
--- a/myserver/include/protocol/Makefile.am
+++ b/myserver/include/protocol/Makefile.am
@@ -1,3 +1,3 @@
 protocolincludedir=$(includedir)/myserver/include/protocol
 protocolinclude_HEADERS = protocol_buffer.h protocol.h
protocols_manager.h url.h
-SUBDIRS = control ftp http https
+SUBDIRS = control ftp gopher http https 
diff --git a/myserver/include/protocol/gopher/Makefile.am
b/myserver/include/protocol/gopher/Makefile.am
new file mode 100644
index 0000000..1995ecf
--- /dev/null
+++ b/myserver/include/protocol/gopher/Makefile.am
@@ -0,0 +1,4 @@
+gopherincludedir=$(includedir)/myserver/include/protocol/gopher
+gopherinclude_HEADERS = gopher.h gopher_content.h engine.h
+
+
diff --git a/myserver/include/protocol/gopher/engine.h
b/myserver/include/protocol/gopher/engine.h
new file mode 100644
index 0000000..dc17d37
--- /dev/null
+++ b/myserver/include/protocol/gopher/engine.h
@@ -0,0 +1,64 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2002-2009 Free Software Foundation, Inc.
+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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef GOPHER_ENGINE_H
+#define GOPHER_ENGINE_H
+
+#include "stdafx.h"
+#include <include/base/hash_map/hash_map.h>
+#include <include/conf/vhost/vhost.h>
+#include <include/base/base64/mime_utils.h>
+
+using namespace std;
+
+class GopherEngine;
+
+typedef void (GopherEngine::* MyMethodPtr)(const string&, const
string&, GopherMenu&);
+
+class GopherEngine
+{
+public:
+  GopherEngine ();
+  GopherContent &incoming (GopherRequest, Vhost *tmp);  
+
+protected:
+  void dirManagement (const string &fname, const string &path,
GopherMenu &tmp);
+  void fileManagement (const string &fname, const string
&path ,GopherMenu &tmp);
+
+  void infoFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void textFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void csoFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void binhexFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void dosbinFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void UUencodeFile (const string &fname, const string &path,
GopherMenu &tmp);
+  void telnetFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void binFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void imageFile (const string &fname, const string &path, GopherMenu
&tmp);
+  void gifFile (const string &fname, const string &path, GopherMenu
&tmp);
+
+  MimeRecord* getMIME (string &filename);
+
+  string abs_path;
+  string hostname;
+  string port;
+  Vhost *Host;
+  HashMap<string,MyMethodPtr> handlers;
+
+  // Class Const
+};
+#endif
diff --git a/myserver/include/protocol/gopher/gopher.h
b/myserver/include/protocol/gopher/gopher.h
new file mode 100644
index 0000000..5df31be
--- /dev/null
+++ b/myserver/include/protocol/gopher/gopher.h
@@ -0,0 +1,102 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2002-2009 Free Software Foundation, Inc.
+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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef GOPHER_H
+# define GOPHER_H
+# include "stdafx.h"
+# include <include/protocol/protocol.h>
+# include <sstream>
+# include <include/protocol/gopher/gopher_content.h>
+
+# include <string>
+# include <vector>
+# include <memory>
+
+class Gopher : public Protocol
+{
+public:
+  Gopher ();
+  virtual ~Gopher ();
+  
+  virtual char* registerName (char* out,int len)
+  { 
+    return registerNameImpl (out, len); 
+  }
+
+  static char* registerNameImpl (char*, int len);
+
+  int controlConnection (ConnectionPtr a,
+                         char *b1,
+                         char *b2,
+                         int bs1,
+                         int bs2,
+                         u_long nbtr,
+                         u_long id);
+  
+
+  static int loadProtocolStatic ();
+  static int unLoadProtocolStatic ();
+
+  void reply (ConnectionPtr a,
+              GopherContent &data);
+};
+
+
+/*!
+ *Adapter class to make Gopher reentrant.
+ */
+
+class GopherProtocol : public Protocol
+{
+public:
+  GopherProtocol ()
+  {
+    protocolOptions = 0;
+  }
+
+  virtual ~GopherProtocol ()
+  {}
+
+  virtual char* registerName (char* out, int len)
+  {
+    return Gopher::registerNameImpl (out, len);
+  }
+
+  virtual int controlConnection (ConnectionPtr a, char *b1, char *b2,
+                                 int bs1, int bs2, u_long nbtr, u_long
id)
+  {
+    Gopher Gopher;
+    int ret = 0;
+
+    ret = Gopher.controlConnection (a, b1, b2, bs1, bs2, nbtr, id);
+
+    return ret;
+  }
+
+  virtual int loadProtocol ()
+  {
+    return Gopher::loadProtocolStatic ();
+  }
+
+  virtual int unLoadProtocol ()
+  {
+    return Gopher::unLoadProtocolStatic ();
+  }
+};
+
+#endif
diff --git a/myserver/include/protocol/gopher/gopher_content.h
b/myserver/include/protocol/gopher/gopher_content.h
new file mode 100644
index 0000000..1e4d5af
--- /dev/null
+++ b/myserver/include/protocol/gopher/gopher_content.h
@@ -0,0 +1,266 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2002-2009 Free Software Foundation, Inc.
+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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef GOPHER_CONTENT_H
+#define GOPHER_CONTENT_H
+
+#include "stdafx.h"
+#include <include/base/socket/socket.h>
+#include <include/base/mem_buff/mem_buff.h>
+#include <include/base/file/file.h>
+#include <include/connection/connection.h>
+#include <vector>
+#include <string>
+
+using namespace std;
+
+class GopherContent
+{
+public:
+  virtual void toProtocol (Socket *s) = 0;
+};
+
+class GopherRequest
+{
+public:
+  GopherRequest ();
+  GopherRequest (string s){ data = s; } 
+  GopherRequest (string s, ConnectionPtr conn)
+  {
+    data = s;
+    baseReq = conn;
+  }
+  void setConnection (ConnectionPtr conn){ baseReq = conn; }
+  string getRequest (){ return data; }
+  ConnectionPtr getConnection (){ return baseReq; }
+protected:
+  string data;
+  ConnectionPtr baseReq;
+};
+
+class GopherItem: public GopherContent
+{
+ public:
+  GopherItem ();
+  GopherItem (char, const char[], const char[], const char[], const
char[]);
+
+  ~GopherItem ();
+
+  void Type (char T){ type=T; }
+  char Type (){ return type; }
+  void Desc (string D){ desc=D; }
+  string Desc (){ return desc; }
+  void Rlocation (string R){ rlocation=R; }
+  string Rlocation (){ return rlocation; }
+  void Host (string H){ host = H; }
+  string Host (){ return host; }
+  void Port (string P){ port=P; }
+  string Port (){ return port; }
+
+  void toProtocol (Socket *s);
+
+ protected:
+  char type;
+  string desc;
+  string rlocation;
+  string host;
+  string port;
+
+  void init (char, const char[], const char[], const char[], const
char[]);
+
+};
+
+class GopherMenu: public GopherContent
+{
+public:
+  void addIntro (string tmp, string host, string port);
+  void addItem (GopherItem data);
+
+  void toProtocol (Socket *s);
+
+private:
+  vector<GopherItem> Items;
+
+};
+
+
+class GopherInfo: public GopherItem
+{
+public:
+  GopherInfo ();
+  GopherInfo (const char[], const char[], const char[], const char[]);
+  ~GopherInfo ();
+
+  void Type (char T){ type='i'; }
+};
+
+class GopherFile: public GopherItem
+{
+public:
+  GopherFile ();
+  GopherFile (const char[], const char[], const char[], const char[]);
+  ~GopherFile ();
+
+  void Type (char T){ type='0'; }
+};
+
+class GopherDirectory: public GopherItem
+{
+public:
+  GopherDirectory ();
+  GopherDirectory (const char[], const char[], const char[], const
char[]);
+  ~GopherDirectory ();
+
+  void Type (char T){ type='1'; }
+};
+
+class GopherCSO: public GopherItem
+{
+public:
+  GopherCSO ();
+  GopherCSO (const char[], const char[], const char[], const char[]);
+  ~GopherCSO ();
+
+  void Type (char T){ type='2'; }
+};
+class GopherError: public GopherItem
+{
+public:
+  GopherError ();
+  GopherError (const char[], const char[], const char[], const char[]);
+  ~GopherError ();
+
+  void Type (char T){ type='3'; }
+};
+
+class GopherMacBinHex: public GopherItem
+{
+public:
+  GopherMacBinHex ();
+  GopherMacBinHex (const char[], const char[], const char[], const
char[]);
+  ~GopherMacBinHex ();
+
+  void Type (char T){ type='4'; }
+};
+
+class GopherDosBin: public GopherItem
+{
+public:
+  GopherDosBin ();
+  GopherDosBin (const char[], const char[], const char[], const
char[]);
+  ~GopherDosBin ();
+
+  void Type (char T){ type='5'; }
+};
+
+class GopherUnixUUencode: public GopherItem
+{
+public:
+  GopherUnixUUencode ();
+  GopherUnixUUencode (const char[], const char[], const char[], const
char[]);
+  ~GopherUnixUUencode ();
+
+  void Type (char T){ type='6'; }
+};
+
+class GopherSearchServer: public GopherItem
+{
+public:
+  GopherSearchServer ();
+  GopherSearchServer (const char[], const char[], const char[], const
char[]);
+  ~GopherSearchServer ();
+
+  void Type (char T){ type='7'; }
+};
+
+class GopherTelnet: public GopherItem
+{
+public:
+  GopherTelnet ();
+  GopherTelnet (const char[], const char[], const char[], const
char[]);
+  ~GopherTelnet ();
+
+  void Type (char T){ type='8'; }
+};
+
+class GopherBin: public GopherItem
+{
+public:
+  GopherBin ();
+  GopherBin (const char[], const char[], const char[], const char[]);
+  ~GopherBin ();
+
+  void Type (char T){ type='9'; }
+};
+
+class GopherRendundantServer: public GopherItem
+{
+public:
+  GopherRendundantServer ();
+  GopherRendundantServer (const char[], const char[], const char[],
const char[]);
+  ~GopherRendundantServer ();
+
+  void Type (char T){ type='+'; }
+};
+
+class GopherGif: public GopherItem
+{
+public:
+  GopherGif ();
+  GopherGif (const char[], const char[], const char[], const char[]);
+  ~GopherGif ();
+
+  void Type (char T){ type='g'; }
+};
+
+class GopherImage: public GopherItem
+{
+public:
+  GopherImage ();
+  GopherImage (const char[], const char[], const char[], const char[]);
+  ~GopherImage ();
+
+  void Type (char T){ type='I'; }
+};
+
+class GopherData: public GopherContent
+{
+public:
+  GopherData (string);
+
+  void toProtocol (Socket *s);
+
+private:
+  string buff;
+};
+
+
+class GopherFileContent: public GopherContent
+{
+public:
+  GopherFileContent (string s);
+  ~GopherFileContent ();
+
+  void toProtocol (Socket *s);
+
+private:
+  File *sfile;
+  MemBuf buf;
+};
+
+#endif
diff --git a/myserver/src/protocol/Makefile.am
b/myserver/src/protocol/Makefile.am
index 0917b3b..0f26c60 100644
--- a/myserver/src/protocol/Makefile.am
+++ b/myserver/src/protocol/Makefile.am
@@ -1,7 +1,7 @@
 lib_LIBRARIES = libprotocol.a
 libprotocol_a_SOURCES = protocol_buffer.cpp protocol.cpp
protocols_manager.cpp \
                        url.cpp
-SUBDIRS = control ftp http https
+SUBDIRS = control ftp gopher http https
 AM_CPPFLAGS = $(all_includes)
 
 install:
diff --git a/myserver/src/protocol/gopher/Makefile.am
b/myserver/src/protocol/gopher/Makefile.am
new file mode 100644
index 0000000..a952fe3
--- /dev/null
+++ b/myserver/src/protocol/gopher/Makefile.am
@@ -0,0 +1,3 @@
+lib_LIBRARIES = libgopher.a
+libgopher_a_SOURCES = gopher.cpp gopher_content.cpp engine.cpp
+AM_CPPFLAGS = $(all_includes)
diff --git a/myserver/src/protocol/gopher/engine.cpp
b/myserver/src/protocol/gopher/engine.cpp
new file mode 100644
index 0000000..67f110f
--- /dev/null
+++ b/myserver/src/protocol/gopher/engine.cpp
@@ -0,0 +1,288 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2002-2009 Free Software Foundation, Inc.
+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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <include/protocol/gopher/gopher_content.h>
+#include <include/protocol/gopher/engine.h>
+#include <include/conf/vhost/vhost.h>
+#include <include/server/server.h>
+#include <include/base/read_directory/read_directory.h>
+#include <include/base/file/files_utility.h>
+#include <include/base/file/file.h>
+#include <include/base/mem_buff/mem_buff.h>
+
+
+#include <iostream>
+#include <fstream>
+#include <sstream>
+
+GopherEngine::GopherEngine()
+{
+  string info = "info";
+  string cso = "cso";
+  string binhex = "binhex";
+  string dosbin = "dosbin";
+  string UUencode = "UUencode";
+  string telnet = "telnet";
+  string bin = "bin";
+  string image = "image";
+  string gif = "gif";
+  string text = "text";
+  handlers.put (info, &GopherEngine::infoFile);
+  handlers.put (text, &GopherEngine::textFile);
+  handlers.put (cso, &GopherEngine::csoFile);
+  handlers.put (binhex, &GopherEngine::binhexFile);
+  handlers.put (dosbin, &GopherEngine::dosbinFile);
+  handlers.put (UUencode, &GopherEngine::UUencodeFile);
+  handlers.put (telnet, &GopherEngine::telnetFile);
+  handlers.put (bin, &GopherEngine::binFile);
+  handlers.put (image, &GopherEngine::imageFile);
+  handlers.put (gif, &GopherEngine::gifFile);
+}
+          
+GopherContent &GopherEngine::incoming(GopherRequest req, Vhost *host)
+{
+  ReadDirectory fd;
+  GopherContent *Gud;
+  GopherMenu *Gu;
+  stringstream Port;
+  Port << host->getPort ();
+  port = Port.str ();
+  hostname = req.getConnection ()->getLocalIpAddr ();
+  abs_path = host->getDocumentRoot ();
+  Host = host;
+  
+  string path = abs_path;
+  path.append ("/");
+  path.append (req.getRequest ());
+  if(! fd.findfirst (path.c_str ()))
+    {
+      Gu = new GopherMenu;
+      if(FilesUtility::getPathRecursionLevel (path)>0)
+        {
+          do {
+            if(fd.name[0]!='.')
+              {
+                if (fd.attrib & FILE_ATTRIBUTE_DIRECTORY)
+                  dirManagement (fd.name, req.getRequest (), *Gu);
+                else
+                  fileManagement (fd.name, req.getRequest (),
*Gu);        
+              }
+          }while(!fd.findnext ());
+        }
+      else
+        Gu->addIntro(string(_("Invalid requested Path\n")), hostname,
port);
+      fd.findclose ();
+    }
+  else
+    {
+      Gud = (GopherContent*)new GopherFileContent (path);
+      return *Gud;
+    }
+  return *Gu;
+}
+
+void GopherEngine::dirManagement (const string &fname, const string
&path, GopherMenu &tmp)
+{
+  string file_path = path;
+  file_path.append ("/");
+  file_path.append (fname);
+  tmp.addItem (GopherDirectory (fname.c_str (),
+        file_path.c_str (),
+        hostname.c_str (),
+        port.c_str ()));
+}
+
+void GopherEngine::fileManagement (const string &fname, const string
&path,GopherMenu &tmp)
+{
+  string ext;
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  string abs_name = abs_path;
+  if (abs_name != "")
+    abs_name.append ("/");
+  abs_name.append (f_path);
+
+  FilesUtility::getFileExt (ext,fname);
+  if (handlers.containsKey (ext.c_str ()))
+    (this->*handlers.get(ext.c_str ())) (fname, path, tmp);
+  else
+    {
+      MimeRecord *mime = getMIME (abs_name);
+      if (mime!=NULL)
+        {    
+          string mymime = mime->mimeType;
+          mymime = mymime.substr (0,mymime.find ('/',0));
+          if (handlers.containsKey (mymime.c_str ()))
+            (this->*handlers.get (mymime.c_str ()))(fname, path, tmp);
+        }
+      else
+        tmp.addItem (GopherFile (fname.c_str (),
+                                 f_path.c_str (),
+                                 hostname.c_str () ,
+                                 port.c_str ()));
+    }
+}
+
+/*!
+ * Returns the MIME type passing its extension.
+ * Returns zero if the file is registered.
+ */
+MimeRecord* GopherEngine::getMIME (string &filename)
+{
+  const char *handler = NULL;
+  
+  if (Host->isMIME ())
+    return Host->getMIME ()->getMIME (filename);
+  
+  return Server::getInstance ()->getMimeManager ()->getMIME (filename,
handler);
+}
+
+void GopherEngine::textFile(const string &fname, const string
&path,GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherFile (fname.c_str (),
+                           f_path.c_str (),
+                           hostname.c_str () ,
+                           port.c_str ()));
+}
+void GopherEngine::csoFile(const string &fname, const string
&path,GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherCSO (fname.c_str (),
+                          f_path.c_str (),
+                          hostname.c_str () ,
+                          port.c_str ()));
+}
+
+void GopherEngine::binhexFile (const string &fname, const string &path,
GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherMacBinHex (fname.c_str (),
+                                f_path.c_str (),
+                                hostname.c_str () ,
+                                port.c_str ()));
+}
+
+void GopherEngine::dosbinFile (const string &fname, const string &path,
GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherDosBin (fname.c_str (),
+                             f_path.c_str (),
+                             hostname.c_str () ,
+                             port.c_str ()));
+}
+
+void GopherEngine::UUencodeFile (const string &fname, const string
&path, GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherUnixUUencode (fname.c_str (),
+                                   f_path.c_str (),
+                                   hostname.c_str () ,
+                                   port.c_str ()));
+}
+
+void GopherEngine::telnetFile (const string &fname, const string &path,
GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherTelnet (fname.c_str (),
+                             f_path.c_str (),
+                             hostname.c_str () ,
+                             port.c_str ()));
+}
+void GopherEngine::binFile (const string &fname, const string &path,
GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherBin (fname.c_str (),
+                          f_path.c_str (),
+                          hostname.c_str () ,
+                          port.c_str ()));
+}
+
+void GopherEngine::imageFile (const string &fname, const string &path,
GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherImage (fname.c_str (),
+                            f_path.c_str (),
+                            hostname.c_str () ,
+                            port.c_str ()));
+}
+
+void GopherEngine::gifFile (const string &fname, const string &path,
GopherMenu &tmp)
+{
+  string f_path = path;
+  if (f_path != "")
+    f_path.append ("/");
+  f_path.append (fname);
+  tmp.addItem (GopherGif (fname.c_str (),
+                          f_path.c_str (),
+                          hostname.c_str () ,
+                          port.c_str ()));
+}
+
+void GopherEngine::infoFile (const string &fname, const string
&path,GopherMenu &tmp)
+{
+  string file_path;
+  file_path.append (abs_path);
+  file_path.append ("/");
+  file_path.append (path);
+  file_path.append ("/");
+  file_path.append (fname);
+  
+  File f;
+  int state;
+  u_long fsize;
+  state = f.openFile (file_path.c_str (), File::READ);
+  if(!state)
+    {
+      MemBuf buff;
+      u_long real;
+      int sts;
+      fsize = f.getFileSize ();
+      buff.setLength (fsize);
+      f.read (buff.getBuffer (), fsize, &real);
+      if(real == fsize)
+        tmp.addIntro (buff.getBuffer (), hostname, port);
+      f.close ();
+    }
+}
diff --git a/myserver/src/protocol/gopher/gopher.cpp
b/myserver/src/protocol/gopher/gopher.cpp
new file mode 100644
index 0000000..bcc91db
--- /dev/null
+++ b/myserver/src/protocol/gopher/gopher.cpp
@@ -0,0 +1,109 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2002-2009 Free Software Foundation, Inc.
+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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+
+#include <iostream>
+
+
+#include <include/protocol/gopher/gopher.h>
+#include <include/protocol/gopher/gopher_content.h>
+#include <include/protocol/gopher/engine.h>
+#include <include/server/server.h>
+#include <include/server/clients_thread.h>
+#include <include/base/string/securestr.h>
+
+
+/*!
+ * Constructor for the Gopher class
+ */
+
+Gopher::Gopher ()
+{}
+
+/*!
+ * Destructor for the Gopher class.
+ */
+Gopher::~Gopher ()
+{}
+
+/*!
+ * Service method
+ */
+
+char *Gopher::registerNameImpl (char* out, int len)
+{
+  if (out)
+    {
+      myserver_strlcpy (out, "GOPHER", len);
+    }
+  return (char*) "GOPHER";
+}
+
+/*!
+ * The loading unloading routine
+ */
+
+int Gopher::loadProtocolStatic ()
+{}
+
+int Gopher::unLoadProtocolStatic ()
+{
+  return 1;
+}
+
+/*!
+ * Main protocol implementation stuff 
+ */
+
+int Gopher::controlConnection (ConnectionPtr pConnection,
+                               char *b1,
+                               char *b2,
+                               int bs1,
+                               int bs2,
+                               u_long nbtr,
+                               u_long id)
+{
+  char buffer[256];
+  u_long nbr;
+  GopherEngine g;
+  
+  if (pConnection == NULL)
+    return ClientsThread::DELETE_CONNECTION;
+  Server *server = Server::getInstance ();
+  if (server == NULL)
+    return ClientsThread::DELETE_CONNECTION;
+  string command (b1);
+  string::size_type loc = command.find ('\n',0);
+  while( loc == string::npos ){
+    pConnection->socket->read (buffer, sizeof (buffer), &nbr);
+    command+=buffer;
+    loc = command.find ('\n',0);
+  }
+  GopherContent &Gu = g.incoming (GopherRequest (command.substr (0,
+
((int)loc)-1),
+                                                 pConnection)
+                                  ,pConnection->host);
+  reply (pConnection,Gu);
+}
+
+void Gopher::reply (ConnectionPtr pConnection,
+                    GopherContent &data)
+{
+  data.toProtocol (pConnection->socket);
+  pConnection->socket->close ();
+}
diff --git a/myserver/src/protocol/gopher/gopher_content.cpp
b/myserver/src/protocol/gopher/gopher_content.cpp
new file mode 100644
index 0000000..e9ff615
--- /dev/null
+++ b/myserver/src/protocol/gopher/gopher_content.cpp
@@ -0,0 +1,363 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 2002-2009 Free Software Foundation, Inc.
+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 3 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <iostream>
+#include <sstream>
+#include <string>
+
+#include <include/protocol/gopher/gopher_content.h>
+#include <include/base/string/stringutils.h>
+
+
+void GopherMenu::addIntro (string freeText, string host, string port)
+{
+  size_t size = freeText.size ();
+  char myline[size];
+  vector<GopherItem> tmp;
+  istream *res;
+  istringstream str (freeText);
+  while (!str.getline (myline, size).eof ())
+    {
+      tmp.push_back (GopherInfo (myline,
+                                 "(None)", 
+                                 host.c_str (),
+                                 port.c_str ()));
+    }
+  tmp.push_back (GopherInfo (myline,
+                             "(None)", 
+                             host.c_str (),
+                             port.c_str ()));
+  Items.insert (Items.begin (),tmp.begin (), tmp.end ());
+}
+                            
+void GopherMenu::addItem (GopherItem i)
+{
+  Items.push_back (i);
+}
+
+void GopherMenu::toProtocol (Socket *s)
+{
+  for(int i=0;i<Items.size ();i++)
+    {
+      Items.at (i).toProtocol (s);
+    }
+  s->send (".\n", 3, 0);
+}
+
+GopherItem::GopherItem (char t,
+      const char d[],
+      const char loc[],
+      const char h[],
+      const char p[])
+{
+  init (t, d, loc, h, p);
+}
+          
+GopherItem::GopherItem ()
+{
+  init(0, "", "", "", "");
+}
+
+void GopherItem::init (char t,
+           const char d[],
+           const char loc[],
+           const char h[],
+           const char p[])
+{
+  Type (t);
+  Desc (d);
+  Rlocation (loc);
+  Host (h);
+  Port (p);
+}
+
+GopherItem::~GopherItem ()
+{}
+
+void GopherItem::toProtocol (Socket *s)
+{
+  string res;
+  res = "-";
+  res[0]=type;
+  res.append (desc);
+  res.append ("\t");
+  res.append (rlocation);
+  res.append ("\t");
+  res.append (host);
+  res.append ("\t");
+  res.append (port);
+  res.append ("\t\n");
+  s->send (res.c_str (), res.length (), 0);
+}
+
+GopherInfo::GopherInfo ()
+{
+  init ('i', "", "", "", "");
+}
+
+GopherInfo::GopherInfo (const char d[],
+      const char loc[],
+      const char h[],
+      const char p[])
+{
+  init ('i', d, loc, h, p);
+}
+
+GopherInfo::~GopherInfo (){}
+
+GopherFile::GopherFile ()
+{
+  init ('0', "", "", "", "");
+}
+
+GopherFile::GopherFile (const char d[],
+            const char loc[],
+      const char h[],
+      const char p[])
+{
+  init ('0', d, loc, h, p);
+}
+
+GopherFile::~GopherFile (){}
+
+
+
+GopherDirectory::GopherDirectory ()
+{
+  init ('1', "", "", "", "");
+}
+
+GopherDirectory::GopherDirectory (const char d[],
+          const char loc[],
+          const char h[],
+          const char p[])
+{
+  init ('1', d, loc, h, p);
+}
+
+GopherDirectory::~GopherDirectory ()
+{}
+
+GopherCSO::GopherCSO ()
+{
+  init ('2', "", "", "", "");
+}
+
+GopherCSO::~GopherCSO ()
+{}
+
+GopherCSO::GopherCSO (const char d[],
+                      const char loc[],
+                      const char h[],
+                      const char p[])
+{
+  init ('2', d, loc, h, p);
+}
+
+GopherError::GopherError ()
+{
+  init ('3', "", "", "", "");
+}
+
+GopherError::~GopherError ()
+{}
+
+GopherError::GopherError (const char d[],
+                          const char loc[],
+                          const char h[],
+                          const char p[])
+{
+  init ('3', d, loc, h, p);
+}
+
+GopherMacBinHex::GopherMacBinHex ()
+{
+  init ('4', "", "", "", "");
+}
+
+GopherMacBinHex::~GopherMacBinHex ()
+{}
+
+GopherMacBinHex::GopherMacBinHex (const char d[],
+                                  const char loc[],
+                                  const char h[],
+                                  const char p[])
+{
+  init ('4', d, loc, h, p);
+}
+
+GopherDosBin::GopherDosBin ()
+{
+  init('5', "", "", "", "");
+}
+
+GopherDosBin::~GopherDosBin ()
+{}
+
+GopherDosBin::GopherDosBin (const char d[],
+                            const char loc[],
+                            const char h[],
+                            const char p[])
+{
+  init ('5', d, loc, h, p);
+}
+
+GopherUnixUUencode::GopherUnixUUencode ()
+{
+  init ('6', "", "", "", "");
+}
+
+GopherUnixUUencode::~GopherUnixUUencode ()
+{}
+
+GopherUnixUUencode::GopherUnixUUencode (const char d[],
+                                        const char loc[],
+                                        const char h[],
+                                        const char p[])
+{
+  init ('6', d, loc, h, p);
+}
+
+GopherSearchServer::GopherSearchServer ()
+{
+  init ('7', "", "", "", "");
+}
+
+GopherSearchServer::~GopherSearchServer ()
+{}
+
+GopherSearchServer::GopherSearchServer (const char d[],
+                                        const char loc[],
+                                        const char h[],
+                                        const char p[])
+{
+  init ('7', d, loc, h, p);
+}
+
+GopherTelnet::GopherTelnet ()
+{
+  init ('8', "", "", "", "");
+}
+
+GopherTelnet::~GopherTelnet ()
+{}
+
+GopherTelnet::GopherTelnet (const char d[],
+                            const char loc[],
+                            const char h[],
+                            const char p[])
+{
+  init ('8', d, loc, h, p);
+}
+
+GopherBin::GopherBin ()
+{
+  init ('9', "", "", "", "");
+}
+
+GopherBin::~GopherBin ()
+{}
+
+GopherBin::GopherBin (const char d[],
+                      const char loc[],
+                      const char h[],
+                      const char p[])
+{
+  init ('9', d, loc, h, p);
+}
+
+GopherRendundantServer::GopherRendundantServer ()
+{
+  init (0, "", "", "", "");
+}
+
+GopherRendundantServer::~GopherRendundantServer ()
+{}
+
+GopherRendundantServer::GopherRendundantServer (const char d[],
+                                                const char loc[],
+                                                const char h[],
+                                                const char p[])
+{
+  init (0, d, loc, h, p);
+}
+
+GopherGif::GopherGif ()
+{
+  init ('g', "", "", "", "");
+}
+
+GopherGif::~GopherGif ()
+{}
+
+GopherGif::GopherGif (const char d[],
+                      const char loc[],
+                      const char h[],
+                      const char p[])
+{
+  init ('g', d, loc, h, p);
+}
+
+GopherImage::GopherImage ()
+{
+  init ('g', "", "", "", "");
+}
+
+GopherImage::~GopherImage ()
+{}
+
+GopherImage::GopherImage (const char d[],
+                          const char loc[],
+                          const char h[],
+                          const char p[])
+{
+  init ('I', d, loc, h, p);
+}
+
+
+GopherData::GopherData (string data)
+{
+  buff = data;
+}
+
+void GopherData::toProtocol (Socket *s)
+{
+  string res = buff;
+  res.append ("\r\n.\r\n");
+  s->send (res.c_str (), res.length (), 0);
+}
+
+
+GopherFileContent::GopherFileContent (string fnome)
+{
+  sfile = new File ();
+  sfile->openFile (fnome.c_str (), File::READ);
+  buf.setLength (4096);
+}
+
+GopherFileContent::~GopherFileContent ()
+{
+  sfile->close ();
+  delete sfile;
+}
+
+void GopherFileContent::toProtocol (Socket *s)
+{
+  u_long tmp;
+  sfile->fastCopyToSocket (s, 0, &buf, &tmp);
+}
diff --git a/myserver/src/server/server.cpp
b/myserver/src/server/server.cpp
index 3b0c29a..da88639 100644
--- a/myserver/src/server/server.cpp
+++ b/myserver/src/server/server.cpp
@@ -22,6 +22,7 @@ along with this program.  If not, see
<http://www.gnu.org/licenses/>.
 #include <include/protocol/http/http.h>
 #include <include/protocol/https/https.h>
 #include <include/protocol/control/control_protocol.h>
+#include <include/protocol/gopher/gopher.h>
 #include <include/protocol/ftp/ftp.h>
 #include <include/base/string/stringutils.h>
 #include <include/base/socket/socket.h>
@@ -360,6 +361,7 @@ void Server::loadPlugins ()
 
   Protocol *protocolsSet[] = {new HttpProtocol (),
                               new HttpsProtocol (),
+                              new GopherProtocol (),
                               new FtpProtocol (),
                               new ControlProtocol (),
                               NULL};
-- 
1.6.3.3







reply via email to

[Prev in Thread] Current Thread [Next in Thread]