Reply.h
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 Pt_Http_Reply_h
30 #define Pt_Http_Reply_h
31 
32 #include <Pt/Http/Api.h>
33 #include <Pt/Http/Message.h>
34 #include <Pt/Signal.h>
35 #include <string>
36 
37 namespace Pt {
38 
39 namespace Http {
40 
43 class PT_HTTP_API Reply : public Message
44 {
45  friend class Connection;
46 
47  public:
51  {
52  Continue = 100,
53  OK = 200,
54  MultipleChoices = 300,
55  BadRequest = 400,
56  Unauthorized = 401,
57  RequestEntityTooLarge = 413,
58  InternalServerError = 500
59  };
60 
61  public:
64  explicit Reply(Http::Connection& conn)
65  : Message(conn)
66  , _statusCode(200)
67  , _statusText("OK")
68  { }
69 
72  void setStatus(unsigned code, const std::string& txt)
73  {
74  _statusCode = code;
75  _statusText = txt;
76  }
77 
80  void setStatus(unsigned code, const char* txt)
81  {
82  _statusCode = code;
83  _statusText = txt;
84  }
85 
87  unsigned statusCode() const
88  { return _statusCode; }
89 
91  const std::string& statusText() const
92  { return _statusText; }
93 
95  void receive();
96 
98  void beginReceive();
99 
101  MessageProgress endReceive();
102 
104  void beginSend(bool finish = true);
105 
107  MessageProgress endSend();
108 
110  Signal<Reply&>& inputReceived()
111  { return _inputReceived; }
112 
114  Signal<Reply&>& outputSent()
115  { return _outputSent; }
116 
119  void clear();
120 
121  protected:
123  void onInput()
124  { _inputReceived.send(*this); }
125 
127  void onOutput()
128  { _outputSent.send(*this); }
129 
130  private:
131  unsigned _statusCode;
132  std::string _statusText;
133  Signal<Reply&> _inputReceived;
134  Signal<Reply&> _outputSent;
135 };
136 
137 } // namespace Http
138 
139 } // namespace Pt
140 
141 #endif // Pt_Http_Reply_h
HTTP message progress.
Definition: Message.h:277
Reply(Http::Connection &conn)
Construct with connection.
Definition: Reply.h:64
void setStatus(unsigned code, const std::string &txt)
Sets the HTTP status.
Definition: Reply.h:72
HTTP message with header and body.
Definition: Message.h:381
Represents a connection between a Signal/Delegate and a slot.
Definition: Connection.h:90
HTTP reply message.
Definition: Reply.h:43
Multicast Signal to call multiple slots.
Definition: Signal.h:109
const std::string & statusText() const
Returns the HTTP status text.
Definition: Reply.h:91
StatusCode
HTTP reply status code.
Definition: Reply.h:50
void setStatus(unsigned code, const char *txt)
Sets the HTTP status.
Definition: Reply.h:80
unsigned statusCode() const
Returns the HTTP status code.
Definition: Reply.h:87