a
Fork of HTTPServer by
Revision 0:52230c56755e, committed 2010-06-11
- Comitter:
- donatien
- Date:
- Fri Jun 11 16:26:10 2010 +0000
- Child:
- 1:56636f5bf71a
- Commit message:
Changed in this revision
Binary file LPC1768/HTTPServer.ar has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/dbg/dbg.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,73 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +//#ifdef DBG_H +//#define DBG_H + +#ifdef __LWIP_DEBUG +#define __DEBUG +#endif + +#ifdef __DEBUG + +#ifndef __DEBUGSTREAM +#define __DEBUGSTREAM + + +class DebugStream +{ +public: +static void debug(const char* format, ...); +static void release(); +private: + +}; + +#undef DBG +#undef DBG_END +#define DBG DebugStream::debug +#define DBG_END DebugStream::release +#endif + +#else +#undef DBG +#undef DBG_END +#define DBG(...) +#define DBG_END() +#endif + +#ifdef __LWIP_DEBUG +#ifndef __SNPRINTF +#define __SNPRINTF +#include "mbed.h" + +//int snprintf(char *str, int size, const char *format, ...); +#endif +#endif + +#ifdef __LWIP_DEBUG +#undef __DEBUG +#endif + +//#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/server/HTTPRequestDispatcher.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,72 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef HTTP_REQUEST_DISPATCHER_H +#define HTTP_REQUEST_DISPATCHER_H + +class HTTPServer; + +#include "if/net/net.h" +#include "api/TCPSocket.h" +#include "HTTPServer.h" + +#include "mbed.h" + +#define HTTP_REQUEST_TIMEOUT 5000 + +#include <string> +using std::string; + +class HTTPRequestDispatcher : public NetService +{ +public: + HTTPRequestDispatcher(HTTPServer* pSvr, TCPSocket* pTCPSocket); + virtual ~HTTPRequestDispatcher(); + +private: + + enum HTTP_METH + { + HTTP_GET, + HTTP_POST, + HTTP_HEAD + }; + + void dispatchRequest(); + + virtual void close(); //Close TCPSocket and destroy data + + void onTCPSocketEvent(TCPSocketEvent e); + + void onTimeout(); //Connection has timed out + + bool getRequest(string* rootPath, string* fullPath, string* meth); + + HTTPServer* m_pSvr; + TCPSocket* m_pTCPSocket; + + Timeout m_watchdog; + bool m_closed; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/server/HTTPRequestHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,95 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef HTTP_REQUEST_HANDLER_H +#define HTTP_REQUEST_HANDLER_H + +#include "if/net/net.h" +#include "api/TCPSocket.h" +//#include "HTTPServer.h" + +#include "mbed.h" + +#include <string> +using std::string; + +#include <map> +using std::map; + +class HTTPRequestHandler : public NetService +{ +public: + HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~HTTPRequestHandler(); + +//protected: + virtual void doGet() = 0; + virtual void doPost() = 0; + virtual void doHead() = 0; + + virtual void onReadable() = 0; //Data has been read + virtual void onWriteable() = 0; //Data has been written & buf is free + virtual void onTimeout(); //Connection has timed out + virtual void onClose() = 0; //Connection is closing + + virtual void close(); //Close socket and destroy data + +protected: + map<string, string>& reqHeaders() /*const*/; + string& path() /*const*/; + int dataLen() const; + int readData(char* buf, int len); + string& rootPath() /*const*/; + + void setErrCode(int errc); + void setContentLen(int len); + + map<string, string>& respHeaders(); + int writeData(const char* buf, int len); + + void setTimeout(int ms); + void resetTimeout(); + +private: + void readHeaders(); //Called at instanciation + void writeHeaders(); //Called at the first writeData call + void onTCPSocketEvent(TCPSocketEvent e); + + TCPSocket* m_pTCPSocket; + map<string, string> m_reqHeaders; + map<string, string> m_respHeaders; + string m_rootPath; + string m_path; + int m_errc; //Response code + + Timeout m_watchdog; + int m_timeout; + + bool m_closed; + bool m_headersSent; + + int readLine(char* str, int maxLen); + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/server/HTTPServer.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,67 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef HTTP_SERVER_H +#define HTTP_SERVER_H + +class HTTPRequestHandler; +class HTTPRequestDispatcher; + +#include "if/net/net.h" +#include "HTTPRequestHandler.h" +#include "HTTPRequestDispatcher.h" + +#include <string> +using std::string; + +#include <map> +using std::map; + +class HTTPServer +{ +public: + HTTPServer(); + ~HTTPServer(); + + template<typename T> + void addHandler(const char* path) //Template decl in header + { m_lpHandlers[path] = &T::inst; } + + void bind(int port = 80); + +private: + friend class HTTPRequestDispatcher; + + void onTCPSocketEvent(TCPSocketEvent e); + + TCPSocket* m_pTCPSocket; + map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) > m_lpHandlers; + +}; + +//Including handlers here for more convenience +#include "impl/RpcHandler.h" +#include "impl/FSHandler.h" +#include "impl/SimpleHandler.h" + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/server/impl/FSHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,52 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef FS_HANDLER_H +#define FS_HANDLER_H + +#include "../HTTPRequestHandler.h" +#include "mbed.h" + +class FSHandler : public HTTPRequestHandler +{ +public: + FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~FSHandler(); + +//protected: + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new FSHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one + + virtual void doGet(); + virtual void doPost(); + virtual void doHead(); + + virtual void onReadable(); //Data has been read + virtual void onWriteable(); //Data has been written & buf is free + virtual void onClose(); //Connection is closing + +private: + FILE* m_fp; + bool m_err404; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/server/impl/RPCHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,50 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef RPC_HANDLER_H +#define RPC_HANDLER_H + +#include "../HTTPRequestHandler.h" + +class RPCHandler : public HTTPRequestHandler +{ +public: + RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~RPCHandler(); + +//protected: + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new RPCHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one + + virtual void doGet(); + virtual void doPost(); + virtual void doHead(); + + virtual void onReadable(); //Data has been read + virtual void onWriteable(); //Data has been written & buf is free + virtual void onClose(); //Connection is closing + +protected: + void cleanReq(char* data); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/server/impl/SimpleHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,47 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef SIMPLE_HANDLER_H +#define SIMPLE_HANDLER_H + +#include "../HTTPRequestHandler.h" + +class SimpleHandler : public HTTPRequestHandler +{ +public: + SimpleHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~SimpleHandler(); + +//protected: + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new SimpleHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one + + virtual void doGet(); + virtual void doPost(); + virtual void doHead(); + + virtual void onReadable(); //Data has been read + virtual void onWriteable(); //Data has been written & buf is free + virtual void onClose(); //Connection is closing +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/util/base64.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,57 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef BASE64_H +#define BASE64_H + +#include <string> +using std::string; + +#ifdef __cplusplus +extern "C" { +#endif + +//Originaly from Rolf's iputil.h + +unsigned int base64enc_len(const char *str); + +void base64enc(const char *input, unsigned int length, char *output); + +#ifdef __cplusplus +} +#endif + +class Base64 +{ +public: + static string encode(const string& str) + { + char* out = new char[ base64enc_len(str.c_str()) ]; + base64enc(str.c_str(), str.length(), out); + string res(out); + delete[] out; + return res; + } +}; + +#endif /* LWIP_UTILS_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC1768/services/http/util/url.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,88 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef URL_H +#define URL_H + +#include "if/net/ipaddr.h" + +#include <string> +using std::string; + +#include "mbed.h" + +#ifdef __cplusplus +extern "C" { +#endif + +char *url_encode(char *str); +char *url_decode(char *str); + +#ifdef __cplusplus +} +#endif + +class Url +{ +public: + static string encode(const string& url) + { + char* c_res = url_encode( (char*) url.c_str() ); + string res(c_res); + free(c_res); //Alloc'ed in url_encode() + return res; + } + + static string decode(const string& url) + { + char* c_res = url_decode( (char*) url.c_str() ); + string res(c_res); + free(c_res); //Alloc'ed in url_decode() + return res; + } + + Url(); + + string getProtocol(); + string getHost(); + bool getHostIp(IpAddr* ip); //If host is in IP form, return true & proper object by ptr + uint16_t getPort(); + string getPath(); + + void setProtocol(string protocol); + void setHost(string host); + void setPort(uint16_t port); + void setPath(string path); + + void fromString(string str); + string toString(); + +private: + string m_protocol; + string m_host; + uint16_t m_port; + string m_path; + +}; + +#endif /* LWIP_UTILS_H */
Binary file LPC2368/HTTPServer.ar has changed
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/dbg/dbg.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,73 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +//#ifdef DBG_H +//#define DBG_H + +#ifdef __LWIP_DEBUG +#define __DEBUG +#endif + +#ifdef __DEBUG + +#ifndef __DEBUGSTREAM +#define __DEBUGSTREAM + + +class DebugStream +{ +public: +static void debug(const char* format, ...); +static void release(); +private: + +}; + +#undef DBG +#undef DBG_END +#define DBG DebugStream::debug +#define DBG_END DebugStream::release +#endif + +#else +#undef DBG +#undef DBG_END +#define DBG(...) +#define DBG_END() +#endif + +#ifdef __LWIP_DEBUG +#ifndef __SNPRINTF +#define __SNPRINTF +#include "mbed.h" + +//int snprintf(char *str, int size, const char *format, ...); +#endif +#endif + +#ifdef __LWIP_DEBUG +#undef __DEBUG +#endif + +//#endif +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/server/HTTPRequestDispatcher.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,72 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef HTTP_REQUEST_DISPATCHER_H +#define HTTP_REQUEST_DISPATCHER_H + +class HTTPServer; + +#include "if/net/net.h" +#include "api/TCPSocket.h" +#include "HTTPServer.h" + +#include "mbed.h" + +#define HTTP_REQUEST_TIMEOUT 5000 + +#include <string> +using std::string; + +class HTTPRequestDispatcher : public NetService +{ +public: + HTTPRequestDispatcher(HTTPServer* pSvr, TCPSocket* pTCPSocket); + virtual ~HTTPRequestDispatcher(); + +private: + + enum HTTP_METH + { + HTTP_GET, + HTTP_POST, + HTTP_HEAD + }; + + void dispatchRequest(); + + virtual void close(); //Close TCPSocket and destroy data + + void onTCPSocketEvent(TCPSocketEvent e); + + void onTimeout(); //Connection has timed out + + bool getRequest(string* rootPath, string* fullPath, string* meth); + + HTTPServer* m_pSvr; + TCPSocket* m_pTCPSocket; + + Timeout m_watchdog; + bool m_closed; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/server/HTTPRequestHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,95 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef HTTP_REQUEST_HANDLER_H +#define HTTP_REQUEST_HANDLER_H + +#include "if/net/net.h" +#include "api/TCPSocket.h" +//#include "HTTPServer.h" + +#include "mbed.h" + +#include <string> +using std::string; + +#include <map> +using std::map; + +class HTTPRequestHandler : public NetService +{ +public: + HTTPRequestHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~HTTPRequestHandler(); + +//protected: + virtual void doGet() = 0; + virtual void doPost() = 0; + virtual void doHead() = 0; + + virtual void onReadable() = 0; //Data has been read + virtual void onWriteable() = 0; //Data has been written & buf is free + virtual void onTimeout(); //Connection has timed out + virtual void onClose() = 0; //Connection is closing + + virtual void close(); //Close socket and destroy data + +protected: + map<string, string>& reqHeaders() /*const*/; + string& path() /*const*/; + int dataLen() const; + int readData(char* buf, int len); + string& rootPath() /*const*/; + + void setErrCode(int errc); + void setContentLen(int len); + + map<string, string>& respHeaders(); + int writeData(const char* buf, int len); + + void setTimeout(int ms); + void resetTimeout(); + +private: + void readHeaders(); //Called at instanciation + void writeHeaders(); //Called at the first writeData call + void onTCPSocketEvent(TCPSocketEvent e); + + TCPSocket* m_pTCPSocket; + map<string, string> m_reqHeaders; + map<string, string> m_respHeaders; + string m_rootPath; + string m_path; + int m_errc; //Response code + + Timeout m_watchdog; + int m_timeout; + + bool m_closed; + bool m_headersSent; + + int readLine(char* str, int maxLen); + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/server/HTTPServer.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,67 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef HTTP_SERVER_H +#define HTTP_SERVER_H + +class HTTPRequestHandler; +class HTTPRequestDispatcher; + +#include "if/net/net.h" +#include "HTTPRequestHandler.h" +#include "HTTPRequestDispatcher.h" + +#include <string> +using std::string; + +#include <map> +using std::map; + +class HTTPServer +{ +public: + HTTPServer(); + ~HTTPServer(); + + template<typename T> + void addHandler(const char* path) //Template decl in header + { m_lpHandlers[path] = &T::inst; } + + void bind(int port = 80); + +private: + friend class HTTPRequestDispatcher; + + void onTCPSocketEvent(TCPSocketEvent e); + + TCPSocket* m_pTCPSocket; + map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*) > m_lpHandlers; + +}; + +//Including handlers here for more convenience +#include "impl/RpcHandler.h" +#include "impl/FSHandler.h" +#include "impl/SimpleHandler.h" + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/server/impl/FSHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,52 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef FS_HANDLER_H +#define FS_HANDLER_H + +#include "../HTTPRequestHandler.h" +#include "mbed.h" + +class FSHandler : public HTTPRequestHandler +{ +public: + FSHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~FSHandler(); + +//protected: + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new FSHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one + + virtual void doGet(); + virtual void doPost(); + virtual void doHead(); + + virtual void onReadable(); //Data has been read + virtual void onWriteable(); //Data has been written & buf is free + virtual void onClose(); //Connection is closing + +private: + FILE* m_fp; + bool m_err404; +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/server/impl/RPCHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,50 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef RPC_HANDLER_H +#define RPC_HANDLER_H + +#include "../HTTPRequestHandler.h" + +class RPCHandler : public HTTPRequestHandler +{ +public: + RPCHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~RPCHandler(); + +//protected: + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new RPCHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one + + virtual void doGet(); + virtual void doPost(); + virtual void doHead(); + + virtual void onReadable(); //Data has been read + virtual void onWriteable(); //Data has been written & buf is free + virtual void onClose(); //Connection is closing + +protected: + void cleanReq(char* data); +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/server/impl/SimpleHandler.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,47 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef SIMPLE_HANDLER_H +#define SIMPLE_HANDLER_H + +#include "../HTTPRequestHandler.h" + +class SimpleHandler : public HTTPRequestHandler +{ +public: + SimpleHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket); + virtual ~SimpleHandler(); + +//protected: + static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new SimpleHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one + + virtual void doGet(); + virtual void doPost(); + virtual void doHead(); + + virtual void onReadable(); //Data has been read + virtual void onWriteable(); //Data has been written & buf is free + virtual void onClose(); //Connection is closing +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/util/base64.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,57 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef BASE64_H +#define BASE64_H + +#include <string> +using std::string; + +#ifdef __cplusplus +extern "C" { +#endif + +//Originaly from Rolf's iputil.h + +unsigned int base64enc_len(const char *str); + +void base64enc(const char *input, unsigned int length, char *output); + +#ifdef __cplusplus +} +#endif + +class Base64 +{ +public: + static string encode(const string& str) + { + char* out = new char[ base64enc_len(str.c_str()) ]; + base64enc(str.c_str(), str.length(), out); + string res(out); + delete[] out; + return res; + } +}; + +#endif /* LWIP_UTILS_H */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LPC2368/services/http/util/url.h Fri Jun 11 16:26:10 2010 +0000 @@ -0,0 +1,88 @@ + +/* +Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#ifndef URL_H +#define URL_H + +#include "if/net/ipaddr.h" + +#include <string> +using std::string; + +#include "mbed.h" + +#ifdef __cplusplus +extern "C" { +#endif + +char *url_encode(char *str); +char *url_decode(char *str); + +#ifdef __cplusplus +} +#endif + +class Url +{ +public: + static string encode(const string& url) + { + char* c_res = url_encode( (char*) url.c_str() ); + string res(c_res); + free(c_res); //Alloc'ed in url_encode() + return res; + } + + static string decode(const string& url) + { + char* c_res = url_decode( (char*) url.c_str() ); + string res(c_res); + free(c_res); //Alloc'ed in url_decode() + return res; + } + + Url(); + + string getProtocol(); + string getHost(); + bool getHostIp(IpAddr* ip); //If host is in IP form, return true & proper object by ptr + uint16_t getPort(); + string getPath(); + + void setProtocol(string protocol); + void setHost(string host); + void setPort(uint16_t port); + void setPath(string path); + + void fromString(string str); + string toString(); + +private: + string m_protocol; + string m_host; + uint16_t m_port; + string m_path; + +}; + +#endif /* LWIP_UTILS_H */