2
* Copyright (C) 2010, 2011 Tuomo Penttinen, all rights reserved.
4
* Author: Tuomo Penttinen <tp@herqq.org>
6
* This file is part of Herqq UPnP (HUPnP) library.
8
* Herqq UPnP is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU Lesser General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
13
* Herqq UPnP is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public License
19
* along with Herqq UPnP. If not, see <http://www.gnu.org/licenses/>.
22
#ifndef HTTP_ASYNCHANDLER_P_H_
23
#define HTTP_ASYNCHANDLER_P_H_
28
// This file is not part of public API and it should
29
// never be included in client code. The contents of this file may
30
// change or the file may be removed without of notice.
33
#include <HUpnpCore/HUpnp>
36
#include "hhttp_header_p.h"
37
#include "hhttp_messaginginfo_p.h"
39
#include <QtCore/QHash>
40
#include <QtCore/QObject>
41
#include <QtCore/QByteArray>
42
#include <QtNetwork/QAbstractSocket>
52
class HHttpAsyncHandler;
57
class HHttpAsyncOperation :
61
H_DISABLE_COPY(HHttpAsyncOperation)
62
friend class HHttpAsyncHandler;
81
Internal_WritingChunkedSizeLine,
82
Internal_WritingChunk,
83
Internal_ReadingHeader,
85
Internal_ReadingChunkSizeLine,
86
Internal_ReadingChunk,
87
Internal_FinishedSuccessfully
92
QByteArray m_dataToSend;
93
// the data which will be sent to the target socket
96
// used only with chunked encoding when a chunk cannot be sent in full and
97
// the operation needs to be continued later
100
// the amount of data that has been successfully sent
102
InternalState m_state;
103
// the current state of this "state machine"
105
HHttpHeader* m_headerRead;
106
// the http reader read from the target socket
107
// (request / response, depends of the setup)
109
QByteArray m_dataRead;
110
// the response data that is currently read from the target socket
113
// the amount of data that should be available (once the operation is
114
// successfully completed)
117
// id for the operation
119
const QByteArray m_loggingIdentifier;
122
// what the operation is supposed to do
129
bool readChunkedSizeLine();
132
// the return value of these two methods indicate if it is okay to continue
133
// the operation. when returned false, the operation has signaled completion
134
// and thus must be aborted immediately.
139
void done_(InternalState state, bool emitSignal = true);
143
void bytesWritten(qint64);
145
void error(QAbstractSocket::SocketError);
159
const QByteArray& loggingIdentifier, unsigned int id, HMessagingInfo* mi,
160
bool waitingRequest, QObject* parent);
163
const QByteArray& loggingIdentifier, unsigned int id, HMessagingInfo* mi,
164
const QByteArray& data, bool sendOnly, QObject* parent);
166
virtual ~HHttpAsyncOperation();
170
inline unsigned int id() const { return m_id; }
172
// the data of the response
173
inline QByteArray dataRead() const { return m_dataRead; }
175
// the header of the response
176
inline const HHttpHeader* headerRead() const { return m_headerRead; }
178
inline HMessagingInfo* messagingInfo() const { return m_mi; }
180
inline HMessagingInfo* takeMessagingInfo()
182
HMessagingInfo* retVal = m_mi; m_mi = 0;
186
inline OpType opType() const { return m_opType; }
190
void done(unsigned int);
194
// Performs async messaging utilizing the event loop.
195
// This class is not thread-safe.
197
class H_UPNP_CORE_EXPORT HHttpAsyncHandler :
201
H_DISABLE_COPY(HHttpAsyncHandler)
202
friend class HHttpAsyncOperation;
206
const QByteArray m_loggingIdentifier;
208
QHash<unsigned int, HHttpAsyncOperation*> m_operations;
210
unsigned int m_lastIdUsed;
214
void done(unsigned int);
218
// user is expected to delete the transferred object
219
void msgIoComplete(HHttpAsyncOperation*);
223
HHttpAsyncHandler(const QByteArray& loggingIdentifier, QObject* parent);
224
virtual ~HHttpAsyncHandler();
228
// \param data contains an entire HTTP message, including headers.
230
// \return an object that contains state data for the operation.
231
// once the operation is done, user is expected to delete the object, but
233
HHttpAsyncOperation* msgIo(HMessagingInfo* mi, const QByteArray& data);
238
HHttpAsyncOperation* msgIo(
239
HMessagingInfo*, HHttpRequestHeader&, const QtSoapMessage&);
244
HHttpAsyncOperation* send(HMessagingInfo*, const QByteArray& data);
247
// waitingRequest == expecting to receive HHttpRequestHeader, otherwise
248
// expecting to receive HHttpResponseHeader
250
HHttpAsyncOperation* receive(HMessagingInfo*, bool waitingRequest);
256
#endif /* HTTP_ASYNCHANDLER_P_H_ */