Fork of HttpServer_snapshot_mbed-os library
Fork of HttpServer_snapshot_mbed-os by
Handler/RPCHandler.cpp@0:fdf9c2c5200f, 2014-02-20 (annotated)
- Committer:
- yueee_yt
- Date:
- Thu Feb 20 05:36:19 2014 +0000
- Revision:
- 0:fdf9c2c5200f
- Child:
- 2:82892309f848
Initialize version
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 | 0:fdf9c2c5200f | 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 | 0:fdf9c2c5200f | 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 | 0:fdf9c2c5200f | 20 | |
yueee_yt | 0:fdf9c2c5200f | 21 | #include "RPCHandler.h" |
yueee_yt | 0:fdf9c2c5200f | 22 | #include "rpc.h" |
yueee_yt | 0:fdf9c2c5200f | 23 | |
yueee_yt | 0:fdf9c2c5200f | 24 | //#define __DEBUG |
yueee_yt | 0:fdf9c2c5200f | 25 | //#include "dbg/dbg.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 | 0:fdf9c2c5200f | 34 | printf("\r\nHandler destroyed\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 35 | } |
yueee_yt | 0:fdf9c2c5200f | 36 | |
yueee_yt | 0:fdf9c2c5200f | 37 | void RPCHandler::doGet() |
yueee_yt | 0:fdf9c2c5200f | 38 | { |
yueee_yt | 0:fdf9c2c5200f | 39 | printf("\r\nIn RPCHandler::doGet()\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 40 | char resp[RPC_DATA_LEN] = {0}; |
yueee_yt | 0:fdf9c2c5200f | 41 | char req[RPC_DATA_LEN] = {0}; |
yueee_yt | 0:fdf9c2c5200f | 42 | |
yueee_yt | 0:fdf9c2c5200f | 43 | printf("\r\nPath : %s\r\n", path().c_str()); |
yueee_yt | 0:fdf9c2c5200f | 44 | printf("\r\nRoot Path : %s\r\n", rootPath().c_str()); |
yueee_yt | 0:fdf9c2c5200f | 45 | |
yueee_yt | 0:fdf9c2c5200f | 46 | //Remove path |
yueee_yt | 0:fdf9c2c5200f | 47 | strncpy(req, path().c_str(), RPC_DATA_LEN-1); |
yueee_yt | 0:fdf9c2c5200f | 48 | printf("\r\nRPC req : %s\r\n", req); |
yueee_yt | 0:fdf9c2c5200f | 49 | |
yueee_yt | 0:fdf9c2c5200f | 50 | //Remove %20, +, from req |
yueee_yt | 0:fdf9c2c5200f | 51 | cleanReq(req); |
yueee_yt | 0:fdf9c2c5200f | 52 | printf("\r\nRPC req : %s\r\n", req); |
yueee_yt | 0:fdf9c2c5200f | 53 | |
yueee_yt | 0:fdf9c2c5200f | 54 | //Do RPC Call |
yueee_yt | 0:fdf9c2c5200f | 55 | //* mbed::rpc(req, resp); //FIXME: Use bool result |
yueee_yt | 0:fdf9c2c5200f | 56 | |
yueee_yt | 0:fdf9c2c5200f | 57 | //Response |
yueee_yt | 0:fdf9c2c5200f | 58 | setContentLen( strlen(resp) ); |
yueee_yt | 0:fdf9c2c5200f | 59 | |
yueee_yt | 0:fdf9c2c5200f | 60 | //Make sure that the browser won't cache this request |
yueee_yt | 0:fdf9c2c5200f | 61 | respHeaders()["Cache-control"]="no-cache;no-store"; |
yueee_yt | 0:fdf9c2c5200f | 62 | // respHeaders()["Cache-control"]="no-store"; |
yueee_yt | 0:fdf9c2c5200f | 63 | respHeaders()["Pragma"]="no-cache"; |
yueee_yt | 0:fdf9c2c5200f | 64 | respHeaders()["Expires"]="0"; |
yueee_yt | 0:fdf9c2c5200f | 65 | |
yueee_yt | 0:fdf9c2c5200f | 66 | //Write data |
yueee_yt | 0:fdf9c2c5200f | 67 | respHeaders()["Connection"] = "close"; |
yueee_yt | 0:fdf9c2c5200f | 68 | writeData(resp, strlen(resp)); |
yueee_yt | 0:fdf9c2c5200f | 69 | printf("\r\nExit RPCHandler::doGet()\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 70 | } |
yueee_yt | 0:fdf9c2c5200f | 71 | |
yueee_yt | 0:fdf9c2c5200f | 72 | void RPCHandler::doPost() |
yueee_yt | 0:fdf9c2c5200f | 73 | { |
yueee_yt | 0:fdf9c2c5200f | 74 | |
yueee_yt | 0:fdf9c2c5200f | 75 | } |
yueee_yt | 0:fdf9c2c5200f | 76 | |
yueee_yt | 0:fdf9c2c5200f | 77 | void RPCHandler::doHead() |
yueee_yt | 0:fdf9c2c5200f | 78 | { |
yueee_yt | 0:fdf9c2c5200f | 79 | |
yueee_yt | 0:fdf9c2c5200f | 80 | } |
yueee_yt | 0:fdf9c2c5200f | 81 | |
yueee_yt | 0:fdf9c2c5200f | 82 | |
yueee_yt | 0:fdf9c2c5200f | 83 | void RPCHandler::onReadable() //Data has been read |
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::onWriteable() //Data has been written & buf is free |
yueee_yt | 0:fdf9c2c5200f | 89 | { |
yueee_yt | 0:fdf9c2c5200f | 90 | printf("\r\nRPCHandler::onWriteable() event\r\n"); |
yueee_yt | 0:fdf9c2c5200f | 91 | close(); //Data written, we can close the connection |
yueee_yt | 0:fdf9c2c5200f | 92 | } |
yueee_yt | 0:fdf9c2c5200f | 93 | |
yueee_yt | 0:fdf9c2c5200f | 94 | void RPCHandler::onClose() //Connection is closing |
yueee_yt | 0:fdf9c2c5200f | 95 | { |
yueee_yt | 0:fdf9c2c5200f | 96 | //Nothing to do |
yueee_yt | 0:fdf9c2c5200f | 97 | } |
yueee_yt | 0:fdf9c2c5200f | 98 | |
yueee_yt | 0:fdf9c2c5200f | 99 | void RPCHandler::cleanReq(char* data) |
yueee_yt | 0:fdf9c2c5200f | 100 | { |
yueee_yt | 0:fdf9c2c5200f | 101 | char* p; |
yueee_yt | 0:fdf9c2c5200f | 102 | static const char* lGarbage[2] = {"%20", "+"}; |
yueee_yt | 0:fdf9c2c5200f | 103 | for(int i = 0; i < 2; i++) |
yueee_yt | 0:fdf9c2c5200f | 104 | { |
yueee_yt | 0:fdf9c2c5200f | 105 | while( p = strstr(data, lGarbage[i]) ) |
yueee_yt | 0:fdf9c2c5200f | 106 | { |
yueee_yt | 0:fdf9c2c5200f | 107 | memset((void*) p, ' ', strlen(lGarbage[i])); |
yueee_yt | 0:fdf9c2c5200f | 108 | } |
yueee_yt | 0:fdf9c2c5200f | 109 | } |
yueee_yt | 0:fdf9c2c5200f | 110 | } |
yueee_yt | 0:fdf9c2c5200f | 111 |