Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

mhtml.cc

Go to the documentation of this file.
00001 
00025 #include "magic/mhtml.h"
00026 #include "magic/mclass.h"
00027 
00028 BEGIN_NAMESPACE (MagiC);
00029 
00030 decl_module (html);
00031 
00032 impl_dynamic (HTMLQuery, {Object});
00033 impl_dynamic (HTMLPageBase, {Object});
00034 
00036 
00037 // unquote
00038 
00039 void unquote (Map<String, String>& map) {
00040     for (MapIter<String,String> i (map); !i.exhausted(); i.next())
00041         i.value ().unquote ('%', String::QUOTE_HTML);
00042 }
00043 
00045 //                                  HTMLQuery
00046 
00047 HTMLQuery::HTMLQuery () {
00048     String contlen_tmp;
00049     contype     = getenv ("CONTENT_TYPE");
00050     contlen_tmp = getenv ("CONTENT_LENGTH");
00051     docroot     = getenv ("DOCUMENT_ROOT");
00052     gwif        = getenv ("GATEWAY_INTERFACE");
00053     hosttype    = getenv ("HOSTTYPE");
00054     httpaccpt   = getenv ("HTTP_ACCEPT");
00055     httppragma  = getenv ("HTTP_PRAGMA");
00056     useragent   = getenv ("HTTP_USER_AGENT");
00057     pathinfo    = getenv ("PATH_INFO");
00058     pathtrans   = getenv ("PATH_TRANSLATED");
00059     querystr    = getenv ("QUERY_STRING");
00060     remaddr     = getenv ("REMOTE_ADDR");
00061     remhost     = getenv ("REMOTE_HOST");
00062     method      = getenv ("REQUEST_METHOD");
00063     sptname     = getenv ("SCRIPT_NAME");
00064     srvname     = getenv ("SERVER_NAME");
00065     srvport     = 80; // atoi (getenv ("SERVER_PORT"));
00066     srvprot     = getenv ("SERVER_PROTOCOL");
00067     srvsoft     = getenv ("SERVER_SOFTWARE");
00068 
00069     contlen = contlen_tmp.isNull()? 0 : atoi (contlen_tmp);
00070 //  printf ("contlen=%d\n", (char*) contlen);
00071 
00072     if (method=="GET")
00073         splitpairs (query, querystr, '=', '&');
00074     else { // POST
00075         if (contlen > 0) {
00076             // Luetaan stdio:ta pituuden verran
00077             char* buff = new char [contlen+1];
00078             fgets (buff, contlen+1, stdin);
00079             querystr = buff;
00080             delete buff;
00081             splitpairs (query, querystr, '=', '&');
00082         }
00083     }
00084 
00085     // Konvertoidaan query-parametrien quotet
00086     unquote (query);
00087 
00088 //  query.print (cout);
00089 }
00090 
00091 void HTMLQuery::printhidden () const {
00092     forStringMap (query, i)
00093         printf ("<INPUT TYPE=HIDDEN NAME='%s' VALUE='%s'>\n",
00094                 (CONSTR) i.key (), (CONSTR) i.value ());
00095 }
00096 
00097 void HTMLQuery::any () {
00098     if (pathinfo.length()>5 && pathinfo.mid(pathinfo.length()-5) == ".html") {
00099         call (pathinfo.mid (1, pathinfo.length()-6)+String("_html"));
00100     } else
00101         printf ("Content-type: text/html\n\n"
00102                 "<H1>Invalid path '%s'</H1>\n"
00103                 "Page couldn't be found. Sorry!\n",
00104                 (CONSTR) pathinfo);
00105     
00106 }
00107 
00108 int HTMLQuery::call (const String& pagename) {
00109     HTMLPageBase* pageobj = (HTMLPageBase*) dyncreate (pagename);
00110     if (pageobj) {
00111         pageobj->printqry (*this);
00112         delete pageobj;
00113         return 0;
00114     } else {
00115         printf ("Content-type: text/html\n\n"
00116                 "<H1>Invalid page called '%s'</H1>\n"
00117                 "Page couldn't be found. Sorry!\n",
00118                 (CONSTR) pathinfo);
00119         return 1;
00120     }
00121 }
00122 
00123 END_NAMESPACE;
00124 

Generated on Thu Feb 10 20:06:41 2005 for LibMagiC by doxygen1.2.18