![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
WIZwikiREST-io exercise code for WIZnet Academy
Dependencies: MbedJSONValue_v102 WIZnetInterface mbed
Fork of WIZwiki-REST-io_v103 by
RequestHandler.cpp@10:fe96beb315e3, 2016-10-06 (annotated)
- Committer:
- joon874
- Date:
- Thu Oct 06 11:19:32 2016 +0000
- Revision:
- 10:fe96beb315e3
- Parent:
- 9:f7d5a168a693
code edited
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joon874 | 0:5886f525a4ad | 1 | #include <stdio.h> |
joon874 | 0:5886f525a4ad | 2 | #include <stdlib.h> |
joon874 | 0:5886f525a4ad | 3 | #include <string.h> |
joon874 | 0:5886f525a4ad | 4 | #include "RequestHandler.h" |
joon874 | 0:5886f525a4ad | 5 | #include "MbedJSONValue.h" |
joon874 | 0:5886f525a4ad | 6 | |
joon874 | 0:5886f525a4ad | 7 | extern MbedJSONValue WIZwikiREST; |
joon874 | 0:5886f525a4ad | 8 | |
joon874 | 4:0b5a199e91f3 | 9 | MbedJSONValue* pDataJson = 0; |
joon874 | 4:0b5a199e91f3 | 10 | |
joon874 | 4:0b5a199e91f3 | 11 | void GetRequestHandler::handle(char* rest_uri, char* request_data, char *reply) |
joon874 | 0:5886f525a4ad | 12 | { |
joon874 | 0:5886f525a4ad | 13 | MbedJSONValue* tmpJson; |
joon874 | 0:5886f525a4ad | 14 | |
joon874 | 0:5886f525a4ad | 15 | char* tok; |
joon874 | 0:5886f525a4ad | 16 | char* last; |
joon874 | 2:ca36ecca24c2 | 17 | const char * pchar = 0; |
joon874 | 2:ca36ecca24c2 | 18 | #ifdef DEBUG_HTTPREQ |
joon874 | 0:5886f525a4ad | 19 | printf("GetRequestHandler():%s\r\n",rest_uri); |
joon874 | 2:ca36ecca24c2 | 20 | #endif |
joon874 | 4:0b5a199e91f3 | 21 | if(!strcmp(rest_uri, "/")){ |
joon874 | 2:ca36ecca24c2 | 22 | tmpJson = &WIZwikiREST; |
joon874 | 0:5886f525a4ad | 23 | } |
joon874 | 4:0b5a199e91f3 | 24 | else{ |
joon874 | 4:0b5a199e91f3 | 25 | tok = strtok_r(rest_uri+1, "/", &last); |
joon874 | 0:5886f525a4ad | 26 | tmpJson = &WIZwikiREST; |
joon874 | 2:ca36ecca24c2 | 27 | |
joon874 | 2:ca36ecca24c2 | 28 | char* name = 0; |
joon874 | 2:ca36ecca24c2 | 29 | while(tok) |
joon874 | 2:ca36ecca24c2 | 30 | { |
joon874 | 2:ca36ecca24c2 | 31 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 32 | printf("tok = %s \r\n", tok); |
joon874 | 2:ca36ecca24c2 | 33 | #endif |
joon874 | 4:0b5a199e91f3 | 34 | if(tmpJson->hasMember(tok)){ |
joon874 | 2:ca36ecca24c2 | 35 | tmpJson = &((*tmpJson)[tok]); |
joon874 | 6:2974bbc94e7a | 36 | //if(!tmpJson->accessible) tmpJson->cb_action(tmpJson); |
joon874 | 2:ca36ecca24c2 | 37 | name = tok; |
joon874 | 2:ca36ecca24c2 | 38 | tok = strtok_r(0, "/", &last); |
joon874 | 2:ca36ecca24c2 | 39 | } |
joon874 | 4:0b5a199e91f3 | 40 | else{ |
joon874 | 6:2974bbc94e7a | 41 | #ifndef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 42 | printf("No Member\r\n"); |
joon874 | 2:ca36ecca24c2 | 43 | #endif |
joon874 | 2:ca36ecca24c2 | 44 | break; |
joon874 | 2:ca36ecca24c2 | 45 | } |
joon874 | 2:ca36ecca24c2 | 46 | } |
joon874 | 4:0b5a199e91f3 | 47 | if(name){ |
joon874 | 6:2974bbc94e7a | 48 | if(tok) |
joon874 | 6:2974bbc94e7a | 49 | { |
joon874 | 4:0b5a199e91f3 | 50 | if(tmpJson->accessible){ |
joon874 | 2:ca36ecca24c2 | 51 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 52 | printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); |
joon874 | 2:ca36ecca24c2 | 53 | #endif |
joon874 | 2:ca36ecca24c2 | 54 | |
joon874 | 4:0b5a199e91f3 | 55 | if(tmpJson->size() > 0){ |
joon874 | 2:ca36ecca24c2 | 56 | *tmpJson = std::string(tok); |
joon874 | 2:ca36ecca24c2 | 57 | tmpJson->cb_action((void*)tok); |
joon874 | 2:ca36ecca24c2 | 58 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 59 | printf("set string:%s\r\n",tok); |
joon874 | 2:ca36ecca24c2 | 60 | #endif |
joon874 | 2:ca36ecca24c2 | 61 | } |
joon874 | 4:0b5a199e91f3 | 62 | else{ |
joon874 | 2:ca36ecca24c2 | 63 | *tmpJson = atoi(tok); |
joon874 | 2:ca36ecca24c2 | 64 | tmpJson->cb_action(&tmpJson->_value); |
joon874 | 2:ca36ecca24c2 | 65 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 66 | printf("set int:%d\r\n",atoi(tok)); |
joon874 | 2:ca36ecca24c2 | 67 | #endif |
joon874 | 2:ca36ecca24c2 | 68 | } |
joon874 | 2:ca36ecca24c2 | 69 | } |
joon874 | 4:0b5a199e91f3 | 70 | else{ |
joon874 | 2:ca36ecca24c2 | 71 | strcpy (reply, "HTTP/1.1 403 OK\r\n"); |
joon874 | 2:ca36ecca24c2 | 72 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 2:ca36ecca24c2 | 73 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 2:ca36ecca24c2 | 74 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); |
joon874 | 2:ca36ecca24c2 | 75 | strcat(reply, "{\"Result : No Accessible\"}"); |
joon874 | 2:ca36ecca24c2 | 76 | strcat (reply, "\r\n\r\n"); |
joon874 | 2:ca36ecca24c2 | 77 | return; |
joon874 | 2:ca36ecca24c2 | 78 | } |
joon874 | 2:ca36ecca24c2 | 79 | } |
MidnightCow | 1:728e5b5c8dae | 80 | } |
joon874 | 4:0b5a199e91f3 | 81 | else{ |
joon874 | 2:ca36ecca24c2 | 82 | strcpy (reply, "HTTP/1.1 404 OK\r\n"); |
joon874 | 2:ca36ecca24c2 | 83 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 2:ca36ecca24c2 | 84 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 2:ca36ecca24c2 | 85 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); |
joon874 | 2:ca36ecca24c2 | 86 | strcat(reply, "{\"Result\" : \"No defined Resource\"}"); |
joon874 | 2:ca36ecca24c2 | 87 | strcat (reply, "\r\n\r\n"); |
joon874 | 2:ca36ecca24c2 | 88 | return; |
joon874 | 0:5886f525a4ad | 89 | } |
MidnightCow | 1:728e5b5c8dae | 90 | } |
joon874 | 2:ca36ecca24c2 | 91 | pchar = (*tmpJson).serialize().c_str(); |
joon874 | 2:ca36ecca24c2 | 92 | strcpy (reply, "HTTP/1.1 200 OK\r\n"); |
joon874 | 2:ca36ecca24c2 | 93 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 2:ca36ecca24c2 | 94 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 2:ca36ecca24c2 | 95 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", strlen(pchar)+4); |
joon874 | 2:ca36ecca24c2 | 96 | strcat (reply, pchar); |
joon874 | 2:ca36ecca24c2 | 97 | strcat (reply, "\r\n\r\n"); |
joon874 | 2:ca36ecca24c2 | 98 | |
MidnightCow | 1:728e5b5c8dae | 99 | return; |
joon874 | 0:5886f525a4ad | 100 | } |
joon874 | 0:5886f525a4ad | 101 | |
joon874 | 8:60d99da6eeb2 | 102 | #ifdef HTTP_POST |
joon874 | 4:0b5a199e91f3 | 103 | void PostRequestHandler::handle(char* rest_uri, char* request_data, char *reply) |
joon874 | 4:0b5a199e91f3 | 104 | { |
joon874 | 4:0b5a199e91f3 | 105 | MbedJSONValue* tmpJson; |
joon874 | 4:0b5a199e91f3 | 106 | char* tok; |
joon874 | 4:0b5a199e91f3 | 107 | char* last; |
joon874 | 4:0b5a199e91f3 | 108 | int errnum = 200; |
joon874 | 4:0b5a199e91f3 | 109 | |
joon874 | 4:0b5a199e91f3 | 110 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 111 | printf("PostRequestHandler():%s\r\n",request_data+4); |
joon874 | 4:0b5a199e91f3 | 112 | #endif |
joon874 | 4:0b5a199e91f3 | 113 | if(!pDataJson){ |
joon874 | 4:0b5a199e91f3 | 114 | pDataJson = new MbedJSONValue(); |
joon874 | 4:0b5a199e91f3 | 115 | } |
joon874 | 4:0b5a199e91f3 | 116 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 117 | printf("Before Parse\r\n"); |
joon874 | 4:0b5a199e91f3 | 118 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 119 | #endif |
joon874 | 4:0b5a199e91f3 | 120 | parse(*pDataJson,(const char*)request_data); |
joon874 | 4:0b5a199e91f3 | 121 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 122 | printf("After Parse\r\n"); |
joon874 | 4:0b5a199e91f3 | 123 | debug_info(); |
joon874 | 4:0b5a199e91f3 | 124 | printf("DataJson._type=%d\r\n",pDataJson->_type); |
joon874 | 4:0b5a199e91f3 | 125 | printf("DataJson=%s\r\n",pDataJson->serialize().c_str()); |
joon874 | 4:0b5a199e91f3 | 126 | printf("DataJson.size()=%d\r\n",pDataJson->size()); |
joon874 | 4:0b5a199e91f3 | 127 | printf("DataJason.type=%d\r\n",pDataJson->_type); |
joon874 | 4:0b5a199e91f3 | 128 | #endif |
joon874 | 4:0b5a199e91f3 | 129 | |
joon874 | 4:0b5a199e91f3 | 130 | if(!strcmp(rest_uri, "/")){ |
joon874 | 4:0b5a199e91f3 | 131 | tmpJson = &WIZwikiREST; |
joon874 | 4:0b5a199e91f3 | 132 | } |
joon874 | 4:0b5a199e91f3 | 133 | else{ |
joon874 | 4:0b5a199e91f3 | 134 | tok = strtok_r(rest_uri+1, "/", &last); |
joon874 | 4:0b5a199e91f3 | 135 | tmpJson = &WIZwikiREST; |
joon874 | 4:0b5a199e91f3 | 136 | |
joon874 | 4:0b5a199e91f3 | 137 | char* name = 0; |
joon874 | 4:0b5a199e91f3 | 138 | while(tok) |
joon874 | 4:0b5a199e91f3 | 139 | { |
joon874 | 4:0b5a199e91f3 | 140 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 141 | printf("tok = %s \r\n", tok); |
joon874 | 4:0b5a199e91f3 | 142 | #endif |
joon874 | 4:0b5a199e91f3 | 143 | if(tmpJson->hasMember(tok)){ |
joon874 | 4:0b5a199e91f3 | 144 | tmpJson = &((*tmpJson)[tok]); |
joon874 | 4:0b5a199e91f3 | 145 | name = tok; |
joon874 | 4:0b5a199e91f3 | 146 | tok = strtok_r(0, "/", &last); |
joon874 | 4:0b5a199e91f3 | 147 | } |
joon874 | 4:0b5a199e91f3 | 148 | else{ |
joon874 | 4:0b5a199e91f3 | 149 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 150 | printf("No Member\r\n"); |
joon874 | 4:0b5a199e91f3 | 151 | #endif |
joon874 | 4:0b5a199e91f3 | 152 | break; |
joon874 | 4:0b5a199e91f3 | 153 | } |
joon874 | 4:0b5a199e91f3 | 154 | } |
joon874 | 4:0b5a199e91f3 | 155 | if(name){ |
joon874 | 4:0b5a199e91f3 | 156 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 157 | printf("Token_name=%s\r\n",name); |
joon874 | 4:0b5a199e91f3 | 158 | #endif |
joon874 | 4:0b5a199e91f3 | 159 | if(tok){ |
joon874 | 4:0b5a199e91f3 | 160 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 161 | printf("It should be no parameters : tok=%s\r\n",tok); |
joon874 | 4:0b5a199e91f3 | 162 | #endif |
joon874 | 4:0b5a199e91f3 | 163 | errnum = 403; |
joon874 | 4:0b5a199e91f3 | 164 | } |
joon874 | 4:0b5a199e91f3 | 165 | } |
joon874 | 4:0b5a199e91f3 | 166 | else{ |
joon874 | 4:0b5a199e91f3 | 167 | errnum = 404; |
joon874 | 4:0b5a199e91f3 | 168 | } |
joon874 | 4:0b5a199e91f3 | 169 | } |
joon874 | 4:0b5a199e91f3 | 170 | if(errnum != 0){ |
joon874 | 4:0b5a199e91f3 | 171 | switch(pDataJson->_type) |
joon874 | 4:0b5a199e91f3 | 172 | { |
joon874 | 4:0b5a199e91f3 | 173 | case MbedJSONValue::TypeInt: |
joon874 | 4:0b5a199e91f3 | 174 | if(tmpJson->accessible){ |
joon874 | 4:0b5a199e91f3 | 175 | *tmpJson = pDataJson->_value.asInt; |
joon874 | 4:0b5a199e91f3 | 176 | tmpJson->cb_action(&tmpJson->_value); |
joon874 | 4:0b5a199e91f3 | 177 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 178 | printf("set int:%d\r\n",atoi(tok)); |
joon874 | 4:0b5a199e91f3 | 179 | #endif |
joon874 | 4:0b5a199e91f3 | 180 | } |
joon874 | 4:0b5a199e91f3 | 181 | else{ |
joon874 | 4:0b5a199e91f3 | 182 | errnum = 403; |
joon874 | 4:0b5a199e91f3 | 183 | } |
joon874 | 4:0b5a199e91f3 | 184 | break; |
joon874 | 4:0b5a199e91f3 | 185 | |
joon874 | 4:0b5a199e91f3 | 186 | case MbedJSONValue::TypeString: |
joon874 | 4:0b5a199e91f3 | 187 | if(tmpJson->accessible){ |
joon874 | 4:0b5a199e91f3 | 188 | *tmpJson = pDataJson->_value.asString; |
joon874 | 5:473a66ae9596 | 189 | tmpJson->cb_action((void*)tmpJson->_value.asString->c_str()); |
joon874 | 4:0b5a199e91f3 | 190 | } |
joon874 | 4:0b5a199e91f3 | 191 | else{ |
joon874 | 4:0b5a199e91f3 | 192 | errnum = 403; |
joon874 | 4:0b5a199e91f3 | 193 | } |
joon874 | 4:0b5a199e91f3 | 194 | break; |
joon874 | 4:0b5a199e91f3 | 195 | |
joon874 | 4:0b5a199e91f3 | 196 | case MbedJSONValue::TypeObject: |
joon874 | 4:0b5a199e91f3 | 197 | for(int i = 0; i < pDataJson->index_token; i++) |
joon874 | 4:0b5a199e91f3 | 198 | { |
joon874 | 4:0b5a199e91f3 | 199 | tok = (char*)pDataJson->token_name[i]->c_str(); |
joon874 | 4:0b5a199e91f3 | 200 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 201 | printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok); |
joon874 | 4:0b5a199e91f3 | 202 | #endif |
joon874 | 4:0b5a199e91f3 | 203 | if(tmpJson->hasMember(tok)){ |
joon874 | 4:0b5a199e91f3 | 204 | if((*tmpJson)[tok].accessible){ |
joon874 | 4:0b5a199e91f3 | 205 | errnum = 200; |
joon874 | 4:0b5a199e91f3 | 206 | if((*pDataJson)[i].size() > 0){ |
joon874 | 8:60d99da6eeb2 | 207 | /* |
joon874 | 8:60d99da6eeb2 | 208 | (*tmpJson)[tok] = (*pDataJson)[i]._value.asString; |
joon874 | 8:60d99da6eeb2 | 209 | (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); |
joon874 | 8:60d99da6eeb2 | 210 | */ |
joon874 | 8:60d99da6eeb2 | 211 | (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str(); |
joon874 | 8:60d99da6eeb2 | 212 | (*tmpJson)[tok].cb_action((void*)((*pDataJson)[i]._value.asString->c_str())); |
joon874 | 8:60d99da6eeb2 | 213 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 214 | printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str()); |
joon874 | 8:60d99da6eeb2 | 215 | #endif |
joon874 | 8:60d99da6eeb2 | 216 | } |
joon874 | 8:60d99da6eeb2 | 217 | else{ |
joon874 | 8:60d99da6eeb2 | 218 | (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt; |
joon874 | 8:60d99da6eeb2 | 219 | (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); |
joon874 | 8:60d99da6eeb2 | 220 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 221 | printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>()); |
joon874 | 8:60d99da6eeb2 | 222 | #endif |
joon874 | 8:60d99da6eeb2 | 223 | } |
joon874 | 8:60d99da6eeb2 | 224 | } |
joon874 | 8:60d99da6eeb2 | 225 | else{ |
joon874 | 8:60d99da6eeb2 | 226 | errnum = 403; break; |
joon874 | 8:60d99da6eeb2 | 227 | } |
joon874 | 8:60d99da6eeb2 | 228 | } |
joon874 | 8:60d99da6eeb2 | 229 | else{ |
joon874 | 8:60d99da6eeb2 | 230 | errnum = 404; break; |
joon874 | 8:60d99da6eeb2 | 231 | } |
joon874 | 8:60d99da6eeb2 | 232 | } |
joon874 | 8:60d99da6eeb2 | 233 | break; |
joon874 | 8:60d99da6eeb2 | 234 | |
joon874 | 8:60d99da6eeb2 | 235 | default: |
joon874 | 8:60d99da6eeb2 | 236 | errnum = 403; break; |
joon874 | 8:60d99da6eeb2 | 237 | } |
joon874 | 8:60d99da6eeb2 | 238 | } |
joon874 | 8:60d99da6eeb2 | 239 | |
joon874 | 8:60d99da6eeb2 | 240 | switch(errnum) |
joon874 | 8:60d99da6eeb2 | 241 | { |
joon874 | 8:60d99da6eeb2 | 242 | case 403: |
joon874 | 8:60d99da6eeb2 | 243 | strcpy (reply, "HTTP/1.1 403 OK\r\n"); |
joon874 | 8:60d99da6eeb2 | 244 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 8:60d99da6eeb2 | 245 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 8:60d99da6eeb2 | 246 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); |
joon874 | 8:60d99da6eeb2 | 247 | strcat(reply, "{\"Result : No Accessible\"}"); |
joon874 | 8:60d99da6eeb2 | 248 | strcat (reply, "\r\n\r\n"); |
joon874 | 8:60d99da6eeb2 | 249 | break; |
joon874 | 8:60d99da6eeb2 | 250 | |
joon874 | 8:60d99da6eeb2 | 251 | case 404: |
joon874 | 8:60d99da6eeb2 | 252 | strcpy (reply, "HTTP/1.1 404 OK\r\n"); |
joon874 | 8:60d99da6eeb2 | 253 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 8:60d99da6eeb2 | 254 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 8:60d99da6eeb2 | 255 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); |
joon874 | 8:60d99da6eeb2 | 256 | strcat(reply, "{\"Result\" : \"No defined Resource\"}"); |
joon874 | 8:60d99da6eeb2 | 257 | strcat (reply, "\r\n\r\n"); |
joon874 | 8:60d99da6eeb2 | 258 | break; |
joon874 | 8:60d99da6eeb2 | 259 | |
joon874 | 8:60d99da6eeb2 | 260 | case 200: |
joon874 | 8:60d99da6eeb2 | 261 | strcpy (reply, "HTTP/1.1 200 OK\r\n"); |
joon874 | 8:60d99da6eeb2 | 262 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 8:60d99da6eeb2 | 263 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 8:60d99da6eeb2 | 264 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4); |
joon874 | 8:60d99da6eeb2 | 265 | strcat (reply, "{\"Result\":\"OK\"}"); |
joon874 | 8:60d99da6eeb2 | 266 | strcat (reply, "\r\n\r\n"); |
joon874 | 8:60d99da6eeb2 | 267 | break; |
joon874 | 8:60d99da6eeb2 | 268 | } |
joon874 | 8:60d99da6eeb2 | 269 | |
joon874 | 8:60d99da6eeb2 | 270 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 271 | printf("Before Delete\r\n"); |
joon874 | 8:60d99da6eeb2 | 272 | debug_info(); |
joon874 | 8:60d99da6eeb2 | 273 | #endif |
joon874 | 8:60d99da6eeb2 | 274 | if(pDataJson){ |
joon874 | 8:60d99da6eeb2 | 275 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 276 | printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token); |
joon874 | 8:60d99da6eeb2 | 277 | #endif |
joon874 | 8:60d99da6eeb2 | 278 | delete pDataJson; |
joon874 | 8:60d99da6eeb2 | 279 | pDataJson = 0; |
joon874 | 8:60d99da6eeb2 | 280 | } |
joon874 | 8:60d99da6eeb2 | 281 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 282 | printf("After Delete\r\n"); |
joon874 | 8:60d99da6eeb2 | 283 | debug_info(); |
joon874 | 8:60d99da6eeb2 | 284 | #endif |
joon874 | 8:60d99da6eeb2 | 285 | } |
joon874 | 8:60d99da6eeb2 | 286 | #endif |
joon874 | 8:60d99da6eeb2 | 287 | |
joon874 | 8:60d99da6eeb2 | 288 | void PutRequestHandler::handle(char* rest_uri, char* request_data, char *reply) |
joon874 | 8:60d99da6eeb2 | 289 | { |
joon874 | 8:60d99da6eeb2 | 290 | MbedJSONValue* tmpJson; |
joon874 | 8:60d99da6eeb2 | 291 | char* tok; |
joon874 | 8:60d99da6eeb2 | 292 | char* last; |
joon874 | 8:60d99da6eeb2 | 293 | int errnum = 200; |
joon874 | 8:60d99da6eeb2 | 294 | |
joon874 | 8:60d99da6eeb2 | 295 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 296 | printf("PutRequestHandler():%s\r\n",request_data+4); |
joon874 | 8:60d99da6eeb2 | 297 | #endif |
joon874 | 8:60d99da6eeb2 | 298 | if(!pDataJson){ |
joon874 | 8:60d99da6eeb2 | 299 | pDataJson = new MbedJSONValue(); |
joon874 | 8:60d99da6eeb2 | 300 | } |
joon874 | 8:60d99da6eeb2 | 301 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 302 | printf("Before Parse\r\n"); |
joon874 | 9:f7d5a168a693 | 303 | //debug_info(); |
joon874 | 8:60d99da6eeb2 | 304 | #endif |
joon874 | 8:60d99da6eeb2 | 305 | parse(*pDataJson,(const char*)request_data); |
joon874 | 8:60d99da6eeb2 | 306 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 307 | printf("After Parse\r\n"); |
joon874 | 9:f7d5a168a693 | 308 | //debug_info(); |
joon874 | 8:60d99da6eeb2 | 309 | printf("DataJson._type=%d\r\n",pDataJson->_type); |
joon874 | 8:60d99da6eeb2 | 310 | printf("DataJson=%s\r\n",pDataJson->serialize().c_str()); |
joon874 | 8:60d99da6eeb2 | 311 | printf("DataJson.size()=%d\r\n",pDataJson->size()); |
joon874 | 8:60d99da6eeb2 | 312 | printf("DataJason.type=%d\r\n",pDataJson->_type); |
joon874 | 8:60d99da6eeb2 | 313 | #endif |
joon874 | 8:60d99da6eeb2 | 314 | |
joon874 | 8:60d99da6eeb2 | 315 | if(!strcmp(rest_uri, "/")){ |
joon874 | 8:60d99da6eeb2 | 316 | tmpJson = &WIZwikiREST; |
joon874 | 8:60d99da6eeb2 | 317 | } |
joon874 | 8:60d99da6eeb2 | 318 | else{ |
joon874 | 8:60d99da6eeb2 | 319 | tok = strtok_r(rest_uri+1, "/", &last); |
joon874 | 8:60d99da6eeb2 | 320 | tmpJson = &WIZwikiREST; |
joon874 | 8:60d99da6eeb2 | 321 | |
joon874 | 8:60d99da6eeb2 | 322 | char* name = 0; |
joon874 | 8:60d99da6eeb2 | 323 | while(tok) |
joon874 | 8:60d99da6eeb2 | 324 | { |
joon874 | 8:60d99da6eeb2 | 325 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 326 | printf("tok = %s \r\n", tok); |
joon874 | 8:60d99da6eeb2 | 327 | #endif |
joon874 | 8:60d99da6eeb2 | 328 | if(tmpJson->hasMember(tok)){ |
joon874 | 8:60d99da6eeb2 | 329 | tmpJson = &((*tmpJson)[tok]); |
joon874 | 8:60d99da6eeb2 | 330 | name = tok; |
joon874 | 8:60d99da6eeb2 | 331 | tok = strtok_r(0, "/", &last); |
joon874 | 8:60d99da6eeb2 | 332 | } |
joon874 | 8:60d99da6eeb2 | 333 | else{ |
joon874 | 8:60d99da6eeb2 | 334 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 335 | printf("No Member\r\n"); |
joon874 | 8:60d99da6eeb2 | 336 | #endif |
joon874 | 8:60d99da6eeb2 | 337 | break; |
joon874 | 8:60d99da6eeb2 | 338 | } |
joon874 | 8:60d99da6eeb2 | 339 | } |
joon874 | 8:60d99da6eeb2 | 340 | if(name){ |
joon874 | 8:60d99da6eeb2 | 341 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 342 | printf("Token_name=%s\r\n",name); |
joon874 | 8:60d99da6eeb2 | 343 | #endif |
joon874 | 8:60d99da6eeb2 | 344 | if(tok){ |
joon874 | 8:60d99da6eeb2 | 345 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 346 | printf("It should be no parameters : tok=%s\r\n",tok); |
joon874 | 8:60d99da6eeb2 | 347 | #endif |
joon874 | 8:60d99da6eeb2 | 348 | errnum = 403; |
joon874 | 8:60d99da6eeb2 | 349 | } |
joon874 | 8:60d99da6eeb2 | 350 | } |
joon874 | 8:60d99da6eeb2 | 351 | else{ |
joon874 | 8:60d99da6eeb2 | 352 | errnum = 404; |
joon874 | 8:60d99da6eeb2 | 353 | } |
joon874 | 8:60d99da6eeb2 | 354 | } |
joon874 | 8:60d99da6eeb2 | 355 | if(errnum != 0){ |
joon874 | 8:60d99da6eeb2 | 356 | switch(pDataJson->_type) |
joon874 | 8:60d99da6eeb2 | 357 | { |
joon874 | 8:60d99da6eeb2 | 358 | case MbedJSONValue::TypeInt: |
joon874 | 8:60d99da6eeb2 | 359 | if(tmpJson->accessible){ |
joon874 | 8:60d99da6eeb2 | 360 | *tmpJson = pDataJson->_value.asInt; |
joon874 | 8:60d99da6eeb2 | 361 | tmpJson->cb_action(&tmpJson->_value); |
joon874 | 8:60d99da6eeb2 | 362 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 363 | printf("set int:%d\r\n",atoi(tok)); |
joon874 | 8:60d99da6eeb2 | 364 | #endif |
joon874 | 8:60d99da6eeb2 | 365 | } |
joon874 | 8:60d99da6eeb2 | 366 | else{ |
joon874 | 8:60d99da6eeb2 | 367 | errnum = 403; |
joon874 | 8:60d99da6eeb2 | 368 | } |
joon874 | 8:60d99da6eeb2 | 369 | break; |
joon874 | 8:60d99da6eeb2 | 370 | |
joon874 | 8:60d99da6eeb2 | 371 | case MbedJSONValue::TypeString: |
joon874 | 8:60d99da6eeb2 | 372 | if(tmpJson->accessible){ |
joon874 | 8:60d99da6eeb2 | 373 | *tmpJson = pDataJson->_value.asString; |
joon874 | 8:60d99da6eeb2 | 374 | tmpJson->cb_action((void*)tmpJson->_value.asString->c_str()); |
joon874 | 8:60d99da6eeb2 | 375 | } |
joon874 | 8:60d99da6eeb2 | 376 | else{ |
joon874 | 8:60d99da6eeb2 | 377 | errnum = 403; |
joon874 | 8:60d99da6eeb2 | 378 | } |
joon874 | 8:60d99da6eeb2 | 379 | break; |
joon874 | 8:60d99da6eeb2 | 380 | |
joon874 | 8:60d99da6eeb2 | 381 | case MbedJSONValue::TypeObject: |
joon874 | 8:60d99da6eeb2 | 382 | for(int i = 0; i < pDataJson->index_token; i++) |
joon874 | 8:60d99da6eeb2 | 383 | { |
joon874 | 8:60d99da6eeb2 | 384 | tok = (char*)pDataJson->token_name[i]->c_str(); |
joon874 | 8:60d99da6eeb2 | 385 | #ifdef DEBUG_HTTPREQ |
joon874 | 8:60d99da6eeb2 | 386 | printf("pDataJson.token_name[%d]->c_str()=%s\r\n",i,tok); |
joon874 | 8:60d99da6eeb2 | 387 | #endif |
joon874 | 8:60d99da6eeb2 | 388 | if(tmpJson->hasMember(tok)){ |
joon874 | 8:60d99da6eeb2 | 389 | if((*tmpJson)[tok].accessible){ |
joon874 | 8:60d99da6eeb2 | 390 | errnum = 200; |
joon874 | 8:60d99da6eeb2 | 391 | if((*pDataJson)[i].size() > 0){ |
joon874 | 5:473a66ae9596 | 392 | //(*tmpJson)[tok] = (*pDataJson)[i]._value.asString; |
joon874 | 5:473a66ae9596 | 393 | (*tmpJson)[tok] = (*pDataJson)[i]._value.asString->c_str(); |
joon874 | 5:473a66ae9596 | 394 | |
joon874 | 5:473a66ae9596 | 395 | //(*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); |
joon874 | 5:473a66ae9596 | 396 | (*tmpJson)[tok].cb_action((void*)((*pDataJson)[i]._value.asString->c_str())); |
joon874 | 4:0b5a199e91f3 | 397 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 398 | printf("String Updated: %s : %s\r\n", tok,(*tmpJson)[tok].get<std::string>().c_str()); |
joon874 | 4:0b5a199e91f3 | 399 | #endif |
joon874 | 4:0b5a199e91f3 | 400 | } |
joon874 | 4:0b5a199e91f3 | 401 | else{ |
joon874 | 4:0b5a199e91f3 | 402 | (*tmpJson)[tok] = (*pDataJson)[i]._value.asInt; |
joon874 | 4:0b5a199e91f3 | 403 | (*tmpJson)[tok].cb_action((void*)&((*pDataJson)[i]._value.asInt)); |
joon874 | 4:0b5a199e91f3 | 404 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 405 | printf("Int Updated: %s : %d\r\n", tok,(*tmpJson)[tok].get<int>()); |
joon874 | 4:0b5a199e91f3 | 406 | #endif |
joon874 | 4:0b5a199e91f3 | 407 | } |
joon874 | 4:0b5a199e91f3 | 408 | } |
joon874 | 4:0b5a199e91f3 | 409 | else{ |
joon874 | 4:0b5a199e91f3 | 410 | errnum = 403; break; |
joon874 | 4:0b5a199e91f3 | 411 | } |
joon874 | 4:0b5a199e91f3 | 412 | } |
joon874 | 4:0b5a199e91f3 | 413 | else{ |
joon874 | 4:0b5a199e91f3 | 414 | errnum = 404; break; |
joon874 | 4:0b5a199e91f3 | 415 | } |
joon874 | 4:0b5a199e91f3 | 416 | } |
joon874 | 4:0b5a199e91f3 | 417 | break; |
joon874 | 4:0b5a199e91f3 | 418 | |
joon874 | 4:0b5a199e91f3 | 419 | default: |
joon874 | 4:0b5a199e91f3 | 420 | errnum = 403; break; |
joon874 | 4:0b5a199e91f3 | 421 | } |
joon874 | 4:0b5a199e91f3 | 422 | } |
joon874 | 4:0b5a199e91f3 | 423 | |
joon874 | 4:0b5a199e91f3 | 424 | switch(errnum) |
joon874 | 4:0b5a199e91f3 | 425 | { |
joon874 | 4:0b5a199e91f3 | 426 | case 403: |
joon874 | 4:0b5a199e91f3 | 427 | strcpy (reply, "HTTP/1.1 403 OK\r\n"); |
joon874 | 4:0b5a199e91f3 | 428 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 4:0b5a199e91f3 | 429 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 4:0b5a199e91f3 | 430 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); |
joon874 | 4:0b5a199e91f3 | 431 | strcat(reply, "{\"Result : No Accessible\"}"); |
joon874 | 4:0b5a199e91f3 | 432 | strcat (reply, "\r\n\r\n"); |
joon874 | 4:0b5a199e91f3 | 433 | break; |
joon874 | 4:0b5a199e91f3 | 434 | |
joon874 | 4:0b5a199e91f3 | 435 | case 404: |
joon874 | 4:0b5a199e91f3 | 436 | strcpy (reply, "HTTP/1.1 404 OK\r\n"); |
joon874 | 4:0b5a199e91f3 | 437 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 4:0b5a199e91f3 | 438 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 4:0b5a199e91f3 | 439 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); |
joon874 | 4:0b5a199e91f3 | 440 | strcat(reply, "{\"Result\" : \"No defined Resource\"}"); |
joon874 | 4:0b5a199e91f3 | 441 | strcat (reply, "\r\n\r\n"); |
joon874 | 4:0b5a199e91f3 | 442 | break; |
joon874 | 4:0b5a199e91f3 | 443 | |
joon874 | 4:0b5a199e91f3 | 444 | case 200: |
joon874 | 4:0b5a199e91f3 | 445 | strcpy (reply, "HTTP/1.1 200 OK\r\n"); |
joon874 | 4:0b5a199e91f3 | 446 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 4:0b5a199e91f3 | 447 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 4:0b5a199e91f3 | 448 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 15+4); |
joon874 | 4:0b5a199e91f3 | 449 | strcat (reply, "{\"Result\":\"OK\"}"); |
joon874 | 4:0b5a199e91f3 | 450 | strcat (reply, "\r\n\r\n"); |
joon874 | 4:0b5a199e91f3 | 451 | break; |
joon874 | 4:0b5a199e91f3 | 452 | } |
joon874 | 4:0b5a199e91f3 | 453 | |
joon874 | 4:0b5a199e91f3 | 454 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 455 | printf("Before Delete\r\n"); |
joon874 | 9:f7d5a168a693 | 456 | //debug_info(); |
joon874 | 4:0b5a199e91f3 | 457 | #endif |
joon874 | 4:0b5a199e91f3 | 458 | if(pDataJson){ |
joon874 | 4:0b5a199e91f3 | 459 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 460 | printf("type;%d, pDataJson->index_token=%d\r\n",pDataJson->_type,pDataJson->index_token); |
joon874 | 4:0b5a199e91f3 | 461 | #endif |
joon874 | 4:0b5a199e91f3 | 462 | delete pDataJson; |
joon874 | 4:0b5a199e91f3 | 463 | pDataJson = 0; |
joon874 | 4:0b5a199e91f3 | 464 | } |
joon874 | 4:0b5a199e91f3 | 465 | #ifdef DEBUG_HTTPREQ |
joon874 | 4:0b5a199e91f3 | 466 | printf("After Delete\r\n"); |
joon874 | 9:f7d5a168a693 | 467 | //debug_info(); |
joon874 | 4:0b5a199e91f3 | 468 | #endif |
joon874 | 4:0b5a199e91f3 | 469 | } |
joon874 | 4:0b5a199e91f3 | 470 | |
joon874 | 4:0b5a199e91f3 | 471 | /* |
joon874 | 4:0b5a199e91f3 | 472 | void DeleteRequestHandler::handle(char* rest_uri, char* request_data, char *reply) |
joon874 | 0:5886f525a4ad | 473 | { |
joon874 | 0:5886f525a4ad | 474 | } |
joon874 | 4:0b5a199e91f3 | 475 | */ |