Adaptation of the HttpServer by user yueee_yt. This version has improved handling of the HTTP headers (**NOTE**: There are limitations with this implementation and it is not fully functional. Use it only as a starting point.)

Dependents:   DMSupport DMSupport DMSupport DMSupport

Fork of DM_HttpServer by EmbeddedArtists AB

Committer:
embeddedartists
Date:
Tue Dec 02 15:04:43 2014 +0000
Revision:
6:7b3320c34654
Parent:
5:b8f6a11c70db
Child:
10:c1c8276af541
Fixed stack sizes so that FSHandler works. Changed printfs to RtosLog. Changed sprintf to snprintf to prevent overwrite.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yueee_yt 0:fdf9c2c5200f 1 /*
yueee_yt 0:fdf9c2c5200f 2 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
yueee_yt 4:1b6b021ee21d 3
yueee_yt 0:fdf9c2c5200f 4 Permission is hereby granted, free of charge, to any person obtaining a copy
yueee_yt 0:fdf9c2c5200f 5 of this software and associated documentation files (the "Software"), to deal
yueee_yt 0:fdf9c2c5200f 6 in the Software without restriction, including without limitation the rights
yueee_yt 0:fdf9c2c5200f 7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
yueee_yt 0:fdf9c2c5200f 8 copies of the Software, and to permit persons to whom the Software is
yueee_yt 0:fdf9c2c5200f 9 furnished to do so, subject to the following conditions:
yueee_yt 4:1b6b021ee21d 10
yueee_yt 0:fdf9c2c5200f 11 The above copyright notice and this permission notice shall be included in
yueee_yt 0:fdf9c2c5200f 12 all copies or substantial portions of the Software.
yueee_yt 4:1b6b021ee21d 13
yueee_yt 0:fdf9c2c5200f 14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
yueee_yt 0:fdf9c2c5200f 15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
yueee_yt 0:fdf9c2c5200f 16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
yueee_yt 0:fdf9c2c5200f 17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
yueee_yt 0:fdf9c2c5200f 18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
yueee_yt 0:fdf9c2c5200f 19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
yueee_yt 0:fdf9c2c5200f 20 THE SOFTWARE.
yueee_yt 0:fdf9c2c5200f 21 */
yueee_yt 4:1b6b021ee21d 22 #define _DEBUG_SIMPLE_HANDLER
yueee_yt 0:fdf9c2c5200f 23
yueee_yt 0:fdf9c2c5200f 24 #include "SimpleHandler.h"
embeddedartists 6:7b3320c34654 25 #include "DMBoard.h"
yueee_yt 0:fdf9c2c5200f 26
yueee_yt 0:fdf9c2c5200f 27 SimpleHandler::SimpleHandler(const char* rootPath, const char* path, TCPSocketConnection* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection)
yueee_yt 0:fdf9c2c5200f 28 {
yueee_yt 4:1b6b021ee21d 29 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 30 DMBoard::instance().logger()->printf("++++(SimpleHeader)Initialize\r\n");
yueee_yt 4:1b6b021ee21d 31 #endif
yueee_yt 0:fdf9c2c5200f 32 }
yueee_yt 0:fdf9c2c5200f 33
yueee_yt 0:fdf9c2c5200f 34 SimpleHandler::~SimpleHandler()
yueee_yt 0:fdf9c2c5200f 35 {
yueee_yt 4:1b6b021ee21d 36 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 37 DMBoard::instance().logger()->printf("++++(SimpleHeader)Handler destroyed\r\n");
yueee_yt 4:1b6b021ee21d 38 #endif
yueee_yt 0:fdf9c2c5200f 39 }
yueee_yt 0:fdf9c2c5200f 40
yueee_yt 0:fdf9c2c5200f 41 void SimpleHandler::doGet()
yueee_yt 0:fdf9c2c5200f 42 {
yueee_yt 4:1b6b021ee21d 43 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 44 DMBoard::instance().logger()->printf("++++(SimpleHeader) doGet()\r\n");
yueee_yt 4:1b6b021ee21d 45 #endif
yueee_yt 4:1b6b021ee21d 46 const char* resp = "Hello world !";
yueee_yt 4:1b6b021ee21d 47 setContentLen( strlen(resp) );
yueee_yt 4:1b6b021ee21d 48 respHeaders()["Connection"] = "close";
yueee_yt 4:1b6b021ee21d 49 writeData(resp, strlen(resp));
yueee_yt 4:1b6b021ee21d 50 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 51 DMBoard::instance().logger()->printf("++++(SimpleHeader) doGet Exit\r\n");
yueee_yt 4:1b6b021ee21d 52 #endif
yueee_yt 0:fdf9c2c5200f 53 }
yueee_yt 0:fdf9c2c5200f 54
yueee_yt 0:fdf9c2c5200f 55 void SimpleHandler::doPost()
yueee_yt 0:fdf9c2c5200f 56 {
embeddedartists 6:7b3320c34654 57 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 58 DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::doPost()\r\n");
embeddedartists 6:7b3320c34654 59 #endif
yueee_yt 0:fdf9c2c5200f 60
yueee_yt 0:fdf9c2c5200f 61 }
yueee_yt 0:fdf9c2c5200f 62
yueee_yt 0:fdf9c2c5200f 63 void SimpleHandler::doHead()
yueee_yt 0:fdf9c2c5200f 64 {
embeddedartists 6:7b3320c34654 65 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 66 DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::doHead()\r\n");
embeddedartists 6:7b3320c34654 67 #endif
yueee_yt 0:fdf9c2c5200f 68
yueee_yt 0:fdf9c2c5200f 69 }
yueee_yt 0:fdf9c2c5200f 70
yueee_yt 4:1b6b021ee21d 71
yueee_yt 0:fdf9c2c5200f 72 void SimpleHandler::onReadable() //Data has been read
yueee_yt 0:fdf9c2c5200f 73 {
embeddedartists 6:7b3320c34654 74 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 75 DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::onReadable() event\r\n");
embeddedartists 6:7b3320c34654 76 #endif
yueee_yt 0:fdf9c2c5200f 77
yueee_yt 0:fdf9c2c5200f 78 }
yueee_yt 0:fdf9c2c5200f 79
yueee_yt 0:fdf9c2c5200f 80 void SimpleHandler::onWriteable() //Data has been written & buf is free
yueee_yt 0:fdf9c2c5200f 81 {
yueee_yt 4:1b6b021ee21d 82 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 83 DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::onWriteable() event\r\n");
yueee_yt 4:1b6b021ee21d 84 #endif
yueee_yt 5:b8f6a11c70db 85 // close(); //Data written, we can close the connection
yueee_yt 0:fdf9c2c5200f 86 }
yueee_yt 0:fdf9c2c5200f 87
yueee_yt 0:fdf9c2c5200f 88 void SimpleHandler::onClose() //Connection is closing
yueee_yt 0:fdf9c2c5200f 89 {
embeddedartists 6:7b3320c34654 90 #ifdef _DEBUG_SIMPLE_HANDLER
embeddedartists 6:7b3320c34654 91 DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::onClose() event\r\n");
embeddedartists 6:7b3320c34654 92 #endif
yueee_yt 4:1b6b021ee21d 93 //Nothing to do
yueee_yt 0:fdf9c2c5200f 94 }