summaryrefslogtreecommitdiff
path: root/server/webserver.cpp
blob: f1b589b61afbd48b61c2325ca45f03dc37f48816 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * webserver.cpp
 *
 *  Created on: 06.08.2012
 *      Author: savop
 */

#include "../include/webserver.h"
#include "../upnp.h"
#include <sstream>
#include <tnt/job.h>

namespace upnp {

cWebserver::cWebserver(std::string address)
: mListenerAddress(address)
, mListenerPort(7649)
, mStaticContentUrl("http/")
, mServiceUrl("services/")
, mWebserverThread(*this)
{
  SetWebserverRootDir(string());
  SetPresentationUrl(string());
}

cWebserver::~cWebserver(){
  mApplication.shutdown();
  mWebserverThread.Stop();
}

bool cWebserver::Start(){
  return mWebserverThread.Start();
}

void cWebserver::Stop(){
  try {
    mApplication.shutdown();
  } catch (const std::exception& e){
    esyslog("UPnP\tError while stopping web server: %s", e.what());
  }
}

bool cWebserver::Initialise(){

  try {
    // Map static contents
    stringstream ss1, ss2;

    mApplication.listen(mListenerAddress.c_str(), mListenerPort);

    mApplication.mapUrl("^/$", "index");

    mApplication.mapUrl("^/index.html", "index");

    ss1.clear(); ss1.str(string());
    ss1 << "^/" << mServiceUrl << "([^/.]+).xml$";

    mApplication.mapUrl(ss1.str(), "$1");

    // Map static contents
    ss1.clear(); ss1.str(string());
    ss1 << "^/" << mStaticContentUrl << "(.*)";

    ss2.clear(); ss2.str(string());
    ss2 << mWebserverRootDir << "$1";

    mApplication.mapUrl(ss1.str(), ss2.str(), "static@tntnet");

    mApplication.mapUrl("^/getStream", "resourceStreamer");

    isyslog("UPnP\tUsing %s for static content delivery.", mWebserverRootDir.c_str());

  } catch (const std::exception& e){
    esyslog("UPnP\tError while initialising web server: %s", e.what());
    return false;
  }

  return true;
}

void cWebserver::SetListenerPort(uint16_t port){
  if(mWebserverThread.Active()) return;

  mListenerPort = port ? port : 7649;
}

void cWebserver::SetWebserverRootDir(std::string rootDirectory){
  if(mWebserverThread.Active()) return;

  if(rootDirectory.empty())
    mWebserverRootDir = std::string(cPluginUpnp::ConfigDirectory(PLUGIN_NAME_I18N)) + "/httpdocs/";
  else
    mWebserverRootDir = rootDirectory;
}

void cWebserver::SetPresentationUrl(std::string presentationUrl){
  if(mWebserverThread.Active()) return;

  if(presentationUrl.empty())
    mPresentationUrl = "index.html";
  else
    mPresentationUrl = presentationUrl;
}

const std::string cWebserver::GetBaseUrl() const {
  stringstream s;
  s << "http://" << mListenerAddress << ":" << mListenerPort << "/";

  return s.str();
}

const std::string cWebserver::GetServiceUrl() const {
  return GetBaseUrl() + mServiceUrl;
}

const std::string cWebserver::GetControlUrl() const {
  stringstream s;
  s << "http://" << UpnpGetServerIpAddress() << ":" << UpnpGetServerPort() << "/" << "services/";
  return s.str();
}

const std::string cWebserver::GetPresentationUrl() const {
  return (mPresentationUrl.find("http://",0) == 0) ? mPresentationUrl : (GetBaseUrl() + mPresentationUrl);
}

const std::string cWebserver::GetStaticContentUrl() const {
  return GetBaseUrl() + mStaticContentUrl;
}

cWebserver::cWSThread::cWSThread(cWebserver& webServer)
: mWebserver(webServer)
{
}

void cWebserver::cWSThread::Action(){
  try {
    mWebserver.mApplication.run();
  } catch (const std::exception& e){
    esyslog("UPnP\tError while starting web server: %s", e.what());
  }
}

void cWebserver::cWSThread::Stop(){
  Cancel(5);
}

}  // namespace upnp