source: SMSSender/common/network/snetworkhelper_params.cpp @ 459:9fa7905e5ae1

new-gtwy-coopmobile
Last change on this file since 459:9fa7905e5ae1 was 459:9fa7905e5ae1, checked in by Sämy Zehnder <saemy.zehnder@…>, 7 years ago

Also percent-encodes the key of a key-value param.

File size: 2.4 KB
Line 
1/*
2  smssender - A frontend for fast and easy SMS sending over different gateways.
3  Copyright (C) 2007-2015, 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#include "snetworkhelper.h"
20
21SKeyValueParam::SKeyValueParam(const QByteArray &key, const QByteArray &value)
22    : m_key(key)
23    , m_value(value)
24{
25}
26
27
28QByteArray SKeyValueParam::encode(SNetworkHelper::PostType postType) const {
29    switch (postType) {
30        case SNetworkHelper::ptUrlEncoded: {
31            QUrl params;
32            params.addEncodedQueryItem(key().toPercentEncoding(),
33                                       value().toPercentEncoding());
34            return params.encodedQuery();
35        }
36
37        case SNetworkHelper::ptMultipart:
38        default:{
39            return "Content-Disposition: form-data; name=\"" + key() + "\"" + "\r\n" +
40                   "\r\n" +
41                   value();
42        }
43    }
44}
45
46QByteArray SKeyValueParam::key() const {
47    return m_key;
48}
49QByteArray SKeyValueParam::value() const {
50    return m_value;
51}
52
53/**************************************************/
54
55SFileParam::SFileParam(const QByteArray &key, const QByteArray &fileName, const QByteArray &fileType, const QByteArray &value)
56    : SKeyValueParam(key, value)
57    , m_fileName(fileName)
58    , m_fileType(fileType)
59{
60}
61
62
63QByteArray SFileParam::encode(SNetworkHelper::PostType postType) const {
64    S_ASSERT(postType == SNetworkHelper::ptMultipart); // Only multipart is allowed
65
66    return "Content-Disposition: form-data; name=\"" + key() + "\"; filename=\"" + fileName() + "\"" + "\r\n" +
67           "Content-Type: " + fileType() + "\r\n" +
68           "Content-Transfer-Encoding: binary" + "\r\n" +
69           "\r\n" +
70           value();
71}
72
73QByteArray SFileParam::fileName() const {
74    return m_fileName;
75}
76QByteArray SFileParam::fileType() const {
77    return m_fileType;
78}
Note: See TracBrowser for help on using the repository browser.