|
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 // HttpLog.h should generally be included first |
|
7 #include "HttpLog.h" |
|
8 |
|
9 #include "nsHttpRequestHead.h" |
|
10 |
|
11 //----------------------------------------------------------------------------- |
|
12 // nsHttpRequestHead |
|
13 //----------------------------------------------------------------------------- |
|
14 |
|
15 namespace mozilla { |
|
16 namespace net { |
|
17 |
|
18 nsHttpRequestHead::nsHttpRequestHead() |
|
19 : mMethod(NS_LITERAL_CSTRING("GET")) |
|
20 , mVersion(NS_HTTP_VERSION_1_1) |
|
21 , mParsedMethod(kMethod_Get) |
|
22 { |
|
23 } |
|
24 |
|
25 void |
|
26 nsHttpRequestHead::SetMethod(const nsACString &method) |
|
27 { |
|
28 mParsedMethod = kMethod_Custom; |
|
29 mMethod = method; |
|
30 if (!strcmp(mMethod.get(), "GET")) { |
|
31 mParsedMethod = kMethod_Get; |
|
32 } else if (!strcmp(mMethod.get(), "POST")) { |
|
33 mParsedMethod = kMethod_Post; |
|
34 } else if (!strcmp(mMethod.get(), "OPTIONS")) { |
|
35 mParsedMethod = kMethod_Options; |
|
36 } else if (!strcmp(mMethod.get(), "CONNECT")) { |
|
37 mParsedMethod = kMethod_Connect; |
|
38 } else if (!strcmp(mMethod.get(), "HEAD")) { |
|
39 mParsedMethod = kMethod_Head; |
|
40 } else if (!strcmp(mMethod.get(), "PUT")) { |
|
41 mParsedMethod = kMethod_Put; |
|
42 } else if (!strcmp(mMethod.get(), "TRACE")) { |
|
43 mParsedMethod = kMethod_Trace; |
|
44 } |
|
45 } |
|
46 |
|
47 bool |
|
48 nsHttpRequestHead::IsSafeMethod() const |
|
49 { |
|
50 // This code will need to be extended for new safe methods, otherwise |
|
51 // they'll default to "not safe". |
|
52 if (IsGet() || IsHead() || IsOptions() || IsTrace()) { |
|
53 return true; |
|
54 } |
|
55 |
|
56 if (mParsedMethod != kMethod_Custom) { |
|
57 return false; |
|
58 } |
|
59 |
|
60 return (!strcmp(mMethod.get(), "PROPFIND") || |
|
61 !strcmp(mMethod.get(), "REPORT") || |
|
62 !strcmp(mMethod.get(), "SEARCH")); |
|
63 } |
|
64 |
|
65 void |
|
66 nsHttpRequestHead::Flatten(nsACString &buf, bool pruneProxyHeaders) |
|
67 { |
|
68 // note: the first append is intentional. |
|
69 |
|
70 buf.Append(mMethod); |
|
71 buf.Append(' '); |
|
72 buf.Append(mRequestURI); |
|
73 buf.AppendLiteral(" HTTP/"); |
|
74 |
|
75 switch (mVersion) { |
|
76 case NS_HTTP_VERSION_1_1: |
|
77 buf.AppendLiteral("1.1"); |
|
78 break; |
|
79 case NS_HTTP_VERSION_0_9: |
|
80 buf.AppendLiteral("0.9"); |
|
81 break; |
|
82 default: |
|
83 buf.AppendLiteral("1.0"); |
|
84 } |
|
85 |
|
86 buf.AppendLiteral("\r\n"); |
|
87 |
|
88 mHeaders.Flatten(buf, pruneProxyHeaders); |
|
89 } |
|
90 |
|
91 } // namespace mozilla::net |
|
92 } // namespace mozilla |