INET Framework for OMNeT++/OMNEST
inet::httptools::HttpServerBase Class Referenceabstract

Web server base class. More...

#include <HttpServerBase.h>

Inheritance diagram for inet::httptools::HttpServerBase:
inet::httptools::HttpNodeBase inet::ILifecycle inet::httptools::HttpServer inet::httptools::HttpServerDirect inet::httptools::HttpServerEvilA inet::httptools::HttpServerEvilB inet::httptools::HttpServerDirectEvilA inet::httptools::HttpServerDirectEvilB

Classes

struct  HtmlPageData
 Describes a HTML page. More...
 

Public Member Functions

 HttpServerBase ()
 
 ~HttpServerBase ()
 
const std::string & getHostName ()
 
- Public Member Functions inherited from inet::httptools::HttpNodeBase
 HttpNodeBase ()
 
- Public Member Functions inherited from inet::ILifecycle
virtual ~ILifecycle ()
 

Protected Member Functions

virtual void initialize (int stage) override
 
virtual int numInitStages () const override
 
virtual void finish () override
 
virtual void handleMessage (cMessage *msg) override=0
 
virtual void refreshDisplay () const override
 
HttpReplyMessagegenerateDocument (HttpRequestMessage *request, const char *resource, int size=0)
 
HttpReplyMessagegenerateResourceMessage (HttpRequestMessage *request, std::string resource, HttpContentType category)
 
HttpReplyMessagehandleGetRequest (HttpRequestMessage *request, std::string resource)
 
HttpReplyMessagegenerateErrorReply (HttpRequestMessage *request, int code)
 
virtual std::string generateBody ()
 
cPacket * handleReceivedMessage (cMessage *msg)
 
void registerWithController ()
 
void readSiteDefinition (std::string file)
 
std::string readHtmlBodyFile (std::string file, std::string path)
 
- Protected Member Functions inherited from inet::httptools::HttpNodeBase
void sendDirectToModule (HttpNodeBase *receiver, cPacket *packet, simtime_t constdelay=0.0, rdObject *rd=nullptr)
 
double transmissionDelay (cPacket *packet)
 
void logRequest (const HttpRequestMessage *httpRequest)
 
void logResponse (const HttpReplyMessage *httpResponse)
 
void logEntry (std::string line)
 
std::string formatHttpRequestShort (const HttpRequestMessage *httpRequest)
 
std::string formatHttpResponseShort (const HttpReplyMessage *httpResponse)
 
std::string formatHttpRequestLong (const HttpRequestMessage *httpRequest)
 
std::string formatHttpResponseLong (const HttpReplyMessage *httpResponse)
 
virtual bool handleOperationStage (LifecycleOperation *operation, int stage, IDoneCallback *doneCallback) override
 Perform one stage of a lifecycle operation. More...
 

Protected Attributes

std::string hostName
 
int port = -1
 
bool scriptedMode = false
 
std::map< std::string, HtmlPageDatahtmlPages
 
std::map< std::string, unsigned int > resources
 
simtime_t activationTime
 
long htmlDocsServed = 0
 
long imgResourcesServed = 0
 
long textResourcesServed = 0
 
long badRequests = 0
 
rdObjectrdReplyDelay = nullptr
 The processing delay of the server. More...
 
rdObjectrdHtmlPageSize = nullptr
 The HTML page size distribution for the site. More...
 
rdObjectrdTextResourceSize = nullptr
 The text resource size distribution for the site. More...
 
rdObjectrdImageResourceSize = nullptr
 The image resource size distribution for the site. More...
 
rdObjectrdNumResources = nullptr
 Number of resources per HTML page. More...
 
rdObjectrdTextImageResourceRatio = nullptr
 The ratio of text resources to images referenced in HTML pages. More...
 
rdObjectrdErrorMsgSize = nullptr
 The size of error messages. More...
 
- Protected Attributes inherited from inet::httptools::HttpNodeBase
unsigned long linkSpeed = 0
 
int httpProtocol = 0
 
std::string logFileName
 
bool enableLogging = true
 
LOG_FORMAT outputFormat = lf_short
 
bool m_bDisplayMessage = true
 
bool m_bDisplayResponseContent = true
 
cModule * host = nullptr
 

Detailed Description

Web server base class.

This module implements a flexible Web server. It is part of the HttpTools project and should be used in conjunction with a number of browsing clients.

The server base class cannot be instantiated directly in a simulation. Use rather the HttpServer for INET TCP/IP applications or HttpServerDirect for direct message passing. See those classes for details. See the INET documentation for details on the StandardHost and the TCP/IP simulation.

