Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MbedJSONValue_copy WIZnetInterface mbed
Fork of WIZwiki-REST_mc 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 name = tok; 00037 tok = strtok_r(0, "/", &last); 00038 } 00039 else{ 00040 #ifdef DEBUG_HTTPREQ 00041 printf("No Member\r\n"); 00042 #endif 00043 break; 00044 } 00045 } 00046 if(name){ 00047 if(tok){ 00048 if(tmpJson->accessible){ 00049 #ifdef DEBUG_HTTPREQ 00050 printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); 00051 #endif 00052 00053 if(tmpJson->size() > 0){ 00054 *tmpJson = std::string(tok); 00055 tmpJson->cb_action((void*)tok); 00056 #ifdef DEBUG_HTTPREQ 00057 printf("set string:%s\r\n",tok); 00058 #endif 00059 } 00060 else{ 00061 *tmpJson = atoi(tok); 00062 tmpJson->cb_action(&tmpJson->_value); 00063 #ifdef DEBUG_HTTPREQ 00064 printf("set int:%d\r\n",atoi(tok)); 00065 #endif 00066 } 00067 } 00068 else{ 00069 strcpy (reply, "HTTP/1.1 403 OK\r\n"); 00070 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00071 strcat (reply, "content-Type: text/json\r\n"); 00072 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); 00073 strcat(reply, "{\"Result : No Accessible\"}"); 00074 strcat (reply, "\r\n\r\n"); 00075 return; 00076 } 00077 } 00078 } 00079 else{ 00080 strcpy (reply, "HTTP/1.1 404 OK\r\n"); 00081 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00082 strcat (reply, "content-Type: text/json\r\n"); 00083 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); 00084 strcat(reply, "{\"Result\" : \"No defined Resource\"}"); 00085 strcat (reply, "\r\n\r\n"); 00086 return; 00087 } 00088 } 00089 pchar = (*tmpJson).serialize().c_str(); 00090 strcpy (reply, "HTTP/1.1 200 OK\r\n"); 00091 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00092 strcat (reply, "content-Type: text/json\r\n"); 00093 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", strlen(pchar)+4); 00094 strcat (reply, pchar); 00095 strcat (reply, "\r\n\r\n"); 00096 00097 return; 00098 } 00099 00100 void PostRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00101 { 00102 MbedJSONValue* tmpJson; 00103 char* tok; 00104 char* last; 00105 int errnum = 200; 00106 00107 #ifdef DEBUG_HTTPREQ 00108 printf("PostRequestHandler():%s\r\n",request_data+4); 00109 #endif 00110 if(!pDataJson){ 00111 pDataJson = new MbedJSONValue(); 00112 } 00113 #ifdef DEBUG_HTTPREQ 00114 printf("Before Parse\r\n"); 00115 debug_info(); 00116 #endif 00117 parse(*pDataJson,(const char*)request_data); 00118 #ifdef DEBUG_HTTPREQ 00119 printf("After Parse\r\n"); 00120 debug_info(); 00121 printf("DataJson._type=%d\r\n",pDataJson->_type); 00122 printf("DataJson=%s\r\n",pDataJson->serialize().c_str()); 00123 printf("DataJson.size()=%d\r\n",pDataJson->size()); 00124 printf("DataJason.type=%d\r\n",pDataJson->_type); 00125 #endif 00126 00127 if(!strcmp(rest_uri, "/")){ 00128 tmpJson = &WIZwikiREST; 00129 } 00130 else{ 00131 tok = strtok_r(rest_uri+1, "/", &last); 00132 tmpJson = &WIZwikiREST; 00133 00134 char* name = 0; 00135 while(tok) 00136 { 00137 #ifdef DEBUG_HTTPREQ 00138 printf("tok = %s \r\n", tok); 00139 #endif 00140 if(tmpJson->hasMember(tok)){ 00141 tmpJson = &((*tmpJson)[tok]); 00142 name = tok; 00143 tok = strtok_r(0, "/", &last); 00144 } 00145 else{ 00146 #ifdef DEBUG_HTTPREQ 00147 printf("No Member\r\n"); 00148 #endif 00149 break; 00150 } 00151 } 00152 if(name){ 00153 #ifdef DEBUG_HTTPREQ 00154 printf("Token_name=%s\r\n",name); 00155 #endif 00156 if(tok){ 00157 #ifdef DEBUG_HTTPREQ 00158 printf("It should be no parameters : tok=%s\r\n",tok); 00159 #endif 00160 errnum = 403; 00161 } 00162 } 00163 else{ 00164 errnum = 404; 00165 } 00166 } 00167 if(errnum != 0){ 00168 switch(pDataJson->_type) 00169 { 00170 case MbedJSONValue::TypeInt: 00171 if(tmpJson->accessible){ 00172 *tmpJson = pDataJson->_value.asInt; 00173 tmpJson->cb_action(&tmpJson->_value); 00174 #ifdef DEBUG_HTTPREQ 00175 printf("set int:%d\r\n",atoi(tok)); 00176 #endif 00177 } 00178 else{ 00179 errnum = 403; 00180 } 00181 break; 00182 00183 case MbedJSONValue::TypeString: 00184 if(tmpJson->accessible){ 00185 *tmpJson = pDataJson->_value.asString; 00186 } 00187 else{ 00188 errnum = 403; 00189 } 00190 break; 00191 00192 case MbedJSONValue::TypeObject: 00193 for(int i = 0; i < pDataJson->index_token; i++) 00194 { 00195 tok = (char*)pDataJson->token_name[i]->c_str(); 00196 #ifdef DEBUG_HTTPREQ 00197 printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok); 00198 #endif 00199 if(tmpJson->hasMember(tok)){ 00200 if((*tmpJson)[tok].accessible){ 00201 errnum = 200; 00202 if((*pDataJson)[i].size() > 0){ 00203 (*tmpJson)[tok] = (*pDataJson)[i]._value.asString; 00204 #ifdef DEBUG_HTTPREQ 00205 printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str()); 00206 #endif 00207 } 00208 else{ 00209 (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt; 00210 (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); 00211 #ifdef DEBUG_HTTPREQ 00212 printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>()); 00213 #endif 00214 } 00215 } 00216 else{ 00217 errnum = 403; break; 00218 } 00219 } 00220 else{ 00221 errnum = 404; break; 00222 } 00223 } 00224 break; 00225 00226 default: 00227 errnum = 403; break; 00228 } 00229 } 00230 00231 switch(errnum) 00232 { 00233 case 403: 00234 strcpy (reply, "HTTP/1.1 403 OK\r\n"); 00235 strcat (reply, "Sever: WIZwiki-REST\r\n"); 00236 strcat (reply, "content-Type: text/json\r\n"); 00237 sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); 00238 strcat(reply, "{\"Result : No Accessible\"}"); 00239 strcat (reply, "\r\n\r\n"); 00240 break; 00241 00242 case 404: 00243 strcpy (reply, "HTTP/1.1 404 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", 33+4); 00247 strcat(reply, "{\"Result\" : \"No defined Resource\"}"); 00248 strcat (reply, "\r\n\r\n"); 00249 break; 00250 00251 case 200: 00252 strcpy (reply, "HTTP/1.1 200 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", 15+4); 00256 strcat (reply, "{\"Result\":\"OK\"}"); 00257 strcat (reply, "\r\n\r\n"); 00258 break; 00259 } 00260 00261 #ifdef DEBUG_HTTPREQ 00262 printf("Before Delete\r\n"); 00263 debug_info(); 00264 #endif 00265 if(pDataJson){ 00266 #ifdef DEBUG_HTTPREQ 00267 printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token); 00268 #endif 00269 delete pDataJson; 00270 pDataJson = 0; 00271 } 00272 #ifdef DEBUG_HTTPREQ 00273 printf("After Delete\r\n"); 00274 debug_info(); 00275 #endif 00276 } 00277 00278 /* 00279 void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00280 { 00281 } 00282 00283 void DeleteRequestHandler::handle(char* rest_uri, char* request_data, char *reply) 00284 { 00285 } 00286 */
Generated on Thu Jul 14 2022 06:34:35 by
1.7.2
