OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef NET_TOOLS_QUIC_TEST_TOOLS_TEST_TOOLS_HTTP_MESSAGE_H_ | |
6 #define NET_TOOLS_QUIC_TEST_TOOLS_TEST_TOOLS_HTTP_MESSAGE_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/macros.h" | |
12 #include "base/strings/string_piece.h" | |
13 #include "net/tools/balsa/balsa_enums.h" | |
14 #include "net/tools/balsa/balsa_headers.h" | |
15 | |
16 namespace net { | |
17 namespace test { | |
18 | |
19 class HttpConstants { | |
20 public: | |
21 enum Version { HTTP_UNKNOWN = 0, HTTP_0_9, HTTP_1_0, HTTP_1_1 }; | |
22 | |
23 enum Method { | |
24 UNKNOWN_METHOD = 0, | |
25 OPTIONS, | |
26 GET, | |
27 HEAD, | |
28 POST, | |
29 PUT, | |
30 DELETE, | |
31 TRACE, | |
32 CONNECT, | |
33 | |
34 MKCOL, | |
35 UNLOCK, | |
36 }; | |
37 }; | |
38 | |
39 // Stripped down wrapper class which basically contains headers and a body. | |
40 class HTTPMessage { | |
41 public: | |
42 typedef HttpConstants::Version Version; | |
43 typedef HttpConstants::Method Method; | |
44 | |
45 // Convenient functions to map strings into enums. The string passed in is | |
46 // not assumed to be NULL-terminated. | |
47 static Version StringToVersion(base::StringPiece str); | |
48 static Method StringToMethod(base::StringPiece str); | |
49 | |
50 static const char* MethodToString(Method method); | |
51 static const char* VersionToString(Version version); | |
52 | |
53 // Default constructor makes an empty HTTP/1.1 GET request. This is typically | |
54 // used to construct a message that will be Initialize()-ed. | |
55 HTTPMessage(); | |
56 | |
57 // Build a request message | |
58 HTTPMessage(Version version, Method request, const std::string& path); | |
59 | |
60 virtual ~HTTPMessage(); | |
61 | |
62 const std::string& body() const { return body_; } | |
63 | |
64 // Adds a header line to the message. | |
65 void AddHeader(const std::string& header, const std::string& value); | |
66 | |
67 // Removes a header line from the message. | |
68 void RemoveHeader(const std::string& header); | |
69 | |
70 // A utility function which calls RemoveHeader followed by AddHeader. | |
71 void ReplaceHeader(const std::string& header, const std::string& value); | |
72 | |
73 // Adds a body and the optional content-length header field (omitted to test | |
74 // read until close test case). To generate a message that has a header field | |
75 // of 0 content-length, call AddBody("", true). | |
76 // Multiple calls to AddBody()/AddChunkedBody() has the effect of overwriting | |
77 // the previous entry without warning. | |
78 void AddBody(const std::string& body, bool add_content_length); | |
79 | |
80 bool has_complete_message() const { return has_complete_message_; } | |
81 void set_has_complete_message(bool value) { has_complete_message_ = value; } | |
82 | |
83 // Do some basic http message consistency checks like: | |
84 // - Valid transfer-encoding header | |
85 // - Valid content-length header | |
86 // - Messages we expect to be complete are complete. | |
87 // This check can be disabled by setting skip_message_validation. | |
88 void ValidateMessage() const; | |
89 | |
90 bool skip_message_validation() const { return skip_message_validation_; } | |
91 void set_skip_message_validation(bool value) { | |
92 skip_message_validation_ = value; | |
93 } | |
94 | |
95 // Allow direct access to the body string. This should be used with caution: | |
96 // it will not update the request headers like AddBody and AddChunkedBody do. | |
97 void set_body(const std::string& body) { body_ = body; } | |
98 | |
99 const BalsaHeaders* headers() const { return &headers_; } | |
100 BalsaHeaders* headers() { return &headers_; } | |
101 | |
102 protected: | |
103 BalsaHeaders headers_; | |
104 | |
105 std::string body_; // the body with chunked framing/gzip compression | |
106 | |
107 bool is_request_; | |
108 | |
109 // True if the message should be considered complete during serialization. | |
110 // Used by SPDY and Streamed RPC clients to decide wherever or not | |
111 // to include fin flags and during message validation (if enabled). | |
112 bool has_complete_message_; | |
113 | |
114 // Allows disabling message validation when creating test messages | |
115 // that are intentionally invalid. | |
116 bool skip_message_validation_; | |
117 | |
118 private: | |
119 void InitializeFields(); | |
120 | |
121 DISALLOW_COPY_AND_ASSIGN(HTTPMessage); | |
122 }; | |
123 | |
124 } // namespace test | |
125 } // namespace net | |
126 | |
127 #endif // NET_TOOLS_QUIC_TEST_TOOLS_TEST_TOOLS_HTTP_MESSAGE_H_ | |
OLD | NEW |