Norimasa Okamoto / WIZ820ioNetIf
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RPCHandler.cpp Source File

RPCHandler.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "RPCHandler.h"
00025 #include "rpc.h"
00026 
00027 //#define __DEBUG
00028 #include "dbg/dbg.h"
00029 
00030 #define RPC_DATA_LEN 128
00031 
00032 RPCHandler::RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
00033 {}
00034 
00035 RPCHandler::~RPCHandler()
00036 {
00037   DBG("\r\nHandler destroyed\r\n");
00038 }
00039 
00040 void RPCHandler::doGet()
00041 {
00042   DBG("\r\nIn RPCHandler::doGet()\r\n");
00043   char resp[RPC_DATA_LEN] = {0};
00044   char req[RPC_DATA_LEN] = {0};
00045   
00046   DBG("\r\nPath : %s\r\n", path().c_str());
00047   DBG("\r\nRoot Path : %s\r\n", rootPath().c_str());
00048   
00049   //Remove path
00050   strncpy(req, path().c_str(), RPC_DATA_LEN-1);
00051   DBG("\r\nRPC req : %s\r\n", req);
00052   
00053   //Remove %20, +, from req
00054   cleanReq(req);
00055   DBG("\r\nRPC req : %s\r\n", req);
00056   
00057   //Do RPC Call
00058   //mbed::rpc(req, resp); //FIXME: Use bool result
00059   RPC::call(req, resp); //FIXME: Use bool result
00060   
00061   //Response
00062   setContentLen( strlen(resp) );
00063   
00064   //Make sure that the browser won't cache this request
00065   respHeaders()["Cache-control"]="no-cache;no-store";
00066  // respHeaders()["Cache-control"]="no-store";
00067   respHeaders()["Pragma"]="no-cache";
00068   respHeaders()["Expires"]="0";
00069   
00070   //Write data
00071   respHeaders()["Connection"] = "close";
00072   writeData(resp, strlen(resp));
00073   DBG("\r\nExit RPCHandler::doGet()\r\n");
00074 }
00075 
00076 void RPCHandler::doPost()
00077 {
00078 
00079 }
00080 
00081 void RPCHandler::doHead()
00082 {
00083 
00084 }
00085 
00086   
00087 void RPCHandler::onReadable() //Data has been read
00088 {
00089 
00090 }
00091 
00092 void RPCHandler::onWriteable() //Data has been written & buf is free
00093 {
00094   DBG("\r\nRPCHandler::onWriteable() event\r\n");
00095   close(); //Data written, we can close the connection
00096 }
00097 
00098 void RPCHandler::onClose() //Connection is closing
00099 {
00100   //Nothing to do
00101 }
00102 
00103 void RPCHandler::cleanReq(char* data)
00104 {
00105   char* p;
00106   static const char* lGarbage[2] = {"%20", "+"};
00107   for(int i = 0; i < 2; i++)
00108   {
00109     while( p = strstr(data, lGarbage[i]) )
00110     {
00111       memset((void*) p, ' ', strlen(lGarbage[i]));
00112     }
00113   }
00114 }
00115   
00116