2001-05-12 01:04:09 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2005-04-01 22:35:14 +04:00
|
|
|
/* vim:set ts=4 sw=4 sts=4 et cin: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
#ifndef nsHttp_h__
|
|
|
|
#define nsHttp_h__
|
|
|
|
|
2001-09-29 02:23:26 +04:00
|
|
|
#if defined(MOZ_LOGGING)
|
|
|
|
#define FORCE_PR_LOG
|
|
|
|
#endif
|
|
|
|
|
Bug 530952: Electrolysis HTTP Channel implementation. author=jduell, r=bsmedberg, sr=bz
- Supports only primitive xpcshell HTTP requests which don't set/get HTTP
headers, don't do redirects, observers, load groups, or anything else other
than basic things like looking at the reply body, Content-type, Content-length, etc.
- Tested with network/test/unit_ipc/test_simple_wrap.js (patch @ bug 521922)
- Only used if "NECKO_E10S_HTTP" set in environment.
- Force http.h to get #included before any IPDL files, to centralize #define
handling of LOG and to make sure FORCE_PR_LOGGING is set if needed (bug
545995)
2009-09-22 22:55:33 +04:00
|
|
|
// e10s mess: IPDL-generatd headers include chromium which both #includes
|
|
|
|
// prlog.h, and #defines LOG in conflict with this file.
|
|
|
|
// Solution: (as described in bug 545995)
|
|
|
|
// 1) ensure that this file is #included before any IPDL-generated files and
|
|
|
|
// anything else that #includes prlog.h, so that we can make sure prlog.h
|
|
|
|
// sees FORCE_PR_LOG if needed.
|
|
|
|
// 2) #include IPDL boilerplate, and then undef LOG so our LOG wins.
|
|
|
|
// 3) nsNetModule.cpp does its own crazy stuff with #including prlog.h
|
|
|
|
// multiple times; allow it to define ALLOW_LATE_NSHTTP_H_INCLUDE to bypass
|
2012-08-28 05:21:20 +04:00
|
|
|
// check.
|
Bug 530952: Electrolysis HTTP Channel implementation. author=jduell, r=bsmedberg, sr=bz
- Supports only primitive xpcshell HTTP requests which don't set/get HTTP
headers, don't do redirects, observers, load groups, or anything else other
than basic things like looking at the reply body, Content-type, Content-length, etc.
- Tested with network/test/unit_ipc/test_simple_wrap.js (patch @ bug 521922)
- Only used if "NECKO_E10S_HTTP" set in environment.
- Force http.h to get #included before any IPDL files, to centralize #define
handling of LOG and to make sure FORCE_PR_LOGGING is set if needed (bug
545995)
2009-09-22 22:55:33 +04:00
|
|
|
#if defined(PR_LOG) && !defined(ALLOW_LATE_NSHTTP_H_INCLUDE)
|
|
|
|
#error "If nsHttp.h #included it must come before any IPDL-generated files or other files that #include prlog.h"
|
|
|
|
#endif
|
2009-10-22 03:01:16 +04:00
|
|
|
#include "mozilla/net/NeckoChild.h"
|
Bug 530952: Electrolysis HTTP Channel implementation. author=jduell, r=bsmedberg, sr=bz
- Supports only primitive xpcshell HTTP requests which don't set/get HTTP
headers, don't do redirects, observers, load groups, or anything else other
than basic things like looking at the reply body, Content-type, Content-length, etc.
- Tested with network/test/unit_ipc/test_simple_wrap.js (patch @ bug 521922)
- Only used if "NECKO_E10S_HTTP" set in environment.
- Force http.h to get #included before any IPDL files, to centralize #define
handling of LOG and to make sure FORCE_PR_LOGGING is set if needed (bug
545995)
2009-09-22 22:55:33 +04:00
|
|
|
#undef LOG
|
2009-10-22 03:01:16 +04:00
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
#include "plstr.h"
|
|
|
|
#include "prlog.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
2002-03-21 01:50:33 +03:00
|
|
|
#include "nsPromiseFlatString.h"
|
2003-06-19 03:16:17 +04:00
|
|
|
#include "nsURLHelper.h"
|
2002-05-03 11:49:43 +04:00
|
|
|
#include "netCore.h"
|
2001-05-12 01:04:09 +04:00
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// Log module for HTTP Protocol logging...
|
|
|
|
//
|
|
|
|
// To enable logging (see prlog.h for full details):
|
|
|
|
//
|
2001-06-14 02:49:56 +04:00
|
|
|
// set NSPR_LOG_MODULES=nsHttp:5
|
2001-05-12 01:04:09 +04:00
|
|
|
// set NSPR_LOG_FILE=http.log
|
|
|
|
//
|
|
|
|
// this enables PR_LOG_ALWAYS level information and places all output in
|
|
|
|
// the file http.log
|
|
|
|
//
|
|
|
|
extern PRLogModuleInfo *gHttpLog;
|
|
|
|
#endif
|
|
|
|
|
2001-08-14 05:03:27 +04:00
|
|
|
// http logging
|
2001-05-12 01:04:09 +04:00
|
|
|
#define LOG1(args) PR_LOG(gHttpLog, 1, args)
|
|
|
|
#define LOG2(args) PR_LOG(gHttpLog, 2, args)
|
|
|
|
#define LOG3(args) PR_LOG(gHttpLog, 3, args)
|
|
|
|
#define LOG4(args) PR_LOG(gHttpLog, 4, args)
|
|
|
|
#define LOG(args) LOG4(args)
|
|
|
|
|
2001-09-29 02:23:26 +04:00
|
|
|
#define LOG1_ENABLED() PR_LOG_TEST(gHttpLog, 1)
|
|
|
|
#define LOG2_ENABLED() PR_LOG_TEST(gHttpLog, 2)
|
|
|
|
#define LOG3_ENABLED() PR_LOG_TEST(gHttpLog, 3)
|
|
|
|
#define LOG4_ENABLED() PR_LOG_TEST(gHttpLog, 4)
|
|
|
|
#define LOG_ENABLED() LOG4_ENABLED()
|
|
|
|
|
2001-08-14 05:03:27 +04:00
|
|
|
// http version codes
|
|
|
|
#define NS_HTTP_VERSION_UNKNOWN 0
|
|
|
|
#define NS_HTTP_VERSION_0_9 9
|
|
|
|
#define NS_HTTP_VERSION_1_0 10
|
|
|
|
#define NS_HTTP_VERSION_1_1 11
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
typedef uint8_t nsHttpVersion;
|
2001-08-14 05:03:27 +04:00
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-08-14 05:03:27 +04:00
|
|
|
// http connection capabilities
|
2003-03-26 08:05:49 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
#define NS_HTTP_ALLOW_KEEPALIVE (1<<0)
|
|
|
|
#define NS_HTTP_ALLOW_PIPELINING (1<<1)
|
2003-03-26 08:05:49 +03:00
|
|
|
|
|
|
|
// a transaction with this caps flag will continue to own the connection,
|
|
|
|
// preventing it from being reclaimed, even after the transaction completes.
|
|
|
|
#define NS_HTTP_STICKY_CONNECTION (1<<2)
|
|
|
|
|
2008-11-04 19:05:46 +03:00
|
|
|
// a transaction with this caps flag will, upon opening a new connection,
|
|
|
|
// bypass the local DNS cache
|
|
|
|
#define NS_HTTP_REFRESH_DNS (1<<3)
|
|
|
|
|
2009-02-18 01:06:52 +03:00
|
|
|
// a transaction with this caps flag will not pass SSL client-certificates
|
|
|
|
// to the server (see bug #466080), but is may also be used for other things
|
|
|
|
#define NS_HTTP_LOAD_ANONYMOUS (1<<4)
|
|
|
|
|
2011-05-21 14:03:36 +04:00
|
|
|
// a transaction with this caps flag keeps timing information
|
|
|
|
#define NS_HTTP_TIMING_ENABLED (1<<5)
|
|
|
|
|
2011-07-22 20:31:37 +04:00
|
|
|
// a transaction with this caps flag will not only not use an existing
|
|
|
|
// persistent connection but it will close outstanding ones to the same
|
|
|
|
// host. Used by a forced reload to reset the connection states.
|
|
|
|
#define NS_HTTP_CLEAR_KEEPALIVES (1<<6)
|
|
|
|
|
2011-12-13 19:55:50 +04:00
|
|
|
// Disallow the use of the SPDY protocol. This is meant for the contexts
|
|
|
|
// such as HTTP upgrade which are nonsensical for SPDY, it is not the
|
|
|
|
// SPDY configuration variable.
|
|
|
|
#define NS_HTTP_DISALLOW_SPDY (1<<7)
|
|
|
|
|
2003-03-26 08:05:49 +03:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// some default values
|
|
|
|
//-----------------------------------------------------------------------------
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2003-03-01 05:18:05 +03:00
|
|
|
#define NS_HTTP_DEFAULT_PORT 80
|
|
|
|
#define NS_HTTPS_DEFAULT_PORT 443
|
|
|
|
|
2003-05-13 08:38:25 +04:00
|
|
|
#define NS_HTTP_HEADER_SEPS ", \t"
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// http atoms...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
struct nsHttpAtom
|
|
|
|
{
|
2006-02-18 01:52:40 +03:00
|
|
|
operator const char *() const { return _val; }
|
|
|
|
const char *get() const { return _val; }
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2001-09-27 03:30:28 +04:00
|
|
|
void operator=(const char *v) { _val = v; }
|
|
|
|
void operator=(const nsHttpAtom &a) { _val = a._val; }
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
// private
|
|
|
|
const char *_val;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsHttp
|
|
|
|
{
|
2005-04-01 22:35:14 +04:00
|
|
|
static nsresult CreateAtomTable();
|
2001-05-12 01:04:09 +04:00
|
|
|
static void DestroyAtomTable();
|
|
|
|
|
2012-03-23 03:39:31 +04:00
|
|
|
// The mutex is valid any time the Atom Table is valid
|
|
|
|
// This mutex is used in the unusual case that the network thread and
|
|
|
|
// main thread might access the same data
|
|
|
|
static mozilla::Mutex *GetLock();
|
|
|
|
|
2001-05-12 01:04:09 +04:00
|
|
|
// will dynamically add atoms to the table if they don't already exist
|
|
|
|
static nsHttpAtom ResolveAtom(const char *);
|
2005-04-01 22:35:14 +04:00
|
|
|
static nsHttpAtom ResolveAtom(const nsACString &s)
|
|
|
|
{
|
|
|
|
return ResolveAtom(PromiseFlatCString(s).get());
|
|
|
|
}
|
2001-05-12 01:04:09 +04:00
|
|
|
|
2006-04-07 21:45:44 +04:00
|
|
|
// returns true if the specified token [start,end) is valid per RFC 2616
|
|
|
|
// section 2.2
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool IsValidToken(const char *start, const char *end);
|
2006-04-07 21:45:44 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static inline bool IsValidToken(const nsCString &s) {
|
2006-04-07 21:45:44 +04:00
|
|
|
const char *start = s.get();
|
|
|
|
return IsValidToken(start, start + s.Length());
|
|
|
|
}
|
|
|
|
|
2006-02-18 01:52:40 +03:00
|
|
|
// find the first instance (case-insensitive comparison) of the given
|
|
|
|
// |token| in the |input| string. the |token| is bounded by elements of
|
|
|
|
// |separators| and may appear at the beginning or end of the |input|
|
|
|
|
// string. null is returned if the |token| is not found. |input| may be
|
|
|
|
// null, in which case null is returned.
|
|
|
|
static const char *FindToken(const char *input, const char *token,
|
|
|
|
const char *separators);
|
|
|
|
|
2006-04-07 21:44:32 +04:00
|
|
|
// This function parses a string containing a decimal-valued, non-negative
|
2012-09-28 23:55:23 +04:00
|
|
|
// 64-bit integer. If the value would exceed INT64_MAX, then false is
|
2011-10-17 18:59:28 +04:00
|
|
|
// returned. Otherwise, this function returns true and stores the
|
2006-04-07 21:44:32 +04:00
|
|
|
// parsed value in |result|. The next unparsed character in |input| is
|
|
|
|
// optionally returned via |next| if |next| is non-null.
|
|
|
|
//
|
|
|
|
// TODO(darin): Replace this with something generic.
|
|
|
|
//
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool ParseInt64(const char *input, const char **next,
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t *result);
|
2006-04-07 21:44:32 +04:00
|
|
|
|
|
|
|
// Variant on ParseInt64 that expects the input string to contain nothing
|
|
|
|
// more than the value being parsed.
|
2012-08-22 19:56:38 +04:00
|
|
|
static inline bool ParseInt64(const char *input, int64_t *result) {
|
2006-04-07 21:44:32 +04:00
|
|
|
const char *next;
|
|
|
|
return ParseInt64(input, &next, result) && *next == '\0';
|
|
|
|
}
|
|
|
|
|
2012-09-22 19:27:17 +04:00
|
|
|
// Return whether the HTTP status code represents a permanent redirect
|
2012-09-25 08:21:10 +04:00
|
|
|
static bool IsPermanentRedirect(PRUint32 httpStatus);
|
2012-09-22 19:27:17 +04:00
|
|
|
|
2012-09-25 19:04:25 +04:00
|
|
|
// Return whether upon a redirect code of httpStatus for method, the
|
|
|
|
// request method should be rewritten to GET.
|
|
|
|
static bool ShouldRewriteRedirectToGET(PRUint32 httpStatus, nsHttpAtom method);
|
|
|
|
|
|
|
|
// Return whether the specified method is safe as per RFC 2616,
|
|
|
|
// Section 9.1.1.
|
|
|
|
static bool IsSafeMethod(nsHttpAtom method);
|
|
|
|
|
2006-02-18 01:52:40 +03:00
|
|
|
// Declare all atoms
|
2012-08-28 05:21:20 +04:00
|
|
|
//
|
2006-02-18 01:52:40 +03:00
|
|
|
// The atom names and values are stored in nsHttpAtomList.h and are brought
|
|
|
|
// to you by the magic of C preprocessing. Add new atoms to nsHttpAtomList
|
|
|
|
// and all support logic will be auto-generated.
|
|
|
|
//
|
2001-05-12 01:04:09 +04:00
|
|
|
#define HTTP_ATOM(_name, _value) static nsHttpAtom _name;
|
|
|
|
#include "nsHttpAtomList.h"
|
|
|
|
#undef HTTP_ATOM
|
|
|
|
};
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// utilities...
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static inline uint32_t
|
2001-05-12 01:04:09 +04:00
|
|
|
PRTimeToSeconds(PRTime t_usec)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
return uint32_t( t_usec / PR_USEC_PER_SEC );
|
2001-05-12 01:04:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define NowInSeconds() PRTimeToSeconds(PR_Now())
|
|
|
|
|
2012-08-30 06:49:31 +04:00
|
|
|
// Round q-value to 2 decimal places; return 2 most significant digits as uint.
|
|
|
|
#define QVAL_TO_UINT(q) ((unsigned int) ((q + 0.005) * 100.0))
|
2002-11-20 23:51:12 +03:00
|
|
|
|
2003-06-19 03:16:17 +04:00
|
|
|
#define HTTP_LWS " \t"
|
2006-02-18 01:52:40 +03:00
|
|
|
#define HTTP_HEADER_VALUE_SEPS HTTP_LWS ","
|
2003-06-19 03:16:17 +04:00
|
|
|
|
2001-08-14 05:03:27 +04:00
|
|
|
#endif // nsHttp_h__
|