Upper Version Add PUT method Delete POST method
Dependencies: Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed
Fork of WIZwiki-REST-io_v102 by
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 #ifdef HTTP_POST 00103 void PostRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00104 { 00105 MbedJSONValue* tmpJson; 00106 char* tok; 00107 char* last; 00108 int errnum = 200; 00109 00110 #ifdef DEBUG_HTTPREQ 00111 printf("PostRequestHandler():%s\r\n",request_data+4); 00112 #endif 00113 if(!pDataJson){ 00114 pDataJson = new MbedJSONValue(); 00115 } 00116 #ifdef DEBUG_HTTPREQ 00117 printf("Before Parse\r\n"); 00118 debug_info(); 00119 #endif 00120 parse(*pDataJson,(const char*)request_data); 00121 #ifdef DEBUG_HTTPREQ 00122 printf("After Parse\r\n"); 00123 debug_info(); 00124 printf("DataJson._type=%d\r\n",pDataJson->_type); 00125 printf("DataJson=%s\r\n",pDataJson->serialize().c_str()); 00126 printf("DataJson.size()=%d\r\n",pDataJson->size()); 00127 printf("DataJason.type=%d\r\n",pDataJson->_type); 00128 #endif 00129 00130 if(!strcmp(rest_uri, "/")){ 00131 tmpJson = &WIZwikiREST; 00132 } 00133 else{ 00134 tok = strtok_r(rest_uri+1, "/", &last); 00135 tmpJson = &WIZwikiREST; 00136 00137 char* name = 0; 00138 while(tok) 00139 { 00140 #ifdef DEBUG_HTTPREQ 00141 printf("tok = %s \r\n", tok); 00142 #endif 00143 if(tmpJson->hasMember(tok)){ 00144 tmpJson = &((*tmpJson)[tok]); 00145 name = tok; 00146 tok = strtok_r(0, "/", &last); 00147 } 00148 else{ 00149 #ifdef DEBUG_HTTPREQ 00150 printf("No Member\r\n"); 00151 #endif 00152 break; 00153 } 00154 } 00155 if(name){ 00156 #ifdef DEBUG_HTTPREQ 00157 printf("Token_name=%s\r\n",name); 00158 #endif 00159 if(tok){ 00160 #ifdef DEBUG_HTTPREQ 00161 printf("It should be no parameters : tok=%s\r\n",tok); 00162 #endif 00163 errnum = 403; 00164 } 00165 } 00166 else{ 00167 errnum = 404; 00168 } 00169 } 00170 if(errnum != 0){ 00171 switch(pDataJson->_type) 00172 { 00173 case MbedJSONValue::TypeInt: 00174 if(tmpJson->accessible){ 00175 *tmpJson = pDataJson->_value.asInt; 00176 tmpJson->cb_action(&tmpJson->_value); 00177 #ifdef DEBUG_HTTPREQ 00178 printf("set int:%d\r\n",atoi(tok)); 00179 #endif 00180 } 00181 else{ 00182 errnum = 403; 00183 } 00184 break; 00185 00186 case MbedJSONValue::TypeString: 00187 if(tmpJson->accessible){ 00188 *tmpJson = pDataJson->_value.asString; 00189 tmpJson->cb_action((void*)tmpJson->_value.asString->c_str()); 00190 } 00191 else{ 00192 errnum = 403; 00193 } 00194 break; 00195 00196 case MbedJSONValue::TypeObject: 00197 for(int i = 0; i < pDataJson->index_token; i++) 00198 { 00199 tok = (char*)pDataJson->token_name[i]->c_str(); 00200 #ifdef DEBUG_HTTPREQ 00201 printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok); 00202 #endif 00203 if(tmpJson->hasMember(tok)){ 00204 if((*tmpJson)[tok].accessible){ 00205 errnum = 200; 00206 if((*pDataJson)[i].size() > 0){ 00207 /* 00208 (*tmpJson)[tok] = (*pDataJson)[i]._value.asString; 00209 (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 00210 */ 00211 (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str(); 00212 (*tmpJson)[tok].cb_action((void*)((*pDataJson)[i]._value.asString->c_str())); 00213 #ifdef DEBUG_HTTPREQ 00214 printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str()); 00215 #endif 00216 } 00217 else{ 00218 (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt; 00219 (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 00220 #ifdef DEBUG_HTTPREQ 00221 printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>()); 00222 #endif 00223 } 00224 } 00225 else{ 00226 errnum = 403; break; 00227 } 00228 } 00229 else{ 00230 errnum = 404; break; 00231 } 00232 } 00233 break; 00234 00235 default: 00236 errnum = 403; break; 00237 } 00238 } 00239 00240 switch(errnum) 00241 { 00242 case 403: 00243 strcpy (reply, "HTTP/1.1 403 OK\r\n"); 00244 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00245 strcat (reply, "content-Type: text/json\r\n"); 00246 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); 00247 strcat(reply, "{\"Result : No Accessible\"}"); 00248 strcat (reply, "\r\n\r\n"); 00249 break; 00250 00251 case 404: 00252 strcpy (reply, "HTTP/1.1 404 OK\r\n"); 00253 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00254 strcat (reply, "content-Type: text/json\r\n"); 00255 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); 00256 strcat(reply, "{\"Result\" : \"No defined Resource\"}"); 00257 strcat (reply, "\r\n\r\n"); 00258 break; 00259 00260 case 200: 00261 strcpy (reply, "HTTP/1.1 200 OK\r\n"); 00262 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00263 strcat (reply, "content-Type: text/json\r\n"); 00264 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4); 00265 strcat (reply, "{\"Result\":\"OK\"}"); 00266 strcat (reply, "\r\n\r\n"); 00267 break; 00268 } 00269 00270 #ifdef DEBUG_HTTPREQ 00271 printf("Before Delete\r\n"); 00272 debug_info(); 00273 #endif 00274 if(pDataJson){ 00275 #ifdef DEBUG_HTTPREQ 00276 printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token); 00277 #endif 00278 delete pDataJson; 00279 pDataJson = 0; 00280 } 00281 #ifdef DEBUG_HTTPREQ 00282 printf("After Delete\r\n"); 00283 debug_info(); 00284 #endif 00285 } 00286 #endif 00287 00288 void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00289 { 00290 MbedJSONValue* tmpJson; 00291 char* tok; 00292 char* last; 00293 int errnum = 200; 00294 00295 #ifdef DEBUG_HTTPREQ 00296 printf("PutRequestHandler():%s\r\n",request_data+4); 00297 #endif 00298 if(!pDataJson){ 00299 pDataJson = new MbedJSONValue(); 00300 } 00301 #ifdef DEBUG_HTTPREQ 00302 printf("Before Parse\r\n"); 00303 debug_info(); 00304 #endif 00305 parse(*pDataJson,(const char*)request_data); 00306 #ifdef DEBUG_HTTPREQ 00307 printf("After Parse\r\n"); 00308 debug_info(); 00309 printf("DataJson._type=%d\r\n",pDataJson->_type); 00310 printf("DataJson=%s\r\n",pDataJson->serialize().c_str()); 00311 printf("DataJson.size()=%d\r\n",pDataJson->size()); 00312 printf("DataJason.type=%d\r\n",pDataJson->_type); 00313 #endif 00314 00315 if(!strcmp(rest_uri, "/")){ 00316 tmpJson = &WIZwikiREST; 00317 } 00318 else{ 00319 tok = strtok_r(rest_uri+1, "/", &last); 00320 tmpJson = &WIZwikiREST; 00321 00322 char* name = 0; 00323 while(tok) 00324 { 00325 #ifdef DEBUG_HTTPREQ 00326 printf("tok = %s \r\n", tok); 00327 #endif 00328 if(tmpJson->hasMember(tok)){ 00329 tmpJson = &((*tmpJson)[tok]); 00330 name = tok; 00331 tok = strtok_r(0, "/", &last); 00332 } 00333 else{ 00334 #ifdef DEBUG_HTTPREQ 00335 printf("No Member\r\n"); 00336 #endif 00337 break; 00338 } 00339 } 00340 if(name){ 00341 #ifdef DEBUG_HTTPREQ 00342 printf("Token_name=%s\r\n",name); 00343 #endif 00344 if(tok){ 00345 #ifdef DEBUG_HTTPREQ 00346 printf("It should be no parameters : tok=%s\r\n",tok); 00347 #endif 00348 errnum = 403; 00349 } 00350 } 00351 else{ 00352 errnum = 404; 00353 } 00354 } 00355 if(errnum != 0){ 00356 switch(pDataJson->_type) 00357 { 00358 case MbedJSONValue::TypeInt: 00359 if(tmpJson->accessible){ 00360 *tmpJson = pDataJson->_value.asInt; 00361 tmpJson->cb_action(&tmpJson->_value); 00362 #ifdef DEBUG_HTTPREQ 00363 printf("set int:%d\r\n",atoi(tok)); 00364 #endif 00365 } 00366 else{ 00367 errnum = 403; 00368 } 00369 break; 00370 00371 case MbedJSONValue::TypeString: 00372 if(tmpJson->accessible){ 00373 *tmpJson = pDataJson->_value.asString; 00374 tmpJson->cb_action((void*)tmpJson->_value.asString->c_str()); 00375 } 00376 else{ 00377 errnum = 403; 00378 } 00379 break; 00380 00381 case MbedJSONValue::TypeObject: 00382 for(int i = 0; i < pDataJson->index_token; i++) 00383 { 00384 tok = (char*)pDataJson->token_name[i]->c_str(); 00385 #ifdef DEBUG_HTTPREQ 00386 printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok); 00387 #endif 00388 if(tmpJson->hasMember(tok)){ 00389 if((*tmpJson)[tok].accessible){ 00390 errnum = 200; 00391 if((*pDataJson)[i].size() > 0){ 00392 //(*tmpJson)[tok] = (*pDataJson)[i]._value.asString; 00393 (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str(); 00394 00395 //(*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 00396 (*tmpJson)[tok].cb_action((void*)((*pDataJson)[i]._value.asString->c_str())); 00397 #ifdef DEBUG_HTTPREQ 00398 printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str()); 00399 #endif 00400 } 00401 else{ 00402 (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt; 00403 (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 00404 #ifdef DEBUG_HTTPREQ 00405 printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>()); 00406 #endif 00407 } 00408 } 00409 else{ 00410 errnum = 403; break; 00411 } 00412 } 00413 else{ 00414 errnum = 404; break; 00415 } 00416 } 00417 break; 00418 00419 default: 00420 errnum = 403; break; 00421 } 00422 } 00423 00424 switch(errnum) 00425 { 00426 case 403: 00427 strcpy (reply, "HTTP/1.1 403 OK\r\n"); 00428 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00429 strcat (reply, "content-Type: text/json\r\n"); 00430 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); 00431 strcat(reply, "{\"Result : No Accessible\"}"); 00432 strcat (reply, "\r\n\r\n"); 00433 break; 00434 00435 case 404: 00436 strcpy (reply, "HTTP/1.1 404 OK\r\n"); 00437 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00438 strcat (reply, "content-Type: text/json\r\n"); 00439 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); 00440 strcat(reply, "{\"Result\" : \"No defined Resource\"}"); 00441 strcat (reply, "\r\n\r\n"); 00442 break; 00443 00444 case 200: 00445 strcpy (reply, "HTTP/1.1 200 OK\r\n"); 00446 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00447 strcat (reply, "content-Type: text/json\r\n"); 00448 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4); 00449 strcat (reply, "{\"Result\":\"OK\"}"); 00450 strcat (reply, "\r\n\r\n"); 00451 break; 00452 } 00453 00454 #ifdef DEBUG_HTTPREQ 00455 printf("Before Delete\r\n"); 00456 debug_info(); 00457 #endif 00458 if(pDataJson){ 00459 #ifdef DEBUG_HTTPREQ 00460 printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token); 00461 #endif 00462 delete pDataJson; 00463 pDataJson = 0; 00464 } 00465 #ifdef DEBUG_HTTPREQ 00466 printf("After Delete\r\n"); 00467 debug_info(); 00468 #endif 00469 } 00470 00471 /* 00472 void DeleteRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00473 { 00474 } 00475 */
Generated on Sat Jul 16 2022 05:32:29 by 1.7.2