netwerk/protocol/http/nsHttpHeaderArray.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/protocol/http/nsHttpHeaderArray.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,185 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* vim: set sw=4 ts=8 et tw=80 : */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef nsHttpHeaderArray_h__
    1.11 +#define nsHttpHeaderArray_h__
    1.12 +
    1.13 +#include "nsHttp.h"
    1.14 +#include "nsTArray.h"
    1.15 +#include "nsString.h"
    1.16 +
    1.17 +class nsIHttpHeaderVisitor;
    1.18 +
    1.19 +namespace mozilla { namespace net {
    1.20 +
    1.21 +class nsHttpHeaderArray
    1.22 +{
    1.23 +public:
    1.24 +    const char *PeekHeader(nsHttpAtom header) const;
    1.25 +
    1.26 +    // Used by internal setters: to set header from network use SetHeaderFromNet
    1.27 +    nsresult SetHeader(nsHttpAtom header, const nsACString &value,
    1.28 +                       bool merge = false);
    1.29 +
    1.30 +    // Merges supported headers. For other duplicate values, determines if error
    1.31 +    // needs to be thrown or 1st value kept.
    1.32 +    nsresult SetHeaderFromNet(nsHttpAtom header, const nsACString &value);
    1.33 +
    1.34 +    nsresult GetHeader(nsHttpAtom header, nsACString &value) const;
    1.35 +    void     ClearHeader(nsHttpAtom h);
    1.36 +
    1.37 +    // Find the location of the given header value, or null if none exists.
    1.38 +    const char *FindHeaderValue(nsHttpAtom header, const char *value) const
    1.39 +    {
    1.40 +        return nsHttp::FindToken(PeekHeader(header), value,
    1.41 +                                 HTTP_HEADER_VALUE_SEPS);
    1.42 +    }
    1.43 +
    1.44 +    // Determine if the given header value exists.
    1.45 +    bool HasHeaderValue(nsHttpAtom header, const char *value) const
    1.46 +    {
    1.47 +        return FindHeaderValue(header, value) != nullptr;
    1.48 +    }
    1.49 +
    1.50 +    nsresult VisitHeaders(nsIHttpHeaderVisitor *visitor);
    1.51 +
    1.52 +    // parse a header line, return the header atom and a pointer to the
    1.53 +    // header value (the substring of the header line -- do not free).
    1.54 +    nsresult ParseHeaderLine(const char *line,
    1.55 +                             nsHttpAtom *header=nullptr,
    1.56 +                             char **value=nullptr);
    1.57 +
    1.58 +    void Flatten(nsACString &, bool pruneProxyHeaders=false);
    1.59 +
    1.60 +    uint32_t Count() const { return mHeaders.Length(); }
    1.61 +
    1.62 +    const char *PeekHeaderAt(uint32_t i, nsHttpAtom &header) const;
    1.63 +
    1.64 +    void Clear();
    1.65 +
    1.66 +    // Must be copy-constructable and assignable
    1.67 +    struct nsEntry
    1.68 +    {
    1.69 +        nsHttpAtom header;
    1.70 +        nsCString value;
    1.71 +
    1.72 +        struct MatchHeader {
    1.73 +          bool Equals(const nsEntry &entry, const nsHttpAtom &header) const {
    1.74 +            return entry.header == header;
    1.75 +          }
    1.76 +        };
    1.77 +    };
    1.78 +
    1.79 +private:
    1.80 +    int32_t LookupEntry(nsHttpAtom header, const nsEntry **) const;
    1.81 +    int32_t LookupEntry(nsHttpAtom header, nsEntry **);
    1.82 +    void MergeHeader(nsHttpAtom header, nsEntry *entry, const nsACString &value);
    1.83 +
    1.84 +    // Header cannot be merged: only one value possible
    1.85 +    bool    IsSingletonHeader(nsHttpAtom header);
    1.86 +    // For some headers we want to track empty values to prevent them being
    1.87 +    // combined with non-empty ones as a CRLF attack vector
    1.88 +    bool    TrackEmptyHeader(nsHttpAtom header);
    1.89 +
    1.90 +    // Subset of singleton headers: should never see multiple, different
    1.91 +    // instances of these, else something fishy may be going on (like CLRF
    1.92 +    // injection)
    1.93 +    bool    IsSuspectDuplicateHeader(nsHttpAtom header);
    1.94 +
    1.95 +    // All members must be copy-constructable and assignable
    1.96 +    nsTArray<nsEntry> mHeaders;
    1.97 +
    1.98 +    friend struct IPC::ParamTraits<nsHttpHeaderArray>;
    1.99 +};
   1.100 +
   1.101 +
   1.102 +//-----------------------------------------------------------------------------
   1.103 +// nsHttpHeaderArray <private>: inline functions
   1.104 +//-----------------------------------------------------------------------------
   1.105 +
   1.106 +inline int32_t
   1.107 +nsHttpHeaderArray::LookupEntry(nsHttpAtom header, const nsEntry **entry) const
   1.108 +{
   1.109 +    uint32_t index = mHeaders.IndexOf(header, 0, nsEntry::MatchHeader());
   1.110 +    if (index != UINT32_MAX)
   1.111 +        *entry = &mHeaders[index];
   1.112 +    return index;
   1.113 +}
   1.114 +
   1.115 +inline int32_t
   1.116 +nsHttpHeaderArray::LookupEntry(nsHttpAtom header, nsEntry **entry)
   1.117 +{
   1.118 +    uint32_t index = mHeaders.IndexOf(header, 0, nsEntry::MatchHeader());
   1.119 +    if (index != UINT32_MAX)
   1.120 +        *entry = &mHeaders[index];
   1.121 +    return index;
   1.122 +}
   1.123 +
   1.124 +inline bool
   1.125 +nsHttpHeaderArray::IsSingletonHeader(nsHttpAtom header)
   1.126 +{
   1.127 +    return header == nsHttp::Content_Type        ||
   1.128 +           header == nsHttp::Content_Disposition ||
   1.129 +           header == nsHttp::Content_Length      ||
   1.130 +           header == nsHttp::User_Agent          ||
   1.131 +           header == nsHttp::Referer             ||
   1.132 +           header == nsHttp::Host                ||
   1.133 +           header == nsHttp::Authorization       ||
   1.134 +           header == nsHttp::Proxy_Authorization ||
   1.135 +           header == nsHttp::If_Modified_Since   ||
   1.136 +           header == nsHttp::If_Unmodified_Since ||
   1.137 +           header == nsHttp::From                ||
   1.138 +           header == nsHttp::Location            ||
   1.139 +           header == nsHttp::Max_Forwards;
   1.140 +}
   1.141 +
   1.142 +inline bool
   1.143 +nsHttpHeaderArray::TrackEmptyHeader(nsHttpAtom header)
   1.144 +{
   1.145 +    return header == nsHttp::Content_Length ||
   1.146 +           header == nsHttp::Location;
   1.147 +}
   1.148 +
   1.149 +inline void
   1.150 +nsHttpHeaderArray::MergeHeader(nsHttpAtom header,
   1.151 +                               nsEntry *entry,
   1.152 +                               const nsACString &value)
   1.153 +{
   1.154 +    if (value.IsEmpty())
   1.155 +        return;   // merge of empty header = no-op
   1.156 +
   1.157 +    // Append the new value to the existing value
   1.158 +    if (header == nsHttp::Set_Cookie ||
   1.159 +        header == nsHttp::WWW_Authenticate ||
   1.160 +        header == nsHttp::Proxy_Authenticate)
   1.161 +    {
   1.162 +        // Special case these headers and use a newline delimiter to
   1.163 +        // delimit the values from one another as commas may appear
   1.164 +        // in the values of these headers contrary to what the spec says.
   1.165 +        entry->value.Append('\n');
   1.166 +    } else {
   1.167 +        // Delimit each value from the others using a comma (per HTTP spec)
   1.168 +        entry->value.AppendLiteral(", ");
   1.169 +    }
   1.170 +    entry->value.Append(value);
   1.171 +}
   1.172 +
   1.173 +inline bool
   1.174 +nsHttpHeaderArray::IsSuspectDuplicateHeader(nsHttpAtom header)
   1.175 +{
   1.176 +    bool retval =  header == nsHttp::Content_Length         ||
   1.177 +                     header == nsHttp::Content_Disposition    ||
   1.178 +                     header == nsHttp::Location;
   1.179 +
   1.180 +    MOZ_ASSERT(!retval || IsSingletonHeader(header),
   1.181 +               "Only non-mergeable headers should be in this list\n");
   1.182 +
   1.183 +    return retval;
   1.184 +}
   1.185 +
   1.186 +}} // namespace mozilla::net
   1.187 +
   1.188 +#endif

mercurial