![](/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@2:ca36ecca24c2, 2016-05-03 (annotated)
- Committer:
- joon874
- Date:
- Tue May 03 02:15:59 2016 +0000
- Revision:
- 2:ca36ecca24c2
- Parent:
- 1:728e5b5c8dae
- Child:
- 4:0b5a199e91f3
Revision 2016.05.03
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 | 0:5886f525a4ad | 9 | void GetRequestHandler::handle(char* rest_uri, char *reply) |
joon874 | 0:5886f525a4ad | 10 | { |
joon874 | 0:5886f525a4ad | 11 | MbedJSONValue* tmpJson; |
joon874 | 0:5886f525a4ad | 12 | |
joon874 | 0:5886f525a4ad | 13 | char* tok; |
joon874 | 0:5886f525a4ad | 14 | char* last; |
joon874 | 2:ca36ecca24c2 | 15 | const char * pchar = 0; |
joon874 | 2:ca36ecca24c2 | 16 | #ifdef DEBUG_HTTPREQ |
joon874 | 0:5886f525a4ad | 17 | printf("GetRequestHandler():%s\r\n",rest_uri); |
joon874 | 2:ca36ecca24c2 | 18 | #endif |
joon874 | 0:5886f525a4ad | 19 | if(!strcmp(rest_uri, "/")) |
joon874 | 0:5886f525a4ad | 20 | { |
joon874 | 2:ca36ecca24c2 | 21 | tmpJson = &WIZwikiREST; |
joon874 | 0:5886f525a4ad | 22 | } |
joon874 | 2:ca36ecca24c2 | 23 | else |
joon874 | 2:ca36ecca24c2 | 24 | { |
joon874 | 0:5886f525a4ad | 25 | tok = strtok_r(rest_uri+1, "/", &last); // 20160226 |
joon874 | 0:5886f525a4ad | 26 | tmpJson = &WIZwikiREST; |
joon874 | 2:ca36ecca24c2 | 27 | |
joon874 | 2:ca36ecca24c2 | 28 | char depth = 0; |
joon874 | 2:ca36ecca24c2 | 29 | char* name = 0; |
joon874 | 2:ca36ecca24c2 | 30 | while(tok) |
joon874 | 2:ca36ecca24c2 | 31 | { |
joon874 | 2:ca36ecca24c2 | 32 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 33 | printf("tok = %s \r\n", tok); // Name |
joon874 | 2:ca36ecca24c2 | 34 | #endif |
joon874 | 2:ca36ecca24c2 | 35 | if(tmpJson->hasMember(tok)) |
joon874 | 2:ca36ecca24c2 | 36 | { |
joon874 | 2:ca36ecca24c2 | 37 | tmpJson = &((*tmpJson)[tok]); |
joon874 | 2:ca36ecca24c2 | 38 | name = tok; |
joon874 | 2:ca36ecca24c2 | 39 | tok = strtok_r(0, "/", &last); |
joon874 | 2:ca36ecca24c2 | 40 | depth++; |
joon874 | 2:ca36ecca24c2 | 41 | } |
joon874 | 2:ca36ecca24c2 | 42 | else |
joon874 | 2:ca36ecca24c2 | 43 | { |
joon874 | 2:ca36ecca24c2 | 44 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 45 | printf("No Member\r\n"); |
joon874 | 2:ca36ecca24c2 | 46 | #endif |
joon874 | 2:ca36ecca24c2 | 47 | break; |
joon874 | 2:ca36ecca24c2 | 48 | } |
joon874 | 2:ca36ecca24c2 | 49 | } |
joon874 | 2:ca36ecca24c2 | 50 | if(name) |
joon874 | 0:5886f525a4ad | 51 | { |
joon874 | 2:ca36ecca24c2 | 52 | if(tok) |
joon874 | 2:ca36ecca24c2 | 53 | { |
joon874 | 2:ca36ecca24c2 | 54 | if(tmpJson->accessible) |
joon874 | 2:ca36ecca24c2 | 55 | { |
joon874 | 2:ca36ecca24c2 | 56 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 57 | printf("accessible : tmpJson->size()=%d\r\n",tmpJson->size()); |
joon874 | 2:ca36ecca24c2 | 58 | #endif |
joon874 | 2:ca36ecca24c2 | 59 | |
joon874 | 2:ca36ecca24c2 | 60 | if(tmpJson->size() > 0) { |
joon874 | 2:ca36ecca24c2 | 61 | *tmpJson = std::string(tok); |
joon874 | 2:ca36ecca24c2 | 62 | tmpJson->cb_action((void*)tok); |
joon874 | 2:ca36ecca24c2 | 63 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 64 | printf("set string:%s\r\n",tok); |
joon874 | 2:ca36ecca24c2 | 65 | #endif |
joon874 | 2:ca36ecca24c2 | 66 | } |
joon874 | 2:ca36ecca24c2 | 67 | else { |
joon874 | 2:ca36ecca24c2 | 68 | *tmpJson = atoi(tok); |
joon874 | 2:ca36ecca24c2 | 69 | tmpJson->cb_action(&tmpJson->_value); |
joon874 | 2:ca36ecca24c2 | 70 | #ifdef DEBUG_HTTPREQ |
joon874 | 2:ca36ecca24c2 | 71 | printf("set int:%d\r\n",atoi(tok)); |
joon874 | 2:ca36ecca24c2 | 72 | #endif |
joon874 | 2:ca36ecca24c2 | 73 | } |
joon874 | 2:ca36ecca24c2 | 74 | } |
joon874 | 2:ca36ecca24c2 | 75 | else |
joon874 | 2:ca36ecca24c2 | 76 | { |
joon874 | 2:ca36ecca24c2 | 77 | strcpy (reply, "HTTP/1.1 403 OK\r\n"); |
joon874 | 2:ca36ecca24c2 | 78 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 2:ca36ecca24c2 | 79 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 2:ca36ecca24c2 | 80 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 26+4); |
joon874 | 2:ca36ecca24c2 | 81 | strcat(reply, "{\"Result : No Accessible\"}"); |
joon874 | 2:ca36ecca24c2 | 82 | strcat (reply, "\r\n\r\n"); |
joon874 | 2:ca36ecca24c2 | 83 | return; |
joon874 | 2:ca36ecca24c2 | 84 | } |
joon874 | 2:ca36ecca24c2 | 85 | } |
MidnightCow | 1:728e5b5c8dae | 86 | } |
joon874 | 0:5886f525a4ad | 87 | else |
joon874 | 0:5886f525a4ad | 88 | { |
joon874 | 2:ca36ecca24c2 | 89 | strcpy (reply, "HTTP/1.1 404 OK\r\n"); |
joon874 | 2:ca36ecca24c2 | 90 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 2:ca36ecca24c2 | 91 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 2:ca36ecca24c2 | 92 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", 33+4); |
joon874 | 2:ca36ecca24c2 | 93 | strcat(reply, "{\"Result\" : \"No defined Resource\"}"); |
joon874 | 2:ca36ecca24c2 | 94 | strcat (reply, "\r\n\r\n"); |
joon874 | 2:ca36ecca24c2 | 95 | return; |
joon874 | 0:5886f525a4ad | 96 | } |
MidnightCow | 1:728e5b5c8dae | 97 | } |
joon874 | 2:ca36ecca24c2 | 98 | pchar = (*tmpJson).serialize().c_str(); |
joon874 | 2:ca36ecca24c2 | 99 | strcpy (reply, "HTTP/1.1 200 OK\r\n"); |
joon874 | 2:ca36ecca24c2 | 100 | strcat (reply, "Sever: WIZwiki-REST\r\n"); |
joon874 | 2:ca36ecca24c2 | 101 | strcat (reply, "content-Type: text/json\r\n"); |
joon874 | 2:ca36ecca24c2 | 102 | sprintf(reply + strlen(reply), "Content-Length: %d\r\n\r\n", strlen(pchar)+4); |
joon874 | 2:ca36ecca24c2 | 103 | strcat (reply, pchar); |
joon874 | 2:ca36ecca24c2 | 104 | strcat (reply, "\r\n\r\n"); |
joon874 | 2:ca36ecca24c2 | 105 | |
MidnightCow | 1:728e5b5c8dae | 106 | return; |
joon874 | 0:5886f525a4ad | 107 | } |
joon874 | 0:5886f525a4ad | 108 | |
joon874 | 0:5886f525a4ad | 109 | void PutRequestHandler::handle(char* rest_uri, char *reply) |
joon874 | 0:5886f525a4ad | 110 | { |
joon874 | 0:5886f525a4ad | 111 | } |
joon874 | 0:5886f525a4ad | 112 | |
joon874 | 0:5886f525a4ad | 113 | void DeleteRequestHandler::handle(char* rest_uri, char *reply) |
joon874 | 0:5886f525a4ad | 114 | { |
joon874 | 0:5886f525a4ad | 115 | } |