Quentin Mettraux
/
MAVErIC_TEST
Working Maveric
HTTPServer.cpp@10:36a2131f636c, 2017-08-22 (annotated)
- Committer:
- mettrque
- Date:
- Tue Aug 22 10:49:39 2017 +0000
- Revision:
- 10:36a2131f636c
- Parent:
- 9:b6a9d1c44ed9
last military version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mettrque | 0:bdca5e4773dd | 1 | /* |
mettrque | 0:bdca5e4773dd | 2 | * HTTPServer.cpp |
mettrque | 0:bdca5e4773dd | 3 | * Copyright (c) 2017, ZHAW |
mettrque | 0:bdca5e4773dd | 4 | * All rights reserved. |
mettrque | 0:bdca5e4773dd | 5 | * |
mettrque | 0:bdca5e4773dd | 6 | * Created on: 02.02.2017 |
mettrque | 0:bdca5e4773dd | 7 | * Author: Marcel Honegger |
mettrque | 0:bdca5e4773dd | 8 | */ |
mettrque | 0:bdca5e4773dd | 9 | |
mettrque | 0:bdca5e4773dd | 10 | #include "HTTPScript.h" |
mettrque | 0:bdca5e4773dd | 11 | #include "HTTPServer.h" |
mettrque | 0:bdca5e4773dd | 12 | |
mettrque | 0:bdca5e4773dd | 13 | using namespace std; |
mettrque | 0:bdca5e4773dd | 14 | |
mettrque | 6:ef95300898b2 | 15 | inline string int2String(int32_t i) |
mettrque | 6:ef95300898b2 | 16 | { |
mettrque | 6:ef95300898b2 | 17 | |
mettrque | 0:bdca5e4773dd | 18 | char buffer[32]; |
mettrque | 0:bdca5e4773dd | 19 | sprintf(buffer, "%d", i); |
mettrque | 6:ef95300898b2 | 20 | |
mettrque | 0:bdca5e4773dd | 21 | return string(buffer); |
mettrque | 0:bdca5e4773dd | 22 | } |
mettrque | 0:bdca5e4773dd | 23 | |
mettrque | 0:bdca5e4773dd | 24 | /** |
mettrque | 0:bdca5e4773dd | 25 | * Create and initialize an http server object. |
mettrque | 0:bdca5e4773dd | 26 | * @param ethernet a reference to the embedded TCP/IP stack to use. |
mettrque | 0:bdca5e4773dd | 27 | */ |
mettrque | 10:36a2131f636c | 28 | HTTPServer::HTTPServer(EthernetInterface* ethernet, FATFileSystem& fileSystem) : fileSystem(fileSystem), thread(osPriorityAboveNormal, STACK_SIZE) |
mettrque | 6:ef95300898b2 | 29 | { |
mettrque | 6:ef95300898b2 | 30 | |
mettrque | 0:bdca5e4773dd | 31 | this->ethernet = ethernet; |
mettrque | 6:ef95300898b2 | 32 | |
mettrque | 0:bdca5e4773dd | 33 | // start thread |
mettrque | 6:ef95300898b2 | 34 | |
mettrque | 0:bdca5e4773dd | 35 | thread.start(callback(this, &HTTPServer::run)); |
mettrque | 0:bdca5e4773dd | 36 | } |
mettrque | 0:bdca5e4773dd | 37 | |
mettrque | 0:bdca5e4773dd | 38 | /** |
mettrque | 0:bdca5e4773dd | 39 | * Delete the http server object. |
mettrque | 0:bdca5e4773dd | 40 | */ |
mettrque | 0:bdca5e4773dd | 41 | HTTPServer::~HTTPServer() {} |
mettrque | 0:bdca5e4773dd | 42 | |
mettrque | 0:bdca5e4773dd | 43 | /** |
mettrque | 0:bdca5e4773dd | 44 | * Registers the given script with the http server. |
mettrque | 0:bdca5e4773dd | 45 | * This allows to call a method of this script object |
mettrque | 0:bdca5e4773dd | 46 | * through virtual cgi-bin requests from a remote system. |
mettrque | 0:bdca5e4773dd | 47 | */ |
mettrque | 6:ef95300898b2 | 48 | void HTTPServer::add(string name, HTTPScript* httpScript) |
mettrque | 6:ef95300898b2 | 49 | { |
mettrque | 6:ef95300898b2 | 50 | |
mettrque | 0:bdca5e4773dd | 51 | httpScriptNames.push_back(name); |
mettrque | 0:bdca5e4773dd | 52 | httpScripts.push_back(httpScript); |
mettrque | 0:bdca5e4773dd | 53 | } |
mettrque | 0:bdca5e4773dd | 54 | |
mettrque | 0:bdca5e4773dd | 55 | /** |
mettrque | 0:bdca5e4773dd | 56 | * Decodes a given URL string into a standard text string. |
mettrque | 0:bdca5e4773dd | 57 | */ |
mettrque | 6:ef95300898b2 | 58 | string HTTPServer::urlDecoder(string url) |
mettrque | 6:ef95300898b2 | 59 | { |
mettrque | 6:ef95300898b2 | 60 | |
mettrque | 0:bdca5e4773dd | 61 | size_t pos = 0; |
mettrque | 0:bdca5e4773dd | 62 | while ((pos = url.find("+")) != string::npos) url = url.substr(0, pos)+" "+url.substr(pos+1); |
mettrque | 0:bdca5e4773dd | 63 | while ((pos = url.find("%08")) != string::npos) url = url.substr(0, pos)+"\b"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 64 | while ((pos = url.find("%09")) != string::npos) url = url.substr(0, pos)+"\t"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 65 | while ((pos = url.find("%0A")) != string::npos) url = url.substr(0, pos)+"\n"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 66 | while ((pos = url.find("%0D")) != string::npos) url = url.substr(0, pos)+"\r"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 67 | while ((pos = url.find("%20")) != string::npos) url = url.substr(0, pos)+" "+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 68 | while ((pos = url.find("%22")) != string::npos) url = url.substr(0, pos)+"\""+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 69 | while ((pos = url.find("%23")) != string::npos) url = url.substr(0, pos)+"#"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 70 | while ((pos = url.find("%24")) != string::npos) url = url.substr(0, pos)+"$"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 71 | while ((pos = url.find("%25")) != string::npos) url = url.substr(0, pos)+"%"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 72 | while ((pos = url.find("%26")) != string::npos) url = url.substr(0, pos)+"&"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 73 | while ((pos = url.find("%2B")) != string::npos) url = url.substr(0, pos)+"+"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 74 | while ((pos = url.find("%2C")) != string::npos) url = url.substr(0, pos)+","+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 75 | while ((pos = url.find("%2F")) != string::npos) url = url.substr(0, pos)+"/"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 76 | while ((pos = url.find("%3A")) != string::npos) url = url.substr(0, pos)+":"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 77 | while ((pos = url.find("%3B")) != string::npos) url = url.substr(0, pos)+";"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 78 | while ((pos = url.find("%3C")) != string::npos) url = url.substr(0, pos)+"<"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 79 | while ((pos = url.find("%3D")) != string::npos) url = url.substr(0, pos)+"="+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 80 | while ((pos = url.find("%3E")) != string::npos) url = url.substr(0, pos)+">"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 81 | while ((pos = url.find("%3F")) != string::npos) url = url.substr(0, pos)+"?"+url.substr(pos+3); |
mettrque | 0:bdca5e4773dd | 82 | while ((pos = url.find("%40")) != string::npos) url = url.substr(0, pos)+"@"+url.substr(pos+3); |
mettrque | 6:ef95300898b2 | 83 | |
mettrque | 0:bdca5e4773dd | 84 | return url; |
mettrque | 0:bdca5e4773dd | 85 | } |
mettrque | 0:bdca5e4773dd | 86 | |
mettrque | 0:bdca5e4773dd | 87 | /** |
mettrque | 0:bdca5e4773dd | 88 | * This <code>run()</code> method binds the TCP/IP server to a given port number |
mettrque | 0:bdca5e4773dd | 89 | * and enters an infinite loop that waits for http requests and then processes |
mettrque | 0:bdca5e4773dd | 90 | * these requests and returns a response. |
mettrque | 0:bdca5e4773dd | 91 | */ |
mettrque | 6:ef95300898b2 | 92 | void HTTPServer::run() |
mettrque | 6:ef95300898b2 | 93 | { |
mettrque | 0:bdca5e4773dd | 94 | // bind the server to a given port number |
mettrque | 6:ef95300898b2 | 95 | |
mettrque | 10:36a2131f636c | 96 | if (server.open(ethernet) < 0) { |
mettrque | 10:36a2131f636c | 97 | printf("Could not open server socket.\n\r"); |
mettrque | 10:36a2131f636c | 98 | server.close(); |
mettrque | 10:36a2131f636c | 99 | } else { |
mettrque | 10:36a2131f636c | 100 | server.bind(PORT_NUMBER); |
mettrque | 10:36a2131f636c | 101 | server.listen(5); |
mettrque | 6:ef95300898b2 | 102 | |
mettrque | 10:36a2131f636c | 103 | // enter infinite loop |
mettrque | 6:ef95300898b2 | 104 | |
mettrque | 10:36a2131f636c | 105 | while (true) { |
mettrque | 6:ef95300898b2 | 106 | |
mettrque | 10:36a2131f636c | 107 | TCPSocket client; |
mettrque | 10:36a2131f636c | 108 | server.accept(&client); |
mettrque | 10:36a2131f636c | 109 | client.set_blocking(false); // set nonblocking mode |
mettrque | 10:36a2131f636c | 110 | client.set_timeout(1500); // set timeout after 1500 ms |
mettrque | 6:ef95300898b2 | 111 | |
mettrque | 10:36a2131f636c | 112 | // read input |
mettrque | 10:36a2131f636c | 113 | |
mettrque | 10:36a2131f636c | 114 | char inputBuffer[2048]; |
mettrque | 10:36a2131f636c | 115 | int32_t inputSize = client.recv(inputBuffer, sizeof(inputBuffer)); |
mettrque | 6:ef95300898b2 | 116 | |
mettrque | 10:36a2131f636c | 117 | if (inputSize > 0) { |
mettrque | 10:36a2131f636c | 118 | |
mettrque | 10:36a2131f636c | 119 | string input(inputBuffer, inputSize); |
mettrque | 10:36a2131f636c | 120 | string header; |
mettrque | 10:36a2131f636c | 121 | string output; |
mettrque | 6:ef95300898b2 | 122 | |
mettrque | 10:36a2131f636c | 123 | // parse input |
mettrque | 10:36a2131f636c | 124 | |
mettrque | 10:36a2131f636c | 125 | if ((input.find("GET") == 0) || (input.find("HEAD") == 0)) { |
mettrque | 10:36a2131f636c | 126 | |
mettrque | 10:36a2131f636c | 127 | if (input.find("cgi-bin") != string::npos) { |
mettrque | 6:ef95300898b2 | 128 | |
mettrque | 10:36a2131f636c | 129 | // process script request with arguments |
mettrque | 6:ef95300898b2 | 130 | |
mettrque | 10:36a2131f636c | 131 | string script = input.substr(input.find("cgi-bin/")+8, input.find(" ", input.find("cgi-bin/")+8)-input.find("cgi-bin/")-8); |
mettrque | 10:36a2131f636c | 132 | string name; |
mettrque | 10:36a2131f636c | 133 | vector<string> names; |
mettrque | 10:36a2131f636c | 134 | vector<string> values; |
mettrque | 6:ef95300898b2 | 135 | |
mettrque | 10:36a2131f636c | 136 | if (script.find("?") != string::npos) { |
mettrque | 6:ef95300898b2 | 137 | |
mettrque | 10:36a2131f636c | 138 | name = script.substr(0, script.find("?")); |
mettrque | 10:36a2131f636c | 139 | script = script.substr(script.find("?")+1); |
mettrque | 6:ef95300898b2 | 140 | |
mettrque | 10:36a2131f636c | 141 | vector<string> arguments; |
mettrque | 10:36a2131f636c | 142 | while (script.find("&") != string::npos) { |
mettrque | 10:36a2131f636c | 143 | arguments.push_back(script.substr(0, script.find("&"))); |
mettrque | 10:36a2131f636c | 144 | script = script.substr(script.find("&")+1); |
mettrque | 10:36a2131f636c | 145 | } |
mettrque | 10:36a2131f636c | 146 | arguments.push_back(script); |
mettrque | 10:36a2131f636c | 147 | |
mettrque | 10:36a2131f636c | 148 | for (int32_t i = 0; i < arguments.size(); i++) { |
mettrque | 10:36a2131f636c | 149 | |
mettrque | 10:36a2131f636c | 150 | if (arguments[i].find("=") != string::npos) { |
mettrque | 6:ef95300898b2 | 151 | |
mettrque | 10:36a2131f636c | 152 | names.push_back(arguments[i].substr(0, arguments[i].find("="))); |
mettrque | 10:36a2131f636c | 153 | values.push_back(urlDecoder(arguments[i].substr(arguments[i].find("=")+1))); |
mettrque | 10:36a2131f636c | 154 | |
mettrque | 10:36a2131f636c | 155 | } else { |
mettrque | 6:ef95300898b2 | 156 | |
mettrque | 10:36a2131f636c | 157 | names.push_back(arguments[i]); |
mettrque | 10:36a2131f636c | 158 | values.push_back(""); |
mettrque | 10:36a2131f636c | 159 | } |
mettrque | 10:36a2131f636c | 160 | } |
mettrque | 6:ef95300898b2 | 161 | |
mettrque | 10:36a2131f636c | 162 | } else { |
mettrque | 6:ef95300898b2 | 163 | |
mettrque | 10:36a2131f636c | 164 | name = script; |
mettrque | 0:bdca5e4773dd | 165 | } |
mettrque | 10:36a2131f636c | 166 | |
mettrque | 10:36a2131f636c | 167 | // look for corresponding script |
mettrque | 6:ef95300898b2 | 168 | |
mettrque | 10:36a2131f636c | 169 | for (int32_t i = 0; i < min(httpScriptNames.size(), httpScripts.size()); i++) { |
mettrque | 10:36a2131f636c | 170 | |
mettrque | 10:36a2131f636c | 171 | if (httpScriptNames[i].compare(name) == 0) { |
mettrque | 6:ef95300898b2 | 172 | |
mettrque | 10:36a2131f636c | 173 | output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"; |
mettrque | 10:36a2131f636c | 174 | output += "<!DOCTYPE html>\r\n"; |
mettrque | 10:36a2131f636c | 175 | output += "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\r\n"; |
mettrque | 10:36a2131f636c | 176 | output += "<body>\r\n"; |
mettrque | 10:36a2131f636c | 177 | output += httpScripts[i]->call(names, values); |
mettrque | 10:36a2131f636c | 178 | output += "</body>\r\n"; |
mettrque | 10:36a2131f636c | 179 | output += "</html>\r\n"; |
mettrque | 6:ef95300898b2 | 180 | |
mettrque | 10:36a2131f636c | 181 | header = "HTTP/1.1 200 OK\r\n"; |
mettrque | 10:36a2131f636c | 182 | header += "Content-Length: "+int2String(output.size())+"\r\n"; |
mettrque | 10:36a2131f636c | 183 | header += "Content-Type: text/xml\r\n"; |
mettrque | 10:36a2131f636c | 184 | header += "Expires: 0\r\n"; |
mettrque | 10:36a2131f636c | 185 | header += "\r\n"; |
mettrque | 6:ef95300898b2 | 186 | |
mettrque | 10:36a2131f636c | 187 | output = header+output; |
mettrque | 0:bdca5e4773dd | 188 | } |
mettrque | 0:bdca5e4773dd | 189 | } |
mettrque | 6:ef95300898b2 | 190 | |
mettrque | 10:36a2131f636c | 191 | // requested script was not found on this server |
mettrque | 6:ef95300898b2 | 192 | |
mettrque | 10:36a2131f636c | 193 | if (output.size() == 0) { |
mettrque | 6:ef95300898b2 | 194 | |
mettrque | 0:bdca5e4773dd | 195 | output += "<!DOCTYPE html>\r\n"; |
mettrque | 10:36a2131f636c | 196 | output += "<html lang=\"en\">\r\n"; |
mettrque | 10:36a2131f636c | 197 | output += "<head>\r\n"; |
mettrque | 10:36a2131f636c | 198 | output += " <title>400 Bad Request</title>\r\n"; |
mettrque | 10:36a2131f636c | 199 | output += " <style type=\"text/css\">\r\n"; |
mettrque | 10:36a2131f636c | 200 | output += " h2 {font-family:Helvetica,Arial,sans-serif; font-size: 24; color:#FFFFFF;}\r\n"; |
mettrque | 10:36a2131f636c | 201 | output += " p {font-family:Helvetica,Arial,sans-serif; font-size: 14; color:#444444;}\r\n"; |
mettrque | 10:36a2131f636c | 202 | output += " </style>\r\n"; |
mettrque | 10:36a2131f636c | 203 | output += "</head>\r\n"; |
mettrque | 10:36a2131f636c | 204 | output += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\r\n"; |
mettrque | 10:36a2131f636c | 205 | output += " <table width=\"100%\" height=\"100%\" border=\"0\" frame=\"void\" cellspacing=\"0\" cellpadding=\"20\">\r\n"; |
mettrque | 10:36a2131f636c | 206 | output += " <tr>\r\n"; |
mettrque | 10:36a2131f636c | 207 | output += " <td width=\"100%\" height=\"30\" bgcolor=\"#0064A6\"><h2>400 Bad Request</h2></td>\r\n"; |
mettrque | 10:36a2131f636c | 208 | output += " </tr>\r\n"; |
mettrque | 10:36a2131f636c | 209 | output += " <tr>\r\n"; |
mettrque | 10:36a2131f636c | 210 | output += " <td valign=\"top\" style=\"border-top-width:2px; border-left-width:0px; border-bottom-width:0px; border-right-width:0px; border-style:solid; border-color:#444444;\">\r\n"; |
mettrque | 10:36a2131f636c | 211 | output += " <p>The requested script could not be found on this server!</p>\r\n"; |
mettrque | 10:36a2131f636c | 212 | output += " </td>\r\n"; |
mettrque | 10:36a2131f636c | 213 | output += " </tr>\r\n"; |
mettrque | 10:36a2131f636c | 214 | output += " </table>\r\n"; |
mettrque | 0:bdca5e4773dd | 215 | output += "</body>\r\n"; |
mettrque | 0:bdca5e4773dd | 216 | output += "</html>\r\n"; |
mettrque | 6:ef95300898b2 | 217 | |
mettrque | 10:36a2131f636c | 218 | header = "HTTP/1.1 404 Not Found\r\n"; |
mettrque | 0:bdca5e4773dd | 219 | header += "Content-Length: "+int2String(output.size())+"\r\n"; |
mettrque | 10:36a2131f636c | 220 | header += "Content-Type: text/html\r\n"; |
mettrque | 0:bdca5e4773dd | 221 | header += "\r\n"; |
mettrque | 6:ef95300898b2 | 222 | |
mettrque | 0:bdca5e4773dd | 223 | output = header+output; |
mettrque | 0:bdca5e4773dd | 224 | } |
mettrque | 10:36a2131f636c | 225 | |
mettrque | 10:36a2131f636c | 226 | client.send((char*)(output.c_str()), output.size()); |
mettrque | 10:36a2131f636c | 227 | |
mettrque | 10:36a2131f636c | 228 | } else { |
mettrque | 10:36a2131f636c | 229 | |
mettrque | 10:36a2131f636c | 230 | // look for file to load and transmit |
mettrque | 10:36a2131f636c | 231 | |
mettrque | 10:36a2131f636c | 232 | string filename = input.substr(input.find("/")+1, input.find(" ", input.find("/"))-input.find("/")-1); |
mettrque | 10:36a2131f636c | 233 | if (filename.size() == 0) filename = "controller.html"; |
mettrque | 10:36a2131f636c | 234 | |
mettrque | 10:36a2131f636c | 235 | filename = "/local/"+filename; |
mettrque | 10:36a2131f636c | 236 | |
mettrque | 10:36a2131f636c | 237 | FILE* file = fopen(filename.c_str(), "r"); |
mettrque | 10:36a2131f636c | 238 | if (file) { |
mettrque | 10:36a2131f636c | 239 | |
mettrque | 10:36a2131f636c | 240 | // requested file exists |
mettrque | 10:36a2131f636c | 241 | fseek(file, 0, SEEK_END); |
mettrque | 10:36a2131f636c | 242 | long n = ftell(file); |
mettrque | 10:36a2131f636c | 243 | //printf("n=%d\r\n",n); |
mettrque | 10:36a2131f636c | 244 | |
mettrque | 10:36a2131f636c | 245 | |
mettrque | 10:36a2131f636c | 246 | header = "HTTP/1.1 200 OK\r\n"; |
mettrque | 10:36a2131f636c | 247 | header += "Content-Length: "+int2String(n)+"\r\n"; |
mettrque | 10:36a2131f636c | 248 | |
mettrque | 10:36a2131f636c | 249 | if (filename.find(".htm") != string::npos) header += "Content-Type: text/html\r\n"; |
mettrque | 10:36a2131f636c | 250 | else if (filename.find(".txt") != string::npos) header += "Content-Type: text/plain\r\n"; |
mettrque | 10:36a2131f636c | 251 | else if (filename.find(".asc") != string::npos) header += "Content-Type: text/plain\r\n"; |
mettrque | 10:36a2131f636c | 252 | else if (filename.find(".css") != string::npos) header += "Content-Type: text/css\r\n"; |
mettrque | 10:36a2131f636c | 253 | else if (filename.find(".c") != string::npos) header += "Content-Type: text/plain\r\n"; |
mettrque | 10:36a2131f636c | 254 | else if (filename.find(".xml") != string::npos) header += "Content-Type: text/xml\r\n"; |
mettrque | 10:36a2131f636c | 255 | else if (filename.find(".dtd") != string::npos) header += "Content-Type: text/xml\r\n"; |
mettrque | 10:36a2131f636c | 256 | else if (filename.find(".js") != string::npos) header += "Content-Type: text/javascript\r\n"; |
mettrque | 10:36a2131f636c | 257 | else if (filename.find(".gif") != string::npos) header += "Content-Type: image/gif\r\n"; |
mettrque | 10:36a2131f636c | 258 | else if (filename.find(".jpg") != string::npos) header += "Content-Type: image/jpeg\r\n"; |
mettrque | 10:36a2131f636c | 259 | else if (filename.find(".png") != string::npos) header += "Content-Type: image/png\r\n"; |
mettrque | 10:36a2131f636c | 260 | else if (filename.find(".xbm") != string::npos) header += "Content-Type: image/x-xbitmap\r\n"; |
mettrque | 10:36a2131f636c | 261 | else if (filename.find(".xpm") != string::npos) header += "Content-Type: image/x-xpixmap\r\n"; |
mettrque | 10:36a2131f636c | 262 | else if (filename.find(".xwd") != string::npos) header += "Content-Type: image/x-xwindowdump\r\n"; |
mettrque | 10:36a2131f636c | 263 | else if (filename.find(".jar") != string::npos) header += "Content-Type: application/x-java-applet\r\n"; |
mettrque | 10:36a2131f636c | 264 | else if (filename.find(".pdf") != string::npos) header += "Content-Type: application/pdf\r\n"; |
mettrque | 10:36a2131f636c | 265 | else if (filename.find(".sig") != string::npos) header += "Content-Type: application/pgp-signature\r\n"; |
mettrque | 10:36a2131f636c | 266 | else if (filename.find(".spl") != string::npos) header += "Content-Type: application/futuresplash\r\n"; |
mettrque | 10:36a2131f636c | 267 | else if (filename.find(".ps") != string::npos) header += "Content-Type: application/postscript\r\n"; |
mettrque | 10:36a2131f636c | 268 | else if (filename.find(".dvi") != string::npos) header += "Content-Type: application/x-dvi\r\n"; |
mettrque | 10:36a2131f636c | 269 | else if (filename.find(".pac") != string::npos) header += "Content-Type: application/x-ns-proxy-autoconfig\r\n"; |
mettrque | 10:36a2131f636c | 270 | else if (filename.find(".swf") != string::npos) header += "Content-Type: application/x-shockwave-flash\r\n"; |
mettrque | 10:36a2131f636c | 271 | else if (filename.find(".tar.gz") != string::npos) header += "Content-Type: application/x-tgz\r\n"; |
mettrque | 10:36a2131f636c | 272 | else if (filename.find(".tar.bz2") != string::npos) header += "Content-Type: application/x-bzip-compressed-tar\r\n"; |
mettrque | 10:36a2131f636c | 273 | else if (filename.find(".gz") != string::npos) header += "Content-Type: application/x-gzip\r\n"; |
mettrque | 10:36a2131f636c | 274 | else if (filename.find(".tgz") != string::npos) header += "Content-Type: application/x-tgz\r\n"; |
mettrque | 10:36a2131f636c | 275 | else if (filename.find(".tar") != string::npos) header += "Content-Type: application/x-tar\r\n"; |
mettrque | 10:36a2131f636c | 276 | else if (filename.find(".bz2") != string::npos) header += "Content-Type: application/x-bzip\r\n"; |
mettrque | 10:36a2131f636c | 277 | else if (filename.find(".tbz") != string::npos) header += "Content-Type: application/x-bzip-compressed-tar\r\n"; |
mettrque | 10:36a2131f636c | 278 | else if (filename.find(".zip") != string::npos) header += "Content-Type: application/zip\r\n"; |
mettrque | 10:36a2131f636c | 279 | else if (filename.find(".mp3") != string::npos) header += "Content-Type: audio/mpeg\r\n"; |
mettrque | 10:36a2131f636c | 280 | else if (filename.find(".m3u") != string::npos) header += "Content-Type: audio/x-mpegurl\r\n"; |
mettrque | 10:36a2131f636c | 281 | else if (filename.find(".wma") != string::npos) header += "Content-Type: audio/x-ms-wma\r\n"; |
mettrque | 10:36a2131f636c | 282 | else if (filename.find(".wax") != string::npos) header += "Content-Type: audio/x-ms-wax\r\n"; |
mettrque | 10:36a2131f636c | 283 | else if (filename.find(".wav") != string::npos) header += "Content-Type: audio/x-wav\r\n"; |
mettrque | 10:36a2131f636c | 284 | else if (filename.find(".ogg") != string::npos) header += "Content-Type: audio/x-wav\r\n"; |
mettrque | 10:36a2131f636c | 285 | else if (filename.find(".mpg") != string::npos) header += "Content-Type: video/mpeg\r\n"; |
mettrque | 10:36a2131f636c | 286 | else if (filename.find(".mp4") != string::npos) header += "Content-Type: video/mp4\r\n"; |
mettrque | 10:36a2131f636c | 287 | else if (filename.find(".mov") != string::npos) header += "Content-Type: video/quicktime\r\n"; |
mettrque | 10:36a2131f636c | 288 | else if (filename.find(".qt") != string::npos) header += "Content-Type: video/quicktime\r\n"; |
mettrque | 10:36a2131f636c | 289 | else if (filename.find(".ogv") != string::npos) header += "Content-Type: video/ogg\r\n"; |
mettrque | 10:36a2131f636c | 290 | else if (filename.find(".avi") != string::npos) header += "Content-Type: video/x-msvideo\r\n"; |
mettrque | 10:36a2131f636c | 291 | else if (filename.find(".asf") != string::npos) header += "Content-Type: video/x-ms-asf\r\n"; |
mettrque | 10:36a2131f636c | 292 | else if (filename.find(".asx") != string::npos) header += "Content-Type: video/x-ms-asf\r\n"; |
mettrque | 10:36a2131f636c | 293 | else if (filename.find(".wmv") != string::npos) header += "Content-Type: video/x-ms-wmv\r\n"; |
mettrque | 10:36a2131f636c | 294 | |
mettrque | 10:36a2131f636c | 295 | header += "\r\n"; |
mettrque | 10:36a2131f636c | 296 | |
mettrque | 10:36a2131f636c | 297 | client.send((uint8_t*)(header.c_str()), header.size()); // BEFORE SEND_ALL |
mettrque | 10:36a2131f636c | 298 | |
mettrque | 10:36a2131f636c | 299 | |
mettrque | 10:36a2131f636c | 300 | |
mettrque | 10:36a2131f636c | 301 | if (input.find("GET") == 0) { |
mettrque | 10:36a2131f636c | 302 | |
mettrque | 10:36a2131f636c | 303 | // transmit file |
mettrque | 10:36a2131f636c | 304 | |
mettrque | 10:36a2131f636c | 305 | fseek(file, 0, SEEK_SET); |
mettrque | 10:36a2131f636c | 306 | char* fileBuffer = new char[1024]; |
mettrque | 10:36a2131f636c | 307 | //uint8_t fileBuffer[1024]; |
mettrque | 10:36a2131f636c | 308 | int read = 0; |
mettrque | 10:36a2131f636c | 309 | while ((read = fread(fileBuffer, 1, 1024, file)) > 0) { |
mettrque | 10:36a2131f636c | 310 | int32_t send = client.send(fileBuffer, read); |
mettrque | 10:36a2131f636c | 311 | fseek(file,-(read-send), SEEK_CUR); |
mettrque | 10:36a2131f636c | 312 | } |
mettrque | 10:36a2131f636c | 313 | delete[] fileBuffer; |
mettrque | 10:36a2131f636c | 314 | } |
mettrque | 10:36a2131f636c | 315 | fclose(file); |
mettrque | 10:36a2131f636c | 316 | |
mettrque | 10:36a2131f636c | 317 | } else { |
mettrque | 10:36a2131f636c | 318 | |
mettrque | 10:36a2131f636c | 319 | // file not found |
mettrque | 10:36a2131f636c | 320 | |
mettrque | 10:36a2131f636c | 321 | output += "<!DOCTYPE html>\r\n"; |
mettrque | 10:36a2131f636c | 322 | output += "<html lang=\"en\">\r\n"; |
mettrque | 10:36a2131f636c | 323 | output += "<head>\r\n"; |
mettrque | 10:36a2131f636c | 324 | output += "<title>404 Not Found</title>\r\n"; |
mettrque | 10:36a2131f636c | 325 | output += "<style type=\"text/css\">\r\n"; |
mettrque | 10:36a2131f636c | 326 | output += " h2 {font-family:Helvetica,Arial,sans-serif; font-size: 24; color:#FFFFFF;}\r\n"; |
mettrque | 10:36a2131f636c | 327 | output += " p {font-family:Helvetica,Arial,sans-serif; font-size: 14; color:#444444;}\r\n"; |
mettrque | 10:36a2131f636c | 328 | output += "</style>\r\n"; |
mettrque | 10:36a2131f636c | 329 | output += "</head>\r\n"; |
mettrque | 10:36a2131f636c | 330 | output += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\r\n"; |
mettrque | 10:36a2131f636c | 331 | output += "<table width=\"100%\" height=\"100%\" border=\"0\" frame=\"void\" cellspacing=\"0\" cellpadding=\"20\">\r\n"; |
mettrque | 10:36a2131f636c | 332 | output += " <tr>\r\n"; |
mettrque | 10:36a2131f636c | 333 | output += " <td width=\"100%\" height=\"30\" bgcolor=\"#0064A6\"><h2>404 Not Found</h2></td>\r\n"; |
mettrque | 10:36a2131f636c | 334 | output += " </tr>\r\n"; |
mettrque | 10:36a2131f636c | 335 | output += " <tr>\r\n"; |
mettrque | 10:36a2131f636c | 336 | output += " <td valign=\"top\" style=\"border-top-width:2px; border-left-width:0px; border-bottom-width:0px; border-right-width:0px; border-style:solid; border-color:#444444;\">\r\n"; |
mettrque | 10:36a2131f636c | 337 | output += " <p>The requested file could not be found on this server!</p>\r\n"; |
mettrque | 10:36a2131f636c | 338 | output += " </td>\r\n"; |
mettrque | 10:36a2131f636c | 339 | output += " </tr>\r\n"; |
mettrque | 10:36a2131f636c | 340 | output += "</table>\r\n"; |
mettrque | 10:36a2131f636c | 341 | output += "</body>\r\n"; |
mettrque | 10:36a2131f636c | 342 | output += "</html>\r\n"; |
mettrque | 10:36a2131f636c | 343 | |
mettrque | 10:36a2131f636c | 344 | header = "HTTP/1.1 404 Not Found\r\n"; |
mettrque | 10:36a2131f636c | 345 | header += "Content-Length: "+int2String(output.size())+"\r\n"; |
mettrque | 10:36a2131f636c | 346 | header += "Content-Type: text/html\r\n"; |
mettrque | 10:36a2131f636c | 347 | header += "\r\n"; |
mettrque | 10:36a2131f636c | 348 | |
mettrque | 10:36a2131f636c | 349 | output = header+output; |
mettrque | 10:36a2131f636c | 350 | |
mettrque | 10:36a2131f636c | 351 | client.send((char*)(output.c_str()), output.size()); |
mettrque | 10:36a2131f636c | 352 | } |
mettrque | 0:bdca5e4773dd | 353 | } |
mettrque | 6:ef95300898b2 | 354 | |
mettrque | 6:ef95300898b2 | 355 | |
mettrque | 6:ef95300898b2 | 356 | |
mettrque | 0:bdca5e4773dd | 357 | } else { |
mettrque | 6:ef95300898b2 | 358 | |
mettrque | 10:36a2131f636c | 359 | // the http method is not known |
mettrque | 6:ef95300898b2 | 360 | |
mettrque | 10:36a2131f636c | 361 | output += "<!DOCTYPE html>\r\n"; |
mettrque | 10:36a2131f636c | 362 | output += "<html lang=\"en\">\r\n"; |
mettrque | 10:36a2131f636c | 363 | output += "<head>\r\n"; |
mettrque | 10:36a2131f636c | 364 | output += "<title>400 Bad Request</title>\r\n"; |
mettrque | 10:36a2131f636c | 365 | output += "<style type=\"text/css\">\r\n"; |
mettrque | 10:36a2131f636c | 366 | output += " h2 {font-family:Helvetica,Arial,sans-serif; font-size: 24; color:#FFFFFF;}\r\n"; |
mettrque | 10:36a2131f636c | 367 | output += " p {font-family:Helvetica,Arial,sans-serif; font-size: 14; color:#444444;}\r\n"; |
mettrque | 10:36a2131f636c | 368 | output += "</style>\r\n"; |
mettrque | 10:36a2131f636c | 369 | output += "</head>\r\n"; |
mettrque | 10:36a2131f636c | 370 | output += "<body leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">\r\n"; |
mettrque | 10:36a2131f636c | 371 | output += "<table width=\"100%\" height=\"100%\" border=\"0\" frame=\"void\" cellspacing=\"0\" cellpadding=\"20\">\r\n"; |
mettrque | 10:36a2131f636c | 372 | output += " <tr>\r\n"; |
mettrque | 10:36a2131f636c | 373 | output += " <td width=\"100%\" height=\"30\" bgcolor=\"#0064A6\"><h2>400 Bad Request</h2></td>\r\n"; |
mettrque | 10:36a2131f636c | 374 | output += " </tr>\r\n"; |
mettrque | 10:36a2131f636c | 375 | output += " <tr>\r\n"; |
mettrque | 10:36a2131f636c | 376 | output += " <td valign=\"top\" style=\"border-top-width:2px; border-left-width:0px; border-bottom-width:0px; border-right-width:0px; border-style:solid; border-color:#444444;\">\r\n"; |
mettrque | 10:36a2131f636c | 377 | output += " <p>The requested method is not supported by this server!</p>\r\n"; |
mettrque | 10:36a2131f636c | 378 | output += " </td>\r\n"; |
mettrque | 10:36a2131f636c | 379 | output += " </tr>\r\n"; |
mettrque | 10:36a2131f636c | 380 | output += " </table>\r\n"; |
mettrque | 10:36a2131f636c | 381 | output += "</body>\r\n"; |
mettrque | 10:36a2131f636c | 382 | output += "</html>\r\n"; |
mettrque | 6:ef95300898b2 | 383 | |
mettrque | 10:36a2131f636c | 384 | header = "HTTP/1.1 404 Not Found\r\n"; |
mettrque | 10:36a2131f636c | 385 | header += "Content-Length: "+int2String(output.size())+"\r\n"; |
mettrque | 10:36a2131f636c | 386 | header += "Content-Type: text/html\r\n"; |
mettrque | 10:36a2131f636c | 387 | header += "\r\n"; |
mettrque | 6:ef95300898b2 | 388 | |
mettrque | 10:36a2131f636c | 389 | output = header+output; |
mettrque | 6:ef95300898b2 | 390 | |
mettrque | 10:36a2131f636c | 391 | client.send((char*)(output.c_str()), output.size()); |
mettrque | 10:36a2131f636c | 392 | } |
mettrque | 10:36a2131f636c | 393 | } |
mettrque | 6:ef95300898b2 | 394 | |
mettrque | 10:36a2131f636c | 395 | client.close(); |
mettrque | 0:bdca5e4773dd | 396 | } |
mettrque | 0:bdca5e4773dd | 397 | } |
mettrque | 0:bdca5e4773dd | 398 | } |
mettrque | 0:bdca5e4773dd | 399 |