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