00001
00002
00003 #include "Iguana/WebFramework/interface/IgStudioWebService.h"
00004 #include "Iguana/WebFramework/interface/IgWebServiceDB.h"
00005 #include "Iguana/WebFramework/interface/IgSocketDriverInfo.h"
00006 #include "Iguana/WebFramework/interface/IgServerSocket.h"
00007 #include "Iguana/WebFramework/interface/IgFileManager.h"
00008 #include "Iguana/WebFramework/interface/IgSocket.h"
00009 #include "Iguana/Framework/interface/IgArgsElement.h"
00010 #include <qfile.h>
00011 #include <qiodevice.h>
00012 #include <qcstring.h>
00013 #include <qstringlist.h>
00014 #include <qregexp.h>
00015 #include <qstring.h>
00016 #include <qapplication.h>
00017 #include <string>
00018 #include <map>
00019 #include <unistd.h>
00020 #include <pthread.h>
00021 #include <stdlib.h>
00022 #include <iostream>
00023 #include <sys/socket.h>
00024 #include <signal.h>
00025 #undef emit
00026 #include <classlib/utils/Callback.h>
00027 #include <classlib/utils/StringOps.h>
00028 #include <classlib/utils/DebugAids.h>
00029 #include <classlib/sysapi/SubProcess.h>
00030 #include <classlib/utils/Argz.h>
00031 #include <classlib/utils/Log.h>
00032 #include <classlib/iobase/Filename.h>
00033 #include <unistd.h>
00034
00035
00036
00037
00038
00039
00040
00041 lat::logflag LFstudioWeb = { 0, "studioWeb", true, -1 };
00042
00043
00044
00045
00046
00047
00048 IgStudioWebService::IgStudioWebService (IgState *state)
00049 : IgWebService (state)
00050 {
00051
00052 getFileManager ()->addPaths ("/share/htdocs/Ig_Modules/IgWebStudio/html/",
00053 true);
00054 getFileManager ()->addPaths ("/src/Ig_Modules/IgWebStudio/images/", true);
00055 getFileManager ()->addPaths ("/src/Ig_Modules/IgWebStudio/html/", true);
00056
00057 registerCallback ("initSession", lat::CreateCallback (this, &IgStudioWebService::initSession));
00058 registerCallback ("login", lat::CreateCallback (this, &IgStudioWebService::login));
00059 registerCallback ("logout", lat::CreateCallback (this, &IgStudioWebService::logout));
00060 registerCallback ("launch", lat::CreateCallback (this, &IgStudioWebService::launch));
00061 }
00062
00063 void
00064 IgStudioWebService::login (Arguments *arguments)
00065 {
00066 QIODevice *outputDevice = arguments->outputDevice ();
00067 IgState *state = arguments->state ();
00068 QTextStream os (outputDevice);
00069 LOG (0, trace, LFstudioWeb, "Cookie: "
00070 << arguments->idCookie () << std::endl);
00071
00072 IgArgsElement *args = IgArgsElement::get (state);
00073 if (args->exists ("--demo"))
00074 {
00075 os << "HTTP/1.1 200 Ok\r\n"
00076 << "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
00077 << "<html>"
00078 << "<head><title>DEMO IN PROGRESS</title></head>"
00079 << "<body><H1>DEMO IN PROGRESS</H1>In order to avoid "
00080 << "interference with the demo, external users cannot login "
00081 << "to the IGUANA server.<br/><br/>"
00082 << "You are kindly requested to look at the speaker.</body>"
00083 << "</html>\r\n";
00084 }
00085 else
00086 os << IgServerSocket::redirect (IgServerSocket::cmdLineProxy() + "/Studio/initSession", "", "");
00087 }
00088
00089 void
00090 IgStudioWebService::logout (Arguments *arguments)
00091 {
00092 QIODevice *outputDevice = arguments->outputDevice ();
00093 QTextStream os (outputDevice);
00094 LOG (0, trace, LFstudioWeb, "Cookie: "
00095 << arguments->idCookie () << std::endl);
00096
00097 os << "HTTP/1.1 200 Ok\r\n"
00098 << "Content-Type: text/html; charset=\"utf-8\"\r\n\r\n"
00099 << "<html><head><title>IGUANA Web Server</title></head><body>"
00100 <<"<H2>IGUANA Server Logout</H2>Thanks a lot for using IGUANA Web Server. "
00101 <<"Please do visit <A HREF=\""<<IgServerSocket::cmdLineProxy()<<"\"> IGUANA server</A> again.<br/><br/>"
00102 <<"<b>--IGUANA Team</b></body></html>\r\n";
00103 outputDevice->close();
00104 outputDevice->flush();
00105 exit (0);
00106 }
00107
00108 void
00109 IgStudioWebService::launch (Arguments *arguments)
00110 {
00111 QIODevice *outputDevice = arguments->outputDevice ();
00112 ArgumentsMap &args = *(arguments->args ());
00113 QTextStream os (outputDevice);
00114
00115 if (args.find ("script") == args.end ())
00116 {
00117 os << IgServerSocket::redirect (IgServerSocket::cmdLineProxy() + "/Studio/login", "", "");
00118 return;
00119 }
00120
00121 if (m_sessionUrl.empty ())
00122 m_sessionUrl = args["script"];
00123
00124 os << IgServerSocket::redirect (m_sessionUrl, "", "");
00125 }
00126
00127 void
00128 IgStudioWebService::initSession (Arguments *arguments)
00129 {
00130 QIODevice *outputDevice = arguments->outputDevice ();
00131 QTextStream os (outputDevice);
00132 LOG (0, trace, LFstudioWeb, "Cookie: "
00133 << arguments->idCookie () << std::endl);
00134
00135 if (! m_sessionUrl.empty ())
00136 {
00137 os << IgServerSocket::redirect(m_sessionUrl, "", "");
00138 return;
00139 }
00140
00141 IgSocket *socket = dynamic_cast<IgSocket *>(outputDevice);
00142 bool validUserAgent = true;
00143 std::string userAgent ("");
00144 if(socket)
00145 {
00146 userAgent = socket->userAgent();
00147 QStringList agents = QStringList::split (QRegExp ("[ \n\r][ \n\r]*"),
00148 userAgent.c_str());
00149 for(int i=0; i<agents.size();i++)
00150 {
00151 if(agents[i]=="MSIE")
00152 {
00153 validUserAgent=false;
00154 break;
00155 }
00156 }
00157 }
00158
00159 IgWebServiceDB *wsdb = IgWebServiceDB::get ();
00160 std::string proxy = IgServerSocket::cmdLineProxy ();
00161
00162 os << "HTTP/1.0 200 Ok\r\n"
00163 "Content-Type: text/html; charset=\"utf-8\"\r\n"
00164 "\r\n";
00165
00166 os << "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\n"
00167 "\"https://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"
00168 "<html xmlns=\"https://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n"
00169 "<head>\n"
00170 "<title>IGUANA WEB STUDIO</title>"
00171 "<link rel=\"stylesheet\" type=\"text/css\" href=\""+proxy+"/Studio/getScript?name=main.css&type=text/css\"/>\n"
00172 "</head><body>\n"
00173 "<div style=\"display: none;\">Welcome user " + arguments->idCookie () + "</div>"
00174 "<div class=\"main\"><div align=right><a href=\""<<proxy<<"/Studio/logout\">Logout</a></div>"
00175 "<div class=\"title\">IGUANA WEB INTERFACE</div><table><tr>";
00176 int j = 0;
00177
00178 for (IgWebServiceDB::Iterator i = wsdb->begin ();
00179 i != wsdb->end ();
00180 i++)
00181 {
00182 if (j > 4)
00183 {
00184 j = 0;
00185 os << "</tr><tr>";
00186 }
00187
00188 using namespace lat;
00189 std::map <std::string, std::string> pluginOpts;
00190
00191 StringList pluginInfo = StringOps::split ((*i)->name (), "?");
00192 if (pluginInfo.size () == 2)
00193 {
00194 StringList keyValues = StringOps::split (pluginInfo [1], "&");
00195 for (StringList::iterator i = keyValues.begin ();
00196 i != keyValues.end ();
00197 i++)
00198 {
00199 StringList keyValue = StringOps::split ((*i), "=");
00200 if (keyValue.size () == 2)
00201 {
00202 typedef std::pair <std::string, std::string> MapPair;
00203 pluginOpts.insert (MapPair (keyValue[0], keyValue [1]));
00204 }
00205 }
00206
00207 if (pluginOpts.find ("app") != pluginOpts.end ())
00208 {
00209 os << "<td><a href=\""<<proxy<< "/Studio/launch?script=" <<proxy<< pluginOpts["url"] << "\">"
00210 "<img src=\"" + pluginOpts["img"] + "\" alt=\"No Image\"/><br/>"
00211 + pluginOpts["name"] + "</a></td>";
00212 j++;
00213 }
00214 }
00215 }
00216 os << "</tr></table>";
00217 if(!validUserAgent)
00218 {
00219 os <<"<br><div class=\"warning\"><center><b>IGUANA Server Warning:</b></center>"
00220 "We are sorry but IGUANA server might not work properly for your broswer \"" + userAgent + "\".</div>\n";
00221 }
00222 os << "</div></body></html>\r\n";
00223 }
00224
00225
00226 const char *
00227 IgStudioWebService::catalogLabel (void)
00228 {
00229 return "Studio";
00230 }