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