Joe Danlloh / lwip

Dependencies:   mbed

Fork of lwip by mbed unsupported

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
root@mbed.org 0:5e1631496985 1 #ifndef HTTPRPC_H
root@mbed.org 0:5e1631496985 2 #define HTTPRPC_H
root@mbed.org 0:5e1631496985 3
root@mbed.org 0:5e1631496985 4 #include "HTTPServer.h"
root@mbed.org 0:5e1631496985 5 #include "platform.h"
root@mbed.org 0:5e1631496985 6 #ifdef MBED_RPC
root@mbed.org 0:5e1631496985 7 #include "rpc.h"
root@mbed.org 0:5e1631496985 8
root@mbed.org 0:5e1631496985 9 /**
root@mbed.org 0:5e1631496985 10 * A datastorage helper for the HTTPRPC class
root@mbed.org 0:5e1631496985 11 */
root@mbed.org 0:5e1631496985 12 class HTTPRPCData : public HTTPData {
root@mbed.org 0:5e1631496985 13 public: char result[255];
root@mbed.org 0:5e1631496985 14 };
root@mbed.org 0:5e1631496985 15
root@mbed.org 0:5e1631496985 16 /**
root@mbed.org 0:5e1631496985 17 * Thsi class enables you to make rpc calls to your mbed.
root@mbed.org 0:5e1631496985 18 * Furthermore it is a good example how to write a HTTPHandler for small data chunks.
root@mbed.org 0:5e1631496985 19 */
root@mbed.org 0:5e1631496985 20 class HTTPRPC : public HTTPHandler {
root@mbed.org 0:5e1631496985 21 public:
root@mbed.org 0:5e1631496985 22 /**
root@mbed.org 0:5e1631496985 23 * We have to know the prefix for the RPCHandler.
root@mbed.org 0:5e1631496985 24 * A good default choice is /rpc so we made this default.
root@mbed.org 0:5e1631496985 25 */
root@mbed.org 0:5e1631496985 26 HTTPRPC(const char *path = "/rpc") : HTTPHandler(path) {}
root@mbed.org 0:5e1631496985 27 HTTPRPC(HTTPServer *server, const char *path = "/rpc") : HTTPHandler(path) { server->addHandler(this); }
root@mbed.org 0:5e1631496985 28
root@mbed.org 0:5e1631496985 29 private:
root@mbed.org 0:5e1631496985 30 /**
root@mbed.org 0:5e1631496985 31 * If you need some Headerfeelds you have tor register the feelds here.
root@mbed.org 0:5e1631496985 32 */
root@mbed.org 0:5e1631496985 33 // virtual void reg(HTTPServer *svr) {
root@mbed.org 0:5e1631496985 34 // svr->registerField("Content-Length");
root@mbed.org 0:5e1631496985 35 // }
root@mbed.org 0:5e1631496985 36
root@mbed.org 0:5e1631496985 37 /**
root@mbed.org 0:5e1631496985 38 * If a new RPCRequest Header is complete received the server will call this function.
root@mbed.org 0:5e1631496985 39 * This is the right place for preparing our datastructures.
root@mbed.org 0:5e1631496985 40 * Furthermore we will execute the rpc call and store the anwere.
root@mbed.org 0:5e1631496985 41 * But we will not send a response. This will be hapen in the send method.
root@mbed.org 0:5e1631496985 42 */
root@mbed.org 0:5e1631496985 43 virtual HTTPStatus init(HTTPConnection *con) const {
root@mbed.org 0:5e1631496985 44 HTTPRPCData *data = new HTTPRPCData();
root@mbed.org 0:5e1631496985 45 con->data = data;
root@mbed.org 0:5e1631496985 46 char *query = con->getURL()+strlen(_prefix);
root@mbed.org 0:5e1631496985 47 clean(query);
root@mbed.org 0:5e1631496985 48 rpc(query, data->result);
root@mbed.org 0:5e1631496985 49
root@mbed.org 0:5e1631496985 50 const char *nfields = "Cache-Control: no-cache, no-store, must-revalidate\r\nPragma: no-cache\r\nExpires: Thu, 01 Dec 1994 16:00:00 GM";
root@mbed.org 0:5e1631496985 51 char *old = (char *)con->getHeaderFields();
root@mbed.org 0:5e1631496985 52 int oldlen = strlen(old);
root@mbed.org 0:5e1631496985 53 int atrlen = strlen(nfields);
root@mbed.org 0:5e1631496985 54 char *fields = new char[atrlen+oldlen+3];
root@mbed.org 0:5e1631496985 55 strcpy(fields,old);
root@mbed.org 0:5e1631496985 56 fields[oldlen+0] = '\r';
root@mbed.org 0:5e1631496985 57 fields[oldlen+1] = '\n';
root@mbed.org 0:5e1631496985 58 strcpy(&fields[oldlen+2], nfields);
root@mbed.org 0:5e1631496985 59 fields[atrlen+2+oldlen] = '\0';
root@mbed.org 0:5e1631496985 60 con->setHeaderFields(fields);
root@mbed.org 0:5e1631496985 61 if(*old) {
root@mbed.org 0:5e1631496985 62 delete old;
root@mbed.org 0:5e1631496985 63 }
root@mbed.org 0:5e1631496985 64
root@mbed.org 0:5e1631496985 65 con->setLength(strlen(data->result));
root@mbed.org 0:5e1631496985 66 return HTTP_OK;
root@mbed.org 0:5e1631496985 67 }
root@mbed.org 0:5e1631496985 68
root@mbed.org 0:5e1631496985 69 /**
root@mbed.org 0:5e1631496985 70 * If we got an POST request the send method will not be executed.
root@mbed.org 0:5e1631496985 71 * If we want to send data we have to trigger it the first time by ourself.
root@mbed.org 0:5e1631496985 72 * So we execute the send method.
root@mbed.org 0:5e1631496985 73 *
root@mbed.org 0:5e1631496985 74 * If the rpc call is the content of the POST body we would not be able to execute it.
root@mbed.org 0:5e1631496985 75 * Were parsing only the URL.
root@mbed.org 0:5e1631496985 76 */
root@mbed.org 0:5e1631496985 77 virtual HTTPHandle data(HTTPConnection *con, void *, int) const {
root@mbed.org 0:5e1631496985 78 return send(con, con->sndbuf());
root@mbed.org 0:5e1631496985 79 }
root@mbed.org 0:5e1631496985 80
root@mbed.org 0:5e1631496985 81 /**
root@mbed.org 0:5e1631496985 82 * Send the result back to the client.
root@mbed.org 0:5e1631496985 83 * If we have not enought space wait for next time.
root@mbed.org 0:5e1631496985 84 */
root@mbed.org 0:5e1631496985 85 virtual HTTPHandle send(HTTPConnection *con, int maximum) const {
root@mbed.org 0:5e1631496985 86 HTTPRPCData *data = static_cast<HTTPRPCData *>(con->data);
root@mbed.org 0:5e1631496985 87 if(maximum>64) {
root@mbed.org 0:5e1631496985 88 con->write(data->result, con->getLength());
root@mbed.org 0:5e1631496985 89 return HTTP_SuccessEnded;
root@mbed.org 0:5e1631496985 90 } else {
root@mbed.org 0:5e1631496985 91 // To less memory.
root@mbed.org 0:5e1631496985 92 return HTTP_SenderMemory;
root@mbed.org 0:5e1631496985 93 }
root@mbed.org 0:5e1631496985 94 }
root@mbed.org 0:5e1631496985 95
root@mbed.org 0:5e1631496985 96 /**
root@mbed.org 0:5e1631496985 97 * To reduce memory usage we sodify the URL directly
root@mbed.org 0:5e1631496985 98 * and replace '%20',',','+','=' with spaces.
root@mbed.org 0:5e1631496985 99 */
root@mbed.org 0:5e1631496985 100 inline void clean(char *str) const {
root@mbed.org 0:5e1631496985 101 while(*str++) {
root@mbed.org 0:5e1631496985 102 if(*str=='%'&&*(str+1)=='2'&&*(str+2)=='0') {
root@mbed.org 0:5e1631496985 103 *str = ' ';
root@mbed.org 0:5e1631496985 104 *(str+1) = ' ';
root@mbed.org 0:5e1631496985 105 *(str+2) = ' ';
root@mbed.org 0:5e1631496985 106 }
root@mbed.org 0:5e1631496985 107 if(*str==','||*str=='+'||*str=='=') {
root@mbed.org 0:5e1631496985 108 *str = ' ';
root@mbed.org 0:5e1631496985 109 }
root@mbed.org 0:5e1631496985 110 }
root@mbed.org 0:5e1631496985 111 }
root@mbed.org 0:5e1631496985 112 };
root@mbed.org 0:5e1631496985 113 #endif
root@mbed.org 0:5e1631496985 114
root@mbed.org 0:5e1631496985 115 #endif