Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httpresponse.h
Go to the documentation of this file.
1 
8 #ifndef _HttpResponse_H_
9 #define _HttpResponse_H_
10 
11 #include <QMap>
12 #include <QObject>
13 #include <QString>
14 #include <QTcpSocket>
15 #include <QTimer>
16 #include "httpcookie.h"
17 
18 namespace HobrasoftHttpd {
19 class HttpConnection;
20 class HttpCookie;
21 
30 class HttpResponse : public QObject {
31  Q_OBJECT
32  public:
33  ~HttpResponse();
34 
39 
43  void setHeader(const QString& name, const QString& value);
44 
48  void setHeader(const QString& name, int value);
49 
59  void setSendHeaders(bool send) { m_sentHeaders = !send; }
60 
64  QMap<QString, QString>& headers();
65 
69  void clearHeaders();
70 
74  const QMap<QString, HttpCookie>& cookies() const { return m_cookies; };
75 
79  HttpCookie cookie(const QString& name) { return m_cookies.value(name); }
80 
84  void setStatus(int code, const QString& description = QString());
85 
94  void write(const QByteArray& data);
95 
96 
100  void flushSocket();
101 
102 
111  void flush();
112 
113 
117  void setCookie(const HttpCookie& cookie);
118 
122  bool containsHeader(const QString& name) { return m_headers.contains(name); }
123 
124 
128  bool chunked();
129 
130 
134  bool isConnected() const;
135 
136  void flushAndClose();
137 
138  void flushAndDelete();
139 
145  void close();
146 
147  private slots:
148  void slotWrite(bool startTimer = true);
149  void socketDisconnected();
150  void socketError(QAbstractSocket::SocketError);
151 
152  private:
153  #ifndef DOXYGEN_SHOULD_SKIP_THIS
154  QTcpSocket *m_socket;
155  HttpConnection *m_connection;
156 
157  QMap<QString, QString> m_headers;
158  QMap<QString, HttpCookie> m_cookies;
159 
160  int m_statusCode;
161  QString m_statusText;
162 
163  bool m_sentHeaders;
164 
165  void writeToSocket(const QByteArray& data);
166  void writeHeaders();
167 
168  QTimer *m_writerTimer;
169 
170  QByteArray m_dataBody;
171  QByteArray m_dataHeaders;
172  int m_dataBodyPointer;
173  int m_dataHeadersPointer;
174  bool m_canWriteToSocket;
175  bool m_closeAfterFlush;
176  bool m_deleteAfterFlush;
177  bool m_flushed;
178  bool m_chunked;
179  #endif
180 };
181 
182 }
183 
184 #endif
const QMap< QString, HttpCookie > & cookies() const
Returns cookies of the response in QMap.
Definition: httpresponse.h:74
HttpResponse(HttpConnection *)
Constructor sets default values for headers (status 200, OK)
HttpCookie cookie(const QString &name)
Returns cookie.
Definition: httpresponse.h:79
One cookie of HTTP protocol.
Definition: httpcookie.h:20
void flush()
Writes last part of the response and closes the socket when possible.
void setSendHeaders(bool send)
Sets the header sending.
Definition: httpresponse.h:59
Response to HTTP request - headers, cookies and body.
Definition: httpresponse.h:30
void flushSocket()
Flushed sockets data to network.
QString value() const
Returns the value of cookie.
Definition: httpcookie.h:55
void write(const QByteArray &data)
Writes data to response body.
void setCookie(const HttpCookie &cookie)
Sets a cookie.
bool containsHeader(const QString &name)
Returns true if headers contains specific header.
Definition: httpresponse.h:122
bool isConnected() const
Returns true if the http connection is in connected state.
void setStatus(int code, const QString &description=QString())
Set the status code and the description of the response.
void close()
Closes socket and destroys connection. Should by called only when "chunked" transport is choosen...
bool chunked()
Returns true if the trasport mode is chunked.
void setHeader(const QString &name, const QString &value)
Sets or rewrite one header.
void clearHeaders()
Clears all headers set.
Namespace of HTTP server.
QMap< QString, QString > & headers()
Returns headers of the response in QMap.
One single connection to http server.