hrev46249 adds 1 changeset to branch 'master' old head: 106b257eddd46771eb79f5409ccf509aac2f80d5 new head: 25b034e99c56c4b297dcb23ea58262fd81660bac overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=25b034e+%5E106b257 ---------------------------------------------------------------------------- 25b034e: HttpRequest: docs and memory management fixes * Now takes ownership of headers, form data and input data * Split Set* and Adopt* methods to help with proper use of this (Set does a copy) * Write documentation. [ Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> ] ---------------------------------------------------------------------------- Revision: hrev46249 Commit: 25b034e99c56c4b297dcb23ea58262fd81660bac URL: http://cgit.haiku-os.org/haiku/commit/?id=25b034e Author: Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> Date: Thu Oct 17 12:10:23 2013 UTC ---------------------------------------------------------------------------- 3 files changed, 206 insertions(+), 12 deletions(-) docs/user/net/HttpRequest.dox | 164 +++++++++++++++++++++++++ headers/os/net/HttpRequest.h | 11 +- src/kits/network/libnetapi/HttpRequest.cpp | 43 +++++-- ---------------------------------------------------------------------------- diff --git a/docs/user/net/HttpRequest.dox b/docs/user/net/HttpRequest.dox new file mode 100644 index 0000000..9a546fa --- /dev/null +++ b/docs/user/net/HttpRequest.dox @@ -0,0 +1,164 @@ +/* + * Copyright 2013 Haiku, Inc. All rights reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Adrien Destugues, pulkomandy@xxxxxxxxxxxxx + * + * Corresponds to: + * headers/os/net/HttpRequest.h rev 46234 + * src/kits/network/libnetapi/HttpRequest.cpp rev 46242 + */ + +/*! + \file HttpRequest.h + \ingroup network + \brief Management of HTTP or HTTPS protocol requests +*/ + +/*! + \class BHttpRequest + \ingroup network + \brief Handles a request over HTTP or HTTPS + + Instances of ths class wil lbe created by the \ref BUrlProtocolRoster for + \ref BUrl with the "http" or "https" protocol. The HTTP protocol is + implemented as specified in RFC2616. The request headers and body can be + customized, then sent to the server. The reply is then parsed and made + available to the application. + + This class only implements the client-side part of HTTP, it can't be used + to build an HTTP server. +*/ + +/*! + \fn void BHttpRequest::SetMethod(const char* method) + \brief Set the HTTP method. + + You can use either one of the standard methods (B_HTTP_GET is the default) + or a custom one. The standard methods are B_HTTP_GET, B_HTTP_POST, + B_HTTP_PUT, B_HTTP_DELETE, B_HTTP_HEAD, B_HTTP_OPTIONS, B_HTTP_TRACE and + B_HTTP_CONNECT. +*/ + +/*! + \fn void BHttpRequest::SetFollowLocation(bool follow) + \brief Enable or disable following HTTP redirects + + An HTTP server can redirect a request to another address, either on the + same host or elseswhere. When FollowLocation is set (the default), these + redirections will be followed until an actual page (or an error) is found. + When it is unset, the redirection will not be followed and will be reported + to the client. +*/ + +/*! + \fn void BHttpRequest::SetMaxRedirections(int8 maxRedirections) + \brief Set the maximal number of redirections to follow before giving up + + This is only useful when \ref SetFollowLocation is enabled. It will abort + the request after the given number of redirections. This avoids and helps + diagnosing redirection cycles, where two addresses redirect to each other. + + The default is to follow at most 8 redirections before giving up. +*/ + +/*! + \fn void BHttpRequest::SetReferred(const BString& referrer) + \brief Set the referrer + + The referrer is a string sent to the server in the "Referrer:" HTTP header + field. It helps the server know where the request comes from. When + following a link in an HTML page, this is usually set to the URL of that + page. +*/ + +/*! + \fn void BHttpRequest::SetUserAgent(const BString& userAgent) + \brief Set the user agent + + The user agent is an identifier for the client sending an HTTP request. + Some servers will use this to send different content depending on the + software asking for a page. + + The default user agent is "Services Kit (Haiku)". +*/ + +/*! + \fn void BHttpRequest::SetHeaders(const BHttpHeaders& headers) + \brief Set the HTTP headers + + This method replaces the whole set of headers for this request with a copy + of the given ones. + + \param headers the header template to copy from. +*/ + +/*! + \fn void BHttpRequest::AdoptHeaders(BHttpHeaders* const headers) + \brief Set the HTTP headers + + This method replaces the whole set of headers for this request. It takes + ownership of the parameter, which must not be used afterwards. +*/ + +/* + \fn void BHttpRequest::SetDiscardDate(bool discard) + + This is currently unused. +*/ + +/* + \fn void BHttpRequest::DisableListener(bool disable) + This is currently unused. +*/ + +/*! + \fn void BHttpRequest::SetAutoReferrer(bool enable) + \brief Automatically set the referrer when the request is done. + + This allows HttpRequest to manage the referrer automatically. Each request + will set the referrer to its own URL so the next request automatically + uses that one. +*/ + +/*! + \fn void BHttpRequest::SetPostFields(const BHttpForm& fields) + \brief Set the fields for form POST data + + Replaces the content of the request with a copy of the given POST fields. +*/ + +/*! + \fn void BHttpRequest::AdoptPostFields(BHttpForm* const fields) + \brief Set the fields for form POST data + + Replaces the content of the request with the given POST fields. + + This method takes ownership of the given form, which must not be used + elsewhere afterwards. +*/ + +/*! + \fn void BHttpRequest::AdoptInputData(BDataIO* const data, const ssize_t size = -1) + \brief Set the request body + + If the size is -1 (the default), the data will be sent using chunked + transfers. If the size is known, it will be sent using the Content-Length + header and non-chunked mode. + + You should set the size whenever possible, as some servers will not handle + chunked mode properly in all cases. + + This method takes ownership of the data, which must not be used elsewhere. +*/ + +/*! + \fn void BHttpRequest::SetUserName(const BString& userName) + \brief Set the user name for HTTP authentication. +*/ + +/*! + \fn void BHttpRequest::SetPassword(const BString& password) + \brief Set the user password for HTTP authentication. +*/ diff --git a/headers/os/net/HttpRequest.h b/headers/os/net/HttpRequest.h index c88c4de..2ed4a1e 100644 --- a/headers/os/net/HttpRequest.h +++ b/headers/os/net/HttpRequest.h @@ -34,15 +34,20 @@ public: void SetMaxRedirections(int8 maxRedirections); void SetReferrer(const BString& referrer); void SetUserAgent(const BString& agent); - void SetHeaders(BHttpHeaders* headers); void SetDiscardData(bool discard); void SetDisableListener(bool disable); void SetAutoReferrer(bool enable); - void SetPostFields(BHttpForm* fields); - void SetInputData(BDataIO* data, ssize_t size = -1); void SetUserName(const BString& name); void SetPassword(const BString& password); + void SetPostFields(const BHttpForm& fields); + void SetHeaders(const BHttpHeaders& headers); + + void AdoptPostFields(BHttpForm* const fields); + void AdoptInputData(BDataIO* const data, + const ssize_t size = -1); + void AdoptHeaders(BHttpHeaders* const headers); + static bool IsInformationalStatusCode(int16 code); static bool IsSuccessStatusCode(int16 code); static bool IsRedirectionStatusCode(int16 code); diff --git a/src/kits/network/libnetapi/HttpRequest.cpp b/src/kits/network/libnetapi/HttpRequest.cpp index 325ceaf..d1ad274 100644 --- a/src/kits/network/libnetapi/HttpRequest.cpp +++ b/src/kits/network/libnetapi/HttpRequest.cpp @@ -39,6 +39,7 @@ BHttpRequest::BHttpRequest(const BUrl& url, BUrlResult& result, bool ssl, fHttpVersion(B_HTTP_11), fRequestStatus(kRequestInitialState), fOptHeaders(NULL), + fOptPostFields(NULL), fOptInputData(NULL), fOptInputDataSize(-1), fOptFollowLocation(true) @@ -54,6 +55,10 @@ BHttpRequest::BHttpRequest(const BUrl& url, BUrlResult& result, bool ssl, BHttpRequest::~BHttpRequest() { delete fSocket; + + delete fOptInputData; + delete fOptHeaders; + delete fOptPostFields; } @@ -93,13 +98,6 @@ BHttpRequest::SetUserAgent(const BString& agent) void -BHttpRequest::SetHeaders(BHttpHeaders* headers) -{ - fOptHeaders = headers; -} - - -void BHttpRequest::SetDiscardData(bool discard) { fOptDiscardData = discard; @@ -121,8 +119,31 @@ BHttpRequest::SetAutoReferrer(bool enable) void -BHttpRequest::SetPostFields(BHttpForm* fields) +BHttpRequest::SetHeaders(const BHttpHeaders& headers) +{ + AdoptHeaders(new BHttpHeaders(headers)); +} + + +void +BHttpRequest::AdoptHeaders(BHttpHeaders* const headers) { + delete fOptHeaders; + fOptHeaders = headers; +} + + +void +BHttpRequest::SetPostFields(const BHttpForm& fields) +{ + AdoptPostFields(new BHttpForm(fields)); +} + + +void +BHttpRequest::AdoptPostFields(BHttpForm* const fields) +{ + delete fOptPostFields; fOptPostFields = fields; if (fOptPostFields != NULL) @@ -131,8 +152,9 @@ BHttpRequest::SetPostFields(BHttpForm* fields) void -BHttpRequest::SetInputData(BDataIO* data, ssize_t size) +BHttpRequest::AdoptInputData(BDataIO* const data, const ssize_t size) { + delete fOptInputData; fOptInputData = data; fOptInputDataSize = size; } @@ -226,6 +248,9 @@ BHttpRequest::StatusString(status_t threadStatus) const void BHttpRequest::_ResetOptions() { + delete fOptPostFields; + delete fOptHeaders; + fOptFollowLocation = true; fOptMaxRedirs = 8; fOptReferer = "";