WIZwikiREST-io Reference code for WIZnet Academy
Dependencies: DHT MbedJSONValue_v102 WIZnetInterface mbed-src
Fork of WIZwiki-REST-io_v103 by
Diff: RequestHandler.cpp
- Revision:
- 2:ca36ecca24c2
- Parent:
- 1:728e5b5c8dae
- Child:
- 4:0b5a199e91f3
diff -r 728e5b5c8dae -r ca36ecca24c2 RequestHandler.cpp --- a/RequestHandler.cpp Tue Apr 05 11:40:33 2016 +0000 +++ b/RequestHandler.cpp Tue May 03 02:15:59 2016 +0000 @@ -12,85 +12,97 @@ char* tok; char* last; - + const char * pchar = 0; +#ifdef DEBUG_HTTPREQ printf("GetRequestHandler():%s\r\n",rest_uri); - +#endif if(!strcmp(rest_uri, "/")) { - strcpy(reply, WIZwikiREST.serialize().c_str()); - return; + tmpJson = &WIZwikiREST; } + else + { tok = strtok_r(rest_uri+1, "/", &last); // 20160226 tmpJson = &WIZwikiREST; - - char depth = 0; - char* name = 0; - while(tok) - { - printf("tok = %s \r\n", tok); // Name - if(tmpJson->hasMember(tok)) + + char depth = 0; + char* name = 0; + while(tok) + { +#ifdef DEBUG_HTTPREQ + printf("tok = %s \r\n", tok); // Name +#endif + if(tmpJson->hasMember(tok)) + { + tmpJson = &((*tmpJson)[tok]); + name = tok; + tok = strtok_r(0, "/", &last); + depth++; + } + else + { +#ifdef DEBUG_HTTPREQ + printf("No Member\r\n"); +#endif + break; + } + } + if(name) { - tmpJson = &((*tmpJson)[tok]); - name = tok; - tok = strtok_r(0, "/", &last); - depth++; + if(tok) + { + if(tmpJson->accessible) + { +#ifdef DEBUG_HTTPREQ + printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); +#endif + + 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 { + *tmpJson = atoi(tok); + tmpJson->cb_action(&tmpJson->_value); +#ifdef DEBUG_HTTPREQ + printf("set int:%d\r\n",atoi(tok)); +#endif + } + } + 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"); + sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); + strcat(reply, "{\"Result : No Accessible\"}"); + strcat (reply, "\r\n\r\n"); + return; + } + } } else { - printf("No Member\r\n"); - break; + 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"); + return; } } - /* - if(tmpJson && tmpJson->size() > 0) - { - strcpy(reply, (*tmpJson).serialize().c_str()); - } - else - { - strcpy(reply, "{\"Result\" : \"No defined Resource\"}"); - } - */ - /* - if(name) - { - printf("name=%s, tok=%s\r\n",name,tok); - if(tok) - { - if(depth == 2) - { - if(!strcmp(name, "MAC")) - strcpy(reply, "{\"Result\" : \"No Accessible\"}"); - else - { - if(tmpJson->size() > 0) *tmpJson = std::string(tok); - else *tmpJson = atoi(tok); - strcpy(reply, (*tmpJson).serialize().c_str()); - } - } - else if(depth == 1) strcpy(reply, "{\"Result : No Accessible\"}"); - } - else strcpy(reply, (*tmpJson).serialize().c_str()); - return; - } - strcpy(reply, "{\"Result\" : \"No defined Resource\"}"); - */ - if(name) - { - if(tok) - { - if(tmpJson->accessible) - { - printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); - if(tmpJson->size() > 0) {*tmpJson = std::string(tok); tmpJson->cb_action((void*)tok); printf("set string:%s\r\n",tok);} - else {*tmpJson = atoi(tok); tmpJson->cb_action(&tmpJson->_value); printf("set int:%d\r\n",atoi(tok));} - strcpy(reply, (*tmpJson).serialize().c_str()); - } - else strcpy(reply, "{\"Result : No Accessible\"}"); - } - else strcpy(reply, (*tmpJson).serialize().c_str()); - } - else strcpy(reply, "{\"Result\" : \"No defined Resource\"}"); + pchar = (*tmpJson).serialize().c_str(); + 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", strlen(pchar)+4); + strcat (reply, pchar); + strcat (reply, "\r\n\r\n"); + return; } @@ -101,5 +113,3 @@ void DeleteRequestHandler::handle(char* rest_uri, char *reply) { } - -