1 /* -*-mode:c++; c-file-style: "gnu";-*- */
2 /*
3  *  $Id: HTTPResponseHeader.h,v 1.10 2014/04/23 20:55:09 sebdiaz Exp $
4  *
5  *  Copyright (C) 1996 - 2004 Stephen F. Booth <sbooth@gnu.org>
6  *                       2007 Sebastien DIAZ <sebastien.diaz@gmail.com>
7  *  Part of the GNU cgicc library, http://www.gnu.org/software/cgicc
8  *
9  *  This library is free software; you can redistribute it and/or
10  *  modify it under the terms of the GNU Lesser General Public
11  *  License as published by the Free Software Foundation; either
12  *  version 3 of the License, or (at your option) any later version.
13  *
14  *  This library is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  Lesser General Public License for more details.
18  *
19  *  You should have received a copy of the GNU Lesser General Public
20  *  License along with this library; if not, write to the Free Software
21  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
22  */
23 
24 #ifndef _HTTPRESPONSEHEADER_H_
25 #define _HTTPRESPONSEHEADER_H_ 1
26 
27 #ifdef __GNUG__
28 #  pragma interface
29 #endif
30 
31 /*! \file HTTPResponseHeader.h
32  * \brief Class for generic, complete HTTP header responses
33  *
34  * This is an class usually only used with Non-Parsed Header (NPH)
35  * applications
36  */
37 
38 #include <string>
39 #include <vector>
40 
41 #include "MStreamable.h"
42 #include "HTTPCookie.h"
43 
44 namespace cgicc {
45 
46   // ============================================================
47   // Class HTTPResponseHeader
48   // ============================================================
49   /*! \class HTTPResponseHeader HTTPResponseHeader.h cgicc/HTTPResponseHeader.h
50    * \brief Generic HTTP response header
51    *
52    * This class represents an HTTP response header as defined in
53    * section 6 of RFC 2616 (see http://www.w3.org)
54    *
55    * All HTTP/1.1 reponses consist of an initial status line containing
56    * the HTTP version, a 3-digit status code, and a human-readable reason
57    * phrase explaining the status code.
58    *
59    * The first digit of the Status-Code defines the class of
60    * response. The last two digits do not have any categorization
61    * role. There are 5 values for the first digit:
62    * <ul>
63    * <li>1xx: Informational - Request received, continuing process</li>
64    * <li>2xx: Success - The action was successfully received,
65    understood, and accepted</li>
66    * <li>3xx: Redirection - Further action must be taken in order to
67    * complete the request</li>
68    * <li>4xx: Client Error - The request contains bad syntax or cannot
69    * be fulfilled</li>
70    * <li>5xx: Server Error - The server failed to fulfill an apparently
71    * valid request</li></ul>
72    */
73   class CGICC_API HTTPResponseHeader : public MStreamable
74   {
75   public:
76 
77     /*! \name Constructor and Destructor */
78     //@{
79 
80     /*!
81      * \brief Create a new HTTP response header
82      * \param http_version The HTTP version string, usually \c HTTP/1.1
83      * \param status_code The 3-digit HTTP status code
84      * \param reason_phrase A short textual description of the status code
85      */
86     HTTPResponseHeader(const std::string& http_version,
87 		       int status_code,
88 		       const std::string& reason_phrase);
89 
90     /*!
91      * \brief Delete this HTTPResponseHeader
92      *
93      */
94     virtual ~HTTPResponseHeader();
95     //@}
96 
97     // ============================================================
98 
99     /*! \name Additional Header Management */
100     //@{
101 
102     /*!
103      * \brief Add a general, response, or entity header to this one
104      *
105      * \param header The text of the header to add
106      * \return A reference to this
107      */
108     HTTPResponseHeader&
109     addHeader(const std::string& header);
110 
111     /*!
112      * \brief Add a general, response, or entity header to this one
113      *
114      * \param name The name of the header element to add
115      * \param value The value of the header element
116      * \return A reference to this
117      */
118     HTTPResponseHeader&
119     addHeader(const std::string& name,
120 	      const std::string& value);
121 
122     /*!
123      * \brief Get a list of all additional headers
124      *
125      * \return A list of all additional headers
126      */
127     inline const std::vector<std::string>&
getHeaders()128     getHeaders() 					const
129     { return fHeaders; }
130     //@}
131 
132     /*! \name Cookie Management */
133     //@{
134 
135     /*!
136      * \brief Set a cookie to go out with this HTTPResponseHeader
137      * \param cookie The HTTPCookie to set
138      */
139     HTTPResponseHeader&
140     setCookie(const HTTPCookie& cookie);
141 
142     /*!
143      * \brief Get a list of all cookies associated with this header
144      * \return All the cookies associated with this header
145      */
146     inline const std::vector<HTTPCookie>&
getCookies()147     getCookies() 					const
148     { return fCookies; }
149     //@}
150 
151     // ============================================================
152 
153     /*! \name Accessor methods
154      * Retrieve information on the header
155      */
156     //@{
157 
158     /*!
159      * \brief Get the HTTP version
160      *
161      * The HTTP version is a string of the form \c HTTP/1.1
162      * \return The HTTP version
163      */
164     inline const std::string&
getHTTPVersion()165     getHTTPVersion() 				const
166     { return fHTTPVersion; }
167 
168     /*!
169      * \brief Get the 3-digit status code
170      *
171      * The 3-digit status code indicates the disposition of the response.
172      * \return The 3-digit status code
173      */
174     inline int
getStatusCode()175     getStatusCode() 				const
176     { return fStatusCode; }
177 
178     /*!
179      * \brief Get the reason phrase associated with the stats code
180      *
181      * The reason phrase is a human-readable interpretation of the status code
182      * \return The reason phrase
183      */
184     inline std::string
getReasonPhrase()185     getReasonPhrase() 				const
186     { return fReasonPhrase; }
187     //@}
188 
189     // ============================================================
190 
191     /*! \name Mutator methods
192      * Set information on the header
193      */
194     //@{
195 
196     /*!
197      * \brief Set the HTTP version
198      *
199      * The HTTP version is a string of the form \c HTTP/1.1
200      * \param http_version The HTTP version string, usually \c HTTP/1.1
201      * \return A reference to this
202      */
203     inline HTTPResponseHeader&
getHTTPVersion(const std::string & http_version)204     getHTTPVersion(const std::string& http_version)
205     { fHTTPVersion = http_version; return *this; }
206 
207     /*!
208      * \brief Get the 3-digit status code
209      *
210      * The 3-digit status code indicates the disposition of the response.
211      * \param status_code The 3-digit HTTP status code
212      * \return A reference to this
213      */
214     inline HTTPResponseHeader&
getStatusCode(int status_code)215     getStatusCode(int status_code)
216     { fStatusCode = status_code; return *this; }
217 
218     /*!
219      * \brief Get the reason phrase associated with the stats code
220      *
221      * The reason phrase is a human-readable interpretation of the status code
222      * \param reason_phrase A short textual description of the status code
223      * \return A reference to this
224      */
225     inline HTTPResponseHeader&
getReasonPhrase(const std::string & reason_phrase)226     getReasonPhrase(const std::string& reason_phrase)
227     { fReasonPhrase = reason_phrase; return *this; }
228     //@}
229 
230     // ============================================================
231 
232     /*! \name Inherited Methods */
233     //@{
234     virtual void
235     render(std::ostream& out) 			const;
236     //@}
237 
238   private:
239     HTTPResponseHeader();
240 
241     std::string 		fHTTPVersion;
242     int 			fStatusCode;
243     std::string 		fReasonPhrase;
244     std::vector<std::string> 	fHeaders;
245     std::vector<HTTPCookie> 	fCookies;
246   };
247 
248 } // namespace cgicc
249 
250 #endif /* ! _HTTPRESPONSEHEADER_H_ */
251