xref: /haiku/headers/private/netservices2/HttpResult.h (revision 71e29bbeea760848317843508c711f2a0b446fbb)
1 /*
2  * Copyright 2022 Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #ifndef _B_HTTP_RESULT_H_
7 #define _B_HTTP_RESULT_H_
8 
9 #include <memory>
10 #include <optional>
11 
12 #include <String.h>
13 
14 class BDataIO;
15 
16 
17 namespace BPrivate {
18 
19 namespace Network {
20 
21 class BHttpFields;
22 struct HttpResultPrivate;
23 
24 
25 struct BHttpBody {
26 			std::optional<BString> text;
27 };
28 
29 
30 enum class BHttpStatusClass : int16 {
31 	Invalid = 000,
32 	Informational = 100,
33 	Success = 200,
34 	Redirection = 300,
35 	ClientError = 400,
36 	ServerError = 500
37 };
38 
39 
40 enum class BHttpStatusCode : int16 {
41 	Unknown = 0,
42 
43 	// Informational status codes
44 	Continue = 100,
45 	SwitchingProtocols,
46 
47 	// Success status codes
48 	Ok = 200,
49 	Created,
50 	Accepted,
51 	NonAuthoritativeInformation,
52 	NoContent,
53 	ResetContent,
54 	PartialContent,
55 
56 	// Redirection status codes
57 	MultipleChoice = 300,
58 	MovedPermanently,
59 	Found,
60 	SeeOther,
61 	NotModified,
62 	UseProxy,
63 	TemporaryRedirect = 307,
64 	PermanentRedirect,
65 
66 	// Client error status codes
67 	BadRequest = 400,
68 	Unauthorized,
69 	PaymentRequired,
70 	Forbidden,
71 	NotFound,
72 	MethodNotAllowed,
73 	NotAcceptable,
74 	ProxyAuthenticationRequired,
75 	RequestTimeout,
76 	Conflict,
77 	Gone,
78 	LengthRequired,
79 	PreconditionFailed,
80 	RequestEntityTooLarge,
81 	RequestUriTooLarge,
82 	UnsupportedMediaType,
83 	RequestedRangeNotSatisfiable,
84 	ExpectationFailed,
85 
86 	// Server error status codes
87 	InternalServerError = 500,
88 	NotImplemented,
89 	BadGateway,
90 	ServiceUnavailable,
91 	GatewayTimeout,
92 };
93 
94 
95 struct BHttpStatus {
96 			int16				code = 0;
97 			BString				text;
98 
99 	// Helpers
100 			BHttpStatusClass	StatusClass() const noexcept;
101 			BHttpStatusCode		StatusCode() const noexcept;
102 };
103 
104 
105 class BHttpResult
106 {
107 public:
108 	// Constructors and destructor
109 								BHttpResult(const BHttpResult& other) = delete;
110 								BHttpResult(BHttpResult&& other) noexcept;
111 								~BHttpResult();
112 
113 	// Assignment operators
114 			BHttpResult&		operator=(const BHttpResult& other) = delete;
115 			BHttpResult&		operator=(BHttpResult&& other) noexcept;
116 
117 	// Blocking Access Functions
118 			const BHttpStatus&	Status() const;
119 			const BHttpFields&	Fields() const;
120 			BHttpBody&			Body() const;
121 
122 	// Check if data is available yet
123 			bool				HasStatus() const;
124 			bool				HasFields() const;
125 			bool				HasBody() const;
126 			bool				IsCompleted() const;
127 
128 	// Identity
129 			int32				Identity() const;
130 
131 private:
132 	friend class BHttpSession;
133 								BHttpResult(std::shared_ptr<HttpResultPrivate> data);
134 			std::shared_ptr<HttpResultPrivate> fData;
135 };
136 
137 
138 } // namespace Network
139 
140 } // namespace BPrivate
141 
142 #endif // _B_HTTP_RESPONSE_H_
143