Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef nsHttpRequestHead_h__
7 : #define nsHttpRequestHead_h__
8 :
9 : #include "nsHttp.h"
10 : #include "nsHttpHeaderArray.h"
11 : #include "nsString.h"
12 : #include "mozilla/ReentrantMonitor.h"
13 :
14 : class nsIHttpHeaderVisitor;
15 :
16 : namespace mozilla { namespace net {
17 :
18 : //-----------------------------------------------------------------------------
19 : // nsHttpRequestHead represents the request line and headers from an HTTP
20 : // request.
21 : //-----------------------------------------------------------------------------
22 :
23 : class nsHttpRequestHead
24 : {
25 : public:
26 : nsHttpRequestHead();
27 : ~nsHttpRequestHead();
28 :
29 : // The following function is only used in HttpChannelParent to avoid
30 : // copying headers. If you use it be careful to do it only under
31 : // nsHttpRequestHead lock!!!
32 : const nsHttpHeaderArray &Headers() const;
33 3 : void Enter() { mReentrantMonitor.Enter(); }
34 3 : void Exit() { mReentrantMonitor.Exit(); }
35 :
36 : void SetHeaders(const nsHttpHeaderArray& aHeaders);
37 :
38 : void SetMethod(const nsACString &method);
39 : void SetVersion(nsHttpVersion version);
40 : void SetRequestURI(const nsACString& s);
41 : void SetPath(const nsACString& s);
42 : uint32_t HeaderCount();
43 :
44 : // Using this function it is possible to itereate through all headers
45 : // automatically under one lock.
46 : MOZ_MUST_USE nsresult
47 : VisitHeaders(nsIHttpHeaderVisitor *visitor,
48 : nsHttpHeaderArray::VisitorFilter filter =
49 : nsHttpHeaderArray::eFilterAll);
50 : void Method(nsACString &aMethod);
51 : nsHttpVersion Version();
52 : void RequestURI(nsACString &RequestURI);
53 : void Path(nsACString &aPath);
54 : void SetHTTPS(bool val);
55 : bool IsHTTPS();
56 :
57 : void SetOrigin(const nsACString &scheme, const nsACString &host,
58 : int32_t port);
59 : void Origin(nsACString &aOrigin);
60 :
61 : MOZ_MUST_USE nsresult SetHeader(const nsACString &h, const nsACString &v,
62 : bool m=false);
63 : MOZ_MUST_USE nsresult SetHeader(nsHttpAtom h, const nsACString &v,
64 : bool m=false);
65 : MOZ_MUST_USE nsresult SetHeader(nsHttpAtom h, const nsACString &v, bool m,
66 : nsHttpHeaderArray::HeaderVariety variety);
67 : MOZ_MUST_USE nsresult SetEmptyHeader(const nsACString &h);
68 : MOZ_MUST_USE nsresult GetHeader(nsHttpAtom h, nsACString &v);
69 :
70 : MOZ_MUST_USE nsresult ClearHeader(nsHttpAtom h);
71 : void ClearHeaders();
72 :
73 : bool HasHeaderValue(nsHttpAtom h, const char *v);
74 : // This function returns true if header is set even if it is an empty
75 : // header.
76 : bool HasHeader(nsHttpAtom h);
77 : void Flatten(nsACString &, bool pruneProxyHeaders = false);
78 :
79 : // Don't allow duplicate values
80 : MOZ_MUST_USE nsresult SetHeaderOnce(nsHttpAtom h, const char *v,
81 : bool merge = false);
82 :
83 : bool IsSafeMethod();
84 :
85 : enum ParsedMethodType
86 : {
87 : kMethod_Custom,
88 : kMethod_Get,
89 : kMethod_Post,
90 : kMethod_Options,
91 : kMethod_Connect,
92 : kMethod_Head,
93 : kMethod_Put,
94 : kMethod_Trace
95 : };
96 :
97 : ParsedMethodType ParsedMethod();
98 : bool EqualsMethod(ParsedMethodType aType);
99 8 : bool IsGet() { return EqualsMethod(kMethod_Get); }
100 9 : bool IsPost() { return EqualsMethod(kMethod_Post); }
101 1 : bool IsOptions() { return EqualsMethod(kMethod_Options); }
102 1 : bool IsConnect() { return EqualsMethod(kMethod_Connect); }
103 4 : bool IsHead() { return EqualsMethod(kMethod_Head); }
104 2 : bool IsPut() { return EqualsMethod(kMethod_Put); }
105 1 : bool IsTrace() { return EqualsMethod(kMethod_Trace); }
106 : void ParseHeaderSet(const char *buffer);
107 : private:
108 : // All members must be copy-constructable and assignable
109 : nsHttpHeaderArray mHeaders;
110 : nsCString mMethod;
111 : nsHttpVersion mVersion;
112 :
113 : // mRequestURI and mPath are strings instead of an nsIURI
114 : // because this is used off the main thread
115 : nsCString mRequestURI;
116 : nsCString mPath;
117 :
118 : nsCString mOrigin;
119 : ParsedMethodType mParsedMethod;
120 : bool mHTTPS;
121 :
122 : // We are using ReentrantMonitor instead of a Mutex because VisitHeader
123 : // function calls nsIHttpHeaderVisitor::VisitHeader while under lock.
124 : ReentrantMonitor mReentrantMonitor;
125 :
126 : // During VisitHeader we sould not allow cal to SetHeader.
127 : bool mInVisitHeaders;
128 : };
129 :
130 : } // namespace net
131 : } // namespace mozilla
132 :
133 : #endif // nsHttpRequestHead_h__
|