Lawrence Lee / Mbed 2 deprecated WIZwiki-REST_mc

Dependencies:   MbedJSONValue1 WIZnetInterface mbed

Fork of WIZwiki-REST_mc by Midnight Cow

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 void GetRequestHandler::handle(char* rest_uri, char *reply)
00010 {
00011     MbedJSONValue* tmpJson;
00012 
00013       char* tok;
00014       char* last;
00015       const char * pchar = 0;
00016 #ifdef DEBUG_HTTPREQ
00017     printf("GetRequestHandler():%s\r\n",rest_uri);
00018 #endif    
00019     if(!strcmp(rest_uri, "/"))
00020     {
00021         tmpJson = &WIZwikiREST;     
00022     }
00023     else
00024     {
00025         tok = strtok_r(rest_uri+1, "/", &last);             // 20160226
00026         tmpJson = &WIZwikiREST;
00027         
00028         char depth = 0;
00029         char* name = 0;
00030         while(tok)
00031         {
00032 #ifdef DEBUG_HTTPREQ            
00033             printf("tok = %s \r\n", tok);               // Name
00034 #endif            
00035             if(tmpJson->hasMember(tok))
00036             {
00037                 tmpJson = &((*tmpJson)[tok]);
00038                 name = tok;
00039                 tok = strtok_r(0, "/", &last);
00040                 depth++;
00041             }
00042             else
00043             {
00044 #ifdef DEBUG_HTTPREQ                
00045                 printf("No Member\r\n");
00046 #endif                
00047                 break;
00048             }
00049         }
00050         if(name)
00051         {
00052             if(tok)
00053             {
00054                 if(tmpJson->accessible)
00055                 {
00056 #ifdef DEBUG_HTTPREQ                    
00057                     printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size());
00058 #endif                    
00059                     
00060                     if(tmpJson->size() > 0) {
00061                         *tmpJson = std::string(tok); 
00062                         tmpJson->cb_action((void*)tok); 
00063 #ifdef DEBUG_HTTPREQ                        
00064                         printf("set string:%s\r\n",tok);
00065 #endif
00066                     }
00067                     else                    {
00068                         *tmpJson = atoi(tok); 
00069                         tmpJson->cb_action(&tmpJson->_value); 
00070 #ifdef DEBUG_HTTPREQ                        
00071                         printf("set int:%d\r\n",atoi(tok));
00072 #endif                        
00073                     }
00074                 }
00075                 else
00076                 {
00077                     strcpy (reply, "HTTP/1.1 403 OK\r\n");
00078                     strcat (reply, "Sever: WIZwiki-REST\r\n");
00079                     strcat (reply, "content-Type: text/json\r\n");
00080                     sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4);
00081                     strcat(reply, "{\"Result : No Accessible\"}");
00082                     strcat (reply, "\r\n\r\n");
00083                     return;
00084                 }
00085             }
00086         }
00087         else
00088         {
00089             strcpy (reply, "HTTP/1.1 404 OK\r\n");
00090             strcat (reply, "Sever: WIZwiki-REST\r\n");
00091             strcat (reply, "content-Type: text/json\r\n");
00092             sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4);
00093             strcat(reply, "{\"Result\" : \"No defined Resource\"}");
00094             strcat (reply, "\r\n\r\n");
00095             return;
00096         }
00097     }
00098     pchar = (*tmpJson).serialize().c_str();
00099     strcpy (reply, "HTTP/1.1 200 OK\r\n");
00100     strcat (reply, "Sever: WIZwiki-REST\r\n");
00101     strcat (reply, "content-Type: text/json\r\n");
00102     sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", strlen(pchar)+4);
00103     strcat (reply, pchar);
00104     strcat (reply, "\r\n\r\n");
00105 
00106     return;
00107 }
00108 
00109 void PutRequestHandler::handle(char* rest_uri, char *reply)
00110 {
00111 }
00112 
00113 void DeleteRequestHandler::handle(char* rest_uri, char *reply)
00114 {
00115 }