A version of LWIP, provided for backwards compatibility.
Dependents: AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more
HTTPRPC.h
00001 #ifndef HTTPRPC_H 00002 #define HTTPRPC_H 00003 00004 #include "HTTPRPC.h" 00005 00006 /** 00007 * A datastorage helper for the HTTPRPC class 00008 */ 00009 class HTTPRPCData : public HTTPData { 00010 public: char result[255]; 00011 }; 00012 00013 /** 00014 * Thsi class enables you to make rpc calls to your mbed. 00015 * Furthermore it is a good example how to write a HTTPHandler for small data chunks. 00016 */ 00017 class HTTPRPC : public HTTPHandler { 00018 public: 00019 /** 00020 * We have to know the prefix for the RPCHandler. 00021 * A good default choice is /rpc so we made this default. 00022 */ 00023 HTTPRPC(const char *path = "/rpc") : HTTPHandler(path) {} 00024 00025 private: 00026 /** 00027 * If you need some Headerfeelds you have tor register the feelds here. 00028 */ 00029 // virtual void reg(HTTPServer *svr) { 00030 // svr->registerField("Content-Length"); 00031 // } 00032 00033 /** 00034 * If a new RPCRequest Header is complete received the server will call this function. 00035 * This is the right place for preparing our datastructures. 00036 * Furthermore we will execute the rpc call and store the anwere. 00037 * But we will not send a response. This will be hapen in the send method. 00038 */ 00039 virtual HTTPStatus init(HTTPConnection *con) const { 00040 HTTPRPCData *data = new HTTPRPCData(); 00041 con->data = data; 00042 char *query = con->getURL()+strlen(_prefix); 00043 clean(query); 00044 printf("'%s'\n", query); 00045 rpc(query, data->result); 00046 00047 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"; 00048 char *old = (char *)con->getHeaderFields(); 00049 int oldlen = strlen(old); 00050 int atrlen = strlen(nfields); 00051 char *fields = new char[atrlen+oldlen+3]; 00052 strcpy(fields,old); 00053 fields[oldlen+0] = '\r'; 00054 fields[oldlen+1] = '\n'; 00055 strcpy(&fields[oldlen+2], nfields); 00056 fields[atrlen+2+oldlen] = '\0'; 00057 con->setHeaderFields(fields); 00058 if(*old) { 00059 delete old; 00060 } 00061 00062 con->setLength(strlen(data->result)); 00063 return HTTP_OK; 00064 } 00065 00066 /** 00067 * If we got an POST request the send method will not be executed. 00068 * If we want to send data we have to trigger it the first time by ourself. 00069 * So we execute the send method. 00070 * 00071 * If the rpc call is the content of the POST body we would not be able to execute it. 00072 * Were parsing only the URL. 00073 */ 00074 virtual HTTPHandle data(HTTPConnection *con, void *, int) const { 00075 return send(con, con->sndbuf()); 00076 } 00077 00078 /** 00079 * Send the result back to the client. 00080 * If we have not enought space wait for next time. 00081 */ 00082 virtual HTTPHandle send(HTTPConnection *con, int maximum) const { 00083 HTTPRPCData *data = static_cast<HTTPRPCData *>(con->data); 00084 if(maximum>64) { 00085 con->write(data->result, con->getLength()); 00086 return HTTP_SuccessEnded; 00087 } else { 00088 // To less memory. 00089 return HTTP_SenderMemory; 00090 } 00091 } 00092 00093 /** 00094 * To reduce memory usage we sodify the URL directly 00095 * and replace '%20',',','+','=' with spaces. 00096 */ 00097 inline void clean(char *str) const { 00098 while(*str++) { 00099 if(*str=='%'&&*(str+1)=='2'&&*(str+2)=='0') { 00100 *str = ' '; 00101 *(str+1) = ' '; 00102 *(str+2) = ' '; 00103 } 00104 if(*str==','||*str=='+'||*str=='=') { 00105 *str = ' '; 00106 } 00107 } 00108 } 00109 }; 00110 00111 #endif
Generated on Tue Jul 12 2022 16:06:07 by 1.7.2