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 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is Mozilla.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Netscape Communications.
20 : * Portions created by the Initial Developer are Copyright (C) 2001
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Darin Fisher <darin@netscape.com> (original author)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef nsHttp_h__
41 : #define nsHttp_h__
42 :
43 : #if defined(MOZ_LOGGING)
44 : #define FORCE_PR_LOG
45 : #endif
46 :
47 : // e10s mess: IPDL-generatd headers include chromium which both #includes
48 : // prlog.h, and #defines LOG in conflict with this file.
49 : // Solution: (as described in bug 545995)
50 : // 1) ensure that this file is #included before any IPDL-generated files and
51 : // anything else that #includes prlog.h, so that we can make sure prlog.h
52 : // sees FORCE_PR_LOG if needed.
53 : // 2) #include IPDL boilerplate, and then undef LOG so our LOG wins.
54 : // 3) nsNetModule.cpp does its own crazy stuff with #including prlog.h
55 : // multiple times; allow it to define ALLOW_LATE_NSHTTP_H_INCLUDE to bypass
56 : // check.
57 : #if defined(PR_LOG) && !defined(ALLOW_LATE_NSHTTP_H_INCLUDE)
58 : #error "If nsHttp.h #included it must come before any IPDL-generated files or other files that #include prlog.h"
59 : #endif
60 : #include "mozilla/net/NeckoChild.h"
61 : #undef LOG
62 :
63 : #include "plstr.h"
64 : #include "prlog.h"
65 : #include "prtime.h"
66 : #include "nsISupportsUtils.h"
67 : #include "nsPromiseFlatString.h"
68 : #include "nsURLHelper.h"
69 : #include "netCore.h"
70 :
71 : #if defined(PR_LOGGING)
72 : //
73 : // Log module for HTTP Protocol logging...
74 : //
75 : // To enable logging (see prlog.h for full details):
76 : //
77 : // set NSPR_LOG_MODULES=nsHttp:5
78 : // set NSPR_LOG_FILE=http.log
79 : //
80 : // this enables PR_LOG_ALWAYS level information and places all output in
81 : // the file http.log
82 : //
83 : extern PRLogModuleInfo *gHttpLog;
84 : #endif
85 :
86 : // http logging
87 : #define LOG1(args) PR_LOG(gHttpLog, 1, args)
88 : #define LOG2(args) PR_LOG(gHttpLog, 2, args)
89 : #define LOG3(args) PR_LOG(gHttpLog, 3, args)
90 : #define LOG4(args) PR_LOG(gHttpLog, 4, args)
91 : #define LOG(args) LOG4(args)
92 :
93 : #define LOG1_ENABLED() PR_LOG_TEST(gHttpLog, 1)
94 : #define LOG2_ENABLED() PR_LOG_TEST(gHttpLog, 2)
95 : #define LOG3_ENABLED() PR_LOG_TEST(gHttpLog, 3)
96 : #define LOG4_ENABLED() PR_LOG_TEST(gHttpLog, 4)
97 : #define LOG_ENABLED() LOG4_ENABLED()
98 :
99 : // http version codes
100 : #define NS_HTTP_VERSION_UNKNOWN 0
101 : #define NS_HTTP_VERSION_0_9 9
102 : #define NS_HTTP_VERSION_1_0 10
103 : #define NS_HTTP_VERSION_1_1 11
104 :
105 : typedef PRUint8 nsHttpVersion;
106 :
107 : //-----------------------------------------------------------------------------
108 : // http connection capabilities
109 : //-----------------------------------------------------------------------------
110 :
111 : #define NS_HTTP_ALLOW_KEEPALIVE (1<<0)
112 : #define NS_HTTP_ALLOW_PIPELINING (1<<1)
113 :
114 : // a transaction with this caps flag will continue to own the connection,
115 : // preventing it from being reclaimed, even after the transaction completes.
116 : #define NS_HTTP_STICKY_CONNECTION (1<<2)
117 :
118 : // a transaction with this caps flag will, upon opening a new connection,
119 : // bypass the local DNS cache
120 : #define NS_HTTP_REFRESH_DNS (1<<3)
121 :
122 : // a transaction with this caps flag will not pass SSL client-certificates
123 : // to the server (see bug #466080), but is may also be used for other things
124 : #define NS_HTTP_LOAD_ANONYMOUS (1<<4)
125 :
126 : // a transaction with this caps flag keeps timing information
127 : #define NS_HTTP_TIMING_ENABLED (1<<5)
128 :
129 : // a transaction with this caps flag will not only not use an existing
130 : // persistent connection but it will close outstanding ones to the same
131 : // host. Used by a forced reload to reset the connection states.
132 : #define NS_HTTP_CLEAR_KEEPALIVES (1<<6)
133 :
134 : // Disallow the use of the SPDY protocol. This is meant for the contexts
135 : // such as HTTP upgrade which are nonsensical for SPDY, it is not the
136 : // SPDY configuration variable.
137 : #define NS_HTTP_DISALLOW_SPDY (1<<7)
138 :
139 : //-----------------------------------------------------------------------------
140 : // some default values
141 : //-----------------------------------------------------------------------------
142 :
143 : // hard upper limit on the number of requests that can be pipelined
144 : #define NS_HTTP_MAX_PIPELINED_REQUESTS 8
145 :
146 : #define NS_HTTP_DEFAULT_PORT 80
147 : #define NS_HTTPS_DEFAULT_PORT 443
148 :
149 : #define NS_HTTP_HEADER_SEPS ", \t"
150 :
151 : //-----------------------------------------------------------------------------
152 : // http atoms...
153 : //-----------------------------------------------------------------------------
154 :
155 : struct nsHttpAtom
156 : {
157 3401867 : operator const char *() const { return _val; }
158 10947 : const char *get() const { return _val; }
159 :
160 : void operator=(const char *v) { _val = v; }
161 79407 : void operator=(const nsHttpAtom &a) { _val = a._val; }
162 :
163 : // private
164 : const char *_val;
165 : };
166 :
167 : struct nsHttp
168 : {
169 : static nsresult CreateAtomTable();
170 : static void DestroyAtomTable();
171 :
172 : // will dynamically add atoms to the table if they don't already exist
173 : static nsHttpAtom ResolveAtom(const char *);
174 9335 : static nsHttpAtom ResolveAtom(const nsACString &s)
175 : {
176 9335 : return ResolveAtom(PromiseFlatCString(s).get());
177 : }
178 :
179 : // returns true if the specified token [start,end) is valid per RFC 2616
180 : // section 2.2
181 : static bool IsValidToken(const char *start, const char *end);
182 :
183 9894 : static inline bool IsValidToken(const nsCString &s) {
184 9894 : const char *start = s.get();
185 9894 : return IsValidToken(start, start + s.Length());
186 : }
187 :
188 : // find the first instance (case-insensitive comparison) of the given
189 : // |token| in the |input| string. the |token| is bounded by elements of
190 : // |separators| and may appear at the beginning or end of the |input|
191 : // string. null is returned if the |token| is not found. |input| may be
192 : // null, in which case null is returned.
193 : static const char *FindToken(const char *input, const char *token,
194 : const char *separators);
195 :
196 : // This function parses a string containing a decimal-valued, non-negative
197 : // 64-bit integer. If the value would exceed LL_MAXINT, then false is
198 : // returned. Otherwise, this function returns true and stores the
199 : // parsed value in |result|. The next unparsed character in |input| is
200 : // optionally returned via |next| if |next| is non-null.
201 : //
202 : // TODO(darin): Replace this with something generic.
203 : //
204 : static bool ParseInt64(const char *input, const char **next,
205 : PRInt64 *result);
206 :
207 : // Variant on ParseInt64 that expects the input string to contain nothing
208 : // more than the value being parsed.
209 10 : static inline bool ParseInt64(const char *input, PRInt64 *result) {
210 : const char *next;
211 10 : return ParseInt64(input, &next, result) && *next == '\0';
212 : }
213 :
214 : // Declare all atoms
215 : //
216 : // The atom names and values are stored in nsHttpAtomList.h and are brought
217 : // to you by the magic of C preprocessing. Add new atoms to nsHttpAtomList
218 : // and all support logic will be auto-generated.
219 : //
220 : #define HTTP_ATOM(_name, _value) static nsHttpAtom _name;
221 : #include "nsHttpAtomList.h"
222 : #undef HTTP_ATOM
223 : };
224 :
225 : //-----------------------------------------------------------------------------
226 : // utilities...
227 : //-----------------------------------------------------------------------------
228 :
229 : static inline PRUint32
230 8341 : PRTimeToSeconds(PRTime t_usec)
231 : {
232 8341 : return PRUint32( t_usec / PR_USEC_PER_SEC );
233 : }
234 :
235 : #define NowInSeconds() PRTimeToSeconds(PR_Now())
236 :
237 : // round q-value to one decimal place; return most significant digit as uint.
238 : #define QVAL_TO_UINT(q) ((unsigned int) ((q + 0.05) * 10.0))
239 :
240 : #define HTTP_LWS " \t"
241 : #define HTTP_HEADER_VALUE_SEPS HTTP_LWS ","
242 :
243 : #endif // nsHttp_h__
|