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