HttpRequest.h revision 021ebc2f
1/*
2 * Copyright 2010-2013 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5#ifndef _B_URL_PROTOCOL_HTTP_H_
6#define _B_URL_PROTOCOL_HTTP_H_
7
8
9#include <deque>
10
11#include <HttpForm.h>
12#include <HttpHeaders.h>
13#include <HttpResult.h>
14#include <NetBuffer.h>
15#include <NetworkAddress.h>
16#include <UrlRequest.h>
17
18
19class BAbstractSocket;
20
21
22class BHttpRequest : public BUrlRequest {
23public:
24								BHttpRequest(const BUrl& url,
25									bool ssl = false,
26									const char* protocolName = "HTTP",
27									BUrlProtocolListener* listener = NULL,
28									BUrlContext* context = NULL);
29								BHttpRequest(const BHttpRequest& other);
30	virtual						~BHttpRequest();
31
32			void				SetMethod(const char* const method);
33			void				SetFollowLocation(bool follow);
34			void				SetMaxRedirections(int8 maxRedirections);
35			void				SetReferrer(const BString& referrer);
36			void				SetUserAgent(const BString& agent);
37			void				SetDiscardData(bool discard);
38			void				SetDisableListener(bool disable);
39			void				SetAutoReferrer(bool enable);
40			void				SetUserName(const BString& name);
41			void				SetPassword(const BString& password);
42			void				SetRangeStart(off_t position);
43			void				SetRangeEnd(off_t position);
44
45			void				SetPostFields(const BHttpForm& fields);
46			void				SetHeaders(const BHttpHeaders& headers);
47
48			void				AdoptPostFields(BHttpForm* const fields);
49			void				AdoptInputData(BDataIO* const data,
50									const ssize_t size = -1);
51            void                AdoptHeaders(BHttpHeaders* const headers);
52
53			status_t			Stop();
54			const BUrlResult&	Result() const;
55
56	static	bool				IsInformationalStatusCode(int16 code);
57	static	bool				IsSuccessStatusCode(int16 code);
58	static	bool				IsRedirectionStatusCode(int16 code);
59	static	bool				IsClientErrorStatusCode(int16 code);
60	static	bool				IsServerErrorStatusCode(int16 code);
61	static	int16				StatusCodeClass(int16 code);
62
63private:
64			void				_ResetOptions();
65			status_t			_ProtocolLoop();
66			bool 				_ResolveHostName();
67			status_t			_MakeRequest();
68
69			void				_SendRequest();
70			void				_SendHeaders();
71			void				_SendPostData();
72
73			status_t			_GetLine(BString& destString);
74
75			void				_ParseStatus();
76			void				_ParseHeaders();
77
78	// URL result parameters access
79			BPositionIO*		_ResultRawData();
80			BHttpHeaders&		_ResultHeaders();
81			void				_SetResultStatusCode(int32 statusCode);
82			BString&			_ResultStatusText();
83
84	// Utility methods
85			bool				_IsDefaultPort();
86
87private:
88			BAbstractSocket*	fSocket;
89			BNetworkAddress		fRemoteAddr;
90			bool				fSSL;
91
92			BString				fRequestMethod;
93			int8				fHttpVersion;
94
95			BNetBuffer			fInputBuffer;
96
97			BHttpHeaders		fHeaders;
98
99	// Request status
100
101			BHttpHeaders		fOutputHeaders;
102			BHttpResult			fResult;
103
104			// Request state/events
105			enum {
106				kRequestInitialState,
107				kRequestStatusReceived,
108				kRequestHeadersReceived,
109				kRequestContentReceived,
110				kRequestTrailingHeadersReceived
111			}					fRequestStatus;
112
113
114	// Protocol options
115			uint8				fOptMaxRedirs;
116			BString				fOptReferer;
117			BString				fOptUserAgent;
118			BString				fOptUsername;
119			BString				fOptPassword;
120			uint32				fOptAuthMethods;
121			BHttpHeaders*		fOptHeaders;
122			BHttpForm*			fOptPostFields;
123			BDataIO*			fOptInputData;
124			ssize_t				fOptInputDataSize;
125			off_t				fOptRangeStart;
126			off_t				fOptRangeEnd;
127			bool				fOptSetCookies : 1;
128			bool				fOptFollowLocation : 1;
129			bool				fOptDiscardData : 1;
130			bool				fOptDisableListener : 1;
131			bool				fOptAutoReferer : 1;
132};
133
134// Request method
135const char* const B_HTTP_GET = "GET";
136const char* const B_HTTP_POST = "POST";
137const char* const B_HTTP_PUT = "PUT";
138const char* const B_HTTP_HEAD = "HEAD";
139const char* const B_HTTP_DELETE = "DELETE";
140const char* const B_HTTP_OPTIONS = "OPTIONS";
141const char* const B_HTTP_TRACE = "TRACE";
142const char* const B_HTTP_CONNECT = "CONNECT";
143
144
145// HTTP Version
146enum {
147	B_HTTP_10 = 1,
148	B_HTTP_11
149};
150
151
152// HTTP status classes
153enum http_status_code_class {
154	B_HTTP_STATUS_CLASS_INVALID			= 000,
155	B_HTTP_STATUS_CLASS_INFORMATIONAL 	= 100,
156	B_HTTP_STATUS_CLASS_SUCCESS			= 200,
157	B_HTTP_STATUS_CLASS_REDIRECTION		= 300,
158	B_HTTP_STATUS_CLASS_CLIENT_ERROR	= 400,
159	B_HTTP_STATUS_CLASS_SERVER_ERROR	= 500
160};
161
162
163// Known HTTP status codes
164enum http_status_code {
165	// Informational status codes
166	B_HTTP_STATUS__INFORMATIONAL_BASE	= 100,
167	B_HTTP_STATUS_CONTINUE = B_HTTP_STATUS__INFORMATIONAL_BASE,
168	B_HTTP_STATUS_SWITCHING_PROTOCOLS,
169	B_HTTP_STATUS__INFORMATIONAL_END,
170
171	// Success status codes
172	B_HTTP_STATUS__SUCCESS_BASE			= 200,
173	B_HTTP_STATUS_OK = B_HTTP_STATUS__SUCCESS_BASE,
174	B_HTTP_STATUS_CREATED,
175	B_HTTP_STATUS_ACCEPTED,
176	B_HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION,
177	B_HTTP_STATUS_NO_CONTENT,
178	B_HTTP_STATUS_RESET_CONTENT,
179	B_HTTP_STATUS_PARTIAL_CONTENT,
180	B_HTTP_STATUS__SUCCESS_END,
181
182	// Redirection status codes
183	B_HTTP_STATUS__REDIRECTION_BASE		= 300,
184	B_HTTP_STATUS_MULTIPLE_CHOICE = B_HTTP_STATUS__REDIRECTION_BASE,
185	B_HTTP_STATUS_MOVED_PERMANENTLY,
186	B_HTTP_STATUS_FOUND,
187	B_HTTP_STATUS_SEE_OTHER,
188	B_HTTP_STATUS_NOT_MODIFIED,
189	B_HTTP_STATUS_USE_PROXY,
190	B_HTTP_STATUS_TEMPORARY_REDIRECT,
191	B_HTTP_STATUS__REDIRECTION_END,
192
193	// Client error status codes
194	B_HTTP_STATUS__CLIENT_ERROR_BASE	= 400,
195	B_HTTP_STATUS_BAD_REQUEST = B_HTTP_STATUS__CLIENT_ERROR_BASE,
196	B_HTTP_STATUS_UNAUTHORIZED,
197	B_HTTP_STATUS_PAYMENT_REQUIRED,
198	B_HTTP_STATUS_FORBIDDEN,
199	B_HTTP_STATUS_NOT_FOUND,
200	B_HTTP_STATUS_METHOD_NOT_ALLOWED,
201	B_HTTP_STATUS_NOT_ACCEPTABLE,
202	B_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED,
203	B_HTTP_STATUS_REQUEST_TIMEOUT,
204	B_HTTP_STATUS_CONFLICT,
205	B_HTTP_STATUS_GONE,
206	B_HTTP_STATUS_LENGTH_REQUIRED,
207	B_HTTP_STATUS_PRECONDITION_FAILED,
208	B_HTTP_STATUS_REQUEST_ENTITY_TOO_LARGE,
209	B_HTTP_STATUS_REQUEST_URI_TOO_LARGE,
210	B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
211	B_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE,
212	B_HTTP_STATUS_EXPECTATION_FAILED,
213	B_HTTP_STATUS__CLIENT_ERROR_END,
214
215	// Server error status codes
216	B_HTTP_STATUS__SERVER_ERROR_BASE 	= 500,
217	B_HTTP_STATUS_INTERNAL_SERVER_ERROR = B_HTTP_STATUS__SERVER_ERROR_BASE,
218	B_HTTP_STATUS_NOT_IMPLEMENTED,
219	B_HTTP_STATUS_BAD_GATEWAY,
220	B_HTTP_STATUS_SERVICE_UNAVAILABLE,
221	B_HTTP_STATUS_GATEWAY_TIMEOUT,
222	B_HTTP_STATUS__SERVER_ERROR_END
223};
224
225
226// HTTP default User-Agent
227#define B_HTTP_PROTOCOL_USER_AGENT_FORMAT "ServicesKit (%s)"
228
229#endif // _B_URL_PROTOCOL_HTTP_H_
230