See also
HttpServer
HttpServerDirect
DirectHost.
Author
Kristjan V. Jonsson

Constructor & Destructor Documentation

inet::httptools::HttpServerBase::HttpServerBase ( )
28 {
29 }
inet::httptools::HttpServerBase::~HttpServerBase ( )
32 {
33  delete rdReplyDelay;
34  delete rdHtmlPageSize;
35  delete rdTextResourceSize;
36  delete rdImageResourceSize;
37  delete rdNumResources;
39  delete rdErrorMsgSize;
40 }
rdObject * rdImageResourceSize
The image resource size distribution for the site.
Definition: HttpServerBase.h:79
rdObject * rdErrorMsgSize
The size of error messages.
Definition: HttpServerBase.h:82
rdObject * rdTextResourceSize
The text resource size distribution for the site.
Definition: HttpServerBase.h:78
rdObject * rdNumResources
Number of resources per HTML page.
Definition: HttpServerBase.h:80
rdObject * rdReplyDelay
The processing delay of the server.
Definition: HttpServerBase.h:76
rdObject * rdTextImageResourceRatio
The ratio of text resources to images referenced in HTML pages.
Definition: HttpServerBase.h:81
rdObject * rdHtmlPageSize
The HTML page size distribution for the site.
Definition: HttpServerBase.h:77

Member Function Documentation

void inet::httptools::HttpServerBase::finish ( )
overrideprotectedvirtual

Reimplemented in inet::httptools::HttpServer, and inet::httptools::HttpServerDirect.

Referenced by inet::httptools::HttpServerDirect::finish(), and inet::httptools::HttpServer::finish().

165 {
166  EV_INFO << "HTML documents served " << htmlDocsServed << "\n";
167  EV_INFO << "Image resources served " << imgResourcesServed << "\n";
168  EV_INFO << "Text resources served " << textResourcesServed << "\n";
169  EV_INFO << "Bad requests " << badRequests << "\n";
170 
171  recordScalar("HTML.served", htmlDocsServed);
172  recordScalar("images.served", imgResourcesServed);
173  recordScalar("text.served", textResourcesServed);
174  recordScalar("bad.requests", badRequests);
175 }
long badRequests
Definition: HttpServerBase.h:74
long htmlDocsServed
Definition: HttpServerBase.h:71
long imgResourcesServed
Definition: HttpServerBase.h:72
long textResourcesServed
Definition: HttpServerBase.h:73
std::string inet::httptools::HttpServerBase::generateBody ( )
protectedvirtual

Reimplemented in inet::httptools::HttpServerDirectEvilA, inet::httptools::HttpServerDirectEvilB, inet::httptools::HttpServerEvilA, and inet::httptools::HttpServerEvilB.

Referenced by generateDocument().

384 {
385  int numResources = (int)rdNumResources->draw();
386  int numImages = (int)(numResources * rdTextImageResourceRatio->draw());
387  int numText = numResources - numImages;
388 
389  std::string result;
390 
391  char tempBuf[128];
392  for (int i = 0; i < numImages; i++) {
393  sprintf(tempBuf, "%s%.4d.%s\n", "IMG", i, "jpg");
394  result.append(tempBuf);
395  }
396  for (int i = 0; i < numText; i++) {
397  sprintf(tempBuf, "%s%.4d.%s\n", "TEXT", i, "txt");
398  result.append(tempBuf);
399  }
400 
401  return result;
402 }
rdObject * rdNumResources
Number of resources per HTML page.
Definition: HttpServerBase.h:80
virtual double draw()=0
rdObject * rdTextImageResourceRatio
The ratio of text resources to images referenced in HTML pages.
Definition: HttpServerBase.h:81
HttpReplyMessage * inet::httptools::HttpServerBase::generateDocument ( HttpRequestMessage request,
const char *  resource,
int  size = 0 
)
protected

Referenced by handleGetRequest().

