HttpRequest.h revision a1cce970
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			BHttpResult			fResult;
102
103			// Request state/events
104			enum {
105				kRequestInitialState,
106				kRequestStatusReceived,
107				kRequestHeadersReceived,
108				kRequestContentReceived,
109				kRequestTrailingHeadersReceived
110			}					fRequestStatus;
111
112
113	// Protocol options
114			uint8				fOptMaxRedirs;
115			BString				fOptReferer;
116			BString				fOptUserAgent;
117			BString				fOptUsername;
118			BString				fOptPassword;
119			uint32				fOptAuthMethods;
120			BHttpHeaders*		fOptHeaders;
121			BHttpForm*			fOptPostFields;
122			BDataIO*			fOptInputData;
123			ssize_t				fOptInputDataSize;
124			off_t				fOptRangeStart;
125			off_t				fOptRangeEnd;
126			bool				fOptSetCookies : 1;
127			bool				fOptFollowLocation : 1;
128			bool				fOptDiscardData : 1;
129			bool				fOptDisableListener : 1;
130			bool				fOptAutoReferer : 1;
131};
132
133// Request method
134const char* const B_HTTP_GET = "GET";
135const char* const B_HTTP_POST = "POST";
136const char* const B_HTTP_PUT = "PUT";
137const char* const B_HTTP_HEAD = "HEAD";
138const char* const B_HTTP_DELETE = "DELETE";
139const char* const B_HTTP_OPTIONS = "OPTIONS";
140const char* const B_HTTP_TRACE = "TRACE";
141const char* const B_HTTP_CONNECT = "CONNECT";
142
143
144// HTTP Version
145enum {
146	B_HTTP_10 = 1,
147	B_HTTP_11
148};
149
150
151// HTTP status classes
152enum http_status_code_class {
153	B_HTTP_STATUS_CLASS_INVALID			= 000,
154	B_HTTP_STATUS_CLASS_INFORMATIONAL 	= 100,
155	B_HTTP_STATUS_CLASS_SUCCESS			= 200,
156	B_HTTP_STATUS_CLASS_REDIRECTION		= 300,
157	B_HTTP_STATUS_CLASS_CLIENT_ERROR	= 400,
158	B_HTTP_STATUS_CLASS_SERVER_ERROR	= 500
159};
160
161
162// Known HTTP status codes
163enum http_status_code {
164	// Informational status codes
165	B_HTTP_STATUS__INFORMATIONAL_BASE	= 100,
166	B_HTTP_STATUS_CONTINUE = B_HTTP_STATUS__INFORMATIONAL_BASE,
167	B_HTTP_STATUS_SWITCHING_PROTOCOLS,
168	B_HTTP_STATUS__INFORMATIONAL_END,
169
170	// Success status codes
171	B_HTTP_STATUS__SUCCESS_BASE			= 200,
172	B_HTTP_STATUS_OK = B_HTTP_STATUS__SUCCESS_BASE,
173	B_HTTP_STATUS_CREATED,
174	B_HTTP_STATUS_ACCEPTED,
175	B_HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION,
176	B_HTTP_STATUS_NO_CONTENT,
177	B_HTTP_STATUS_RESET_CONTENT,
178	B_HTTP_STATUS_PARTIAL_CONTENT,
179	B_HTTP_STATUS__SUCCESS_END,
180
181	// Redirection status codes
182	B_HTTP_STATUS__REDIRECTION_BASE		= 300,
183	B_HTTP_STATUS_MULTIPLE_CHOICE = B_HTTP_STATUS__REDIRECTION_BASE,
184	B_HTTP_STATUS_MOVED_PERMANENTLY,
185	B_HTTP_STATUS_FOUND,
186	B_HTTP_STATUS_SEE_OTHER,
187	B_HTTP_STATUS_NOT_MODIFIED,
188	B_HTTP_STATUS_USE_PROXY,
189	B_HTTP_STATUS_TEMPORARY_REDIRECT,
190	B_HTTP_STATUS__REDIRECTION_END,
191
192	// Client error status codes
193	B_HTTP_STATUS__CLIENT_ERROR_BASE	= 400,
194	B_HTTP_STATUS_BAD_REQUEST = B_HTTP_STATUS__CLIENT_ERROR_BASE,
195	B_HTTP_STATUS_UNAUTHORIZED,
196	B_HTTP_STATUS_PAYMENT_REQUIRED,
197	B_HTTP_STATUS_FORBIDDEN,
198	B_HTTP_STATUS_NOT_FOUND,
199	B_HTTP_STATUS_METHOD_NOT_ALLOWED,
200	B_HTTP_STATUS_NOT_ACCEPTABLE,
201	B_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED,
202	B_HTTP_STATUS_REQUEST_TIMEOUT,
203	B_HTTP_STATUS_CONFLICT,
204	B_HTTP_STATUS_GONE,
205	B_HTTP_STATUS_LENGTH_REQUIRED,
206	B_HTTP_STATUS_PRECONDITION_FAILED,
207	B_HTTP_STATUS_REQUEST_ENTITY_TOO_LARGE,
208	B_HTTP_STATUS_REQUEST_URI_TOO_LARGE,
209	B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
210	B_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE,
211	B_HTTP_STATUS_EXPECTATION_FAILED,
212	B_HTTP_STATUS__CLIENT_ERROR_END,
213
214	// Server error status codes
215	B_HTTP_STATUS__SERVER_ERROR_BASE 	= 500,
216	B_HTTP_STATUS_INTERNAL_SERVER_ERROR = B_HTTP_STATUS__SERVER_ERROR_BASE,
217	B_HTTP_STATUS_NOT_IMPLEMENTED,
218	B_HTTP_STATUS_BAD_GATEWAY,
219	B_HTTP_STATUS_SERVICE_UNAVAILABLE,
220	B_HTTP_STATUS_GATEWAY_TIMEOUT,
221	B_HTTP_STATUS__SERVER_ERROR_END
222};
223
224
225// HTTP default User-Agent
226#define B_HTTP_PROTOCOL_USER_AGENT_FORMAT "ServicesKit (%s)"
227
228#endif // _B_URL_PROTOCOL_HTTP_H_
229