Richard Blimmel
/
CIG_HTTPClient
HTTPClient using static IP
NetServices/services/http/server/impl/RPCHandler.cpp@0:d8f2f7d5f31b, 2011-05-30 (annotated)
- Committer:
- mr_q
- Date:
- Mon May 30 11:53:37 2011 +0000
- Revision:
- 0:d8f2f7d5f31b
v0.01 Draft
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mr_q | 0:d8f2f7d5f31b | 1 | #pragma diag_remark 1293 |
mr_q | 0:d8f2f7d5f31b | 2 | /* |
mr_q | 0:d8f2f7d5f31b | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
mr_q | 0:d8f2f7d5f31b | 4 | |
mr_q | 0:d8f2f7d5f31b | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
mr_q | 0:d8f2f7d5f31b | 6 | of this software and associated documentation files (the "Software"), to deal |
mr_q | 0:d8f2f7d5f31b | 7 | in the Software without restriction, including without limitation the rights |
mr_q | 0:d8f2f7d5f31b | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mr_q | 0:d8f2f7d5f31b | 9 | copies of the Software, and to permit persons to whom the Software is |
mr_q | 0:d8f2f7d5f31b | 10 | furnished to do so, subject to the following conditions: |
mr_q | 0:d8f2f7d5f31b | 11 | |
mr_q | 0:d8f2f7d5f31b | 12 | The above copyright notice and this permission notice shall be included in |
mr_q | 0:d8f2f7d5f31b | 13 | all copies or substantial portions of the Software. |
mr_q | 0:d8f2f7d5f31b | 14 | |
mr_q | 0:d8f2f7d5f31b | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mr_q | 0:d8f2f7d5f31b | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mr_q | 0:d8f2f7d5f31b | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mr_q | 0:d8f2f7d5f31b | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mr_q | 0:d8f2f7d5f31b | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mr_q | 0:d8f2f7d5f31b | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
mr_q | 0:d8f2f7d5f31b | 21 | THE SOFTWARE. |
mr_q | 0:d8f2f7d5f31b | 22 | */ |
mr_q | 0:d8f2f7d5f31b | 23 | |
mr_q | 0:d8f2f7d5f31b | 24 | #include "RPCHandler.h" |
mr_q | 0:d8f2f7d5f31b | 25 | #include "rpc.h" |
mr_q | 0:d8f2f7d5f31b | 26 | #include "url.h" |
mr_q | 0:d8f2f7d5f31b | 27 | |
mr_q | 0:d8f2f7d5f31b | 28 | //#define __DEBUG |
mr_q | 0:d8f2f7d5f31b | 29 | #include "dbg/dbg.h" |
mr_q | 0:d8f2f7d5f31b | 30 | |
mr_q | 0:d8f2f7d5f31b | 31 | #define RPC_DATA_LEN 128 |
mr_q | 0:d8f2f7d5f31b | 32 | |
mr_q | 0:d8f2f7d5f31b | 33 | RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket) |
mr_q | 0:d8f2f7d5f31b | 34 | {} |
mr_q | 0:d8f2f7d5f31b | 35 | |
mr_q | 0:d8f2f7d5f31b | 36 | RPCHandler::~RPCHandler() |
mr_q | 0:d8f2f7d5f31b | 37 | { |
mr_q | 0:d8f2f7d5f31b | 38 | DBG("\r\nHandler destroyed\r\n"); |
mr_q | 0:d8f2f7d5f31b | 39 | } |
mr_q | 0:d8f2f7d5f31b | 40 | |
mr_q | 0:d8f2f7d5f31b | 41 | void RPCHandler::doGet() |
mr_q | 0:d8f2f7d5f31b | 42 | { |
mr_q | 0:d8f2f7d5f31b | 43 | DBG("\r\nIn RPCHandler::doGet()\r\n"); |
mr_q | 0:d8f2f7d5f31b | 44 | char resp[RPC_DATA_LEN] = {0}; |
mr_q | 0:d8f2f7d5f31b | 45 | char req[RPC_DATA_LEN] = {0}; |
mr_q | 0:d8f2f7d5f31b | 46 | |
mr_q | 0:d8f2f7d5f31b | 47 | DBG("\r\nPath : %s\r\n", path().c_str()); |
mr_q | 0:d8f2f7d5f31b | 48 | DBG("\r\nRoot Path : %s\r\n", rootPath().c_str()); |
mr_q | 0:d8f2f7d5f31b | 49 | |
mr_q | 0:d8f2f7d5f31b | 50 | //Remove path |
mr_q | 0:d8f2f7d5f31b | 51 | strncpy(req, path().c_str(), RPC_DATA_LEN-1); |
mr_q | 0:d8f2f7d5f31b | 52 | DBG("\r\nRPC req : %s\r\n", req); |
mr_q | 0:d8f2f7d5f31b | 53 | |
mr_q | 0:d8f2f7d5f31b | 54 | //Remove %20, +, from req |
mr_q | 0:d8f2f7d5f31b | 55 | cleanReq(req); |
mr_q | 0:d8f2f7d5f31b | 56 | DBG("\r\nRPC req : %s\r\n", req); |
mr_q | 0:d8f2f7d5f31b | 57 | |
mr_q | 0:d8f2f7d5f31b | 58 | //Do RPC Call |
mr_q | 0:d8f2f7d5f31b | 59 | mbed::rpc(req, resp); //FIXME: Use bool result |
mr_q | 0:d8f2f7d5f31b | 60 | |
mr_q | 0:d8f2f7d5f31b | 61 | //Response |
mr_q | 0:d8f2f7d5f31b | 62 | setContentLen( strlen(resp) ); |
mr_q | 0:d8f2f7d5f31b | 63 | |
mr_q | 0:d8f2f7d5f31b | 64 | //Make sure that the browser won't cache this request |
mr_q | 0:d8f2f7d5f31b | 65 | respHeaders()["Cache-control"]="no-cache;no-store"; |
mr_q | 0:d8f2f7d5f31b | 66 | // respHeaders()["Cache-control"]="no-store"; |
mr_q | 0:d8f2f7d5f31b | 67 | respHeaders()["Pragma"]="no-cache"; |
mr_q | 0:d8f2f7d5f31b | 68 | respHeaders()["Expires"]="0"; |
mr_q | 0:d8f2f7d5f31b | 69 | |
mr_q | 0:d8f2f7d5f31b | 70 | //Write data |
mr_q | 0:d8f2f7d5f31b | 71 | respHeaders()["Connection"] = "close"; |
mr_q | 0:d8f2f7d5f31b | 72 | writeData(resp, strlen(resp)); |
mr_q | 0:d8f2f7d5f31b | 73 | DBG("\r\nExit RPCHandler::doGet()\r\n"); |
mr_q | 0:d8f2f7d5f31b | 74 | } |
mr_q | 0:d8f2f7d5f31b | 75 | |
mr_q | 0:d8f2f7d5f31b | 76 | void RPCHandler::doPost() |
mr_q | 0:d8f2f7d5f31b | 77 | { |
mr_q | 0:d8f2f7d5f31b | 78 | |
mr_q | 0:d8f2f7d5f31b | 79 | } |
mr_q | 0:d8f2f7d5f31b | 80 | |
mr_q | 0:d8f2f7d5f31b | 81 | void RPCHandler::doHead() |
mr_q | 0:d8f2f7d5f31b | 82 | { |
mr_q | 0:d8f2f7d5f31b | 83 | |
mr_q | 0:d8f2f7d5f31b | 84 | } |
mr_q | 0:d8f2f7d5f31b | 85 | |
mr_q | 0:d8f2f7d5f31b | 86 | |
mr_q | 0:d8f2f7d5f31b | 87 | void RPCHandler::onReadable() //Data has been read |
mr_q | 0:d8f2f7d5f31b | 88 | { |
mr_q | 0:d8f2f7d5f31b | 89 | |
mr_q | 0:d8f2f7d5f31b | 90 | } |
mr_q | 0:d8f2f7d5f31b | 91 | |
mr_q | 0:d8f2f7d5f31b | 92 | void RPCHandler::onWriteable() //Data has been written & buf is free |
mr_q | 0:d8f2f7d5f31b | 93 | { |
mr_q | 0:d8f2f7d5f31b | 94 | DBG("\r\nRPCHandler::onWriteable() event\r\n"); |
mr_q | 0:d8f2f7d5f31b | 95 | close(); //Data written, we can close the connection |
mr_q | 0:d8f2f7d5f31b | 96 | } |
mr_q | 0:d8f2f7d5f31b | 97 | |
mr_q | 0:d8f2f7d5f31b | 98 | void RPCHandler::onClose() //Connection is closing |
mr_q | 0:d8f2f7d5f31b | 99 | { |
mr_q | 0:d8f2f7d5f31b | 100 | //Nothing to do |
mr_q | 0:d8f2f7d5f31b | 101 | } |
mr_q | 0:d8f2f7d5f31b | 102 | |
mr_q | 0:d8f2f7d5f31b | 103 | void RPCHandler::cleanReq(char* data) |
mr_q | 0:d8f2f7d5f31b | 104 | { |
mr_q | 0:d8f2f7d5f31b | 105 | char* decoded = url_decode(data); |
mr_q | 0:d8f2f7d5f31b | 106 | strcpy(data, decoded); |
mr_q | 0:d8f2f7d5f31b | 107 | free(decoded); |
mr_q | 0:d8f2f7d5f31b | 108 | /* |
mr_q | 0:d8f2f7d5f31b | 109 | char* p; |
mr_q | 0:d8f2f7d5f31b | 110 | static const char* lGarbage[2] = {"%20", "+"}; |
mr_q | 0:d8f2f7d5f31b | 111 | for(int i = 0; i < 2; i++) |
mr_q | 0:d8f2f7d5f31b | 112 | { |
mr_q | 0:d8f2f7d5f31b | 113 | while( p = strstr(data, lGarbage[i]) ) |
mr_q | 0:d8f2f7d5f31b | 114 | { |
mr_q | 0:d8f2f7d5f31b | 115 | memset((void*) p, ' ', strlen(lGarbage[i])); |
mr_q | 0:d8f2f7d5f31b | 116 | } |
mr_q | 0:d8f2f7d5f31b | 117 | } |
mr_q | 0:d8f2f7d5f31b | 118 | */ |
mr_q | 0:d8f2f7d5f31b | 119 | } |
mr_q | 0:d8f2f7d5f31b | 120 | |
mr_q | 0:d8f2f7d5f31b | 121 |