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