YARP
Yet Another Robot Platform
StyleNameService.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * All rights reserved.
4  *
5  * This software may be modified and distributed under the terms of the
6  * BSD-3-Clause license. See the accompanying LICENSE file for details.
7  */
8 
9 #include <cstdio>
10 
12 
13 #include <yarp/os/Value.h>
14 
15 using namespace yarp::os;
16 using namespace yarp::serversql::impl;
17 
18 bool StyleNameService::apply(yarp::os::Bottle& cmd,
19  yarp::os::Bottle& reply,
20  yarp::os::Bottle& event,
21  const yarp::os::Contact& remote) {
22  if (cmd.get(0).asString()!="web") return false;
23 
24  if (!content.check("main.css")) {
25  if (!options.check("web")) {
26  content.put("main.css","\n\
27 body {\n\
28  background: white;\n\
29  color: black;\n\
30 }\n\
31 h1 {\n\
32  font-family: verdana, arial, sans-serif;\n\
33  font-size: 300%;\n\
34  color: #339966;\n\
35 }\n\
36 div {\n\
37  padding-bottom: 10px;\n\
38 }\n\
39 a:link{\n\
40  color: #114499;\n\
41  text-decoration: none;\n\
42 }\n\
43 a:visited {\n\
44  color: #114499;\n\
45  text-decoration: none;\n\
46 }\n\
47 a:hover{\n\
48  color: red;\n\
49  text-decoration: none;\n\
50 }\n\
51 ");
52  mime.put("main.css","text/css");
53  }
54  }
55 
56  Bottle& bot = reply;
57  bot.addString("web");
58 
59  std::string code = cmd.get(1).asString();
60  std::string uri = cmd.check("REQUEST_URI",Value("")).toString();
61  if (uri.length()>=4) { uri = uri.substr(4); } else { uri = ""; }
62  std::string fileName = uri;
63 
64  if ((!content.check(uri))||options.check("no-web-cache")) {
65  if (options.check("web")) {
66  std::string accum;
67  bool first = true;
68  for (size_t i=0; i<fileName.length(); i++) {
69  char ch = fileName[i];
70  if (ch == '.' && !first) continue;
71  if (ch == '/') { first = true; continue; }
72  first = false;
73  if (ch>='a'&&ch<='z') continue;
74  if (ch>='A'&&ch<='Z') continue;
75  if (ch>='0'&&ch<='9') continue;
76  if (ch == '-' || ch == '_') continue;
77  ((char*)fileName.c_str())[i] = '_';
78  }
79  if (fileName == "") {
80  fileName = "index.html";
81  }
82  fileName = options.find("web").asString() + "/" + fileName;
83  char buf[25600];
84  FILE *fin = fopen(fileName.c_str(),"rb");
85  if (fin != nullptr) {
86  size_t len = 0;
87  do {
88  len = fread(buf,1,sizeof(buf),fin);
89  if (len>=1) {
90  accum += std::string(buf,len);
91  }
92  } while (len>=1);
93  fclose(fin);
94  fin = nullptr;
95  }
96  content.put(uri,accum);
97  if (uri.find(".css")!=std::string::npos) {
98  mime.put(uri,"text/css");
99  } else if (uri.find(".png")!=std::string::npos) {
100  mime.put(uri,"image/png");
101  } else if (uri.find(".jpg")!=std::string::npos) {
102  mime.put(uri,"image/jpeg");
103  } else if (uri.find(".js")!=std::string::npos) {
104  mime.put(uri,"text/javascript");
105  } else {
106  mime.put(uri,"text/html");
107  }
108  }
109  }
110 
111  if (content.check(uri)) {
112  std::string txt = content.find(uri).asString();
113  std::string txtMime = mime.find(uri).asString();
114  printf(" * %s %s %d bytes, %s\n",
115  cmd.toString().c_str(),
116  (fileName!=uri)?fileName.c_str():"",
117  (int)txt.length(),
118  txtMime.c_str());
119  bot.addString(txt);
120  bot.addString("mime");
121  bot.addString(txtMime);
122  } else {
123  printf(" * %s - Unknown\n", cmd.toString().c_str());
124  bot.addString("Hmm.");
125  }
126  return true;
127 }
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
StyleNameService.h
yarp::os::Bottle::check
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Bottle.cpp:280
yarp::os::Bottle::get
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:249
yarp::os::Value::asString
virtual std::string asString() const
Get string value.
Definition: Value.cpp:237
yarp::os::Bottle::addString
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition: Bottle.cpp:173
yarp::os
An interface to the operating system, including Port based communication.
Definition: AbstractCarrier.h:17
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::serversql::impl
Definition: Allocator.h:18
Value.h