Upper Version Add PUT method Delete POST method

Dependencies:   Adafruit_GFX MbedJSONValue_v102 WIZnetInterface mbed

Fork of WIZwiki-REST-io_v102 by Lawrence Lee

RequestHandler.cpp

Committer:
joon874
Date:
2016-05-03
Revision:
2:ca36ecca24c2
Parent:
1:728e5b5c8dae
Child:
4:0b5a199e91f3

File content as of revision 2:ca36ecca24c2:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "RequestHandler.h"
#include "MbedJSONValue.h"

extern MbedJSONValue WIZwikiREST;

void GetRequestHandler::handle(char* rest_uri, char *reply)
{
    MbedJSONValue* tmpJson;

      char* tok;
      char* last;
      const char * pchar = 0;
#ifdef DEBUG_HTTPREQ
    printf("GetRequestHandler():%s\r\n",rest_uri);
#endif    
    if(!strcmp(rest_uri, "/"))
    {
        tmpJson = &WIZwikiREST;     
    }
    else
    {
        tok = strtok_r(rest_uri+1, "/", &last);             // 20160226
        tmpJson = &WIZwikiREST;
        
        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)
        {
            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
        {
            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;
        }
    }
    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;
}

void PutRequestHandler::handle(char* rest_uri, char *reply)
{
}

void DeleteRequestHandler::handle(char* rest_uri, char *reply)
{
}