Savarese Software Research Corporation
FCGIResponse.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2006-2009 Savarese Software Research Corporation
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     https://www.savarese.com/software/ApacheLicense-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <ssrc/wispers/fcgi/FCGIResponse.h>
00018 
00019 #include <sstream>
00020 
00021 __BEGIN_NS_SSRC_WSPR_FCGI
00022 
00023 // TODO: Provide more general cookie handling (as well as session cookie
00024 // configuration in relay).  Need a generalized header manipulation
00025 // system and a Cookie class.
00026 void FCGIResponse::set_session_id(const sid_type & session_id,
00027                                   const unsigned int max_age,
00028                                   const bool secure)
00029 {
00030   std::ostringstream ostr;
00031 
00032   _session_id = session_id;
00033 
00034   ostr << "Set-Cookie: WSPRSID=" << session_id << "; Path=/; Version=1";
00035 
00036   if(max_age == 1) {
00037     ostr << "; Discard";
00038   } else {
00039     ostr << "; Max-Age=" << max_age;
00040   }
00041 
00042   if(secure) {
00043     ostr << "; Secure";
00044   }
00045 
00046   _session_cookie = std::move(ostr.str());
00047 }
00048 
00049 // TODO: Move header output and buffer output into two separate functions.
00050 void FCGIResponse::fcgi_output(const char *content,
00051                                const unsigned int content_length,
00052                                bool cache_disable)
00053 {
00054   FCGI::FCGX_Stream * const out = _request->_fcgx_request->out;
00055   if(cache_disable) {
00056     FCGI::FCGX_FPrintF(out,
00057                        "Pragma: no-cache\r\n"
00058                        "Expires: Fri, 01 Jan 1990 00:00:00 GMT\r\n"
00059                        "Cache-control: no-cache, must-revalidate, no-cache=\"Set-Cookie\", private\r\n");
00060   }
00061 
00062   if(!_session_cookie.empty()) {
00063     FCGI::FCGX_FPrintF(out, "%s\r\n", _session_cookie.c_str());
00064   }
00065 
00066   FCGI::FCGX_FPrintF(out,
00067                      "Status: %d\r\n"
00068                      "Content-Type: %s\r\n\r\n",
00069                      status(), content_type().c_str());
00070 
00071   if(content && content_length > 0) {
00072     FCGI::FCGX_PutStr(content, content_length, out);
00073   }
00074 }
00075 
00076 void FCGIResponse::send_redirect(const string & redirect) {
00077   try {
00078     const string && location(_request->to_absolute_url(redirect));
00079 
00080     if(!_session_cookie.empty())
00081       FCGI::FCGX_FPrintF(_request->_fcgx_request->out,
00082                          "Status: %d\r\nLocation: %s\r\n%s\r\n\r\n",
00083                          StatusFound, location.c_str(),
00084                          _session_cookie.c_str());
00085     else
00086       FCGI::FCGX_FPrintF(_request->_fcgx_request->out,
00087                          "Status: %d\r\nLocation: %s\r\n\r\n",
00088                          StatusFound, location.c_str());
00089 
00090     fcgx_finish(StatusFound);
00091   } catch(const std::invalid_argument & ia) {
00092     send_error(StatusInternalServerError);
00093   }
00094 }
00095 
00096 __END_NS_SSRC_WSPR_FCGI

Savarese Software Research Corporation
Copyright © 2006-2011 Savarese Software Research Corporation. All rights reserved.