Branch data Line data Source code
1 : : /*
2 : : * Copyright 2006-2009 Savarese Software Research Corporation
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * https://www.savarese.com/software/ApacheLicense-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #include <ssrc/wispers/fcgi/FCGIResponse.h>
18 : :
19 : : #include <sstream>
20 : :
21 : : __BEGIN_NS_SSRC_WSPR_FCGI
22 : :
23 : : // TODO: Provide more general cookie handling (as well as session cookie
24 : : // configuration in relay). Need a generalized header manipulation
25 : : // system and a Cookie class.
26 : 0 : void FCGIResponse::set_session_id(const sid_type & session_id,
27 : : const unsigned int max_age,
28 : : const bool secure)
29 : : {
30 : 0 : std::ostringstream ostr;
31 : :
32 [ # # ]: 0 : _session_id = session_id;
33 : :
34 [ # # # # : 0 : ostr << "Set-Cookie: WSPRSID=" << session_id << "; Path=/; Version=1";
# # ]
35 : :
36 [ # # ]: 0 : if(max_age == 1) {
37 [ # # ]: 0 : ostr << "; Discard";
38 : : } else {
39 [ # # # # ]: 0 : ostr << "; Max-Age=" << max_age;
40 : : }
41 : :
42 [ # # ]: 0 : if(secure) {
43 [ # # ]: 0 : ostr << "; Secure";
44 : : }
45 : :
46 [ # # # # : 0 : _session_cookie = std::move(ostr.str());
# # # # ]
47 : 0 : }
48 : :
49 : : // TODO: Move header output and buffer output into two separate functions.
50 : 0 : void FCGIResponse::fcgi_output(const char *content,
51 : : const unsigned int content_length,
52 : : bool cache_disable)
53 : : {
54 : 0 : FCGI::FCGX_Stream * const out = _request->_fcgx_request->out;
55 [ # # ]: 0 : if(cache_disable) {
56 : : FCGI::FCGX_FPrintF(out,
57 : : "Pragma: no-cache\r\n"
58 : : "Expires: Fri, 01 Jan 1990 00:00:00 GMT\r\n"
59 : 0 : "Cache-control: no-cache, must-revalidate, no-cache=\"Set-Cookie\", private\r\n");
60 : : }
61 : :
62 [ # # ]: 0 : if(!_session_cookie.empty()) {
63 : 0 : FCGI::FCGX_FPrintF(out, "%s\r\n", _session_cookie.c_str());
64 : : }
65 : :
66 : : FCGI::FCGX_FPrintF(out,
67 : : "Status: %d\r\n"
68 : : "Content-Type: %s\r\n\r\n",
69 : 0 : status(), content_type().c_str());
70 : :
71 [ # # # # ]: 0 : if(content && content_length > 0) {
72 : 0 : FCGI::FCGX_PutStr(content, content_length, out);
73 : : }
74 : 0 : }
75 : :
76 : 0 : void FCGIResponse::send_redirect(const string & redirect) {
77 : : try {
78 [ # # # # ]: 0 : const string && location(_request->to_absolute_url(redirect));
79 : :
80 [ # # # # ]: 0 : if(!_session_cookie.empty())
81 : 0 : FCGI::FCGX_FPrintF(_request->_fcgx_request->out,
82 : : "Status: %d\r\nLocation: %s\r\n%s\r\n\r\n",
83 : : StatusFound, location.c_str(),
84 [ # # # # : 0 : _session_cookie.c_str());
# # ]
85 : : else
86 : 0 : FCGI::FCGX_FPrintF(_request->_fcgx_request->out,
87 : : "Status: %d\r\nLocation: %s\r\n\r\n",
88 [ # # # # ]: 0 : StatusFound, location.c_str());
89 : :
90 [ # # ]: 0 : fcgx_finish(StatusFound);
91 [ # # ]: 0 : } catch(const std::invalid_argument & ia) {
92 [ # # ]: 0 : send_error(StatusInternalServerError);
93 : : }
94 : 0 : }
95 : :
96 [ + - + - ]: 6 : __END_NS_SSRC_WSPR_FCGI
|