iipsrv 1.2
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-2023 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 cors; // CORS (Cross-Origin Resource Sharing) setting
55 std::string contentDisposition; // File name to use for Content Disposition header
56 std::string status; // HTTP status code
57 bool _cachable; // Indicate whether response should be cached
58 bool _sent; // Indicate whether a response has been sent
59
60
61 public:
62
65
66
68
69 void setProtocol( const std::string& p ) { protocol = p; };
70
71
73
74 void setMimeType( const std::string& m ) { mimeType = "Content-Type: " + m; };
75
76
78
79 void setLastModified( const std::string& m ) { modified = "Last-Modified: " + m; };
80
81
83
84 void setContentDisposition( const std::string& name, const std::string& type = "inline" ) {
85 contentDisposition = "Content-Disposition: " + type + "; filename=\"" + name + "\"";
86 }
87
88
90
91 void addResponse( const std::string& r );
92
93
95
96 void addResponse( const char* c );
97
98
100
103 void addResponse( const char* c, int a );
104
105
107
110 void addResponse( std::string c, const std::string& a );
111
112
114
118 void addResponse( const char* c, int a, int b );
119
120
122
125 void setError( const std::string& code, const std::string& arg );
126
127
129
130 void setCORS( const std::string& c ){
131 if(!c.empty()){
132 cors =
133 "Access-Control-Allow-Origin: " + c + eof +
134 "Access-Control-Allow-Methods: GET, POST, OPTIONS" + eof +
135 "Access-Control-Allow-Headers: Accept, Content-Type, X-Requested-With, If-Modified-Since" + eof +
136 "Access-Control-Max-Age: 86400";
137 }
138 };
139
140
142 std::string getCORS(){ return cors; };
143
144
146
147 void setCacheControl( const std::string& c ){ cacheControl = "Cache-Control: " + c; };
148
149
151
152 void setCachability( bool cachable ){ _cachable = cachable; };
153
154
156
157 bool cachable(){ return _cachable; };
158
159
161 std::string getCacheControl(){ return cacheControl; };
162
163
165
166 void setStatus( const std::string& s ){ status = "Status: " + s; }
167
168
170 std::string formatResponse();
171
172
174 bool isSet(){
175 if( error.length() || responseBody.length() || protocol.length() ) return true;
176 else return false;
177 }
178
179
182 if( error.length() ) return true;
183 else return false;
184 }
185
186
188 void setImageSent() { _sent = true; };
189
190
192 bool imageSent() { return _sent; };
193
194
196
197 std::string getAdvert();
198
199
201
205 std::string createHTTPHeader( const std::string& mimeType, const std::string& timeStamp, unsigned int contentLength=0 );
206
207};
208
209
210#endif
Class to handle non-image IIP responses including errors.
Definition: IIPResponse.h:40
bool cachable()
Is response cachable?
Definition: IIPResponse.h:157
std::string getAdvert()
Display our advertising banner ;-)
void setStatus(const std::string &s)
Set HTTP status code.
Definition: IIPResponse.h:166
void setCORS(const std::string &c)
Set CORS setting.
Definition: IIPResponse.h:130
bool imageSent()
Indicate whether a response has been sent.
Definition: IIPResponse.h:192
bool errorIsSet()
Indicate whether we have an error message.
Definition: IIPResponse.h:181
void addResponse(const char *c, int a, int b)
Add a response string.
void addResponse(const char *c, int a)
Add a response string.
void setError(const std::string &code, const std::string &arg)
Set an error.
void setMimeType(const std::string &m)
Set the Mime Type.
Definition: IIPResponse.h:74
bool isSet()
Indicate whether this object has had any arguments passed to it.
Definition: IIPResponse.h:174
IIPResponse()
Constructor.
void addResponse(const char *c)
Add a response string.
void addResponse(std::string c, const std::string &a)
Add a response string.
void setCacheControl(const std::string &c)
Set Cache-Control value.
Definition: IIPResponse.h:147
void setLastModified(const std::string &m)
Set the Last Modified header.
Definition: IIPResponse.h:79
void setImageSent()
Set the sent flag indicating that some sort of response has been sent.
Definition: IIPResponse.h:188
void setProtocol(const std::string &p)
Set the IIP protocol version.
Definition: IIPResponse.h:69
std::string getCORS()
Get CORS setting.
Definition: IIPResponse.h:142
std::string getCacheControl()
Get Cache-Control value.
Definition: IIPResponse.h:161
void setContentDisposition(const std::string &name, const std::string &type="inline")
Set Content Disposition header.
Definition: IIPResponse.h:84
void setCachability(bool cachable)
Set whether the response should be cached.
Definition: IIPResponse.h:152
std::string createHTTPHeader(const std::string &mimeType, const std::string &timeStamp, unsigned int contentLength=0)
Convenience function to generate HTTP header fields.
void addResponse(const std::string &r)
Add a response string.
std::string formatResponse()
Get a formatted string to send back.