summaryrefslogtreecommitdiffstats
path: root/test/WopiTestServer.hpp
blob: 702e0ff02c2cce5bd5657d5d89f32eed7b05a306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#include "config.h"

#include "Protocol.hpp"
#include "HttpRequest.hpp"
#include "helpers.hpp"
#include "Log.hpp"
#include "Unit.hpp"
#include "Util.hpp"
#include "UnitWSDClient.hpp"
#include "StringVector.hpp"
#include "lokassert.hpp"

#include <Poco/JSON/Object.h>
#include <Poco/URI.h>
#include <Poco/Util/LayeredConfiguration.h>

#include <cctype>
#include <sstream>
#include <vector>

namespace Poco
{
namespace Net
{
class HTTPRequest;
}

} // namespace Poco

/// Simulates a WOPI server for testing purposes.
/// Currently only serves one file contents.
/// Furthermore, the file URI doesn't contain the
/// real filename (in most tests), instead filenames
/// 1 to 9 are considered special.
class WopiTestServer : public UnitWSDClient
{
private:

    enum class COOLStatusCode
    {
        DocChanged = 1010
    };

    /// Content of the file.
    std::string _fileContent;

    /// The filename. TODO: Support multiple ones.
    std::string _filename;

    /// Last modified time of the file
    std::chrono::system_clock::time_point _fileLastModifiedTime;

    /// The number of CheckFileInfo invocations.
    std::size_t _countCheckFileInfo;
    /// The number of GetFile invocations.
    std::size_t _countGetFile;
    /// The number of rename invocations.
    std::size_t _countPutRelative;
    /// The number of upload invocations.
    std::size_t _countPutFile;

    /// The default filename when only content is given.
    static constexpr auto DefaultFilename = "hello.txt";

protected:

    const std::string& getFileContent() const { return _fileContent; }

    /// Sets the file content to a given value and update the last file modified time
    void setFileContent(const std::string& fileContent)
    {
        LOG_TST("setFileContent: [" << COOLProtocol::getAbbreviatedMessage(fileContent ) << ']');
        _fileContent = fileContent;
        _fileLastModifiedTime = std::chrono::system_clock::now();
    }

    const std::chrono::system_clock::time_point& getFileLastModifiedTime() const
    {
        return _fileLastModifiedTime;
    }

    WopiTestServer(const std::string& name, const std::string& filenameOrContents = "Hello, world")
        : UnitWSDClient(name)
        , _filename(DefaultFilename)
        , _countCheckFileInfo(0)
        , _countGetFile(0)
        , _countPutRelative(0)
        , _countPutFile(0)
    {
        LOG_TST("WopiTestServer created for [" << getTestname() << ']');

        // Read the document data and store as string in memory.
        const auto data = helpers::readDataFromFile(filenameOrContents);
        if (!data.empty())
        {
            // That was a filename, set its contents.
            LOG_TST("WopiTestServer created with " << data.size() << " bytes from file ["
                                                   << filenameOrContents << "]");
            _filename = filenameOrContents; // Capture the real filename.
            setFileContent(Util::toString(data));
        }
        else
        {
            // Not a valid filename, assume it's some data.
            LOG_TST("WopiTestServer created with " << filenameOrContents.size()
                                                   << " bytes from data.");
            setFileContent(filenameOrContents);
        }
    }

    std::size_t getCountCheckFileInfo() const { return _countCheckFileInfo; }
    void resetCountCheckFileInfo() { _countCheckFileInfo = 0; }
    std::size_t getCountGetFile() const { return _countGetFile; }
    void resetCountGetFile() { _countGetFile = 0; }
    std::size_t getCountPutRelative() const { return _countPutRelative; }
    void resetCountPutRelative() { _countPutRelative = 0; }
    std::size_t getCountPutFile() const { return _countPutFile; }
    void resetCountPutFile() { _countPutFile = 0; }

    virtual void assertCheckFileInfoRequest(const Poco::Net::HTTPRequest& /*request*/)
    {
    }

    virtual void assertGetFileRequest(const Poco::Net::HTTPRequest& /*request*/)
    {
    }

    /// Assert the PutFile request is valid and optionally return a response.
    virtual std::unique_ptr<http::Response>
    assertPutFileRequest(const Poco::Net::HTTPRequest& /*request*/)
    {
        return nullptr; // Success.
    }

