iipsrv 1.3
iipsrv is an advanced high-performance feature-rich image server for web-based streamed viewing and zooming of ultra high-resolution images
IIPResponse.h
1/*
2 IIP Response Handler Class
3
4 Copyright (C) 2003-2025 Ruven Pillay.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19*/
20
21
22#ifndef _IIPRESPONSE_H
23#define _IIPRESPONSE_H
24
25#ifndef VERSION
26#define VERSION "999"
27#endif
28
29// Fix missing snprintf in Windows
30#if defined _MSC_VER && _MSC_VER<1900
31#define snprintf _snprintf
32#endif
33
34
35#include <string>
36
37
39
41
42
43 private:
44
45 std::string server; // Server header
46 std::string powered; // Powered By header
47 std::string modified; // Last modified header
48 std::string cacheControl; // Cache control header
49 std::string mimeType; // Mime type header
50 std::string eof; // End of response delimitter eg "\r\n"
51 std::string protocol; // IIP protocol version
52 std::string responseBody; // The main response
53 std::string error; // Error message
54 std::string allow; // Allow header
55 std::string cors; // CORS (Cross-Origin Resource Sharing) setting
56 std::string contentDisposition; // File name to use for Content Disposition header
57 std::string status; // HTTP status code
58 bool _cachable; // Indicate whether response should be cached
59 bool _sent; // Indicate whether a response has been sent
60
61
62 public:
63
66
67
69
70 void setProtocol( const std::string& p ) { protocol = p; };
71
72
74
75 void setMimeType( const std::string& m ) { mimeType = "Content-Type: " + m; };
76
77
79
80 void setLastModified( const std::string& m ) { modified = "Last-Modified: " + m; };
81
82
84
87 void setContentDisposition( const std::string& name, const std::string& type = "inline" ) {
88 contentDisposition = "Content-Disposition: " + type + "; filename=\"" + name + "\"";
89 }
90
91
93
94 void addResponse( const std::string& r );
95
96
98
99 void addResponse( const char* c );
100
101
103
106 void addResponse( const char* c, int a );
107
108
110
113 void addResponse( std::string c, const std::string& a );
114
115
117
121 void addResponse( const char* c, int a, int b );
122
123
125
128 void setError( const std::string& code, const std::string& arg );
129
130
132
133 void setCORS( const std::string& c ){
134 if(!c.empty()){
135 cors =
136 "Access-Control-Allow-Origin: " + c + eof +
137 "Access-Control-Allow-Methods: GET, POST, OPTIONS" + eof +
138 "Access-Control-Allow-Headers: Accept, Content-Type, X-Requested-With, If-Modified-Since" + eof +
139 "Access-Control-Max-Age: 86400";
140 }
141 };
142
143
145 std::string getCORS(){ return cors; };
146
147
149
150 void setCacheControl( const std::string& c ){ cacheControl = "Cache-Control: " + c; };
151
152
154
155 void setCachability( bool cachable ){ _cachable = cachable; };
156
157
159
160 bool cachable(){ return _cachable; };
161
162
164 std::string getCacheControl(){ return cacheControl; };
165
166
168
169 void setStatus( const std::string& s ){ status = "Status: " + s; }
170
171
173 std::string formatResponse();
174
175
177 bool isSet(){
178 if( error.length() || responseBody.length() || protocol.length() ) return true;
179 else return false;
180 }
181
182
185 if( error.length() ) return true;
186 else return false;
187 }
188
189
191 void setImageSent() { _sent = true; };
192
193
195 bool imageSent() { return _sent; };
196
197
199
200 std::string getAdvert();
201
202
204
209 std::string createHTTPHeader( const std::string& mimeType, const std::string& timeStamp, unsigned int contentLength=0 );
210
211
213
216 std::string getHeaderResponse( bool addCORS=false );
217
218};
219
220#endif
Class to handle non-image IIP responses including errors.
Definition IIPResponse.h:40
bool cachable()
Is response cachable?
Definition IIPResponse.h:160
void setStatus(const std::string &s)
Set HTTP status code.
Definition IIPResponse.h:169
void setCORS(const std::string &c)
Set CORS setting.
Definition IIPResponse.h:133
bool imageSent()
Indicate whether a response has been sent.
Definition IIPResponse.h:195
bool errorIsSet()
Indicate whether we have an error message.
Definition IIPResponse.h:184
std::string formatResponse()
Get a formatted string to send back.
Definition IIPResponse.cc:99
void setError(const std::string &code, const std::string &arg)
Set an error.
Definition IIPResponse.cc:91
void setMimeType(const std::string &m)
Set the Mime Type.
Definition IIPResponse.h:75
bool isSet()
Indicate whether this object has had any arguments passed to it.
Definition IIPResponse.h:177
IIPResponse()
Constructor.
Definition IIPResponse.cc:30
std::string getAdvert()
Display our advertising banner ;-)
Definition IIPResponse.cc:123
std::string createHTTPHeader(const std::string &mimeType, const std::string &timeStamp, unsigned int contentLength=0)
Convenience function to generate HTTP header fields.
Definition IIPResponse.cc:137
void setCacheControl(const std::string &c)
Set Cache-Control value.
Definition IIPResponse.h:150
void setLastModified(const std::string &m)
Set the Last Modified header.
Definition IIPResponse.h:80
void setImageSent()
Set the sent flag indicating that some sort of response has been sent.
Definition IIPResponse.h:191
std::string getHeaderResponse(bool addCORS=false)
Convenience function to generate a header-only response.
Definition IIPResponse.cc:164
void setProtocol(const std::string &p)
Set the IIP protocol version.
Definition IIPResponse.h:70
std::string getCORS()
Get CORS setting.
Definition IIPResponse.h:145
std::string getCacheControl()
Get Cache-Control value.
Definition IIPResponse.h:164
void setContentDisposition(const std::string &name, const std::string &type="inline")
Set Content Disposition header.
Definition IIPResponse.h:87
void setCachability(bool cachable)
Set whether the response should be cached.
Definition IIPResponse.h:155
void addResponse(const std::string &r)
Add a response string.
Definition IIPResponse.cc:48