ROME 2 Lab5

Committer:
oehlemar
Date:
Fri Jun 12 08:19:42 2020 +0000
Revision:
1:5201940a41c1
Parent:
0:893a1e710078
asdf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oehlemar 0:893a1e710078 1 /*
oehlemar 0:893a1e710078 2 * HTTPServer.cpp
oehlemar 0:893a1e710078 3 * Copyright (c) 2020, ZHAW
oehlemar 0:893a1e710078 4 * All rights reserved.
oehlemar 0:893a1e710078 5 */
oehlemar 0:893a1e710078 6
oehlemar 0:893a1e710078 7 #include <algorithm>
oehlemar 0:893a1e710078 8 #include "HTTPScript.h"
oehlemar 0:893a1e710078 9 #include "HTTPServer.h"
oehlemar 0:893a1e710078 10
oehlemar 0:893a1e710078 11 using namespace std;
oehlemar 0:893a1e710078 12
oehlemar 0:893a1e710078 13 inline string int2String(int i) {
oehlemar 0:893a1e710078 14
oehlemar 0:893a1e710078 15 char buffer[32];
oehlemar 0:893a1e710078 16 sprintf(buffer, "%d", i);
oehlemar 0:893a1e710078 17
oehlemar 0:893a1e710078 18 return string(buffer);
oehlemar 0:893a1e710078 19 }
oehlemar 0:893a1e710078 20
oehlemar 0:893a1e710078 21 const unsigned int HTTPServer::INPUT_BUFFER_SIZE = 1024; // size of receive buffer, given in [bytes]
oehlemar 0:893a1e710078 22 const int HTTPServer::SOCKET_TIMEOUT = 1000; // timeout of socket, given in [ms]
oehlemar 0:893a1e710078 23
oehlemar 0:893a1e710078 24 /**
oehlemar 0:893a1e710078 25 * Create and initialize an http server object.
oehlemar 0:893a1e710078 26 * @param ethernet a reference to the embedded TCP/IP stack to use.
oehlemar 0:893a1e710078 27 */
oehlemar 0:893a1e710078 28 HTTPServer::HTTPServer(EthernetInterface& ethernet) : ethernet(ethernet), thread(osPriorityNormal, STACK_SIZE) {
oehlemar 0:893a1e710078 29
oehlemar 0:893a1e710078 30 // start thread
oehlemar 0:893a1e710078 31
oehlemar 0:893a1e710078 32 thread.start(callback(this, &HTTPServer::run));
oehlemar 0:893a1e710078 33 }
oehlemar 0:893a1e710078 34
oehlemar 0:893a1e710078 35 /**
oehlemar 0:893a1e710078 36 * Delete the http server object.
oehlemar 0:893a1e710078 37 */
oehlemar 0:893a1e710078 38 HTTPServer::~HTTPServer() {}
oehlemar 0:893a1e710078 39
oehlemar 0:893a1e710078 40 /**
oehlemar 0:893a1e710078 41 * Registers the given script with the http server.
oehlemar 0:893a1e710078 42 * This allows to call a method of this script object
oehlemar 0:893a1e710078 43 * through virtual cgi-bin requests from a remote system.
oehlemar 0:893a1e710078 44 */
oehlemar 0:893a1e710078 45 void HTTPServer::add(string name, HTTPScript* httpScript) {
oehlemar 0:893a1e710078 46
oehlemar 0:893a1e710078 47 httpScriptNames.push_back(name);
oehlemar 0:893a1e710078 48 httpScripts.push_back(httpScript);
oehlemar 0:893a1e710078 49 }
oehlemar 0:893a1e710078 50
oehlemar 0:893a1e710078 51 /**
oehlemar 0:893a1e710078 52 * Decodes a given URL string into a standard text string.
oehlemar 0:893a1e710078 53 */
oehlemar 0:893a1e710078 54 string HTTPServer::urlDecoder(string url) {
oehlemar 0:893a1e710078 55
oehlemar 0:893a1e710078 56 size_t pos = 0;
oehlemar 0:893a1e710078 57 while ((pos = url.find("+")) != string::npos) url = url.substr(0, pos)+" "+url.substr(pos+1);
oehlemar 0:893a1e710078 58 while ((pos = url.find("%08")) != string::npos) url = url.substr(0, pos)+"\b"+url.substr(pos+3);
oehlemar 0:893a1e710078 59 while ((pos = url.find("%09")) != string::npos) url = url.substr(0, pos)+"\t"+url.substr(pos+3);
oehlemar 0:893a1e710078 60 while ((pos = url.find("%0A")) != string::npos) url = url.substr(0, pos)+"\n"+url.substr(pos+3);
oehlemar 0:893a1e710078 61 while ((pos = url.find("%0D")) != string::npos) url = url.substr(0, pos)+"\r"+url.substr(pos+3);
oehlemar 0:893a1e710078 62 while ((pos = url.find("%20")) != string::npos) url = url.substr(0, pos)+" "+url.substr(pos+3);
oehlemar 0:893a1e710078 63 while ((pos = url.find("%22")) != string::npos) url = url.substr(0, pos)+"\""+url.substr(pos+3);
oehlemar 0:893a1e710078 64 while ((pos = url.find("%23")) != string::npos) url = url.substr(0, pos)+"#"+url.substr(pos+3);
oehlemar 0:893a1e710078 65 while ((pos = url.find("%24")) != string::npos) url = url.substr(0, pos)+"$"+url.substr(pos+3);
oehlemar 0:893a1e710078 66 while ((pos = url.find("%25")) != string::npos) url = url.substr(0, pos)+"%"+url.substr(pos+3);
oehlemar 0:893a1e710078 67 while ((pos = url.find("%26")) != string::npos) url = url.substr(0, pos)+"&"+url.substr(pos+3);
oehlemar 0:893a1e710078 68 while ((pos = url.find("%2B")) != string::npos) url = url.substr(0, pos)+"+"+url.substr(pos+3);
oehlemar 0:893a1e710078 69 while ((pos = url.find("%2C")) != string::npos) url = url.substr(0, pos)+","+url.substr(pos+3);
oehlemar 0:893a1e710078 70 while ((pos = url.find("%2F")) != string::npos) url = url.substr(0, pos)+"/"+url.substr(pos+3);
oehlemar 0:893a1e710078 71 while ((pos = url.find("%3A")) != string::npos) url = url.substr(0, pos)+":"+url.substr(pos+3);
oehlemar 0:893a1e710078 72 while ((pos = url.find("%3B")) != string::npos) url = url.substr(0, pos)+";"+url.substr(pos+3);
oehlemar 0:893a1e710078 73 while ((pos = url.find("%3C")) != string::npos) url = url.substr(0, pos)+"<"+url.substr(pos+3);
oehlemar 0:893a1e710078 74 while ((pos = url.find("%3D")) != string::npos) url = url.substr(0, pos)+"="+url.substr(pos+3);
oehlemar 0:893a1e710078 75 while ((pos = url.find("%3E")) != string::npos) url = url.substr(0, pos)+">"+url.substr(pos+3);
oehlemar 0:893a1e710078 76 while ((pos = url.find("%3F")) != string::npos) url = url.substr(0, pos)+"?"+url.substr(pos+3);
oehlemar 0:893a1e710078 77 while ((pos = url.find("%40")) != string::npos) url = url.substr(0, pos)+"@"+url.substr(pos+3);
oehlemar 0:893a1e710078 78
oehlemar 0:893a1e710078 79 return url;
oehlemar 0:893a1e710078 80 }
oehlemar 0:893a1e710078 81
oehlemar 0:893a1e710078 82 /**
oehlemar 0:893a1e710078 83 * This <code>run()</code> method binds the TCP/IP server to a given port number
oehlemar 0:893a1e710078 84 * and enters an infinite loop that waits for http requests and then processes
oehlemar 0:893a1e710078 85 * these requests and returns a response.
oehlemar 0:893a1e710078 86 */
oehlemar 0:893a1e710078 87 void HTTPServer::run() {
oehlemar 0:893a1e710078 88
oehlemar 0:893a1e710078 89 // bind the server to a given port number
oehlemar 0:893a1e710078 90
oehlemar 0:893a1e710078 91 server.open(&ethernet);
oehlemar 0:893a1e710078 92 server.bind(PORT_NUMBER);
oehlemar 0:893a1e710078 93 server.listen();
oehlemar 0:893a1e710078 94
oehlemar 0:893a1e710078 95 // enter infinite loop
oehlemar 0:893a1e710078 96
oehlemar 0:893a1e710078 97 while (true) {
oehlemar 0:893a1e710078 98
oehlemar 0:893a1e710078 99 TCPSocket* client = server.accept();
oehlemar 0:893a1e710078 100 if (client != NULL) {
oehlemar 0:893a1e710078 101
oehlemar 0:893a1e710078 102 client->set_blocking(true);
oehlemar 0:893a1e710078 103 client->set_timeout(SOCKET_TIMEOUT); // set timeout of socket
oehlemar 0:893a1e710078 104
oehlemar 0:893a1e710078 105 // read input
oehlemar 0:893a1e710078 106
oehlemar 0:893a1e710078 107 char buffer[INPUT_BUFFER_SIZE];
oehlemar 0:893a1e710078 108 int size = client->recv(buffer, sizeof(buffer));
oehlemar 0:893a1e710078 109
oehlemar 0:893a1e710078 110 if (size > 0) {
oehlemar 0:893a1e710078 111
oehlemar 0:893a1e710078 112 string input(buffer, size);
oehlemar 0:893a1e710078 113 string header;
oehlemar 0:893a1e710078 114 string output;
oehlemar 0:893a1e710078 115
oehlemar 0:893a1e710078 116 // parse input
oehlemar 0:893a1e710078 117
oehlemar 0:893a1e710078 118 if ((input.find("GET") == 0) || (input.find("HEAD") == 0)) {
oehlemar 0:893a1e710078 119
oehlemar 0:893a1e710078 120 if (input.find("cgi-bin") != string::npos) {
oehlemar 0:893a1e710078 121
oehlemar 0:893a1e710078 122 // process script request with arguments
oehlemar 0:893a1e710078 123
oehlemar 0:893a1e710078 124 string script = input.substr(input.find("cgi-bin/")+8, input.find(" ", input.find("cgi-bin/")+8)-input.find("cgi-bin/")-8);
oehlemar 0:893a1e710078 125 string name;
oehlemar 0:893a1e710078 126 vector<string> names;
oehlemar 0:893a1e710078 127 vector<string> values;
oehlemar 0:893a1e710078 128
oehlemar 0:893a1e710078 129 if (script.find("?") != string::npos) {
oehlemar 0:893a1e710078 130
oehlemar 0:893a1e710078 131 name = script.substr(0, script.find("?"));
oehlemar 0:893a1e710078 132 script = script.substr(script.find("?")+1);
oehlemar 0:893a1e710078 133
oehlemar 0:893a1e710078 134 vector<string> arguments;
oehlemar 0:893a1e710078 135 while (script.find("&") != string::npos) {
oehlemar 0:893a1e710078 136 arguments.push_back(script.substr(0, script.find("&")));
oehlemar 0:893a1e710078 137 script = script.substr(script.find("&")+1);
oehlemar 0:893a1e710078 138 }
oehlemar 0:893a1e710078 139 arguments.push_back(script);
oehlemar 0:893a1e710078 140
oehlemar 0:893a1e710078 141 for (int i = 0; i < arguments.size(); i++) {
oehlemar 0:893a1e710078 142
oehlemar 0:893a1e710078 143 if (arguments[i].find("=") != string::npos) {
oehlemar 0:893a1e710078 144
oehlemar 0:893a1e710078 145 names.push_back(arguments[i].substr(0, arguments[i].find("=")));
oehlemar 0:893a1e710078 146 values.push_back(urlDecoder(arguments[i].substr(arguments[i].find("=")+1)));
oehlemar 0:893a1e710078 147
oehlemar 0:893a1e710078 148 } else {
oehlemar 0:893a1e710078 149
oehlemar 0:893a1e710078 150 names.push_back(arguments[i]);
oehlemar 0:893a1e710078 151 values.push_back("");
oehlemar 0:893a1e710078 152 }
oehlemar 0:893a1e710078 153 }
oehlemar 0:893a1e710078 154
oehlemar 0:893a1e710078 155 } else {
oehlemar 0:893a1e710078 156
oehlemar 0:893a1e710078 157 name = script;
oehlemar 0:893a1e710078 158 }
oehlemar 0:893a1e710078 159
oehlemar 0:893a1e710078 160 // look for corresponding script
oehlemar 0:893a1e710078 161
oehlemar 0:893a1e710078 162 for (int i = 0; i < min(httpScriptNames.size(), httpScripts.size()); i++) {
oehlemar 0:893a1e710078 163
oehlemar 0:893a1e710078 164 if (httpScriptNames[i].compare(name) == 0) {
oehlemar 0:893a1e710078 165
oehlemar 0:893a1e710078 166 output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
oehlemar 0:893a1e710078 167 output += "<!DOCTYPE html>\r\n";
oehlemar 0:893a1e710078 168 output += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\r\n";
oehlemar 0:893a1e710078 169 output += "<body>\r\n";
oehlemar 0:893a1e710078 170 output += httpScripts[i]->call(names, values);
oehlemar 0:893a1e710078 171 output += "</body>\r\n";
oehlemar 0:893a1e710078 172 output += "</html>\r\n";
oehlemar 0:893a1e710078 173
oehlemar 0:893a1e710078 174 header = "HTTP/1.1 200 OK\r\n";
oehlemar 0:893a1e710078 175 header += "Content-Length: "+int2String(output.size())+"\r\n";
oehlemar 0:893a1e710078 176 header += "Content-Type: text/xml\r\n";
oehlemar 0:893a1e710078 177 header += "Expires: 0\r\n";
oehlemar 0:893a1e710078 178 header += "\r\n";
oehlemar 0:893a1e710078 179
oehlemar 0:893a1e710078 180 output = header+output;
oehlemar 0:893a1e710078 181 }
oehlemar 0:893a1e710078 182 }
oehlemar 0:893a1e710078 183
oehlemar 0:893a1e710078 184 // requested script was not found on this server
oehlemar 0:893a1e710078 185
oehlemar 0:893a1e710078 186 if ((output).size() == 0) {
oehlemar 0:893a1e710078 187
oehlemar 0:893a1e710078 188 output = "<!DOCTYPE html>\r\n";
oehlemar 0:893a1e710078 189 output += "<html lang=\"en\">\r\n";
oehlemar 0:893a1e710078 190 output += "<head>\r\n";
oehlemar 0:893a1e710078 191 output += " <title>404 Not Found</title>\r\n";
oehlemar 0:893a1e710078 192 output += " <style type=\"text/css\">\r\n";
oehlemar 0:893a1e710078 193 output += " h2 {font-family:Helvetica,Arial,sans-serif; font-size: 24; color:#FFFFFF;}\r\n";
oehlemar 0:893a1e710078 194 output += " p {font-family:Helvetica,Arial,sans-serif; font-size: 14; color:#444444;}\r\n";
oehlemar 0:893a1e710078 195 output += " </style>\r\n";
oehlemar 0:893a1e710078 196 output += "</head>\r\n";
oehlemar 0:893a1e710078 197 output += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\r\n";
oehlemar 0:893a1e710078 198 output += " <table width=\"100%\" height=\"100%\" border=\"0\" frame=\"void\" cellspacing=\"0\" cellpadding=\"20\">\r\n";
oehlemar 0:893a1e710078 199 output += " <tr>\r\n";
oehlemar 0:893a1e710078 200 output += " <th width=\"100%\" height=\"30\" bgcolor=\"#0064A6\"><h2>400 Bad Request</h2></th>\r\n";
oehlemar 0:893a1e710078 201 output += " </tr>\r\n";
oehlemar 0:893a1e710078 202 output += " <tr>\r\n";
oehlemar 0:893a1e710078 203 output += " <td valign=\"top\">\r\n";
oehlemar 0:893a1e710078 204 output += " <p>The requested script could not be found on this server!</p>\r\n";
oehlemar 0:893a1e710078 205 output += " </td>\r\n";
oehlemar 0:893a1e710078 206 output += " </tr>\r\n";
oehlemar 0:893a1e710078 207 output += " </table>\r\n";
oehlemar 0:893a1e710078 208 output += "</body>\r\n";
oehlemar 0:893a1e710078 209 output += "</html>\r\n";
oehlemar 0:893a1e710078 210
oehlemar 0:893a1e710078 211 header = "HTTP/1.1 404 Not Found\r\n";
oehlemar 0:893a1e710078 212 header += "Content-Length: "+int2String(output.size())+"\r\n";
oehlemar 0:893a1e710078 213 header += "Content-Type: text/html\r\n";
oehlemar 0:893a1e710078 214 header += "\r\n";
oehlemar 0:893a1e710078 215
oehlemar 0:893a1e710078 216 output = header+output;
oehlemar 0:893a1e710078 217 }
oehlemar 0:893a1e710078 218
oehlemar 0:893a1e710078 219 // write output
oehlemar 0:893a1e710078 220
oehlemar 0:893a1e710078 221 void* address = (void*)(output).c_str();
oehlemar 0:893a1e710078 222 int offset = 0;
oehlemar 0:893a1e710078 223 while (offset < (output).size()) offset += client->send((void*)(static_cast<int>(reinterpret_cast<intptr_t>(address))+offset), (output).size()-offset);
oehlemar 0:893a1e710078 224
oehlemar 0:893a1e710078 225 } else {
oehlemar 0:893a1e710078 226
oehlemar 0:893a1e710078 227 // transmit static file
oehlemar 0:893a1e710078 228
oehlemar 0:893a1e710078 229 output = "<!DOCTYPE html>\r\n";
oehlemar 0:893a1e710078 230 output += "<html lang=\"en\">\r\n";
oehlemar 0:893a1e710078 231 output += "<head>\r\n";
oehlemar 0:893a1e710078 232 output += " <meta charset=\"utf-8\"/>\r\n";
oehlemar 0:893a1e710078 233 output += " <title>LIDAR Scan</title>\r\n";
oehlemar 0:893a1e710078 234 output += " <style type=\"text/css\">\r\n";
oehlemar 0:893a1e710078 235 output += " html {background-color: #223344;}\r\n";
oehlemar 0:893a1e710078 236 output += " h2 {font-family:Helvetica,Arial,sans-serif; font-size: 24; color:#FFFFFF;}\r\n";
oehlemar 0:893a1e710078 237 output += " p {font-family:Helvetica,Arial,sans-serif; font-size: 16; color:#EEEEEE;}\r\n";
oehlemar 0:893a1e710078 238 output += " </style>\r\n";
oehlemar 0:893a1e710078 239 output += "</head>\r\n";
oehlemar 0:893a1e710078 240 output += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\r\n";
oehlemar 0:893a1e710078 241 output += " <script type=\"text/javascript\">\r\n";
oehlemar 0:893a1e710078 242 output += " var xmlhttp = null;\r\n";
oehlemar 1:5201940a41c1 243 output += " var task = window.setInterval(\"update()\", 250);\r\n";
oehlemar 0:893a1e710078 244 output += " function update() {\r\n";
oehlemar 0:893a1e710078 245 output += " if (window.XMLHttpRequest) {\r\n";
oehlemar 0:893a1e710078 246 output += " xmlhttp = new XMLHttpRequest();\r\n";
oehlemar 0:893a1e710078 247 output += " } else if (window.ActiveXObject) {\r\n";
oehlemar 0:893a1e710078 248 output += " try {\r\n";
oehlemar 0:893a1e710078 249 output += " xmlhttp = new ActiveXObject(\"Msxml2.XMLHTTP\");\r\n";
oehlemar 0:893a1e710078 250 output += " } catch (exception) {\r\n";
oehlemar 0:893a1e710078 251 output += " try {\r\n";
oehlemar 0:893a1e710078 252 output += " xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\r\n";
oehlemar 0:893a1e710078 253 output += " } catch (exception) {}\r\n";
oehlemar 0:893a1e710078 254 output += " }\r\n";
oehlemar 0:893a1e710078 255 output += " }\r\n";
oehlemar 0:893a1e710078 256 output += " xmlhttp.onreadystatechange = refresh;\r\n";
oehlemar 0:893a1e710078 257 output += " xmlhttp.open(\"GET\", \"/cgi-bin/lidar\");\r\n";
oehlemar 0:893a1e710078 258 output += " xmlhttp.send(null);\r\n";
oehlemar 0:893a1e710078 259 output += " }\r\n";
oehlemar 0:893a1e710078 260 output += " function refresh() {\r\n";
oehlemar 0:893a1e710078 261 output += " if (xmlhttp.readyState == 4) {\r\n";
oehlemar 0:893a1e710078 262 output += " var xml = xmlhttp.responseXML;\r\n";
oehlemar 0:893a1e710078 263 output += " var intValues = xml.getElementsByTagName(\"int\");\r\n";
oehlemar 0:893a1e710078 264 output += " var floatValues = xml.getElementsByTagName(\"float\");\r\n";
oehlemar 0:893a1e710078 265 output += " var sizeScan = intValues[0].childNodes[0].nodeValue;\r\n";
oehlemar 0:893a1e710078 266 output += " var sizeBeacons = intValues[1].childNodes[0].nodeValue;\r\n";
oehlemar 0:893a1e710078 267 output += " var x = [];\r\n";
oehlemar 0:893a1e710078 268 output += " var y = [];\r\n";
oehlemar 0:893a1e710078 269 output += " var xBeacon = [];\r\n";
oehlemar 0:893a1e710078 270 output += " var yBeacon = [];\r\n";
oehlemar 0:893a1e710078 271 output += " for (i = 0; i < sizeScan; i++) {\r\n";
oehlemar 0:893a1e710078 272 output += " x.push(-floatValues[2*i+1].childNodes[0].nodeValue);\r\n";
oehlemar 0:893a1e710078 273 output += " y.push(floatValues[2*i].childNodes[0].nodeValue);\r\n";
oehlemar 0:893a1e710078 274 output += " }\r\n";
oehlemar 0:893a1e710078 275 output += " for (i = 0; i < sizeBeacons; i++) {\r\n";
oehlemar 0:893a1e710078 276 output += " xBeacon.push(-floatValues[2*i+2*sizeScan+1].childNodes[0].nodeValue);\r\n";
oehlemar 0:893a1e710078 277 output += " yBeacon.push(floatValues[2*i+2*sizeScan].childNodes[0].nodeValue);\r\n";
oehlemar 0:893a1e710078 278 output += " }\r\n";
oehlemar 0:893a1e710078 279 output += " drawScan(\"lidar\", x, y, xBeacon, yBeacon);\r\n";
oehlemar 0:893a1e710078 280 output += " }\r\n";
oehlemar 0:893a1e710078 281 output += " }\r\n";
oehlemar 0:893a1e710078 282 output += " function drawScan(id, x, y, xBeacon, yBeacon) {\r\n";
oehlemar 0:893a1e710078 283 output += " var canvas = document.getElementById(id);\r\n";
oehlemar 0:893a1e710078 284 output += " var width = window.innerWidth-50;\r\n";
oehlemar 0:893a1e710078 285 output += " var height = window.innerHeight-50;\r\n";
oehlemar 0:893a1e710078 286 output += " canvas.width = 2*width;\r\n";
oehlemar 0:893a1e710078 287 output += " canvas.height = 2*height;\r\n";
oehlemar 0:893a1e710078 288 output += " canvas.style.width = width+\"px\";\r\n";
oehlemar 0:893a1e710078 289 output += " canvas.style.height = height+\"px\";\r\n";
oehlemar 0:893a1e710078 290 output += " var ctx = canvas.getContext(\"2d\");\r\n";
oehlemar 0:893a1e710078 291 output += " ctx.scale(2,2);\r\n";
oehlemar 0:893a1e710078 292 output += " ctx.fillStyle = \"#334455\";\r\n";
oehlemar 0:893a1e710078 293 output += " ctx.fillRect(0.5, 0.5, width-1, height-1);\r\n";
oehlemar 0:893a1e710078 294 output += " var xMax = 3.0;\r\n";
oehlemar 0:893a1e710078 295 output += " var yMax = 3.0;\r\n";
oehlemar 0:893a1e710078 296 output += " if (width > height) {\r\n";
oehlemar 0:893a1e710078 297 output += " xMax = yMax*width/height;\r\n";
oehlemar 0:893a1e710078 298 output += " } else {\r\n";
oehlemar 0:893a1e710078 299 output += " yMax = xMax*height/width;\r\n";
oehlemar 0:893a1e710078 300 output += " }\r\n";
oehlemar 0:893a1e710078 301 output += " ctx.strokeStyle = \"#445566\";\r\n";
oehlemar 0:893a1e710078 302 output += " ctx.lineWidth = 1;\r\n";
oehlemar 0:893a1e710078 303 output += " ctx.beginPath();\r\n";
oehlemar 0:893a1e710078 304 output += " for (xGrid = 0.0; xGrid < xMax; xGrid += 0.1) {\r\n";
oehlemar 0:893a1e710078 305 output += " ctx.moveTo(width/2.0+xGrid/xMax*width/2.0, 0.0);\r\n";
oehlemar 0:893a1e710078 306 output += " ctx.lineTo(width/2.0+xGrid/xMax*width/2.0, height);\r\n";
oehlemar 0:893a1e710078 307 output += " ctx.moveTo(width/2.0-xGrid/xMax*width/2.0, 0.0);\r\n";
oehlemar 0:893a1e710078 308 output += " ctx.lineTo(width/2.0-xGrid/xMax*width/2.0, height);\r\n";
oehlemar 0:893a1e710078 309 output += " }\r\n";
oehlemar 0:893a1e710078 310 output += " for (yGrid = 0.0; yGrid < yMax; yGrid += 0.1) {\r\n";
oehlemar 0:893a1e710078 311 output += " ctx.moveTo(0.0, height/2.0+yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 312 output += " ctx.lineTo(width, height/2.0+yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 313 output += " ctx.moveTo(0.0, height/2.0-yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 314 output += " ctx.lineTo(width, height/2.0-yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 315 output += " }\r\n";
oehlemar 0:893a1e710078 316 output += " ctx.stroke();\r\n";
oehlemar 0:893a1e710078 317 output += " ctx.strokeStyle = \"#667788\";\r\n";
oehlemar 0:893a1e710078 318 output += " ctx.lineWidth = 1;\r\n";
oehlemar 0:893a1e710078 319 output += " ctx.beginPath();\r\n";
oehlemar 0:893a1e710078 320 output += " for (xGrid = 0.0; xGrid < xMax; xGrid += 1.0) {\r\n";
oehlemar 0:893a1e710078 321 output += " ctx.moveTo(width/2.0+xGrid/xMax*width/2.0, 0.0);\r\n";
oehlemar 0:893a1e710078 322 output += " ctx.lineTo(width/2.0+xGrid/xMax*width/2.0, height);\r\n";
oehlemar 0:893a1e710078 323 output += " ctx.moveTo(width/2.0-xGrid/xMax*width/2.0, 0.0);\r\n";
oehlemar 0:893a1e710078 324 output += " ctx.lineTo(width/2.0-xGrid/xMax*width/2.0, height);\r\n";
oehlemar 0:893a1e710078 325 output += " }\r\n";
oehlemar 0:893a1e710078 326 output += " for (yGrid = 0.0; yGrid < yMax; yGrid += 1.0) {\r\n";
oehlemar 0:893a1e710078 327 output += " ctx.moveTo(0.0, height/2.0+yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 328 output += " ctx.lineTo(width, height/2.0+yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 329 output += " ctx.moveTo(0.0, height/2.0-yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 330 output += " ctx.lineTo(width, height/2.0-yGrid/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 331 output += " }\r\n";
oehlemar 0:893a1e710078 332 output += " ctx.stroke();\r\n";
oehlemar 0:893a1e710078 333 output += " ctx.strokeStyle = \"white\";\r\n";
oehlemar 0:893a1e710078 334 output += " ctx.lineWidth = 0;\r\n";
oehlemar 0:893a1e710078 335 output += " ctx.fillStyle = \"white\";\r\n";
oehlemar 0:893a1e710078 336 output += " ctx.beginPath();\r\n";
oehlemar 0:893a1e710078 337 output += " ctx.moveTo(width/2.0, height/2.0);\r\n";
oehlemar 0:893a1e710078 338 output += " ctx.arc(width/2.0, height/2.0, 8, 0, 2*Math.PI, false);\r\n";
oehlemar 0:893a1e710078 339 output += " ctx.moveTo(width/2.0, height/2.0+8);\r\n";
oehlemar 0:893a1e710078 340 output += " ctx.arc(width/2.0, height/2.0+8, 5, 0, 2*Math.PI, false);\r\n";
oehlemar 0:893a1e710078 341 output += " ctx.fill();\r\n";
oehlemar 0:893a1e710078 342 output += " ctx.stroke();\r\n";
oehlemar 0:893a1e710078 343 output += " ctx.strokeStyle = \"#FF0000\";\r\n";
oehlemar 0:893a1e710078 344 output += " ctx.fillStyle = \"#FF0000\";\r\n";
oehlemar 0:893a1e710078 345 output += " ctx.beginPath();\r\n";
oehlemar 0:893a1e710078 346 output += " for (i = 0; i < Math.min(xBeacon.length, yBeacon.length); i++) {\r\n";
oehlemar 0:893a1e710078 347 output += " ctx.moveTo(width/2.0+xBeacon[i]/xMax*width/2.0, height/2.0-yBeacon[i]/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 348 output += " ctx.arc(width/2.0+xBeacon[i]/xMax*width/2.0, height/2.0-yBeacon[i]/yMax*height/2.0, 5, 0, 2*Math.PI, false);\r\n";
oehlemar 0:893a1e710078 349 output += " }\r\n";
oehlemar 0:893a1e710078 350 output += " ctx.fill();\r\n";
oehlemar 0:893a1e710078 351 output += " ctx.stroke();\r\n";
oehlemar 0:893a1e710078 352 output += " ctx.strokeStyle = \"#FFFF00\";\r\n";
oehlemar 0:893a1e710078 353 output += " ctx.lineWidth = 0.5;\r\n";
oehlemar 0:893a1e710078 354 output += " ctx.beginPath();\r\n";
oehlemar 0:893a1e710078 355 output += " ctx.moveTo(width/2.0+x[0]/xMax*width/2.0, height/2.0-y[0]/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 356 output += " for (i = 1; i < Math.min(x.length, y.length); i++) {\r\n";
oehlemar 0:893a1e710078 357 output += " ctx.lineTo(width/2.0+x[i]/xMax*width/2.0, height/2.0-y[i]/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 358 output += " }\r\n";
oehlemar 0:893a1e710078 359 output += " ctx.lineTo(width/2.0+x[0]/xMax*width/2.0, height/2.0-y[0]/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 360 output += " ctx.stroke();\r\n";
oehlemar 0:893a1e710078 361 output += " ctx.fillStyle = \"#FFFF00\";\r\n";
oehlemar 0:893a1e710078 362 output += " ctx.beginPath();\r\n";
oehlemar 0:893a1e710078 363 output += " for (i = 0; i < Math.min(x.length, y.length); i++) {\r\n";
oehlemar 0:893a1e710078 364 output += " ctx.moveTo(width/2.0+x[i]/xMax*width/2.0, height/2.0-y[i]/yMax*height/2.0);\r\n";
oehlemar 0:893a1e710078 365 output += " ctx.arc(width/2.0+x[i]/xMax*width/2.0, height/2.0-y[i]/yMax*height/2.0, 2, 0, 2*Math.PI, false);\r\n";
oehlemar 0:893a1e710078 366 output += " }\r\n";
oehlemar 0:893a1e710078 367 output += " ctx.fill();\r\n";
oehlemar 0:893a1e710078 368 output += " ctx.stroke();\r\n";
oehlemar 0:893a1e710078 369 output += " ctx.strokeStyle = \"white\";\r\n";
oehlemar 0:893a1e710078 370 output += " ctx.lineWidth = 1;\r\n";
oehlemar 0:893a1e710078 371 output += " ctx.strokeRect(0.5, 0.5, width-1, height-1);\r\n";
oehlemar 0:893a1e710078 372 output += " }\r\n";
oehlemar 0:893a1e710078 373 output += " </script>\r\n";
oehlemar 0:893a1e710078 374 output += " <table width=\"100%\" height=\"100%\" border=\"0\" frame=\"void\" cellspacing=\"20\" cellpadding=\"0\">\r\n";
oehlemar 0:893a1e710078 375 output += " <tr>\r\n";
oehlemar 0:893a1e710078 376 output += " <th><canvas id=\"lidar\"></canvas></th>\r\n";
oehlemar 0:893a1e710078 377 output += " </tr>\r\n";
oehlemar 0:893a1e710078 378 output += " </table>\r\n";
oehlemar 0:893a1e710078 379 output += "</body>\r\n";
oehlemar 0:893a1e710078 380 output += "</html>\r\n";
oehlemar 0:893a1e710078 381
oehlemar 0:893a1e710078 382 header = "HTTP/1.1 404 Not Found\r\n";
oehlemar 0:893a1e710078 383 header += "Content-Length: "+int2String(output.size())+"\r\n";
oehlemar 0:893a1e710078 384 header += "Content-Type: text/html\r\n";
oehlemar 0:893a1e710078 385 header += "\r\n";
oehlemar 0:893a1e710078 386
oehlemar 0:893a1e710078 387 output = header+output;
oehlemar 0:893a1e710078 388
oehlemar 0:893a1e710078 389 // write output
oehlemar 0:893a1e710078 390
oehlemar 0:893a1e710078 391 void* address = (void*)output.c_str();
oehlemar 0:893a1e710078 392 int offset = 0;
oehlemar 0:893a1e710078 393 while (offset < output.size()) offset += client->send((void*)(static_cast<int>(reinterpret_cast<intptr_t>(address))+offset), output.size()-offset);
oehlemar 0:893a1e710078 394 }
oehlemar 0:893a1e710078 395
oehlemar 0:893a1e710078 396 } else {
oehlemar 0:893a1e710078 397
oehlemar 0:893a1e710078 398 // the http method is not known
oehlemar 0:893a1e710078 399
oehlemar 0:893a1e710078 400 output = "<!DOCTYPE html>\r\n";
oehlemar 0:893a1e710078 401 output += "<html lang=\"en\">\r\n";
oehlemar 0:893a1e710078 402 output += "<head>\r\n";
oehlemar 0:893a1e710078 403 output += " <title>400 Bad Request</title>\r\n";
oehlemar 0:893a1e710078 404 output += " <style type=\"text/css\">\r\n";
oehlemar 0:893a1e710078 405 output += " h2 {font-family:Helvetica,Arial,sans-serif; font-size: 24; color:#FFFFFF;}\r\n";
oehlemar 0:893a1e710078 406 output += " p {font-family:Helvetica,Arial,sans-serif; font-size: 14; color:#444444;}\r\n";
oehlemar 0:893a1e710078 407 output += " </style>\r\n";
oehlemar 0:893a1e710078 408 output += "</head>\r\n";
oehlemar 0:893a1e710078 409 output += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\r\n";
oehlemar 0:893a1e710078 410 output += " <table width=\"100%\" height=\"100%\" border=\"0\" frame=\"void\" cellspacing=\"0\" cellpadding=\"20\">\r\n";
oehlemar 0:893a1e710078 411 output += " <tr>\r\n";
oehlemar 0:893a1e710078 412 output += " <th width=\"100%\" height=\"30\" bgcolor=\"#0064A6\"><h2>400 Bad Request</h2></th>\r\n";
oehlemar 0:893a1e710078 413 output += " </tr>\r\n";
oehlemar 0:893a1e710078 414 output += " <tr>\r\n";
oehlemar 0:893a1e710078 415 output += " <td valign=\"top\">\r\n";
oehlemar 0:893a1e710078 416 output += " <p>The requested method is not supported by this server!</p>\r\n";
oehlemar 0:893a1e710078 417 output += " </td>\r\n";
oehlemar 0:893a1e710078 418 output += " </tr>\r\n";
oehlemar 0:893a1e710078 419 output += " </table>\r\n";
oehlemar 0:893a1e710078 420 output += "</body>\r\n";
oehlemar 0:893a1e710078 421 output += "</html>\r\n";
oehlemar 0:893a1e710078 422
oehlemar 0:893a1e710078 423 header = "HTTP/1.1 400 Bad Request\r\n";
oehlemar 0:893a1e710078 424 header += "Content-Length: "+int2String(output.size())+"\r\n";
oehlemar 0:893a1e710078 425 header += "Content-Type: text/html\r\n";
oehlemar 0:893a1e710078 426 header += "\r\n";
oehlemar 0:893a1e710078 427
oehlemar 0:893a1e710078 428 output = header+output;
oehlemar 0:893a1e710078 429
oehlemar 0:893a1e710078 430 // write output
oehlemar 0:893a1e710078 431
oehlemar 0:893a1e710078 432 void* address = (void*)output.c_str();
oehlemar 0:893a1e710078 433 int offset = 0;
oehlemar 0:893a1e710078 434 while (offset < output.size()) offset += client->send((void*)(static_cast<int>(reinterpret_cast<intptr_t>(address))+offset), output.size()-offset);
oehlemar 0:893a1e710078 435 }
oehlemar 0:893a1e710078 436 }
oehlemar 0:893a1e710078 437
oehlemar 0:893a1e710078 438 client->close();
oehlemar 0:893a1e710078 439
oehlemar 0:893a1e710078 440 } // client != NULL
oehlemar 0:893a1e710078 441
oehlemar 0:893a1e710078 442 } // infinite while loop
oehlemar 0:893a1e710078 443 }
oehlemar 0:893a1e710078 444