HTTPServer

Committer:
jphuc96
Date:
Sat Sep 16 02:39:55 2017 +0000
Revision:
0:caf5feddac47
v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jphuc96 0:caf5feddac47 1 /* FsHandler.cpp */
jphuc96 0:caf5feddac47 2 #include "mbed.h"
jphuc96 0:caf5feddac47 3 #include "RpcHandler.h"
jphuc96 0:caf5feddac47 4 #include "mbed_rpc.h"
jphuc96 0:caf5feddac47 5
jphuc96 0:caf5feddac47 6
jphuc96 0:caf5feddac47 7 #define DEBUG 1
jphuc96 0:caf5feddac47 8 #include "hl_debug.h"
jphuc96 0:caf5feddac47 9
jphuc96 0:caf5feddac47 10 RPC rpc("rpc");
jphuc96 0:caf5feddac47 11
jphuc96 0:caf5feddac47 12
jphuc96 0:caf5feddac47 13 HTTPRpcRequestHandler::HTTPRpcRequestHandler(const char* rootPath, const char* localPath, HTTPConnection::HTTPMessage& Msg, TCPSocketConnection& Tcp)
jphuc96 0:caf5feddac47 14 : HTTPRequestHandler(Msg, Tcp)
jphuc96 0:caf5feddac47 15 {
jphuc96 0:caf5feddac47 16 m_rootPath = rootPath;
jphuc96 0:caf5feddac47 17 m_localPath = localPath;
jphuc96 0:caf5feddac47 18
jphuc96 0:caf5feddac47 19 handleRequest();
jphuc96 0:caf5feddac47 20 }
jphuc96 0:caf5feddac47 21
jphuc96 0:caf5feddac47 22
jphuc96 0:caf5feddac47 23 HTTPRpcRequestHandler::~HTTPRpcRequestHandler()
jphuc96 0:caf5feddac47 24 {
jphuc96 0:caf5feddac47 25 }
jphuc96 0:caf5feddac47 26
jphuc96 0:caf5feddac47 27
jphuc96 0:caf5feddac47 28 int HTTPRpcRequestHandler::handleGetRequest()
jphuc96 0:caf5feddac47 29 {
jphuc96 0:caf5feddac47 30 char outBuf[256] = {};
jphuc96 0:caf5feddac47 31 bool retval = false;
jphuc96 0:caf5feddac47 32 int err = 404;
jphuc96 0:caf5feddac47 33 string rpc_args("");
jphuc96 0:caf5feddac47 34
jphuc96 0:caf5feddac47 35 INFO("Handling RPC Get Requesst.");
jphuc96 0:caf5feddac47 36 // This version of the RPC handler does only accept native RPC commands in the format
jphuc96 0:caf5feddac47 37 // /<class>/<method> <argument1> [<argument2> [<argument3> ...]]
jphuc96 0:caf5feddac47 38 // So we can simply pass our local pathg to our rpc
jphuc96 0:caf5feddac47 39
jphuc96 0:caf5feddac47 40 // Append missing slash if needed
jphuc96 0:caf5feddac47 41 if (m_localPath.c_str()[0] != '/') {
jphuc96 0:caf5feddac47 42 rpc_args = "/";
jphuc96 0:caf5feddac47 43 }
jphuc96 0:caf5feddac47 44 // replace the HTTP strings with ascii strings
jphuc96 0:caf5feddac47 45 for (int i = 0 ; i < m_localPath.length() ; i++) {
jphuc96 0:caf5feddac47 46 if (m_localPath.substr(i,3) == "%20") {
jphuc96 0:caf5feddac47 47 rpc_args += " ";
jphuc96 0:caf5feddac47 48 i+=2;
jphuc96 0:caf5feddac47 49 }
jphuc96 0:caf5feddac47 50 else {
jphuc96 0:caf5feddac47 51 rpc_args += m_localPath.substr(i,1);
jphuc96 0:caf5feddac47 52 }
jphuc96 0:caf5feddac47 53 }
jphuc96 0:caf5feddac47 54 INFO("RPC to %s", rpc_args.c_str());
jphuc96 0:caf5feddac47 55 retval = rpc.call(rpc_args.c_str(),outBuf);
jphuc96 0:caf5feddac47 56 INFO("RPC Request returned %d with args : %s", retval==true ? 200 : 0, outBuf);
jphuc96 0:caf5feddac47 57 if (retval) {
jphuc96 0:caf5feddac47 58 // No error
jphuc96 0:caf5feddac47 59 startResponse(200, strlen(outBuf));
jphuc96 0:caf5feddac47 60 processResponse(strlen(outBuf), outBuf);
jphuc96 0:caf5feddac47 61 endResponse();
jphuc96 0:caf5feddac47 62 err = 0;
jphuc96 0:caf5feddac47 63 }
jphuc96 0:caf5feddac47 64
jphuc96 0:caf5feddac47 65 return err;
jphuc96 0:caf5feddac47 66 }
jphuc96 0:caf5feddac47 67
jphuc96 0:caf5feddac47 68 int HTTPRpcRequestHandler::handlePutRequest()
jphuc96 0:caf5feddac47 69 {
jphuc96 0:caf5feddac47 70 return 404;
jphuc96 0:caf5feddac47 71 }
jphuc96 0:caf5feddac47 72
jphuc96 0:caf5feddac47 73 int HTTPRpcRequestHandler::handlePostRequest()
jphuc96 0:caf5feddac47 74 {
jphuc96 0:caf5feddac47 75 return 404;
jphuc96 0:caf5feddac47 76 }