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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers <title>SimpleHandler.cpp Source File</title>

SimpleHandler.cpp

00001 /*
00002 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00003 
00004 Permission is hereby granted, free of charge, to any person obtaining a copy
00005 of this software and associated documentation files (the "Software"), to deal
00006 in the Software without restriction, including without limitation the rights
00007 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008 copies of the Software, and to permit persons to whom the Software is
00009 furnished to do so, subject to the following conditions:
00010 
00011 The above copyright notice and this permission notice shall be included in
00012 all copies or substantial portions of the Software.
00013 
00014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020 THE SOFTWARE.
00021 */
00022 #define _DEBUG_SIMPLE_HANDLER
00023 
00024 #include "SimpleHandler.h"
00025 #include "DMBoard.h"
00026 
00027 SimpleHandler::SimpleHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocketConnection) : HTTPRequestHandler(rootPath, path, pTCPSocketConnection)
00028 {
00029 #ifdef _DEBUG_SIMPLE_HANDLER
00030     DMBoard::instance().logger()->printf("++++(SimpleHeader)Initialize\r\n");
00031 #endif
00032 }
00033 
00034 SimpleHandler::~SimpleHandler()
00035 {
00036 #ifdef _DEBUG_SIMPLE_HANDLER
00037     DMBoard::instance().logger()->printf("++++(SimpleHeader)Handler destroyed\r\n");
00038 #endif
00039 }
00040 
00041 void SimpleHandler::doGet()
00042 {
00043 #ifdef _DEBUG_SIMPLE_HANDLER
00044     DMBoard::instance().logger()->printf("++++(SimpleHeader) doGet()\r\n");
00045 #endif
00046     const char* resp = "Hello world !";
00047     setContentLen( strlen(resp) );
00048     respHeaders()["Connection"] = "close";
00049     writeData(resp, strlen(resp));
00050 #ifdef _DEBUG_SIMPLE_HANDLER
00051     DMBoard::instance().logger()->printf("++++(SimpleHeader) doGet Exit\r\n");
00052 #endif
00053 }
00054 
00055 void SimpleHandler::doPost()
00056 {
00057 #ifdef _DEBUG_SIMPLE_HANDLER
00058     DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::doPost()\r\n");
00059 #endif
00060 
00061 }
00062 
00063 void SimpleHandler::doHead()
00064 {
00065 #ifdef _DEBUG_SIMPLE_HANDLER
00066     DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::doHead()\r\n");
00067 #endif
00068 
00069 }
00070 
00071 
00072 void SimpleHandler::onReadable() //Data has been read
00073 {
00074 #ifdef _DEBUG_SIMPLE_HANDLER
00075     DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::onReadable() event\r\n");
00076 #endif
00077 
00078 }
00079 
00080 void SimpleHandler::onWriteable() //Data has been written & buf is free
00081 {
00082 #ifdef _DEBUG_SIMPLE_HANDLER
00083     DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::onWriteable() event\r\n");
00084 #endif
00085 //    close(); //Data written, we can close the connection
00086 }
00087 
00088 void SimpleHandler::onClose() //Connection is closing
00089 {
00090 #ifdef _DEBUG_SIMPLE_HANDLER
00091     DMBoard::instance().logger()->printf("\r\n++++SimpleHandler::onClose() event\r\n");
00092 #endif
00093     //Nothing to do
00094 }