1/*
2 * Copyright (c) 2018, Daniel Gultsch All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
13 *
14 * 3. Neither the name of the copyright holder nor the names of its contributors
15 * may be used to endorse or promote products derived from this software without
16 * specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30package eu.siacs.conversations.http;
31
32import android.util.Log;
33
34import java.io.File;
35import java.net.MalformedURLException;
36import java.net.URL;
37import java.util.HashMap;
38
39import eu.siacs.conversations.Config;
40import eu.siacs.conversations.entities.Account;
41import eu.siacs.conversations.entities.DownloadableFile;
42import eu.siacs.conversations.parser.IqParser;
43import eu.siacs.conversations.services.XmppConnectionService;
44import eu.siacs.conversations.xml.Element;
45import eu.siacs.conversations.xml.Namespace;
46import eu.siacs.conversations.xmpp.stanzas.IqPacket;
47import rocks.xmpp.addr.Jid;
48
49public class SlotRequester {
50
51 private XmppConnectionService service;
52
53 public SlotRequester(XmppConnectionService service) {
54 this.service = service;
55 }
56
57 public void request(Method method, Account account, DownloadableFile file, String mime, String md5, OnSlotRequested callback) {
58 if (method == Method.HTTP_UPLOAD) {
59 Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD);
60 requestHttpUpload(account, host, file, mime, callback);
61 } else if (method == Method.HTTP_UPLOAD_LEGACY) {
62 Jid host = account.getXmppConnection().findDiscoItemByFeature(Namespace.HTTP_UPLOAD_LEGACY);
63 requestHttpUploadLegacy(account, host, file, mime, callback);
64 } else {
65 requestP1S3(account, Jid.of(account.getServer()), file.getName(), md5, callback);
66 }
67 }
68
69 private void requestHttpUploadLegacy(Account account, Jid host, DownloadableFile file, String mime, OnSlotRequested callback) {
70 IqPacket request = service.getIqGenerator().requestHttpUploadLegacySlot(host, file, mime);
71 service.sendIqPacket(account, request, (a, packet) -> {
72 if (packet.getType() == IqPacket.TYPE.RESULT) {
73 Element slotElement = packet.findChild("slot", Namespace.HTTP_UPLOAD_LEGACY);
74 if (slotElement != null) {
75 try {
76 final String putUrl = slotElement.findChildContent("put");
77 final String getUrl = slotElement.findChildContent("get");
78 if (getUrl != null && putUrl != null) {
79 Slot slot = new Slot(new URL(putUrl));
80 slot.getUrl = new URL(getUrl);
81 slot.headers = new HashMap<>();
82 slot.headers.put("Content-Type", mime == null ? "application/octet-stream" : mime);
83 callback.success(slot);
84 return;
85 }
86 } catch (MalformedURLException e) {
87 //fall through
88 }
89 }
90 }
91 Log.d(Config.LOGTAG, account.getJid().toString() + ": invalid response to slot request " + packet);
92 callback.failure(IqParser.extractErrorMessage(packet));
93 });
94
95 }
96
97 private void requestHttpUpload(Account account, Jid host, DownloadableFile file, String mime, OnSlotRequested callback) {
98 IqPacket request = service.getIqGenerator().requestHttpUploadSlot(host, file, mime);
99 service.sendIqPacket(account, request, (a, packet) -> {
100 if (packet.getType() == IqPacket.TYPE.RESULT) {
101 Element slotElement = packet.findChild("slot", Namespace.HTTP_UPLOAD);
102 if (slotElement != null) {
103 try {
104 final Element put = slotElement.findChild("put");
105 final Element get = slotElement.findChild("get");
106 final String putUrl = put == null ? null : put.getAttribute("url");
107 final String getUrl = get == null ? null : get.getAttribute("url");
108 if (getUrl != null && putUrl != null) {
109 Slot slot = new Slot(new URL(putUrl));
110 slot.getUrl = new URL(getUrl);
111 slot.headers = new HashMap<>();
112 for (Element child : put.getChildren()) {
113 if ("header".equals(child.getName())) {
114 final String name = child.getAttribute("name");
115 final String value = child.getContent();
116 if (HttpUploadConnection.WHITE_LISTED_HEADERS.contains(name) && value != null && !value.trim().contains("\n")) {
117 slot.headers.put(name, value.trim());
118 }
119 }
120 }
121 slot.headers.put("Content-Type", mime == null ? "application/octet-stream" : mime);
122 callback.success(slot);
123 return;
124 }
125 } catch (MalformedURLException e) {
126 //fall through
127 }
128 }
129 }
130 Log.d(Config.LOGTAG, account.getJid().toString() + ": invalid response to slot request " + packet);
131 callback.failure(IqParser.extractErrorMessage(packet));
132 });
133
134 }
135
136 private void requestP1S3(final Account account, Jid host, String filename, String md5, OnSlotRequested callback) {
137 IqPacket request = service.getIqGenerator().requestP1S3Slot(host, md5);
138 service.sendIqPacket(account, request, (a, packet) -> {
139 if (packet.getType() == IqPacket.TYPE.RESULT) {
140 String putUrl = packet.query(Namespace.P1_S3_FILE_TRANSFER).getAttribute("upload");
141 String id = packet.query().getAttribute("fileid");
142 try {
143 if (putUrl != null && id != null) {
144 Slot slot = new Slot(new URL(putUrl));
145 slot.getUrl = P1S3UrlStreamHandler.of(id, filename);
146 slot.headers = new HashMap<>();
147 slot.headers.put("Content-MD5", md5);
148 slot.headers.put("Content-Type", " "); //required to force it to empty. otherwise library will set something
149 callback.success(slot);
150 return;
151 }
152 } catch (MalformedURLException e) {
153 //fall through;
154 }
155 }
156 callback.failure("unable to request slot");
157 });
158 Log.d(Config.LOGTAG, "requesting slot with p1. md5=" + md5);
159 }
160
161
162 public interface OnSlotRequested {
163
164 void success(Slot slot);
165
166 void failure(String message);
167
168 }
169
170 public static class Slot {
171 private final URL putUrl;
172 private URL getUrl;
173 private HashMap<String, String> headers;
174
175 private Slot(URL putUrl) {
176 this.putUrl = putUrl;
177 }
178
179 public URL getPutUrl() {
180 return putUrl;
181 }
182
183 public URL getGetUrl() {
184 return getUrl;
185 }
186
187 public HashMap<String, String> getHeaders() {
188 return headers;
189 }
190 }
191}