Nuria Framework - Network
Network module of the NuriaProject Framework
 All Classes Functions Typedefs Enumerations Enumerator
httpclient.hpp
1 /* Copyright (c) 2014, The Nuria Project
2  * This software is provided 'as-is', without any express or implied
3  * warranty. In no event will the authors be held liable for any damages
4  * arising from the use of this software.
5  * Permission is granted to anyone to use this software for any purpose,
6  * including commercial applications, and to alter it and redistribute it
7  * freely, subject to the following restrictions:
8  * 1. The origin of this software must not be misrepresented; you must not
9  * claim that you wrote the original software. If you use this software
10  * in a product, an acknowledgment in the product documentation would be
11  * appreciated but is not required.
12  * 2. Altered source versions must be plainly marked as such, and must not be
13  * misrepresented as being the original software.
14  * 3. This notice may not be removed or altered from any source
15  * distribution.
16  */
17 
18 #ifndef NURIA_HTTPCLIENT_HPP
19 #define NURIA_HTTPCLIENT_HPP
20 
21 #include "httppostbodyreader.hpp"
22 #include "network_global.hpp"
23 #include <QNetworkCookie>
24 #include <QHostAddress>
25 #include <QIODevice>
26 #include <QMetaType>
27 #include <QMap>
28 #include <QUrl>
29 
30 namespace Nuria {
31 
32 class HttpClientPrivate;
33 class HttpTransport;
34 class HttpServer;
35 class HttpNode;
36 class SlotInfo;
37 
92 class NURIA_NETWORK_EXPORT HttpClient : public QIODevice {
93  Q_OBJECT
94  Q_ENUMS(HttpVerb)
95  Q_ENUMS(HttpHeader)
96  Q_FLAGS(HttpVerbs)
97 public:
98 
102  enum HttpVersion {
103  HttpUnknown,
105  Http1_1
106  };
107 
112  enum HttpVerb {
117  InvalidVerb = 0,
118  GET = 1,
119  POST = 2,
120  HEAD = 4,
121  PUT = 8,
122  DELETE = 16,
123 
125  AllVerbs = GET | POST | HEAD | PUT | DELETE
126  };
127 
128  Q_DECLARE_FLAGS(HttpVerbs, HttpVerb)
129 
130 
135  enum HttpHeader {
136 
137  /* Request and response */
138  HeaderCacheControl = 0,
139  HeaderContentLength,
140  HeaderContentType,
141  HeaderConnection,
142  HeaderDate,
143 
144  /* Request only */
145  HeaderHost = 1000,
147  HeaderAccept,
148  HeaderAcceptCharset,
149  HeaderAcceptEncoding,
150  HeaderAcceptLanguage,
151  HeaderAuthorization,
152  HeaderCookie,
153  HeaderRange,
154  HeaderReferer,
155  HeaderDoNotTrack,
156  HeaderExpect,
157 
158  /* Response only */
159  HeaderContentEncoding = 2000,
160  HeaderContentLanguage,
161  HeaderContentDisposition,
162  HeaderContentRange,
163  HeaderLastModified,
164  HeaderRefresh,
165  HeaderSetCookie,
166  HeaderTransferEncoding,
167  HeaderLocation
168 
169  };
170 
172  typedef QMultiMap< QByteArray, QByteArray > HeaderMap;
173 
179  explicit HttpClient (HttpTransport *transport, HttpServer *server);
180 
182  virtual ~HttpClient ();
183 
187  HttpTransport *transport () const;
188 
193  static QByteArray httpStatusCodeName (int code);
194 
198  static QByteArray httpHeaderName (HttpHeader header);
199 
201  bool isHeaderReady () const;
202 
208  bool responseHeaderSent () const;
209 
211  HttpVerb verb () const;
212 
214  QUrl path () const;
215 
220  bool hasRequestHeader (const QByteArray &key) const;
221 
223  bool hasRequestHeader (HttpHeader header) const;
224 
232  QByteArray requestHeader (const QByteArray &key) const;
233 
235  QByteArray requestHeader (HttpHeader header) const;
236 
243  QList< QByteArray > requestHeaders (const QByteArray &key) const;
244 
246  QList< QByteArray > requestHeaders (HttpHeader header) const;
247 
249  const HeaderMap &requestHeaders () const;
250 
252  bool hasResponseHeader (const QByteArray &key) const;
253 
255  bool hasResponseHeader (HttpHeader header) const;
256 
260  const HeaderMap &responseHeaders () const;
261 
265  QList< QByteArray > responseHeaders (const QByteArray &key) const;
266 
268  QList< QByteArray > responseHeaders (HttpHeader header) const;
269 
283  bool setResponseHeader (const QByteArray &key, const QByteArray &value, bool append = false);
284 
286  bool setResponseHeader (HttpHeader header, const QByteArray &value, bool append = false);
287 
292  bool setResponseHeaders (const HeaderMap &headers);
293 
302  bool pipeToClient (QIODevice *device, qint64 maxlen = -1);
303 
316  bool pipeFromPostBody (QIODevice *device, bool takeOwnership = false);
317 
323  qint64 rangeStart () const;
324 
330  qint64 rangeEnd () const;
331 
336  qint64 contentLength () const;
337 
343  bool setRangeStart (qint64 pos);
344 
350  bool setRangeEnd (qint64 pos);
351 
357  bool setContentLength (qint64 length);
358 
364  virtual bool isSequential () const;
365 
370  qint64 postBodyLength () const;
371 
375  qint64 postBodyTransferred ();
376 
380  QHostAddress localAddress () const;
381 
385  quint16 localPort () const;
386 
390  QHostAddress peerAddress () const;
391 
395  quint16 peerPort () const;
396 
401  int responseCode () const;
402 
406  void setResponseCode (int code);
407 
412  bool isConnectionSecure () const;
413 
417  HttpServer *httpServer () const;
418 
420  typedef QMap< QByteArray, QNetworkCookie > Cookies;
421 
427  Cookies cookies ();
428 
435  QNetworkCookie cookie (const QByteArray &name);
436 
442  bool hasCookie (const QByteArray &name);
443 
452  void setCookie (const QByteArray &name, const QByteArray &value,
453  const QDateTime &expires, bool secure = false);
454 
469  void setCookie (const QByteArray &name, const QByteArray &value,
470  qint64 maxAge = 0, bool secure = false);
471 
479  void setCookie (const QNetworkCookie &cookie);
480 
489  void removeCookie (const QByteArray &name);
490 
495  bool keepConnectionOpen () const;
496 
507  void setKeepConnectionOpen (bool keepOpen);
508 
512  SlotInfo slotInfo () const;
513 
518  void setSlotInfo (const SlotInfo &info);
519 
527  bool hasReadablePostBody () const;
528 
536  HttpPostBodyReader *postBodyReader ();
537 
543  bool atEnd () const override;
544 
545  //
546  qint64 pos () const override;
547  qint64 size () const override;
548  bool seek (qint64 pos) override;
549  bool reset () override;
550 
551 signals:
552 
554  void disconnected ();
555 
557  void headerReady ();
558 
560  void postBodyComplete ();
561 
562 public slots:
563 
573  bool killConnection (int error, const QString &cause = QString());
574 
581  bool sendResponseHeader ();
582 
593  void close () override;
594 
599  void forceClose ();
600 
601 private slots:
602 
604  void clientDisconnected ();
605 
607  void receivedData ();
608 
610  void pipeToClientReadyRead ();
611 
612 protected:
613 
615  virtual qint64 readData (char *data, qint64 maxlen);
616 
618  virtual qint64 writeData (const char *data, qint64 len);
619 
625  bool resolveUrl (const QUrl &url);
626 
630  bool bufferPostBody ();
631 
632 private:
633  friend class HttpServer;
634  friend class HttpNode;
635 
639  bool readRangeRequestHeader ();
640 
647  void readRequestCookies ();
648 
649  qint64 parseIntegerHeaderValue (const QByteArray &value);
650  bool readPostBodyContentLength ();
651  bool send100ContinueIfClientExpectsIt ();
652  bool readAllAvailableHeaderLines ();
653  bool readFirstLine (const QByteArray &line);
654  bool readHeader (const QByteArray &line);
655  bool isReceivedHeaderHttp11Compliant ();
656  bool verifyPostRequestCompliance ();
657  bool verifyCompleteHeader ();
658  bool invokeRequestedPath ();
659  bool closeConnectionIfNoLongerNeeded ();
660  bool verifyRequestBodyOrClose ();
661  bool requestHasPostBody () const;
662  bool postProcessRequestHeader ();
663  bool contentTypeIsMultipart (const QByteArray &value) const;
664  HttpPostBodyReader *createHttpMultiPartReader (const QByteArray &header);
665 
670  bool sendPipeChunkToClient ();
671 
672  //
673  HttpClientPrivate *d_ptr;
674 
675 };
676 
677 }
678 
679 Q_DECLARE_OPERATORS_FOR_FLAGS(Nuria::HttpClient::HttpVerbs)
680 Q_DECLARE_METATYPE(Nuria::HttpClient::HttpVersion)
681 Q_DECLARE_METATYPE(Nuria::HttpClient::HttpVerbs)
682 Q_DECLARE_METATYPE(Nuria::HttpClient::HttpVerb)
683 Q_DECLARE_METATYPE(Nuria::HttpClient*)
684 
685 #endif // NURIA_HTTPCLIENT_HPP
HttpHeader
Definition: httpclient.hpp:135
QMultiMap< QByteArray, QByteArray > HeaderMap
Definition: httpclient.hpp:172
User-Agent, must be present in a HTTP/1.1 session.
Definition: httpclient.hpp:146
The HttpServer class provides a simple HTTP server. This server is directed at providing interactive ...
Definition: httpserver.hpp:40
Http 1.0.
Definition: httpclient.hpp:104
The HttpClient class represents a connection to a client.
Definition: httpclient.hpp:92
Definition: httpnode.hpp:39
QMap< QByteArray, QNetworkCookie > Cookies
Definition: httpclient.hpp:420
The HttpNode class represents a virtual directory (or a virtual file) HttpServer uses these nodes to ...
Definition: httpnode.hpp:140
Abstract class for readers of the body of HTTP POST requests.
Definition: httppostbodyreader.hpp:41
HttpVerb
Definition: httpclient.hpp:112
HttpVersion
Definition: httpclient.hpp:102
Abstract data transport for HttpClient.
Definition: httptransport.hpp:34