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