|
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 |
|
13 namespace mozilla { namespace net { |
|
14 |
|
15 //----------------------------------------------------------------------------- |
|
16 // nsHttpRequestHead represents the request line and headers from an HTTP |
|
17 // request. |
|
18 //----------------------------------------------------------------------------- |
|
19 |
|
20 class nsHttpRequestHead |
|
21 { |
|
22 public: |
|
23 nsHttpRequestHead(); |
|
24 |
|
25 void SetMethod(const nsACString &method); |
|
26 void SetVersion(nsHttpVersion version) { mVersion = version; } |
|
27 void SetRequestURI(const nsCSubstring &s) { mRequestURI = s; } |
|
28 |
|
29 const nsHttpHeaderArray &Headers() const { return mHeaders; } |
|
30 nsHttpHeaderArray & Headers() { return mHeaders; } |
|
31 const nsCString &Method() const { return mMethod; } |
|
32 nsHttpVersion Version() const { return mVersion; } |
|
33 const nsCSubstring &RequestURI() const { return mRequestURI; } |
|
34 |
|
35 const char *PeekHeader(nsHttpAtom h) const |
|
36 { |
|
37 return mHeaders.PeekHeader(h); |
|
38 } |
|
39 nsresult SetHeader(nsHttpAtom h, const nsACString &v, bool m=false) { return mHeaders.SetHeader(h, v, m); } |
|
40 nsresult GetHeader(nsHttpAtom h, nsACString &v) const |
|
41 { |
|
42 return mHeaders.GetHeader(h, v); |
|
43 } |
|
44 void ClearHeader(nsHttpAtom h) { mHeaders.ClearHeader(h); } |
|
45 void ClearHeaders() { mHeaders.Clear(); } |
|
46 |
|
47 const char *FindHeaderValue(nsHttpAtom h, const char *v) const |
|
48 { |
|
49 return mHeaders.FindHeaderValue(h, v); |
|
50 } |
|
51 bool HasHeaderValue(nsHttpAtom h, const char *v) const |
|
52 { |
|
53 return mHeaders.HasHeaderValue(h, v); |
|
54 } |
|
55 |
|
56 void Flatten(nsACString &, bool pruneProxyHeaders = false); |
|
57 |
|
58 // Don't allow duplicate values |
|
59 nsresult SetHeaderOnce(nsHttpAtom h, const char *v, bool merge = false) |
|
60 { |
|
61 if (!merge || !HasHeaderValue(h, v)) |
|
62 return mHeaders.SetHeader(h, nsDependentCString(v), merge); |
|
63 return NS_OK; |
|
64 } |
|
65 |
|
66 bool IsSafeMethod() const; |
|
67 |
|
68 enum ParsedMethodType |
|
69 { |
|
70 kMethod_Custom, |
|
71 kMethod_Get, |
|
72 kMethod_Post, |
|
73 kMethod_Options, |
|
74 kMethod_Connect, |
|
75 kMethod_Head, |
|
76 kMethod_Put, |
|
77 kMethod_Trace |
|
78 }; |
|
79 |
|
80 ParsedMethodType ParsedMethod() const { return mParsedMethod; } |
|
81 bool EqualsMethod(ParsedMethodType aType) const { return mParsedMethod == aType; } |
|
82 bool IsGet() const { return EqualsMethod(kMethod_Get); } |
|
83 bool IsPost() const { return EqualsMethod(kMethod_Post); } |
|
84 bool IsOptions() const { return EqualsMethod(kMethod_Options); } |
|
85 bool IsConnect() const { return EqualsMethod(kMethod_Connect); } |
|
86 bool IsHead() const { return EqualsMethod(kMethod_Head); } |
|
87 bool IsPut() const { return EqualsMethod(kMethod_Put); } |
|
88 bool IsTrace() const { return EqualsMethod(kMethod_Trace); } |
|
89 |
|
90 private: |
|
91 // All members must be copy-constructable and assignable |
|
92 nsHttpHeaderArray mHeaders; |
|
93 nsCString mMethod; |
|
94 nsHttpVersion mVersion; |
|
95 nsCString mRequestURI; |
|
96 ParsedMethodType mParsedMethod; |
|
97 }; |
|
98 |
|
99 }} // namespace mozilla::net |
|
100 |
|
101 #endif // nsHttpRequestHead_h__ |