293 {
294  EV_DEBUG << "Generating HTML document for request " << request->getName() << " from " << request->getSenderModule()->getName() << endl;
295 
296  char szReply[512];
297  sprintf(szReply, "HTTP/1.1 200 OK (%s)", resource);
298  HttpReplyMessage *replymsg = new HttpReplyMessage(szReply);
299  replymsg->setHeading("HTTP/1.1 200 OK");
300  replymsg->setOriginatorUrl(hostName.c_str());
301  replymsg->setTargetUrl(request->originatorUrl());
302  replymsg->setProtocol(request->protocol());
303  replymsg->setSerial(request->serial());
304  replymsg->setResult(200);
305  replymsg->setContentType(CT_HTML); // Emulates the content-type header field
306  replymsg->setKind(HTTPT_RESPONSE_MESSAGE);
307 
308  if (scriptedMode) {
309  replymsg->setPayload(htmlPages[resource].body.c_str());
310  size = htmlPages[resource].size;
311  }
312  else {
313  replymsg->setPayload(generateBody().c_str());
314  }
315 
316  if (size == 0) {
317  EV_DEBUG << "Using random distribution for page size" << endl;
318  size = (int)rdHtmlPageSize->draw();
319  }
320 
321  replymsg->setByteLength(size);
322  EV_DEBUG << "Serving a HTML document of length " << replymsg->getByteLength() << " bytes" << endl;
323 
324  htmlDocsServed++;
325 
326  return replymsg;
327 }
std::map< std::string, HtmlPageData > htmlPages
Definition: HttpServerBase.h:66
std::string hostName
Definition: HttpServerBase.h:63
long htmlDocsServed
Definition: HttpServerBase.h:71
virtual std::string generateBody()
Definition: HttpServerBase.cc:383
virtual double draw()=0
Definition: HttpMessages_m.h:162
bool scriptedMode
Definition: HttpServerBase.h:65
rdObject * rdHtmlPageSize
The HTML page size distribution for the site.
Definition: HttpServerBase.h:77
#define HTTPT_RESPONSE_MESSAGE
Definition: HttpNodeBase.h:41
HttpReplyMessage * inet::httptools::HttpServerBase::generateErrorReply ( HttpRequestMessage request,
int  code 
)
protected

Referenced by handleGetRequest(), and handleReceivedMessage().

366 {
367  char szErrStr[32];
368  sprintf(szErrStr, "HTTP/1.1 %.3d %s", code, htmlErrFromCode(code).c_str());
369  HttpReplyMessage *replymsg = new HttpReplyMessage(szErrStr);
370  replymsg->setHeading(szErrStr);
371  replymsg->setOriginatorUrl(hostName.c_str());
372  replymsg->setTargetUrl(request->originatorUrl());
373  replymsg->setProtocol(request->protocol()); // MIGRATE40: kvj
374  replymsg->setSerial(request->serial());
375  replymsg->setResult(code);
376  replymsg->setByteLength((int)rdErrorMsgSize->draw());
377  replymsg->setKind(HTTPT_RESPONSE_MESSAGE);
378 
379  badRequests++;
380  return replymsg;
381 }
long badRequests
Definition: HttpServerBase.h:74
std::string htmlErrFromCode(int code)
Definition: HttpUtils.cc:162
std::string hostName
Definition: HttpServerBase.h:63
rdObject * rdErrorMsgSize
The size of error messages.
Definition: HttpServerBase.h:82
virtual double draw()=0
#define HTTPT_RESPONSE_MESSAGE
Definition: HttpNodeBase.h:41
HttpReplyMessage * inet::httptools::HttpServerBase::generateResourceMessage ( HttpRequestMessage request,
std::string  resource,
HttpContentType  category 
)
protected

Referenced by handleGetRequest().

330 {
331  EV_DEBUG << "Generating resource message in response to request " << request->heading() << " with serial " << request->serial() << endl;
332 
333  if (category == CT_TEXT)
335  else if (category == CT_IMAGE)
337 
338  int size;
339  if (scriptedMode)
340  size = resources[resource];
341  else if (category==CT_TEXT)
342  size = (int) rdTextResourceSize->draw();
343  else if (category==CT_IMAGE)
344  size = (int) rdImageResourceSize->draw();
345  else
346  throw cRuntimeError("Invalid resource category");
347 
348  char szReply[512];
349  sprintf(szReply, "HTTP/1.1 200 OK (%s)", resource.c_str());
350  HttpReplyMessage *replymsg = new HttpReplyMessage(szReply);
351  replymsg->setHeading("HTTP/1.1 200 OK");
352  replymsg->setOriginatorUrl(hostName.c_str());
353  replymsg->setTargetUrl(request->originatorUrl());
354  replymsg->setProtocol(request->protocol()); // MIGRATE40: kvj
355  replymsg->setSerial(request->serial());
356  replymsg->setResult(200);
357  replymsg->setContentType(category); // Emulates the content-type header field
358  replymsg->setByteLength(size); // Set the resource size
359  replymsg->setKind(HTTPT_RESPONSE_MESSAGE);
360 
361  sprintf(szReply, "RESOURCE-BODY:%s", resource.c_str());
362  return replymsg;
363 }
rdObject * rdImageResourceSize
The image resource size distribution for the site.
Definition: HttpServerBase.h:79
std::string hostName
Definition: HttpServerBase.h:63
Definition: HttpMessages_m.h:163
rdObject * rdTextResourceSize
The text resource size distribution for the site.
Definition: HttpServerBase.h:78
std::map< std::string, unsigned int > resources
Definition: HttpServerBase.h:67
long imgResourcesServed
Definition: HttpServerBase.h:72
virtual double draw()=0
bool scriptedMode
Definition: HttpServerBase.h:65
Definition: HttpMessages_m.h:164
#define HTTPT_RESPONSE_MESSAGE
Definition: HttpNodeBase.h:41
long textResourcesServed
Definition: HttpServerBase.h:73
const std::string& inet::httptools::HttpServerBase::getHostName ( )
inline
HttpReplyMessage * inet::httptools::HttpServerBase::handleGetRequest ( HttpRequestMessage request,
std::string  resource 
)
protected

