window

Fork of httpServer by hero kim

Committer:
schenk
Date:
Mon Oct 05 06:35:09 2015 +0000
Revision:
2:4c6d58b112d3
Parent:
0:e59cc54df17c
window

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hjjeon 0:e59cc54df17c 1 /* FsHandler.cpp */
hjjeon 0:e59cc54df17c 2 #include "mbed.h"
hjjeon 0:e59cc54df17c 3 #include "FsHandler.h"
hjjeon 0:e59cc54df17c 4 //#define DEBUG
hjjeon 0:e59cc54df17c 5 #include "hl_debug.h"
hjjeon 0:e59cc54df17c 6
schenk 2:4c6d58b112d3 7
hjjeon 0:e59cc54df17c 8
hjjeon 0:e59cc54df17c 9 DigitalIn din(PC_14);
schenk 2:4c6d58b112d3 10 int mode2 = 0;
hjjeon 0:e59cc54df17c 11
hjjeon 0:e59cc54df17c 12 static int matchstrings(const char* one, const char* two)
hjjeon 0:e59cc54df17c 13 {
hjjeon 0:e59cc54df17c 14 int m = 0;
hjjeon 0:e59cc54df17c 15
hjjeon 0:e59cc54df17c 16 for (m = 0; m < min(strlen(one), strlen(two)) ; m++) {
hjjeon 0:e59cc54df17c 17 if (one[m] != two[m])
hjjeon 0:e59cc54df17c 18 return m;
hjjeon 0:e59cc54df17c 19 }
hjjeon 0:e59cc54df17c 20 return m;
hjjeon 0:e59cc54df17c 21 }
hjjeon 0:e59cc54df17c 22
hjjeon 0:e59cc54df17c 23 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap;
hjjeon 0:e59cc54df17c 24
hjjeon 0:e59cc54df17c 25 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
hjjeon 0:e59cc54df17c 26 : HTTPRequestHandler(Msg, Tcp)
hjjeon 0:e59cc54df17c 27 {
hjjeon 0:e59cc54df17c 28 m_rootPath = rootPath;
hjjeon 0:e59cc54df17c 29 m_localPath = localPath;
hjjeon 0:e59cc54df17c 30
hjjeon 0:e59cc54df17c 31 string myPath = m_rootPath + m_localPath;
hjjeon 0:e59cc54df17c 32
hjjeon 0:e59cc54df17c 33 // Now replace the virtual root path with a mounted device path
hjjeon 0:e59cc54df17c 34 std::map<const char*, const char*>::iterator it;
hjjeon 0:e59cc54df17c 35 const char *bestMatch = NULL;
hjjeon 0:e59cc54df17c 36 const char *bestMatchExchange = NULL;
hjjeon 0:e59cc54df17c 37 int match_ind = -1;
hjjeon 0:e59cc54df17c 38 for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) {
hjjeon 0:e59cc54df17c 39 // find best match (if the given logical path is containted in the root
hjjeon 0:e59cc54df17c 40 int s = matchstrings(myPath.c_str(), it->second);
hjjeon 0:e59cc54df17c 41 INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s);
hjjeon 0:e59cc54df17c 42 if ((s == strlen(it->second)) && (s > match_ind)) {
hjjeon 0:e59cc54df17c 43 match_ind = s;
hjjeon 0:e59cc54df17c 44 bestMatch = it->first;
hjjeon 0:e59cc54df17c 45 bestMatchExchange = it->second;
hjjeon 0:e59cc54df17c 46 }
hjjeon 0:e59cc54df17c 47 }
hjjeon 0:e59cc54df17c 48
hjjeon 0:e59cc54df17c 49 if (bestMatch != NULL) {
hjjeon 0:e59cc54df17c 50 m_rootPath = bestMatch;
hjjeon 0:e59cc54df17c 51 m_localPath = string(myPath).substr(strlen(bestMatchExchange));
hjjeon 0:e59cc54df17c 52 }
hjjeon 0:e59cc54df17c 53
hjjeon 0:e59cc54df17c 54 handleRequest();
hjjeon 0:e59cc54df17c 55 }
hjjeon 0:e59cc54df17c 56
hjjeon 0:e59cc54df17c 57 HTTPFsRequestHandler::~HTTPFsRequestHandler()
hjjeon 0:e59cc54df17c 58 {
hjjeon 0:e59cc54df17c 59 }
hjjeon 0:e59cc54df17c 60
hjjeon 0:e59cc54df17c 61 int HTTPFsRequestHandler::handleGetRequest()
hjjeon 0:e59cc54df17c 62 {
hjjeon 0:e59cc54df17c 63 HTTPHeaders headers;
hjjeon 0:e59cc54df17c 64 int retval = 0; //success
hjjeon 0:e59cc54df17c 65 uint8_t pin_state;
hjjeon 0:e59cc54df17c 66
hjjeon 0:e59cc54df17c 67 if( std::string::npos != msg.uri.find("get_dio14.cgi") )
hjjeon 0:e59cc54df17c 68 {
hjjeon 0:e59cc54df17c 69 if(din)
hjjeon 0:e59cc54df17c 70 pin_state = 1;
hjjeon 0:e59cc54df17c 71 else
hjjeon 0:e59cc54df17c 72 pin_state = 0;
hjjeon 0:e59cc54df17c 73
hjjeon 0:e59cc54df17c 74 /*
hjjeon 0:e59cc54df17c 75 *len = sprintf((char *)buf, "DioCallback({\"dio_p\":\"14\",\
hjjeon 0:e59cc54df17c 76 \"dio_s\":\"%d\"\
hjjeon 0:e59cc54df17c 77 });",
hjjeon 0:e59cc54df17c 78 pin_state // Digital io status
hjjeon 0:e59cc54df17c 79 );
hjjeon 0:e59cc54df17c 80
hjjeon 0:e59cc54df17c 81
hjjeon 0:e59cc54df17c 82 Tcp.
hjjeon 0:e59cc54df17c 83 */
hjjeon 0:e59cc54df17c 84 }
hjjeon 0:e59cc54df17c 85 else //read html pages
hjjeon 0:e59cc54df17c 86 {
hjjeon 0:e59cc54df17c 87 if (m_localPath.length() > 4)
hjjeon 0:e59cc54df17c 88 getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str());
hjjeon 0:e59cc54df17c 89 else
hjjeon 0:e59cc54df17c 90 getStandardHeaders(headers);
hjjeon 0:e59cc54df17c 91
hjjeon 0:e59cc54df17c 92 INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str());
hjjeon 0:e59cc54df17c 93
hjjeon 0:e59cc54df17c 94 std::string reqPath;
hjjeon 0:e59cc54df17c 95
hjjeon 0:e59cc54df17c 96 // Check if we received a directory with the local bath
hjjeon 0:e59cc54df17c 97 if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) {
hjjeon 0:e59cc54df17c 98 // yes, we shall append the default page name
schenk 2:4c6d58b112d3 99 m_localPath += "dio_page.html";
hjjeon 0:e59cc54df17c 100 }
hjjeon 0:e59cc54df17c 101
hjjeon 0:e59cc54df17c 102 reqPath = m_rootPath + m_localPath;
hjjeon 0:e59cc54df17c 103
hjjeon 0:e59cc54df17c 104 INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str());
hjjeon 0:e59cc54df17c 105
hjjeon 0:e59cc54df17c 106 FILE *fp = fopen(reqPath.c_str(), "r");
hjjeon 0:e59cc54df17c 107 if (fp != NULL) {
hjjeon 0:e59cc54df17c 108 char * pBuffer = NULL;
hjjeon 0:e59cc54df17c 109 int sz = 8192;
hjjeon 0:e59cc54df17c 110 while( pBuffer == NULL) {
hjjeon 0:e59cc54df17c 111 sz /= 2;
hjjeon 0:e59cc54df17c 112 pBuffer = (char*)malloc(sz);
hjjeon 0:e59cc54df17c 113 if (sz < 128)
hjjeon 0:e59cc54df17c 114 error ("OutOfMemory");
hjjeon 0:e59cc54df17c 115 }
hjjeon 0:e59cc54df17c 116
hjjeon 0:e59cc54df17c 117 // File was found and can be returned
hjjeon 0:e59cc54df17c 118
hjjeon 0:e59cc54df17c 119 // first determine the size
hjjeon 0:e59cc54df17c 120 fseek(fp, 0, SEEK_END);
hjjeon 0:e59cc54df17c 121 long size = ftell(fp);
hjjeon 0:e59cc54df17c 122 fseek(fp, 0, SEEK_SET);
hjjeon 0:e59cc54df17c 123
hjjeon 0:e59cc54df17c 124 startResponse(200, size);
hjjeon 0:e59cc54df17c 125 while(!feof(fp) && !ferror(fp)) {
hjjeon 0:e59cc54df17c 126 int cnt = fread(pBuffer, 1, sz , fp);
hjjeon 0:e59cc54df17c 127 if (cnt < 0)
hjjeon 0:e59cc54df17c 128 cnt = 0;
hjjeon 0:e59cc54df17c 129 processResponse(cnt, pBuffer);
hjjeon 0:e59cc54df17c 130 }
hjjeon 0:e59cc54df17c 131 delete pBuffer;
hjjeon 0:e59cc54df17c 132 endResponse();
hjjeon 0:e59cc54df17c 133 fclose(fp);
hjjeon 0:e59cc54df17c 134 }
hjjeon 0:e59cc54df17c 135 else {
hjjeon 0:e59cc54df17c 136 retval = 404;
hjjeon 0:e59cc54df17c 137 ERR("Requested file was not found !");
hjjeon 0:e59cc54df17c 138 }
hjjeon 0:e59cc54df17c 139 }
hjjeon 0:e59cc54df17c 140
hjjeon 0:e59cc54df17c 141 return retval;
hjjeon 0:e59cc54df17c 142 }
hjjeon 0:e59cc54df17c 143
hjjeon 0:e59cc54df17c 144 int HTTPFsRequestHandler::handlePostRequest()
hjjeon 0:e59cc54df17c 145 {
hjjeon 0:e59cc54df17c 146
hjjeon 0:e59cc54df17c 147 int pin = 0;
hjjeon 0:e59cc54df17c 148
hjjeon 0:e59cc54df17c 149 if( std::string::npos != msg.uri.find("set_dio.cgi") )
hjjeon 0:e59cc54df17c 150 {
hjjeon 0:e59cc54df17c 151 pin = get_http_param_value("pin");
schenk 2:4c6d58b112d3 152
schenk 2:4c6d58b112d3 153
schenk 2:4c6d58b112d3 154 if(pin == 9)
schenk 2:4c6d58b112d3 155 {
schenk 2:4c6d58b112d3 156 if(get_http_param_value("val") == 0){
schenk 2:4c6d58b112d3 157 DigitalOut(D9,0);
schenk 2:4c6d58b112d3 158 DigitalOut(D8,1);
schenk 2:4c6d58b112d3 159 wait(0.87);
schenk 2:4c6d58b112d3 160 DigitalOut(D9,0);
schenk 2:4c6d58b112d3 161 DigitalOut(D8,0);
schenk 2:4c6d58b112d3 162 }// opened status
schenk 2:4c6d58b112d3 163 else{
schenk 2:4c6d58b112d3 164 DigitalOut(D9,1);
schenk 2:4c6d58b112d3 165 DigitalOut(D8,0);
schenk 2:4c6d58b112d3 166 wait(0.8);
schenk 2:4c6d58b112d3 167 DigitalOut(D9,0);
schenk 2:4c6d58b112d3 168 DigitalOut(D8,0);
schenk 2:4c6d58b112d3 169 //closed status
schenk 2:4c6d58b112d3 170 }
schenk 2:4c6d58b112d3 171
hjjeon 0:e59cc54df17c 172 }
schenk 2:4c6d58b112d3 173 else if(pin == 10)
schenk 2:4c6d58b112d3 174 {
schenk 2:4c6d58b112d3 175 if(get_http_param_value("val") == 0){
schenk 2:4c6d58b112d3 176 DigitalOut(D13,1);
schenk 2:4c6d58b112d3 177 }// turn on
schenk 2:4c6d58b112d3 178 else{
schenk 2:4c6d58b112d3 179 DigitalOut(D13,0);
schenk 2:4c6d58b112d3 180
schenk 2:4c6d58b112d3 181 }//turn off
schenk 2:4c6d58b112d3 182
hjjeon 0:e59cc54df17c 183 }
schenk 2:4c6d58b112d3 184
hjjeon 0:e59cc54df17c 185 else
hjjeon 0:e59cc54df17c 186 {
hjjeon 0:e59cc54df17c 187 WARN("Wrong pin number");
hjjeon 0:e59cc54df17c 188 }
hjjeon 0:e59cc54df17c 189
hjjeon 0:e59cc54df17c 190 return 0;
hjjeon 0:e59cc54df17c 191 }
hjjeon 0:e59cc54df17c 192 else
hjjeon 0:e59cc54df17c 193 {
hjjeon 0:e59cc54df17c 194 return 404;
hjjeon 0:e59cc54df17c 195 }
hjjeon 0:e59cc54df17c 196 }
hjjeon 0:e59cc54df17c 197
hjjeon 0:e59cc54df17c 198 int HTTPFsRequestHandler::handlePutRequest()
hjjeon 0:e59cc54df17c 199 {
hjjeon 0:e59cc54df17c 200 return 404;
hjjeon 0:e59cc54df17c 201 }
hjjeon 0:e59cc54df17c 202
hjjeon 0:e59cc54df17c 203 uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name)
hjjeon 0:e59cc54df17c 204 {
hjjeon 0:e59cc54df17c 205 uint8_t * name = 0;
hjjeon 0:e59cc54df17c 206 uint8_t * pos2;
hjjeon 0:e59cc54df17c 207 uint16_t len = 0;
hjjeon 0:e59cc54df17c 208 char value[10];
hjjeon 0:e59cc54df17c 209 uint32_t ret = 0;
hjjeon 0:e59cc54df17c 210
hjjeon 0:e59cc54df17c 211 //msg.attri
hjjeon 0:e59cc54df17c 212 if((name = (uint8_t *)strstr(msg.attri, param_name)))
hjjeon 0:e59cc54df17c 213 {
hjjeon 0:e59cc54df17c 214 name += strlen(param_name) + 1;
hjjeon 0:e59cc54df17c 215 pos2 = (uint8_t*)strstr((char*)name, "&");
hjjeon 0:e59cc54df17c 216 if(!pos2)
hjjeon 0:e59cc54df17c 217 {
hjjeon 0:e59cc54df17c 218 pos2 = name + strlen((char*)name);
hjjeon 0:e59cc54df17c 219 }
hjjeon 0:e59cc54df17c 220 len = pos2 - name;
hjjeon 0:e59cc54df17c 221
hjjeon 0:e59cc54df17c 222 if(len)
hjjeon 0:e59cc54df17c 223 {
hjjeon 0:e59cc54df17c 224 strncpy(value, (char*)name, len);
hjjeon 0:e59cc54df17c 225 ret = atoi(value);
hjjeon 0:e59cc54df17c 226 }
hjjeon 0:e59cc54df17c 227 }
hjjeon 0:e59cc54df17c 228 return ret;
hjjeon 0:e59cc54df17c 229 }
hjjeon 0:e59cc54df17c 230