source: SMSSender/common/network/snetworkhelper.h @ 315:e6c8bb244ecd

separation-frontend-backend
Last change on this file since 315:e6c8bb244ecd was 315:e6c8bb244ecd, checked in by Sämy Zehnder <saemy.zehnder@…>, 6 years ago
  • Separates the frontend from the backend code.
  • At this point the schoolnet gateway is converted only.
  • Removes bc & da prefixes from the filenames.
File size: 5.7 KB
Line 
1/*
2 smssender - A frontend for fast and easy SMS sending over different gateways.
3 Copyright (C) 2007-2012, gorrión. See http://smssender.gorrion.ch
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef SNETWORKHELPER_H_
20#define SNETWORKHELPER_H_
21
22#include <QList>
23#include <QMap>
24#include <QMutex>
25#include <QNetworkAccessManager>
26#include <QNetworkCookieJar>
27#include <QNetworkRequest>
28#include <QNetworkReply>
29#include <QSslConfiguration>
30#include <QThread>
31#include <QUrl>
32
33#include "common/global.h"
34#include "common/utils/sshared.h"
35
36typedef QMap<QByteArray, QByteArray> HeadersMap;
37
38class IParam;
39class SNetworkReply;
40
41// TODO: rename into SHttpHelper
42class COMMON_EXPORT SNetworkHelper : public QObject {
43    Q_OBJECT
44
45public:
46    static const uint MAX_REDIRECT_COUNT;
47
48    enum RedirectHandling {
49        rhNone,
50        rhHandleRedirects
51    };
52
53    enum PostType {
54        ptUrlEncoded,
55        ptMultipart
56    };
57
58public:
59    SNetworkHelper(QObject *parent = NULL);
60    ~SNetworkHelper();
61
62    QNetworkAccessManager &networkAccessManager();
63
64    SNetworkReply          syncGet(const QUrl& destination, SNetworkHelper::RedirectHandling redirectHandling = SNetworkHelper::rhHandleRedirects);
65    SNetworkReply          syncGet(QNetworkRequest& request, SNetworkHelper::RedirectHandling redirectHandling = SNetworkHelper::rhHandleRedirects);
66    SNetworkReply          asyncGet(const QUrl& destination);
67    SNetworkReply          asyncGet(QNetworkRequest& request);
68    SNetworkReply          syncPost(const QUrl& destination, QList<IParam*>& posts, SNetworkHelper::PostType postType = SNetworkHelper::ptUrlEncoded, SNetworkHelper::RedirectHandling redirectHandling = SNetworkHelper::rhHandleRedirects);
69    SNetworkReply          syncPost(QNetworkRequest& request, QList<IParam*>& posts, SNetworkHelper::PostType postType = SNetworkHelper::ptUrlEncoded, SNetworkHelper::RedirectHandling redirectHandling = SNetworkHelper::rhHandleRedirects);
70    SNetworkReply          asyncPost(const QUrl& destination, QList<IParam*>& posts, SNetworkHelper::PostType postType = SNetworkHelper::ptUrlEncoded);
71    SNetworkReply          asyncPost(QNetworkRequest& request, QList<IParam*>& posts, SNetworkHelper::PostType postType = SNetworkHelper::ptUrlEncoded);
72
73    void                   addTrustedCA(const QString& certFilename, QSsl::EncodingFormat format = QSsl::Pem);
74
75    static void            waitFor(const SNetworkReply& networkReply);
76    SNetworkReply          handleRedirect(SNetworkReply reply, SNetworkHelper::RedirectHandling redirectHandling);
77
78    QNetworkRequest        createDefaultRequest(const QUrl& url);
79    QUrl                   getRedirectUrl(const SNetworkReply& reply);
80
81    void                   clearCookies();
82
83    void                   attachDefaultHeaders(QNetworkRequest& request);
84    void                   attachDefaultSSLConfiguration(QNetworkRequest& request);
85
86    static QString         htmldecodeString(const QString& str);
87    static QString         urldecodeString(const QString& str);
88
89public:
90    HeadersMap&            defaultHeaders();
91    QSslConfiguration&     defaultSSLConfiguration();
92
93    void                   setDefaultHeaders(const HeadersMap& headers);
94    void                   setDefaultSSLConfiguration(const QSslConfiguration& sslConfiguration);
95
96private:
97    void                   initDefaultSSLConfiguration();
98
99    void                   adjustUrl(QUrl& url);
100    QByteArray             paramsToEncoded(const QList<IParam*>& posts, SNetworkHelper::PostType postType);
101    QUrl                   lastUrl();
102
103private:
104    static void                   fillEscapeChars();
105    static QMap<QString, QString> escapeChars();
106
107private slots:
108    void onThreadTerminated();
109    void onSslErrors(QNetworkReply* reply, const QList<QSslError>& errors);
110
111private:
112    const QString MULTIPART_BOUNDARY;
113
114    QNetworkCookieJar *                      sharedCookieJar_;
115    QMap<QThread *, QNetworkAccessManager *> networkAccessManager_;
116    QMap<QNetworkAccessManager *, QUrl>      lastUrl_;
117
118    HeadersMap        defaultHeaders_;
119    QSslConfiguration defaultSSLConfiguration_;
120
121    static QMap<QString, QString> escapeChars_;
122};
123
124
125class COMMON_EXPORT IParam {
126public:
127    virtual QByteArray encode(SNetworkHelper::PostType postType) const =0;
128
129public:
130    virtual ~IParam() {}
131};
132Q_DECLARE_INTERFACE(IParam, "ch.gorrion.smssender.IParam/1.0")
133
134
135class COMMON_EXPORT SKeyValueParam : public IParam {
136    Q_INTERFACES(IParam)
137
138public:
139    SKeyValueParam(const QByteArray& key, const QByteArray& value);
140    SKeyValueParam(const SKeyValueParam& other);
141
142    QByteArray encode(SNetworkHelper::PostType postType) const;
143
144public:
145    QByteArray key() const;
146    QByteArray value() const;
147
148private:
149    QByteArray key_;
150    QByteArray value_;
151};
152
153
154class COMMON_EXPORT SFileParam : public SKeyValueParam {
155public:
156    SFileParam(const QByteArray& key, const QByteArray& fileName, const QByteArray& fileType, const QByteArray& value);
157    SFileParam(const SFileParam& other);
158
159    QByteArray encode(SNetworkHelper::PostType postType) const;
160
161public:
162    QByteArray fileName() const;
163    QByteArray fileType() const;
164
165private:
166    QByteArray fileName_;
167    QByteArray fileType_;
168};
169
170#endif /* SNETWORKHELPER_H_ */
Note: See TracBrowser for help on using the repository browser.