Seo-young Yoon / httpServer

Dependents:   Internet-Piano_WIZwiki-W7500

Fork of httpServer by IOP

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FsHandler.cpp Source File

FsHandler.cpp

00001 /* FsHandler.cpp */
00002 #include "mbed.h"
00003 #include "FsHandler.h"
00004 //#define DEBUG
00005 #include "hl_debug.h"
00006 
00007 
00008 
00009 DigitalOut led_red(LED_RED, 1);
00010 DigitalOut led_green(LED_GREEN, 1);
00011 DigitalOut led_blue(LED_BLUE, 1);
00012 
00013 DigitalIn  din(PC_14);
00014 
00015 static int matchstrings(const char* one, const char* two)
00016 {
00017     int m = 0;
00018     
00019     for (m = 0; m < min(strlen(one), strlen(two)) ; m++) {
00020         if (one[m] != two[m])
00021             return m;
00022     }
00023     return m;
00024 }
00025 
00026 std::map<const char*, const char*> HTTPFsRequestHandler::m_fsMap;
00027  
00028 HTTPFsRequestHandler::HTTPFsRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
00029     : HTTPRequestHandler(Msg, Tcp)
00030 {
00031     m_rootPath = rootPath;
00032     m_localPath = localPath;
00033     
00034     string myPath = m_rootPath + m_localPath;
00035     
00036     //  Now replace the virtual root path with a mounted device path
00037     std::map<const char*, const char*>::iterator it;
00038     const char *bestMatch = NULL;
00039     const char *bestMatchExchange = NULL;
00040     int match_ind = -1;
00041     for (it = m_fsMap.begin() ; it != m_fsMap.end() ; it++) {
00042         //  find best match (if the given logical path is containted in the root
00043         int s = matchstrings(myPath.c_str(), it->second);
00044         INFO("Matching Root %s with handler %s results in %d identical characters\n", myPath.c_str(), it->second, s);
00045         if ((s == strlen(it->second)) && (s > match_ind)) {
00046             match_ind = s;
00047             bestMatch = it->first;
00048             bestMatchExchange = it->second;
00049         }
00050     }
00051 
00052     if (bestMatch != NULL) {
00053         m_rootPath = bestMatch;
00054         m_localPath = string(myPath).substr(strlen(bestMatchExchange));
00055     }
00056             
00057     handleRequest();
00058 }
00059 
00060 HTTPFsRequestHandler::~HTTPFsRequestHandler()
00061 {
00062 }
00063 
00064 int HTTPFsRequestHandler::handleGetRequest()
00065 {
00066     HTTPHeaders headers;
00067     int retval = 0;   //success
00068     uint8_t pin_state;
00069     
00070     if( std::string::npos != msg.uri.find("get_dio14.cgi") )
00071     {
00072         if(din)
00073             pin_state = 1;
00074         else 
00075             pin_state = 0;
00076         
00077         /*
00078         *len = sprintf((char *)buf, "DioCallback({\"dio_p\":\"14\",\
00079                                             \"dio_s\":\"%d\"\
00080                                             });",
00081                                             pin_state              // Digital io status
00082                                             );
00083         
00084             
00085         Tcp.
00086         */
00087     }
00088     else //read html pages
00089     {
00090         if (m_localPath.length() > 4) 
00091             getStandardHeaders(headers, m_localPath.substr(m_localPath.length()-4).c_str());
00092         else
00093             getStandardHeaders(headers);
00094         
00095         INFO("Handling Get Request (root = %s, local = %s).", m_rootPath.c_str(), m_localPath.c_str());
00096         
00097         std::string reqPath;
00098     
00099         //  Check if we received a directory with the local bath
00100         if ((m_localPath.length() == 0) || (m_localPath.substr( m_localPath.length()-1, 1) == "/")) {
00101             //  yes, we shall append the default page name
00102             m_localPath += "index.html";
00103         }
00104         
00105         reqPath = m_rootPath + m_localPath;
00106         
00107         INFO("Mapping \"%s\" to \"%s\"", msg.uri.c_str(), reqPath.c_str());
00108             
00109         FILE *fp = fopen(reqPath.c_str(), "r");
00110         if (fp != NULL) {
00111             char * pBuffer = NULL;
00112             int sz = 8192;
00113             while( pBuffer == NULL) {
00114                 sz /= 2;
00115                 pBuffer = (char*)malloc(sz);
00116                 if (sz < 128)
00117                     error ("OutOfMemory");
00118             }
00119             
00120             //  File was found and can be returned
00121         
00122             //  first determine the size
00123             fseek(fp, 0, SEEK_END);
00124             long size = ftell(fp);
00125             fseek(fp, 0, SEEK_SET);
00126         
00127             startResponse(200, size);
00128             while(!feof(fp) && !ferror(fp)) {
00129                 int cnt = fread(pBuffer, 1, sz , fp);
00130                 if (cnt < 0)
00131                     cnt = 0;
00132                 processResponse(cnt, pBuffer);
00133             }
00134             delete pBuffer;
00135             endResponse();
00136             fclose(fp);
00137         }
00138         else {
00139             retval = 404;
00140             ERR("Requested file was not found !");
00141         }
00142     }
00143     
00144     return retval;
00145 }
00146 
00147 int HTTPFsRequestHandler::handlePostRequest()
00148 {
00149     int i = 0;
00150     int pin = 0;
00151     
00152     if( std::string::npos != msg.uri.find("set_LED.cgi") )
00153     {
00154         pin = get_http_param_value("pin");
00155         if(pin==0)      led_red = 0;
00156         else if(pin==1) led_red = 1;
00157         else if(pin==2) led_green = 0;
00158         else
00159         {
00160             WARN("Wrong pin number");
00161         }   
00162         
00163         return 0;
00164     } 
00165     else if( std::string::npos != msg.uri.find("set_AUTO.cgi") )
00166     {
00167         pin = get_http_param_value("pin");
00168         if(pin==0)   
00169         {
00170  
00171         }    
00172         else
00173         {
00174             WARN("Wrong pin number");
00175         }   
00176         
00177         return 0;
00178     }   
00179     else
00180     {
00181            return 404;
00182     }
00183 }
00184 
00185 int HTTPFsRequestHandler::handlePutRequest()
00186 {
00187     return 404;
00188 }
00189 
00190 uint32_t HTTPFsRequestHandler::get_http_param_value(char* param_name)
00191 {
00192     uint8_t * name = 0;
00193     uint8_t * pos2;
00194     uint16_t len = 0;
00195     char value[10];
00196     uint32_t ret = 0;
00197     
00198     //msg.attri
00199     if((name = (uint8_t *)strstr(msg.attri, param_name)))
00200     {
00201         name += strlen(param_name) + 1;
00202         pos2 = (uint8_t*)strstr((char*)name, "&");
00203         if(!pos2)
00204         {
00205             pos2 = name + strlen((char*)name);
00206         }
00207         len = pos2 - name;
00208         
00209         if(len)
00210         {
00211             strncpy(value, (char*)name, len);
00212             ret = atoi(value);
00213         }
00214     }
00215     return ret;
00216 }