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