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