netwerk/protocol/http/nsHttpResponseHead.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/protocol/http/nsHttpResponseHead.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsHttpResponseHead_h__
    1.10 +#define nsHttpResponseHead_h__
    1.11 +
    1.12 +#include "nsHttpHeaderArray.h"
    1.13 +#include "nsHttp.h"
    1.14 +#include "nsString.h"
    1.15 +
    1.16 +namespace mozilla { namespace net {
    1.17 +
    1.18 +//-----------------------------------------------------------------------------
    1.19 +// nsHttpResponseHead represents the status line and headers from an HTTP
    1.20 +// response.
    1.21 +//-----------------------------------------------------------------------------
    1.22 +
    1.23 +class nsHttpResponseHead
    1.24 +{
    1.25 +public:
    1.26 +    nsHttpResponseHead() : mVersion(NS_HTTP_VERSION_1_1)
    1.27 +                         , mStatus(200)
    1.28 +                         , mContentLength(UINT64_MAX)
    1.29 +                         , mCacheControlNoStore(false)
    1.30 +                         , mCacheControlNoCache(false)
    1.31 +                         , mPragmaNoCache(false) {}
    1.32 +
    1.33 +    const nsHttpHeaderArray & Headers()   const { return mHeaders; }
    1.34 +    nsHttpHeaderArray    &Headers()             { return mHeaders; }
    1.35 +    nsHttpVersion         Version()       const { return mVersion; }
    1.36 +// X11's Xlib.h #defines 'Status' to 'int' on some systems!
    1.37 +#undef Status
    1.38 +    uint16_t              Status()        const { return mStatus; }
    1.39 +    const nsAFlatCString &StatusText()    const { return mStatusText; }
    1.40 +    int64_t               ContentLength() const { return mContentLength; }
    1.41 +    const nsAFlatCString &ContentType()   const { return mContentType; }
    1.42 +    const nsAFlatCString &ContentCharset() const { return mContentCharset; }
    1.43 +    bool                  NoStore() const { return mCacheControlNoStore; }
    1.44 +    bool                  NoCache() const { return (mCacheControlNoCache || mPragmaNoCache); }
    1.45 +    /**
    1.46 +     * Full length of the entity. For byte-range requests, this may be larger
    1.47 +     * than ContentLength(), which will only represent the requested part of the
    1.48 +     * entity.
    1.49 +     */
    1.50 +    int64_t               TotalEntitySize() const;
    1.51 +
    1.52 +    const char *PeekHeader(nsHttpAtom h) const      { return mHeaders.PeekHeader(h); }
    1.53 +    nsresult SetHeader(nsHttpAtom h, const nsACString &v, bool m=false);
    1.54 +    nsresult GetHeader(nsHttpAtom h, nsACString &v) const { return mHeaders.GetHeader(h, v); }
    1.55 +    void     ClearHeader(nsHttpAtom h)              { mHeaders.ClearHeader(h); }
    1.56 +    void     ClearHeaders()                         { mHeaders.Clear(); }
    1.57 +
    1.58 +    const char *FindHeaderValue(nsHttpAtom h, const char *v) const
    1.59 +    {
    1.60 +      return mHeaders.FindHeaderValue(h, v);
    1.61 +    }
    1.62 +    bool        HasHeaderValue(nsHttpAtom h, const char *v) const
    1.63 +    {
    1.64 +      return mHeaders.HasHeaderValue(h, v);
    1.65 +    }
    1.66 +
    1.67 +    void     SetContentType(const nsACString &s)    { mContentType = s; }
    1.68 +    void     SetContentCharset(const nsACString &s) { mContentCharset = s; }
    1.69 +    void     SetContentLength(int64_t);
    1.70 +
    1.71 +    // write out the response status line and headers as a single text block,
    1.72 +    // optionally pruning out transient headers (ie. headers that only make
    1.73 +    // sense the first time the response is handled).
    1.74 +    void     Flatten(nsACString &, bool pruneTransients);
    1.75 +
    1.76 +    // parse flattened response head. block must be null terminated. parsing is
    1.77 +    // destructive.
    1.78 +    nsresult Parse(char *block);
    1.79 +
    1.80 +    // parse the status line. line must be null terminated.
    1.81 +    void     ParseStatusLine(const char *line);
    1.82 +
    1.83 +    // parse a header line. line must be null terminated. parsing is destructive.
    1.84 +    nsresult ParseHeaderLine(const char *line);
    1.85 +
    1.86 +    // cache validation support methods
    1.87 +    nsresult ComputeFreshnessLifetime(uint32_t *) const;
    1.88 +    nsresult ComputeCurrentAge(uint32_t now, uint32_t requestTime, uint32_t *result) const;
    1.89 +    bool     MustValidate() const;
    1.90 +    bool     MustValidateIfExpired() const;
    1.91 +
    1.92 +    // returns true if the server appears to support byte range requests.
    1.93 +    bool     IsResumable() const;
    1.94 +
    1.95 +    // returns true if the Expires header has a value in the past relative to the
    1.96 +    // value of the Date header.
    1.97 +    bool     ExpiresInPast() const;
    1.98 +
    1.99 +    // update headers...
   1.100 +    nsresult UpdateHeaders(const nsHttpHeaderArray &headers);
   1.101 +
   1.102 +    // reset the response head to it's initial state
   1.103 +    void     Reset();
   1.104 +
   1.105 +    // these return failure if the header does not exist.
   1.106 +    nsresult ParseDateHeader(nsHttpAtom header, uint32_t *result) const;
   1.107 +    nsresult GetAgeValue(uint32_t *result) const;
   1.108 +    nsresult GetMaxAgeValue(uint32_t *result) const;
   1.109 +    nsresult GetDateValue(uint32_t *result) const
   1.110 +    {
   1.111 +        return ParseDateHeader(nsHttp::Date, result);
   1.112 +    }
   1.113 +    nsresult GetExpiresValue(uint32_t *result) const ;
   1.114 +    nsresult GetLastModifiedValue(uint32_t *result) const
   1.115 +    {
   1.116 +        return ParseDateHeader(nsHttp::Last_Modified, result);
   1.117 +    }
   1.118 +
   1.119 +private:
   1.120 +    void     AssignDefaultStatusText();
   1.121 +    void     ParseVersion(const char *);
   1.122 +    void     ParseCacheControl(const char *);
   1.123 +    void     ParsePragma(const char *);
   1.124 +
   1.125 +private:
   1.126 +    // All members must be copy-constructable and assignable
   1.127 +    nsHttpHeaderArray mHeaders;
   1.128 +    nsHttpVersion     mVersion;
   1.129 +    uint16_t          mStatus;
   1.130 +    nsCString         mStatusText;
   1.131 +    int64_t           mContentLength;
   1.132 +    nsCString         mContentType;
   1.133 +    nsCString         mContentCharset;
   1.134 +    bool              mCacheControlNoStore;
   1.135 +    bool              mCacheControlNoCache;
   1.136 +    bool              mPragmaNoCache;
   1.137 +
   1.138 +    friend struct IPC::ParamTraits<nsHttpResponseHead>;
   1.139 +};
   1.140 +}} // namespace mozilla::net
   1.141 +
   1.142 +#endif // nsHttpResponseHead_h__

mercurial