    virtual void assertPutRelativeFileRequest(const Poco::Net::HTTPRequest& /*request*/)
    {
    }

    virtual void assertRenameFileRequest(const Poco::Net::HTTPRequest& /*request*/)
    {
    }

    /// Called when the server receives a Lock or Unlock request.
    virtual std::unique_ptr<http::Response>
    assertLockRequest(const Poco::Net::HTTPRequest& /*request*/)
    {
        return nullptr; // Success.
    }

    /// Given a URI, returns the filename.
    ///FIXME: this should be remove when we support multiple files properly.
    virtual std::string getFilename(const Poco::URI& uri) const
    {
        std::string filename = extractFilenameFromWopiUri(uri.getPath());

        // Note: This is a fake implementation.
        if (filename == "3")
        {
            // Test '%' in the filename.
            //FIXME: pass this in the URI.
            return "he%llo.txt";
        }

        if (filename.size() == 1 && std::isdigit(filename[0]))
        {
            const auto number = std::stoi(filename);
            if (number >= 1 && number <= 9)
            {
                // Fake filename, depends on implicit filename.
                return DefaultFilename;
            }
        }

        // Return the filename given in the URI.
        return filename;
    }

    /// Returns the virtual root-path that we serve.
    static const std::string& getURIRootPath()
    {
        static const std::string RootPath = "/wopi/files/";
        return RootPath;
    }

    /// Given a wopi URI, extracts the filename.
    static std::string extractFilenameFromWopiUri(const std::string& uriPath)
    {
        if (Util::startsWith(uriPath, getURIRootPath()))
        {
            const auto first = getURIRootPath().size();
            const auto it = uriPath.find_first_of('/', first);
            return uriPath.substr(first, it);
        }

        return std::string();
    }

    /// Returns true iff @uriPath is a Wopi path but not to the contents.
    static bool isWopiInfoRequest(const std::string& uriPath)
    {
        return Util::startsWith(uriPath, getURIRootPath()) && !Util::endsWith(uriPath, "/contents");
    }

    /// Returns true iff @uriPath is a Wopi path to the contents of a file.
    static bool isWopiContentRequest(const std::string& uriPath)
    {
        return Util::startsWith(uriPath, getURIRootPath()) && Util::endsWith(uriPath, "/contents");
    }

    void configure(Poco::Util::LayeredConfiguration& config) override
    {
        UnitWSD::configure(config);
        // we're still internally confused as to https vs. http in places.
        config.setBool("storage.ssl.as_scheme", false);
    }

    /// Handles WOPI CheckFileInfo requests.
    virtual bool handleCheckFileInfoRequest(const Poco::Net::HTTPRequest& request,
                                            std::shared_ptr<StreamSocket>& socket)
    {
        const Poco::URI uriReq(request.getURI());

        Poco::JSON::Object::Ptr fileInfo = new Poco::JSON::Object();
        fileInfo->set("BaseFileName", getFilename(uriReq));
        fileInfo->set("Size", getFileContent().size());
        fileInfo->set("Version", "1.0");
        fileInfo->set("OwnerId", "test");
        fileInfo->set("UserId", "test");
        fileInfo->set("UserFriendlyName", "test");
        fileInfo->set("UserCanWrite", "true");
        fileInfo->set("PostMessageOrigin", "localhost");
        fileInfo->set("LastModifiedTime",
                      Util::getIso8601FracformatTime(getFileLastModifiedTime()));
        fileInfo->set("EnableOwnerTermination", "true");
        fileInfo->set("SupportsLocks", "false");
        configCheckFileInfo(fileInfo);

        std::ostringstream jsonStream;
        fileInfo->stringify(jsonStream);

        http::Response httpResponse(http::StatusCode::OK);
        httpResponse.set("Last-Modified", Util::getHttpTime(getFileLastModifiedTime()));
        httpResponse.setBody(jsonStream.str(), "application/json; charset=utf-8");
        socket->sendAndShutdown(httpResponse);

        return true;
    }

    /// Override to set the CheckFileInfo attributes.
    virtual void configCheckFileInfo(Poco::JSON::Object::Ptr /*fileInfo*/) {}

    virtual bool handleGetFileRequest(const Poco::Net::HTTPRequest&,
                                      std::shared_ptr<StreamSocket>& socket)
    {
        http::Response httpResponse(http::StatusCode::OK);
        httpResponse.set("Last-Modified", Util::getHttpTime(getFileLastModifiedTime()));
        httpResponse.setBody(getFileContent(), "application/octet-stream");
        socket->sendAndShutdown(httpResponse);

        return true;
    }

