DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (b3ecb5aef45a)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsHttpResponseHead_h__
#define nsHttpResponseHead_h__

#include "nsHttpHeaderArray.h"
#include "nsHttp.h"
#include "nsString.h"
#include "mozilla/RecursiveMutex.h"

#ifdef Status
/* Xlib headers insist on this for some reason... Nuke it because
   it'll override our member name */
typedef Status __StatusTmp;
#  undef Status
typedef __StatusTmp Status;
#endif

class nsIHttpHeaderVisitor;

// This needs to be forward declared here so we can include only this header
// without also including PHttpChannelParams.h
namespace IPC {
template <typename>
struct ParamTraits;
}  // namespace IPC

namespace mozilla {
namespace net {

//-----------------------------------------------------------------------------
// nsHttpResponseHead represents the status line and headers from an HTTP
// response.
//-----------------------------------------------------------------------------

class nsHttpResponseHead {
 public:
  nsHttpResponseHead()
      : mVersion(HttpVersion::v1_1),
        mStatus(200),
        mContentLength(-1),
        mCacheControlPublic(false),
        mCacheControlPrivate(false),
        mCacheControlNoStore(false),
        mCacheControlNoCache(false),
        mCacheControlImmutable(false),
        mPragmaNoCache(false),
        mRecursiveMutex("nsHttpResponseHead.mRecursiveMutex"),
        mInVisitHeaders(false) {}

  nsHttpResponseHead(const nsHttpResponseHead& aOther);
  nsHttpResponseHead& operator=(const nsHttpResponseHead& aOther);

  void Enter() { mRecursiveMutex.Lock(); }
  void Exit() { mRecursiveMutex.Unlock(); }

  HttpVersion Version();
  uint16_t Status();
  void StatusText(nsACString& aStatusText);
  int64_t ContentLength();
  void ContentType(nsACString& aContentType);
  void ContentCharset(nsACString& aContentCharset);
  bool Public();
  bool Private();
  bool NoStore();
  bool NoCache();
  bool Immutable();
  /**
   * Full length of the entity. For byte-range requests, this may be larger
   * than ContentLength(), which will only represent the requested part of the
   * entity.
   */
  int64_t TotalEntitySize();

  MOZ_MUST_USE nsresult SetHeader(const nsACString& h, const nsACString& v,
                                  bool m = false);
  MOZ_MUST_USE nsresult SetHeader(nsHttpAtom h, const nsACString& v,
                                  bool m = false);
  MOZ_MUST_USE nsresult GetHeader(nsHttpAtom h, nsACString& v);
  void ClearHeader(nsHttpAtom h);
  void ClearHeaders();
  bool HasHeaderValue(nsHttpAtom h, const char* v);
  bool HasHeader(nsHttpAtom h);

  void SetContentType(const nsACString& s);
  void SetContentCharset(const nsACString& s);
  void SetContentLength(int64_t);

  // write out the response status line and headers as a single text block,
  // optionally pruning out transient headers (ie. headers that only make
  // sense the first time the response is handled).
  // Both functions append to the string supplied string.
  void Flatten(nsACString&, bool pruneTransients);
  void FlattenNetworkOriginalHeaders(nsACString& buf);

  // The next 2 functions parse flattened response head and original net
  // headers. They are used when we are reading an entry from the cache.
  //
  // To keep proper order of the original headers we MUST call
  // ParseCachedOriginalHeaders FIRST and then ParseCachedHead.
  //
  // block must be null terminated.
  MOZ_MUST_USE nsresult ParseCachedHead(const char* block);
  MOZ_MUST_USE nsresult ParseCachedOriginalHeaders(char* block);

  // parse the status line.
  void ParseStatusLine(const nsACString& line);

  // parse a header line.
  MOZ_MUST_USE nsresult ParseHeaderLine(const nsACString& line);

  // cache validation support methods
  MOZ_MUST_USE nsresult ComputeFreshnessLifetime(uint32_t*);
  MOZ_MUST_USE nsresult ComputeCurrentAge(uint32_t now, uint32_t requestTime,
                                          uint32_t* result);
  bool MustValidate();
  bool MustValidateIfExpired();

