![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Upper Version Add PUT method Delete POST method
Dependencies: Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed
Fork of WIZwiki-REST-io_v102 by
Diff: RequestHandler.cpp
- Revision:
- 4:0b5a199e91f3
- Parent:
- 2:ca36ecca24c2
- Child:
- 5:473a66ae9596
--- a/RequestHandler.cpp Tue May 03 02:21:56 2016 +0000 +++ b/RequestHandler.cpp Tue May 31 10:31:30 2016 +0000 @@ -6,7 +6,9 @@ extern MbedJSONValue WIZwikiREST; -void GetRequestHandler::handle(char* rest_uri, char *reply) +MbedJSONValue* pDataJson = 0; + +void GetRequestHandler::handle(char* rest_uri, char* request_data, char *reply) { MbedJSONValue* tmpJson; @@ -16,55 +18,46 @@ #ifdef DEBUG_HTTPREQ printf("GetRequestHandler():%s\r\n",rest_uri); #endif - if(!strcmp(rest_uri, "/")) - { + if(!strcmp(rest_uri, "/")){ tmpJson = &WIZwikiREST; } - else - { - tok = strtok_r(rest_uri+1, "/", &last); // 20160226 + else{ + tok = strtok_r(rest_uri+1, "/", &last); tmpJson = &WIZwikiREST; - char depth = 0; char* name = 0; while(tok) { #ifdef DEBUG_HTTPREQ - printf("tok = %s \r\n", tok); // Name + printf("tok = %s \r\n", tok); #endif - if(tmpJson->hasMember(tok)) - { + if(tmpJson->hasMember(tok)){ tmpJson = &((*tmpJson)[tok]); name = tok; tok = strtok_r(0, "/", &last); - depth++; } - else - { + else{ #ifdef DEBUG_HTTPREQ printf("No Member\r\n"); #endif break; } } - if(name) - { - if(tok) - { - if(tmpJson->accessible) - { + if(name){ + if(tok){ + if(tmpJson->accessible){ #ifdef DEBUG_HTTPREQ printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); #endif - if(tmpJson->size() > 0) { + if(tmpJson->size() > 0){ *tmpJson = std::string(tok); tmpJson->cb_action((void*)tok); #ifdef DEBUG_HTTPREQ printf("set string:%s\r\n",tok); #endif } - else { + else{ *tmpJson = atoi(tok); tmpJson->cb_action(&tmpJson->_value); #ifdef DEBUG_HTTPREQ @@ -72,8 +65,7 @@ #endif } } - else - { + else{ strcpy (reply, "HTTP/1.1 403 OK\r\n"); strcat (reply, "Sever: WIZwiki-REST\r\n"); strcat (reply, "content-Type: text/json\r\n"); @@ -84,8 +76,7 @@ } } } - else - { + else{ strcpy (reply, "HTTP/1.1 404 OK\r\n"); strcat (reply, "Sever: WIZwiki-REST\r\n"); strcat (reply, "content-Type: text/json\r\n"); @@ -106,10 +97,190 @@ return; } -void PutRequestHandler::handle(char* rest_uri, char *reply) +void PostRequestHandler::handle(char* rest_uri, char* request_data, char *reply) +{ + MbedJSONValue* tmpJson; + char* tok; + char* last; + int errnum = 200; + +#ifdef DEBUG_HTTPREQ + printf("PostRequestHandler():%s\r\n",request_data+4); +#endif + if(!pDataJson){ + pDataJson = new MbedJSONValue(); + } +#ifdef DEBUG_HTTPREQ + printf("Before Parse\r\n"); + debug_info(); +#endif + parse(*pDataJson,(const char*)request_data); +#ifdef DEBUG_HTTPREQ + printf("After Parse\r\n"); + debug_info(); + printf("DataJson._type=%d\r\n",pDataJson->_type); + printf("DataJson=%s\r\n",pDataJson->serialize().c_str()); + printf("DataJson.size()=%d\r\n",pDataJson->size()); + printf("DataJason.type=%d\r\n",pDataJson->_type); +#endif + + if(!strcmp(rest_uri, "/")){ + tmpJson = &WIZwikiREST; + } + else{ + tok = strtok_r(rest_uri+1, "/", &last); + tmpJson = &WIZwikiREST; + + char* name = 0; + while(tok) + { +#ifdef DEBUG_HTTPREQ + printf("tok = %s \r\n", tok); +#endif + if(tmpJson->hasMember(tok)){ + tmpJson = &((*tmpJson)[tok]); + name = tok; + tok = strtok_r(0, "/", &last); + } + else{ +#ifdef DEBUG_HTTPREQ + printf("No Member\r\n"); +#endif + break; + } + } + if(name){ +#ifdef DEBUG_HTTPREQ + printf("Token_name=%s\r\n",name); +#endif + if(tok){ +#ifdef DEBUG_HTTPREQ + printf("It should be no parameters : tok=%s\r\n",tok); +#endif + errnum = 403; + } + } + else{ + errnum = 404; + } + } + if(errnum != 0){ + switch(pDataJson->_type) + { + case MbedJSONValue::TypeInt: + if(tmpJson->accessible){ + *tmpJson = pDataJson->_value.asInt; + tmpJson->cb_action(&tmpJson->_value); +#ifdef DEBUG_HTTPREQ + printf("set int:%d\r\n",atoi(tok)); +#endif + } + else{ + errnum = 403; + } + break; + + case MbedJSONValue::TypeString: + if(tmpJson->accessible){ + *tmpJson = pDataJson->_value.asString; + } + else{ + errnum = 403; + } + break; + + case MbedJSONValue::TypeObject: + for(int i = 0; i < pDataJson->index_token; i++) + { + tok = (char*)pDataJson->token_name[i]->c_str(); +#ifdef DEBUG_HTTPREQ + printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok); +#endif + if(tmpJson->hasMember(tok)){ + if((*tmpJson)[tok].accessible){ + errnum = 200; + if((*pDataJson)[i].size() > 0){ + (*tmpJson)[tok] = (*pDataJson)[i]._value.asString; +#ifdef DEBUG_HTTPREQ + printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str()); +#endif + } + else{ + (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt; + (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); +#ifdef DEBUG_HTTPREQ + printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>()); +#endif + } + } + else{ + errnum = 403; break; + } + } + else{ + errnum = 404; break; + } + } + break; + + default: + errnum = 403; break; + } + } + + switch(errnum) + { + case 403: + strcpy (reply, "HTTP/1.1 403 OK\r\n"); + strcat (reply, "Sever: WIZwiki-REST\r\n"); + strcat (reply, "content-Type: text/json\r\n"); + sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); + strcat(reply, "{\"Result : No Accessible\"}"); + strcat (reply, "\r\n\r\n"); + break; + + case 404: + strcpy (reply, "HTTP/1.1 404 OK\r\n"); + strcat (reply, "Sever: WIZwiki-REST\r\n"); + strcat (reply, "content-Type: text/json\r\n"); + sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); + strcat(reply, "{\"Result\" : \"No defined Resource\"}"); + strcat (reply, "\r\n\r\n"); + break; + + case 200: + strcpy (reply, "HTTP/1.1 200 OK\r\n"); + strcat (reply, "Sever: WIZwiki-REST\r\n"); + strcat (reply, "content-Type: text/json\r\n"); + sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4); + strcat (reply, "{\"Result\":\"OK\"}"); + strcat (reply, "\r\n\r\n"); + break; + } + +#ifdef DEBUG_HTTPREQ + printf("Before Delete\r\n"); + debug_info(); +#endif + if(pDataJson){ +#ifdef DEBUG_HTTPREQ + printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token); +#endif + delete pDataJson; + pDataJson = 0; + } +#ifdef DEBUG_HTTPREQ + printf("After Delete\r\n"); + debug_info(); +#endif +} + +/* +void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply) { } -void DeleteRequestHandler::handle(char* rest_uri, char *reply) +void DeleteRequestHandler::handle(char* rest_uri, char* request_data, char *reply) { } +*/ \ No newline at end of file