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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
/*
* webserver.cpp
*
* Created on: 06.08.2012
* Author: savop
*/
#include "../include/webserver.h"
#include "../upnp.h"
#include <sstream>
namespace upnp {
cWebserver::cWebserver(std::string address)
: mListenerAddress(address)
, mListenerPort(7649)
, mWebserverThread(*this)
{
SetWebserverRootDir(string(), string(), string());
SetServiceUrl(string(), string());
}
cWebserver::~cWebserver(){
}
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");
// Map static contents
ss1.clear(); ss1.str(string());
ss1 << "^/" << mServiceUrl << "([^/.]+)";
ss2.clear(); ss2.str(string());
ss2 << mServiceUrl << "$1";
mApplication.mapUrl(ss1.str(), ss2.str(), "serviceRedirect");
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, std::string staticContentUrl, std::string presentationUrl){
if(mWebserverThread.Active()) return;
if(rootDirectory.empty())
mWebserverRootDir = std::string(cPluginUpnp::ConfigDirectory(PLUGIN_NAME_I18N)) + "/httpdocs/";
else
mWebserverRootDir = rootDirectory;
if(staticContentUrl.empty())
mStaticContentUrl = "http/";
else
mStaticContentUrl = staticContentUrl;
if(presentationUrl.empty())
mPresentationUrl = "index.html";
else
mPresentationUrl = presentationUrl;
}
void cWebserver::SetServiceUrl(std::string descriptionUrl, std::string controlUrl){
if(mWebserverThread.Active()) return;
if(descriptionUrl.empty()){
mServiceUrl = "services/";
}
else
mServiceUrl = descriptionUrl;
if(controlUrl.empty()){
stringstream s;
s << "http://" << UpnpGetServerIpAddress() << ":" << UpnpGetServerPort() << "/" << "services/";
mControlUrl = s.str();
} else {
mControlUrl = controlUrl;
}
}
const std::string cWebserver::GetBaseUrl() const {
stringstream s;
s << "http://" << mListenerAddress << ":" << mListenerPort << "/";
return s.str();
}
const std::string cWebserver::GetServiceUrl() const {
return mServiceUrl;
}
const std::string cWebserver::GetControlUrl() const {
return mControlUrl;
}
const std::string cWebserver::GetPresentationUrl() const {
return mPresentationUrl;
}
const std::string cWebserver::GetStaticContentUrl() const {
return 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());
}
}
} // namespace upnp
|