1 /*
2  * Copyright (C) 2009 by Marc Boris Duerner, Tommi Maekitalo
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
27  */
28 
29 #ifndef cxxtools_Http_Socket_h
30 #define cxxtools_Http_Socket_h
31 
32 #include <cxxtools/net/tcpsocket.h>
33 #include <cxxtools/http/request.h>
34 #include <cxxtools/http/reply.h>
35 #include <cxxtools/iostream.h>
36 #include <cxxtools/timer.h>
37 #include <cxxtools/connectable.h>
38 #include <cxxtools/signal.h>
39 #include <cxxtools/method.h>
40 #include "parser.h"
41 
42 namespace cxxtools {
43 
44 namespace http {
45 
46 class ServerImpl;
47 class Responder;
48 
49 class Socket : public net::TcpSocket, public Connectable
50 {
51         class ParseEvent : public HeaderParser::MessageHeaderEvent
52         {
53                 Request& _request;
54 
55             public:
ParseEvent(Request & request)56                 explicit ParseEvent(Request& request)
57                     : HeaderParser::MessageHeaderEvent(request.header()),
58                       _request(request)
59                     { }
60 
61                 virtual void onMethod(const std::string& method);
62                 virtual void onUrl(const std::string& url);
63                 virtual void onUrlParam(const std::string& q);
64         };
65 
66     public:
67         Socket(ServerImpl& server, net::TcpServer& tcpServer);
68         explicit Socket(Socket& socket);
69         ~Socket();
70 
71         void accept();
hasAccepted()72         bool hasAccepted() const  { return _accepted; }
73 
74         void setSelector(SelectorBase* s);
75         void removeSelector();
76 
77         void onIODeviceInput(IODevice& iodevice);
78         void onInput(StreamBuffer& sb);
79         bool onOutput(StreamBuffer& sb);
80         void onTimeout();
81 
82         bool doReply();
83         void sendReply();
isReady()84         bool isReady() const
85         { return _parser.end() && _contentLength == 0; }
86 
request()87         const Request& request() const { return _request; }
reply()88         const Reply& reply() const     { return _reply; }
89 
90         Signal<Socket&> inputReady;
91         Signal<Socket&> timeout;
92 
buffer()93         StreamBuffer& buffer()         { return _stream.buffer(); }
94 
95         MethodSlot<void, Socket, StreamBuffer&> inputSlot;
96 
97         Connection inputConnection;
98         Connection timeoutConnection;
99 
100     private:
101         net::TcpServer& _tcpServer;
102         ServerImpl& _server;
103 
104         ParseEvent _parseEvent;
105         HeaderParser _parser;
106         Request _request;
107         Reply _reply;
108 
109         Timer _timer;
110         int _contentLength;
111         Responder* _responder;
112         IOStream _stream;
113 
114         bool _accepted;
115 };
116 
117 } // namespace http
118 
119 } // namespace cxxtools
120 
121 #endif
122