libktorrent  2.2.0
httpconnection.h
1 /***************************************************************************
2  * Copyright (C) 2008 by Joris Guisson and Ivan Vasic *
3  * joris.guisson@gmail.com *
4  * ivasic@gmail.com *
5  * *
6  * This program is free software; you can redistribute it and/or modify *
7  * it under the terms of the GNU General Public License as published by *
8  * the Free Software Foundation; either version 2 of the License, or *
9  * (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License *
17  * along with this program; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
20  ***************************************************************************/
21 #ifndef BTHTTPCONNECTION_H
22 #define BTHTTPCONNECTION_H
23 
24 #include <QMutex>
25 #include <QTimer>
26 #include <QUrl>
27 #include <net/streamsocket.h>
28 #include <net/addressresolver.h>
29 
30 class QUrl;
31 
32 namespace bt
33 {
34 
41  class HttpConnection : public QObject, public net::SocketReader, public net::StreamSocketListener
42  {
43  Q_OBJECT
44  public:
46  ~HttpConnection() override;
47 
49  int responseCode() const {return response_code;}
50 
52  bool isRedirected() const {return redirected;}
53 
55  QUrl redirectedUrl() const {return redirected_url;}
56 
62  void setGroupIDs(Uint32 up,Uint32 down);
63 
68  void connectTo(const QUrl &url);
69 
75  void connectToProxy(const QString & proxy,Uint16 proxy_port);
76 
78  bool ok() const;
79 
81  bool connected() const;
82 
84  bool closed() const;
85 
87  bool ready() const;
88 
96  bool get(const QString & host,const QString & path,const QString & query,bt::Uint64 start,bt::Uint64 len);
97 
98  void onDataReady(Uint8* buf,Uint32 size) override;
99  void connectFinished(bool succeeded) override;
100  void dataSent() override;
101 
107  bool getData(QByteArray & data);
108 
110  int getDownloadRate() const;
111 
113  const QString getStatusString() const;
114 
115  private Q_SLOTS:
116  void hostResolved(net::AddressResolver* ar);
117  void connectTimeout();
118  void replyTimeout();
119 
120  Q_SIGNALS:
121  void startReplyTimer(int timeout);
122  void stopReplyTimer();
123  void stopConnectTimer();
124 
125  private:
126  enum State
127  {
128  IDLE,RESOLVING,CONNECTING,ACTIVE,ERROR,CLOSED
129  };
130 
131  struct HttpGet
132  {
133  QString host;
134  QString path;
135  QString query;
136  bt::Uint64 start;
137  bt::Uint64 len;
138  bt::Uint64 data_received;
139  QByteArray buffer;
140  QByteArray piece_data;
141  bool response_header_received;
142  bool request_sent;
143  QString failure_reason;
144  bool redirected;
145  QUrl redirected_to;
146  bt::Uint64 content_length;
147  int response_code;
148 
149  HttpGet(const QString & host,const QString & path,const QString & query,bt::Uint64 start,bt::Uint64 len,bool using_proxy);
150  virtual ~HttpGet();
151 
152  bool onDataReady(Uint8* buf,Uint32 size);
153  bool finished() const {return data_received >= len;}
154  };
155 
156  net::StreamSocket* sock;
157  State state;
158  mutable QMutex mutex;
159  HttpGet* request;
160  bool using_proxy;
161  QString status;
162  QTimer connect_timer;
163  QTimer reply_timer;
164  Uint32 up_gid,down_gid;
165  bool close_when_finished;
166  bool redirected;
167  QUrl redirected_url;
168  int response_code;
169  };
170 }
171 
172 #endif
net::StreamSocketListener
Definition: streamsocket.h:50
net::AddressResolver
Definition: addressresolver.h:55
bt::HttpConnection::connectTo
void connectTo(const QUrl &url)
bt::HttpConnection::dataSent
void dataSent() override
bt::HttpConnection::get
bool get(const QString &host, const QString &path, const QString &query, bt::Uint64 start, bt::Uint64 len)
bt::HttpConnection::connectFinished
void connectFinished(bool succeeded) override
bt::HttpConnection::onDataReady
void onDataReady(Uint8 *buf, Uint32 size) override
net::StreamSocket
Definition: streamsocket.h:69
bt::HttpConnection::ok
bool ok() const
Check if the connection is OK.
bt::HttpConnection::setGroupIDs
void setGroupIDs(Uint32 up, Uint32 down)
bt::HttpConnection::connectToProxy
void connectToProxy(const QString &proxy, Uint16 proxy_port)
bt::HttpConnection::isRedirected
bool isRedirected() const
Is this connection redirected.
Definition: httpconnection.h:52
bt::HttpConnection::connected
bool connected() const
See if we are connected.
bt::HttpConnection::closed
bool closed() const
Has the connection been closed.
bt::HttpConnection::getData
bool getData(QByteArray &data)
bt::HttpConnection::ready
bool ready() const
Ready to do another request.
bt::HttpConnection::getDownloadRate
int getDownloadRate() const
Get the current download rate.
bt::HttpConnection
Definition: httpconnection.h:42
bt::HttpConnection::redirectedUrl
QUrl redirectedUrl() const
Get the redirected url.
Definition: httpconnection.h:55
net::SocketReader
Definition: trafficshapedsocket.h:52
bt::HttpConnection::getStatusString
const QString getStatusString() const
Get the status string.
bt::HttpConnection::responseCode
int responseCode() const
Get the last http response code.
Definition: httpconnection.h:49