Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim:set ts=4 sw=4 sts=4 et cin: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef nsHttp_h__ |
michael@0 | 8 | #define nsHttp_h__ |
michael@0 | 9 | |
michael@0 | 10 | #include <stdint.h> |
michael@0 | 11 | #include "prtime.h" |
michael@0 | 12 | #include "nsString.h" |
michael@0 | 13 | #include "nsError.h" |
michael@0 | 14 | |
michael@0 | 15 | // http version codes |
michael@0 | 16 | #define NS_HTTP_VERSION_UNKNOWN 0 |
michael@0 | 17 | #define NS_HTTP_VERSION_0_9 9 |
michael@0 | 18 | #define NS_HTTP_VERSION_1_0 10 |
michael@0 | 19 | #define NS_HTTP_VERSION_1_1 11 |
michael@0 | 20 | #define NS_HTTP_VERSION_2_0 20 |
michael@0 | 21 | |
michael@0 | 22 | namespace mozilla { |
michael@0 | 23 | |
michael@0 | 24 | class Mutex; |
michael@0 | 25 | |
michael@0 | 26 | namespace net { |
michael@0 | 27 | enum { |
michael@0 | 28 | SPDY_VERSION_2_REMOVED = 2, |
michael@0 | 29 | SPDY_VERSION_3 = 3, |
michael@0 | 30 | SPDY_VERSION_31 = 4, |
michael@0 | 31 | |
michael@0 | 32 | // leave room for official versions. telem goes to 48 |
michael@0 | 33 | // 24 was a internal spdy/3.1 |
michael@0 | 34 | // 25 was spdy/4a2 |
michael@0 | 35 | // 26 was http/2-draft08 and http/2-draft07 (they were the same) |
michael@0 | 36 | // 27 was also http/2-draft09 |
michael@0 | 37 | HTTP2_VERSION_DRAFT10 = 27 |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | typedef uint8_t nsHttpVersion; |
michael@0 | 41 | |
michael@0 | 42 | #define NS_HTTP2_DRAFT_VERSION HTTP2_VERSION_DRAFT10 |
michael@0 | 43 | #define NS_HTTP2_DRAFT_TOKEN "h2-10" |
michael@0 | 44 | |
michael@0 | 45 | //----------------------------------------------------------------------------- |
michael@0 | 46 | // http connection capabilities |
michael@0 | 47 | //----------------------------------------------------------------------------- |
michael@0 | 48 | |
michael@0 | 49 | #define NS_HTTP_ALLOW_KEEPALIVE (1<<0) |
michael@0 | 50 | #define NS_HTTP_ALLOW_PIPELINING (1<<1) |
michael@0 | 51 | |
michael@0 | 52 | // a transaction with this caps flag will continue to own the connection, |
michael@0 | 53 | // preventing it from being reclaimed, even after the transaction completes. |
michael@0 | 54 | #define NS_HTTP_STICKY_CONNECTION (1<<2) |
michael@0 | 55 | |
michael@0 | 56 | // a transaction with this caps flag will, upon opening a new connection, |
michael@0 | 57 | // bypass the local DNS cache |
michael@0 | 58 | #define NS_HTTP_REFRESH_DNS (1<<3) |
michael@0 | 59 | |
michael@0 | 60 | // a transaction with this caps flag will not pass SSL client-certificates |
michael@0 | 61 | // to the server (see bug #466080), but is may also be used for other things |
michael@0 | 62 | #define NS_HTTP_LOAD_ANONYMOUS (1<<4) |
michael@0 | 63 | |
michael@0 | 64 | // a transaction with this caps flag keeps timing information |
michael@0 | 65 | #define NS_HTTP_TIMING_ENABLED (1<<5) |
michael@0 | 66 | |
michael@0 | 67 | // a transaction with this flag blocks the initiation of other transactons |
michael@0 | 68 | // in the same load group until it is complete |
michael@0 | 69 | #define NS_HTTP_LOAD_AS_BLOCKING (1<<6) |
michael@0 | 70 | |
michael@0 | 71 | // Disallow the use of the SPDY protocol. This is meant for the contexts |
michael@0 | 72 | // such as HTTP upgrade which are nonsensical for SPDY, it is not the |
michael@0 | 73 | // SPDY configuration variable. |
michael@0 | 74 | #define NS_HTTP_DISALLOW_SPDY (1<<7) |
michael@0 | 75 | |
michael@0 | 76 | // a transaction with this flag loads without respect to whether the load |
michael@0 | 77 | // group is currently blocking on some resources |
michael@0 | 78 | #define NS_HTTP_LOAD_UNBLOCKED (1<<8) |
michael@0 | 79 | |
michael@0 | 80 | // These flags allow a transaction to use TLS false start with |
michael@0 | 81 | // weaker security profiles based on past history |
michael@0 | 82 | #define NS_HTTP_ALLOW_RSA_FALSESTART (1<<9) |
michael@0 | 83 | |
michael@0 | 84 | //----------------------------------------------------------------------------- |
michael@0 | 85 | // some default values |
michael@0 | 86 | //----------------------------------------------------------------------------- |
michael@0 | 87 | |
michael@0 | 88 | #define NS_HTTP_DEFAULT_PORT 80 |
michael@0 | 89 | #define NS_HTTPS_DEFAULT_PORT 443 |
michael@0 | 90 | |
michael@0 | 91 | #define NS_HTTP_HEADER_SEPS ", \t" |
michael@0 | 92 | |
michael@0 | 93 | //----------------------------------------------------------------------------- |
michael@0 | 94 | // http atoms... |
michael@0 | 95 | //----------------------------------------------------------------------------- |
michael@0 | 96 | |
michael@0 | 97 | struct nsHttpAtom |
michael@0 | 98 | { |
michael@0 | 99 | operator const char *() const { return _val; } |
michael@0 | 100 | const char *get() const { return _val; } |
michael@0 | 101 | |
michael@0 | 102 | void operator=(const char *v) { _val = v; } |
michael@0 | 103 | void operator=(const nsHttpAtom &a) { _val = a._val; } |
michael@0 | 104 | |
michael@0 | 105 | // private |
michael@0 | 106 | const char *_val; |
michael@0 | 107 | }; |
michael@0 | 108 | |
michael@0 | 109 | struct nsHttp |
michael@0 | 110 | { |
michael@0 | 111 | static nsresult CreateAtomTable(); |
michael@0 | 112 | static void DestroyAtomTable(); |
michael@0 | 113 | |
michael@0 | 114 | // The mutex is valid any time the Atom Table is valid |
michael@0 | 115 | // This mutex is used in the unusual case that the network thread and |
michael@0 | 116 | // main thread might access the same data |
michael@0 | 117 | static Mutex *GetLock(); |
michael@0 | 118 | |
michael@0 | 119 | // will dynamically add atoms to the table if they don't already exist |
michael@0 | 120 | static nsHttpAtom ResolveAtom(const char *); |
michael@0 | 121 | static nsHttpAtom ResolveAtom(const nsACString &s) |
michael@0 | 122 | { |
michael@0 | 123 | return ResolveAtom(PromiseFlatCString(s).get()); |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | // returns true if the specified token [start,end) is valid per RFC 2616 |
michael@0 | 127 | // section 2.2 |
michael@0 | 128 | static bool IsValidToken(const char *start, const char *end); |
michael@0 | 129 | |
michael@0 | 130 | static inline bool IsValidToken(const nsCString &s) { |
michael@0 | 131 | const char *start = s.get(); |
michael@0 | 132 | return IsValidToken(start, start + s.Length()); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | // find the first instance (case-insensitive comparison) of the given |
michael@0 | 136 | // |token| in the |input| string. the |token| is bounded by elements of |
michael@0 | 137 | // |separators| and may appear at the beginning or end of the |input| |
michael@0 | 138 | // string. null is returned if the |token| is not found. |input| may be |
michael@0 | 139 | // null, in which case null is returned. |
michael@0 | 140 | static const char *FindToken(const char *input, const char *token, |
michael@0 | 141 | const char *separators); |
michael@0 | 142 | |
michael@0 | 143 | // This function parses a string containing a decimal-valued, non-negative |
michael@0 | 144 | // 64-bit integer. If the value would exceed INT64_MAX, then false is |
michael@0 | 145 | // returned. Otherwise, this function returns true and stores the |
michael@0 | 146 | // parsed value in |result|. The next unparsed character in |input| is |
michael@0 | 147 | // optionally returned via |next| if |next| is non-null. |
michael@0 | 148 | // |
michael@0 | 149 | // TODO(darin): Replace this with something generic. |
michael@0 | 150 | // |
michael@0 | 151 | static bool ParseInt64(const char *input, const char **next, |
michael@0 | 152 | int64_t *result); |
michael@0 | 153 | |
michael@0 | 154 | // Variant on ParseInt64 that expects the input string to contain nothing |
michael@0 | 155 | // more than the value being parsed. |
michael@0 | 156 | static inline bool ParseInt64(const char *input, int64_t *result) { |
michael@0 | 157 | const char *next; |
michael@0 | 158 | return ParseInt64(input, &next, result) && *next == '\0'; |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | // Return whether the HTTP status code represents a permanent redirect |
michael@0 | 162 | static bool IsPermanentRedirect(uint32_t httpStatus); |
michael@0 | 163 | |
michael@0 | 164 | // Declare all atoms |
michael@0 | 165 | // |
michael@0 | 166 | // The atom names and values are stored in nsHttpAtomList.h and are brought |
michael@0 | 167 | // to you by the magic of C preprocessing. Add new atoms to nsHttpAtomList |
michael@0 | 168 | // and all support logic will be auto-generated. |
michael@0 | 169 | // |
michael@0 | 170 | #define HTTP_ATOM(_name, _value) static nsHttpAtom _name; |
michael@0 | 171 | #include "nsHttpAtomList.h" |
michael@0 | 172 | #undef HTTP_ATOM |
michael@0 | 173 | }; |
michael@0 | 174 | |
michael@0 | 175 | //----------------------------------------------------------------------------- |
michael@0 | 176 | // utilities... |
michael@0 | 177 | //----------------------------------------------------------------------------- |
michael@0 | 178 | |
michael@0 | 179 | static inline uint32_t |
michael@0 | 180 | PRTimeToSeconds(PRTime t_usec) |
michael@0 | 181 | { |
michael@0 | 182 | return uint32_t( t_usec / PR_USEC_PER_SEC ); |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | #define NowInSeconds() PRTimeToSeconds(PR_Now()) |
michael@0 | 186 | |
michael@0 | 187 | // Round q-value to 2 decimal places; return 2 most significant digits as uint. |
michael@0 | 188 | #define QVAL_TO_UINT(q) ((unsigned int) ((q + 0.005) * 100.0)) |
michael@0 | 189 | |
michael@0 | 190 | #define HTTP_LWS " \t" |
michael@0 | 191 | #define HTTP_HEADER_VALUE_SEPS HTTP_LWS "," |
michael@0 | 192 | |
michael@0 | 193 | } // namespace mozilla::net |
michael@0 | 194 | } // namespace mozilla |
michael@0 | 195 | |
michael@0 | 196 | #endif // nsHttp_h__ |