  // return true if the response contains a valid Cache-control:
  // stale-while-revalidate and |now| is less than or equal |expiration +
  // stale-while-revalidate|.  Otherwise false.
  bool StaleWhileRevalidate(uint32_t now, uint32_t expiration);

  // returns true if the server appears to support byte range requests.
  bool IsResumable();

  // returns true if the Expires header has a value in the past relative to the
  // value of the Date header.
  bool ExpiresInPast();

  // update headers...
  MOZ_MUST_USE nsresult UpdateHeaders(nsHttpResponseHead* headers);

  // reset the response head to it's initial state
  void Reset();

  MOZ_MUST_USE nsresult GetAgeValue(uint32_t* result);
  MOZ_MUST_USE nsresult GetMaxAgeValue(uint32_t* result);
  MOZ_MUST_USE nsresult GetStaleWhileRevalidateValue(uint32_t* result);
  MOZ_MUST_USE nsresult GetDateValue(uint32_t* result);
  MOZ_MUST_USE nsresult GetExpiresValue(uint32_t* result);
  MOZ_MUST_USE nsresult GetLastModifiedValue(uint32_t* result);

  bool operator==(const nsHttpResponseHead& aOther) const;

  // Using this function it is possible to itereate through all headers
  // automatically under one lock.
  MOZ_MUST_USE nsresult VisitHeaders(nsIHttpHeaderVisitor* visitor,
                                     nsHttpHeaderArray::VisitorFilter filter);
  MOZ_MUST_USE nsresult GetOriginalHeader(nsHttpAtom aHeader,
                                          nsIHttpHeaderVisitor* aVisitor);

  bool HasContentType();
  bool HasContentCharset();

 private:
  MOZ_MUST_USE nsresult SetHeader_locked(nsHttpAtom atom, const nsACString& h,
                                         const nsACString& v, bool m = false);
  void AssignDefaultStatusText();
  void ParseVersion(const char*);
  void ParseCacheControl(const char*);
  void ParsePragma(const char*);

  void ParseStatusLine_locked(const nsACString& line);
  MOZ_MUST_USE nsresult ParseHeaderLine_locked(const nsACString& line,
                                               bool originalFromNetHeaders);

  // these return failure if the header does not exist.
  MOZ_MUST_USE nsresult ParseDateHeader(nsHttpAtom header,
                                        uint32_t* result) const;

  bool ExpiresInPast_locked() const;
  MOZ_MUST_USE nsresult GetAgeValue_locked(uint32_t* result) const;
  MOZ_MUST_USE nsresult GetExpiresValue_locked(uint32_t* result) const;
  MOZ_MUST_USE nsresult GetMaxAgeValue_locked(uint32_t* result) const;
  MOZ_MUST_USE nsresult
  GetStaleWhileRevalidateValue_locked(uint32_t* result) const;

  MOZ_MUST_USE nsresult GetDateValue_locked(uint32_t* result) const {
    return ParseDateHeader(nsHttp::Date, result);
  }

  MOZ_MUST_USE nsresult GetLastModifiedValue_locked(uint32_t* result) const {
    return ParseDateHeader(nsHttp::Last_Modified, result);
  }

 private:
  // All members must be copy-constructable and assignable
  nsHttpHeaderArray mHeaders;
  HttpVersion mVersion;
  uint16_t mStatus;
  nsCString mStatusText;
  int64_t mContentLength;
  nsCString mContentType;
  nsCString mContentCharset;
  bool mCacheControlPublic;
  bool mCacheControlPrivate;
  bool mCacheControlNoStore;
  bool mCacheControlNoCache;
  bool mCacheControlImmutable;
  bool mPragmaNoCache;

  // We are using RecursiveMutex instead of a Mutex because VisitHeader
  // function calls nsIHttpHeaderVisitor::VisitHeader while under lock.
  RecursiveMutex mRecursiveMutex;
  // During VisitHeader we sould not allow cal to SetHeader.
  bool mInVisitHeaders;

  friend struct IPC::ParamTraits<nsHttpResponseHead>;
};

}  // namespace net
}  // namespace mozilla

#endif  // nsHttpResponseHead_h__