Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: RPC_HTTP RPC_HTTP_WIZnetInterface RPC_HTTP rpc_over_http_TL_interrupter_gatePJ
Fork of HTTPServer by
Revision 1:6b7472d5e9ee, committed 2013-05-26
- Comitter:
- leihen
- Date:
- Sun May 26 22:49:42 2013 +0000
- Parent:
- 0:7a2421e63e74
- Child:
- 2:8653bbcf7e58
- Commit message:
- Basic functionality demonstrated. One issue exists with error pages, which does not work 100%
;
Changed in this revision
--- a/HTTPConnection.cpp Sun May 26 20:13:28 2013 +0000
+++ b/HTTPConnection.cpp Sun May 26 22:49:42 2013 +0000
@@ -8,7 +8,7 @@
using std::string;
-#if (1 && !defined(TARGET_LPC11U24))
+#if (0 && !defined(TARGET_LPC11U24))
#define INFO(x, ...) std::printf("[HttpConnection : INFO]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[HttpConnection : WARN]"x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) std::printf("[HttpConnection : ERR]"x"\r\n", ##__VA_ARGS__);
@@ -86,10 +86,10 @@
}
}
if (rcvd == 0) {
- sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
- m_Tcp.set_blocking(true);
- m_Tcp.send_all(echoHeader,strlen(echoHeader));
- m_Tcp.send_all(buffer,strlen(buffer));
+// sprintf(echoHeader,"HTTP/1.1 200 OK\n\rContent-Length: %d\n\rContent-Type: text\n\rConnection: Close\n\r\n\r",strlen(buffer));
+// m_Tcp.set_blocking(true);
+// m_Tcp.send_all(echoHeader,strlen(echoHeader));
+// m_Tcp.send_all(buffer,strlen(buffer));
/// INSERT PRCESSING OF REQUESST HERE
/// END OF PROCESSING REQUEST
--- a/HTTPConnection.h Sun May 26 20:13:28 2013 +0000
+++ b/HTTPConnection.h Sun May 26 22:49:42 2013 +0000
@@ -8,6 +8,8 @@
#include <string>
#include <map>
+class HTTPServer;
+
enum HTTPRequestType
{
HTTP_RT_GET,
@@ -55,10 +57,11 @@
int poll();
protected:
-
+ friend class HTTPServer;
+
TCPSocketConnection m_Tcp;
+ HTTPMessage m_Msg;
- HTTPMessage m_Msg;
int parse(const char *buffer);
int parseHeader(const char *buffer);
int receiveHeaders(const char* buffer, int nBuffSize);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPRequestHandler.cpp Sun May 26 22:49:42 2013 +0000
@@ -0,0 +1,12 @@
+/* HTTPRequestHandler.cpp */
+#include "mbed.h"
+#include "HTTPRequestHandler.h"
+
+
+HTTPRequestHandler::HTTPRequestHandler()
+{
+}
+
+HTTPRequestHandler::~HTTPRequestHandler()
+{
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPRequestHandler.h Sun May 26 22:49:42 2013 +0000
@@ -0,0 +1,26 @@
+/* HTTPRequestHandler.h */
+#ifndef __HTTPREQUESTHANDLER_H__
+#define __HTTPREQUESTHANDLER_H__
+
+#include "mbed.h"
+#include "HTTPConnection.h"
+
+class HTTPRequestHandler
+{
+ public:
+ /**
+ public constructor
+ */
+ HTTPRequestHandler();
+
+ /**
+ destructor
+ */
+ ~HTTPRequestHandler();
+
+
+ void HandleRequest(HTTPMessage& );
+
+};
+
+#endif // __HTTPREQUESTHANDLER_H__
\ No newline at end of file
--- a/HTTPServer.cpp Sun May 26 20:13:28 2013 +0000
+++ b/HTTPServer.cpp Sun May 26 22:49:42 2013 +0000
@@ -6,7 +6,7 @@
DigitalOut led3(LED3);
DigitalOut led4(LED4);
-#if (1 && !defined(TARGET_LPC11U24))
+#if (0 && !defined(TARGET_LPC11U24))
#define INFO(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
#define WARN(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : WARN]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
#define ERR(x, ...) if (m_pDbg) m_pDbg->printf("[HttpServer : ERR]"x"\r\n", ##__VA_ARGS__); else printf("[HttpServer : DBG]"x"\r\n", ##__VA_ARGS__);
@@ -16,6 +16,7 @@
#define ERR(x, ...)
#endif
+static const char* szMsgs = "No such file or folder.";
HTTPServer::HTTPServer(Serial* pDbg)
{
@@ -107,12 +108,47 @@
HTTPConnection con;
int c = con.poll();
if (c == 0) {
+ // Handle the request
+ HandleRequest(con.m_Msg, Clnt);
}
led2 = 0;
led3 = 0;
}
-
INFO("Leaving polling thread");
return 0;
+}
+
+void HTTPServer::HandleRequest(HTTPMessage& msg, TCPSocketConnection& tcp)
+{
+ static char echoHeader[256] = {};
+ static const char* szPage = {
+ "<HTML>\r\n"
+ "<HEAD>\r\n"
+ "<META content=\"text/html\" http-equiv=Content-Type>\r\n"
+ "</HEAD>\r\n"
+ "<BODY>\r\n"
+ "<h1>ERROR 404</h1>\r\n"
+ "<P>File not found<P>\r\n"
+ "</BODY>\r\n"
+ "</HTML>\r\n\r\n"};
+
+ map<string, HTTPRequestHandlerFunction>::iterator it;
+
+ it = m_pHandlers.find(msg.uri);
+
+ if (it == m_pHandlers.end()) {
+ // There is no such handler, so return invalid
+
+ tcp.set_blocking(true, 1500);
+ sprintf(echoHeader,"HTTP/1.1 404 Fail\r\nContent-Length: %d\r\nContent-Type: text/html\r\nServer: mbed embedded\r\nConnection: Close\r\n\r\n",strlen(szPage));
+ tcp.send(echoHeader,strlen(echoHeader));
+ tcp.send((char*)szPage,strlen(szMsgs));
+ INFO("Webrequest left unhandled.");
+ }
+ else {
+ // Handler was found so pass action to handler
+ INFO("Routing webrequest !");
+ (it->second)(msg, tcp);
+ }
}
\ No newline at end of file
--- a/HTTPServer.h Sun May 26 20:13:28 2013 +0000
+++ b/HTTPServer.h Sun May 26 22:49:42 2013 +0000
@@ -3,10 +3,18 @@
#define __HTTPSERVER_H__
#include "mbed.h"
#include "HTTPConnection.h"
+#include "HTTPRequestHandler.h"
+
+#include <map>
+using std::map;
+#include <string>
+using std::string;
#include <TCPSocketConnection.h>
#include <TCPSocketServer.h>
+typedef void (*HTTPRequestHandlerFunction)(HTTPMessage&, TCPSocketConnection&);
+
/** Class HTTPServer for WiFly Interface Library
*
*/
@@ -16,7 +24,8 @@
public:
HTTPServer(Serial* pDbg = NULL);
~HTTPServer();
-/*
+
+
struct handlersComp //Used to order handlers in the right way
{
bool operator() (const string& handler1, const string& handler2) const
@@ -30,7 +39,7 @@
return ((&handler1)>(&handler2));
}
};
- */
+
///Adds a handler
/**
Appends a handler to the handlers list
@@ -39,7 +48,10 @@
*/
// template<typename T>
// void addHandler(const char* path) //Template decl in header
-// { m_lpHandlers[path] = &T::inst; }
+// { m_pHandlers[path] = &T::inst; }
+
+ void addHandler(const char* path, HTTPRequestHandlerFunction hdlFunc)
+ { m_pHandlers[path] = hdlFunc; }
///Starts listening
/**
@@ -62,7 +74,9 @@
bool m_bServerListening;
Serial* m_pDbg;
-
+ void HandleRequest(HTTPMessage& con, TCPSocketConnection&);
+
+ map<string, HTTPRequestHandlerFunction> m_pHandlers;
};
#endif //__HTTPSERVER_H__
\ No newline at end of file
