2010-10-27 18:03:31 +04:00
|
|
|
/*
|
2013-11-05 05:16:26 +04:00
|
|
|
* Copyright 2010-2013 Haiku Inc. All rights reserved.
|
2010-10-27 18:03:31 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*/
|
|
|
|
#ifndef _B_URL_PROTOCOL_HTTP_H_
|
|
|
|
#define _B_URL_PROTOCOL_HTTP_H_
|
|
|
|
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
|
2014-11-04 18:19:56 +03:00
|
|
|
#include <Certificate.h>
|
2010-10-27 18:03:31 +04:00
|
|
|
#include <HttpForm.h>
|
|
|
|
#include <HttpHeaders.h>
|
2013-10-24 19:26:17 +04:00
|
|
|
#include <HttpResult.h>
|
2012-05-12 11:53:47 +04:00
|
|
|
#include <NetworkAddress.h>
|
2014-08-04 17:59:53 +04:00
|
|
|
#include <NetworkRequest.h>
|
2010-10-27 18:03:31 +04:00
|
|
|
|
|
|
|
|
2015-11-11 03:06:01 +03:00
|
|
|
namespace BPrivate {
|
|
|
|
class CheckedSecureSocket;
|
|
|
|
class CheckedProxySecureSocket;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-08-04 17:59:53 +04:00
|
|
|
class BHttpRequest : public BNetworkRequest {
|
2010-10-27 18:03:31 +04:00
|
|
|
public:
|
2013-10-08 13:30:56 +04:00
|
|
|
BHttpRequest(const BUrl& url,
|
Network Kit: Coverity scan review and fixes
CID 1108353, 1108335: memory leak.
CID 610473: unused variable.
CID 1108446, 1108433, 1108432, 1108419, 1108400, 991710, 991713, 991712,
610098, 610097, 610096, 610095: uninitialized field
CID 1108421: unused field
Change the ownership of the result for Url/HttpRequests. The request now
owns its result and you either access it by reference while the request
is live, or copy it to keep it after the request destruction. To help
with that, get BUrlResult copy constructor and assignment operator to
work.
Performance issue: copying the BUrlResult also copies the underlying
BMallocIO data. This should be shared between the BUrlResult objects to
make the copy lighter. The case of BUrlSynchronousRequest is now
particularly inefficient, with at least 2 copies needed to get at the
result.
2013-10-20 01:31:57 +04:00
|
|
|
bool ssl = false,
|
2014-06-06 01:18:45 +04:00
|
|
|
const char* protocolName = "HTTP",
|
2010-10-27 18:03:31 +04:00
|
|
|
BUrlProtocolListener* listener = NULL,
|
2013-10-08 13:30:56 +04:00
|
|
|
BUrlContext* context = NULL);
|
2014-04-12 10:48:57 +04:00
|
|
|
BHttpRequest(const BHttpRequest& other);
|
2013-10-08 13:30:56 +04:00
|
|
|
virtual ~BHttpRequest();
|
|
|
|
|
2013-11-05 05:16:26 +04:00
|
|
|
void SetMethod(const char* const method);
|
|
|
|
void SetFollowLocation(bool follow);
|
|
|
|
void SetMaxRedirections(int8 maxRedirections);
|
|
|
|
void SetReferrer(const BString& referrer);
|
|
|
|
void SetUserAgent(const BString& agent);
|
|
|
|
void SetDiscardData(bool discard);
|
|
|
|
void SetDisableListener(bool disable);
|
|
|
|
void SetAutoReferrer(bool enable);
|
|
|
|
void SetUserName(const BString& name);
|
|
|
|
void SetPassword(const BString& password);
|
2014-04-12 10:48:57 +04:00
|
|
|
void SetRangeStart(off_t position);
|
|
|
|
void SetRangeEnd(off_t position);
|
2013-11-05 05:16:26 +04:00
|
|
|
|
|
|
|
void SetPostFields(const BHttpForm& fields);
|
|
|
|
void SetHeaders(const BHttpHeaders& headers);
|
|
|
|
|
|
|
|
void AdoptPostFields(BHttpForm* const fields);
|
|
|
|
void AdoptInputData(BDataIO* const data,
|
2013-10-17 16:10:23 +04:00
|
|
|
const ssize_t size = -1);
|
2014-10-21 13:12:10 +04:00
|
|
|
void AdoptHeaders(BHttpHeaders* const headers);
|
2013-10-17 16:10:23 +04:00
|
|
|
|
2014-01-02 11:35:54 +04:00
|
|
|
status_t Stop();
|
2013-12-09 23:26:13 +04:00
|
|
|
const BUrlResult& Result() const;
|
2013-10-24 19:26:17 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
static bool IsInformationalStatusCode(int16 code);
|
|
|
|
static bool IsSuccessStatusCode(int16 code);
|
|
|
|
static bool IsRedirectionStatusCode(int16 code);
|
|
|
|
static bool IsClientErrorStatusCode(int16 code);
|
|
|
|
static bool IsServerErrorStatusCode(int16 code);
|
|
|
|
static int16 StatusCodeClass(int16 code);
|
|
|
|
|
|
|
|
private:
|
|
|
|
void _ResetOptions();
|
|
|
|
status_t _ProtocolLoop();
|
|
|
|
status_t _MakeRequest();
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2019-01-27 10:42:30 +03:00
|
|
|
BString _SerializeRequest();
|
|
|
|
BString _SerializeHeaders();
|
2014-07-21 13:45:13 +04:00
|
|
|
void _SendPostData();
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
void _ParseStatus();
|
|
|
|
void _ParseHeaders();
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2013-10-24 19:26:17 +04:00
|
|
|
// URL result parameters access
|
2014-06-06 01:18:45 +04:00
|
|
|
BPositionIO* _ResultRawData();
|
|
|
|
BHttpHeaders& _ResultHeaders();
|
|
|
|
void _SetResultStatusCode(int32 statusCode);
|
|
|
|
BString& _ResultStatusText();
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2014-11-04 18:19:56 +03:00
|
|
|
// SSL failure management
|
2015-11-11 03:06:01 +03:00
|
|
|
friend class BPrivate::CheckedSecureSocket;
|
|
|
|
friend class BPrivate::CheckedProxySecureSocket;
|
2014-11-04 18:19:56 +03:00
|
|
|
bool _CertificateVerificationFailed(
|
|
|
|
BCertificate& certificate,
|
|
|
|
const char* message);
|
|
|
|
|
2014-07-28 13:05:58 +04:00
|
|
|
// Utility methods
|
|
|
|
bool _IsDefaultPort();
|
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
private:
|
2012-05-12 14:37:07 +04:00
|
|
|
bool fSSL;
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2013-10-15 16:43:09 +04:00
|
|
|
BString fRequestMethod;
|
2010-10-27 18:03:31 +04:00
|
|
|
int8 fHttpVersion;
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
BHttpHeaders fHeaders;
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
// Request status
|
2013-10-09 17:46:10 +04:00
|
|
|
|
2013-10-24 19:26:17 +04:00
|
|
|
BHttpResult fResult;
|
2013-10-09 17:46:10 +04:00
|
|
|
|
|
|
|
// Request state/events
|
|
|
|
enum {
|
|
|
|
kRequestInitialState,
|
|
|
|
kRequestStatusReceived,
|
|
|
|
kRequestHeadersReceived,
|
|
|
|
kRequestContentReceived,
|
|
|
|
kRequestTrailingHeadersReceived
|
|
|
|
} fRequestStatus;
|
2013-02-11 00:36:34 +04:00
|
|
|
|
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
// Protocol options
|
|
|
|
uint8 fOptMaxRedirs;
|
|
|
|
BString fOptReferer;
|
|
|
|
BString fOptUserAgent;
|
|
|
|
BString fOptUsername;
|
|
|
|
BString fOptPassword;
|
|
|
|
uint32 fOptAuthMethods;
|
|
|
|
BHttpHeaders* fOptHeaders;
|
|
|
|
BHttpForm* fOptPostFields;
|
|
|
|
BDataIO* fOptInputData;
|
2013-10-08 13:30:56 +04:00
|
|
|
ssize_t fOptInputDataSize;
|
2014-04-12 10:48:57 +04:00
|
|
|
off_t fOptRangeStart;
|
|
|
|
off_t fOptRangeEnd;
|
2010-10-27 18:03:31 +04:00
|
|
|
bool fOptSetCookies : 1;
|
|
|
|
bool fOptFollowLocation : 1;
|
|
|
|
bool fOptDiscardData : 1;
|
|
|
|
bool fOptDisableListener : 1;
|
|
|
|
bool fOptAutoReferer : 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Request method
|
2013-10-15 16:43:09 +04:00
|
|
|
const char* const B_HTTP_GET = "GET";
|
|
|
|
const char* const B_HTTP_POST = "POST";
|
|
|
|
const char* const B_HTTP_PUT = "PUT";
|
|
|
|
const char* const B_HTTP_HEAD = "HEAD";
|
|
|
|
const char* const B_HTTP_DELETE = "DELETE";
|
|
|
|
const char* const B_HTTP_OPTIONS = "OPTIONS";
|
|
|
|
const char* const B_HTTP_TRACE = "TRACE";
|
|
|
|
const char* const B_HTTP_CONNECT = "CONNECT";
|
2010-10-27 18:03:31 +04:00
|
|
|
|
|
|
|
|
|
|
|
// HTTP Version
|
|
|
|
enum {
|
|
|
|
B_HTTP_10 = 1,
|
|
|
|
B_HTTP_11
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// HTTP status classes
|
|
|
|
enum http_status_code_class {
|
|
|
|
B_HTTP_STATUS_CLASS_INVALID = 000,
|
|
|
|
B_HTTP_STATUS_CLASS_INFORMATIONAL = 100,
|
|
|
|
B_HTTP_STATUS_CLASS_SUCCESS = 200,
|
|
|
|
B_HTTP_STATUS_CLASS_REDIRECTION = 300,
|
|
|
|
B_HTTP_STATUS_CLASS_CLIENT_ERROR = 400,
|
|
|
|
B_HTTP_STATUS_CLASS_SERVER_ERROR = 500
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Known HTTP status codes
|
|
|
|
enum http_status_code {
|
|
|
|
// Informational status codes
|
|
|
|
B_HTTP_STATUS__INFORMATIONAL_BASE = 100,
|
|
|
|
B_HTTP_STATUS_CONTINUE = B_HTTP_STATUS__INFORMATIONAL_BASE,
|
|
|
|
B_HTTP_STATUS_SWITCHING_PROTOCOLS,
|
|
|
|
B_HTTP_STATUS__INFORMATIONAL_END,
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
// Success status codes
|
|
|
|
B_HTTP_STATUS__SUCCESS_BASE = 200,
|
|
|
|
B_HTTP_STATUS_OK = B_HTTP_STATUS__SUCCESS_BASE,
|
|
|
|
B_HTTP_STATUS_CREATED,
|
|
|
|
B_HTTP_STATUS_ACCEPTED,
|
|
|
|
B_HTTP_STATUS_NON_AUTHORITATIVE_INFORMATION,
|
|
|
|
B_HTTP_STATUS_NO_CONTENT,
|
|
|
|
B_HTTP_STATUS_RESET_CONTENT,
|
|
|
|
B_HTTP_STATUS_PARTIAL_CONTENT,
|
|
|
|
B_HTTP_STATUS__SUCCESS_END,
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
// Redirection status codes
|
|
|
|
B_HTTP_STATUS__REDIRECTION_BASE = 300,
|
|
|
|
B_HTTP_STATUS_MULTIPLE_CHOICE = B_HTTP_STATUS__REDIRECTION_BASE,
|
|
|
|
B_HTTP_STATUS_MOVED_PERMANENTLY,
|
|
|
|
B_HTTP_STATUS_FOUND,
|
|
|
|
B_HTTP_STATUS_SEE_OTHER,
|
|
|
|
B_HTTP_STATUS_NOT_MODIFIED,
|
|
|
|
B_HTTP_STATUS_USE_PROXY,
|
|
|
|
B_HTTP_STATUS_TEMPORARY_REDIRECT,
|
|
|
|
B_HTTP_STATUS__REDIRECTION_END,
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
// Client error status codes
|
|
|
|
B_HTTP_STATUS__CLIENT_ERROR_BASE = 400,
|
|
|
|
B_HTTP_STATUS_BAD_REQUEST = B_HTTP_STATUS__CLIENT_ERROR_BASE,
|
|
|
|
B_HTTP_STATUS_UNAUTHORIZED,
|
|
|
|
B_HTTP_STATUS_PAYMENT_REQUIRED,
|
|
|
|
B_HTTP_STATUS_FORBIDDEN,
|
|
|
|
B_HTTP_STATUS_NOT_FOUND,
|
|
|
|
B_HTTP_STATUS_METHOD_NOT_ALLOWED,
|
|
|
|
B_HTTP_STATUS_NOT_ACCEPTABLE,
|
|
|
|
B_HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED,
|
|
|
|
B_HTTP_STATUS_REQUEST_TIMEOUT,
|
|
|
|
B_HTTP_STATUS_CONFLICT,
|
|
|
|
B_HTTP_STATUS_GONE,
|
|
|
|
B_HTTP_STATUS_LENGTH_REQUIRED,
|
|
|
|
B_HTTP_STATUS_PRECONDITION_FAILED,
|
|
|
|
B_HTTP_STATUS_REQUEST_ENTITY_TOO_LARGE,
|
|
|
|
B_HTTP_STATUS_REQUEST_URI_TOO_LARGE,
|
|
|
|
B_HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE,
|
|
|
|
B_HTTP_STATUS_REQUESTED_RANGE_NOT_SATISFIABLE,
|
|
|
|
B_HTTP_STATUS_EXPECTATION_FAILED,
|
|
|
|
B_HTTP_STATUS__CLIENT_ERROR_END,
|
2013-02-11 00:36:34 +04:00
|
|
|
|
2010-10-27 18:03:31 +04:00
|
|
|
// Server error status codes
|
|
|
|
B_HTTP_STATUS__SERVER_ERROR_BASE = 500,
|
|
|
|
B_HTTP_STATUS_INTERNAL_SERVER_ERROR = B_HTTP_STATUS__SERVER_ERROR_BASE,
|
|
|
|
B_HTTP_STATUS_NOT_IMPLEMENTED,
|
|
|
|
B_HTTP_STATUS_BAD_GATEWAY,
|
|
|
|
B_HTTP_STATUS_SERVICE_UNAVAILABLE,
|
|
|
|
B_HTTP_STATUS_GATEWAY_TIMEOUT,
|
|
|
|
B_HTTP_STATUS__SERVER_ERROR_END
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// HTTP default User-Agent
|
|
|
|
#define B_HTTP_PROTOCOL_USER_AGENT_FORMAT "ServicesKit (%s)"
|
|
|
|
|
|
|
|
#endif // _B_URL_PROTOCOL_HTTP_H_
|