    virtual bool handleHttpGetRequest(const Poco::Net::HTTPRequest& request,
                                      std::shared_ptr<StreamSocket>& socket)
    {
        LOG_ASSERT_MSG(request.getMethod() == "GET", "Expect an HTTP GET request");

        const Poco::URI uriReq(request.getURI());

        if (isWopiInfoRequest(uriReq.getPath())) // CheckFileInfo
        {
            ++_countCheckFileInfo;
            LOG_TST("FakeWOPIHost: Handling CheckFileInfo (#" << _countCheckFileInfo
                                                              << "): " << uriReq.getPath());

            assertCheckFileInfoRequest(request);

            return handleCheckFileInfoRequest(request, socket);
        }
        else if (isWopiContentRequest(uriReq.getPath())) // GetFile
        {
            ++_countGetFile;
            LOG_TST("FakeWOPIHost: Handling GetFile (#" << _countGetFile
                                                        << "): " << uriReq.getPath());

            assertGetFileRequest(request);

            return handleGetFileRequest(request, socket);
        }

        LOG_TST("FakeWOPIHost: not a wopi request, skipping: " << uriReq.getPath());
        return false;
    }

    virtual bool handleHttpPostRequest(const Poco::Net::HTTPRequest& request,
                                       Poco::MemoryInputStream& message,
                                       std::shared_ptr<StreamSocket>& socket)
    {
        LOG_ASSERT_MSG(request.getMethod() == "POST", "Expect an HTTP POST request");

        const Poco::URI uriReq(request.getURI());
        if (isWopiInfoRequest(uriReq.getPath()))
        {
            if (!request.get("X-WOPI-Lock", std::string()).empty())
            {
                const std::string op = request.get("X-WOPI-Override", std::string());
                if (op == "LOCK" || op == "UNLOCK")
                {
                    std::unique_ptr<http::Response> response = assertLockRequest(request);
                    if (!response)
                    {
                        LOG_TST("FakeWOPIHost: " << op << " operation on [" << uriReq.getPath()
                                                 << "] succeeded 200 OK");
                        http::Response httpResponse(http::StatusCode::OK);
                        socket->sendAndShutdown(httpResponse);
                    }
                    else
                    {
                        LOG_TST("FakeWOPIHost: " << op << " operation on [" << uriReq.getPath()
                                                 << "]: " << response->statusLine().statusCode()
                                                 << ' ' << response->statusLine().reasonPhrase());
                        socket->sendAndShutdown(*response);
                    }
                }
                else
                {
                    http::Response httpResponse(http::StatusCode::Conflict);
                    httpResponse.set("X-WOPI-LockFailureReason", "Invalid lock operation");
                    socket->sendAndShutdown(httpResponse);
                }
            }
            else
            {
                const std::string wopiURL =
                    helpers::getTestServerURI() +
                    "/something wopi/files/1?access_token=anything&reuse_cookies=cook=well";

                std::string content;
                if (request.get("X-WOPI-Override") == std::string("PUT_RELATIVE"))
                {
                    ++_countPutRelative;
                    LOG_TST("FakeWOPIHost: Handling PutRelativeFile (#"
                            << _countPutRelative << "): " << uriReq.getPath());

                    LOK_ASSERT_EQUAL(std::string("PUT_RELATIVE"), request.get("X-WOPI-Override"));
                    assertPutRelativeFileRequest(request);
                    content = "{ \"Name\":\"hello world%1.pdf\", \"Url\":\"" + wopiURL + "\" }";
                }
                else
                {
                    // rename file; response should be the file name without the url and the extension
                    LOK_ASSERT_EQUAL(std::string("RENAME_FILE"), request.get("X-WOPI-Override"));
                    assertRenameFileRequest(request);
                    content = "{ \"Name\":\"hello\", \"Url\":\"" + wopiURL + "\" }";
                }

                http::Response httpResponse(http::StatusCode::OK);
                httpResponse.set("Last-Modified", Util::getHttpTime(getFileLastModifiedTime()));
                httpResponse.setBody(content, "application/json; charset=utf-8");
                socket->sendAndShutdown(httpResponse);
            }

            return true;
        }
        else if (isWopiContentRequest(uriReq.getPath()))
        {
            ++_countPutFile;
            LOG_TST("FakeWOPIHost: Handling PutFile (#" << _countPutFile
                                                        << "): " << uriReq.getPath());

            const std::string wopiTimestamp = request.get("X-COOL-WOPI-Timestamp", std::string());
            if (!wopiTimestamp.empty())
            {
                const std::string fileModifiedTime =
                    Util::getIso8601FracformatTime(getFileLastModifiedTime());
                if (wopiTimestamp != fileModifiedTime)
                {
                    LOG_TST("FakeWOPIHost: Document conflict detected, Stored ModifiedTime: ["
                            << fileModifiedTime << "], Upload ModifiedTime: [" << wopiTimestamp
                            << ']');
                    http::Response httpResponse(http::StatusCode::Conflict);
                    httpResponse.setBody(
                        "{\"COOLStatusCode\":" +
                        std::to_string(static_cast<int>(COOLStatusCode::DocChanged)) + '}');
                    socket->sendAndShutdown(httpResponse);
                    return true;
                }
            }
            else
            {
                LOG_TST("FakeWOPIHost: Forced document upload");
            }

            std::unique_ptr<http::Response> response = assertPutFileRequest(request);
            if (!response || response->statusLine().statusCategory() ==
                                 http::StatusLine::StatusCodeClass::Successful)
            {
                const std::streamsize size = request.getContentLength();
                LOG_TST("FakeWOPIHost: Writing document contents in storage (" << size << "bytes)");
                std::vector<char> buffer(size);
                message.read(buffer.data(), size);
                setFileContent(Util::toString(buffer));
            }

            if (response)
            {
                LOG_TST("FakeWOPIHost: Response to POST "
                        << uriReq.getPath() << ": " << response->statusLine().statusCode() << ' '
                        << response->statusLine().reasonPhrase());
                socket->sendAndShutdown(*response);
            }
            else
            {
                // By default we return success.
                const std::string body = "{\"LastModifiedTime\": \"" +
                                         Util::getIso8601FracformatTime(getFileLastModifiedTime()) +
                                         "\" }";
                LOG_TST("FakeWOPIHost: Response (default) to POST " << uriReq.getPath()
                                                                    << ": 200 OK " << body);
                http::Response httpResponse(http::StatusCode::OK);
                httpResponse.setBody(body, "application/json; charset=utf-8");
                socket->sendAndShutdown(httpResponse);
            }

            return true;
        }

        return false;
    }

    /// In some very rare cases we may get requests from other tests.
    /// This asserts that the URI in question is for our test.
    void assertTargetTest(const Poco::URI& uri)
    {
        const auto params = uri.getQueryParameters();
        const auto testnameIt = std::find_if(params.begin(), params.end(),
                                             [](const std::pair<std::string, std::string>& pair)
                                             { return pair.first == "testname"; });

        LOK_ASSERT_MESSAGE_SILENT("Request belongs to an unknown test", testnameIt != params.end());

        const std::string target = StringVector::tokenize(testnameIt->second, '/')[0];
        LOK_ASSERT_EQUAL_MESSAGE("Request belongs to a different test", getTestname(), target);
    }

    /// Here we act as a WOPI server, so that we have a server that responds to
    /// the wopi requests without additional expensive setup.
    bool handleHttpRequest(const Poco::Net::HTTPRequest& request, Poco::MemoryInputStream& message,
                           std::shared_ptr<StreamSocket>& socket) override
    {
        Poco::URI uriReq(Util::decodeURIComponent(request.getURI()));

        {
            std::ostringstream oss;
            oss << "FakeWOPIHost: " << request.getMethod() << " request URI [" << uriReq.toString()
                << "]:\n";
            for (const auto& pair : request)
            {
                oss << '\t' << pair.first << ": " << pair.second << " / ";
            }

            LOG_TST(oss.str());
        }

        assertTargetTest(uriReq);

        if (request.getMethod() == "GET")
        {
            return handleHttpGetRequest(request, socket);
        }
        else if (request.getMethod() == "POST")
        {
            return handleHttpPostRequest(request, message, socket);
        }
        else if (!Util::startsWith(uriReq.getPath(), "/cool/")) // Skip requests to the websrv.
        {
            // Complain if we are expected to handle something that we don't.
            LOG_TST("ERROR: FakeWOPIHost: Request, cannot handle request: " << uriReq.getPath());
        }

        return false;
    }

};

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */