annotate OrthancFramework/Sources/HttpServer/HttpOutput.cpp @ 5594:a906dc19264c find-refactoring

created FindResponse::Resource::Format()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 04 May 2024 15:25:19 +0200
parents dca738d7846b
children 1b31ab38ea94
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1882
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1200
diff changeset
4 * Department, University Hospital of Liege, Belgium
5485
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5469
diff changeset
5 * Copyright (C) 2017-2024 Osimis S.A., Belgium
48b8dae6dc77 upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5469
diff changeset
6 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
11 * the License, or (at your option) any later version.
136
fe180eae201d openssl exception
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 59
diff changeset
12 *
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
16 * Lesser General Public License for more details.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
19 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4044
diff changeset
20 * <http://www.gnu.org/licenses/>.
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
824
a811bdf8b8eb precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 689
diff changeset
24 #include "../PrecompiledHeaders.h"
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "HttpOutput.h"
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
3380
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
27 #include "../ChunkedBuffer.h"
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
28 #include "../Compression/GzipCompressor.h"
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
29 #include "../Compression/ZlibCompressor.h"
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1430
diff changeset
30 #include "../Logging.h"
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1430
diff changeset
31 #include "../OrthancException.h"
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1430
diff changeset
32 #include "../Toolbox.h"
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
33 #include "../SystemToolbox.h"
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1430
diff changeset
34
324
64925c94825c api improvement
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
35 #include <iostream>
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include <vector>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 #include <stdio.h>
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include <boost/lexical_cast.hpp>
1486
f967bdf8534e refactoring to Logging.h
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1430
diff changeset
39
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
3178
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
41 #if ORTHANC_ENABLE_CIVETWEB == 1
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
42 # if !defined(CIVETWEB_HAS_DISABLE_KEEP_ALIVE)
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
43 # error Macro CIVETWEB_HAS_DISABLE_KEEP_ALIVE must be defined
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
44 # endif
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
45 #endif
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
46
5469
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
47 static const std::string X_CONTENT_TYPE_OPTIONS = "X-Content-Type-Options";
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
48
3178
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
49
59
c996319e90bc renaming in Core
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 50
diff changeset
50 namespace Orthanc
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 {
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
52 HttpOutput::StateMachine::StateMachine(IHttpOutputStream& stream,
5119
bdec57f3cbf2 New configuration KeepAliveTimeout with a default value of 1 second
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
53 bool isKeepAlive,
bdec57f3cbf2 New configuration KeepAliveTimeout with a default value of 1 second
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
54 unsigned int keepAliveTimeout) :
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
55 stream_(stream),
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
56 state_(State_WritingHeader),
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
57 isContentCompressible_(false),
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
58 status_(HttpStatus_200_Ok),
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
59 hasContentLength_(false),
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
60 contentLength_(0),
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
61 contentPosition_(0),
5119
bdec57f3cbf2 New configuration KeepAliveTimeout with a default value of 1 second
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
62 keepAlive_(isKeepAlive),
5469
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
63 keepAliveTimeout_(keepAliveTimeout),
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
64 hasXContentTypeOptions_(false)
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
65 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
66 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
67
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
68 HttpOutput::StateMachine::~StateMachine()
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
69 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
70 if (state_ != State_Done)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
71 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
72 //asm volatile ("int3;");
1200
1e1390665639 fix sample
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1115
diff changeset
73 //LOG(ERROR) << "This HTTP answer does not contain any body";
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
74 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
75
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
76 if (hasContentLength_ && contentPosition_ != contentLength_)
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
77 {
5547
dca738d7846b clarified error message
Alain Mazy <am@osimis.io>
parents: 5485
diff changeset
78 LOG(ERROR) << "This HTTP answer has not sent the proper number of bytes in its body. The remote client has likely closed the connection.";
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
79 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
80 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
81
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
82
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
83 void HttpOutput::StateMachine::SetHttpStatus(HttpStatus status)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
84 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
85 if (state_ != State_WritingHeader)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
86 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
87 throw OrthancException(ErrorCode_BadSequenceOfCalls);
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
88 }
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
89
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
90 status_ = status;
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
91 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
92
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
93
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
94 void HttpOutput::StateMachine::SetContentLength(uint64_t length)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
95 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
96 if (state_ != State_WritingHeader)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
97 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
98 throw OrthancException(ErrorCode_BadSequenceOfCalls);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
99 }
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
100
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
101 hasContentLength_ = true;
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
102 contentLength_ = length;
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
103 }
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
104
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
105 void HttpOutput::StateMachine::SetContentType(const char* contentType)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
106 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
107 AddHeader("Content-Type", contentType);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
108 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
109
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
110 void HttpOutput::StateMachine::SetContentCompressible(bool isContentCompressible)
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
111 {
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
112 isContentCompressible_ = isContentCompressible;
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
113 }
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
114
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
115 bool HttpOutput::StateMachine::IsContentCompressible() const
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
116 {
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
117 // We assume that all files that compress correctly (mainly JSON, XML) are clearly identified.
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
118 return isContentCompressible_;
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
119 }
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
120
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
121 void HttpOutput::StateMachine::SetContentFilename(const char* filename)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
122 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
123 // TODO Escape double quotes
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
124 AddHeader("Content-Disposition", "filename=\"" + std::string(filename) + "\"");
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
125 }
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
126
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
127 void HttpOutput::StateMachine::SetCookie(const std::string& cookie,
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
128 const std::string& value)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
129 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
130 if (state_ != State_WritingHeader)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
131 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
132 throw OrthancException(ErrorCode_BadSequenceOfCalls);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
133 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
134
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
135 // TODO Escape "=" characters
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
136 AddHeader("Set-Cookie", cookie + "=" + value);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
137 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
138
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
139
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
140 void HttpOutput::StateMachine::AddHeader(const std::string& header,
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
141 const std::string& value)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
142 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
143 if (state_ != State_WritingHeader)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
144 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
145 throw OrthancException(ErrorCode_BadSequenceOfCalls);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
146 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
147
5469
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
148 if (header == X_CONTENT_TYPE_OPTIONS)
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
149 {
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
150 hasXContentTypeOptions_ = true;
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
151 }
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
152
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
153 headers_.push_back(header + ": " + value + "\r\n");
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
154 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
155
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
156 void HttpOutput::StateMachine::ClearHeaders()
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
157 {
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
158 if (state_ != State_WritingHeader)
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
159 {
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
160 throw OrthancException(ErrorCode_BadSequenceOfCalls);
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
161 }
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
162
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
163 headers_.clear();
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
164 }
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
165
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
166 void HttpOutput::StateMachine::SendBody(const void* buffer, size_t length)
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
167 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
168 if (state_ == State_Done)
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
169 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
170 if (length == 0)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
171 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
172 return;
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
173 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
174 else
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
175 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
176 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
177 "Because of keep-alive connections, the entire body must "
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
178 "be sent at once or Content-Length must be given");
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
179 }
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
180 }
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
181
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
182 if (state_ == State_WritingMultipart)
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
183 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
184 throw OrthancException(ErrorCode_InternalError);
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
185 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
186
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
187 if (state_ == State_WritingHeader)
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
188 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
189 // Send the HTTP header before writing the body
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
190
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
191 stream_.OnHttpStatusReceived(status_);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
192
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
193 std::string s = "HTTP/1.1 " +
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
194 boost::lexical_cast<std::string>(status_) +
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
195 " " + std::string(EnumerationToString(status_)) +
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
196 "\r\n";
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
197
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
198 if (keepAlive_)
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
199 {
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
200 s += "Connection: keep-alive\r\n";
4301
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
201
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
202 /**
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
203 * [LIFY-2311] The "Keep-Alive" HTTP header was missing in
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
204 * Orthanc <= 1.8.0, which notably caused failures if
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
205 * uploading DICOM instances by applying Java's
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
206 * "org.apache.http.client.methods.HttpPost()" on "/instances"
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
207 * URI, if "PoolingHttpClientConnectionManager" was in used. A
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
208 * workaround was to manually set a timeout for the keep-alive
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
209 * client to, say, 200 milliseconds, by using
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
210 * "HttpClients.custom().setKeepAliveStrategy((httpResponse,httpContext)->200)".
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
211 * Note that the "timeout" value can only be integer in the
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
212 * HTTP header, so we can't use the milliseconds granularity.
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
213 **/
6919242d2265 Fix keep-alive in the embedded HTTP server by setting the "Keep-Alive" HTTP header
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4298
diff changeset
214 s += ("Keep-Alive: timeout=" +
5119
bdec57f3cbf2 New configuration KeepAliveTimeout with a default value of 1 second
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
215 boost::lexical_cast<std::string>(keepAliveTimeout_) + "\r\n");
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
216 }
3171
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
217 else
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
218 {
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
219 s += "Connection: close\r\n";
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
220 }
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
221
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
222 for (std::list<std::string>::const_iterator
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
223 it = headers_.begin(); it != headers_.end(); ++it)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
224 {
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
225 s += *it;
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
226 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
227
5469
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
228 if (!hasXContentTypeOptions_)
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
229 {
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
230 // Always include this header to prevent MIME Confusion attacks:
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
231 // https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#x-content-type-options
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
232 s += X_CONTENT_TYPE_OPTIONS + ": nosniff\r\n";
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
233 }
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
234
1115
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
235 if (status_ != HttpStatus_200_Ok)
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
236 {
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
237 hasContentLength_ = false;
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
238 }
da56a7916e8a Experimental "KeepAlive" configuration option to enable HTTP Keep-Alive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1113
diff changeset
239
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
240 uint64_t contentLength = (hasContentLength_ ? contentLength_ : length);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
241 s += "Content-Length: " + boost::lexical_cast<std::string>(contentLength) + "\r\n\r\n";
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
242
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
243 stream_.Send(true, s.c_str(), s.size());
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
244 state_ = State_WritingBody;
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
245 }
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
246
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
247 if (hasContentLength_ &&
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
248 contentPosition_ + length > contentLength_)
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
249 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
250 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
251 "The body size exceeds what was declared with SetContentSize()");
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
252 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
253
910
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
254 if (length > 0)
28a52982196e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 908
diff changeset
255 {
911
306afd58a0b3 fix build
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 910
diff changeset
256 stream_.Send(false, buffer, length);
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
257 contentPosition_ += length;
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 324
diff changeset
258 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 324
diff changeset
259
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
260 if (!hasContentLength_ ||
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
261 contentPosition_ == contentLength_)
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 324
diff changeset
262 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
263 state_ = State_Done;
330
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 324
diff changeset
264 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 324
diff changeset
265 }
78a8eaa5f30b cookies
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 324
diff changeset
266
207
7f74209ea0f8 RestApi
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 155
diff changeset
267
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
268 void HttpOutput::StateMachine::CloseBody()
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
269 {
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
270 switch (state_)
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
271 {
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
272 case State_WritingHeader:
1522
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1521
diff changeset
273 SetContentLength(0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1521
diff changeset
274 SendBody(NULL, 0);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1521
diff changeset
275 break;
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
276
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
277 case State_WritingBody:
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
278 if (!hasContentLength_ ||
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
279 contentPosition_ == contentLength_)
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
280 {
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
281 state_ = State_Done;
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
282 }
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
283 else
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
284 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
285 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
286 "The body size has not reached what was declared with SetContentSize()");
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
287 }
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
288
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
289 break;
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
290
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
291 case State_WritingMultipart:
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
292 throw OrthancException(ErrorCode_BadSequenceOfCalls,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
293 "Cannot invoke CloseBody() with multipart outputs");
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
294
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
295 case State_Done:
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
296 return; // Ignore
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
297
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
298 default:
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
299 throw OrthancException(ErrorCode_InternalError);
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
300 }
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
301 }
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
302
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
303
1517
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
304 HttpCompression HttpOutput::GetPreferredCompression(size_t bodySize) const
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
305 {
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
306 // Do not compress small files since there is no real size benefit.
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
307 if (bodySize < 2048)
1517
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
308 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
309 return HttpCompression_None;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
310 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
311
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
312 // Prefer "gzip" over "deflate" if the choice is offered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
313
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
314 if (isGzipAllowed_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
315 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
316 return HttpCompression_Gzip;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
317 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
318 else if (isDeflateAllowed_)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
319 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
320 return HttpCompression_Deflate;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
321 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
322 else
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
323 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
324 return HttpCompression_None;
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
325 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
326 }
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
327
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1514
diff changeset
328
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
329 HttpOutput::HttpOutput(IHttpOutputStream &stream,
5119
bdec57f3cbf2 New configuration KeepAliveTimeout with a default value of 1 second
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
330 bool isKeepAlive,
bdec57f3cbf2 New configuration KeepAliveTimeout with a default value of 1 second
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
331 unsigned int keepAliveTimeout) :
bdec57f3cbf2 New configuration KeepAliveTimeout with a default value of 1 second
Alain Mazy <am@osimis.io>
parents: 4870
diff changeset
332 stateMachine_(stream, isKeepAlive, keepAliveTimeout),
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
333 isDeflateAllowed_(false),
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
334 isGzipAllowed_(false)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
335 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
336 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
337
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
338 void HttpOutput::SetDeflateAllowed(bool allowed)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
339 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
340 isDeflateAllowed_ = allowed;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
341 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
342
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
343 bool HttpOutput::IsDeflateAllowed() const
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
344 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
345 return isDeflateAllowed_;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
346 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
347
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
348 void HttpOutput::SetGzipAllowed(bool allowed)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
349 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
350 isGzipAllowed_ = allowed;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
351 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
352
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
353 bool HttpOutput::IsGzipAllowed() const
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
354 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
355 return isGzipAllowed_;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
356 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
357
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
358
1042
8d1845feb277 set cookies, not allowed methods, unauthorized in plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 911
diff changeset
359 void HttpOutput::SendMethodNotAllowed(const std::string& allowed)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
361 stateMachine_.ClearHeaders();
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
362 stateMachine_.SetHttpStatus(HttpStatus_405_MethodNotAllowed);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
363 stateMachine_.AddHeader("Allow", allowed);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
364 stateMachine_.SendBody(NULL, 0);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367
1567
9c5d93510414 If error while calling the REST API, the answer body contains an error description
jodogne
parents: 1523
diff changeset
368 void HttpOutput::SendStatus(HttpStatus status,
5462
505416b269a0 Fix XSS in Orthanc error reporting (as reported by Sébastien Doria, Vumetric Cybersecurity)
Alain Mazy <am@osimis.io>
parents: 5432
diff changeset
369 const char* message,
505416b269a0 Fix XSS in Orthanc error reporting (as reported by Sébastien Doria, Vumetric Cybersecurity)
Alain Mazy <am@osimis.io>
parents: 5432
diff changeset
370 size_t messageSize)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371 {
1954
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1953
diff changeset
372 if (status == HttpStatus_301_MovedPermanently ||
3425
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3416
diff changeset
373 //status == HttpStatus_401_Unauthorized ||
473
c9a5d72f8481 changing the namespace of HTTP enumerations
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 398
diff changeset
374 status == HttpStatus_405_MethodNotAllowed)
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 {
2954
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
376 throw OrthancException(ErrorCode_ParameterOutOfRange,
d924f9bb61cc taking advantage of details in OrthancException
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2908
diff changeset
377 "Please use the dedicated methods to this HTTP status code in HttpOutput");
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378 }
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
380 stateMachine_.SetHttpStatus(status);
5462
505416b269a0 Fix XSS in Orthanc error reporting (as reported by Sébastien Doria, Vumetric Cybersecurity)
Alain Mazy <am@osimis.io>
parents: 5432
diff changeset
381
505416b269a0 Fix XSS in Orthanc error reporting (as reported by Sébastien Doria, Vumetric Cybersecurity)
Alain Mazy <am@osimis.io>
parents: 5432
diff changeset
382 if (messageSize > 0)
505416b269a0 Fix XSS in Orthanc error reporting (as reported by Sébastien Doria, Vumetric Cybersecurity)
Alain Mazy <am@osimis.io>
parents: 5432
diff changeset
383 {
5469
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
384 // Assume that the body always contains a textual description of the error
514fd39f87a8 improved handling of X-Content-Type-Options
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5463
diff changeset
385 stateMachine_.SetContentType("text/plain");
5462
505416b269a0 Fix XSS in Orthanc error reporting (as reported by Sébastien Doria, Vumetric Cybersecurity)
Alain Mazy <am@osimis.io>
parents: 5432
diff changeset
386 }
505416b269a0 Fix XSS in Orthanc error reporting (as reported by Sébastien Doria, Vumetric Cybersecurity)
Alain Mazy <am@osimis.io>
parents: 5432
diff changeset
387
1645
1558b3226b18 IHttpExceptionFormatter
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1592
diff changeset
388 stateMachine_.SendBody(message, messageSize);
339
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
389 }
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
390
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
391 void HttpOutput::SendStatus(HttpStatus status)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
392 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
393 SendStatus(status, NULL, 0);
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
394 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
395
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
396 void HttpOutput::SendStatus(HttpStatus status, const std::string &message)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
397 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
398 SendStatus(status, message.c_str(), message.size());
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
399 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
400
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
401 void HttpOutput::SetContentType(MimeType contentType)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
402 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
403 stateMachine_.SetContentType(EnumerationToString(contentType));
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
404 stateMachine_.SetContentCompressible(SystemToolbox::IsContentCompressible(contentType));
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
405 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
406
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
407 void HttpOutput::SetContentType(const std::string &contentType)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
408 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
409 stateMachine_.SetContentType(contentType.c_str());
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
410 stateMachine_.SetContentCompressible(SystemToolbox::IsContentCompressible(contentType));
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
411 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
412
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
413 void HttpOutput::SetContentFilename(const char *filename)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
414 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
415 stateMachine_.SetContentFilename(filename);
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
416 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
417
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
418 void HttpOutput::SetCookie(const std::string &cookie, const std::string &value)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
419 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
420 stateMachine_.SetCookie(cookie, value);
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
421 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
422
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
423 void HttpOutput::AddHeader(const std::string &key, const std::string &value)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
424 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
425 stateMachine_.AddHeader(key, value);
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
426 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
427
339
639272ef7615 answer raw buffers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 330
diff changeset
428
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 void HttpOutput::Redirect(const std::string& path)
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
431 stateMachine_.ClearHeaders();
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
432 stateMachine_.SetHttpStatus(HttpStatus_301_MovedPermanently);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
433 stateMachine_.AddHeader("Location", path);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
434 stateMachine_.SendBody(NULL, 0);
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 }
908
e078ea944089 refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
436
e078ea944089 refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
437
e078ea944089 refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
438 void HttpOutput::SendUnauthorized(const std::string& realm)
e078ea944089 refactoring HttpOutput
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 824
diff changeset
439 {
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
440 stateMachine_.ClearHeaders();
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
441 stateMachine_.SetHttpStatus(HttpStatus_401_Unauthorized);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
442 stateMachine_.AddHeader("WWW-Authenticate", "Basic realm=\"" + realm + "\"");
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
443 stateMachine_.SendBody(NULL, 0);
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
444 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
445
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
446 void HttpOutput::StartMultipart(const std::string &subType, const std::string &contentType)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
447 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
448 stateMachine_.StartMultipart(subType, contentType);
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
449 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
450
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
451 void HttpOutput::SendMultipartItem(const void *item,
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
452 size_t size,
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
453 const std::map<std::string, std::string> &headers)
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
454 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
455 stateMachine_.SendMultipartItem(item, size, headers);
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
456 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
457
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
458 void HttpOutput::CloseMultipart()
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
459 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
460 stateMachine_.CloseMultipart();
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
461 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
462
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
463 bool HttpOutput::IsWritingMultipart() const
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
464 {
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
465 return stateMachine_.GetState() == StateMachine::State_WritingMultipart;
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
466 }
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
467
2908
9d277f8ad698 new enumeration: MimeType
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2905
diff changeset
468
4298
db3932f9660d abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
469 void HttpOutput::Answer(const void* buffer,
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
470 size_t length)
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
471 {
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
472 if (length == 0)
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
473 {
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
474 AnswerEmpty();
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
475 return;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
476 }
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
477
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
478 HttpCompression compression = GetPreferredCompression(length);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
479
5407
3206537cbb56 HttpCompression: now disabled by default + only compress known compressible content types
Alain Mazy <am@osimis.io>
parents: 5185
diff changeset
480 if (compression == HttpCompression_None || !IsContentCompressible())
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
481 {
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
482 stateMachine_.SetContentLength(length);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
483 stateMachine_.SendBody(buffer, length);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
484 return;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
485 }
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
486
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
487 std::string compressed, encoding;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
488
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
489 switch (compression)
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
490 {
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
491 case HttpCompression_Deflate:
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
492 {
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
493 encoding = "deflate";
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
494 ZlibCompressor compressor;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
495 // Do not prefix the buffer with its uncompressed size, to be compatible with "deflate"
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
496 compressor.SetPrefixWithUncompressedSize(false);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
497 compressor.Compress(compressed, buffer, length);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
498 break;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
499 }
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
500
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
501 case HttpCompression_Gzip:
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
502 {
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
503 encoding = "gzip";
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
504 GzipCompressor compressor;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
505 compressor.Compress(compressed, buffer, length);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
506 break;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
507 }
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
508
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
509 default:
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
510 throw OrthancException(ErrorCode_InternalError);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
511 }
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
512
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
513 LOG(TRACE) << "Compressing a HTTP answer using " << encoding;
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
514
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
515 // The body is empty, do not use HTTP compression
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
516 if (compressed.size() == 0)
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
517 {
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
518 AnswerEmpty();
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
519 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
520 else
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
521 {
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
522 stateMachine_.AddHeader("Content-Encoding", encoding);
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
523 stateMachine_.SetContentLength(compressed.size());
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
524 stateMachine_.SendBody(compressed.c_str(), compressed.size());
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
525 }
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
526
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
527 stateMachine_.CloseBody();
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
528 }
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
529
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
530
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
531 void HttpOutput::Answer(const std::string& str)
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
532 {
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
533 Answer(str.size() == 0 ? NULL : str.c_str(), str.size());
1511
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
534 }
7962563129c9 starting support of deflate/gzip content types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1486
diff changeset
535
1521
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
536
3606278d305e refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1519
diff changeset
537 void HttpOutput::AnswerEmpty()
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
538 {
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
539 stateMachine_.CloseBody();
1113
ba5c0908600c Refactoring of HttpOutput ("Content-Length" header is now always sent)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1042
diff changeset
540 }
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
541
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
542
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
543 void HttpOutput::StateMachine::CheckHeadersCompatibilityWithMultipart() const
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
544 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
545 for (std::list<std::string>::const_iterator
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
546 it = headers_.begin(); it != headers_.end(); ++it)
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
547 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
548 if (!Toolbox::StartsWith(*it, "Set-Cookie: "))
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
549 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
550 throw OrthancException(ErrorCode_BadSequenceOfCalls,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
551 "The only headers that can be set in multipart answers "
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
552 "are Set-Cookie (here: " + *it + " is set)");
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
553 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
554 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
555 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
556
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
557
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
558 static void PrepareMultipartMainHeader(std::string& boundary,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
559 std::string& contentTypeHeader,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
560 const std::string& subType,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
561 const std::string& contentType)
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
562 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
563 if (subType != "mixed" &&
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
564 subType != "related")
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
565 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
566 throw OrthancException(ErrorCode_ParameterOutOfRange);
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
567 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
568
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
569 /**
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
570 * Fix for issue 54 ("Decide what to do wrt. quoting of multipart
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
571 * answers"). The "type" parameter in the "Content-Type" HTTP
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
572 * header must be quoted if it contains a forward slash "/". This
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
573 * is necessary for DICOMweb compatibility with OsiriX, but breaks
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
574 * compatibility with old releases of the client in the Orthanc
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
575 * DICOMweb plugin <= 0.3 (releases >= 0.4 work fine).
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
576 *
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
577 * Full history is available at the following locations:
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
578 * - In changeset 2248:69b0f4e8a49b:
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
579 * # hg history -v -r 2248
5432
59e3b6f8c5be migration to UCLouvain servers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5407
diff changeset
580 * - https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=54
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
581 * - https://groups.google.com/d/msg/orthanc-users/65zhIM5xbKI/TU5Q1_LhAwAJ
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
582 **/
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
583 std::string tmp;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
584 if (contentType.find('/') == std::string::npos)
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
585 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
586 // No forward slash in the content type
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
587 tmp = contentType;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
588 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
589 else
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
590 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
591 // Quote the content type because of the forward slash
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
592 tmp = "\"" + contentType + "\"";
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
593 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
594
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
595 boundary = Toolbox::GenerateUuid() + "-" + Toolbox::GenerateUuid();
3616
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
596
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
597 /**
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
598 * Fix for issue #165: "Encapsulation boundaries must not appear
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
599 * within the encapsulations, and must be no longer than 70
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
600 * characters, not counting the two leading hyphens."
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
601 * https://tools.ietf.org/html/rfc1521
5432
59e3b6f8c5be migration to UCLouvain servers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 5407
diff changeset
602 * https://orthanc.uclouvain.be/bugs/show_bug.cgi?id=165
3616
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
603 **/
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
604 if (boundary.size() != 36 + 1 + 36) // one UUID contains 36 characters
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
605 {
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
606 throw OrthancException(ErrorCode_InternalError);
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
607 }
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
608
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
609 boundary = boundary.substr(0, 70);
b6a569e6e85b Fix issue #165
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3528
diff changeset
610
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
611 contentTypeHeader = ("multipart/" + subType + "; type=" + tmp + "; boundary=" + boundary);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
612 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
613
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
614
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
615 void HttpOutput::StateMachine::StartStreamInternal(const std::string& contentType)
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
616 {
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
617 if (state_ != State_WritingHeader)
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
618 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
619 throw OrthancException(ErrorCode_BadSequenceOfCalls);
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
620 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
621
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
622 if (status_ != HttpStatus_200_Ok)
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
623 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
624 SendBody(NULL, 0);
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
625 return;
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
626 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
627
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
628 stream_.OnHttpStatusReceived(status_);
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
629
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
630 std::string header = "HTTP/1.1 200 OK\r\n";
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
631
3154
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
632 if (keepAlive_)
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
633 {
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
634 #if ORTHANC_ENABLE_MONGOOSE == 1
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
635 throw OrthancException(ErrorCode_NotImplemented,
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
636 "Multipart answers are not implemented together "
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
637 "with keep-alive connections if using Mongoose");
3178
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
638
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
639 #elif ORTHANC_ENABLE_CIVETWEB == 1
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
640 # if CIVETWEB_HAS_DISABLE_KEEP_ALIVE == 1
3154
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
641 // Turn off Keep-Alive for multipart answers
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
642 // https://github.com/civetweb/civetweb/issues/727
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
643 stream_.DisableKeepAlive();
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
644 header += "Connection: close\r\n";
3178
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
645 # else
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
646 // The function "mg_disable_keep_alive()" is not available,
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
647 // let's continue with Keep-Alive. Performance of WADO-RS will
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
648 // decrease.
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
649 header += "Connection: keep-alive\r\n";
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
650 # endif
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
651
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
652 #else
6d558598d713 Fix build with unpatched versions of Civetweb (missing "mg_disable_keep_alive()")
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3171
diff changeset
653 # error Please support your embedded Web server here
3154
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
654 #endif
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
655 }
3171
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
656 else
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
657 {
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
658 header += "Connection: close\r\n";
81cd9a4f3018 always add HTTP header "Connection: close" if keep-alive is disabled
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3154
diff changeset
659 }
3154
6e8822be2f08 Fix compatibility with DICOMweb plugin (allow multipart answers over HTTP Keep-Alive)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3060
diff changeset
660
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
661 for (std::list<std::string>::const_iterator
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
662 it = headers_.begin(); it != headers_.end(); ++it)
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
663 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
664 header += *it;
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
665 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
666
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
667 header += ("Content-Type: " + contentType + "\r\n\r\n");
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
668
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
669 stream_.Send(true, header.c_str(), header.size());
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
670 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
671
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
673 void HttpOutput::StateMachine::StartMultipart(const std::string& subType,
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
674 const std::string& contentType)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
675 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
676 CheckHeadersCompatibilityWithMultipart();
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
677
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
678 std::string contentTypeHeader;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
679 PrepareMultipartMainHeader(multipartBoundary_, contentTypeHeader, subType, contentType);
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
680 multipartContentType_ = contentType;
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
681
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
682 StartStreamInternal(contentTypeHeader);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
683
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
684 state_ = State_WritingMultipart;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
685 }
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
686
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
687
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
688 void HttpOutput::StateMachine::StartStream(const std::string& contentType)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
689 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
690 StartStreamInternal(contentType);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
691 state_ = State_WritingStream;
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
692 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
693
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
694
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
695 static void PrepareMultipartItemHeader(std::string& target,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
696 size_t length,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
697 const std::map<std::string, std::string>& headers,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
698 const std::string& boundary,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
699 const std::string& contentType)
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
700 {
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
701 target = "--" + boundary + "\r\n";
1882
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
702
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
703 bool hasContentType = false;
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
704 bool hasContentLength = false;
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
705 bool hasMimeVersion = false;
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
706
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
707 for (std::map<std::string, std::string>::const_iterator
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
708 it = headers.begin(); it != headers.end(); ++it)
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
709 {
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
710 target += it->first + ": " + it->second + "\r\n";
1882
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
711
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
712 std::string tmp;
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
713 Toolbox::ToLowerCase(tmp, it->first);
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
714
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
715 if (tmp == "content-type")
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
716 {
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
717 hasContentType = true;
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
718 }
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
719
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
720 if (tmp == "content-length")
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
721 {
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
722 hasContentLength = true;
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
723 }
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
724
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
725 if (tmp == "mime-version")
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
726 {
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
727 hasMimeVersion = true;
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
728 }
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
729 }
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
730
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
731 if (!hasContentType)
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
732 {
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
733 target += "Content-Type: " + contentType + "\r\n";
1882
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
734 }
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
735
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
736 if (!hasContentLength)
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
737 {
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
738 target += "Content-Length: " + boost::lexical_cast<std::string>(length) + "\r\n";
1882
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
739 }
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
740
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
741 if (!hasMimeVersion)
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
742 {
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
743 target += "MIME-Version: 1.0\r\n\r\n";
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
744 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
745 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
746
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
747
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
748 void HttpOutput::StateMachine::SendMultipartItem(const void* item,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
749 size_t length,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
750 const std::map<std::string, std::string>& headers)
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
751 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
752 if (state_ != State_WritingMultipart)
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
753 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
754 throw OrthancException(ErrorCode_BadSequenceOfCalls);
1882
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
755 }
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
756
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
757 std::string header;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
758 PrepareMultipartItemHeader(header, length, headers, multipartBoundary_, multipartContentType_);
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
759 stream_.Send(false, header.c_str(), header.size());
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
760
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
761 if (length > 0)
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
762 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
763 stream_.Send(false, item, length);
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
764 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
765
1882
5cf2bd0abfa2 OrthancPluginSendMultipartItem2 for DICOMweb
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1875
diff changeset
766 stream_.Send(false, "\r\n", 2);
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
767 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
768
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
769
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
770 void HttpOutput::StateMachine::CloseMultipart()
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
771 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
772 if (state_ != State_WritingMultipart)
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
773 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
774 throw OrthancException(ErrorCode_BadSequenceOfCalls);
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
775 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
776
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
777 // The two lines below might throw an exception, if the client has
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
778 // closed the connection. Such an error is ignored.
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
779 try
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
780 {
1832
b7da58699f92 Fix formatting of multipart HTTP answers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1645
diff changeset
781 std::string header = "--" + multipartBoundary_ + "--\r\n";
1430
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
782 stream_.Send(false, header.c_str(), header.size());
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
783 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
784 catch (OrthancException&)
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
785 {
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
786 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
787
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
788 state_ = State_Done;
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
789 }
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
790
ad94a3583b07 Plugins can send answers as multipart messages
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
791
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
792 void HttpOutput::StateMachine::SendStreamItem(const void* data,
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
793 size_t size)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
794 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
795 if (state_ != State_WritingStream)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
796 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
797 throw OrthancException(ErrorCode_BadSequenceOfCalls);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
798 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
799 else
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
800 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
801 if (size > 0)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
802 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
803 stream_.Send(false, data, size);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
804 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
805 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
806 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
807
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
808
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
809 void HttpOutput::StateMachine::CloseStream()
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
810 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
811 if (state_ != State_WritingStream)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
812 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
813 throw OrthancException(ErrorCode_BadSequenceOfCalls);
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
814 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
815 else
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
816 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
817 state_ = State_Done;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
818 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
819 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
820
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
821
3380
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
822 static void AnswerStreamAsBuffer(HttpOutput& output,
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
823 IHttpStreamAnswer& stream)
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
824 {
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
825 ChunkedBuffer buffer;
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
826
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
827 while (stream.ReadNextChunk())
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
828 {
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
829 if (stream.GetChunkSize() > 0)
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
830 {
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
831 buffer.AddChunk(stream.GetChunkContent(), stream.GetChunkSize());
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
832 }
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
833 }
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
834
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
835 std::string s;
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
836 buffer.Flatten(s);
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
837
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
838 output.SetContentType(stream.GetContentType());
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
839
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
840 std::string filename;
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
841 if (stream.HasContentFilename(filename))
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
842 {
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
843 output.SetContentFilename(filename.c_str());
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
844 }
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
845
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
846 output.Answer(s);
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
847 }
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
848
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
849
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
850 void HttpOutput::Answer(IHttpStreamAnswer& stream)
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
851 {
1523
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
852 HttpCompression compression = stream.SetupHttpCompression(isGzipAllowed_, isDeflateAllowed_);
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
853
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
854 switch (compression)
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
855 {
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
856 case HttpCompression_None:
3380
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
857 {
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
858 if (isGzipAllowed_ || isDeflateAllowed_)
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
859 {
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
860 // New in Orthanc 1.5.7: Compress streams without built-in
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
861 // compression, if requested by the "Accept-Encoding" HTTP
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
862 // header
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
863 AnswerStreamAsBuffer(*this, stream);
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
864 return;
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
865 }
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
866
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
867 break;
3380
0528a6c36f3d HTTP header "Accept-Encoding" is honored for streams without built-in support for compression
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3178
diff changeset
868 }
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
869
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
870 case HttpCompression_Gzip:
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
871 stateMachine_.AddHeader("Content-Encoding", "gzip");
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
872 break;
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
873
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
874 case HttpCompression_Deflate:
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
875 stateMachine_.AddHeader("Content-Encoding", "deflate");
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
876 break;
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
877
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
878 default:
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
879 throw OrthancException(ErrorCode_ParameterOutOfRange);
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
880 }
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
881
1523
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
882 stateMachine_.SetContentLength(stream.GetContentLength());
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
883
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
884 std::string contentType = stream.GetContentType();
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
885 if (contentType.empty())
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
886 {
2905
ae20fccdd867 refactoring mime types
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2805
diff changeset
887 contentType = MIME_BINARY;
1523
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
888 }
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
889
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
890 stateMachine_.SetContentType(contentType.c_str());
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
891
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
892 std::string filename;
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
893 if (stream.HasContentFilename(filename))
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
894 {
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
895 SetContentFilename(filename.c_str());
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
896 }
c388502a066d testing FilesystemHttpSender
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1522
diff changeset
897
1519
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
898 while (stream.ReadNextChunk())
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
899 {
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
900 stateMachine_.SendBody(stream.GetChunkContent(),
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
901 stream.GetChunkSize());
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
902 }
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
903
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
904 stateMachine_.CloseBody();
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
905 }
8bd0d897763f refactoring: IHttpStreamAnswer
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1517
diff changeset
906
3528
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
907
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
908 void HttpOutput::AnswerMultipartWithoutChunkedTransfer(
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
909 const std::string& subType,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
910 const std::string& contentType,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
911 const std::vector<const void*>& parts,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
912 const std::vector<size_t>& sizes,
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
913 const std::vector<const std::map<std::string, std::string>*>& headers)
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
914 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
915 if (parts.size() != sizes.size())
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
916 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
917 throw OrthancException(ErrorCode_ParameterOutOfRange);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
918 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
919
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
920 stateMachine_.CheckHeadersCompatibilityWithMultipart();
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
921
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
922 std::string boundary, contentTypeHeader;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
923 PrepareMultipartMainHeader(boundary, contentTypeHeader, subType, contentType);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
924 SetContentType(contentTypeHeader);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
925
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
926 std::map<std::string, std::string> empty;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
927
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
928 ChunkedBuffer chunked;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
929 for (size_t i = 0; i < parts.size(); i++)
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
930 {
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
931 std::string partHeader;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
932 PrepareMultipartItemHeader(partHeader, sizes[i], headers[i] == NULL ? empty : *headers[i],
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
933 boundary, contentType);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
934
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
935 chunked.AddChunk(partHeader);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
936 chunked.AddChunk(parts[i], sizes[i]);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
937 chunked.AddChunk("\r\n");
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
938 }
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
939
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
940 chunked.AddChunk("--" + boundary + "--\r\n");
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
941
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
942 std::string body;
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
943 chunked.Flatten(body);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
944 Answer(body);
f6fe095f7130 don't open a multipart stream if plugin only sends one part
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3425
diff changeset
945 }
4672
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
946
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
947
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
948 void HttpOutput::AnswerWithoutBuffering(IHttpStreamAnswer& stream)
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
949 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
950 std::string contentType = stream.GetContentType();
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
951 if (contentType.empty())
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
952 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
953 contentType = MIME_BINARY;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
954 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
955
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
956 std::string filename;
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
957 if (stream.HasContentFilename(filename))
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
958 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
959 stateMachine_.AddHeader("Content-Disposition", "filename=\"" + std::string(filename) + "\"");
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
960 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
961
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
962 stateMachine_.StartStream(contentType.c_str());
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
963
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
964 while (stream.ReadNextChunk())
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
965 {
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
966 stateMachine_.SendStreamItem(stream.GetChunkContent(), stream.GetChunkSize());
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
967 }
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
968
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
969 stateMachine_.CloseStream();
d9942d48fea7 ZipWriter::CancelStream(), ZipWriter::GetArchiveSize() and HttpOutput::AnswerWithoutBuffering()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4476
diff changeset
970 }
0
3959d33612cc initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
971 }