Referenced by handleReceivedMessage().

248 {
249  EV_DEBUG << "Handling GET request " << request->getName() << " resource: " << resource << endl;
250 
251  resource = trimLeft(resource, "/");
252  std::vector<std::string> req = parseResourceName(resource);
253  if (req.size() != 3) {
254  EV_ERROR << "Invalid GET request string: " << request->heading() << endl;
255  return generateErrorReply(request, 400);
256  }
257 
259 
260  if (cat == CT_HTML) {
261  if (scriptedMode) {
262  if (resource.empty() && htmlPages.find("root") != htmlPages.end()) {
263  EV_DEBUG << "Generating root resource" << endl;
264  return generateDocument(request, "root");
265  }
266  if (htmlPages.find(resource) == htmlPages.end()) {
267  if (htmlPages.find("default") != htmlPages.end()) {
268  EV_DEBUG << "Generating default resource" << endl;
269  return generateDocument(request, "default");
270  }
271  else {
272  EV_ERROR << "Page not found: " << resource << endl;
273  return generateErrorReply(request, 404);
274  }
275  }
276  }
277  return generateDocument(request, resource.c_str());
278  }
279  else if (cat == CT_TEXT || cat == CT_IMAGE) {
280  if (scriptedMode && resources.find(resource) == resources.end()) {
281  EV_ERROR << "Resource not found: " << resource << endl;
282  return generateErrorReply(request, 404);
283  }
284  return generateResourceMessage(request, resource, cat);
285  }
286  else {
287  EV_ERROR << "Unknown or unsupported resource requested in " << request->heading() << endl;
288  return generateErrorReply(request, 400);
289  }
290 }
HttpContentType
Enum generated from inet/applications/httptools/common/HttpMessages.msg:99 by nedtool.
Definition: HttpMessages_m.h:160
std::string trimLeft(std::string s)
Definition: HttpUtils.cc:29
HttpReplyMessage * generateErrorReply(HttpRequestMessage *request, int code)
Definition: HttpServerBase.cc:365
std::map< std::string, HtmlPageData > htmlPages
Definition: HttpServerBase.h:66
HttpReplyMessage * generateResourceMessage(HttpRequestMessage *request, std::string resource, HttpContentType category)
Definition: HttpServerBase.cc:329
Definition: HttpMessages_m.h:163
std::vector< std::string > parseResourceName(std::string resource)
Definition: HttpUtils.cc:103
std::map< std::string, unsigned int > resources
Definition: HttpServerBase.h:67
HttpContentType getResourceCategory(std::vector< std::string > res)
Definition: HttpUtils.cc:142
HttpReplyMessage * generateDocument(HttpRequestMessage *request, const char *resource, int size=0)
Definition: HttpServerBase.cc:292
Definition: HttpMessages_m.h:162
bool scriptedMode
Definition: HttpServerBase.h:65
Definition: HttpMessages_m.h:164
void inet::httptools::HttpServerBase::handleMessage ( cMessage *  msg)
overrideprotectedpure virtual

Implemented in inet::httptools::HttpServer, and inet::httptools::HttpServerDirect.

196 {
197  // Override in derived classes
198 }
cPacket * inet::httptools::HttpServerBase::handleReceivedMessage ( cMessage *  msg)
protected

Referenced by inet::httptools::HttpServerDirect::handleMessage(), and inet::httptools::HttpServer::socketDataArrived().

