student test for professor

Dependencies:   EthernetInterface mbed-rpc mbed-rtos mbed

Fork of HTTP-Server by Francois Berder

Committer:
feb11
Date:
Wed Jul 17 10:15:05 2013 +0000
Revision:
0:9e4bcb10b3e3
Initial import

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:9e4bcb10b3e3 1 #include "RPCCommand.h"
feb11 0:9e4bcb10b3e3 2 #include "mbed.h"
feb11 0:9e4bcb10b3e3 3 #include "RPCType.h"
feb11 0:9e4bcb10b3e3 4
feb11 0:9e4bcb10b3e3 5
feb11 0:9e4bcb10b3e3 6 RPCCommand::RPCCommand():
feb11 0:9e4bcb10b3e3 7 cmd(),
feb11 0:9e4bcb10b3e3 8 obj_name(NULL),
feb11 0:9e4bcb10b3e3 9 func_name(NULL)
feb11 0:9e4bcb10b3e3 10 {
feb11 0:9e4bcb10b3e3 11
feb11 0:9e4bcb10b3e3 12 }
feb11 0:9e4bcb10b3e3 13
feb11 0:9e4bcb10b3e3 14 bool RPCCommand::decode(char *buffer)
feb11 0:9e4bcb10b3e3 15 {
feb11 0:9e4bcb10b3e3 16 if(buffer == NULL)
feb11 0:9e4bcb10b3e3 17 return false;
feb11 0:9e4bcb10b3e3 18 if(buffer[0] != '/')
feb11 0:9e4bcb10b3e3 19 return false;
feb11 0:9e4bcb10b3e3 20
feb11 0:9e4bcb10b3e3 21 ++buffer;
feb11 0:9e4bcb10b3e3 22 char *tmp = strchr(buffer ,'/');
feb11 0:9e4bcb10b3e3 23
feb11 0:9e4bcb10b3e3 24 if(tmp == NULL)
feb11 0:9e4bcb10b3e3 25 return false;
feb11 0:9e4bcb10b3e3 26 if(tmp == buffer)
feb11 0:9e4bcb10b3e3 27 return false;
feb11 0:9e4bcb10b3e3 28
feb11 0:9e4bcb10b3e3 29 tmp[0] = '\0';
feb11 0:9e4bcb10b3e3 30 obj_name = buffer;
feb11 0:9e4bcb10b3e3 31
feb11 0:9e4bcb10b3e3 32 buffer = tmp+1;
feb11 0:9e4bcb10b3e3 33
feb11 0:9e4bcb10b3e3 34 if(buffer[0] == '\0' || buffer[0] == '?')
feb11 0:9e4bcb10b3e3 35 return false;
feb11 0:9e4bcb10b3e3 36
feb11 0:9e4bcb10b3e3 37 func_name = buffer;
feb11 0:9e4bcb10b3e3 38
feb11 0:9e4bcb10b3e3 39 tmp = strchr(buffer, '?');
feb11 0:9e4bcb10b3e3 40 if(tmp != NULL)
feb11 0:9e4bcb10b3e3 41 {
feb11 0:9e4bcb10b3e3 42 if(tmp[1] == '\0')
feb11 0:9e4bcb10b3e3 43 return false;
feb11 0:9e4bcb10b3e3 44 tmp[0] = '\0';
feb11 0:9e4bcb10b3e3 45 }
feb11 0:9e4bcb10b3e3 46
feb11 0:9e4bcb10b3e3 47 cmd[0] = '\0';
feb11 0:9e4bcb10b3e3 48 strcat(cmd, "/");
feb11 0:9e4bcb10b3e3 49 strcat(cmd, obj_name);
feb11 0:9e4bcb10b3e3 50 strcat(cmd, "/");
feb11 0:9e4bcb10b3e3 51 strcat(cmd, func_name);
feb11 0:9e4bcb10b3e3 52
feb11 0:9e4bcb10b3e3 53 if(tmp == NULL)
feb11 0:9e4bcb10b3e3 54 return true;
feb11 0:9e4bcb10b3e3 55
feb11 0:9e4bcb10b3e3 56 buffer = tmp+1;
feb11 0:9e4bcb10b3e3 57 do
feb11 0:9e4bcb10b3e3 58 {
feb11 0:9e4bcb10b3e3 59 tmp = strchr(buffer, '&');
feb11 0:9e4bcb10b3e3 60
feb11 0:9e4bcb10b3e3 61 if(tmp != NULL)
feb11 0:9e4bcb10b3e3 62 {
feb11 0:9e4bcb10b3e3 63 if(tmp[1] == '\0' || buffer == tmp)
feb11 0:9e4bcb10b3e3 64 return false;
feb11 0:9e4bcb10b3e3 65 tmp[0] = '\0';
feb11 0:9e4bcb10b3e3 66 }
feb11 0:9e4bcb10b3e3 67
feb11 0:9e4bcb10b3e3 68 char *sep = strchr(buffer, '=');
feb11 0:9e4bcb10b3e3 69 if(sep == NULL)
feb11 0:9e4bcb10b3e3 70 return false;
feb11 0:9e4bcb10b3e3 71 if(sep == buffer)
feb11 0:9e4bcb10b3e3 72 return false;
feb11 0:9e4bcb10b3e3 73 if(sep[1] == '\0' || sep[1] == '&')
feb11 0:9e4bcb10b3e3 74 return false;
feb11 0:9e4bcb10b3e3 75
feb11 0:9e4bcb10b3e3 76 strcat(cmd, " ");
feb11 0:9e4bcb10b3e3 77 strcat(cmd, sep+1);
feb11 0:9e4bcb10b3e3 78
feb11 0:9e4bcb10b3e3 79 if(tmp != NULL)
feb11 0:9e4bcb10b3e3 80 buffer = tmp+1;
feb11 0:9e4bcb10b3e3 81 else
feb11 0:9e4bcb10b3e3 82 buffer = NULL;
feb11 0:9e4bcb10b3e3 83 }while(buffer);
feb11 0:9e4bcb10b3e3 84
feb11 0:9e4bcb10b3e3 85 return true;
feb11 0:9e4bcb10b3e3 86 }
feb11 0:9e4bcb10b3e3 87
feb11 0:9e4bcb10b3e3 88
feb11 0:9e4bcb10b3e3 89
feb11 0:9e4bcb10b3e3 90 char* RPCCommand::get_cmd() const
feb11 0:9e4bcb10b3e3 91 {
feb11 0:9e4bcb10b3e3 92 return (char*)cmd;
feb11 0:9e4bcb10b3e3 93 }
feb11 0:9e4bcb10b3e3 94
feb11 0:9e4bcb10b3e3 95 RPC_COMMAND_TYPE RPCCommand::get_type() const
feb11 0:9e4bcb10b3e3 96 {
feb11 0:9e4bcb10b3e3 97 if(!strcmp(func_name, "new") && RPCType::instance().is_supported_type(obj_name))
feb11 0:9e4bcb10b3e3 98 return CREATE;
feb11 0:9e4bcb10b3e3 99
feb11 0:9e4bcb10b3e3 100 RPC* r = RPC::lookup(obj_name);
feb11 0:9e4bcb10b3e3 101 if(r == NULL)
feb11 0:9e4bcb10b3e3 102 return INVALID;
feb11 0:9e4bcb10b3e3 103
feb11 0:9e4bcb10b3e3 104 if(!strcmp(func_name, "delete"))
feb11 0:9e4bcb10b3e3 105 return DELETE;
feb11 0:9e4bcb10b3e3 106
feb11 0:9e4bcb10b3e3 107 const struct rpc_method *methods = r->get_rpc_methods();
feb11 0:9e4bcb10b3e3 108 int i = 0;
feb11 0:9e4bcb10b3e3 109 while(methods[i].name != NULL)
feb11 0:9e4bcb10b3e3 110 {
feb11 0:9e4bcb10b3e3 111 if(!strcmp(func_name, methods[i].name))
feb11 0:9e4bcb10b3e3 112 {
feb11 0:9e4bcb10b3e3 113 return FUNCTION_CALL;
feb11 0:9e4bcb10b3e3 114 }
feb11 0:9e4bcb10b3e3 115 ++i;
feb11 0:9e4bcb10b3e3 116 }
feb11 0:9e4bcb10b3e3 117
feb11 0:9e4bcb10b3e3 118 return INVALID;
feb11 0:9e4bcb10b3e3 119 }
feb11 0:9e4bcb10b3e3 120
feb11 0:9e4bcb10b3e3 121 char* RPCCommand::get_obj_name() const
feb11 0:9e4bcb10b3e3 122 {
feb11 0:9e4bcb10b3e3 123 return obj_name;
feb11 0:9e4bcb10b3e3 124 }
feb11 0:9e4bcb10b3e3 125
feb11 0:9e4bcb10b3e3 126 char* RPCCommand::get_func_name() const
feb11 0:9e4bcb10b3e3 127 {
feb11 0:9e4bcb10b3e3 128 return func_name;
feb11 0:9e4bcb10b3e3 129 }
feb11 0:9e4bcb10b3e3 130