00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include <time.h>
00037 #include "mem_io_handler.h"
00038 #include "web_request_handler.h"
00039 #include "config_manager.h"
00040 #include "content_manager.h"
00041 #include "web/pages.h"
00042 #include "tools.h"
00043 #include "hash.h"
00044
00045 using namespace zmm;
00046 using namespace mxml;
00047
00048 WebRequestHandler::WebRequestHandler() : RequestHandler()
00049 {
00050 checkRequestCalled = false;
00051 params = Ref<Dictionary>(new Dictionary());
00052 }
00053
00054 int WebRequestHandler::intParam(String name, int invalid)
00055 {
00056 String value = param(name);
00057 if (!string_ok(value))
00058 return invalid;
00059 else
00060 return value.toInt();
00061 }
00062
00063 bool WebRequestHandler::boolParam(zmm::String name)
00064 {
00065 String value = param(name);
00066 return string_ok(value) && (value == "1" || value == "true");
00067 }
00068
00069 void WebRequestHandler::check_request(bool checkLogin)
00070 {
00071
00072
00073
00074
00075
00076 checkRequestCalled = true;
00077
00078 String sid = param(_("sid"));
00079 if (sid == nil)
00080 throw SessionException(_("no session id given"));
00081
00082 if ((session = SessionManager::getInstance()->getSession(sid)) == nil)
00083 throw SessionException(_("invalid session id"));
00084
00085 if (checkLogin && ! session->isLoggedIn())
00086 throw LoginException(_("not logged in"));
00087 session->access();
00088 }
00089
00090 String WebRequestHandler::renderXMLHeader()
00091 {
00092 return _("<?xml version=\"1.0\" encoding=\"") +
00093 DEFAULT_INTERNAL_CHARSET +"\"?>\n";
00094 }
00095
00096 void WebRequestHandler::get_info(IN const char *filename, OUT struct File_Info *info)
00097 {
00098 info->file_length = -1;
00099 info->last_modified = time(NULL);
00100 info->is_directory = 0;
00101 info->is_readable = 1;
00102
00103 String contentType;
00104
00105 String mimetype;
00106 String returnType = param(_("return_type"));
00107 if (string_ok(returnType) && returnType == "xml")
00108 mimetype = _(MIMETYPE_XML);
00109 else
00110 mimetype = _(MIMETYPE_JSON);
00111
00112 contentType = mimetype + "; charset=" + DEFAULT_INTERNAL_CHARSET;
00113
00114 info->content_type = ixmlCloneDOMString(contentType.c_str());
00115 info->http_header = ixmlCloneDOMString("Cache-Control: no-cache, must-revalidate");
00116 }
00117
00118 Ref<IOHandler> WebRequestHandler::open(IN enum UpnpOpenFileMode mode)
00119 {
00120 root = Ref<Element>(new Element(_("root")));
00121 out = Ref<StringBuffer>(new StringBuffer());
00122
00123 String error = nil;
00124 int error_code = 0;
00125
00126 String output;
00127
00128 try
00129 {
00130 if(!ConfigManager::getInstance()->getBoolOption(CFG_SERVER_UI_ENABLED))
00131 {
00132 log_warning("The UI is disabled in the configuration file. See README.\n");
00133 error = _("The UI is disabled in the configuration file. See README.");
00134 error_code = 900;
00135 }
00136 else
00137 {
00138 process();
00139
00140 if (checkRequestCalled)
00141 {
00142
00143 appendTask(root, ContentManager::getInstance()->getCurrentTask());
00144
00145 handleUpdateIDs();
00146 }
00147 }
00148 }
00149 catch (LoginException e)
00150 {
00151 error = e.getMessage();
00152 error_code = 300;
00153 }
00154 catch (ObjectNotFoundException e)
00155 {
00156 error = e.getMessage();;
00157 error_code = 200;
00158 }
00159 catch (SessionException e)
00160 {
00161 error = e.getMessage();
00162 error_code = 400;
00163 }
00164 catch (StorageException e)
00165 {
00166 error = e.getUserMessage();
00167 error_code = 500;
00168 e.printStackTrace();
00169 }
00170 catch (Exception e)
00171 {
00172 error = _("Error: ") + e.getMessage();
00173 error_code = 800;
00174 e.printStackTrace();
00175 }
00176
00177 if (! string_ok(error))
00178 {
00179 root->setAttribute(_("success"), _("1"), mxml_bool_type);
00180 }
00181 else
00182 {
00183 root->setAttribute(_("success"), _("0"), mxml_bool_type);
00184 Ref<Element> errorEl(new Element(_("error")));
00185 errorEl->setTextKey(_("text"));
00186 errorEl->setText(error);
00187
00188 if (error_code == 0)
00189 error_code = 899;
00190 errorEl->setAttribute(_("code"), String::from(error_code));
00191 root->appendElementChild(errorEl);
00192 }
00193
00194 String returnType = param(_("return_type"));
00195 if (string_ok(returnType) && returnType == "xml")
00196 {
00197 #ifdef TOMBDEBUG
00198 try
00199 {
00200
00201 XML2JSON::getJSON(root);
00202
00203 }
00204 catch(Exception e)
00205 {
00206 e.printStackTrace();
00207 }
00208 #endif
00209 output = renderXMLHeader() + root->print();
00210 }
00211 else
00212 {
00213 try
00214 {
00215 output = XML2JSON::getJSON(root);
00216 }
00217 catch(Exception e)
00218 {
00219 e.printStackTrace();
00220 }
00221 }
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238 Ref<MemIOHandler> io_handler(new MemIOHandler(output));
00239 io_handler->open(mode);
00240 return RefCast(io_handler, IOHandler);
00241 }
00242
00243 Ref<IOHandler> WebRequestHandler::open(IN const char *filename,
00244 OUT struct File_Info *info,
00245 IN enum UpnpOpenFileMode mode)
00246 {
00247 log_debug("request: %s\n", filename);
00248 this->filename = filename;
00249 this->mode = mode;
00250
00251 String path, parameters;
00252 split_url(filename, URL_UI_PARAM_SEPARATOR, path, parameters);
00253
00254 params->decode(parameters);
00255
00256 get_info(NULL, info);
00257 return open(mode);
00258 }
00259
00260 void WebRequestHandler::handleUpdateIDs()
00261 {
00262
00263
00264 String updates = param(_("updates"));
00265 if (string_ok(updates))
00266 {
00267 Ref<Element> updateIDs(new Element(_("update_ids")));
00268 root->appendElementChild(updateIDs);
00269 if (updates == "check")
00270 {
00271 updateIDs->setAttribute(_("pending"), session->hasUIUpdateIDs() ? _("1") : _("0"), mxml_bool_type);
00272 }
00273 else if (updates == "get")
00274 {
00275 addUpdateIDs(updateIDs, session);
00276 }
00277 }
00278 }
00279
00280 void WebRequestHandler::addUpdateIDs(Ref<Element> updateIDsEl, Ref<Session> session)
00281 {
00282 String updateIDs = session->getUIUpdateIDs();
00283 if (string_ok(updateIDs))
00284 {
00285 log_debug("UI: sending update ids: %s\n", updateIDs.c_str());
00286 updateIDsEl->setTextKey(_("ids"));
00287 updateIDsEl->setText(updateIDs);
00288 updateIDsEl->setAttribute(_("updates"), _("1"), mxml_bool_type);
00289 }
00290 }
00291
00292 void WebRequestHandler::appendTask(Ref<Element> el, Ref<GenericTask> task)
00293 {
00294 if (task == nil || el == nil)
00295 return;
00296 Ref<Element> taskEl (new Element(_("task")));
00297 taskEl->setAttribute(_("id"), String::from(task->getID()), mxml_int_type);
00298 taskEl->setAttribute(_("cancellable"), task->isCancellable() ? _("1") : _("0"), mxml_bool_type);
00299 taskEl->setTextKey(_("text"));
00300 taskEl->setText(task->getDescription());
00301 el->appendElementChild(taskEl);
00302 }
00303
00304 String WebRequestHandler::mapAutoscanType(int type)
00305 {
00306 if (type == 1)
00307 return _("ui");
00308 else if (type == 2)
00309 return _("persistent");
00310 else
00311 return _("none");
00312 }
00313
00314 int WebRequestHandler::remapAutoscanType(String type)
00315 {
00316 if (type == "ui")
00317 return 1;
00318 else if (type == "persistent")
00319 return 2;
00320 else
00321 return 0;
00322 }