annotate UnitTestsSources/RestApiTests.cpp @ 1782:9f2df9bb2cdf

fix
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Nov 2015 09:05:43 +0100
parents 5ad4e4d92ecb
children dbb07eb1a2f3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
827
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
1 /**
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1064
diff changeset
3 * Copyright (C) 2012-2015 Sebastien Jodogne, Medical Physics
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1064
diff changeset
4 * Department, University Hospital of Liege, Belgium
827
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
5 *
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
6 * This program is free software: you can redistribute it and/or
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
7 * modify it under the terms of the GNU General Public License as
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
9 * License, or (at your option) any later version.
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
10 *
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
11 * In addition, as a special exception, the copyright holders of this
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
12 * program give permission to link the code of its release with the
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
15 * the linked executables. You must obey the GNU General Public License
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
19 * you do not wish to do so, delete this exception statement from your
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
20 * version. If you delete this exception statement from all source files
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
21 * in the program, then also delete it here.
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
22 *
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
23 * This program is distributed in the hope that it will be useful, but
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
26 * General Public License for more details.
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
27 *
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
28 * You should have received a copy of the GNU General Public License
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
30 **/
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
31
3d6f9b7d0add precompiled headers in unit tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 707
diff changeset
32
831
84513f2ee1f3 pch for unit tests and server
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 827
diff changeset
33 #include "PrecompiledHeadersUnitTests.h"
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "gtest/gtest.h"
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <ctype.h>
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
37 #include <boost/lexical_cast.hpp>
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
38 #include <algorithm>
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
40 #include "../Core/ChunkedBuffer.h"
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
41 #include "../Core/HttpClient.h"
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1441
diff changeset
42 #include "../Core/Logging.h"
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #include "../Core/RestApi/RestApi.h"
221
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
44 #include "../Core/Uuid.h"
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
45 #include "../Core/OrthancException.h"
e7432706b354 accessors to storage
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 210
diff changeset
46 #include "../Core/Compression/ZlibCompressor.h"
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
47 #include "../Core/RestApi/RestApiHierarchy.h"
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 using namespace Orthanc;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
51 #if !defined(UNIT_TESTS_WITH_HTTP_CONNEXIONS)
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
52 #error "Please set UNIT_TESTS_WITH_HTTP_CONNEXIONS"
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
53 #endif
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
54
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
55
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
56
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
57 TEST(HttpClient, Basic)
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
58 {
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
59 HttpClient c;
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
60 ASSERT_FALSE(c.IsVerbose());
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
61 c.SetVerbose(true);
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
62 ASSERT_TRUE(c.IsVerbose());
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
63 c.SetVerbose(false);
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
64 ASSERT_FALSE(c.IsVerbose());
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
65
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
66 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
67 Json::Value v;
1419
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
68 c.SetUrl("http://www.montefiore.ulg.ac.be/~jodogne/Orthanc/Configuration.json");
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
69 c.Apply(v);
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
70 ASSERT_TRUE(v.isMember("StorageDirectory"));
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
71 #endif
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
72 }
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
73
1532
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
74
1545
33d34bc4ac15 fix msvc warnings
jodogne
parents: 1534
diff changeset
75 #if UNIT_TESTS_WITH_HTTP_CONNEXIONS == 1 && ORTHANC_SSL_ENABLED == 1
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
76
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
77 /**
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
78 The HTTPS CA certificates for BitBucket were extracted as follows:
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
79
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
80 (1) We retrieve the certification chain of BitBucket:
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
81
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
82 # echo | openssl s_client -showcerts -connect www.bitbucket.org:443
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
83
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
84 (2) We see that the certification authority (CA) is
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
85 "www.digicert.com", and the root certificate is "DigiCert High
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
86 Assurance EV Root CA". As a consequence, we navigate to DigiCert to
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
87 find the URL to this CA certificate:
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
88
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
89 firefox https://www.digicert.com/digicert-root-certificates.htm
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
90
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
91 (3) Once we get the URL to the CA certificate, we convert it to a C
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
92 macro that can be used by libcurl:
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
93
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
94 # cd UnitTestsSources
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
95 # ../Resources/RetrieveCACertificates.py BITBUCKET_CERTIFICATES https://www.digicert.com/CACerts/DigiCertHighAssuranceEVRootCA.crt > BitbucketCACertificates.h
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
96 **/
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
97
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
98 #include "BitbucketCACertificates.h"
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
99
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1532
diff changeset
100 TEST(HttpClient, Ssl)
1532
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
101 {
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
102 Toolbox::WriteFile(BITBUCKET_CERTIFICATES, "UnitTestsResults/bitbucket.cert");
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
103
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
104 /*{
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
105 std::string s;
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
106 Toolbox::ReadFile(s, "/usr/share/ca-certificates/mozilla/WoSign.crt");
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
107 Toolbox::WriteFile(s, "UnitTestsResults/bitbucket.cert");
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
108 }*/
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
109
1532
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
110 HttpClient c;
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
111 c.SetHttpsVerifyPeers(true);
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
112 c.SetHttpsCACertificates("UnitTestsResults/bitbucket.cert");
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1532
diff changeset
113 c.SetUrl("https://bitbucket.org/sjodogne/orthanc/raw/Orthanc-0.9.3/Resources/Configuration.json");
1532
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
114
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
115 Json::Value v;
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
116 c.Apply(v);
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
117 ASSERT_TRUE(v.isMember("LuaScripts"));
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
118 }
1533
0011cc99443c improving HTTPS support
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1532
diff changeset
119
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
120 TEST(HttpClient, SslNoVerification)
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
121 {
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
122 HttpClient c;
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
123 c.SetHttpsVerifyPeers(false);
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
124 c.SetUrl("https://bitbucket.org/sjodogne/orthanc/raw/Orthanc-0.9.3/Resources/Configuration.json");
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
125
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
126 Json::Value v;
1532
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
127 c.Apply(v);
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
128 ASSERT_TRUE(v.isMember("LuaScripts"));
1532
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
129 }
1534
95b3b0260240 Options to validate peers against CA certificates in HTTPS requests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1533
diff changeset
130
1532
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
131 #endif
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
132
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1492
diff changeset
133
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
134 TEST(RestApi, ChunkedBuffer)
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
135 {
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
136 ChunkedBuffer b;
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
137 ASSERT_EQ(0u, b.GetNumBytes());
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
138
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
139 b.AddChunk("hello", 5);
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
140 ASSERT_EQ(5u, b.GetNumBytes());
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
141
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
142 b.AddChunk("world", 5);
1492
9f66a12eb8fc fix warnings for gcc 5
jodogne
parents: 1486
diff changeset
143 ASSERT_EQ(10u, b.GetNumBytes());
707
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
144
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
145 std::string s;
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
146 b.Flatten(s);
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
147 ASSERT_EQ("helloworld", s);
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
148 }
203157cb4fde unit tests of httpclient
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 632
diff changeset
149
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
150 TEST(RestApi, ParseCookies)
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
151 {
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
152 IHttpHandler::Arguments headers;
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
153 IHttpHandler::Arguments cookies;
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
154
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
155 headers["cookie"] = "a=b;c=d;;;e=f;;g=h;";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
156 HttpToolbox::ParseCookies(cookies, headers);
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
157 ASSERT_EQ(4u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
158 ASSERT_EQ("b", cookies["a"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
159 ASSERT_EQ("d", cookies["c"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
160 ASSERT_EQ("f", cookies["e"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
161 ASSERT_EQ("h", cookies["g"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
162
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
163 headers["cookie"] = " name = value ; name2=value2";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
164 HttpToolbox::ParseCookies(cookies, headers);
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
165 ASSERT_EQ(2u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
166 ASSERT_EQ("value", cookies["name"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
167 ASSERT_EQ("value2", cookies["name2"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
168
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
169 headers["cookie"] = " ;;; ";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
170 HttpToolbox::ParseCookies(cookies, headers);
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
171 ASSERT_EQ(0u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
172
332
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
173 headers["cookie"] = " ; n=v ;; ";
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
174 HttpToolbox::ParseCookies(cookies, headers);
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
175 ASSERT_EQ(1u, cookies.size());
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
176 ASSERT_EQ("v", cookies["n"]);
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
177 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 281
diff changeset
178
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 TEST(RestApi, RestApiPath)
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 {
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
181 IHttpHandler::Arguments args;
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 UriComponents trail;
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 RestApiPath uri("/coucou/{abc}/d/*");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/e/f/g"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 ASSERT_EQ(1u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 ASSERT_EQ(3u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 ASSERT_EQ("moi", args["abc"]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 ASSERT_EQ("e", trail[0]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 ASSERT_EQ("f", trail[1]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 ASSERT_EQ("g", trail[2]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/f"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d/"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 ASSERT_FALSE(uri.Match(args, trail, "/a/moi/d"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi"));
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
198
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
199 ASSERT_EQ(3u, uri.GetLevelCount());
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
200 ASSERT_TRUE(uri.IsUniversalTrailing());
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
201
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
202 ASSERT_EQ("coucou", uri.GetLevelName(0));
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
203 ASSERT_THROW(uri.GetWildcardName(0), OrthancException);
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
204
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
205 ASSERT_EQ("abc", uri.GetWildcardName(1));
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
206 ASSERT_THROW(uri.GetLevelName(1), OrthancException);
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
207
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
208 ASSERT_EQ("d", uri.GetLevelName(2));
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
209 ASSERT_THROW(uri.GetWildcardName(2), OrthancException);
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 RestApiPath uri("/coucou/{abc}/d");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 ASSERT_FALSE(uri.Match(args, trail, "/coucou/moi/d/e/f/g"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 ASSERT_TRUE(uri.Match(args, trail, "/coucou/moi/d"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 ASSERT_EQ(1u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 ASSERT_EQ(0u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 ASSERT_EQ("moi", args["abc"]);
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
219
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
220 ASSERT_EQ(3u, uri.GetLevelCount());
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
221 ASSERT_FALSE(uri.IsUniversalTrailing());
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
222
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
223 ASSERT_EQ("coucou", uri.GetLevelName(0));
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
224 ASSERT_THROW(uri.GetWildcardName(0), OrthancException);
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
225
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
226 ASSERT_EQ("abc", uri.GetWildcardName(1));
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
227 ASSERT_THROW(uri.GetLevelName(1), OrthancException);
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
228
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
229 ASSERT_EQ("d", uri.GetLevelName(2));
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
230 ASSERT_THROW(uri.GetWildcardName(2), OrthancException);
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 {
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 RestApiPath uri("/*");
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235 ASSERT_TRUE(uri.Match(args, trail, "/a/b/c"));
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 ASSERT_EQ(0u, args.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 ASSERT_EQ(3u, trail.size());
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238 ASSERT_EQ("a", trail[0]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 ASSERT_EQ("b", trail[1]);
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 ASSERT_EQ("c", trail[2]);
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
241
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
242 ASSERT_EQ(0u, uri.GetLevelCount());
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
243 ASSERT_TRUE(uri.IsUniversalTrailing());
209
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 }
9960642f0f45 abstraction of RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 }
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
246
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
247
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
248
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
249
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
250
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
251
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
252 static int testValue;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
253
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
254 template <int value>
974
83622b0f544c refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 970
diff changeset
255 static void SetValue(RestApiGetCall& get)
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
256 {
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
257 testValue = value;
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
258 }
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
259
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
260
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
261 static bool GetDirectory(Json::Value& target,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
262 RestApiHierarchy& hierarchy,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
263 const std::string& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
264 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
265 UriComponents p;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
266 Toolbox::SplitUriComponents(p, uri);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
267 return hierarchy.GetDirectory(target, p);
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
268 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
269
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
270
978
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
271
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
272 namespace
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
273 {
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
274 class MyVisitor : public RestApiHierarchy::IVisitor
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
275 {
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
276 public:
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
277 virtual bool Visit(const RestApiHierarchy::Resource& resource,
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
278 const UriComponents& uri,
1441
f3672356c121 refactoring: IHttpHandler and HttpToolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1419
diff changeset
279 const IHttpHandler::Arguments& components,
978
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
280 const UriComponents& trailing)
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
281 {
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
282 return resource.Handle(*reinterpret_cast<RestApiGetCall*>(NULL));
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
283 }
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
284 };
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
285 }
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
286
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
287
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
288 static bool HandleGet(RestApiHierarchy& hierarchy,
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
289 const std::string& uri)
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
290 {
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
291 UriComponents p;
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
292 Toolbox::SplitUriComponents(p, uri);
978
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
293 MyVisitor visitor;
ce3106e5843f refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 975
diff changeset
294 return hierarchy.LookupResource(p, visitor);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
295 }
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
296
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
297
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
298 TEST(RestApi, RestApiHierarchy)
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
299 {
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
300 RestApiHierarchy root;
970
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
301 root.Register("/hello/world/test", SetValue<1>);
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
302 root.Register("/hello/world/test2", SetValue<2>);
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
303 root.Register("/hello/{world}/test3/test4", SetValue<3>);
1a3817d84f39 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 969
diff changeset
304 root.Register("/hello2/*", SetValue<4>);
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
305
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
306 Json::Value m;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
307 root.CreateSiteMap(m);
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
308 std::cout << m;
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
309
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
310 Json::Value d;
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
311 ASSERT_FALSE(GetDirectory(d, root, "/hello"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
312
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
313 ASSERT_TRUE(GetDirectory(d, root, "/hello/a"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
314 ASSERT_EQ(1u, d.size());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
315 ASSERT_EQ("test3", d[0].asString());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
316
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
317 ASSERT_TRUE(GetDirectory(d, root, "/hello/world"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
318 ASSERT_EQ(2u, d.size());
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
319
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
320 ASSERT_TRUE(GetDirectory(d, root, "/hello/a/test3"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
321 ASSERT_EQ(1u, d.size());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
322 ASSERT_EQ("test4", d[0].asString());
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
323
1064
cd20e2568fc2 fix tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 978
diff changeset
324 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test"));
cd20e2568fc2 fix tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 978
diff changeset
325 ASSERT_TRUE(GetDirectory(d, root, "/hello/world/test2"));
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
326 ASSERT_FALSE(GetDirectory(d, root, "/hello2"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
327
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
328 testValue = 0;
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
329 ASSERT_TRUE(HandleGet(root, "/hello/world/test"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
330 ASSERT_EQ(testValue, 1);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
331 ASSERT_TRUE(HandleGet(root, "/hello/world/test2"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
332 ASSERT_EQ(testValue, 2);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
333 ASSERT_TRUE(HandleGet(root, "/hello/b/test3/test4"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
334 ASSERT_EQ(testValue, 3);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
335 ASSERT_FALSE(HandleGet(root, "/hello/b/test3/test"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
336 ASSERT_EQ(testValue, 3);
969
3dce528b0cc2 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 968
diff changeset
337 ASSERT_TRUE(HandleGet(root, "/hello2/a/b"));
968
8ed284e79850 RestApiHierarchy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 967
diff changeset
338 ASSERT_EQ(testValue, 4);
966
886652370ff2 accelerating REST API matching
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 831
diff changeset
339 }
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
340
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
341
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
342
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
343
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
344 namespace Orthanc
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
345 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
346 class HttpContentNegociation : public boost::noncopyable
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
347 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
348 public:
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
349 typedef std::map<std::string, std::string> HttpHeaders;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
350
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
351 class IHandler : public boost::noncopyable
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
352 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
353 public:
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
354 virtual ~IHandler()
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
355 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
356 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
357
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
358 virtual void Handle(const std::string& type,
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
359 const std::string& subtype) = 0;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
360 };
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
361
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
362 private:
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
363 struct Handler
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
364 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
365 std::string type_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
366 std::string subtype_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
367 IHandler& handler_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
368
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
369 Handler(const std::string& type,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
370 const std::string& subtype,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
371 IHandler& handler) :
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
372 type_(type),
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
373 subtype_(subtype),
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
374 handler_(handler)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
375 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
376 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
377
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
378 bool IsMatch(const std::string& type,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
379 const std::string& subtype) const
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
380 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
381 if (type == "*" && subtype == "*")
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
382 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
383 return true;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
384 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
385
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
386 if (subtype == "*" && type == type_)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
387 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
388 return true;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
389 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
390
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
391 return type == type_ && subtype == subtype_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
392 }
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
393
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
394 void Call() const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
395 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
396 handler_.Handle(type_, subtype_);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
397 }
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
398 };
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
399
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
400
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
401 struct Reference : public boost::noncopyable
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
402 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
403 const Handler& handler_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
404 uint8_t level_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
405 float quality_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
406
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
407 Reference(const Handler& handler,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
408 const std::string& type,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
409 const std::string& subtype,
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
410 float quality) :
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
411 handler_(handler),
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
412 quality_(quality)
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
413 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
414 if (type == "*" && subtype == "*")
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
415 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
416 level_ = 0;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
417 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
418 else if (subtype == "*")
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
419 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
420 level_ = 1;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
421 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
422 else
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
423 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
424 level_ = 2;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
425 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
426 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
427
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
428 bool operator< (const Reference& other) const
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
429 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
430 if (level_ < other.level_)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
431 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
432 return true;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
433 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
434
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
435 if (level_ > other.level_)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
436 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
437 return false;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
438 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
439
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
440 return quality_ < other.quality_;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
441 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
442 };
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
443
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
444
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
445 typedef std::vector<std::string> Tokens;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
446 typedef std::list<Handler> Handlers;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
447
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
448 Handlers handlers_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
449
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
450
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
451 static bool SplitPair(std::string& first /* out */,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
452 std::string& second /* out */,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
453 const std::string& source,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
454 char separator)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
455 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
456 size_t pos = source.find(separator);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
457
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
458 if (pos == std::string::npos)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
459 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
460 return false;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
461 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
462 else
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
463 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
464 first = Toolbox::StripSpaces(source.substr(0, pos));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
465 second = Toolbox::StripSpaces(source.substr(pos + 1));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
466 return true;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
467 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
468 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
469
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
470
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
471 static float GetQuality(const Tokens& parameters)
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
472 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
473 for (size_t i = 1; i < parameters.size(); i++)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
474 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
475 std::string key, value;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
476 if (SplitPair(key, value, parameters[i], '=') &&
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
477 key == "q")
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
478 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
479 float quality;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
480 bool ok = false;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
481
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
482 try
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
483 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
484 quality = boost::lexical_cast<float>(value);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
485 ok = (quality >= 0.0f && quality <= 1.0f);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
486 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
487 catch (boost::bad_lexical_cast&)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
488 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
489 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
490
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
491 if (ok)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
492 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
493 return quality;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
494 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
495 else
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
496 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
497 LOG(ERROR) << "Quality parameter out of range in a HTTP request (must be between 0 and 1): " << value;
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
498 throw OrthancException(ErrorCode_BadRequest);
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
499 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
500 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
501 }
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
502
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
503 return 1.0f; // Default quality
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
504 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
505
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
506
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
507 static void SelectBestMatch(std::auto_ptr<Reference>& best,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
508 const Handler& handler,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
509 const std::string& type,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
510 const std::string& subtype,
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
511 float quality)
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
512 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
513 std::auto_ptr<Reference> match(new Reference(handler, type, subtype, quality));
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
514
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
515 if (best.get() == NULL ||
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
516 *best < *match)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
517 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
518 best = match;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
519 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
520 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
521
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
522
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
523 public:
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
524 void Register(const std::string& mime,
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
525 IHandler& handler)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
526 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
527 std::string type, subtype;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
528
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
529 if (SplitPair(type, subtype, mime, '/') &&
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
530 type != "*" &&
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
531 subtype != "*")
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
532 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
533 handlers_.push_back(Handler(type, subtype, handler));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
534 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
535 else
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
536 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
537 throw OrthancException(ErrorCode_ParameterOutOfRange);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
538 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
539 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
540
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
541
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
542 bool Apply(const HttpHeaders& headers)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
543 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
544 HttpHeaders::const_iterator accept = headers.find("accept");
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
545 if (accept != headers.end())
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
546 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
547 return Apply(accept->second);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
548 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
549 else
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
550 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
551 return Apply("*/*");
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
552 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
553 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
554
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
555
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
556 bool Apply(const std::string& accept)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
557 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
558 // http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
559 // https://en.wikipedia.org/wiki/Content_negotiation
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
560 // http://www.newmediacampaigns.com/blog/browser-rest-http-accept-headers
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
561
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
562 Tokens mediaRanges;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
563 Toolbox::TokenizeString(mediaRanges, accept, ',');
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
564
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
565 std::auto_ptr<Reference> bestMatch;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
566
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
567 for (Tokens::const_iterator it = mediaRanges.begin();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
568 it != mediaRanges.end(); ++it)
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
569 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
570 Tokens parameters;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
571 Toolbox::TokenizeString(parameters, *it, ';');
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
572
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
573 if (parameters.size() > 0)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
574 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
575 float quality = GetQuality(parameters);
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
576
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
577 std::string type, subtype;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
578 if (SplitPair(type, subtype, parameters[0], '/'))
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
579 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
580 for (Handlers::const_iterator it2 = handlers_.begin();
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
581 it2 != handlers_.end(); ++it2)
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
582 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
583 if (it2->IsMatch(type, subtype))
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
584 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
585 SelectBestMatch(bestMatch, *it2, type, subtype, quality);
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
586 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
587 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
588 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
589 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
590 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
591
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
592 if (bestMatch.get() == NULL) // No match was found
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
593 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
594 return false;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
595 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
596 else
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
597 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
598 bestMatch->handler_.Call();
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
599 return true;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
600 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
601 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
602 };
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
603 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
604
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
605
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
606
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
607 namespace
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
608 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
609 class AcceptHandler : public Orthanc::HttpContentNegociation::IHandler
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
610 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
611 private:
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
612 std::string type_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
613 std::string subtype_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
614
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
615 public:
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
616 AcceptHandler()
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
617 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
618 Reset();
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
619 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
620
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
621 void Reset()
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
622 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
623 Handle("nope", "nope");
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
624 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
625
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
626 const std::string& GetType() const
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
627 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
628 return type_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
629 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
630
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
631 const std::string& GetSubType() const
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
632 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
633 return subtype_;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
634 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
635
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
636 virtual void Handle(const std::string& type,
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
637 const std::string& subtype)
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
638 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
639 type_ = type;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
640 subtype_ = subtype;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
641 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
642 };
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
643 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
644
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
645
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
646 TEST(RestApi, HttpContentNegociation)
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
647 {
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
648 // Reference: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
649
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
650 AcceptHandler h;
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
651
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
652 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
653 Orthanc::HttpContentNegociation d;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
654 d.Register("audio/mp3", h);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
655 d.Register("audio/basic", h);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
656
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
657 ASSERT_TRUE(d.Apply("audio/*; q=0.2, audio/basic"));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
658 ASSERT_EQ("audio", h.GetType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
659 ASSERT_EQ("basic", h.GetSubType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
660
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
661 ASSERT_TRUE(d.Apply("audio/*; q=0.2, audio/nope"));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
662 ASSERT_EQ("audio", h.GetType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
663 ASSERT_EQ("mp3", h.GetSubType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
664
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
665 ASSERT_FALSE(d.Apply("application/*; q=0.2, application/pdf"));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
666
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
667 ASSERT_TRUE(d.Apply("*/*; application/*; q=0.2, application/pdf"));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
668 ASSERT_EQ("audio", h.GetType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
669 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
670
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
671 // "This would be interpreted as "text/html and text/x-c are the
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
672 // preferred media types, but if they do not exist, then send the
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
673 // text/x-dvi entity, and if that does not exist, send the
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
674 // text/plain entity.""
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
675 const std::string T1 = "text/plain; q=0.5, text/html, text/x-dvi; q=0.8, text/x-c";
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
676
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
677 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
678 Orthanc::HttpContentNegociation d;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
679 d.Register("text/plain", h);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
680 d.Register("text/html", h);
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
681 d.Register("text/x-dvi", h);
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
682 ASSERT_TRUE(d.Apply(T1));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
683 ASSERT_EQ("text", h.GetType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
684 ASSERT_EQ("html", h.GetSubType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
685 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
686
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
687 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
688 Orthanc::HttpContentNegociation d;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
689 d.Register("text/plain", h);
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
690 d.Register("text/x-dvi", h);
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
691 d.Register("text/x-c", h);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
692 ASSERT_TRUE(d.Apply(T1));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
693 ASSERT_EQ("text", h.GetType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
694 ASSERT_EQ("x-c", h.GetSubType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
695 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
696
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
697 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
698 Orthanc::HttpContentNegociation d;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
699 d.Register("text/plain", h);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
700 d.Register("text/x-dvi", h);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
701 d.Register("text/x-c", h);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
702 d.Register("text/html", h);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
703 ASSERT_TRUE(d.Apply(T1));
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
704 ASSERT_EQ("text", h.GetType());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
705 ASSERT_TRUE(h.GetSubType() == "x-c" || h.GetSubType() == "html");
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
706 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
707
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
708 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
709 Orthanc::HttpContentNegociation d;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
710 d.Register("text/plain", h);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
711 d.Register("text/x-dvi", h);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
712 ASSERT_TRUE(d.Apply(T1));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
713 ASSERT_EQ("text", h.GetType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
714 ASSERT_EQ("x-dvi", h.GetSubType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
715 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
716
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
717 {
1782
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1781
diff changeset
718 Orthanc::HttpContentNegociation d;
1781
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
719 d.Register("text/plain", h);
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
720 ASSERT_TRUE(d.Apply(T1));
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
721 ASSERT_EQ("text", h.GetType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
722 ASSERT_EQ("plain", h.GetSubType());
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
723 }
5ad4e4d92ecb AcceptMediaDispatcher bootstrap
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1545
diff changeset
724 }