201 {
202  HttpRequestMessage *request = check_and_cast<HttpRequestMessage *>(msg);
203  if (request == nullptr)
204  throw cRuntimeError("Message (%s)%s is not a valid request", msg->getClassName(), msg->getName());
205 
206  EV_DEBUG << "Handling received message " << msg->getName() << ". Target URL: " << request->targetUrl() << endl;
207 
208  logRequest(request);
209 
210  if (extractServerName(request->targetUrl()) != hostName) {
211  // This should never happen but lets check
212  throw cRuntimeError("Received message intended for '%s'", request->targetUrl()); // TODO: DEBUG HERE
213  return nullptr;
214  }
215 
216  HttpReplyMessage *replymsg;
217 
218  // Parse the request string on spaces
219  cStringTokenizer tokenizer = cStringTokenizer(request->heading(), " ");
220  std::vector<std::string> res = tokenizer.asVector();
221  if (res.size() != 3) {
222  EV_ERROR << "Invalid request string: " << request->heading() << endl;
223  replymsg = generateErrorReply(request, 400);
224  logResponse(replymsg);
225  return replymsg;
226  }
227 
228  if (request->badRequest()) {
229  // Bad requests get a 404 reply.
230  EV_ERROR << "Bad request - bad flag set. Message: " << request->getName() << endl;
231  replymsg = generateErrorReply(request, 404);
232  }
233  else if (res[0] == "GET") {
234  replymsg = handleGetRequest(request, res[1]); // Pass in the resource string part
235  }
236  else {
237  EV_ERROR << "Unsupported request type " << res[0] << " for " << request->heading() << endl;
238  replymsg = generateErrorReply(request, 400);
239  }
240 
241  if (replymsg != nullptr)
242  logResponse(replymsg);
243 
244  return replymsg;
245 }
void logRequest(const HttpRequestMessage *httpRequest)
Definition: HttpNodeBase.cc:61
HttpReplyMessage * generateErrorReply(HttpRequestMessage *request, int code)
Definition: HttpServerBase.cc:365
std::string hostName
Definition: HttpServerBase.h:63
std::string extractServerName(const char *url)
Definition: HttpUtils.cc:60
void logResponse(const HttpReplyMessage *httpResponse)
Definition: HttpNodeBase.cc:73
HttpReplyMessage * handleGetRequest(HttpRequestMessage *request, std::string resource)
Definition: HttpServerBase.cc:247
void inet::httptools::HttpServerBase::initialize ( int  stage)
overrideprotectedvirtual

Reimplemented from inet::httptools::HttpNodeBase.

Reimplemented in inet::httptools::HttpServer, inet::httptools::HttpServerDirectEvilA, inet::httptools::HttpServerDirectEvilB, inet::httptools::HttpServerEvilA, inet::httptools::HttpServerEvilB, and inet::httptools::HttpServerDirect.

Referenced by inet::httptools::HttpServerDirect::initialize(), and inet::httptools::HttpServer::initialize().

43 {
45 
46  if (stage == INITSTAGE_LOCAL) {
47  EV_DEBUG << "Initializing server component\n";
48 
49  hostName = (const char *)par("hostName");
50  if (hostName.empty()) {
51  hostName = "www.";
52  hostName += host->getFullName();
53  hostName += ".com";
54  }
55  EV_DEBUG << "Initializing HTTP server. Using WWW name " << hostName << endl;
56  port = par("port");
57 
58  logFileName = par("logFile").stdstringValue();
59  enableLogging = logFileName != "";
61 
62  httpProtocol = par("httpProtocol");
63 
64  cXMLElement *rootelement = par("config").xmlValue();
65  if (rootelement == nullptr)
66  throw cRuntimeError("Configuration file is not defined");
67 
68  // Initialize the distribution objects for random browsing
69  // @todo Skip initialization of random objects for scripted servers?
70  cXMLAttributeMap attributes;
71  rdObjectFactory rdFactory;
72 
73  // The reply delay
74  cXMLElement *element = rootelement->getFirstChildWithTag("replyDelay");
75  if (element == nullptr)
76  throw cRuntimeError("Reply delay parameter undefined in XML configuration");
77  attributes = element->getAttributes();
78  rdReplyDelay = rdFactory.create(attributes);
79  if (rdReplyDelay == nullptr)
80  throw cRuntimeError("Reply delay random object could not be created");
81 
82  // HTML page size
83  element = rootelement->getFirstChildWithTag("htmlPageSize");
84  if (element == nullptr)
85  throw cRuntimeError("HTML page size parameter undefined in XML configuration");
86  attributes = element->getAttributes();
87  rdHtmlPageSize = rdFactory.create(attributes);
88  if (rdHtmlPageSize == nullptr)
89  throw cRuntimeError("HTML page size random object could not be created");
90 
91  // Text resource size
92  element = rootelement->getFirstChildWithTag("textResourceSize");
93  if (element == nullptr)
94  throw cRuntimeError("Text resource size parameter undefined in XML configuration");
95  attributes = element->getAttributes();
96  rdTextResourceSize = rdFactory.create(attributes);
97  if (rdTextResourceSize == nullptr)
98  throw cRuntimeError("Text resource size random object could not be created");
99 
100  // Image resource size
101  element = rootelement->getFirstChildWithTag("imageResourceSize");
102  if (element == nullptr)
103  throw cRuntimeError("Image resource size parameter undefined in XML configuration");
104  attributes = element->getAttributes();
105  rdImageResourceSize = rdFactory.create(attributes);
106  if (rdImageResourceSize == nullptr)
107  throw cRuntimeError("Image resource size random object could not be created");
108 
109  // Number of resources per page
110  element = rootelement->getFirstChildWithTag("numResources");
111  if (element == nullptr)
112  throw cRuntimeError("Number of resources parameter undefined in XML configuration");
113  attributes = element->getAttributes();
114  rdNumResources = rdFactory.create(attributes);
115  if (rdNumResources == nullptr)
116  throw cRuntimeError("Number of resources random object could not be created");
117 
118  // Text/Image resources ratio
119  element = rootelement->getFirstChildWithTag("textImageResourceRatio");
120  if (element == nullptr)
121  throw cRuntimeError("Text/image resource ratio parameter undefined in XML configuration");
122  attributes = element->getAttributes();
123  rdTextImageResourceRatio = rdFactory.create(attributes);
124  if (rdTextImageResourceRatio == nullptr)
125  throw cRuntimeError("Text/image resource ratio random object could not be created");
126 
127  // Error message size
128  element = rootelement->getFirstChildWithTag("errorMessageSize");
129  if (element == nullptr)
130  throw cRuntimeError("Error message size parameter undefined in XML configuration");
131  attributes = element->getAttributes();
132  rdErrorMsgSize = rdFactory.create(attributes);
133  if (rdErrorMsgSize == nullptr)
134  throw cRuntimeError("Error message size random object could not be created");
135 
136  activationTime = par("activationTime");
137  EV_INFO << "Activation time is " << activationTime << endl;
138 
139  std::string siteDefinition = (const char *)par("siteDefinition");
140  scriptedMode = !siteDefinition.empty();
141  if (scriptedMode)
142  readSiteDefinition(siteDefinition);
143 
144  // Register the server with the controller object
146 
147  // Initialize statistics
149 
150  // Initialize watches
151  WATCH(htmlDocsServed);
152  WATCH(imgResourcesServed);
153  WATCH(textResourcesServed);
154  WATCH(badRequests);
155  }
156  else if (stage == INITSTAGE_APPLICATION_LAYER) {
157  NodeStatus *nodeStatus = dynamic_cast<NodeStatus *>(findContainingNode(this)->getSubmodule("status"));
158  bool isOperational = (!nodeStatus) || nodeStatus->getState() == NodeStatus::UP;
159  if (!isOperational)
160  throw cRuntimeError("This module doesn't support starting in node DOWN state");
161  }
162 }
long badRequests
Definition: HttpServerBase.h:74
rdObject * rdImageResourceSize
The image resource size distribution for the site.
Definition: HttpServerBase.h:79
virtual void initialize(int stage) override
Definition: HttpNodeBase.cc:32
std::string hostName
Definition: HttpServerBase.h:63
cModule * host
Definition: HttpNodeBase.h:66
rdObject * rdErrorMsgSize
The size of error messages.
Definition: HttpServerBase.h:82
int httpProtocol
Definition: HttpNodeBase.h:60
rdObject * rdTextResourceSize
The text resource size distribution for the site.
Definition: HttpServerBase.h:78
int port
Definition: HttpServerBase.h:64
long htmlDocsServed
Definition: HttpServerBase.h:71
long imgResourcesServed
Definition: HttpServerBase.h:72
LOG_FORMAT outputFormat
Definition: HttpNodeBase.h:63
rdObject * rdNumResources
Number of resources per HTML page.
Definition: HttpServerBase.h:80
std::string logFileName
Definition: HttpNodeBase.h:61
rdObject * rdReplyDelay
The processing delay of the server.
Definition: HttpServerBase.h:76
cModule * findContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:56
Local initializations.
Definition: InitStages.h:35
bool enableLogging
Definition: HttpNodeBase.h:62
void registerWithController()
Definition: HttpServerBase.cc:404
bool scriptedMode
Definition: HttpServerBase.h:65
rdObject * rdTextImageResourceRatio
The ratio of text resources to images referenced in HTML pages.
Definition: HttpServerBase.h:81
simtime_t activationTime
Definition: HttpServerBase.h:68
rdObject * rdHtmlPageSize
The HTML page size distribution for the site.
Definition: HttpServerBase.h:77
void readSiteDefinition(std::string file)
Definition: HttpServerBase.cc:413
Definition: HttpNodeBase.h:44
long textResourcesServed
Definition: HttpServerBase.h:73
Initialization of applications.
Definition: InitStages.h:106
Definition: NodeStatus.h:40
virtual int inet::httptools::HttpServerBase::numInitStages ( ) const
inlineoverrideprotectedvirtual
std::string inet::httptools::HttpServerBase::readHtmlBodyFile ( std::string  file,
std::string  path 
)
protected

Referenced by readSiteDefinition().

508 {
509  EV_DEBUG << "Reading HTML page definition file" << endl;
510 
511  std::string filePath = path + file;
512  std::string line;
513  std::string body = "";
514  std::ifstream htmlfilestream;
515  htmlfilestream.open(filePath.c_str());
516 
517  if (htmlfilestream.fail())
518  throw cRuntimeError("Could not open page definition file '%s'", filePath.c_str());
519  while (!std::getline(htmlfilestream, line).eof()) {
520  line = trim(line);
521  if (line.empty() || line[0] == '#')
522  continue;
523  body += line;
524  body += "\n";
525  }
526  htmlfilestream.close();
527  return body;
528 }
std::string trim(std::string str)
Definition: HttpUtils.cc:53
void inet::httptools::HttpServerBase::readSiteDefinition ( std::string  file)
protected

Referenced by initialize().

414 {
415  EV_DEBUG << "Reading site definition file " << file << endl;
416 
417  std::ifstream tracefilestream;
418  tracefilestream.open(file.c_str());
419  if (tracefilestream.fail())
420  throw cRuntimeError("Could not open site definition file %s", file.c_str());
421 
422  std::vector<std::string> siteFileSplit = splitFile(file);
423  std::string line;
424  std::string key;
425  std::string htmlfile;
426  std::string body;
427  std::string value1;
428  std::string value2;
429  std::string sectionsub;
430  int size;
431  int linecount = 0;
432  bool siteSection = false;
433  bool resourceSection = false;
434 
435  while (!std::getline(tracefilestream, line).eof()) {
436  linecount++;
437  line = trim(line);
438  if (line.empty() || line[0] == '#')
439  continue;
440  sectionsub = getDelimited(line, "[", "]");
441  if (!sectionsub.empty()) {
442  // Section
443  siteSection = sectionsub == "HTML";
444  resourceSection = sectionsub == "RESOURCES";
445  }
446  else {
447  cStringTokenizer tokenizer = cStringTokenizer(line.c_str(), ";");
448  std::vector<std::string> res = tokenizer.asVector();
449 
450  if (siteSection) {
451  if (res.size() < 2 || res.size() > 3)
452  throw cRuntimeError("Invalid format of site configuration file '%s'. Site section, line (%d): %s",
453  file.c_str(), linecount, line.c_str());
454  key = trimLeft(res[0], "/");
455  if (key.empty()) {
456  if (htmlPages.find("root") == htmlPages.end())
457  key = "root";
458  else
459  throw cRuntimeError("Second root page found in site definition file %s, line (%d): %s",
460  file.c_str(), linecount, line.c_str());
461  }
462  htmlfile = res[1];
463  body = readHtmlBodyFile(htmlfile, siteFileSplit[0]); // Pass in the path of the definition file. Page defs are relative to that.
464  size = 0;
465  if (res.size() > 2) {
466  try {
467  size = atoi(res[2].c_str());
468  }
469  catch (...) {
470  throw cRuntimeError("Invalid format of site configuration file '%s'. Resource section, size, line (%d): %s",
471  file.c_str(), linecount, line.c_str());
472  }
473  }
474  EV_DEBUG << "Adding html page definition " << key << ". The page size is " << size << endl;
475  htmlPages[key].size = size;
476  htmlPages[key].body = body;
477  }
478  else if (resourceSection) {
479  if (res.size() < 2 || res.size() > 3)
480  throw cRuntimeError("Invalid format of site configuration file '%s'. Resource section, line (%d): %s",
481  file.c_str(), linecount, line.c_str());
482  key = res[0];
483  value1 = res[1];
484  try {
485  size = atoi(value1.c_str());
486  }
487  catch (...) {
488  throw cRuntimeError("Invalid format of site configuration file '%s'. Resource section, size, line (%d): %s",
489  file.c_str(), linecount, line.c_str());
490  }
491  if (res.size() > 2) {
492  // The type parameter - skip this
493  }
494 
495  resources[key] = size;
496  EV_DEBUG << "Adding resource " << key << " of size " << size << endl;
497  }
498  else {
499  throw cRuntimeError("Invalid format of site configuration file '%s'. Unknown section, line (%d): %s",
500  file.c_str(), linecount, line.c_str());
501  }
502  }
503  }
504  tracefilestream.close();
505 }
std::string trimLeft(std::string s)
Definition: HttpUtils.cc:29
std::map< std::string, HtmlPageData > htmlPages
Definition: HttpServerBase.h:66
std::map< std::string, unsigned int > resources
Definition: HttpServerBase.h:67
std::string readHtmlBodyFile(std::string file, std::string path)
Definition: HttpServerBase.cc:507
std::string getDelimited(std::string str, std::string ldelim, std::string rdelim)
Definition: HttpUtils.cc:128
std::vector< std::string > splitFile(std::string fileName)
Definition: HttpUtils.cc:209
std::string trim(std::string str)
Definition: HttpUtils.cc:53
void inet::httptools::HttpServerBase::refreshDisplay ( ) const
overrideprotectedvirtual
179 {
180  char buf[1024];
181  sprintf(buf, "%ld", htmlDocsServed);
182  cDisplayString& ds = host->getDisplayString();
183  ds.setTagArg("t", 0, buf);
184 
185  if (activationTime <= simTime()) {
186  ds.setTagArg("i2", 0, "status/up");
187  ds.setTagArg("i2", 1, "green");
188  }
189  else {
190  ds.setTagArg("i2", 0, "status/down");
191  ds.setTagArg("i2", 1, "red");
192  }
193 }
cModule * host
Definition: HttpNodeBase.h:66
long htmlDocsServed
Definition: HttpServerBase.h:71
uint32_t ds
Definition: TCP_NSC.cc:76
simtime_t activationTime
Definition: HttpServerBase.h:68
void inet::httptools::HttpServerBase::registerWithController ( )
protected

Referenced by initialize().

405 {
406  // Find controller object and register
407  HttpController *controller = check_and_cast_nullable<HttpController *>(getSimulation()->getSystemModule()->getSubmodule("controller"));
408  if (controller == nullptr)
409  throw cRuntimeError("Controller module not found");
410  controller->registerServer(host->getFullPath().c_str(), hostName.c_str(), port, INSERT_END, activationTime);
411 }
std::string hostName
Definition: HttpServerBase.h:63
cModule * host
Definition: HttpNodeBase.h:66
int port
Definition: HttpServerBase.h:64
#define INSERT_END
Definition: HttpController.h:38
simtime_t activationTime
Definition: HttpServerBase.h:68

Member Data Documentation

simtime_t inet::httptools::HttpServerBase::activationTime
protected
long inet::httptools::HttpServerBase::badRequests = 0
protected
long inet::httptools::HttpServerBase::htmlDocsServed = 0
protected
std::map<std::string, HtmlPageData> inet::httptools::HttpServerBase::htmlPages
protected
long inet::httptools::HttpServerBase::imgResourcesServed = 0
protected
int inet::httptools::HttpServerBase::port = -1
protected
rdObject* inet::httptools::HttpServerBase::rdErrorMsgSize = nullptr
protected

The size of error messages.

Referenced by generateErrorReply(), initialize(), and ~HttpServerBase().

rdObject* inet::httptools::HttpServerBase::rdHtmlPageSize = nullptr
protected

The HTML page size distribution for the site.

Referenced by generateDocument(), initialize(), and ~HttpServerBase().

rdObject* inet::httptools::HttpServerBase::rdImageResourceSize = nullptr
protected

The image resource size distribution for the site.

Referenced by generateResourceMessage(), initialize(), and ~HttpServerBase().

rdObject* inet::httptools::HttpServerBase::rdNumResources = nullptr
protected

Number of resources per HTML page.

Referenced by generateBody(), initialize(), and ~HttpServerBase().

rdObject* inet::httptools::HttpServerBase::rdReplyDelay = nullptr
protected

The processing delay of the server.

Referenced by inet::httptools::HttpServerDirect::handleMessage(), initialize(), and ~HttpServerBase().

rdObject* inet::httptools::HttpServerBase::rdTextImageResourceRatio = nullptr
protected

The ratio of text resources to images referenced in HTML pages.

Referenced by generateBody(), initialize(), and ~HttpServerBase().

rdObject* inet::httptools::HttpServerBase::rdTextResourceSize = nullptr
protected

The text resource size distribution for the site.

Referenced by generateResourceMessage(), initialize(), and ~HttpServerBase().

std::map<std::string, unsigned int> inet::httptools::HttpServerBase::resources
protected
bool inet::httptools::HttpServerBase::scriptedMode = false
protected
long inet::httptools::HttpServerBase::textResourcesServed = 0
protected

The documentation for this class was generated from the following files: