Back to index

lightning-sunbird  0.9+nobinonly
nsHttpResponseHead.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Darin Fisher <darin@netscape.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsHttpResponseHead_h__
00040 #define nsHttpResponseHead_h__
00041 
00042 #include "nsHttpHeaderArray.h"
00043 #include "nsHttp.h"
00044 #include "nsString.h"
00045 
00046 //-----------------------------------------------------------------------------
00047 // nsHttpResponseHead represents the status line and headers from an HTTP
00048 // response.
00049 //-----------------------------------------------------------------------------
00050 
00051 class nsHttpResponseHead
00052 {
00053 public:
00054     nsHttpResponseHead() : mVersion(NS_HTTP_VERSION_1_1)
00055                          , mStatus(200)
00056                          , mContentLength(LL_MAXUINT)
00057                          , mCacheControlNoStore(PR_FALSE)
00058                          , mCacheControlNoCache(PR_FALSE)
00059                          , mPragmaNoCache(PR_FALSE) {}
00060    ~nsHttpResponseHead() 
00061     {
00062         Reset();
00063     }
00064     
00065     nsHttpHeaderArray    &Headers()        { return mHeaders; }
00066     nsHttpVersion         Version()        { return mVersion; }
00067     PRUint16              Status()         { return mStatus; }
00068     const nsAFlatCString &StatusText()     { return mStatusText; }
00069     PRInt64               ContentLength()  { return mContentLength; }
00070     const nsAFlatCString &ContentType()    { return mContentType; }
00071     const nsAFlatCString &ContentCharset() { return mContentCharset; }
00072     PRBool                NoStore()        { return mCacheControlNoStore; }
00073     PRBool                NoCache()        { return (mCacheControlNoCache || mPragmaNoCache); }
00079     PRInt64               TotalEntitySize();
00080 
00081     const char *PeekHeader(nsHttpAtom h)            { return mHeaders.PeekHeader(h); }
00082     nsresult SetHeader(nsHttpAtom h, const nsACString &v, PRBool m=PR_FALSE);
00083     nsresult GetHeader(nsHttpAtom h, nsACString &v) { return mHeaders.GetHeader(h, v); }
00084     void     ClearHeader(nsHttpAtom h)              { mHeaders.ClearHeader(h); }
00085     void     ClearHeaders()                         { mHeaders.Clear(); }
00086 
00087     void     SetContentType(const nsACString &s)    { mContentType = s; }
00088     void     SetContentCharset(const nsACString &s) { mContentCharset = s; }
00089     void     SetContentLength(PRInt64);
00090 
00091     // write out the response status line and headers as a single text block,
00092     // optionally pruning out transient headers (ie. headers that only make
00093     // sense the first time the response is handled).
00094     void     Flatten(nsACString &, PRBool pruneTransients);
00095 
00096     // parse flattened response head. block must be null terminated. parsing is
00097     // destructive.
00098     nsresult Parse(char *block);
00099 
00100     // parse the status line. line must be null terminated.
00101     void     ParseStatusLine(char *line);
00102 
00103     // parse a header line. line must be null terminated. parsing is destructive.
00104     void     ParseHeaderLine(char *line);
00105 
00106     // cache validation support methods
00107     nsresult ComputeFreshnessLifetime(PRUint32 *);
00108     nsresult ComputeCurrentAge(PRUint32 now, PRUint32 requestTime, PRUint32 *result);
00109     PRBool   MustValidate();
00110     PRBool   MustValidateIfExpired();
00111 
00112     // returns true if the server appears to support byte range requests.
00113     PRBool   IsResumable();
00114 
00115     // returns true if the Expires header has a value in the past relative to the
00116     // value of the Date header.
00117     PRBool   ExpiresInPast();
00118 
00119     // update headers...
00120     nsresult UpdateHeaders(nsHttpHeaderArray &headers); 
00121 
00122     // reset the response head to it's initial state
00123     void     Reset();
00124 
00125     // these return failure if the header does not exist.
00126     nsresult ParseDateHeader(nsHttpAtom header, PRUint32 *result);
00127     nsresult GetAgeValue(PRUint32 *result);
00128     nsresult GetMaxAgeValue(PRUint32 *result);
00129     nsresult GetDateValue(PRUint32 *result)         { return ParseDateHeader(nsHttp::Date, result); }
00130     nsresult GetExpiresValue(PRUint32 *result);
00131     nsresult GetLastModifiedValue(PRUint32 *result) { return ParseDateHeader(nsHttp::Last_Modified, result); }
00132 
00133 private:
00134     void     ParseVersion(const char *);
00135     void     ParseCacheControl(const char *);
00136     void     ParsePragma(const char *);
00137 
00138 private:
00139     nsHttpHeaderArray mHeaders;
00140     nsHttpVersion     mVersion;
00141     PRUint16          mStatus;
00142     nsCString         mStatusText;
00143     PRInt64           mContentLength;
00144     nsCString         mContentType;
00145     nsCString         mContentCharset;
00146     PRPackedBool      mCacheControlNoStore;
00147     PRPackedBool      mCacheControlNoCache;
00148     PRPackedBool      mPragmaNoCache;
00149 };
00150 
00151 #endif // nsHttpResponseHead_h__