
An example HTTP Server using new Ethernet Interface and localfilesystem.
Dependencies: EthernetInterface HttpServer mbed-rpc mbed-rtos mbed
Fork of giken9_HTMLServer_Sample by
HttpServer/Handler/RPCHandler.cpp@3:f0c16cc3a4ef, 2014-12-23 (annotated)
- Committer:
- mkilivan
- Date:
- Tue Dec 23 18:40:55 2014 +0000
- Revision:
- 3:f0c16cc3a4ef
- Parent:
- 0:7766f6712673
Used library links instead of cloned files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yueee_yt | 0:7766f6712673 | 1 | /* |
yueee_yt | 0:7766f6712673 | 2 | Permission is hereby granted, free of charge, to any person obtaining a copy |
yueee_yt | 0:7766f6712673 | 3 | of this software and associated documentation files (the "Software"), to deal |
yueee_yt | 0:7766f6712673 | 4 | in the Software without restriction, including without limitation the rights |
yueee_yt | 0:7766f6712673 | 5 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
yueee_yt | 0:7766f6712673 | 6 | copies of the Software, and to permit persons to whom the Software is |
yueee_yt | 0:7766f6712673 | 7 | furnished to do so, subject to the following conditions: |
yueee_yt | 0:7766f6712673 | 8 | |
yueee_yt | 0:7766f6712673 | 9 | The above copyright notice and this permission notice shall be included in |
yueee_yt | 0:7766f6712673 | 10 | all copies or substantial portions of the Software. |
yueee_yt | 0:7766f6712673 | 11 | |
yueee_yt | 0:7766f6712673 | 12 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
yueee_yt | 0:7766f6712673 | 13 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
yueee_yt | 0:7766f6712673 | 14 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
yueee_yt | 0:7766f6712673 | 15 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
yueee_yt | 0:7766f6712673 | 16 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
yueee_yt | 0:7766f6712673 | 17 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
yueee_yt | 0:7766f6712673 | 18 | THE SOFTWARE. |
yueee_yt | 0:7766f6712673 | 19 | */ |
yueee_yt | 0:7766f6712673 | 20 | |
yueee_yt | 0:7766f6712673 | 21 | //#define _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 22 | |
yueee_yt | 0:7766f6712673 | 23 | #include "RPCHandler.h" |
yueee_yt | 0:7766f6712673 | 24 | #include "mbed_rpc.h" |
yueee_yt | 0:7766f6712673 | 25 | |
yueee_yt | 0:7766f6712673 | 26 | #define RPC_DATA_LEN 64 |
yueee_yt | 0:7766f6712673 | 27 | |
yueee_yt | 0:7766f6712673 | 28 | RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection) |
yueee_yt | 0:7766f6712673 | 29 | {} |
yueee_yt | 0:7766f6712673 | 30 | |
yueee_yt | 0:7766f6712673 | 31 | RPCHandler::~RPCHandler() |
yueee_yt | 0:7766f6712673 | 32 | { |
yueee_yt | 0:7766f6712673 | 33 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 34 | printf("++++(RPC Handler)Handler destroyed\r\n"); |
yueee_yt | 0:7766f6712673 | 35 | #endif |
yueee_yt | 0:7766f6712673 | 36 | } |
yueee_yt | 0:7766f6712673 | 37 | |
yueee_yt | 0:7766f6712673 | 38 | void RPCHandler::doGet() |
yueee_yt | 0:7766f6712673 | 39 | { |
yueee_yt | 0:7766f6712673 | 40 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 41 | printf("++++(RPC Handler)doGet\r\n"); |
yueee_yt | 0:7766f6712673 | 42 | #endif |
yueee_yt | 0:7766f6712673 | 43 | char resp[RPC_DATA_LEN] = {0}; |
yueee_yt | 0:7766f6712673 | 44 | char req[RPC_DATA_LEN] = {0}; |
yueee_yt | 0:7766f6712673 | 45 | |
yueee_yt | 0:7766f6712673 | 46 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 47 | printf("++++(RPC Handler)Path : %s\r\n", path().c_str()); |
yueee_yt | 0:7766f6712673 | 48 | printf("++++(RPC Handler)Root Path : %s\r\n", rootPath().c_str()); |
yueee_yt | 0:7766f6712673 | 49 | #endif |
yueee_yt | 0:7766f6712673 | 50 | //Remove path |
yueee_yt | 0:7766f6712673 | 51 | strncpy(req, path().c_str(), RPC_DATA_LEN-1); |
yueee_yt | 0:7766f6712673 | 52 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 53 | printf("++++(RPC Handler)RPC req(before) : %s\r\n", req); |
yueee_yt | 0:7766f6712673 | 54 | #endif |
yueee_yt | 0:7766f6712673 | 55 | //Remove "%20", "+", "," from req |
yueee_yt | 0:7766f6712673 | 56 | cleanReq(req); |
yueee_yt | 0:7766f6712673 | 57 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 58 | printf("++++(RPC Handler)RPC req(after) : %s\r\n", req); |
yueee_yt | 0:7766f6712673 | 59 | #endif |
yueee_yt | 0:7766f6712673 | 60 | //Do RPC Call |
yueee_yt | 0:7766f6712673 | 61 | RPC::call(req, resp); //FIXME: Use bool result |
yueee_yt | 0:7766f6712673 | 62 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 63 | printf("++++(RPC Handler)Response %s \r\n",resp); |
yueee_yt | 0:7766f6712673 | 64 | #endif |
yueee_yt | 0:7766f6712673 | 65 | //Response |
yueee_yt | 0:7766f6712673 | 66 | setContentLen( strlen(resp) ); |
yueee_yt | 0:7766f6712673 | 67 | |
yueee_yt | 0:7766f6712673 | 68 | //Make sure that the browser won't cache this request |
yueee_yt | 0:7766f6712673 | 69 | // respHeaders()["Cache-control"]="no-cache;no-store"; |
yueee_yt | 0:7766f6712673 | 70 | // respHeaders()["Pragma"]="no-cache"; |
yueee_yt | 0:7766f6712673 | 71 | // respHeaders()["Expires"]="0"; |
yueee_yt | 0:7766f6712673 | 72 | addRespHeaders("Cache-control","no-cache;no-store"); |
yueee_yt | 0:7766f6712673 | 73 | addRespHeaders("Pragma","no-cache"); |
yueee_yt | 0:7766f6712673 | 74 | addRespHeaders("Expires","0"); |
yueee_yt | 0:7766f6712673 | 75 | |
yueee_yt | 0:7766f6712673 | 76 | //Write data |
yueee_yt | 0:7766f6712673 | 77 | // respHeaders()["Connection"] = "close"; |
yueee_yt | 0:7766f6712673 | 78 | addRespHeaders("Connection", "close"); |
yueee_yt | 0:7766f6712673 | 79 | writeData(resp, strlen(resp)); |
yueee_yt | 0:7766f6712673 | 80 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 81 | printf("++++(RPC Handler)Exit RPCHandler::doGet()\r\n"); |
yueee_yt | 0:7766f6712673 | 82 | #endif |
yueee_yt | 0:7766f6712673 | 83 | } |
yueee_yt | 0:7766f6712673 | 84 | |
yueee_yt | 0:7766f6712673 | 85 | void RPCHandler::doPost() |
yueee_yt | 0:7766f6712673 | 86 | { |
yueee_yt | 0:7766f6712673 | 87 | |
yueee_yt | 0:7766f6712673 | 88 | } |
yueee_yt | 0:7766f6712673 | 89 | |
yueee_yt | 0:7766f6712673 | 90 | void RPCHandler::doHead() |
yueee_yt | 0:7766f6712673 | 91 | { |
yueee_yt | 0:7766f6712673 | 92 | |
yueee_yt | 0:7766f6712673 | 93 | } |
yueee_yt | 0:7766f6712673 | 94 | |
yueee_yt | 0:7766f6712673 | 95 | |
yueee_yt | 0:7766f6712673 | 96 | void RPCHandler::onReadable() //Data has been read |
yueee_yt | 0:7766f6712673 | 97 | { |
yueee_yt | 0:7766f6712673 | 98 | |
yueee_yt | 0:7766f6712673 | 99 | } |
yueee_yt | 0:7766f6712673 | 100 | |
yueee_yt | 0:7766f6712673 | 101 | void RPCHandler::onWriteable() //Data has been written & buf is free |
yueee_yt | 0:7766f6712673 | 102 | { |
yueee_yt | 0:7766f6712673 | 103 | #ifdef _DEBUG_RPC_HANDLER |
yueee_yt | 0:7766f6712673 | 104 | printf("++++(RPC Handler)onWriteable event\r\n"); |
yueee_yt | 0:7766f6712673 | 105 | #endif |
yueee_yt | 0:7766f6712673 | 106 | // close(); //Data written, we can close the connection |
yueee_yt | 0:7766f6712673 | 107 | } |
yueee_yt | 0:7766f6712673 | 108 | |
yueee_yt | 0:7766f6712673 | 109 | void RPCHandler::onClose() //Connection is closing |
yueee_yt | 0:7766f6712673 | 110 | { |
yueee_yt | 0:7766f6712673 | 111 | //Nothing to do |
yueee_yt | 0:7766f6712673 | 112 | } |
yueee_yt | 0:7766f6712673 | 113 | |
yueee_yt | 0:7766f6712673 | 114 | void RPCHandler::cleanReq(char* data) |
yueee_yt | 0:7766f6712673 | 115 | { |
yueee_yt | 0:7766f6712673 | 116 | char* p; |
yueee_yt | 0:7766f6712673 | 117 | if((p = strstr(data, "+"))!=NULL)memset((void*) p, ' ', 1); |
yueee_yt | 0:7766f6712673 | 118 | else if((p = strstr(data, ","))!=NULL)memset((void*) p, ' ', 1); |
yueee_yt | 0:7766f6712673 | 119 | else if((p = strstr(data, "%20"))!=NULL) { |
yueee_yt | 0:7766f6712673 | 120 | memset((void*) p, ' ', 1); |
yueee_yt | 0:7766f6712673 | 121 | while(*(p+2)!=NULL) { |
yueee_yt | 0:7766f6712673 | 122 | p++; |
yueee_yt | 0:7766f6712673 | 123 | memset((void*) p,*(p+2),1); |
yueee_yt | 0:7766f6712673 | 124 | } |
yueee_yt | 0:7766f6712673 | 125 | } |
yueee_yt | 0:7766f6712673 | 126 | |
yueee_yt | 0:7766f6712673 | 127 | if((p = strstr(data, "+"))!=NULL)memset((void*) p, ' ', 1); |
yueee_yt | 0:7766f6712673 | 128 | else if((p = strstr(data, ","))!=NULL)memset((void*) p, ' ', 1); |
yueee_yt | 0:7766f6712673 | 129 | else if((p = strstr(data, "%20"))!=NULL) { |
yueee_yt | 0:7766f6712673 | 130 | memset((void*) p, ' ', 1); |
yueee_yt | 0:7766f6712673 | 131 | while(*(p+2)!=NULL) { |
yueee_yt | 0:7766f6712673 | 132 | p++; |
yueee_yt | 0:7766f6712673 | 133 | memset((void*) p,*(p+2),1); |
yueee_yt | 0:7766f6712673 | 134 | } |
yueee_yt | 0:7766f6712673 | 135 | } |
yueee_yt | 0:7766f6712673 | 136 | } |
yueee_yt | 0:7766f6712673 | 137 | |
yueee_yt | 0:7766f6712673 | 138 | |
yueee_yt | 0:7766f6712673 | 139 | |
yueee_yt | 0:7766f6712673 | 140 |