Lawrence Lee / Mbed 2 deprecated WIZwiki-REST-io_v102

Dependencies:   Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed

Fork of WIZwiki-REST-io_v101 by Lawrence Lee

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RequestHandler.cpp Source File

RequestHandler.cpp

00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include "RequestHandler.h"
00005 #include "MbedJSONValue.h"
00006 
00007 extern MbedJSONValue WIZwikiREST;
00008 
00009 MbedJSONValue*    pDataJson = 0;
00010 
00011 void GetRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
00012 {
00013     MbedJSONValue* tmpJson;
00014 
00015       char* tok;
00016       char* last;
00017       const char * pchar = 0;
00018 #ifdef DEBUG_HTTPREQ
00019     printf("GetRequestHandler():%s\r\n",rest_uri);
00020 #endif    
00021     if(!strcmp(rest_uri, "/")){
00022         tmpJson = &WIZwikiREST;     
00023     }
00024     else{
00025         tok = strtok_r(rest_uri+1, "/", &last);
00026         tmpJson = &WIZwikiREST;
00027         
00028         char* name = 0;
00029         while(tok)
00030         {
00031 #ifdef DEBUG_HTTPREQ            
00032             printf("tok = %s \r\n", tok);
00033 #endif            
00034             if(tmpJson->hasMember(tok)){
00035                 tmpJson = &((*tmpJson)[tok]);
00036                 //if(!tmpJson->accessible) tmpJson->cb_action(tmpJson);
00037                 name = tok;
00038                 tok = strtok_r(0, "/", &last);
00039             }
00040             else{
00041 #ifndef DEBUG_HTTPREQ                
00042                 printf("No Member\r\n");
00043 #endif                
00044                 break;
00045             }
00046         }
00047         if(name){
00048             if(tok)
00049             {
00050                 if(tmpJson->accessible){
00051 #ifdef DEBUG_HTTPREQ                    
00052                     printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size());
00053 #endif                    
00054                     
00055                     if(tmpJson->size() > 0){
00056                         *tmpJson = std::string(tok); 
00057                         tmpJson->cb_action((void*)tok); 
00058 #ifdef DEBUG_HTTPREQ                        
00059                         printf("set string:%s\r\n",tok);
00060 #endif
00061                     }
00062                     else{
00063                         *tmpJson = atoi(tok); 
00064                         tmpJson->cb_action(&tmpJson->_value); 
00065 #ifdef DEBUG_HTTPREQ                        
00066                         printf("set int:%d\r\n",atoi(tok));
00067 #endif                        
00068                     }
00069                 }
00070                 else{
00071                     strcpy (reply, "HTTP/1.1 403 OK\r\n");
00072                     strcat (reply, "Sever: WIZwiki-REST\r\n");
00073                     strcat (reply, "content-Type: text/json\r\n");
00074                     sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4);
00075                     strcat(reply, "{\"Result : No Accessible\"}");
00076                     strcat (reply, "\r\n\r\n");
00077                     return;
00078                 }
00079             }
00080         }
00081         else{
00082             strcpy (reply, "HTTP/1.1 404 OK\r\n");
00083             strcat (reply, "Sever: WIZwiki-REST\r\n");
00084             strcat (reply, "content-Type: text/json\r\n");
00085             sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4);
00086             strcat(reply, "{\"Result\" : \"No defined Resource\"}");
00087             strcat (reply, "\r\n\r\n");
00088             return;
00089         }
00090     }
00091     pchar = (*tmpJson).serialize().c_str();
00092     strcpy (reply, "HTTP/1.1 200 OK\r\n");
00093     strcat (reply, "Sever: WIZwiki-REST\r\n");
00094     strcat (reply, "content-Type: text/json\r\n");
00095     sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", strlen(pchar)+4);
00096     strcat (reply, pchar);
00097     strcat (reply, "\r\n\r\n");
00098 
00099     return;
00100 }
00101 
00102 void PostRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
00103 {
00104     MbedJSONValue* tmpJson;
00105       char* tok;
00106       char* last;
00107       int errnum = 200;
00108       
00109 #ifdef DEBUG_HTTPREQ
00110     printf("PostRequestHandler():%s\r\n",request_data+4);
00111 #endif
00112     if(!pDataJson){
00113         pDataJson = new MbedJSONValue();  
00114     }
00115 #ifdef DEBUG_HTTPREQ
00116     printf("Before Parse\r\n");
00117     debug_info();
00118 #endif
00119     parse(*pDataJson,(const char*)request_data);
00120 #ifdef DEBUG_HTTPREQ
00121     printf("After Parse\r\n");
00122     debug_info();
00123     printf("DataJson._type=%d\r\n",pDataJson->_type);
00124     printf("DataJson=%s\r\n",pDataJson->serialize().c_str());
00125     printf("DataJson.size()=%d\r\n",pDataJson->size());
00126     printf("DataJason.type=%d\r\n",pDataJson->_type);
00127 #endif    
00128 
00129     if(!strcmp(rest_uri, "/")){
00130         tmpJson = &WIZwikiREST;     
00131     }
00132     else{
00133         tok = strtok_r(rest_uri+1, "/", &last);
00134         tmpJson = &WIZwikiREST;
00135         
00136         char* name = 0;
00137         while(tok)
00138         {
00139 #ifdef DEBUG_HTTPREQ            
00140             printf("tok = %s \r\n", tok);
00141 #endif            
00142             if(tmpJson->hasMember(tok)){
00143                 tmpJson = &((*tmpJson)[tok]);
00144                 name = tok;
00145                 tok = strtok_r(0, "/", &last);
00146             }
00147             else{
00148 #ifdef DEBUG_HTTPREQ                
00149                 printf("No Member\r\n");
00150 #endif                
00151                 break;
00152             }
00153         }
00154         if(name){
00155 #ifdef DEBUG_HTTPREQ                            
00156             printf("Token_name=%s\r\n",name);
00157 #endif
00158             if(tok){
00159 #ifdef DEBUG_HTTPREQ                                            
00160                 printf("It should be no parameters : tok=%s\r\n",tok);
00161 #endif                
00162                 errnum = 403;
00163             }
00164         }
00165         else{
00166             errnum = 404;
00167         }
00168     }
00169     if(errnum != 0){
00170         switch(pDataJson->_type)
00171         {
00172             case MbedJSONValue::TypeInt:
00173                 if(tmpJson->accessible){
00174                     *tmpJson = pDataJson->_value.asInt; 
00175                     tmpJson->cb_action(&tmpJson->_value); 
00176 #ifdef DEBUG_HTTPREQ                        
00177                     printf("set int:%d\r\n",atoi(tok));
00178 #endif                        
00179                 }
00180                 else{
00181                     errnum = 403;
00182                 }
00183                 break;
00184                 
00185             case MbedJSONValue::TypeString:
00186                 if(tmpJson->accessible){
00187                     *tmpJson = pDataJson->_value.asString;
00188                     tmpJson->cb_action((void*)tmpJson->_value.asString->c_str()); 
00189                 }
00190                 else{
00191                     errnum = 403;
00192                 }
00193                 break;
00194                 
00195             case MbedJSONValue::TypeObject:
00196                 for(int i = 0; i < pDataJson->index_token; i++)
00197                 {
00198                     tok = (char*)pDataJson->token_name[i]->c_str();
00199 #ifdef DEBUG_HTTPREQ                                                
00200                     printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok);
00201 #endif                    
00202                     if(tmpJson->hasMember(tok)){
00203                         if((*tmpJson)[tok].accessible){
00204                             errnum = 200;
00205                             if((*pDataJson)[i].size() > 0){
00206                                 //(*tmpJson)[tok] = (*pDataJson)[i]._value.asString;
00207                                 (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str();
00208                                 
00209                                 //(*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 
00210                                 (*tmpJson)[tok].cb_action((void*)((*pDataJson)[i]._value.asString->c_str())); 
00211 #ifdef DEBUG_HTTPREQ                                                            
00212                                 printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str());
00213 #endif                                
00214                             }
00215                             else{
00216                                 (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt;
00217                                 (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 
00218 #ifdef DEBUG_HTTPREQ
00219                                 printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>());
00220 #endif                                
00221                             }
00222                         }
00223                         else{
00224                             errnum = 403; break;
00225                         }
00226                     }
00227                     else{
00228                         errnum = 404; break;
00229                     }
00230                 }
00231                 break;
00232                 
00233             default:
00234                 errnum = 403; break;
00235         }    
00236     }
00237         
00238     switch(errnum)
00239     {
00240         case 403:
00241                 strcpy (reply, "HTTP/1.1 403 OK\r\n");
00242                 strcat (reply, "Sever: WIZwiki-REST\r\n");
00243                 strcat (reply, "content-Type: text/json\r\n");
00244                 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4);
00245                 strcat(reply, "{\"Result : No Accessible\"}");
00246                 strcat (reply, "\r\n\r\n");
00247                 break;
00248             
00249         case 404:
00250                 strcpy (reply, "HTTP/1.1 404 OK\r\n");
00251                 strcat (reply, "Sever: WIZwiki-REST\r\n");
00252                 strcat (reply, "content-Type: text/json\r\n");
00253                 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4);
00254                 strcat(reply, "{\"Result\" : \"No defined Resource\"}");
00255                 strcat (reply, "\r\n\r\n");
00256                 break;
00257             
00258         case 200:
00259                 strcpy (reply, "HTTP/1.1 200 OK\r\n");
00260                 strcat (reply, "Sever: WIZwiki-REST\r\n");
00261                 strcat (reply, "content-Type: text/json\r\n");
00262                 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4);
00263                 strcat (reply, "{\"Result\":\"OK\"}");
00264                 strcat (reply, "\r\n\r\n");
00265                 break;
00266     }
00267     
00268 #ifdef DEBUG_HTTPREQ
00269     printf("Before Delete\r\n");
00270     debug_info();
00271 #endif
00272     if(pDataJson){
00273 #ifdef DEBUG_HTTPREQ
00274         printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token);
00275 #endif
00276         delete pDataJson;
00277         pDataJson = 0;
00278     }
00279 #ifdef DEBUG_HTTPREQ
00280     printf("After Delete\r\n");
00281     debug_info();
00282 #endif
00283 }
00284 
00285 /*
00286 void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
00287 {
00288 }
00289 
00290 void DeleteRequestHandler::handle(char* rest_uri, char* request_data, char *reply)
00291 {
00292 }
00293 */