Single instance HTTP Server using WiFly Interface.

Dependents:   WiFlyHTTPServerSample MultiThreadingHTTPServer

This is my implementation for a HTTP Server using the WiFly Interface. Please note that this is still under development.

It may still contain several bugs. I have tested it using a 1768 on an application board plus RN-XV board.

Currently there is only a FileSystem implemented. Also it is limited to GET request.

I try to extend it further so it will be more useful.

Btw, it does NOT work with RTOS, which seems not to be the Problem of my library.

Do not Forget to Import the WiFly Interface into your Project when using this library.

Change History:

REV5: - added support for basic RPC GET request functionality.

REV4: - added argument parsing from the request uri. - documentation extended and updated.

Committer:
leihen
Date:
Tue May 28 01:56:14 2013 +0000
Revision:
3:d6224049b3bf
Parent:
1:6b7472d5e9ee
Child:
4:d065642c32cc
First beta version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leihen 1:6b7472d5e9ee 1 /* HTTPRequestHandler.h */
leihen 3:d6224049b3bf 2 /*
leihen 3:d6224049b3bf 3 Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
leihen 3:d6224049b3bf 4
leihen 3:d6224049b3bf 5 Permission is hereby granted, free of charge, to any person obtaining a copy
leihen 3:d6224049b3bf 6 of this software and associated documentation files (the "Software"), to deal
leihen 3:d6224049b3bf 7 in the Software without restriction, including without limitation the rights
leihen 3:d6224049b3bf 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
leihen 3:d6224049b3bf 9 copies of the Software, and to permit persons to whom the Software is
leihen 3:d6224049b3bf 10 furnished to do so, subject to the following conditions:
leihen 3:d6224049b3bf 11
leihen 3:d6224049b3bf 12 The above copyright notice and this permission notice shall be included in
leihen 3:d6224049b3bf 13 all copies or substantial portions of the Software.
leihen 3:d6224049b3bf 14
leihen 3:d6224049b3bf 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
leihen 3:d6224049b3bf 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
leihen 3:d6224049b3bf 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
leihen 3:d6224049b3bf 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
leihen 3:d6224049b3bf 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
leihen 3:d6224049b3bf 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
leihen 3:d6224049b3bf 21 THE SOFTWARE.
leihen 3:d6224049b3bf 22 */
leihen 1:6b7472d5e9ee 23 #ifndef __HTTPREQUESTHANDLER_H__
leihen 1:6b7472d5e9ee 24 #define __HTTPREQUESTHANDLER_H__
leihen 1:6b7472d5e9ee 25
leihen 1:6b7472d5e9ee 26 #include "mbed.h"
leihen 1:6b7472d5e9ee 27 #include "HTTPConnection.h"
leihen 1:6b7472d5e9ee 28
leihen 3:d6224049b3bf 29 #include <map>
leihen 3:d6224049b3bf 30
leihen 3:d6224049b3bf 31 typedef std::map<const char*,const char*> HTTPHeaders;
leihen 3:d6224049b3bf 32
leihen 3:d6224049b3bf 33 /** class HTTPRequestHandler is the base class for HTTP Handler request implementations.
leihen 3:d6224049b3bf 34 *
leihen 3:d6224049b3bf 35 */
leihen 1:6b7472d5e9ee 36 class HTTPRequestHandler
leihen 1:6b7472d5e9ee 37 {
leihen 3:d6224049b3bf 38 protected:
leihen 3:d6224049b3bf 39 HTTPConnection::HTTPMessage& msg;
leihen 3:d6224049b3bf 40 TCPSocketConnection& tcp;
leihen 3:d6224049b3bf 41
leihen 1:6b7472d5e9ee 42 public:
leihen 3:d6224049b3bf 43 /** Constructor for HTTPRequestHandler objects.
leihen 1:6b7472d5e9ee 44 */
leihen 3:d6224049b3bf 45 HTTPRequestHandler(HTTPConnection::HTTPMessage&, TCPSocketConnection&);
leihen 3:d6224049b3bf 46
leihen 3:d6224049b3bf 47 /** Destructor for HTTPRequestHandler objects.
leihen 3:d6224049b3bf 48 */
leihen 3:d6224049b3bf 49 virtual ~HTTPRequestHandler();
leihen 3:d6224049b3bf 50
leihen 3:d6224049b3bf 51 /** Handler function which will be used by the HTTPServer to serve requests.
leihen 3:d6224049b3bf 52 * @param msg : Request Message information.
leihen 3:d6224049b3bf 53 * @param tcp : The socket which represents the active connection to the client.
leihen 3:d6224049b3bf 54 */
leihen 3:d6224049b3bf 55 virtual void handleRequest();
leihen 1:6b7472d5e9ee 56
leihen 3:d6224049b3bf 57 /** Handler function which will handle errors and return errors correctly
leihen 3:d6224049b3bf 58 * @param errorCode : The error code returned by the HTTP Server to represent the error condition.
leihen 3:d6224049b3bf 59 * @param msg : Request Message information.
leihen 3:d6224049b3bf 60 * @param tcp : The socket which represents the active connection to the client.
leihen 3:d6224049b3bf 61 */
leihen 3:d6224049b3bf 62 virtual void handleError(int errorCode, HTTPHeaders* header = NULL);
leihen 3:d6224049b3bf 63
leihen 3:d6224049b3bf 64 /** Function sends the response header which consists of the return code and the headers section
leihen 3:d6224049b3bf 65 * the response header also contains the length (in bytes) of the body. You need to send the body
leihen 3:d6224049b3bf 66 * right after calling this function. Please note that you do not need to consider the terminating
leihen 3:d6224049b3bf 67 * CR+LF after your last CR+LF. This will be sent automatically once \c endResponse is called. Also
leihen 3:d6224049b3bf 68 * the Length given in \c nLen does not need to consider this additional chars. It will also be
leihen 3:d6224049b3bf 69 * automatically added in \c startResponse. if you need to change the headers, please do NOT
leihen 3:d6224049b3bf 70 * specify the \c Content-Length Header. This is done automatically be the function.
leihen 3:d6224049b3bf 71 */
leihen 3:d6224049b3bf 72 void startResponse(int returnCode, int nLen, HTTPHeaders* header = NULL);
leihen 3:d6224049b3bf 73 void processResponse(int nLen, char* body );
leihen 3:d6224049b3bf 74 void endResponse();
leihen 3:d6224049b3bf 75
leihen 3:d6224049b3bf 76 protected:
leihen 3:d6224049b3bf 77 /** Handler function to serve GET requests
leihen 3:d6224049b3bf 78 */
leihen 3:d6224049b3bf 79 virtual int handleGetRequest() = 0;
leihen 1:6b7472d5e9ee 80
leihen 3:d6224049b3bf 81 /** Handler function to serve PUT requests
leihen 3:d6224049b3bf 82 */
leihen 3:d6224049b3bf 83 // virtual int handlePutRequest() = 0;
leihen 1:6b7472d5e9ee 84
leihen 3:d6224049b3bf 85 /** Handler function to serve POST requests
leihen 3:d6224049b3bf 86 */
leihen 3:d6224049b3bf 87 // virtual int handlePostRequest() = 0;
leihen 3:d6224049b3bf 88
leihen 1:6b7472d5e9ee 89 };
leihen 1:6b7472d5e9ee 90
leihen 1:6b7472d5e9ee 91 #endif // __HTTPREQUESTHANDLER_H__