Experimental HTTPClient with proxy support
services/http/server/HTTPServer.cpp@0:b56b6a05cad4, 2011-06-29 (annotated)
- Committer:
- igorsk
- Date:
- Wed Jun 29 16:01:58 2011 +0000
- Revision:
- 0:b56b6a05cad4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
igorsk | 0:b56b6a05cad4 | 1 | |
igorsk | 0:b56b6a05cad4 | 2 | /* |
igorsk | 0:b56b6a05cad4 | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
igorsk | 0:b56b6a05cad4 | 4 | |
igorsk | 0:b56b6a05cad4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
igorsk | 0:b56b6a05cad4 | 6 | of this software and associated documentation files (the "Software"), to deal |
igorsk | 0:b56b6a05cad4 | 7 | in the Software without restriction, including without limitation the rights |
igorsk | 0:b56b6a05cad4 | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
igorsk | 0:b56b6a05cad4 | 9 | copies of the Software, and to permit persons to whom the Software is |
igorsk | 0:b56b6a05cad4 | 10 | furnished to do so, subject to the following conditions: |
igorsk | 0:b56b6a05cad4 | 11 | |
igorsk | 0:b56b6a05cad4 | 12 | The above copyright notice and this permission notice shall be included in |
igorsk | 0:b56b6a05cad4 | 13 | all copies or substantial portions of the Software. |
igorsk | 0:b56b6a05cad4 | 14 | |
igorsk | 0:b56b6a05cad4 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
igorsk | 0:b56b6a05cad4 | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
igorsk | 0:b56b6a05cad4 | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
igorsk | 0:b56b6a05cad4 | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
igorsk | 0:b56b6a05cad4 | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
igorsk | 0:b56b6a05cad4 | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
igorsk | 0:b56b6a05cad4 | 21 | THE SOFTWARE. |
igorsk | 0:b56b6a05cad4 | 22 | */ |
igorsk | 0:b56b6a05cad4 | 23 | |
igorsk | 0:b56b6a05cad4 | 24 | #include "HTTPServer.h" |
igorsk | 0:b56b6a05cad4 | 25 | |
igorsk | 0:b56b6a05cad4 | 26 | //#define __DEBUG |
igorsk | 0:b56b6a05cad4 | 27 | #include "dbg/dbg.h" |
igorsk | 0:b56b6a05cad4 | 28 | |
igorsk | 0:b56b6a05cad4 | 29 | HTTPServer::HTTPServer() |
igorsk | 0:b56b6a05cad4 | 30 | { |
igorsk | 0:b56b6a05cad4 | 31 | m_pTCPSocket = new TCPSocket; |
igorsk | 0:b56b6a05cad4 | 32 | m_pTCPSocket->setOnEvent(this, &HTTPServer::onTCPSocketEvent); |
igorsk | 0:b56b6a05cad4 | 33 | } |
igorsk | 0:b56b6a05cad4 | 34 | |
igorsk | 0:b56b6a05cad4 | 35 | HTTPServer::~HTTPServer() |
igorsk | 0:b56b6a05cad4 | 36 | { |
igorsk | 0:b56b6a05cad4 | 37 | delete m_pTCPSocket; |
igorsk | 0:b56b6a05cad4 | 38 | } |
igorsk | 0:b56b6a05cad4 | 39 | |
igorsk | 0:b56b6a05cad4 | 40 | void HTTPServer::bind(int port /*= 80*/) |
igorsk | 0:b56b6a05cad4 | 41 | { |
igorsk | 0:b56b6a05cad4 | 42 | Host h(IpAddr(127,0,0,1), port, "localhost"); |
igorsk | 0:b56b6a05cad4 | 43 | m_pTCPSocket->bind(h); |
igorsk | 0:b56b6a05cad4 | 44 | m_pTCPSocket->listen(); //Listen |
igorsk | 0:b56b6a05cad4 | 45 | } |
igorsk | 0:b56b6a05cad4 | 46 | |
igorsk | 0:b56b6a05cad4 | 47 | #if 0 //Just for clarity |
igorsk | 0:b56b6a05cad4 | 48 | template<typename T> |
igorsk | 0:b56b6a05cad4 | 49 | void HTTPServer::addHandler(const char* path) |
igorsk | 0:b56b6a05cad4 | 50 | { |
igorsk | 0:b56b6a05cad4 | 51 | m_lpHandlers[path] = &T::inst; |
igorsk | 0:b56b6a05cad4 | 52 | |
igorsk | 0:b56b6a05cad4 | 53 | } |
igorsk | 0:b56b6a05cad4 | 54 | #endif |
igorsk | 0:b56b6a05cad4 | 55 | |
igorsk | 0:b56b6a05cad4 | 56 | void HTTPServer::onTCPSocketEvent(TCPSocketEvent e) |
igorsk | 0:b56b6a05cad4 | 57 | { |
igorsk | 0:b56b6a05cad4 | 58 | |
igorsk | 0:b56b6a05cad4 | 59 | DBG("\r\nHTTPServer::onTCPSocketEvent : Event %d\r\n", e); |
igorsk | 0:b56b6a05cad4 | 60 | |
igorsk | 0:b56b6a05cad4 | 61 | if(e==TCPSOCKET_ACCEPT) |
igorsk | 0:b56b6a05cad4 | 62 | { |
igorsk | 0:b56b6a05cad4 | 63 | TCPSocket* pTCPSocket; |
igorsk | 0:b56b6a05cad4 | 64 | Host client; |
igorsk | 0:b56b6a05cad4 | 65 | |
igorsk | 0:b56b6a05cad4 | 66 | if( !!m_pTCPSocket->accept(&client, &pTCPSocket) ) |
igorsk | 0:b56b6a05cad4 | 67 | { |
igorsk | 0:b56b6a05cad4 | 68 | DBG("\r\nHTTPServer::onTCPSocketEvent : Could not accept connection.\r\n"); |
igorsk | 0:b56b6a05cad4 | 69 | return; //Error in accept, discard connection |
igorsk | 0:b56b6a05cad4 | 70 | } |
igorsk | 0:b56b6a05cad4 | 71 | |
igorsk | 0:b56b6a05cad4 | 72 | HTTPRequestDispatcher* pDispatcher = new HTTPRequestDispatcher(this, pTCPSocket); //TCPSocket ownership is passed to dispatcher |
igorsk | 0:b56b6a05cad4 | 73 | //The dispatcher object will destroy itself when done, or will be destroyed on Server destruction |
igorsk | 0:b56b6a05cad4 | 74 | |
igorsk | 0:b56b6a05cad4 | 75 | } |
igorsk | 0:b56b6a05cad4 | 76 | |
igorsk | 0:b56b6a05cad4 | 77 | } |