Back to index

lightning-sunbird  0.9+nobinonly
nsHttp.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* vim:set ts=4 sw=4 sts=4 et cin: */
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications.
00020  * Portions created by the Initial Developer are Copyright (C) 2001
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Darin Fisher <darin@netscape.com> (original author)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef nsHttp_h__
00041 #define nsHttp_h__
00042 
00043 #if defined(MOZ_LOGGING)
00044 #define FORCE_PR_LOG
00045 #endif
00046 
00047 #include "plstr.h"
00048 #include "prlog.h"
00049 #include "prtime.h"
00050 #include "nsISupportsUtils.h"
00051 #include "nsPromiseFlatString.h"
00052 #include "nsURLHelper.h"
00053 #include "netCore.h"
00054 
00055 #if defined(PR_LOGGING)
00056 //
00057 // Log module for HTTP Protocol logging...
00058 //
00059 // To enable logging (see prlog.h for full details):
00060 //
00061 //    set NSPR_LOG_MODULES=nsHttp:5
00062 //    set NSPR_LOG_FILE=http.log
00063 //
00064 // this enables PR_LOG_ALWAYS level information and places all output in
00065 // the file http.log
00066 //
00067 extern PRLogModuleInfo *gHttpLog;
00068 #endif
00069 
00070 // http logging
00071 #define LOG1(args) PR_LOG(gHttpLog, 1, args)
00072 #define LOG2(args) PR_LOG(gHttpLog, 2, args)
00073 #define LOG3(args) PR_LOG(gHttpLog, 3, args)
00074 #define LOG4(args) PR_LOG(gHttpLog, 4, args)
00075 #define LOG(args) LOG4(args)
00076 
00077 #define LOG1_ENABLED() PR_LOG_TEST(gHttpLog, 1)
00078 #define LOG2_ENABLED() PR_LOG_TEST(gHttpLog, 2)
00079 #define LOG3_ENABLED() PR_LOG_TEST(gHttpLog, 3)
00080 #define LOG4_ENABLED() PR_LOG_TEST(gHttpLog, 4)
00081 #define LOG_ENABLED() LOG4_ENABLED()
00082 
00083 // http default buffer geometry
00084 #define NS_HTTP_SEGMENT_SIZE  4096
00085 #define NS_HTTP_SEGMENT_COUNT 16   // 64k maximum
00086 #define NS_HTTP_MAX_ODA_SIZE  (NS_HTTP_SEGMENT_SIZE * 4) // 16k
00087 
00088 // http version codes
00089 #define NS_HTTP_VERSION_UNKNOWN  0
00090 #define NS_HTTP_VERSION_0_9      9
00091 #define NS_HTTP_VERSION_1_0     10
00092 #define NS_HTTP_VERSION_1_1     11
00093 
00094 typedef PRUint8 nsHttpVersion;
00095 
00096 //-----------------------------------------------------------------------------
00097 // http connection capabilities
00098 //-----------------------------------------------------------------------------
00099 
00100 #define NS_HTTP_ALLOW_KEEPALIVE      (1<<0)
00101 #define NS_HTTP_ALLOW_PIPELINING     (1<<1)
00102 
00103 // a transaction with this caps flag will continue to own the connection,
00104 // preventing it from being reclaimed, even after the transaction completes.
00105 #define NS_HTTP_STICKY_CONNECTION    (1<<2)
00106 
00107 //-----------------------------------------------------------------------------
00108 // some default values
00109 //-----------------------------------------------------------------------------
00110 
00111 // hard upper limit on the number of requests that can be pipelined
00112 #define NS_HTTP_MAX_PIPELINED_REQUESTS 8 
00113 
00114 #define NS_HTTP_DEFAULT_PORT  80
00115 #define NS_HTTPS_DEFAULT_PORT 443
00116 
00117 #define NS_HTTP_HEADER_SEPS ", \t"
00118 
00119 //-----------------------------------------------------------------------------
00120 // http atoms...
00121 //-----------------------------------------------------------------------------
00122 
00123 struct nsHttpAtom
00124 {
00125     operator const char *() { return _val; }
00126     const char *get() { return _val; }
00127 
00128     void operator=(const char *v) { _val = v; }
00129     void operator=(const nsHttpAtom &a) { _val = a._val; }
00130 
00131     // private
00132     const char *_val;
00133 };
00134 
00135 struct nsHttp
00136 {
00137     static nsresult CreateAtomTable();
00138     static void DestroyAtomTable();
00139 
00140     // will dynamically add atoms to the table if they don't already exist
00141     static nsHttpAtom ResolveAtom(const char *);
00142     static nsHttpAtom ResolveAtom(const nsACString &s)
00143     {
00144         return ResolveAtom(PromiseFlatCString(s).get());
00145     }
00146 
00147     // returns true if the specified token [start,end) is valid per RFC 2616
00148     // section 2.2
00149     static PRBool IsValidToken(const char *start, const char *end);
00150 
00151     static inline PRBool IsValidToken(const nsCString &s) {
00152         const char *start = s.get();
00153         return IsValidToken(start, start + s.Length());
00154     }
00155 
00156     // This function parses a string containing a decimal-valued, non-negative
00157     // 64-bit integer.  If the value would exceed LL_MAXINT, then PR_FALSE is
00158     // returned.  Otherwise, this function returns PR_TRUE and stores the
00159     // parsed value in |result|.  The next unparsed character in |input| is
00160     // optionally returned via |next| if |next| is non-null.
00161     //
00162     // TODO(darin): Replace this with something generic.
00163     //
00164     static PRBool ParseInt64(const char *input, const char **next,
00165                              PRInt64 *result);
00166 
00167     // Variant on ParseInt64 that expects the input string to contain nothing
00168     // more than the value being parsed.
00169     static inline PRBool ParseInt64(const char *input, PRInt64 *result) {
00170         const char *next;
00171         return ParseInt64(input, &next, result) && *next == '\0';
00172     }
00173 
00174     /* Declare all atoms
00175      *
00176      * The atom names and values are stored in nsHttpAtomList.h and
00177      * are brought to you by the magic of C preprocessing
00178      *
00179      * Add new atoms to nsHttpAtomList and all support logic will be auto-generated
00180      */
00181 #define HTTP_ATOM(_name, _value) static nsHttpAtom _name;
00182 #include "nsHttpAtomList.h"
00183 #undef HTTP_ATOM
00184 };
00185 
00186 //-----------------------------------------------------------------------------
00187 // utilities...
00188 //-----------------------------------------------------------------------------
00189 
00190 static inline PRUint32
00191 PRTimeToSeconds(PRTime t_usec)
00192 {
00193     PRTime usec_per_sec;
00194     PRUint32 t_sec;
00195     LL_I2L(usec_per_sec, PR_USEC_PER_SEC);
00196     LL_DIV(t_usec, t_usec, usec_per_sec);
00197     LL_L2I(t_sec, t_usec);
00198     return t_sec;
00199 }
00200 
00201 #define NowInSeconds() PRTimeToSeconds(PR_Now())
00202 
00203 // ripped from glib.h
00204 #undef  CLAMP
00205 #define CLAMP(x, low, high)  (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
00206 
00207 // nsCRT::strdup likes to convert nsnull to "" 
00208 #define strdup_if(s) (s ? nsCRT::strdup(s) : nsnull)
00209 
00210 // round q-value to one decimal place; return most significant digit as uint.
00211 #define QVAL_TO_UINT(q) ((unsigned int) ((q + 0.05) * 10.0))
00212 
00213 #define HTTP_LWS " \t"
00214 
00215 #endif // nsHttp_h__