KVIrc  4.9.2
DeveloperAPIs
KviHttpRequest.h
Go to the documentation of this file.
1 #ifndef _KviHttpRequest_h_
2 #define _KviHttpRequest_h_
3 //=============================================================================
4 //
5 // File : KviHttpRequest.h
6 // Creation date : Sat Aug 17 13:43:31 2002 GMT by Szymon Stefanek
7 //
8 // This file is part of the KVIrc IRC client distribution
9 // Copyright (C) 2002-2010 Szymon Stefanek (pragma at kvirc dot net)
10 //
11 // This program is FREE software. You can redistribute it and/or
12 // modify it under the terms of the GNU General Public License
13 // as published by the Free Software Foundation; either version 2
14 // of the License, or (at your option) any later version.
15 //
16 // This program is distributed in the HOPE that it will be USEFUL,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 // See the GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program. If not, write to the Free Software Foundation,
23 // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 //
25 //=============================================================================
26 
27 #include "kvi_settings.h"
28 #include "kvi_inttypes.h"
29 
30 #ifdef Status
31 #undef Status
32 #endif // otherwise QTextStream freaks out
33 
34 #include <QTextStream> // with automoc we need this to be included before the other files
35 
36 #include "KviHeapObject.h"
37 #include "KviCString.h"
38 #include "KviUrl.h"
39 #include "KviPointerHashTable.h"
40 
41 #include <QObject>
42 
43 #include <QtNetwork/QAbstractSocket>
44 
45 class KviDnsResolver;
46 class KviDataBuffer;
47 
48 //
49 // This class implements a HTTP protocol client.
50 // It's able to send GET, POST and HEAD requests,
51 // download stuff to a file or to a qt SLOT().
52 //
53 
54 // FIXME: Document and hide internals.
55 
57 
58 class KVILIB_API KviHttpRequest : public QObject, public KviHeapObject
59 {
60  Q_OBJECT
61 public:
63  {
64  HeadersOnly, // Download headers only (HEAD request)
65  WholeFile, // Emit the data as whole file (binaryData() is emitted)
66  Blocks, // Emit the data as blocks (binaryData() is emitted)
67  Lines, // Emit the data as ASCII text lines (the client must take care of decoding the data)
68  StoreToFile // Store the data to a file
69  };
70 
72  {
73  Overwrite, // Overwrite existing file
74  RenameIncoming, // Automatically rename the incoming file
75  RenameExisting, // Automatically rename the existing file
76  Resume // Attempt to resume the file (get partial content)
77  };
78 
79 public:
81 
82  virtual ~KviHttpRequest();
83 
84 protected:
85  // data
86  KviUrl m_url; // the url requested by the user
87  QString m_szFileName;
91  unsigned int m_uMaxContentLength;
92  unsigned int m_uContentOffset;
93  QString m_szPostData;
96  // status
97  QString m_szLastError;
98  unsigned int m_uTotalSize;
99  unsigned int m_uReceivedSize;
100  // internal status
101  KviUrl m_connectionUrl; // the actual url used in the connection (may change upon redirect)
104  bool m_bGzip;
105  unsigned int m_uRemainingChunkSize;
106  bool m_bIgnoreRemainingData; // used in chunked transfer after the last chunk has been seen
107  unsigned int m_uConnectionTimeout; // in seconds, 60 secs by default
108  unsigned int m_uRedirectCount;
109 
110 private:
112 
113 public:
114  bool followRedirects() const
115  {
116  return m_bFollowRedirects;
117  }
118 
119  void setFollowRedirects(bool bFollow)
120  {
121  m_bFollowRedirects = bFollow;
122  }
123 
124  void setMaximumRedirectCount(unsigned int uCount)
125  {
126  m_uMaximumRedirectCount = uCount;
127  }
128 
129  void setConnectionTimeout(unsigned int uConnectionTimeout)
130  {
131  m_uConnectionTimeout = uConnectionTimeout;
132  if(m_uConnectionTimeout < 5)
133  m_uConnectionTimeout = 5; // keep it sane
134  }
135 
136  const KviUrl & url() { return m_url; };
137  ProcessingType processingType() { return m_eProcessingType; };
138  ExistingFileAction existingFileAction() { return m_eExistingFileAction; };
139  const QString & fileName() { return m_szFileName; };
140  void * privateData() { return m_pPrivateData; };
141  unsigned int maxContentLength() { return m_uMaxContentLength; };
142  unsigned int contentOffset() { return m_uContentOffset; };
143  unsigned int totalSize() { return m_uTotalSize; };
144  unsigned int receivedSize() { return m_uReceivedSize; };
145 
146  void reset();
147 
148  void setPostData(const QString & szPostData) { m_szPostData = szPostData; };
149  void setUrl(const KviUrl & u)
150  {
151  m_url = u;
152  m_connectionUrl = u;
153  };
154  void setProcessingType(ProcessingType t) { m_eProcessingType = t; };
155  void setExistingFileAction(ExistingFileAction a) { m_eExistingFileAction = a; };
156  void setFileName(const QString & szFileName) { m_szFileName = szFileName; };
157  void setPrivateData(void * ptr) { m_pPrivateData = ptr; };
158  void setMaxContentLength(int uMaxContentLength) { m_uMaxContentLength = uMaxContentLength; }; //0 means unlimited
159  // this will work regardless of ExistingFileAction : even if the file doesn't exist
160  void setContentOffset(int uContentOffset) { m_uContentOffset = uContentOffset; };
161 
162  bool start();
163 
164  // this is a shortcut for reset()+setUrl()+setProcessingType()+setFileName()+start()
165  bool get(const KviUrl & u, ProcessingType p = WholeFile, const QString & szFileName = QString());
166 
167  const QString & lastError() { return m_szLastError; };
168 
169  void abort();
170 signals:
171  void resolvingHost(const QString & hostname);
172  void contactingHost(const QString & ipandport);
173  void connectionEstablished();
174  void receivedResponse(const QString & response);
175 
176  void terminated(bool bSuccess);
177 
178  void status(const QString & message);
179  void data(const KviCString & data);
180  void binaryData(const KviDataBuffer & data);
182  void requestSent(const QStringList & request);
183 
184 private:
185  void closeSocket();
186  bool doConnect();
187 
188  void processData(KviDataBuffer * data);
189  bool processHeader(KviCString & szHeader);
190  bool openFile();
191  void emitLines(KviDataBuffer * pDataBuffer);
192 
193  void resetStatus();
194  void resetData();
195  void resetInternalStatus();
196 
197 private slots:
198  void slotSocketReadDataReady();
199  void slotSocketDisconnected();
200  void slotSocketConnected();
201  void slotSocketError(QAbstractSocket::SocketError socketError);
202  void slotConnectionTimedOut();
203  void slotSocketHostResolved();
204 };
205 
206 #endif //_KviHttpRequest_h_
const QString & fileName()
Definition: KviHttpRequest.h:139
unsigned int m_uContentOffset
Definition: KviHttpRequest.h:92
void setPostData(const QString &szPostData)
Definition: KviHttpRequest.h:148
unsigned int m_uConnectionTimeout
Definition: KviHttpRequest.h:107
void setMaximumRedirectCount(unsigned int uCount)
Definition: KviHttpRequest.h:124
Definition: KviHeapObject.h:124
void setConnectionTimeout(unsigned int uConnectionTimeout)
Definition: KviHttpRequest.h:129
void setContentOffset(int uContentOffset)
Definition: KviHttpRequest.h:160
unsigned int totalSize()
Definition: KviHttpRequest.h:143
Definition: KviCString.h:105
void setMaxContentLength(int uMaxContentLength)
Definition: KviHttpRequest.h:158
void setUrl(const KviUrl &u)
Definition: KviHttpRequest.h:149
#define a
Definition: detector.cpp:91
unsigned int m_uReceivedSize
Definition: KviHttpRequest.h:99
KviUrl m_url
Definition: KviHttpRequest.h:86
Definition: KviHttpRequest.h:73
bool m_bGzip
Definition: KviHttpRequest.h:104
Definition: KviHttpRequest.h:74
void setExistingFileAction(ExistingFileAction a)
Definition: KviHttpRequest.h:155
bool m_bChunkedTransferEncoding
Definition: KviHttpRequest.h:103
Definition: KviHttpRequest.h:67
bool m_bFollowRedirects
Definition: KviHttpRequest.h:94
ExistingFileAction
Definition: KviHttpRequest.h:71
unsigned int receivedSize()
Definition: KviHttpRequest.h:144
const QString & lastError()
Definition: KviHttpRequest.h:167
KviUrl m_connectionUrl
Definition: KviHttpRequest.h:101
Definition: KviDataBuffer.h:30
Definition: KviHttpRequest.h:75
Definition: KviHttpRequest.h:66
Definition: KviHttpRequest.h:64
Definition: KviHttpRequest.cpp:46
QString m_szPostData
Definition: KviHttpRequest.h:93
void * privateData()
Definition: KviHttpRequest.h:140
unsigned int maxContentLength()
Definition: KviHttpRequest.h:141
void setFileName(const QString &szFileName)
Definition: KviHttpRequest.h:156
Pointer Hash Table.
KviHttpRequestPrivate * m_p
Definition: KviHttpRequest.h:111
QString m_szFileName
Definition: KviHttpRequest.h:87
bool followRedirects() const
Definition: KviHttpRequest.h:114
void setPrivateData(void *ptr)
Definition: KviHttpRequest.h:157
unsigned int m_uRedirectCount
Definition: KviHttpRequest.h:108
The KviDnsResolver class.
Definition: KviDnsResolver.h:93
unsigned int contentOffset()
Definition: KviHttpRequest.h:142
ProcessingType
Definition: KviHttpRequest.h:62
QString m_szLastError
Definition: KviHttpRequest.h:97
unsigned int m_uRemainingChunkSize
Definition: KviHttpRequest.h:105
Definition: KviHttpRequest.h:65
void setProcessingType(ProcessingType t)
Definition: KviHttpRequest.h:154
QHashIterator< int, QFile * > t(getDict)
Definition: KviHttpRequest.h:58
bool m_bIgnoreRemainingData
Definition: KviHttpRequest.h:106
ExistingFileAction existingFileAction()
Definition: KviHttpRequest.h:138
Heap Object.
ProcessingType m_eProcessingType
Definition: KviHttpRequest.h:88
unsigned int m_uMaximumRedirectCount
Definition: KviHttpRequest.h:95
ExistingFileAction m_eExistingFileAction
Definition: KviHttpRequest.h:89
void * m_pPrivateData
Definition: KviHttpRequest.h:90
#define u
Definition: detector.cpp:85
unsigned int m_uTotalSize
Definition: KviHttpRequest.h:98
A fast pointer hash table implementation.
Definition: KviPointerHashTable.h:391
bool m_bHeaderProcessed
Definition: KviHttpRequest.h:102
unsigned int m_uMaxContentLength
Definition: KviHttpRequest.h:91
This file contains compile time settings.
Definition: KviUrl.h:31
#define p
Definition: detector.cpp:80
ProcessingType processingType()
Definition: KviHttpRequest.h:137
void setFollowRedirects(bool bFollow)
Definition: KviHttpRequest.h:119
#define KVILIB_API
Definition: kvi_settings.h:125
QString hostname()
Definition: KviRuntimeInfo.cpp:720
const KviUrl & url()
Definition: KviHttpRequest.h:136